Skip to content

Measuring query performance over time with pg_stat_statements

pg_stat_statements tells you how much time each statement has used since some point in the past. To see whether a query got slower, you need how much it used in the last few minutes. The subtraction is simple; knowing when it is wrong is the work.

Counters are cumulative

Columns such as calls, total_exec_time, rows and shared_blks_read grow from the moment an entry is created until it is reset or evicted. The activity in an interval is the current value minus the value at the previous sample, for the same entry. An entry is identified by userid, dbid, queryid and toplevel.

-- One sample. Read the server clock in the same session.
SELECT clock_timestamp() AS sampled_at;
SELECT userid, dbid, queryid, toplevel, calls, total_exec_time, rows
FROM pg_stat_statements;
SELECT dealloc, stats_reset FROM pg_stat_statements_info;

Use the server's clock for both ends of the interval, not the clock of the machine that polls. A missed sample only makes the interval longer; the difference is still exact.

Compute means from deltas

The mean execution time in an interval is Δtotal_exec_time / Δcalls. Subtracting two values of mean_exec_time does not give it: the cumulative mean moves slowly because it averages the whole history, so a query that doubled in the last five minutes can show almost no change. When Δcalls is zero the interval has no mean.

min_exec_time, max_exec_time and stddev_exec_time also cover the whole life of the entry and cannot be split into intervals. Percentiles cannot be derived from these aggregates at all.

Resets

pg_stat_statements_reset() sets counters back to zero, for everything or for selected entries. A naive delta across a reset is negative or, worse, small and plausible.

  • When pg_stat_statements_info.stats_reset changed between two samples, every entry started again at that time: its current value is the activity since stats_reset, not since the previous sample.
  • From PostgreSQL 17, each entry has stats_since. If it lies between the two samples, the current value is the activity since that time.
  • If any counter of a row decreased, drop the row for that interval. Clamping to zero or guessing a wrap hides the problem.

With these rules a computed value is never larger than the real activity, and it is exact when no reset happened in the interval. A reset that leaves no marker and is followed by enough new activity to pass the old value cannot be detected; it can only make a value too small.

Restarts and crashes

pg_postmaster_start_time() tells you the server restarted. Whether the counters survived depends on how it stopped. After a clean shutdown pg_stat_statements saves its statistics (with pg_stat_statements.save on, the default) and stats_reset is unchanged, so deltas can continue. After a crash the statistics are lost and stats_reset changes, so treat it as a reset. If you cannot read stats_reset, you cannot tell the two apart; drop the interval rather than guess.

Evictions

pg_stat_statements keeps at most pg_stat_statements.max entries (5,000 by default) and evicts the least used when it is full. pg_stat_statements_info.dealloc counts evictions. An evicted statement that runs again comes back as a new entry starting from zero.

On PostgreSQL 17 and later, stats_since shows whether an entry is new. Before 17, if dealloc grew during an interval, every delta in it may compare two different lives of an entry; mark them as unverified. A rising dealloc is also a reason to raise pg_stat_statements.max.

When two samples are not comparable

  • A different server (system identifier), a different major version or a dropped and recreated database: start over.
  • A different minor version: queryid is only documented as stable for the same server version, so start statement history over even though other statistics may continue.
  • An entry that appears when the previous sample was truncated or hidden from your role is a baseline, not new activity.

Columns that mislead

  • shared_blks_read counts blocks not found in shared buffers. The operating system may still have served them from its page cache; it is not a disk read count.
  • Timing columns such as shared_blk_read_time stay at zero unless track_io_timing is on. Zero there means not measured, not fast.
  • Without pg_read_all_stats (included in pg_monitor), other users' statements are hidden. Missing is not the same as idle.

How Querywake applies this

The Querywake collector applies these rules before anything is stored, and marks every batch with what happened in it: a reset, a restart, evictions, a truncated sample. Regression detection then compares each statement with its own baseline. A detection whose windows contain one of those events is marked degraded, and its confidence is capped at low.

Read the setup guide, see an incident in the read-only demo, or start free on one database.

PostgreSQL references: pg_stat_statements and the cumulative statistics system.