Sub topic 4
The Visibility Map: Why the Index Alone Is Not Enough
This chapter assumes that you know basic SELECT, CREATE INDEX, and EXPLAIN syntax. It also assumes that you understand that a B-tree index stores ordered references to table rows.
An index does not contain visibility information
An index-only scan can read requested column values from an index without visiting the table heap. The heap is PostgreSQL's main table storage, where complete row versions, called tuples, live.
However, PostgreSQL uses multiversion concurrency control (MVCC). MVCC lets different transactions see different committed versions of a row. An index entry usually does not contain enough information to decide whether its row version is visible to the current query.
For example, an index entry might identify a row with customer_id = 42 and contain its total. PostgreSQL still needs to know whether that row version is committed and visible to the query's snapshot.
The visibility map provides this information at the page level.
How the visibility map works
A visibility map is a small relation associated with each table. It records status for each heap page. A heap page is a fixed-size block containing several table tuples.
The important status is the all-visible bit:
- Set means every tuple on the heap page is visible to every transaction that needs to check it.
- Clear means PostgreSQL must inspect the heap page before trusting the index entry.
- An all-frozen bit can also record that tuples no longer need normal transaction-age checks.
The map uses one bit per status and is much smaller than the table. An index-only scan checks the map before deciding whether a heap visit is necessary.
Why VACUUM matters
VACUUM examines dead and live row versions. When it confirms that every tuple on a heap page is visible to all relevant transactions, it sets that page's all-visible bit.
Changes can clear the bit. An UPDATE creates a new row version and may affect the page's visibility status. Inserts and deletes can do the same. Therefore, an index-only scan can work well immediately after maintenance and require heap visits after heavy write activity.
Run VACUUM before testing a covering index on a mostly stable table:
VACUUM (ANALYZE) orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;ANALYZE updates planner statistics. It does not set visibility-map bits by itself; the vacuum portion does that work.
Reading the result
With a suitable covering index, the plan may contain Index Only Scan. Its Heap Fetches value shows how often PostgreSQL still had to visit the heap:
| Heap Fetches | Likely meaning |
|---|---|
| 0 or close to 0 | Most matching pages were all-visible |
| A moderate value | Some pages changed or have not been vacuumed |
| High relative to rows returned | The scan is doing substantial heap visibility work |
A zero value does not mean that the index contains all MVCC metadata. It means the visibility map allowed PostgreSQL to trust the index entries without checking the heap.
Exercise
Why can an index-only scan still show nonzero Heap Fetches?
Show answer
The index contains the requested values, but the visibility map has not marked every matching heap page as all-visible, so PostgreSQL visits some pages to check MVCC visibility.