1 /*-------------------------------------------------------------------------
4 * Target list, qualification, joininfo initialization routines
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_operator.h"
18 #include "catalog/pg_type.h"
19 #include "optimizer/clauses.h"
20 #include "optimizer/cost.h"
21 #include "optimizer/joininfo.h"
22 #include "optimizer/pathnode.h"
23 #include "optimizer/paths.h"
24 #include "optimizer/placeholder.h"
25 #include "optimizer/planmain.h"
26 #include "optimizer/prep.h"
27 #include "optimizer/restrictinfo.h"
28 #include "optimizer/var.h"
29 #include "parser/parse_expr.h"
30 #include "parser/parse_oper.h"
31 #include "utils/builtins.h"
32 #include "utils/lsyscache.h"
33 #include "utils/syscache.h"
36 /* These parameters are set by GUC */
37 int from_collapse_limit
;
38 int join_collapse_limit
;
41 static List
*deconstruct_recurse(PlannerInfo
*root
, Node
*jtnode
,
42 bool below_outer_join
,
43 Relids
*qualscope
, Relids
*inner_join_rels
);
44 static SpecialJoinInfo
*make_outerjoininfo(PlannerInfo
*root
,
45 Relids left_rels
, Relids right_rels
,
46 Relids inner_join_rels
,
47 JoinType jointype
, List
*clause
);
48 static void distribute_qual_to_rels(PlannerInfo
*root
, Node
*clause
,
50 bool below_outer_join
,
54 Relids outerjoin_nonnullable
);
55 static bool check_outerjoin_delay(PlannerInfo
*root
, Relids
*relids_p
,
57 static bool check_redundant_nullability_qual(PlannerInfo
*root
, Node
*clause
);
58 static void check_mergejoinable(RestrictInfo
*restrictinfo
);
59 static void check_hashjoinable(RestrictInfo
*restrictinfo
);
62 /*****************************************************************************
66 *****************************************************************************/
69 * add_base_rels_to_query
71 * Scan the query's jointree and create baserel RelOptInfos for all
72 * the base relations (ie, table, subquery, and function RTEs)
73 * appearing in the jointree.
75 * The initial invocation must pass root->parse->jointree as the value of
76 * jtnode. Internally, the function recurses through the jointree.
78 * At the end of this process, there should be one baserel RelOptInfo for
79 * every non-join RTE that is used in the query. Therefore, this routine
80 * is the only place that should call build_simple_rel with reloptkind
81 * RELOPT_BASEREL. (Note: build_simple_rel recurses internally to build
82 * "other rel" RelOptInfos for the members of any appendrels we find here.)
85 add_base_rels_to_query(PlannerInfo
*root
, Node
*jtnode
)
89 if (IsA(jtnode
, RangeTblRef
))
91 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
93 (void) build_simple_rel(root
, varno
, RELOPT_BASEREL
);
95 else if (IsA(jtnode
, FromExpr
))
97 FromExpr
*f
= (FromExpr
*) jtnode
;
100 foreach(l
, f
->fromlist
)
101 add_base_rels_to_query(root
, lfirst(l
));
103 else if (IsA(jtnode
, JoinExpr
))
105 JoinExpr
*j
= (JoinExpr
*) jtnode
;
107 add_base_rels_to_query(root
, j
->larg
);
108 add_base_rels_to_query(root
, j
->rarg
);
111 elog(ERROR
, "unrecognized node type: %d",
112 (int) nodeTag(jtnode
));
116 /*****************************************************************************
120 *****************************************************************************/
123 * build_base_rel_tlists
124 * Add targetlist entries for each var needed in the query's final tlist
125 * to the appropriate base relations.
127 * We mark such vars as needed by "relation 0" to ensure that they will
128 * propagate up through all join plan steps.
131 build_base_rel_tlists(PlannerInfo
*root
, List
*final_tlist
)
133 List
*tlist_vars
= pull_var_clause((Node
*) final_tlist
, true);
135 if (tlist_vars
!= NIL
)
137 add_vars_to_targetlist(root
, tlist_vars
, bms_make_singleton(0));
138 list_free(tlist_vars
);
143 * add_vars_to_targetlist
144 * For each variable appearing in the list, add it to the owning
145 * relation's targetlist if not already present, and mark the variable
146 * as being needed for the indicated join (or for final output if
147 * where_needed includes "relation 0").
149 * The list may also contain PlaceHolderVars. These don't necessarily
150 * have a single owning relation; we keep their attr_needed info in
151 * root->placeholder_list instead.
154 add_vars_to_targetlist(PlannerInfo
*root
, List
*vars
, Relids where_needed
)
158 Assert(!bms_is_empty(where_needed
));
162 Node
*node
= (Node
*) lfirst(temp
);
166 Var
*var
= (Var
*) node
;
167 RelOptInfo
*rel
= find_base_rel(root
, var
->varno
);
168 int attno
= var
->varattno
;
170 Assert(attno
>= rel
->min_attr
&& attno
<= rel
->max_attr
);
171 attno
-= rel
->min_attr
;
172 if (rel
->attr_needed
[attno
] == NULL
)
174 /* Variable not yet requested, so add to reltargetlist */
175 /* XXX is copyObject necessary here? */
176 rel
->reltargetlist
= lappend(rel
->reltargetlist
,
179 rel
->attr_needed
[attno
] = bms_add_members(rel
->attr_needed
[attno
],
182 else if (IsA(node
, PlaceHolderVar
))
184 PlaceHolderVar
*phv
= (PlaceHolderVar
*) node
;
185 PlaceHolderInfo
*phinfo
= find_placeholder_info(root
, phv
);
187 phinfo
->ph_needed
= bms_add_members(phinfo
->ph_needed
,
191 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(node
));
196 /*****************************************************************************
198 * JOIN TREE PROCESSING
200 *****************************************************************************/
203 * deconstruct_jointree
204 * Recursively scan the query's join tree for WHERE and JOIN/ON qual
205 * clauses, and add these to the appropriate restrictinfo and joininfo
206 * lists belonging to base RelOptInfos. Also, add SpecialJoinInfo nodes
207 * to root->join_info_list for any outer joins appearing in the query tree.
208 * Return a "joinlist" data structure showing the join order decisions
209 * that need to be made by make_one_rel().
211 * The "joinlist" result is a list of items that are either RangeTblRef
212 * jointree nodes or sub-joinlists. All the items at the same level of
213 * joinlist must be joined in an order to be determined by make_one_rel()
214 * (note that legal orders may be constrained by SpecialJoinInfo nodes).
215 * A sub-joinlist represents a subproblem to be planned separately. Currently
216 * sub-joinlists arise only from FULL OUTER JOIN or when collapsing of
217 * subproblems is stopped by join_collapse_limit or from_collapse_limit.
219 * NOTE: when dealing with inner joins, it is appropriate to let a qual clause
220 * be evaluated at the lowest level where all the variables it mentions are
221 * available. However, we cannot push a qual down into the nullable side(s)
222 * of an outer join since the qual might eliminate matching rows and cause a
223 * NULL row to be incorrectly emitted by the join. Therefore, we artificially
224 * OR the minimum-relids of such an outer join into the required_relids of
225 * clauses appearing above it. This forces those clauses to be delayed until
226 * application of the outer join (or maybe even higher in the join tree).
229 deconstruct_jointree(PlannerInfo
*root
)
232 Relids inner_join_rels
;
234 /* Start recursion at top of jointree */
235 Assert(root
->parse
->jointree
!= NULL
&&
236 IsA(root
->parse
->jointree
, FromExpr
));
238 return deconstruct_recurse(root
, (Node
*) root
->parse
->jointree
, false,
239 &qualscope
, &inner_join_rels
);
243 * deconstruct_recurse
244 * One recursion level of deconstruct_jointree processing.
247 * jtnode is the jointree node to examine
248 * below_outer_join is TRUE if this node is within the nullable side of a
249 * higher-level outer join
251 * *qualscope gets the set of base Relids syntactically included in this
252 * jointree node (do not modify or free this, as it may also be pointed
253 * to by RestrictInfo and SpecialJoinInfo nodes)
254 * *inner_join_rels gets the set of base Relids syntactically included in
255 * inner joins appearing at or below this jointree node (do not modify
256 * or free this, either)
257 * Return value is the appropriate joinlist for this jointree node
259 * In addition, entries will be added to root->join_info_list for outer joins.
262 deconstruct_recurse(PlannerInfo
*root
, Node
*jtnode
, bool below_outer_join
,
263 Relids
*qualscope
, Relids
*inner_join_rels
)
270 *inner_join_rels
= NULL
;
273 if (IsA(jtnode
, RangeTblRef
))
275 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
277 /* No quals to deal with, just return correct result */
278 *qualscope
= bms_make_singleton(varno
);
279 /* A single baserel does not create an inner join */
280 *inner_join_rels
= NULL
;
281 joinlist
= list_make1(jtnode
);
283 else if (IsA(jtnode
, FromExpr
))
285 FromExpr
*f
= (FromExpr
*) jtnode
;
290 * First, recurse to handle child joins. We collapse subproblems into
291 * a single joinlist whenever the resulting joinlist wouldn't exceed
292 * from_collapse_limit members. Also, always collapse one-element
293 * subproblems, since that won't lengthen the joinlist anyway.
296 *inner_join_rels
= NULL
;
298 remaining
= list_length(f
->fromlist
);
299 foreach(l
, f
->fromlist
)
301 Relids sub_qualscope
;
305 sub_joinlist
= deconstruct_recurse(root
, lfirst(l
),
309 *qualscope
= bms_add_members(*qualscope
, sub_qualscope
);
310 sub_members
= list_length(sub_joinlist
);
312 if (sub_members
<= 1 ||
313 list_length(joinlist
) + sub_members
+ remaining
<= from_collapse_limit
)
314 joinlist
= list_concat(joinlist
, sub_joinlist
);
316 joinlist
= lappend(joinlist
, sub_joinlist
);
320 * A FROM with more than one list element is an inner join subsuming
321 * all below it, so we should report inner_join_rels = qualscope. If
322 * there was exactly one element, we should (and already did) report
323 * whatever its inner_join_rels were. If there were no elements (is
324 * that possible?) the initialization before the loop fixed it.
326 if (list_length(f
->fromlist
) > 1)
327 *inner_join_rels
= *qualscope
;
330 * Now process the top-level quals.
332 foreach(l
, (List
*) f
->quals
)
334 Node
*qual
= (Node
*) lfirst(l
);
336 distribute_qual_to_rels(root
, qual
,
337 false, below_outer_join
, JOIN_INNER
,
338 *qualscope
, NULL
, NULL
);
341 else if (IsA(jtnode
, JoinExpr
))
343 JoinExpr
*j
= (JoinExpr
*) jtnode
;
352 SpecialJoinInfo
*sjinfo
;
356 * Order of operations here is subtle and critical. First we recurse
357 * to handle sub-JOINs. Their join quals will be placed without
358 * regard for whether this level is an outer join, which is correct.
359 * Then we place our own join quals, which are restricted by lower
360 * outer joins in any case, and are forced to this level if this is an
361 * outer join and they mention the outer side. Finally, if this is an
362 * outer join, we create a join_info_list entry for the join. This
363 * will prevent quals above us in the join tree that use those rels
364 * from being pushed down below this level. (It's okay for upper
365 * quals to be pushed down to the outer side, however.)
370 leftjoinlist
= deconstruct_recurse(root
, j
->larg
,
372 &leftids
, &left_inners
);
373 rightjoinlist
= deconstruct_recurse(root
, j
->rarg
,
375 &rightids
, &right_inners
);
376 *qualscope
= bms_union(leftids
, rightids
);
377 *inner_join_rels
= *qualscope
;
378 /* Inner join adds no restrictions for quals */
379 nonnullable_rels
= NULL
;
383 leftjoinlist
= deconstruct_recurse(root
, j
->larg
,
385 &leftids
, &left_inners
);
386 rightjoinlist
= deconstruct_recurse(root
, j
->rarg
,
388 &rightids
, &right_inners
);
389 *qualscope
= bms_union(leftids
, rightids
);
390 *inner_join_rels
= bms_union(left_inners
, right_inners
);
391 nonnullable_rels
= leftids
;
394 leftjoinlist
= deconstruct_recurse(root
, j
->larg
,
396 &leftids
, &left_inners
);
397 rightjoinlist
= deconstruct_recurse(root
, j
->rarg
,
399 &rightids
, &right_inners
);
400 *qualscope
= bms_union(leftids
, rightids
);
401 *inner_join_rels
= bms_union(left_inners
, right_inners
);
402 /* Semi join adds no restrictions for quals */
403 nonnullable_rels
= NULL
;
406 leftjoinlist
= deconstruct_recurse(root
, j
->larg
,
408 &leftids
, &left_inners
);
409 rightjoinlist
= deconstruct_recurse(root
, j
->rarg
,
411 &rightids
, &right_inners
);
412 *qualscope
= bms_union(leftids
, rightids
);
413 *inner_join_rels
= bms_union(left_inners
, right_inners
);
414 /* each side is both outer and inner */
415 nonnullable_rels
= *qualscope
;
418 /* JOIN_RIGHT was eliminated during reduce_outer_joins() */
419 elog(ERROR
, "unrecognized join type: %d",
421 nonnullable_rels
= NULL
; /* keep compiler quiet */
422 leftjoinlist
= rightjoinlist
= NIL
;
427 * For an OJ, form the SpecialJoinInfo now, because we need the OJ's
428 * semantic scope (ojscope) to pass to distribute_qual_to_rels. But
429 * we mustn't add it to join_info_list just yet, because we don't want
430 * distribute_qual_to_rels to think it is an outer join below us.
432 * Semijoins are a bit of a hybrid: we build a SpecialJoinInfo,
433 * but we want ojscope = NULL for distribute_qual_to_rels.
435 if (j
->jointype
!= JOIN_INNER
)
437 sjinfo
= make_outerjoininfo(root
,
442 if (j
->jointype
== JOIN_SEMI
)
445 ojscope
= bms_union(sjinfo
->min_lefthand
,
446 sjinfo
->min_righthand
);
454 /* Process the qual clauses */
455 foreach(l
, (List
*) j
->quals
)
457 Node
*qual
= (Node
*) lfirst(l
);
459 distribute_qual_to_rels(root
, qual
,
460 false, below_outer_join
, j
->jointype
,
462 ojscope
, nonnullable_rels
);
465 /* Now we can add the SpecialJoinInfo to join_info_list */
467 root
->join_info_list
= lappend(root
->join_info_list
, sjinfo
);
470 * Finally, compute the output joinlist. We fold subproblems together
471 * except at a FULL JOIN or where join_collapse_limit would be
474 if (j
->jointype
== JOIN_FULL
)
476 /* force the join order exactly at this node */
477 joinlist
= list_make1(list_make2(leftjoinlist
, rightjoinlist
));
479 else if (list_length(leftjoinlist
) + list_length(rightjoinlist
) <=
482 /* OK to combine subproblems */
483 joinlist
= list_concat(leftjoinlist
, rightjoinlist
);
487 /* can't combine, but needn't force join order above here */
491 /* avoid creating useless 1-element sublists */
492 if (list_length(leftjoinlist
) == 1)
493 leftpart
= (Node
*) linitial(leftjoinlist
);
495 leftpart
= (Node
*) leftjoinlist
;
496 if (list_length(rightjoinlist
) == 1)
497 rightpart
= (Node
*) linitial(rightjoinlist
);
499 rightpart
= (Node
*) rightjoinlist
;
500 joinlist
= list_make2(leftpart
, rightpart
);
505 elog(ERROR
, "unrecognized node type: %d",
506 (int) nodeTag(jtnode
));
507 joinlist
= NIL
; /* keep compiler quiet */
514 * Build a SpecialJoinInfo for the current outer join
517 * left_rels: the base Relids syntactically on outer side of join
518 * right_rels: the base Relids syntactically on inner side of join
519 * inner_join_rels: base Relids participating in inner joins below this one
520 * jointype: what it says (must always be LEFT, FULL, SEMI, or ANTI)
521 * clause: the outer join's join condition (in implicit-AND format)
523 * The node should eventually be appended to root->join_info_list, but we
524 * do not do that here.
526 * Note: we assume that this function is invoked bottom-up, so that
527 * root->join_info_list already contains entries for all outer joins that are
528 * syntactically below this one.
530 static SpecialJoinInfo
*
531 make_outerjoininfo(PlannerInfo
*root
,
532 Relids left_rels
, Relids right_rels
,
533 Relids inner_join_rels
,
534 JoinType jointype
, List
*clause
)
536 SpecialJoinInfo
*sjinfo
= makeNode(SpecialJoinInfo
);
537 Relids clause_relids
;
538 Relids strict_relids
;
540 Relids min_righthand
;
544 * We should not see RIGHT JOIN here because left/right were switched
547 Assert(jointype
!= JOIN_INNER
);
548 Assert(jointype
!= JOIN_RIGHT
);
551 * Presently the executor cannot support FOR UPDATE/SHARE marking of rels
552 * appearing on the nullable side of an outer join. (It's somewhat unclear
553 * what that would mean, anyway: what should we mark when a result row is
554 * generated from no element of the nullable relation?) So, complain if
555 * any nullable rel is FOR UPDATE/SHARE.
557 * You might be wondering why this test isn't made far upstream in the
558 * parser. It's because the parser hasn't got enough info --- consider
559 * FOR UPDATE applied to a view. Only after rewriting and flattening do
560 * we know whether the view contains an outer join.
562 foreach(l
, root
->parse
->rowMarks
)
564 RowMarkClause
*rc
= (RowMarkClause
*) lfirst(l
);
566 if (bms_is_member(rc
->rti
, right_rels
) ||
567 (jointype
== JOIN_FULL
&& bms_is_member(rc
->rti
, left_rels
)))
569 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
570 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to the nullable side of an outer join")));
573 sjinfo
->syn_lefthand
= left_rels
;
574 sjinfo
->syn_righthand
= right_rels
;
575 sjinfo
->jointype
= jointype
;
576 /* this always starts out false */
577 sjinfo
->delay_upper_joins
= false;
578 sjinfo
->join_quals
= clause
;
580 /* If it's a full join, no need to be very smart */
581 if (jointype
== JOIN_FULL
)
583 sjinfo
->min_lefthand
= bms_copy(left_rels
);
584 sjinfo
->min_righthand
= bms_copy(right_rels
);
585 sjinfo
->lhs_strict
= false; /* don't care about this */
590 * Retrieve all relids mentioned within the join clause.
592 clause_relids
= pull_varnos((Node
*) clause
);
595 * For which relids is the clause strict, ie, it cannot succeed if the
596 * rel's columns are all NULL?
598 strict_relids
= find_nonnullable_rels((Node
*) clause
);
600 /* Remember whether the clause is strict for any LHS relations */
601 sjinfo
->lhs_strict
= bms_overlap(strict_relids
, left_rels
);
604 * Required LHS always includes the LHS rels mentioned in the clause. We
605 * may have to add more rels based on lower outer joins; see below.
607 min_lefthand
= bms_intersect(clause_relids
, left_rels
);
610 * Similarly for required RHS. But here, we must also include any lower
611 * inner joins, to ensure we don't try to commute with any of them.
613 min_righthand
= bms_int_members(bms_union(clause_relids
, inner_join_rels
),
616 foreach(l
, root
->join_info_list
)
618 SpecialJoinInfo
*otherinfo
= (SpecialJoinInfo
*) lfirst(l
);
620 /* ignore full joins --- other mechanisms preserve their ordering */
621 if (otherinfo
->jointype
== JOIN_FULL
)
625 * For a lower OJ in our LHS, if our join condition uses the lower
626 * join's RHS and is not strict for that rel, we must preserve the
627 * ordering of the two OJs, so add lower OJ's full syntactic relset to
628 * min_lefthand. (We must use its full syntactic relset, not just its
629 * min_lefthand + min_righthand. This is because there might be other
630 * OJs below this one that this one can commute with, but we cannot
631 * commute with them if we don't with this one.)
633 * Note: I believe we have to insist on being strict for at least one
634 * rel in the lower OJ's min_righthand, not its whole syn_righthand.
636 if (bms_overlap(left_rels
, otherinfo
->syn_righthand
) &&
637 bms_overlap(clause_relids
, otherinfo
->syn_righthand
) &&
638 !bms_overlap(strict_relids
, otherinfo
->min_righthand
))
640 min_lefthand
= bms_add_members(min_lefthand
,
641 otherinfo
->syn_lefthand
);
642 min_lefthand
= bms_add_members(min_lefthand
,
643 otherinfo
->syn_righthand
);
647 * For a lower OJ in our RHS, if our join condition does not use the
648 * lower join's RHS and the lower OJ's join condition is strict, we
649 * can interchange the ordering of the two OJs; otherwise we must add
650 * lower OJ's full syntactic relset to min_righthand.
652 * Here, we have to consider that "our join condition" includes any
653 * clauses that syntactically appeared above the lower OJ and below
654 * ours; those are equivalent to degenerate clauses in our OJ and must
655 * be treated as such. Such clauses obviously can't reference our
656 * LHS, and they must be non-strict for the lower OJ's RHS (else
657 * reduce_outer_joins would have reduced the lower OJ to a plain
658 * join). Hence the other ways in which we handle clauses within our
659 * join condition are not affected by them. The net effect is
660 * therefore sufficiently represented by the delay_upper_joins flag
661 * saved for us by check_outerjoin_delay.
663 if (bms_overlap(right_rels
, otherinfo
->syn_righthand
))
665 if (bms_overlap(clause_relids
, otherinfo
->syn_righthand
) ||
666 !otherinfo
->lhs_strict
|| otherinfo
->delay_upper_joins
)
668 min_righthand
= bms_add_members(min_righthand
,
669 otherinfo
->syn_lefthand
);
670 min_righthand
= bms_add_members(min_righthand
,
671 otherinfo
->syn_righthand
);
677 * If we found nothing to put in min_lefthand, punt and make it the full
678 * LHS, to avoid having an empty min_lefthand which will confuse later
679 * processing. (We don't try to be smart about such cases, just correct.)
680 * Likewise for min_righthand.
682 if (bms_is_empty(min_lefthand
))
683 min_lefthand
= bms_copy(left_rels
);
684 if (bms_is_empty(min_righthand
))
685 min_righthand
= bms_copy(right_rels
);
687 /* Now they'd better be nonempty */
688 Assert(!bms_is_empty(min_lefthand
));
689 Assert(!bms_is_empty(min_righthand
));
690 /* Shouldn't overlap either */
691 Assert(!bms_overlap(min_lefthand
, min_righthand
));
693 sjinfo
->min_lefthand
= min_lefthand
;
694 sjinfo
->min_righthand
= min_righthand
;
700 /*****************************************************************************
704 *****************************************************************************/
707 * distribute_qual_to_rels
708 * Add clause information to either the baserestrictinfo or joininfo list
709 * (depending on whether the clause is a join) of each base relation
710 * mentioned in the clause. A RestrictInfo node is created and added to
711 * the appropriate list for each rel. Alternatively, if the clause uses a
712 * mergejoinable operator and is not delayed by outer-join rules, enter
713 * the left- and right-side expressions into the query's list of
714 * EquivalenceClasses.
716 * 'clause': the qual clause to be distributed
717 * 'is_deduced': TRUE if the qual came from implied-equality deduction
718 * 'below_outer_join': TRUE if the qual is from a JOIN/ON that is below the
719 * nullable side of a higher-level outer join
720 * 'jointype': type of join the qual is from (JOIN_INNER for a WHERE clause)
721 * 'qualscope': set of baserels the qual's syntactic scope covers
722 * 'ojscope': NULL if not an outer-join qual, else the minimum set of baserels
723 * needed to form this join
724 * 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
725 * baserels appearing on the outer (nonnullable) side of the join
726 * (for FULL JOIN this includes both sides of the join, and must in fact
729 * 'qualscope' identifies what level of JOIN the qual came from syntactically.
730 * 'ojscope' is needed if we decide to force the qual up to the outer-join
731 * level, which will be ojscope not necessarily qualscope.
733 * At the time this is called, root->join_info_list must contain entries for
734 * all and only those special joins that are syntactically below this qual.
737 distribute_qual_to_rels(PlannerInfo
*root
, Node
*clause
,
739 bool below_outer_join
,
743 Relids outerjoin_nonnullable
)
747 bool outerjoin_delayed
;
748 bool pseudoconstant
= false;
749 bool maybe_equivalence
;
750 bool maybe_outer_join
;
751 RestrictInfo
*restrictinfo
;
754 * Retrieve all relids mentioned within the clause.
756 relids
= pull_varnos(clause
);
759 * Cross-check: clause should contain no relids not within its scope.
760 * Otherwise the parser messed up.
762 if (!bms_is_subset(relids
, qualscope
))
763 elog(ERROR
, "JOIN qualification cannot refer to other relations");
764 if (ojscope
&& !bms_is_subset(relids
, ojscope
))
765 elog(ERROR
, "JOIN qualification cannot refer to other relations");
768 * If the clause is variable-free, our normal heuristic for pushing it
769 * down to just the mentioned rels doesn't work, because there are none.
771 * If the clause is an outer-join clause, we must force it to the OJ's
772 * semantic level to preserve semantics.
774 * Otherwise, when the clause contains volatile functions, we force it to
775 * be evaluated at its original syntactic level. This preserves the
776 * expected semantics.
778 * When the clause contains no volatile functions either, it is actually a
779 * pseudoconstant clause that will not change value during any one
780 * execution of the plan, and hence can be used as a one-time qual in a
781 * gating Result plan node. We put such a clause into the regular
782 * RestrictInfo lists for the moment, but eventually createplan.c will
783 * pull it out and make a gating Result node immediately above whatever
784 * plan node the pseudoconstant clause is assigned to. It's usually best
785 * to put a gating node as high in the plan tree as possible. If we are
786 * not below an outer join, we can actually push the pseudoconstant qual
787 * all the way to the top of the tree. If we are below an outer join, we
788 * leave the qual at its original syntactic level (we could push it up to
789 * just below the outer join, but that seems more complex than it's
792 if (bms_is_empty(relids
))
796 /* clause is attached to outer join, eval it there */
797 relids
= bms_copy(ojscope
);
798 /* mustn't use as gating qual, so don't mark pseudoconstant */
802 /* eval at original syntactic level */
803 relids
= bms_copy(qualscope
);
804 if (!contain_volatile_functions(clause
))
806 /* mark as gating qual */
807 pseudoconstant
= true;
808 /* tell createplan.c to check for gating quals */
809 root
->hasPseudoConstantQuals
= true;
810 /* if not below outer join, push it to top of tree */
811 if (!below_outer_join
)
813 get_relids_in_jointree((Node
*) root
->parse
->jointree
,
820 * Check to see if clause application must be delayed by outer-join
823 * A word about is_pushed_down: we mark the qual as "pushed down" if
824 * it is (potentially) applicable at a level different from its original
825 * syntactic level. This flag is used to distinguish OUTER JOIN ON quals
826 * from other quals pushed down to the same joinrel. The rules are:
827 * WHERE quals and INNER JOIN quals: is_pushed_down = true.
828 * Non-degenerate OUTER JOIN quals: is_pushed_down = false.
829 * Degenerate OUTER JOIN quals: is_pushed_down = true.
830 * A "degenerate" OUTER JOIN qual is one that doesn't mention the
831 * non-nullable side, and hence can be pushed down into the nullable side
832 * without changing the join result. It is correct to treat it as a
833 * regular filter condition at the level where it is evaluated.
835 * Note: it is not immediately obvious that a simple boolean is enough
836 * for this: if for some reason we were to attach a degenerate qual to
837 * its original join level, it would need to be treated as an outer join
838 * qual there. However, this cannot happen, because all the rels the
839 * clause mentions must be in the outer join's min_righthand, therefore
840 * the join it needs must be formed before the outer join; and we always
841 * attach quals to the lowest level where they can be evaluated. But
842 * if we were ever to re-introduce a mechanism for delaying evaluation
843 * of "expensive" quals, this area would need work.
849 * If the qual came from implied-equality deduction, it should not be
850 * outerjoin-delayed, else deducer blew it. But we can't check this
851 * because the join_info_list may now contain OJs above where the qual
855 is_pushed_down
= true;
856 outerjoin_delayed
= false;
857 /* Don't feed it back for more deductions */
858 maybe_equivalence
= false;
859 maybe_outer_join
= false;
861 else if (bms_overlap(relids
, outerjoin_nonnullable
))
864 * The qual is attached to an outer join and mentions (some of the)
865 * rels on the nonnullable side, so it's not degenerate.
867 * We can't use such a clause to deduce equivalence (the left and
868 * right sides might be unequal above the join because one of them has
869 * gone to NULL) ... but we might be able to use it for more limited
870 * deductions, if it is mergejoinable. So consider adding it to the
871 * lists of set-aside outer-join clauses.
873 is_pushed_down
= false;
874 maybe_equivalence
= false;
875 maybe_outer_join
= true;
877 /* Check to see if must be delayed by lower outer join */
878 outerjoin_delayed
= check_outerjoin_delay(root
, &relids
, false);
881 * Now force the qual to be evaluated exactly at the level of joining
882 * corresponding to the outer join. We cannot let it get pushed down
883 * into the nonnullable side, since then we'd produce no output rows,
884 * rather than the intended single null-extended row, for any
885 * nonnullable-side rows failing the qual.
887 * (Do this step after calling check_outerjoin_delay, because that
892 Assert(!pseudoconstant
);
897 * Normal qual clause or degenerate outer-join clause. Either way, we
898 * can mark it as pushed-down.
900 is_pushed_down
= true;
902 /* Check to see if must be delayed by lower outer join */
903 outerjoin_delayed
= check_outerjoin_delay(root
, &relids
, true);
905 if (outerjoin_delayed
)
907 /* Should still be a subset of current scope ... */
908 Assert(bms_is_subset(relids
, qualscope
));
911 * Because application of the qual will be delayed by outer join,
912 * we mustn't assume its vars are equal everywhere.
914 maybe_equivalence
= false;
917 * It's possible that this is an IS NULL clause that's redundant
918 * with a lower antijoin; if so we can just discard it. We need
919 * not test in any of the other cases, because this will only
920 * be possible for pushed-down, delayed clauses.
922 if (check_redundant_nullability_qual(root
, clause
))
928 * Qual is not delayed by any lower outer-join restriction, so we
929 * can consider feeding it to the equivalence machinery. However,
930 * if it's itself within an outer-join clause, treat it as though
931 * it appeared below that outer join (note that we can only get
932 * here when the clause references only nullable-side rels).
934 maybe_equivalence
= true;
935 if (outerjoin_nonnullable
!= NULL
)
936 below_outer_join
= true;
940 * Since it doesn't mention the LHS, it's certainly not useful as a
941 * set-aside OJ clause, even if it's in an OJ.
943 maybe_outer_join
= false;
947 * Build the RestrictInfo node itself.
949 restrictinfo
= make_restrictinfo((Expr
*) clause
,
956 * If it's a join clause (either naturally, or because delayed by
957 * outer-join rules), add vars used in the clause to targetlists of their
958 * relations, so that they will be emitted by the plan nodes that scan
959 * those relations (else they won't be available at the join node!).
961 * Note: if the clause gets absorbed into an EquivalenceClass then this
962 * may be unnecessary, but for now we have to do it to cover the case
963 * where the EC becomes ec_broken and we end up reinserting the original
964 * clauses into the plan.
966 if (bms_membership(relids
) == BMS_MULTIPLE
)
968 List
*vars
= pull_var_clause(clause
, true);
970 add_vars_to_targetlist(root
, vars
, relids
);
975 * We check "mergejoinability" of every clause, not only join clauses,
976 * because we want to know about equivalences between vars of the same
977 * relation, or between vars and consts.
979 check_mergejoinable(restrictinfo
);
982 * If it is a true equivalence clause, send it to the EquivalenceClass
983 * machinery. We do *not* attach it directly to any restriction or join
984 * lists. The EC code will propagate it to the appropriate places later.
986 * If the clause has a mergejoinable operator and is not
987 * outerjoin-delayed, yet isn't an equivalence because it is an outer-join
988 * clause, the EC code may yet be able to do something with it. We add it
989 * to appropriate lists for further consideration later. Specifically:
991 * If it is a left or right outer-join qualification that relates the two
992 * sides of the outer join (no funny business like leftvar1 = leftvar2 +
993 * rightvar), we add it to root->left_join_clauses or
994 * root->right_join_clauses according to which side the nonnullable
995 * variable appears on.
997 * If it is a full outer-join qualification, we add it to
998 * root->full_join_clauses. (Ideally we'd discard cases that aren't
999 * leftvar = rightvar, as we do for left/right joins, but this routine
1000 * doesn't have the info needed to do that; and the current usage of the
1001 * full_join_clauses list doesn't require that, so it's not currently
1002 * worth complicating this routine's API to make it possible.)
1004 * If none of the above hold, pass it off to
1005 * distribute_restrictinfo_to_rels().
1007 if (restrictinfo
->mergeopfamilies
)
1009 if (maybe_equivalence
)
1011 if (process_equivalence(root
, restrictinfo
, below_outer_join
))
1013 /* EC rejected it, so pass to distribute_restrictinfo_to_rels */
1015 else if (maybe_outer_join
&& restrictinfo
->can_join
)
1017 if (bms_is_subset(restrictinfo
->left_relids
,
1018 outerjoin_nonnullable
) &&
1019 !bms_overlap(restrictinfo
->right_relids
,
1020 outerjoin_nonnullable
))
1022 /* we have outervar = innervar */
1023 root
->left_join_clauses
= lappend(root
->left_join_clauses
,
1027 if (bms_is_subset(restrictinfo
->right_relids
,
1028 outerjoin_nonnullable
) &&
1029 !bms_overlap(restrictinfo
->left_relids
,
1030 outerjoin_nonnullable
))
1032 /* we have innervar = outervar */
1033 root
->right_join_clauses
= lappend(root
->right_join_clauses
,
1037 if (jointype
== JOIN_FULL
)
1039 /* FULL JOIN (above tests cannot match in this case) */
1040 root
->full_join_clauses
= lappend(root
->full_join_clauses
,
1047 /* No EC special case applies, so push it into the clause lists */
1048 distribute_restrictinfo_to_rels(root
, restrictinfo
);
1052 * check_outerjoin_delay
1053 * Detect whether a qual referencing the given relids must be delayed
1054 * in application due to the presence of a lower outer join, and/or
1055 * may force extra delay of higher-level outer joins.
1057 * If the qual must be delayed, add relids to *relids_p to reflect the lowest
1058 * safe level for evaluating the qual, and return TRUE. Any extra delay for
1059 * higher-level joins is reflected by setting delay_upper_joins to TRUE in
1060 * SpecialJoinInfo structs.
1062 * For an is_pushed_down qual, we can evaluate the qual as soon as (1) we have
1063 * all the rels it mentions, and (2) we are at or above any outer joins that
1064 * can null any of these rels and are below the syntactic location of the
1065 * given qual. We must enforce (2) because pushing down such a clause below
1066 * the OJ might cause the OJ to emit null-extended rows that should not have
1067 * been formed, or that should have been rejected by the clause. (This is
1068 * only an issue for non-strict quals, since if we can prove a qual mentioning
1069 * only nullable rels is strict, we'd have reduced the outer join to an inner
1070 * join in reduce_outer_joins().)
1072 * To enforce (2), scan the join_info_list and merge the required-relid sets of
1073 * any such OJs into the clause's own reference list. At the time we are
1074 * called, the join_info_list contains only outer joins below this qual. We
1075 * have to repeat the scan until no new relids get added; this ensures that
1076 * the qual is suitably delayed regardless of the order in which OJs get
1077 * executed. As an example, if we have one OJ with LHS=A, RHS=B, and one with
1078 * LHS=B, RHS=C, it is implied that these can be done in either order; if the
1079 * B/C join is done first then the join to A can null C, so a qual actually
1080 * mentioning only C cannot be applied below the join to A.
1082 * For a non-pushed-down qual, this isn't going to determine where we place the
1083 * qual, but we need to determine outerjoin_delayed anyway for possible use
1084 * in reconsider_outer_join_clauses().
1086 * Lastly, a pushed-down qual that references the nullable side of any current
1087 * join_info_list member and has to be evaluated above that OJ (because its
1088 * required relids overlap the LHS too) causes that OJ's delay_upper_joins
1089 * flag to be set TRUE. This will prevent any higher-level OJs from
1090 * being interchanged with that OJ, which would result in not having any
1091 * correct place to evaluate the qual. (The case we care about here is a
1092 * sub-select WHERE clause within the RHS of some outer join. The WHERE
1093 * clause must effectively be treated as a degenerate clause of that outer
1094 * join's condition. Rather than trying to match such clauses with joins
1095 * directly, we set delay_upper_joins here, and when the upper outer join
1096 * is processed by make_outerjoininfo, it will refrain from allowing the
1097 * two OJs to commute.)
1100 check_outerjoin_delay(PlannerInfo
*root
, Relids
*relids_p
,
1101 bool is_pushed_down
)
1103 Relids relids
= *relids_p
;
1104 bool outerjoin_delayed
;
1107 outerjoin_delayed
= false;
1113 foreach(l
, root
->join_info_list
)
1115 SpecialJoinInfo
*sjinfo
= (SpecialJoinInfo
*) lfirst(l
);
1117 /* do we reference any nullable rels of this OJ? */
1118 if (bms_overlap(relids
, sjinfo
->min_righthand
) ||
1119 (sjinfo
->jointype
== JOIN_FULL
&&
1120 bms_overlap(relids
, sjinfo
->min_lefthand
)))
1122 /* yes, so set the result flag */
1123 outerjoin_delayed
= true;
1124 /* have we included all its rels in relids? */
1125 if (!bms_is_subset(sjinfo
->min_lefthand
, relids
) ||
1126 !bms_is_subset(sjinfo
->min_righthand
, relids
))
1128 /* no, so add them in */
1129 relids
= bms_add_members(relids
, sjinfo
->min_lefthand
);
1130 relids
= bms_add_members(relids
, sjinfo
->min_righthand
);
1131 /* we'll need another iteration */
1134 /* set delay_upper_joins if needed */
1135 if (is_pushed_down
&& sjinfo
->jointype
!= JOIN_FULL
&&
1136 bms_overlap(relids
, sjinfo
->min_lefthand
))
1137 sjinfo
->delay_upper_joins
= true;
1140 } while (found_some
);
1143 return outerjoin_delayed
;
1147 * check_redundant_nullability_qual
1148 * Check to see if the qual is an IS NULL qual that is redundant with
1149 * a lower JOIN_ANTI join.
1151 * We want to suppress redundant IS NULL quals, not so much to save cycles
1152 * as to avoid generating bogus selectivity estimates for them. So if
1153 * redundancy is detected here, distribute_qual_to_rels() just throws away
1157 check_redundant_nullability_qual(PlannerInfo
*root
, Node
*clause
)
1159 Var
*forced_null_var
;
1160 Index forced_null_rel
;
1163 /* Check for IS NULL, and identify the Var forced to NULL */
1164 forced_null_var
= find_forced_null_var(clause
);
1165 if (forced_null_var
== NULL
)
1167 forced_null_rel
= forced_null_var
->varno
;
1170 * If the Var comes from the nullable side of a lower antijoin, the
1171 * IS NULL condition is necessarily true.
1173 foreach(lc
, root
->join_info_list
)
1175 SpecialJoinInfo
*sjinfo
= (SpecialJoinInfo
*) lfirst(lc
);
1177 if (sjinfo
->jointype
== JOIN_ANTI
&&
1178 bms_is_member(forced_null_rel
, sjinfo
->syn_righthand
))
1186 * distribute_restrictinfo_to_rels
1187 * Push a completed RestrictInfo into the proper restriction or join
1190 * This is the last step of distribute_qual_to_rels() for ordinary qual
1191 * clauses. Clauses that are interesting for equivalence-class processing
1192 * are diverted to the EC machinery, but may ultimately get fed back here.
1195 distribute_restrictinfo_to_rels(PlannerInfo
*root
,
1196 RestrictInfo
*restrictinfo
)
1198 Relids relids
= restrictinfo
->required_relids
;
1201 switch (bms_membership(relids
))
1206 * There is only one relation participating in the clause, so it
1207 * is a restriction clause for that relation.
1209 rel
= find_base_rel(root
, bms_singleton_member(relids
));
1211 /* Add clause to rel's restriction list */
1212 rel
->baserestrictinfo
= lappend(rel
->baserestrictinfo
,
1218 * The clause is a join clause, since there is more than one rel
1223 * Check for hashjoinable operators. (We don't bother setting the
1224 * hashjoin info if we're not going to need it.)
1226 if (enable_hashjoin
)
1227 check_hashjoinable(restrictinfo
);
1230 * Add clause to the join lists of all the relevant relations.
1232 add_join_clause_to_rels(root
, restrictinfo
, relids
);
1237 * clause references no rels, and therefore we have no place to
1238 * attach it. Shouldn't get here if callers are working properly.
1240 elog(ERROR
, "cannot cope with variable-free clause");
1246 * process_implied_equality
1247 * Create a restrictinfo item that says "item1 op item2", and push it
1248 * into the appropriate lists. (In practice opno is always a btree
1249 * equality operator.)
1251 * "qualscope" is the nominal syntactic level to impute to the restrictinfo.
1252 * This must contain at least all the rels used in the expressions, but it
1253 * is used only to set the qual application level when both exprs are
1254 * variable-free. Otherwise the qual is applied at the lowest join level
1255 * that provides all its variables.
1257 * "both_const" indicates whether both items are known pseudo-constant;
1258 * in this case it is worth applying eval_const_expressions() in case we
1259 * can produce constant TRUE or constant FALSE. (Otherwise it's not,
1260 * because the expressions went through eval_const_expressions already.)
1262 * This is currently used only when an EquivalenceClass is found to
1263 * contain pseudoconstants. See path/pathkeys.c for more details.
1266 process_implied_equality(PlannerInfo
*root
,
1271 bool below_outer_join
,
1277 * Build the new clause. Copy to ensure it shares no substructure with
1278 * original (this is necessary in case there are subselects in there...)
1280 clause
= make_opclause(opno
,
1281 BOOLOID
, /* opresulttype */
1282 false, /* opretset */
1283 (Expr
*) copyObject(item1
),
1284 (Expr
*) copyObject(item2
));
1286 /* If both constant, try to reduce to a boolean constant. */
1289 clause
= (Expr
*) eval_const_expressions(root
, (Node
*) clause
);
1291 /* If we produced const TRUE, just drop the clause */
1292 if (clause
&& IsA(clause
, Const
))
1294 Const
*cclause
= (Const
*) clause
;
1296 Assert(cclause
->consttype
== BOOLOID
);
1297 if (!cclause
->constisnull
&& DatumGetBool(cclause
->constvalue
))
1302 /* Make a copy of qualscope to avoid problems if source EC changes */
1303 qualscope
= bms_copy(qualscope
);
1306 * Push the new clause into all the appropriate restrictinfo lists.
1308 distribute_qual_to_rels(root
, (Node
*) clause
,
1309 true, below_outer_join
, JOIN_INNER
,
1310 qualscope
, NULL
, NULL
);
1314 * build_implied_join_equality --- build a RestrictInfo for a derived equality
1316 * This overlaps the functionality of process_implied_equality(), but we
1317 * must return the RestrictInfo, not push it into the joininfo tree.
1320 build_implied_join_equality(Oid opno
,
1325 RestrictInfo
*restrictinfo
;
1329 * Build the new clause. Copy to ensure it shares no substructure with
1330 * original (this is necessary in case there are subselects in there...)
1332 clause
= make_opclause(opno
,
1333 BOOLOID
, /* opresulttype */
1334 false, /* opretset */
1335 (Expr
*) copyObject(item1
),
1336 (Expr
*) copyObject(item2
));
1338 /* Make a copy of qualscope to avoid problems if source EC changes */
1339 qualscope
= bms_copy(qualscope
);
1342 * Build the RestrictInfo node itself.
1344 restrictinfo
= make_restrictinfo(clause
,
1345 true, /* is_pushed_down */
1346 false, /* outerjoin_delayed */
1347 false, /* pseudoconstant */
1350 /* Set mergejoinability info always, and hashjoinability if enabled */
1351 check_mergejoinable(restrictinfo
);
1352 if (enable_hashjoin
)
1353 check_hashjoinable(restrictinfo
);
1355 return restrictinfo
;
1359 /*****************************************************************************
1361 * CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
1363 *****************************************************************************/
1366 * check_mergejoinable
1367 * If the restrictinfo's clause is mergejoinable, set the mergejoin
1368 * info fields in the restrictinfo.
1370 * Currently, we support mergejoin for binary opclauses where
1371 * the operator is a mergejoinable operator. The arguments can be
1372 * anything --- as long as there are no volatile functions in them.
1375 check_mergejoinable(RestrictInfo
*restrictinfo
)
1377 Expr
*clause
= restrictinfo
->clause
;
1380 if (restrictinfo
->pseudoconstant
)
1382 if (!is_opclause(clause
))
1384 if (list_length(((OpExpr
*) clause
)->args
) != 2)
1387 opno
= ((OpExpr
*) clause
)->opno
;
1389 if (op_mergejoinable(opno
) &&
1390 !contain_volatile_functions((Node
*) clause
))
1391 restrictinfo
->mergeopfamilies
= get_mergejoin_opfamilies(opno
);
1394 * Note: op_mergejoinable is just a hint; if we fail to find the operator
1395 * in any btree opfamilies, mergeopfamilies remains NIL and so the clause
1396 * is not treated as mergejoinable.
1401 * check_hashjoinable
1402 * If the restrictinfo's clause is hashjoinable, set the hashjoin
1403 * info fields in the restrictinfo.
1405 * Currently, we support hashjoin for binary opclauses where
1406 * the operator is a hashjoinable operator. The arguments can be
1407 * anything --- as long as there are no volatile functions in them.
1410 check_hashjoinable(RestrictInfo
*restrictinfo
)
1412 Expr
*clause
= restrictinfo
->clause
;
1415 if (restrictinfo
->pseudoconstant
)
1417 if (!is_opclause(clause
))
1419 if (list_length(((OpExpr
*) clause
)->args
) != 2)
1422 opno
= ((OpExpr
*) clause
)->opno
;
1424 if (op_hashjoinable(opno
) &&
1425 !contain_volatile_functions((Node
*) clause
))
1426 restrictinfo
->hashjoinoperator
= opno
;