Sub topic 6
Reading `EXPLAIN (ANALYZE, BUFFERS)` and `Heap Fetches`
This chapter assumes you know basic SQL, B-tree indexes, and simple EXPLAIN plans. The examples use the orders table and indexes from the earlier examples.
An index-only scan can return the columns a query needs from an index. However, PostgreSQL still needs to check whether each table row is visible to the current transaction. The plan shows how often it had to visit the table heap in the Heap Fetches line.
Read a plan from the outside in
Run a query with execution details and buffer statistics:
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;A simplified result might look like this:
Index Only Scan using orders_customer_total_idx on orders
(cost=0.42..8.44 rows=4 width=10)
(actual time=0.035..0.041 rows=4 loops=1)
Index Cond: (customer_id = 42)
Heap Fetches: 0
Buffers: shared hit=3
Planning Time: 0.120 ms
Execution Time: 0.070 msThe important parts are:
| Plan field | Meaning |
|---|---|
| `cost` | Planner estimates, not milliseconds |
| `rows` | Estimated rows produced by this node |
| `actual time` | Measured startup and completion time in milliseconds |
| `loops` | Number of times the node ran |
| `Heap Fetches` | Table rows visited for visibility checks |
| `Buffers` | Shared memory pages read or found in cache |
The planner's estimates appear before the actual values. For example, rows=4 is the estimate, while actual ... rows=4 is the measured result. Large differences can indicate stale statistics or a query whose data distribution is difficult to estimate.
Buffers: shared hit=3 means PostgreSQL found three needed pages in its shared buffer cache. A read count means it had to read pages from storage during execution.
Understand the scan node
The scan type tells you where PostgreSQL found the result:
Seq Scanreads the table, usually because many rows match or no useful index exists.Index Scanuses an index to find row locations, then visits the heap to retrieve columns or check visibility.Bitmap Heap Scanfirst builds a bitmap of matching heap locations and then reads those heap pages in batches.Index Only Scangets requested columns from the index and visits the heap only when visibility information is needed.
The following path shows the decision for an index-only scan:
Interpret `Heap Fetches`
Heap Fetches: 0 means every matching index entry came from a heap page marked all-visible in the visibility map. PostgreSQL could trust that the row's visibility did not require a table lookup.
A nonzero value does not mean the index-only scan failed. It means some heap visits were necessary:
Index Only Scan using orders_customer_total_idx on orders
(actual time=0.040..0.220 rows=4 loops=1)
Heap Fetches: 4If four rows matched and there were four heap fetches, the visibility map did not help for those rows. Updates, inserts, or deletes can clear the all-visible mark for affected pages. VACUUM can later inspect those pages and mark them all-visible again when appropriate.
Diagnose unexpected plans
If PostgreSQL chooses an Index Scan instead, check these possibilities:
- The index does not contain every selected column or predicate column.
- The query returns too many rows, so a sequential or bitmap scan is cheaper.
- The table pages are not sufficiently all-visible.
- Statistics are stale. Run
ANALYZE orders. - The index access method does not support index-only scans for the needed data types.
- The cost settings make heap access appear inexpensive.
Use EXPLAIN (ANALYZE, BUFFERS) after testing representative data. A plan with a low execution time and a few heap fetches can be perfectly adequate. Do not add a covering index only to force Heap Fetches: 0.
Exercise
An index-only plan reports 10,000 output rows and 10,000 Heap Fetches. What does this tell you?
Show answer
PostgreSQL used the index-only scan, but it visited the heap for visibility checks for every output row. Check table activity and run VACUUM when appropriate, then measure the plan again.