Skip to content

Reading a Plan Tree: Start at the Leaves

A plan is a tree printed upside down. Reading it in execution order instead of top to bottom turns a wall of text into a diagnosis.

Elias Rowe

4 min readPostgreSQL 18

Reading a Plan Tree: Start at the Leaves — PostgreSQL article cover

A query plan is printed root first and executed leaf first. Read top to bottom, the most expensive-looking node is the last operation performed and the one least worth optimizing.

The shape

Every line beginning with -> is a node. Indentation is nesting: a more indented node is a child of the one above it, and it produces rows for that parent.

plan.txt
Sort (cost=18841.22..18843.72 rows=1000 width=48)
Sort Key: o.created_at DESC
-> Hash Join (cost=1204.00..18791.39 rows=1000 width=48)
Hash Cond: (o.customer_id = c.id)
-> Seq Scan on orders o (cost=0.00..17280.00 rows=1000 width=32)
Filter: (status = 'pending'::text)
-> Hash (cost=1079.00..1079.00 rows=10000 width=16)
-> Seq Scan on customers c (cost=0.00..1079.00 rows=10000 width=16)

Read it from the inside out. The scan on customers runs first and feeds a hash table. The scan on orders runs and is probed against that hash. The join output is sorted. The sort is what the client receives.

Why does the top node show the largest cost?

A node’s cost and actual time include its children. The documentation states this for cost directly, and the same holds for time: in its own nested-loop example the inner index scans account for 0.030 ms inside a parent that reports 0.051 ms. The Sort above shows a total cost of 18843.72, but almost all of it belongs to the hash join beneath it, which in turn inherits most of its cost from the sequential scan on orders.

To find what a node contributed by itself, subtract the children. In this plan the sort adds roughly 52 cost units to an 18791 cost input. Optimizing the sort would be pointless; the scan is the entire query.

This is the most common misreading. The expensive-looking node at the top is usually just carrying the weight of what is below it.

Why does a cheap-looking node make the query slow?

Timings are reported per loop. A node that ran 5,000 times shows the average duration of one execution, not the total.

Nested Loop (cost=0.43..40218.00 rows=5000 width=48)
(actual time=0.028..1284.551 rows=5000 loops=1)
-> Index Scan using orders_pkey on orders o
(actual time=0.021..0.024 rows=1 loops=5000)

0.024 milliseconds looks harmless. Multiplied by 5,000 loops it is 120 milliseconds of the query, and the surrounding nested loop accounts for the rest. Whenever a plan is slower than the sum of its parts appears to justify, check the loops column first.

The lines that are not nodes

Under a node, unindented attributes carry the useful detail.

Filter is a condition applied after rows are read. Rows Removed by Filter is how many were read and discarded — a large value means the work happened and was thrown away, which is exactly the case an index would prevent.

Index Cond is a condition applied by the index itself, so those rows are never read. A predicate appearing as Filter when you expected Index Cond means the index could not be used for that condition, and that is a finding. Which conditions an index can answer that way follows from the order of its columns.

Heap Fetches on an index-only scan reports how often the visibility map bit was not set for the page, so the heap entry had to be visited to find out whether the row was visible.

Node types worth recognizing on sight

You do not need to know every node type, but a handful appear in almost every plan and knowing what each implies saves a lookup.

A Seq Scan reads the whole table in physical order. It is not automatically bad; on a small table or a query returning most rows it is the correct choice, and the row estimate is what decides.

An Index Scan walks the index and fetches each matching row from the heap. An Index Only Scan skips the heap fetch when the visibility map says the page is fully visible, which is why its Heap Fetches line matters.

A Bitmap Heap Scan sits between the two. It collects row locations from an index into a bitmap, sorts them into physical order, and reads the heap sequentially. The planner chooses it when there are too many rows for individual random fetches but too few to justify reading everything.

Among joins, a Nested Loop runs its inner side once per outer row and is cheap only when the outer side is small. A Hash Join builds a hash table from one side and probes it with the other, paying memory for a single pass. A Merge Join requires both inputs sorted and is the usual choice when they already are.

Finally, a Materialize or a Sort node with a Sort Method of external merge Disk is telling you that the operation did not fit in work_mem and spilled. That line is a direct, actionable finding, and it is easy to miss because it sits below the node rather than in it.

Where to look first

Start at the leaves and walk up until the row counts stop making sense. Structure tells you what the query did; it does not tell you whether the planner was justified in choosing it. That is the subject of the next part: the comparison between what the planner expected and what it got.

Frequently asked questions

How do I read a PostgreSQL execution plan?
Read it in execution order, which is the opposite of print order. The first line is the last operation performed, and execution starts at the most indented nodes and flows upward. Indentation encodes nesting: a more indented node is a child that produces rows for the node above it. Start at the leaves and walk up until the row counts stop making sense.
Why does the top node of a PostgreSQL plan show the largest cost?
Because cost and actual time are cumulative. A node's numbers already include everything beneath it, so the node printed at the top carries the weight of the whole tree. To find what a node contributed by itself, subtract its children. This is the most common misreading of a plan, and it sends people optimizing the wrong operation.
What does loops mean in a PostgreSQL EXPLAIN plan?
Loops is the number of times a node ran, and timings are reported per loop rather than as a total. A node showing 0.024 milliseconds over 5,000 loops accounts for 120 milliseconds of the query. Whenever a plan is slower than the sum of its parts appears to justify, the loops column is the first thing to check.
What is the difference between Index Cond and Filter in a PostgreSQL plan?
Index Cond is a condition applied by the index itself, so non-matching rows are never read. Filter is applied after rows have been read, and Rows Removed by Filter reports how many were read and discarded. A predicate appearing as Filter when it was expected as Index Cond means the index could not be used for that condition, which is a finding in itself.

References

  1. docsPostgreSQL 18 documentation — Planner and Optimizer (opens in a new tab)

    How the plan tree is built, which is what the printed indentation reflects.

share

-- written by

Elias RoweDatabase engineer

Elias Rowe writes about database engineering, SQL performance, and production systems. He focuses on measurable behavior, practical trade-offs, and conclusions that can be reproduced rather than assumed.

SQL Fundamentals

Why Your Benchmark Measured the Cache

A query run five times in a row reports the speed of memory, not of the system. Which layer answered the request is the first thing to establish.

· 3 min

Start typing to search the archive.