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

Generated by: LCOV version 1.13