Transaction ID wraparound in PostgreSQL: what it is and how to stay far away from it
PostgreSQL's 32-bit transaction IDs must be frozen before they wrap. Here's how to see how close each database is, and what to do if it's getting near.
Every transaction that writes in PostgreSQL gets a transaction ID (XID). XIDs are 32-bit numbers, so there are about 4 billion of them, and PostgreSQL compares them in a circle: roughly 2 billion are "in the past" and 2 billion "in the future". To stop old rows suddenly looking like they come from the future, VACUUM freezes old rows, marking them as visible to everyone forever.
If freezing falls far enough behind, PostgreSQL protects your data the only way it can: it warns loudly in the log and, if it gets very close to the limit, stops accepting new write transactions until a VACUUM has run. That's the outage people mean by "wraparound".
How close are you?
Per database:
SELECT datname, age(datfrozenxid) AS xid_age,
round(100.0 * age(datfrozenxid) / 2000000000, 1) AS pct_of_limit
FROM pg_database ORDER BY 2 DESC;
Per table, in the current database:
SELECT c.oid::regclass AS table_name, age(c.relfrozenxid) AS xid_age,
pg_size_pretty(pg_total_relation_size(c.oid)) AS size
FROM pg_class c WHERE c.relkind IN ('r', 'm', 't')
ORDER BY 2 DESC LIMIT 20;
What "normal" looks like
Autovacuum starts an aggressive vacuum on a table when its age passes autovacuum_freeze_max_age (default 200 million), even if autovacuum is otherwise switched off. So on a healthy server, table ages hover in the low hundreds of millions and keep dropping back.
Reasons to look closer:
- Ages climbing well past
autovacuum_freeze_max_ageand not coming down. - Any database past roughly a billion (half-way to the limit).
- Warnings in the log saying a database "must be vacuumed within" a number of transactions.
What stops freezing
The same things that stop ordinary vacuum:
- Long-running transactions or sessions sitting idle in transaction.
- Prepared transactions nobody committed (
SELECT * FROM pg_prepared_xacts;). - Replication slots that are no longer consumed (
SELECT slot_name, active, xmin, catalog_xmin FROM pg_replication_slots;). - Autovacuum disabled on a table (
autovacuum_enabled = falsein its options). - Autovacuum throttled so hard it never finishes a big table.
Fix the blocker first, or the vacuum you run next won't be able to advance the freeze age.
Bringing a table back down
Once the blocker is gone, freeze the oldest tables yourself in a quiet period:
VACUUM (FREEZE, VERBOSE) big_table;
Then make sure autovacuum keeps up next time: re-enable it if it was off for the table, lower the per-table scale factor, and give autovacuum more I/O budget (see our autovacuum guide).
Make it boring
Wraparound is entirely preventable. It only happens when nobody watches freeze age. Check it weekly at minimum, or let a tool do it.
SQLNirvana tracks freeze age for every table and database against both autovacuum_freeze_max_age and the hard limit, and raises a critical item long before it becomes urgent.