Learn SQL
-- performance.vacuum_bloat_analyze

Bloat, VACUUM & ANALYZE in SQL

Dead versions slow scans; stats steer plans

--concept

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.

--how it works
1

MVCC leaves dead

UPDATE/DELETE create new versions; old ones linger as dead tuples indexes still chase.

2

VACUUM reclaims

Frees pages, refreshes visibility maps — index-only scans stop checking heap.

3

ANALYZE teaches planner

Fresh histograms fix 10×+ misestimates. Autovacuum steady-state; manual after bulks.

4

Confirm with evidence

Healthy-looking slow scans + pgstattuple bloat = VACUUM case.

--visualization
DEAD VERSIONSVACUUMANALYZE
Stage 1 / 3
Writes leave dead MVCC versions — indexes still chase them (bloat).
--example
query.sql
1VACUUM (ANALYZE) employees;
Result
jobeffect
reclaimdead space freed
statsplanner 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.

--variants

Post-bulk VACUUM ANALYZE

Reclaim + reteach after loads.

query.sql
1VACUUM (ANALYZE) employees;
Result
jobeffect
reclaimdead freed
statsestimates trusted
1Bulk UPDATE left 3 dead versions per hot row.
2VACUUM frees + marks all-visible.
3ANALYZE fixes the salary histogram for next plan.

Bloat vs missing-index signal

Same slowness, different cures.

query.sql
1-- bloat: slow scan, good plan, pgstattuple high dead
2-- missing index: Seq Scan where Index Scan belongs (EXPLAIN)
Result
signalcure
bloatVACUUM
no indexCREATE INDEX
1Plan shape decides the cure.
2Bloat keeps the right plan slow.
3Missing index picks the wrong plan.
--key facts
Bloatdead MVCC versions indexes still chase
VACUUMreclaims + enables index-only
ANALYZEfresh stats — estimates you can trust
--use cases
Post-migration hygiene: VACUUM ANALYZE cadence
Index-only adoption: visibility-map health
--under the hood
Autovacuum throttles for OLTP; bulk ETL outruns it — schedule manual windows.
Visibility-map bits are per-page — one dead tuple dirties the page for index-only checks.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.