]> begriffs open source - ai-pg/blob - full-docs/man3/SPI_prepare.3
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man3 / SPI_prepare.3
1 '\" t
2 .\"     Title: SPI_prepare
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 SPI_prepare \- prepare a statement, without executing it yet
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 SPIPlanPtr SPI_prepare(const char * \fIcommand\fR, int \fInargs\fR, Oid * \fIargtypes\fR)
36 .fi
37 .SH "DESCRIPTION"
38 .PP
39 \fBSPI_prepare\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\&.
42 .PP
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\&.
44 \fBSPI_prepare\fR
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\&.
46 .PP
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\&.
51 .PP
52 The statement returned by
53 \fBSPI_prepare\fR
54 can be used only in the current invocation of the C function, since
55 \fBSPI_finish\fR
56 frees memory allocated for such a statement\&. But the statement can be saved for longer using the functions
57 \fBSPI_keepplan\fR
58 or
59 \fBSPI_saveplan\fR\&.
60 .SH "ARGUMENTS"
61 .PP
62 const char * \fIcommand\fR
63 .RS 4
64 command string
65 .RE
66 .PP
67 int \fInargs\fR
68 .RS 4
69 number of input parameters ($1,
70 $2, etc\&.)
71 .RE
72 .PP
73 Oid * \fIargtypes\fR
74 .RS 4
75 pointer to an array containing the
76 OIDs of the data types of the parameters
77 .RE
78 .SH "RETURN VALUE"
79 .PP
80 \fBSPI_prepare\fR
81 returns a non\-null pointer to an
82 SPIPlan, which is an opaque struct representing a prepared statement\&. On error,
83 NULL
84 will be returned, and
85 \fISPI_result\fR
86 will be set to one of the same error codes used by
87 \fBSPI_execute\fR, except that it is set to
88 SPI_ERROR_ARGUMENT
89 if
90 \fIcommand\fR
91 is
92 NULL, or if
93 \fInargs\fR
94 is less than 0, or if
95 \fInargs\fR
96 is greater than 0 and
97 \fIargtypes\fR
98 is
99 NULL\&.
100 .SH "NOTES"
101 .PP
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
109 or
110 CURSOR_OPT_CUSTOM_PLAN
111 flag to
112 \fBSPI_prepare_cursor\fR, to force use of generic or custom plans respectively\&.
113 .PP
114 Although the main point of a prepared statement is to avoid repeated parse analysis and planning of the statement,
115 PostgreSQL
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
117 search_path
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
120 PostgreSQL
121 9\&.3\&.) See
122 \fBPREPARE\fR(7)
123 for more information about the behavior of prepared statements\&.
124 .PP
125 This function should only be called from a connected C function\&.
126 .PP
127 SPIPlanPtr
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
130 PostgreSQL\&.
131 .PP
132 The name
133 SPIPlanPtr
134 is somewhat historical, since the data structure no longer necessarily contains an execution plan\&.