PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 2D000: invalid transaction termination

A COMMIT inside PL/pgSQL ran in a place where PostgreSQL does not allow ending the transaction.

PostgreSQL 17.12
2D000: invalid transaction termination

Jump to a cause

  1. 01The procedure was called inside BEGIN ... ROLLBACK
  2. 02COMMIT written inside a function (CREATE FUNCTION)
  3. 03COMMIT inside a BEGIN ... EXCEPTION block

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

Since PostgreSQL 11, a procedure run with CALL can use COMMIT and ROLLBACK. But only under conditions: the CALL must not be inside a transaction block you opened, the code must be a procedure (not a function), and the COMMIT must not be inside a BEGIN ... EXCEPTION block. Otherwise you get 2D000.

The CONTEXT line points to the COMMIT, for example PL/pgSQL function batch_insert() line 5 at COMMIT. In the third case below the message itself is different: cannot commit while a subtransaction is active. Below are the three cases we reproduced on PostgreSQL 17, each with the fix actually run.

01The procedure was called inside BEGIN ... ROLLBACK

batch_insert() commits after each insert. Running BEGIN; CALL batch_insert(); failed with invalid transaction termination at the COMMIT on line 5: when CALL runs inside a transaction block you started, the procedure cannot commit it.

The catalog confirmed it is a real procedure (prokind = p), so nothing was wrong with the procedure itself. Calling it on its own, without BEGIN, ran to CALL and inserted row 1 to row 3.

Setup
Setuprun as lab_a
CREATE TABLE logs (id int GENERATED ALWAYS AS IDENTITY, msg text);
CREATE PROCEDURE batch_insert() LANGUAGE plpgsql AS $$
BEGIN
  FOR i IN 1..3 LOOP
    INSERT INTO logs (msg) VALUES ('row ' || i);
    COMMIT;
  END LOOP;
END $$;
OUTPUT
CREATE TABLE
CREATE PROCEDURE
Reproducerun as lab_a
BEGIN;
CALL batch_insert();
ROLLBACK;
✕ ERROR
BEGIN
ERROR:  2D000: invalid transaction termination
CONTEXT:  PL/pgSQL function batch_insert() line 5 at COMMIT
ROLLBACK
Checkrun as lab_a
SELECT proname, prokind FROM pg_proc WHERE proname = 'batch_insert';
OUTPUT
   proname    | prokind
--------------+---------
 batch_insert | p
(1 row)

If the procedure commits by itself, call it outside any explicit transaction.

After the fixrun as lab_a
CALL batch_insert();
SELECT id, msg FROM logs ORDER BY id;
✓ SUCCESS
CALL
 id |  msg
----+-------
  5 | row 1
  6 | row 2
  7 | row 3
(3 rows)

02COMMIT written inside a function (CREATE FUNCTION)

add_log was created with CREATE FUNCTION and has a COMMIT. SELECT add_log('hello') failed at that COMMIT (line 4 at COMMIT). A function always runs inside the caller's transaction and cannot end it.

We dropped the function and recreated the same body as CREATE PROCEDURE add_log(p_msg text). CALL add_log('hello') succeeded and the row hello was saved.

Setup
Setuprun as lab_a
CREATE TABLE logs (id int GENERATED ALWAYS AS IDENTITY, msg text);
CREATE FUNCTION add_log(p_msg text) RETURNS void LANGUAGE plpgsql AS $$
BEGIN
  INSERT INTO logs (msg) VALUES (p_msg);
  COMMIT;
END $$;
OUTPUT
CREATE TABLE
CREATE FUNCTION
Reproducerun as lab_a
SELECT add_log('hello');
✕ ERROR
ERROR:  2D000: invalid transaction termination
CONTEXT:  PL/pgSQL function add_log(text) line 4 at COMMIT
Fixrun as lab_a
DROP FUNCTION add_log(text);
CREATE PROCEDURE add_log(p_msg text) LANGUAGE plpgsql AS $$
BEGIN
  INSERT INTO logs (msg) VALUES (p_msg);
  COMMIT;
END $$;
OUTPUT
DROP FUNCTION
CREATE PROCEDURE

If you do not really need to commit midway, just remove the COMMIT and keep it a function.

After the fixrun as lab_a
CALL add_log('hello');
SELECT id, msg FROM logs;
✓ SUCCESS
CALL
 id |  msg
----+-------
  5 | hello
(1 row)

03COMMIT inside a BEGIN ... EXCEPTION block

Here the message is cannot commit while a subtransaction is active, still SQLSTATE 2D000. The loop body was wrapped in BEGIN ... EXCEPTION WHEN unique_violation ... END, and the COMMIT was inside it. A block with an EXCEPTION clause runs as a subtransaction, and you cannot commit while it is open.

We moved the COMMIT after the inner END;, still inside the loop. CALL batch_insert() then completed and inserted row 1 to row 3, still committing once per loop iteration.

Setup
Setuprun as lab_a
CREATE TABLE logs (id int GENERATED ALWAYS AS IDENTITY, msg text);
CREATE PROCEDURE batch_insert() LANGUAGE plpgsql AS $$
BEGIN
  FOR i IN 1..3 LOOP
    BEGIN
      INSERT INTO logs (msg) VALUES ('row ' || i);
      COMMIT;
    EXCEPTION WHEN unique_violation THEN
      RAISE NOTICE 'skip %', i;
    END;
  END LOOP;
END $$;
OUTPUT
CREATE TABLE
CREATE PROCEDURE
Reproducerun as lab_a
CALL batch_insert();
✕ ERROR
ERROR:  2D000: cannot commit while a subtransaction is active
CONTEXT:  PL/pgSQL function batch_insert() line 6 at COMMIT
Fixrun as lab_a
CREATE OR REPLACE PROCEDURE batch_insert() LANGUAGE plpgsql AS $$
BEGIN
  FOR i IN 1..3 LOOP
    BEGIN
      INSERT INTO logs (msg) VALUES ('row ' || i);
    EXCEPTION WHEN unique_violation THEN
      RAISE NOTICE 'skip %', i;
    END;
    COMMIT;
  END LOOP;
END $$;
OUTPUT
CREATE PROCEDURE
After the fixrun as lab_a
CALL batch_insert();
SELECT id, msg FROM logs ORDER BY id;
✓ SUCCESS
CALL
 id |  msg
----+-------
  5 | row 1
  6 | row 2
  7 | row 3
(3 rows)

FAQ

What does "invalid transaction termination" mean in PostgreSQL?

A COMMIT or ROLLBACK ran inside PL/pgSQL where the transaction cannot be ended: in a function, or in a procedure called inside an explicit transaction block.

Can I COMMIT inside a PostgreSQL function?

No. In this test the COMMIT in a function failed with 2D000. Recreating it as a procedure and running it with CALL worked.

Why does CALL fail with 2D000 even though it is a procedure?

Because it was called inside BEGIN. The same procedure succeeded when called without an explicit transaction.

How do I fix "cannot commit while a subtransaction is active"?

Move the COMMIT out of the BEGIN ... EXCEPTION ... END block. Putting it right after the block's END; inside the loop worked here.

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