Back to Blog
Blog

PostgreSQL Autovacuum Tuning: How to Check It Is Keeping Up

Director, Nixsoft Technologies

November 25, 2026
6 min read
PostgreSQL Autovacuum Tuning: How to Check It Is Keeping Up

When a PostgreSQL database slows down or starts eating disk space faster than row counts explain, autovacuum is one of the first places I look. In most cases, the daemon is not broken or disabled. It is running with default thresholds that make sense for small tables and fall behind on busy, multi-million-row tables.

Before touching a single parameter, I want hard numbers on which tables have dead tuples piling up, whether workers are running, and whether something else in the database is holding cleanup back.

Why autovacuum falls behind

PostgreSQL uses multiversion concurrency control (MVCC). When an UPDATE or DELETE runs, PostgreSQL does not overwrite or erase the old row version right away. Old row versions, known as dead tuples, stay in the table until no active transaction can still see them. Standard VACUUM scans those pages, marks space from dead tuples as reusable, updates the visibility map so index-only scans can skip heap fetches, and keeps planner statistics current when paired with ANALYZE.

As documented in the PostgreSQL Routine Vacuuming manual, autovacuum triggers a VACUUM on a table when the number of obsolete tuples exceeds:

vacuum threshold = autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor * reltuples

By default, autovacuum_vacuum_threshold is 50 and autovacuum_vacuum_scale_factor is 0.2 (20% of the table). Newer PostgreSQL releases also cap that number with autovacuum_vacuum_max_threshold, which defaults to 100 million tuples.

Consider what 0.2 means in practice:

  • On a 10,000-row table, autovacuum triggers at 2,050 dead tuples.
  • On a 50-million-row table, autovacuum waits until roughly 10 million tuples are dead before starting.

Wait that long on a high-churn table and you accumulate gigabytes of dead space. Once a worker finally starts, it runs under cost-based throttling (autovacuum_vacuum_cost_delay = 2ms and vacuum_cost_limit = 200 by default, shared across active workers when autovacuum_vacuum_cost_limit is -1). With autovacuum_max_workers defaulting to 3, three large tables can occupy every worker slot for hours while smaller hot tables wait in line.

Checks I run first

When I inspect a PostgreSQL server, I run three checks in order before proposing any configuration change.

1. Dead tuples and last autovacuum timestamps

Start with pg_stat_user_tables to see where dead tuples are concentrated and when each table was last vacuumed or analyzed:

SELECT
    schemaname,
    relname,
    pg_size_pretty(pg_total_relation_size(relid)) AS total_size,
    n_live_tup,
    n_dead_tup,
    ROUND(100.0 * n_dead_tup / NULLIF(n_live_tup + n_dead_tup, 0), 2) AS dead_pct,
    last_autovacuum,
    last_autoanalyze,
    autovacuum_count
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 15;

Look for large tables with high n_dead_tup and an old or NULL last_autovacuum timestamp. Remember that n_dead_tup comes from cumulative statistics and resets if stats were reset, so pair it with table size trends.

2. Active autovacuum workers and progress

Next, check whether autovacuum workers are currently running, what phase they are in, and whether any are running to prevent transaction ID wraparound:

SELECT
    a.pid,
    a.datname,
    NOW() - a.xact_start AS duration,
    p.phase,
    p.heap_blks_scanned,
    p.heap_blks_total,
    a.query
FROM pg_stat_activity a
LEFT JOIN pg_stat_progress_vacuum p ON a.pid = p.pid
WHERE a.backend_type = 'autovacuum worker';

If query ends with (to prevent wraparound), PostgreSQL is running an emergency vacuum because a table’s relfrozenxid exceeded autovacuum_freeze_max_age (200 million transactions by default). Unlike normal autovacuums, wraparound vacuums do not automatically yield to conflicting locks. You can also check age(relfrozenxid) across pg_class to spot tables approaching that limit early.

3. Blockers holding back cleanup

Even aggressive autovacuum cannot remove dead tuples that are still visible to an old snapshot. Before tuning vacuum speed, check for long-running transactions in pg_stat_activity:

SELECT
    pid,
    usename,
    application_name,
    state,
    NOW() - xact_start AS xact_age,
    age(backend_xid) AS xid_age,
    age(backend_xmin) AS xmin_age,
    LEFT(query, 80) AS query_preview
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
ORDER BY xact_start ASC
LIMIT 10;

Look especially for sessions stuck in idle in transaction with a large xmin_age or old xact_start. Also inspect pg_replication_slots for inactive slots (active = false) whose xmin or catalog_xmin has stopped advancing, and pg_prepared_xacts for forgotten prepared transactions. Clearing those blockers often fixes dead tuple buildup immediately without any config change.

Per-table settings for busy tables

Once blockers are clear, avoid slashing autovacuum_vacuum_scale_factor globally across the whole instance unless your workload truly calls for it. A tiny global scale factor can cause workers to churn continuously through small lookup tables.

Instead, override storage parameters on the specific high-churn tables identified in step one:

ALTER TABLE orders SET (
    autovacuum_vacuum_scale_factor = 0.02,
    autovacuum_vacuum_threshold = 1000,
    autovacuum_analyze_scale_factor = 0.01,
    autovacuum_vacuum_cost_limit = 1000
);

This keeps the trigger tight on a busy table without changing behavior for the rest of the database. Notice autovacuum_vacuum_cost_limit as well: per the PostgreSQL Vacuuming configuration docs, workers processing tables with a per-table cost limit or cost delay do not pull from the shared global worker budget, letting that table finish faster without starving other tables.

Two edge cases are easy to miss:

  • Append-only tables: Tables that mostly receive INSERTs still benefit from vacuuming to set visibility map bits and freeze tuples. PostgreSQL triggers insert vacuums using autovacuum_vacuum_insert_threshold (default 1000) and autovacuum_vacuum_insert_scale_factor (default 0.2).
  • Partitioned tables: Autovacuum processes individual partitions, not the parent partitioned table itself. Run periodic manual ANALYZE on partitioned parent tables when partition data changes so query plans stay accurate.

Monitoring bloat and vacuum progress over time

Tuning once is not enough if traffic patterns shift. A few lightweight habits keep autovacuum visible before space runs tight:

  • Set log_autovacuum_min_duration (for example to 1000 milliseconds, or 0 briefly while diagnosing) so slow runs, dead tuple counts, and buffer hits land in your server logs.
  • Alert on n_dead_tup growth, max(age(relfrozenxid)) per database, long idle in transaction sessions, and inactive replication slots.
  • Review table and index sizes weekly rather than jumping straight to VACUUM FULL, which takes an ACCESS EXCLUSIVE lock and temporarily requires extra disk space for a full table rewrite.

If you are planning broader throughput and connection management work alongside vacuum tuning, read our guide on Scaling PostgreSQL for High-Traffic Applications or see how our team handles ongoing tuning under Database Managed Services & Remote DBA.

Conclusion

Checking autovacuum health comes down to three habits: measure dead tuples against table size, remove transaction or replication-slot blockers first, and apply per-table storage settings to your busiest tables before altering global defaults.

If your PostgreSQL tables are growing faster than autovacuum can reclaim space, or you want a second pair of eyes on vacuum settings, replication health, and tested restore drills, reach out on our Nixsoft contact page to book a 30-minute technical call.

Share this post: