Postgresql 中文操作指南
47.4. Transaction Management #
不可能通过 SPI 函数(如 SPI_execute)运行诸如 COMMIT 和 ROLLBACK 等事务控制命令。然而,有些单独的界面函数允许通过 SPI 进行事务控制。
It is not possible to run transaction control commands such as COMMIT and ROLLBACK through SPI functions such as SPI_execute. There are, however, separate interface functions that allow transaction control through SPI.
在不考虑调用上下文的情况下,在用户自定义的 SQL 可调用函数中启动和结束事务通常是不安全和明智的。例如,在作为某种 SQL 命令的一部分的复杂 SQL 表达式的一部分的函数中设置事务边界很可能会导致模糊的内部错误或崩溃。此处展示的界面函数主要供过程性语言实现用来支持 CALL 命令调用的 SQL 级过程中进行的事务管理,并考虑了 CALL 调用的上下文。使用 SPI 的用 C 实现的过程可以实现相同的逻辑,但这些细节超出了本文件的范围。
It is not generally safe and sensible to start and end transactions in arbitrary user-defined SQL-callable functions without taking into account the context in which they are called. For example, a transaction boundary in the middle of a function that is part of a complex SQL expression that is part of some SQL command will probably result in obscure internal errors or crashes. The interface functions presented here are primarily intended to be used by procedural language implementations to support transaction management in SQL-level procedures that are invoked by the CALL command, taking the context of the CALL invocation into account. SPI-using procedures implemented in C can implement the same logic, but the details of that are beyond the scope of this documentation.