Your Postgres logs are warning that the database must be vacuumed within some number of transactions. That is a countdown. Ignore it and the cluster stops accepting writes.
PostgreSQL identifies every transaction with a 32-bit ID. There are only about two billion usable values, laid out on a circle, and each row remembers the transaction that wrote it so the database can decide what each session may see. Rows that get old enough are frozen — marked as visible to everyone — which retires their transaction ID and keeps the circle from closing on itself. Freezing is autovacuum’s job. When autovacuum falls behind, the oldest unfrozen ID drifts further and further from the newest one, and Postgres starts warning you before the gap becomes unrecoverable.
The message looks like this:
WARNING: database "app_prod" must be vacuumed within 10985967 transactions
HINT: To avoid a database shutdown, execute a database-wide VACUUM in that database.
You might also need to commit or roll back old prepared transactions,
or drop stale replication slots.
Do not clear the log and move on. The number only goes down. This is the fix, in the order I run it under pressure.
Find what is holding the horizon back
The frozen horizon of the whole database is dragged backwards by whichever table has the oldest relfrozenxid. You do not need to vacuum everything — you need to find the one or two tables that are behind. The age() function tells you how many transactions old a frozen ID is; the closer to the two-billion ceiling, the more urgent.
-- Databases, ordered by how far behind their frozen horizon is
SELECT datname, age(datfrozenxid) AS xid_age
FROM pg_database
ORDER BY xid_age DESC;
-- Within the offending database: the tables actually to blame
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') -- tables, matviews, TOAST
ORDER BY xid_age DESC
LIMIT 20;
Anything with an age approaching autovacuum_freeze_max_age (default 200 million) should already be getting an anti-wraparound autovacuum. Anything approaching 2 billion is the reason you are reading this. Note the table names at the top of that list — those are your targets.
Freeze the offenders, do not vacuum the world
A plain VACUUM will not necessarily advance relfrozenxid as far as you need. Ask for an aggressive freeze explicitly. Run it against the specific tables from the query above, starting with the oldest:
-- Targeted, in psql, against the worst offender first
VACUUM (FREEZE, VERBOSE) public.events;
VACUUM (FREEZE, VERBOSE) public.audit_log;
If several tables are behind and you have spare I/O, vacuumdb parallelises across tables and keeps you out of a single long-running psql session:
# One heavy table, foreground
vacuumdb --freeze --table=public.events --dbname=app_prod --verbose
# Whole database, four workers in parallel
vacuumdb --all --freeze --jobs=4 --analyze --verbose
Watch the age() query fall as the freeze completes. On a large, bloated table this can take hours and hammer disk — but a freezing vacuum is throttled by the same cost settings as routine autovacuum, so it will not saturate a busy production box unless you raise those limits (which, mid-incident, you should — see below). If the machine is memory-starved and swapping while the vacuum runs, that is its own problem worth looking at your swap configuration for.
If it already went read-only
If you missed the warnings, the database refuses to assign new transaction IDs and you see this on every write:
ERROR: database is not accepting commands that assign new XIDs
to avoid wraparound data loss in database "app_prod"
HINT: Execute a database-wide VACUUM in that database.
This is far less dramatic than the folklore suggests. On modern PostgreSQL you do not need to stop the postmaster or drop into single-user mode. The server deliberately keeps a safety margin of transaction IDs in reserve precisely so an administrator can connect and run the vacuum that fixes it. Connect as normal and run the targeted VACUUM (FREEZE) above. To avoid extra work while the clock is against you, skip the truncate phase:
VACUUM (FREEZE, TRUNCATE OFF, VERBOSE) public.events;
Two things commonly hold the horizon hostage even after you vacuum: a long-idle transaction pinning old IDs, and an abandoned replication slot or prepared transaction. Check pg_stat_activity for ancient xact_start values, pg_replication_slots for inactive slots, and pg_prepared_xacts for orphaned two-phase commits. Vacuum cannot advance past whatever the oldest of those is holding.
Why autovacuum fell behind — and stopping it recurring
Reaching this point means autovacuum could not keep pace with the write rate. Usually one of: too few workers for the number of large tables, cost limits throttling it into irrelevance on a busy server, a per-table autovacuum_enabled = false someone set years ago and forgot, or a persistent long-running transaction that repeatedly blocks freezing. The failure mode is quiet, which is exactly why it bites.
Once you are out of the woods, give autovacuum more room so it never approaches the ceiling again:
-- More headroom before an anti-wraparound vacuum is forced
-- (requires a server restart; max permitted is 2000000000)
ALTER SYSTEM SET autovacuum_freeze_max_age = '1000000000';
-- Let autovacuum actually move on a busy box (reloadable)
ALTER SYSTEM SET autovacuum_vacuum_cost_limit = '2000';
ALTER SYSTEM SET autovacuum_vacuum_cost_delay = '2ms';
-- More workers for many large tables (requires a restart)
ALTER SYSTEM SET autovacuum_max_workers = '5';
-- See what autovacuum is doing, in the log (reloadable)
ALTER SYSTEM SET log_autovacuum_min_duration = '0';
SELECT pg_reload_conf(); -- applies the reloadable ones now
Raising autovacuum_freeze_max_age buys headroom but does not do the work — it only delays the forced vacuum, so it is worthless unless autovacuum is also fast enough to use the extra room. The two levers that matter are the cost limit and the worker count. For a genuinely write-heavy table, per-table storage parameters beat cluster-wide defaults, which I have written up separately in autovacuum tuning for a write-heavy Postgres table.
Then add the one alert that would have made this a non-event: page when max(age(datfrozenxid)) across your databases crosses, say, 500 million. That is a single cheap query on a schedule. Transaction ID wraparound is never a surprise — it is a warning you did not have anyone watching for.
Free interactive tool
Website compliance checklist
What your site has to do, based on what it actually does
Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.
Everything that applies
Ordered by what to do first: legal requirements you can close quickly, then larger pieces of work, then what is expected rather than required. Not exhaustive, and not a legal audit.
Dated PDF, yours to keep or circulate.
Most technology problems are not technology problems. They are control problems.
The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.