Sub topic 1
What pg_stat_statements Tells You and What It Does Not
Prerequisites
This chapter assumes that you know basic SQL, including SELECT, ORDER BY, and LIMIT. You should also know how to connect to PostgreSQL with psql or another SQL client. You do not need to have enabled pg_stat_statements yet.
What it records
pg_stat_statements is a PostgreSQL extension that collects cumulative statistics about SQL statements. It keeps one row for each tracked query shape and updates that row whenever PostgreSQL executes a matching statement.
The statistics can show:
- How many times a query ran (
calls) - How much execution time all calls consumed (
total_exec_time) - The average execution time per call (
mean_exec_time) - How many rows the query returned or affected (
rows) - How many data blocks PostgreSQL found in shared buffers (
shared_blks_hit) - How many blocks PostgreSQL had to read elsewhere (
shared_blks_read) - How many temporary blocks it read or wrote
These values help you decide which queries deserve investigation. For example, a query that runs 100,000 times for 2 milliseconds can consume more database time than a query that runs once for 10 seconds.
What one row represents
The extension groups similar statements instead of storing a separate history record for every execution. Literal values are usually replaced with parameters when PostgreSQL creates the normalized query text.
For example, these statements normally belong to the same group:
SELECT * FROM orders WHERE customer_id = 42;
SELECT * FROM orders WHERE customer_id = 87;The recorded query may look similar to this:
SELECT * FROM orders WHERE customer_id = $1;The queryid identifies the grouped query. It is useful for ranking and comparing observations, but it is not a permanent identity for application code. Query identifiers can change after PostgreSQL upgrades, changes in parsing or planning behavior, or other environment changes.
Normalization makes aggregate questions possible. You can ask, “Which query pattern consumed the most time?” rather than examining every distinct parameter value.
What it does not record
pg_stat_statements is not a complete query history. It does not normally tell you:
- The duration of every individual execution
- Which parameter values were used for each call
- The exact plan selected for every execution
- A timeline showing when each call occurred
- The wait event or lock that delayed a particular call
- The result rows returned by a query
- Unlimited query text
Its time columns are accumulated counters or summaries. A high mean_exec_time tells you that executions were slow on average, but it does not show whether every call was slow or whether a few extreme calls raised the average. Later, you can use stddev_exec_time, min_exec_time, and max_exec_time to look for variation.
The query text is also limited by PostgreSQL configuration. Very long statements can be truncated, so the displayed text might not include the part needed to distinguish two statements. Entries can also be evicted when the configured statement-entry limit is reached.
Use these statistics to find candidates, not to prove the cause of a slowdown. After selecting a candidate, run it with representative parameters under EXPLAIN (ANALYZE, BUFFERS) and inspect the actual plan.
> callout note > Statistics are cumulative since collection began or since the last reset. Always record the measurement period when comparing results.
A useful mental model
Think of pg_stat_statements as a dashboard of grouped counters. It answers, “Which query patterns have used the most resources during this measurement period?” It does not answer, “What happened during one specific execution?”
That distinction determines the next step:
- Rank query groups by total time, average time, calls, or I/O.
- Choose a query that matches the performance problem.
- Reproduce it with realistic parameters.
- Use
EXPLAIN (ANALYZE, BUFFERS)for plan-level evidence.
Exercise
Why can a query with a low mean execution time still be an important performance problem?
Show answer
It may run so many times that its total execution time is larger than the total time of slower queries that run rarely.