Sub topic 14
Limits, eviction, and overhead
Assume you know basic SQL and have used pg_stat_statements to rank queries. This chapter focuses on what the extension cannot tell you and how its settings affect the results.
The statistics are summaries, not a history
pg_stat_statements keeps one cumulative row for each tracked, normalized query. That row combines executions over time. It does not store a separate record for every execution.
As a result, the view cannot answer questions such as:
- Which user ran this query at 14:03?
- What was the duration of every individual execution?
- Which parameter value caused the slow run?
- What did the query plan look like during one past execution?
Columns such as min_exec_time, max_exec_time, and stddev_exec_time summarize variation, but they do not identify the executions behind those values. Use logs, tracing, or a monitoring system when you need per-execution history.
Query text can be truncated
The query column has a configured maximum length. PostgreSQL stores only the beginning of a long statement when the statement exceeds that limit. The setting is controlled by track_activity_query_size.
A truncated query can still be useful for grouping and ranking because queryid identifies the normalized statement. However, the displayed text may omit the table, predicate, or comment that you need for investigation. Check the application code or database logs when the stored text is incomplete.
The query text can also contain sensitive values or identifiers. Limit access to the view and avoid exporting it casually.
Entries can be evicted
The extension stores tracked statements in a shared pool whose size is controlled by pg_stat_statements.max. If more distinct statements appear than the pool can hold, PostgreSQL removes older or less-used entries to make room.
Eviction has two consequences:
- A query may disappear from the view even though it still runs.
- Its accumulated counters are lost when its entry is removed.
The setting is read at server start, so changing it normally requires a restart. Increasing it preserves more query shapes but consumes more shared memory. It does not create a historical archive.
Measuring overhead and choosing settings
Tracking every statement adds work. PostgreSQL must identify the normalized query, update counters, and maintain shared state. The cost is usually acceptable, but high-throughput systems should measure it rather than assume it is free.
| Setting | Controls | Main trade-off |
|---|---|---|
| `pg_stat_statements.max` | Number of tracked statement entries | More memory versus fewer evictions |
| `track_activity_query_size` | Maximum stored query-text length | More memory versus more complete text |
| `track_io_timing` | Timing for block I/O operations | Better I/O detail versus additional timing overhead |
track_io_timing is useful when interpreting I/O timing columns, but it can add system-call overhead. Enable it when the extra detail supports a diagnostic goal, then compare system performance with and without it.
A practical decision rule
Treat pg_stat_statements as a screening tool. Use it to find candidates by total time, mean time, calls, buffer reads, or temporary blocks. Then use current query text, application context, logs, and EXPLAIN (ANALYZE, BUFFERS) to investigate.
Resetting the view starts a new measurement window, but it also discards the current counters. Record important results before resetting, and reset only when a defined measurement period makes the data easier to interpret.
Exercise
Why might a query disappear from pgstatstatements, and what can the view not tell you about an individual execution?
Show answer
It may be evicted when the number of distinct tracked statements exceeds pgstatstatements.max. The view cannot provide a complete per-execution history or explain which parameter value caused a past slow execution.