Sub topic 11
Visibility Map, Freezing, and Transaction ID Wraparound
This chapter assumes you know basic SQL, PostgreSQL transactions, and indexes. You will learn how PostgreSQL tracks page visibility, freezes old row versions, and prevents transaction ID wraparound.
The visibility map
PostgreSQL stores table data in pages. A visibility map is a small data structure with two bits for each table page:
- The all-visible bit means every row version on the page is visible to every current transaction.
- The all-frozen bit means every row version on the page has been frozen and no longer needs normal transaction ID checks.
The all-frozen state implies all-visible, but an all-visible page is not necessarily all-frozen.
VACUUM sets these bits after checking a page. If a transaction later changes a row on that page, PostgreSQL clears the all-visible bit. It also clears the all-frozen bit when the page is no longer fully frozen.
The visibility map has two important uses:
- VACUUM can skip some work on pages that are already known to be all-visible.
- An index-only scan can return a value from an index without fetching the table page when the page's all-visible bit is set.
Freezing old row versions
PostgreSQL uses transaction IDs, or XIDs, to decide whether a row version is visible. XIDs are finite. They eventually wrap around and start again at lower values.
A row version that contains a very old committed XID could then be mistaken for a row created by a newer transaction. PostgreSQL prevents this by freezing old row versions. A frozen row no longer depends on its original XID for visibility checks.
VACUUM normally freezes tuples older than the configured freeze age. Important settings include:
vacuum_freeze_min_age: the minimum tuple age before normal VACUUM freezes it.vacuum_freeze_table_age: the table age at which VACUUM performs a more complete freeze scan.autovacuum_freeze_max_age: the maximum table age before PostgreSQL forces an anti-wraparound autovacuum.
The table's relfrozenxid records the oldest unfrozen transaction ID that might still matter. Its age is more useful than the raw XID value.
SELECT c.oid::regclass AS table_name,
age(c.relfrozenxid) AS xid_age,
pg_size_pretty(pg_table_size(c.oid)) AS table_size
FROM pg_class AS c
JOIN pg_namespace AS n
ON n.oid = c.relnamespace
WHERE c.relkind IN ('r', 'm')
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY age(c.relfrozenxid) DESC
LIMIT 20;Transaction ID wraparound
Wraparound is a correctness and availability risk, not ordinary table bloat. If a table approaches the anti-wraparound limit, PostgreSQL launches an aggressive autovacuum. This work can happen even when normal dead-tuple thresholds would not trigger autovacuum.
If PostgreSQL cannot advance the table's relfrozenxid far enough, it eventually refuses transactions that could create unsafe XIDs. The database may become effectively read-only until maintenance succeeds.
Finding freeze pressure
This query compares the oldest database-wide unfrozen XID with the current age of each database:
SELECT datname,
age(datfrozenxid) AS xid_age
FROM pg_database
ORDER BY age(datfrozenxid) DESC;A large age indicates that freezing has not advanced far enough. Investigate long-running transactions, prepared transactions, replication slots, unavailable workers, and insufficient maintenance capacity. These can prevent VACUUM from removing old row versions or advancing visibility safely.
Exercise
Exercise
What is the difference between the all-visible and all-frozen bits, and why does transaction ID freezing matter?
Show answer
The all-visible bit says every row on a page is visible to all current transactions. The all-frozen bit says every row is also frozen and no longer needs its original transaction ID. Freezing prevents old XIDs from becoming ambiguous after transaction ID wraparound.