diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2004-06-11 01:09:22 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2004-06-11 01:09:22 +0000 |
commit | 7643bed58ecc87eedf7da1ed1938e85ed770d2f8 (patch) | |
tree | 0d97a4c6cb2d02ba9f9d9b5b5d4e1a6d9cbd538e /doc/src | |
parent | 5fe8c7d6e50e3a310498f333e4f5130659c931fb (diff) | |
download | postgresql-7643bed58ecc87eedf7da1ed1938e85ed770d2f8.tar.gz postgresql-7643bed58ecc87eedf7da1ed1938e85ed770d2f8.zip |
When using extended-query protocol, postpone planning of unnamed statements
until Bind is received, so that actual parameter values are visible to the
planner. Make use of the parameter values for estimation purposes (but
don't fold them into the actual plan). This buys back most of the
potential loss of plan quality that ensues from using out-of-line
parameters instead of putting literal values right into the query text.
This patch creates a notion of constant-folding expressions 'for
estimation purposes only', in which case we can be more aggressive than
the normal eval_const_expressions() logic can be. Right now the only
difference in behavior is inserting bound values for Params, but it will
be interesting to look at other possibilities. One that we've seen
come up repeatedly is reducing now() and related functions to current
values, so that queries like ... WHERE timestampcol > now() - '1 day'
have some chance of being planned effectively.
Oliver Jowett, with some kibitzing from Tom Lane.
Diffstat (limited to 'doc/src')
-rw-r--r-- | doc/src/sgml/protocol.sgml | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml index de3b72738d7..a88bca30026 100644 --- a/doc/src/sgml/protocol.sgml +++ b/doc/src/sgml/protocol.sgml @@ -1,4 +1,4 @@ -<!-- $PostgreSQL: pgsql/doc/src/sgml/protocol.sgml,v 1.51 2004/03/21 22:29:10 tgl Exp $ --> +<!-- $PostgreSQL: pgsql/doc/src/sgml/protocol.sgml,v 1.52 2004/06/11 01:08:33 tgl Exp $ --> <chapter id="protocol"> <title>Frontend/Backend Protocol</title> @@ -685,6 +685,46 @@ </note> <para> + Query planning for named prepared-statement objects occurs when the Parse + message is received. If a query will be repeatedly executed with + different parameters, it may be beneficial to send a single Parse message + containing a parameterized query, followed by multiple Bind + and Execute messages. This will avoid replanning the query on each + execution. + </para> + + <para> + The unnamed prepared statement is likewise planned during Parse processing + if the Parse message defines no parameters. But if there are parameters, + query planning is delayed until the first Bind message for the statement + is received. The planner will consider the actual values of the parameters + provided in the Bind message when planning the query. + </para> + + <note> + <para> + Query plans generated from a parameterized query may be less + efficient than query plans generated from an equivalent query with actual + parameter values substituted. The query planner cannot make decisions + based on actual parameter values (for example, index selectivity) when + planning a parameterized query assigned to a named prepared-statement + object. This possible penalty is avoided when using the unnamed + statement, since it is not planned until actual parameter values are + available. + </para> + + <para> + If a second or subsequent Bind referencing the unnamed prepared-statement + object is received without an intervening Parse, the query is + not replanned. The parameter values used in the first Bind message may + produce a query plan that is only efficient for a subset of possible + parameter values. To force replanning of the query for a fresh set of + parameters, send another Parse message to replace the unnamed + prepared-statement object. + </para> + </note> + + <para> If successfully created, a named portal object lasts till the end of the current transaction, unless explicitly destroyed. An unnamed portal is destroyed at the end of the transaction, or as soon as the next Bind |