LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/path - joinrels.c (source / functions) Coverage Total Hit UBC CBC
Current: f76c13edadc2b319036e0d238703ed56bb23f934 vs 9e17d25e79d4756be08b4a5521b4b58450217137 Lines: 94.5 % 619 585 34 585
Current Date: 2026-09-20 14:13:17 +0900 Functions: 100.0 % 20 20 20
Baseline: lcov-20260920-baseline Branches: 80.7 % 688 555 133 555
Baseline Date: 2026-09-20 14:13:13 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 8 8 8
(30,360] days: 95.7 % 46 44 2 44
(360..) days: 94.3 % 565 533 32 533
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 19 19 19
Branch coverage date bins:
(7,30] days: 85.7 % 14 12 2 12
(30,360] days: 85.4 % 48 41 7 41
(360..) days: 80.2 % 626 502 124 502

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * joinrels.c
                                  4                 :                :  *    Routines to determine which relations should be joined
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-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
 6140 tgl@sss.pgh.pa.us          78                 :CBC      107658 : join_search_one_level(PlannerInfo *root, int level)
                                 79                 :                : {
                                 80                 :         107658 :     List      **joinrels = root->join_rel_level;
                                 81                 :                :     ListCell   *r;
                                 82                 :                :     int         k;
                                 83                 :                : 
                                 84         [ -  + ]:         107658 :     Assert(joinrels[level] == NIL);
                                 85                 :                : 
                                 86                 :                :     /* Set join_cur_level so that new joinrels are added to proper list */
                                 87                 :         107658 :     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                 :                :      */
 9313 bruce@momjian.us           96   [ +  +  +  +  :         381186 :     foreach(r, joinrels[level - 1])
                                              +  + ]
                                 97                 :                :     {
10076                            98                 :         273528 :         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
                                 99                 :                : 
 7156 tgl@sss.pgh.pa.us         100   [ +  +  +  +  :         294972 :         if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
                                              +  + ]
                                101                 :          21444 :             has_join_restriction(root, old_rel))
 9722                           102                 :         264197 :         {
                                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                 :                :              */
 5266                           118         [ +  + ]:         264197 :             if (level == 2)     /* consider remaining initial rels */
 1141 drowley@postgresql.o      119                 :         176703 :                 first_rel = foreach_current_index(r) + 1;
                                120                 :                :             else
                                121                 :          87494 :                 first_rel = 0;
                                122                 :                : 
                                123                 :         264197 :             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                 :                :              */
 6140 tgl@sss.pgh.pa.us         139                 :           9331 :             make_rels_by_clauseless_joins(root,
                                140                 :                :                                           old_rel,
 2624                           141                 :           9331 :                                           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                 :                :      */
 9313 bruce@momjian.us          153                 :         107658 :     for (k = 2;; k++)
 9980                           154                 :          12363 :     {
 9504 tgl@sss.pgh.pa.us         155                 :         120021 :         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         [ +  + ]:         120021 :         if (k > other_level)
 9722                           162                 :         107658 :             break;
                                163                 :                : 
 9504                           164   [ +  -  +  +  :          59738 :         foreach(r, joinrels[k])
                                              +  + ]
                                165                 :                :         {
                                166                 :          47375 :             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                 :                :              */
 7183                           175   [ +  +  +  + ]:          47375 :             if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
 7156                           176         [ +  + ]:            357 :                 !has_join_restriction(root, old_rel))
 7222                           177                 :            238 :                 continue;
                                178                 :                : 
 1141 drowley@postgresql.o      179         [ +  + ]:          47137 :             if (k == other_level)   /* only consider remaining rels */
                                180                 :          34764 :                 first_rel = foreach_current_index(r) + 1;
                                181                 :                :             else
                                182                 :          12373 :                 first_rel = 0;
                                183                 :                : 
                                184   [ +  -  +  +  :         179341 :             for_each_from(r2, joinrels[other_level], first_rel)
                                              +  + ]
                                185                 :                :             {
 9504 tgl@sss.pgh.pa.us         186                 :         132204 :                 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
                                187                 :                : 
 8625                           188         [ +  + ]:         132204 :                 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                 :                :                      */
 7156                           195   [ +  +  +  + ]:          16262 :                     if (have_relevant_joinclause(root, old_rel, new_rel) ||
                                196                 :            955 :                         have_join_order_restriction(root, old_rel, new_rel))
                                197                 :                :                     {
 6140                           198                 :          14407 :                         (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                 :                :      */
 5149                           224         [ +  + ]:         107658 :     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   [ +  -  +  +  :             55 :         foreach(r, joinrels[level - 1])
                                              +  + ]
                                231                 :                :         {
                                232                 :             35 :             RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
                                233                 :                : 
                                234                 :             35 :             make_rels_by_clauseless_joins(root,
                                235                 :                :                                           old_rel,
 2624                           236                 :             35 :                                           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                 :                :          */
 5138                           257         [ +  + ]:             20 :         if (joinrels[level] == NIL &&
                                258         [ -  + ]:             10 :             root->join_info_list == NIL &&
 3936 tgl@sss.pgh.pa.us         259         [ #  # ]:UBC           0 :             !root->hasLateralRTEs)
 5149                           260         [ #  # ]:              0 :             elog(ERROR, "failed to build any %d-way joins", level);
                                261                 :                :     }
11030 scrappy@hub.org           262                 :CBC      107658 : }
                                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
 7777 tgl@sss.pgh.pa.us         285                 :         264197 : 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                 :                : 
 1141 drowley@postgresql.o      292   [ +  -  +  +  :         784257 :     for_each_from(l, other_rels, first_rel_idx)
                                              +  + ]
                                293                 :                :     {
 7773 tgl@sss.pgh.pa.us         294                 :         520060 :         RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
                                295                 :                : 
                                296   [ +  +  +  + ]:         817066 :         if (!bms_overlap(old_rel->relids, other_rel->relids) &&
 7156                           297         [ +  + ]:         354370 :             (have_relevant_joinclause(root, old_rel, other_rel) ||
                                298                 :          57364 :              have_join_order_restriction(root, old_rel, other_rel)))
                                299                 :                :         {
 6140                           300                 :         249146 :             (void) make_join_rel(root, old_rel, other_rel);
                                301                 :                :         }
                                302                 :                :     }
11030 scrappy@hub.org           303                 :         264197 : }
                                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
 7777 tgl@sss.pgh.pa.us         319                 :           9366 : make_rels_by_clauseless_joins(PlannerInfo *root,
                                320                 :                :                               RelOptInfo *old_rel,
                                321                 :                :                               List *other_rels)
                                322                 :                : {
                                323                 :                :     ListCell   *l;
                                324                 :                : 
 2624                           325   [ +  -  +  +  :          30653 :     foreach(l, other_rels)
                                              +  + ]
                                326                 :                :     {
 6140                           327                 :          21287 :         RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
                                328                 :                : 
 8625                           329         [ +  + ]:          21287 :         if (!bms_overlap(other_rel->relids, old_rel->relids))
                                330                 :                :         {
 6140                           331                 :          10123 :             (void) make_join_rel(root, old_rel, other_rel);
                                332                 :                :         }
                                333                 :                :     }
11030 scrappy@hub.org           334                 :           9366 : }
                                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
 6904 tgl@sss.pgh.pa.us         355                 :         281079 : 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                 :                :      */
 6611                           369                 :         281079 :     *sjinfo_p = NULL;
                                370                 :         281079 :     *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                 :         281079 :     match_sjinfo = NULL;
                                378                 :         281079 :     reversed = false;
 6272                           379                 :         281079 :     unique_ified = false;
 4063                           380                 :         281079 :     must_be_leftjoin = false;
                                381                 :                : 
 6611                           382   [ +  +  +  +  :         541715 :     foreach(l, root->join_info_list)
                                              +  + ]
                                383                 :                :     {
                                384                 :         279722 :         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         [ +  + ]:         279722 :         if (!bms_overlap(sjinfo->min_righthand, joinrelids))
 7579                           392                 :          97159 :             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                 :                :          */
 6611                           398         [ +  + ]:         182563 :         if (bms_is_subset(joinrelids, sjinfo->min_righthand))
 7579                           399                 :           4564 :             continue;
                                400                 :                : 
                                401                 :                :         /*
                                402                 :                :          * Also, not relevant if SJ is already done within either input.
                                403                 :                :          */
 6611                           404   [ +  +  +  + ]:         323131 :         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
                                405                 :         145132 :             bms_is_subset(sjinfo->min_righthand, rel1->relids))
 7579                           406                 :          65320 :             continue;
 6611                           407   [ +  +  +  + ]:         126564 :         if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
                                408                 :          13885 :             bms_is_subset(sjinfo->min_righthand, rel2->relids))
 7579                           409                 :           7138 :             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                 :                :          */
 6268                           417         [ +  + ]:         105541 :         if (sjinfo->jointype == JOIN_SEMI)
                                418                 :                :         {
                                419         [ +  + ]:           8706 :             if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
                                420         [ +  + ]:           1212 :                 !bms_equal(sjinfo->syn_righthand, rel1->relids))
                                421                 :            475 :                 continue;
                                422         [ +  + ]:           8231 :             if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
                                423         [ +  + ]:           5711 :                 !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                 :                :          */
 6611                           434   [ +  +  +  + ]:         184627 :         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
                                435                 :          79710 :             bms_is_subset(sjinfo->min_righthand, rel2->relids))
                                436                 :                :         {
                                437         [ -  + ]:          74015 :             if (match_sjinfo)
 6884 bruce@momjian.us          438                 :          19086 :                 return false;   /* invalid join path */
 6611 tgl@sss.pgh.pa.us         439                 :          74015 :             match_sjinfo = sjinfo;
                                440                 :          74015 :             reversed = false;
                                441                 :                :         }
                                442   [ +  +  +  + ]:          37424 :         else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
                                443                 :           6522 :                  bms_is_subset(sjinfo->min_righthand, rel1->relids))
                                444                 :                :         {
                                445         [ -  + ]:           5499 :             if (match_sjinfo)
 6884 bruce@momjian.us          446                 :UBC           0 :                 return false;   /* invalid join path */
 6611 tgl@sss.pgh.pa.us         447                 :CBC        5499 :             match_sjinfo = sjinfo;
                                448                 :           5499 :             reversed = true;
                                449                 :                :         }
 6511                           450   [ +  +  +  + ]:          27842 :         else if (sjinfo->jointype == JOIN_SEMI &&
 6505                           451         [ +  + ]:           2900 :                  bms_equal(sjinfo->syn_righthand, rel2->relids) &&
  397 rguo@postgresql.org       452                 :            461 :                  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                 :                :              */
 6511 tgl@sss.pgh.pa.us         476         [ +  + ]:            274 :             if (match_sjinfo)
                                477                 :              5 :                 return false;   /* invalid join path */
                                478                 :            269 :             match_sjinfo = sjinfo;
                                479                 :            269 :             reversed = false;
 6272                           480                 :            269 :             unique_ified = true;
                                481                 :                :         }
 6511                           482   [ +  +  +  + ]:          27294 :         else if (sjinfo->jointype == JOIN_SEMI &&
 6505                           483         [ +  + ]:           2360 :                  bms_equal(sjinfo->syn_righthand, rel1->relids) &&
  397 rguo@postgresql.org       484                 :            195 :                  create_unique_paths(root, rel1, sjinfo) != NULL)
                                485                 :                :         {
                                486                 :                :             /* Reversed semijoin case */
 6511 tgl@sss.pgh.pa.us         487         [ -  + ]:             86 :             if (match_sjinfo)
 6511 tgl@sss.pgh.pa.us         488                 :UBC           0 :                 return false;   /* invalid join path */
 6511 tgl@sss.pgh.pa.us         489                 :CBC          86 :             match_sjinfo = sjinfo;
                                490                 :             86 :             reversed = true;
 6272                           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                 :                :              */
 4057                           512   [ +  +  +  + ]:          30873 :             if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
                                513                 :           5830 :                 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                 :                :              */
 4063                           522   [ +  +  +  + ]:          36413 :             if (sjinfo->jointype != JOIN_LEFT ||
 4064                           523                 :          11515 :                 bms_overlap(joinrelids, sjinfo->min_lefthand))
                                524                 :          19081 :                 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                 :                :              */
 4063                           532                 :           5817 :             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   [ +  +  +  + ]:         261993 :     if (must_be_leftjoin &&
                                546                 :           3051 :         (match_sjinfo == NULL ||
                                547         [ +  - ]:           3051 :          match_sjinfo->jointype != JOIN_LEFT ||
                                548         [ -  + ]:           3051 :          !match_sjinfo->lhs_strict))
 6904                           549                 :           1349 :         return false;           /* invalid join path */
                                550                 :                : 
                                551                 :                :     /*
                                552                 :                :      * We also have to check for constraints imposed by LATERAL references.
                                553                 :                :      */
 3936                           554         [ +  + ]:         260644 :     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                 :          26370 :         lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
                                573                 :          26370 :         lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
                                574   [ +  +  +  + ]:          26370 :         if (lateral_fwd && lateral_rev)
                                575                 :             23 :             return false;       /* have lateral refs in both directions */
                                576         [ +  + ]:          26347 :         if (lateral_fwd)
                                577                 :                :         {
                                578                 :                :             /* has to be implemented as nestloop with rel1 on left */
 5138                           579   [ +  +  +  - ]:           9734 :             if (match_sjinfo &&
 4069                           580         [ +  + ]:           1350 :                 (reversed ||
                                581                 :           1335 :                  unique_ified ||
                                582         [ -  + ]:           1335 :                  match_sjinfo->jointype == JOIN_FULL))
 5138                           583                 :             15 :                 return false;   /* not implementable as nestloop */
                                584                 :                :             /* check there is a direct reference from rel2 to rel1 */
 3936                           585         [ +  + ]:           9719 :             if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
                                586                 :             35 :                 return false;   /* only indirect refs, so reject */
                                587                 :                :         }
                                588         [ +  + ]:          16613 :         else if (lateral_rev)
                                589                 :                :         {
                                590                 :                :             /* has to be implemented as nestloop with rel2 on left */
 5138                           591         [ +  + ]:           2581 :             if (match_sjinfo &&
 4069                           592   [ +  -  +  - ]:             85 :                 (!reversed ||
                                593                 :             85 :                  unique_ified ||
                                594         [ -  + ]:             85 :                  match_sjinfo->jointype == JOIN_FULL))
 5138 tgl@sss.pgh.pa.us         595                 :UBC           0 :                 return false;   /* not implementable as nestloop */
                                596                 :                :             /* check there is a direct reference from rel1 to rel2 */
 3936 tgl@sss.pgh.pa.us         597         [ -  + ]:CBC        2581 :             if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
 3936 tgl@sss.pgh.pa.us         598                 :UBC           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                 :                :          */
 3936 tgl@sss.pgh.pa.us         614                 :CBC       26297 :         join_lateral_rels = min_join_parameterization(root, joinrelids,
                                615                 :                :                                                       rel1, rel2);
                                616         [ +  + ]:          26297 :         if (join_lateral_rels)
                                617                 :                :         {
                                618                 :           4163 :             Relids      join_plus_rhs = bms_copy(joinrelids);
                                619                 :                :             bool        more;
                                620                 :                : 
                                621                 :                :             do
                                622                 :                :             {
                                623                 :           4677 :                 more = false;
                                624   [ +  +  +  +  :           6740 :                 foreach(l, root->join_info_list)
                                              +  + ]
                                625                 :                :                 {
                                626                 :           2063 :                     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
                                627                 :                : 
                                628                 :                :                     /* ignore full joins --- their ordering is predetermined */
 2722                           629         [ +  + ]:           2063 :                     if (sjinfo->jointype == JOIN_FULL)
                                630                 :             15 :                         continue;
                                631                 :                : 
 3936                           632         [ +  + ]:           2048 :                     if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
                                633         [ +  + ]:           1723 :                         !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
                                634                 :                :                     {
                                635                 :            639 :                         join_plus_rhs = bms_add_members(join_plus_rhs,
 3378                           636                 :            639 :                                                         sjinfo->min_righthand);
 3936                           637                 :            639 :                         more = true;
                                638                 :                :                     }
                                639                 :                :                 }
                                640         [ +  + ]:           4677 :             } while (more);
                                641         [ +  + ]:           4163 :             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                 :                :              */
   16 rguo@postgresql.org       656         [ +  + ]:           3719 :             if (bms_overlap(join_lateral_rels, root->outer_join_rels))
                                657                 :                :             {
                                658   [ +  -  +  +  :             90 :                 foreach(l, root->join_info_list)
                                              +  + ]
                                659                 :                :                 {
                                660                 :             70 :                     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
                                661                 :                : 
                                662         [ +  + ]:             70 :                     if (!bms_is_member(sjinfo->ojrelid, join_lateral_rels))
                                663                 :             30 :                         continue;
                                664   [ +  -  +  + ]:             80 :                     if (bms_overlap(join_plus_rhs, sjinfo->min_lefthand) ||
                                665                 :             40 :                         bms_overlap(join_plus_rhs, sjinfo->min_righthand))
                                666                 :             20 :                         return false;   /* OJ can't be formed outside join */
                                667                 :                :                 }
                                668                 :                :             }
                                669                 :                :         }
                                670                 :                :     }
                                671                 :                : 
                                672                 :                :     /* Otherwise, it's a valid join */
 6611 tgl@sss.pgh.pa.us         673                 :         260107 :     *sjinfo_p = match_sjinfo;
                                674                 :         260107 :     *reversed_p = reversed;
 6904                           675                 :         260107 :     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
  909 amitlan@postgresql.o      690                 :         939009 : init_dummy_sjinfo(SpecialJoinInfo *sjinfo, Relids left_relids,
                                691                 :                :                   Relids right_relids)
                                692                 :                : {
                                693                 :         939009 :     sjinfo->type = T_SpecialJoinInfo;
                                694                 :         939009 :     sjinfo->min_lefthand = left_relids;
                                695                 :         939009 :     sjinfo->min_righthand = right_relids;
                                696                 :         939009 :     sjinfo->syn_lefthand = left_relids;
                                697                 :         939009 :     sjinfo->syn_righthand = right_relids;
                                698                 :         939009 :     sjinfo->jointype = JOIN_INNER;
                                699                 :         939009 :     sjinfo->ojrelid = 0;
                                700                 :         939009 :     sjinfo->commute_above_l = NULL;
                                701                 :         939009 :     sjinfo->commute_above_r = NULL;
                                702                 :         939009 :     sjinfo->commute_below_l = NULL;
                                703                 :         939009 :     sjinfo->commute_below_r = NULL;
                                704                 :                :     /* we don't bother trying to make the remaining fields valid */
                                705                 :         939009 :     sjinfo->lhs_strict = false;
                                706                 :         939009 :     sjinfo->semi_can_btree = false;
                                707                 :         939009 :     sjinfo->semi_can_hash = false;
                                708                 :         939009 :     sjinfo->semi_operators = NIL;
                                709                 :         939009 :     sjinfo->semi_rhs_exprs = NIL;
                                710                 :         939009 : }
                                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 *
 6904 tgl@sss.pgh.pa.us         725                 :         280660 : make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                                726                 :                : {
                                727                 :                :     Relids      joinrelids;
                                728                 :                :     SpecialJoinInfo *sjinfo;
                                729                 :                :     bool        reversed;
 1222                           730                 :         280660 :     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. */
 6904                           736         [ -  + ]:         280660 :     Assert(!bms_overlap(rel1->relids, rel2->relids));
                                737                 :                : 
                                738                 :                :     /* Construct Relids set that identifies the joinrel (without OJ as yet). */
                                739                 :         280660 :     joinrelids = bms_union(rel1->relids, rel2->relids);
                                740                 :                : 
                                741                 :                :     /* Check validity and determine join type. */
 6611                           742         [ +  + ]:         280660 :     if (!join_is_legal(root, rel1, rel2, joinrelids,
                                743                 :                :                        &sjinfo, &reversed))
                                744                 :                :     {
                                745                 :                :         /* invalid join path */
 6904                           746                 :          20778 :         bms_free(joinrelids);
                                747                 :          20778 :         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                 :                :      */
 1222                           754                 :         259882 :     joinrelids = add_outer_joins_to_relids(root, joinrelids, sjinfo,
                                755                 :                :                                            &pushed_down_joins);
                                756                 :                : 
                                757                 :                :     /* Swap rels if needed to match the join info. */
 6611                           758         [ +  + ]:         259882 :     if (reversed)
                                759                 :                :     {
                                760                 :           5555 :         RelOptInfo *trel = rel1;
                                761                 :                : 
                                762                 :           5555 :         rel1 = rel2;
                                763                 :           5555 :         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         [ +  + ]:         259882 :     if (sjinfo == NULL)
                                772                 :                :     {
                                773                 :         180289 :         sjinfo = &sjinfo_data;
  909 amitlan@postgresql.o      774                 :         180289 :         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                 :                :      */
 1222 tgl@sss.pgh.pa.us         781                 :         259882 :     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                 :                :      */
 6754                           789         [ +  + ]:         259882 :     if (is_dummy_rel(joinrel))
                                790                 :                :     {
                                791                 :            420 :         bms_free(joinrelids);
                                792                 :            420 :         return joinrel;
                                793                 :                :     }
                                794                 :                : 
                                795                 :                :     /* Build a grouped join relation for 'joinrel' if possible. */
  347 rguo@postgresql.org       796                 :         259462 :     make_grouped_join_rel(root, rel1, rel2, joinrel, sjinfo,
                                797                 :                :                           restrictlist);
                                798                 :                : 
                                799                 :                :     /* Add paths to the join relation. */
 3477 rhaas@postgresql.org      800                 :         259462 :     populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
                                801                 :                :                                 restrictlist);
                                802                 :                : 
                                803                 :         259462 :     bms_free(joinrelids);
                                804                 :                : 
                                805                 :         259462 :     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
 1222 tgl@sss.pgh.pa.us         826                 :         266491 : 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   [ +  +  +  + ]:         266491 :     if (sjinfo == NULL || sjinfo->ojrelid == 0)
                                832                 :         203710 :         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         [ +  + ]:          62781 :     if (sjinfo->jointype != JOIN_LEFT)
                                841                 :           2007 :         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         [ +  + ]:          60774 :     if (!bms_is_subset(sjinfo->commute_below_l, input_relids))
                                851                 :           2750 :         return input_relids;
                                852                 :                : 
                                853                 :                :     /* OK to add OJ's own relid */
                                854                 :          58024 :     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         [ +  + ]:          58024 :     if (sjinfo->commute_above_l)
                                862                 :                :     {
                                863                 :           9587 :         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   [ +  -  +  +  :          32891 :         foreach(lc, root->join_info_list)
                                              +  + ]
                                874                 :                :         {
                                875                 :          23304 :             SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
                                876                 :                : 
                                877         [ +  + ]:          23304 :             if (othersj == sjinfo ||
                                878   [ +  +  -  + ]:          13717 :                 othersj->ojrelid == 0 || othersj->jointype != JOIN_LEFT)
                                879                 :           9597 :                 continue;       /* definitely not interesting */
                                880                 :                : 
                                881         [ +  + ]:          13707 :             if (!bms_is_member(othersj->ojrelid, commute_above_rels))
                                882                 :           3998 :                 continue;
                                883                 :                : 
                                884                 :                :             /* Add it if not already present but conditions now satisfied */
                                885   [ +  -  +  + ]:          19418 :             if (!bms_is_member(othersj->ojrelid, input_relids) &&
                                886         [ +  + ]:          19388 :                 bms_is_subset(othersj->min_lefthand, input_relids) &&
                                887         [ +  + ]:          14579 :                 bms_is_subset(othersj->min_righthand, input_relids) &&
                                888                 :           4900 :                 bms_is_subset(othersj->commute_below_l, input_relids))
                                889                 :                :             {
                                890                 :           4870 :                 input_relids = bms_add_member(input_relids, othersj->ojrelid);
                                891                 :                :                 /* report such pushed down outer joins, if asked */
                                892         [ +  - ]:           4870 :                 if (pushed_down_joins != NULL)
                                893                 :           4870 :                     *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                 :           4870 :                 commute_above_rels = bms_add_members(commute_above_rels,
                                902                 :           4870 :                                                      othersj->commute_above_l);
                                903                 :                :             }
                                904                 :                :         }
                                905                 :                :     }
                                906                 :                : 
                                907                 :          58024 :     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
  347 rguo@postgresql.org       942                 :         275911 : 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         [ +  + ]:         275911 :     if (root->agg_clause_list == NIL ||
                                958         [ +  + ]:          15627 :         root->group_expr_list == NIL)
                                959                 :         260511 :         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);
  341                          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                 :                :              */
  347                          1023                 :            560 :             grouped_rel->rows = agg_info->grouped_rows;
  341                          1024                 :            560 :             agg_info->apply_agg_at = bms_copy(joinrel->relids);
                               1025                 :                :         }
                               1026                 :                : 
  347                          1027                 :          14057 :         grouped_rel->agg_info = agg_info;
                               1028                 :          14057 :         joinrel->grouped_rel = grouped_rel;
                               1029                 :                :     }
                               1030                 :                : 
                               1031         [ -  + ]:          14537 :     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))
  347 rguo@postgresql.org      1035                 :UBC           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                 :                :      */
  347 rguo@postgresql.org      1041         [ +  + ]:CBC       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                 :                :      */
  341                          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. */
  347                          1071   [ +  -  -  + ]:            240 :             set_joinrel_size_estimates(root, grouped_rel,
                               1072                 :                :                                        rel1_empty ? rel1 : grouped_rel1,
                               1073                 :                :                                        rel2_empty ? rel2 : grouped_rel2,
                               1074                 :                :                                        sjinfo, restrictlist);
  341                          1075                 :            240 :             grouped_rel->agg_info->apply_agg_at = apply_agg_at;
                               1076                 :                :         }
                               1077                 :                :         else
  347 rguo@postgresql.org      1078                 :UBC           0 :             return;
                               1079                 :                :     }
                               1080                 :                : 
                               1081                 :                :     /* Make paths for the grouped join relation. */
  347 rguo@postgresql.org      1082   [ +  +  +  + ]:CBC       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
 3477 rhaas@postgresql.org     1098                 :         289648 : 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                 :                :      */
 6611 tgl@sss.pgh.pa.us        1122   [ +  +  +  +  :         289648 :     switch (sjinfo->jointype)
                                              +  - ]
                               1123                 :                :     {
 9504                          1124                 :         207409 :         case JOIN_INNER:
 6608                          1125   [ +  +  +  +  :         414793 :             if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
                                              +  + ]
 3075                          1126                 :         207384 :                 restriction_is_constant_false(restrictlist, joinrel, false))
                               1127                 :                :             {
 6608                          1128                 :            180 :                 mark_dummy_rel(joinrel);
 6754                          1129                 :            180 :                 break;
                               1130                 :                :             }
 6611                          1131                 :         207229 :             add_paths_to_joinrel(root, joinrel, rel1, rel2,
                               1132                 :                :                                  JOIN_INNER, sjinfo,
                               1133                 :                :                                  restrictlist);
                               1134                 :         207229 :             add_paths_to_joinrel(root, joinrel, rel2, rel1,
                               1135                 :                :                                  JOIN_INNER, sjinfo,
                               1136                 :                :                                  restrictlist);
 9504                          1137                 :         207229 :             break;
                               1138                 :          62181 :         case JOIN_LEFT:
 5850                          1139   [ +  +  +  + ]:         124317 :             if (is_dummy_rel(rel1) ||
 3075                          1140                 :          62136 :                 restriction_is_constant_false(restrictlist, joinrel, true))
                               1141                 :                :             {
 6608                          1142                 :             69 :                 mark_dummy_rel(joinrel);
 6754                          1143                 :             69 :                 break;
                               1144                 :                :             }
 3075                          1145   [ +  +  +  + ]:          62303 :             if (restriction_is_constant_false(restrictlist, joinrel, false) &&
 6608                          1146                 :            191 :                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
                               1147                 :            171 :                 mark_dummy_rel(rel2);
 6611                          1148                 :          62112 :             add_paths_to_joinrel(root, joinrel, rel1, rel2,
                               1149                 :                :                                  JOIN_LEFT, sjinfo,
                               1150                 :                :                                  restrictlist);
                               1151                 :          62112 :             add_paths_to_joinrel(root, joinrel, rel2, rel1,
                               1152                 :                :                                  JOIN_RIGHT, sjinfo,
                               1153                 :                :                                  restrictlist);
 9504                          1154                 :          62112 :             break;
                               1155                 :           1433 :         case JOIN_FULL:
 5850                          1156   [ -  +  -  -  :           2866 :             if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
                                              +  + ]
 3075                          1157                 :           1433 :                 restriction_is_constant_false(restrictlist, joinrel, true))
                               1158                 :                :             {
 6608                          1159                 :             10 :                 mark_dummy_rel(joinrel);
 6754                          1160                 :             10 :                 break;
                               1161                 :                :             }
 6611                          1162                 :           1423 :             add_paths_to_joinrel(root, joinrel, rel1, rel2,
                               1163                 :                :                                  JOIN_FULL, sjinfo,
                               1164                 :                :                                  restrictlist);
                               1165                 :           1423 :             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                 :                :              */
 5743                          1176         [ -  + ]:           1423 :             if (joinrel->pathlist == NIL)
 5743 tgl@sss.pgh.pa.us        1177         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               1178                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1179                 :                :                          errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
 8644 tgl@sss.pgh.pa.us        1180                 :CBC        1423 :             break;
 6611                          1181                 :           6347 :         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                 :                :              */
 6511                          1189   [ +  +  +  - ]:          12385 :             if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
                               1190                 :           6038 :                 bms_is_subset(sjinfo->min_righthand, rel2->relids))
                               1191                 :                :             {
                               1192   [ +  +  +  -  :          12071 :                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
                                              +  + ]
 3075                          1193                 :           6033 :                     restriction_is_constant_false(restrictlist, joinrel, false))
                               1194                 :                :                 {
 6511                          1195                 :             10 :                     mark_dummy_rel(joinrel);
                               1196                 :             10 :                     break;
                               1197                 :                :                 }
                               1198                 :           6028 :                 add_paths_to_joinrel(root, joinrel, rel1, rel2,
                               1199                 :                :                                      JOIN_SEMI, sjinfo,
                               1200                 :                :                                      restrictlist);
  807 rguo@postgresql.org      1201                 :           6028 :                 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                 :                :              */
 6611 tgl@sss.pgh.pa.us        1214   [ +  -  +  + ]:          12674 :             if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
  397 rguo@postgresql.org      1215                 :           6337 :                 (unique_rel2 = create_unique_paths(root, rel2, sjinfo)) != NULL)
                               1216                 :                :             {
 5850 tgl@sss.pgh.pa.us        1217   [ +  -  +  -  :           9776 :                 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
                                              -  + ]
 3075                          1218                 :           4888 :                     restriction_is_constant_false(restrictlist, joinrel, false))
                               1219                 :                :                 {
 5850 tgl@sss.pgh.pa.us        1220                 :UBC           0 :                     mark_dummy_rel(joinrel);
                               1221                 :              0 :                     break;
                               1222                 :                :                 }
  397 rguo@postgresql.org      1223                 :CBC        4888 :                 add_paths_to_joinrel(root, joinrel, rel1, unique_rel2,
                               1224                 :                :                                      JOIN_UNIQUE_INNER, sjinfo,
                               1225                 :                :                                      restrictlist);
                               1226                 :           4888 :                 add_paths_to_joinrel(root, joinrel, unique_rel2, rel1,
                               1227                 :                :                                      JOIN_UNIQUE_OUTER, sjinfo,
                               1228                 :                :                                      restrictlist);
                               1229                 :                :             }
 8644 tgl@sss.pgh.pa.us        1230                 :           6337 :             break;
 6611                          1231                 :          12278 :         case JOIN_ANTI:
 5850                          1232   [ +  -  -  + ]:          24556 :             if (is_dummy_rel(rel1) ||
 3075                          1233                 :          12278 :                 restriction_is_constant_false(restrictlist, joinrel, true))
                               1234                 :                :             {
 6608 tgl@sss.pgh.pa.us        1235                 :UBC           0 :                 mark_dummy_rel(joinrel);
 6754                          1236                 :              0 :                 break;
                               1237                 :                :             }
 3075 tgl@sss.pgh.pa.us        1238   [ -  +  -  - ]:CBC       12278 :             if (restriction_is_constant_false(restrictlist, joinrel, false) &&
 6608 tgl@sss.pgh.pa.us        1239                 :UBC           0 :                 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
                               1240                 :              0 :                 mark_dummy_rel(rel2);
 6611 tgl@sss.pgh.pa.us        1241                 :CBC       12278 :             add_paths_to_joinrel(root, joinrel, rel1, rel2,
                               1242                 :                :                                  JOIN_ANTI, sjinfo,
                               1243                 :                :                                  restrictlist);
 1264                          1244                 :          12278 :             add_paths_to_joinrel(root, joinrel, rel2, rel1,
                               1245                 :                :                                  JOIN_RIGHT_ANTI, sjinfo,
                               1246                 :                :                                  restrictlist);
 8644                          1247                 :          12278 :             break;
 9504 tgl@sss.pgh.pa.us        1248                 :UBC           0 :         default:
                               1249                 :                :             /* other values not expected here */
 6611                          1250         [ #  # ]:              0 :             elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
                               1251                 :                :             break;
                               1252                 :                :     }
                               1253                 :                : 
                               1254                 :                :     /* Apply partitionwise join technique, if possible. */
 3138 peter_e@gmx.net          1255                 :CBC      289648 :     try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
11030 scrappy@hub.org          1256                 :         289648 : }
                               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
 7156 tgl@sss.pgh.pa.us        1280                 :          60199 : have_join_order_restriction(PlannerInfo *root,
                               1281                 :                :                             RelOptInfo *rel1, RelOptInfo *rel2)
                               1282                 :                : {
 6904                          1283                 :          60199 :     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                 :                :      */
 3936                          1290   [ +  +  +  + ]:         112945 :     if (bms_overlap(rel1->relids, rel2->direct_lateral_relids) ||
                               1291                 :          52746 :         bms_overlap(rel2->relids, rel1->direct_lateral_relids))
                               1292                 :           8436 :         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   [ +  +  +  +  :          53551 :     foreach(l, root->placeholder_list)
                                              +  + ]
                               1302                 :                :     {
                               1303                 :           1838 :         PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
                               1304                 :                : 
                               1305   [ +  +  +  + ]:           2193 :         if (bms_is_subset(rel1->relids, phinfo->ph_eval_at) &&
                               1306                 :            355 :             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                 :                :      */
 6611                          1318   [ +  +  +  +  :         130497 :     foreach(l, root->join_info_list)
                                              +  + ]
                               1319                 :                :     {
                               1320                 :          80082 :         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
                               1321                 :                : 
                               1322                 :                :         /* ignore full joins --- other mechanisms handle them */
                               1323         [ +  + ]:          80082 :         if (sjinfo->jointype == JOIN_FULL)
 7156                          1324                 :             35 :             continue;
                               1325                 :                : 
                               1326                 :                :         /* Can we perform the SJ with these rels? */
 6611                          1327   [ +  +  +  + ]:          97851 :         if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
                               1328                 :          17804 :             bms_is_subset(sjinfo->min_righthand, rel2->relids))
                               1329                 :                :         {
 6904                          1330                 :            885 :             result = true;
                               1331                 :            885 :             break;
                               1332                 :                :         }
 6611                          1333   [ +  +  +  + ]:          84130 :         if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
                               1334                 :           4968 :             bms_is_subset(sjinfo->min_righthand, rel1->relids))
                               1335                 :                :         {
 6904                          1336                 :            250 :             result = true;
                               1337                 :            250 :             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                 :                :          */
 6611                          1345   [ +  +  +  + ]:          94807 :         if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
                               1346                 :          15895 :             bms_overlap(sjinfo->min_righthand, rel2->relids))
                               1347                 :                :         {
 6904                          1348                 :            103 :             result = true;
                               1349                 :            103 :             break;
                               1350                 :                :         }
                               1351                 :                : 
                               1352                 :                :         /* Likewise for the LHS. */
 6611                          1353   [ +  +  +  + ]:          98320 :         if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
                               1354                 :          19511 :             bms_overlap(sjinfo->min_lefthand, rel2->relids))
                               1355                 :                :         {
 6904                          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         [ +  + ]:          51713 :     if (result)
                               1371                 :                :     {
                               1372   [ +  +  +  + ]:           2471 :         if (has_legal_joinclause(root, rel1) ||
                               1373                 :           1173 :             has_legal_joinclause(root, rel2))
                               1374                 :            225 :             result = false;
                               1375                 :                :     }
                               1376                 :                : 
                               1377                 :          51713 :     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
 7156                          1393                 :          21801 : has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
                               1394                 :                : {
                               1395                 :                :     ListCell   *l;
                               1396                 :                : 
 3936                          1397   [ +  +  +  + ]:          21801 :     if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
                               1398                 :          10360 :         return true;
                               1399                 :                : 
                               1400   [ +  +  +  +  :          12271 :     foreach(l, root->placeholder_list)
                                              +  + ]
                               1401                 :                :     {
                               1402                 :            875 :         PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
                               1403                 :                : 
                               1404         [ +  + ]:            875 :         if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
                               1405         [ +  + ]:            210 :             !bms_equal(rel->relids, phinfo->ph_eval_at))
 5138                          1406                 :             45 :             return true;
                               1407                 :                :     }
                               1408                 :                : 
 6611                          1409   [ +  +  +  +  :          12206 :     foreach(l, root->join_info_list)
                                              +  + ]
                               1410                 :                :     {
                               1411                 :           2637 :         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
                               1412                 :                : 
                               1413                 :                :         /* ignore full joins --- other mechanisms preserve their ordering */
                               1414         [ +  + ]:           2637 :         if (sjinfo->jointype == JOIN_FULL)
 7156                          1415                 :             70 :             continue;
                               1416                 :                : 
                               1417                 :                :         /* ignore if SJ is already contained in rel */
 6611                          1418   [ +  +  +  + ]:           4013 :         if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
                               1419                 :           1446 :             bms_is_subset(sjinfo->min_righthand, rel->relids))
 7156                          1420                 :            422 :             continue;
                               1421                 :                : 
                               1422                 :                :         /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
 6611                          1423   [ +  +  +  + ]:           3246 :         if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
                               1424                 :           1101 :             bms_overlap(sjinfo->min_righthand, rel->relids))
 7156                          1425                 :           1827 :             return true;
                               1426                 :                :     }
                               1427                 :                : 
                               1428                 :           9569 :     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
 6904                          1449                 :           2471 : has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
                               1450                 :                : {
                               1451                 :                :     ListCell   *lc;
                               1452                 :                : 
 6827                          1453   [ +  -  +  +  :           9488 :     foreach(lc, root->initial_rels)
                                              +  + ]
                               1454                 :                :     {
                               1455                 :           7242 :         RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
                               1456                 :                : 
                               1457                 :                :         /* ignore rels that are already in "rel" */
 6904                          1458         [ +  + ]:           7242 :         if (bms_overlap(rel->relids, rel2->relids))
                               1459                 :           3050 :             continue;
                               1460                 :                : 
                               1461         [ +  + ]:           4192 :         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                 :            419 :             joinrelids = bms_union(rel->relids, rel2->relids);
                               1469                 :                : 
 6611                          1470         [ +  + ]:            419 :             if (join_is_legal(root, rel, rel2, joinrelids,
                               1471                 :                :                               &sjinfo, &reversed))
                               1472                 :                :             {
                               1473                 :                :                 /* Yes, this will work */
 6904                          1474                 :            225 :                 bms_free(joinrelids);
                               1475                 :            225 :                 return true;
                               1476                 :                :             }
                               1477                 :                : 
                               1478                 :            194 :             bms_free(joinrelids);
                               1479                 :                :         }
                               1480                 :                :     }
                               1481                 :                : 
                               1482                 :           2246 :     return false;
                               1483                 :                : }
                               1484                 :                : 
                               1485                 :                : 
                               1486                 :                : /*
                               1487                 :                :  * is_dummy_rel --- has relation been proven empty?
                               1488                 :                :  */
                               1489                 :                : bool
 6754                          1490                 :        2338581 : 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                 :                :      */
 2754                          1499         [ +  + ]:        2338581 :     if (rel->pathlist == NIL)
                               1500                 :        1122914 :         return false;
                               1501                 :        1215667 :     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         [ +  + ]:        1275417 :         if (IsA(path, ProjectionPath))
                               1512                 :          52064 :             path = ((ProjectionPath *) path)->subpath;
                               1513         [ +  + ]:        1223353 :         else if (IsA(path, ProjectSetPath))
                               1514                 :           7686 :             path = ((ProjectSetPath *) path)->subpath;
                               1515                 :                :         else
                               1516                 :        1215667 :             break;
                               1517                 :                :     }
                               1518   [ +  +  +  + ]:        1215667 :     if (IS_DUMMY_APPEND(path))
                               1519                 :           5129 :         return true;
                               1520                 :        1210538 :     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
 6608                          1539                 :            611 : mark_dummy_rel(RelOptInfo *rel)
                               1540                 :                : {
                               1541                 :                :     MemoryContext oldcontext;
  222 rhaas@postgresql.org     1542                 :            611 :     AppendPathInput in = {0};
                               1543                 :                : 
                               1544                 :                :     /* Already marked? */
 5639 tgl@sss.pgh.pa.us        1545         [ +  + ]:            611 :     if (is_dummy_rel(rel))
                               1546                 :             15 :         return;
                               1547                 :                : 
                               1548                 :                :     /* No, so choose correct context to make the dummy path in */
                               1549                 :            596 :     oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
                               1550                 :                : 
                               1551                 :                :     /* Set dummy size estimate */
 6754                          1552                 :            596 :     rel->rows = 0;
                               1553                 :                : 
                               1554                 :                :     /* Evict any previously chosen paths */
                               1555                 :            596 :     rel->pathlist = NIL;
 3896 rhaas@postgresql.org     1556                 :            596 :     rel->partial_pathlist = NIL;
                               1557                 :                : 
                               1558                 :                :     /* Set up the dummy path */
  222                          1559                 :            596 :     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 */
 6608 tgl@sss.pgh.pa.us        1564                 :            596 :     set_cheapest(rel);
                               1565                 :                : 
 5639                          1566                 :            596 :     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
 3075                          1583                 :         368542 : 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                 :                :      */
 6608                          1595   [ +  +  +  +  :         785126 :     foreach(lc, restrictlist)
                                              +  + ]
                               1596                 :                :     {
 3450                          1597                 :         416969 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
                               1598                 :                : 
 3075                          1599   [ +  +  +  +  :         416969 :         if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
                                              +  - ]
 5850                          1600                 :          97301 :             continue;
                               1601                 :                : 
 6608                          1602   [ +  -  +  + ]:         319668 :         if (rinfo->clause && IsA(rinfo->clause, Const))
                               1603                 :                :         {
 6310 bruce@momjian.us         1604                 :           4790 :             Const      *con = (Const *) rinfo->clause;
                               1605                 :                : 
                               1606                 :                :             /* constant NULL is as good as constant FALSE for our purposes */
 6608 tgl@sss.pgh.pa.us        1607         [ +  + ]:           4790 :             if (con->constisnull)
                               1608                 :            385 :                 return true;
                               1609         [ +  + ]:           4700 :             if (!DatumGetBool(con->constvalue))
                               1610                 :            295 :                 return true;
                               1611                 :                :         }
                               1612                 :                :     }
                               1613                 :         368157 :     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
 3138 peter_e@gmx.net          1637                 :         289648 : try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
                               1638                 :                :                        RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
                               1639                 :                :                        List *parent_restrictlist)
                               1640                 :                : {
 2799 efujita@postgresql.o     1641   [ +  +  +  + ]:         289648 :     bool        rel1_is_simple = IS_SIMPLE_REL(rel1);
                               1642   [ +  +  +  + ]:         289648 :     bool        rel2_is_simple = IS_SIMPLE_REL(rel2);
 2356                          1643                 :         289648 :     List       *parts1 = NIL;
                               1644                 :         289648 :     List       *parts2 = NIL;
                               1645                 :         289648 :     ListCell   *lcr1 = NULL;
                               1646                 :         289648 :     ListCell   *lcr2 = NULL;
                               1647                 :                :     int         cnt_parts;
                               1648                 :                : 
                               1649                 :                :     /* Guard against stack overflow due to overly deep partition hierarchy. */
 3271 rhaas@postgresql.org     1650                 :         289648 :     check_stack_depth();
                               1651                 :                : 
                               1652                 :                :     /* Nothing to do, if the join relation is not partitioned. */
 2356 efujita@postgresql.o     1653   [ +  +  +  + ]:         289648 :     if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
 3271 rhaas@postgresql.org     1654                 :         283184 :         return;
                               1655                 :                : 
                               1656                 :                :     /* The join relation should have consider_partitionwise_join set. */
 2942 efujita@postgresql.o     1657         [ -  + ]:           6579 :     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                 :                :      */
 2356                          1663   [ +  -  +  +  :           6579 :     if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
                                     +  +  +  -  +  
                                     -  +  -  +  -  
                                     +  -  +  -  -  
                                                 + ]
                               1664                 :             15 :         return;
                               1665                 :                : 
 3271 rhaas@postgresql.org     1666   [ +  -  +  -  :           6564 :     Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
                                     +  -  +  -  +  
                                     -  +  -  +  -  
                                     +  -  +  -  +  
                                        -  +  -  -  
                                                 + ]
                               1667                 :                : 
                               1668                 :                :     /* The joining relations should have consider_partitionwise_join set. */
 2942 efujita@postgresql.o     1669   [ +  -  -  + ]:           6564 :     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                 :                :      */
 3271 rhaas@postgresql.org     1676   [ +  -  -  + ]:           6564 :     Assert(joinrel->part_scheme == rel1->part_scheme &&
                               1677                 :                :            joinrel->part_scheme == rel2->part_scheme);
                               1678                 :                : 
 2356 efujita@postgresql.o     1679   [ +  +  -  + ]:           6564 :     Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
                               1680                 :                : 
                               1681                 :           6564 :     compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
                               1682                 :                :                              &parts1, &parts2);
                               1683                 :                : 
                               1684         [ +  + ]:           6564 :     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         [ +  + ]:          23075 :     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         [ +  + ]:          16611 :         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                 :          14936 :             child_rel1 = rel1->part_rels[cnt_parts];
                               1718                 :          14936 :             child_rel2 = rel2->part_rels[cnt_parts];
                               1719                 :                :         }
                               1720                 :                : 
                               1721   [ +  +  -  + ]:          16611 :         rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
                               1722   [ +  +  -  + ]:          16611 :         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                 :                :          */
 2731 tgl@sss.pgh.pa.us        1731   [ +  +  +  - ]:          16611 :         switch (parent_sjinfo->jointype)
                               1732                 :                :         {
                               1733                 :          14268 :             case JOIN_INNER:
                               1734                 :                :             case JOIN_SEMI:
                               1735   [ +  +  +  + ]:          14268 :                 if (rel1_empty || rel2_empty)
                               1736                 :             62 :                     continue;   /* ignore this join segment */
                               1737                 :          14228 :                 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)
 2731 tgl@sss.pgh.pa.us        1745                 :UBC           0 :                     continue;   /* ignore this join segment */
 2731 tgl@sss.pgh.pa.us        1746                 :CBC         599 :                 break;
 2731 tgl@sss.pgh.pa.us        1747                 :UBC           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                 :                :          */
 2731 tgl@sss.pgh.pa.us        1759   [ +  +  +  + ]:CBC       16549 :         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                 :                :          */
 2799 efujita@postgresql.o     1775   [ +  +  -  + ]:          16449 :         if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
                               1776                 :                :         {
 2799 efujita@postgresql.o     1777         [ #  # ]:UBC           0 :             Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
                               1778         [ #  # ]:              0 :             Assert(IS_DUMMY_REL(child_rel1));
 2731 tgl@sss.pgh.pa.us        1779                 :              0 :             joinrel->nparts = 0;
                               1780                 :              0 :             return;
                               1781                 :                :         }
 2799 efujita@postgresql.o     1782   [ +  +  -  + ]:CBC       16449 :         if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
                               1783                 :                :         {
 2799 efujita@postgresql.o     1784         [ #  # ]:UBC           0 :             Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
                               1785         [ #  # ]:              0 :             Assert(IS_DUMMY_REL(child_rel2));
 2731 tgl@sss.pgh.pa.us        1786                 :              0 :             joinrel->nparts = 0;
                               1787                 :              0 :             return;
                               1788                 :                :         }
                               1789                 :                : 
                               1790                 :                :         /* We should never try to join two overlapping sets of rels. */
 3271 rhaas@postgresql.org     1791         [ -  + ]:CBC       16449 :         Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
                               1792                 :                : 
                               1793                 :                :         /*
                               1794                 :                :          * Construct SpecialJoinInfo from parent join relations's
                               1795                 :                :          * SpecialJoinInfo.
                               1796                 :                :          */
                               1797                 :          16449 :         child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
                               1798                 :                :                                                child_rel1->relids,
                               1799                 :                :                                                child_rel2->relids);
                               1800                 :                : 
                               1801                 :                :         /* Find the AppendRelInfo structures */
  783 rguo@postgresql.org      1802                 :          16449 :         child_relids = bms_union(child_rel1->relids, child_rel2->relids);
                               1803                 :          16449 :         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 =
 3271 rhaas@postgresql.org     1811                 :          16449 :             (List *) adjust_appendrel_attrs(root,
                               1812                 :                :                                             (Node *) parent_restrictlist,
                               1813                 :                :                                             nappinfos, appinfos);
                               1814                 :                : 
                               1815                 :                :         /* Find or construct the child join's RelOptInfo */
                               1816                 :          16449 :         child_joinrel = joinrel->part_rels[cnt_parts];
                               1817         [ +  + ]:          16449 :         if (!child_joinrel)
                               1818                 :                :         {
                               1819                 :          15505 :             child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
                               1820                 :                :                                                  joinrel, child_restrictlist,
                               1821                 :                :                                                  child_sjinfo, nappinfos, appinfos);
                               1822                 :          15505 :             joinrel->part_rels[cnt_parts] = child_joinrel;
 1874 drowley@postgresql.o     1823                 :          15505 :             joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts);
 2356 efujita@postgresql.o     1824                 :          15505 :             joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
                               1825                 :          15505 :                                                     child_joinrel->relids);
                               1826                 :                :         }
                               1827                 :                : 
                               1828                 :                :         /* Assert we got the right one */
 1156 tgl@sss.pgh.pa.us        1829         [ -  + ]:          16449 :         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 */
  347 rguo@postgresql.org      1834                 :          16449 :         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 */
 3271 rhaas@postgresql.org     1839                 :          16449 :         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                 :                :          */
 1156 tgl@sss.pgh.pa.us        1849                 :          16449 :         pfree(appinfos);
  783 rguo@postgresql.org      1850                 :          16449 :         bms_free(child_relids);
  578                          1851                 :          16449 :         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 *
 2804 alvherre@alvh.no-ip.     1864                 :          16449 : build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
                               1865                 :                :                         Relids left_relids, Relids right_relids)
                               1866                 :                : {
                               1867                 :          16449 :     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. */
  909 amitlan@postgresql.o     1874         [ +  + ]:          16449 :     if (parent_sjinfo->jointype == JOIN_INNER)
                               1875                 :                :     {
                               1876         [ -  + ]:          13798 :         Assert(parent_sjinfo->ojrelid == 0);
                               1877                 :          13798 :         init_dummy_sjinfo(sjinfo, left_relids, right_relids);
                               1878                 :          13798 :         return sjinfo;
                               1879                 :                :     }
                               1880                 :                : 
 2804 alvherre@alvh.no-ip.     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
  578 rguo@postgresql.org      1918                 :          16449 : 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         [ +  + ]:          16449 :     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         [ -  + ]:           2651 :         Assert(child_sjinfo->commute_above_l == parent_sjinfo->commute_above_l);
                               1940         [ -  + ]:           2651 :         Assert(child_sjinfo->commute_above_r == parent_sjinfo->commute_above_r);
                               1941         [ -  + ]:           2651 :         Assert(child_sjinfo->commute_below_l == parent_sjinfo->commute_below_l);
                               1942         [ -  + ]:           2651 :         Assert(child_sjinfo->commute_below_r == parent_sjinfo->commute_below_r);
                               1943                 :                : 
                               1944         [ -  + ]:           2651 :         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                 :          16449 :     pfree(child_sjinfo);
  909 amitlan@postgresql.o     1953                 :          16449 : }
                               1954                 :                : 
                               1955                 :                : /*
                               1956                 :                :  * compute_partition_bounds
                               1957                 :                :  *      Compute the partition bounds for a join rel from those for inputs
                               1958                 :                :  */
                               1959                 :                : static void
 2356 efujita@postgresql.o     1960                 :           6564 : 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         [ +  + ]:           6564 :     if (joinrel->nparts == -1)
                               1970                 :                :     {
                               1971                 :           6218 :         PartitionScheme part_scheme = joinrel->part_scheme;
                               1972                 :           6218 :         PartitionBoundInfo boundinfo = NULL;
                               1973                 :           6218 :         int         nparts = 0;
                               1974                 :                : 
                               1975         [ -  + ]:           6218 :         Assert(joinrel->boundinfo == NULL);
                               1976         [ -  + ]:           6218 :         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         [ +  + ]:           6218 :         if (!rel1->partbounds_merged &&
                               1989         [ +  - ]:           6168 :             !rel2->partbounds_merged &&
                               1990   [ +  +  +  + ]:          12120 :             rel1->nparts == rel2->nparts &&
                               1991                 :           5952 :             partition_bounds_equal(part_scheme->partnatts,
                               1992                 :                :                                    part_scheme->parttyplen,
                               1993                 :                :                                    part_scheme->parttypbyval,
                               1994                 :                :                                    rel1->boundinfo, rel2->boundinfo))
                               1995                 :                :         {
                               1996                 :           5512 :             boundinfo = rel1->boundinfo;
                               1997                 :           5512 :             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         [ -  + ]:           6122 :         Assert(nparts > 0);
                               2018                 :           6122 :         joinrel->boundinfo = boundinfo;
                               2019                 :           6122 :         joinrel->nparts = nparts;
  284 michael@paquier.xyz      2020                 :           6122 :         joinrel->part_rels = palloc0_array(RelOptInfo *, nparts);
                               2021                 :                :     }
                               2022                 :                :     else
                               2023                 :                :     {
 2356 efujita@postgresql.o     2024         [ -  + ]:            346 :         Assert(joinrel->nparts > 0);
                               2025         [ -  + ]:            346 :         Assert(joinrel->boundinfo);
                               2026         [ -  + ]:            346 :         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         [ +  + ]:            346 :         if (joinrel->partbounds_merged)
                               2036                 :                :         {
                               2037                 :             30 :             get_matching_part_pairs(root, joinrel, rel1, rel2,
                               2038                 :                :                                     parts1, parts2);
                               2039         [ -  + ]:             30 :             Assert(list_length(*parts1) == joinrel->nparts);
                               2040         [ -  + ]:             30 :             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                 :                :         {
 2356 efujita@postgresql.o     2079                 :UBC           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                 :                :          */
 2356 efujita@postgresql.o     2088                 :CBC          80 :         child_relids1 = bms_intersect(child_joinrel->relids,
                               2089                 :             80 :                                       rel1->all_partrels);
                               2090         [ -  + ]:             80 :         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                 :                :         {
 2356 efujita@postgresql.o     2103                 :UBC           0 :             int         varno = bms_singleton_member(child_relids1);
                               2104                 :                : 
                               2105                 :              0 :             child_rel1 = find_base_rel(root, varno);
                               2106                 :                :         }
                               2107                 :                :         else
 2356 efujita@postgresql.o     2108                 :CBC          80 :             child_rel1 = find_join_rel(root, child_relids1);
                               2109         [ -  + ]:             80 :         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         [ -  + ]:             80 :         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
 2356 efujita@postgresql.o     2129                 :UBC           0 :             child_rel2 = find_join_rel(root, child_relids2);
 2356 efujita@postgresql.o     2130         [ -  + ]:CBC          80 :         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