@@ -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