SPI_prepare
SPI_prepare
SPI_exec
or similar functionThe Server Programming Interface (SPI) gives users the ability to run SQL queries inside user-defined C functions.
Note: The available Procedural Languages (PL) give an alternate means to build functions that can execute queries.
In fact, SPI is just a set of native interface functions to simplify access to the Parser, Planner, Optimizer and Executor. SPI also does some memory management.
To avoid misunderstanding we'll use function to mean SPI interface functions and procedure for user-defined C-functions using SPI.
Procedures which use SPI are called by the Executor. The SPI calls recursively invoke the Executor in turn to run queries. When the Executor is invoked recursively, it may itself call procedures which may make SPI calls.
Note that if during execution of a query from a procedure the transaction is aborted, 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. This will probably be changed in future versions.
A related restriction is the inability to execute BEGIN, END and ABORT (transaction control statements). This will also be changed in the future.
If successful, SPI functions return a non-negative result (either via a returned integer value or in SPI_result global variable, as described below). On error, a negative or NULL result will be returned.
SPI_prepare
SPI_prepare