What is EXPLAIN and how do you use it?
EXPLAIN shows a query's execution plan: how the database intends to read the tables. EXPLAIN ANALYZE also runs the query and adds actual time and row counts. A plan is read for the node type, discarded rows and the gap between estimate and actual, and cost is not milliseconds. For UPDATE and DELETE, ANALYZE really changes data, so it is run in a transaction with ROLLBACK.
SQL · Constraints and indexes
A bug "the page is slow" with no numbers gets bounced. What exactly to bring the developer?
A ticket "transaction history opens slowly" is returned with "what exactly is slow?". The execution plan answers: which node spends the time, how many rows the database read and discarded, whether an index was used. EXPLAIN shows the plan the database intends to run, and EXPLAIN ANALYZE runs the query and adds actual figures. The lesson is about reading this output and not harming the data.
How it works and why. A query says what is needed, and the plan is how: the order and method of reading tables. The planner estimates the cost of options from statistics (how many rows in the table, how values are distributed) and picks the cheapest. So the plan depends on the data: on a dev with a hundred rows it differs from a staging with a million. That is also why analysis must run on data close to production. Plan nodes are read from the inside out and bottom up, and cost is in arbitrary units, not milliseconds: it is compared only with itself.
The key nodes:
- Seq Scan: reads the whole table. Suspicious on a big table with a selective filter, normal on a small one.
- Index Scan, Index Only Scan, Bitmap Heap Scan: a search through an index, different ways of fetching rows after it.
- Nested Loop, Hash Join, Merge Join: ways to join tables.
- Sort: sorting an index could have removed.
Example: from "slow" to the cause. A table of a million rows, the history query of one account:
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM transactions
WHERE account_id = 10 AND created_at >= '2026-09-01';
An illustrative output without an index (shortened, numbers made up for the example). It is built so the figures agree: a million rows minus 48 found is 999,952 discarded.
Seq Scan on transactions (cost=0.00..21450.00 rows=52 width=48)
(actual time=0.02..95.40 rows=48 loops=1)
Filter: (account_id = 10 AND created_at >= '2026-09-01')
Rows Removed by Filter: 999952
Planning Time: 0.15 ms
Execution Time: 95.50 ms
- Seq Scan: the whole table was read for 48 rows.
- 999,952 rows were read and discarded: this is where the time goes.
- The actual time is 95 ms. It is the sum of all nodes, and in production under many queries it multiplies.
After creating the index (account_id, created_at) the same query (also an illustrative output):
Index Scan using idx_transactions_account_created
on transactions (cost=0.42..12.10 rows=52 width=48)
(actual time=0.03..0.21 rows=48 loops=1)
Index Cond: (account_id = 10 AND created_at >= '2026-09-01')
Planning Time: 0.20 ms
Execution Time: 0.30 ms
The node changed to Index Scan, the condition moved to Index Cond, no rows were discarded, the time is 0.3 ms. For a bug report this is ideal evidence: "the plan shows a Seq Scan discarding 999,952 rows, an index on (account_id, created_at) is needed". The estimate (rows=52) and the actual (rows=48) are close, a good sign. If the estimate were 1 and the actual 48,000, the statistics are stale or the data is unevenly distributed: ANALYZE table, which refreshes them, helps.
EXPLAIN and EXPLAIN ANALYZE
Plan only
The query does not run and no data changes. You see estimates, not facts.
Plan and facts
The query really runs: actual time and rows appear, but UPDATE and DELETE will change data.
A safe way for queries that change data:
BEGIN;
EXPLAIN ANALYZE
DELETE FROM transactions WHERE status = 'failed';
ROLLBACK; -- the data is back
Limits and alternatives. A rollback undoes row changes but not everything: used sequence values are not returned, triggers fired, and the query may have taken locks and load. On a shared staging and on production, do not run EXPLAIN ANALYZE on modifying queries. BUFFERS adds the number of pages read: useful when the time is unstable because of the cache. For a slow page a single plan is not enough: you must find which SQL the application actually sends (query logs, pg_stat_statements) and only then analyse it. The plan points to the cause, but the fix (an index, a rewritten query, a limit) is chosen by the developer.
Common mistakes.
- Running EXPLAIN ANALYZE for UPDATE or DELETE without a transaction and ROLLBACK: the data is really changed.
- Analysing a plan on a small database and drawing conclusions about production.
- Reading cost as milliseconds.
- Seeing a Seq Scan and demanding an index at once: on a small table or with a wide filter it is the right choice.
- Analysing not the query the application sends (other parameters, another plan).
Check yourself
In a plan a node has rows=1 as the estimate while the actual is rows=48000. What does it mean and what to try?
Answer
The planner estimated badly: the statistics are stale or the values are unevenly distributed. Because of it, it may have chosen a poor node. First run ANALYZE on the table and look at the plan again; if the gap stays, it is a reason for the developer to consider extended statistics or another query.
A colleague runs EXPLAIN ANALYZE DELETE FROM transactions WHERE status = 'failed' on a shared staging "just to look". What is wrong and how to make it safer?
Answer
ANALYZE really executes the DELETE, so the rows will be deleted. Safer: plain EXPLAIN without ANALYZE (it only builds the plan), or BEGIN, EXPLAIN ANALYZE, ROLLBACK. Even then on a shared staging the query creates load, so a copy is better.