PostgreSQL raises branch_transaction_already_active (SQLSTATE 25002) when a session tries to open a new branch of a distributed transaction while another branch is still active.
branch_transaction_already_active appears when you attempt to start a second branch in the same distributed transaction before the first branch is ended. Finish, commit, or roll back the current branch, or use SAVEPOINTs instead, to resolve the error.
branch_transaction_already_active
PostgreSQL returns SQLSTATE 25002 with the message "branch transaction already active" when a client issues a command that starts a new branch of a distributed two phase transaction while the current branch has not yet completed.
The server rejects the request to protect transaction integrity.
Fixing it quickly matters because an unresolved branch can block commits, hold locks, and cause deadlocks in related sessions.
The error is triggered when BEGIN, START TRANSACTION, or XA START is executed while another branch created by the same global transaction identifier (GTRID) remains active. PostgreSQL detects overlapping distributed branches and raises 25002.
Client libraries that automatically open transactions inside an existing explicit transaction also cause the issue.
Tools that wrap each statement in BEGIN...COMMIT can collide with manual transaction control.
Finish the current branch with COMMIT PREPARED, ROLLBACK PREPARED, or END before creating another branch. If you only need nesting inside one connection, use SAVEPOINT instead of BEGIN.
Review connection pool settings so libraries do not issue implicit START TRANSACTION statements.
Ensure each XA START or pg_prepared_xact aligns with a matching END.
When using a Java EE application server with XA datasources, duplicate XA START calls often overlap. Verify that each method closes its transaction context.
In psql scripts, running BEGIN twice without COMMIT in between reproduces the error.
Insert a COMMIT after the first block or convert the second BEGIN to SAVEPOINT.
Adopt a strict transaction pattern: START - work - END - PREPARE - COMMIT. Monitor pg_prepared_xacts to detect forgotten branches.
Use connection pool parameters that keep autocommit on unless a global manager changes it.
Galaxy’s SQL editor highlights uncommitted transactions and lets teams endorse templates that follow best practice, lowering the chance of accidental nested BEGIN calls.
SQLSTATE 25001 (active_sql_transaction) appears when a command that requires autocommit is run inside a transaction. SQLSTATE 25005 (inappropriate_access_mode_for_branch_transaction) surfaces when an access mode conflicts with the branch.
Troubleshoot them with similar steps: end or roll back the current transaction first.
.
No. PostgreSQL supports SAVEPOINTs, which provide partial rollback but are not full nested transactions.
The error only appears when you explicitly use two phase commit or XA. Avoiding those features prevents the condition.
Galaxy warns when a second BEGIN is typed inside an open block and offers one-click COMMIT or ROLLBACK.
branch_transaction_already_active exists in all supported PostgreSQL versions because it is defined in the SQL standard error class 25.