Databases

PostgreSQL autovacuum CPU spikes and accelerating table bloat after lowering autovacuum_vacuum_cost_delay

sara_solve
1mo ago
Environment

Environment

PostgreSQL 13 on Debian 11 VM, OLTP workload with frequent updates/deletes on several large partitioned tables; autovacuum_vacuum_cost_delay changed from 20ms down to 2–5ms across the cluster two days ago.

Symptoms

Symptoms

Unexpected CPU consumption by autovacuum workers (50–80% CPU on several cores), workload latency increases for application queries; reported table bloat on several large tables increasing over consecutive hours despite autovacuum activity.

Observations

Observations

postgres logs show frequent autovacuum launches and repeated vacuums on same partitions. iostat shows moderate IO but not saturated. Autovacuum workers sometimes conflict with hot transactions; autovacuum worker counts at max configured value.

Already attempted

Already attempted

Rolled autovacuum_vacuum_cost_delay back to 20ms on one replica (observed modest CPU drop there). Increased autovacuum_max_workers temporarily; no manual VACUUM FULL performed because of production constraints.

Question

Question

How should autovacuum settings be tuned to reduce CPU spikes and bloat while keeping space reclaimed without causing heavy contention or excessive worker churn?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

autovacuum_vacuum_cost_delay set too low causing autovacuum to run aggressively and steal CPU cycles while not finishing tasks efficiently 92%
maintenance_work_mem too small so each vacuum workset is split into many I/O-bound passes increasing total overhead 68%
autovacuum settings (scale_factor/threshold) not tuned for high-churn tables, causing repeated vacuums on hot partitions 55%

Summary

Setting autovacuum_vacuum_cost_delay very low makes autovacuum work more continuously and aggressively, which can increase CPU and worker churn (autovacuum spawns frequently, contends with user queries) and paradoxically accelerate bloat metrics if vacuum can't complete or is constantly preempted. The root is an imbalance between autovacuum aggressiveness, cost limits, maintenance_work_mem, and the rate of update/delete churn on large tables. Other contributors can be too-low autovacuum_vacuum_scale_factor/threshold for heavy-write tables and insufficient maintenance_work_mem causing many small passes rather than efficient vacuums.

Suggested Fixes

Diagnostic step 1
Restore autovacuum_vacuum_cost_delay to a conservative value (e.g., 20ms), monitor CPU and autovacuum log frequency for 1–2 hours.
Diagnostic step 2
Increase autovacuum_vacuum_cost_limit and maintenance_work_mem moderately (e.g., maintenance_work_mem to 256–1024MB depending on available RAM) so vacuums can batch more work per pass.
Diagnostic step 3
Lower autovacuum_vacuum_scale_factor for the largest/high-write tables or add table-level autovacuum settings to run less frequently but with larger cost limits.

Continue troubleshooting

Matched from the title, description and tags