gpt4 book ai didi

postgresql - 为什么不能在 PostgreSQL 过程中使用提交和回滚?

转载 作者:行者123 更新时间:2023-11-29 12:35:59 26 4
gpt4 key购买 nike

我正在 PostgreSQL 数据库上创建过程。我了解到无法在这些过程中使用回滚。

为什么?

是否可以使用commit?

我猜这与 ACID 属性有关,但是如果我们在一个过程中有两个插入操作怎么办。如果第二个失败,第二个会回滚吗?

谢谢。

最佳答案

Postgres' overview通过解释它们的功能与传统存储过程的不同之处给出提示:

Functions created with PL/pgSQL can be used anywhere that built-in functions could be used. For example, it is possible to create complex conditional computation functions and later use them to define operators or use them in index expressions.

这使得在各种可能情况下支持函数内的事务变得很尴尬。来自 the docs :

Functions and trigger procedures are always executed within a transaction established by an outer query... However, a block containing an EXCEPTION clause effectively forms a subtransaction that can be rolled back without affecting the outer transaction.

如果您要在一个函数中有两个 INSERT,第一个可以包装在 EXCEPTION 中 block 以捕获任何错误并决定是否应执行第二个错误。

关于postgresql - 为什么不能在 PostgreSQL 过程中使用提交和回滚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42350723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com