Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/monitor-vacuum-analyze.md
1---2title: Maintain Table Statistics with VACUUM and ANALYZE3impact: MEDIUM4impactDescription: 2-10x better query plans with accurate statistics5tags: vacuum, analyze, statistics, maintenance, autovacuum6---78## Maintain Table Statistics with VACUUM and ANALYZE910Outdated statistics cause the query planner to make poor decisions. VACUUM reclaims space, ANALYZE updates statistics.1112**Incorrect (stale statistics):**1314```sql15-- Table has 1M rows but stats say 100016-- Query planner chooses wrong strategy17explain select * from orders where status = 'pending';18-- Shows: Seq Scan (because stats show small table)19-- Actually: Index Scan would be much faster20```2122**Correct (maintain fresh statistics):**2324```sql25-- Manually analyze after large data changes26analyze orders;2728-- Analyze specific columns used in WHERE clauses29analyze orders (status, created_at);3031-- Check when tables were last analyzed32select33relname,34last_vacuum,35last_autovacuum,36last_analyze,37last_autoanalyze38from pg_stat_user_tables39order by last_analyze nulls first;40```4142Autovacuum tuning for busy tables:4344```sql45-- Increase frequency for high-churn tables46alter table orders set (47autovacuum_vacuum_scale_factor = 0.05, -- Vacuum at 5% dead tuples (default 20%)48autovacuum_analyze_scale_factor = 0.02 -- Analyze at 2% changes (default 10%)49);5051-- Check autovacuum status52select * from pg_stat_progress_vacuum;53```5455Reference: [VACUUM](https://supabase.com/docs/guides/database/database-size#vacuum-operations)56