Skip to content

Instantly share code, notes, and snippets.

@Weiyuan-Lane
Created May 16, 2021 13:00
Show Gist options
  • Save Weiyuan-Lane/314aa7d52a1f218198c1a401c45673fb to your computer and use it in GitHub Desktop.
Save Weiyuan-Lane/314aa7d52a1f218198c1a401c45673fb to your computer and use it in GitHub Desktop.
pg_stat_statement mean query
SELECT
pg_stat_statements.query as query,
round(pg_stat_statements.total_exec_time::numeric, 2) AS total_time,
pg_stat_statements.calls,
round(pg_stat_statements.mean_exec_time::numeric, 2) AS mean,
round((100 * pg_stat_statements.total_exec_time /
sum(pg_stat_statements.total_exec_time::numeric) OVER ())::numeric, 2) AS percentage_cpu,
round((100 * pg_stat_statements.calls /
sum(pg_stat_statements.calls::numeric) OVER ())::numeric, 2) AS percentage_calls,
pg_user.usename as username,
pg_database.datname as database
FROM
pg_stat_statements
JOIN
pg_database ON pg_database.oid = pg_stat_statements.dbid
JOIN
pg_user ON pg_stat_statements.userid = pg_user.usesysid
ORDER BY mean DESC LIMIT 20;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment