diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2006-07-01 18:38:33 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2006-07-01 18:38:33 +0000 |
commit | cffd89ca736e485309cd51ae056f837bd7e683ad (patch) | |
tree | 7ebf13ae5d921d074382d80be66a8d97e7a822e1 /src/backend/optimizer/path/indxpath.c | |
parent | 68628fc38ea7a3c72f6a813b0193d836731d9c10 (diff) | |
download | postgresql-cffd89ca736e485309cd51ae056f837bd7e683ad.tar.gz postgresql-cffd89ca736e485309cd51ae056f837bd7e683ad.zip |
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
Diffstat (limited to 'src/backend/optimizer/path/indxpath.c')
-rw-r--r-- | src/backend/optimizer/path/indxpath.c | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c index 75f2487757e..6e6f4ac3a71 100644 --- a/src/backend/optimizer/path/indxpath.c +++ b/src/backend/optimizer/path/indxpath.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.208 2006/06/07 17:08:07 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.209 2006/07/01 18:38:32 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -998,6 +998,15 @@ match_clause_to_indexcol(IndexOptInfo *index, Oid expr_op; bool plain_op; + /* + * Never match pseudoconstants to indexes. (Normally this could not + * happen anyway, since a pseudoconstant clause couldn't contain a + * Var, but what if someone builds an expression index on a constant? + * It's not totally unreasonable to do so with a partial index, either.) + */ + if (rinfo->pseudoconstant) + return false; + /* First check for boolean-index cases. */ if (IsBooleanOpclass(opclass)) { @@ -2212,6 +2221,7 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups) make_restrictinfo(boolqual, true, false, + false, NULL)); continue; } @@ -2577,7 +2587,7 @@ expand_indexqual_rowcompare(RestrictInfo *rinfo, matching_cols); rc->rargs = list_truncate((List *) copyObject(clause->rargs), matching_cols); - return make_restrictinfo((Expr *) rc, true, false, NULL); + return make_restrictinfo((Expr *) rc, true, false, false, NULL); } else { @@ -2586,7 +2596,7 @@ expand_indexqual_rowcompare(RestrictInfo *rinfo, opexpr = make_opclause(linitial_oid(new_ops), BOOLOID, false, copyObject(linitial(clause->largs)), copyObject(linitial(clause->rargs))); - return make_restrictinfo(opexpr, true, false, NULL); + return make_restrictinfo(opexpr, true, false, false, NULL); } } @@ -2678,7 +2688,7 @@ prefix_quals(Node *leftop, Oid opclass, elog(ERROR, "no = operator for opclass %u", opclass); expr = make_opclause(oproid, BOOLOID, false, (Expr *) leftop, (Expr *) prefix_const); - result = list_make1(make_restrictinfo(expr, true, false, NULL)); + result = list_make1(make_restrictinfo(expr, true, false, false, NULL)); return result; } @@ -2693,7 +2703,7 @@ prefix_quals(Node *leftop, Oid opclass, elog(ERROR, "no >= operator for opclass %u", opclass); expr = make_opclause(oproid, BOOLOID, false, (Expr *) leftop, (Expr *) prefix_const); - result = list_make1(make_restrictinfo(expr, true, false, NULL)); + result = list_make1(make_restrictinfo(expr, true, false, false, NULL)); /*------- * If we can create a string larger than the prefix, we can say @@ -2709,7 +2719,8 @@ prefix_quals(Node *leftop, Oid opclass, elog(ERROR, "no < operator for opclass %u", opclass); expr = make_opclause(oproid, BOOLOID, false, (Expr *) leftop, (Expr *) greaterstr); - result = lappend(result, make_restrictinfo(expr, true, false, NULL)); + result = lappend(result, + make_restrictinfo(expr, true, false, false, NULL)); } return result; @@ -2772,7 +2783,7 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass, Datum rightop) (Expr *) leftop, (Expr *) makeConst(datatype, -1, opr1right, false, false)); - result = list_make1(make_restrictinfo(expr, true, false, NULL)); + result = list_make1(make_restrictinfo(expr, true, false, false, NULL)); /* create clause "key <= network_scan_last( rightop )" */ @@ -2787,7 +2798,8 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass, Datum rightop) (Expr *) leftop, (Expr *) makeConst(datatype, -1, opr2right, false, false)); - result = lappend(result, make_restrictinfo(expr, true, false, NULL)); + result = lappend(result, + make_restrictinfo(expr, true, false, false, NULL)); return result; } |