Autovacuum stalls and partition bloat due to lingering prepared transactions holding old snapshots in PostgreSQL 13
Environment
PostgreSQL 13.5 on Debian 11, partitioned table with daily partitions, primary server handling OLTP + occasional two-phase commits via application using PREPARE TRANSACTION, autovacuum enabled with default settings.
Symptoms
Autovacuum does not reclaim space on older partitions; reltuples and pg_total_relation_size grow, queries slow on partitions with many dead rows. VACUUM and manual VACUUM FREEZE on affected partitions run but report very old xmin and skip freezing.
Observations
pg_stat_activity shows several prepared transactions in pg_prepared_xacts with timestamps hours/days in the past. pg_stat_database's age of datfrozenxid is increasing. No active session appears to hold a normal in-transaction snapshot, but prepared transactions reference old xmin preventing vacuum from advancing oldestXmin.
Already attempted
Restarted postgres (short relief), forced autovacuum_worker_limit low/high changes, attempted manual VACUUM FULL (blocked because of snapshot), contacted application owners who claim prepare/commit flows should always finish but sometimes crash before COMMIT PREPARED.
Question
How do I safely remove or resolve abandoned prepared transactions that block autovacuum and prevent partitions from being vacuumed, and what safeguards prevent recurrence?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases40% match
PostgreSQL VACUUM stalls on partitioned table during heavy insert bursts; autovacuum appears idle
Open0 answers - Databases40% match
JDBC prepared statements on application server suddenly select a generic plan causing a 15× slowdown for partitioned table queries
Open0 answers - Databases36% match
PostgreSQL 14: Rapid table bloat after nightly batch UPDATEs despite autovacuum enabled
Open0 answers - Databases35% match
PostgreSQL prepared statements pick a generic plan after ANALYZE on a partitioned table, causing 10× slower execution
Open0 answers - Databases34% match
PostgreSQL autovacuum ignores newly created partitions causing rapid bloat and slow queries on recent partitions
Open0 answers