-- View: sonex.v_active_sessions

-- DROP VIEW sonex.v_active_sessions;

CREATE OR REPLACE VIEW sonex.v_active_sessions
WITH (
  security_invoker=true
) AS
 SELECT acc.first_name,
    acc.last_name,
    a.usename AS account_name,
    acc.department,
    a.state,
    a.application_name,
    a.datname AS database_name,
    a.client_addr::text AS client_addr,
    a.client_port,
    a.pid
   FROM pg_stat_activity a
     LEFT JOIN sonex.accounts acc ON acc.account_name::text = a.usename
  WHERE a.usename IS NOT NULL AND a.backend_type = 'client backend'::text
  ORDER BY acc.first_name, acc.last_name, a.usename;

ALTER TABLE sonex.v_active_sessions
    OWNER TO postgres;

GRANT SELECT ON TABLE sonex.v_active_sessions TO permission_sessions_view;
GRANT ALL ON TABLE sonex.v_active_sessions TO postgres;
GRANT INSERT, DELETE, SELECT, UPDATE ON TABLE sonex.v_active_sessions TO sonex;

