Transaction ID Wraparound

The 32-Bit XID Space

Postgres assigns every read-write transaction a 32-bit transaction ID (XID). The counter increments monotonically from 3. With 32 bits, the maximum is 4.3 billion — but Postgres does not treat XIDs as a linear number line. It treats the XID space as a circle:

If XID_B - XID_A > 2^31:  A is "newer" than B  (wrap has occurred)
If XID_B - XID_A < 2^31:  B is "newer" than A  (normal order)

→ Any XID can "see" ~2.1 billion XIDs ahead as future,
  and ~2.1 billion behind as past.
XID Space — circular, 2^32 total, sliding 2^31 window
XID Space — circular, 2^32 total, sliding 2^31 window

Freezing: How Postgres Stays Ahead of Wraparound

As the XID counter approaches current_xid + 2^31, Postgres needs to ensure old tuples remain visible. It does this by freezing them — replacing their xmin with the special value FrozenTransactionId (XID 2), which is always considered "in the past" by every transaction, forever.

GUCDefaultMeaning
autovacuum_freeze_max_age200,000,000When a tuple's xmin is this many transactions behind current XID, autovacuum freezes it even if there are no dead tuples.
pg_class.relfrozenxidper tableThe oldest non-frozen XID still present in the table. VACUUM updates this after a freeze pass.
pg_database.datfrozenxidper databaseThe minimum of all relfrozenxid values across all tables in the DB.
-- How far are we from wraparound? Add to your DBA runbook.
SELECT datname,
       age(datfrozenxid)                  AS xid_age,
       2147483647 - age(datfrozenxid)     AS xids_remaining
FROM pg_database
ORDER BY xid_age DESC;

-- Danger zones:
--   xid_age > 1,500,000,000  → investigate aggressively
--   xid_age > 1,900,000,000  → Postgres warns in logs
--   xid_age > 2,000,000,000  → Postgres refuses new transactions

-- Also check table-level:
SELECT relname, age(relfrozenxid) AS table_xid_age
FROM pg_class
WHERE relkind = 'r'
ORDER BY table_xid_age DESC LIMIT 20;

What "Wraparound" Actually Means

If age(datfrozenxid) ever exceeds 2^31 (~2.1 billion), old tuples that were previously visible — because their xmin was "in the past" — are now mathematically "in the future." Every row in those pages suddenly becomes invisible to all queries.

-- The error you never want to see in production
FATAL: database is not accepting commands to avoid wraparound data loss

The database does not corrupt silently — it enters a protective read-only shutdown, refusing all new transactions until you run an offline VACUUM FREEZE on every table. This can take hours on large databases.