PostgreSQL Enterprise (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How does `autovacuum` work and how do you tune it?
senior LevelCode ExampleSystem DesignPostgreSQL
In Simple Words:
Autovacuum is a background daemon consisting of launcher and worker processes that automatically runs VACUUM and ANALYZE:
- Trigger Formula: $$\text{Threshold} = \text{autovacuum_vacuum_threshold} + (\text{autovacuum_vacuum_scale_factor} \times \text{reltuples})$$
- Default scale factor is
0.2(20%). On a table with 100 million rows, autovacuum will not trigger until 20 million rows are updated or deleted! - Tuning for Large Tables: Set aggressive table-specific thresholds:
sql
ALTER TABLE large_orders SET (
autovacuum_vacuum_scale_factor = 0.02, -- Run at 2% modifications
autovacuum_vacuum_threshold = 5000
);