Bloat, VACUUM & ANALYZE in SQL
Dead versions slow scans; stats steer plans
MVCC updates/deletes leave dead row versions indexes still point at — bloat slows scans and wastes space. VACUUM reclaims dead space and refreshes visibility maps (enabling index-only scans); ANALYZE refreshes planner statistics; autovacuum handles steady state, manual tuning handles bulks. Bloated indexes show as slow scans with healthy-looking plans.
MVCC leaves dead
UPDATE/DELETE create new versions; old ones linger as dead tuples indexes still chase.
VACUUM reclaims
Frees pages, refreshes visibility maps — index-only scans stop checking heap.
ANALYZE teaches planner
Fresh histograms fix 10×+ misestimates. Autovacuum steady-state; manual after bulks.
Confirm with evidence
Healthy-looking slow scans + pgstattuple bloat = VACUUM case.
1VACUUM (ANALYZE) employees;
| job | effect |
|---|---|
| reclaim | dead space freed |
| stats | planner sees true distribution |
Bulk UPDATE left three dead versions per hot row. VACUUM frees the pages and marks all-visible; ANALYZE teaches the planner the new salary histogram for the next EXPLAIN.
Post-bulk VACUUM ANALYZE
Reclaim + reteach after loads.
1VACUUM (ANALYZE) employees;
| job | effect |
|---|---|
| reclaim | dead freed |
| stats | estimates trusted |
Bloat vs missing-index signal
Same slowness, different cures.
1-- bloat: slow scan, good plan, pgstattuple high dead2-- missing index: Seq Scan where Index Scan belongs (EXPLAIN)
| signal | cure |
|---|---|
| bloat | VACUUM |
| no index | CREATE INDEX |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.