skip to content

freezing and wraparound

32-bit transaction ids go round in a circle. freezing, age(), and the shutdown nobody wants.

address
everything.mvcc.freezing_and_wraparound
part
part 14, MVCC and vacuum
chapter
chapter 10 of 12
reading time
8 min

transaction ids are 32 bits, so they wrap around after about 4 billion. postgres compares them on a circle: for any xid, the 2 billion before it are the past and the 2 billion after it are the future. a tuple whose xmin drifts more than 2 billion behind would suddenly look like it's from the future, and vanish. freezing prevents that: VACUUM marks old tuples as older than everything, and age(relfrozenxid) tracks how far behind each table's oldest unfrozen xid is.

the picture

you know how every tuple carries its xmin? that's a 32-bit number, and a busy server can burn through a few thousand xids a second. 4 billion goes faster than it sounds. postgres can't make the field bigger without making every tuple bigger, so it lets the numbers wrap, and compares them modulo 2³².

think of a clock face with 4 billion marks. "now" is the hand. the half circle behind the hand is the past, the half in front is the future. as the hand moves round, a mark that was in the past slides, one day, into the future. unless you've stamped the tuple "older than any clock" first: that's freezing.

current xid =
150,000,752
the row =
2³² xids, wrappingpastfuture
age(xmin) = 150,000,000 · autovacuum freezes past 200,000,000

xmin 752 is 150,000,000 transactions in the past: visible.

the past: visible xidscurrent xidfreeze age linethe row, once it wraps
model of 32-bit xid comparison (modulo 2³², 2³¹ each way), drawn from the postgres 18 docs
a model, not a capture. drag the current xid round the circle. the row's xmin stays put; once the hand is more than 2³¹ ahead of it, it lands in the future half and the row would vanish. switch the row to frozen and it's safe at any angle. the tick is autovacuum_freeze_max_age, where an anti-wraparound vacuum would have come for it long before.

the 64-bit view exists too: pg_current_xact_id() returns an xid8 with an epoch counter in the high bits, and that one never wraps. but tuples store only the low 32 bits, and that's the number that has to be dealt with.

what freezing does to a tuple

a frozen tuple's xmin no longer means anything: it's visible to every snapshot, forever, without looking at the commit log. on disk it's two hint bits set at once, HEAP_XMIN_COMMITTED and HEAP_XMIN_INVALID, which together read as HEAP_XMIN_FROZEN. here's a fresh three-row table on a real server, from load to frozen:

the page =
byte 0 header line pointerstuples, byte 8,192
lower 36free 8,036 B, unlitupper 8072
lpctidxminxmaxt_ctididowner
(0,1)7580(0,1)1alice
(0,2)7580(0,2)2bob
(0,3)7580(0,3)3carol
tuple (0,1) · off 8152 · len 39 B
t_xmin758 committed
t_xmax0 nobody deleted it
t_ctid(0,1) itself: the newest version
infomaskHASVARWIDTHXMIN_COMMITTEDXMIN_INVALIDXMAX_INVALIDXMIN_FROZEN
t_hoff24 B
dataid 1owner alicebalance 100.00
headerline pointer → tuplefree space, unlitdeadin progresswhat this step changed
real postgres 18.6 output · a native server · traces/freeze

VACUUM (FREEZE). every tuple gets the frozen bits, xmin stays as it was, the page is all-frozen, and relfrozenxid jumps to the present.

switch moments and watch slot 1's infomask. after the load: no hints. after a plain VACUUM: XMIN_COMMITTED, and the page is all-visible, but young tuples aren't frozen. after VACUUM (FREEZE): XMIN_COMMITTED and XMIN_INVALID together, which pageinspect reports as XMIN_FROZEN.

relfrozenxid, the table's "everything older than this is frozen" mark, went 757 → 758 → 759, and age(relfrozenxid) from 2 to 1 to 0.

predict the output
after VACUUM (FREEZE), what's in a frozen tuple's t_xmin?
sql
VACUUM (FREEZE) accounts;
SELECT t_xmin FROM heap_page_items(get_raw_page('accounts', 0));

the same in the browser, on lab, with the whole database's numbers too:

sqldb=lab · session frz
SELECT datname, datfrozenxid, age(datfrozenxid) FROM pg_database ORDER BY datname;
SELECT relname, relfrozenxid, age(relfrozenxid) FROM pg_class
WHERE relname IN ('accounts', 'notes') ORDER BY relname;
output, from postgres 18.6
#1 · SELECT 3
datnamenamedatfrozenxidxidageint4
postgres7449
template07449
template17449
(3 rows)
#2 · SELECT 2
relnamenamerelfrozenxidxidageint4
accounts7521
notes7521
(2 rows)

