Sub topic 1
What pg_stat_statements Tells You and What It Does Not
Prerequisites
You should know basic SQL, including SELECT, ORDER BY, LIMIT, and aggregate expressions. You need access to a PostgreSQL instance where you can change configuration, or a colleague who can. You should also be comfortable using psql or another SQL client, and have a general idea of what a query plan is.
What pg_stat_statements Tells You and What It Does Not
pgstatstatements is a PostgreSQL extension that keeps cumulative execution statistics for each normalized query. It records how often each statement ran, the total and average time it took, and how many rows it returned.
It does not record each individual execution, so it cannot show when one specific run was slow. It also does not show query plans. Use it to find which statements deserve attention, then use EXPLAIN to learn why they are slow.
Enabling pg_stat_statements
The extension must load when the server starts. Add this line to postgresql.conf, then restart the server:
# postgresql.conf (changing this setting requires a server restart)
shared_preload_libraries = 'pg_stat_statements'After the restart, create the extension in the database you want to query:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;How Queries Are Grouped: Normalization and queryid
PostgreSQL normalizes each statement by replacing constant values with placeholders. The extension then groups all executions that share the same normalized text.
| Statements executed | Normalized form | Rows in the view |
|---|---|---|
| SELECT * FROM orders WHERE customer_id = 42 | SELECT * FROM orders WHERE customer_id = $1 | 1 |
| SELECT * FROM orders WHERE customer_id = 97 | SELECT * FROM orders WHERE customer_id = $1 | (same row) |
Each normalized statement gets a queryid, a hash that identifies the group. The same statement usually gets the same queryid on the same PostgreSQL version, but the value can differ across major versions.
> Hmm, avoid block quote.
Reading the Core Columns
The view has many columns. These are the ones you will use most often:
| Column | Meaning |
|---|---|
| queryid | Hash identifying the normalized statement |
| query | Normalized statement text |
| calls | Number of executions |
| total_exec_time | Total execution time in milliseconds (PostgreSQL 13 and later) |
| mean_exec_time | Average execution time per call in milliseconds |
| rows | Total rows returned or affected |
The following query lists the ten statements that consumed the most total time:
SELECT queryid,
calls,
round(total_exec_time::numeric, 2) AS total_ms,
round(mean_exec_time::numeric, 2) AS mean_ms,
rows,
left(query, 80) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;Exercise
Exercise
Two runs of SELECT name FROM users WHERE id = 1 and SELECT name FROM users WHERE id = 2 happen once each. How many rows does pgstatstatements show for them, and why?
Show answer
One row. PostgreSQL normalizes both constants to $1, so both executions share one normalized text and one queryid, and the extension adds their counts to that row (calls = 2).