Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17
25P02 is never the original problem. After any error inside BEGIN … COMMIT, PostgreSQL marks the transaction as failed and rejects every following statement with 25P02. Scroll up: the first ERROR in the transaction (23505, 42P01, 23502, 57014 below) is what you need to fix.
In all four cases the final COMMIT printed ROLLBACK: nothing from that transaction was saved, including statements that ran before the error.
01A statement failed (duplicate key) and the next one was sent in the same transaction
The first INSERT hits 23505 because id 1 already exists. The second INSERT (id 2) is valid but is rejected with 25P02, and COMMIT answers ROLLBACK. The table still has only Kim.
Setup
CREATE TABLE members (id int PRIMARY KEY, name text);
INSERT INTO members VALUES (1, 'Kim');CREATE TABLE
INSERT 0 1BEGIN;
INSERT INTO members VALUES (1, 'Kim');
INSERT INTO members VALUES (2, 'Lee');
COMMIT;BEGIN
ERROR: 23505: duplicate key value violates unique constraint "members_pkey"
DETAIL: Key (id)=(1) already exists.
SCHEMA NAME: lab_a
TABLE NAME: members
CONSTRAINT NAME: members_pkey
ERROR: 25P02: current transaction is aborted, commands ignored until end of transaction block
ROLLBACKSELECT id, name FROM members ORDER BY id; id | name
----+------
1 | Kim
(1 row)Fix the first error. Here the existing row is expected, so ON CONFLICT (id) DO NOTHING skips it (INSERT 0 0), the next INSERT runs, and COMMIT really commits. Otherwise, after an error send ROLLBACK and start the transaction again.
BEGIN;
INSERT INTO members VALUES (1, 'Kim') ON CONFLICT (id) DO NOTHING;
INSERT INTO members VALUES (2, 'Lee');
COMMIT;
SELECT id, name FROM members ORDER BY id;BEGIN
INSERT 0 0
INSERT 0 1
COMMIT
id | name
----+------
1 | Kim
2 | Lee
(2 rows)02A typo in a migration script run inside one transaction
The script wraps DDL in BEGIN … COMMIT. ALTER TABLE ordres fails with 42P01 (typo), so CREATE INDEX gets 25P02 and the whole script is rolled back. The orders columns are unchanged (id, amount).
Setup
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);CREATE TABLEBEGIN;
ALTER TABLE ordres ADD COLUMN status text;
CREATE INDEX orders_status_idx ON orders (status);
COMMIT;BEGIN
ERROR: 42P01: relation "ordres" does not exist
ERROR: 25P02: current transaction is aborted, commands ignored until end of transaction block
ROLLBACKSELECT column_name FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'orders' ORDER BY ordinal_position; column_name
-------------
id
amount
(2 rows)Correct the table name and run the whole script again. The ALTER and the index are created together. Running migrations in one transaction is what kept the table from being half-changed.
BEGIN;
ALTER TABLE orders ADD COLUMN status text;
CREATE INDEX orders_status_idx ON orders (status);
COMMIT;BEGIN
ALTER TABLE
CREATE INDEX
COMMIT03One bad row in a batch load aborts all rows
Rows are inserted one by one in a transaction. A-1 succeeds (INSERT 0 1), A-2 has a NULL price (23502), A-3 gets 25P02, and the rollback also removes A-1 — products has 0 rows.
Setup
CREATE TABLE products (sku text PRIMARY KEY, price numeric NOT NULL);
CREATE TABLE staging (sku text, price numeric);
INSERT INTO staging VALUES ('A-1', 100), ('A-2', NULL), ('A-3', 300);CREATE TABLE
CREATE TABLE
INSERT 0 3BEGIN;
INSERT INTO products SELECT sku, price FROM staging WHERE sku = 'A-1';
INSERT INTO products SELECT sku, price FROM staging WHERE sku = 'A-2';
INSERT INTO products SELECT sku, price FROM staging WHERE sku = 'A-3';
COMMIT;BEGIN
INSERT 0 1
ERROR: 23502: null value in column "price" of relation "products" violates not-null constraint
DETAIL: Failing row contains (A-2, null).
SCHEMA NAME: lab_a
TABLE NAME: products
COLUMN NAME: price
ERROR: 25P02: current transaction is aborted, commands ignored until end of transaction block
ROLLBACKSELECT count(*) FROM products; count
-------
0
(1 row)If bad rows should be skipped, give each row its own sub-transaction. In PL/pgSQL a BEGIN … EXCEPTION … END block does this: A-2 is reported as skip A-2, and A-1 and A-3 are saved. Check the skipped rows afterwards.
DO $$
DECLARE r record;
BEGIN
FOR r IN SELECT sku, price FROM staging ORDER BY sku LOOP
BEGIN
INSERT INTO products VALUES (r.sku, r.price);
EXCEPTION WHEN not_null_violation THEN
RAISE NOTICE 'skip %', r.sku;
END;
END LOOP;
END $$;
SELECT sku, price FROM products ORDER BY sku;NOTICE: 00000: skip A-2
DO
sku | price
-----+-------
A-1 | 100
A-3 | 300
(2 rows)04A slow statement inside the transaction is cancelled by statement_timeout
The user has statement_timeout set to 1 second, and a statement in the middle of the transaction (here pg_sleep(2), in real life a heavy query or aggregation) took 2 seconds. It was cancelled with 57014 (canceling statement due to statement timeout), the whole transaction became aborted, and the next UPDATE got 25P02.
The final COMMIT ended as ROLLBACK, so even the UPDATE of row 1 was lost (all three rows are new). The first error above the 25P02 is the real cause.
Setup
ALTER ROLE lab_a SET statement_timeout = '1s';ALTER ROLECREATE TABLE orders (id int PRIMARY KEY, status text);
INSERT INTO orders SELECT g, 'new' FROM generate_series(1, 3) g;CREATE TABLE
INSERT 0 3BEGIN;
UPDATE orders SET status = 'paid' WHERE id = 1;
SELECT pg_sleep(2);
UPDATE orders SET status = 'paid' WHERE id = 2;
COMMIT;
SELECT id, status FROM orders ORDER BY id;BEGIN
UPDATE 1
ERROR: 57014: canceling statement due to statement timeout
ERROR: 25P02: current transaction is aborted, commands ignored until end of transaction block
ROLLBACK
id | status
----+--------
1 | new
2 | new
3 | new
(3 rows)SHOW statement_timeout;
SELECT rolname, rolconfig FROM pg_roles WHERE rolname = current_user; statement_timeout
-------------------
1s
(1 row)
rolname | rolconfig
---------+------------------------
lab_a | {statement_timeout=1s}
(1 row)statement_timeout is 1s, and pg_roles.rolconfig contains {statement_timeout=1s}, so every session of this user gets the 1-second limit.
ALTER ROLE lab_a SET statement_timeout = '5s';ALTER ROLEAfter changing the user's statement_timeout to 5 seconds and reconnecting, the same transaction reached COMMIT and rows 1 and 2 became paid. Before raising a limit, check whether that statement really needs that long.
BEGIN;
UPDATE orders SET status = 'paid' WHERE id = 1;
SELECT pg_sleep(2);
UPDATE orders SET status = 'paid' WHERE id = 2;
COMMIT;
SELECT id, status FROM orders ORDER BY id;BEGIN
UPDATE 1
pg_sleep
----------
(1 row)
UPDATE 1
COMMIT
id | status
----+--------
1 | paid
2 | paid
3 | new
(3 rows)FAQ
How do I fix 'current transaction is aborted, commands ignored until end of transaction block'?
Run ROLLBACK, then find and fix the first error that happened in that transaction. 25P02 only means a previous statement failed.
Why did COMMIT return ROLLBACK?
The transaction had already failed. PostgreSQL rolls it back on COMMIT, so nothing in it was saved, even the statements that succeeded.
How can I continue a transaction after an error in PostgreSQL?
Fix the failing statement, or wrap each risky statement in a sub-transaction. In PL/pgSQL a BEGIN … EXCEPTION block does this, as in the batch example: the bad row is skipped and the others are saved.
Why does my app keep getting 25P02 on every query?
The connection is still inside the failed transaction. Make sure the code calls ROLLBACK when a statement throws, before reusing the connection.
After a statement timeout, every query says current transaction is aborted.
When statement_timeout (57014) cancels a statement, the transaction becomes aborted and every statement is rejected with 25P02 until ROLLBACK. In the run above even COMMIT ended as ROLLBACK. Check the limit with SHOW statement_timeout; and pg_roles.rolconfig.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12