Skip to content

Instantly share code, notes, and snippets.

@rcanepa
Last active May 2, 2024 07:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rcanepa/535163dc249539912c25 to your computer and use it in GitHub Desktop.
Save rcanepa/535163dc249539912c25 to your computer and use it in GitHub Desktop.
Activate pg_stat_statements on PostgreSQL
1) Edit file postgresql.conf and add the next 3 lines (any where):
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.max = 10000
pg_stat_statements.track = all
2) Restart PostgreSQL
3) Execute the next command on psql, pgAdmin or similar:
CREATE EXTENSION pg_stat_statements;
4) Checking some results:
SELECT query, calls, total_time, rows, 100.0 * shared_blks_hit / nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5;
SELECT
(total_time / 1000 / 60) as total_minutes,
(total_time/calls) as average_time,
query
FROM pg_stat_statements
ORDER BY 1 DESC
LIMIT 100;
5) More information:
http://www.postgresql.org/docs/9.4/static/pgstatstatements.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment