历史版本。 PostgreSQL 9.1 已结束支持。 2016-10-27. 请参阅 当前版本手册.
第 43 章 服务器编程接口
目录
- 43.1. 接口函数
- SPI_connect — connect a procedure to the SPI manager
- SPI_finish — disconnect a procedure from the SPI manager
- SPI_push — push SPI stack to allow recursive SPI usage
- SPI_pop — pop SPI stack to return from recursive SPI usage
- SPI_execute — execute a command
- SPI_exec — execute a read/write command
- SPI_execute_with_args — execute a command with out-of-line parameters
- SPI_prepare — prepare a plan for a command, without executing it yet
- SPI_prepare_cursor — prepare a plan for a command, without executing it yet
- SPI_prepare_params — prepare a plan for a command, without executing it yet
- SPI_getargcount — return the number of arguments needed by a plan
prepared by
SPI_prepare - SPI_getargtypeid — return the data type OID for an argument of
a plan prepared by
SPI_prepare - SPI_is_cursor_plan — return
trueif a plan prepared bySPI_preparecan be used withSPI_cursor_open - SPI_execute_plan — execute a plan prepared by
SPI_prepare - SPI_execute_plan_with_paramlist — execute a plan prepared by
SPI_prepare - SPI_execp — execute a plan in read/write mode
- SPI_cursor_open — set up a cursor using a plan created with
SPI_prepare - SPI_cursor_open_with_args — set up a cursor using a query and parameters
- SPI_cursor_open_with_paramlist — set up a cursor using parameters
- SPI_cursor_find — find an existing cursor by name
- SPI_cursor_fetch — fetch some rows from a cursor
- SPI_cursor_move — move a cursor
- SPI_scroll_cursor_fetch — fetch some rows from a cursor
- SPI_scroll_cursor_move — move a cursor
- SPI_cursor_close — close a cursor
- SPI_saveplan — save a plan
- 43.2. 接口支持函数
- SPI_fname — determine the column name for the specified column number
- SPI_fnumber — determine the column number for the specified column name
- SPI_getvalue — return the string value of the specified column
- SPI_getbinval — return the binary value of the specified column
- SPI_gettype — return the data type name of the specified column
- SPI_gettypeid — return the data type OID of the specified column
- SPI_getrelname — return the name of the specified relation
- SPI_getnspname — return the namespace of the specified relation
- 43.3. 内存管理
- SPI_palloc — allocate memory in the upper executor context
- SPI_repalloc — reallocate memory in the upper executor context
- SPI_pfree — free memory in the upper executor context
- SPI_copytuple — make a copy of a row in the upper executor context
- SPI_returntuple — prepare to return a tuple as a Datum
- SPI_modifytuple — create a row by replacing selected fields of a given row
- SPI_freetuple — free a row allocated in the upper executor context
- SPI_freetuptable — free a row set created by
SPI_executeor a similar function - SPI_freeplan — free a previously saved plan
- 43.4. 数据更改的可见性
- 43.5. 示例
服务器编程接口(SPI)使用户定义 C 函数的编写者能够在其函数中运行 SQL 命令。SPI 是一组接口函数,用于简化对解析器、规划器和执行器的访问。SPI 还负责一部分内存管理工作。
注意
可用的过程语言提供了多种从过程中执行 SQL 命令的方法。这些设施大多基于 SPI,因此本文档对这些语言的用户也会有帮助。
为避免误解,本文用“函数”指SPI接口函数,而用“过程”指使用SPI的用户定义 C 函数。
注意,如果通过 SPI 调用的某条命令失败,控制不会返回到你的过程。相反,执行该过程的事务或子事务会被回滚。(考虑到 SPI 函数大多记录了错误返回约定,这一点可能看起来有些意外。不过,这些约定只适用于在 SPI 函数自身内部检测到的错误。)如果在可能失败的 SPI 调用外围建立自己的子事务,则可以在出错后重新取得控制权。
SPI 函数在成功时返回非负结果(要么直接作为整数返回值,要么如后文所述存放在全局变量 SPI_result 中)。发生错误时,则返回负值或 NULL。
使用 SPI 的源代码文件必须包含头文件
executor/spi.h。
43.1. 接口函数 #
- SPI_connect — connect a procedure to the SPI manager
- SPI_finish — disconnect a procedure from the SPI manager
- SPI_push — push SPI stack to allow recursive SPI usage
- SPI_pop — pop SPI stack to return from recursive SPI usage
- SPI_execute — execute a command
- SPI_exec — execute a read/write command
- SPI_execute_with_args — execute a command with out-of-line parameters
- SPI_prepare — prepare a plan for a command, without executing it yet
- SPI_prepare_cursor — prepare a plan for a command, without executing it yet
- SPI_prepare_params — prepare a plan for a command, without executing it yet
- SPI_getargcount — return the number of arguments needed by a plan
prepared by
SPI_prepare - SPI_getargtypeid — return the data type OID for an argument of
a plan prepared by
SPI_prepare - SPI_is_cursor_plan — return
trueif a plan prepared bySPI_preparecan be used withSPI_cursor_open - SPI_execute_plan — execute a plan prepared by
SPI_prepare - SPI_execute_plan_with_paramlist — execute a plan prepared by
SPI_prepare - SPI_execp — execute a plan in read/write mode
- SPI_cursor_open — set up a cursor using a plan created with
SPI_prepare - SPI_cursor_open_with_args — set up a cursor using a query and parameters
- SPI_cursor_open_with_paramlist — set up a cursor using parameters
- SPI_cursor_find — find an existing cursor by name
- SPI_cursor_fetch — fetch some rows from a cursor
- SPI_cursor_move — move a cursor
- SPI_scroll_cursor_fetch — fetch some rows from a cursor
- SPI_scroll_cursor_move — move a cursor
- SPI_cursor_close — close a cursor
- SPI_saveplan — save a plan