Can you please guide what is wrong with below code which gives an issue with below error:
ERROR: invalid transaction termination.
CREATE OR REPLACE PROCEDURE "app"."sproc_data_create"
( name varchar(100))
LANGUAGE plpgsql
AS
$$
Declare idIdentity int;
BEGIN
INSERT INTO data(Name) VALUES (name) RETURNING Id into idIdentity;
INSERT INTO relationalData(dataId) VALUES (idIdentity);
COMMIT;
END
For below scenarios, this does not work executing commit for one insert statement and expected to rollback second insert statement. This rollback both insert statements.
BEGIN
INSERT INTO data(Name) VALUES (name) RETURNING Id into idIdentity;
COMMIT;
INSERT INTO relationalData(dataId) VALUES (idIdentity);
END
Execute below script:
/*
do
begin
CALL "app"."sproc_data_create"("name field")
end
*/
//invalid transaction termination
CONTEXT: PL/pgSQL function
Found an issue, I was executing "call" procedure statement inside DO BEGIN/END statement and that creates parent transaction scope.
As found on below post. (Basically, "call" has parent transaction scope and I use commit/rollback under subTransaction scope within procedure. By removing DO BEGIN/END wrapper from "call" statement, it resolves the issue.
Can a Postgres Commit Exist in Procedure that has an Exception Block?
Thanks