<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Server Programming Interface</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 7.4.1 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Server Programming" HREF="server-programming.html"><LINK REL="PREVIOUS" TITLE="Database Access" HREF="plpython-database.html"><LINK REL="NEXT" TITLE="SPI_connect" HREF="spi-spi-connect.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META NAME="creation" CONTENT="2003-12-22T03:48:47"></HEAD ><BODY CLASS="CHAPTER" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 7.4.1 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="plpython-database.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="plpython.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="reference.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="spi-spi-connect.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="CHAPTER" ><H1 ><A NAME="SPI" ></A >Chapter 41. Server Programming Interface</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT >41.1. <A HREF="spi.html#SPI-INTERFACE" >Interface Functions</A ></DT ><DD ><DL ><DT ><A HREF="spi-spi-connect.html" >SPI_connect</A > -- connect a procedure to the SPI manager</DT ><DT ><A HREF="spi-spi-finish.html" >SPI_finish</A > -- disconnect a procedure from the SPI manager</DT ><DT ><A HREF="spi-spi-exec.html" >SPI_exec</A > -- execute a command</DT ><DT ><A HREF="spi-spi-prepare.html" >SPI_prepare</A > -- prepare a plan for a command, without executing it yet</DT ><DT ><A HREF="spi-spi-execp.html" >SPI_execp</A > -- executes a plan prepared by <CODE CLASS="FUNCTION" >SPI_prepare</CODE ></DT ><DT ><A HREF="spi-spi-cursor-open.html" >SPI_cursor_open</A > -- set up a cursor using a plan created with <CODE CLASS="FUNCTION" >SPI_prepare</CODE ></DT ><DT ><A HREF="spi-spi-cursor-find.html" >SPI_cursor_find</A > -- find an existing cursor by name</DT ><DT ><A HREF="spi-spi-cursor-fetch.html" >SPI_cursor_fetch</A > -- fetch some rows from a cursor</DT ><DT ><A HREF="spi-spi-cursor-move.html" >SPI_cursor_move</A > -- move a cursor</DT ><DT ><A HREF="spi-spi-cursor-close.html" >SPI_cursor_close</A > -- close a cursor</DT ><DT ><A HREF="spi-spi-saveplan.html" >SPI_saveplan</A > -- save a plan</DT ></DL ></DD ><DT >41.2. <A HREF="spi-interface-support.html" >Interface Support Functions</A ></DT ><DD ><DL ><DT ><A HREF="spi-spi-fname.html" >SPI_fname</A > -- determine the column name for the specified column number</DT ><DT ><A HREF="spi-spi-fnumber.html" >SPI_fnumber</A > -- determine the column number for the specified column name</DT ><DT ><A HREF="spi-spi-getvalue.html" >SPI_getvalue</A > -- return the string value of the specified column</DT ><DT ><A HREF="spi-spi-getbinval.html" >SPI_getbinval</A > -- return the binary value of the specified column</DT ><DT ><A HREF="spi-spi-gettype.html" >SPI_gettype</A > -- return the data type name of the specified column</DT ><DT ><A HREF="spi-spi-gettypeid.html" >SPI_gettypeid</A > -- return the data type <ACRONYM CLASS="ACRONYM" >OID</ACRONYM > of the specified column</DT ><DT ><A HREF="spi-spi-getrelname.html" >SPI_getrelname</A > -- return the name of the specified relation</DT ></DL ></DD ><DT >41.3. <A HREF="spi-memory.html" >Memory Management</A ></DT ><DD ><DL ><DT ><A HREF="spi-spi-palloc.html" >SPI_palloc</A > -- allocate memory in the upper executor context</DT ><DT ><A HREF="spi-realloc.html" >SPI_repalloc</A > -- reallocate memory in the upper executor context</DT ><DT ><A HREF="spi-spi-pfree.html" >SPI_pfree</A > -- free memory in the upper executor context</DT ><DT ><A HREF="spi-spi-copytuple.html" >SPI_copytuple</A > -- make a copy of a row in the upper executor context</DT ><DT ><A HREF="spi-spi-copytupledesc.html" >SPI_copytupledesc</A > -- make a copy of a row descriptor in the upper executor context</DT ><DT ><A HREF="spi-spi-copytupleintoslot.html" >SPI_copytupleintoslot</A > -- make a copy of a row and descriptor in the upper executor context</DT ><DT ><A HREF="spi-spi-modifytuple.html" >SPI_modifytuple</A > -- create a row by replacing selected fields of a given row</DT ><DT ><A HREF="spi-spi-freetuple.html" >SPI_freetuple</A > -- frees a row allocated in the upper executor context</DT ><DT ><A HREF="spi-spi-freetupletable.html" >SPI_freetuptable</A > -- free a row set created by <CODE CLASS="FUNCTION" >SPI_exec</CODE > or a similar function</DT ><DT ><A HREF="spi-spi-freeplan.html" >SPI_freeplan</A > -- free a previously saved plan</DT ></DL ></DD ><DT >41.4. <A HREF="spi-visibility.html" >Visibility of Data Changes</A ></DT ><DT >41.5. <A HREF="spi-examples.html" >Examples</A ></DT ></DL ></DIV ><A NAME="AEN33154" ></A ><P > The <I CLASS="FIRSTTERM" >Server Programming Interface</I > (<ACRONYM CLASS="ACRONYM" >SPI</ACRONYM >) gives writers of user-defined <ACRONYM CLASS="ACRONYM" >C</ACRONYM > functions the ability to run <ACRONYM CLASS="ACRONYM" >SQL</ACRONYM > commands inside their functions. <ACRONYM CLASS="ACRONYM" >SPI</ACRONYM > is a set of interface functions to simplify access to the parser, planner, optimizer, and executor. <ACRONYM CLASS="ACRONYM" >SPI</ACRONYM > also does some memory management. </P ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > The available procedural languages provide various means to execute SQL commands from procedures. Some of these are based on or modelled after SPI, so this documentation might be of use for users of those languages as well. </P ></BLOCKQUOTE ></DIV ><P > To avoid misunderstanding we'll use the term <SPAN CLASS="QUOTE" >"function"</SPAN > when we speak of <ACRONYM CLASS="ACRONYM" >SPI</ACRONYM > interface functions and <SPAN CLASS="QUOTE" >"procedure"</SPAN > for a user-defined C-function that is using <ACRONYM CLASS="ACRONYM" >SPI</ACRONYM >. </P ><P > 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 <TT CLASS="COMMAND" >BEGIN</TT >, <TT CLASS="COMMAND" >COMMIT</TT >, and <TT CLASS="COMMAND" >ROLLBACK</TT > (transaction control statements) inside a procedure. Both of these restrictions will probably be changed in the future. </P ><P > <ACRONYM CLASS="ACRONYM" >SPI</ACRONYM > functions return a nonnegative result on success (either via a returned integer value or in the global variable <VAR CLASS="VARNAME" >SPI_result</VAR >, as described below). On error, a negative result or <TT CLASS="SYMBOL" >NULL</TT > will be returned. </P ><P > Source code files that use SPI must include the header file <TT CLASS="FILENAME" >executor/spi.h</TT >. </P ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="SPI-INTERFACE" >41.1. Interface Functions</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="spi-spi-connect.html" >SPI_connect</A > -- connect a procedure to the SPI manager</DT ><DT ><A HREF="spi-spi-finish.html" >SPI_finish</A > -- disconnect a procedure from the SPI manager</DT ><DT ><A HREF="spi-spi-exec.html" >SPI_exec</A > -- execute a command</DT ><DT ><A HREF="spi-spi-prepare.html" >SPI_prepare</A > -- prepare a plan for a command, without executing it yet</DT ><DT ><A HREF="spi-spi-execp.html" >SPI_execp</A > -- executes a plan prepared by <CODE CLASS="FUNCTION" >SPI_prepare</CODE ></DT ><DT ><A HREF="spi-spi-cursor-open.html" >SPI_cursor_open</A > -- set up a cursor using a plan created with <CODE CLASS="FUNCTION" >SPI_prepare</CODE ></DT ><DT ><A HREF="spi-spi-cursor-find.html" >SPI_cursor_find</A > -- find an existing cursor by name</DT ><DT ><A HREF="spi-spi-cursor-fetch.html" >SPI_cursor_fetch</A > -- fetch some rows from a cursor</DT ><DT ><A HREF="spi-spi-cursor-move.html" >SPI_cursor_move</A > -- move a cursor</DT ><DT ><A HREF="spi-spi-cursor-close.html" >SPI_cursor_close</A > -- close a cursor</DT ><DT ><A HREF="spi-spi-saveplan.html" >SPI_saveplan</A > -- save a plan</DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="plpython-database.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="spi-spi-connect.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Database Access</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="server-programming.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >SPI_connect</TD ></TR ></TABLE ></DIV ></BODY ></HTML >