0B000
PostgreSQLERRORNotableInvalid Transaction InitiationHIGH confidence

invalid transaction initiation

What this means

SQLSTATE 0B000 is raised when a transaction-control command (e.g., BEGIN) is issued in a context where a new transaction cannot be initiated — for example, inside a function that is already within an implicit transaction.

Why it happens
  1. 1Issuing BEGIN inside a PL/pgSQL function or trigger (which already runs inside a transaction)
  2. 2Attempting to start a nested transaction using BEGIN where only SAVEPOINTs are permitted
How to reproduce

Calling BEGIN inside a PL/pgSQL function.

trigger — this will error
trigger — this will error
CREATE OR REPLACE FUNCTION bad_begin() RETURNS VOID AS $
BEGIN
  BEGIN; -- invalid: already inside a transaction
END;
$ LANGUAGE plpgsql;

expected output

ERROR:  cannot begin/end transactions in PL/pgSQL

Fix 1

Use SAVEPOINTs instead of nested BEGIN

WHEN When you need nested transaction semantics inside a function.

Use SAVEPOINTs instead of nested BEGIN
SAVEPOINT my_save;
-- ... work ...
ROLLBACK TO SAVEPOINT my_save;
RELEASE SAVEPOINT my_save;

Why this works

SAVEPOINTs provide rollback-to-point semantics within the enclosing transaction without requiring a new BEGIN.

Fix 2

Use procedures with autonomous transaction control (Postgres 11+)

WHEN When the function genuinely needs to commit work independently.

Use procedures with autonomous transaction control (Postgres 11+)
CREATE PROCEDURE my_proc() LANGUAGE plpgsql AS $
BEGIN
  -- work ...
  COMMIT;
END $;

Why this works

Procedures (not functions) can call COMMIT and ROLLBACK in Postgres 11+.

Version notes
Postgres 11+

Procedures can use COMMIT/ROLLBACK. Functions still cannot.

Sources
Official documentation ↗

Class 0B — Invalid Transaction Initiation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors