PostgreSQL Error 25005: No Active SQL Transaction Fix
In this article
Error 25005 in PostgreSQL means you tried to run a branch transaction command, typically ROLLBACK TO SAVEPOINT or RELEASE SAVEPOINT, when no SQL transaction is currently open. The fix is to wrap the statement in an explicit transaction: start with BEGIN;, create a savepoint with SAVEPOINT name;, do your work, then roll back or release the savepoint, and finally COMMIT; or ROLLBACK;. If you are using a framework or driver, make sure it actually opens a transaction before it issues savepoint commands, and check that autocommit is not silently ending the transaction early.
What You'll Need
- Access to a PostgreSQL database (version 9.0 or later supports savepoints; most current installs do).
- A SQL client such as psql, pgAdmin, DBeaver, or your application's database driver.
- Permission to run transactions on the target tables.
- The exact statement that produced the error, so you can see whether it was a savepoint command or a procedure call that uses one internally.
Step-by-Step
- Confirm the error context. Read the full error message. Error 25005 is raised for savepoint operations outside a transaction block. If the message mentions a function or procedure, the savepoint command is probably inside PL/pgSQL or a driver wrapper, not typed by you directly.
- Check whether a transaction is open. In psql, the prompt changes to
=#inside a transaction and=>outside one. You can also runSELECT txid_current_if_assigned();; a NULL result usually means no transaction ID has been assigned yet, though this is only a hint. The reliable check is whether you have issuedBEGINand not yet issuedCOMMITorROLLBACK. - Wrap the savepoint work in an explicit transaction. Run the commands in this order in one session:
Each savepoint command must run afterBEGIN; SAVEPOINT my_savepoint; -- your statements here ROLLBACK TO SAVEPOINT my_savepoint; -- or RELEASE SAVEPOINT my_savepoint; COMMIT;BEGINand before the finalCOMMITorROLLBACK. - Check autocommit in your client. Many GUI clients and drivers default to autocommit on. With autocommit on, each statement is its own transaction, so a later
ROLLBACK TO SAVEPOINThas nothing to attach to. In psql, autocommit is off by default, so this is usually a driver or GUI setting. Turn autocommit off for the session, or explicitly issueBEGINbefore the savepoint. - Review application code for implicit commits. Statements such as
COMMIT,ROLLBACK, and some DDL commands (for exampleCREATE DATABASEorVACUUM) cannot run inside a transaction block and will end or prevent one. If your code callsCOMMITbefore the savepoint command, the transaction is already gone. - Check connection pooling and transaction settings. Poolers in transaction mode may hand your session to a different backend between statements. If a savepoint is created on one connection and rolled back on another, PostgreSQL sees no active transaction. Use session mode for pooling, or keep the entire savepoint sequence on one connection.
- Inspect stored procedures and functions. PL/pgSQL blocks can contain
EXCEPTIONhandlers that create implicit savepoints. If a function is called outside a transaction and its handler tries to roll back to a savepoint, error 25005 can appear. Call the function inside an explicitBEGIN ... COMMITblock, or fix the function so it does not rely on a savepoint when called standalone. - Retest with a minimal script. Open a fresh session, run
BEGIN; SAVEPOINT test_sp; SELECT 1; ROLLBACK TO SAVEPOINT test_sp; COMMIT;. If that succeeds, the problem is in your application's transaction handling, not in PostgreSQL itself.
Common Mistakes
- Issuing
SAVEPOINTwithoutBEGIN. Savepoints only exist inside a transaction block; outside one, PostgreSQL raises 25005. - Assuming autocommit is off. Many drivers and GUI tools commit each statement automatically, so the savepoint disappears immediately.
- Mixing pooled connections. Creating a savepoint on one pooled connection and rolling it back on another fails because the second connection has no active transaction.
- Committing too early. A
COMMITorROLLBACKbefore the savepoint command ends the transaction and makes the savepoint invalid. - Ignoring the difference between
ROLLBACK TO SAVEPOINTandROLLBACK. A plainROLLBACKends the whole transaction, so any later savepoint command will fail.
Tips
- Keep savepoint names short and meaningful; they are scoped to the current transaction and are released automatically at commit.
- Use
RELEASE SAVEPOINTwhen you no longer need a savepoint; it frees resources without ending the transaction. - In application code, use your driver's transaction API (for example
connection.begin()in many libraries) rather than raw SQLBEGIN, so the driver tracks transaction state correctly. - Test transaction logic in psql first; it makes the transaction state visible in the prompt and removes driver behaviour from the equation.
- Log the SQL statements your application sends around the failure so you can see whether a commit or rollback happened earlier than expected.
Troubleshooting
If the minimal script in step 8 also fails, check the PostgreSQL server logs for the exact statement and session state; the log usually shows whether a transaction was open. If the error only appears through an application, enable statement logging in the driver and compare the sequence with the working psql script. If a connection pooler is involved, switch to session pooling or pin the connection for the transaction. If a stored procedure raises the error, examine its exception handlers and any nested BEGIN ... EXCEPTION blocks; the handler may be trying to roll back to a savepoint that the caller never created. As a last resort, reproduce the issue on a test database with the same schema and driver version, then simplify the code until the failing statement is isolated.
Frequently Asked Questions
What does error 25005 mean exactly?
It is PostgreSQL's no_active_sql_transaction error, raised when a command that requires an active transaction block (such as a savepoint command) is executed outside one.
Does autocommit cause error 25005?
Yes, indirectly. With autocommit on, each statement runs in its own transaction, so a savepoint created by one statement is gone before the next statement runs.
Can I use savepoints without BEGIN?
No. Savepoints exist only inside an explicit transaction block, so you must issue BEGIN first.
Why does it work in psql but fail in my app?
psql disables autocommit by default, while many drivers and GUI tools enable it. The app is likely committing each statement before the savepoint command runs.
Can connection pooling trigger this error?
Yes, if the pooler switches connections between statements. The savepoint is created on one backend and the rollback is sent to another, which has no active transaction.
Is error 25005 dangerous to my data?
No. It is a transaction-state error, not corruption. The failed statement is rejected, and no partial changes from that statement are applied.