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
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 $$;CREATE TABLE
CREATE PROCEDUREBEGIN;
CALL batch_insert();
ROLLBACK;BEGIN
ERROR: 2D000: invalid transaction termination
CONTEXT: PL/pgSQL function batch_insert() line 5 at COMMIT
ROLLBACKSELECT proname, prokind FROM pg_proc WHERE proname = 'batch_insert'; proname | prokind
--------------+---------
batch_insert | p
(1 row)If the procedure commits by itself, call it outside any explicit transaction.
CALL batch_insert();
SELECT id, msg FROM logs ORDER BY id;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
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 $$;CREATE TABLE
CREATE FUNCTIONSELECT add_log('hello');ERROR: 2D000: invalid transaction termination
CONTEXT: PL/pgSQL function add_log(text) line 4 at COMMITDROP FUNCTION add_log(text);
CREATE PROCEDURE add_log(p_msg text) LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO logs (msg) VALUES (p_msg);
COMMIT;
END $$;DROP FUNCTION
CREATE PROCEDUREIf you do not really need to commit midway, just remove the COMMIT and keep it a function.
CALL add_log('hello');
SELECT id, msg FROM logs;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
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 $$;CREATE TABLE
CREATE PROCEDURECALL batch_insert();ERROR: 2D000: cannot commit while a subtransaction is active
CONTEXT: PL/pgSQL function batch_insert() line 6 at COMMITCREATE 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 $$;CREATE PROCEDURECALL batch_insert();
SELECT id, msg FROM logs ORDER BY id;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