PostgreSQL 25P02: current transaction is aborted
An earlier statement in this transaction failed, so PostgreSQL refuses everything else until you ROLLBACK (or roll back to a savepoint).
- SQLSTATE
25P02- Name
in_failed_sql_transaction- Category
- Invalid Transaction State
What it means
In PostgreSQL, any error inside a transaction block puts the whole transaction into a failed state. From then on every statement except ROLLBACK (or ROLLBACK TO SAVEPOINT) fails with 25P02 without running. COMMIT on a failed transaction does not commit; it ends the transaction and reports ROLLBACK.
25P02 is never the real problem. It is the echo of an error that happened earlier in the same transaction.
Common causes
- A statement failed earlier in the transaction (a constraint violation, a typo, a bad cast) and the code kept going without rolling back.
- An application catches an exception from one query and reuses the same connection, still inside the failed transaction.
- A connection pool hands out a connection that was left in a failed transaction by its previous user.
How to fix it
-
Look for the first error in the transaction, in your application log or the server log, and fix that.
-
End the failed transaction with
ROLLBACK, then start again.ROLLBACK;
-
If some statements are allowed to fail without losing the rest of the transaction, wrap each one in a savepoint.
BEGIN; INSERT INTO audit_log (msg) VALUES ('start'); SAVEPOINT try_insert; INSERT INTO users (id, email) VALUES (1, '[email protected]'); -- may fail -- on error: ROLLBACK TO SAVEPOINT try_insert; COMMIT; -- audit_log row is kept -
In
psql,\set ON_ERROR_ROLLBACK interactivemakes psql set an implicit savepoint before each statement, so one typo doesn't abort an interactive transaction.
Reproduce it safely
BEGIN; SELECT 1/0; -- ERROR: division by zero SELECT 1; -- ERROR: current transaction is aborted ... ROLLBACK;
Questions
Why does COMMIT say ROLLBACK?
The transaction had already failed, so there is nothing valid to commit. PostgreSQL ends it and reports ROLLBACK to make that clear.
Related errors
40P01deadlock detected23505duplicate key violates unique constraint22012division by zero3930SQL Server: Transaction cannot be committed
More invalid transaction state errors
25001cannot run inside a transaction block25006read-only SQL transaction25P03idle-in-transaction session timeout
The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.
Work on PostgreSQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.