]> begriffs open source - ai-pg/blob - full-docs/man7/PREPARE.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / PREPARE.7
1 '\" t
2 .\"     Title: 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 "PREPARE" "7" "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 PREPARE \- prepare a statement for execution
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 PREPARE \fIname\fR [ ( \fIdata_type\fR [, \&.\&.\&.] ) ] AS \fIstatement\fR
36 .fi
37 .SH "DESCRIPTION"
38 .PP
39 \fBPREPARE\fR
40 creates a prepared statement\&. A prepared statement is a server\-side object that can be used to optimize performance\&. When the
41 \fBPREPARE\fR
42 statement is executed, the specified statement is parsed, analyzed, and rewritten\&. When an
43 \fBEXECUTE\fR
44 command is subsequently issued, the prepared statement is planned and executed\&. This division of labor avoids repetitive parse analysis work, while allowing the execution plan to depend on the specific parameter values supplied\&.
45 .PP
46 Prepared statements can take parameters: values that are substituted into the statement when it is executed\&. When creating the prepared statement, refer to parameters by position, using
47 $1,
48 $2, etc\&. A corresponding list of parameter data types can optionally be specified\&. When a parameter\*(Aqs data type is not specified or is declared as
49 unknown, the type is inferred from the context in which the parameter is first referenced (if possible)\&. When executing the statement, specify the actual values for these parameters in the
50 \fBEXECUTE\fR
51 statement\&. Refer to
52 \fBEXECUTE\fR(7)
53 for more information about that\&.
54 .PP
55 Prepared statements only last for the duration of the current database session\&. When the session ends, the prepared statement is forgotten, so it must be recreated before being used again\&. This also means that a single prepared statement cannot be used by multiple simultaneous database clients; however, each client can create their own prepared statement to use\&. Prepared statements can be manually cleaned up using the
56 \fBDEALLOCATE\fR
57 command\&.
58 .PP
59 Prepared statements potentially have the largest performance advantage when a single session is being used to execute a large number of similar statements\&. The performance difference will be particularly significant if the statements are complex to plan or rewrite, e\&.g\&., if the query involves a join of many tables or requires the application of several rules\&. If the statement is relatively simple to plan and rewrite but relatively expensive to execute, the performance advantage of prepared statements will be less noticeable\&.
60 .SH "PARAMETERS"
61 .PP
62 \fIname\fR
63 .RS 4
64 An arbitrary name given to this particular prepared statement\&. It must be unique within a single session and is subsequently used to execute or deallocate a previously prepared statement\&.
65 .RE
66 .PP
67 \fIdata_type\fR
68 .RS 4
69 The data type of a parameter to the prepared statement\&. If the data type of a particular parameter is unspecified or is specified as
70 unknown, it will be inferred from the context in which the parameter is first referenced\&. To refer to the parameters in the prepared statement itself, use
71 $1,
72 $2, etc\&.
73 .RE
74 .PP
75 \fIstatement\fR
76 .RS 4
77 Any
78 \fBSELECT\fR,
79 \fBINSERT\fR,
80 \fBUPDATE\fR,
81 \fBDELETE\fR,
82 \fBMERGE\fR, or
83 \fBVALUES\fR
84 statement\&.
85 .RE
86 .SH "NOTES"
87 .PP
88 A prepared statement can be executed with either a
89 generic plan
90 or a
91 custom plan\&. A generic plan is the same across all executions, while a custom plan is generated for a specific execution using the parameter values given in that call\&. Use of a generic plan avoids planning overhead, but in some situations a custom plan will be much more efficient to execute because the planner can make use of knowledge of the parameter values\&. (Of course, if the prepared statement has no parameters, then this is moot and a generic plan is always used\&.)
92 .PP
93 By default (that is, when
94 plan_cache_mode
95 is set to
96 auto), the server will automatically choose whether to use a generic or custom plan for a prepared statement that has parameters\&. The current rule for this is that the first five executions are done with custom plans and the average estimated cost of those plans is calculated\&. Then a generic plan is created and its estimated cost is compared to the average custom\-plan cost\&. Subsequent executions use the generic plan if its cost is not so much higher than the average custom\-plan cost as to make repeated replanning seem preferable\&.
97 .PP
98 This heuristic can be overridden, forcing the server to use either generic or custom plans, by setting
99 \fIplan_cache_mode\fR
100 to
101 force_generic_plan
102 or
103 force_custom_plan
104 respectively\&. This setting is primarily useful if the generic plan\*(Aqs cost estimate is badly off for some reason, allowing it to be chosen even though its actual cost is much more than that of a custom plan\&.
105 .PP
106 To examine the query plan
107 PostgreSQL
108 is using for a prepared statement, use
109 \fBEXPLAIN\fR, for example
110 .sp
111 .if n \{\
112 .RS 4
113 .\}
114 .nf
115 EXPLAIN EXECUTE \fIname\fR(\fIparameter_values\fR);
116 .fi
117 .if n \{\
118 .RE
119 .\}
120 .sp
121 If a generic plan is in use, it will contain parameter symbols
122 $\fIn\fR, while a custom plan will have the supplied parameter values substituted into it\&.
123 .PP
124 For more information on query planning and the statistics collected by
125 PostgreSQL
126 for that purpose, see the
127 \fBANALYZE\fR(7)
128 documentation\&.
129 .PP
130 Although the main point of a prepared statement is to avoid repeated parse analysis and planning of the statement,
131 PostgreSQL
132 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 or their planner statistics have been updated since the previous use of the prepared statement\&. Also, if the value of
133 search_path
134 changes from one use to the next, the statement will be re\-parsed using the new
135 \fIsearch_path\fR\&. (This latter behavior is new as of
136 PostgreSQL
137 9\&.3\&.) These rules make use of a prepared statement semantically almost equivalent to re\-submitting the same query text over and over, but with a performance benefit if no object definitions are changed, especially if the best plan remains the same across uses\&. An example of a case where the semantic equivalence is not perfect is that if the statement refers to a table by an unqualified name, and then a new table of the same name is created in a schema appearing earlier in the
138 \fIsearch_path\fR, no automatic re\-parse will occur since no object used in the statement changed\&. However, if some other change forces a re\-parse, the new table will be referenced in subsequent uses\&.
139 .PP
140 You can see all prepared statements available in the session by querying the
141 pg_prepared_statements
142 system view\&.
143 .SH "EXAMPLES"
144 .PP
145 Create a prepared statement for an
146 \fBINSERT\fR
147 statement, and then execute it:
148 .sp
149 .if n \{\
150 .RS 4
151 .\}
152 .nf
153 PREPARE fooplan (int, text, bool, numeric) AS
154     INSERT INTO foo VALUES($1, $2, $3, $4);
155 EXECUTE fooplan(1, \*(AqHunter Valley\*(Aq, \*(Aqt\*(Aq, 200\&.00);
156 .fi
157 .if n \{\
158 .RE
159 .\}
160 .PP
161 Create a prepared statement for a
162 \fBSELECT\fR
163 statement, and then execute it:
164 .sp
165 .if n \{\
166 .RS 4
167 .\}
168 .nf
169 PREPARE usrrptplan (int) AS
170     SELECT * FROM users u, logs l WHERE u\&.usrid=$1 AND u\&.usrid=l\&.usrid
171     AND l\&.date = $2;
172 EXECUTE usrrptplan(1, current_date);
173 .fi
174 .if n \{\
175 .RE
176 .\}
177 .sp
178 In this example, the data type of the second parameter is not specified, so it is inferred from the context in which
179 $2
180 is used\&.
181 .SH "COMPATIBILITY"
182 .PP
183 The SQL standard includes a
184 \fBPREPARE\fR
185 statement, but it is only for use in embedded SQL\&. This version of the
186 \fBPREPARE\fR
187 statement also uses a somewhat different syntax\&.
188 .SH "SEE ALSO"
189 \fBDEALLOCATE\fR(7), \fBEXECUTE\fR(7)