LCOV - code coverage report
Current view: top level - src/backend/optimizer/path - joinrels.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 525 552 95.1 %
Date: 2025-02-22 07:14:56 Functions: 20 20 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * joinrels.c
       4             :  *    Routines to determine which relations should be joined
       5             :  *
       6             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/optimizer/path/joinrels.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "miscadmin.h"
      18             : #include "optimizer/appendinfo.h"
      19             : #include "optimizer/joininfo.h"
      20             : #include "optimizer/pathnode.h"
      21             : #include "optimizer/paths.h"
      22             : #include "partitioning/partbounds.h"
      23             : #include "utils/memutils.h"
      24             : 
      25             : 
      26             : static void make_rels_by_clause_joins(PlannerInfo *root,
      27             :                                       RelOptInfo *old_rel,
      28             :                                       List *other_rels,
      29             :                                       int first_rel_idx);
      30             : static void make_rels_by_clauseless_joins(PlannerInfo *root,
      31             :                                           RelOptInfo *old_rel,
      32             :                                           List *other_rels);
      33             : static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
      34             : static bool has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel);
      35             : static bool restriction_is_constant_false(List *restrictlist,
      36             :                                           RelOptInfo *joinrel,
      37             :                                           bool only_pushed_down);
      38             : static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
      39             :                                         RelOptInfo *rel2, RelOptInfo *joinrel,
      40             :                                         SpecialJoinInfo *sjinfo, List *restrictlist);
      41             : static void try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1,
      42             :                                    RelOptInfo *rel2, RelOptInfo *joinrel,
      43             :                                    SpecialJoinInfo *parent_sjinfo,
      44             :                                    List *parent_restrictlist);
      45             : static SpecialJoinInfo *build_child_join_sjinfo(PlannerInfo *root,
      46             :                                                 SpecialJoinInfo *parent_sjinfo,
      47             :                                                 Relids left_relids, Relids right_relids);
      48             : static void free_child_join_sjinfo(SpecialJoinInfo *child_sjinfo,
      49             :                                    SpecialJoinInfo *parent_sjinfo);
      50             : static void compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
      51             :                                      RelOptInfo *rel2, RelOptInfo *joinrel,
      52             :                                      SpecialJoinInfo *parent_sjinfo,
      53             :                                      List **parts1, List **parts2);
      54             : static void get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
      55             :                                     RelOptInfo *rel1, RelOptInfo *rel2,
      56             :                                     List **parts1, List **parts2);
      57             : 
      58             : 
      59             : /*
      60             :  * join_search_one_level
      61             :  *    Consider ways to produce join relations containing exactly 'level'
      62             :  *    jointree items.  (This is one step of the dynamic-programming method
      63             :  *    embodied in standard_join_search.)  Join rel nodes for each feasible
      64             :  *    combination of lower-level rels are created and returned in a list.
      65             :  *    Implementation paths are created for each such joinrel, too.
      66             :  *
      67             :  * level: level of rels we want to make this time
      68             :  * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
      69             :  *
      70             :  * The result is returned in root->join_rel_level[level].
      71             :  */
      72             : void
      73      192648 : join_search_one_level(PlannerInfo *root, int level)
      74             : {
      75      192648 :     List      **joinrels = root->join_rel_level;
      76             :     ListCell   *r;
      77             :     int         k;
      78             : 
      79             :     Assert(joinrels[level] == NIL);
      80             : 
      81             :     /* Set join_cur_level so that new joinrels are added to proper list */
      82      192648 :     root->join_cur_level = level;
      83             : 
      84             :     /*
      85             :      * First, consider left-sided and right-sided plans, in which rels of
      86             :      * exactly level-1 member relations are joined against initial relations.
      87             :      * We prefer to join using join clauses, but if we find a rel of level-1
      88             :      * members that has no join clauses, we will generate Cartesian-product
      89             :      * joins against all initial rels not already contained in it.
      90             :      */
      91      723096 :     foreach(r, joinrels[level - 1])
      92             :     {
      93      530448 :         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
      94             : 
      95      561110 :         if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
      96       30662 :             has_join_restriction(root, old_rel))
      97      520182 :         {
      98             :             int         first_rel;
      99             : 
     100             :             /*
     101             :              * There are join clauses or join order restrictions relevant to
     102             :              * this rel, so consider joins between this rel and (only) those
     103             :              * initial rels it is linked to by a clause or restriction.
     104             :              *
     105             :              * At level 2 this condition is symmetric, so there is no need to
     106             :              * look at initial rels before this one in the list; we already
     107             :              * considered such joins when we were at the earlier rel.  (The
     108             :              * mirror-image joins are handled automatically by make_join_rel.)
     109             :              * In later passes (level > 2), we join rels of the previous level
     110             :              * to each initial rel they don't already include but have a join
     111             :              * clause or restriction with.
     112             :              */
     113      520182 :             if (level == 2)     /* consider remaining initial rels */
     114      307888 :                 first_rel = foreach_current_index(r) + 1;
     115             :             else
     116      212294 :                 first_rel = 0;
     117             : 
     118      520182 :             make_rels_by_clause_joins(root, old_rel, joinrels[1], first_rel);
     119             :         }
     120             :         else
     121             :         {
     122             :             /*
     123             :              * Oops, we have a relation that is not joined to any other
     124             :              * relation, either directly or by join-order restrictions.
     125             :              * Cartesian product time.
     126             :              *
     127             :              * We consider a cartesian product with each not-already-included
     128             :              * initial rel, whether it has other join clauses or not.  At
     129             :              * level 2, if there are two or more clauseless initial rels, we
     130             :              * will redundantly consider joining them in both directions; but
     131             :              * such cases aren't common enough to justify adding complexity to
     132             :              * avoid the duplicated effort.
     133             :              */
     134       10266 :             make_rels_by_clauseless_joins(root,
     135             :                                           old_rel,
     136       10266 :                                           joinrels[1]);
     137             :         }
     138             :     }
     139             : 
     140             :     /*
     141             :      * Now, consider "bushy plans" in which relations of k initial rels are
     142             :      * joined to relations of level-k initial rels, for 2 <= k <= level-2.
     143             :      *
     144             :      * We only consider bushy-plan joins for pairs of rels where there is a
     145             :      * suitable join clause (or join order restriction), in order to avoid
     146             :      * unreasonable growth of planning time.
     147             :      */
     148      192648 :     for (k = 2;; k++)
     149       22988 :     {
     150      215636 :         int         other_level = level - k;
     151             : 
     152             :         /*
     153             :          * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
     154             :          * need to go as far as the halfway point.
     155             :          */
     156      215636 :         if (k > other_level)
     157      192648 :             break;
     158             : 
     159      116684 :         foreach(r, joinrels[k])
     160             :         {
     161       93696 :             RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
     162             :             int         first_rel;
     163             :             ListCell   *r2;
     164             : 
     165             :             /*
     166             :              * We can ignore relations without join clauses here, unless they
     167             :              * participate in join-order restrictions --- then we might have
     168             :              * to force a bushy join plan.
     169             :              */
     170       93696 :             if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
     171         312 :                 !has_join_restriction(root, old_rel))
     172         216 :                 continue;
     173             : 
     174       93480 :             if (k == other_level)   /* only consider remaining rels */
     175       79724 :                 first_rel = foreach_current_index(r) + 1;
     176             :             else
     177       13756 :                 first_rel = 0;
     178             : 
     179      319832 :             for_each_from(r2, joinrels[other_level], first_rel)
     180             :             {
     181      226352 :                 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
     182             : 
     183      226352 :                 if (!bms_overlap(old_rel->relids, new_rel->relids))
     184             :                 {
     185             :                     /*
     186             :                      * OK, we can build a rel of the right level from this
     187             :                      * pair of rels.  Do so if there is at least one relevant
     188             :                      * join clause or join order restriction.
     189             :                      */
     190       31276 :                     if (have_relevant_joinclause(root, old_rel, new_rel) ||
     191        1118 :                         have_join_order_restriction(root, old_rel, new_rel))
     192             :                     {
     193       29094 :                         (void) make_join_rel(root, old_rel, new_rel);
     194             :                     }
     195             :                 }
     196             :             }
     197             :         }
     198             :     }
     199             : 
     200             :     /*----------
     201             :      * Last-ditch effort: if we failed to find any usable joins so far, force
     202             :      * a set of cartesian-product joins to be generated.  This handles the
     203             :      * special case where all the available rels have join clauses but we
     204             :      * cannot use any of those clauses yet.  This can only happen when we are
     205             :      * considering a join sub-problem (a sub-joinlist) and all the rels in the
     206             :      * sub-problem have only join clauses with rels outside the sub-problem.
     207             :      * An example is
     208             :      *
     209             :      *      SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
     210             :      *      WHERE a.w = c.x and b.y = d.z;
     211             :      *
     212             :      * If the "a INNER JOIN b" sub-problem does not get flattened into the
     213             :      * upper level, we must be willing to make a cartesian join of a and b;
     214             :      * but the code above will not have done so, because it thought that both
     215             :      * a and b have joinclauses.  We consider only left-sided and right-sided
     216             :      * cartesian joins in this case (no bushy).
     217             :      *----------
     218             :      */
     219      192648 :     if (joinrels[level] == NIL)
     220             :     {
     221             :         /*
     222             :          * This loop is just like the first one, except we always call
     223             :          * make_rels_by_clauseless_joins().
     224             :          */
     225          54 :         foreach(r, joinrels[level - 1])
     226             :         {
     227          36 :             RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
     228             : 
     229          36 :             make_rels_by_clauseless_joins(root,
     230             :                                           old_rel,
     231          36 :                                           joinrels[1]);
     232             :         }
     233             : 
     234             :         /*----------
     235             :          * When special joins are involved, there may be no legal way
     236             :          * to make an N-way join for some values of N.  For example consider
     237             :          *
     238             :          * SELECT ... FROM t1 WHERE
     239             :          *   x IN (SELECT ... FROM t2,t3 WHERE ...) AND
     240             :          *   y IN (SELECT ... FROM t4,t5 WHERE ...)
     241             :          *
     242             :          * We will flatten this query to a 5-way join problem, but there are
     243             :          * no 4-way joins that join_is_legal() will consider legal.  We have
     244             :          * to accept failure at level 4 and go on to discover a workable
     245             :          * bushy plan at level 5.
     246             :          *
     247             :          * However, if there are no special joins and no lateral references
     248             :          * then join_is_legal() should never fail, and so the following sanity
     249             :          * check is useful.
     250             :          *----------
     251             :          */
     252          18 :         if (joinrels[level] == NIL &&
     253           6 :             root->join_info_list == NIL &&
     254           0 :             !root->hasLateralRTEs)
     255           0 :             elog(ERROR, "failed to build any %d-way joins", level);
     256             :     }
     257      192648 : }
     258             : 
     259             : /*
     260             :  * make_rels_by_clause_joins
     261             :  *    Build joins between the given relation 'old_rel' and other relations
     262             :  *    that participate in join clauses that 'old_rel' also participates in
     263             :  *    (or participate in join-order restrictions with it).
     264             :  *    The join rels are returned in root->join_rel_level[join_cur_level].
     265             :  *
     266             :  * Note: at levels above 2 we will generate the same joined relation in
     267             :  * multiple ways --- for example (a join b) join c is the same RelOptInfo as
     268             :  * (b join c) join a, though the second case will add a different set of Paths
     269             :  * to it.  This is the reason for using the join_rel_level mechanism, which
     270             :  * automatically ensures that each new joinrel is only added to the list once.
     271             :  *
     272             :  * 'old_rel' is the relation entry for the relation to be joined
     273             :  * 'other_rels': a list containing the other rels to be considered for joining
     274             :  * 'first_rel_idx': the first rel to be considered in 'other_rels'
     275             :  *
     276             :  * Currently, this is only used with initial rels in other_rels, but it
     277             :  * will work for joining to joinrels too.
     278             :  */
     279             : static void
     280      520182 : make_rels_by_clause_joins(PlannerInfo *root,
     281             :                           RelOptInfo *old_rel,
     282             :                           List *other_rels,
     283             :                           int first_rel_idx)
     284             : {
     285             :     ListCell   *l;
     286             : 
     287     1634378 :     for_each_from(l, other_rels, first_rel_idx)
     288             :     {
     289     1114196 :         RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
     290             : 
     291     1717400 :         if (!bms_overlap(old_rel->relids, other_rel->relids) &&
     292      705696 :             (have_relevant_joinclause(root, old_rel, other_rel) ||
     293      102492 :              have_join_order_restriction(root, old_rel, other_rel)))
     294             :         {
     295      512484 :             (void) make_join_rel(root, old_rel, other_rel);
     296             :         }
     297             :     }
     298      520182 : }
     299             : 
     300             : /*
     301             :  * make_rels_by_clauseless_joins
     302             :  *    Given a relation 'old_rel' and a list of other relations
     303             :  *    'other_rels', create a join relation between 'old_rel' and each
     304             :  *    member of 'other_rels' that isn't already included in 'old_rel'.
     305             :  *    The join rels are returned in root->join_rel_level[join_cur_level].
     306             :  *
     307             :  * 'old_rel' is the relation entry for the relation to be joined
     308             :  * 'other_rels': a list containing the other rels to be considered for joining
     309             :  *
     310             :  * Currently, this is only used with initial rels in other_rels, but it would
     311             :  * work for joining to joinrels too.
     312             :  */
     313             : static void
     314       10302 : make_rels_by_clauseless_joins(PlannerInfo *root,
     315             :                               RelOptInfo *old_rel,
     316             :                               List *other_rels)
     317             : {
     318             :     ListCell   *l;
     319             : 
     320       32634 :     foreach(l, other_rels)
     321             :     {
     322       22332 :         RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
     323             : 
     324       22332 :         if (!bms_overlap(other_rel->relids, old_rel->relids))
     325             :         {
     326       11024 :             (void) make_join_rel(root, old_rel, other_rel);
     327             :         }
     328             :     }
     329       10302 : }
     330             : 
     331             : 
     332             : /*
     333             :  * join_is_legal
     334             :  *     Determine whether a proposed join is legal given the query's
     335             :  *     join order constraints; and if it is, determine the join type.
     336             :  *
     337             :  * Caller must supply not only the two rels, but the union of their relids.
     338             :  * (We could simplify the API by computing joinrelids locally, but this
     339             :  * would be redundant work in the normal path through make_join_rel.
     340             :  * Note that this value does NOT include the RT index of any outer join that
     341             :  * might need to be performed here, so it's not the canonical identifier
     342             :  * of the join relation.)
     343             :  *
     344             :  * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
     345             :  * else it's set to point to the associated SpecialJoinInfo node.  Also,
     346             :  * *reversed_p is set true if the given relations need to be swapped to
     347             :  * match the SpecialJoinInfo node.
     348             :  */
     349             : static bool
     350      557720 : join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
     351             :               Relids joinrelids,
     352             :               SpecialJoinInfo **sjinfo_p, bool *reversed_p)
     353             : {
     354             :     SpecialJoinInfo *match_sjinfo;
     355             :     bool        reversed;
     356             :     bool        unique_ified;
     357             :     bool        must_be_leftjoin;
     358             :     ListCell   *l;
     359             : 
     360             :     /*
     361             :      * Ensure output params are set on failure return.  This is just to
     362             :      * suppress uninitialized-variable warnings from overly anal compilers.
     363             :      */
     364      557720 :     *sjinfo_p = NULL;
     365      557720 :     *reversed_p = false;
     366             : 
     367             :     /*
     368             :      * If we have any special joins, the proposed join might be illegal; and
     369             :      * in any case we have to determine its join type.  Scan the join info
     370             :      * list for matches and conflicts.
     371             :      */
     372      557720 :     match_sjinfo = NULL;
     373      557720 :     reversed = false;
     374      557720 :     unique_ified = false;
     375      557720 :     must_be_leftjoin = false;
     376             : 
     377      863716 :     foreach(l, root->join_info_list)
     378             :     {
     379      315120 :         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
     380             : 
     381             :         /*
     382             :          * This special join is not relevant unless its RHS overlaps the
     383             :          * proposed join.  (Check this first as a fast path for dismissing
     384             :          * most irrelevant SJs quickly.)
     385             :          */
     386      315120 :         if (!bms_overlap(sjinfo->min_righthand, joinrelids))
     387      105304 :             continue;
     388             : 
     389             :         /*
     390             :          * Also, not relevant if proposed join is fully contained within RHS
     391             :          * (ie, we're still building up the RHS).
     392             :          */
     393      209816 :         if (bms_is_subset(joinrelids, sjinfo->min_righthand))
     394        5220 :             continue;
     395             : 
     396             :         /*
     397             :          * Also, not relevant if SJ is already done within either input.
     398             :          */
     399      381076 :         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
     400      176480 :             bms_is_subset(sjinfo->min_righthand, rel1->relids))
     401       85096 :             continue;
     402      137404 :         if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
     403       17904 :             bms_is_subset(sjinfo->min_righthand, rel2->relids))
     404        8966 :             continue;
     405             : 
     406             :         /*
     407             :          * If it's a semijoin and we already joined the RHS to any other rels
     408             :          * within either input, then we must have unique-ified the RHS at that
     409             :          * point (see below).  Therefore the semijoin is no longer relevant in
     410             :          * this join path.
     411             :          */
     412      110534 :         if (sjinfo->jointype == JOIN_SEMI)
     413             :         {
     414        7816 :             if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
     415        1552 :                 !bms_equal(sjinfo->syn_righthand, rel1->relids))
     416         618 :                 continue;
     417        7198 :             if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
     418        4128 :                 !bms_equal(sjinfo->syn_righthand, rel2->relids))
     419         214 :                 continue;
     420             :         }
     421             : 
     422             :         /*
     423             :          * If one input contains min_lefthand and the other contains
     424             :          * min_righthand, then we can perform the SJ at this join.
     425             :          *
     426             :          * Reject if we get matches to more than one SJ; that implies we're
     427             :          * considering something that's not really valid.
     428             :          */
     429      200934 :         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
     430       91232 :             bms_is_subset(sjinfo->min_righthand, rel2->relids))
     431             :         {
     432       84720 :             if (match_sjinfo)
     433        9124 :                 return false;   /* invalid join path */
     434       84720 :             match_sjinfo = sjinfo;
     435       84720 :             reversed = false;
     436             :         }
     437       33582 :         else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
     438        8600 :                  bms_is_subset(sjinfo->min_righthand, rel1->relids))
     439             :         {
     440        7370 :             if (match_sjinfo)
     441           0 :                 return false;   /* invalid join path */
     442        7370 :             match_sjinfo = sjinfo;
     443        7370 :             reversed = true;
     444             :         }
     445       20590 :         else if (sjinfo->jointype == JOIN_SEMI &&
     446        3530 :                  bms_equal(sjinfo->syn_righthand, rel2->relids) &&
     447         552 :                  create_unique_path(root, rel2, rel2->cheapest_total_path,
     448             :                                     sjinfo) != NULL)
     449             :         {
     450             :             /*----------
     451             :              * For a semijoin, we can join the RHS to anything else by
     452             :              * unique-ifying the RHS (if the RHS can be unique-ified).
     453             :              * We will only get here if we have the full RHS but less
     454             :              * than min_lefthand on the LHS.
     455             :              *
     456             :              * The reason to consider such a join path is exemplified by
     457             :              *  SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
     458             :              * If we insist on doing this as a semijoin we will first have
     459             :              * to form the cartesian product of A*B.  But if we unique-ify
     460             :              * C then the semijoin becomes a plain innerjoin and we can join
     461             :              * in any order, eg C to A and then to B.  When C is much smaller
     462             :              * than A and B this can be a huge win.  So we allow C to be
     463             :              * joined to just A or just B here, and then make_join_rel has
     464             :              * to handle the case properly.
     465             :              *
     466             :              * Note that actually we'll allow unique-ified C to be joined to
     467             :              * some other relation D here, too.  That is legal, if usually not
     468             :              * very sane, and this routine is only concerned with legality not
     469             :              * with whether the join is good strategy.
     470             :              *----------
     471             :              */
     472         546 :             if (match_sjinfo)
     473          84 :                 return false;   /* invalid join path */
     474         462 :             match_sjinfo = sjinfo;
     475         462 :             reversed = false;
     476         462 :             unique_ified = true;
     477             :         }
     478       19498 :         else if (sjinfo->jointype == JOIN_SEMI &&
     479        2722 :                  bms_equal(sjinfo->syn_righthand, rel1->relids) &&
     480         290 :                  create_unique_path(root, rel1, rel1->cheapest_total_path,
     481             :                                     sjinfo) != NULL)
     482             :         {
     483             :             /* Reversed semijoin case */
     484         290 :             if (match_sjinfo)
     485          78 :                 return false;   /* invalid join path */
     486         212 :             match_sjinfo = sjinfo;
     487         212 :             reversed = true;
     488         212 :             unique_ified = true;
     489             :         }
     490             :         else
     491             :         {
     492             :             /*
     493             :              * Otherwise, the proposed join overlaps the RHS but isn't a valid
     494             :              * implementation of this SJ.  But don't panic quite yet: the RHS
     495             :              * violation might have occurred previously, in one or both input
     496             :              * relations, in which case we must have previously decided that
     497             :              * it was OK to commute some other SJ with this one.  If we need
     498             :              * to perform this join to finish building up the RHS, rejecting
     499             :              * it could lead to not finding any plan at all.  (This can occur
     500             :              * because of the heuristics elsewhere in this file that postpone
     501             :              * clauseless joins: we might not consider doing a clauseless join
     502             :              * within the RHS until after we've performed other, validly
     503             :              * commutable SJs with one or both sides of the clauseless join.)
     504             :              * This consideration boils down to the rule that if both inputs
     505             :              * overlap the RHS, we can allow the join --- they are either
     506             :              * fully within the RHS, or represent previously-allowed joins to
     507             :              * rels outside it.
     508             :              */
     509       24438 :             if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
     510        7662 :                 bms_overlap(rel2->relids, sjinfo->min_righthand))
     511         180 :                 continue;       /* assume valid previous violation of RHS */
     512             : 
     513             :             /*
     514             :              * The proposed join could still be legal, but only if we're
     515             :              * allowed to associate it into the RHS of this SJ.  That means
     516             :              * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
     517             :              * not FULL) and the proposed join must not overlap the LHS.
     518             :              */
     519       30918 :             if (sjinfo->jointype != JOIN_LEFT ||
     520       14322 :                 bms_overlap(joinrelids, sjinfo->min_lefthand))
     521        8962 :                 return false;   /* invalid join path */
     522             : 
     523             :             /*
     524             :              * To be valid, the proposed join must be a LEFT join; otherwise
     525             :              * it can't associate into this SJ's RHS.  But we may not yet have
     526             :              * found the SpecialJoinInfo matching the proposed join, so we
     527             :              * can't test that yet.  Remember the requirement for later.
     528             :              */
     529        7634 :             must_be_leftjoin = true;
     530             :         }
     531             :     }
     532             : 
     533             :     /*
     534             :      * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
     535             :      * proposed join can't associate into an SJ's RHS.
     536             :      *
     537             :      * Also, fail if the proposed join's predicate isn't strict; we're
     538             :      * essentially checking to see if we can apply outer-join identity 3, and
     539             :      * that's a requirement.  (This check may be redundant with checks in
     540             :      * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
     541             :      */
     542      548596 :     if (must_be_leftjoin &&
     543        4692 :         (match_sjinfo == NULL ||
     544        4692 :          match_sjinfo->jointype != JOIN_LEFT ||
     545        4692 :          !match_sjinfo->lhs_strict))
     546        1486 :         return false;           /* invalid join path */
     547             : 
     548             :     /*
     549             :      * We also have to check for constraints imposed by LATERAL references.
     550             :      */
     551      547110 :     if (root->hasLateralRTEs)
     552             :     {
     553             :         bool        lateral_fwd;
     554             :         bool        lateral_rev;
     555             :         Relids      join_lateral_rels;
     556             : 
     557             :         /*
     558             :          * The proposed rels could each contain lateral references to the
     559             :          * other, in which case the join is impossible.  If there are lateral
     560             :          * references in just one direction, then the join has to be done with
     561             :          * a nestloop with the lateral referencer on the inside.  If the join
     562             :          * matches an SJ that cannot be implemented by such a nestloop, the
     563             :          * join is impossible.
     564             :          *
     565             :          * Also, if the lateral reference is only indirect, we should reject
     566             :          * the join; whatever rel(s) the reference chain goes through must be
     567             :          * joined to first.
     568             :          *
     569             :          * Another case that might keep us from building a valid plan is the
     570             :          * implementation restriction described by have_dangerous_phv().
     571             :          */
     572       16804 :         lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
     573       16804 :         lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
     574       16804 :         if (lateral_fwd && lateral_rev)
     575          18 :             return false;       /* have lateral refs in both directions */
     576       16786 :         if (lateral_fwd)
     577             :         {
     578             :             /* has to be implemented as nestloop with rel1 on left */
     579       10580 :             if (match_sjinfo &&
     580         390 :                 (reversed ||
     581         378 :                  unique_ified ||
     582         378 :                  match_sjinfo->jointype == JOIN_FULL))
     583          12 :                 return false;   /* not implementable as nestloop */
     584             :             /* check there is a direct reference from rel2 to rel1 */
     585       10568 :             if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
     586          42 :                 return false;   /* only indirect refs, so reject */
     587             :             /* check we won't have a dangerous PHV */
     588       10526 :             if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
     589          72 :                 return false;   /* might be unable to handle required PHV */
     590             :         }
     591        6206 :         else if (lateral_rev)
     592             :         {
     593             :             /* has to be implemented as nestloop with rel2 on left */
     594        1176 :             if (match_sjinfo &&
     595          72 :                 (!reversed ||
     596          72 :                  unique_ified ||
     597          72 :                  match_sjinfo->jointype == JOIN_FULL))
     598           0 :                 return false;   /* not implementable as nestloop */
     599             :             /* check there is a direct reference from rel1 to rel2 */
     600        1176 :             if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
     601           0 :                 return false;   /* only indirect refs, so reject */
     602             :             /* check we won't have a dangerous PHV */
     603        1176 :             if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
     604          84 :                 return false;   /* might be unable to handle required PHV */
     605             :         }
     606             : 
     607             :         /*
     608             :          * LATERAL references could also cause problems later on if we accept
     609             :          * this join: if the join's minimum parameterization includes any rels
     610             :          * that would have to be on the inside of an outer join with this join
     611             :          * rel, then it's never going to be possible to build the complete
     612             :          * query using this join.  We should reject this join not only because
     613             :          * it'll save work, but because if we don't, the clauseless-join
     614             :          * heuristics might think that legality of this join means that some
     615             :          * other join rel need not be formed, and that could lead to failure
     616             :          * to find any plan at all.  We have to consider not only rels that
     617             :          * are directly on the inner side of an OJ with the joinrel, but also
     618             :          * ones that are indirectly so, so search to find all such rels.
     619             :          */
     620       16576 :         join_lateral_rels = min_join_parameterization(root, joinrelids,
     621             :                                                       rel1, rel2);
     622       16576 :         if (join_lateral_rels)
     623             :         {
     624        1684 :             Relids      join_plus_rhs = bms_copy(joinrelids);
     625             :             bool        more;
     626             : 
     627             :             do
     628             :             {
     629        2080 :                 more = false;
     630        3982 :                 foreach(l, root->join_info_list)
     631             :                 {
     632        1902 :                     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
     633             : 
     634             :                     /* ignore full joins --- their ordering is predetermined */
     635        1902 :                     if (sjinfo->jointype == JOIN_FULL)
     636          18 :                         continue;
     637             : 
     638        1884 :                     if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
     639        1590 :                         !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
     640             :                     {
     641         546 :                         join_plus_rhs = bms_add_members(join_plus_rhs,
     642         546 :                                                         sjinfo->min_righthand);
     643         546 :                         more = true;
     644             :                     }
     645             :                 }
     646        2080 :             } while (more);
     647        1684 :             if (bms_overlap(join_plus_rhs, join_lateral_rels))
     648         312 :                 return false;   /* will not be able to join to some RHS rel */
     649             :         }
     650             :     }
     651             : 
     652             :     /* Otherwise, it's a valid join */
     653      546570 :     *sjinfo_p = match_sjinfo;
     654      546570 :     *reversed_p = reversed;
     655      546570 :     return true;
     656             : }
     657             : 
     658             : /*
     659             :  * init_dummy_sjinfo
     660             :  *    Populate the given SpecialJoinInfo for a plain inner join between the
     661             :  *    left and right relations specified by left_relids and right_relids
     662             :  *    respectively.
     663             :  *
     664             :  * Normally, an inner join does not have a SpecialJoinInfo node associated with
     665             :  * it. But some functions involved in join planning require one containing at
     666             :  * least the information of which relations are being joined.  So we initialize
     667             :  * that information here.
     668             :  */
     669             : void
     670     1791548 : init_dummy_sjinfo(SpecialJoinInfo *sjinfo, Relids left_relids,
     671             :                   Relids right_relids)
     672             : {
     673     1791548 :     sjinfo->type = T_SpecialJoinInfo;
     674     1791548 :     sjinfo->min_lefthand = left_relids;
     675     1791548 :     sjinfo->min_righthand = right_relids;
     676     1791548 :     sjinfo->syn_lefthand = left_relids;
     677     1791548 :     sjinfo->syn_righthand = right_relids;
     678     1791548 :     sjinfo->jointype = JOIN_INNER;
     679     1791548 :     sjinfo->ojrelid = 0;
     680     1791548 :     sjinfo->commute_above_l = NULL;
     681     1791548 :     sjinfo->commute_above_r = NULL;
     682     1791548 :     sjinfo->commute_below_l = NULL;
     683     1791548 :     sjinfo->commute_below_r = NULL;
     684             :     /* we don't bother trying to make the remaining fields valid */
     685     1791548 :     sjinfo->lhs_strict = false;
     686     1791548 :     sjinfo->semi_can_btree = false;
     687     1791548 :     sjinfo->semi_can_hash = false;
     688     1791548 :     sjinfo->semi_operators = NIL;
     689     1791548 :     sjinfo->semi_rhs_exprs = NIL;
     690     1791548 : }
     691             : 
     692             : /*
     693             :  * make_join_rel
     694             :  *     Find or create a join RelOptInfo that represents the join of
     695             :  *     the two given rels, and add to it path information for paths
     696             :  *     created with the two rels as outer and inner rel.
     697             :  *     (The join rel may already contain paths generated from other
     698             :  *     pairs of rels that add up to the same set of base rels.)
     699             :  *
     700             :  * NB: will return NULL if attempted join is not valid.  This can happen
     701             :  * when working with outer joins, or with IN or EXISTS clauses that have been
     702             :  * turned into joins.
     703             :  */
     704             : RelOptInfo *
     705      557354 : make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
     706             : {
     707             :     Relids      joinrelids;
     708             :     SpecialJoinInfo *sjinfo;
     709             :     bool        reversed;
     710      557354 :     List       *pushed_down_joins = NIL;
     711             :     SpecialJoinInfo sjinfo_data;
     712             :     RelOptInfo *joinrel;
     713             :     List       *restrictlist;
     714             : 
     715             :     /* We should never try to join two overlapping sets of rels. */
     716             :     Assert(!bms_overlap(rel1->relids, rel2->relids));
     717             : 
     718             :     /* Construct Relids set that identifies the joinrel (without OJ as yet). */
     719      557354 :     joinrelids = bms_union(rel1->relids, rel2->relids);
     720             : 
     721             :     /* Check validity and determine join type. */
     722      557354 :     if (!join_is_legal(root, rel1, rel2, joinrelids,
     723             :                        &sjinfo, &reversed))
     724             :     {
     725             :         /* invalid join path */
     726       10994 :         bms_free(joinrelids);
     727       10994 :         return NULL;
     728             :     }
     729             : 
     730             :     /*
     731             :      * Add outer join relid(s) to form the canonical relids.  Any added outer
     732             :      * joins besides sjinfo itself are appended to pushed_down_joins.
     733             :      */
     734      546360 :     joinrelids = add_outer_joins_to_relids(root, joinrelids, sjinfo,
     735             :                                            &pushed_down_joins);
     736             : 
     737             :     /* Swap rels if needed to match the join info. */
     738      546360 :     if (reversed)
     739             :     {
     740        7426 :         RelOptInfo *trel = rel1;
     741             : 
     742        7426 :         rel1 = rel2;
     743        7426 :         rel2 = trel;
     744             :     }
     745             : 
     746             :     /*
     747             :      * If it's a plain inner join, then we won't have found anything in
     748             :      * join_info_list.  Make up a SpecialJoinInfo so that selectivity
     749             :      * estimation functions will know what's being joined.
     750             :      */
     751      546360 :     if (sjinfo == NULL)
     752             :     {
     753      454122 :         sjinfo = &sjinfo_data;
     754      454122 :         init_dummy_sjinfo(sjinfo, rel1->relids, rel2->relids);
     755             :     }
     756             : 
     757             :     /*
     758             :      * Find or build the join RelOptInfo, and compute the restrictlist that
     759             :      * goes with this particular joining.
     760             :      */
     761      546360 :     joinrel = build_join_rel(root, joinrelids, rel1, rel2,
     762             :                              sjinfo, pushed_down_joins,
     763             :                              &restrictlist);
     764             : 
     765             :     /*
     766             :      * If we've already proven this join is empty, we needn't consider any
     767             :      * more paths for it.
     768             :      */
     769      546360 :     if (is_dummy_rel(joinrel))
     770             :     {
     771         456 :         bms_free(joinrelids);
     772         456 :         return joinrel;
     773             :     }
     774             : 
     775             :     /* Add paths to the join relation. */
     776      545904 :     populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
     777             :                                 restrictlist);
     778             : 
     779      545904 :     bms_free(joinrelids);
     780             : 
     781      545904 :     return joinrel;
     782             : }
     783             : 
     784             : /*
     785             :  * add_outer_joins_to_relids
     786             :  *    Add relids to input_relids to represent any outer joins that will be
     787             :  *    calculated at this join.
     788             :  *
     789             :  * input_relids is the union of the relid sets of the two input relations.
     790             :  * Note that we modify this in-place and return it; caller must bms_copy()
     791             :  * it first, if a separate value is desired.
     792             :  *
     793             :  * sjinfo represents the join being performed.
     794             :  *
     795             :  * If the current join completes the calculation of any outer joins that
     796             :  * have been pushed down per outer-join identity 3, those relids will be
     797             :  * added to the result along with sjinfo's own relid.  If pushed_down_joins
     798             :  * is not NULL, then also the SpecialJoinInfos for such added outer joins will
     799             :  * be appended to *pushed_down_joins (so caller must initialize it to NIL).
     800             :  */
     801             : Relids
     802      553420 : add_outer_joins_to_relids(PlannerInfo *root, Relids input_relids,
     803             :                           SpecialJoinInfo *sjinfo,
     804             :                           List **pushed_down_joins)
     805             : {
     806             :     /* Nothing to do if this isn't an outer join with an assigned relid. */
     807      553420 :     if (sjinfo == NULL || sjinfo->ojrelid == 0)
     808      468854 :         return input_relids;
     809             : 
     810             :     /*
     811             :      * If it's not a left join, we have no rules that would permit executing
     812             :      * it in non-syntactic order, so just form the syntactic relid set.  (This
     813             :      * is just a quick-exit test; we'd come to the same conclusion anyway,
     814             :      * since its commute_below_l and commute_above_l sets must be empty.)
     815             :      */
     816       84566 :     if (sjinfo->jointype != JOIN_LEFT)
     817        2122 :         return bms_add_member(input_relids, sjinfo->ojrelid);
     818             : 
     819             :     /*
     820             :      * We cannot add the OJ relid if this join has been pushed into the RHS of
     821             :      * a syntactically-lower left join per OJ identity 3.  (If it has, then we
     822             :      * cannot claim that its outputs represent the final state of its RHS.)
     823             :      * There will not be any other OJs that can be added either, so we're
     824             :      * done.
     825             :      */
     826       82444 :     if (!bms_is_subset(sjinfo->commute_below_l, input_relids))
     827        4404 :         return input_relids;
     828             : 
     829             :     /* OK to add OJ's own relid */
     830       78040 :     input_relids = bms_add_member(input_relids, sjinfo->ojrelid);
     831             : 
     832             :     /*
     833             :      * Contrariwise, if we are now forming the final result of such a commuted
     834             :      * pair of OJs, it's time to add the relid(s) of the pushed-down join(s).
     835             :      * We can skip this if this join was never a candidate to be pushed up.
     836             :      */
     837       78040 :     if (sjinfo->commute_above_l)
     838             :     {
     839       14300 :         Relids      commute_above_rels = bms_copy(sjinfo->commute_above_l);
     840             :         ListCell   *lc;
     841             : 
     842             :         /*
     843             :          * The current join could complete the nulling of more than one
     844             :          * pushed-down join, so we have to examine all the SpecialJoinInfos.
     845             :          * Because join_info_list was built in bottom-up order, it's
     846             :          * sufficient to traverse it once: an ojrelid we add in one loop
     847             :          * iteration would not have affected decisions of earlier iterations.
     848             :          */
     849       45912 :         foreach(lc, root->join_info_list)
     850             :         {
     851       31612 :             SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
     852             : 
     853       31612 :             if (othersj == sjinfo ||
     854       17312 :                 othersj->ojrelid == 0 || othersj->jointype != JOIN_LEFT)
     855       14312 :                 continue;       /* definitely not interesting */
     856             : 
     857       17300 :             if (!bms_is_member(othersj->ojrelid, commute_above_rels))
     858        2876 :                 continue;
     859             : 
     860             :             /* Add it if not already present but conditions now satisfied */
     861       28848 :             if (!bms_is_member(othersj->ojrelid, input_relids) &&
     862       28824 :                 bms_is_subset(othersj->min_lefthand, input_relids) &&
     863       21676 :                 bms_is_subset(othersj->min_righthand, input_relids) &&
     864        7276 :                 bms_is_subset(othersj->commute_below_l, input_relids))
     865             :             {
     866        7240 :                 input_relids = bms_add_member(input_relids, othersj->ojrelid);
     867             :                 /* report such pushed down outer joins, if asked */
     868        7240 :                 if (pushed_down_joins != NULL)
     869        7240 :                     *pushed_down_joins = lappend(*pushed_down_joins, othersj);
     870             : 
     871             :                 /*
     872             :                  * We must also check any joins that othersj potentially
     873             :                  * commutes with.  They likewise must appear later in
     874             :                  * join_info_list than othersj itself, so we can visit them
     875             :                  * later in this loop.
     876             :                  */
     877        7240 :                 commute_above_rels = bms_add_members(commute_above_rels,
     878        7240 :                                                      othersj->commute_above_l);
     879             :             }
     880             :         }
     881             :     }
     882             : 
     883       78040 :     return input_relids;
     884             : }
     885             : 
     886             : /*
     887             :  * populate_joinrel_with_paths
     888             :  *    Add paths to the given joinrel for given pair of joining relations. The
     889             :  *    SpecialJoinInfo provides details about the join and the restrictlist
     890             :  *    contains the join clauses and the other clauses applicable for given pair
     891             :  *    of the joining relations.
     892             :  */
     893             : static void
     894      551258 : populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
     895             :                             RelOptInfo *rel2, RelOptInfo *joinrel,
     896             :                             SpecialJoinInfo *sjinfo, List *restrictlist)
     897             : {
     898             :     /*
     899             :      * Consider paths using each rel as both outer and inner.  Depending on
     900             :      * the join type, a provably empty outer or inner rel might mean the join
     901             :      * is provably empty too; in which case throw away any previously computed
     902             :      * paths and mark the join as dummy.  (We do it this way since it's
     903             :      * conceivable that dummy-ness of a multi-element join might only be
     904             :      * noticeable for certain construction paths.)
     905             :      *
     906             :      * Also, a provably constant-false join restriction typically means that
     907             :      * we can skip evaluating one or both sides of the join.  We do this by
     908             :      * marking the appropriate rel as dummy.  For outer joins, a
     909             :      * constant-false restriction that is pushed down still means the whole
     910             :      * join is dummy, while a non-pushed-down one means that no inner rows
     911             :      * will join so we can treat the inner rel as dummy.
     912             :      *
     913             :      * We need only consider the jointypes that appear in join_info_list, plus
     914             :      * JOIN_INNER.
     915             :      */
     916      551258 :     switch (sjinfo->jointype)
     917             :     {
     918      455942 :         case JOIN_INNER:
     919      911866 :             if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
     920      455924 :                 restriction_is_constant_false(restrictlist, joinrel, false))
     921             :             {
     922         198 :                 mark_dummy_rel(joinrel);
     923         198 :                 break;
     924             :             }
     925      455744 :             add_paths_to_joinrel(root, joinrel, rel1, rel2,
     926             :                                  JOIN_INNER, sjinfo,
     927             :                                  restrictlist);
     928      455744 :             add_paths_to_joinrel(root, joinrel, rel2, rel1,
     929             :                                  JOIN_INNER, sjinfo,
     930             :                                  restrictlist);
     931      455744 :             break;
     932       84110 :         case JOIN_LEFT:
     933      168166 :             if (is_dummy_rel(rel1) ||
     934       84056 :                 restriction_is_constant_false(restrictlist, joinrel, true))
     935             :             {
     936          86 :                 mark_dummy_rel(joinrel);
     937          86 :                 break;
     938             :             }
     939       84216 :             if (restriction_is_constant_false(restrictlist, joinrel, false) &&
     940         192 :                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
     941         168 :                 mark_dummy_rel(rel2);
     942       84024 :             add_paths_to_joinrel(root, joinrel, rel1, rel2,
     943             :                                  JOIN_LEFT, sjinfo,
     944             :                                  restrictlist);
     945       84024 :             add_paths_to_joinrel(root, joinrel, rel2, rel1,
     946             :                                  JOIN_RIGHT, sjinfo,
     947             :                                  restrictlist);
     948       84024 :             break;
     949        1720 :         case JOIN_FULL:
     950        3440 :             if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
     951        1720 :                 restriction_is_constant_false(restrictlist, joinrel, true))
     952             :             {
     953          12 :                 mark_dummy_rel(joinrel);
     954          12 :                 break;
     955             :             }
     956        1708 :             add_paths_to_joinrel(root, joinrel, rel1, rel2,
     957             :                                  JOIN_FULL, sjinfo,
     958             :                                  restrictlist);
     959        1708 :             add_paths_to_joinrel(root, joinrel, rel2, rel1,
     960             :                                  JOIN_FULL, sjinfo,
     961             :                                  restrictlist);
     962             : 
     963             :             /*
     964             :              * If there are join quals that aren't mergeable or hashable, we
     965             :              * may not be able to build any valid plan.  Complain here so that
     966             :              * we can give a somewhat-useful error message.  (Since we have no
     967             :              * flexibility of planning for a full join, there's no chance of
     968             :              * succeeding later with another pair of input rels.)
     969             :              */
     970        1708 :             if (joinrel->pathlist == NIL)
     971           0 :                 ereport(ERROR,
     972             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     973             :                          errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
     974        1708 :             break;
     975        4760 :         case JOIN_SEMI:
     976             : 
     977             :             /*
     978             :              * We might have a normal semijoin, or a case where we don't have
     979             :              * enough rels to do the semijoin but can unique-ify the RHS and
     980             :              * then do an innerjoin (see comments in join_is_legal).  In the
     981             :              * latter case we can't apply JOIN_SEMI joining.
     982             :              */
     983        9198 :             if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
     984        4438 :                 bms_is_subset(sjinfo->min_righthand, rel2->relids))
     985             :             {
     986        8870 :                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
     987        4432 :                     restriction_is_constant_false(restrictlist, joinrel, false))
     988             :                 {
     989          12 :                     mark_dummy_rel(joinrel);
     990          12 :                     break;
     991             :                 }
     992        4426 :                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
     993             :                                      JOIN_SEMI, sjinfo,
     994             :                                      restrictlist);
     995        4426 :                 add_paths_to_joinrel(root, joinrel, rel2, rel1,
     996             :                                      JOIN_RIGHT_SEMI, sjinfo,
     997             :                                      restrictlist);
     998             :             }
     999             : 
    1000             :             /*
    1001             :              * If we know how to unique-ify the RHS and one input rel is
    1002             :              * exactly the RHS (not a superset) we can consider unique-ifying
    1003             :              * it and then doing a regular join.  (The create_unique_path
    1004             :              * check here is probably redundant with what join_is_legal did,
    1005             :              * but if so the check is cheap because it's cached.  So test
    1006             :              * anyway to be sure.)
    1007             :              */
    1008        9496 :             if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
    1009        4748 :                 create_unique_path(root, rel2, rel2->cheapest_total_path,
    1010             :                                    sjinfo) != NULL)
    1011             :             {
    1012        9268 :                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
    1013        4634 :                     restriction_is_constant_false(restrictlist, joinrel, false))
    1014             :                 {
    1015           0 :                     mark_dummy_rel(joinrel);
    1016           0 :                     break;
    1017             :                 }
    1018        4634 :                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
    1019             :                                      JOIN_UNIQUE_INNER, sjinfo,
    1020             :                                      restrictlist);
    1021        4634 :                 add_paths_to_joinrel(root, joinrel, rel2, rel1,
    1022             :                                      JOIN_UNIQUE_OUTER, sjinfo,
    1023             :                                      restrictlist);
    1024             :             }
    1025        4748 :             break;
    1026        4726 :         case JOIN_ANTI:
    1027        9452 :             if (is_dummy_rel(rel1) ||
    1028        4726 :                 restriction_is_constant_false(restrictlist, joinrel, true))
    1029             :             {
    1030           0 :                 mark_dummy_rel(joinrel);
    1031           0 :                 break;
    1032             :             }
    1033        4726 :             if (restriction_is_constant_false(restrictlist, joinrel, false) &&
    1034           0 :                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
    1035           0 :                 mark_dummy_rel(rel2);
    1036        4726 :             add_paths_to_joinrel(root, joinrel, rel1, rel2,
    1037             :                                  JOIN_ANTI, sjinfo,
    1038             :                                  restrictlist);
    1039        4726 :             add_paths_to_joinrel(root, joinrel, rel2, rel1,
    1040             :                                  JOIN_RIGHT_ANTI, sjinfo,
    1041             :                                  restrictlist);
    1042        4726 :             break;
    1043           0 :         default:
    1044             :             /* other values not expected here */
    1045           0 :             elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
    1046             :             break;
    1047             :     }
    1048             : 
    1049             :     /* Apply partitionwise join technique, if possible. */
    1050      551258 :     try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
    1051      551258 : }
    1052             : 
    1053             : 
    1054             : /*
    1055             :  * have_join_order_restriction
    1056             :  *      Detect whether the two relations should be joined to satisfy
    1057             :  *      a join-order restriction arising from special or lateral joins.
    1058             :  *
    1059             :  * In practice this is always used with have_relevant_joinclause(), and so
    1060             :  * could be merged with that function, but it seems clearer to separate the
    1061             :  * two concerns.  We need this test because there are degenerate cases where
    1062             :  * a clauseless join must be performed to satisfy join-order restrictions.
    1063             :  * Also, if one rel has a lateral reference to the other, or both are needed
    1064             :  * to compute some PHV, we should consider joining them even if the join would
    1065             :  * be clauseless.
    1066             :  *
    1067             :  * Note: this is only a problem if one side of a degenerate outer join
    1068             :  * contains multiple rels, or a clauseless join is required within an
    1069             :  * IN/EXISTS RHS; else we will find a join path via the "last ditch" case in
    1070             :  * join_search_one_level().  We could dispense with this test if we were
    1071             :  * willing to try bushy plans in the "last ditch" case, but that seems much
    1072             :  * less efficient.
    1073             :  */
    1074             : bool
    1075      105890 : have_join_order_restriction(PlannerInfo *root,
    1076             :                             RelOptInfo *rel1, RelOptInfo *rel2)
    1077             : {
    1078      105890 :     bool        result = false;
    1079             :     ListCell   *l;
    1080             : 
    1081             :     /*
    1082             :      * If either side has a direct lateral reference to the other, attempt the
    1083             :      * join regardless of outer-join considerations.
    1084             :      */
    1085      201944 :     if (bms_overlap(rel1->relids, rel2->direct_lateral_relids) ||
    1086       96054 :         bms_overlap(rel2->relids, rel1->direct_lateral_relids))
    1087       10664 :         return true;
    1088             : 
    1089             :     /*
    1090             :      * Likewise, if both rels are needed to compute some PlaceHolderVar,
    1091             :      * attempt the join regardless of outer-join considerations.  (This is not
    1092             :      * very desirable, because a PHV with a large eval_at set will cause a lot
    1093             :      * of probably-useless joins to be considered, but failing to do this can
    1094             :      * cause us to fail to construct a plan at all.)
    1095             :      */
    1096       96880 :     foreach(l, root->placeholder_list)
    1097             :     {
    1098        1708 :         PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
    1099             : 
    1100        2080 :         if (bms_is_subset(rel1->relids, phinfo->ph_eval_at) &&
    1101         372 :             bms_is_subset(rel2->relids, phinfo->ph_eval_at))
    1102          54 :             return true;
    1103             :     }
    1104             : 
    1105             :     /*
    1106             :      * It's possible that the rels correspond to the left and right sides of a
    1107             :      * degenerate outer join, that is, one with no joinclause mentioning the
    1108             :      * non-nullable side; in which case we should force the join to occur.
    1109             :      *
    1110             :      * Also, the two rels could represent a clauseless join that has to be
    1111             :      * completed to build up the LHS or RHS of an outer join.
    1112             :      */
    1113      190750 :     foreach(l, root->join_info_list)
    1114             :     {
    1115       96896 :         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
    1116             : 
    1117             :         /* ignore full joins --- other mechanisms handle them */
    1118       96896 :         if (sjinfo->jointype == JOIN_FULL)
    1119          42 :             continue;
    1120             : 
    1121             :         /* Can we perform the SJ with these rels? */
    1122      118184 :         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
    1123       21330 :             bms_is_subset(sjinfo->min_righthand, rel2->relids))
    1124             :         {
    1125         988 :             result = true;
    1126         988 :             break;
    1127             :         }
    1128      101824 :         if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
    1129        5958 :             bms_is_subset(sjinfo->min_righthand, rel1->relids))
    1130             :         {
    1131         180 :             result = true;
    1132         180 :             break;
    1133             :         }
    1134             : 
    1135             :         /*
    1136             :          * Might we need to join these rels to complete the RHS?  We have to
    1137             :          * use "overlap" tests since either rel might include a lower SJ that
    1138             :          * has been proven to commute with this one.
    1139             :          */
    1140      114928 :         if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
    1141       19242 :             bms_overlap(sjinfo->min_righthand, rel2->relids))
    1142             :         {
    1143         120 :             result = true;
    1144         120 :             break;
    1145             :         }
    1146             : 
    1147             :         /* Likewise for the LHS. */
    1148      118844 :         if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
    1149       23278 :             bms_overlap(sjinfo->min_lefthand, rel2->relids))
    1150             :         {
    1151          30 :             result = true;
    1152          30 :             break;
    1153             :         }
    1154             :     }
    1155             : 
    1156             :     /*
    1157             :      * We do not force the join to occur if either input rel can legally be
    1158             :      * joined to anything else using joinclauses.  This essentially means that
    1159             :      * clauseless bushy joins are put off as long as possible. The reason is
    1160             :      * that when there is a join order restriction high up in the join tree
    1161             :      * (that is, with many rels inside the LHS or RHS), we would otherwise
    1162             :      * expend lots of effort considering very stupid join combinations within
    1163             :      * its LHS or RHS.
    1164             :      */
    1165       95172 :     if (result)
    1166             :     {
    1167        2540 :         if (has_legal_joinclause(root, rel1) ||
    1168        1222 :             has_legal_joinclause(root, rel2))
    1169         210 :             result = false;
    1170             :     }
    1171             : 
    1172       95172 :     return result;
    1173             : }
    1174             : 
    1175             : 
    1176             : /*
    1177             :  * has_join_restriction
    1178             :  *      Detect whether the specified relation has join-order restrictions,
    1179             :  *      due to being inside an outer join or an IN (sub-SELECT),
    1180             :  *      or participating in any LATERAL references or multi-rel PHVs.
    1181             :  *
    1182             :  * Essentially, this tests whether have_join_order_restriction() could
    1183             :  * succeed with this rel and some other one.  It's OK if we sometimes
    1184             :  * say "true" incorrectly.  (Therefore, we don't bother with the relatively
    1185             :  * expensive has_legal_joinclause test.)
    1186             :  */
    1187             : static bool
    1188       30974 : has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
    1189             : {
    1190             :     ListCell   *l;
    1191             : 
    1192       30974 :     if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
    1193       18480 :         return true;
    1194             : 
    1195       13202 :     foreach(l, root->placeholder_list)
    1196             :     {
    1197         756 :         PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
    1198             : 
    1199         756 :         if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
    1200         162 :             !bms_equal(rel->relids, phinfo->ph_eval_at))
    1201          48 :             return true;
    1202             :     }
    1203             : 
    1204       13240 :     foreach(l, root->join_info_list)
    1205             :     {
    1206        2758 :         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
    1207             : 
    1208             :         /* ignore full joins --- other mechanisms preserve their ordering */
    1209        2758 :         if (sjinfo->jointype == JOIN_FULL)
    1210          86 :             continue;
    1211             : 
    1212             :         /* ignore if SJ is already contained in rel */
    1213        4128 :         if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
    1214        1456 :             bms_is_subset(sjinfo->min_righthand, rel->relids))
    1215         372 :             continue;
    1216             : 
    1217             :         /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
    1218        3492 :         if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
    1219        1192 :             bms_overlap(sjinfo->min_righthand, rel->relids))
    1220        1964 :             return true;
    1221             :     }
    1222             : 
    1223       10482 :     return false;
    1224             : }
    1225             : 
    1226             : 
    1227             : /*
    1228             :  * has_legal_joinclause
    1229             :  *      Detect whether the specified relation can legally be joined
    1230             :  *      to any other rels using join clauses.
    1231             :  *
    1232             :  * We consider only joins to single other relations in the current
    1233             :  * initial_rels list.  This is sufficient to get a "true" result in most real
    1234             :  * queries, and an occasional erroneous "false" will only cost a bit more
    1235             :  * planning time.  The reason for this limitation is that considering joins to
    1236             :  * other joins would require proving that the other join rel can legally be
    1237             :  * formed, which seems like too much trouble for something that's only a
    1238             :  * heuristic to save planning time.  (Note: we must look at initial_rels
    1239             :  * and not all of the query, since when we are planning a sub-joinlist we
    1240             :  * may be forced to make clauseless joins within initial_rels even though
    1241             :  * there are join clauses linking to other parts of the query.)
    1242             :  */
    1243             : static bool
    1244        2540 : has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
    1245             : {
    1246             :     ListCell   *lc;
    1247             : 
    1248        9420 :     foreach(lc, root->initial_rels)
    1249             :     {
    1250        7090 :         RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
    1251             : 
    1252             :         /* ignore rels that are already in "rel" */
    1253        7090 :         if (bms_overlap(rel->relids, rel2->relids))
    1254        2990 :             continue;
    1255             : 
    1256        4100 :         if (have_relevant_joinclause(root, rel, rel2))
    1257             :         {
    1258             :             Relids      joinrelids;
    1259             :             SpecialJoinInfo *sjinfo;
    1260             :             bool        reversed;
    1261             : 
    1262             :             /* join_is_legal needs relids of the union */
    1263         366 :             joinrelids = bms_union(rel->relids, rel2->relids);
    1264             : 
    1265         366 :             if (join_is_legal(root, rel, rel2, joinrelids,
    1266             :                               &sjinfo, &reversed))
    1267             :             {
    1268             :                 /* Yes, this will work */
    1269         210 :                 bms_free(joinrelids);
    1270         210 :                 return true;
    1271             :             }
    1272             : 
    1273         156 :             bms_free(joinrelids);
    1274             :         }
    1275             :     }
    1276             : 
    1277        2330 :     return false;
    1278             : }
    1279             : 
    1280             : 
    1281             : /*
    1282             :  * There's a pitfall for creating parameterized nestloops: suppose the inner
    1283             :  * rel (call it A) has a parameter that is a PlaceHolderVar, and that PHV's
    1284             :  * minimum eval_at set includes the outer rel (B) and some third rel (C).
    1285             :  * We might think we could create a B/A nestloop join that's parameterized by
    1286             :  * C.  But we would end up with a plan in which the PHV's expression has to be
    1287             :  * evaluated as a nestloop parameter at the B/A join; and the executor is only
    1288             :  * set up to handle simple Vars as NestLoopParams.  Rather than add complexity
    1289             :  * and overhead to the executor for such corner cases, it seems better to
    1290             :  * forbid the join.  (Note that we can still make use of A's parameterized
    1291             :  * path with pre-joined B+C as the outer rel.  have_join_order_restriction()
    1292             :  * ensures that we will consider making such a join even if there are not
    1293             :  * other reasons to do so.)
    1294             :  *
    1295             :  * So we check whether any PHVs used in the query could pose such a hazard.
    1296             :  * We don't have any simple way of checking whether a risky PHV would actually
    1297             :  * be used in the inner plan, and the case is so unusual that it doesn't seem
    1298             :  * worth working very hard on it.
    1299             :  *
    1300             :  * This needs to be checked in two places.  If the inner rel's minimum
    1301             :  * parameterization would trigger the restriction, then join_is_legal() should
    1302             :  * reject the join altogether, because there will be no workable paths for it.
    1303             :  * But joinpath.c has to check again for every proposed nestloop path, because
    1304             :  * the inner path might have more than the minimum parameterization, causing
    1305             :  * some PHV to be dangerous for it that otherwise wouldn't be.
    1306             :  */
    1307             : bool
    1308       44028 : have_dangerous_phv(PlannerInfo *root,
    1309             :                    Relids outer_relids, Relids inner_params)
    1310             : {
    1311             :     ListCell   *lc;
    1312             : 
    1313       46890 :     foreach(lc, root->placeholder_list)
    1314             :     {
    1315        3114 :         PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
    1316             : 
    1317        3114 :         if (!bms_is_subset(phinfo->ph_eval_at, inner_params))
    1318        2298 :             continue;           /* ignore, could not be a nestloop param */
    1319         816 :         if (!bms_overlap(phinfo->ph_eval_at, outer_relids))
    1320         192 :             continue;           /* ignore, not relevant to this join */
    1321         624 :         if (bms_is_subset(phinfo->ph_eval_at, outer_relids))
    1322         372 :             continue;           /* safe, it can be eval'd within outerrel */
    1323             :         /* Otherwise, it's potentially unsafe, so reject the join */
    1324         252 :         return true;
    1325             :     }
    1326             : 
    1327             :     /* OK to perform the join */
    1328       43776 :     return false;
    1329             : }
    1330             : 
    1331             : 
    1332             : /*
    1333             :  * is_dummy_rel --- has relation been proven empty?
    1334             :  */
    1335             : bool
    1336     3772058 : is_dummy_rel(RelOptInfo *rel)
    1337             : {
    1338             :     Path       *path;
    1339             : 
    1340             :     /*
    1341             :      * A rel that is known dummy will have just one path that is a childless
    1342             :      * Append.  (Even if somehow it has more paths, a childless Append will
    1343             :      * have cost zero and hence should be at the front of the pathlist.)
    1344             :      */
    1345     3772058 :     if (rel->pathlist == NIL)
    1346     1803020 :         return false;
    1347     1969038 :     path = (Path *) linitial(rel->pathlist);
    1348             : 
    1349             :     /*
    1350             :      * Initially, a dummy path will just be a childless Append.  But in later
    1351             :      * planning stages we might stick a ProjectSetPath and/or ProjectionPath
    1352             :      * on top, since Append can't project.  Rather than make assumptions about
    1353             :      * which combinations can occur, just descend through whatever we find.
    1354             :      */
    1355             :     for (;;)
    1356             :     {
    1357     2025346 :         if (IsA(path, ProjectionPath))
    1358       52498 :             path = ((ProjectionPath *) path)->subpath;
    1359     1972848 :         else if (IsA(path, ProjectSetPath))
    1360        3810 :             path = ((ProjectSetPath *) path)->subpath;
    1361             :         else
    1362     1969038 :             break;
    1363             :     }
    1364     1969038 :     if (IS_DUMMY_APPEND(path))
    1365        3716 :         return true;
    1366     1965322 :     return false;
    1367             : }
    1368             : 
    1369             : /*
    1370             :  * Mark a relation as proven empty.
    1371             :  *
    1372             :  * During GEQO planning, this can get invoked more than once on the same
    1373             :  * baserel struct, so it's worth checking to see if the rel is already marked
    1374             :  * dummy.
    1375             :  *
    1376             :  * Also, when called during GEQO join planning, we are in a short-lived
    1377             :  * memory context.  We must make sure that the dummy path attached to a
    1378             :  * baserel survives the GEQO cycle, else the baserel is trashed for future
    1379             :  * GEQO cycles.  On the other hand, when we are marking a joinrel during GEQO,
    1380             :  * we don't want the dummy path to clutter the main planning context.  Upshot
    1381             :  * is that the best solution is to explicitly make the dummy path in the same
    1382             :  * context the given RelOptInfo is in.
    1383             :  */
    1384             : void
    1385         566 : mark_dummy_rel(RelOptInfo *rel)
    1386             : {
    1387             :     MemoryContext oldcontext;
    1388             : 
    1389             :     /* Already marked? */
    1390         566 :     if (is_dummy_rel(rel))
    1391          18 :         return;
    1392             : 
    1393             :     /* No, so choose correct context to make the dummy path in */
    1394         548 :     oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
    1395             : 
    1396             :     /* Set dummy size estimate */
    1397         548 :     rel->rows = 0;
    1398             : 
    1399             :     /* Evict any previously chosen paths */
    1400         548 :     rel->pathlist = NIL;
    1401         548 :     rel->partial_pathlist = NIL;
    1402             : 
    1403             :     /* Set up the dummy path */
    1404         548 :     add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
    1405             :                                               NIL, rel->lateral_relids,
    1406             :                                               0, false, -1));
    1407             : 
    1408             :     /* Set or update cheapest_total_path and related fields */
    1409         548 :     set_cheapest(rel);
    1410             : 
    1411         548 :     MemoryContextSwitchTo(oldcontext);
    1412             : }
    1413             : 
    1414             : 
    1415             : /*
    1416             :  * restriction_is_constant_false --- is a restrictlist just FALSE?
    1417             :  *
    1418             :  * In cases where a qual is provably constant FALSE, eval_const_expressions
    1419             :  * will generally have thrown away anything that's ANDed with it.  In outer
    1420             :  * join situations this will leave us computing cartesian products only to
    1421             :  * decide there's no match for an outer row, which is pretty stupid.  So,
    1422             :  * we need to detect the case.
    1423             :  *
    1424             :  * If only_pushed_down is true, then consider only quals that are pushed-down
    1425             :  * from the point of view of the joinrel.
    1426             :  */
    1427             : static bool
    1428      644242 : restriction_is_constant_false(List *restrictlist,
    1429             :                               RelOptInfo *joinrel,
    1430             :                               bool only_pushed_down)
    1431             : {
    1432             :     ListCell   *lc;
    1433             : 
    1434             :     /*
    1435             :      * Despite the above comment, the restriction list we see here might
    1436             :      * possibly have other members besides the FALSE constant, since other
    1437             :      * quals could get "pushed down" to the outer join level.  So we check
    1438             :      * each member of the list.
    1439             :      */
    1440     1419982 :     foreach(lc, restrictlist)
    1441             :     {
    1442      776162 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
    1443             : 
    1444      776162 :         if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
    1445      110816 :             continue;
    1446             : 
    1447      665346 :         if (rinfo->clause && IsA(rinfo->clause, Const))
    1448             :         {
    1449        5100 :             Const      *con = (Const *) rinfo->clause;
    1450             : 
    1451             :             /* constant NULL is as good as constant FALSE for our purposes */
    1452        5100 :             if (con->constisnull)
    1453         422 :                 return true;
    1454        4992 :             if (!DatumGetBool(con->constvalue))
    1455         314 :                 return true;
    1456             :         }
    1457             :     }
    1458      643820 :     return false;
    1459             : }
    1460             : 
    1461             : /*
    1462             :  * Assess whether join between given two partitioned relations can be broken
    1463             :  * down into joins between matching partitions; a technique called
    1464             :  * "partitionwise join"
    1465             :  *
    1466             :  * Partitionwise join is possible when a. Joining relations have same
    1467             :  * partitioning scheme b. There exists an equi-join between the partition keys
    1468             :  * of the two relations.
    1469             :  *
    1470             :  * Partitionwise join is planned as follows (details: optimizer/README.)
    1471             :  *
    1472             :  * 1. Create the RelOptInfos for joins between matching partitions i.e
    1473             :  * child-joins and add paths to them.
    1474             :  *
    1475             :  * 2. Construct Append or MergeAppend paths across the set of child joins.
    1476             :  * This second phase is implemented by generate_partitionwise_join_paths().
    1477             :  *
    1478             :  * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
    1479             :  * obtained by translating the respective parent join structures.
    1480             :  */
    1481             : static void
    1482      551258 : try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
    1483             :                        RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
    1484             :                        List *parent_restrictlist)
    1485             : {
    1486      551258 :     bool        rel1_is_simple = IS_SIMPLE_REL(rel1);
    1487      551258 :     bool        rel2_is_simple = IS_SIMPLE_REL(rel2);
    1488      551258 :     List       *parts1 = NIL;
    1489      551258 :     List       *parts2 = NIL;
    1490      551258 :     ListCell   *lcr1 = NULL;
    1491      551258 :     ListCell   *lcr2 = NULL;
    1492             :     int         cnt_parts;
    1493             : 
    1494             :     /* Guard against stack overflow due to overly deep partition hierarchy. */
    1495      551258 :     check_stack_depth();
    1496             : 
    1497             :     /* Nothing to do, if the join relation is not partitioned. */
    1498      551258 :     if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
    1499      549222 :         return;
    1500             : 
    1501             :     /* The join relation should have consider_partitionwise_join set. */
    1502             :     Assert(joinrel->consider_partitionwise_join);
    1503             : 
    1504             :     /*
    1505             :      * We can not perform partitionwise join if either of the joining
    1506             :      * relations is not partitioned.
    1507             :      */
    1508        2174 :     if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
    1509          18 :         return;
    1510             : 
    1511             :     Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
    1512             : 
    1513             :     /* The joining relations should have consider_partitionwise_join set. */
    1514             :     Assert(rel1->consider_partitionwise_join &&
    1515             :            rel2->consider_partitionwise_join);
    1516             : 
    1517             :     /*
    1518             :      * The partition scheme of the join relation should match that of the
    1519             :      * joining relations.
    1520             :      */
    1521             :     Assert(joinrel->part_scheme == rel1->part_scheme &&
    1522             :            joinrel->part_scheme == rel2->part_scheme);
    1523             : 
    1524             :     Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
    1525             : 
    1526        2156 :     compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
    1527             :                              &parts1, &parts2);
    1528             : 
    1529        2156 :     if (joinrel->partbounds_merged)
    1530             :     {
    1531         768 :         lcr1 = list_head(parts1);
    1532         768 :         lcr2 = list_head(parts2);
    1533             :     }
    1534             : 
    1535             :     /*
    1536             :      * Create child-join relations for this partitioned join, if those don't
    1537             :      * exist. Add paths to child-joins for a pair of child relations
    1538             :      * corresponding to the given pair of parent relations.
    1539             :      */
    1540        7574 :     for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
    1541             :     {
    1542             :         RelOptInfo *child_rel1;
    1543             :         RelOptInfo *child_rel2;
    1544             :         bool        rel1_empty;
    1545             :         bool        rel2_empty;
    1546             :         SpecialJoinInfo *child_sjinfo;
    1547             :         List       *child_restrictlist;
    1548             :         RelOptInfo *child_joinrel;
    1549             :         AppendRelInfo **appinfos;
    1550             :         int         nappinfos;
    1551             :         Relids      child_relids;
    1552             : 
    1553        5538 :         if (joinrel->partbounds_merged)
    1554             :         {
    1555        2010 :             child_rel1 = lfirst_node(RelOptInfo, lcr1);
    1556        2010 :             child_rel2 = lfirst_node(RelOptInfo, lcr2);
    1557        2010 :             lcr1 = lnext(parts1, lcr1);
    1558        2010 :             lcr2 = lnext(parts2, lcr2);
    1559             :         }
    1560             :         else
    1561             :         {
    1562        3528 :             child_rel1 = rel1->part_rels[cnt_parts];
    1563        3528 :             child_rel2 = rel2->part_rels[cnt_parts];
    1564             :         }
    1565             : 
    1566        5538 :         rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
    1567        5538 :         rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
    1568             : 
    1569             :         /*
    1570             :          * Check for cases where we can prove that this segment of the join
    1571             :          * returns no rows, due to one or both inputs being empty (including
    1572             :          * inputs that have been pruned away entirely).  If so just ignore it.
    1573             :          * These rules are equivalent to populate_joinrel_with_paths's rules
    1574             :          * for dummy input relations.
    1575             :          */
    1576        5538 :         switch (parent_sjinfo->jointype)
    1577             :         {
    1578        2732 :             case JOIN_INNER:
    1579             :             case JOIN_SEMI:
    1580        2732 :                 if (rel1_empty || rel2_empty)
    1581          64 :                     continue;   /* ignore this join segment */
    1582        2696 :                 break;
    1583        2084 :             case JOIN_LEFT:
    1584             :             case JOIN_ANTI:
    1585        2084 :                 if (rel1_empty)
    1586          28 :                     continue;   /* ignore this join segment */
    1587        2056 :                 break;
    1588         722 :             case JOIN_FULL:
    1589         722 :                 if (rel1_empty && rel2_empty)
    1590           0 :                     continue;   /* ignore this join segment */
    1591         722 :                 break;
    1592           0 :             default:
    1593             :                 /* other values not expected here */
    1594           0 :                 elog(ERROR, "unrecognized join type: %d",
    1595             :                      (int) parent_sjinfo->jointype);
    1596             :                 break;
    1597             :         }
    1598             : 
    1599             :         /*
    1600             :          * If a child has been pruned entirely then we can't generate paths
    1601             :          * for it, so we have to reject partitionwise joining unless we were
    1602             :          * able to eliminate this partition above.
    1603             :          */
    1604        5474 :         if (child_rel1 == NULL || child_rel2 == NULL)
    1605             :         {
    1606             :             /*
    1607             :              * Mark the joinrel as unpartitioned so that later functions treat
    1608             :              * it correctly.
    1609             :              */
    1610         120 :             joinrel->nparts = 0;
    1611         120 :             return;
    1612             :         }
    1613             : 
    1614             :         /*
    1615             :          * If a leaf relation has consider_partitionwise_join=false, it means
    1616             :          * that it's a dummy relation for which we skipped setting up tlist
    1617             :          * expressions and adding EC members in set_append_rel_size(), so
    1618             :          * again we have to fail here.
    1619             :          */
    1620        5354 :         if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
    1621             :         {
    1622             :             Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
    1623             :             Assert(IS_DUMMY_REL(child_rel1));
    1624           0 :             joinrel->nparts = 0;
    1625           0 :             return;
    1626             :         }
    1627        5354 :         if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
    1628             :         {
    1629             :             Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
    1630             :             Assert(IS_DUMMY_REL(child_rel2));
    1631           0 :             joinrel->nparts = 0;
    1632           0 :             return;
    1633             :         }
    1634             : 
    1635             :         /* We should never try to join two overlapping sets of rels. */
    1636             :         Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
    1637             : 
    1638             :         /*
    1639             :          * Construct SpecialJoinInfo from parent join relations's
    1640             :          * SpecialJoinInfo.
    1641             :          */
    1642        5354 :         child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
    1643             :                                                child_rel1->relids,
    1644             :                                                child_rel2->relids);
    1645             : 
    1646             :         /* Find the AppendRelInfo structures */
    1647        5354 :         child_relids = bms_union(child_rel1->relids, child_rel2->relids);
    1648        5354 :         appinfos = find_appinfos_by_relids(root, child_relids,
    1649             :                                            &nappinfos);
    1650             : 
    1651             :         /*
    1652             :          * Construct restrictions applicable to the child join from those
    1653             :          * applicable to the parent join.
    1654             :          */
    1655             :         child_restrictlist =
    1656        5354 :             (List *) adjust_appendrel_attrs(root,
    1657             :                                             (Node *) parent_restrictlist,
    1658             :                                             nappinfos, appinfos);
    1659             : 
    1660             :         /* Find or construct the child join's RelOptInfo */
    1661        5354 :         child_joinrel = joinrel->part_rels[cnt_parts];
    1662        5354 :         if (!child_joinrel)
    1663             :         {
    1664        4846 :             child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
    1665             :                                                  joinrel, child_restrictlist,
    1666             :                                                  child_sjinfo, nappinfos, appinfos);
    1667        4846 :             joinrel->part_rels[cnt_parts] = child_joinrel;
    1668        4846 :             joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts);
    1669        4846 :             joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
    1670        4846 :                                                     child_joinrel->relids);
    1671             :         }
    1672             : 
    1673             :         /* Assert we got the right one */
    1674             :         Assert(bms_equal(child_joinrel->relids,
    1675             :                          adjust_child_relids(joinrel->relids,
    1676             :                                              nappinfos, appinfos)));
    1677             : 
    1678             :         /* And make paths for the child join */
    1679        5354 :         populate_joinrel_with_paths(root, child_rel1, child_rel2,
    1680             :                                     child_joinrel, child_sjinfo,
    1681             :                                     child_restrictlist);
    1682             : 
    1683             :         /*
    1684             :          * When there are thousands of partitions involved, this loop will
    1685             :          * accumulate a significant amount of memory usage from objects that
    1686             :          * are only needed within the loop.  Free these local objects eagerly
    1687             :          * at the end of each iteration.
    1688             :          */
    1689        5354 :         pfree(appinfos);
    1690        5354 :         bms_free(child_relids);
    1691        5354 :         free_child_join_sjinfo(child_sjinfo, parent_sjinfo);
    1692             :     }
    1693             : }
    1694             : 
    1695             : /*
    1696             :  * Construct the SpecialJoinInfo for a child-join by translating
    1697             :  * SpecialJoinInfo for the join between parents. left_relids and right_relids
    1698             :  * are the relids of left and right side of the join respectively.
    1699             :  *
    1700             :  * If translations are added to or removed from this function, consider
    1701             :  * updating free_child_join_sjinfo() accordingly.
    1702             :  */
    1703             : static SpecialJoinInfo *
    1704        5354 : build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
    1705             :                         Relids left_relids, Relids right_relids)
    1706             : {
    1707        5354 :     SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
    1708             :     AppendRelInfo **left_appinfos;
    1709             :     int         left_nappinfos;
    1710             :     AppendRelInfo **right_appinfos;
    1711             :     int         right_nappinfos;
    1712             : 
    1713             :     /* Dummy SpecialJoinInfos can be created without any translation. */
    1714        5354 :     if (parent_sjinfo->jointype == JOIN_INNER)
    1715             :     {
    1716             :         Assert(parent_sjinfo->ojrelid == 0);
    1717        2246 :         init_dummy_sjinfo(sjinfo, left_relids, right_relids);
    1718        2246 :         return sjinfo;
    1719             :     }
    1720             : 
    1721        3108 :     memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
    1722        3108 :     left_appinfos = find_appinfos_by_relids(root, left_relids,
    1723             :                                             &left_nappinfos);
    1724        3108 :     right_appinfos = find_appinfos_by_relids(root, right_relids,
    1725             :                                              &right_nappinfos);
    1726             : 
    1727        3108 :     sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
    1728             :                                                left_nappinfos, left_appinfos);
    1729        3108 :     sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand,
    1730             :                                                 right_nappinfos,
    1731             :                                                 right_appinfos);
    1732        3108 :     sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
    1733             :                                                left_nappinfos, left_appinfos);
    1734        3108 :     sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand,
    1735             :                                                 right_nappinfos,
    1736             :                                                 right_appinfos);
    1737             :     /* outer-join relids need no adjustment */
    1738        6216 :     sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
    1739        3108 :                                                              (Node *) sjinfo->semi_rhs_exprs,
    1740             :                                                              right_nappinfos,
    1741             :                                                              right_appinfos);
    1742             : 
    1743        3108 :     pfree(left_appinfos);
    1744        3108 :     pfree(right_appinfos);
    1745             : 
    1746        3108 :     return sjinfo;
    1747             : }
    1748             : 
    1749             : /*
    1750             :  * free_child_join_sjinfo
    1751             :  *      Free memory consumed by a SpecialJoinInfo created by
    1752             :  *      build_child_join_sjinfo()
    1753             :  *
    1754             :  * Only members that are translated copies of their counterpart in the parent
    1755             :  * SpecialJoinInfo are freed here.
    1756             :  */
    1757             : static void
    1758        5354 : free_child_join_sjinfo(SpecialJoinInfo *child_sjinfo,
    1759             :                        SpecialJoinInfo *parent_sjinfo)
    1760             : {
    1761             :     /*
    1762             :      * Dummy SpecialJoinInfos of inner joins do not have any translated fields
    1763             :      * and hence no fields that to be freed.
    1764             :      */
    1765        5354 :     if (child_sjinfo->jointype != JOIN_INNER)
    1766             :     {
    1767        3108 :         if (child_sjinfo->min_lefthand != parent_sjinfo->min_lefthand)
    1768        3090 :             bms_free(child_sjinfo->min_lefthand);
    1769             : 
    1770        3108 :         if (child_sjinfo->min_righthand != parent_sjinfo->min_righthand)
    1771        3108 :             bms_free(child_sjinfo->min_righthand);
    1772             : 
    1773        3108 :         if (child_sjinfo->syn_lefthand != parent_sjinfo->syn_lefthand)
    1774        3108 :             bms_free(child_sjinfo->syn_lefthand);
    1775             : 
    1776        3108 :         if (child_sjinfo->syn_righthand != parent_sjinfo->syn_righthand)
    1777        3108 :             bms_free(child_sjinfo->syn_righthand);
    1778             : 
    1779             :         Assert(child_sjinfo->commute_above_l == parent_sjinfo->commute_above_l);
    1780             :         Assert(child_sjinfo->commute_above_r == parent_sjinfo->commute_above_r);
    1781             :         Assert(child_sjinfo->commute_below_l == parent_sjinfo->commute_below_l);
    1782             :         Assert(child_sjinfo->commute_below_r == parent_sjinfo->commute_below_r);
    1783             : 
    1784             :         Assert(child_sjinfo->semi_operators == parent_sjinfo->semi_operators);
    1785             : 
    1786             :         /*
    1787             :          * semi_rhs_exprs may in principle be freed, but a simple pfree() does
    1788             :          * not suffice, so we leave it alone.
    1789             :          */
    1790             :     }
    1791             : 
    1792        5354 :     pfree(child_sjinfo);
    1793        5354 : }
    1794             : 
    1795             : /*
    1796             :  * compute_partition_bounds
    1797             :  *      Compute the partition bounds for a join rel from those for inputs
    1798             :  */
    1799             : static void
    1800        2156 : compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
    1801             :                          RelOptInfo *rel2, RelOptInfo *joinrel,
    1802             :                          SpecialJoinInfo *parent_sjinfo,
    1803             :                          List **parts1, List **parts2)
    1804             : {
    1805             :     /*
    1806             :      * If we don't have the partition bounds for the join rel yet, try to
    1807             :      * compute those along with pairs of partitions to be joined.
    1808             :      */
    1809        2156 :     if (joinrel->nparts == -1)
    1810             :     {
    1811        1980 :         PartitionScheme part_scheme = joinrel->part_scheme;
    1812        1980 :         PartitionBoundInfo boundinfo = NULL;
    1813        1980 :         int         nparts = 0;
    1814             : 
    1815             :         Assert(joinrel->boundinfo == NULL);
    1816             :         Assert(joinrel->part_rels == NULL);
    1817             : 
    1818             :         /*
    1819             :          * See if the partition bounds for inputs are exactly the same, in
    1820             :          * which case we don't need to work hard: the join rel will have the
    1821             :          * same partition bounds as inputs, and the partitions with the same
    1822             :          * cardinal positions will form the pairs.
    1823             :          *
    1824             :          * Note: even in cases where one or both inputs have merged bounds, it
    1825             :          * would be possible for both the bounds to be exactly the same, but
    1826             :          * it seems unlikely to be worth the cycles to check.
    1827             :          */
    1828        1980 :         if (!rel1->partbounds_merged &&
    1829        1920 :             !rel2->partbounds_merged &&
    1830        3582 :             rel1->nparts == rel2->nparts &&
    1831        1662 :             partition_bounds_equal(part_scheme->partnatts,
    1832             :                                    part_scheme->parttyplen,
    1833             :                                    part_scheme->parttypbyval,
    1834             :                                    rel1->boundinfo, rel2->boundinfo))
    1835             :         {
    1836        1134 :             boundinfo = rel1->boundinfo;
    1837        1134 :             nparts = rel1->nparts;
    1838             :         }
    1839             :         else
    1840             :         {
    1841             :             /* Try merging the partition bounds for inputs. */
    1842         846 :             boundinfo = partition_bounds_merge(part_scheme->partnatts,
    1843         846 :                                                part_scheme->partsupfunc,
    1844             :                                                part_scheme->partcollation,
    1845             :                                                rel1, rel2,
    1846             :                                                parent_sjinfo->jointype,
    1847             :                                                parts1, parts2);
    1848         846 :             if (boundinfo == NULL)
    1849             :             {
    1850         114 :                 joinrel->nparts = 0;
    1851         114 :                 return;
    1852             :             }
    1853         732 :             nparts = list_length(*parts1);
    1854         732 :             joinrel->partbounds_merged = true;
    1855             :         }
    1856             : 
    1857             :         Assert(nparts > 0);
    1858        1866 :         joinrel->boundinfo = boundinfo;
    1859        1866 :         joinrel->nparts = nparts;
    1860        1866 :         joinrel->part_rels =
    1861        1866 :             (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
    1862             :     }
    1863             :     else
    1864             :     {
    1865             :         Assert(joinrel->nparts > 0);
    1866             :         Assert(joinrel->boundinfo);
    1867             :         Assert(joinrel->part_rels);
    1868             : 
    1869             :         /*
    1870             :          * If the join rel's partbounds_merged flag is true, it means inputs
    1871             :          * are not guaranteed to have the same partition bounds, therefore we
    1872             :          * can't assume that the partitions at the same cardinal positions
    1873             :          * form the pairs; let get_matching_part_pairs() generate the pairs.
    1874             :          * Otherwise, nothing to do since we can assume that.
    1875             :          */
    1876         176 :         if (joinrel->partbounds_merged)
    1877             :         {
    1878          36 :             get_matching_part_pairs(root, joinrel, rel1, rel2,
    1879             :                                     parts1, parts2);
    1880             :             Assert(list_length(*parts1) == joinrel->nparts);
    1881             :             Assert(list_length(*parts2) == joinrel->nparts);
    1882             :         }
    1883             :     }
    1884             : }
    1885             : 
    1886             : /*
    1887             :  * get_matching_part_pairs
    1888             :  *      Generate pairs of partitions to be joined from inputs
    1889             :  */
    1890             : static void
    1891          36 : get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
    1892             :                         RelOptInfo *rel1, RelOptInfo *rel2,
    1893             :                         List **parts1, List **parts2)
    1894             : {
    1895          36 :     bool        rel1_is_simple = IS_SIMPLE_REL(rel1);
    1896          36 :     bool        rel2_is_simple = IS_SIMPLE_REL(rel2);
    1897             :     int         cnt_parts;
    1898             : 
    1899          36 :     *parts1 = NIL;
    1900          36 :     *parts2 = NIL;
    1901             : 
    1902         132 :     for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
    1903             :     {
    1904          96 :         RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
    1905             :         RelOptInfo *child_rel1;
    1906             :         RelOptInfo *child_rel2;
    1907             :         Relids      child_relids1;
    1908             :         Relids      child_relids2;
    1909             : 
    1910             :         /*
    1911             :          * If this segment of the join is empty, it means that this segment
    1912             :          * was ignored when previously creating child-join paths for it in
    1913             :          * try_partitionwise_join() as it would not contribute to the join
    1914             :          * result, due to one or both inputs being empty; add NULL to each of
    1915             :          * the given lists so that this segment will be ignored again in that
    1916             :          * function.
    1917             :          */
    1918          96 :         if (!child_joinrel)
    1919             :         {
    1920           0 :             *parts1 = lappend(*parts1, NULL);
    1921           0 :             *parts2 = lappend(*parts2, NULL);
    1922           0 :             continue;
    1923             :         }
    1924             : 
    1925             :         /*
    1926             :          * Get a relids set of partition(s) involved in this join segment that
    1927             :          * are from the rel1 side.
    1928             :          */
    1929          96 :         child_relids1 = bms_intersect(child_joinrel->relids,
    1930          96 :                                       rel1->all_partrels);
    1931             :         Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
    1932             : 
    1933             :         /*
    1934             :          * Get a child rel for rel1 with the relids.  Note that we should have
    1935             :          * the child rel even if rel1 is a join rel, because in that case the
    1936             :          * partitions specified in the relids would have matching/overlapping
    1937             :          * boundaries, so the specified partitions should be considered as
    1938             :          * ones to be joined when planning partitionwise joins of rel1,
    1939             :          * meaning that the child rel would have been built by the time we get
    1940             :          * here.
    1941             :          */
    1942          96 :         if (rel1_is_simple)
    1943             :         {
    1944           0 :             int         varno = bms_singleton_member(child_relids1);
    1945             : 
    1946           0 :             child_rel1 = find_base_rel(root, varno);
    1947             :         }
    1948             :         else
    1949          96 :             child_rel1 = find_join_rel(root, child_relids1);
    1950             :         Assert(child_rel1);
    1951             : 
    1952             :         /*
    1953             :          * Get a relids set of partition(s) involved in this join segment that
    1954             :          * are from the rel2 side.
    1955             :          */
    1956          96 :         child_relids2 = bms_intersect(child_joinrel->relids,
    1957          96 :                                       rel2->all_partrels);
    1958             :         Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
    1959             : 
    1960             :         /*
    1961             :          * Get a child rel for rel2 with the relids.  See above comments.
    1962             :          */
    1963          96 :         if (rel2_is_simple)
    1964             :         {
    1965          96 :             int         varno = bms_singleton_member(child_relids2);
    1966             : 
    1967          96 :             child_rel2 = find_base_rel(root, varno);
    1968             :         }
    1969             :         else
    1970           0 :             child_rel2 = find_join_rel(root, child_relids2);
    1971             :         Assert(child_rel2);
    1972             : 
    1973             :         /*
    1974             :          * The join of rel1 and rel2 is legal, so is the join of the child
    1975             :          * rels obtained above; add them to the given lists as a join pair
    1976             :          * producing this join segment.
    1977             :          */
    1978          96 :         *parts1 = lappend(*parts1, child_rel1);
    1979          96 :         *parts2 = lappend(*parts2, child_rel2);
    1980             :     }
    1981          36 : }

Generated by: LCOV version 1.14