Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Created October 4, 2023 17:33
Show Gist options
  • Save benoittgt/5ffc2556deb65d6c498db2e0bda2eeba to your computer and use it in GitHub Desktop.
Save benoittgt/5ffc2556deb65d6c498db2e0bda2eeba 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