These are among the scariest messages PostgreSQL can print:
WARNING: database "app" must be vacuumed within 38123456 transactions
HINT: To avoid a database shutdown, execute a database-wide VACUUM in that database.
and, if nobody acts:
ERROR: database is not accepting commands that assign new transaction IDs to avoid
wraparound data loss in database "app"
At that point writes stop and the database is effectively read-only. It's preventable, and if it happens, it's recoverable. Here's what's going on.
Why transaction IDs wrap around
Every writing transaction gets a 32-bit transaction ID (XID), and row versions record the XID that created them. With 32 bits, only about 2 billion XIDs can be "in the past" at any moment; beyond that, old rows would suddenly look like they were created in the future, and become invisible.
PostgreSQL prevents this by freezing old rows: VACUUM marks them as visible to everyone, so their XID no longer matters. Autovacuum normally does this in the background. It forces an anti-wraparound vacuum on any table whose oldest unfrozen XID is older than autovacuum_freeze_max_age (200 million by default).
The warnings start when fewer than about 40 million XIDs remain before the hard limit. The shutdown of new XIDs happens when about 3 million remain, to protect the data.
Check how close you are
-- Per database
SELECT datname, age(datfrozenxid) AS xid_age
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 (, , )
age(c.relfrozenxid)
LIMIT ;
