Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fab9d1d

Browse files
committed
document when PREPARE uses generic plans
Also explain how generic plans are created. Link to PREPARE docs from wire-protocol prepare docs. Reported-by: Jonathan Rogers Discussion: https://www.postgresql.org/message-id/flat/561E749D.4090301%40socialserve.com
1 parent 13e7453 commit fab9d1d

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

doc/src/sgml/libpq.sgml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2303,8 +2303,8 @@ PGresult *PQprepare(PGconn *conn,
23032303
<para>
23042304
<function>PQprepare</> creates a prepared statement for later
23052305
execution with <function>PQexecPrepared</>. This feature allows
2306-
commands that will be used repeatedly to be parsed and planned just
2307-
once, rather than each time they are executed.
2306+
commands to be executed repeatedly without being parsed and
2307+
planned each time; see <xref linkend="SQL-PREPARE"> for details.
23082308
<function>PQprepare</> is supported only in protocol 3.0 and later
23092309
connections; it will fail when using protocol 2.0.
23102310
</para>

doc/src/sgml/ref/prepare.sgml

+33-15
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ PREPARE <replaceable class="PARAMETER">name</replaceable> [ ( <replaceable class
7070
</para>
7171

7272
<para>
73-
Prepared statements have the largest performance advantage when a
74-
single session is being used to execute a large number of similar
73+
Prepared statements potentially have the largest performance advantage
74+
when a single session is being used to execute a large number of similar
7575
statements. The performance difference will be particularly
76-
significant if the statements are complex to plan or rewrite, for
77-
example, if the query involves a join of many tables or requires
76+
significant if the statements are complex to plan or rewrite, e.g.
77+
if the query involves a join of many tables or requires
7878
the application of several rules. If the statement is relatively simple
7979
to plan and rewrite but relatively expensive to execute, the
8080
performance advantage of prepared statements will be less noticeable.
@@ -123,26 +123,44 @@ PREPARE <replaceable class="PARAMETER">name</replaceable> [ ( <replaceable class
123123
</variablelist>
124124
</refsect1>
125125

126-
<refsect1>
126+
<refsect1 id="SQL-PREPARE-notes">
127127
<title>Notes</title>
128128

129129
<para>
130-
If a prepared statement is executed enough times, the server may eventually
131-
decide to save and re-use a generic plan rather than re-planning each time.
132-
This will occur immediately if the prepared statement has no parameters;
133-
otherwise it occurs only if the generic plan appears to be not much more
134-
expensive than a plan that depends on specific parameter values.
135-
Typically, a generic plan will be selected only if the query's performance
136-
is estimated to be fairly insensitive to the specific parameter values
137-
supplied.
130+
Prepared statements can use generic plans rather than re-planning with
131+
each set of supplied <command>EXECUTE</command> values. This occurs
132+
immediately for prepared statements with no parameters; otherwise
133+
it occurs only after five or more executions produce plans whose
134+
estimated cost average (including planning overhead) is more expensive
135+
than the generic plan cost estimate. Once a generic plan is chosen,
136+
it is used for the remaining lifetime of the prepared statement.
137+
Using <command>EXECUTE</command> values which are rare in columns with
138+
many duplicates can generate custom plans that are so much cheaper
139+
than the generic plan, even after adding planning overhead, that the
140+
generic plan might never be used.
141+
</para>
142+
143+
<para>
144+
A generic plan assumes that each value supplied to
145+
<command>EXECUTE</command> is one of the column's distinct values
146+
and that column values are uniformly distributed. For example,
147+
if statistics record three distinct column values, a generic plan
148+
assumes a column equality comparison will match 33% of processed rows.
149+
Column statistics also allow generic plans to accurately compute the
150+
selectivity of unique columns. Comparisons on non-uniformly-distributed
151+
columns and specification of non-existent values affects the average
152+
plan cost, and hence if and when a generic plan is chosen.
138153
</para>
139154

140155
<para>
141156
To examine the query plan <productname>PostgreSQL</productname> is using
142-
for a prepared statement, use <xref linkend="sql-explain">.
157+
for a prepared statement, use <xref linkend="sql-explain">, e.g.
158+
<command>EXPLAIN EXECUTE</>.
143159
If a generic plan is in use, it will contain parameter symbols
144160
<literal>$<replaceable>n</></literal>, while a custom plan will have the
145-
current actual parameter values substituted into it.
161+
supplied parameter values substituted into it.
162+
The row estimates in the generic plan reflect the selectivity
163+
computed for the parameters.
146164
</para>
147165

148166
<para>

0 commit comments

Comments
 (0)