Sub topic 13
Finding the Node That Costs the Most
Assume you can write SELECT queries with WHERE, JOIN, GROUP BY, and ORDER BY. You should also know what a database index does and how to run SQL in psql or another SQL client.
A query plan is a tree. Each node performs one operation, such as reading a table, joining rows, sorting results, or calculating an aggregate. To find a slow part, inspect the nodes from the bottom upward, then compare their estimated work with their actual work.
Read the plan as a tree
Run the query with execution details:
EXPLAIN (ANALYZE, BUFFERS)
SELECT c.name, count(*) AS order_count
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= DATE '2026-01-01'
GROUP BY c.name
ORDER BY order_count DESC
LIMIT 10;The plan usually looks like this:
Limit
-> Sort
-> HashAggregate
-> Hash Join
-> Seq Scan on orders
-> Hash
-> Seq Scan on customersThe most-indented nodes run first. Their output flows into their parent nodes.
A parent includes the work of its children. Therefore, do not add every node's cost together. That would count the same work multiple times. Instead, find where time accumulates and where the flow of rows changes sharply.
Use actual time and loops
With ANALYZE, PostgreSQL executes the query and reports values such as:
Hash Join (actual time=12.400..410.800 rows=850000 loops=1)The two time values are startup time and time until the node produced all its rows. The rows value is the number of rows produced per loop. loops tells you how many times PostgreSQL ran the node.
A node with a small time but many loops can be expensive overall. For example, a node showing actual time=0.020..0.030 loops=100000 may account for about 3,000 milliseconds of work. Check the product of the per-loop time and loop count when judging repeated work.
| Plan detail | What to inspect |
|---|---|
| `actual time` | How long one execution of the node took |
| `loops` | How often PostgreSQL repeated the node |
| `rows` | How many rows the node produced per loop |
| Parent node | Whether later work, such as sorting, magnified the cost |
Separate the bottleneck from the symptom
A sort may appear near the top with a high total time, but its input may already contain far too many rows. Trace downward and ask:
- Did a scan read a large table unnecessarily?
- Did a join produce far more rows than expected?
- Did an aggregate or sort receive rows that could have been filtered earlier?
- Did a nested loop repeat an expensive inner scan?
- Did a node spill work to temporary disk?
BUFFERS adds I/O evidence. shared hit means PostgreSQL found pages in the buffer cache. shared read means it had to read pages from storage. A node with many reads may be limited by I/O, while a node with mostly hits may be limited by CPU or row processing.
A practical investigation order
- Find the largest actual time near the plan's main path.
- Multiply repeated work by the
loopscount. - Follow large row increases back toward the scan or join that caused them.
- Check whether high buffer reads explain the delay.
- Compare estimated rows with actual rows before choosing a fix.
This method distinguishes the expensive operation from a parent that merely reports the cost of all work beneath it.
Exercise
A node reports actual time=2.000..2.500 rows=5 loops=10000. Is it safe to call this cheap because one loop takes only 2.5 milliseconds?
Show answer
No. The node repeats 10,000 times. Its displayed time is per loop, so the repeated work can total about 25 seconds before considering other plan overhead.