Postgres error 25P02: in failed sql transaction
current transaction is aborted, commands ignored until end of transaction block
An earlier statement in this transaction failed, and Postgres refuses everything else until the transaction ends.
Common causes
- Code caught an error from one statement and carried on using the same transaction.
- A pooled connection was returned with a failed transaction still open.
How to fix it
- Find the first error in the log: that is the real problem. This one is only the consequence.
- Roll back after any error, or use a SAVEPOINT around statements that are allowed to fail.
- Make sure the pool resets connections (ROLLBACK or DISCARD ALL) when they are returned.
begin;
savepoint try_insert;
insert into tags (name) values ('postgres'); -- may fail
rollback to savepoint try_insert; -- the transaction is usable again
commit;Through a REST API
PostgREST, the REST layer behind supabase-js and Scribase, answers HTTP 500 for this error and returns the SQLSTATE in the code field of the JSON error body.
Questions
What does Postgres error 25P02 mean?
25P02 is in_failed_sql_transaction in class 25 (Invalid Transaction State). An earlier statement in this transaction failed, and Postgres refuses everything else until the transaction ends.
How do I fix 25P02?
Find the first error in the log: that is the real problem. This one is only the consequence. Roll back after any error, or use a SAVEPOINT around statements that are allowed to fail. Make sure the pool resets connections (ROLLBACK or DISCARD ALL) when they are returned.
What HTTP status does a REST API return for 25P02?
PostgREST, the REST layer behind supabase-js, answers 500.
Other class 25 errors
- 25006 cannot execute INSERT in a read-only transaction