About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.
ERROR: current transaction is aborted, commands ignored until end of transaction block is the second error, not the first. An earlier statement in the same transaction failed, PostgreSQL marked the whole transaction as failed, and it now refuses everything except the commands that end it. The cause is the earlier error, which is in the server log just before this one. Find it, fix it, and then end the failed transaction as the Fix section describes, with ROLLBACK and the warning that goes with it.
What the error means
The message carries SQLSTATE 25P02, named in_failed_sql_transaction in the Appendix A table, in the class "Invalid Transaction State". PostgreSQL 17 source (tag REL_17_9) raises it in postgres.c when a command arrives while the transaction is in the aborted state. That is our reading of the code. The same code can add a detail line, Abort reason: recovery conflict, when a standby cancelled the earlier query.
The Transactions page of the manual shows what ends the state. Inside a transaction block, ROLLBACK TO a savepoint "is the only way to regain control of a transaction block that was put in aborted state by the system due to an error, short of rolling it back completely and starting again". The rest of the manual's description of ROLLBACK is plain: it "rolls back the current transaction and causes all the updates made by the transaction to be discarded".
The pg_stat_activity view shows the state from outside. Its manual page describes idle in transaction (aborted) as "similar to idle in transaction, except one of the statements in the transaction caused an error".
In psql with autocommit off, the manual says "you must explicitly abandon any failed transaction by entering ABORT or ROLLBACK".
Likely causes
| Cause | What you see | How to confirm |
|---|---|---|
| The application caught an earlier error and carried on in the same transaction | One real error, then 25P02 on every later statement | The log shows an ERROR from the same process just before the first 25P02 |
A psql script with BEGIN has no stop on error |
A cascade of 25P02 lines after one failure | The script output starts with one different error |
| A connection pool or driver returns a failed transaction to the pool (our own reading) | Sessions in idle in transaction (aborted) |
pg_stat_activity shows that state for old sessions |
| The first failure was expected (a duplicate row, a missing table) and the code relied on it (our own reading) | The first error is a routine one such as 23505 | The code catches and ignores that error |
| A standby cancelled the earlier query | The detail line says Abort reason: recovery conflict |
See PostgreSQL "canceling statement due to conflict with recovery": fix standby query cancellations |
Prove the cause before changing anything
Everything here only reads.
1. Find the first error in the same session. Take the process ID from the log line of the 25P02 error. The log_line_prefix setting must print it: the manual lists %p as the process ID. Then read the lines of that process before it:
grep -n "\[<pid>\]" <logfile>
The server log records the statement that failed when its level is at or above log_min_error_statement, whose default is ERROR, so the line before the first 25P02 normally names the failing statement. A good result is one clear ERROR followed by the aborted-transaction lines. A bad result is no earlier error at all: then the failing statement was logged on another connection or the log prefix does not carry the process ID. Changing the prefix is a configuration change and is not part of this check.
2. Look for sessions stuck in the failed state.
SELECT pid, usename, application_name, state, xact_start, query
FROM pg_stat_activity
WHERE state = 'idle in transaction (aborted)';
A good result is no rows. A bad result is rows whose xact_start is minutes old: the client received an error and never sent ROLLBACK, so it holds a connection (and, our reading, any locks the earlier statements took) in a dead transaction.
3. Check how the client starts transactions. The manual's psql page describes this behaviour for AUTOCOMMIT off: it "works by issuing an implicit BEGIN for you". Some drivers and frameworks do the same (our own reading). Read the application's connection settings for autocommit, and its error handling around each statement.
Fix
Fix the first error first. Ending the transaction only clears the aftermath.
End the failed transaction
The statement ROLLBACK; ends the failed transaction and discards what it did.
First save the current state. With the default log_min_error_statement of ERROR, the log lines record the failing statement and its error, not the earlier statements of the transaction. They are a record of the first error, not a copy of the work:
grep -n "\[<pid>\]" <logfile> > failed-tx-before.txt
Risk: the next statement discards every change made earlier in the failed transaction. A rolled-back transaction cannot be recovered. Those statements are no longer recoverable from the database, and the saved log lines name only the first error. The work is rerun from the application's own record of the transaction, such as the input file or the queue message it was built from.
Send it from the client that holds the transaction:
ROLLBACK;
Only if you need to back out: there is none for the discarded work. After the first error named in failed-tx-before.txt is fixed, run the whole transaction again from the application's own input.
Stop a psql script at its first error
The manual says ON_ERROR_STOP makes psql "stop immediately" at the first error, instead of continuing, which is what produces the cascade.
First save the current state:
cp -p import.sql import.sql.2026-12-03
Risk: the next edit adds a line that makes psql stop at the first error in import.sql. A script that used to run past harmless errors now ends early, and, run as a script, exits with code 3, "to distinguish this case from fatal error conditions".
Add this as the first line of the script (our own rendering of the manual's \set command for this variable):
\set ON_ERROR_STOP on
Only if you need to back out: the copy below overwrites the edited import.sql with the saved one, so any other change made to the script since the save is lost. Copy the saved file back.
cp -p import.sql.2026-12-03 import.sql
Use savepoints where one failure is acceptable
The application can wrap a statement that may fail in a savepoint. The manual's example shows the pattern: SAVEPOINT my_savepoint;, the risky statement, and on failure ROLLBACK TO SAVEPOINT my_savepoint;. The transaction then continues, and the work before the savepoint is kept. A savepoint is a change to application code, so the application owner writes and tests it. The manual says ROLLBACK TO SAVEPOINT "implicitly destroys all savepoints that were established after the named savepoint".
psql's ON_ERROR_ROLLBACK does the same thing for a script, by issuing "an implicit SAVEPOINT for you, just before each command". With it on, the manual says "the error is ignored and the transaction continues". That also ignores real errors, and the manual's default is off. Our own advice: use it only in interactive sessions, where a person reads each result.
Example (illustrative): On app01, an import job loads 500 rows into db01 in one transaction. Row 37 repeats an existing key and fails with duplicate key value violates unique constraint. The code logs the error and carries on. Rows 38 to 500 each return current transaction is aborted, commands ignored until end of transaction block, which is 500 - 37 = 463 of them, plus the first error: 464 error lines for one bad row. The log search finds the 23505 error first. The code is changed to run each row inside a savepoint and to skip the one duplicate, so 499 rows commit.
Confirm the fix
- Repeat prove step 2. No session is in
idle in transaction (aborted). - Rerun the job. The log has no 25P02 lines, and any real error is a single line followed by the handling you chose.
- If you added
ON_ERROR_STOP, a deliberate bad statement now ends the script at once, and psql run as a script exits with code 3.
What to watch afterwards
- The count of 25P02 in the log, per hour. Each one means some client kept sending statements after a failure.
- Sessions held in the aborted state may keep the locks their earlier statements took until the client ends the transaction (our reading), so a growing count can also show up as lock waits elsewhere. See PostgreSQL ERROR: deadlock detected: read the log, then fix the lock order for lock conflicts between live transactions.
- The first error in the chain is often one of the retryable kinds. A serialization failure is retried by running the whole transaction again; see PostgreSQL "ERROR: could not serialize access due to concurrent update" (40001): retry the whole transaction.
Read next
Related articles you may want next.
- TroubleshootingPostgreSQL ERROR: deadlock detected: read the log, then fix the lock orderPostgreSQL 'ERROR: deadlock detected' (40P01): read the server log, find the two statements, fix the lock order and retry the aborted transaction.Read article
- TroubleshootingPostgreSQL "ERROR: could not serialize access due to concurrent update" (40001): retry the whole transactionPostgreSQL 'could not serialize access due to concurrent update' (40001)? Read the isolation level, find the overlap, and retry the whole transaction.Read article
- TroubleshootingPostgreSQL "canceling statement due to conflict with recovery": fix standby query cancellationsStandby queries failing with 'canceling statement due to conflict with recovery'? Read pg_stat_database_conflicts, then tune delay or feedback.Read article
Risks and limits
- These commands have not been run or lab-tested.
ROLLBACKcannot be taken back. The saved log lines are a record, not a way to restore the data.- Savepoints inside a transaction are not a substitute for fixing the failing statement.
- The message text is a reading of PostgreSQL 17 source; other releases may word it differently.
Sources
- 3.4. Transactions — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- SAVEPOINT — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- ROLLBACK — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- ROLLBACK TO SAVEPOINT — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- psql — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- 27.2. The Cumulative Statistics System — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- 19.8. Error Reporting and Logging — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- Appendix A. PostgreSQL Error Codes — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- postgres.c — PostgreSQL source repository, tag REL_17_9, no date shown, accessed 3 December 2026
This article was originally published on .