Monitor Vacuum Analyze
title: Maintain Table Statistics with VACUUM and ANALYZE impact: MEDIUM impactDescription: 2-10x better query plans with accurate statistics tags: vacuum, analyze, statistics, maintenance, autovacuum
Section titled “title: Maintain Table Statistics with VACUUM and ANALYZE impact: MEDIUM impactDescription: 2-10x better query plans with accurate statistics tags: vacuum, analyze, statistics, maintenance, autovacuum”Maintain Table Statistics with VACUUM and ANALYZE
Section titled “Maintain Table Statistics with VACUUM and ANALYZE”Outdated statistics cause the query planner to make poor decisions. VACUUM reclaims space, ANALYZE updates statistics.
Incorrect (stale statistics):
-- Table has 1M rows but stats say 1000-- Query planner chooses wrong strategyexplain select * from orders where status = 'pending';-- Shows: Seq Scan (because stats show small table)-- Actually: Index Scan would be much fasterCorrect (maintain fresh statistics):
-- Manually analyze after large data changesanalyze orders;
-- Analyze specific columns used in WHERE clausesanalyze orders (status, created_at);
-- Check when tables were last analyzedselect relname, last_vacuum, last_autovacuum, last_analyze, last_autoanalyzefrom pg_stat_user_tablesorder by last_analyze nulls first;Autovacuum tuning for busy tables:
-- Increase frequency for high-churn tablesalter table orders set ( autovacuum_vacuum_scale_factor = 0.05, -- Vacuum at 5% dead tuples (default 20%) autovacuum_analyze_scale_factor = 0.02 -- Analyze at 2% changes (default 10%));
-- Check autovacuum statusselect * from pg_stat_progress_vacuum;Reference: VACUUM