Sub topic 9
Resetting Statistics and Measuring a Time Window
Assume you know basic SQL and can run queries in psql or another PostgreSQL client. This chapter uses pg_stat_statements after it has been enabled and created in the current database.
Why a Time Window Matters
The counters in pg_stat_statements accumulate over time. A query may appear near the top because it has been running for weeks, even if it is no longer important. To study a particular deployment, release, or workload, measure a defined time window.
A typical measurement has four stages:
- Record the start of the measurement.
- Reset the statistics.
- Run a representative workload.
- Rank the collected statistics before recording the end time.
Resetting the Counters
Call pg_stat_statements_reset() to discard the accumulated statistics.
SELECT pg_stat_statements_reset();The default call resets all statistics visible to the extension. It affects other sessions that use the same PostgreSQL instance, so coordinate the reset with other database users. Do not reset production statistics during an investigation unless you have agreed on the measurement window.
You can record the boundaries manually:
SELECT clock_timestamp() AS window_start;
SELECT pg_stat_statements_reset();
-- Run the workload being measured here.
SELECT clock_timestamp() AS window_end;The reset itself does not make the results meaningful. The workload between the reset and the final query must represent the traffic you want to understand. For example, a short test with one request may not reveal the queries that dominate a busy application.
>callout note > Resetting removes the earlier cumulative data. Save any existing report before resetting if you may need to compare it later.
Saving a Snapshot
After the workload finishes, save the important columns to a file or monitoring table. A simple ranking can identify the statements that consumed the most execution time during the window.
SELECT queryid,
calls,
round(total_exec_time::numeric, 2) AS total_ms,
round(mean_exec_time::numeric, 2) AS mean_ms,
rows,
left(query, 120) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;total_exec_time is cumulative for all calls recorded since the reset. mean_exec_time is the average execution time per call during that same period. The report is therefore a summary of the window, not a history of individual executions.
A useful window should include normal concurrency, representative parameter values, and the traffic pattern you are investigating. If you are testing a release, warm-up traffic and steady-state traffic may need separate windows. Record the PostgreSQL version, application version, workload description, and reset and snapshot times with the report.
Comparing Multiple Windows
Do not compare raw totals from windows with different durations or traffic volumes. A two-hour window will usually contain more calls than a ten-minute window. Compare call counts, mean time, and, when appropriate, rates such as calls per minute.
You can keep a snapshot from each window and compare whether the same queryid remains expensive. A query that appears only once may be a one-off administrative operation. A query that consistently ranks high across representative windows is a stronger optimization candidate.
pg_stat_statements does not preserve the timeline of each execution. Once you reset the counters, the earlier values cannot be recovered from the extension. For detailed time-series behavior, export snapshots periodically or use a monitoring system.
Exercise
Exercise
Why should you save a report before calling pgstatstatements_reset()?
Show answer
The reset discards the accumulated counters, so saving the report preserves the earlier measurement for later comparison.