PostgreSQL Error 25002: Fix Branch Transaction Already Active
In this article
PostgreSQL error 25002, "branch transaction already active," occurs when your session tries to start a new transaction while one is already open on the same connection. The fix is to find the unclosed transaction and either commit it, roll it back, or restructure your code so it does not issue a second BEGIN. In plain terms: one connection can only have one active transaction at a time, and the server is refusing to nest a second one.
This guide walks through the likely causes, the exact steps to clear the error, and the patterns that prevent it from coming back. It applies to PostgreSQL 10 and later, including managed services such as Amazon RDS, Azure Database for PostgreSQL, and Google Cloud SQL, though menu labels in admin tools can differ slightly by version.
What You'll Need
- Access to the PostgreSQL server with a role that can view active sessions (pg_stat_activity) and, if needed, terminate them.
- The connection string or client application that is producing the error.
- A SQL client such as psql, pgAdmin, or DBeaver to inspect session state.
- Write access to the application code or ORM configuration if the cause is in your software.
Step-by-Step
- Reproduce the error and note the connection. Run the failing operation once and capture the full error message. PostgreSQL reports error code 25002 with the text "branch transaction already active." Note which application, script, or SQL client produced it. If several services share a connection pool, identify the specific service.
- Inspect active sessions. Connect with psql or pgAdmin and run:
SELECT pid, usename, state, query, state_change FROM pg_stat_activity WHERE state <> 'idle';
Look for sessions in stateidle in transaction. That state means a transaction was opened but never committed or rolled back, and it is the most common source of this error. Also check for sessions running a long query that may have issued BEGIN earlier. - Check the client's transaction mode. Many drivers and ORMs open a transaction automatically. In psql, autocommit is on by default; in JDBC, autocommit is off by default. If your code calls
BEGINmanually while the driver already started a transaction, PostgreSQL raises 25002. Review the connection settings for autocommit, and check whether your framework wraps each statement in a transaction. - Find the unclosed transaction in your code. Search for
BEGIN,START TRANSACTION, or ORM session starts that are not paired with a commit or rollback. Common culprits are early returns, exceptions thrown before commit, and loops that begin a transaction on each iteration without ending the previous one. Add a rollback in the error handler so the transaction always closes. - Close the stuck session. If a session is stuck in
idle in transactionand you cannot reach the application, terminate it from another connection:SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle in transaction' AND pid <> pg_backend_pid();
This rolls back the open transaction and frees the connection. Only terminate sessions you own or have confirmed are stale; terminating an active session can interrupt a running job. - Restart the application or connection pool. If the error persists after clearing sessions, restart the application so the pool opens fresh connections. Some pools keep a connection in a transaction state after a failed request; a restart clears that.
- Adjust the transaction pattern. If your code needs multiple statements, wrap them in a single transaction and commit once. If you need to run independent statements, keep autocommit on and avoid manual BEGIN. For stored procedures, use
CALLwith transaction control inside the procedure only where the server supports it; otherwise keep transaction control in the client. - Test the fix. Re-run the failing operation and confirm no 25002 appears. Then run a second operation on the same connection to verify the transaction was closed properly. Check pg_stat_activity again to ensure no sessions remain in
idle in transaction.
Common Mistakes
- Issuing
BEGINinside a driver that already manages transactions, which creates a nested transaction attempt. - Forgetting to roll back after an exception, leaving the connection in
idle in transaction. - Terminating the wrong session with pg_terminate_backend, which can interrupt a legitimate job.
- Assuming the error is a server bug when it is almost always a client-side transaction left open.
- Using savepoints as a substitute for proper transaction handling; savepoints do not allow a second top-level transaction.
Tips
- Set a short
idle_in_transaction_session_timeouton the database or role to automatically close abandoned transactions. - Enable statement logging during debugging to see exactly when BEGIN and COMMIT are sent.
- Use a connection pool health check that rolls back any open transaction before returning a connection.
- In ORMs, prefer the session or unit-of-work pattern that commits or rolls back automatically.
Troubleshooting
If the error continues after closing sessions and restarting the app, check for multiple application instances sharing the same database and each holding a transaction. Review the logs for repeated BEGIN statements without matching COMMIT. If you use a connection pooler such as PgBouncer in transaction mode, confirm the pooler is not mixing sessions. In rare cases, a long-running transaction from an administrative tool may be holding the state; query pg_stat_activity for the oldest xact_start and investigate that session. If you cannot identify the source, temporarily set idle_in_transaction_session_timeout to a low value to force cleanup, then fix the application pattern.
Frequently Asked Questions
What does error 25002 mean exactly?
It means the session already has an active transaction and cannot start another one. PostgreSQL does not support nested top-level transactions, so a second BEGIN is rejected.
Can I use savepoints to avoid this error?
No. Savepoints work inside an existing transaction; they do not allow a second transaction to start. You must commit or roll back the first one.
Why does it happen only sometimes?
It usually depends on code paths. An early return or exception may skip the commit, leaving the transaction open for the next request on that connection.
Is it safe to terminate idle in transaction sessions?
Yes, if you have confirmed the session is stale and belongs to your application. Terminating it rolls back the open transaction. Do not terminate sessions you do not own.
Does this error occur in managed PostgreSQL services?
Yes. The error is raised by the PostgreSQL server itself, so it appears on RDS, Azure Database, Cloud SQL, and self-hosted instances alike.
How do I prevent it permanently?
Use one transaction per unit of work, always commit or roll back, and set an idle transaction timeout as a safety net.