The line a plain plan leaves out#
In the fixture, a session had work_mem set to 8 MB. Plain EXPLAIN printed:
Index Scan using orders_status_idx on orders (cost=0.29..13.82 rows=153 width=24)
Index Cond: (status = 'open'::text)
Executing the same query on the disposable fixture with EXPLAIN (ANALYZE, BUFFERS, SETTINGS) produced:
Index Scan using orders_status_idx on orders (cost=0.29..13.82 rows=153 width=24) (actual time=0.056..0.253 rows=200.00 loops=1)
Index Cond: (status = 'open'::text)
Buffers: shared hit=200 read=2
Settings: work_mem = '8MB'
The second carries three things the first lacks: the actual row count beside the estimate (200 against 153), the blocks touched, and the changed setting. Timing and index-search lines are omitted here. Run with PostgreSQL 18. The EXPLAIN reference says that SETTINGS includes options affecting planning whose value differs from the built-in default. A value that equals the default is not printed, so an absent line means the default and not that you forgot to look.
EXPLAIN ANALYZE executes the statement. An UPDATE can fire triggers, and even a SELECT can call a function that changes state. A sequence value consumed by nextval stays consumed after rollback. If you have not established the effects, use an estimated plan in production. How PostgreSQL handles sequences
What to send#
SELECT version();
SHOW plan_cache_mode;
EXPLAIN (SETTINGS, VERBOSE, FORMAT JSON) SELECT ...;
SELECT relname, reltuples, relpages FROM pg_class WHERE relname IN ('orders');
SELECT last_analyze, last_autoanalyze, n_mod_since_analyze
FROM pg_stat_user_tables WHERE relname = 'orders';
Use the text format for people and JSON for tools. A prepared statement may run with a generic plan that your literal values do not show. State how the application sends the query, and take the plan the same way if you can, for example with EXPLAIN EXECUTE.
Literals in the query and filter values in the plan can be personal data. Replace them, and say that a substituted value may have a different frequency. That changes the plan as much as anything else.
Read what came back#
| Observation | What it supports | What else produces it |
|---|---|---|
| Actual rows far from estimated | Poor statistics for this predicate | Correlated columns, an unusual parameter |
Many read blocks on the first run, hit on the second | A cold cache | Another session loaded the pages |
| Same plan, slower there | Data volume, I/O or load | Bloat, a different machine |
| Different plan | Settings, statistics or version | A missing index, a different parameter type |
On the disposable copy, capture three executions if cache state might explain the difference. Label the first run and keep the later runs beside it so the receiver can compare reads with cache hits.
Decide from a comparison#
Ask for the same block from both environments and compare in this order: version, settings, row counts, statistics age, plan. Stop at the first difference that could explain the result, change only that in a session, and plan again. SET LOCAL inside a transaction keeps the experiment to one transaction.
Worked example#
This example is fictional. Staging takes 40 ms and production 9 s, with different plans. Both blocks show the same version and similar row counts. Staging’s settings line includes random_page_cost = '1.1'. The production plan omits it. The owner checks the setting and the tablespace costs before comparing estimated plans with SET LOCAL random_page_cost = 1.1. The planner then chooses the staging plan. The handoff to the database owner is that single comparison and the question of whether the storage justifies the value globally.
Copying statistics is the next idea people have, and it needs the data size as well. Return to handing over evidence.