Skip to main content
Interactive pgtune Configurator →Open Architecture Blueprint ↗
P0 INCIDENTL6 BARM02 · 6 MIN READ

PostgreSQL MVCC Dead-Tuple Bloat & Blocked AUTOVACUUM

Diagnose why PostgreSQL 16 AUTOVACUUM cannot reclaim dead tuples when idle-in-transaction sessions or orphan replication slots pin the xmin horizon.

PRODUCTION SYMPTOM & ALERT SIGNATURE

Primary PostgreSQL 16 p99 read latency climbs 8x over 4 hours even though traffic is flat. pg_stat_user_tables shows n_dead_tup exceeding 40% of table size and autovacuum runs repeatedly without reducing page count.

pg_stat_user_tables_n_dead_tup{relname="orders"} / pg_stat_user_tables_n_live_tup > 0.35 AND max(pg_stat_activity_max_tx_duration{state="idle in transaction"}) > 600
01 · ROOT-CAUSE MECHANICS (WHY IT HAPPENS)

PostgreSQL implements Multi-Version Concurrency Control (MVCC) by never overwriting rows in place. Every UPDATE writes a new tuple version and stamps the old tuple header with xmax, leaving both versions inside 8KB heap pages.

AUTOVACUUM can only reclaim dead tuples whose xmax is strictly older than the global oldest active transaction horizon (backend_xmin across active sessions, prepared transactions, and logical/physical replication slots).

A single forgotten analytics query or crashed worker stuck in "idle in transaction" pins the global xmin horizon for the entire cluster. Heap pages fill with unreachable tuples, forcing sequential and index scans to read megabytes of dead versions from disk and shared_buffers.

INTERACTIVE 8KB HEAP PAGE LAB · RUN IT RIGHT HERE
POSTGRESQL 16 · 8KB HEAP PAGE MVCC SIMULATORNO SIGNUP REQUIRED
HEALTHY · 4/8 FREE HEADROOM
SLOT #1
LIVE
xmin:201
SLOT #2
LIVE
xmin:202
SLOT #3
LIVE
xmin:203
SLOT #4
LIVE
xmin:204
SLOT #5
FREE
--
SLOT #6
FREE
--
SLOT #7
FREE
--
SLOT #8
FREE
--
$ Page 0 nominal: 4 live tuples, 4 free slots. Click "1. BEGIN Long Tx" then "2. RUN UPDATE" to reproduce production MVCC bloat.
6 interactive physics simulators + 12 live Firecracker Linux VMs + 74 WASM/SQL drills inside.
02 · LIVE TRIAGE COMMANDS (COPY & RUN)
Find the session pinning the global xmin horizon
$ psql -c "SELECT pid, usename, state, now() - xact_start AS tx_age, backend_xmin, left(query, 80) FROM pg_stat_activity WHERE backend_xmin IS NOT NULL ORDER BY age(backend_xmin) DESC LIMIT 10;"
Smoking gun: A session in state="idle in transaction" with tx_age > 15m and an old backend_xmin.
Check inactive replication slots holding back catalog_xmin
$ psql -c "SELECT slot_name, active, xmin, catalog_xmin, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS wal_lag FROM pg_replication_slots;"
Smoking gun: active=false with non-null xmin/catalog_xmin and growing WAL lag.
Inspect dead tuple ratio and last autovacuum timestamp
$ psql -c "SELECT 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_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;"
Smoking gun: dead_pct > 25% even when last_autovacuum ran minutes ago.
03 · PRODUCTION DIFF (BROKEN VS HARDENED)
BROKEN · Broken postgresql.conf (unbounded idle transactions & slow default vacuum)
# Default settings allow a single crashed worker to pin xmin for hours
idle_in_transaction_session_timeout = 0
autovacuum_vacuum_scale_factor = 0.2      # Waits for 20% dead rows on a 100M-row table!
autovacuum_vacuum_cost_limit = 200        # Throttled too hard on NVMe SSDs
hot_standby_feedback = on                 # Replica BI queries pin primary xmin
HARDENED · Production PostgreSQL 16 Guardrails (bounded xmin + NVMe autovacuum)
# Hard-cap idle-in-transaction sessions so xmin can never pin indefinitely
idle_in_transaction_session_timeout = '60s'
statement_timeout = '15s'
lock_timeout = '2s'

# Trigger autovacuum earlier on hot tables and match NVMe IOPS
autovacuum_vacuum_scale_factor = 0.02
autovacuum_vacuum_threshold = 1000
autovacuum_vacuum_cost_limit = 2000
autovacuum_vacuum_cost_delay = '2ms'
max_slot_wal_keep_size = '20GB'
04 · MINUTE-24 BAR-RAISER CHECKPOINT (L6 · M02)TEST YOUR INSTINCT · ZERO SIGNUP

You run manual VACUUM orders during an outage, and Postgres reports "0 dead row versions removed, 1,420,000 dead row versions cannot be removed yet". What is the exact next action?

05 · POST-FIX VERIFICATION CHECKLIST
  • Confirm zero sessions remain in idle in transaction > 60s via pg_stat_activity.
  • Run VACUUM (VERBOSE, ANALYZE) orders; verify "tuples removed" increases and "tuples not removable" drops near 0.
  • Check EXPLAIN (ANALYZE, BUFFERS) on the primary OLTP query: shared hit/read buffer count should drop proportionally to reclaimed dead tuples.
06 · FREQUENTLY ASKED ENGINEERING QUESTIONS

Why does VACUUM say dead row versions cannot be removed yet?

PostgreSQL preserves any dead tuple whose xmax is newer than the oldest active snapshot horizon across pg_stat_activity, pg_prepared_xacts, and pg_replication_slots (including standbys with hot_standby_feedback=on).

Does standard VACUUM return disk space to the operating system?

Standard VACUUM marks space reusable inside existing 8KB heap pages (via the Free Space Map) without blocking reads or writes. To shrink physical file size online without table locks, use pg_repack instead of VACUUM FULL.

What is a safe idle_in_transaction_session_timeout for OLTP services?

Most production OLTP workloads enforce 30s to 60s globally and override per-role only for short batch migrations. Interactive BI queries should run against a dedicated read replica without hot_standby_feedback pinning primary OLTP tables.