【发布时间】:2021-06-01 19:44:29
【问题描述】:
我在 Postgresql 中有 2 个存储过程:sp_main、sp_child。
sp_main 在过程中调用sp_child。问题是,每当sp_child input1 = -1 时,它就会回滚sp_main 第一个插入语句。
在这种情况下我该如何使用savepoint 语句?
问题总结:
call1() --> call2() (call2 will have commit/rollback but will not affect call1)
代码:
CREATE OR REPLACE PROCEDURE sp_main(INOUT main_result integer)
LANGUAGE plpgsql
AS $$
begin
declare resultc integer
resultc=0;
---
---first insert
insert into table1
values(-1,'a'); --input1=-1
---
call sp_child(1,resultc); --call child sp.
main_result=resultc;
---
---last insert
insert into table2
values(-7,'a');
commit;
END ;
$$
;
CREATE OR REPLACE PROCEDURE sp_child(input1 integer DEFAULT 0,INOUT v_result integer DEFAULT 5)
LANGUAGE plpgsql
AS $$
begin
v_result=1;
---
---query_child
insert into table2
values(-10,'a');
---
if input1=-1 then
Rollback;
end if;
END ;
$$
;
call sp_main(1);
【问题讨论】:
-
你使用的postgresql是什么版本?
-
版本 13 @mshabou
-
看看 pg_background 扩展:dpavlin.wordpress.com/2017/05/09/…
标签: postgresql