Postgresql 中文操作指南
Chapter 47. Server Programming Interface
Table of Contents
Server Programming Interface (SPI) 使用户自定义 C 函数的编写者能够在其函数或过程中运行 SQL 命令。SPI 是一组界面函数,用于简化对解释器、规划器和执行器的访问。SPI 还会进行一些内存管理。
The Server Programming Interface (SPI) gives writers of user-defined C functions the ability to run SQL commands inside their functions or procedures. SPI is a set of interface functions to simplify access to the parser, planner, and executor. SPI also does some memory management.
Note
可用的过程语言提供了各种方法来从函数执行 SQL 命令。这些功能中的大多数都是基于 SPI 的,因此本文档可能对这些语言的用户也有用。
The available procedural languages provide various means to execute SQL commands from functions. Most of these facilities are based on SPI, so this documentation might be of use for users of those languages as well.
请注意,如果通过 SPI 调用的命令失败,那么不会将控制权返回给您的 C 函数。相反,将回滚您的 C 函数执行所在的事务或子事务。(给定 SPI 函数大多有记录的错误返回约定,这可能看起来令人惊讶。但是,这些约定仅适用于 SPI 函数本身中检测到的错误。)可以通过在可能失败的 SPI 调用周围建立自己的子事务来在错误后恢复控制。
Note that if a command invoked via SPI fails, then control will not be returned to your C function. Rather, the transaction or subtransaction in which your C function executes will be rolled back. (This might seem surprising given that the SPI functions mostly have documented error-return conventions. Those conventions only apply for errors detected within the SPI functions themselves, however.) It is possible to recover control after an error by establishing your own subtransaction surrounding SPI calls that might fail.
SPI 函数在成功时返回非负结果(通过返回的整数值或在全局变量 SPI_result 中,如以下所述)。如果发生错误,将返回负结果或 NULL 。
SPI functions return a nonnegative result on success (either via a returned integer value or in the global variable SPI_result, as described below). On error, a negative result or NULL will be returned.
使用 SPI 的源代码文件必须包括头文件 executor/spi.h 。
Source code files that use SPI must include the header file executor/spi.h.