This page in other versions: 8.4 / 9.0 / 9.1 / 9.2 / 9.3  |  Development versions: devel  |  Unsupported versions: 7.1 / 7.2 / 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3

Chapter 44. Server Programming Interface

Table of Contents
44.1. Interface Functions
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 statement, without executing it yet
SPI_prepare_cursor -- prepare a statement, without executing it yet
SPI_prepare_params -- prepare a statement, without executing it yet
SPI_getargcount -- return the number of arguments needed by a statement prepared by SPI_prepare
SPI_getargtypeid -- return the data type OID for an argument of a statement prepared by SPI_prepare
SPI_is_cursor_plan -- return true if a statement prepared by SPI_prepare can be used with SPI_cursor_open
SPI_execute_plan -- execute a statement prepared by SPI_prepare
SPI_execute_plan_with_paramlist -- execute a statement prepared by SPI_prepare
SPI_execp -- execute a statement in read/write mode
SPI_cursor_open -- set up a cursor using a statement 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_keepplan -- save a prepared statement
SPI_saveplan -- save a prepared statement
44.2. Interface Support Functions
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
44.3. Memory Management
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 prepared statement
44.4. Visibility of Data Changes
44.5. Examples

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

Note: The available procedural languages provide various means to execute SQL commands from procedures. Most of these facilities are based on SPI, so this documentation might be of use for users of those languages as well.

To avoid misunderstanding we'll use the term "function" when we speak of SPI interface functions and "procedure" for a user-defined C-function that is using SPI.

Note that if a command invoked via SPI fails, then control will not be returned to your procedure. Rather, the transaction or subtransaction in which your procedure 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. This is not currently documented because the mechanisms required are still in flux.

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.

Add Comment

Please use this form to add your own comments regarding your experience with particular features of PostgreSQL, clarifications of the documentation, or hints for other users. Please note, this is not a support forum, and your IP address will be logged. If you have a question or need help, please see the faq, try a mailing list, or join us on IRC. Note that submissions containing URLs or other keywords commonly found in 'spam' comments may be silently discarded. Please contact the webmaster if you think this is happening to you in error.

Proceed to the comment form.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group