The idea · Lab 3.2.3A normal Index Scan uses the index only to find matching row locations, then goes to the table heap for the actual row data. If the index already stores every column a query needs — either because those columns are part of the indexed key, or added purely for this purpose via INCLUDE — PostgreSQL can, in principle, answer the query from the index alone, never touching the heap at all. That is an Index Only Scan, and it is genuinely faster: no random-access heap lookups per row, just a walk through the index structure.
But "in principle" carries a real asterisk. An Index Only Scan still has to confirm each row is visible to the current transaction — MVCC visibility cannot be determined from the index entry alone. PostgreSQL's visibility map tracks, per heap page, whether every row on it is known to be visible to everyone; only when a page is marked all-visible there can an Index Only Scan trust the index and skip the heap for that row entirely. A freshly inserted table has no pages marked all-visible yet, so even a perfectly-designed covering index still reports real, nonzero Heap Fetches — until VACUUM updates the map.
A Covering Index, and an Unexpected Heap Fetch
CREATE INDEX idx_covering_email ON idx_users3(email) INCLUDE (name, created_at);
EXPLAIN (ANALYZE, TIMING OFF)
SELECT name, created_at FROM idx_users3 WHERE email = 'user25000@example.com';
Index Only Scan using idx_covering_email on idx_users3 (actual rows=1.00 loops=1)
Index Cond: (email = 'user25000@example.com'::text)
Heap Fetches: 1
The plan already says "Index Only Scan" — the index structurally has everything this query needs, email as the search key and name/created_at riding along via INCLUDE. And yet "Heap Fetches: 1" means it touched the table heap anyway, for this one row. Structurally covered is not the same as visibility-confirmed.
VACUUM, and the Same Query Again
VACUUM idx_users3;
EXPLAIN (ANALYZE, TIMING OFF)
SELECT name, created_at FROM idx_users3 WHERE email = 'user25000@example.com';
Index Only Scan using idx_covering_email on idx_users3 (actual rows=1.00 loops=1)
Index Cond: (email = 'user25000@example.com'::text)
Heap Fetches: 0
Identical index, identical query — "Heap Fetches: 0" now. VACUUM updated the visibility map, marking the heap page this row lives on as all-visible, so the Index Only Scan can now trust the index entry completely and skip the heap.
Confirming the Index Is Actually Being Used
SELECT relname, idx_scan FROM pg_stat_user_indexes WHERE indexrelname = 'idx_covering_email';
idx_scan increments on every scan that used this index — a direct, cumulative confirmation independent of any single EXPLAIN run, useful for auditing which indexes a production workload actually exercises over time (the same view Lab 3.2.6 uses to find indexes that never get used at all).
Key concepts
INCLUDE columns
Extra columns attached to a B-tree index purely for their payload value, not as part of the searchable key. They add no sort order and cannot be used in an Index Cond, but their values are stored directly in the index, so a query needing only the indexed key columns plus the INCLUDE columns can be answered entirely from the index — a smaller, cheaper index than making every column part of the actual sort key.
Visibility map
A compact, per-relation bitmap tracking which heap pages are known to contain only rows visible to every current and future transaction (no pending updates or deletes still awaiting cleanup). An Index Only Scan consults this map before trusting an index entry alone; for any page not yet marked all-visible, it still has to fetch the actual heap row to check visibility, which is exactly what a nonzero Heap Fetches count reports. VACUUM is what updates the map.
What to remember
- INCLUDE columns let a B-tree index carry extra payload for a query to read, without making those columns part of the actual sorted search key
- "Index Only Scan" in a plan means the index structurally covers the query — it does not by itself mean the heap was never touched
- Heap Fetches reports how many rows still needed a heap visit despite an Index Only Scan, gated by the visibility map, not by the index design
- VACUUM updates the visibility map; only after that does a covering index's Heap Fetches actually reach zero for previously-modified pages
- pg_stat_user_indexes.idx_scan is a cumulative, ground-truth usage counter — useful for confirming an index is actually being exercised by real traffic, independent of any single EXPLAIN run