跳至主要內容

KlustronDB大约 4 分钟

第 47 章 服务器编程接口

目录

47.1. 接口函数 : SPI_connect — 连接一个C函数到 SPI 管理器

[SPI_finish](spi-spi-finish.md) — 将一个C函数从 SPI 管理器断开

[SPI_execute](spi-spi-execute.md) — 执行一个命令

[SPI_exec](spi-spi-exec.md) — 执行一个读/写命令

[SPI_execute_with_args](spi-spi-execute-with-args.md) — 用线外参数执行一个命令

[SPI_prepare](spi-spi-prepare.md) — 准备一个语句,但不执行它

[SPI_prepare_cursor](spi-spi-prepare-cursor.md) — 预备一个语句,但是不执行它

[SPI_prepare_params](spi-spi-prepare-params.md) — 预备一个语句,但是不执行它

[SPI_getargcount](spi-spi-getargcount.md) — 返回一个由`SPI_prepare` 准备好的语句所需的参数数量

[SPI_getargtypeid](spi-spi-getargtypeid.md) — 为由`SPI_prepare` 准备好的一个语句的一个参数返回其数据类型 OID

[SPI_is_cursor_plan](spi-spi-is-cursor-plan.md) — 如果一个由`SPI_prepare`预备好 的语句可以用于`SPI_cursor_open`则返回 `true`

[SPI_execute_plan](spi-spi-execute-plan.md) — 执行一个由`SPI_prepare`预备好的语句

[SPI_execute_plan_with_paramlist](spi-spi-execute-plan-with-paramlist.md) — 执行一个由`SPI_prepare`预备好的语句

[SPI_execp](spi-spi-execp.md) — 以读/写模式执行一个语句

[SPI_cursor_open](spi-spi-cursor-open.md) — 使用由`SPI_prepare`创建的 语句建立一个游标

[SPI_cursor_open_with_args](spi-spi-cursor-open-with-args.md) — 使用一个查询和参数建立一个游标

[SPI_cursor_open_with_paramlist](spi-spi-cursor-open-with-paramlist.md) — 使用参数建立一个游标

[SPI_cursor_find](spi-spi-cursor-find.md) — 用名称查找一个现有的游标

[SPI_cursor_fetch](spi-spi-cursor-fetch.md) — 从一个游标取出一些行

[SPI_cursor_move](spi-spi-cursor-move.md) — 移动一个游标

[SPI_scroll_cursor_fetch](spi-spi-scroll-cursor-fetch.md) — 从一个游标取出一些行

[SPI_scroll_cursor_move](spi-spi-scroll-cursor-move.md) — 移动一个游标

[SPI_cursor_close](spi-spi-cursor-close.md) — 关闭一个游标

[SPI_keepplan](spi-spi-keepplan.md) — 保存一个预备语句

[SPI_saveplan](spi-spi-saveplan.md) — 保存一个预备语句

[SPI_register_relation](spi-spi-register-relation.md) — make an ephemeral named relation available by name in SPI queries

[SPI_unregister_relation](spi-spi-unregister-relation.md) — remove an ephemeral named relation from the registry

[SPI_register_trigger_data](spi-spi-register-trigger-data.md) — make ephemeral trigger data available in SPI queries

47.2. 接口支持函数 : SPI_fname — 为指定的列号确定列名

[SPI_fnumber](spi-spi-fnumber.md) — 为一个指定的列名确定列号

[SPI_getvalue](spi-spi-getvalue.md) — 返回指定列的字符串值

[SPI_getbinval](spi-spi-getbinval.md) — 返回指定列的二进制值

[SPI_gettype](spi-spi-gettype.md) — 返回指定列的数据类型名称

[SPI_gettypeid](spi-spi-gettypeid.md) — 返回指定列的数据类型的OID

[SPI_getrelname](spi-spi-getrelname.md) — 返回指定关系的名称

[SPI_getnspname](spi-spi-getnspname.md) — 返回指定关系的名字空间

[SPI_result_code_string](spi-spi-result-code-string.md) — return error code as string

47.3. 内存管理 : SPI_palloc — 在上层执行器上下文中分配内存

[SPI_repalloc](spi-realloc.md) — 在上层执行器上下文中重分配内存

[SPI_pfree](spi-spi-pfree.md) — 在上层执行器上下文中释放内存

[SPI_copytuple](spi-spi-copytuple.md) — 在上层执行器上下文中创建一行的拷贝

[SPI_returntuple](spi-spi-returntuple.md) — 准备把一个元组返回为一个 Datum

[SPI_modifytuple](spi-spi-modifytuple.md) — 通过替换一个给定行的选定域来创建一行

[SPI_freetuple](spi-spi-freetuple.md) — 释放一个在上层执行器上下文中分配的行

[SPI_freetuptable](spi-spi-freetupletable.md) — 释放一个由`SPI_execute` 或者类似函数创建的行集合

[SPI_freeplan](spi-spi-freeplan.md) — 释放一个之前保存的预备语句

47.4. 事务管理 : SPI_commit — commit the current transaction

[SPI_rollback](spi-spi-rollback.md) — abort the current transaction

[SPI_start_transaction](spi-spi-start-transaction.md) — start a new transaction

47.5. 数据改变的可见性

47.6. 例子

服务器编程接口(SPI)给予用户定义C函数编写者在其函数内运行SQL命令的能力。SPI是一组接口函数,它们可以简化对解析器、规划器和执行器的访问。SPI也做一些内存管理。

注意

可用的过程语言提供了多种方法从函数中执行 SQL 命令。大部分这些设施都是基于 SPI 的,因此这个文档也对那些语言的用户有用。

注意如果一个通过 SPI 调用的命令失败,那么控制将会返回到你的C函数中。当然啦,你的C函数所在的事务或者子事务将被回滚(这可能看起来令人惊讶,因为据文档所说 SPI 函数大多数都有错误返回约定。但是那些约定只适用于在 SPI 函数本身内部检测到的错误)。通过在可能失败的 SPI 调用周围建立自己的子事务可以在错误之后恢复控制。

SPI成功时返回一个非负结果(要么通过一个返回的整数值,要么如下所述放在全局变量SPI_result中)。错误时,将会返回一个负结果或者NULL

使用 SPI 的源代码文件必须包括头文件executor/spi.h