【问题标题】:Commit and rollback statement (in a child stored procedure) affect main stored procedure in Postregsql?提交和回滚语句(在子存储过程中)会影响 Postgresql 中的主存储过程吗?
【发布时间】:2021-06-01 19:44:29
【问题描述】:

我在 Postgresql 中有 2 个存储过程:sp_mainsp_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


【解决方案1】:

保存点对您没有帮助(在 PL/pgSQL 中,由 BEGIN ... EXCEPTION ... END 块实现)。保存点启动子事务,但如果您ROLLBACK,则包括其所有子事务在内的整个事务将被中止。

如果sp_child 发出ROLLBACK,则无法在sp_main 中保持事务打开。

您将不得不重新设计您的事务处理。现在的方式无论如何都没有意义:sp_child 执行的任何 INSERT 都会回滚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    相关资源
    最近更新 更多