PostgreSQL 7.4beta4 Documentation
Prev | Fast Backward |  | Fast Forward | Next

# Chapter 37. Server Programming Interface

The Server Programming Interface (SPI) gives users the ability to run SQL commands inside user-defined C functions. SPI is a set of interface functions to simplify access to the parser, planner, optimizer, and executor. SPI also does some memory management.

To avoid misunderstanding we'll use the term "function" when we speak of SPI interface functions and "procedure" for user-defined C-functions, which may be using SPI.

Note that if during the execution of a procedure the transaction is aborted because of an error in a command, then control will not be returned to your procedure. Rather, all work will be rolled back and the server will wait for the next command from the client. A related restriction is the inability to execute `BEGIN`, `COMMIT`, and `ROLLBACK` (transaction control statements) inside a procedure. Both of these restrictions will probably be changed in the future.

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.

Source code files that use SPI must include the header file `executor/spi.h`.

> Note: The available procedural languages provide different means to execute SQL commands from procedures. Some of these are modelled after SPI, so this documentation might be of use for those users as well.

# 37.1. Interface Functions

Prev | Home | Next
A Complete Example | Up | SPI_connect