ParatuTech
Database journalPostgreSQL

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.

01 A version is still needed
Old snapshot remainsSnapshot opened earlierMay still need the old row versionOld version v1New version v2UPDATE creates a newer versionKeep v1 while it may be visible
02 It becomes eligible
Snapshot blocker endsRecheck retention horizonsOther blockers may still matterOld version v1New version v2VACUUM can reclaim eligible v1
Illustrative MVCC visibility, not transaction IDs or a complete blocker inventory. An old snapshot can hold back row-version reclamation. Once all relevant horizons allow it, VACUUM can reclaim eligible tuples for reuse; ordinary VACUUM generally does not shrink the relation file.

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.

Discuss PostgreSQL maintenance with ParatuTech.

A considered next step

Bring the workload, the symptoms and the evidence. We’ll help you frame the next decision.

Discuss your database