选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0
历史版本PostgreSQL 9.1 已于 2016 年 10 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本

第 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 true if a plan prepared by SPI_prepare can be used with SPI_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_execute or 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 true if a plan prepared by SPI_prepare can be used with SPI_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

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。