3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "SPI_PREPARE" "3" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 SPI_prepare \- prepare a statement, without executing it yet
35 SPIPlanPtr SPI_prepare(const char * \fIcommand\fR, int \fInargs\fR, Oid * \fIargtypes\fR)
40 creates and returns a prepared statement for the specified command, but doesn\*(Aqt execute the command\&. The prepared statement can later be executed repeatedly using
41 \fBSPI_execute_plan\fR\&.
43 When the same or a similar command is to be executed repeatedly, it is generally advantageous to perform parse analysis only once, and might furthermore be advantageous to re\-use an execution plan for the command\&.
45 converts a command string into a prepared statement that encapsulates the results of parse analysis\&. The prepared statement also provides a place for caching an execution plan if it is found that generating a custom plan for each execution is not helpful\&.
47 A prepared command can be generalized by writing parameters ($1,
48 $2, etc\&.) in place of what would be constants in a normal command\&. The actual values of the parameters are then specified when
49 \fBSPI_execute_plan\fR
50 is called\&. This allows the prepared command to be used over a wider range of situations than would be possible without parameters\&.
52 The statement returned by
54 can be used only in the current invocation of the C function, since
56 frees memory allocated for such a statement\&. But the statement can be saved for longer using the functions
62 const char * \fIcommand\fR
69 number of input parameters ($1,
75 pointer to an array containing the
76 OIDs of the data types of the parameters
81 returns a non\-null pointer to an
82 SPIPlan, which is an opaque struct representing a prepared statement\&. On error,
86 will be set to one of the same error codes used by
87 \fBSPI_execute\fR, except that it is set to
102 If no parameters are defined, a generic plan will be created at the first use of
103 \fBSPI_execute_plan\fR, and used for all subsequent executions as well\&. If there are parameters, the first few uses of
104 \fBSPI_execute_plan\fR
105 will generate custom plans that are specific to the supplied parameter values\&. After enough uses of the same prepared statement,
106 \fBSPI_execute_plan\fR
107 will build a generic plan, and if that is not too much more expensive than the custom plans, it will start using the generic plan instead of re\-planning each time\&. If this default behavior is unsuitable, you can alter it by passing the
108 CURSOR_OPT_GENERIC_PLAN
110 CURSOR_OPT_CUSTOM_PLAN
112 \fBSPI_prepare_cursor\fR, to force use of generic or custom plans respectively\&.
114 Although the main point of a prepared statement is to avoid repeated parse analysis and planning of the statement,
116 will force re\-analysis and re\-planning of the statement before using it whenever database objects used in the statement have undergone definitional (DDL) changes since the previous use of the prepared statement\&. Also, if the value of
118 changes from one use to the next, the statement will be re\-parsed using the new
119 \fIsearch_path\fR\&. (This latter behavior is new as of
123 for more information about the behavior of prepared statements\&.
125 This function should only be called from a connected C function\&.
128 is declared as a pointer to an opaque struct type in
129 spi\&.h\&. It is unwise to try to access its contents directly, as that makes your code much more likely to break in future revisions of
134 is somewhat historical, since the data structure no longer necessarily contains an execution plan\&.