LCOV - code coverage report
Current view: top level - src/backend/optimizer/path - joinrels.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 94.9 % 587 557
Test Date: 2026-09-07 00:17:14 Functions: 100.0 % 20 20
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 85.9 % 596 512

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

Generated by: LCOV version 2.0-1