©
This document usesPHP Chinese website manualRelease
SPIPlanPtr SPI_prepare(const char *command, intnargs, Oid *argtypes)
SPI_prepare
creates and returns an execution plan for the specified command, but doesn't execute the command. This function should only be called from a connected procedure.
When the same or a similar command is to be executed repeatedly, it might be advantageous to perform the planning only once.SPI_prepare
converts a command string into an execution plan that can be executed repeatedly usingSPI_execute_plan
.
A prepared command can be generalized by writing parameters ($1,$2, etc.) in place of what would be constants in a normal command. The actual values of the parameters are then specified whenSPI_execute_plan
is called. This allows the prepared command to be used over a wider range of situations than would be possible without parameters.
The plan returned bySPI_prepare
can be used only in the current invocation of the procedure, sinceSPI_finish
frees memory allocated for a plan. But a plan can be saved for longer using the functionSPI_saveplan
.
command string
number of input parameters ($1,$2, etc.)
pointer to an array containing theOIDs of the data types of the parameters
SPI_prepare
returns a non-null pointer to an execution plan. On error,NULLwill be returned, andSPI_resultwill be set to one of the same error codes used bySPI_execute
, except that it is set toSPI_ERROR_ARGUMENTifcommandisNULL, or ifnargsis less than 0, or ifnargsis greater than 0 andargtypesisNULL.
SPIPlanPtris declared as a pointer to an opaque struct type inspi.h. It is unwise to try to access its contents directly, as that makes your code much more likely to break in future revisions ofPostgreSQL.
There is a disadvantage to using parameters: since the planner does not know the values that will be supplied for the parameters, it might make worse planning choices than it would make for a normal command with all constants visible.