Skip to content

Instantly share code, notes, and snippets.

@Relrin
Last active June 20, 2018 08:40
Show Gist options
  • Save Relrin/53ac6929ad1558ebf8e5e2d2279cf208 to your computer and use it in GitHub Desktop.
Save Relrin/53ac6929ad1558ebf8e5e2d2279cf208 to your computer and use it in GitHub Desktop.
Profiling PostgreSQL queries
1) In psql tool find the path to configuration file:
SHOW config_file;
2) In config:
shared_preload_libraries = 'pg_stat_statements'
3) After it restart the node via using `service postgresql restart`
4) In psql:
CREATE EXTENSION pg_stat_statements;
Queries:
SELECT pid,datname,usename,client_addr,query_start,query FROM pg_stat_activity;
# limit of slow queries
SELECT substring(query, 1, 50) AS short_query,
round(total_time::numeric, 2) AS total_time,
calls,
round(mean_time::numeric, 2) AS mean,
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