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.
xmin 752 is 150,000,000 transactions in the past: visible.
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:
| lp | ctid | xmin | xmax | t_ctid | id | owner |
|---|---|---|---|---|---|---|
| (0,1) | 758 | 0 | (0,1) | 1 | alice | |
| (0,2) | 758 | 0 | (0,2) | 2 | bob | |
| (0,3) | 758 | 0 | (0,3) | 3 | carol |
VACUUM (FREEZE). every tuple gets the frozen bits, xmin stays as it was, the page is all-frozen, and relfrozenxid jumps to the present.
relfrozenxid, the table's "everything older than this is frozen" mark, went 757 → 758 → 759, and age(relfrozenxid) from 2 to 1 to 0.
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:
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;| datnamename | datfrozenxidxid | ageint4 |
|---|---|---|
| postgres | 744 | 9 |
| template0 | 744 | 9 |
| template1 | 744 | 9 |
| relnamename | relfrozenxidxid | ageint4 |
|---|---|---|
| accounts | 752 | 1 |
| notes | 752 | 1 |
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:
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');| lpint2 | t_xminxid | t_infomaskint4 | combined_flagstext[] |
|---|---|---|---|
| 1 | 752 | 2818 | {HEAP_XMIN_FROZEN} |
| 2 | 752 | 2818 | {HEAP_XMIN_FROZEN} |
| 3 | 752 | 2818 | {HEAP_XMIN_FROZEN} |
| relfrozenxidxid | ageint4 |
|---|---|
| 753 | 0 |
| all_visiblebool | all_frozenbool |
|---|---|
| t | t |
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_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:
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:
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.