Overview

VACUUM reclaims space from dead row versions left by MVCC (pg-mvcc), updates the visibility map and freezes old transaction ids. Autovacuum runs it automatically. Mis-tuned or blocked vacuum is one of the most common causes of PostgreSQL performance decay.

Core concepts

  • Plain VACUUM marks dead space reusable inside the table and does not return it to the operating system (except trailing empty pages). It runs concurrently with reads and writes.
  • VACUUM FULL rewrites the table into a new file, shrinking it, but takes an ACCESS EXCLUSIVE lock. Prefer pg_repack or a rebuild strategy on busy tables.
  • ANALYZE refreshes planner statistics (query-planning); autovacuum runs it too.
  • Freezing marks very old rows as visible to everyone so transaction id wraparound cannot hide them.
  • Visibility map lets VACUUM skip all-visible pages and enables index-only scans (indexing-theory).
  • HOT updates (heap-only tuples): if no indexed column changes and the page has free room, the new version stays on the same page with no new index entries, and page pruning can reclaim it cheaply. fillfactor < 100 leaves room for this.

Autovacuum triggers when dead tuples exceed autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor * reltuples (defaults 50 and 0.2, so 20% of the table). Other defaults: three workers, autovacuum_naptime one minute, autovacuum_freeze_max_age 200 million.

Worked example

Verified on PostgreSQL 17, a table b of 100,000 rows with a 200-byte column:

StepSize of b
After inserting 100,000 rows23 MB
After UPDATE of every row46 MB (old versions kept)
After plain VACUUM46 MB (space reusable, file not shrunk)
After VACUUM FULL23 MB

HOT in practice, on a 50,000-row table updated on every second row:

CREATE TABLE fh(id int PRIMARY KEY, pad text) WITH (fillfactor = 70);
-- ... load, then: UPDATE fh SET pad = 'y' WHERE id % 2 = 0;
SELECT n_tup_upd, n_tup_hot_upd FROM pg_stat_user_tables WHERE relname = 'fh';
-- 25000 | 21524

With fillfactor = 70, about 86% of the updates were HOT; the same update on the completely full table b produced no HOT updates because its pages had no free room.

Monitoring queries:

SELECT relname, n_live_tup, n_dead_tup, last_autovacuum, last_autoanalyze
FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;
 
SELECT datname, age(datfrozenxid) FROM pg_database ORDER BY 2 DESC;  -- alert far below 2 billion

Counters in pg_stat_user_tables update asynchronously, so they may lag a statement or two.

Pitfalls and production notes

  • Vacuum cannot remove versions that an old snapshot can still see. Long transactions, abandoned replication slots, prepared transactions and hot_standby_feedback all pin the horizon. Fix the cause, not the vacuum settings.
  • The default 20% scale factor is too lazy for very large tables; set per-table values, for example ALTER TABLE big SET (autovacuum_vacuum_scale_factor = 0.01).
  • Autovacuum throttles itself with cost limits; on fast storage raise autovacuum_vacuum_cost_limit if it falls behind.
  • Bloated indexes need REINDEX CONCURRENTLY.
  • Never disable autovacuum globally; wraparound protection will force an aggressive vacuum at the worst time.
  • Bulk delete + reinsert patterns bloat; consider partitioning and dropping partitions (table-partitioning).

Practice questions

  1. Table size doubled after a mass update and VACUUM did not shrink it. Is something wrong?
  2. Why does fillfactor help update-heavy tables?
  3. How do you find the transaction that stops vacuum from cleaning up?

References

pg-mvcc · pg-monitoring · pg-tuning · table-partitioning