VACUUM does not rewrite the table. It is a targeted reclamation pass:
Scan heap pages — identify tuples where xmax is committed and no running transaction can still see the old version (past the global xmin horizon).
Mark dead tuples free — set their ItemId status to LP_UNUSED in the page header. Space becomes available for new inserts into that page.
Update the Free Space Map (FSM) — the _fsm fork records free space per page; future INSERTs reuse it without extending the file.
Update planner statistics in pg_class.relpages / reltuples — this is what feeds the planner in the Reading the planner part.
Truncate trailing empty pages (briefly locks, only if possible) — the only time regular VACUUM shrinks the file.
VACUUMVERBOSEaccounts;-- INFO: vacuuming "public.accounts"
-- INFO: scanned index "accounts_pkey" to remove 4200 row versions
-- INFO: "accounts": removed 4200 row versions in 180 pages
-- INFO: "accounts": found 4200 removable, 95800 nonremovable row versions
-- INFO: free space map contains 180 pages with free space
VACUUM vs. VACUUM FULL
VACUUM
VACUUM FULL
Rewrites table
No
Yes — new heap file
Lock held
ShareUpdateExclusive (allows reads & writes)
AccessExclusive — blocks all queries
Shrinks file
Only trailing empty pages
Always, fully compacted
Index rebuild
No
Yes
Use case
Routine maintenance
Emergency bloat reduction
Autovacuum Trigger Condition
Autovacuum fires on a table when:
n_dead_tup > autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor × reltuples
Defaults:
autovacuum_vacuum_threshold = 50
autovacuum_vacuum_scale_factor = 0.2 (20%)
For a 10M-row table: 50 + 0.2 × 10,000,000 = 2,000,050 dead tuples before autovacuum fires
That is a lot of dead tuples to tolerate on a high-write table. Always tune per-table:
ALTERTABLEeventsSET(autovacuum_vacuum_scale_factor=0.01,-- trigger at 1% dead tuples
autovacuum_vacuum_threshold=1000,autovacuum_vacuum_cost_delay=2-- ms between cost-limit pauses
);