Autovacuum Is Not a Garbage Collector

2026-07-19databases

The mental model most people have of VACUUM is that it reclaims space from deleted rows, like a garbage collector. That model is wrong in a way that causes real outages. VACUUM exists because PostgreSQL's MVCC keeps multiple row versions on disk, and old versions must be marked as removable before their space can be reused. It is about visibility, and space reclamation is a side effect.

Why the distinction matters operationally

If VACUUM is a garbage collector, then it is an optimization: it runs when convenient, and skipping it costs disk space. If VACUUM is a visibility mechanism, then it is mandatory: skipping it costs correctness, and the failure is transaction ID wraparound, which takes the database read-only until a full vacuum completes.

Wraparound is not theoretical. It happens on systems with long-running transactions, replication slots that nobody monitors, or autovacuum that has been effectively disabled by a per-table setting someone added to reduce I/O. The last case is the most common, and the least understood.

The settings that actually matter

Scaling autovacuum workers is usually the wrong fix. What matters more:

A replication slot with a stale xmin is a slow-motion wraparound. It prevents vacuum from removing any row version newer than the slot's position. If the slot belongs to a replica that has been down for a week, you are accumulating dead tuples across the entire cluster.

The one query to run weekly

Query the maximum transaction ID age across all databases and alert well before it approaches two billion. The cost of the query is zero. The cost of discovering wraparound during a busy Tuesday is not.

← All posts