Diagnosing Bloat and Vacuum Strategy
Understand how MVCC creates table and index bloat, how to measure it, and how to tune autovacuum to keep performance high.
Diagnosing Bloat and Vacuum Strategy is a free Advanced PostgreSQL: Indexing, Partitioning, Replication lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Advanced PostgreSQL: Indexing, Partitioning, Replication learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
MVCC and Dead Tuples
PostgreSQL uses MVCC: updates and deletes leave behind old row versions called dead tuples. Until they are cleaned up, they occupy space and slow scans. This wasted space is bloat.
What VACUUM Does
VACUUM reclaims dead tuples for reuse and updates visibility information. It usually does not return space to the OS; VACUUM FULL does but rewrites the whole table and takes a strong lock.
Measuring Bloat
Inspect dead tuple counts per table from the statistics view.
SELECT relname, n_live_tup, n_dead_tup,
last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC;Autovacuum Basics
Autovacuum runs in the background, triggering when dead tuples exceed a threshold based on table size and the scale factor setting.
-- trigger ~ threshold + scale_factor * n_live_tup
autovacuum_vacuum_scale_factor = 0.2Tuning Hot Tables
For large, frequently updated tables, the default 20% scale factor is too lazy. Lower it per table so vacuum runs more often on less garbage.
ALTER TABLE orders SET (
autovacuum_vacuum_scale_factor = 0.02);Vacuum Throttling
Autovacuum throttles itself with cost limits to avoid I/O storms. On modern hardware you can raise autovacuum_vacuum_cost_limit so vacuum finishes faster.
autovacuum_vacuum_cost_limit = 2000Transaction ID Wraparound
VACUUM also prevents transaction ID wraparound, a catastrophic condition. Aggressive anti-wraparound vacuums are non-negotiable and cannot be skipped.
SELECT datname, age(datfrozenxid)
FROM pg_database ORDER BY 2 DESC;Index Bloat
Indexes bloat too. REINDEX CONCURRENTLY rebuilds an index without blocking writes, restoring its compactness.
REINDEX INDEX CONCURRENTLY orders_pkey;HOT Updates
Heap-Only Tuple updates avoid index churn when no indexed column changes. Leaving some free space via a lower fillfactor helps HOT updates and reduces bloat.
ALTER TABLE orders SET (fillfactor = 90);VACUUM vs ANALYZE
VACUUM reclaims space; ANALYZE refreshes the planner statistics. Autovacuum does both, but after big bulk loads run ANALYZE manually for fresh plans.
ANALYZE orders;A Monitoring Habit
Alert on rising n_dead_tup, growing table size with stable row counts, and high age(datfrozenxid). These early signals let you tune before queries slow down.
Quick Check
A large, hot table keeps growing despite stable row counts. What is the likely cause and fix?
Recap
You learned to diagnose bloat from MVCC dead tuples, measure it with pg_stat_user_tables, tune autovacuum per table, guard against ID wraparound, and use REINDEX CONCURRENTLY and fillfactor to keep performance high.
Frequently asked questions
Is the “Diagnosing Bloat and Vacuum Strategy” lesson free?
Yes — the full text of “Diagnosing Bloat and Vacuum Strategy” is free to read here on the web, and the Advanced PostgreSQL: Indexing, Partitioning, Replication course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Advanced PostgreSQL: Indexing, Partitioning, Replication course, upgrade to CoddyKit PRO.
What will I learn in “Diagnosing Bloat and Vacuum Strategy”?
Understand how MVCC creates table and index bloat, how to measure it, and how to tune autovacuum to keep performance high. You practise Advanced PostgreSQL: Indexing, Partitioning, Replication with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Advanced PostgreSQL: Indexing, Partitioning, Replication?
No prior experience is required. Advanced PostgreSQL: Indexing, Partitioning, Replication on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Diagnosing Bloat and Vacuum Strategy” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Advanced PostgreSQL: Indexing, Partitioning, Replication lesson?
Yes. Every Advanced PostgreSQL: Indexing, Partitioning, Replication lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Holistic Performance Tuning
- Advanced Monitoring and Alerting
- Future Trends in PostgreSQL
- Diagnosing Bloat and Vacuum Strategy