← Concepts

阅读中文版本

Autovacuum and Dead Tuple Reclamation

Autovacuum reclaims space from dead row versions left by MVCC updates, and tuning its thresholds prevents bloat and transaction ID wraparound.

database

Definition

Autovacuum is the background process that removes row versions made obsolete by updates and deletes, refreshes planner statistics, and advances the visibility map so indexes can be scanned more cheaply.

Context and scope

It is the operational counterpart of the multi-version concurrency control model used by PostgreSQL and similar engines.

Common misconceptions

  • vacuum rewrites tables into a compact form immediately
  • disabling autovacuum improves performance on a busy table

Anti-patterns

  • disabling autovacuum on high-churn tables to avoid the overhead
  • setting thresholds so high that a large table is never vacuumed

Related concepts

Related examples

Authoritative source

https://www.postgresql.org/docs/current/routine-vacuuming.html

Definition

Autovacuum is the background process that removes row versions made obsolete by updates and deletes, refreshes planner statistics, and advances the visibility map so indexes can be scanned more cheaply.

It is the operational counterpart of the multi-version concurrency control model used by PostgreSQL and similar engines. Compared to simpler alternatives, this concept fits best when the trade-off it represents is the one the system actually cares about.

When to use it

A common use case is applying it at the boundary where the cost of getting it wrong is highest, and the scenario usually involves a concrete guarantee the system must uphold. Unlike an ad hoc approach, it makes that guarantee explicit, reviewable, and testable.

How it works

Every update creates a new row version and marks the old one dead; without reclamation the table grows even when the row count is constant, a condition called bloat.

Plain vacuum only makes space reusable inside the table, whereas vacuum full rewrites it and takes an exclusive lock, so routine maintenance relies on the former.

Trade-offs and alternatives

The visibility map and the free space map shorten index-only scans and speed future inserts, which is why monitoring vacuum lag is as important as monitoring query time. The main drawback is the extra complexity it introduces, so the benefit should clearly outweigh that cost before a team adopts it.

Common misconceptions

A persistent misconception is that vacuum rewrites tables into a compact form immediately. Another is that disabling autovacuum improves performance on a busy table. For example, an engineer who assumes otherwise may design around a guarantee that does not actually exist, which only becomes obvious under load or failure.

Anti-patterns to avoid

Two recurring anti-patterns are to disabling autovacuum on high-churn tables to avoid the overhead and to setting thresholds so high that a large table is never vacuumed. Both tend to surface as hard-to-debug production issues rather than obvious test failures. Related concepts include LATERAL Joins, Upsert with INSERT … ON CONFLICT, The RETURNING Clause.