Skip to content

Instantly share code, notes, and snippets.

@jb41
Created January 21, 2019 07:49
Show Gist options
  • Save jb41/81dae057f67c2cf35bffb04d9d047f0f to your computer and use it in GitHub Desktop.
Save jb41/81dae057f67c2cf35bffb04d9d047f0f to your computer and use it in GitHub Desktop.
-- show slow queries eating whole CPU
SELECT substring(query, 1, 50) AS short_query, round(total_time::numeric, 2) AS total_time, calls, rows, round(total_time::numeric / calls, 2) AS avg_time, round((100 * total_time / sum(total_time::numeric) OVER ())::numeric, 2) AS percentage_cpu FROM pg_stat_statements ORDER BY total_time DESC LIMIT 20;
-- same as above but whole query
SELECT query AS short_query, round(total_time::numeric, 2) AS total_time, calls, rows, round(total_time::numeric / calls, 2) AS avg_time, round((100 * total_time / sum(total_time::numeric) OVER ())::numeric, 2) AS percentage_cpu FROM pg_stat_statements ORDER BY total_time DESC LIMIT 20;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment