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

Commit 6b73d7e

Browse files
committed
Fix an oversight I made in a cleanup patch over a year ago:
eval_const_expressions needs to be passed the PlannerInfo ("root") structure, because in some cases we want it to substitute values for Param nodes. (So "constant" is not so constant as all that ...) This mistake partially disabled optimization of unnamed extended-Query statements in 8.3: in particular the LIKE-to-indexscan optimization would never be applied if the LIKE pattern was passed as a parameter, and constraint exclusion depending on a parameter value didn't work either.
1 parent d344115 commit 6b73d7e

File tree

8 files changed

+33
-24
lines changed

8 files changed

+33
-24
lines changed

src/backend/optimizer/path/allpaths.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.169 2008/03/24 21:53:03 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.170 2008/04/01 00:48:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -205,7 +205,7 @@ set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
205205
* set_append_rel_pathlist().
206206
*/
207207
if (rel->reloptkind == RELOPT_BASEREL &&
208-
relation_excluded_by_constraints(rel, rte))
208+
relation_excluded_by_constraints(root, rel, rte))
209209
{
210210
set_dummy_rel_pathlist(rel);
211211
return;
@@ -321,7 +321,7 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
321321
adjust_appendrel_attrs((Node *) rel->baserestrictinfo,
322322
appinfo);
323323

324-
if (relation_excluded_by_constraints(childrel, childRTE))
324+
if (relation_excluded_by_constraints(root, childrel, childRTE))
325325
{
326326
/*
327327
* This child need not be scanned, so we can omit it from the

src/backend/optimizer/plan/initsplan.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.138 2008/01/09 20:42:28 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.139 2008/04/01 00:48:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1219,7 +1219,7 @@ process_implied_equality(PlannerInfo *root,
12191219
/* If both constant, try to reduce to a boolean constant. */
12201220
if (both_const)
12211221
{
1222-
clause = (Expr *) eval_const_expressions((Node *) clause);
1222+
clause = (Expr *) eval_const_expressions(root, (Node *) clause);
12231223

12241224
/* If we produced const TRUE, just drop the clause */
12251225
if (clause && IsA(clause, Const))

src/backend/optimizer/plan/planner.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.230 2008/03/29 00:15:28 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.231 2008/04/01 00:48:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -503,7 +503,7 @@ preprocess_expression(PlannerInfo *root, Node *expr, int kind)
503503
(root->parse->jointree->fromlist != NIL ||
504504
kind == EXPRKIND_QUAL ||
505505
root->query_level > 1))
506-
expr = eval_const_expressions(expr);
506+
expr = eval_const_expressions(root, expr);
507507

508508
/*
509509
* If it's a qual or havingQual, canonicalize it.

src/backend/optimizer/util/clauses.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.256 2008/03/25 22:42:43 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.257 2008/04/01 00:48:33 tgl Exp $
1212
*
1313
* HISTORY
1414
* AUTHOR DATE MAJOR EVENT
@@ -1687,16 +1687,22 @@ rowtype_field_matches(Oid rowtypeid, int fieldnum,
16871687
* We assume that the tree has already been type-checked and contains
16881688
* only operators and functions that are reasonable to try to execute.
16891689
*
1690+
* NOTE: "root" can be passed as NULL if the caller never wants to do any
1691+
* Param substitutions.
1692+
*
16901693
* NOTE: the planner assumes that this will always flatten nested AND and
16911694
* OR clauses into N-argument form. See comments in prepqual.c.
16921695
*--------------------
16931696
*/
16941697
Node *
1695-
eval_const_expressions(Node *node)
1698+
eval_const_expressions(PlannerInfo *root, Node *node)
16961699
{
16971700
eval_const_expressions_context context;
16981701

1699-
context.boundParams = NULL; /* don't use any bound params */
1702+
if (root)
1703+
context.boundParams = root->glob->boundParams; /* bound Params */
1704+
else
1705+
context.boundParams = NULL;
17001706
context.active_fns = NIL; /* nothing being recursively simplified */
17011707
context.case_val = NULL; /* no CASE being examined */
17021708
context.estimate = false; /* safe transformations only */

src/backend/optimizer/util/plancat.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.144 2008/03/26 21:10:38 alvherre Exp $
12+
* $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.145 2008/04/01 00:48:33 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -47,7 +47,8 @@ bool constraint_exclusion = false;
4747
get_relation_info_hook_type get_relation_info_hook = NULL;
4848

4949

50-
static List *get_relation_constraints(Oid relationObjectId, RelOptInfo *rel,
50+
static List *get_relation_constraints(PlannerInfo *root,
51+
Oid relationObjectId, RelOptInfo *rel,
5152
bool include_notnull);
5253

5354

@@ -462,7 +463,8 @@ estimate_rel_size(Relation rel, int32 *attr_widths,
462463
* point in caching the data in RelOptInfo.
463464
*/
464465
static List *
465-
get_relation_constraints(Oid relationObjectId, RelOptInfo *rel,
466+
get_relation_constraints(PlannerInfo *root,
467+
Oid relationObjectId, RelOptInfo *rel,
466468
bool include_notnull)
467469
{
468470
List *result = NIL;
@@ -497,7 +499,7 @@ get_relation_constraints(Oid relationObjectId, RelOptInfo *rel,
497499
* stuff involving subqueries, however, since we don't allow any
498500
* in check constraints.)
499501
*/
500-
cexpr = eval_const_expressions(cexpr);
502+
cexpr = eval_const_expressions(root, cexpr);
501503

502504
cexpr = (Node *) canonicalize_qual((Expr *) cexpr);
503505

@@ -561,7 +563,8 @@ get_relation_constraints(Oid relationObjectId, RelOptInfo *rel,
561563
* it can be called before filling in other fields of the RelOptInfo.
562564
*/
563565
bool
564-
relation_excluded_by_constraints(RelOptInfo *rel, RangeTblEntry *rte)
566+
relation_excluded_by_constraints(PlannerInfo *root,
567+
RelOptInfo *rel, RangeTblEntry *rte)
565568
{
566569
List *safe_restrictions;
567570
List *constraint_pred;
@@ -600,7 +603,7 @@ relation_excluded_by_constraints(RelOptInfo *rel, RangeTblEntry *rte)
600603
* OK to fetch the constraint expressions. Include "col IS NOT NULL"
601604
* expressions for attnotnull columns, in case we can refute those.
602605
*/
603-
constraint_pred = get_relation_constraints(rte->relid, rel, true);
606+
constraint_pred = get_relation_constraints(root, rte->relid, rel, true);
604607

605608
/*
606609
* We do not currently enforce that CHECK constraints contain only

src/backend/utils/cache/relcache.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.269 2008/03/26 21:10:39 alvherre Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.270 2008/04/01 00:48:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3068,7 +3068,7 @@ RelationGetIndexExpressions(Relation relation)
30683068
* them to similarly-processed qual clauses, and may fail to detect valid
30693069
* matches without this. We don't bother with canonicalize_qual, however.
30703070
*/
3071-
result = (List *) eval_const_expressions((Node *) result);
3071+
result = (List *) eval_const_expressions(NULL, (Node *) result);
30723072

30733073
/*
30743074
* Also mark any coercion format fields as "don't care", so that the
@@ -3138,7 +3138,7 @@ RelationGetIndexPredicate(Relation relation)
31383138
* stuff involving subqueries, however, since we don't allow any in index
31393139
* predicates.)
31403140
*/
3141-
result = (List *) eval_const_expressions((Node *) result);
3141+
result = (List *) eval_const_expressions(NULL, (Node *) result);
31423142

31433143
result = (List *) canonicalize_qual((Expr *) result);
31443144

src/include/optimizer/clauses.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/optimizer/clauses.h,v 1.89 2008/03/18 22:04:14 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/optimizer/clauses.h,v 1.90 2008/04/01 00:48:33 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -75,7 +75,7 @@ extern Node *strip_implicit_coercions(Node *node);
7575

7676
extern void set_coercionform_dontcare(Node *node);
7777

78-
extern Node *eval_const_expressions(Node *node);
78+
extern Node *eval_const_expressions(PlannerInfo *root, Node *node);
7979

8080
extern Node *estimate_expression_value(PlannerInfo *root, Node *node);
8181

src/include/optimizer/plancat.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/optimizer/plancat.h,v 1.48 2008/03/15 20:46:31 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/optimizer/plancat.h,v 1.49 2008/04/01 00:48:33 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -31,8 +31,8 @@ extern void get_relation_info(PlannerInfo *root, Oid relationObjectId,
3131
extern void estimate_rel_size(Relation rel, int32 *attr_widths,
3232
BlockNumber *pages, double *tuples);
3333

34-
extern bool relation_excluded_by_constraints(RelOptInfo *rel,
35-
RangeTblEntry *rte);
34+
extern bool relation_excluded_by_constraints(PlannerInfo *root,
35+
RelOptInfo *rel, RangeTblEntry *rte);
3636

3737
extern List *build_physical_tlist(PlannerInfo *root, RelOptInfo *rel);
3838

0 commit comments

Comments
 (0)