Autovacuum bloat management

From CarlosCaPe/octorato · View interactive version

Autovacuum bloat management is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Autovacuum & Table Bloat Management

How to use this skill

Skill file

Autovacuum & Table Bloat Management

Source: PostgreSQL Best Practices

-- "Maintenance, vacuum, and bloat" section

What

Monitoring and tuning PostgreSQL's autovacuum system to prevent table and

index bloat. Includes per-table autovacuum parameter tuning for high-churn

tables.

Why

PostgreSQL uses MVCC (Multi-Version Concurrency Control). Every UPDATE creates

a new row version; DELETE marks the old version as dead. These dead tuples

accumulate as bloat until VACUUM reclaims the space.

If autovacuum can't keep up:

How

Monitor dead tuples and vacuum activity

SELECT
    schemaname,
    relname,
    n_live_tup,
    n_dead_tup,
    ROUND(100.0 * n_dead_tup / NULLIF(n_live_tup + n_dead_tup, 0), 1)
        AS dead_pct,
    last_vacuum,
    last_autovacuum,
    last_analyze,
    last_autoanalyze
FROM pg_stat_user_tables
WHERE n_dead_tup > 1000
ORDER BY n_dead_tup DESC;

Check table bloat estimate

SELECT
    schemaname || '.' || relname AS table_name,
    pg_size_pretty(pg_total_relation_size(relid)) AS total_size,
    pg_size_pretty(pg_relation_size(relid)) AS table_size,
    pg_size_pretty(pg_indexes_size(relid)) AS index_size,
    n_live_tup,
    n_dead_tup
FROM pg_stat_user_tables
WHERE schemaname = 'public'
ORDER BY pg_total_relation_size(relid) DESC;

Tune autovacuum for hot tables

-- High-churn table: vacuum more aggressively
ALTER TABLE public."AuditLog" SET (
    autovacuum_vacuum_scale_factor = 0.05,   -- vacuum at 5% dead (default 20%)
    autovacuum_analyze_scale_factor = 0.02   -- analyze at 2% changes (default 10%)
);

-- Verify settings
SELECT relname, reloptions
FROM pg_class
WHERE relname = 'AuditLog';

Default autovacuum thresholds (PG 16 docs: runtime-config-autovacuum.html)

| Parameter | Default | Meaning |

|-----------|---------|---------||

| autovacuum_vacuum_threshold | 50 | Minimum dead tuples before vacuum |

| autovacuum_vacuum_scale_factor | 0.20 | Fraction of table that must be dead |

| autovacuum_vacuum_insert_threshold | 1000 | Inserts before insert-triggered vacuum (PG 13+) |

| autovacuum_vacuum_insert_scale_factor | 0.20 | Fraction of table size for insert-triggered vacuum |

| autovacuum_analyze_threshold | 50 | Minimum changes before analyze |

| autovacuum_analyze_scale_factor | 0.10 | Fraction of table that must change |

Trigger formula: Vacuum runs when

dead_tuples > threshold + scale_factor * n_live_tup

For a 1M-row table with defaults: vacuum at 50 + 0.20 * 1,000,000 = 200,050 dead tuples.

With tuned 0.05: vacuum at 50 + 0.05 * 1,000,000 = 50,050 dead tuples.

Manual vacuum (when needed)

-- Standard vacuum (non-blocking, reclaims dead tuples)
VACUUM (VERBOSE) public."AuditLog";

-- Vacuum + analyze (update planner statistics too)
VACUUM (VERBOSE, ANALYZE) public."AuditLog";

-- VACUUM FULL (rewrites table, reclaims disk space, but LOCKS table)
-- Use only for extreme bloat; prefer REINDEX CONCURRENTLY for indexes
VACUUM FULL public."AuditLog";

Reindex for index bloat

-- Non-blocking index rebuild
REINDEX INDEX CONCURRENTLY public."ix_auditlog_createddate";

-- Check for invalid indexes (failed concurrent operations)
SELECT indexrelid::regclass, indisvalid
FROM pg_index
WHERE NOT indisvalid;

Decision Matrix

| Scenario | Action |

|----------|--------|

| Dead tuple % < 10% | Normal -- autovacuum handling it |

| Dead tuple % 10-30% | Consider lowering scale_factor |

| Dead tuple % > 30% | Immediate: manual VACUUM; then tune autovacuum |

| Table size >> expected for row count | Possible bloat; check dead_pct |

| After bulk DELETE/UPDATE | Run VACUUM ANALYZE manually |

| After index creation | Run ANALYZE to update stats |

When to Use

Where We Used It

Related Skills

References

-- "Maintenance, vacuum, and bloat" section and Backlog #9

Gotchas

approach transaction ID wraparound (catastrophic)

use only as a last resort during maintenance windows

the table file stays the same size. Only VACUUM FULL shrinks the file.

and update planner statistics

Server Parameters in the Azure Portal (server-wide) or per-table via ALTER


Category: Tooling | Origin: Audit TDDs, PostgreSQL Best Practices

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.