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
VACUUMmarks 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 FULLrewrites the table into a new file, shrinking it, but takes anACCESS EXCLUSIVElock. Preferpg_repackor a rebuild strategy on busy tables.ANALYZErefreshes 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
VACUUMskip 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:
| Step | Size of b |
|---|---|
| After inserting 100,000 rows | 23 MB |
After UPDATE of every row | 46 MB (old versions kept) |
After plain VACUUM | 46 MB (space reusable, file not shrunk) |
After VACUUM FULL | 23 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 | 21524With 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 billionCounters 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_feedbackall 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_limitif 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
- Table size doubled after a mass update and
VACUUMdid not shrink it. Is something wrong? - Why does
fillfactorhelp update-heavy tables? - How do you find the transaction that stops vacuum from cleaning up?
References
- PostgreSQL manual, Routine Vacuuming: https://www.postgresql.org/docs/current/routine-vacuuming.html
- PostgreSQL manual, Heap-Only Tuples: https://www.postgresql.org/docs/current/storage-hot.html