英文:
PL/pgSQL transaction behavior when function calls another
问题
I have function A calling function B.
Does the transaction support of A, include B? Meaning B succeeds, but after that A fails.
CREATE OR REPLACE FUNCTION A(<params>)
RETURNS bool as
$$
DECLARE
<>
BEGIN
<..>
PERFORM "B"(<params>);
<..> -- error occurs here
END;
$$ LANGUAGE plpgsql;
I don't want B to be committed. Is this implicitly guaranteed or do I need to explicitly take care of things (if so, how should I?)
英文:
I have function A calling function B.
Does the transaction support of A, include B? Meaning B succeeds, but after that A fails.
CREATE OR REPLACE FUNCTION A(<params>)
RETURNS bool as
$$
DECLARE
<>
BEGIN
<..>
PERFORM "B"(<params>);
<..> -- error occurs here
END;
$$ LANGUAGE plpgsql;
I don't want B to be committed. Is this implicitly guaranteed or do I need to explicitly take care of things (if so, how should I?)
答案1
得分: 1
这是隐式保证的。在 PostgreSQL 中,几乎所有的 SQL 语句都在一个单一事务中运行,INSERT
、UPDATE
、DELETE
和 SELECT
一直都是如此。一个函数总是从这些语句之一中调用,因此该函数及其调用的所有函数都会在与调用该函数的语句相同的单一事务中再次运行。如果有一个 ROLLBACK
,整个语句以及它直接或间接调用的所有函数的效果都会被回滚。
英文:
That is implicitly guaranteed. In PostgreSQL, almost all SQL statements run in a single transaction, and INSERT
, UPDATE
, DELETE
and SELECT
always do. A function always gets called from one of these statements, so that function and all the functions it calls again run in the same single transaction as the statement that called the function. If there is a ROLLBACK
the whole statement and the effects of all the functions it called directly or indirectly get rolled back.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论