Database Administration — Fri Apr 17

← Home | ← database-admin

PostgreSQL - DBA Toolkit - Query Performance with pg_stat_statements

Fri Apr 17 — Use pg_stat_statements to find the queries costing you the most — by total time, average time, and I/O — across your entire workload.
#postgresql #dba #monitoring #performance #queries #pg_stat_statements #tool-kit

The Best Query Tuning Tool in PostgreSQL

pg_stat_statements is an extension that tracks execution statistics for every distinct SQL statement run against the database.

Not just the queries running right now. Every query — normalised, aggregated, and ranked by total impact. It answers the question that pg_stat_activity cannot: across everything this database has run, which queries are causing the most work?

This is the starting point for any serious query tuning effort. Without it you are guessing. With it you have a ranked list of exactly where to focus.

QueryPerformancepgstatstatements.sql
    Loading…
  

Enabling pg_stat_statements

If you get an error that the view does not exist, the extension needs to be loaded:

Enabling_pg_stat_statements.sql
    Loading…
  

Reading the Results

Top Queries by Total Time

ColumnWhat It Tells You
queryNormalised SQL (literals replaced with $1, $2, etc.)
callsTotal times this statement was executed
total_exec_timeCumulative execution time in milliseconds
mean_exec_timeAverage execution time per call
min_exec_time / max_exec_timeBest and worst case — high variance suggests plan instability
stddev_exec_timeStandard deviation — high value means inconsistent performance
rowsTotal rows returned or affected

I/O and Cache Stats

ColumnWhat It Tells You
shared_blks_hitBlocks served from shared buffer cache
shared_blks_readBlocks read from disk
blk_read_timeTotal time spent reading blocks from disk
blk_write_timeTotal time spent writing blocks
temp_blks_read / temp_blks_writtenTemp file usage — sign of insufficient work_mem for this query

What to Watch For

SignalWhat It Means
High total_exec_time with low callsA rare but extremely slow query — probably a bad plan or missing index
High total_exec_time with high callsA fast query run constantly — optimise it and the savings multiply
mean_exec_time high relative to min_exec_timePlan instability — query sometimes gets a good plan, sometimes a bad one
High shared_blks_read vs shared_blks_hitLow cache hit rate for this query — it is reading a lot from disk
temp_blks_written > 0Query is spilling to disk — increase work_mem or add an index
High stddev_exec_timeExecution time varies wildly — look for lock waits or parameter sniffing

Resetting Statistics

ResettingStatistics.sql
    Loading…
  

Gareth Winterman