Sub topic 3
How Queries Are Grouped: Normalization and queryid
This chapter assumes you know basic SQL, can use SELECT, ORDER BY, and LIMIT, and understand that PostgreSQL can produce a query plan for a statement.
pg_stat_statements stores cumulative statistics by query shape rather than by every individual execution. This lets you see that one kind of query ran 50,000 times, even when each execution used a different value.
Normalization replaces changing values
Consider these statements:
SELECT * FROM orders WHERE customer_id = 42;
SELECT * FROM orders WHERE customer_id = 87;
SELECT * FROM orders WHERE customer_id = 105;They have different text, but the same structure. Each statement selects from orders and filters customer_id with one integer value. pg_stat_statements normalizes the constants, conceptually treating them like parameters:
SELECT * FROM orders WHERE customer_id = $1;The executions are then accumulated in one statistics row. The row's calls value represents all executions in that group, and its execution-time columns include the time from all of them.
Normalization applies to constants such as numbers and quoted strings. It does not make structurally different statements equivalent. These queries usually belong to different groups:
SELECT * FROM orders WHERE customer_id = $1;
SELECT * FROM orders WHERE customer_id = $1 AND status = $2;The second query has an additional predicate, so PostgreSQL treats it as a different query shape.
`queryid` identifies the query shape
The queryid column is a numeric identifier derived from the parsed query structure. It is the key you can use to connect repeated executions to one statistics row.
The identifier is based on the query's internal representation, not only on its visible characters. Differences in whitespace and capitalization generally do not create separate query shapes. A different table, predicate, join, or expression usually does.
| Query characteristic | Usually grouped together? | Reason |
|---|---|---|
| Different literal values | Yes | Constants are normalized |
| Different whitespace | Yes | Formatting is not the query structure |
| An added predicate | No | The parsed structure changes |
| Different table names | No | The referenced objects change |
The query column contains representative, normalized query text. Use queryid when grouping or joining your own reports. Do not treat the text as a permanent identifier. Text can be truncated, and the exact normalized representation can vary between PostgreSQL versions.
Inspecting grouped executions
This query shows how often each normalized query ran:
SELECT queryid,
calls,
rows,
round(mean_exec_time::numeric, 2) AS mean_ms,
left(query, 120) AS query
FROM pg_stat_statements
ORDER BY calls DESC
LIMIT 20;A high calls value identifies a frequently executed query shape. It does not prove that the query is slow. A statement averaging 2 milliseconds can consume more total database time than a statement averaging 2 seconds if it runs often enough.
Parameter values are not preserved as a per-execution history. After seeing a query group, you may need application logs, tracing, or a representative parameter value to reproduce a particular case. This matters when different values produce different plans or runtimes.
Common grouping mistakes
- Assuming every distinct SQL string gets its own row.
- Assuming similar-looking statements always share a
queryid. - Comparing
queryidvalues across PostgreSQL versions without checking compatibility. - Expecting
pg_stat_statementsto show which parameter value caused a slow execution. - Reading a high call count as proof of poor per-call performance.
Exercise
Two statements differ only because one uses customerid = 10 and the other uses customerid = 20. Will pgstatstatements normally accumulate them in the same row, and what should you inspect to confirm the grouping?
Show answer
Yes. Constants are normally normalized, so both executions should contribute to the same query shape. Inspect queryid, calls, and the normalized query column.