Commit d7a6a04d authored by Tom Lane's avatar Tom Lane

Fix planner to restore its previous level of intelligence about pushing

constants through full joins, as in

	select * from tenk1 a full join tenk1 b using (unique1)
	where unique1 = 42;

which should generate a fairly cheap plan where we apply the constraint
unique1 = 42 in each relation scan.  This had been broken by my patch of
2008-06-27, which is now reverted in favor of a more invasive but hopefully
less incorrect approach.  That patch was meant to prevent incorrect extraction
of OR'd indexclauses from OR conditions above an outer join.  To do that
correctly we need more information than the outerjoin_delay flag can provide,
so add a nullable_relids field to RestrictInfo that records exactly which
relations are nulled by outer joins that are underneath a particular qual
clause.  A side benefit is that we can make the test in create_or_index_quals
more specific: it is now smart enough to extract an OR'd indexclause into the
outer side of an outer join, even though it must not do so in the inner side.
The old coding couldn't distinguish these cases so it could not do either.
parent c5593d54
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.429 2009/04/05 19:59:39 tgl Exp $ * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.430 2009/04/16 20:42:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1606,6 +1606,7 @@ _copyRestrictInfo(RestrictInfo *from) ...@@ -1606,6 +1606,7 @@ _copyRestrictInfo(RestrictInfo *from)
COPY_SCALAR_FIELD(pseudoconstant); COPY_SCALAR_FIELD(pseudoconstant);
COPY_BITMAPSET_FIELD(clause_relids); COPY_BITMAPSET_FIELD(clause_relids);
COPY_BITMAPSET_FIELD(required_relids); COPY_BITMAPSET_FIELD(required_relids);
COPY_BITMAPSET_FIELD(nullable_relids);
COPY_BITMAPSET_FIELD(left_relids); COPY_BITMAPSET_FIELD(left_relids);
COPY_BITMAPSET_FIELD(right_relids); COPY_BITMAPSET_FIELD(right_relids);
COPY_NODE_FIELD(orclause); COPY_NODE_FIELD(orclause);
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.352 2009/04/05 19:59:40 tgl Exp $ * $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.353 2009/04/16 20:42:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -759,6 +759,7 @@ _equalRestrictInfo(RestrictInfo *a, RestrictInfo *b) ...@@ -759,6 +759,7 @@ _equalRestrictInfo(RestrictInfo *a, RestrictInfo *b)
COMPARE_SCALAR_FIELD(is_pushed_down); COMPARE_SCALAR_FIELD(is_pushed_down);
COMPARE_SCALAR_FIELD(outerjoin_delayed); COMPARE_SCALAR_FIELD(outerjoin_delayed);
COMPARE_BITMAPSET_FIELD(required_relids); COMPARE_BITMAPSET_FIELD(required_relids);
COMPARE_BITMAPSET_FIELD(nullable_relids);
/* /*
* We ignore all the remaining fields, since they may not be set yet, and * We ignore all the remaining fields, since they may not be set yet, and
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.358 2009/04/05 19:59:40 tgl Exp $ * $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.359 2009/04/16 20:42:16 tgl Exp $
* *
* NOTES * NOTES
* Every node type that can appear in stored rules' parsetrees *must* * Every node type that can appear in stored rules' parsetrees *must*
...@@ -1613,6 +1613,7 @@ _outRestrictInfo(StringInfo str, RestrictInfo *node) ...@@ -1613,6 +1613,7 @@ _outRestrictInfo(StringInfo str, RestrictInfo *node)
WRITE_BOOL_FIELD(pseudoconstant); WRITE_BOOL_FIELD(pseudoconstant);
WRITE_BITMAPSET_FIELD(clause_relids); WRITE_BITMAPSET_FIELD(clause_relids);
WRITE_BITMAPSET_FIELD(required_relids); WRITE_BITMAPSET_FIELD(required_relids);
WRITE_BITMAPSET_FIELD(nullable_relids);
WRITE_BITMAPSET_FIELD(left_relids); WRITE_BITMAPSET_FIELD(left_relids);
WRITE_BITMAPSET_FIELD(right_relids); WRITE_BITMAPSET_FIELD(right_relids);
WRITE_NODE_FIELD(orclause); WRITE_NODE_FIELD(orclause);
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.238 2009/03/11 03:32:22 tgl Exp $ * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.239 2009/04/16 20:42:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -2325,11 +2325,7 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups) ...@@ -2325,11 +2325,7 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
if (boolqual) if (boolqual)
{ {
resultquals = lappend(resultquals, resultquals = lappend(resultquals,
make_restrictinfo(boolqual, make_simple_restrictinfo(boolqual));
true,
false,
false,
NULL));
continue; continue;
} }
} }
...@@ -2360,11 +2356,7 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups) ...@@ -2360,11 +2356,7 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
{ {
Assert(index->amsearchnulls); Assert(index->amsearchnulls);
resultquals = lappend(resultquals, resultquals = lappend(resultquals,
make_restrictinfo(clause, make_simple_restrictinfo(clause));
true,
false,
false,
NULL));
} }
else else
elog(ERROR, "unsupported indexqual type: %d", elog(ERROR, "unsupported indexqual type: %d",
...@@ -2737,7 +2729,7 @@ expand_indexqual_rowcompare(RestrictInfo *rinfo, ...@@ -2737,7 +2729,7 @@ expand_indexqual_rowcompare(RestrictInfo *rinfo,
matching_cols); matching_cols);
rc->rargs = list_truncate((List *) copyObject(clause->rargs), rc->rargs = list_truncate((List *) copyObject(clause->rargs),
matching_cols); matching_cols);
return make_restrictinfo((Expr *) rc, true, false, false, NULL); return make_simple_restrictinfo((Expr *) rc);
} }
else else
{ {
...@@ -2746,7 +2738,7 @@ expand_indexqual_rowcompare(RestrictInfo *rinfo, ...@@ -2746,7 +2738,7 @@ expand_indexqual_rowcompare(RestrictInfo *rinfo,
opexpr = make_opclause(linitial_oid(new_ops), BOOLOID, false, opexpr = make_opclause(linitial_oid(new_ops), BOOLOID, false,
copyObject(linitial(clause->largs)), copyObject(linitial(clause->largs)),
copyObject(linitial(clause->rargs))); copyObject(linitial(clause->rargs)));
return make_restrictinfo(opexpr, true, false, false, NULL); return make_simple_restrictinfo(opexpr);
} }
} }
...@@ -2832,7 +2824,7 @@ prefix_quals(Node *leftop, Oid opfamily, ...@@ -2832,7 +2824,7 @@ prefix_quals(Node *leftop, Oid opfamily,
elog(ERROR, "no = operator for opfamily %u", opfamily); elog(ERROR, "no = operator for opfamily %u", opfamily);
expr = make_opclause(oproid, BOOLOID, false, expr = make_opclause(oproid, BOOLOID, false,
(Expr *) leftop, (Expr *) prefix_const); (Expr *) leftop, (Expr *) prefix_const);
result = list_make1(make_restrictinfo(expr, true, false, false, NULL)); result = list_make1(make_simple_restrictinfo(expr));
return result; return result;
} }
...@@ -2847,7 +2839,7 @@ prefix_quals(Node *leftop, Oid opfamily, ...@@ -2847,7 +2839,7 @@ prefix_quals(Node *leftop, Oid opfamily,
elog(ERROR, "no >= operator for opfamily %u", opfamily); elog(ERROR, "no >= operator for opfamily %u", opfamily);
expr = make_opclause(oproid, BOOLOID, false, expr = make_opclause(oproid, BOOLOID, false,
(Expr *) leftop, (Expr *) prefix_const); (Expr *) leftop, (Expr *) prefix_const);
result = list_make1(make_restrictinfo(expr, true, false, false, NULL)); result = list_make1(make_simple_restrictinfo(expr));
/*------- /*-------
* If we can create a string larger than the prefix, we can say * If we can create a string larger than the prefix, we can say
...@@ -2864,8 +2856,7 @@ prefix_quals(Node *leftop, Oid opfamily, ...@@ -2864,8 +2856,7 @@ prefix_quals(Node *leftop, Oid opfamily,
{ {
expr = make_opclause(oproid, BOOLOID, false, expr = make_opclause(oproid, BOOLOID, false,
(Expr *) leftop, (Expr *) greaterstr); (Expr *) leftop, (Expr *) greaterstr);
result = lappend(result, result = lappend(result, make_simple_restrictinfo(expr));
make_restrictinfo(expr, true, false, false, NULL));
} }
return result; return result;
...@@ -2928,7 +2919,7 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop) ...@@ -2928,7 +2919,7 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
(Expr *) leftop, (Expr *) leftop,
(Expr *) makeConst(datatype, -1, -1, opr1right, (Expr *) makeConst(datatype, -1, -1, opr1right,
false, false)); false, false));
result = list_make1(make_restrictinfo(expr, true, false, false, NULL)); result = list_make1(make_simple_restrictinfo(expr));
/* create clause "key <= network_scan_last( rightop )" */ /* create clause "key <= network_scan_last( rightop )" */
...@@ -2943,8 +2934,7 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop) ...@@ -2943,8 +2934,7 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
(Expr *) leftop, (Expr *) leftop,
(Expr *) makeConst(datatype, -1, -1, opr2right, (Expr *) makeConst(datatype, -1, -1, opr2right,
false, false)); false, false));
result = lappend(result, result = lappend(result, make_simple_restrictinfo(expr));
make_restrictinfo(expr, true, false, false, NULL));
return result; return result;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/path/orindxpath.c,v 1.88 2009/02/15 20:16:21 tgl Exp $ * $PostgreSQL: pgsql/src/backend/optimizer/path/orindxpath.c,v 1.89 2009/04/16 20:42:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -89,12 +89,18 @@ create_or_index_quals(PlannerInfo *root, RelOptInfo *rel) ...@@ -89,12 +89,18 @@ create_or_index_quals(PlannerInfo *root, RelOptInfo *rel)
ListCell *i; ListCell *i;
/* /*
* Find potentially interesting OR joinclauses. Note we must ignore any * Find potentially interesting OR joinclauses.
* joinclauses that are marked outerjoin_delayed or !is_pushed_down, *
* because they cannot be pushed down to the per-relation level due to * We must ignore clauses for which the target rel is in nullable_relids;
* outer-join rules. (XXX in some cases it might be possible to allow * that means there's an outer join below the clause and so it can't be
* this, but it would require substantially more bookkeeping about where * enforced at the relation scan level.
* the clause came from.) *
* We must also ignore clauses that are marked !is_pushed_down (ie they
* are themselves outer-join clauses). It would be safe to extract an
* index condition from such a clause if we are within the nullable rather
* than the non-nullable side of its join, but we haven't got enough
* context here to tell which applies. OR clauses in outer-join quals
* aren't exactly common, so we'll let that case go unoptimized for now.
*/ */
foreach(i, rel->joininfo) foreach(i, rel->joininfo)
{ {
...@@ -102,7 +108,7 @@ create_or_index_quals(PlannerInfo *root, RelOptInfo *rel) ...@@ -102,7 +108,7 @@ create_or_index_quals(PlannerInfo *root, RelOptInfo *rel)
if (restriction_is_or_clause(rinfo) && if (restriction_is_or_clause(rinfo) &&
rinfo->is_pushed_down && rinfo->is_pushed_down &&
!rinfo->outerjoin_delayed) !bms_is_member(rel->relid, rinfo->nullable_relids))
{ {
/* /*
* Use the generate_bitmap_or_paths() machinery to estimate the * Use the generate_bitmap_or_paths() machinery to estimate the
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.149 2009/02/27 22:41:38 tgl Exp $ * $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.150 2009/04/16 20:42:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -53,7 +53,7 @@ static void distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -53,7 +53,7 @@ static void distribute_qual_to_rels(PlannerInfo *root, Node *clause,
Relids ojscope, Relids ojscope,
Relids outerjoin_nonnullable); Relids outerjoin_nonnullable);
static bool check_outerjoin_delay(PlannerInfo *root, Relids *relids_p, static bool check_outerjoin_delay(PlannerInfo *root, Relids *relids_p,
bool is_pushed_down); Relids *nullable_relids_p, bool is_pushed_down);
static bool check_redundant_nullability_qual(PlannerInfo *root, Node *clause); static bool check_redundant_nullability_qual(PlannerInfo *root, Node *clause);
static void check_mergejoinable(RestrictInfo *restrictinfo); static void check_mergejoinable(RestrictInfo *restrictinfo);
static void check_hashjoinable(RestrictInfo *restrictinfo); static void check_hashjoinable(RestrictInfo *restrictinfo);
...@@ -755,6 +755,7 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -755,6 +755,7 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
bool pseudoconstant = false; bool pseudoconstant = false;
bool maybe_equivalence; bool maybe_equivalence;
bool maybe_outer_join; bool maybe_outer_join;
Relids nullable_relids;
RestrictInfo *restrictinfo; RestrictInfo *restrictinfo;
/* /*
...@@ -861,6 +862,7 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -861,6 +862,7 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
Assert(!ojscope); Assert(!ojscope);
is_pushed_down = true; is_pushed_down = true;
outerjoin_delayed = false; outerjoin_delayed = false;
nullable_relids = NULL;
/* Don't feed it back for more deductions */ /* Don't feed it back for more deductions */
maybe_equivalence = false; maybe_equivalence = false;
maybe_outer_join = false; maybe_outer_join = false;
...@@ -882,7 +884,10 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -882,7 +884,10 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
maybe_outer_join = true; maybe_outer_join = true;
/* Check to see if must be delayed by lower outer join */ /* Check to see if must be delayed by lower outer join */
outerjoin_delayed = check_outerjoin_delay(root, &relids, false); outerjoin_delayed = check_outerjoin_delay(root,
&relids,
&nullable_relids,
false);
/* /*
* Now force the qual to be evaluated exactly at the level of joining * Now force the qual to be evaluated exactly at the level of joining
...@@ -907,7 +912,10 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -907,7 +912,10 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
is_pushed_down = true; is_pushed_down = true;
/* Check to see if must be delayed by lower outer join */ /* Check to see if must be delayed by lower outer join */
outerjoin_delayed = check_outerjoin_delay(root, &relids, true); outerjoin_delayed = check_outerjoin_delay(root,
&relids,
&nullable_relids,
true);
if (outerjoin_delayed) if (outerjoin_delayed)
{ {
...@@ -957,7 +965,8 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -957,7 +965,8 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
is_pushed_down, is_pushed_down,
outerjoin_delayed, outerjoin_delayed,
pseudoconstant, pseudoconstant,
relids); relids,
nullable_relids);
/* /*
* If it's a join clause (either naturally, or because delayed by * If it's a join clause (either naturally, or because delayed by
...@@ -1064,7 +1073,9 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -1064,7 +1073,9 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
* If the qual must be delayed, add relids to *relids_p to reflect the lowest * If the qual must be delayed, add relids to *relids_p to reflect the lowest
* safe level for evaluating the qual, and return TRUE. Any extra delay for * safe level for evaluating the qual, and return TRUE. Any extra delay for
* higher-level joins is reflected by setting delay_upper_joins to TRUE in * higher-level joins is reflected by setting delay_upper_joins to TRUE in
* SpecialJoinInfo structs. * SpecialJoinInfo structs. We also compute nullable_relids, the set of
* referenced relids that are nullable by lower outer joins (note that this
* can be nonempty even for a non-delayed qual).
* *
* For an is_pushed_down qual, we can evaluate the qual as soon as (1) we have * For an is_pushed_down qual, we can evaluate the qual as soon as (1) we have
* all the rels it mentions, and (2) we are at or above any outer joins that * all the rels it mentions, and (2) we are at or above any outer joins that
...@@ -1087,8 +1098,8 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -1087,8 +1098,8 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
* mentioning only C cannot be applied below the join to A. * mentioning only C cannot be applied below the join to A.
* *
* For a non-pushed-down qual, this isn't going to determine where we place the * For a non-pushed-down qual, this isn't going to determine where we place the
* qual, but we need to determine outerjoin_delayed anyway for possible use * qual, but we need to determine outerjoin_delayed and nullable_relids anyway
* in reconsider_outer_join_clauses(). * for use later in the planning process.
* *
* Lastly, a pushed-down qual that references the nullable side of any current * Lastly, a pushed-down qual that references the nullable side of any current
* join_info_list member and has to be evaluated above that OJ (because its * join_info_list member and has to be evaluated above that OJ (because its
...@@ -1104,13 +1115,26 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, ...@@ -1104,13 +1115,26 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
* two OJs to commute.) * two OJs to commute.)
*/ */
static bool static bool
check_outerjoin_delay(PlannerInfo *root, Relids *relids_p, check_outerjoin_delay(PlannerInfo *root,
Relids *relids_p, /* in/out parameter */
Relids *nullable_relids_p, /* output parameter */
bool is_pushed_down) bool is_pushed_down)
{ {
Relids relids = *relids_p; Relids relids;
Relids nullable_relids;
bool outerjoin_delayed; bool outerjoin_delayed;
bool found_some; bool found_some;
/* fast path if no special joins */
if (root->join_info_list == NIL)
{
*nullable_relids_p = NULL;
return false;
}
/* must copy relids because we need the original value at the end */
relids = bms_copy(*relids_p);
nullable_relids = NULL;
outerjoin_delayed = false; outerjoin_delayed = false;
do do
{ {
...@@ -1126,18 +1150,23 @@ check_outerjoin_delay(PlannerInfo *root, Relids *relids_p, ...@@ -1126,18 +1150,23 @@ check_outerjoin_delay(PlannerInfo *root, Relids *relids_p,
(sjinfo->jointype == JOIN_FULL && (sjinfo->jointype == JOIN_FULL &&
bms_overlap(relids, sjinfo->min_lefthand))) bms_overlap(relids, sjinfo->min_lefthand)))
{ {
/* yes, so set the result flag */ /* yes; have we included all its rels in relids? */
outerjoin_delayed = true;
/* have we included all its rels in relids? */
if (!bms_is_subset(sjinfo->min_lefthand, relids) || if (!bms_is_subset(sjinfo->min_lefthand, relids) ||
!bms_is_subset(sjinfo->min_righthand, relids)) !bms_is_subset(sjinfo->min_righthand, relids))
{ {
/* no, so add them in */ /* no, so add them in */
relids = bms_add_members(relids, sjinfo->min_lefthand); relids = bms_add_members(relids, sjinfo->min_lefthand);
relids = bms_add_members(relids, sjinfo->min_righthand); relids = bms_add_members(relids, sjinfo->min_righthand);
outerjoin_delayed = true;
/* we'll need another iteration */ /* we'll need another iteration */
found_some = true; found_some = true;
} }
/* track all the nullable rels of relevant OJs */
nullable_relids = bms_add_members(nullable_relids,
sjinfo->min_righthand);
if (sjinfo->jointype == JOIN_FULL)
nullable_relids = bms_add_members(nullable_relids,
sjinfo->min_lefthand);
/* set delay_upper_joins if needed */ /* set delay_upper_joins if needed */
if (is_pushed_down && sjinfo->jointype != JOIN_FULL && if (is_pushed_down && sjinfo->jointype != JOIN_FULL &&
bms_overlap(relids, sjinfo->min_lefthand)) bms_overlap(relids, sjinfo->min_lefthand))
...@@ -1146,7 +1175,13 @@ check_outerjoin_delay(PlannerInfo *root, Relids *relids_p, ...@@ -1146,7 +1175,13 @@ check_outerjoin_delay(PlannerInfo *root, Relids *relids_p,
} }
} while (found_some); } while (found_some);
/* identify just the actually-referenced nullable rels */
nullable_relids = bms_int_members(nullable_relids, *relids_p);
/* replace *relids_p, and return nullable_relids */
bms_free(*relids_p);
*relids_p = relids; *relids_p = relids;
*nullable_relids_p = nullable_relids;
return outerjoin_delayed; return outerjoin_delayed;
} }
...@@ -1352,7 +1387,8 @@ build_implied_join_equality(Oid opno, ...@@ -1352,7 +1387,8 @@ build_implied_join_equality(Oid opno,
true, /* is_pushed_down */ true, /* is_pushed_down */
false, /* outerjoin_delayed */ false, /* outerjoin_delayed */
false, /* pseudoconstant */ false, /* pseudoconstant */
qualscope); qualscope, /* required_relids */
NULL); /* nullable_relids */
/* Set mergejoinability info always, and hashjoinability if enabled */ /* Set mergejoinability info always, and hashjoinability if enabled */
check_mergejoinable(restrictinfo); check_mergejoinable(restrictinfo);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/util/restrictinfo.c,v 1.57 2009/02/06 23:43:23 tgl Exp $ * $PostgreSQL: pgsql/src/backend/optimizer/util/restrictinfo.c,v 1.58 2009/04/16 20:42:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -27,12 +27,14 @@ static RestrictInfo *make_restrictinfo_internal(Expr *clause, ...@@ -27,12 +27,14 @@ static RestrictInfo *make_restrictinfo_internal(Expr *clause,
bool is_pushed_down, bool is_pushed_down,
bool outerjoin_delayed, bool outerjoin_delayed,
bool pseudoconstant, bool pseudoconstant,
Relids required_relids); Relids required_relids,
Relids nullable_relids);
static Expr *make_sub_restrictinfos(Expr *clause, static Expr *make_sub_restrictinfos(Expr *clause,
bool is_pushed_down, bool is_pushed_down,
bool outerjoin_delayed, bool outerjoin_delayed,
bool pseudoconstant, bool pseudoconstant,
Relids required_relids); Relids required_relids,
Relids nullable_relids);
static bool join_clause_is_redundant(PlannerInfo *root, static bool join_clause_is_redundant(PlannerInfo *root,
RestrictInfo *rinfo, RestrictInfo *rinfo,
List *reference_list); List *reference_list);
...@@ -44,9 +46,9 @@ static bool join_clause_is_redundant(PlannerInfo *root, ...@@ -44,9 +46,9 @@ static bool join_clause_is_redundant(PlannerInfo *root,
* Build a RestrictInfo node containing the given subexpression. * Build a RestrictInfo node containing the given subexpression.
* *
* The is_pushed_down, outerjoin_delayed, and pseudoconstant flags for the * The is_pushed_down, outerjoin_delayed, and pseudoconstant flags for the
* RestrictInfo must be supplied by the caller. required_relids can be NULL, * RestrictInfo must be supplied by the caller, as well as the correct value
* in which case it defaults to the actual clause contents (i.e., * for nullable_relids. required_relids can be NULL, in which case it
* clause_relids). * defaults to the actual clause contents (i.e., clause_relids).
* *
* We initialize fields that depend only on the given subexpression, leaving * We initialize fields that depend only on the given subexpression, leaving
* others that depend on context (or may never be needed at all) to be filled * others that depend on context (or may never be needed at all) to be filled
...@@ -57,7 +59,8 @@ make_restrictinfo(Expr *clause, ...@@ -57,7 +59,8 @@ make_restrictinfo(Expr *clause,
bool is_pushed_down, bool is_pushed_down,
bool outerjoin_delayed, bool outerjoin_delayed,
bool pseudoconstant, bool pseudoconstant,
Relids required_relids) Relids required_relids,
Relids nullable_relids)
{ {
/* /*
* If it's an OR clause, build a modified copy with RestrictInfos inserted * If it's an OR clause, build a modified copy with RestrictInfos inserted
...@@ -68,7 +71,8 @@ make_restrictinfo(Expr *clause, ...@@ -68,7 +71,8 @@ make_restrictinfo(Expr *clause,
is_pushed_down, is_pushed_down,
outerjoin_delayed, outerjoin_delayed,
pseudoconstant, pseudoconstant,
required_relids); required_relids,
nullable_relids);
/* Shouldn't be an AND clause, else AND/OR flattening messed up */ /* Shouldn't be an AND clause, else AND/OR flattening messed up */
Assert(!and_clause((Node *) clause)); Assert(!and_clause((Node *) clause));
...@@ -78,7 +82,8 @@ make_restrictinfo(Expr *clause, ...@@ -78,7 +82,8 @@ make_restrictinfo(Expr *clause,
is_pushed_down, is_pushed_down,
outerjoin_delayed, outerjoin_delayed,
pseudoconstant, pseudoconstant,
required_relids); required_relids,
nullable_relids);
} }
/* /*
...@@ -92,8 +97,8 @@ make_restrictinfo(Expr *clause, ...@@ -92,8 +97,8 @@ make_restrictinfo(Expr *clause,
* RestrictInfos. * RestrictInfos.
* *
* The caller must pass is_pushed_down, but we assume outerjoin_delayed * The caller must pass is_pushed_down, but we assume outerjoin_delayed
* and pseudoconstant are false (no such qual should ever get into a * and pseudoconstant are false and nullable_relids is NULL (no other
* bitmapqual). * kind of qual should ever get into a bitmapqual).
* *
* If include_predicates is true, we add any partial index predicates to * If include_predicates is true, we add any partial index predicates to
* the explicit index quals. When this is not true, we return a condition * the explicit index quals. When this is not true, we return a condition
...@@ -224,6 +229,7 @@ make_restrictinfo_from_bitmapqual(Path *bitmapqual, ...@@ -224,6 +229,7 @@ make_restrictinfo_from_bitmapqual(Path *bitmapqual,
is_pushed_down, is_pushed_down,
false, false,
false, false,
NULL,
NULL)); NULL));
} }
} }
...@@ -250,6 +256,7 @@ make_restrictinfo_from_bitmapqual(Path *bitmapqual, ...@@ -250,6 +256,7 @@ make_restrictinfo_from_bitmapqual(Path *bitmapqual,
is_pushed_down, is_pushed_down,
false, false,
false, false,
NULL,
NULL)); NULL));
} }
} }
...@@ -274,7 +281,8 @@ make_restrictinfo_internal(Expr *clause, ...@@ -274,7 +281,8 @@ make_restrictinfo_internal(Expr *clause,
bool is_pushed_down, bool is_pushed_down,
bool outerjoin_delayed, bool outerjoin_delayed,
bool pseudoconstant, bool pseudoconstant,
Relids required_relids) Relids required_relids,
Relids nullable_relids)
{ {
RestrictInfo *restrictinfo = makeNode(RestrictInfo); RestrictInfo *restrictinfo = makeNode(RestrictInfo);
...@@ -284,6 +292,7 @@ make_restrictinfo_internal(Expr *clause, ...@@ -284,6 +292,7 @@ make_restrictinfo_internal(Expr *clause,
restrictinfo->outerjoin_delayed = outerjoin_delayed; restrictinfo->outerjoin_delayed = outerjoin_delayed;
restrictinfo->pseudoconstant = pseudoconstant; restrictinfo->pseudoconstant = pseudoconstant;
restrictinfo->can_join = false; /* may get set below */ restrictinfo->can_join = false; /* may get set below */
restrictinfo->nullable_relids = nullable_relids;
/* /*
* If it's a binary opclause, set up left/right relids info. In any case * If it's a binary opclause, set up left/right relids info. In any case
...@@ -369,7 +378,8 @@ make_restrictinfo_internal(Expr *clause, ...@@ -369,7 +378,8 @@ make_restrictinfo_internal(Expr *clause,
* simple clauses are valid RestrictInfos. * simple clauses are valid RestrictInfos.
* *
* The same is_pushed_down, outerjoin_delayed, and pseudoconstant flag * The same is_pushed_down, outerjoin_delayed, and pseudoconstant flag
* values can be applied to all RestrictInfo nodes in the result. * values can be applied to all RestrictInfo nodes in the result. Likewise
* for nullable_relids.
* *
* The given required_relids are attached to our top-level output, * The given required_relids are attached to our top-level output,
* but any OR-clause constituents are allowed to default to just the * but any OR-clause constituents are allowed to default to just the
...@@ -380,7 +390,8 @@ make_sub_restrictinfos(Expr *clause, ...@@ -380,7 +390,8 @@ make_sub_restrictinfos(Expr *clause,
bool is_pushed_down, bool is_pushed_down,
bool outerjoin_delayed, bool outerjoin_delayed,
bool pseudoconstant, bool pseudoconstant,
Relids required_relids) Relids required_relids,
Relids nullable_relids)
{ {
if (or_clause((Node *) clause)) if (or_clause((Node *) clause))
{ {
...@@ -393,13 +404,15 @@ make_sub_restrictinfos(Expr *clause, ...@@ -393,13 +404,15 @@ make_sub_restrictinfos(Expr *clause,
is_pushed_down, is_pushed_down,
outerjoin_delayed, outerjoin_delayed,
pseudoconstant, pseudoconstant,
NULL)); NULL,
nullable_relids));
return (Expr *) make_restrictinfo_internal(clause, return (Expr *) make_restrictinfo_internal(clause,
make_orclause(orlist), make_orclause(orlist),
is_pushed_down, is_pushed_down,
outerjoin_delayed, outerjoin_delayed,
pseudoconstant, pseudoconstant,
required_relids); required_relids,
nullable_relids);
} }
else if (and_clause((Node *) clause)) else if (and_clause((Node *) clause))
{ {
...@@ -412,7 +425,8 @@ make_sub_restrictinfos(Expr *clause, ...@@ -412,7 +425,8 @@ make_sub_restrictinfos(Expr *clause,
is_pushed_down, is_pushed_down,
outerjoin_delayed, outerjoin_delayed,
pseudoconstant, pseudoconstant,
required_relids)); required_relids,
nullable_relids));
return make_andclause(andlist); return make_andclause(andlist);
} }
else else
...@@ -421,7 +435,8 @@ make_sub_restrictinfos(Expr *clause, ...@@ -421,7 +435,8 @@ make_sub_restrictinfos(Expr *clause,
is_pushed_down, is_pushed_down,
outerjoin_delayed, outerjoin_delayed,
pseudoconstant, pseudoconstant,
required_relids); required_relids,
nullable_relids);
} }
/* /*
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/nodes/relation.h,v 1.171 2009/03/26 17:15:35 tgl Exp $ * $PostgreSQL: pgsql/src/include/nodes/relation.h,v 1.172 2009/04/16 20:42:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -920,8 +920,14 @@ typedef struct HashPath ...@@ -920,8 +920,14 @@ typedef struct HashPath
* *
* RestrictInfo nodes also contain an outerjoin_delayed flag, which is true * RestrictInfo nodes also contain an outerjoin_delayed flag, which is true
* if the clause's applicability must be delayed due to any outer joins * if the clause's applicability must be delayed due to any outer joins
* appearing below its own syntactic level (ie, it references any Vars from * appearing below it (ie, it has to be postponed to some join level higher
* the nullable side of any lower outer join). * than the set of relations it actually references). There is also a
* nullable_relids field, which is the set of rels it references that can be
* forced null by some outer join below the clause. outerjoin_delayed = true
* is subtly different from nullable_relids != NULL: a clause might reference
* some nullable rels and yet not be outerjoin_delayed because it also
* references all the other rels of the outer join(s). A clause that is not
* outerjoin_delayed can be enforced anywhere it is computable.
* *
* In general, the referenced clause might be arbitrarily complex. The * In general, the referenced clause might be arbitrarily complex. The
* kinds of clauses we can handle as indexscan quals, mergejoin clauses, * kinds of clauses we can handle as indexscan quals, mergejoin clauses,
...@@ -983,6 +989,9 @@ typedef struct RestrictInfo ...@@ -983,6 +989,9 @@ typedef struct RestrictInfo
/* The set of relids required to evaluate the clause: */ /* The set of relids required to evaluate the clause: */
Relids required_relids; Relids required_relids;
/* The relids used in the clause that are nullable by lower outer joins: */
Relids nullable_relids;
/* These fields are set for any binary opclause: */ /* These fields are set for any binary opclause: */
Relids left_relids; /* relids in left side of clause */ Relids left_relids; /* relids in left side of clause */
Relids right_relids; /* relids in right side of clause */ Relids right_relids; /* relids in right side of clause */
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/optimizer/restrictinfo.h,v 1.42 2009/01/01 17:24:00 momjian Exp $ * $PostgreSQL: pgsql/src/include/optimizer/restrictinfo.h,v 1.43 2009/04/16 20:42:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -17,11 +17,16 @@ ...@@ -17,11 +17,16 @@
#include "nodes/relation.h" #include "nodes/relation.h"
/* Convenience macro for the common case of a valid-everywhere qual */
#define make_simple_restrictinfo(clause) \
make_restrictinfo(clause, true, false, false, NULL, NULL)
extern RestrictInfo *make_restrictinfo(Expr *clause, extern RestrictInfo *make_restrictinfo(Expr *clause,
bool is_pushed_down, bool is_pushed_down,
bool outerjoin_delayed, bool outerjoin_delayed,
bool pseudoconstant, bool pseudoconstant,
Relids required_relids); Relids required_relids,
Relids nullable_relids);
extern List *make_restrictinfo_from_bitmapqual(Path *bitmapqual, extern List *make_restrictinfo_from_bitmapqual(Path *bitmapqual,
bool is_pushed_down, bool is_pushed_down,
bool include_predicates); bool include_predicates);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment