Sub topic 8
Taking a Suspect Query to EXPLAIN (ANALYZE, BUFFERS)
This chapter assumes you know basic SQL, can read SELECT statements, and understand that a query plan describes how PostgreSQL executes a query. You also need access to a PostgreSQL database where you can run diagnostic queries.
pg_stat_statements tells you which normalized queries deserve attention. It does not show every execution step. After choosing a suspect query, use EXPLAIN to inspect its plan.
From a statistic to a plan
Start with a ranking such as total execution time or mean execution time. Copy the query text and replace its parameter placeholders with representative real values.
SELECT queryid,
calls,
round(total_exec_time::numeric, 2) AS total_ms,
round(mean_exec_time::numeric, 2) AS mean_ms,
left(query, 120) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;The queryid identifies the normalized statement. The text may contain placeholders such as $1, because PostgreSQL groups statements that differ only in literal values. EXPLAIN needs an executable statement, so provide actual values when you investigate it.
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
WHERE customer_id = 42;This example assumes an orders table with a customer_id column. Replace it with the statement you found in your own database.
ANALYZE makes PostgreSQL run the query and add actual row counts and timings. BUFFERS reports how many shared and temporary blocks the plan used.
Reading the important differences
Compare the estimated values with the actual values. Large differences often explain a poor plan.
| Plan detail | What it tells you | Useful clue |
|---|---|---|
| `cost` | Optimizer estimate, not milliseconds | Compare alternative plans |
| `actual time` | Measured time for a node | Find expensive steps |
| `rows` and `actual rows` | Estimated and observed row counts | Detect inaccurate statistics |
| `Buffers: shared hit` | Blocks found in shared memory | Indicates cached reads |
| `Buffers: shared read` | Blocks read from storage | Indicates physical I/O |
| `temp read` or `temp written` | Temporary block activity | Indicates spilling work |
A Seq Scan reads a table or relation sequentially. It can be correct for a small table or a query that needs most rows. An Index Scan or Bitmap Heap Scan may be better when a selective condition matches a suitable index.
Pay attention to nodes with high actual time and nodes where actual rows differs greatly from the estimate. An underestimate can cause PostgreSQL to choose nested-loop joins or other plans that work poorly for the real number of rows.
A practical investigation
Use this sequence:
- Choose a query with meaningful call volume or high cumulative time.
- Test representative values, including values that usually run slowly.
- Look for the slowest plan nodes.
- Check row-estimate errors and buffer reads.
- Test a proposed index or query change separately.
- Recheck
pg_stat_statementsafter the workload runs.
One plan describes one execution. It cannot explain every parameter value or capture a history of plan changes. Compare several representative executions when performance varies.
Exercise
Why can a query with a low mean execution time still deserve investigation?
Show answer
A high call count can make its total execution time large, so small per-call costs may consume substantial system capacity.