Skip to content

Instantly share code, notes, and snippets.

@MaherSaif
Forked from benoittgt/pg_stat_statements.sql
Created March 28, 2025 02:40
Show Gist options
  • Save MaherSaif/d65c2224b92c2e1925883d732f0575fa to your computer and use it in GitHub Desktop.
Save MaherSaif/d65c2224b92c2e1925883d732f0575fa to your computer and use it in GitHub Desktop.
Quickly look at pg_stat_statements
-- based on https://www.crunchydata.com/blog/understanding-postgres-iops
SELECT
interval '1 millisecond' * total_exec_time AS "Total Exec. Time",
to_char (
(total_exec_time / sum(total_exec_time) OVER ()) * 100,
'FM90D0'
) || '%' AS "Proportional Exec. Time",
to_char (calls, 'FM999G999G999G990') AS "Calls",
interval '1 millisecond' * (blk_read_time + blk_write_time) AS "Time Spent on IO",
CASE
WHEN length (query) <= 41 THEN query
ELSE regexp_replace (query, '^(.{20}).*(.{20})$', '\1 {...} \2')
END AS "Query",
queryid AS "Query ID"
FROM
pg_stat_statements
ORDER BY
4 DESC
LIMIT
10;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment