Sub topic 11
Unstable queries: stddev, min, and max
This chapter assumes you know basic SQL, can query pg_stat_statements, and understand that a query plan describes how PostgreSQL executes a statement. You will use execution-time statistics to find queries whose performance changes substantially from one call to another.
Why averages are not enough
mean_exec_time is the average execution time for all recorded calls of a normalized query. An average can hide important variation. For example, a query might usually finish in 10 milliseconds but occasionally take 2 seconds because it waits for disk I/O, receives an unusual parameter, or competes with another workload.
The statistics view provides three useful measurements:
min_exec_time: the fastest recorded execution, in milliseconds.max_exec_time: the slowest recorded execution, in milliseconds.stddev_exec_time: the standard deviation of execution time, in milliseconds. A larger value means the calls vary more around the mean.calls: the number of executions included in the measurements.
These values are cumulative. They describe the calls collected since the statistics were reset or since the relevant entry was created.
Find queries with variable execution time
Start with queries that have enough calls to make the comparison useful. A query with only two calls can have a large standard deviation by chance.
SELECT queryid,
calls,
round(mean_exec_time::numeric, 2) AS mean_ms,
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,
left(query, 100) AS query
FROM pg_stat_statements
WHERE calls >= 50
ORDER BY stddev_exec_time DESC
LIMIT 10;Compare the columns as a group:
| Pattern | What it may indicate |
|---|---|
| Low mean, high max | Occasional slow calls or outliers |
| High mean, low standard deviation | Consistently slow work |
| Large difference between min and max | Parameter, cache, lock, or I/O sensitivity |
| High standard deviation with few calls | Too little evidence for a confident conclusion |
For example, suppose a query has a mean of 35 ms, a minimum of 8 ms, and a maximum of 1,200 ms. The maximum is more than 34 times the mean. That query may be acceptable most of the time but still cause user-visible latency when an outlier occurs.
The standard deviation helps rank variation, but it does not show when the slow executions happened. pg_stat_statements stores cumulative summaries rather than a row for every execution.
A practical investigation path
Use the summary to choose a candidate, then investigate the likely source of variation.
Run EXPLAIN (ANALYZE, BUFFERS) with real parameter values, but test carefully. ANALYZE executes the statement. For a write statement, use a transaction and roll it back when that is safe.
Also compare min_exec_time and max_exec_time after a known workload. If the spread disappears after warming the cache, the first executions may have read more data from storage. If the spread remains, different parameter values, locks, or changing plans may be involved.
Measuring a fresh interval
Cumulative values can mix old and new behavior. Reset the statistics before a controlled test window, then run the representative workload.
SELECT pg_stat_statements_reset();
-- Run the workload for the interval you want to study.
-- Then repeat the variability query.Resetting affects all collected statement statistics, so coordinate this action with other users of the view.
Exercise
A query has 200 calls, a mean time of 12 ms, a minimum of 9 ms, a maximum of 900 ms, and a high standard deviation. Is it consistently slow or unstable, and what should you do next?
Show answer
It is unstable rather than consistently slow. Compare the parameters and workload for slow calls, then use EXPLAIN (ANALYZE, BUFFERS) with representative values and check for I/O or blocking.