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.
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"}) > 600PostgreSQL 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.
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?
- 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.
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.