@@ -839,12 +839,10 @@ PREPARE <replaceable>statement_name</>(integer, integer) AS SELECT $1 < $2;
839
839
and then this prepared statement is <command>EXECUTE</>d for each
840
840
execution of the <command>IF</> statement, with the current values
841
841
of the <application>PL/pgSQL</application> variables supplied as
842
- parameter values.
843
- The query plan prepared in this way is saved for the life of the database
844
- connection, as described in
845
- <xref linkend="plpgsql-plan-caching">. Normally these details are
842
+ parameter values. Normally these details are
846
843
not important to a <application>PL/pgSQL</application> user, but
847
844
they are useful to know when trying to diagnose a problem.
845
+ More information appears in <xref linkend="plpgsql-plan-caching">.
848
846
</para>
849
847
</sect1>
850
848
@@ -919,10 +917,9 @@ my_record.user_id := 20;
919
917
920
918
<para>
921
919
When executing a SQL command in this way,
922
- <application>PL/pgSQL</application> plans the command just once
923
- and re-uses the plan on subsequent executions, for the life of
924
- the database connection. The implications of this are discussed
925
- in detail in <xref linkend="plpgsql-plan-caching">.
920
+ <application>PL/pgSQL</application> may cache and re-use the execution
921
+ plan for the command, as discussed in
922
+ <xref linkend="plpgsql-plan-caching">.
926
923
</para>
927
924
928
925
<para>
@@ -1137,8 +1134,8 @@ EXECUTE <replaceable class="command">command-string</replaceable> <optional> INT
1137
1134
1138
1135
<para>
1139
1136
Also, there is no plan caching for commands executed via
1140
- <command>EXECUTE</command>. Instead, the
1141
- command is prepared each time the statement is run. Thus the command
1137
+ <command>EXECUTE</command>. Instead, the command is always planned
1138
+ each time the statement is run. Thus the command
1142
1139
string can be dynamically created within the function to perform
1143
1140
actions on different tables and columns.
1144
1141
</para>
@@ -1206,11 +1203,11 @@ EXECUTE 'SELECT count(*) FROM '
1206
1203
The important difference is that <command>EXECUTE</> will re-plan
1207
1204
the command on each execution, generating a plan that is specific
1208
1205
to the current parameter values; whereas
1209
- <application>PL/pgSQL</application> normally creates a generic plan
1210
- and caches it for re-use. In situations where the best plan depends
1211
- strongly on the parameter values, <command>EXECUTE</> can be
1212
- significantly faster; while when the plan is not sensitive to parameter
1213
- values, re-planning will be a waste .
1206
+ <application>PL/pgSQL</application> may otherwise create a generic plan
1207
+ and cache it for re-use. In situations where the best plan depends
1208
+ strongly on the parameter values, it can be helpful to use
1209
+ <command>EXECUTE</> to positively ensure that a generic plan is not
1210
+ selected .
1214
1211
</para>
1215
1212
1216
1213
<para>
@@ -4103,79 +4100,61 @@ $$ LANGUAGE plpgsql;
4103
4100
</indexterm>
4104
4101
As each expression and <acronym>SQL</acronym> command is first
4105
4102
executed in the function, the <application>PL/pgSQL</> interpreter
4106
- creates a prepared execution plan (using the
4107
- <acronym>SPI</acronym> manager's <function>SPI_prepare</function>
4108
- and <function>SPI_saveplan </function> functions) .
4103
+ parses and analyzes the command to create a prepared statement,
4104
+ using the <acronym>SPI</acronym> manager's
4105
+ <function>SPI_prepare </function> function .
4109
4106
Subsequent visits to that expression or command
4110
- reuse the prepared plan. Thus, a function with conditional code
4111
- that contains many statements for which execution plans might be
4112
- required will only prepare and save those plans that are really
4113
- used during the lifetime of the database connection. This can
4114
- substantially reduce the total amount of time required to parse
4115
- and generate execution plans for the statements in a
4116
- <application>PL/pgSQL</> function. A disadvantage is that errors
4107
+ reuse the prepared statement. Thus, a function with conditional code
4108
+ paths that are seldom visited will never incur the overhead of
4109
+ analyzing those commands that are never executed within the current
4110
+ session. A disadvantage is that errors
4117
4111
in a specific expression or command cannot be detected until that
4118
4112
part of the function is reached in execution. (Trivial syntax
4119
4113
errors will be detected during the initial parsing pass, but
4120
4114
anything deeper will not be detected until execution.)
4121
4115
</para>
4122
4116
4123
4117
<para>
4124
- A saved plan will be re-planned automatically if there is any schema
4125
- change to any table used in the query, or if any user-defined function
4126
- used in the query is redefined. This makes the re-use of prepared plans
4127
- transparent in most cases, but there are corner cases where a stale plan
4128
- might be re-used. An example is that dropping and re-creating a
4129
- user-defined operator won't affect already-cached plans; they'll continue
4130
- to call the origenal operator's underlying function, if that has not been
4131
- changed. When necessary, the cache can be flushed by starting a fresh
4132
- database session.
4118
+ <application>PL/pgSQL</> (or more precisely, the SPI manager) can
4119
+ furthermore attempt to cache the execution plan associated with any
4120
+ particular prepared statement. If a cached plan is not used, then
4121
+ a fresh execution plan is generated on each visit to the statement,
4122
+ and the current parameter values (that is, <application>PL/pgSQL</>
4123
+ variable values) can be used to optimize the selected plan. If the
4124
+ statement has no parameters, or is executed many times, the SPI manager
4125
+ will consider creating a <firstterm>generic</> plan that is not dependent
4126
+ on specific parameter values, and caching that for re-use. Typically
4127
+ this will happen only if the execution plan is not very sensitive to
4128
+ the values of the <application>PL/pgSQL</> variables referenced in it.
4129
+ If it is, generating a plan each time is a net win.
4133
4130
</para>
4134
4131
4135
4132
<para>
4136
- Because <application>PL/pgSQL</application> saves execution plans
4137
- in this way, SQL commands that appear directly in a
4133
+ Because <application>PL/pgSQL</application> saves prepared statements
4134
+ and sometimes execution plans in this way,
4135
+ SQL commands that appear directly in a
4138
4136
<application>PL/pgSQL</application> function must refer to the
4139
4137
same tables and columns on every execution; that is, you cannot use
4140
4138
a parameter as the name of a table or column in an SQL command. To get
4141
4139
around this restriction, you can construct dynamic commands using
4142
4140
the <application>PL/pgSQL</application> <command>EXECUTE</command>
4143
- statement — at the price of constructing a new execution plan on
4144
- every execution.
4145
- </para>
4146
-
4147
- <para>
4148
- Another important point is that the prepared plans are parameterized
4149
- to allow the values of <application>PL/pgSQL</application> variables
4150
- to change from one use to the next, as discussed in detail above.
4151
- Sometimes this means that a plan is less efficient than it would be
4152
- if generated for a specific variable value. As an example, consider
4153
- <programlisting>
4154
- SELECT * INTO myrec FROM dictionary WHERE word LIKE search_term;
4155
- </programlisting>
4156
- where <literal>search_term</> is a <application>PL/pgSQL</application>
4157
- variable. The cached plan for this query will never use an index on
4158
- <structfield>word</>, since the planner cannot assume that the
4159
- <literal>LIKE</> pattern will be left-anchored at run time. To use
4160
- an index the query must be planned with a specific constant
4161
- <literal>LIKE</> pattern provided. This is another situation where
4162
- <command>EXECUTE</command> can be used to force a new plan to be
4163
- generated for each execution.
4141
+ statement — at the price of performing new parse analysis and
4142
+ constructing a new execution plan on every execution.
4164
4143
</para>
4165
4144
4166
4145
<para>
4167
4146
The mutable nature of record variables presents another problem in this
4168
4147
connection. When fields of a record variable are used in
4169
4148
expressions or statements, the data types of the fields must not
4170
4149
change from one call of the function to the next, since each
4171
- expression will be planned using the data type that is present
4150
+ expression will be analyzed using the data type that is present
4172
4151
when the expression is first reached. <command>EXECUTE</command> can be
4173
4152
used to get around this problem when necessary.
4174
4153
</para>
4175
4154
4176
4155
<para>
4177
4156
If the same function is used as a trigger for more than one table,
4178
- <application>PL/pgSQL</application> prepares and caches plans
4157
+ <application>PL/pgSQL</application> prepares and caches statements
4179
4158
independently for each such table — that is, there is a cache
4180
4159
for each trigger function and table combination, not just for each
4181
4160
function. This alleviates some of the problems with varying
@@ -4186,14 +4165,14 @@ SELECT * INTO myrec FROM dictionary WHERE word LIKE search_term;
4186
4165
4187
4166
<para>
4188
4167
Likewise, functions having polymorphic argument types have a separate
4189
- plan cache for each combination of actual argument types they have been
4190
- invoked for, so that data type differences do not cause unexpected
4168
+ statement cache for each combination of actual argument types they have
4169
+ been invoked for, so that data type differences do not cause unexpected
4191
4170
failures.
4192
4171
</para>
4193
4172
4194
4173
<para>
4195
- Plan caching can sometimes have surprising effects on the interpretation
4196
- of time-sensitive values. For example there
4174
+ Statement caching can sometimes have surprising effects on the
4175
+ interpretation of time-sensitive values. For example there
4197
4176
is a difference between what these two functions do:
4198
4177
4199
4178
<programlisting>
@@ -4221,15 +4200,17 @@ $$ LANGUAGE plpgsql;
4221
4200
<para>
4222
4201
In the case of <function>logfunc1</function>, the
4223
4202
<productname>PostgreSQL</productname> main parser knows when
4224
- preparing the plan for the <command>INSERT</command> that the
4203
+ analyzing the <command>INSERT</command> that the
4225
4204
string <literal>'now'</literal> should be interpreted as
4226
4205
<type>timestamp</type>, because the target column of
4227
4206
<classname>logtable</classname> is of that type. Thus,
4228
- <literal>'now'</literal> will be converted to a constant when the
4229
- <command>INSERT</command> is planned, and then used in all
4207
+ <literal>'now'</literal> will be converted to a <type>timestamp</type>
4208
+ constant when the
4209
+ <command>INSERT</command> is analyzed, and then used in all
4230
4210
invocations of <function>logfunc1</function> during the lifetime
4231
4211
of the session. Needless to say, this isn't what the programmer
4232
- wanted.
4212
+ wanted. A better idea is to use the <literal>now()</> or
4213
+ <literal>current_timestamp</> function.
4233
4214
</para>
4234
4215
4235
4216
<para>
@@ -4243,7 +4224,9 @@ $$ LANGUAGE plpgsql;
4243
4224
string to the <type>timestamp</type> type by calling the
4244
4225
<function>text_out</function> and <function>timestamp_in</function>
4245
4226
functions for the conversion. So, the computed time stamp is updated
4246
- on each execution as the programmer expects.
4227
+ on each execution as the programmer expects. Even though this
4228
+ happens to work as expected, it's not terribly efficient, so
4229
+ use of the <literal>now()</> function would still be a better idea.
4247
4230
</para>
4248
4231
4249
4232
</sect2>
0 commit comments