Sub topic 5
VACUUM and the all-visible bit
This chapter assumes you know basic SQL, B-tree indexes, and how to read a simple EXPLAIN plan. You also need a table such as orders with an index that can support an index-only scan.
Why the index needs help from the heap
An index entry usually stores an indexed value and a tuple identifier, or TID. The TID points to a row location in the table's heap. An index-only scan can return selected column values from the index, but it still needs to know whether each referenced row is visible to the current transaction.
PostgreSQL uses multi-version concurrency control, or MVCC. An update creates a new row version, and an old version can remain in the heap until cleanup. Therefore, the index alone cannot always prove that an entry represents a visible row.
The visibility map provides that proof at page level. It stores bits for heap pages:
- The all-visible bit means every tuple on the page is visible to every transaction that needs to see it.
- The all-frozen bit means transaction IDs on the page no longer need freezing work.
For an index-only scan, the all-visible bit is the important one.
What VACUUM changes
VACUUM scans table pages and removes or records obsolete row versions when it can do so safely. It also updates the visibility map. When every tuple on a page is known to be visible, PostgreSQL sets that page's all-visible bit.
An insert or update generally clears the all-visible bit for the affected page. This is necessary because the page now contains a row version whose visibility must be checked. A later VACUUM can set the bit again after it verifies the page.
Run this example after creating a covering index on orders:
VACUUM (ANALYZE) orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;ANALYZE refreshes planner statistics. It does not set visibility-map bits by itself. The VACUUM part performs visibility work.
VACUUM does not guarantee that every page becomes all-visible. Pages with recently changed rows, open transactions that still need old row versions, or other visibility conditions may remain not all-visible.
Observing visibility
The pg_class view gives a table-level summary:
SELECT relname, relpages, relallvisible
FROM pg_class
WHERE relname = 'orders';relallvisible is the estimated number of heap pages marked all-visible, while relpages is the estimated total number of pages. These values help you spot a table with little visibility coverage, but they are not a row count.
For page-level visibility details, install and query the pg_visibility extension:
CREATE EXTENSION IF NOT EXISTS pg_visibility;
SELECT *
FROM pg_visibility_map_summary('orders');A high all-visible fraction makes index-only scans more effective. It does not force the planner to choose one.
Maintenance and write activity
Autovacuum normally performs this maintenance automatically. Its timing depends on table size, modification rates, and configuration. A busy table may repeatedly lose all-visible bits as it receives updates, so a covering index can still show heap fetches.
Do not run plain VACUUM inside a transaction block. In production, prefer normal autovacuum unless you have a specific maintenance reason to run it manually.
Exercise
After updating rows covered by an index-only query, which operation can restore all-visible bits after the page is safe to mark?
Show answer
VACUUM can recheck the page and restore its all-visible bit when every tuple on the page is visible.