Sub topic 6
Reading EXPLAIN (ANALYZE, BUFFERS) and Heap Fetches
This chapter assumes you can run EXPLAIN and recognize node names like Seq Scan and Index Scan. It also assumes you know that the visibility map and VACUUM exist, as covered in the previous chapters. Here you learn to read the numbers in a plan and judge whether an index-only scan is saving you work.
What each option adds
Plain EXPLAIN shows the plan the planner chose, with estimates. EXPLAIN ANALYZE runs the query and adds measured row counts and times. BUFFERS adds counts of 8 kB pages touched by each node.
Three facts matter before you read any output:
EXPLAIN ANALYZEexecutes the statement. AnUPDATEorDELETEreally changes data.actual timeandrowsare averages per loop. Multiply byloopsto get totals.- Since PostgreSQL 18,
ANALYZEincludesBUFFERSby default. Writing it explicitly is harmless and works on older versions.
A plan that must visit the heap
Use the one-million-row orders table from the chapter setup. Create an index on customer_id alone and ask for a column the index does not hold.
CREATE INDEX orders_customer_idx ON orders (customer_id);
VACUUM (ANALYZE) orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total FROM orders WHERE customer_id = 42;A typical result looks like this. The numbers are illustrative, and yours will differ.
Bitmap Heap Scan on orders (cost=5.30..380.20 rows=100 width=10) (actual time=0.150..0.700 rows=98 loops=1)
Recheck Cond: (customer_id = 42)
Heap Blocks: exact=97
Buffers: shared hit=100
-> Bitmap Index Scan on orders_customer_idx (cost=0.00..5.27 rows=100 width=0) (actual time=0.090..0.090 rows=98 loops=1)
Index Cond: (customer_id = 42)
Buffers: shared hit=3The index lacks total, so PostgreSQL must read heap pages. Here that is 97 heap pages plus 3 index pages, for 100 buffers in total. The matching rows are scattered across the table, which is why the heap page count is close to the row count. You may see a plain Index Scan instead, depending on settings and statistics. Either way, the heap is visited.
The same query as an index-only scan
Now add total as a payload column.
CREATE INDEX orders_customer_total_idx
ON orders (customer_id) INCLUDE (total);
VACUUM (ANALYZE) orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total FROM orders WHERE customer_id = 42;Index Only Scan using orders_customer_total_idx on orders (cost=0.42..6.10 rows=100 width=10) (actual time=0.030..0.060 rows=98 loops=1)
Index Cond: (customer_id = 42)
Heap Fetches: 0
Buffers: shared hit=4The buffer count dropped from about 100 to 4. The matching entries sit next to each other in the index, so a handful of index pages (plus, at most, a visibility map page) answers the query. This is the result you are aiming for.
What Heap Fetches counts
An index-only scan reads an index entry and then checks the visibility map for the heap page that entry points to.
- If the page is marked all-visible, every row on it is visible to every transaction. The scan returns the value from the index and does not touch the heap.
- If the page is not marked all-visible, the scan visits the heap tuple to check visibility. Each such visit adds one to
Heap Fetches.
Heap Fetches therefore counts index entries that needed a heap visit, not distinct pages. If three matching rows share one page that is not all-visible, you can see three heap fetches for one page. In the buffer counts, heap pages that were fetched appear as additional buffers on the node.
| `Heap Fetches` relative to `rows` | Meaning | Usual action |
|---|---|---|
| 0 or close to 0 | The visibility map covers the pages you read. | None. |
| A large fraction of `rows` | Many pages are not all-visible. The scan works but behaves like an ordinary index scan with extra steps. | Check recent writes and `VACUUM`. |
| Equal to `rows` | The visibility map gave no help. | Run `VACUUM`. Look for long transactions. |
Seeing heap fetches appear
An update clears the all-visible bit on every page it touches. The following sequence shows the effect, then the repair.
UPDATE orders SET total = total + 1 WHERE customer_id = 42;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total FROM orders WHERE customer_id = 42;
VACUUM orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total FROM orders WHERE customer_id = 42;The first plan should still be an Index Only Scan, but with Heap Fetches greater than zero, because the new row versions sit on pages that are no longer all-visible. The second plan, after VACUUM, should report fewer fetches or none. VACUUM can set the bit again only for pages where all remaining tuples are visible to every open transaction.
The planner also relies on this state. It reads relallvisible from pg_class, which VACUUM and ANALYZE update. If that number is low, the planner expects many heap fetches and may prefer a different plan. You can check it with the query from the visibility map chapter.
SELECT relname, relpages, relallvisible
FROM pg_class
WHERE relname = 'orders';Measuring a write without keeping it
Because EXPLAIN ANALYZE executes the statement, wrap data-changing experiments in a transaction and roll back.
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
UPDATE orders SET total = total + 1 WHERE customer_id = 42;
ROLLBACK;The rollback discards the changes. The dead row versions the update created still remain in the heap until VACUUM removes them.
Reading the buffer counters
The Buffers: line has several counters. Each is a count of 8 kB pages for that node and its children.
shared hitis the number of pages found inshared_buffers.shared readis the number of pages requested from the operating system, which may be served from its cache or from disk.shared dirtiedandshared writtencount pages modified or flushed. They appear mainly with writes.
Compare hit + read between two plan shapes to compare the work they do. Run each query twice. The first run may load pages from disk and show many read values. The second run shows the steady-state hit counts you should compare.
Estimated versus actual
The cost numbers (cost=0.42..6.10) are in planner units, not milliseconds. Compare rows= in the estimate with rows= in the actual section. A large gap means the statistics are stale or the filter is correlated with other columns, and ANALYZE is the first thing to try. The estimate of heap visits for an Index Only Scan comes from relallvisible, so a bad estimate there can lead the planner to avoid a covering index that would work well.
A reading routine
Read plans in the same order each time.
Common misreads
- "Index Only Scan means no heap access." Not when
Heap Fetchesis high. The node name describes what the planner hoped for.Heap Fetchesreports what happened. - "Fewer buffers always means faster." Buffers measure pages touched, not time. A cold cache can make a query with few buffers slower than a warm query with many.
- "Heap Fetches counts pages." It counts index entries that required a heap visit.
- "One run is enough." Timing and
readcounts vary with cache state. Repeat the query and compare the warm runs.
Exercise
After a large batch UPDATE, your query still shows "Index Only Scan" but the plan reports Heap Fetches: 4800 for rows=5000. What does this tell you, and what do you do first?
Show answer
Almost every index entry required a heap visit, so the visibility map was not marking most of the touched pages as all-visible. The UPDATE cleared those bits. Run VACUUM on the table (a plain VACUUM is enough) and repeat the EXPLAIN (ANALYZE, BUFFERS). If Heap Fetches stays high, look for a long-running or idle-in-transaction session holding back the cleanup horizon, because VACUUM cannot mark pages all-visible while an old transaction might still need to see older row versions.