]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/initsplan.c
Restructure planner's handling of inheritance. Rather than processing
[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-2005, 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.114 2006/01/31 21:39:24 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "catalog/pg_operator.h"
18 #include "catalog/pg_type.h"
19 #include "nodes/makefuncs.h"
20 #include "optimizer/clauses.h"
21 #include "optimizer/cost.h"
22 #include "optimizer/joininfo.h"
23 #include "optimizer/pathnode.h"
24 #include "optimizer/paths.h"
25 #include "optimizer/planmain.h"
26 #include "optimizer/restrictinfo.h"
27 #include "optimizer/tlist.h"
28 #include "optimizer/var.h"
29 #include "parser/parsetree.h"
30 #include "parser/parse_expr.h"
31 #include "parser/parse_oper.h"
32 #include "utils/builtins.h"
33 #include "utils/lsyscache.h"
34 #include "utils/syscache.h"
35
36
37 /* These parameters are set by GUC */
38 int                     from_collapse_limit;
39 int                     join_collapse_limit;
40
41
42 static void add_vars_to_targetlist(PlannerInfo *root, List *vars,
43                                            Relids where_needed);
44 static List *deconstruct_recurse(PlannerInfo *root, Node *jtnode,
45                                                                  bool below_outer_join, Relids *qualscope);
46 static OuterJoinInfo *make_outerjoininfo(PlannerInfo *root,
47                                                                                  Relids left_rels, Relids right_rels,
48                                                                                  bool is_full_join, Node *clause);
49 static void distribute_qual_to_rels(PlannerInfo *root, Node *clause,
50                                                 bool is_pushed_down,
51                                                 bool is_deduced,
52                                                 bool below_outer_join,
53                                                 Relids qualscope,
54                                                 Relids ojscope,
55                                                 Relids outerjoin_nonnullable);
56 static bool qual_is_redundant(PlannerInfo *root, RestrictInfo *restrictinfo,
57                                   List *restrictlist);
58 static void check_mergejoinable(RestrictInfo *restrictinfo);
59 static void check_hashjoinable(RestrictInfo *restrictinfo);
60
61
62 /*****************************************************************************
63  *
64  *       JOIN TREES
65  *
66  *****************************************************************************/
67
68 /*
69  * add_base_rels_to_query
70  *
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.
74  *
75  * At the end of this process, there should be one baserel RelOptInfo for
76  * every non-join RTE that is used in the query.  Therefore, this routine
77  * is the only place that should call build_simple_rel with reloptkind
78  * RELOPT_BASEREL.  However, otherrels will be built later for append relation
79  * members.
80  */
81 void
82 add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
83 {
84         if (jtnode == NULL)
85                 return;
86         if (IsA(jtnode, RangeTblRef))
87         {
88                 int                     varno = ((RangeTblRef *) jtnode)->rtindex;
89
90                 (void) build_simple_rel(root, varno, RELOPT_BASEREL);
91         }
92         else if (IsA(jtnode, FromExpr))
93         {
94                 FromExpr   *f = (FromExpr *) jtnode;
95                 ListCell   *l;
96
97                 foreach(l, f->fromlist)
98                         add_base_rels_to_query(root, lfirst(l));
99         }
100         else if (IsA(jtnode, JoinExpr))
101         {
102                 JoinExpr   *j = (JoinExpr *) jtnode;
103
104                 add_base_rels_to_query(root, j->larg);
105                 add_base_rels_to_query(root, j->rarg);
106         }
107         else
108                 elog(ERROR, "unrecognized node type: %d",
109                          (int) nodeTag(jtnode));
110 }
111
112
113 /*****************************************************************************
114  *
115  *       TARGET LISTS
116  *
117  *****************************************************************************/
118
119 /*
120  * build_base_rel_tlists
121  *        Add targetlist entries for each var needed in the query's final tlist
122  *        to the appropriate base relations.
123  *
124  * We mark such vars as needed by "relation 0" to ensure that they will
125  * propagate up through all join plan steps.
126  */
127 void
128 build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
129 {
130         List       *tlist_vars = pull_var_clause((Node *) final_tlist, false);
131
132         if (tlist_vars != NIL)
133         {
134                 add_vars_to_targetlist(root, tlist_vars, bms_make_singleton(0));
135                 list_free(tlist_vars);
136         }
137 }
138
139 /*
140  * add_vars_to_targetlist
141  *        For each variable appearing in the list, add it to the owning
142  *        relation's targetlist if not already present, and mark the variable
143  *        as being needed for the indicated join (or for final output if
144  *        where_needed includes "relation 0").
145  */
146 static void
147 add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
148 {
149         ListCell   *temp;
150
151         Assert(!bms_is_empty(where_needed));
152
153         foreach(temp, vars)
154         {
155                 Var                *var = (Var *) lfirst(temp);
156                 RelOptInfo *rel = find_base_rel(root, var->varno);
157                 int                     attrno = var->varattno;
158
159                 Assert(attrno >= rel->min_attr && attrno <= rel->max_attr);
160                 attrno -= rel->min_attr;
161                 if (bms_is_empty(rel->attr_needed[attrno]))
162                 {
163                         /* Variable not yet requested, so add to reltargetlist */
164                         /* XXX is copyObject necessary here? */
165                         rel->reltargetlist = lappend(rel->reltargetlist, copyObject(var));
166                 }
167                 rel->attr_needed[attrno] = bms_add_members(rel->attr_needed[attrno],
168                                                                                                    where_needed);
169         }
170 }
171
172
173 /*****************************************************************************
174  *
175  *        JOIN TREE PROCESSING
176  *
177  *****************************************************************************/
178
179 /*
180  * deconstruct_jointree
181  *        Recursively scan the query's join tree for WHERE and JOIN/ON qual
182  *        clauses, and add these to the appropriate restrictinfo and joininfo
183  *        lists belonging to base RelOptInfos.  Also, add OuterJoinInfo nodes
184  *        to root->oj_info_list for any outer joins appearing in the query tree.
185  *        Return a "joinlist" data structure showing the join order decisions
186  *        that need to be made by make_one_rel().
187  *
188  * The "joinlist" result is a list of items that are either RangeTblRef
189  * jointree nodes or sub-joinlists.  All the items at the same level of
190  * joinlist must be joined in an order to be determined by make_one_rel()
191  * (note that legal orders may be constrained by OuterJoinInfo nodes).
192  * A sub-joinlist represents a subproblem to be planned separately. Currently
193  * sub-joinlists arise only from FULL OUTER JOIN or when collapsing of
194  * subproblems is stopped by join_collapse_limit or from_collapse_limit.
195  *
196  * NOTE: when dealing with inner joins, it is appropriate to let a qual clause
197  * be evaluated at the lowest level where all the variables it mentions are
198  * available.  However, we cannot push a qual down into the nullable side(s)
199  * of an outer join since the qual might eliminate matching rows and cause a
200  * NULL row to be incorrectly emitted by the join.  Therefore, we artificially
201  * OR the minimum-relids of such an outer join into the required_relids of
202  * clauses appearing above it.  This forces those clauses to be delayed until
203  * application of the outer join (or maybe even higher in the join tree).
204  */
205 List *
206 deconstruct_jointree(PlannerInfo *root)
207 {
208         Relids          qualscope;
209
210         /* Start recursion at top of jointree */
211         Assert(root->parse->jointree != NULL &&
212                    IsA(root->parse->jointree, FromExpr));
213
214         return deconstruct_recurse(root, (Node *) root->parse->jointree, false,
215                                                            &qualscope);
216 }
217
218 /*
219  * deconstruct_recurse
220  *        One recursion level of deconstruct_jointree processing.
221  *
222  * Inputs:
223  *      jtnode is the jointree node to examine
224  *      below_outer_join is TRUE if this node is within the nullable side of a
225  *              higher-level outer join
226  * Outputs:
227  *      *qualscope gets the set of base Relids syntactically included in this
228  *              jointree node (do not modify or free this, as it may also be pointed
229  *              to by RestrictInfo nodes)
230  *      Return value is the appropriate joinlist for this jointree node
231  *
232  * In addition, entries will be added to root->oj_info_list for outer joins.
233  */
234 static List *
235 deconstruct_recurse(PlannerInfo *root, Node *jtnode, bool below_outer_join,
236                                         Relids *qualscope)
237 {
238         List       *joinlist;
239
240         if (jtnode == NULL)
241         {
242                 *qualscope = NULL;
243                 return NIL;
244         }
245         if (IsA(jtnode, RangeTblRef))
246         {
247                 int                     varno = ((RangeTblRef *) jtnode)->rtindex;
248
249                 /* No quals to deal with, just return correct result */
250                 *qualscope = bms_make_singleton(varno);
251                 joinlist = list_make1(jtnode);
252         }
253         else if (IsA(jtnode, FromExpr))
254         {
255                 FromExpr   *f = (FromExpr *) jtnode;
256                 int                     remaining;
257                 ListCell   *l;
258
259                 /*
260                  * First, recurse to handle child joins.  We collapse subproblems
261                  * into a single joinlist whenever the resulting joinlist wouldn't
262                  * exceed from_collapse_limit members.  Also, always collapse
263                  * one-element subproblems, since that won't lengthen the joinlist
264                  * anyway.
265                  */
266                 *qualscope = NULL;
267                 joinlist = NIL;
268                 remaining = list_length(f->fromlist);
269                 foreach(l, f->fromlist)
270                 {
271                         Relids  sub_qualscope;
272                         List   *sub_joinlist;
273                         int             sub_members;
274
275                         sub_joinlist = deconstruct_recurse(root, lfirst(l),
276                                                                                            below_outer_join,
277                                                                                            &sub_qualscope);
278                         *qualscope = bms_add_members(*qualscope, sub_qualscope);
279                         sub_members = list_length(sub_joinlist);
280                         remaining--;
281                         if (sub_members <= 1 ||
282                                 list_length(joinlist) + sub_members + remaining <= from_collapse_limit)
283                                 joinlist = list_concat(joinlist, sub_joinlist);
284                         else
285                                 joinlist = lappend(joinlist, sub_joinlist);
286                 }
287
288                 /*
289                  * Now process the top-level quals.  These are always marked as
290                  * "pushed down", since they clearly didn't come from a JOIN expr.
291                  */
292                 foreach(l, (List *) f->quals)
293                         distribute_qual_to_rels(root, (Node *) lfirst(l),
294                                                                         true, false, below_outer_join,
295                                                                         *qualscope, NULL, NULL);
296         }
297         else if (IsA(jtnode, JoinExpr))
298         {
299                 JoinExpr   *j = (JoinExpr *) jtnode;
300                 Relids          leftids,
301                                         rightids,
302                                         nonnullable_rels,
303                                         ojscope;
304                 List       *leftjoinlist,
305                                    *rightjoinlist;
306                 OuterJoinInfo *ojinfo;
307                 ListCell   *qual;
308
309                 /*
310                  * Order of operations here is subtle and critical.  First we recurse
311                  * to handle sub-JOINs.  Their join quals will be placed without
312                  * regard for whether this level is an outer join, which is correct.
313                  * Then we place our own join quals, which are restricted by lower
314                  * outer joins in any case, and are forced to this level if this is an
315                  * outer join and they mention the outer side.  Finally, if this is an
316                  * outer join, we create an oj_info_list entry for the join.  This
317                  * will prevent quals above us in the join tree that use those rels
318                  * from being pushed down below this level.  (It's okay for upper
319                  * quals to be pushed down to the outer side, however.)
320                  */
321                 switch (j->jointype)
322                 {
323                         case JOIN_INNER:
324                                 leftjoinlist = deconstruct_recurse(root, j->larg,
325                                                                                                    below_outer_join,
326                                                                                                    &leftids);
327                                 rightjoinlist = deconstruct_recurse(root, j->rarg,
328                                                                                                         below_outer_join,
329                                                                                                         &rightids);
330                                 *qualscope = bms_union(leftids, rightids);
331                                 /* Inner join adds no restrictions for quals */
332                                 nonnullable_rels = NULL;
333                                 break;
334                         case JOIN_LEFT:
335                                 leftjoinlist = deconstruct_recurse(root, j->larg,
336                                                                                                    below_outer_join,
337                                                                                                    &leftids);
338                                 rightjoinlist = deconstruct_recurse(root, j->rarg,
339                                                                                                         true,
340                                                                                                         &rightids);
341                                 *qualscope = bms_union(leftids, rightids);
342                                 nonnullable_rels = leftids;
343                                 break;
344                         case JOIN_FULL:
345                                 leftjoinlist = deconstruct_recurse(root, j->larg,
346                                                                                                    true,
347                                                                                                    &leftids);
348                                 rightjoinlist = deconstruct_recurse(root, j->rarg,
349                                                                                                         true,
350                                                                                                         &rightids);
351                                 *qualscope = bms_union(leftids, rightids);
352                                 /* each side is both outer and inner */
353                                 nonnullable_rels = *qualscope;
354                                 break;
355                         case JOIN_RIGHT:
356                                 /* notice we switch leftids and rightids */
357                                 leftjoinlist = deconstruct_recurse(root, j->larg,
358                                                                                                    true,
359                                                                                                    &rightids);
360                                 rightjoinlist = deconstruct_recurse(root, j->rarg,
361                                                                                                         below_outer_join,
362                                                                                                         &leftids);
363                                 *qualscope = bms_union(leftids, rightids);
364                                 nonnullable_rels = leftids;
365                                 break;
366                         case JOIN_UNION:
367
368                                 /*
369                                  * This is where we fail if upper levels of planner haven't
370                                  * rewritten UNION JOIN as an Append ...
371                                  */
372                                 ereport(ERROR,
373                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
374                                                  errmsg("UNION JOIN is not implemented")));
375                                 nonnullable_rels = NULL;                /* keep compiler quiet */
376                                 leftjoinlist = rightjoinlist = NIL;
377                                 break;
378                         default:
379                                 elog(ERROR, "unrecognized join type: %d",
380                                          (int) j->jointype);
381                                 nonnullable_rels = NULL;                /* keep compiler quiet */
382                                 leftjoinlist = rightjoinlist = NIL;
383                                 break;
384                 }
385
386                 /*
387                  * For an OJ, form the OuterJoinInfo now, because we need the OJ's
388                  * semantic scope (ojscope) to pass to distribute_qual_to_rels.
389                  */
390                 if (j->jointype != JOIN_INNER)
391                 {
392                         ojinfo = make_outerjoininfo(root, leftids, rightids,
393                                                                                 (j->jointype == JOIN_FULL), j->quals);
394                         ojscope = bms_union(ojinfo->min_lefthand, ojinfo->min_righthand);
395                 }
396                 else
397                 {
398                         ojinfo = NULL;
399                         ojscope = NULL;
400                 }
401
402                 /* Process the qual clauses */
403                 foreach(qual, (List *) j->quals)
404                         distribute_qual_to_rels(root, (Node *) lfirst(qual),
405                                                                         false, false, below_outer_join,
406                                                                         *qualscope, ojscope, nonnullable_rels);
407
408                 /* Now we can add the OuterJoinInfo to oj_info_list */
409                 if (ojinfo)
410                         root->oj_info_list = lappend(root->oj_info_list, ojinfo);
411
412                 /*
413                  * Finally, compute the output joinlist.  We fold subproblems together
414                  * except at a FULL JOIN or where join_collapse_limit would be
415                  * exceeded.
416                  */
417                 if (j->jointype != JOIN_FULL &&
418                         (list_length(leftjoinlist) + list_length(rightjoinlist) <=
419                          join_collapse_limit))
420                         joinlist = list_concat(leftjoinlist, rightjoinlist);
421                 else                                    /* force the join order at this node */
422                         joinlist = list_make1(list_make2(leftjoinlist, rightjoinlist));
423         }
424         else
425         {
426                 elog(ERROR, "unrecognized node type: %d",
427                          (int) nodeTag(jtnode));
428                 joinlist = NIL;                 /* keep compiler quiet */
429         }
430         return joinlist;
431 }
432
433 /*
434  * make_outerjoininfo
435  *        Build an OuterJoinInfo for the current outer join
436  *
437  * Inputs:
438  *      left_rels: the base Relids syntactically on outer side of join
439  *      right_rels: the base Relids syntactically on inner side of join
440  *      is_full_join: what it says
441  *      clause: the outer join's join condition
442  *
443  * If the join is a RIGHT JOIN, left_rels and right_rels are switched by
444  * the caller, so that left_rels is always the nonnullable side.  Hence
445  * we need only distinguish the LEFT and FULL cases.
446  *
447  * The node should eventually be put into root->oj_info_list, but we
448  * do not do that here.
449  */
450 static OuterJoinInfo *
451 make_outerjoininfo(PlannerInfo *root,
452                                    Relids left_rels, Relids right_rels,
453                                    bool is_full_join, Node *clause)
454 {
455         OuterJoinInfo *ojinfo = makeNode(OuterJoinInfo);
456         Relids          clause_relids;
457         Relids          strict_relids;
458         ListCell   *l;
459
460         /* If it's a full join, no need to be very smart */
461         ojinfo->is_full_join = is_full_join;
462         if (is_full_join)
463         {
464                 ojinfo->min_lefthand = left_rels;
465                 ojinfo->min_righthand = right_rels;
466                 ojinfo->lhs_strict = false;                     /* don't care about this */
467                 return ojinfo;
468         }
469
470         /*
471          * Retrieve all relids mentioned within the join clause.
472          */
473         clause_relids = pull_varnos(clause);
474
475         /*
476          * For which relids is the clause strict, ie, it cannot succeed if the
477          * rel's columns are all NULL?
478          */
479         strict_relids = find_nonnullable_rels(clause);
480
481         /* Remember whether the clause is strict for any LHS relations */
482         ojinfo->lhs_strict = bms_overlap(strict_relids, left_rels);
483
484         /*
485          * Required LHS is basically the LHS rels mentioned in the clause...
486          * but if there aren't any, punt and make it the full LHS, to avoid
487          * having an empty min_lefthand which will confuse later processing.
488          * (We don't try to be smart about such cases, just correct.)
489          * We may have to add more rels based on lower outer joins; see below.
490          */
491         ojinfo->min_lefthand = bms_intersect(clause_relids, left_rels);
492         if (bms_is_empty(ojinfo->min_lefthand))
493                 ojinfo->min_lefthand = bms_copy(left_rels);
494
495         /*
496          * Required RHS is normally the full set of RHS rels.  Sometimes we
497          * can exclude some, see below.
498          */
499         ojinfo->min_righthand = bms_copy(right_rels);
500
501         foreach(l, root->oj_info_list)
502         {
503                 OuterJoinInfo *otherinfo = (OuterJoinInfo *) lfirst(l);
504
505                 /* ignore full joins --- other mechanisms preserve their ordering */
506                 if (otherinfo->is_full_join)
507                         continue;
508
509                 /*
510                  * For a lower OJ in our LHS, if our join condition uses the lower
511                  * join's RHS and is not strict for that rel, we must preserve the
512                  * ordering of the two OJs, so add lower OJ's full required relset to
513                  * min_lefthand.
514                  */
515                 if (bms_overlap(ojinfo->min_lefthand, otherinfo->min_righthand) &&
516                         !bms_overlap(strict_relids, otherinfo->min_righthand))
517                 {
518                         ojinfo->min_lefthand = bms_add_members(ojinfo->min_lefthand,
519                                                                                                    otherinfo->min_lefthand);
520                         ojinfo->min_lefthand = bms_add_members(ojinfo->min_lefthand,
521                                                                                                    otherinfo->min_righthand);
522                 }
523                 /*
524                  * For a lower OJ in our RHS, if our join condition does not use the
525                  * lower join's RHS and the lower OJ's join condition is strict, we
526                  * can interchange the ordering of the two OJs, so exclude the lower
527                  * RHS from our min_righthand.
528                  */
529                 if (bms_overlap(ojinfo->min_righthand, otherinfo->min_righthand) &&
530                         !bms_overlap(clause_relids, otherinfo->min_righthand) &&
531                         otherinfo->lhs_strict)
532                 {
533                         ojinfo->min_righthand = bms_del_members(ojinfo->min_righthand,
534                                                                                                         otherinfo->min_righthand);
535                 }
536         }
537
538         /* Neither set should be empty, else we might get confused later */
539         Assert(!bms_is_empty(ojinfo->min_lefthand));
540         Assert(!bms_is_empty(ojinfo->min_righthand));
541         /* Shouldn't overlap either */
542         Assert(!bms_overlap(ojinfo->min_lefthand, ojinfo->min_righthand));
543
544         return ojinfo;
545 }
546
547
548 /*****************************************************************************
549  *
550  *        QUALIFICATIONS
551  *
552  *****************************************************************************/
553
554 /*
555  * distribute_qual_to_rels
556  *        Add clause information to either the baserestrictinfo or joininfo list
557  *        (depending on whether the clause is a join) of each base relation
558  *        mentioned in the clause.      A RestrictInfo node is created and added to
559  *        the appropriate list for each rel.  Also, if the clause uses a
560  *        mergejoinable operator and is not delayed by outer-join rules, enter
561  *        the left- and right-side expressions into the query's lists of
562  *        equijoined vars.
563  *
564  * 'clause': the qual clause to be distributed
565  * 'is_pushed_down': if TRUE, force the clause to be marked 'is_pushed_down'
566  *              (this indicates the clause came from a FromExpr, not a JoinExpr)
567  * 'is_deduced': TRUE if the qual came from implied-equality deduction
568  * 'below_outer_join': TRUE if the qual is from a JOIN/ON that is below the
569  *              nullable side of a higher-level outer join.
570  * 'qualscope': set of baserels the qual's syntactic scope covers
571  * 'ojscope': NULL if not an outer-join qual, else the minimum set of baserels
572  *              needed to form this join
573  * 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
574  *              baserels appearing on the outer (nonnullable) side of the join
575  *              (for FULL JOIN this includes both sides of the join, and must in fact
576  *              equal qualscope)
577  *
578  * 'qualscope' identifies what level of JOIN the qual came from syntactically.
579  * 'ojscope' is needed if we decide to force the qual up to the outer-join
580  * level, which will be ojscope not necessarily qualscope.
581  */
582 static void
583 distribute_qual_to_rels(PlannerInfo *root, Node *clause,
584                                                 bool is_pushed_down,
585                                                 bool is_deduced,
586                                                 bool below_outer_join,
587                                                 Relids qualscope,
588                                                 Relids ojscope,
589                                                 Relids outerjoin_nonnullable)
590 {
591         Relids          relids;
592         bool            outerjoin_delayed;
593         bool            maybe_equijoin;
594         bool            maybe_outer_join;
595         RestrictInfo *restrictinfo;
596         RelOptInfo *rel;
597         List       *vars;
598
599         /*
600          * Retrieve all relids mentioned within the clause.
601          */
602         relids = pull_varnos(clause);
603
604         /*
605          * Cross-check: clause should contain no relids not within its scope.
606          * Otherwise the parser messed up.
607          */
608         if (!bms_is_subset(relids, qualscope))
609                 elog(ERROR, "JOIN qualification may not refer to other relations");
610         if (ojscope && !bms_is_subset(relids, ojscope))
611                 elog(ERROR, "JOIN qualification may not refer to other relations");
612
613         /*
614          * If the clause is variable-free, we force it to be evaluated at its
615          * original syntactic level.  Note that this should not happen for
616          * top-level clauses, because query_planner() special-cases them.  But it
617          * will happen for variable-free JOIN/ON clauses.  We don't have to be
618          * real smart about such a case, we just have to be correct.  Also note
619          * that for an outer-join clause, we must force it to the OJ's semantic
620          * level, not the syntactic scope.
621          */
622         if (bms_is_empty(relids))
623                 relids = ojscope ? ojscope : qualscope;
624
625         /*
626          * Check to see if clause application must be delayed by outer-join
627          * considerations.
628          */
629         if (is_deduced)
630         {
631                 /*
632                  * If the qual came from implied-equality deduction, we always
633                  * evaluate the qual at its natural semantic level.  It is the
634                  * responsibility of the deducer not to create any quals that should
635                  * be delayed by outer-join rules.
636                  */
637                 Assert(bms_equal(relids, qualscope));
638                 Assert(!ojscope);
639                 /* Needn't feed it back for more deductions */
640                 outerjoin_delayed = false;
641                 maybe_equijoin = false;
642                 maybe_outer_join = false;
643         }
644         else if (bms_overlap(relids, outerjoin_nonnullable))
645         {
646                 /*
647                  * The qual is attached to an outer join and mentions (some of the)
648                  * rels on the nonnullable side.  Force the qual to be evaluated
649                  * exactly at the level of joining corresponding to the outer join. We
650                  * cannot let it get pushed down into the nonnullable side, since then
651                  * we'd produce no output rows, rather than the intended single
652                  * null-extended row, for any nonnullable-side rows failing the qual.
653                  *
654                  * Note: an outer-join qual that mentions only nullable-side rels can
655                  * be pushed down into the nullable side without changing the join
656                  * result, so we treat it the same as an ordinary inner-join qual,
657                  * except for not setting maybe_equijoin (see below).
658                  */
659                 Assert(ojscope);
660                 relids = ojscope;
661                 outerjoin_delayed = true;
662
663                 /*
664                  * We can't use such a clause to deduce equijoin (the left and right
665                  * sides might be unequal above the join because one of them has gone
666                  * to NULL) ... but we might be able to use it for more limited
667                  * purposes.  Note: for the current uses of deductions from an
668                  * outer-join clause, it seems safe to make the deductions even when
669                  * the clause is below a higher-level outer join; so we do not check
670                  * below_outer_join here.
671                  */
672                 maybe_equijoin = false;
673                 maybe_outer_join = true;
674         }
675         else
676         {
677                 /*
678                  * For a non-outer-join qual, we can evaluate the qual as soon as (1)
679                  * we have all the rels it mentions, and (2) we are at or above any
680                  * outer joins that can null any of these rels and are below the
681                  * syntactic location of the given qual. To enforce the latter, scan
682                  * the oj_info_list and merge the required-relid sets of any such OJs
683                  * into the clause's own reference list.  At the time we are called,
684                  * the oj_info_list contains only outer joins below this qual.
685                  */
686                 Relids          addrelids = NULL;
687                 ListCell   *l;
688
689                 outerjoin_delayed = false;
690                 foreach(l, root->oj_info_list)
691                 {
692                         OuterJoinInfo *ojinfo = (OuterJoinInfo *) lfirst(l);
693
694                         if (bms_overlap(relids, ojinfo->min_righthand) ||
695                                 (ojinfo->is_full_join &&
696                                  bms_overlap(relids, ojinfo->min_lefthand)))
697                         {
698                                 addrelids = bms_add_members(addrelids, ojinfo->min_lefthand);
699                                 addrelids = bms_add_members(addrelids, ojinfo->min_righthand);
700                                 outerjoin_delayed = true;
701                         }
702                 }
703
704                 if (bms_is_subset(addrelids, relids))
705                 {
706                         /*
707                          * Qual is not delayed by any lower outer-join restriction. If it
708                          * is not itself below or within an outer join, we can consider it
709                          * "valid everywhere", so consider feeding it to the equijoin
710                          * machinery.  (If it is within an outer join, we can't consider
711                          * it "valid everywhere": once the contained variables have gone
712                          * to NULL, we'd be asserting things like NULL = NULL, which is
713                          * not true.)
714                          */
715                         if (!below_outer_join && outerjoin_nonnullable == NULL)
716                                 maybe_equijoin = true;
717                         else
718                                 maybe_equijoin = false;
719                 }
720                 else
721                 {
722                         relids = bms_union(relids, addrelids);
723                         /* Should still be a subset of current scope ... */
724                         Assert(bms_is_subset(relids, qualscope));
725
726                         /*
727                          * Because application of the qual will be delayed by outer join,
728                          * we mustn't assume its vars are equal everywhere.
729                          */
730                         maybe_equijoin = false;
731                 }
732                 bms_free(addrelids);
733                 maybe_outer_join = false;
734         }
735
736         /*
737          * Mark the qual as "pushed down" if it can be applied at a level below
738          * its original syntactic level.  This allows us to distinguish original
739          * JOIN/ON quals from higher-level quals pushed down to the same joinrel.
740          * A qual originating from WHERE is always considered "pushed down".
741          * Note that for an outer-join qual, we have to compare to ojscope not
742          * qualscope.
743          */
744         if (!is_pushed_down)
745                 is_pushed_down = !bms_equal(relids, ojscope ? ojscope : qualscope);
746
747         /*
748          * Build the RestrictInfo node itself.
749          */
750         restrictinfo = make_restrictinfo((Expr *) clause,
751                                                                          is_pushed_down,
752                                                                          outerjoin_delayed,
753                                                                          relids);
754
755         /*
756          * Figure out where to attach it.
757          */
758         switch (bms_membership(relids))
759         {
760                 case BMS_SINGLETON:
761
762                         /*
763                          * There is only one relation participating in 'clause', so
764                          * 'clause' is a restriction clause for that relation.
765                          */
766                         rel = find_base_rel(root, bms_singleton_member(relids));
767
768                         /*
769                          * Check for a "mergejoinable" clause even though it's not a join
770                          * clause.      This is so that we can recognize that "a.x = a.y"
771                          * makes x and y eligible to be considered equal, even when they
772                          * belong to the same rel.      Without this, we would not recognize
773                          * that "a.x = a.y AND a.x = b.z AND a.y = c.q" allows us to
774                          * consider z and q equal after their rels are joined.
775                          */
776                         check_mergejoinable(restrictinfo);
777
778                         /*
779                          * If the clause was deduced from implied equality, check to see
780                          * whether it is redundant with restriction clauses we already
781                          * have for this rel.  Note we cannot apply this check to
782                          * user-written clauses, since we haven't found the canonical
783                          * pathkey sets yet while processing user clauses. (NB: no
784                          * comparable check is done in the join-clause case; redundancy
785                          * will be detected when the join clause is moved into a join
786                          * rel's restriction list.)
787                          */
788                         if (!is_deduced ||
789                                 !qual_is_redundant(root, restrictinfo,
790                                                                    rel->baserestrictinfo))
791                         {
792                                 /* Add clause to rel's restriction list */
793                                 rel->baserestrictinfo = lappend(rel->baserestrictinfo,
794                                                                                                 restrictinfo);
795                         }
796                         break;
797                 case BMS_MULTIPLE:
798
799                         /*
800                          * 'clause' is a join clause, since there is more than one rel in
801                          * the relid set.
802                          */
803
804                         /*
805                          * Check for hash or mergejoinable operators.
806                          *
807                          * We don't bother setting the hashjoin info if we're not going to
808                          * need it.  We do want to know about mergejoinable ops in all
809                          * cases, however, because we use mergejoinable ops for other
810                          * purposes such as detecting redundant clauses.
811                          */
812                         check_mergejoinable(restrictinfo);
813                         if (enable_hashjoin)
814                                 check_hashjoinable(restrictinfo);
815
816                         /*
817                          * Add clause to the join lists of all the relevant relations.
818                          */
819                         add_join_clause_to_rels(root, restrictinfo, relids);
820
821                         /*
822                          * Add vars used in the join clause to targetlists of their
823                          * relations, so that they will be emitted by the plan nodes that
824                          * scan those relations (else they won't be available at the join
825                          * node!).
826                          */
827                         vars = pull_var_clause(clause, false);
828                         add_vars_to_targetlist(root, vars, relids);
829                         list_free(vars);
830                         break;
831                 default:
832
833                         /*
834                          * 'clause' references no rels, and therefore we have no place to
835                          * attach it.  Shouldn't get here if callers are working properly.
836                          */
837                         elog(ERROR, "cannot cope with variable-free clause");
838                         break;
839         }
840
841         /*
842          * If the clause has a mergejoinable operator, we may be able to deduce
843          * more things from it under the principle of transitivity.
844          *
845          * If it is not an outer-join qualification nor bubbled up due to an outer
846          * join, then the two sides represent equivalent PathKeyItems for path
847          * keys: any path that is sorted by one side will also be sorted by the
848          * other (as soon as the two rels are joined, that is).  Pass such clauses
849          * to add_equijoined_keys.
850          *
851          * If it is a left or right outer-join qualification that relates the two
852          * sides of the outer join (no funny business like leftvar1 = leftvar2 +
853          * rightvar), we add it to root->left_join_clauses or
854          * root->right_join_clauses according to which side the nonnullable
855          * variable appears on.
856          *
857          * If it is a full outer-join qualification, we add it to
858          * root->full_join_clauses.  (Ideally we'd discard cases that aren't
859          * leftvar = rightvar, as we do for left/right joins, but this routine
860          * doesn't have the info needed to do that; and the current usage of the
861          * full_join_clauses list doesn't require that, so it's not currently
862          * worth complicating this routine's API to make it possible.)
863          */
864         if (restrictinfo->mergejoinoperator != InvalidOid)
865         {
866                 if (maybe_equijoin)
867                         add_equijoined_keys(root, restrictinfo);
868                 else if (maybe_outer_join && restrictinfo->can_join)
869                 {
870                         if (bms_is_subset(restrictinfo->left_relids,
871                                                           outerjoin_nonnullable) &&
872                                 !bms_overlap(restrictinfo->right_relids,
873                                                          outerjoin_nonnullable))
874                         {
875                                 /* we have outervar = innervar */
876                                 root->left_join_clauses = lappend(root->left_join_clauses,
877                                                                                                   restrictinfo);
878                         }
879                         else if (bms_is_subset(restrictinfo->right_relids,
880                                                                    outerjoin_nonnullable) &&
881                                          !bms_overlap(restrictinfo->left_relids,
882                                                                   outerjoin_nonnullable))
883                         {
884                                 /* we have innervar = outervar */
885                                 root->right_join_clauses = lappend(root->right_join_clauses,
886                                                                                                    restrictinfo);
887                         }
888                         else if (bms_equal(outerjoin_nonnullable, qualscope))
889                         {
890                                 /* FULL JOIN (above tests cannot match in this case) */
891                                 root->full_join_clauses = lappend(root->full_join_clauses,
892                                                                                                   restrictinfo);
893                         }
894                 }
895         }
896 }
897
898 /*
899  * process_implied_equality
900  *        Check to see whether we already have a restrictinfo item that says
901  *        item1 = item2, and create one if not; or if delete_it is true,
902  *        remove any such restrictinfo item.
903  *
904  * This processing is a consequence of transitivity of mergejoin equality:
905  * if we have mergejoinable clauses A = B and B = C, we can deduce A = C
906  * (where = is an appropriate mergejoinable operator).  See path/pathkeys.c
907  * for more details.
908  */
909 void
910 process_implied_equality(PlannerInfo *root,
911                                                  Node *item1, Node *item2,
912                                                  Oid sortop1, Oid sortop2,
913                                                  Relids item1_relids, Relids item2_relids,
914                                                  bool delete_it)
915 {
916         Relids          relids;
917         BMS_Membership membership;
918         RelOptInfo *rel1;
919         List       *restrictlist;
920         ListCell   *itm;
921         Oid                     ltype,
922                                 rtype;
923         Operator        eq_operator;
924         Form_pg_operator pgopform;
925         Expr       *clause;
926
927         /* Get set of relids referenced in the two expressions */
928         relids = bms_union(item1_relids, item2_relids);
929         membership = bms_membership(relids);
930
931         /*
932          * generate_implied_equalities() shouldn't call me on two constants.
933          */
934         Assert(membership != BMS_EMPTY_SET);
935
936         /*
937          * If the exprs involve a single rel, we need to look at that rel's
938          * baserestrictinfo list.  If multiple rels, we can scan the joininfo list
939          * of any of 'em.
940          */
941         if (membership == BMS_SINGLETON)
942         {
943                 rel1 = find_base_rel(root, bms_singleton_member(relids));
944                 restrictlist = rel1->baserestrictinfo;
945         }
946         else
947         {
948                 Relids          other_rels;
949                 int                     first_rel;
950
951                 /* Copy relids, find and remove one member */
952                 other_rels = bms_copy(relids);
953                 first_rel = bms_first_member(other_rels);
954                 bms_free(other_rels);
955
956                 rel1 = find_base_rel(root, first_rel);
957                 restrictlist = rel1->joininfo;
958         }
959
960         /*
961          * Scan to see if equality is already known.  If so, we're done in the add
962          * case, and done after removing it in the delete case.
963          */
964         foreach(itm, restrictlist)
965         {
966                 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(itm);
967                 Node       *left,
968                                    *right;
969
970                 if (restrictinfo->mergejoinoperator == InvalidOid)
971                         continue;                       /* ignore non-mergejoinable clauses */
972                 /* We now know the restrictinfo clause is a binary opclause */
973                 left = get_leftop(restrictinfo->clause);
974                 right = get_rightop(restrictinfo->clause);
975                 if ((equal(item1, left) && equal(item2, right)) ||
976                         (equal(item2, left) && equal(item1, right)))
977                 {
978                         /* found a matching clause */
979                         if (delete_it)
980                         {
981                                 if (membership == BMS_SINGLETON)
982                                 {
983                                         /* delete it from local restrictinfo list */
984                                         rel1->baserestrictinfo = list_delete_ptr(rel1->baserestrictinfo,
985                                                                                                                          restrictinfo);
986                                 }
987                                 else
988                                 {
989                                         /* let joininfo.c do it */
990                                         remove_join_clause_from_rels(root, restrictinfo, relids);
991                                 }
992                         }
993                         return;                         /* done */
994                 }
995         }
996
997         /* Didn't find it.  Done if deletion requested */
998         if (delete_it)
999                 return;
1000
1001         /*
1002          * This equality is new information, so construct a clause representing it
1003          * to add to the query data structures.
1004          */
1005         ltype = exprType(item1);
1006         rtype = exprType(item2);
1007         eq_operator = compatible_oper(list_make1(makeString("=")),
1008                                                                   ltype, rtype, true);
1009         if (!HeapTupleIsValid(eq_operator))
1010         {
1011                 /*
1012                  * Would it be safe to just not add the equality to the query if we
1013                  * have no suitable equality operator for the combination of
1014                  * datatypes?  NO, because sortkey selection may screw up anyway.
1015                  */
1016                 ereport(ERROR,
1017                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1018                 errmsg("could not identify an equality operator for types %s and %s",
1019                            format_type_be(ltype), format_type_be(rtype))));
1020         }
1021         pgopform = (Form_pg_operator) GETSTRUCT(eq_operator);
1022
1023         /*
1024          * Let's just make sure this appears to be a compatible operator.
1025          */
1026         if (pgopform->oprlsortop != sortop1 ||
1027                 pgopform->oprrsortop != sortop2 ||
1028                 pgopform->oprresult != BOOLOID)
1029                 ereport(ERROR,
1030                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1031                                  errmsg("equality operator for types %s and %s should be merge-joinable, but isn't",
1032                                                 format_type_be(ltype), format_type_be(rtype))));
1033
1034         /*
1035          * Now we can build the new clause.  Copy to ensure it shares no
1036          * substructure with original (this is necessary in case there are
1037          * subselects in there...)
1038          */
1039         clause = make_opclause(oprid(eq_operator),      /* opno */
1040                                                    BOOLOID,             /* opresulttype */
1041                                                    false,               /* opretset */
1042                                                    (Expr *) copyObject(item1),
1043                                                    (Expr *) copyObject(item2));
1044
1045         ReleaseSysCache(eq_operator);
1046
1047         /*
1048          * Push the new clause into all the appropriate restrictinfo lists.
1049          *
1050          * Note: we mark the qual "pushed down" to ensure that it can never be
1051          * taken for an original JOIN/ON clause.
1052          */
1053         distribute_qual_to_rels(root, (Node *) clause,
1054                                                         true, true, false, relids, NULL, NULL);
1055 }
1056
1057 /*
1058  * qual_is_redundant
1059  *        Detect whether an implied-equality qual that turns out to be a
1060  *        restriction clause for a single base relation is redundant with
1061  *        already-known restriction clauses for that rel.  This occurs with,
1062  *        for example,
1063  *                              SELECT * FROM tab WHERE f1 = f2 AND f2 = f3;
1064  *        We need to suppress the redundant condition to avoid computing
1065  *        too-small selectivity, not to mention wasting time at execution.
1066  *
1067  * Note: quals of the form "var = const" are never considered redundant,
1068  * only those of the form "var = var".  This is needed because when we
1069  * have constants in an implied-equality set, we use a different strategy
1070  * that suppresses all "var = var" deductions.  We must therefore keep
1071  * all the "var = const" quals.
1072  */
1073 static bool
1074 qual_is_redundant(PlannerInfo *root,
1075                                   RestrictInfo *restrictinfo,
1076                                   List *restrictlist)
1077 {
1078         Node       *newleft;
1079         Node       *newright;
1080         List       *oldquals;
1081         ListCell   *olditem;
1082         List       *equalexprs;
1083         bool            someadded;
1084
1085         /* Never redundant unless vars appear on both sides */
1086         if (bms_is_empty(restrictinfo->left_relids) ||
1087                 bms_is_empty(restrictinfo->right_relids))
1088                 return false;
1089
1090         newleft = get_leftop(restrictinfo->clause);
1091         newright = get_rightop(restrictinfo->clause);
1092
1093         /*
1094          * Set cached pathkeys.  NB: it is okay to do this now because this
1095          * routine is only invoked while we are generating implied equalities.
1096          * Therefore, the equi_key_list is already complete and so we can
1097          * correctly determine canonical pathkeys.
1098          */
1099         cache_mergeclause_pathkeys(root, restrictinfo);
1100         /* If different, say "not redundant" (should never happen) */
1101         if (restrictinfo->left_pathkey != restrictinfo->right_pathkey)
1102                 return false;
1103
1104         /*
1105          * Scan existing quals to find those referencing same pathkeys. Usually
1106          * there will be few, if any, so build a list of just the interesting
1107          * ones.
1108          */
1109         oldquals = NIL;
1110         foreach(olditem, restrictlist)
1111         {
1112                 RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
1113
1114                 if (oldrinfo->mergejoinoperator != InvalidOid)
1115                 {
1116                         cache_mergeclause_pathkeys(root, oldrinfo);
1117                         if (restrictinfo->left_pathkey == oldrinfo->left_pathkey &&
1118                                 restrictinfo->right_pathkey == oldrinfo->right_pathkey)
1119                                 oldquals = lcons(oldrinfo, oldquals);
1120                 }
1121         }
1122         if (oldquals == NIL)
1123                 return false;
1124
1125         /*
1126          * Now, we want to develop a list of exprs that are known equal to the
1127          * left side of the new qual.  We traverse the old-quals list repeatedly
1128          * to transitively expand the exprs list.  If at any point we find we can
1129          * reach the right-side expr of the new qual, we are done.      We give up
1130          * when we can't expand the equalexprs list any more.
1131          */
1132         equalexprs = list_make1(newleft);
1133         do
1134         {
1135                 someadded = false;
1136                 /* cannot use foreach here because of possible list_delete */
1137                 olditem = list_head(oldquals);
1138                 while (olditem)
1139                 {
1140                         RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
1141                         Node       *oldleft = get_leftop(oldrinfo->clause);
1142                         Node       *oldright = get_rightop(oldrinfo->clause);
1143                         Node       *newguy = NULL;
1144
1145                         /* must advance olditem before list_delete possibly pfree's it */
1146                         olditem = lnext(olditem);
1147
1148                         if (list_member(equalexprs, oldleft))
1149                                 newguy = oldright;
1150                         else if (list_member(equalexprs, oldright))
1151                                 newguy = oldleft;
1152                         else
1153                                 continue;
1154                         if (equal(newguy, newright))
1155                                 return true;    /* we proved new clause is redundant */
1156                         equalexprs = lcons(newguy, equalexprs);
1157                         someadded = true;
1158
1159                         /*
1160                          * Remove this qual from list, since we don't need it anymore.
1161                          */
1162                         oldquals = list_delete_ptr(oldquals, oldrinfo);
1163                 }
1164         } while (someadded);
1165
1166         return false;                           /* it's not redundant */
1167 }
1168
1169
1170 /*****************************************************************************
1171  *
1172  *       CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
1173  *
1174  *****************************************************************************/
1175
1176 /*
1177  * check_mergejoinable
1178  *        If the restrictinfo's clause is mergejoinable, set the mergejoin
1179  *        info fields in the restrictinfo.
1180  *
1181  *        Currently, we support mergejoin for binary opclauses where
1182  *        the operator is a mergejoinable operator.  The arguments can be
1183  *        anything --- as long as there are no volatile functions in them.
1184  */
1185 static void
1186 check_mergejoinable(RestrictInfo *restrictinfo)
1187 {
1188         Expr       *clause = restrictinfo->clause;
1189         Oid                     opno,
1190                                 leftOp,
1191                                 rightOp;
1192
1193         if (!is_opclause(clause))
1194                 return;
1195         if (list_length(((OpExpr *) clause)->args) != 2)
1196                 return;
1197
1198         opno = ((OpExpr *) clause)->opno;
1199
1200         if (op_mergejoinable(opno,
1201                                                  &leftOp,
1202                                                  &rightOp) &&
1203                 !contain_volatile_functions((Node *) clause))
1204         {
1205                 restrictinfo->mergejoinoperator = opno;
1206                 restrictinfo->left_sortop = leftOp;
1207                 restrictinfo->right_sortop = rightOp;
1208         }
1209 }
1210
1211 /*
1212  * check_hashjoinable
1213  *        If the restrictinfo's clause is hashjoinable, set the hashjoin
1214  *        info fields in the restrictinfo.
1215  *
1216  *        Currently, we support hashjoin for binary opclauses where
1217  *        the operator is a hashjoinable operator.      The arguments can be
1218  *        anything --- as long as there are no volatile functions in them.
1219  */
1220 static void
1221 check_hashjoinable(RestrictInfo *restrictinfo)
1222 {
1223         Expr       *clause = restrictinfo->clause;
1224         Oid                     opno;
1225
1226         if (!is_opclause(clause))
1227                 return;
1228         if (list_length(((OpExpr *) clause)->args) != 2)
1229                 return;
1230
1231         opno = ((OpExpr *) clause)->opno;
1232
1233         if (op_hashjoinable(opno) &&
1234                 !contain_volatile_functions((Node *) clause))
1235                 restrictinfo->hashjoinoperator = opno;
1236 }