// SQL snippet

Check autovacuum status and dead-tuple backlog in PostgreSQL

Autovacuum runs in the background without telling you when it falls behind. This shows the dead-row backlog per table.

Main statement
PostgreSQL

The task

UPDATEs and DELETEs leave dead tuples that only VACUUM can reclaim. Autovacuum handles this automatically, but on high-churn tables it can lose the race: the dead-tuple backlog grows, the table bloats, and scans slow down. pg_stat_user_tables exposes the backlog and when autovacuum last touched each table.

The snippet

Written for PostgreSQL.

PostgreSQL
SELECT relname AS table_name,
       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_autovacuum, autovacuum_count,
       last_vacuum, vacuum_count,
       pg_size_pretty(pg_total_relation_size(relid)) AS total_size
FROM pg_stat_user_tables
WHERE n_dead_tup > 0
ORDER BY n_dead_tup DESC
LIMIT 25;

How it works

n_dead_tup is an estimate of dead row versions, maintained from the counts of updates and deletes and reset by each vacuum. Autovacuum starts on a table once dead rows exceed autovacuum_vacuum_threshold plus autovacuum_vacuum_scale_factor times the row count, 50 plus 20 percent by default, so a dead_pct well above 20 percent means vacuum is behind. last_autovacuum shows when the automatic process last ran; last_vacuum records manual runs. A high dead_pct with a recent last_autovacuum has two possible causes. Either churn outpaces vacuum, fixed by per-table tuning (a lower autovacuum_vacuum_scale_factor so it starts sooner, a higher autovacuum_vacuum_cost_limit so it works faster), or vacuum ran but was not allowed to remove the rows because an old transaction, replication slot or prepared transaction still needs them. Check long-transactions before tuning.

Variations

Tables autovacuum has never reached

SELECT relname, n_dead_tup, n_live_tup, last_autovacuum
FROM pg_stat_user_tables
WHERE last_autovacuum IS NULL AND n_dead_tup > 0
ORDER BY n_dead_tup DESC;

Watch autovacuum working right now

SELECT p.pid, p.datname, p.relid::regclass AS table_name, p.phase,
       p.heap_blks_scanned, p.heap_blks_total,
       ROUND(100.0 * p.heap_blks_scanned / NULLIF(p.heap_blks_total, 0), 1) AS pct_scanned,
       p.index_vacuum_count,
       now() - a.xact_start AS running_for
FROM pg_stat_progress_vacuum p
LEFT JOIN pg_stat_activity a ON a.pid = p.pid
ORDER BY running_for DESC NULLS LAST;

Questions that come up

Autovacuum runs constantly on one table. Is that a problem?

Not by itself. Autovacuum is triggered by churn, so constant runs on a table mean constant churn, which is fine if dead_pct stays low. It becomes a problem when each pass leaves the dead count about where it was: vacuum is scanning but cannot remove rows an old snapshot still needs. The VACUUM VERBOSE output, or the autovacuum log lines with log_autovacuum_min_duration set, report how many dead rows were not yet removable. index_vacuum_count above 1 in the progress view means the dead-row list did not fit in maintenance_work_mem and the indexes are being scanned more than once.

What holds back vacuum besides long transactions?

An inactive replication slot (pg_replication_slots.xmin or catalog_xmin), a standby running long queries with hot_standby_feedback = on, and forgotten prepared transactions (pg_prepared_xacts) all hold back the oldest row version vacuum may remove, for every database in the cluster. A table with autovacuum_enabled = false in its storage options is simply never vacuumed automatically. Each of these shows up as dead row counts that stay high however vacuum is tuned.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.