PostgreSQL Execution Plan Analysis
You view a PostgreSQL execution plan with EXPLAIN. Plain EXPLAIN shows the plan the planner chose with estimated costs and row counts, and EXPLAIN ANALYZE actually runs the statement and adds the actual time, row count, and loop count to each node. Adding BUFFERS shows the number of blocks each node read. Tuning means using EXPLAIN (ANALYZE, BUFFERS) to find the nodes that did a lot of work, and, where the estimate and the actual diverge, working out why the planner chose as it did.
Summary
| Item | Content |
|---|---|
| Estimated plan | EXPLAIN statement. Does not execute |
| Actual plan | EXPLAIN (ANALYZE, BUFFERS) statement. It actually runs the statement, so run DML inside a transaction and ROLLBACK |
| Commonly used options | VERBOSE (output columns, schema names), SETTINGS (planner settings that differ from the defaults), FORMAT JSON, TIMING OFF (removes timer overhead), WAL (WAL generated) |
| Version-specific options | GENERIC_PLAN (16+, generic plan without bind values), MEMORY (17+, memory used for planning), SERIALIZE (17+, cost of serializing the result). From 18, BUFFERS is on by default with ANALYZE |
| Collection in production | auto_explain extension (logs the plans of slow statements), pg_stat_statements (cumulative time and blocks per statement) |
| Trying a different plan | No hints. Compare using planner settings such as SET enable_nestloop = off (pg_hint_plan is an external extension) |
| Sandbox | Shows EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) as a table. Starts = loops, A-Rows = rows × loops, Buffers = shared hit + read |
Reading the Output
This is the actual output from the sandbox for a query on the last 7 days of orders from VIP customers.
Nested Loop (cost=0.72..8721.74 rows=137 width=21) (actual time=0.033..4.497 rows=155 loops=1)
Buffers: shared hit=3991
-> Index Scan using customers_x1 on customers c (cost=0.29..841.97 rows=970 width=13) (actual time=0.018..0.666 rows=1000 loops=1)
Index Cond: ((grade)::text = 'VIP'::text)
Buffers: shared hit=837
-> Index Scan using orders_x1 on orders o (cost=0.43..8.11 rows=1 width=16) (actual time=0.004..0.004 rows=0 loops=1000)
Index Cond: ((cust_id = c.cust_id) AND (ord_dt >= '2025-09-20 00:00:00'::timestamp without time zone))
Buffers: shared hit=3154
Planning:
Buffers: shared hit=293
Planning Time: 0.353 ms
Execution Time: 4.520 ms
1. Node order
- Lines that start with
->are child nodes. The deeper the indentation, the earlier the node starts, and siblings run from top to bottom. - The first child of a join node is the outer side and the second is the inner side. In a hash join, a
Hashnode sits above the second child, and that side is the hash table (Build). InitPlanis a subquery that runs only once (independent of the outer row), andSubPlanis a subquery that runs for each outer row.
2. The two parentheses: estimate and actual
| Value | Meaning |
|---|---|
cost=0.72..8721.74 | Startup cost..total cost. The unit is relative to seq_page_cost (one sequential page = 1). It is not time |
rows=137 | Estimated row count (per execution) |
width=21 | Estimated row width (bytes) |
actual time=0.033..4.497 | Time to the first row..to the last row (ms). Average per execution |
rows=155 | Actual row count. Average per execution (rounded) |
loops=1 | Number of times this node ran |
When loops is greater than 1, multiply the time and row count. In the example above, the orders_x1 lookup shows rows=0 loops=1000. The average is 0.155, which just rounds to 0, and the sum of the actual rows is 155. The startup cost is the work that must be done before the first row can be produced (sorting, building a hash table), and when there is a LIMIT, the planner prefers plans with a small startup cost.
3. Buffers
| Value | Meaning |
|---|---|
shared hit | Blocks found in the shared buffers (shared_buffers) |
shared read | Blocks not in the shared buffers and read from the OS. They may come from the OS file cache |
shared dirtied / written | Blocks this execution dirtied / wrote directly |
temp read / written | Temporary file blocks used by sorts and hashes that exceeded work_mem |
Planning: Buffers | Catalog blocks read while building the plan (13+) |
Buffers is a cumulative value that includes child nodes. In the example above, the share of Nested Loop itself is 3,991 − 837 − 3,154 = 0. When you run the same statement twice, the second run usually turns read into hit. If track_io_timing is on, an I/O Timings line also appears.
4. Where the estimate and the actual diverge
Compare rows (estimate) × loops with the actual row count. In the example above, VIP is estimated at 970 and actually 1,000, which is close. This is because the planner has the frequency of each grade value (MCV) (planner statistics). The lowest node where they diverge by 10 times or more is the cause. Look at the statistics (pg_stats) of the columns used in that node's condition and at the shape of the condition (expressions, correlation).
5. Extra lines per node
| Line | Nodes where it appears | Meaning and what to check |
|---|---|---|
Index Cond | Index Scan, Index Only Scan, Bitmap Index Scan | The condition that sets the index search range (Oracle's access) |
Filter / Rows Removed by Filter | Most nodes | The condition applied after reading and the number of rows discarded (per execution). A large number means the index did not cover the whole condition |
Recheck Cond / Heap Blocks: exact=… lossy=… | Bitmap Heap Scan | If lossy appears, work_mem is small and rows are rechecked per page |
Heap Fetches | Index Only Scan | Number of times the heap was checked because the Visibility Map was not "all visible". A large number means VACUUM is needed |
Join Filter / Rows Removed by Join Filter | Join | Filters, after the join, conditions that are not join keys |
Hash Cond / Buckets … Batches … Memory Usage | Hash Join, Hash | If Batches > 1, the hash exceeded work_mem and was split to disk |
Sort Method | Sort | quicksort (memory), top-N heapsort (LIMIT), external merge Disk: …kB (spilled to disk) |
Presorted Key | Incremental Sort | The leading columns that are already sorted |
Hits / Misses / Evictions | Memoize | Hits of the NL inner-side result cache |
Batches … Disk Usage | HashAggregate (13+) | The hash aggregate spilled to disk |
(never executed) | Every node | Never executed. The outer side returned 0 rows or the branch was pruned |
Collecting Plans in Production
-- Log the plans of slow statements to the server log (session or postgresql.conf)
LOAD 'auto_explain';
SET auto_explain.log_min_duration = '500ms';
SET auto_explain.log_analyze = on;
SET auto_explain.log_buffers = on;
-- Cumulative statistics per statement
SELECT queryid, calls, total_exec_time, mean_exec_time, rows,
shared_blks_hit, shared_blks_read, temp_blks_written
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;
auto_explain.log_analyze adds execution statistics overhead to every statement, so in production set log_min_duration large enough, or measure only a fraction with auto_explain.sample_rate.
Prepared Statements and Generic Plans
For a prepared statement (PREPARE, or server-side preparation by a driver), PostgreSQL builds a custom plan from the bind values on each of the first 5 executions. After that, it switches to a generic plan if the generic plan's cost is not much worse than the average of the custom plans. On a column with skewed values, a generic plan can be bad for some values.
Setting plan_cache_mode | Behavior |
|---|---|
auto (default) | The 5-execution rule above |
force_custom_plan | Always a custom plan |
force_generic_plan | Always a generic plan |
EXPLAIN (GENERIC_PLAN) (16+) shows the generic plan without bind values. The sandbox builds a new plan for every statement, so it is always a custom plan.
Comparison with Other DBMSs
| Item | PostgreSQL | Oracle | SQL Server |
|---|---|---|---|
| Actual plan | EXPLAIN (ANALYZE, BUFFERS) | DISPLAY_CURSOR … ALLSTATS LAST | Actual execution plan |
| Actual rows | rows (per-execution average) × loops | A-Rows (total) | Actual Rows (total) |
| Condition | Index Cond / Filter | access / filter | Seek Predicates / Predicate |
| Plan pinning | None (extension pg_hint_plan) | Hints, SQL Plan Baseline | Hints, Query Store forcing |
Learn more: Oracle Execution Plan Analysis, SQL Server Execution Plan Analysis, Join Methods.
NOTE Options and output lines differ slightly by version. The output in this document is from PostgreSQL 17 in the sandbox.