PostgreSQL Enterprise (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
What is the Transaction ID (XID) Wraparound problem and how does Freeze VACUUM prevent it?
senior LevelDatabases & SQLPostgreSQL
In Simple Words:
PostgreSQL uses a 32-bit integer for Transaction IDs (xmin/xmax), supporting $2^{32} \approx 4.29 \text{ billion}$ transactions:
- XIDs wrap around in a circle. Two transactions are compared using modulo arithmetic: a transaction is "in the past" if it is within 2 billion IDs, and "in the future" otherwise.
- The Wraparound Disaster: If a database runs 2 billion transactions without maintenance, historical transactions wrap around to the "future", making all historical table data instantly vanish!
- Prevention (Freezing): Autovacuum performs Aggressive Freeze VACUUM, replacing ancient
xminvalues with a special frozen transaction ID (FrozenTransactionId = 2), marking rows as permanently visible in the past forever.