]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/initsplan.c
3c08d0def91c89888a63b85fdfe60ec3848a2f5b
[postgresql] / src / backend / optimizer / plan / initsplan.c
1 /*-------------------------------------------------------------------------
2  *
3  * initsplan.c
4  *        Target list, qualification, joininfo initialization routines
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.141 2008/08/14 18:47:59 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
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/planmain.h"
25 #include "optimizer/prep.h"
26 #include "optimizer/restrictinfo.h"
27 #include "optimizer/var.h"
28 #include "parser/parse_expr.h"
29 #include "parser/parse_oper.h"
30 #include "utils/builtins.h"
31 #include "utils/lsyscache.h"
32 #include "utils/syscache.h"
33
34
35 /* These parameters are set by GUC */
36 int                     from_collapse_limit;
37 int                     join_collapse_limit;
38
39
40 static List *deconstruct_recurse(PlannerInfo *root, Node *jtnode,
41                                         bool below_outer_join,
42                                         Relids *qualscope, Relids *inner_join_rels);
43 static SpecialJoinInfo *make_outerjoininfo(PlannerInfo *root,
44                                    Relids left_rels, Relids right_rels,
45                                    Relids inner_join_rels,
46                                    JoinType jointype, List *clause);
47 static void distribute_qual_to_rels(PlannerInfo *root, Node *clause,
48                                                 bool is_deduced,
49                                                 bool below_outer_join,
50                                                 Relids qualscope,
51                                                 Relids ojscope,
52                                                 Relids outerjoin_nonnullable);
53 static void distribute_sublink_quals_to_rels(PlannerInfo *root,
54                                                                  FlattenedSubLink *fslink,
55                                                                  bool below_outer_join);
56 static bool check_outerjoin_delay(PlannerInfo *root, Relids *relids_p,
57                                           bool is_pushed_down);
58 static bool check_redundant_nullability_qual(PlannerInfo *root, Node *clause);
59 static void check_mergejoinable(RestrictInfo *restrictinfo);
60 static void check_hashjoinable(RestrictInfo *restrictinfo);
61
62
63 /*****************************************************************************
64  *
65  *       JOIN TREES
66  *
67  *****************************************************************************/
68
69 /*
70  * add_base_rels_to_query
71  *
72  *        Scan the query's jointree and create baserel RelOptInfos for all
73  *        the base relations (ie, table, subquery, and function RTEs)
74  *        appearing in the jointree.
75  *
76  * The initial invocation must pass root->parse->jointree as the value of
77  * jtnode.      Internally, the function recurses through the jointree.
78  *
79  * At the end of this process, there should be one baserel RelOptInfo for
80  * every non-join RTE that is used in the query.  Therefore, this routine
81  * is the only place that should call build_simple_rel with reloptkind
82  * RELOPT_BASEREL.      (Note: build_simple_rel recurses internally to build
83  * "other rel" RelOptInfos for the members of any appendrels we find here.)
84  */
85 void
86 add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
87 {
88         if (jtnode == NULL)
89                 return;
90         if (IsA(jtnode, RangeTblRef))
91         {
92                 int                     varno = ((RangeTblRef *) jtnode)->rtindex;
93
94                 (void) build_simple_rel(root, varno, RELOPT_BASEREL);
95         }
96         else if (IsA(jtnode, FromExpr))
97         {
98                 FromExpr   *f = (FromExpr *) jtnode;
99                 ListCell   *l;
100
101                 foreach(l, f->fromlist)
102                         add_base_rels_to_query(root, lfirst(l));
103         }
104         else if (IsA(jtnode, JoinExpr))
105         {
106                 JoinExpr   *j = (JoinExpr *) jtnode;
107
108                 add_base_rels_to_query(root, j->larg);
109                 add_base_rels_to_query(root, j->rarg);
110         }
111         else
112                 elog(ERROR, "unrecognized node type: %d",
113                          (int) nodeTag(jtnode));
114 }
115
116
117 /*****************************************************************************
118  *
119  *       TARGET LISTS
120  *
121  *****************************************************************************/
122
123 /*
124  * build_base_rel_tlists
125  *        Add targetlist entries for each var needed in the query's final tlist
126  *        to the appropriate base relations.
127  *
128  * We mark such vars as needed by "relation 0" to ensure that they will
129  * propagate up through all join plan steps.
130  */
131 void
132 build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
133 {
134         List       *tlist_vars = pull_var_clause((Node *) final_tlist, false);
135
136         if (tlist_vars != NIL)
137         {
138                 add_vars_to_targetlist(root, tlist_vars, bms_make_singleton(0));
139                 list_free(tlist_vars);
140         }
141 }
142
143 /*
144  * add_vars_to_targetlist
145  *        For each variable appearing in the list, add it to the owning
146  *        relation's targetlist if not already present, and mark the variable
147  *        as being needed for the indicated join (or for final output if
148  *        where_needed includes "relation 0").
149  */
150 void
151 add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
152 {
153         ListCell   *temp;
154
155         Assert(!bms_is_empty(where_needed));
156
157         foreach(temp, vars)
158         {
159                 Var                *var = (Var *) lfirst(temp);
160                 RelOptInfo *rel = find_base_rel(root, var->varno);
161                 int                     attrno = var->varattno;
162
163                 Assert(attrno >= rel->min_attr && attrno <= rel->max_attr);
164                 attrno -= rel->min_attr;
165                 if (bms_is_empty(rel->attr_needed[attrno]))
166                 {
167                         /* Variable not yet requested, so add to reltargetlist */
168                         /* XXX is copyObject necessary here? */
169                         rel->reltargetlist = lappend(rel->reltargetlist, copyObject(var));
170                 }
171                 rel->attr_needed[attrno] = bms_add_members(rel->attr_needed[attrno],
172                                                                                                    where_needed);
173         }
174 }
175
176
177 /*****************************************************************************
178  *
179  *        JOIN TREE PROCESSING
180  *
181  *****************************************************************************/
182
183 /*
184  * deconstruct_jointree
185  *        Recursively scan the query's join tree for WHERE and JOIN/ON qual
186  *        clauses, and add these to the appropriate restrictinfo and joininfo
187  *        lists belonging to base RelOptInfos.  Also, add SpecialJoinInfo nodes
188  *        to root->join_info_list for any outer joins appearing in the query tree.
189  *        Return a "joinlist" data structure showing the join order decisions
190  *        that need to be made by make_one_rel().
191  *
192  * The "joinlist" result is a list of items that are either RangeTblRef
193  * jointree nodes or sub-joinlists.  All the items at the same level of
194  * joinlist must be joined in an order to be determined by make_one_rel()
195  * (note that legal orders may be constrained by SpecialJoinInfo nodes).
196  * A sub-joinlist represents a subproblem to be planned separately. Currently
197  * sub-joinlists arise only from FULL OUTER JOIN or when collapsing of
198  * subproblems is stopped by join_collapse_limit or from_collapse_limit.
199  *
200  * NOTE: when dealing with inner joins, it is appropriate to let a qual clause
201  * be evaluated at the lowest level where all the variables it mentions are
202  * available.  However, we cannot push a qual down into the nullable side(s)
203  * of an outer join since the qual might eliminate matching rows and cause a
204  * NULL row to be incorrectly emitted by the join.      Therefore, we artificially
205  * OR the minimum-relids of such an outer join into the required_relids of
206  * clauses appearing above it.  This forces those clauses to be delayed until
207  * application of the outer join (or maybe even higher in the join tree).
208  */
209 List *
210 deconstruct_jointree(PlannerInfo *root)
211 {
212         Relids          qualscope;
213         Relids          inner_join_rels;
214
215         /* Start recursion at top of jointree */
216         Assert(root->parse->jointree != NULL &&
217                    IsA(root->parse->jointree, FromExpr));
218
219         return deconstruct_recurse(root, (Node *) root->parse->jointree, false,
220                                                            &qualscope, &inner_join_rels);
221 }
222
223 /*
224  * deconstruct_recurse
225  *        One recursion level of deconstruct_jointree processing.
226  *
227  * Inputs:
228  *      jtnode is the jointree node to examine
229  *      below_outer_join is TRUE if this node is within the nullable side of a
230  *              higher-level outer join
231  * Outputs:
232  *      *qualscope gets the set of base Relids syntactically included in this
233  *              jointree node (do not modify or free this, as it may also be pointed
234  *              to by RestrictInfo and SpecialJoinInfo nodes)
235  *      *inner_join_rels gets the set of base Relids syntactically included in
236  *              inner joins appearing at or below this jointree node (do not modify
237  *              or free this, either)
238  *      Return value is the appropriate joinlist for this jointree node
239  *
240  * In addition, entries will be added to root->join_info_list for outer joins.
241  */
242 static List *
243 deconstruct_recurse(PlannerInfo *root, Node *jtnode, bool below_outer_join,
244                                         Relids *qualscope, Relids *inner_join_rels)
245 {
246         List       *joinlist;
247
248         if (jtnode == NULL)
249         {
250                 *qualscope = NULL;
251                 *inner_join_rels = NULL;
252                 return NIL;
253         }
254         if (IsA(jtnode, RangeTblRef))
255         {
256                 int                     varno = ((RangeTblRef *) jtnode)->rtindex;
257
258                 /* No quals to deal with, just return correct result */
259                 *qualscope = bms_make_singleton(varno);
260                 /* A single baserel does not create an inner join */
261                 *inner_join_rels = NULL;
262                 joinlist = list_make1(jtnode);
263         }
264         else if (IsA(jtnode, FromExpr))
265         {
266                 FromExpr   *f = (FromExpr *) jtnode;
267                 int                     remaining;
268                 ListCell   *l;
269
270                 /*
271                  * First, recurse to handle child joins.  We collapse subproblems into
272                  * a single joinlist whenever the resulting joinlist wouldn't exceed
273                  * from_collapse_limit members.  Also, always collapse one-element
274                  * subproblems, since that won't lengthen the joinlist anyway.
275                  */
276                 *qualscope = NULL;
277                 *inner_join_rels = NULL;
278                 joinlist = NIL;
279                 remaining = list_length(f->fromlist);
280                 foreach(l, f->fromlist)
281                 {
282                         Relids          sub_qualscope;
283                         List       *sub_joinlist;
284                         int                     sub_members;
285
286                         sub_joinlist = deconstruct_recurse(root, lfirst(l),
287                                                                                            below_outer_join,
288                                                                                            &sub_qualscope,
289                                                                                            inner_join_rels);
290                         *qualscope = bms_add_members(*qualscope, sub_qualscope);
291                         sub_members = list_length(sub_joinlist);
292                         remaining--;
293                         if (sub_members <= 1 ||
294                                 list_length(joinlist) + sub_members + remaining <= from_collapse_limit)
295                                 joinlist = list_concat(joinlist, sub_joinlist);
296                         else
297                                 joinlist = lappend(joinlist, sub_joinlist);
298                 }
299
300                 /*
301                  * A FROM with more than one list element is an inner join subsuming
302                  * all below it, so we should report inner_join_rels = qualscope. If
303                  * there was exactly one element, we should (and already did) report
304                  * whatever its inner_join_rels were.  If there were no elements (is
305                  * that possible?) the initialization before the loop fixed it.
306                  */
307                 if (list_length(f->fromlist) > 1)
308                         *inner_join_rels = *qualscope;
309
310                 /*
311                  * Now process the top-level quals.
312                  */
313                 foreach(l, (List *) f->quals)
314                 {
315                         Node   *qual = (Node *) lfirst(l);
316
317                         /* FlattenedSubLink wrappers need special processing */
318                         if (qual && IsA(qual, FlattenedSubLink))
319                                 distribute_sublink_quals_to_rels(root,
320                                                                                                  (FlattenedSubLink *) qual,
321                                                                                                  below_outer_join);
322                         else
323                                 distribute_qual_to_rels(root, qual,
324                                                                                 false, below_outer_join,
325                                                                                 *qualscope, NULL, NULL);
326                 }
327         }
328         else if (IsA(jtnode, JoinExpr))
329         {
330                 JoinExpr   *j = (JoinExpr *) jtnode;
331                 Relids          leftids,
332                                         rightids,
333                                         left_inners,
334                                         right_inners,
335                                         nonnullable_rels,
336                                         ojscope;
337                 List       *leftjoinlist,
338                                    *rightjoinlist;
339                 SpecialJoinInfo *sjinfo;
340                 ListCell   *l;
341
342                 /*
343                  * Order of operations here is subtle and critical.  First we recurse
344                  * to handle sub-JOINs.  Their join quals will be placed without
345                  * regard for whether this level is an outer join, which is correct.
346                  * Then we place our own join quals, which are restricted by lower
347                  * outer joins in any case, and are forced to this level if this is an
348                  * outer join and they mention the outer side.  Finally, if this is an
349                  * outer join, we create a join_info_list entry for the join.  This
350                  * will prevent quals above us in the join tree that use those rels
351                  * from being pushed down below this level.  (It's okay for upper
352                  * quals to be pushed down to the outer side, however.)
353                  */
354                 switch (j->jointype)
355                 {
356                         case JOIN_INNER:
357                                 leftjoinlist = deconstruct_recurse(root, j->larg,
358                                                                                                    below_outer_join,
359                                                                                                    &leftids, &left_inners);
360                                 rightjoinlist = deconstruct_recurse(root, j->rarg,
361                                                                                                         below_outer_join,
362                                                                                                         &rightids, &right_inners);
363                                 *qualscope = bms_union(leftids, rightids);
364                                 *inner_join_rels = *qualscope;
365                                 /* Inner join adds no restrictions for quals */
366                                 nonnullable_rels = NULL;
367                                 break;
368                         case JOIN_LEFT:
369                         case JOIN_ANTI:
370                                 leftjoinlist = deconstruct_recurse(root, j->larg,
371                                                                                                    below_outer_join,
372                                                                                                    &leftids, &left_inners);
373                                 rightjoinlist = deconstruct_recurse(root, j->rarg,
374                                                                                                         true,
375                                                                                                         &rightids, &right_inners);
376                                 *qualscope = bms_union(leftids, rightids);
377                                 *inner_join_rels = bms_union(left_inners, right_inners);
378                                 nonnullable_rels = leftids;
379                                 break;
380                         case JOIN_FULL:
381                                 leftjoinlist = deconstruct_recurse(root, j->larg,
382                                                                                                    true,
383                                                                                                    &leftids, &left_inners);
384                                 rightjoinlist = deconstruct_recurse(root, j->rarg,
385                                                                                                         true,
386                                                                                                         &rightids, &right_inners);
387                                 *qualscope = bms_union(leftids, rightids);
388                                 *inner_join_rels = bms_union(left_inners, right_inners);
389                                 /* each side is both outer and inner */
390                                 nonnullable_rels = *qualscope;
391                                 break;
392                         default:
393                                 /* JOIN_RIGHT was eliminated during reduce_outer_joins() */
394                                 elog(ERROR, "unrecognized join type: %d",
395                                          (int) j->jointype);
396                                 nonnullable_rels = NULL;                /* keep compiler quiet */
397                                 leftjoinlist = rightjoinlist = NIL;
398                                 break;
399                 }
400
401                 /*
402                  * For an OJ, form the SpecialJoinInfo now, because we need the OJ's
403                  * semantic scope (ojscope) to pass to distribute_qual_to_rels.  But
404                  * we mustn't add it to join_info_list just yet, because we don't want
405                  * distribute_qual_to_rels to think it is an outer join below us.
406                  */
407                 if (j->jointype != JOIN_INNER)
408                 {
409                         sjinfo = make_outerjoininfo(root,
410                                                                                 leftids, rightids,
411                                                                                 *inner_join_rels,
412                                                                                 j->jointype,
413                                                                                 (List *) j->quals);
414                         ojscope = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
415                 }
416                 else
417                 {
418                         sjinfo = NULL;
419                         ojscope = NULL;
420                 }
421
422                 /* Process the qual clauses */
423                 foreach(l, (List *) j->quals)
424                 {
425                         Node   *qual = (Node *) lfirst(l);
426
427                         /* FlattenedSubLink wrappers need special processing */
428                         if (qual && IsA(qual, FlattenedSubLink))
429                                 distribute_sublink_quals_to_rels(root,
430                                                                                                  (FlattenedSubLink *) qual,
431                                                                                                  below_outer_join);
432                         else
433                                 distribute_qual_to_rels(root, qual,
434                                                                                 false, below_outer_join,
435                                                                                 *qualscope,
436                                                                                 ojscope, nonnullable_rels);
437                 }
438
439                 /* Now we can add the SpecialJoinInfo to join_info_list */
440                 if (sjinfo)
441                         root->join_info_list = lappend(root->join_info_list, sjinfo);
442
443                 /*
444                  * Finally, compute the output joinlist.  We fold subproblems together
445                  * except at a FULL JOIN or where join_collapse_limit would be
446                  * exceeded.
447                  */
448                 if (j->jointype == JOIN_FULL)
449                 {
450                         /* force the join order exactly at this node */
451                         joinlist = list_make1(list_make2(leftjoinlist, rightjoinlist));
452                 }
453                 else if (list_length(leftjoinlist) + list_length(rightjoinlist) <=
454                                  join_collapse_limit)
455                 {
456                         /* OK to combine subproblems */
457                         joinlist = list_concat(leftjoinlist, rightjoinlist);
458                 }
459                 else
460                 {
461                         /* can't combine, but needn't force join order above here */
462                         Node       *leftpart,
463                                            *rightpart;
464
465                         /* avoid creating useless 1-element sublists */
466                         if (list_length(leftjoinlist) == 1)
467                                 leftpart = (Node *) linitial(leftjoinlist);
468                         else
469                                 leftpart = (Node *) leftjoinlist;
470                         if (list_length(rightjoinlist) == 1)
471                                 rightpart = (Node *) linitial(rightjoinlist);
472                         else
473                                 rightpart = (Node *) rightjoinlist;
474                         joinlist = list_make2(leftpart, rightpart);
475                 }
476         }
477         else
478         {
479                 elog(ERROR, "unrecognized node type: %d",
480                          (int) nodeTag(jtnode));
481                 joinlist = NIL;                 /* keep compiler quiet */
482         }
483         return joinlist;
484 }
485
486 /*
487  * make_outerjoininfo
488  *        Build a SpecialJoinInfo for the current outer join
489  *
490  * Inputs:
491  *      left_rels: the base Relids syntactically on outer side of join
492  *      right_rels: the base Relids syntactically on inner side of join
493  *      inner_join_rels: base Relids participating in inner joins below this one
494  *      jointype: what it says (must always be LEFT, FULL, SEMI, or ANTI)
495  *      clause: the outer join's join condition (in implicit-AND format)
496  *
497  * The node should eventually be appended to root->join_info_list, but we
498  * do not do that here.
499  *
500  * Note: we assume that this function is invoked bottom-up, so that
501  * root->join_info_list already contains entries for all outer joins that are
502  * syntactically below this one.
503  */
504 static SpecialJoinInfo *
505 make_outerjoininfo(PlannerInfo *root,
506                                    Relids left_rels, Relids right_rels,
507                                    Relids inner_join_rels,
508                                    JoinType jointype, List *clause)
509 {
510         SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
511         Relids          clause_relids;
512         Relids          strict_relids;
513         Relids          min_lefthand;
514         Relids          min_righthand;
515         ListCell   *l;
516
517         /*
518          * We should not see RIGHT JOIN here because left/right were switched
519          * earlier
520          */
521         Assert(jointype != JOIN_INNER);
522         Assert(jointype != JOIN_RIGHT);
523
524         /*
525          * Presently the executor cannot support FOR UPDATE/SHARE marking of rels
526          * appearing on the nullable side of an outer join. (It's somewhat unclear
527          * what that would mean, anyway: what should we mark when a result row is
528          * generated from no element of the nullable relation?)  So, complain if
529          * any nullable rel is FOR UPDATE/SHARE.
530          *
531          * You might be wondering why this test isn't made far upstream in the
532          * parser.      It's because the parser hasn't got enough info --- consider
533          * FOR UPDATE applied to a view.  Only after rewriting and flattening do
534          * we know whether the view contains an outer join.
535          */
536         foreach(l, root->parse->rowMarks)
537         {
538                 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
539
540                 if (bms_is_member(rc->rti, right_rels) ||
541                         (jointype == JOIN_FULL && bms_is_member(rc->rti, left_rels)))
542                         ereport(ERROR,
543                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
544                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to the nullable side of an outer join")));
545         }
546
547         sjinfo->syn_lefthand = left_rels;
548         sjinfo->syn_righthand = right_rels;
549         sjinfo->jointype = jointype;
550         /* this always starts out false */
551         sjinfo->delay_upper_joins = false;
552         sjinfo->join_quals = clause;
553
554         /* If it's a full join, no need to be very smart */
555         if (jointype == JOIN_FULL)
556         {
557                 sjinfo->min_lefthand = bms_copy(left_rels);
558                 sjinfo->min_righthand = bms_copy(right_rels);
559                 sjinfo->lhs_strict = false;             /* don't care about this */
560                 return sjinfo;
561         }
562
563         /*
564          * Retrieve all relids mentioned within the join clause.
565          */
566         clause_relids = pull_varnos((Node *) clause);
567
568         /*
569          * For which relids is the clause strict, ie, it cannot succeed if the
570          * rel's columns are all NULL?
571          */
572         strict_relids = find_nonnullable_rels((Node *) clause);
573
574         /* Remember whether the clause is strict for any LHS relations */
575         sjinfo->lhs_strict = bms_overlap(strict_relids, left_rels);
576
577         /*
578          * Required LHS always includes the LHS rels mentioned in the clause. We
579          * may have to add more rels based on lower outer joins; see below.
580          */
581         min_lefthand = bms_intersect(clause_relids, left_rels);
582
583         /*
584          * Similarly for required RHS.  But here, we must also include any lower
585          * inner joins, to ensure we don't try to commute with any of them.
586          */
587         min_righthand = bms_int_members(bms_union(clause_relids, inner_join_rels),
588                                                                         right_rels);
589
590         foreach(l, root->join_info_list)
591         {
592                 SpecialJoinInfo *otherinfo = (SpecialJoinInfo *) lfirst(l);
593
594                 /* ignore full joins --- other mechanisms preserve their ordering */
595                 if (otherinfo->jointype == JOIN_FULL)
596                         continue;
597
598                 /*
599                  * For a lower OJ in our LHS, if our join condition uses the lower
600                  * join's RHS and is not strict for that rel, we must preserve the
601                  * ordering of the two OJs, so add lower OJ's full syntactic relset to
602                  * min_lefthand.  (We must use its full syntactic relset, not just its
603                  * min_lefthand + min_righthand.  This is because there might be other
604                  * OJs below this one that this one can commute with, but we cannot
605                  * commute with them if we don't with this one.)
606                  *
607                  * Note: I believe we have to insist on being strict for at least one
608                  * rel in the lower OJ's min_righthand, not its whole syn_righthand.
609                  */
610                 if (bms_overlap(left_rels, otherinfo->syn_righthand) &&
611                         bms_overlap(clause_relids, otherinfo->syn_righthand) &&
612                         !bms_overlap(strict_relids, otherinfo->min_righthand))
613                 {
614                         min_lefthand = bms_add_members(min_lefthand,
615                                                                                    otherinfo->syn_lefthand);
616                         min_lefthand = bms_add_members(min_lefthand,
617                                                                                    otherinfo->syn_righthand);
618                 }
619
620                 /*
621                  * For a lower OJ in our RHS, if our join condition does not use the
622                  * lower join's RHS and the lower OJ's join condition is strict, we
623                  * can interchange the ordering of the two OJs; otherwise we must add
624                  * lower OJ's full syntactic relset to min_righthand.
625                  *
626                  * Here, we have to consider that "our join condition" includes any
627                  * clauses that syntactically appeared above the lower OJ and below
628                  * ours; those are equivalent to degenerate clauses in our OJ and must
629                  * be treated as such.  Such clauses obviously can't reference our
630                  * LHS, and they must be non-strict for the lower OJ's RHS (else
631                  * reduce_outer_joins would have reduced the lower OJ to a plain
632                  * join).  Hence the other ways in which we handle clauses within our
633                  * join condition are not affected by them.  The net effect is
634                  * therefore sufficiently represented by the delay_upper_joins flag
635                  * saved for us by check_outerjoin_delay.
636                  */
637                 if (bms_overlap(right_rels, otherinfo->syn_righthand))
638                 {
639                         if (bms_overlap(clause_relids, otherinfo->syn_righthand) ||
640                                 !otherinfo->lhs_strict || otherinfo->delay_upper_joins)
641                         {
642                                 min_righthand = bms_add_members(min_righthand,
643                                                                                                 otherinfo->syn_lefthand);
644                                 min_righthand = bms_add_members(min_righthand,
645                                                                                                 otherinfo->syn_righthand);
646                         }
647                 }
648         }
649
650         /*
651          * If we found nothing to put in min_lefthand, punt and make it the full
652          * LHS, to avoid having an empty min_lefthand which will confuse later
653          * processing. (We don't try to be smart about such cases, just correct.)
654          * Likewise for min_righthand.
655          */
656         if (bms_is_empty(min_lefthand))
657                 min_lefthand = bms_copy(left_rels);
658         if (bms_is_empty(min_righthand))
659                 min_righthand = bms_copy(right_rels);
660
661         /* Now they'd better be nonempty */
662         Assert(!bms_is_empty(min_lefthand));
663         Assert(!bms_is_empty(min_righthand));
664         /* Shouldn't overlap either */
665         Assert(!bms_overlap(min_lefthand, min_righthand));
666
667         sjinfo->min_lefthand = min_lefthand;
668         sjinfo->min_righthand = min_righthand;
669
670         return sjinfo;
671 }
672
673
674 /*****************************************************************************
675  *
676  *        QUALIFICATIONS
677  *
678  *****************************************************************************/
679
680 /*
681  * distribute_qual_to_rels
682  *        Add clause information to either the baserestrictinfo or joininfo list
683  *        (depending on whether the clause is a join) of each base relation
684  *        mentioned in the clause.      A RestrictInfo node is created and added to
685  *        the appropriate list for each rel.  Alternatively, if the clause uses a
686  *        mergejoinable operator and is not delayed by outer-join rules, enter
687  *        the left- and right-side expressions into the query's list of
688  *        EquivalenceClasses.
689  *
690  * 'clause': the qual clause to be distributed
691  * 'is_deduced': TRUE if the qual came from implied-equality deduction
692  * 'below_outer_join': TRUE if the qual is from a JOIN/ON that is below the
693  *              nullable side of a higher-level outer join
694  * 'qualscope': set of baserels the qual's syntactic scope covers
695  * 'ojscope': NULL if not an outer-join qual, else the minimum set of baserels
696  *              needed to form this join
697  * 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
698  *              baserels appearing on the outer (nonnullable) side of the join
699  *              (for FULL JOIN this includes both sides of the join, and must in fact
700  *              equal qualscope)
701  *
702  * 'qualscope' identifies what level of JOIN the qual came from syntactically.
703  * 'ojscope' is needed if we decide to force the qual up to the outer-join
704  * level, which will be ojscope not necessarily qualscope.
705  */
706 static void
707 distribute_qual_to_rels(PlannerInfo *root, Node *clause,
708                                                 bool is_deduced,
709                                                 bool below_outer_join,
710                                                 Relids qualscope,
711                                                 Relids ojscope,
712                                                 Relids outerjoin_nonnullable)
713 {
714         Relids          relids;
715         bool            is_pushed_down;
716         bool            outerjoin_delayed;
717         bool            pseudoconstant = false;
718         bool            maybe_equivalence;
719         bool            maybe_outer_join;
720         RestrictInfo *restrictinfo;
721
722         /*
723          * Retrieve all relids mentioned within the clause.
724          */
725         relids = pull_varnos(clause);
726
727         /*
728          * Cross-check: clause should contain no relids not within its scope.
729          * Otherwise the parser messed up.
730          */
731         if (!bms_is_subset(relids, qualscope))
732                 elog(ERROR, "JOIN qualification cannot refer to other relations");
733         if (ojscope && !bms_is_subset(relids, ojscope))
734                 elog(ERROR, "JOIN qualification cannot refer to other relations");
735
736         /*
737          * If the clause is variable-free, our normal heuristic for pushing it
738          * down to just the mentioned rels doesn't work, because there are none.
739          *
740          * If the clause is an outer-join clause, we must force it to the OJ's
741          * semantic level to preserve semantics.
742          *
743          * Otherwise, when the clause contains volatile functions, we force it to
744          * be evaluated at its original syntactic level.  This preserves the
745          * expected semantics.
746          *
747          * When the clause contains no volatile functions either, it is actually a
748          * pseudoconstant clause that will not change value during any one
749          * execution of the plan, and hence can be used as a one-time qual in a
750          * gating Result plan node.  We put such a clause into the regular
751          * RestrictInfo lists for the moment, but eventually createplan.c will
752          * pull it out and make a gating Result node immediately above whatever
753          * plan node the pseudoconstant clause is assigned to.  It's usually best
754          * to put a gating node as high in the plan tree as possible. If we are
755          * not below an outer join, we can actually push the pseudoconstant qual
756          * all the way to the top of the tree.  If we are below an outer join, we
757          * leave the qual at its original syntactic level (we could push it up to
758          * just below the outer join, but that seems more complex than it's
759          * worth).
760          */
761         if (bms_is_empty(relids))
762         {
763                 if (ojscope)
764                 {
765                         /* clause is attached to outer join, eval it there */
766                         relids = bms_copy(ojscope);
767                         /* mustn't use as gating qual, so don't mark pseudoconstant */
768                 }
769                 else
770                 {
771                         /* eval at original syntactic level */
772                         relids = bms_copy(qualscope);
773                         if (!contain_volatile_functions(clause))
774                         {
775                                 /* mark as gating qual */
776                                 pseudoconstant = true;
777                                 /* tell createplan.c to check for gating quals */
778                                 root->hasPseudoConstantQuals = true;
779                                 /* if not below outer join, push it to top of tree */
780                                 if (!below_outer_join)
781                                         relids = get_relids_in_jointree((Node *) root->parse->jointree);
782                         }
783                 }
784         }
785
786         /*----------
787          * Check to see if clause application must be delayed by outer-join
788          * considerations.
789          *
790          * A word about is_pushed_down: we mark the qual as "pushed down" if
791          * it is (potentially) applicable at a level different from its original
792          * syntactic level.  This flag is used to distinguish OUTER JOIN ON quals
793          * from other quals pushed down to the same joinrel.  The rules are:
794          *              WHERE quals and INNER JOIN quals: is_pushed_down = true.
795          *              Non-degenerate OUTER JOIN quals: is_pushed_down = false.
796          *              Degenerate OUTER JOIN quals: is_pushed_down = true.
797          * A "degenerate" OUTER JOIN qual is one that doesn't mention the
798          * non-nullable side, and hence can be pushed down into the nullable side
799          * without changing the join result.  It is correct to treat it as a
800          * regular filter condition at the level where it is evaluated.
801          *
802          * Note: it is not immediately obvious that a simple boolean is enough
803          * for this: if for some reason we were to attach a degenerate qual to
804          * its original join level, it would need to be treated as an outer join
805          * qual there.  However, this cannot happen, because all the rels the
806          * clause mentions must be in the outer join's min_righthand, therefore
807          * the join it needs must be formed before the outer join; and we always
808          * attach quals to the lowest level where they can be evaluated.  But
809          * if we were ever to re-introduce a mechanism for delaying evaluation
810          * of "expensive" quals, this area would need work.
811          *----------
812          */
813         if (is_deduced)
814         {
815                 /*
816                  * If the qual came from implied-equality deduction, it should not be
817                  * outerjoin-delayed, else deducer blew it.  But we can't check this
818                  * because the join_info_list may now contain OJs above where the qual
819                  * belongs.
820                  */
821                 Assert(!ojscope);
822                 is_pushed_down = true;
823                 outerjoin_delayed = false;
824                 /* Don't feed it back for more deductions */
825                 maybe_equivalence = false;
826                 maybe_outer_join = false;
827         }
828         else if (bms_overlap(relids, outerjoin_nonnullable))
829         {
830                 /*
831                  * The qual is attached to an outer join and mentions (some of the)
832                  * rels on the nonnullable side, so it's not degenerate.
833                  *
834                  * We can't use such a clause to deduce equivalence (the left and
835                  * right sides might be unequal above the join because one of them has
836                  * gone to NULL) ... but we might be able to use it for more limited
837                  * deductions, if it is mergejoinable.  So consider adding it to the
838                  * lists of set-aside outer-join clauses.
839                  */
840                 is_pushed_down = false;
841                 maybe_equivalence = false;
842                 maybe_outer_join = true;
843
844                 /* Check to see if must be delayed by lower outer join */
845                 outerjoin_delayed = check_outerjoin_delay(root, &relids, false);
846
847                 /*
848                  * Now force the qual to be evaluated exactly at the level of joining
849                  * corresponding to the outer join.  We cannot let it get pushed down
850                  * into the nonnullable side, since then we'd produce no output rows,
851                  * rather than the intended single null-extended row, for any
852                  * nonnullable-side rows failing the qual.
853                  *
854                  * (Do this step after calling check_outerjoin_delay, because that
855                  * trashes relids.)
856                  */
857                 Assert(ojscope);
858                 relids = ojscope;
859                 Assert(!pseudoconstant);
860         }
861         else
862         {
863                 /*
864                  * Normal qual clause or degenerate outer-join clause.  Either way, we
865                  * can mark it as pushed-down.
866                  */
867                 is_pushed_down = true;
868
869                 /* Check to see if must be delayed by lower outer join */
870                 outerjoin_delayed = check_outerjoin_delay(root, &relids, true);
871
872                 if (outerjoin_delayed)
873                 {
874                         /* Should still be a subset of current scope ... */
875                         Assert(bms_is_subset(relids, qualscope));
876
877                         /*
878                          * Because application of the qual will be delayed by outer join,
879                          * we mustn't assume its vars are equal everywhere.
880                          */
881                         maybe_equivalence = false;
882
883                         /*
884                          * It's possible that this is an IS NULL clause that's redundant
885                          * with a lower antijoin; if so we can just discard it.  We need
886                          * not test in any of the other cases, because this will only
887                          * be possible for pushed-down, delayed clauses.
888                          */
889                         if (check_redundant_nullability_qual(root, clause))
890                                 return;
891                 }
892                 else
893                 {
894                         /*
895                          * Qual is not delayed by any lower outer-join restriction, so we
896                          * can consider feeding it to the equivalence machinery. However,
897                          * if it's itself within an outer-join clause, treat it as though
898                          * it appeared below that outer join (note that we can only get
899                          * here when the clause references only nullable-side rels).
900                          */
901                         maybe_equivalence = true;
902                         if (outerjoin_nonnullable != NULL)
903                                 below_outer_join = true;
904                 }
905
906                 /*
907                  * Since it doesn't mention the LHS, it's certainly not useful as a
908                  * set-aside OJ clause, even if it's in an OJ.
909                  */
910                 maybe_outer_join = false;
911         }
912
913         /*
914          * Build the RestrictInfo node itself.
915          */
916         restrictinfo = make_restrictinfo((Expr *) clause,
917                                                                          is_pushed_down,
918                                                                          outerjoin_delayed,
919                                                                          pseudoconstant,
920                                                                          relids);
921
922         /*
923          * If it's a join clause (either naturally, or because delayed by
924          * outer-join rules), add vars used in the clause to targetlists of their
925          * relations, so that they will be emitted by the plan nodes that scan
926          * those relations (else they won't be available at the join node!).
927          *
928          * Note: if the clause gets absorbed into an EquivalenceClass then this
929          * may be unnecessary, but for now we have to do it to cover the case
930          * where the EC becomes ec_broken and we end up reinserting the original
931          * clauses into the plan.
932          */
933         if (bms_membership(relids) == BMS_MULTIPLE)
934         {
935                 List       *vars = pull_var_clause(clause, false);
936
937                 add_vars_to_targetlist(root, vars, relids);
938                 list_free(vars);
939         }
940
941         /*
942          * We check "mergejoinability" of every clause, not only join clauses,
943          * because we want to know about equivalences between vars of the same
944          * relation, or between vars and consts.
945          */
946         check_mergejoinable(restrictinfo);
947
948         /*
949          * If it is a true equivalence clause, send it to the EquivalenceClass
950          * machinery.  We do *not* attach it directly to any restriction or join
951          * lists.  The EC code will propagate it to the appropriate places later.
952          *
953          * If the clause has a mergejoinable operator and is not
954          * outerjoin-delayed, yet isn't an equivalence because it is an outer-join
955          * clause, the EC code may yet be able to do something with it.  We add it
956          * to appropriate lists for further consideration later.  Specifically:
957          *
958          * If it is a left or right outer-join qualification that relates the two
959          * sides of the outer join (no funny business like leftvar1 = leftvar2 +
960          * rightvar), we add it to root->left_join_clauses or
961          * root->right_join_clauses according to which side the nonnullable
962          * variable appears on.
963          *
964          * If it is a full outer-join qualification, we add it to
965          * root->full_join_clauses.  (Ideally we'd discard cases that aren't
966          * leftvar = rightvar, as we do for left/right joins, but this routine
967          * doesn't have the info needed to do that; and the current usage of the
968          * full_join_clauses list doesn't require that, so it's not currently
969          * worth complicating this routine's API to make it possible.)
970          *
971          * If none of the above hold, pass it off to
972          * distribute_restrictinfo_to_rels().
973          */
974         if (restrictinfo->mergeopfamilies)
975         {
976                 if (maybe_equivalence)
977                 {
978                         if (process_equivalence(root, restrictinfo, below_outer_join))
979                                 return;
980                         /* EC rejected it, so pass to distribute_restrictinfo_to_rels */
981                 }
982                 else if (maybe_outer_join && restrictinfo->can_join)
983                 {
984                         if (bms_is_subset(restrictinfo->left_relids,
985                                                           outerjoin_nonnullable) &&
986                                 !bms_overlap(restrictinfo->right_relids,
987                                                          outerjoin_nonnullable))
988                         {
989                                 /* we have outervar = innervar */
990                                 root->left_join_clauses = lappend(root->left_join_clauses,
991                                                                                                   restrictinfo);
992                                 return;
993                         }
994                         if (bms_is_subset(restrictinfo->right_relids,
995                                                           outerjoin_nonnullable) &&
996                                 !bms_overlap(restrictinfo->left_relids,
997                                                          outerjoin_nonnullable))
998                         {
999                                 /* we have innervar = outervar */
1000                                 root->right_join_clauses = lappend(root->right_join_clauses,
1001                                                                                                    restrictinfo);
1002                                 return;
1003                         }
1004                         if (bms_equal(outerjoin_nonnullable, qualscope))
1005                         {
1006                                 /* FULL JOIN (above tests cannot match in this case) */
1007                                 root->full_join_clauses = lappend(root->full_join_clauses,
1008                                                                                                   restrictinfo);
1009                                 return;
1010                         }
1011                 }
1012         }
1013
1014         /* No EC special case applies, so push it into the clause lists */
1015         distribute_restrictinfo_to_rels(root, restrictinfo);
1016 }
1017
1018 /*
1019  * distribute_sublink_quals_to_rels
1020  *        Pull sublink quals out of a FlattenedSubLink node and distribute
1021  *        them appropriately; then add a SpecialJoinInfo node to the query's
1022  *        join_info_list.  The FlattenedSubLink node itself is no longer
1023  *        needed and does not propagate into further processing.
1024  */
1025 static void
1026 distribute_sublink_quals_to_rels(PlannerInfo *root,
1027                                                                  FlattenedSubLink *fslink,
1028                                                                  bool below_outer_join)
1029 {
1030         List       *quals = make_ands_implicit(fslink->quals);
1031         SpecialJoinInfo *sjinfo;
1032         Relids          qualscope;
1033         Relids          ojscope;
1034         ListCell   *l;
1035
1036         /*
1037          * Build a suitable SpecialJoinInfo for the sublink.  Note: using
1038          * righthand as inner_join_rels is the conservative worst case;
1039          * it might be possible to use a smaller set and thereby allow
1040          * the sublink join to commute with others inside its RHS.
1041          */
1042         sjinfo = make_outerjoininfo(root,
1043                                                                 fslink->lefthand, fslink->righthand,
1044                                                                 fslink->righthand,
1045                                                                 fslink->jointype,
1046                                                                 quals);
1047
1048         qualscope = bms_union(sjinfo->syn_lefthand, sjinfo->syn_righthand);
1049         ojscope = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
1050
1051         /* Distribute the join quals much as for a regular LEFT JOIN */
1052         foreach(l, quals)
1053         {
1054                 Node   *qual = (Node *) lfirst(l);
1055
1056                 distribute_qual_to_rels(root, qual,
1057                                                                 false, below_outer_join,
1058                                                                 qualscope, ojscope,
1059                                                                 fslink->lefthand);
1060         }
1061
1062         /* Now we can add the SpecialJoinInfo to join_info_list */
1063         root->join_info_list = lappend(root->join_info_list, sjinfo);
1064 }
1065
1066 /*
1067  * check_outerjoin_delay
1068  *              Detect whether a qual referencing the given relids must be delayed
1069  *              in application due to the presence of a lower outer join, and/or
1070  *              may force extra delay of higher-level outer joins.
1071  *
1072  * If the qual must be delayed, add relids to *relids_p to reflect the lowest
1073  * safe level for evaluating the qual, and return TRUE.  Any extra delay for
1074  * higher-level joins is reflected by setting delay_upper_joins to TRUE in
1075  * SpecialJoinInfo structs.
1076  *
1077  * For an is_pushed_down qual, we can evaluate the qual as soon as (1) we have
1078  * all the rels it mentions, and (2) we are at or above any outer joins that
1079  * can null any of these rels and are below the syntactic location of the
1080  * given qual.  We must enforce (2) because pushing down such a clause below
1081  * the OJ might cause the OJ to emit null-extended rows that should not have
1082  * been formed, or that should have been rejected by the clause.  (This is
1083  * only an issue for non-strict quals, since if we can prove a qual mentioning
1084  * only nullable rels is strict, we'd have reduced the outer join to an inner
1085  * join in reduce_outer_joins().)
1086  *
1087  * To enforce (2), scan the join_info_list and merge the required-relid sets of
1088  * any such OJs into the clause's own reference list.  At the time we are
1089  * called, the join_info_list contains only outer joins below this qual.  We
1090  * have to repeat the scan until no new relids get added; this ensures that
1091  * the qual is suitably delayed regardless of the order in which OJs get
1092  * executed.  As an example, if we have one OJ with LHS=A, RHS=B, and one with
1093  * LHS=B, RHS=C, it is implied that these can be done in either order; if the
1094  * B/C join is done first then the join to A can null C, so a qual actually
1095  * mentioning only C cannot be applied below the join to A.
1096  *
1097  * For a non-pushed-down qual, this isn't going to determine where we place the
1098  * qual, but we need to determine outerjoin_delayed anyway for possible use
1099  * in reconsider_outer_join_clauses().
1100  *
1101  * Lastly, a pushed-down qual that references the nullable side of any current
1102  * join_info_list member and has to be evaluated above that OJ (because its
1103  * required relids overlap the LHS too) causes that OJ's delay_upper_joins
1104  * flag to be set TRUE.  This will prevent any higher-level OJs from
1105  * being interchanged with that OJ, which would result in not having any
1106  * correct place to evaluate the qual.  (The case we care about here is a
1107  * sub-select WHERE clause within the RHS of some outer join.  The WHERE
1108  * clause must effectively be treated as a degenerate clause of that outer
1109  * join's condition.  Rather than trying to match such clauses with joins
1110  * directly, we set delay_upper_joins here, and when the upper outer join
1111  * is processed by make_outerjoininfo, it will refrain from allowing the
1112  * two OJs to commute.)
1113  */
1114 static bool
1115 check_outerjoin_delay(PlannerInfo *root, Relids *relids_p,
1116                                           bool is_pushed_down)
1117 {
1118         Relids          relids = *relids_p;
1119         bool            outerjoin_delayed;
1120         bool            found_some;
1121
1122         outerjoin_delayed = false;
1123         do
1124         {
1125                 ListCell   *l;
1126
1127                 found_some = false;
1128                 foreach(l, root->join_info_list)
1129                 {
1130                         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1131
1132                         /* do we reference any nullable rels of this OJ? */
1133                         if (bms_overlap(relids, sjinfo->min_righthand) ||
1134                                 (sjinfo->jointype == JOIN_FULL &&
1135                                  bms_overlap(relids, sjinfo->min_lefthand)))
1136                         {
1137                                 /* yes, so set the result flag */
1138                                 outerjoin_delayed = true;
1139                                 /* have we included all its rels in relids? */
1140                                 if (!bms_is_subset(sjinfo->min_lefthand, relids) ||
1141                                         !bms_is_subset(sjinfo->min_righthand, relids))
1142                                 {
1143                                         /* no, so add them in */
1144                                         relids = bms_add_members(relids, sjinfo->min_lefthand);
1145                                         relids = bms_add_members(relids, sjinfo->min_righthand);
1146                                         /* we'll need another iteration */
1147                                         found_some = true;
1148                                 }
1149                                 /* set delay_upper_joins if needed */
1150                                 if (is_pushed_down && sjinfo->jointype != JOIN_FULL &&
1151                                         bms_overlap(relids, sjinfo->min_lefthand))
1152                                         sjinfo->delay_upper_joins = true;
1153                         }
1154                 }
1155         } while (found_some);
1156
1157         *relids_p = relids;
1158         return outerjoin_delayed;
1159 }
1160
1161 /*
1162  * check_redundant_nullability_qual
1163  *        Check to see if the qual is an IS NULL qual that is redundant with
1164  *        a lower JOIN_ANTI join.
1165  *
1166  * We want to suppress redundant IS NULL quals, not so much to save cycles
1167  * as to avoid generating bogus selectivity estimates for them.  So if
1168  * redundancy is detected here, distribute_qual_to_rels() just throws away
1169  * the qual.
1170  */
1171 static bool
1172 check_redundant_nullability_qual(PlannerInfo *root, Node *clause)
1173 {
1174         Var                *forced_null_var;
1175         Index           forced_null_rel;
1176         SpecialJoinInfo *match_sjinfo = NULL;
1177         ListCell   *lc;
1178
1179         /* Check for IS NULL, and identify the Var forced to NULL */
1180         forced_null_var = find_forced_null_var(clause);
1181         if (forced_null_var == NULL)
1182                 return false;
1183         forced_null_rel = forced_null_var->varno;
1184
1185         /*
1186          * Search to see if there's a matching antijoin that is not masked by
1187          * a higher outer join.  Because we have to scan the join info bottom-up,
1188          * we have to continue looking after finding a match to check for masking
1189          * joins.  This logic should agree with reduce_outer_joins's code
1190          * to detect antijoins on the basis of IS NULL clauses.  (It's tempting
1191          * to consider adding some data structures to avoid redundant work,
1192          * but in practice this code shouldn't get executed often enough to
1193          * make it worth the trouble.)
1194          */
1195         foreach(lc, root->join_info_list)
1196         {
1197                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
1198
1199                 /* Check for match ... */
1200                 if (sjinfo->jointype == JOIN_ANTI &&
1201                         bms_is_member(forced_null_rel, sjinfo->syn_righthand))
1202                 {
1203                         List   *nonnullable_vars;
1204
1205                         nonnullable_vars = find_nonnullable_vars((Node *) sjinfo->join_quals);
1206                         if (list_member(nonnullable_vars, forced_null_var))
1207                         {
1208                                 match_sjinfo = sjinfo;
1209                                 continue;
1210                         }
1211                 }
1212                 /*
1213                  * Else, if we had a lower match, check to see if the target var is
1214                  * from the nullable side of this OJ.  If so, this OJ masks the
1215                  * lower one and we can no longer consider the IS NULL as redundant
1216                  * with the lower antijoin.
1217                  */
1218                 if (!match_sjinfo)
1219                         continue;
1220                 if (bms_is_member(forced_null_rel, sjinfo->syn_righthand) ||
1221                         (sjinfo->jointype == JOIN_FULL &&
1222                          bms_is_member(forced_null_rel, sjinfo->syn_lefthand)))
1223                         match_sjinfo = NULL;
1224         }
1225
1226         return (match_sjinfo != NULL);
1227 }
1228
1229 /*
1230  * distribute_restrictinfo_to_rels
1231  *        Push a completed RestrictInfo into the proper restriction or join
1232  *        clause list(s).
1233  *
1234  * This is the last step of distribute_qual_to_rels() for ordinary qual
1235  * clauses.  Clauses that are interesting for equivalence-class processing
1236  * are diverted to the EC machinery, but may ultimately get fed back here.
1237  */
1238 void
1239 distribute_restrictinfo_to_rels(PlannerInfo *root,
1240                                                                 RestrictInfo *restrictinfo)
1241 {
1242         Relids          relids = restrictinfo->required_relids;
1243         RelOptInfo *rel;
1244
1245         switch (bms_membership(relids))
1246         {
1247                 case BMS_SINGLETON:
1248
1249                         /*
1250                          * There is only one relation participating in the clause, so it
1251                          * is a restriction clause for that relation.
1252                          */
1253                         rel = find_base_rel(root, bms_singleton_member(relids));
1254
1255                         /* Add clause to rel's restriction list */
1256                         rel->baserestrictinfo = lappend(rel->baserestrictinfo,
1257                                                                                         restrictinfo);
1258                         break;
1259                 case BMS_MULTIPLE:
1260
1261                         /*
1262                          * The clause is a join clause, since there is more than one rel
1263                          * in its relid set.
1264                          */
1265
1266                         /*
1267                          * Check for hashjoinable operators.  (We don't bother setting the
1268                          * hashjoin info if we're not going to need it.)
1269                          */
1270                         if (enable_hashjoin)
1271                                 check_hashjoinable(restrictinfo);
1272
1273                         /*
1274                          * Add clause to the join lists of all the relevant relations.
1275                          */
1276                         add_join_clause_to_rels(root, restrictinfo, relids);
1277                         break;
1278                 default:
1279
1280                         /*
1281                          * clause references no rels, and therefore we have no place to
1282                          * attach it.  Shouldn't get here if callers are working properly.
1283                          */
1284                         elog(ERROR, "cannot cope with variable-free clause");
1285                         break;
1286         }
1287 }
1288
1289 /*
1290  * process_implied_equality
1291  *        Create a restrictinfo item that says "item1 op item2", and push it
1292  *        into the appropriate lists.  (In practice opno is always a btree
1293  *        equality operator.)
1294  *
1295  * "qualscope" is the nominal syntactic level to impute to the restrictinfo.
1296  * This must contain at least all the rels used in the expressions, but it
1297  * is used only to set the qual application level when both exprs are
1298  * variable-free.  Otherwise the qual is applied at the lowest join level
1299  * that provides all its variables.
1300  *
1301  * "both_const" indicates whether both items are known pseudo-constant;
1302  * in this case it is worth applying eval_const_expressions() in case we
1303  * can produce constant TRUE or constant FALSE.  (Otherwise it's not,
1304  * because the expressions went through eval_const_expressions already.)
1305  *
1306  * This is currently used only when an EquivalenceClass is found to
1307  * contain pseudoconstants.  See path/pathkeys.c for more details.
1308  */
1309 void
1310 process_implied_equality(PlannerInfo *root,
1311                                                  Oid opno,
1312                                                  Expr *item1,
1313                                                  Expr *item2,
1314                                                  Relids qualscope,
1315                                                  bool below_outer_join,
1316                                                  bool both_const)
1317 {
1318         Expr       *clause;
1319
1320         /*
1321          * Build the new clause.  Copy to ensure it shares no substructure with
1322          * original (this is necessary in case there are subselects in there...)
1323          */
1324         clause = make_opclause(opno,
1325                                                    BOOLOID,             /* opresulttype */
1326                                                    false,               /* opretset */
1327                                                    (Expr *) copyObject(item1),
1328                                                    (Expr *) copyObject(item2));
1329
1330         /* If both constant, try to reduce to a boolean constant. */
1331         if (both_const)
1332         {
1333                 clause = (Expr *) eval_const_expressions(root, (Node *) clause);
1334
1335                 /* If we produced const TRUE, just drop the clause */
1336                 if (clause && IsA(clause, Const))
1337                 {
1338                         Const      *cclause = (Const *) clause;
1339
1340                         Assert(cclause->consttype == BOOLOID);
1341                         if (!cclause->constisnull && DatumGetBool(cclause->constvalue))
1342                                 return;
1343                 }
1344         }
1345
1346         /* Make a copy of qualscope to avoid problems if source EC changes */
1347         qualscope = bms_copy(qualscope);
1348
1349         /*
1350          * Push the new clause into all the appropriate restrictinfo lists.
1351          */
1352         distribute_qual_to_rels(root, (Node *) clause,
1353                                                         true, below_outer_join,
1354                                                         qualscope, NULL, NULL);
1355 }
1356
1357 /*
1358  * build_implied_join_equality --- build a RestrictInfo for a derived equality
1359  *
1360  * This overlaps the functionality of process_implied_equality(), but we
1361  * must return the RestrictInfo, not push it into the joininfo tree.
1362  */
1363 RestrictInfo *
1364 build_implied_join_equality(Oid opno,
1365                                                         Expr *item1,
1366                                                         Expr *item2,
1367                                                         Relids qualscope)
1368 {
1369         RestrictInfo *restrictinfo;
1370         Expr       *clause;
1371
1372         /*
1373          * Build the new clause.  Copy to ensure it shares no substructure with
1374          * original (this is necessary in case there are subselects in there...)
1375          */
1376         clause = make_opclause(opno,
1377                                                    BOOLOID,             /* opresulttype */
1378                                                    false,               /* opretset */
1379                                                    (Expr *) copyObject(item1),
1380                                                    (Expr *) copyObject(item2));
1381
1382         /* Make a copy of qualscope to avoid problems if source EC changes */
1383         qualscope = bms_copy(qualscope);
1384
1385         /*
1386          * Build the RestrictInfo node itself.
1387          */
1388         restrictinfo = make_restrictinfo(clause,
1389                                                                          true,          /* is_pushed_down */
1390                                                                          false,         /* outerjoin_delayed */
1391                                                                          false,         /* pseudoconstant */
1392                                                                          qualscope);
1393
1394         /* Set mergejoinability info always, and hashjoinability if enabled */
1395         check_mergejoinable(restrictinfo);
1396         if (enable_hashjoin)
1397                 check_hashjoinable(restrictinfo);
1398
1399         return restrictinfo;
1400 }
1401
1402
1403 /*****************************************************************************
1404  *
1405  *       CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
1406  *
1407  *****************************************************************************/
1408
1409 /*
1410  * check_mergejoinable
1411  *        If the restrictinfo's clause is mergejoinable, set the mergejoin
1412  *        info fields in the restrictinfo.
1413  *
1414  *        Currently, we support mergejoin for binary opclauses where
1415  *        the operator is a mergejoinable operator.  The arguments can be
1416  *        anything --- as long as there are no volatile functions in them.
1417  */
1418 static void
1419 check_mergejoinable(RestrictInfo *restrictinfo)
1420 {
1421         Expr       *clause = restrictinfo->clause;
1422         Oid                     opno;
1423
1424         if (restrictinfo->pseudoconstant)
1425                 return;
1426         if (!is_opclause(clause))
1427                 return;
1428         if (list_length(((OpExpr *) clause)->args) != 2)
1429                 return;
1430
1431         opno = ((OpExpr *) clause)->opno;
1432
1433         if (op_mergejoinable(opno) &&
1434                 !contain_volatile_functions((Node *) clause))
1435                 restrictinfo->mergeopfamilies = get_mergejoin_opfamilies(opno);
1436
1437         /*
1438          * Note: op_mergejoinable is just a hint; if we fail to find the operator
1439          * in any btree opfamilies, mergeopfamilies remains NIL and so the clause
1440          * is not treated as mergejoinable.
1441          */
1442 }
1443
1444 /*
1445  * check_hashjoinable
1446  *        If the restrictinfo's clause is hashjoinable, set the hashjoin
1447  *        info fields in the restrictinfo.
1448  *
1449  *        Currently, we support hashjoin for binary opclauses where
1450  *        the operator is a hashjoinable operator.      The arguments can be
1451  *        anything --- as long as there are no volatile functions in them.
1452  */
1453 static void
1454 check_hashjoinable(RestrictInfo *restrictinfo)
1455 {
1456         Expr       *clause = restrictinfo->clause;
1457         Oid                     opno;
1458
1459         if (restrictinfo->pseudoconstant)
1460                 return;
1461         if (!is_opclause(clause))
1462                 return;
1463         if (list_length(((OpExpr *) clause)->args) != 2)
1464                 return;
1465
1466         opno = ((OpExpr *) clause)->opno;
1467
1468         if (op_hashjoinable(opno) &&
1469                 !contain_volatile_functions((Node *) clause))
1470                 restrictinfo->hashjoinoperator = opno;
1471 }