Diagnostic Queries
Symptoms
A PREPARE TRANSACTION failed because the maximum number of concurrent prepared (two-phase) transactions is already in use. PostgreSQL raises SQLSTATE 53400 (configuration_limit_exceeded).
- The cap set by
max_prepared_transactionswas reached. - Common when prepared transactions are not committed/rolled back promptly.
- Often a sign of an orphaned 2PC transaction.
What the server log shows
ERROR: maximum number of prepared transactions reached
HINT: Increase max_prepared_transactions (currently 0).
Why PostgreSQL raises this — what the manual says
the PREPARE TRANSACTION reference (Notes):
“If you have not set up an external transaction manager to track prepared transactions and ensure they get closed out promptly, it is best to keep the prepared-transaction feature disabled by setting max_prepared_transactions to zero.”
Two-phase commit reserves slots bounded by max_prepared_transactions. When all slots are occupied (often by transactions left prepared but not finalized), new PREPARE TRANSACTION calls fail with 53400.
Common causes
max_prepared_transactionsset to 0 or too low.- Orphaned prepared transactions never committed/rolled back.
- A transaction manager leaking 2PC transactions.
Relevant GUC parameters
| Parameter | Default | Effect |
|---|---|---|
max_prepared_transactions |
0 | Maximum concurrent prepared (2PC) transactions; 0 disables 2PC. |
How to fix it
- Raise
max_prepared_transactions(requires restart) for genuine 2PC workloads. - Find and resolve orphans:
SELECT * FROM pg_prepared_xacts;thenCOMMIT/ROLLBACK PREPARED. - Fix the transaction manager so it finalizes prepared transactions.
Diagnostic query
SELECT gid, prepared, owner, database FROM pg_prepared_xacts ORDER BY prepared;
Long-lived rows here indicate orphaned 2PC transactions holding slots (and locks/WAL).
Related & next steps
Reference: PostgreSQL 18 — PREPARE TRANSACTION.
Thanks — noted. This helps keep the database accurate.