LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/path - joinrels.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 93.8 % 535 502 10 19 4 6 284 12 200 21 290 2 2
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 17 17 14 3 14
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a