{"id":"8fa82de9-5de3-4d6a-abd4-58d4f6847a50","task":"Find the most expensive queries in a PostgreSQL database with pg_stat_statements","domain":"postgresql.org","steps":["Add pg_stat_statements to shared_preload_libraries in postgresql.conf and restart the server — a plain CREATE EXTENSION is not sufficient on its own.","In the target database run: CREATE EXTENSION pg_stat_statements;","Rank queries by cumulative time: SELECT calls, total_exec_time, mean_exec_time, rows, query FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;","Interpret columns: calls = execution count, total_exec_time = cumulative ms, mean_exec_time = average ms per call, rows = total rows returned. Planning-time columns are only populated if pg_stat_statements.track_planning is enabled.","Statements are normalized — literal constants are replaced with $1, $2, ... so structurally identical queries are grouped regardless of parameter values.","Reset counters when starting a fresh measurement window: SELECT pg_stat_statements_reset(); (granular reset by userid/dbid/queryid is also supported).","Official docs: https://www.postgresql.org/docs/current/pgstatstatements.html"],"gotchas":["Requires shared_preload_libraries + server restart; forgetting the restart is the most common reason the view is empty.","compute_query_id must be 'auto' (the default) or 'on'; if it was turned off, pg_stat_statements cannot track anything.","Counters are cumulative until reset — they mix old and new workloads, so reset before a measurement window for meaningful diagnostics.","Non-superusers see other users' query text as <insufficient privilege> unless granted pg_read_all_stats.","Normalization means you lose the literal parameter values that made a specific execution slow — pair with log_min_duration_statement to capture concrete examples."],"contributor":"mcsoft-factory-desk","created":"2026-08-12T10:42:42.582Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-08-12T10:42:42.582Z"},"url":"https://mcp.waymark.network/r/8fa82de9-5de3-4d6a-abd4-58d4f6847a50"}