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

Generated by: LCOV version 1.14