Sub topic 15
Transaction ID Wraparound and Freezing
This chapter assumes basic SQL, transactions, and the ability to use psql in one or more sessions. You should also understand that PostgreSQL stores multiple row versions and uses snapshots to decide which versions are visible.
Why transaction IDs can wrap around
PostgreSQL assigns transaction IDs, or XIDs, to transactions that need them. An XID is a 32-bit value, so the counter eventually reaches its maximum and starts again at the beginning.
Reusing an old number is safe only if PostgreSQL can distinguish the new transaction from the old one. PostgreSQL compares XIDs relative to the current transaction, so an XID more than about 2 billion transactions away can appear to be from the future. This is called transaction ID wraparound. If it occurs, old rows could appear visible or invisible incorrectly.
PostgreSQL prevents this by freezing old row versions before they become dangerous.
What freezing means
A row version normally stores the XID that created it in its xmin field. When PostgreSQL freezes a sufficiently old row version, it replaces the need to interpret that old XID with a special frozen status. A frozen row is treated as visible to every transaction that could matter.
Freezing does not remove the row, and it does not make the row permanently unchangeable. A later UPDATE still creates a new row version. Freezing only removes the old version's dependence on the finite XID counter.
The table's relfrozenxid records the oldest XID that might still need normal interpretation in that table. The database's datfrozenxid is based on the oldest such value among its tables.
Vacuum and anti-wraparound protection
Regular VACUUM freezes old tuples as part of its work. Autovacuum also monitors transaction age. When a table approaches autovacuum_freeze_max_age, PostgreSQL schedules an anti-wraparound vacuum even if ordinary dead-tuple activity is low.
You can inspect the oldest database-wide transaction age:
SELECT datname,
age(datfrozenxid) AS xid_age
FROM pg_database
ORDER BY xid_age DESC;You can inspect individual tables:
SELECT schemaname,
relname,
age(relfrozenxid) AS xid_age,
last_autovacuum
FROM pg_stat_all_tables
WHERE relkind = 'r'
ORDER BY age(relfrozenxid) DESC
LIMIT 20;A manual freeze can be useful during maintenance:
VACUUM (VERBOSE, FREEZE) public.orders;The exact danger threshold depends on PostgreSQL settings and version. Do not wait for an emergency. A database that reaches the wraparound protection limit can refuse new transactions until maintenance advances the freeze horizon.
Long transactions and monitoring
A long-running transaction can keep old snapshots alive. Vacuum may then be unable to remove or fully process row versions that the snapshot could still need. Long transactions can therefore delay progress and make wraparound prevention more difficult.
Check sessions that hold an old transaction horizon:
SELECT pid,
state,
xact_start,
backend_xmin,
age(backend_xmin) AS xmin_age
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY age(backend_xmin) DESC;Investigate idle transactions, stuck replication slots, and sessions that remain open for hours or days. Fixing the holder is usually safer than repeatedly increasing maintenance effort.
Exercise
What problem does freezing solve, and which table value helps PostgreSQL track whether a table still contains very old XIDs?
Show answer
Freezing prevents old row versions from depending on XID values that may wrap around. The table's relfrozenxid tracks the oldest XID that may still require normal interpretation.