Sub topic 3
Resetting statistics and measuring a time window
This chapter assumes you have pgstatstatements enabled, can run SQL against the server, and know the core columns from the previous chapter. Resetting needs superuser rights or a role granted execute on the reset function.
Counters in pgstatstatements only grow until you reset them. A ranking taken today therefore mixes last month's workload with this week's. To measure a specific period, reset first, run the workload, then read the results.
SELECT pg_stat_statements_reset();
-- run the workload you want to measure, then:
SELECT queryid, calls, round(total_exec_time::numeric, 2) AS total_ms
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;Queries that spill to temporary files
When a sort or hash does not fit in work_mem, PostgreSQL writes temporary blocks to disk. The temp_blks_written column counts these writes. A non-zero value often means the query needs more memory or a different plan.
Unstable queries: stddev, min, and max
The columns stddev_exec_time, min_exec_time, and max_exec_time show how much a query's run time varies. A high standard deviation with a low minimum suggests that some runs are fast and others are slow, which often points to cache effects or lock waits.
Tuning pg_stat_statements settings
These settings control the extension, and most require a restart:
pg_stat_statements.maxsets how many statements are tracked (default 5000).pg_stat_statements.trackchoosestop,all, ornone. The defaulttoptracks only top-level statements.pg_stat_statements.track_planningadds planning-time columns, and it is off by default because it adds overhead.
Permissions and query text visibility
Users without the pg_read_all_stats role see full query text only for their own statements. Other queries appear as <insufficient privilege>.
Limits, eviction, and overhead
Query text is truncated to track_activity_query_size (default 1024 bytes). When the tracked count exceeds pg_stat_statements.max, the least-used entries are discarded, so a rare query can disappear from the list. The extension keeps only totals and averages, not a history of each execution. Its overhead is small but not zero, and it grows with the number of distinct statements.
Common errors:
- Forgetting to reset, so old data distorts the ranking.
- Reading a ranking without noting when the last reset happened.
- Assuming a missing query was never run, when eviction may have removed it.
SELECT queryid, calls,
round(stddev_exec_time::numeric, 2) AS stddev_ms,
round(min_exec_time::numeric, 2) AS min_ms,
round(max_exec_time::numeric, 2) AS max_ms,
temp_blks_written
FROM pg_stat_statements
WHERE calls >= 20
ORDER BY stddev_exec_time DESC
LIMIT 10;Exercise
Exercise
You reset statistics at 09:00 and want to measure a 30-minute batch job. Which two statements do you run, and why must you reset first?
Show answer
Run SELECT pgstatstatementsreset(); at 09:00, let the batch job run, then query pgstat_statements to read only that period's totals. Without the reset, counters from earlier work would be mixed into the results.