PostgreSQL · Maintenance
PostgreSQL autovacuum: check the oldest horizon before tuning
See why old snapshots and replication horizons can retain row versions, then separate cleanup limits from vacuum capacity.
A PostgreSQL table can keep growing while autovacuum is running. Before increasing workers or reducing thresholds, check whether old row versions are eligible for removal. More maintenance capacity cannot override a snapshot that still needs those versions.
The short version
What to take away
- Vacuum must retain row versions that another transaction may still need.
- Inspect transaction and replication horizons before changing autovacuum settings.
- Dead-row estimates and table size alone do not prove the cause of retained space.
Follow a row through the horizon
Imagine a reader holding an older snapshot. Another transaction updates a row and commits its new version. The reader may still need the original version, so vacuum must retain it. Once no relevant transaction or replication horizon requires that version, a later vacuum can reclaim its space.
Normal vacuum generally makes space reusable inside the table. It does not normally return that space to the operating system. VACUUM FULL rewrites the table and requires an ACCESS EXCLUSIVE lock, so it needs a separately planned maintenance decision. Routine vacuuming.
Gather evidence before changing settings
Start with a read-only review of the affected tables and sessions:
- Compare estimated dead rows, recent vacuum activity and growth over time.
- Inspect transaction start times, session states, backend_xid and backend_xmin in pg_stat_activity.
- Identify the application or job responsible for a potentially old horizon, and ask whether its transaction is expected to remain open.
- If two-phase transactions are used, inspect old entries in pg_prepared_xacts and identify their coordinator before any intervention.
Connection age and transaction age are clues. Confirm the relevant XID or xmin horizon instead of assuming every open transaction retains an old snapshot. Likewise, n_dead_tup is an estimate, not a precise measurement of wasted bytes. Statistics can also lag and remain cached within a monitoring transaction. Activity and table statistics.
Avoid terminating sessions as a diagnostic shortcut. First understand the work they own and the consequences of interrupting it.
Include replication in the investigation
In pg_replication_slots, xmin can retain row versions and catalog_xmin can retain catalog versions. The slot's restart_lsn concerns WAL retention, which is a separate dependency. Review the consumer and its purpose before considering slot changes. Replication-slot horizons.
Standby feedback can protect standby queries from cleanup conflicts while contributing to bloat on the primary for some workloads. That trade-off needs evidence from both systems. Hot standby feedback.
If horizons are progressing normally, investigate autovacuum scheduling, thresholds and available resources. Change one justified factor, then compare cleanup progress and application impact.
Sources and next step
This checklist uses PostgreSQL 18 documentation. Adapt it to the installed version and operational constraints.