Databases

Autovacuum stalls and partition bloat due to lingering prepared transactions holding old snapshots in PostgreSQL 13

noahnode
1mo ago
Environment

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

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

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

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

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?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Application leaves PREPARE TRANSACTION requests uncommitted (crash or bug), leaving persistent prepared transactions that block VACUUM 90%
Misconfigured or missing failover cleanup that should ROLLBACK PREPARED on aborted participants 40%
Logical replication or long-running replica slots referencing old snapshots (less likely than prepared txns) 30%

Summary

Prepared transactions (two-phase commit) retain the transaction snapshot until COMMIT PREPARED or ROLLBACK PREPARED is executed. Abandoned prepared transactions hold back the oldestxmin so autovacuum cannot remove tuples created before that snapshot, causing bloat. Restarting the server does not remove prepared transactions; they persist across restarts. Fixes include identifying and explicitly rolling back abandoned prepared transactions, modifying application behavior to avoid leaving prepared transactions open, and adding server-side timeouts or monitoring.

Suggested Fixes

Diagnostic step 1
List prepared transactions: SELECT * FROM pg_prepared_xacts; note gid, prepared timestamp and owner.
Diagnostic step 2
If you can verify a prepared transaction is abandoned, run ROLLBACK PREPARED 'gid' on the primary to remove it; do this for one transaction at a time and monitor relfrozenxid/pg_stat_all_tables.
Diagnostic step 3
If multiple abandoned prepared txns exist, coordinate with application owners before ROLLBACK PREPARED. For emergency cleanup, ROLLBACK PREPARED is safe for abandoned work but will lose prepared updates.

Continue troubleshooting

Matched from the title, description and tags