datfrozenxid is the minimum relfrozenxid of every table in the database, so the database is only as young as its oldest table. now freeze accounts:

sqldb=lab · session frz
continues session frz (1 earlier block run first)
VACUUM (FREEZE) accounts;
SELECT lp, t_xmin, t_infomask, (heap_tuple_infomask_flags(t_infomask, t_infomask2)).combined_flags
FROM heap_page_items(get_raw_page('accounts', 0)) ORDER BY lp;
SELECT relfrozenxid, age(relfrozenxid) FROM pg_class WHERE relname = 'accounts';
SELECT all_visible, all_frozen FROM pg_visibility_map('accounts');
output, from postgres 18.6
#1 · VACUUM
#2 · SELECT 3
lpint2t_xminxidt_infomaskint4combined_flagstext[]
17522818{HEAP_XMIN_FROZEN}
27522818{HEAP_XMIN_FROZEN}
37522818{HEAP_XMIN_FROZEN}
(3 rows)
#3 · SELECT 1
relfrozenxidxidageint4
7530
(1 row)
#4 · SELECT 1
all_visibleboolall_frozenbool
tt
(1 row)

t_xmin still 752, HEAP_XMIN_FROZEN in the combined flags, relfrozenxid moved up to the present, and the page is all-frozen in the visibility map. that last bit means no vacuum ever has to read this page for freezing again, until something changes on it.

the ladder of ages

vacuum doesn't freeze every tuple the moment it can: a tuple that's about to be updated would be frozen for nothing. instead there's a ladder of ages, each a setting, each with a default a real server reports:

vacuum_freeze_min_age50,000,000 xids
vacuum_freeze_table_age150,000,000 xids
autovacuum_freeze_max_age200,000,000 xids
vacuum_failsafe_age1,600,000,000 xids
xid assignment stops2,144,483,648 xids
real output, a postgres 18.6 server · vacuum-settings
the freezing ages for transaction ids, from pg_settings on a postgres 18.6 server, and the point (2³¹ minus three million) where postgres stops handing out xids. everything left of autovacuum_freeze_max_age is routine.
vacuum_freeze_min_age
any vacuum freezes the tuples it sees that are older than this. postgres 18 also lets an ordinary vacuum freeze some all-visible pages early, a little at a time ("eagerly scanned" in VACUUM VERBOSE) pg 18+.
vacuum_freeze_table_age
once a table's relfrozenxid is this old, its next vacuum is aggressive: it reads every page that isn't all-frozen, so relfrozenxid can move.
autovacuum_freeze_max_age
past this, autovacuum vacuums the table to prevent wraparound, whatever its other settings, even with autovacuum off.
vacuum_failsafe_age
past this, a running vacuum stops being polite: no cost delay, no index vacuuming, no ring buffer, just freeze and advance relfrozenxid as fast as it can.

the shutdown nobody wants

if all of that fails (autovacuum disabled, workers always busy, or a transaction left open for weeks so nothing can be frozen), the database counts down. at 40 million xids from wraparound every session starts getting warnings:

text
WARNING:  database "mydb" must be vacuumed within 39985967 transactions
HINT:  To avoid XID assignment failures, execute a database-wide VACUUM in that database.

and at three million it refuses to assign any more xids at all. reads keep working; anything that writes gets:

text
ERROR:  database is not accepting commands that assign new transaction IDs to avoid wraparound data loss in database "mydb"
HINT:  Execute a database-wide VACUUM in that database.

(both quoted from the postgres 18 manual, routine vacuuming.) it's an outage, on purpose: the alternative is rows that silently disappear. the way out is the same as the way to avoid it: end the old transactions, prepared transactions and replication slots holding the horizon back, then a plain database-wide VACUUM. not VACUUM FULL, which needs an xid itself.

the commit log only needs to remember xids back to the oldest datfrozenxid, so freezing also lets pg_xact throw old segments away (the commit log). and there's a second counter with exactly the same problem, for row locks shared by several transactions: MultiXacts.

keylines

  • 32-bit xids wrap; postgres compares them on a circle, 2 billion back and 2 billion ahead.
  • freezing sets two infomask bits that mean "older than everything". xmin itself stays.
  • relfrozenxid per table, datfrozenxid per database: watch their age().
  • min age, table age, max age, failsafe: a ladder from "freeze if handy" to "drop everything and freeze".
  • three million xids from the edge, postgres stops accepting writes rather than lose rows.