Sub topic 14
The Visibility Map and Index-Only Scans
This chapter assumes basic SQL, transactions, BEGIN/COMMIT, and familiarity with PostgreSQL indexes and EXPLAIN. It also assumes that a table may contain several row versions because of MVCC.
Why the visibility map exists
An index entry usually identifies a heap tuple, which is PostgreSQL's stored row version. Even when an index contains every column needed by a query, PostgreSQL normally checks the heap to confirm that the tuple is visible to the current snapshot.
The visibility map reduces this work. It stores a small amount of metadata for each table page:
- All-visible means every tuple on the page is visible to every current and future transaction.
- All-frozen means every tuple is frozen, so transaction ID checks are no longer needed for those tuples.
PostgreSQL records these flags in a separate visibility map fork for each table. The map is much smaller than the table, so checking a page's flag is inexpensive.
Index-only scans
An index-only scan can return a query's values from the index without reading the heap. Two conditions must hold:
- The index must contain all columns needed by the query, including columns used in filtering and output.
- The visibility map must say that the referenced heap page is all-visible.
The second condition matters because ordinary PostgreSQL indexes do not store the full MVCC visibility information needed to decide whether each row version is visible. The visibility map provides a page-level shortcut. If the bit is clear, PostgreSQL visits the heap for a precise check.
CREATE TABLE orders (
order_id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
total numeric NOT NULL
);
CREATE INDEX orders_customer_status_idx
ON orders (customer_id, status);
EXPLAIN (ANALYZE, BUFFERS)
SELECT status
FROM orders
WHERE customer_id = 42;Look for Index Only Scan. Its Heap Fetches value shows how often PostgreSQL still had to visit the heap. A zero value means the visibility map allowed every matching index entry to be trusted. A large value means the scan was only partly index-only in practice.
How pages gain and lose visibility
VACUUM examines table pages and can set the all-visible bit after it confirms that every tuple on a page is visible to all relevant transactions. An update, insert, or delete affecting a page clears that page's all-visible bit because the page may now contain a recently created or obsolete row version.
This means index-only scans work best for tables that are mostly read-only or are vacuumed promptly. A covering index alone does not guarantee that the heap will never be read.
Checking scan quality
Use EXPLAIN (ANALYZE, BUFFERS) to compare plans before and after vacuuming. Also inspect table statistics and vacuum activity when heap fetches remain high. More frequent vacuuming may improve visibility, but it also consumes I/O and maintenance resources. Adding columns to an index can enable index-only scans, but increases index size and write cost.
Exercise
A query uses an index containing every selected column, but EXPLAIN reports many Heap Fetches. What is the likely reason?
Show answer
The referenced heap pages are not marked all-visible, often because recent writes or delayed VACUUM left row versions requiring heap visibility checks.