Autovacuum Tuning — Complete Guide
Autovacuum Tuning — 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 86 of 100
Autovacuum Tuning
SQL ✓ → Advanced
Advanced · 2 — Production · ~10 min · PostgreSQL — Monitoring & Troubleshooting
What is this?
Autovacuum tuning adjusts cost delay, scale factor, and worker count so hot tables vacuum before bloat and wraparound — per-table storage parameters override globals.
Why should you care?
PostgresVerse orders after flash sale hits n_dead_tup millions — default autovacuum too slow for churn rate.
See it live — copy this example
Run in pgAdmin or psql.
ALTER TABLE orders SET (
autovacuum_vacuum_scale_factor = 0.02,
autovacuum_analyze_scale_factor = 0.01,
autovacuum_vacuum_cost_delay = 2
);
SELECT relname, last_autovacuum, last_autoanalyze, n_dead_tup
FROM pg_stat_user_tables
WHERE relname = 'orders';
What happened?
- Lower scale_factor triggers vacuum at 2% dead tuples not 20%.
- Reduced cost_delay makes vacuum more aggressive on this table only.
- Monitor last_autovacuum after change.
Practice next
- Check n_dead_tup and last_autovacuum on hot tables.
- Apply per-table settings on orders.
- Watch pg_stat_progress_vacuum during peak churn test.
- Increase autovacuum_max_workers temporarily during sale prep.
- Log autovacuum duration via log_autovacuum_min_duration = 0 in lab.
Remember
Tune hot tables individually. Analyze scale factor separate from vacuum. Monitor wraparound age pg_database datfrozenxid.
PostgresVerse sale prep
DBA lowers orders scale_factor week before sale; dead tuples stay under 5%.
Outcome: No autovacuum emergency mid-event; index-only scans stay valid.
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!