VACUUM & ANALYZE — Complete Guide
VACUUM & ANALYZE — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 29 of 100
VACUUM & ANALYZE
SQL → Advanced
SQL · 1 — Queries · ~6 min · PostgreSQL — Indexing & Performance
What is this?
VACUUM reclaims dead tuple space and updates visibility map; ANALYZE refreshes planner statistics. Autovacuum runs both automatically but heavy tables need tuning.
Why should you care?
PostgresVerse orders table after flash sale has bloat and wrong reltuples — queries slow and index-only scans fail without vacuum.
See it live — copy this example
Run in pgAdmin or psql.
SELECT relname, n_dead_tup, last_vacuum, last_autovacuum
FROM pg_stat_user_tables
WHERE relname = 'orders';
VACUUM (ANALYZE, VERBOSE) orders;
What happened?
- pg_stat_user_tables shows dead tuple count and vacuum timestamps.
- VACUUM ANALYZE cleans dead rows and updates stats in one manual pass.
Practice next
- Update many rows in orders to create dead tuples.
- Check n_dead_tup before vacuum.
- Run VACUUM ANALYZE and recheck.
- SET autovacuum_vacuum_scale_factor = 0.01 on hot table via ALTER TABLE storage.
- Inspect pg_stat_progress_vacuum during long run.
Remember
Dead tuples accumulate from UPDATE/DELETE. VACUUM marks space reusable; FULL rewrites file. ANALYZE feeds accurate stats to planner.
PostgresVerse post-sale cleanup
After Big Billion Days, DBA monitors n_dead_tup and bumps autovacuum on orders.
Outcome: P95 checkout latency returns to baseline within hours.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!