1.VACUUM, VACUUM FULL and ANALYZE
VACUUM reclaims dead tuple space for reuse. VACUUM FULL rewrites the whole table to reclaim space AND shrink the file, at the cost of an exclusive lock. ANALYZE is a separate job entirely: it collects statistics about a table's data distribution so the query planner can make good decisions — it does not touch dead tuples at all. VACUUM ANALYZE runs both together in one pass.
Key features
- VACUUM: reclaims dead tuple space for reuse within the table, does not shrink the file, requires no exclusive lock
- VACUUM FULL: rewrites the table into a new, compact file, does shrink it and returns space to the OS, requires an ACCESS EXCLUSIVE lock
- ANALYZE: collects statistics (row count estimate, most-common values, histograms) the query planner uses — completely separate concern from dead tuples
- VACUUM ANALYZE (or just letting autovacuum handle both) runs both operations together in a single table scan
- Stale statistics from a missing ANALYZE can cause bad query plans even on a table with zero bloat — the two problems are independent
VACUUM vs VACUUM FULL vs ANALYZE
- VACUUM / VACUUM FULL
- Reclaims dead tuple space
- VACUUM: reuse only, no lock; FULL: reuse + shrink, ACCESS EXCLUSIVE lock
- Never touches planner statistics
- ANALYZE
- Refreshes row-count and value-distribution statistics
- The query planner reads these to choose a plan
- Never touches dead tuples
Command
VACUUM accounts; -- reclaim dead tuple space for reuse
ANALYZE accounts; -- refresh planner statistics
VACUUM ANALYZE accounts; -- both, in one pass
VACUUM FULL accounts; -- reclaim AND shrink -- rare, blockingVACUUM reclaims dead tuple space for reuse (not shrink). VACUUM FULL reclaims AND shrinks, at the cost of an ACCESS EXCLUSIVE lock. ANALYZE refreshes planner statistics — a completely separate concern from dead tuples. A table can need one badly without needing the other at all.