Skip to content

Instantly share code, notes, and snippets.

@lfittl
Created October 19, 2015 22:17
Show Gist options
  • Save lfittl/9ee78ac200e4e7ebe33d to your computer and use it in GitHub Desktop.
Save lfittl/9ee78ac200e4e7ebe33d to your computer and use it in GitHub Desktop.
CREATE SCHEMA monitoring;
CREATE OR REPLACE FUNCTION monitoring.get_stat_statements() RETURNS SETOF pg_stat_statements AS
$$
SELECT * FROM public.pg_stat_statements
WHERE dbid IN (SELECT oid FROM pg_database WHERE datname = current_database());
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
CREATE USER monitoring PASSWORD 'mypassword';
REVOKE ALL ON SCHEMA public FROM monitoring;
GRANT USAGE ON SCHEMA monitoring TO monitoring;
@lfittl
Copy link
Author

lfittl commented Oct 19, 2015

After creating this with a super user, use

SELECT * FROM monitoring.get_stat_statements();

for querying as the monitoring user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment