🐘 PostgreSQL Intermediate

What is autovacuum in PostgreSQL?

Answer

Autovacuum is a background daemon that automatically runs VACUUM and ANALYZE on tables when needed. It monitors table activity via pg_stat_user_tables and triggers when dead tuple counts or stale statistics reach thresholds. Key configuration parameters: autovacuum_vacuum_threshold (50 dead tuples before considering vacuum), autovacuum_vacuum_scale_factor (0.2 = 20% of table size), autovacuum_analyze_threshold, autovacuum_analyze_scale_factor. For large, high-churn tables, reduce scale factors: ALTER TABLE big_table SET (autovacuum_vacuum_scale_factor = 0.01);. Monitor autovacuum: SELECT relname, last_vacuum, last_autovacuum, n_dead_tup FROM pg_stat_user_tables;. Never disable autovacuum — it prevents transaction ID wraparound (a catastrophic failure mode).