Postgres 11 - COMMIT Procedure


Postgres 11 - COMMIT Procedure



With the recent update of Postres 11. It now supports stored procedures. Transactions are finally supported in stored procedures in Postgres. However, I have been trying to perform a commit within a procedure with no luck.


CREATE OR REPLACE PROCEDURE test_update(
IN pid character,
IN pcategoryname character varying,
IN pwebcode character varying,
IN porder numeric,
IN pupdatedby character varying,
IN pupdateddate timestamp without time zone,
IN plang character varying,
INOUT cresults refcursor)
LANGUAGE plpgsql
AS
$BODY$
DECLARE

cnt bigint;
rtnCode char(1);

BEGIN
cresults := 'cur';

BEGIN
update test_table
set FCATEGORYNAME = pCategoryName,
FWEBCODE = pWebCode,
FORDER = pOrder,
FUPDATEDBY = pUpdatedBy,
FUPDATEDDATE = pUpdatedDate,
FLANGCODE = pLang
where lower(FID) = lower(pId);
COMMIT;
end;
.
.
EXCEPTION WHEN ....
.
.
OPEN cresults FOR VALUES ('stringresult');

end;
$BODY$;



UPDATED
Edited so that the Update and Commit is within a block that does not have any exception.



This was previously used to execute the procedure. I was able to fetch the result of the refcursor. Does not work anymore after the commit is added.:


begin;
CALL testupdate('ad3caecb-9235-4945-b37a-9b7ff89fdfe0','aa','138',0,'test','2018/06/29 18:04:03','zh-cn','');
fetch all in cur;
commit;



but when i execute with:


CALL testupdate('ad3caecb-9235-4945-b37a-9b7ff89fdfe0','aa','138',0,'test','2018/06/29 18:04:03','zh-cn','');



.. it runs fine and the row got updated, but this way of running doesnt allow me to fetch the result my refcursor.



Is there any way to get the result of my refcursor when excuting a procedure with COMMIT. Any help is greatly appreciated. Thanks





And which exception in throws exactly?
– JosMac
Jun 29 at 10:46





Exception: SQL Error: invalid transaction termination at line 19 COMMIT
– deviantxdes
Jun 29 at 10:52






Quote from the manual: "A transaction cannot be ended inside a block with exception handlers." - you need to put the update and commit into a block without an exception handler (currently your procedure is a single block that has an exception handler)
– a_horse_with_no_name
Jun 29 at 10:55


update


commit





Okay removing the exeption does work if i run "Call <proc>" alone, Thanks!, But when i run "begin; Call <proc>; fetch all in cur; commit;" the same exception error appears. I have to call it the latter way to get the result of the cur. is this possible?
– deviantxdes
Jun 29 at 11:02






@a_horse_with_no_name thanks for your input. i have updated my question
– deviantxdes
Jun 29 at 11:08









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV