PL/pgSQL函数调用另一个函数时的事务行为

huangapple go评论68阅读模式
英文:

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.

  1. CREATE OR REPLACE FUNCTION A(<params>)
  2. RETURNS bool as
  3. $$
  4. DECLARE
  5. <>
  6. BEGIN
  7. <..>
  8. PERFORM "B"(<params>);
  9. <..> -- error occurs here
  10. END;
  11. $$ 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.

  1. CREATE OR REPLACE FUNCTION A(&lt;params&gt;)
  2. RETURNS bool as
  3. $$
  4. DECLARE
  5. &lt;&gt;
  6. BEGIN
  7. &lt;..&gt;
  8. PERFORM &quot;B&quot;(&lt;params&gt;);
  9. &lt;..&gt; -- error occurs here
  10. END;
  11. $$ 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 语句都在一个单一事务中运行,INSERTUPDATEDELETESELECT 一直都是如此。一个函数总是从这些语句之一中调用,因此该函数及其调用的所有函数都会在与调用该函数的语句相同的单一事务中再次运行。如果有一个 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.

huangapple
  • 本文由 发表于 2023年5月28日 16:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定