LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/path - equivclass.c (source / functions) Coverage Total Hit LBC UBC GBC CBC
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 92.6 % 1134 1050 1 83 3 1047
Current Date: 2026-03-14 14:10:32 -0400 Functions: 93.8 % 48 45 3 45
Baseline: lcov-20260315-024220-baseline Branches: 78.1 % 1052 822 1 229 3 819
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 76.2 % 181 138 43 138
(360..) days: 95.7 % 953 912 1 40 3 909
Function coverage date bins:
(30,360] days: 78.6 % 14 11 3 11
(360..) days: 100.0 % 34 34 34
Branch coverage date bins:
(30,360] days: 59.1 % 176 104 72 104
(360..) days: 82.0 % 876 718 1 157 3 715

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * equivclass.c
                                  4                 :                :  *    Routines for managing EquivalenceClasses
                                  5                 :                :  *
                                  6                 :                :  * See src/backend/optimizer/README for discussion of EquivalenceClasses.
                                  7                 :                :  *
                                  8                 :                :  *
                                  9                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 10                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 11                 :                :  *
                                 12                 :                :  * IDENTIFICATION
                                 13                 :                :  *    src/backend/optimizer/path/equivclass.c
                                 14                 :                :  *
                                 15                 :                :  *-------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : #include "postgres.h"
                                 18                 :                : 
                                 19                 :                : #include <limits.h>
                                 20                 :                : 
                                 21                 :                : #include "access/stratnum.h"
                                 22                 :                : #include "catalog/pg_type.h"
                                 23                 :                : #include "common/hashfn.h"
                                 24                 :                : #include "nodes/makefuncs.h"
                                 25                 :                : #include "nodes/nodeFuncs.h"
                                 26                 :                : #include "optimizer/appendinfo.h"
                                 27                 :                : #include "optimizer/clauses.h"
                                 28                 :                : #include "optimizer/optimizer.h"
                                 29                 :                : #include "optimizer/pathnode.h"
                                 30                 :                : #include "optimizer/paths.h"
                                 31                 :                : #include "optimizer/planmain.h"
                                 32                 :                : #include "optimizer/restrictinfo.h"
                                 33                 :                : #include "rewrite/rewriteManip.h"
                                 34                 :                : #include "utils/lsyscache.h"
                                 35                 :                : 
                                 36                 :                : 
                                 37                 :                : static EquivalenceMember *make_eq_member(EquivalenceClass *ec,
                                 38                 :                :                                          Expr *expr, Relids relids,
                                 39                 :                :                                          JoinDomain *jdomain,
                                 40                 :                :                                          EquivalenceMember *parent,
                                 41                 :                :                                          Oid datatype);
                                 42                 :                : static EquivalenceMember *add_eq_member(EquivalenceClass *ec,
                                 43                 :                :                                         Expr *expr, Relids relids,
                                 44                 :                :                                         JoinDomain *jdomain,
                                 45                 :                :                                         Oid datatype);
                                 46                 :                : static EquivalenceMember *add_child_eq_member(PlannerInfo *root,
                                 47                 :                :                                               EquivalenceClass *ec,
                                 48                 :                :                                               int ec_index, Expr *expr,
                                 49                 :                :                                               Relids relids,
                                 50                 :                :                                               JoinDomain *jdomain,
                                 51                 :                :                                               EquivalenceMember *parent_em,
                                 52                 :                :                                               Oid datatype,
                                 53                 :                :                                               Index child_relid);
                                 54                 :                : static void generate_base_implied_equalities_const(PlannerInfo *root,
                                 55                 :                :                                                    EquivalenceClass *ec);
                                 56                 :                : static void generate_base_implied_equalities_no_const(PlannerInfo *root,
                                 57                 :                :                                                       EquivalenceClass *ec);
                                 58                 :                : static void generate_base_implied_equalities_broken(PlannerInfo *root,
                                 59                 :                :                                                     EquivalenceClass *ec);
                                 60                 :                : static List *generate_join_implied_equalities_normal(PlannerInfo *root,
                                 61                 :                :                                                      EquivalenceClass *ec,
                                 62                 :                :                                                      Relids join_relids,
                                 63                 :                :                                                      Relids outer_relids,
                                 64                 :                :                                                      Relids inner_relids);
                                 65                 :                : static List *generate_join_implied_equalities_broken(PlannerInfo *root,
                                 66                 :                :                                                      EquivalenceClass *ec,
                                 67                 :                :                                                      Relids nominal_join_relids,
                                 68                 :                :                                                      Relids outer_relids,
                                 69                 :                :                                                      Relids nominal_inner_relids,
                                 70                 :                :                                                      RelOptInfo *inner_rel);
                                 71                 :                : static Oid  select_equality_operator(EquivalenceClass *ec,
                                 72                 :                :                                      Oid lefttype, Oid righttype);
                                 73                 :                : static RestrictInfo *create_join_clause(PlannerInfo *root,
                                 74                 :                :                                         EquivalenceClass *ec, Oid opno,
                                 75                 :                :                                         EquivalenceMember *leftem,
                                 76                 :                :                                         EquivalenceMember *rightem,
                                 77                 :                :                                         EquivalenceClass *parent_ec);
                                 78                 :                : static bool reconsider_outer_join_clause(PlannerInfo *root,
                                 79                 :                :                                          OuterJoinClauseInfo *ojcinfo,
                                 80                 :                :                                          bool outer_on_left);
                                 81                 :                : static bool reconsider_full_join_clause(PlannerInfo *root,
                                 82                 :                :                                         OuterJoinClauseInfo *ojcinfo);
                                 83                 :                : static JoinDomain *find_join_domain(PlannerInfo *root, Relids relids);
                                 84                 :                : static Bitmapset *get_eclass_indexes_for_relids(PlannerInfo *root,
                                 85                 :                :                                                 Relids relids);
                                 86                 :                : static Bitmapset *get_common_eclass_indexes(PlannerInfo *root, Relids relids1,
                                 87                 :                :                                             Relids relids2);
                                 88                 :                : static void ec_build_derives_hash(PlannerInfo *root, EquivalenceClass *ec);
                                 89                 :                : static void ec_add_derived_clauses(EquivalenceClass *ec, List *clauses);
                                 90                 :                : static void ec_add_derived_clause(EquivalenceClass *ec, RestrictInfo *clause);
                                 91                 :                : static void ec_add_clause_to_derives_hash(EquivalenceClass *ec, RestrictInfo *rinfo);
                                 92                 :                : static RestrictInfo *ec_search_clause_for_ems(PlannerInfo *root, EquivalenceClass *ec,
                                 93                 :                :                                               EquivalenceMember *leftem,
                                 94                 :                :                                               EquivalenceMember *rightem,
                                 95                 :                :                                               EquivalenceClass *parent_ec);
                                 96                 :                : static RestrictInfo *ec_search_derived_clause_for_ems(PlannerInfo *root,
                                 97                 :                :                                                       EquivalenceClass *ec,
                                 98                 :                :                                                       EquivalenceMember *leftem,
                                 99                 :                :                                                       EquivalenceMember *rightem,
                                100                 :                :                                                       EquivalenceClass *parent_ec);
                                101                 :                : 
                                102                 :                : /*
                                103                 :                :  * Hash key identifying a derived clause.
                                104                 :                :  *
                                105                 :                :  * This structure should not be filled manually. Use fill_ec_derives_key() to
                                106                 :                :  * set it up in canonical form.
                                107                 :                :  */
                                108                 :                : typedef struct
                                109                 :                : {
                                110                 :                :     EquivalenceMember *em1;
                                111                 :                :     EquivalenceMember *em2;
                                112                 :                :     EquivalenceClass *parent_ec;
                                113                 :                : } ECDerivesKey;
                                114                 :                : 
                                115                 :                : /* Hash table entry in ec_derives_hash. */
                                116                 :                : typedef struct
                                117                 :                : {
                                118                 :                :     uint32      status;
                                119                 :                :     ECDerivesKey key;
                                120                 :                :     RestrictInfo *rinfo;
                                121                 :                : } ECDerivesEntry;
                                122                 :                : 
                                123                 :                : /* Threshold for switching from list to hash table */
                                124                 :                : #define EC_DERIVES_HASH_THRESHOLD 32
                                125                 :                : 
                                126                 :                : #define SH_PREFIX               derives
                                127                 :                : #define SH_ELEMENT_TYPE         ECDerivesEntry
                                128                 :                : #define SH_KEY_TYPE             ECDerivesKey
                                129                 :                : #define SH_KEY                  key
                                130                 :                : #define SH_HASH_KEY(tb, key)    \
                                131                 :                :     hash_bytes((const unsigned char *) &(key), sizeof(ECDerivesKey))
                                132                 :                : #define SH_EQUAL(tb, a, b)      \
                                133                 :                :     ((a).em1 == (b).em1 && (a).em2 == (b).em2 && (a).parent_ec == (b).parent_ec)
                                134                 :                : #define SH_SCOPE                static inline
                                135                 :                : #define SH_DECLARE
                                136                 :                : #define SH_DEFINE
                                137                 :                : #include "lib/simplehash.h"
                                138                 :                : 
                                139                 :                : /*
                                140                 :                :  * process_equivalence
                                141                 :                :  *    The given clause has a mergejoinable operator and is not an outer-join
                                142                 :                :  *    qualification, so its two sides can be considered equal
                                143                 :                :  *    anywhere they are both computable; moreover that equality can be
                                144                 :                :  *    extended transitively.  Record this knowledge in the EquivalenceClass
                                145                 :                :  *    data structure, if applicable.  Returns true if successful, false if not
                                146                 :                :  *    (in which case caller should treat the clause as ordinary, not an
                                147                 :                :  *    equivalence).
                                148                 :                :  *
                                149                 :                :  * In some cases, although we cannot convert a clause into EquivalenceClass
                                150                 :                :  * knowledge, we can still modify it to a more useful form than the original.
                                151                 :                :  * Then, *p_restrictinfo will be replaced by a new RestrictInfo, which is what
                                152                 :                :  * the caller should use for further processing.
                                153                 :                :  *
                                154                 :                :  * jdomain is the join domain within which the given clause was found.
                                155                 :                :  * This limits the applicability of deductions from the EquivalenceClass,
                                156                 :                :  * as described in optimizer/README.
                                157                 :                :  *
                                158                 :                :  * We reject proposed equivalence clauses if they contain leaky functions
                                159                 :                :  * and have security_level above zero.  The EC evaluation rules require us to
                                160                 :                :  * apply certain tests at certain joining levels, and we can't tolerate
                                161                 :                :  * delaying any test on security_level grounds.  By rejecting candidate clauses
                                162                 :                :  * that might require security delays, we ensure it's safe to apply an EC
                                163                 :                :  * clause as soon as it's supposed to be applied.
                                164                 :                :  *
                                165                 :                :  * On success return, we have also initialized the clause's left_ec/right_ec
                                166                 :                :  * fields to point to the EquivalenceClass representing it.  This saves lookup
                                167                 :                :  * effort later.
                                168                 :                :  *
                                169                 :                :  * Note: constructing merged EquivalenceClasses is a standard UNION-FIND
                                170                 :                :  * problem, for which there exist better data structures than simple lists.
                                171                 :                :  * If this code ever proves to be a bottleneck then it could be sped up ---
                                172                 :                :  * but for now, simple is beautiful.
                                173                 :                :  *
                                174                 :                :  * Note: this is only called during planner startup, not during GEQO
                                175                 :                :  * exploration, so we need not worry about whether we're in the right
                                176                 :                :  * memory context.
                                177                 :                :  */
                                178                 :                : bool
 3080 tgl@sss.pgh.pa.us         179                 :CBC      182081 : process_equivalence(PlannerInfo *root,
                                180                 :                :                     RestrictInfo **p_restrictinfo,
                                181                 :                :                     JoinDomain *jdomain)
                                182                 :                : {
                                183                 :         182081 :     RestrictInfo *restrictinfo = *p_restrictinfo;
 6994                           184                 :         182081 :     Expr       *clause = restrictinfo->clause;
                                185                 :                :     Oid         opno,
                                186                 :                :                 collation,
                                187                 :                :                 item1_type,
                                188                 :                :                 item2_type;
                                189                 :                :     Expr       *item1;
                                190                 :                :     Expr       *item2;
                                191                 :                :     Relids      item1_relids,
                                192                 :                :                 item2_relids;
                                193                 :                :     List       *opfamilies;
                                194                 :                :     EquivalenceClass *ec1,
                                195                 :                :                *ec2;
                                196                 :                :     EquivalenceMember *em1,
                                197                 :                :                *em2;
                                198                 :                :     ListCell   *lc1;
                                199                 :                :     int         ec2_idx;
                                200                 :                : 
                                201                 :                :     /* Should not already be marked as having generated an eclass */
 5616                           202         [ -  + ]:         182081 :     Assert(restrictinfo->left_ec == NULL);
                                203         [ -  + ]:         182081 :     Assert(restrictinfo->right_ec == NULL);
                                204                 :                : 
                                205                 :                :     /* Reject if it is potentially postponable by security considerations */
 3343                           206   [ +  +  +  + ]:         182081 :     if (restrictinfo->security_level > 0 && !restrictinfo->leakproof)
                                207                 :            104 :         return false;
                                208                 :                : 
                                209                 :                :     /* Extract info from given clause */
 6994                           210         [ -  + ]:         181977 :     Assert(is_opclause(clause));
                                211                 :         181977 :     opno = ((OpExpr *) clause)->opno;
 5475                           212                 :         181977 :     collation = ((OpExpr *) clause)->inputcollid;
 6994                           213                 :         181977 :     item1 = (Expr *) get_leftop(clause);
                                214                 :         181977 :     item2 = (Expr *) get_rightop(clause);
                                215                 :         181977 :     item1_relids = restrictinfo->left_relids;
                                216                 :         181977 :     item2_relids = restrictinfo->right_relids;
                                217                 :                : 
                                218                 :                :     /*
                                219                 :                :      * Ensure both input expressions expose the desired collation (their types
                                220                 :                :      * should be OK already); see comments for canonicalize_ec_expression.
                                221                 :                :      */
 5475                           222                 :         181977 :     item1 = canonicalize_ec_expression(item1,
                                223                 :                :                                        exprType((Node *) item1),
                                224                 :                :                                        collation);
                                225                 :         181977 :     item2 = canonicalize_ec_expression(item2,
                                226                 :                :                                        exprType((Node *) item2),
                                227                 :                :                                        collation);
                                228                 :                : 
                                229                 :                :     /*
                                230                 :                :      * Clauses of the form X=X cannot be translated into EquivalenceClasses.
                                231                 :                :      * We'd either end up with a single-entry EC, losing the knowledge that
                                232                 :                :      * the clause was present at all, or else make an EC with duplicate
                                233                 :                :      * entries, causing other issues.
                                234                 :                :      */
 6011                           235         [ +  + ]:         181977 :     if (equal(item1, item2))
                                236                 :                :     {
                                237                 :                :         /*
                                238                 :                :          * If the operator is strict, then the clause can be treated as just
                                239                 :                :          * "X IS NOT NULL".  (Since we know we are considering a top-level
                                240                 :                :          * qual, we can ignore the difference between FALSE and NULL results.)
                                241                 :                :          * It's worth making the conversion because we'll typically get a much
                                242                 :                :          * better selectivity estimate than we would for X=X.
                                243                 :                :          *
                                244                 :                :          * If the operator is not strict, we can't be sure what it will do
                                245                 :                :          * with NULLs, so don't attempt to optimize it.
                                246                 :                :          */
 3080                           247                 :             27 :         set_opfuncid((OpExpr *) clause);
                                248         [ +  - ]:             27 :         if (func_strict(((OpExpr *) clause)->opfuncid))
                                249                 :                :         {
                                250                 :             27 :             NullTest   *ntest = makeNode(NullTest);
                                251                 :                : 
                                252                 :             27 :             ntest->arg = item1;
                                253                 :             27 :             ntest->nulltesttype = IS_NOT_NULL;
                                254                 :             27 :             ntest->argisrow = false; /* correct even if composite arg */
                                255                 :             27 :             ntest->location = -1;
                                256                 :                : 
                                257                 :             27 :             *p_restrictinfo =
 1879                           258                 :             27 :                 make_restrictinfo(root,
                                259                 :                :                                   (Expr *) ntest,
 3080                           260                 :             27 :                                   restrictinfo->is_pushed_down,
 1025                           261                 :             27 :                                   restrictinfo->has_clone,
                                262                 :             27 :                                   restrictinfo->is_clone,
 3080                           263                 :             27 :                                   restrictinfo->pseudoconstant,
                                264                 :                :                                   restrictinfo->security_level,
                                265                 :                :                                   NULL,
                                266                 :                :                                   restrictinfo->incompatible_relids,
                                267                 :                :                                   restrictinfo->outer_relids);
                                268                 :                :         }
                                269                 :             27 :         return false;
                                270                 :                :     }
                                271                 :                : 
                                272                 :                :     /*
                                273                 :                :      * We use the declared input types of the operator, not exprType() of the
                                274                 :                :      * inputs, as the nominal datatypes for opfamily lookup.  This presumes
                                275                 :                :      * that btree operators are always registered with amoplefttype and
                                276                 :                :      * amoprighttype equal to their declared input types.  We will need this
                                277                 :                :      * info anyway to build EquivalenceMember nodes, and by extracting it now
                                278                 :                :      * we can use type comparisons to short-circuit some equal() tests.
                                279                 :                :      */
 6994                           280                 :         181950 :     op_input_types(opno, &item1_type, &item2_type);
                                281                 :                : 
                                282                 :         181950 :     opfamilies = restrictinfo->mergeopfamilies;
                                283                 :                : 
                                284                 :                :     /*
                                285                 :                :      * Sweep through the existing EquivalenceClasses looking for matches to
                                286                 :                :      * item1 and item2.  These are the possible outcomes:
                                287                 :                :      *
                                288                 :                :      * 1. We find both in the same EC.  The equivalence is already known, so
                                289                 :                :      * there's nothing to do.
                                290                 :                :      *
                                291                 :                :      * 2. We find both in different ECs.  Merge the two ECs together.
                                292                 :                :      *
                                293                 :                :      * 3. We find just one.  Add the other to its EC.
                                294                 :                :      *
                                295                 :                :      * 4. We find neither.  Make a new, two-entry EC.
                                296                 :                :      *
                                297                 :                :      * Note: since all ECs are built through this process or the similar
                                298                 :                :      * search in get_eclass_for_sort_expr(), it's impossible that we'd match
                                299                 :                :      * an item in more than one existing nonvolatile EC.  So it's okay to stop
                                300                 :                :      * at the first match.
                                301                 :                :      */
                                302                 :         181950 :     ec1 = ec2 = NULL;
 6992                           303                 :         181950 :     em1 = em2 = NULL;
 1970 drowley@postgresql.o      304                 :         181950 :     ec2_idx = -1;
 6994 tgl@sss.pgh.pa.us         305   [ +  +  +  +  :         310190 :     foreach(lc1, root->eq_classes)
                                              +  + ]
                                306                 :                :     {
                                307                 :         128267 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
                                308                 :                :         ListCell   *lc2;
                                309                 :                : 
                                310                 :                :         /* Never match to a volatile EC */
                                311         [ -  + ]:         128267 :         if (cur_ec->ec_has_volatile)
 6994 tgl@sss.pgh.pa.us         312                 :UBC           0 :             continue;
                                313                 :                : 
                                314                 :                :         /*
                                315                 :                :          * The collation has to match; check this first since it's cheaper
                                316                 :                :          * than the opfamily comparison.
                                317                 :                :          */
 5475 tgl@sss.pgh.pa.us         318         [ +  + ]:CBC      128267 :         if (collation != cur_ec->ec_collation)
                                319                 :          10354 :             continue;
                                320                 :                : 
                                321                 :                :         /*
                                322                 :                :          * A "match" requires matching sets of btree opfamilies.  Use of
                                323                 :                :          * equal() for this test has implications discussed in the comments
                                324                 :                :          * for get_mergejoin_opfamilies().
                                325                 :                :          */
 6994                           326         [ +  + ]:         117913 :         if (!equal(opfamilies, cur_ec->ec_opfamilies))
                                327                 :          33117 :             continue;
                                328                 :                : 
                                329                 :                :         /* We don't expect any children yet */
  341 drowley@postgresql.o      330         [ -  + ]:          84796 :         Assert(cur_ec->ec_childmembers == NULL);
                                331                 :                : 
 6994 tgl@sss.pgh.pa.us         332   [ +  -  +  +  :         253420 :         foreach(lc2, cur_ec->ec_members)
                                              +  + ]
                                333                 :                :         {
                                334                 :         168651 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
                                335                 :                : 
                                336                 :                :             /* Child members should not exist in ec_members */
  341 drowley@postgresql.o      337         [ -  + ]:         168651 :             Assert(!cur_em->em_is_child);
                                338                 :                : 
                                339                 :                :             /*
                                340                 :                :              * Match constants only within the same JoinDomain (see
                                341                 :                :              * optimizer/README).
                                342                 :                :              */
 1140 tgl@sss.pgh.pa.us         343   [ +  +  +  + ]:         168651 :             if (cur_em->em_is_const && cur_em->em_jdomain != jdomain)
 6994                           344                 :           2474 :                 continue;
                                345                 :                : 
                                346         [ +  + ]:         166177 :             if (!ec1 &&
                                347   [ +  +  +  + ]:         319806 :                 item1_type == cur_em->em_datatype &&
                                348                 :         159821 :                 equal(item1, cur_em->em_expr))
                                349                 :                :             {
                                350                 :           9550 :                 ec1 = cur_ec;
 6992                           351                 :           9550 :                 em1 = cur_em;
 6994                           352         [ +  + ]:           9550 :                 if (ec2)
                                353                 :             15 :                     break;
                                354                 :                :             }
                                355                 :                : 
                                356         [ +  + ]:         166162 :             if (!ec2 &&
                                357   [ +  +  +  + ]:         328933 :                 item2_type == cur_em->em_datatype &&
                                358                 :         164317 :                 equal(item2, cur_em->em_expr))
                                359                 :                :             {
                                360                 :           2507 :                 ec2 = cur_ec;
 1970 drowley@postgresql.o      361                 :           2507 :                 ec2_idx = foreach_current_index(lc1);
 6992 tgl@sss.pgh.pa.us         362                 :           2507 :                 em2 = cur_em;
 6994                           363         [ +  + ]:           2507 :                 if (ec1)
                                364                 :             12 :                     break;
                                365                 :                :             }
                                366                 :                :         }
                                367                 :                : 
                                368   [ +  +  +  + ]:          84796 :         if (ec1 && ec2)
                                369                 :             27 :             break;
                                370                 :                :     }
                                371                 :                : 
                                372                 :                :     /* Sweep finished, what did we find? */
                                373                 :                : 
                                374   [ +  +  +  + ]:         181950 :     if (ec1 && ec2)
                                375                 :                :     {
                                376                 :                :         /* If case 1, nothing to do, except add to sources */
                                377         [ +  + ]:             27 :         if (ec1 == ec2)
                                378                 :                :         {
                                379                 :              6 :             ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
 3343                           380                 :              6 :             ec1->ec_min_security = Min(ec1->ec_min_security,
                                381                 :                :                                        restrictinfo->security_level);
                                382                 :              6 :             ec1->ec_max_security = Max(ec1->ec_max_security,
                                383                 :                :                                        restrictinfo->security_level);
                                384                 :                :             /* mark the RI as associated with this eclass */
 5616                           385                 :              6 :             restrictinfo->left_ec = ec1;
                                386                 :              6 :             restrictinfo->right_ec = ec1;
                                387                 :                :             /* mark the RI as usable with this pair of EMs */
 6992                           388                 :              6 :             restrictinfo->left_em = em1;
                                389                 :              6 :             restrictinfo->right_em = em2;
 6994                           390                 :              6 :             return true;
                                391                 :                :         }
                                392                 :                : 
                                393                 :                :         /*
                                394                 :                :          * Case 2: need to merge ec1 and ec2.  This should never happen after
                                395                 :                :          * the ECs have reached canonical state; otherwise, pathkeys could be
                                396                 :                :          * rendered non-canonical by the merge, and relation eclass indexes
                                397                 :                :          * would get broken by removal of an eq_classes list entry.
                                398                 :                :          */
 2429 drowley@postgresql.o      399         [ -  + ]:             21 :         if (root->ec_merging_done)
 4703 tgl@sss.pgh.pa.us         400         [ #  # ]:UBC           0 :             elog(ERROR, "too late to merge equivalence classes");
                                401                 :                : 
                                402                 :                :         /*
                                403                 :                :          * We add ec2's items to ec1, then set ec2's ec_merged link to point
                                404                 :                :          * to ec1 and remove ec2 from the eq_classes list.  We cannot simply
                                405                 :                :          * delete ec2 because that could leave dangling pointers in existing
                                406                 :                :          * PathKeys.  We leave it behind with a link so that the merged EC can
                                407                 :                :          * be found.
                                408                 :                :          */
 6994 tgl@sss.pgh.pa.us         409                 :CBC          21 :         ec1->ec_members = list_concat(ec1->ec_members, ec2->ec_members);
                                410                 :             21 :         ec1->ec_sources = list_concat(ec1->ec_sources, ec2->ec_sources);
                                411                 :                : 
                                412                 :                :         /*
                                413                 :                :          * Appends ec2's derived clauses to ec1->ec_derives_list and adds them
                                414                 :                :          * to ec1->ec_derives_hash if present.
                                415                 :                :          */
  345 amitlan@postgresql.o      416                 :             21 :         ec_add_derived_clauses(ec1, ec2->ec_derives_list);
 6994 tgl@sss.pgh.pa.us         417                 :             21 :         ec1->ec_relids = bms_join(ec1->ec_relids, ec2->ec_relids);
                                418                 :             21 :         ec1->ec_has_const |= ec2->ec_has_const;
                                419                 :                :         /* can't need to set has_volatile */
 3343                           420                 :             21 :         ec1->ec_min_security = Min(ec1->ec_min_security,
                                421                 :                :                                    ec2->ec_min_security);
                                422                 :             21 :         ec1->ec_max_security = Max(ec1->ec_max_security,
                                423                 :                :                                    ec2->ec_max_security);
 6994                           424                 :             21 :         ec2->ec_merged = ec1;
 1970 drowley@postgresql.o      425                 :             21 :         root->eq_classes = list_delete_nth_cell(root->eq_classes, ec2_idx);
                                426                 :                :         /* just to avoid debugging confusion w/ dangling pointers: */
 6994 tgl@sss.pgh.pa.us         427                 :             21 :         ec2->ec_members = NIL;
                                428                 :             21 :         ec2->ec_sources = NIL;
  345 amitlan@postgresql.o      429                 :             21 :         ec_clear_derived_clauses(ec2);
 6994 tgl@sss.pgh.pa.us         430                 :             21 :         ec2->ec_relids = NULL;
                                431                 :             21 :         ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
 3343                           432                 :             21 :         ec1->ec_min_security = Min(ec1->ec_min_security,
                                433                 :                :                                    restrictinfo->security_level);
                                434                 :             21 :         ec1->ec_max_security = Max(ec1->ec_max_security,
                                435                 :                :                                    restrictinfo->security_level);
                                436                 :                :         /* mark the RI as associated with this eclass */
 5616                           437                 :             21 :         restrictinfo->left_ec = ec1;
                                438                 :             21 :         restrictinfo->right_ec = ec1;
                                439                 :                :         /* mark the RI as usable with this pair of EMs */
 6992                           440                 :             21 :         restrictinfo->left_em = em1;
                                441                 :             21 :         restrictinfo->right_em = em2;
                                442                 :                :     }
 6994                           443         [ +  + ]:         181923 :     else if (ec1)
                                444                 :                :     {
                                445                 :                :         /* Case 3: add item2 to ec1 */
 1140                           446                 :           9523 :         em2 = add_eq_member(ec1, item2, item2_relids,
                                447                 :                :                             jdomain, item2_type);
 6994                           448                 :           9523 :         ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
 3343                           449                 :           9523 :         ec1->ec_min_security = Min(ec1->ec_min_security,
                                450                 :                :                                    restrictinfo->security_level);
                                451                 :           9523 :         ec1->ec_max_security = Max(ec1->ec_max_security,
                                452                 :                :                                    restrictinfo->security_level);
                                453                 :                :         /* mark the RI as associated with this eclass */
 5616                           454                 :           9523 :         restrictinfo->left_ec = ec1;
                                455                 :           9523 :         restrictinfo->right_ec = ec1;
                                456                 :                :         /* mark the RI as usable with this pair of EMs */
 6992                           457                 :           9523 :         restrictinfo->left_em = em1;
                                458                 :           9523 :         restrictinfo->right_em = em2;
                                459                 :                :     }
 6994                           460         [ +  + ]:         172400 :     else if (ec2)
                                461                 :                :     {
                                462                 :                :         /* Case 3: add item1 to ec2 */
 1140                           463                 :           2480 :         em1 = add_eq_member(ec2, item1, item1_relids,
                                464                 :                :                             jdomain, item1_type);
 6994                           465                 :           2480 :         ec2->ec_sources = lappend(ec2->ec_sources, restrictinfo);
 3343                           466                 :           2480 :         ec2->ec_min_security = Min(ec2->ec_min_security,
                                467                 :                :                                    restrictinfo->security_level);
                                468                 :           2480 :         ec2->ec_max_security = Max(ec2->ec_max_security,
                                469                 :                :                                    restrictinfo->security_level);
                                470                 :                :         /* mark the RI as associated with this eclass */
 5616                           471                 :           2480 :         restrictinfo->left_ec = ec2;
                                472                 :           2480 :         restrictinfo->right_ec = ec2;
                                473                 :                :         /* mark the RI as usable with this pair of EMs */
 6992                           474                 :           2480 :         restrictinfo->left_em = em1;
                                475                 :           2480 :         restrictinfo->right_em = em2;
                                476                 :                :     }
                                477                 :                :     else
                                478                 :                :     {
                                479                 :                :         /* Case 4: make a new, two-entry EC */
 6994                           480                 :         169920 :         EquivalenceClass *ec = makeNode(EquivalenceClass);
                                481                 :                : 
                                482                 :         169920 :         ec->ec_opfamilies = opfamilies;
 5475                           483                 :         169920 :         ec->ec_collation = collation;
  341 drowley@postgresql.o      484                 :         169920 :         ec->ec_childmembers_size = 0;
 6994 tgl@sss.pgh.pa.us         485                 :         169920 :         ec->ec_members = NIL;
  341 drowley@postgresql.o      486                 :         169920 :         ec->ec_childmembers = NULL;
 6994 tgl@sss.pgh.pa.us         487                 :         169920 :         ec->ec_sources = list_make1(restrictinfo);
  345 amitlan@postgresql.o      488                 :         169920 :         ec->ec_derives_list = NIL;
                                489                 :         169920 :         ec->ec_derives_hash = NULL;
 6994 tgl@sss.pgh.pa.us         490                 :         169920 :         ec->ec_relids = NULL;
                                491                 :         169920 :         ec->ec_has_const = false;
                                492                 :         169920 :         ec->ec_has_volatile = false;
                                493                 :         169920 :         ec->ec_broken = false;
 6702                           494                 :         169920 :         ec->ec_sortref = 0;
 3343                           495                 :         169920 :         ec->ec_min_security = restrictinfo->security_level;
                                496                 :         169920 :         ec->ec_max_security = restrictinfo->security_level;
 6994                           497                 :         169920 :         ec->ec_merged = NULL;
 1140                           498                 :         169920 :         em1 = add_eq_member(ec, item1, item1_relids,
                                499                 :                :                             jdomain, item1_type);
                                500                 :         169920 :         em2 = add_eq_member(ec, item2, item2_relids,
                                501                 :                :                             jdomain, item2_type);
                                502                 :                : 
 6994                           503                 :         169920 :         root->eq_classes = lappend(root->eq_classes, ec);
                                504                 :                : 
                                505                 :                :         /* mark the RI as associated with this eclass */
 5616                           506                 :         169920 :         restrictinfo->left_ec = ec;
                                507                 :         169920 :         restrictinfo->right_ec = ec;
                                508                 :                :         /* mark the RI as usable with this pair of EMs */
 6992                           509                 :         169920 :         restrictinfo->left_em = em1;
                                510                 :         169920 :         restrictinfo->right_em = em2;
                                511                 :                :     }
                                512                 :                : 
 6994                           513                 :         181944 :     return true;
                                514                 :                : }
                                515                 :                : 
                                516                 :                : /*
                                517                 :                :  * canonicalize_ec_expression
                                518                 :                :  *
                                519                 :                :  * This function ensures that the expression exposes the expected type and
                                520                 :                :  * collation, so that it will be equal() to other equivalence-class expressions
                                521                 :                :  * that it ought to be equal() to.
                                522                 :                :  *
                                523                 :                :  * The rule for datatypes is that the exposed type should match what it would
                                524                 :                :  * be for an input to an operator of the EC's opfamilies; which is usually
                                525                 :                :  * the declared input type of the operator, but in the case of polymorphic
                                526                 :                :  * operators no relabeling is wanted (compare the behavior of parse_coerce.c).
                                527                 :                :  * Expressions coming in from quals will generally have the right type
                                528                 :                :  * already, but expressions coming from indexkeys may not (because they are
                                529                 :                :  * represented without any explicit relabel in pg_index), and the same problem
                                530                 :                :  * occurs for sort expressions (because the parser is likewise cavalier about
                                531                 :                :  * putting relabels on them).  Such cases will be binary-compatible with the
                                532                 :                :  * real operators, so adding a RelabelType is sufficient.
                                533                 :                :  *
                                534                 :                :  * Also, the expression's exposed collation must match the EC's collation.
                                535                 :                :  * This is important because in comparisons like "foo < bar COLLATE baz",
                                536                 :                :  * only one of the expressions has the correct exposed collation as we receive
                                537                 :                :  * it from the parser.  Forcing both of them to have it ensures that all
                                538                 :                :  * variant spellings of such a construct behave the same.  Again, we can
                                539                 :                :  * stick on a RelabelType to force the right exposed collation.  (It might
                                540                 :                :  * work to not label the collation at all in EC members, but this is risky
                                541                 :                :  * since some parts of the system expect exprCollation() to deliver the
                                542                 :                :  * right answer for a sort key.)
                                543                 :                :  */
                                544                 :                : Expr *
 5475                           545                 :        1688932 : canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
                                546                 :                : {
                                547                 :        1688932 :     Oid         expr_type = exprType((Node *) expr);
                                548                 :                : 
                                549                 :                :     /*
                                550                 :                :      * For a polymorphic-input-type opclass, just keep the same exposed type.
                                551                 :                :      * RECORD opclasses work like polymorphic-type ones for this purpose.
                                552                 :                :      */
 2860                           553   [ +  -  +  +  :        1688932 :     if (IsPolymorphicType(req_type) || req_type == RECORDOID)
                                     +  -  +  +  +  
                                     +  +  +  +  -  
                                     +  -  +  -  +  
                                        -  +  -  +  
                                                 + ]
 5475                           554                 :           4898 :         req_type = expr_type;
                                555                 :                : 
                                556                 :                :     /*
                                557                 :                :      * No work if the expression exposes the right type/collation already.
                                558                 :                :      */
                                559   [ +  +  +  + ]:        3339549 :     if (expr_type != req_type ||
                                560                 :        1650617 :         exprCollation((Node *) expr) != req_collation)
                                561                 :                :     {
                                562                 :                :         /*
                                563                 :                :          * If we have to change the type of the expression, set typmod to -1,
                                564                 :                :          * since the new type may not have the same typmod interpretation.
                                565                 :                :          * When we only have to change collation, preserve the exposed typmod.
                                566                 :                :          */
                                567                 :                :         int32       req_typmod;
                                568                 :                : 
 2034                           569         [ +  + ]:          39321 :         if (expr_type != req_type)
                                570                 :          38315 :             req_typmod = -1;
                                571                 :                :         else
                                572                 :           1006 :             req_typmod = exprTypmod((Node *) expr);
                                573                 :                : 
                                574                 :                :         /*
                                575                 :                :          * Use applyRelabelType so that we preserve const-flatness.  This is
                                576                 :                :          * important since eval_const_expressions has already been applied.
                                577                 :                :          */
                                578                 :          39321 :         expr = (Expr *) applyRelabelType((Node *) expr,
                                579                 :                :                                          req_type, req_typmod, req_collation,
                                580                 :                :                                          COERCE_IMPLICIT_CAST, -1, false);
                                581                 :                :     }
                                582                 :                : 
 5475                           583                 :        1688932 :     return expr;
                                584                 :                : }
                                585                 :                : 
                                586                 :                : /*
                                587                 :                :  * make_eq_member
                                588                 :                :  *      Build a new EquivalenceMember without adding it to an EC.  If 'parent'
                                589                 :                :  *      is NULL, the result will be a parent member, otherwise a child member.
                                590                 :                :  */
                                591                 :                : static EquivalenceMember *
  341 drowley@postgresql.o      592                 :         551823 : make_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids,
                                593                 :                :                JoinDomain *jdomain, EquivalenceMember *parent, Oid datatype)
                                594                 :                : {
 6994 tgl@sss.pgh.pa.us         595                 :         551823 :     EquivalenceMember *em = makeNode(EquivalenceMember);
                                596                 :                : 
                                597                 :         551823 :     em->em_expr = expr;
                                598                 :         551823 :     em->em_relids = relids;
                                599                 :         551823 :     em->em_is_const = false;
 1140                           600                 :         551823 :     em->em_is_child = (parent != NULL);
 6994                           601                 :         551823 :     em->em_datatype = datatype;
 1140                           602                 :         551823 :     em->em_jdomain = jdomain;
                                603                 :         551823 :     em->em_parent = parent;
                                604                 :                : 
 6994                           605         [ +  + ]:         551823 :     if (bms_is_empty(relids))
                                606                 :                :     {
                                607                 :                :         /*
                                608                 :                :          * No Vars, assume it's a pseudoconstant.  This is correct for entries
                                609                 :                :          * generated from process_equivalence(), because a WHERE clause can't
                                610                 :                :          * contain aggregates or SRFs, and non-volatility was checked before
                                611                 :                :          * process_equivalence() ever got called.  But
                                612                 :                :          * get_eclass_for_sort_expr() has to work harder.  We put the tests
                                613                 :                :          * there not here to save cycles in the equivalence case.
                                614                 :                :          */
 1140                           615         [ -  + ]:         130655 :         Assert(!parent);
 6994                           616                 :         130655 :         em->em_is_const = true;
                                617                 :         130655 :         ec->ec_has_const = true;
                                618                 :                :         /* it can't affect ec_relids */
                                619                 :                :     }
                                620                 :                : 
  341 drowley@postgresql.o      621                 :         551823 :     return em;
                                622                 :                : }
                                623                 :                : 
                                624                 :                : /*
                                625                 :                :  * add_eq_member - build a new non-child EquivalenceMember and add it to 'ec'.
                                626                 :                :  */
                                627                 :                : static EquivalenceMember *
                                628                 :         495167 : add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids,
                                629                 :                :               JoinDomain *jdomain, Oid datatype)
                                630                 :                : {
                                631                 :         495167 :     EquivalenceMember *em = make_eq_member(ec, expr, relids, jdomain,
                                632                 :                :                                            NULL, datatype);
                                633                 :                : 
                                634                 :                :     /* add to the members list */
                                635                 :         495167 :     ec->ec_members = lappend(ec->ec_members, em);
                                636                 :                : 
                                637                 :                :     /* record the relids for parent members */
                                638                 :         495167 :     ec->ec_relids = bms_add_members(ec->ec_relids, relids);
                                639                 :                : 
                                640                 :         495167 :     return em;
                                641                 :                : }
                                642                 :                : 
                                643                 :                : /*
                                644                 :                :  * add_child_eq_member
                                645                 :                :  *      Create an em_is_child=true EquivalenceMember and add it to 'ec'.
                                646                 :                :  *
                                647                 :                :  * 'root' is the PlannerInfo that 'ec' belongs to.
                                648                 :                :  * 'ec' is the EquivalenceClass to add the child member to.
                                649                 :                :  * 'ec_index' the index of 'ec' within root->eq_classes, or -1 if maintaining
                                650                 :                :  * the RelOptInfo.eclass_indexes isn't needed.
                                651                 :                :  * 'expr' is the em_expr for the new member.
                                652                 :                :  * 'relids' is the 'em_relids' for the new member.
                                653                 :                :  * 'jdomain' is the 'em_jdomain' for the new member.
                                654                 :                :  * 'parent_em' is the parent member of the child to create.
                                655                 :                :  * 'datatype' is the em_datatype of the new member.
                                656                 :                :  * 'child_relid' defines which element of ec_childmembers to add this member
                                657                 :                :  * to.  This is generally a RELOPT_OTHER_MEMBER_REL, but for set operations
                                658                 :                :  * can be a RELOPT_BASEREL representing the set-op children.
                                659                 :                :  */
                                660                 :                : static EquivalenceMember *
                                661                 :          56656 : add_child_eq_member(PlannerInfo *root, EquivalenceClass *ec, int ec_index,
                                662                 :                :                     Expr *expr, Relids relids, JoinDomain *jdomain,
                                663                 :                :                     EquivalenceMember *parent_em, Oid datatype,
                                664                 :                :                     Index child_relid)
                                665                 :                : {
                                666                 :                :     EquivalenceMember *em;
                                667                 :                : 
                                668         [ -  + ]:          56656 :     Assert(parent_em != NULL);
                                669                 :                : 
                                670                 :                :     /*
                                671                 :                :      * Allocate the array to store child members; an array of Lists indexed by
                                672                 :                :      * relid, or expand the existing one, if necessary.
                                673                 :                :      */
                                674         [ +  + ]:          56656 :     if (unlikely(ec->ec_childmembers_size < root->simple_rel_array_size))
                                675                 :                :     {
                                676         [ +  - ]:          14794 :         if (ec->ec_childmembers == NULL)
                                677                 :          14794 :             ec->ec_childmembers = palloc0_array(List *, root->simple_rel_array_size);
                                678                 :                :         else
  341 drowley@postgresql.o      679                 :UBC           0 :             ec->ec_childmembers = repalloc0_array(ec->ec_childmembers, List *,
                                680                 :                :                                                   ec->ec_childmembers_size,
                                681                 :                :                                                   root->simple_rel_array_size);
                                682                 :                : 
  341 drowley@postgresql.o      683                 :CBC       14794 :         ec->ec_childmembers_size = root->simple_rel_array_size;
                                684                 :                :     }
                                685                 :                : 
                                686                 :          56656 :     em = make_eq_member(ec, expr, relids, jdomain, parent_em, datatype);
                                687                 :                : 
                                688                 :                :     /* add member to the ec_childmembers List for the given child_relid */
                                689                 :          56656 :     ec->ec_childmembers[child_relid] = lappend(ec->ec_childmembers[child_relid], em);
                                690                 :                : 
                                691                 :                :     /* Record this EC index for the child rel */
                                692         [ +  + ]:          56656 :     if (ec_index >= 0)
                                693                 :                :     {
                                694                 :          36228 :         RelOptInfo *child_rel = root->simple_rel_array[child_relid];
                                695                 :                : 
                                696                 :          36228 :         child_rel->eclass_indexes =
                                697                 :          36228 :             bms_add_member(child_rel->eclass_indexes, ec_index);
                                698                 :                :     }
                                699                 :                : 
 6992 tgl@sss.pgh.pa.us         700                 :          56656 :     return em;
                                701                 :                : }
                                702                 :                : 
                                703                 :                : 
                                704                 :                : /*
                                705                 :                :  * get_eclass_for_sort_expr
                                706                 :                :  *    Given an expression and opfamily/collation info, find an existing
                                707                 :                :  *    equivalence class it is a member of; if none, optionally build a new
                                708                 :                :  *    single-member EquivalenceClass for it.
                                709                 :                :  *
                                710                 :                :  * sortref is the SortGroupRef of the originating SortGroupClause, if any,
                                711                 :                :  * or zero if not.  (It should never be zero if the expression is volatile!)
                                712                 :                :  *
                                713                 :                :  * If rel is not NULL, it identifies a specific relation we're considering
                                714                 :                :  * a path for, and indicates that child EC members for that relation can be
                                715                 :                :  * considered.  Otherwise child members are ignored.  (Note: since child EC
                                716                 :                :  * members aren't guaranteed unique, a non-NULL value means that there could
                                717                 :                :  * be more than one EC that matches the expression; if so it's order-dependent
                                718                 :                :  * which one you get.  This is annoying but it only happens in corner cases,
                                719                 :                :  * so for now we live with just reporting the first match.  See also
                                720                 :                :  * generate_implied_equalities_for_column and match_pathkeys_to_index.)
                                721                 :                :  *
                                722                 :                :  * If create_it is true, we'll build a new EquivalenceClass when there is no
                                723                 :                :  * match.  If create_it is false, we just return NULL when no match.
                                724                 :                :  *
                                725                 :                :  * This can be used safely both before and after EquivalenceClass merging;
                                726                 :                :  * since it never causes merging it does not invalidate any existing ECs
                                727                 :                :  * or PathKeys.  However, ECs added after path generation has begun are
                                728                 :                :  * of limited usefulness, so usually it's best to create them beforehand.
                                729                 :                :  *
                                730                 :                :  * Note: opfamilies must be chosen consistently with the way
                                731                 :                :  * process_equivalence() would do; that is, generated from a mergejoinable
                                732                 :                :  * equality operator.  Else we might fail to detect valid equivalences,
                                733                 :                :  * generating poor (but not incorrect) plans.
                                734                 :                :  */
                                735                 :                : EquivalenceClass *
 6994                           736                 :        1246530 : get_eclass_for_sort_expr(PlannerInfo *root,
                                737                 :                :                          Expr *expr,
                                738                 :                :                          List *opfamilies,
                                739                 :                :                          Oid opcintype,
                                740                 :                :                          Oid collation,
                                741                 :                :                          Index sortref,
                                742                 :                :                          Relids rel,
                                743                 :                :                          bool create_it)
                                744                 :                : {
                                745                 :                :     JoinDomain *jdomain;
                                746                 :                :     Relids      expr_relids;
                                747                 :                :     EquivalenceClass *newec;
                                748                 :                :     EquivalenceMember *newem;
                                749                 :                :     ListCell   *lc1;
                                750                 :                :     MemoryContext oldcontext;
                                751                 :                : 
                                752                 :                :     /*
                                753                 :                :      * Ensure the expression exposes the correct type and collation.
                                754                 :                :      */
 5475                           755                 :        1246530 :     expr = canonicalize_ec_expression(expr, opcintype, collation);
                                756                 :                : 
                                757                 :                :     /*
                                758                 :                :      * Since SortGroupClause nodes are top-level expressions (GROUP BY, ORDER
                                759                 :                :      * BY, etc), they can be presumed to belong to the top JoinDomain.
                                760                 :                :      */
 1140                           761                 :        1246530 :     jdomain = linitial_node(JoinDomain, root->join_domains);
                                762                 :                : 
                                763                 :                :     /*
                                764                 :                :      * Scan through the existing EquivalenceClasses for a match
                                765                 :                :      */
 6994                           766   [ +  +  +  +  :        4170798 :     foreach(lc1, root->eq_classes)
                                              +  + ]
                                767                 :                :     {
                                768                 :        3647853 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
                                769                 :                :         EquivalenceMemberIterator it;
                                770                 :                :         EquivalenceMember *cur_em;
                                771                 :                : 
                                772                 :                :         /*
                                773                 :                :          * Never match to a volatile EC, except when we are looking at another
                                774                 :                :          * reference to the same volatile SortGroupClause.
                                775                 :                :          */
 6028                           776   [ +  +  +  + ]:        3647853 :         if (cur_ec->ec_has_volatile &&
                                777         [ +  + ]:             18 :             (sortref == 0 || sortref != cur_ec->ec_sortref))
 6702                           778                 :        1590108 :             continue;
                                779                 :                : 
 5475                           780         [ +  + ]:        3647543 :         if (collation != cur_ec->ec_collation)
                                781                 :         983697 :             continue;
 6994                           782         [ +  + ]:        2663846 :         if (!equal(opfamilies, cur_ec->ec_opfamilies))
                                783                 :         606101 :             continue;
                                784                 :                : 
  341 drowley@postgresql.o      785                 :        2057745 :         setup_eclass_member_iterator(&it, cur_ec, rel);
                                786         [ +  + ]:        4527361 :         while ((cur_em = eclass_member_iterator_next(&it)) != NULL)
                                787                 :                :         {
                                788                 :                :             /*
                                789                 :                :              * Ignore child members unless they match the request.
                                790                 :                :              */
 5112 tgl@sss.pgh.pa.us         791         [ +  + ]:        3193201 :             if (cur_em->em_is_child &&
                                792         [ -  + ]:          51332 :                 !bms_equal(cur_em->em_relids, rel))
 5112 tgl@sss.pgh.pa.us         793                 :UBC           0 :                 continue;
                                794                 :                : 
                                795                 :                :             /*
                                796                 :                :              * Match constants only within the same JoinDomain (see
                                797                 :                :              * optimizer/README).
                                798                 :                :              */
 1140 tgl@sss.pgh.pa.us         799   [ +  +  +  + ]:CBC     3193201 :             if (cur_em->em_is_const && cur_em->em_jdomain != jdomain)
 6994                           800                 :          54563 :                 continue;
                                801                 :                : 
 5475                           802   [ +  +  +  + ]:        6249067 :             if (opcintype == cur_em->em_datatype &&
 6994                           803                 :        3110429 :                 equal(expr, cur_em->em_expr))
  647 akorotkov@postgresql      804                 :         723585 :                 return cur_ec;  /* Match! */
                                805                 :                :         }
                                806                 :                :     }
                                807                 :                : 
                                808                 :                :     /* No match; does caller want a NULL result? */
 5616 tgl@sss.pgh.pa.us         809         [ +  + ]:         522945 :     if (!create_it)
                                810                 :         379621 :         return NULL;
                                811                 :                : 
                                812                 :                :     /*
                                813                 :                :      * OK, build a new single-member EC
                                814                 :                :      *
                                815                 :                :      * Here, we must be sure that we construct the EC in the right context.
                                816                 :                :      */
 6994                           817                 :         143324 :     oldcontext = MemoryContextSwitchTo(root->planner_cxt);
                                818                 :                : 
                                819                 :         143324 :     newec = makeNode(EquivalenceClass);
                                820                 :         143324 :     newec->ec_opfamilies = list_copy(opfamilies);
 5475                           821                 :         143324 :     newec->ec_collation = collation;
  341 drowley@postgresql.o      822                 :         143324 :     newec->ec_childmembers_size = 0;
 6994 tgl@sss.pgh.pa.us         823                 :         143324 :     newec->ec_members = NIL;
  341 drowley@postgresql.o      824                 :         143324 :     newec->ec_childmembers = NULL;
 6994 tgl@sss.pgh.pa.us         825                 :         143324 :     newec->ec_sources = NIL;
  345 amitlan@postgresql.o      826                 :         143324 :     newec->ec_derives_list = NIL;
                                827                 :         143324 :     newec->ec_derives_hash = NULL;
 6994 tgl@sss.pgh.pa.us         828                 :         143324 :     newec->ec_relids = NULL;
                                829                 :         143324 :     newec->ec_has_const = false;
                                830                 :         143324 :     newec->ec_has_volatile = contain_volatile_functions((Node *) expr);
                                831                 :         143324 :     newec->ec_broken = false;
 6702                           832                 :         143324 :     newec->ec_sortref = sortref;
 3343                           833                 :         143324 :     newec->ec_min_security = UINT_MAX;
                                834                 :         143324 :     newec->ec_max_security = 0;
 6994                           835                 :         143324 :     newec->ec_merged = NULL;
                                836                 :                : 
 5861 bruce@momjian.us          837   [ +  +  -  + ]:         143324 :     if (newec->ec_has_volatile && sortref == 0) /* should not happen */
 6028 tgl@sss.pgh.pa.us         838         [ #  # ]:UBC           0 :         elog(ERROR, "volatile EquivalenceClass has no sortref");
                                839                 :                : 
                                840                 :                :     /*
                                841                 :                :      * Get the precise set of relids appearing in the expression.
                                842                 :                :      */
 1879 tgl@sss.pgh.pa.us         843                 :CBC      143324 :     expr_relids = pull_varnos(root, (Node *) expr);
                                844                 :                : 
 4503                           845                 :         143324 :     newem = add_eq_member(newec, copyObject(expr), expr_relids,
                                846                 :                :                           jdomain, opcintype);
                                847                 :                : 
                                848                 :                :     /*
                                849                 :                :      * add_eq_member doesn't check for volatile functions, set-returning
                                850                 :                :      * functions, aggregates, or window functions, but such could appear in
                                851                 :                :      * sort expressions; so we have to check whether its const-marking was
                                852                 :                :      * correct.
                                853                 :                :      */
 6994                           854         [ +  + ]:         143324 :     if (newec->ec_has_const)
                                855                 :                :     {
 6826                           856   [ +  +  +  + ]:           9896 :         if (newec->ec_has_volatile ||
                                857         [ +  + ]:           9755 :             expression_returns_set((Node *) expr) ||
 6286                           858         [ +  + ]:           9592 :             contain_agg_clause((Node *) expr) ||
                                859                 :           4752 :             contain_window_function((Node *) expr))
                                860                 :                :         {
 6994                           861                 :            232 :             newec->ec_has_const = false;
 6992                           862                 :            232 :             newem->em_is_const = false;
                                863                 :                :         }
                                864                 :                :     }
                                865                 :                : 
 6994                           866                 :         143324 :     root->eq_classes = lappend(root->eq_classes, newec);
                                867                 :                : 
                                868                 :                :     /*
                                869                 :                :      * If EC merging is already complete, we have to mop up by adding the new
                                870                 :                :      * EC to the eclass_indexes of the relation(s) mentioned in it.
                                871                 :                :      */
 2429 drowley@postgresql.o      872         [ +  + ]:         143324 :     if (root->ec_merging_done)
                                873                 :                :     {
                                874                 :          82645 :         int         ec_index = list_length(root->eq_classes) - 1;
                                875                 :          82645 :         int         i = -1;
                                876                 :                : 
                                877         [ +  + ]:         159339 :         while ((i = bms_next_member(newec->ec_relids, i)) > 0)
                                878                 :                :         {
                                879                 :          76694 :             RelOptInfo *rel = root->simple_rel_array[i];
                                880                 :                : 
                                881                 :                :             /* ignore the RTE_GROUP RTE */
  551 rguo@postgresql.org       882         [ +  + ]:          76694 :             if (i == root->group_rtindex)
                                883                 :            356 :                 continue;
                                884                 :                : 
 1140 tgl@sss.pgh.pa.us         885         [ +  + ]:          76338 :             if (rel == NULL)    /* must be an outer join */
                                886                 :                :             {
                                887         [ -  + ]:           3422 :                 Assert(bms_is_member(i, root->outer_join_rels));
                                888                 :           3422 :                 continue;
                                889                 :                :             }
                                890                 :                : 
 1126                           891         [ -  + ]:          72916 :             Assert(rel->reloptkind == RELOPT_BASEREL);
                                892                 :                : 
 2429 drowley@postgresql.o      893                 :          72916 :             rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
                                894                 :                :                                                  ec_index);
                                895                 :                :         }
                                896                 :                :     }
                                897                 :                : 
 6994 tgl@sss.pgh.pa.us         898                 :         143324 :     MemoryContextSwitchTo(oldcontext);
                                899                 :                : 
                                900                 :         143324 :     return newec;
                                901                 :                : }
                                902                 :                : 
                                903                 :                : /*
                                904                 :                :  * find_ec_member_matching_expr
                                905                 :                :  *      Locate an EquivalenceClass member matching the given expr, if any;
                                906                 :                :  *      return NULL if no match.
                                907                 :                :  *
                                908                 :                :  * "Matching" is defined as "equal after stripping RelabelTypes".
                                909                 :                :  * This is used for identifying sort expressions, and we need to allow
                                910                 :                :  * binary-compatible relabeling for some cases involving binary-compatible
                                911                 :                :  * sort operators.
                                912                 :                :  *
                                913                 :                :  * Child EC members are ignored unless they belong to given 'relids'.
                                914                 :                :  */
                                915                 :                : EquivalenceMember *
 1790                           916                 :         183851 : find_ec_member_matching_expr(EquivalenceClass *ec,
                                917                 :                :                              Expr *expr,
                                918                 :                :                              Relids relids)
                                919                 :                : {
                                920                 :                :     EquivalenceMemberIterator it;
                                921                 :                :     EquivalenceMember *em;
                                922                 :                : 
                                923                 :                :     /* We ignore binary-compatible relabeling on both ends */
                                924   [ +  -  +  + ]:         197473 :     while (expr && IsA(expr, RelabelType))
                                925                 :          13622 :         expr = ((RelabelType *) expr)->arg;
                                926                 :                : 
  341 drowley@postgresql.o      927                 :         183851 :     setup_eclass_member_iterator(&it, ec, relids);
                                928         [ +  + ]:         308254 :     while ((em = eclass_member_iterator_next(&it)) != NULL)
                                929                 :                :     {
                                930                 :                :         Expr       *emexpr;
                                931                 :                : 
                                932                 :                :         /*
                                933                 :                :          * We shouldn't be trying to sort by an equivalence class that
                                934                 :                :          * contains a constant, so no need to consider such cases any further.
                                935                 :                :          */
 1790 tgl@sss.pgh.pa.us         936         [ -  + ]:         203331 :         if (em->em_is_const)
 1790 tgl@sss.pgh.pa.us         937                 :UBC           0 :             continue;
                                938                 :                : 
                                939                 :                :         /*
                                940                 :                :          * Ignore child members unless they belong to the requested rel.
                                941                 :                :          */
 1790 tgl@sss.pgh.pa.us         942         [ +  + ]:CBC      203331 :         if (em->em_is_child &&
                                943         [ +  + ]:           6059 :             !bms_is_subset(em->em_relids, relids))
                                944                 :           2040 :             continue;
                                945                 :                : 
                                946                 :                :         /*
                                947                 :                :          * Match if same expression (after stripping relabel).
                                948                 :                :          */
                                949                 :         201291 :         emexpr = em->em_expr;
                                950   [ +  -  +  + ]:         205415 :         while (emexpr && IsA(emexpr, RelabelType))
                                951                 :           4124 :             emexpr = ((RelabelType *) emexpr)->arg;
                                952                 :                : 
                                953         [ +  + ]:         201291 :         if (equal(emexpr, expr))
                                954                 :          78928 :             return em;
                                955                 :                :     }
                                956                 :                : 
                                957                 :         104923 :     return NULL;
                                958                 :                : }
                                959                 :                : 
                                960                 :                : /*
                                961                 :                :  * find_computable_ec_member
                                962                 :                :  *      Locate an EquivalenceClass member that can be computed from the
                                963                 :                :  *      expressions appearing in "exprs"; return NULL if no match.
                                964                 :                :  *
                                965                 :                :  * "exprs" can be either a list of bare expression trees, or a list of
                                966                 :                :  * TargetEntry nodes.  Typically it will contain Vars and possibly Aggrefs
                                967                 :                :  * and WindowFuncs; however, when considering an appendrel member the list
                                968                 :                :  * could contain arbitrary expressions.  We consider an EC member to be
                                969                 :                :  * computable if all the Vars, PlaceHolderVars, Aggrefs, and WindowFuncs
                                970                 :                :  * it needs are present in "exprs".
                                971                 :                :  *
                                972                 :                :  * There is some subtlety in that definition: for example, if an EC member is
                                973                 :                :  * Var_A + 1 while what is in "exprs" is Var_A + 2, it's still computable.
                                974                 :                :  * This works because in the final plan tree, the EC member's expression will
                                975                 :                :  * be computed as part of the same plan node targetlist that is currently
                                976                 :                :  * represented by "exprs".  So if we have Var_A available for the existing
                                977                 :                :  * tlist member, it must be OK to use it in the EC expression too.
                                978                 :                :  *
                                979                 :                :  * Unlike find_ec_member_matching_expr, there's no special provision here
                                980                 :                :  * for binary-compatible relabeling.  This is intentional: if we have to
                                981                 :                :  * compute an expression in this way, setrefs.c is going to insist on exact
                                982                 :                :  * matches of Vars to the source tlist.
                                983                 :                :  *
                                984                 :                :  * Child EC members are ignored unless they belong to given 'relids'.
                                985                 :                :  * Also, non-parallel-safe expressions are ignored if 'require_parallel_safe'.
                                986                 :                :  *
                                987                 :                :  * Note: some callers pass root == NULL for notational reasons.  This is OK
                                988                 :                :  * when require_parallel_safe is false.
                                989                 :                :  */
                                990                 :                : EquivalenceMember *
                                991                 :           4155 : find_computable_ec_member(PlannerInfo *root,
                                992                 :                :                           EquivalenceClass *ec,
                                993                 :                :                           List *exprs,
                                994                 :                :                           Relids relids,
                                995                 :                :                           bool require_parallel_safe)
                                996                 :                : {
                                997                 :                :     List       *exprvars;
                                998                 :                :     EquivalenceMemberIterator it;
                                999                 :                :     EquivalenceMember *em;
                               1000                 :                : 
                               1001                 :                :     /*
                               1002                 :                :      * Pull out the Vars and quasi-Vars present in "exprs".  In the typical
                               1003                 :                :      * non-appendrel case, this is just another representation of the same
                               1004                 :                :      * list.  However, it does remove the distinction between the case of a
                               1005                 :                :      * list of plain expressions and a list of TargetEntrys.
                               1006                 :                :      */
  519                          1007                 :           4155 :     exprvars = pull_var_clause((Node *) exprs,
                               1008                 :                :                                PVC_INCLUDE_AGGREGATES |
                               1009                 :                :                                PVC_INCLUDE_WINDOWFUNCS |
                               1010                 :                :                                PVC_INCLUDE_PLACEHOLDERS);
                               1011                 :                : 
  341 drowley@postgresql.o     1012                 :           4155 :     setup_eclass_member_iterator(&it, ec, relids);
                               1013         [ +  + ]:           8350 :     while ((em = eclass_member_iterator_next(&it)) != NULL)
                               1014                 :                :     {
                               1015                 :                :         List       *emvars;
                               1016                 :                :         ListCell   *lc2;
                               1017                 :                : 
                               1018                 :                :         /*
                               1019                 :                :          * We shouldn't be trying to sort by an equivalence class that
                               1020                 :                :          * contains a constant, so no need to consider such cases any further.
                               1021                 :                :          */
 1790 tgl@sss.pgh.pa.us        1022         [ -  + ]:           4437 :         if (em->em_is_const)
 1790 tgl@sss.pgh.pa.us        1023                 :UBC           0 :             continue;
                               1024                 :                : 
                               1025                 :                :         /*
                               1026                 :                :          * Ignore child members unless they belong to the requested rel.
                               1027                 :                :          */
 1790 tgl@sss.pgh.pa.us        1028         [ +  + ]:CBC        4437 :         if (em->em_is_child &&
                               1029         [ +  + ]:            162 :             !bms_is_subset(em->em_relids, relids))
                               1030                 :             66 :             continue;
                               1031                 :                : 
                               1032                 :                :         /*
                               1033                 :                :          * Match if all Vars and quasi-Vars are present in "exprs".
                               1034                 :                :          */
  519                          1035                 :           4371 :         emvars = pull_var_clause((Node *) em->em_expr,
                               1036                 :                :                                  PVC_INCLUDE_AGGREGATES |
                               1037                 :                :                                  PVC_INCLUDE_WINDOWFUNCS |
                               1038                 :                :                                  PVC_INCLUDE_PLACEHOLDERS);
                               1039   [ +  +  +  +  :           4728 :         foreach(lc2, emvars)
                                              +  + ]
                               1040                 :                :         {
                               1041         [ +  + ]:           4471 :             if (!list_member(exprvars, lfirst(lc2)))
 1790                          1042                 :           4114 :                 break;
                               1043                 :                :         }
  519                          1044                 :           4371 :         list_free(emvars);
 1790                          1045         [ +  + ]:           4371 :         if (lc2)
                               1046                 :           4114 :             continue;           /* we hit a non-available Var */
                               1047                 :                : 
                               1048                 :                :         /*
                               1049                 :                :          * If requested, reject expressions that are not parallel-safe.  We
                               1050                 :                :          * check this last because it's a rather expensive test.
                               1051                 :                :          */
                               1052         [ +  + ]:            257 :         if (require_parallel_safe &&
                               1053         [ +  + ]:             64 :             !is_parallel_safe(root, (Node *) em->em_expr))
                               1054                 :             15 :             continue;
                               1055                 :                : 
                               1056                 :            242 :         return em;              /* found usable expression */
                               1057                 :                :     }
                               1058                 :                : 
                               1059                 :           3913 :     return NULL;
                               1060                 :                : }
                               1061                 :                : 
                               1062                 :                : /*
                               1063                 :                :  * relation_can_be_sorted_early
                               1064                 :                :  *      Can this relation be sorted on this EC before the final output step?
                               1065                 :                :  *
                               1066                 :                :  * To succeed, we must find an EC member that prepare_sort_from_pathkeys knows
                               1067                 :                :  * how to sort on, given the rel's reltarget as input.  There are also a few
                               1068                 :                :  * additional constraints based on the fact that the desired sort will be done
                               1069                 :                :  * "early", within the scan/join part of the plan.  Also, non-parallel-safe
                               1070                 :                :  * expressions are ignored if 'require_parallel_safe'.
                               1071                 :                :  *
                               1072                 :                :  * At some point we might want to return the identified EquivalenceMember,
                               1073                 :                :  * but for now, callers only want to know if there is one.
                               1074                 :                :  */
                               1075                 :                : bool
                               1076                 :           9983 : relation_can_be_sorted_early(PlannerInfo *root, RelOptInfo *rel,
                               1077                 :                :                              EquivalenceClass *ec, bool require_parallel_safe)
                               1078                 :                : {
                               1079                 :           9983 :     PathTarget *target = rel->reltarget;
                               1080                 :                :     EquivalenceMember *em;
                               1081                 :                :     ListCell   *lc;
                               1082                 :                : 
                               1083                 :                :     /*
                               1084                 :                :      * Reject volatile ECs immediately; such sorts must always be postponed.
                               1085                 :                :      */
                               1086         [ +  + ]:           9983 :     if (ec->ec_has_volatile)
                               1087                 :             36 :         return false;
                               1088                 :                : 
                               1089                 :                :     /*
                               1090                 :                :      * Try to find an EM directly matching some reltarget member.
                               1091                 :                :      */
                               1092   [ +  +  +  +  :          22837 :     foreach(lc, target->exprs)
                                              +  + ]
                               1093                 :                :     {
                               1094                 :          18875 :         Expr       *targetexpr = (Expr *) lfirst(lc);
                               1095                 :                : 
                               1096                 :          18875 :         em = find_ec_member_matching_expr(ec, targetexpr, rel->relids);
                               1097         [ +  + ]:          18875 :         if (!em)
 1958 tomas.vondra@postgre     1098                 :          12890 :             continue;
                               1099                 :                : 
                               1100                 :                :         /*
                               1101                 :                :          * Reject expressions involving set-returning functions, as those
                               1102                 :                :          * can't be computed early either.  (Note: this test and the following
                               1103                 :                :          * one are effectively checking properties of targetexpr, so there's
                               1104                 :                :          * no point in asking whether some other EC member would be better.)
                               1105                 :                :          */
 1320 tgl@sss.pgh.pa.us        1106         [ -  + ]:           5985 :         if (expression_returns_set((Node *) em->em_expr))
 1910 tomas.vondra@postgre     1107                 :UBC           0 :             continue;
                               1108                 :                : 
                               1109                 :                :         /*
                               1110                 :                :          * If requested, reject expressions that are not parallel-safe.  We
                               1111                 :                :          * check this last because it's a rather expensive test.
                               1112                 :                :          */
 1790 tgl@sss.pgh.pa.us        1113         [ +  - ]:CBC        5985 :         if (require_parallel_safe &&
                               1114         [ -  + ]:           5985 :             !is_parallel_safe(root, (Node *) em->em_expr))
 1910 tomas.vondra@postgre     1115                 :UBC           0 :             continue;
                               1116                 :                : 
 1790 tgl@sss.pgh.pa.us        1117                 :CBC        5985 :         return true;
                               1118                 :                :     }
                               1119                 :                : 
                               1120                 :                :     /*
                               1121                 :                :      * Try to find an expression computable from the reltarget.
                               1122                 :                :      */
                               1123                 :           3962 :     em = find_computable_ec_member(root, ec, target->exprs, rel->relids,
                               1124                 :                :                                    require_parallel_safe);
                               1125         [ +  + ]:           3962 :     if (!em)
                               1126                 :           3913 :         return false;
                               1127                 :                : 
                               1128                 :                :     /*
                               1129                 :                :      * Reject expressions involving set-returning functions, as those can't be
                               1130                 :                :      * computed early either.  (There's no point in looking for another EC
                               1131                 :                :      * member in this case; since SRFs can't appear in WHERE, they cannot
                               1132                 :                :      * belong to multi-member ECs.)
                               1133                 :                :      */
 1320                          1134         [ +  + ]:             49 :     if (expression_returns_set((Node *) em->em_expr))
 1790                          1135                 :              6 :         return false;
                               1136                 :                : 
                               1137                 :             43 :     return true;
                               1138                 :                : }
                               1139                 :                : 
                               1140                 :                : /*
                               1141                 :                :  * generate_base_implied_equalities
                               1142                 :                :  *    Generate any restriction clauses that we can deduce from equivalence
                               1143                 :                :  *    classes.
                               1144                 :                :  *
                               1145                 :                :  * When an EC contains pseudoconstants, our strategy is to generate
                               1146                 :                :  * "member = const1" clauses where const1 is the first constant member, for
                               1147                 :                :  * every other member (including other constants).  If we are able to do this
                               1148                 :                :  * then we don't need any "var = var" comparisons because we've successfully
                               1149                 :                :  * constrained all the vars at their points of creation.  If we fail to
                               1150                 :                :  * generate any of these clauses due to lack of cross-type operators, we fall
                               1151                 :                :  * back to the "ec_broken" strategy described below.  (XXX if there are
                               1152                 :                :  * multiple constants of different types, it's possible that we might succeed
                               1153                 :                :  * in forming all the required clauses if we started from a different const
                               1154                 :                :  * member; but this seems a sufficiently hokey corner case to not be worth
                               1155                 :                :  * spending lots of cycles on.)
                               1156                 :                :  *
                               1157                 :                :  * For ECs that contain no pseudoconstants, we generate derived clauses
                               1158                 :                :  * "member1 = member2" for each pair of members belonging to the same base
                               1159                 :                :  * relation (actually, if there are more than two for the same base relation,
                               1160                 :                :  * we only need enough clauses to link each to each other).  This provides
                               1161                 :                :  * the base case for the recursion: each row emitted by a base relation scan
                               1162                 :                :  * will constrain all computable members of the EC to be equal.  As each
                               1163                 :                :  * join path is formed, we'll add additional derived clauses on-the-fly
                               1164                 :                :  * to maintain this invariant (see generate_join_implied_equalities).
                               1165                 :                :  *
                               1166                 :                :  * If the opfamilies used by the EC do not provide complete sets of cross-type
                               1167                 :                :  * equality operators, it is possible that we will fail to generate a clause
                               1168                 :                :  * that must be generated to maintain the invariant.  (An example: given
                               1169                 :                :  * "WHERE a.x = b.y AND b.y = a.z", the scheme breaks down if we cannot
                               1170                 :                :  * generate "a.x = a.z" as a restriction clause for A.)  In this case we mark
                               1171                 :                :  * the EC "ec_broken" and fall back to regurgitating its original source
                               1172                 :                :  * RestrictInfos at appropriate times.  We do not try to retract any derived
                               1173                 :                :  * clauses already generated from the broken EC, so the resulting plan could
                               1174                 :                :  * be poor due to bad selectivity estimates caused by redundant clauses.  But
                               1175                 :                :  * the correct solution to that is to fix the opfamilies ...
                               1176                 :                :  *
                               1177                 :                :  * Equality clauses derived by this function are passed off to
                               1178                 :                :  * process_implied_equality (in plan/initsplan.c) to be inserted into the
                               1179                 :                :  * restrictinfo datastructures.  Note that this must be called after initial
                               1180                 :                :  * scanning of the quals and before Path construction begins.
                               1181                 :                :  *
                               1182                 :                :  * We make no attempt to avoid generating duplicate RestrictInfos here: we
                               1183                 :                :  * don't search existing source or derived clauses in the EC for matches.  It
                               1184                 :                :  * doesn't really seem worth the trouble to do so.
                               1185                 :                :  */
                               1186                 :                : void
 6994                          1187                 :         184686 : generate_base_implied_equalities(PlannerInfo *root)
                               1188                 :                : {
                               1189                 :                :     int         ec_index;
                               1190                 :                :     ListCell   *lc;
                               1191                 :                : 
                               1192                 :                :     /*
                               1193                 :                :      * At this point, we're done absorbing knowledge of equivalences in the
                               1194                 :                :      * query, so no further EC merging should happen, and ECs remaining in the
                               1195                 :                :      * eq_classes list can be considered canonical.  (But note that it's still
                               1196                 :                :      * possible for new single-member ECs to be added through
                               1197                 :                :      * get_eclass_for_sort_expr().)
                               1198                 :                :      */
 2429 drowley@postgresql.o     1199                 :         184686 :     root->ec_merging_done = true;
                               1200                 :                : 
                               1201                 :         184686 :     ec_index = 0;
 6994 tgl@sss.pgh.pa.us        1202   [ +  +  +  +  :         415264 :     foreach(lc, root->eq_classes)
                                              +  + ]
                               1203                 :                :     {
                               1204                 :         230578 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
 2429 drowley@postgresql.o     1205                 :         230578 :         bool        can_generate_joinclause = false;
                               1206                 :                :         int         i;
                               1207                 :                : 
 6695 bruce@momjian.us         1208         [ -  + ]:         230578 :         Assert(ec->ec_merged == NULL);   /* else shouldn't be in list */
                               1209         [ -  + ]:         230578 :         Assert(!ec->ec_broken); /* not yet anyway... */
                               1210                 :                : 
                               1211                 :                :         /*
                               1212                 :                :          * Generate implied equalities that are restriction clauses.
                               1213                 :                :          * Single-member ECs won't generate any deductions, either here or at
                               1214                 :                :          * the join level.
                               1215                 :                :          */
 2429 drowley@postgresql.o     1216         [ +  + ]:         230578 :         if (list_length(ec->ec_members) > 1)
                               1217                 :                :         {
                               1218         [ +  + ]:         170944 :             if (ec->ec_has_const)
                               1219                 :         125605 :                 generate_base_implied_equalities_const(root, ec);
                               1220                 :                :             else
                               1221                 :          45339 :                 generate_base_implied_equalities_no_const(root, ec);
                               1222                 :                : 
                               1223                 :                :             /* Recover if we failed to generate required derived clauses */
                               1224         [ +  + ]:         170944 :             if (ec->ec_broken)
                               1225                 :             15 :                 generate_base_implied_equalities_broken(root, ec);
                               1226                 :                : 
                               1227                 :                :             /* Detect whether this EC might generate join clauses */
                               1228                 :         170944 :             can_generate_joinclause =
                               1229                 :         170944 :                 (bms_membership(ec->ec_relids) == BMS_MULTIPLE);
                               1230                 :                :         }
                               1231                 :                : 
                               1232                 :                :         /*
                               1233                 :                :          * Mark the base rels cited in each eclass (which should all exist by
                               1234                 :                :          * now) with the eq_classes indexes of all eclasses mentioning them.
                               1235                 :                :          * This will let us avoid searching in subsequent lookups.  While
                               1236                 :                :          * we're at it, we can mark base rels that have pending eclass joins;
                               1237                 :                :          * this is a cheap version of has_relevant_eclass_joinclause().
                               1238                 :                :          */
                               1239                 :         230578 :         i = -1;
                               1240         [ +  + ]:         519035 :         while ((i = bms_next_member(ec->ec_relids, i)) > 0)
                               1241                 :                :         {
                               1242                 :         288457 :             RelOptInfo *rel = root->simple_rel_array[i];
                               1243                 :                : 
                               1244                 :                :             /* ignore the RTE_GROUP RTE */
  551 rguo@postgresql.org      1245         [ -  + ]:         288457 :             if (i == root->group_rtindex)
  551 rguo@postgresql.org      1246                 :UBC           0 :                 continue;
                               1247                 :                : 
 1140 tgl@sss.pgh.pa.us        1248         [ +  + ]:CBC      288457 :             if (rel == NULL)    /* must be an outer join */
                               1249                 :                :             {
                               1250         [ -  + ]:           3044 :                 Assert(bms_is_member(i, root->outer_join_rels));
                               1251                 :           3044 :                 continue;
                               1252                 :                :             }
                               1253                 :                : 
 2429 drowley@postgresql.o     1254         [ -  + ]:         285413 :             Assert(rel->reloptkind == RELOPT_BASEREL);
                               1255                 :                : 
                               1256                 :         285413 :             rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
                               1257                 :                :                                                  ec_index);
                               1258                 :                : 
                               1259         [ +  + ]:         285413 :             if (can_generate_joinclause)
                               1260                 :         109076 :                 rel->has_eclass_joins = true;
                               1261                 :                :         }
                               1262                 :                : 
                               1263                 :         230578 :         ec_index++;
                               1264                 :                :     }
 6994 tgl@sss.pgh.pa.us        1265                 :         184686 : }
                               1266                 :                : 
                               1267                 :                : /*
                               1268                 :                :  * generate_base_implied_equalities when EC contains pseudoconstant(s)
                               1269                 :                :  */
                               1270                 :                : static void
                               1271                 :         125605 : generate_base_implied_equalities_const(PlannerInfo *root,
                               1272                 :                :                                        EquivalenceClass *ec)
                               1273                 :                : {
                               1274                 :         125605 :     EquivalenceMember *const_em = NULL;
                               1275                 :                :     ListCell   *lc;
                               1276                 :                : 
                               1277                 :                :     /*
                               1278                 :                :      * In the trivial case where we just had one "var = const" clause, push
                               1279                 :                :      * the original clause back into the main planner machinery.  There is
                               1280                 :                :      * nothing to be gained by doing it differently, and we save the effort to
                               1281                 :                :      * re-build and re-analyze an equality clause that will be exactly
                               1282                 :                :      * equivalent to the old one.
                               1283                 :                :      */
 6687                          1284   [ +  +  +  - ]:         241803 :     if (list_length(ec->ec_members) == 2 &&
                               1285                 :         116198 :         list_length(ec->ec_sources) == 1)
                               1286                 :                :     {
                               1287                 :         116198 :         RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
                               1288                 :                : 
 1140                          1289                 :         116198 :         distribute_restrictinfo_to_rels(root, restrictinfo);
                               1290                 :         116198 :         return;
                               1291                 :                :     }
                               1292                 :                : 
                               1293                 :                :     /* We don't expect any children yet */
  341 drowley@postgresql.o     1294         [ -  + ]:           9407 :     Assert(ec->ec_childmembers == NULL);
                               1295                 :                : 
                               1296                 :                :     /*
                               1297                 :                :      * Find the constant member to use.  We prefer an actual constant to
                               1298                 :                :      * pseudo-constants (such as Params), because the constraint exclusion
                               1299                 :                :      * machinery might be able to exclude relations on the basis of generated
                               1300                 :                :      * "var = const" equalities, but "var = param" won't work for that.
                               1301                 :                :      */
 6994 tgl@sss.pgh.pa.us        1302   [ +  -  +  +  :          21928 :     foreach(lc, ec->ec_members)
                                              +  + ]
                               1303                 :                :     {
                               1304                 :          21886 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
                               1305                 :                : 
                               1306         [ +  + ]:          21886 :         if (cur_em->em_is_const)
                               1307                 :                :         {
                               1308                 :           9410 :             const_em = cur_em;
 4888                          1309         [ +  + ]:           9410 :             if (IsA(cur_em->em_expr, Const))
                               1310                 :           9365 :                 break;
                               1311                 :                :         }
                               1312                 :                :     }
 6994                          1313         [ -  + ]:           9407 :     Assert(const_em != NULL);
                               1314                 :                : 
                               1315                 :                :     /* Generate a derived equality against each other member */
                               1316   [ +  -  +  +  :          37694 :     foreach(lc, ec->ec_members)
                                              +  + ]
                               1317                 :                :     {
                               1318                 :          28302 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
                               1319                 :                :         Oid         eq_op;
                               1320                 :                :         RestrictInfo *rinfo;
                               1321                 :                : 
                               1322                 :                :         /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     1323         [ -  + ]:          28302 :         Assert(!cur_em->em_is_child);
 6994 tgl@sss.pgh.pa.us        1324         [ +  + ]:          28302 :         if (cur_em == const_em)
                               1325                 :           9395 :             continue;
                               1326                 :          18907 :         eq_op = select_equality_operator(ec,
                               1327                 :                :                                          cur_em->em_datatype,
                               1328                 :                :                                          const_em->em_datatype);
                               1329         [ +  + ]:          18907 :         if (!OidIsValid(eq_op))
                               1330                 :                :         {
                               1331                 :                :             /* failed... */
                               1332                 :             15 :             ec->ec_broken = true;
                               1333                 :             15 :             break;
                               1334                 :                :         }
                               1335                 :                : 
                               1336                 :                :         /*
                               1337                 :                :          * We use the constant's em_jdomain as qualscope, so that if the
                               1338                 :                :          * generated clause is variable-free (i.e, both EMs are consts) it
                               1339                 :                :          * will be enforced at the join domain level.
                               1340                 :                :          */
 1964                          1341                 :          18892 :         rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
                               1342                 :                :                                          cur_em->em_expr, const_em->em_expr,
 1140                          1343                 :          18892 :                                          const_em->em_jdomain->jd_relids,
                               1344                 :                :                                          ec->ec_min_security,
 1964                          1345                 :          18892 :                                          cur_em->em_is_const);
                               1346                 :                : 
                               1347                 :                :         /*
                               1348                 :                :          * If the clause didn't degenerate to a constant, fill in the correct
                               1349                 :                :          * markings for a mergejoinable clause, and save it as a derived
                               1350                 :                :          * clause. (We will not re-use such clauses directly, but selectivity
                               1351                 :                :          * estimation may consult those later.  Note that this use of derived
                               1352                 :                :          * clauses does not overlap with its use for join clauses, since we
                               1353                 :                :          * never generate join clauses from an ec_has_const eclass.)
                               1354                 :                :          */
                               1355   [ +  -  +  + ]:          18892 :         if (rinfo && rinfo->mergeopfamilies)
                               1356                 :                :         {
                               1357                 :                :             /* it's not redundant, so don't set parent_ec */
                               1358                 :          18826 :             rinfo->left_ec = rinfo->right_ec = ec;
                               1359                 :          18826 :             rinfo->left_em = cur_em;
                               1360                 :          18826 :             rinfo->right_em = const_em;
  345 amitlan@postgresql.o     1361                 :          18826 :             ec_add_derived_clause(ec, rinfo);
                               1362                 :                :         }
                               1363                 :                :     }
                               1364                 :                : }
                               1365                 :                : 
                               1366                 :                : /*
                               1367                 :                :  * generate_base_implied_equalities when EC contains no pseudoconstants
                               1368                 :                :  */
                               1369                 :                : static void
 6994 tgl@sss.pgh.pa.us        1370                 :          45339 : generate_base_implied_equalities_no_const(PlannerInfo *root,
                               1371                 :                :                                           EquivalenceClass *ec)
                               1372                 :                : {
                               1373                 :                :     EquivalenceMember **prev_ems;
                               1374                 :                :     ListCell   *lc;
                               1375                 :                : 
                               1376                 :                :     /*
                               1377                 :                :      * We scan the EC members once and track the last-seen member for each
                               1378                 :                :      * base relation.  When we see another member of the same base relation,
                               1379                 :                :      * we generate "prev_em = cur_em".  This results in the minimum number of
                               1380                 :                :      * derived clauses, but it's possible that it will fail when a different
                               1381                 :                :      * ordering would succeed.  XXX FIXME: use a UNION-FIND algorithm similar
                               1382                 :                :      * to the way we build merged ECs.  (Use a list-of-lists for each rel.)
                               1383                 :                :      */
                               1384                 :                :     prev_ems = (EquivalenceMember **)
                               1385                 :          45339 :         palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
                               1386                 :                : 
                               1387                 :                :     /* We don't expect any children yet */
  341 drowley@postgresql.o     1388         [ -  + ]:          45339 :     Assert(ec->ec_childmembers == NULL);
                               1389                 :                : 
 6994 tgl@sss.pgh.pa.us        1390   [ +  -  +  +  :         137508 :     foreach(lc, ec->ec_members)
                                              +  + ]
                               1391                 :                :     {
                               1392                 :          92169 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
                               1393                 :                :         int         relid;
                               1394                 :                : 
                               1395                 :                :         /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     1396         [ -  + ]:          92169 :         Assert(!cur_em->em_is_child);
                               1397                 :                : 
 4125 tgl@sss.pgh.pa.us        1398         [ +  + ]:          92169 :         if (!bms_get_singleton_member(cur_em->em_relids, &relid))
 6994                          1399                 :             99 :             continue;
                               1400         [ -  + ]:          92070 :         Assert(relid < root->simple_rel_array_size);
                               1401                 :                : 
                               1402         [ +  + ]:          92070 :         if (prev_ems[relid] != NULL)
                               1403                 :                :         {
                               1404                 :            227 :             EquivalenceMember *prev_em = prev_ems[relid];
                               1405                 :                :             Oid         eq_op;
                               1406                 :                :             RestrictInfo *rinfo;
                               1407                 :                : 
                               1408                 :            227 :             eq_op = select_equality_operator(ec,
                               1409                 :                :                                              prev_em->em_datatype,
                               1410                 :                :                                              cur_em->em_datatype);
                               1411         [ -  + ]:            227 :             if (!OidIsValid(eq_op))
                               1412                 :                :             {
                               1413                 :                :                 /* failed... */
 6994 tgl@sss.pgh.pa.us        1414                 :UBC           0 :                 ec->ec_broken = true;
                               1415                 :              0 :                 break;
                               1416                 :                :             }
                               1417                 :                : 
                               1418                 :                :             /*
                               1419                 :                :              * The expressions aren't constants, so the passed qualscope will
                               1420                 :                :              * never be used to place the generated clause.  We just need to
                               1421                 :                :              * be sure it covers both expressions, which em_relids should do.
                               1422                 :                :              */
 1964 tgl@sss.pgh.pa.us        1423                 :CBC         227 :             rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
                               1424                 :                :                                              prev_em->em_expr, cur_em->em_expr,
                               1425                 :                :                                              cur_em->em_relids,
                               1426                 :                :                                              ec->ec_min_security,
                               1427                 :                :                                              false);
                               1428                 :                : 
                               1429                 :                :             /*
                               1430                 :                :              * If the clause didn't degenerate to a constant, fill in the
                               1431                 :                :              * correct markings for a mergejoinable clause.  We don't record
                               1432                 :                :              * it as a derived clause, since we don't currently need to
                               1433                 :                :              * re-find such clauses, and don't want to clutter the
                               1434                 :                :              * derived-clause set with non-join clauses.
                               1435                 :                :              */
                               1436   [ +  -  +  - ]:            227 :             if (rinfo && rinfo->mergeopfamilies)
                               1437                 :                :             {
                               1438                 :                :                 /* it's not redundant, so don't set parent_ec */
                               1439                 :            227 :                 rinfo->left_ec = rinfo->right_ec = ec;
                               1440                 :            227 :                 rinfo->left_em = prev_em;
                               1441                 :            227 :                 rinfo->right_em = cur_em;
                               1442                 :                :             }
                               1443                 :                :         }
 6994                          1444                 :          92070 :         prev_ems[relid] = cur_em;
                               1445                 :                :     }
                               1446                 :                : 
                               1447                 :          45339 :     pfree(prev_ems);
                               1448                 :                : 
                               1449                 :                :     /*
                               1450                 :                :      * We also have to make sure that all the Vars used in the member clauses
                               1451                 :                :      * will be available at any join node we might try to reference them at.
                               1452                 :                :      * For the moment we force all the Vars to be available at all join nodes
                               1453                 :                :      * for this eclass.  Perhaps this could be improved by doing some
                               1454                 :                :      * pre-analysis of which members we prefer to join, but it's no worse than
                               1455                 :                :      * what happened in the pre-8.3 code.  (Note: rebuild_eclass_attr_needed
                               1456                 :                :      * needs to match this code.)
                               1457                 :                :      */
                               1458   [ +  -  +  +  :         137508 :     foreach(lc, ec->ec_members)
                                              +  + ]
                               1459                 :                :     {
                               1460                 :          92169 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
 6174                          1461                 :          92169 :         List       *vars = pull_var_clause((Node *) cur_em->em_expr,
                               1462                 :                :                                            PVC_RECURSE_AGGREGATES |
                               1463                 :                :                                            PVC_RECURSE_WINDOWFUNCS |
                               1464                 :                :                                            PVC_INCLUDE_PLACEHOLDERS);
                               1465                 :                : 
 1306                          1466                 :          92169 :         add_vars_to_targetlist(root, vars, ec->ec_relids);
 6994                          1467                 :          92169 :         list_free(vars);
                               1468                 :                :     }
                               1469                 :          45339 : }
                               1470                 :                : 
                               1471                 :                : /*
                               1472                 :                :  * generate_base_implied_equalities cleanup after failure
                               1473                 :                :  *
                               1474                 :                :  * What we must do here is push any zero- or one-relation source RestrictInfos
                               1475                 :                :  * of the EC back into the main restrictinfo datastructures.  Multi-relation
                               1476                 :                :  * clauses will be regurgitated later by generate_join_implied_equalities().
                               1477                 :                :  * (We do it this way to maintain continuity with the case that ec_broken
                               1478                 :                :  * becomes set only after we've gone up a join level or two.)  However, for
                               1479                 :                :  * an EC that contains constants, we can adopt a simpler strategy and just
                               1480                 :                :  * throw back all the source RestrictInfos immediately; that works because
                               1481                 :                :  * we know that such an EC can't become broken later.  (This rule justifies
                               1482                 :                :  * ignoring ec_has_const ECs in generate_join_implied_equalities, even when
                               1483                 :                :  * they are broken.)
                               1484                 :                :  */
                               1485                 :                : static void
                               1486                 :             15 : generate_base_implied_equalities_broken(PlannerInfo *root,
                               1487                 :                :                                         EquivalenceClass *ec)
                               1488                 :                : {
                               1489                 :                :     ListCell   *lc;
                               1490                 :                : 
                               1491   [ +  -  +  +  :             48 :     foreach(lc, ec->ec_sources)
                                              +  + ]
                               1492                 :                :     {
                               1493                 :             33 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
                               1494                 :                : 
 5078                          1495   [ -  +  -  - ]:             33 :         if (ec->ec_has_const ||
 5078 tgl@sss.pgh.pa.us        1496                 :UBC           0 :             bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
 6994 tgl@sss.pgh.pa.us        1497                 :CBC          33 :             distribute_restrictinfo_to_rels(root, restrictinfo);
                               1498                 :                :     }
                               1499                 :             15 : }
                               1500                 :                : 
                               1501                 :                : 
                               1502                 :                : /*
                               1503                 :                :  * generate_join_implied_equalities
                               1504                 :                :  *    Generate any join clauses that we can deduce from equivalence classes.
                               1505                 :                :  *
                               1506                 :                :  * At a join node, we must enforce restriction clauses sufficient to ensure
                               1507                 :                :  * that all equivalence-class members computable at that node are equal.
                               1508                 :                :  * Since the set of clauses to enforce can vary depending on which subset
                               1509                 :                :  * relations are the inputs, we have to compute this afresh for each join
                               1510                 :                :  * relation pair.  Hence a fresh List of RestrictInfo nodes is built and
                               1511                 :                :  * passed back on each call.
                               1512                 :                :  *
                               1513                 :                :  * In addition to its use at join nodes, this can be applied to generate
                               1514                 :                :  * eclass-based join clauses for use in a parameterized scan of a base rel.
                               1515                 :                :  * The reason for the asymmetry of specifying the inner rel as a RelOptInfo
                               1516                 :                :  * and the outer rel by Relids is that this usage occurs before we have
                               1517                 :                :  * built any join RelOptInfos.
                               1518                 :                :  *
                               1519                 :                :  * An annoying special case for parameterized scans is that the inner rel can
                               1520                 :                :  * be an appendrel child (an "other rel").  In this case we must generate
                               1521                 :                :  * appropriate clauses using child EC members.  add_child_rel_equivalences
                               1522                 :                :  * must already have been done for the child rel.
                               1523                 :                :  *
                               1524                 :                :  * The results are sufficient for use in merge, hash, and plain nestloop join
                               1525                 :                :  * methods.  We do not worry here about selecting clauses that are optimal
                               1526                 :                :  * for use in a parameterized indexscan.  indxpath.c makes its own selections
                               1527                 :                :  * of clauses to use, and if the ones we pick here are redundant with those,
                               1528                 :                :  * the extras will be eliminated at createplan time, using the parent_ec
                               1529                 :                :  * markers that we provide (see is_redundant_derived_clause()).
                               1530                 :                :  *
                               1531                 :                :  * Because the same join clauses are likely to be needed multiple times as
                               1532                 :                :  * we consider different join paths, we avoid generating multiple copies:
                               1533                 :                :  * whenever we select a particular pair of EquivalenceMembers to join,
                               1534                 :                :  * we check to see if the pair matches any original clause (in ec_sources)
                               1535                 :                :  * or previously-built derived clause.  This saves memory and allows
                               1536                 :                :  * re-use of information cached in RestrictInfos.  We also avoid generating
                               1537                 :                :  * commutative duplicates, i.e. if the algorithm selects "a.x = b.y" but
                               1538                 :                :  * we already have "b.y = a.x", we return the existing clause.
                               1539                 :                :  *
                               1540                 :                :  * If we are considering an outer join, sjinfo is the associated OJ info,
                               1541                 :                :  * otherwise it can be NULL.
                               1542                 :                :  *
                               1543                 :                :  * join_relids should always equal bms_union(outer_relids, inner_rel->relids)
                               1544                 :                :  * plus whatever add_outer_joins_to_relids() would add.  We could simplify
                               1545                 :                :  * this function's API by computing it internally, but most callers have the
                               1546                 :                :  * value at hand anyway.
                               1547                 :                :  */
                               1548                 :                : List *
                               1549                 :         331921 : generate_join_implied_equalities(PlannerInfo *root,
                               1550                 :                :                                  Relids join_relids,
                               1551                 :                :                                  Relids outer_relids,
                               1552                 :                :                                  RelOptInfo *inner_rel,
                               1553                 :                :                                  SpecialJoinInfo *sjinfo)
                               1554                 :                : {
 2429 drowley@postgresql.o     1555                 :         331921 :     List       *result = NIL;
                               1556                 :         331921 :     Relids      inner_relids = inner_rel->relids;
                               1557                 :                :     Relids      nominal_inner_relids;
                               1558                 :                :     Relids      nominal_join_relids;
                               1559                 :                :     Bitmapset  *matching_ecs;
                               1560                 :                :     int         i;
                               1561                 :                : 
                               1562                 :                :     /* If inner rel is a child, extra setup work is needed */
                               1563   [ +  +  +  +  :         331921 :     if (IS_OTHER_REL(inner_rel))
                                              -  + ]
                               1564                 :                :     {
                               1565         [ -  + ]:           3842 :         Assert(!bms_is_empty(inner_rel->top_parent_relids));
                               1566                 :                : 
                               1567                 :                :         /* Fetch relid set for the topmost parent rel */
                               1568                 :           3842 :         nominal_inner_relids = inner_rel->top_parent_relids;
                               1569                 :                :         /* ECs will be marked with the parent's relid, not the child's */
                               1570                 :           3842 :         nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
 1033 tgl@sss.pgh.pa.us        1571                 :           3842 :         nominal_join_relids = add_outer_joins_to_relids(root,
                               1572                 :                :                                                         nominal_join_relids,
                               1573                 :                :                                                         sjinfo,
                               1574                 :                :                                                         NULL);
                               1575                 :                :     }
                               1576                 :                :     else
                               1577                 :                :     {
 2429 drowley@postgresql.o     1578                 :         328079 :         nominal_inner_relids = inner_relids;
                               1579                 :         328079 :         nominal_join_relids = join_relids;
                               1580                 :                :     }
                               1581                 :                : 
                               1582                 :                :     /*
                               1583                 :                :      * Examine all potentially-relevant eclasses.
                               1584                 :                :      *
                               1585                 :                :      * If we are considering an outer join, we must include "join" clauses
                               1586                 :                :      * that mention either input rel plus the outer join's relid; these
                               1587                 :                :      * represent post-join filter clauses that have to be applied at this
                               1588                 :                :      * join.  We don't have infrastructure that would let us identify such
                               1589                 :                :      * eclasses cheaply, so just fall back to considering all eclasses
                               1590                 :                :      * mentioning anything in nominal_join_relids.
                               1591                 :                :      *
                               1592                 :                :      * At inner joins, we can be smarter: only consider eclasses mentioning
                               1593                 :                :      * both input rels.
                               1594                 :                :      */
 1033 tgl@sss.pgh.pa.us        1595   [ +  +  +  + ]:         331921 :     if (sjinfo && sjinfo->ojrelid != 0)
 1116                          1596                 :          56624 :         matching_ecs = get_eclass_indexes_for_relids(root, nominal_join_relids);
                               1597                 :                :     else
                               1598                 :         275297 :         matching_ecs = get_common_eclass_indexes(root, nominal_inner_relids,
                               1599                 :                :                                                  outer_relids);
                               1600                 :                : 
 2429 drowley@postgresql.o     1601                 :         331921 :     i = -1;
                               1602         [ +  + ]:         957235 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
                               1603                 :                :     {
                               1604                 :         625314 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
                               1605                 :         625314 :         List       *sublist = NIL;
                               1606                 :                : 
                               1607                 :                :         /* ECs containing consts do not need any further enforcement */
                               1608         [ +  + ]:         625314 :         if (ec->ec_has_const)
                               1609                 :          73749 :             continue;
                               1610                 :                : 
                               1611                 :                :         /* Single-member ECs won't generate any deductions */
                               1612         [ +  + ]:         551565 :         if (list_length(ec->ec_members) <= 1)
                               1613                 :         285274 :             continue;
                               1614                 :                : 
                               1615                 :                :         /* Sanity check that this eclass overlaps the join */
                               1616         [ -  + ]:         266291 :         Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
                               1617                 :                : 
                               1618         [ +  + ]:         266291 :         if (!ec->ec_broken)
                               1619                 :         266129 :             sublist = generate_join_implied_equalities_normal(root,
                               1620                 :                :                                                               ec,
                               1621                 :                :                                                               join_relids,
                               1622                 :                :                                                               outer_relids,
                               1623                 :                :                                                               inner_relids);
                               1624                 :                : 
                               1625                 :                :         /* Recover if we failed to generate required derived clauses */
                               1626         [ +  + ]:         266291 :         if (ec->ec_broken)
                               1627                 :            180 :             sublist = generate_join_implied_equalities_broken(root,
                               1628                 :                :                                                               ec,
                               1629                 :                :                                                               nominal_join_relids,
                               1630                 :                :                                                               outer_relids,
                               1631                 :                :                                                               nominal_inner_relids,
                               1632                 :                :                                                               inner_rel);
                               1633                 :                : 
                               1634                 :         266291 :         result = list_concat(result, sublist);
                               1635                 :                :     }
                               1636                 :                : 
                               1637                 :         331921 :     return result;
                               1638                 :                : }
                               1639                 :                : 
                               1640                 :                : /*
                               1641                 :                :  * generate_join_implied_equalities_for_ecs
                               1642                 :                :  *    As above, but consider only the listed ECs.
                               1643                 :                :  *
                               1644                 :                :  * For the sole current caller, we can assume sjinfo == NULL, that is we are
                               1645                 :                :  * not interested in outer-join filter clauses.  This might need to change
                               1646                 :                :  * in future.
                               1647                 :                :  */
                               1648                 :                : List *
 3607 tgl@sss.pgh.pa.us        1649                 :           3995 : generate_join_implied_equalities_for_ecs(PlannerInfo *root,
                               1650                 :                :                                          List *eclasses,
                               1651                 :                :                                          Relids join_relids,
                               1652                 :                :                                          Relids outer_relids,
                               1653                 :                :                                          RelOptInfo *inner_rel)
                               1654                 :                : {
 6994                          1655                 :           3995 :     List       *result = NIL;
 5078                          1656                 :           3995 :     Relids      inner_relids = inner_rel->relids;
                               1657                 :                :     Relids      nominal_inner_relids;
                               1658                 :                :     Relids      nominal_join_relids;
                               1659                 :                :     ListCell   *lc;
                               1660                 :                : 
                               1661                 :                :     /* If inner rel is a child, extra setup work is needed */
 3268 rhaas@postgresql.org     1662   [ +  +  +  -  :           3995 :     if (IS_OTHER_REL(inner_rel))
                                              -  + ]
                               1663                 :                :     {
 3268 rhaas@postgresql.org     1664         [ -  + ]:GBC           9 :         Assert(!bms_is_empty(inner_rel->top_parent_relids));
                               1665                 :                : 
                               1666                 :                :         /* Fetch relid set for the topmost parent rel */
                               1667                 :              9 :         nominal_inner_relids = inner_rel->top_parent_relids;
                               1668                 :                :         /* ECs will be marked with the parent's relid, not the child's */
 5078 tgl@sss.pgh.pa.us        1669                 :              9 :         nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
                               1670                 :                :     }
                               1671                 :                :     else
                               1672                 :                :     {
 5078 tgl@sss.pgh.pa.us        1673                 :CBC        3986 :         nominal_inner_relids = inner_relids;
                               1674                 :           3986 :         nominal_join_relids = join_relids;
                               1675                 :                :     }
                               1676                 :                : 
 3607                          1677   [ +  -  +  +  :           8338 :     foreach(lc, eclasses)
                                              +  + ]
                               1678                 :                :     {
 6994                          1679                 :           4343 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
 6695 bruce@momjian.us         1680                 :           4343 :         List       *sublist = NIL;
                               1681                 :                : 
                               1682                 :                :         /* ECs containing consts do not need any further enforcement */
 6994 tgl@sss.pgh.pa.us        1683         [ -  + ]:           4343 :         if (ec->ec_has_const)
 6994 tgl@sss.pgh.pa.us        1684                 :UBC           0 :             continue;
                               1685                 :                : 
                               1686                 :                :         /* Single-member ECs won't generate any deductions */
 6994 tgl@sss.pgh.pa.us        1687         [ -  + ]:CBC        4343 :         if (list_length(ec->ec_members) <= 1)
 6994 tgl@sss.pgh.pa.us        1688                 :UBC           0 :             continue;
                               1689                 :                : 
                               1690                 :                :         /* We can quickly ignore any that don't overlap the join, too */
 5078 tgl@sss.pgh.pa.us        1691         [ -  + ]:CBC        4343 :         if (!bms_overlap(ec->ec_relids, nominal_join_relids))
 6994 tgl@sss.pgh.pa.us        1692                 :UBC           0 :             continue;
                               1693                 :                : 
 6994 tgl@sss.pgh.pa.us        1694         [ +  - ]:CBC        4343 :         if (!ec->ec_broken)
                               1695                 :           4343 :             sublist = generate_join_implied_equalities_normal(root,
                               1696                 :                :                                                               ec,
                               1697                 :                :                                                               join_relids,
                               1698                 :                :                                                               outer_relids,
                               1699                 :                :                                                               inner_relids);
                               1700                 :                : 
                               1701                 :                :         /* Recover if we failed to generate required derived clauses */
                               1702         [ -  + ]:           4343 :         if (ec->ec_broken)
 6994 tgl@sss.pgh.pa.us        1703                 :UBC           0 :             sublist = generate_join_implied_equalities_broken(root,
                               1704                 :                :                                                               ec,
                               1705                 :                :                                                               nominal_join_relids,
                               1706                 :                :                                                               outer_relids,
                               1707                 :                :                                                               nominal_inner_relids,
                               1708                 :                :                                                               inner_rel);
                               1709                 :                : 
 6994 tgl@sss.pgh.pa.us        1710                 :CBC        4343 :         result = list_concat(result, sublist);
                               1711                 :                :     }
                               1712                 :                : 
                               1713                 :           3995 :     return result;
                               1714                 :                : }
                               1715                 :                : 
                               1716                 :                : /*
                               1717                 :                :  * generate_join_implied_equalities for a still-valid EC
                               1718                 :                :  */
                               1719                 :                : static List *
                               1720                 :         270472 : generate_join_implied_equalities_normal(PlannerInfo *root,
                               1721                 :                :                                         EquivalenceClass *ec,
                               1722                 :                :                                         Relids join_relids,
                               1723                 :                :                                         Relids outer_relids,
                               1724                 :                :                                         Relids inner_relids)
                               1725                 :                : {
                               1726                 :         270472 :     List       *result = NIL;
                               1727                 :         270472 :     List       *new_members = NIL;
                               1728                 :         270472 :     List       *outer_members = NIL;
                               1729                 :         270472 :     List       *inner_members = NIL;
                               1730                 :                :     EquivalenceMemberIterator it;
                               1731                 :                :     EquivalenceMember *cur_em;
                               1732                 :                : 
                               1733                 :                :     /*
                               1734                 :                :      * First, scan the EC to identify member values that are computable at the
                               1735                 :                :      * outer rel, at the inner rel, or at this relation but not in either
                               1736                 :                :      * input rel.  The outer-rel members should already be enforced equal,
                               1737                 :                :      * likewise for the inner-rel members.  We'll need to create clauses to
                               1738                 :                :      * enforce that any newly computable members are all equal to each other
                               1739                 :                :      * as well as to at least one input member, plus enforce at least one
                               1740                 :                :      * outer-rel member equal to at least one inner-rel member.
                               1741                 :                :      */
  341 drowley@postgresql.o     1742                 :         270472 :     setup_eclass_member_iterator(&it, ec, join_relids);
                               1743         [ +  + ]:         865377 :     while ((cur_em = eclass_member_iterator_next(&it)) != NULL)
                               1744                 :                :     {
                               1745                 :                :         /*
                               1746                 :                :          * We don't need to check explicitly for child EC members.  This test
                               1747                 :                :          * against join_relids will cause them to be ignored except when
                               1748                 :                :          * considering a child inner rel, which is what we want.
                               1749                 :                :          */
 5078 tgl@sss.pgh.pa.us        1750         [ +  + ]:         594905 :         if (!bms_is_subset(cur_em->em_relids, join_relids))
                               1751                 :          54623 :             continue;           /* not computable yet, or wrong child */
                               1752                 :                : 
                               1753         [ +  + ]:         540282 :         if (bms_is_subset(cur_em->em_relids, outer_relids))
 6994                          1754                 :         311141 :             outer_members = lappend(outer_members, cur_em);
 5078                          1755         [ +  + ]:         229141 :         else if (bms_is_subset(cur_em->em_relids, inner_relids))
 6994                          1756                 :         227977 :             inner_members = lappend(inner_members, cur_em);
                               1757                 :                :         else
                               1758                 :           1164 :             new_members = lappend(new_members, cur_em);
                               1759                 :                :     }
                               1760                 :                : 
                               1761                 :                :     /*
                               1762                 :                :      * First, select the joinclause if needed.  We can equate any one outer
                               1763                 :                :      * member to any one inner member, but we have to find a datatype
                               1764                 :                :      * combination for which an opfamily member operator exists.  If we have
                               1765                 :                :      * choices, we prefer simple Var members (possibly with RelabelType) since
                               1766                 :                :      * these are (a) cheapest to compute at runtime and (b) most likely to
                               1767                 :                :      * have useful statistics. Also, prefer operators that are also
                               1768                 :                :      * hashjoinable.
                               1769                 :                :      */
                               1770   [ +  +  +  + ]:         270472 :     if (outer_members && inner_members)
                               1771                 :                :     {
                               1772                 :         220985 :         EquivalenceMember *best_outer_em = NULL;
                               1773                 :         220985 :         EquivalenceMember *best_inner_em = NULL;
 6695 bruce@momjian.us         1774                 :         220985 :         Oid         best_eq_op = InvalidOid;
                               1775                 :         220985 :         int         best_score = -1;
                               1776                 :                :         RestrictInfo *rinfo;
                               1777                 :                :         ListCell   *lc1;
                               1778                 :                : 
 6994 tgl@sss.pgh.pa.us        1779   [ +  -  +  +  :         228889 :         foreach(lc1, outer_members)
                                              +  + ]
                               1780                 :                :         {
                               1781                 :         221024 :             EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
                               1782                 :                :             ListCell   *lc2;
                               1783                 :                : 
                               1784   [ +  -  +  +  :         228940 :             foreach(lc2, inner_members)
                                              +  + ]
                               1785                 :                :             {
                               1786                 :         221036 :                 EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
                               1787                 :                :                 Oid         eq_op;
                               1788                 :                :                 int         score;
                               1789                 :                : 
                               1790                 :         221036 :                 eq_op = select_equality_operator(ec,
                               1791                 :                :                                                  outer_em->em_datatype,
                               1792                 :                :                                                  inner_em->em_datatype);
                               1793         [ +  + ]:         221036 :                 if (!OidIsValid(eq_op))
                               1794                 :             18 :                     continue;
                               1795                 :         221018 :                 score = 0;
                               1796         [ +  + ]:         221018 :                 if (IsA(outer_em->em_expr, Var) ||
                               1797         [ +  + ]:           8921 :                     (IsA(outer_em->em_expr, RelabelType) &&
                               1798         [ +  + ]:           2167 :                      IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
                               1799                 :         214216 :                     score++;
                               1800         [ +  + ]:         221018 :                 if (IsA(inner_em->em_expr, Var) ||
                               1801         [ +  + ]:           5818 :                     (IsA(inner_em->em_expr, RelabelType) &&
                               1802         [ +  + ]:           4536 :                      IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
                               1803                 :         219718 :                     score++;
 5554                          1804         [ +  + ]:         221018 :                 if (op_hashjoinable(eq_op,
 5615                          1805                 :         221018 :                                     exprType((Node *) outer_em->em_expr)))
 6994                          1806                 :         220979 :                     score++;
                               1807         [ +  + ]:         221018 :                 if (score > best_score)
                               1808                 :                :                 {
                               1809                 :         220967 :                     best_outer_em = outer_em;
                               1810                 :         220967 :                     best_inner_em = inner_em;
                               1811                 :         220967 :                     best_eq_op = eq_op;
                               1812                 :         220967 :                     best_score = score;
                               1813         [ +  + ]:         220967 :                     if (best_score == 3)
 6695 bruce@momjian.us         1814                 :         213120 :                         break;  /* no need to look further */
                               1815                 :                :                 }
                               1816                 :                :             }
 6994 tgl@sss.pgh.pa.us        1817         [ +  + ]:         221024 :             if (best_score == 3)
 6695 bruce@momjian.us         1818                 :         213120 :                 break;          /* no need to look further */
                               1819                 :                :         }
 6994 tgl@sss.pgh.pa.us        1820         [ +  + ]:         220985 :         if (best_score < 0)
                               1821                 :                :         {
                               1822                 :                :             /* failed... */
                               1823                 :             18 :             ec->ec_broken = true;
                               1824                 :             18 :             return NIL;
                               1825                 :                :         }
                               1826                 :                : 
                               1827                 :                :         /*
                               1828                 :                :          * Create clause, setting parent_ec to mark it as redundant with other
                               1829                 :                :          * joinclauses
                               1830                 :                :          */
 6992                          1831                 :         220967 :         rinfo = create_join_clause(root, ec, best_eq_op,
                               1832                 :                :                                    best_outer_em, best_inner_em,
                               1833                 :                :                                    ec);
                               1834                 :                : 
 6994                          1835                 :         220967 :         result = lappend(result, rinfo);
                               1836                 :                :     }
                               1837                 :                : 
                               1838                 :                :     /*
                               1839                 :                :      * Now deal with building restrictions for any expressions that involve
                               1840                 :                :      * Vars from both sides of the join.  We have to equate all of these to
                               1841                 :                :      * each other as well as to at least one old member (if any).
                               1842                 :                :      *
                               1843                 :                :      * XXX as in generate_base_implied_equalities_no_const, we could be a lot
                               1844                 :                :      * smarter here to avoid unnecessary failures in cross-type situations.
                               1845                 :                :      * For now, use the same left-to-right method used there.
                               1846                 :                :      */
                               1847         [ +  + ]:         270454 :     if (new_members)
                               1848                 :                :     {
                               1849                 :           1146 :         List       *old_members = list_concat(outer_members, inner_members);
                               1850                 :           1146 :         EquivalenceMember *prev_em = NULL;
                               1851                 :                :         RestrictInfo *rinfo;
                               1852                 :                :         ListCell   *lc1;
                               1853                 :                : 
                               1854                 :                :         /* For now, arbitrarily take the first old_member as the one to use */
                               1855         [ +  + ]:           1146 :         if (old_members)
                               1856                 :            975 :             new_members = lappend(new_members, linitial(old_members));
                               1857                 :                : 
                               1858   [ +  -  +  +  :           3285 :         foreach(lc1, new_members)
                                              +  + ]
                               1859                 :                :         {
  341 drowley@postgresql.o     1860                 :           2139 :             cur_em = (EquivalenceMember *) lfirst(lc1);
                               1861                 :                : 
 6994 tgl@sss.pgh.pa.us        1862         [ +  + ]:           2139 :             if (prev_em != NULL)
                               1863                 :                :             {
                               1864                 :                :                 Oid         eq_op;
                               1865                 :                : 
                               1866                 :            993 :                 eq_op = select_equality_operator(ec,
                               1867                 :                :                                                  prev_em->em_datatype,
                               1868                 :                :                                                  cur_em->em_datatype);
                               1869         [ -  + ]:            993 :                 if (!OidIsValid(eq_op))
                               1870                 :                :                 {
                               1871                 :                :                     /* failed... */
 6994 tgl@sss.pgh.pa.us        1872                 :UBC           0 :                     ec->ec_broken = true;
                               1873                 :              0 :                     return NIL;
                               1874                 :                :                 }
                               1875                 :                :                 /* do NOT set parent_ec, this qual is not redundant! */
 6992 tgl@sss.pgh.pa.us        1876                 :CBC         993 :                 rinfo = create_join_clause(root, ec, eq_op,
                               1877                 :                :                                            prev_em, cur_em,
                               1878                 :                :                                            NULL);
                               1879                 :                : 
 6994                          1880                 :            993 :                 result = lappend(result, rinfo);
                               1881                 :                :             }
                               1882                 :           2139 :             prev_em = cur_em;
                               1883                 :                :         }
                               1884                 :                :     }
                               1885                 :                : 
                               1886                 :         270454 :     return result;
                               1887                 :                : }
                               1888                 :                : 
                               1889                 :                : /*
                               1890                 :                :  * generate_join_implied_equalities cleanup after failure
                               1891                 :                :  *
                               1892                 :                :  * Return any original RestrictInfos that are enforceable at this join.
                               1893                 :                :  *
                               1894                 :                :  * In the case of a child inner relation, we have to translate the
                               1895                 :                :  * original RestrictInfos from parent to child Vars.
                               1896                 :                :  */
                               1897                 :                : static List *
                               1898                 :            180 : generate_join_implied_equalities_broken(PlannerInfo *root,
                               1899                 :                :                                         EquivalenceClass *ec,
                               1900                 :                :                                         Relids nominal_join_relids,
                               1901                 :                :                                         Relids outer_relids,
                               1902                 :                :                                         Relids nominal_inner_relids,
                               1903                 :                :                                         RelOptInfo *inner_rel)
                               1904                 :                : {
                               1905                 :            180 :     List       *result = NIL;
                               1906                 :                :     ListCell   *lc;
                               1907                 :                : 
                               1908   [ +  -  +  +  :            492 :     foreach(lc, ec->ec_sources)
                                              +  + ]
                               1909                 :                :     {
                               1910                 :            312 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
 5078                          1911                 :            312 :         Relids      clause_relids = restrictinfo->required_relids;
                               1912                 :                : 
                               1913         [ +  + ]:            312 :         if (bms_is_subset(clause_relids, nominal_join_relids) &&
                               1914         [ +  + ]:            168 :             !bms_is_subset(clause_relids, outer_relids) &&
                               1915         [ +  - ]:            156 :             !bms_is_subset(clause_relids, nominal_inner_relids))
 6994                          1916                 :            156 :             result = lappend(result, restrictinfo);
                               1917                 :                :     }
                               1918                 :                : 
                               1919                 :                :     /*
                               1920                 :                :      * If we have to translate, just brute-force apply adjust_appendrel_attrs
                               1921                 :                :      * to all the RestrictInfos at once.  This will result in returning
                               1922                 :                :      * RestrictInfos that are not included in EC's derived clauses, but there
                               1923                 :                :      * shouldn't be any duplication, and it's a sufficiently narrow corner
                               1924                 :                :      * case that we shouldn't sweat too much over it anyway.
                               1925                 :                :      *
                               1926                 :                :      * Since inner_rel might be an indirect descendant of the baserel
                               1927                 :                :      * mentioned in the ec_sources clauses, we have to be prepared to apply
                               1928                 :                :      * multiple levels of Var translation.
                               1929                 :                :      */
 3268 rhaas@postgresql.org     1930   [ +  +  +  -  :            180 :     if (IS_OTHER_REL(inner_rel) && result != NIL)
                                        -  +  +  + ]
 4183 tgl@sss.pgh.pa.us        1931                 :             81 :         result = (List *) adjust_appendrel_attrs_multilevel(root,
                               1932                 :                :                                                             (Node *) result,
                               1933                 :                :                                                             inner_rel,
 1305                          1934                 :             81 :                                                             inner_rel->top_parent);
                               1935                 :                : 
 6994                          1936                 :            180 :     return result;
                               1937                 :                : }
                               1938                 :                : 
                               1939                 :                : 
                               1940                 :                : /*
                               1941                 :                :  * select_equality_operator
                               1942                 :                :  *    Select a suitable equality operator for comparing two EC members
                               1943                 :                :  *
                               1944                 :                :  * Returns InvalidOid if no operator can be found for this datatype combination
                               1945                 :                :  */
                               1946                 :                : static Oid
 6695 bruce@momjian.us         1947                 :         319809 : select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
                               1948                 :                : {
                               1949                 :                :     ListCell   *lc;
                               1950                 :                : 
 6994 tgl@sss.pgh.pa.us        1951   [ +  -  +  +  :         319842 :     foreach(lc, ec->ec_opfamilies)
                                              +  + ]
                               1952                 :                :     {
 6695 bruce@momjian.us         1953                 :         319809 :         Oid         opfamily = lfirst_oid(lc);
                               1954                 :                :         Oid         opno;
                               1955                 :                : 
  343 peter@eisentraut.org     1956                 :         319809 :         opno = get_opfamily_member_for_cmptype(opfamily, lefttype, righttype, COMPARE_EQ);
 3343 tgl@sss.pgh.pa.us        1957         [ +  + ]:         319809 :         if (!OidIsValid(opno))
                               1958                 :             33 :             continue;
                               1959                 :                :         /* If no barrier quals in query, don't worry about leaky operators */
                               1960         [ +  + ]:         319776 :         if (ec->ec_max_security == 0)
                               1961                 :         319776 :             return opno;
                               1962                 :                :         /* Otherwise, insist that selected operators be leakproof */
                               1963         [ +  - ]:            289 :         if (get_func_leakproof(get_opcode(opno)))
 6994                          1964                 :            289 :             return opno;
                               1965                 :                :     }
                               1966                 :             33 :     return InvalidOid;
                               1967                 :                : }
                               1968                 :                : 
                               1969                 :                : 
                               1970                 :                : /*
                               1971                 :                :  * create_join_clause
                               1972                 :                :  *    Find or make a RestrictInfo comparing the two given EC members
                               1973                 :                :  *    with the given operator (or, possibly, its commutator, because
                               1974                 :                :  *    the ordering of the operands in the result is not guaranteed).
                               1975                 :                :  *
                               1976                 :                :  * parent_ec is either equal to ec (if the clause is a potentially-redundant
                               1977                 :                :  * join clause) or NULL (if not).  We have to treat this as part of the
                               1978                 :                :  * match requirements --- it's possible that a clause comparing the same two
                               1979                 :                :  * EMs is a join clause in one join path and a restriction clause in another.
                               1980                 :                :  */
                               1981                 :                : static RestrictInfo *
 6992                          1982                 :         301777 : create_join_clause(PlannerInfo *root,
                               1983                 :                :                    EquivalenceClass *ec, Oid opno,
                               1984                 :                :                    EquivalenceMember *leftem,
                               1985                 :                :                    EquivalenceMember *rightem,
                               1986                 :                :                    EquivalenceClass *parent_ec)
                               1987                 :                : {
                               1988                 :                :     RestrictInfo *rinfo;
 1140                          1989                 :         301777 :     RestrictInfo *parent_rinfo = NULL;
                               1990                 :                :     MemoryContext oldcontext;
                               1991                 :                : 
  345 amitlan@postgresql.o     1992                 :         301777 :     rinfo = ec_search_clause_for_ems(root, ec, leftem, rightem, parent_ec);
                               1993         [ +  + ]:         301777 :     if (rinfo)
                               1994                 :         251468 :         return rinfo;
                               1995                 :                : 
                               1996                 :                :     /*
                               1997                 :                :      * Not there, so build it, in planner context so we can re-use it. (Not
                               1998                 :                :      * important in normal planning, but definitely so in GEQO.)
                               1999                 :                :      */
 6992 tgl@sss.pgh.pa.us        2000                 :          50309 :     oldcontext = MemoryContextSwitchTo(root->planner_cxt);
                               2001                 :                : 
                               2002                 :                :     /*
                               2003                 :                :      * If either EM is a child, recursively create the corresponding
                               2004                 :                :      * parent-to-parent clause, so that we can duplicate its rinfo_serial.
                               2005                 :                :      */
 1140                          2006   [ +  +  +  + ]:          50309 :     if (leftem->em_is_child || rightem->em_is_child)
                               2007                 :                :     {
                               2008         [ +  + ]:           2234 :         EquivalenceMember *leftp = leftem->em_parent ? leftem->em_parent : leftem;
                               2009         [ +  + ]:           2234 :         EquivalenceMember *rightp = rightem->em_parent ? rightem->em_parent : rightem;
                               2010                 :                : 
                               2011                 :           2234 :         parent_rinfo = create_join_clause(root, ec, opno,
                               2012                 :                :                                           leftp, rightp,
                               2013                 :                :                                           parent_ec);
                               2014                 :                :     }
                               2015                 :                : 
 1879                          2016                 :          50309 :     rinfo = build_implied_join_equality(root,
                               2017                 :                :                                         opno,
                               2018                 :                :                                         ec->ec_collation,
                               2019                 :                :                                         leftem->em_expr,
                               2020                 :                :                                         rightem->em_expr,
 6687                          2021                 :          50309 :                                         bms_union(leftem->em_relids,
 4896                          2022                 :          50309 :                                                   rightem->em_relids),
                               2023                 :                :                                         ec->ec_min_security);
                               2024                 :                : 
                               2025                 :                :     /*
                               2026                 :                :      * If either EM is a child, force the clause's clause_relids to include
                               2027                 :                :      * the relid(s) of the child rel.  In normal cases it would already, but
                               2028                 :                :      * not if we are considering appendrel child relations with pseudoconstant
                               2029                 :                :      * translated variables (i.e., UNION ALL sub-selects with constant output
                               2030                 :                :      * items).  We must do this so that join_clause_is_movable_into() will
                               2031                 :                :      * think that the clause should be evaluated at the correct place.
                               2032                 :                :      */
  698                          2033         [ +  + ]:          50309 :     if (leftem->em_is_child)
                               2034                 :           1958 :         rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
                               2035                 :           1958 :                                                leftem->em_relids);
                               2036         [ +  + ]:          50309 :     if (rightem->em_is_child)
                               2037                 :            276 :         rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
                               2038                 :            276 :                                                rightem->em_relids);
                               2039                 :                : 
                               2040                 :                :     /* If it's a child clause, copy the parent's rinfo_serial */
 1140                          2041         [ +  + ]:          50309 :     if (parent_rinfo)
                               2042                 :           2234 :         rinfo->rinfo_serial = parent_rinfo->rinfo_serial;
                               2043                 :                : 
                               2044                 :                :     /* Mark the clause as redundant, or not */
 6992                          2045                 :          50309 :     rinfo->parent_ec = parent_ec;
                               2046                 :                : 
                               2047                 :                :     /*
                               2048                 :                :      * We know the correct values for left_ec/right_ec, ie this particular EC,
                               2049                 :                :      * so we can just set them directly instead of forcing another lookup.
                               2050                 :                :      */
                               2051                 :          50309 :     rinfo->left_ec = ec;
                               2052                 :          50309 :     rinfo->right_ec = ec;
                               2053                 :                : 
                               2054                 :                :     /* Mark it as usable with these EMs */
                               2055                 :          50309 :     rinfo->left_em = leftem;
                               2056                 :          50309 :     rinfo->right_em = rightem;
                               2057                 :                :     /* and save it for possible re-use */
  345 amitlan@postgresql.o     2058                 :          50309 :     ec_add_derived_clause(ec, rinfo);
                               2059                 :                : 
 6992 tgl@sss.pgh.pa.us        2060                 :          50309 :     MemoryContextSwitchTo(oldcontext);
                               2061                 :                : 
                               2062                 :          50309 :     return rinfo;
                               2063                 :                : }
                               2064                 :                : 
                               2065                 :                : 
                               2066                 :                : /*
                               2067                 :                :  * reconsider_outer_join_clauses
                               2068                 :                :  *    Re-examine any outer-join clauses that were set aside by
                               2069                 :                :  *    distribute_qual_to_rels(), and see if we can derive any
                               2070                 :                :  *    EquivalenceClasses from them.  Then, if they were not made
                               2071                 :                :  *    redundant, push them out into the regular join-clause lists.
                               2072                 :                :  *
                               2073                 :                :  * When we have mergejoinable clauses A = B that are outer-join clauses,
                               2074                 :                :  * we can't blindly combine them with other clauses A = C to deduce B = C,
                               2075                 :                :  * since in fact the "equality" A = B won't necessarily hold above the
                               2076                 :                :  * outer join (one of the variables might be NULL instead).  Nonetheless
                               2077                 :                :  * there are cases where we can add qual clauses using transitivity.
                               2078                 :                :  *
                               2079                 :                :  * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
                               2080                 :                :  * for which there is also an equivalence clause OUTERVAR = CONSTANT.
                               2081                 :                :  * It is safe and useful to push a clause INNERVAR = CONSTANT into the
                               2082                 :                :  * evaluation of the inner (nullable) relation, because any inner rows not
                               2083                 :                :  * meeting this condition will not contribute to the outer-join result anyway.
                               2084                 :                :  * (Any outer rows they could join to will be eliminated by the pushed-down
                               2085                 :                :  * equivalence clause.)
                               2086                 :                :  *
                               2087                 :                :  * Note that the above rule does not work for full outer joins; nor is it
                               2088                 :                :  * very interesting to consider cases where the generated equivalence clause
                               2089                 :                :  * would involve relations outside the outer join, since such clauses couldn't
                               2090                 :                :  * be pushed into the inner side's scan anyway.  So the restriction to
                               2091                 :                :  * outervar = pseudoconstant is not really giving up anything.
                               2092                 :                :  *
                               2093                 :                :  * For full-join cases, we can only do something useful if it's a FULL JOIN
                               2094                 :                :  * USING and a merged column has an equivalence MERGEDVAR = CONSTANT.
                               2095                 :                :  * By the time it gets here, the merged column will look like
                               2096                 :                :  *      COALESCE(LEFTVAR, RIGHTVAR)
                               2097                 :                :  * and we will have a full-join clause LEFTVAR = RIGHTVAR that we can match
                               2098                 :                :  * the COALESCE expression to. In this situation we can push LEFTVAR = CONSTANT
                               2099                 :                :  * and RIGHTVAR = CONSTANT into the input relations, since any rows not
                               2100                 :                :  * meeting these conditions cannot contribute to the join result.
                               2101                 :                :  *
                               2102                 :                :  * Again, there isn't any traction to be gained by trying to deal with
                               2103                 :                :  * clauses comparing a mergedvar to a non-pseudoconstant.  So we can make
                               2104                 :                :  * use of the EquivalenceClasses to search for matching variables that were
                               2105                 :                :  * equivalenced to constants.  The interesting outer-join clauses were
                               2106                 :                :  * accumulated for us by distribute_qual_to_rels.
                               2107                 :                :  *
                               2108                 :                :  * When we find one of these cases, we implement the changes we want by
                               2109                 :                :  * generating a new equivalence clause INNERVAR = CONSTANT (or LEFTVAR, etc)
                               2110                 :                :  * and pushing it into the EquivalenceClass structures.  This is because we
                               2111                 :                :  * may already know that INNERVAR is equivalenced to some other var(s), and
                               2112                 :                :  * we'd like the constant to propagate to them too.  Note that it would be
                               2113                 :                :  * unsafe to merge any existing EC for INNERVAR with the OUTERVAR's EC ---
                               2114                 :                :  * that could result in propagating constant restrictions from
                               2115                 :                :  * INNERVAR to OUTERVAR, which would be very wrong.
                               2116                 :                :  *
                               2117                 :                :  * It's possible that the INNERVAR is also an OUTERVAR for some other
                               2118                 :                :  * outer-join clause, in which case the process can be repeated.  So we repeat
                               2119                 :                :  * looping over the lists of clauses until no further deductions can be made.
                               2120                 :                :  * Whenever we do make a deduction, we remove the generating clause from the
                               2121                 :                :  * lists, since we don't want to make the same deduction twice.
                               2122                 :                :  *
                               2123                 :                :  * If we don't find any match for a set-aside outer join clause, we must
                               2124                 :                :  * throw it back into the regular joinclause processing by passing it to
                               2125                 :                :  * distribute_restrictinfo_to_rels().  If we do generate a derived clause,
                               2126                 :                :  * however, the outer-join clause is redundant.  We must still put some
                               2127                 :                :  * clause into the regular processing, because otherwise the join will be
                               2128                 :                :  * seen as a clauseless join and avoided during join order searching.
                               2129                 :                :  * We handle this by generating a constant-TRUE clause that is marked with
                               2130                 :                :  * the same required_relids etc as the removed outer-join clause, thus
                               2131                 :                :  * making it a join clause between the correct relations.
                               2132                 :                :  */
                               2133                 :                : void
 6994                          2134                 :         184686 : reconsider_outer_join_clauses(PlannerInfo *root)
                               2135                 :                : {
                               2136                 :                :     bool        found;
                               2137                 :                :     ListCell   *cell;
                               2138                 :                : 
                               2139                 :                :     /* Outer loop repeats until we find no more deductions */
                               2140                 :                :     do
                               2141                 :                :     {
 6640                          2142                 :         185719 :         found = false;
                               2143                 :                : 
                               2144                 :                :         /* Process the LEFT JOIN clauses */
 2435                          2145   [ +  +  +  +  :         201090 :         foreach(cell, root->left_join_clauses)
                                              +  + ]
                               2146                 :                :         {
 1140                          2147                 :          15371 :             OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
                               2148                 :                : 
                               2149         [ +  + ]:          15371 :             if (reconsider_outer_join_clause(root, ojcinfo, true))
                               2150                 :                :             {
                               2151                 :            496 :                 RestrictInfo *rinfo = ojcinfo->rinfo;
                               2152                 :                : 
 6640                          2153                 :            496 :                 found = true;
                               2154                 :                :                 /* remove it from the list */
                               2155                 :            496 :                 root->left_join_clauses =
 2435                          2156                 :            496 :                     foreach_delete_current(root->left_join_clauses, cell);
                               2157                 :                :                 /* throw back a dummy replacement clause (see notes above) */
 1140                          2158                 :            496 :                 rinfo = make_restrictinfo(root,
                               2159                 :            496 :                                           (Expr *) makeBoolConst(true, false),
 1025                          2160                 :            496 :                                           rinfo->is_pushed_down,
                               2161                 :            496 :                                           rinfo->has_clone,
                               2162                 :            496 :                                           rinfo->is_clone,
                               2163                 :                :                                           false,    /* pseudoconstant */
                               2164                 :                :                                           0,    /* security_level */
                               2165                 :                :                                           rinfo->required_relids,
                               2166                 :                :                                           rinfo->incompatible_relids,
                               2167                 :                :                                           rinfo->outer_relids);
 6640                          2168                 :            496 :                 distribute_restrictinfo_to_rels(root, rinfo);
                               2169                 :                :             }
                               2170                 :                :         }
                               2171                 :                : 
                               2172                 :                :         /* Process the RIGHT JOIN clauses */
 2435                          2173   [ +  +  +  +  :         203655 :         foreach(cell, root->right_join_clauses)
                                              +  + ]
                               2174                 :                :         {
 1140                          2175                 :          17936 :             OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
                               2176                 :                : 
                               2177         [ +  + ]:          17936 :             if (reconsider_outer_join_clause(root, ojcinfo, false))
                               2178                 :                :             {
                               2179                 :            540 :                 RestrictInfo *rinfo = ojcinfo->rinfo;
                               2180                 :                : 
 6640                          2181                 :            540 :                 found = true;
                               2182                 :                :                 /* remove it from the list */
                               2183                 :            540 :                 root->right_join_clauses =
 2435                          2184                 :            540 :                     foreach_delete_current(root->right_join_clauses, cell);
                               2185                 :                :                 /* throw back a dummy replacement clause (see notes above) */
 1140                          2186                 :            540 :                 rinfo = make_restrictinfo(root,
                               2187                 :            540 :                                           (Expr *) makeBoolConst(true, false),
 1025                          2188                 :            540 :                                           rinfo->is_pushed_down,
                               2189                 :            540 :                                           rinfo->has_clone,
                               2190                 :            540 :                                           rinfo->is_clone,
                               2191                 :                :                                           false,    /* pseudoconstant */
                               2192                 :                :                                           0,    /* security_level */
                               2193                 :                :                                           rinfo->required_relids,
                               2194                 :                :                                           rinfo->incompatible_relids,
                               2195                 :                :                                           rinfo->outer_relids);
 6640                          2196                 :            540 :                 distribute_restrictinfo_to_rels(root, rinfo);
                               2197                 :                :             }
                               2198                 :                :         }
                               2199                 :                : 
                               2200                 :                :         /* Process the FULL JOIN clauses */
 2435                          2201   [ +  +  +  +  :         186373 :         foreach(cell, root->full_join_clauses)
                                              +  + ]
                               2202                 :                :         {
 1140                          2203                 :            654 :             OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
                               2204                 :                : 
                               2205         [ +  + ]:            654 :             if (reconsider_full_join_clause(root, ojcinfo))
                               2206                 :                :             {
                               2207                 :              3 :                 RestrictInfo *rinfo = ojcinfo->rinfo;
                               2208                 :                : 
 6640                          2209                 :              3 :                 found = true;
                               2210                 :                :                 /* remove it from the list */
                               2211                 :              3 :                 root->full_join_clauses =
 2435                          2212                 :              3 :                     foreach_delete_current(root->full_join_clauses, cell);
                               2213                 :                :                 /* throw back a dummy replacement clause (see notes above) */
 1140                          2214                 :              3 :                 rinfo = make_restrictinfo(root,
                               2215                 :              3 :                                           (Expr *) makeBoolConst(true, false),
 1025                          2216                 :              3 :                                           rinfo->is_pushed_down,
                               2217                 :              3 :                                           rinfo->has_clone,
                               2218                 :              3 :                                           rinfo->is_clone,
                               2219                 :                :                                           false,    /* pseudoconstant */
                               2220                 :                :                                           0,    /* security_level */
                               2221                 :                :                                           rinfo->required_relids,
                               2222                 :                :                                           rinfo->incompatible_relids,
                               2223                 :                :                                           rinfo->outer_relids);
 6640                          2224                 :              3 :                 distribute_restrictinfo_to_rels(root, rinfo);
                               2225                 :                :             }
                               2226                 :                :         }
                               2227         [ +  + ]:         185719 :     } while (found);
                               2228                 :                : 
                               2229                 :                :     /* Now, any remaining clauses have to be thrown back */
                               2230   [ +  +  +  +  :         199191 :     foreach(cell, root->left_join_clauses)
                                              +  + ]
                               2231                 :                :     {
 1140                          2232                 :          14505 :         OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
                               2233                 :                : 
                               2234                 :          14505 :         distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
                               2235                 :                :     }
 6640                          2236   [ +  +  +  +  :         201518 :     foreach(cell, root->right_join_clauses)
                                              +  + ]
                               2237                 :                :     {
 1140                          2238                 :          16832 :         OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
                               2239                 :                : 
                               2240                 :          16832 :         distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
                               2241                 :                :     }
 6640                          2242   [ +  +  +  +  :         185337 :     foreach(cell, root->full_join_clauses)
                                              +  + ]
                               2243                 :                :     {
 1140                          2244                 :            651 :         OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
                               2245                 :                : 
                               2246                 :            651 :         distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
                               2247                 :                :     }
 6994                          2248                 :         184686 : }
                               2249                 :                : 
                               2250                 :                : /*
                               2251                 :                :  * reconsider_outer_join_clauses for a single LEFT/RIGHT JOIN clause
                               2252                 :                :  *
                               2253                 :                :  * Returns true if we were able to propagate a constant through the clause.
                               2254                 :                :  */
                               2255                 :                : static bool
 1140                          2256                 :          33307 : reconsider_outer_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo,
                               2257                 :                :                              bool outer_on_left)
                               2258                 :                : {
                               2259                 :          33307 :     RestrictInfo *rinfo = ojcinfo->rinfo;
                               2260                 :          33307 :     SpecialJoinInfo *sjinfo = ojcinfo->sjinfo;
                               2261                 :                :     Expr       *outervar,
                               2262                 :                :                *innervar;
                               2263                 :                :     Oid         opno,
                               2264                 :                :                 collation,
                               2265                 :                :                 left_type,
                               2266                 :                :                 right_type,
                               2267                 :                :                 inner_datatype;
                               2268                 :                :     Relids      inner_relids;
                               2269                 :                :     ListCell   *lc1;
                               2270                 :                : 
 6994                          2271         [ -  + ]:          33307 :     Assert(is_opclause(rinfo->clause));
 6640                          2272                 :          33307 :     opno = ((OpExpr *) rinfo->clause)->opno;
 5475                          2273                 :          33307 :     collation = ((OpExpr *) rinfo->clause)->inputcollid;
                               2274                 :                : 
                               2275                 :                :     /* Extract needed info from the clause */
 6640                          2276                 :          33307 :     op_input_types(opno, &left_type, &right_type);
 6994                          2277         [ +  + ]:          33307 :     if (outer_on_left)
                               2278                 :                :     {
                               2279                 :          15371 :         outervar = (Expr *) get_leftop(rinfo->clause);
                               2280                 :          15371 :         innervar = (Expr *) get_rightop(rinfo->clause);
                               2281                 :          15371 :         inner_datatype = right_type;
 6687                          2282                 :          15371 :         inner_relids = rinfo->right_relids;
                               2283                 :                :     }
                               2284                 :                :     else
                               2285                 :                :     {
 6994                          2286                 :          17936 :         outervar = (Expr *) get_rightop(rinfo->clause);
                               2287                 :          17936 :         innervar = (Expr *) get_leftop(rinfo->clause);
                               2288                 :          17936 :         inner_datatype = left_type;
 6687                          2289                 :          17936 :         inner_relids = rinfo->left_relids;
                               2290                 :                :     }
                               2291                 :                : 
                               2292                 :                :     /* Scan EquivalenceClasses for a match to outervar */
 6994                          2293   [ +  -  +  +  :         220104 :     foreach(lc1, root->eq_classes)
                                              +  + ]
                               2294                 :                :     {
                               2295                 :         187833 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
                               2296                 :                :         bool        match;
                               2297                 :                :         ListCell   *lc2;
                               2298                 :                : 
                               2299                 :                :         /* We don't expect any children yet */
  341 drowley@postgresql.o     2300         [ -  + ]:         187833 :         Assert(cur_ec->ec_childmembers == NULL);
                               2301                 :                : 
                               2302                 :                :         /* Ignore EC unless it contains pseudoconstants */
 6994 tgl@sss.pgh.pa.us        2303         [ +  + ]:         187833 :         if (!cur_ec->ec_has_const)
                               2304                 :         144583 :             continue;
                               2305                 :                :         /* Never match to a volatile EC */
                               2306         [ -  + ]:          43250 :         if (cur_ec->ec_has_volatile)
 6994 tgl@sss.pgh.pa.us        2307                 :UBC           0 :             continue;
                               2308                 :                :         /* It has to match the outer-join clause as to semantics, too */
 5475 tgl@sss.pgh.pa.us        2309         [ +  + ]:CBC       43250 :         if (collation != cur_ec->ec_collation)
                               2310                 :           2675 :             continue;
 6994                          2311         [ +  + ]:          40575 :         if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
                               2312                 :          10452 :             continue;
                               2313                 :                :         /* Does it contain a match to outervar? */
                               2314                 :          30123 :         match = false;
                               2315   [ +  -  +  +  :          93289 :         foreach(lc2, cur_ec->ec_members)
                                              +  + ]
                               2316                 :                :         {
                               2317                 :          64202 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
                               2318                 :                : 
                               2319                 :                :             /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     2320         [ -  + ]:          64202 :             Assert(!cur_em->em_is_child);
 6994 tgl@sss.pgh.pa.us        2321         [ +  + ]:          64202 :             if (equal(outervar, cur_em->em_expr))
                               2322                 :                :             {
                               2323                 :           1036 :                 match = true;
                               2324                 :           1036 :                 break;
                               2325                 :                :             }
                               2326                 :                :         }
                               2327         [ +  + ]:          30123 :         if (!match)
                               2328                 :          29087 :             continue;           /* no match, so ignore this EC */
                               2329                 :                : 
                               2330                 :                :         /*
                               2331                 :                :          * Yes it does!  Try to generate a clause INNERVAR = CONSTANT for each
                               2332                 :                :          * CONSTANT in the EC.  Note that we must succeed with at least one
                               2333                 :                :          * constant before we can decide to throw away the outer-join clause.
                               2334                 :                :          */
                               2335                 :           1036 :         match = false;
                               2336   [ +  -  +  +  :           3653 :         foreach(lc2, cur_ec->ec_members)
                                              +  + ]
                               2337                 :                :         {
                               2338                 :           2617 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
                               2339                 :                :             Oid         eq_op;
                               2340                 :                :             RestrictInfo *newrinfo;
                               2341                 :                :             JoinDomain *jdomain;
                               2342                 :                : 
                               2343         [ +  + ]:           2617 :             if (!cur_em->em_is_const)
                               2344                 :           1560 :                 continue;       /* ignore non-const members */
                               2345                 :           1057 :             eq_op = select_equality_operator(cur_ec,
                               2346                 :                :                                              inner_datatype,
                               2347                 :                :                                              cur_em->em_datatype);
                               2348         [ -  + ]:           1057 :             if (!OidIsValid(eq_op))
 6994 tgl@sss.pgh.pa.us        2349                 :UBC           0 :                 continue;       /* can't generate equality */
 1879 tgl@sss.pgh.pa.us        2350                 :CBC        1057 :             newrinfo = build_implied_join_equality(root,
                               2351                 :                :                                                    eq_op,
                               2352                 :                :                                                    cur_ec->ec_collation,
                               2353                 :                :                                                    innervar,
                               2354                 :                :                                                    cur_em->em_expr,
                               2355                 :                :                                                    bms_copy(inner_relids),
                               2356                 :                :                                                    cur_ec->ec_min_security);
                               2357                 :                :             /* This equality holds within the OJ's child JoinDomain */
 1140                          2358                 :           1057 :             jdomain = find_join_domain(root, sjinfo->syn_righthand);
                               2359         [ +  - ]:           1057 :             if (process_equivalence(root, &newrinfo, jdomain))
 6994                          2360                 :           1057 :                 match = true;
                               2361                 :                :         }
                               2362                 :                : 
                               2363                 :                :         /*
                               2364                 :                :          * If we were able to equate INNERVAR to any constant, report success.
                               2365                 :                :          * Otherwise, fall out of the search loop, since we know the OUTERVAR
                               2366                 :                :          * appears in at most one EC.
                               2367                 :                :          */
                               2368         [ +  - ]:           1036 :         if (match)
 6640                          2369                 :           1036 :             return true;
                               2370                 :                :         else
 6994 tgl@sss.pgh.pa.us        2371                 :UBC           0 :             break;
                               2372                 :                :     }
                               2373                 :                : 
 6640 tgl@sss.pgh.pa.us        2374                 :CBC       32271 :     return false;               /* failed to make any deduction */
                               2375                 :                : }
                               2376                 :                : 
                               2377                 :                : /*
                               2378                 :                :  * reconsider_outer_join_clauses for a single FULL JOIN clause
                               2379                 :                :  *
                               2380                 :                :  * Returns true if we were able to propagate a constant through the clause.
                               2381                 :                :  */
                               2382                 :                : static bool
 1140                          2383                 :            654 : reconsider_full_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo)
                               2384                 :                : {
                               2385                 :            654 :     RestrictInfo *rinfo = ojcinfo->rinfo;
                               2386                 :            654 :     SpecialJoinInfo *sjinfo = ojcinfo->sjinfo;
                               2387                 :            654 :     Relids      fjrelids = bms_make_singleton(sjinfo->ojrelid);
                               2388                 :                :     Expr       *leftvar;
                               2389                 :                :     Expr       *rightvar;
                               2390                 :                :     Oid         opno,
                               2391                 :                :                 collation,
                               2392                 :                :                 left_type,
                               2393                 :                :                 right_type;
                               2394                 :                :     Relids      left_relids,
                               2395                 :                :                 right_relids;
                               2396                 :                :     ListCell   *lc1;
                               2397                 :                : 
                               2398                 :                :     /* Extract needed info from the clause */
 6994                          2399         [ -  + ]:            654 :     Assert(is_opclause(rinfo->clause));
 6640                          2400                 :            654 :     opno = ((OpExpr *) rinfo->clause)->opno;
 5475                          2401                 :            654 :     collation = ((OpExpr *) rinfo->clause)->inputcollid;
 6640                          2402                 :            654 :     op_input_types(opno, &left_type, &right_type);
 6994                          2403                 :            654 :     leftvar = (Expr *) get_leftop(rinfo->clause);
                               2404                 :            654 :     rightvar = (Expr *) get_rightop(rinfo->clause);
 6687                          2405                 :            654 :     left_relids = rinfo->left_relids;
                               2406                 :            654 :     right_relids = rinfo->right_relids;
                               2407                 :                : 
 6994                          2408   [ +  -  +  +  :           3318 :     foreach(lc1, root->eq_classes)
                                              +  + ]
                               2409                 :                :     {
                               2410                 :           2667 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
                               2411                 :           2667 :         EquivalenceMember *coal_em = NULL;
                               2412                 :                :         bool        match;
                               2413                 :                :         bool        matchleft;
                               2414                 :                :         bool        matchright;
                               2415                 :                :         ListCell   *lc2;
 1970 drowley@postgresql.o     2416                 :           2667 :         int         coal_idx = -1;
                               2417                 :                : 
                               2418                 :                :         /* We don't expect any children yet */
  341                          2419         [ -  + ]:           2667 :         Assert(cur_ec->ec_childmembers == NULL);
                               2420                 :                : 
                               2421                 :                :         /* Ignore EC unless it contains pseudoconstants */
 6994 tgl@sss.pgh.pa.us        2422         [ +  + ]:           2667 :         if (!cur_ec->ec_has_const)
                               2423                 :           2519 :             continue;
                               2424                 :                :         /* Never match to a volatile EC */
                               2425         [ -  + ]:            148 :         if (cur_ec->ec_has_volatile)
 6994 tgl@sss.pgh.pa.us        2426                 :UBC           0 :             continue;
                               2427                 :                :         /* It has to match the outer-join clause as to semantics, too */
 5475 tgl@sss.pgh.pa.us        2428         [ +  + ]:CBC         148 :         if (collation != cur_ec->ec_collation)
                               2429                 :             18 :             continue;
 6994                          2430         [ -  + ]:            130 :         if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
 6994 tgl@sss.pgh.pa.us        2431                 :UBC           0 :             continue;
                               2432                 :                : 
                               2433                 :                :         /*
                               2434                 :                :          * Does it contain a COALESCE(leftvar, rightvar) construct?
                               2435                 :                :          *
                               2436                 :                :          * We can assume the COALESCE() inputs are in the same order as the
                               2437                 :                :          * join clause, since both were automatically generated in the cases
                               2438                 :                :          * we care about.
                               2439                 :                :          *
                               2440                 :                :          * XXX currently this may fail to match in cross-type cases because
                               2441                 :                :          * the COALESCE will contain typecast operations while the join clause
                               2442                 :                :          * may not (if there is a cross-type mergejoin operator available for
                               2443                 :                :          * the two column types). Is it OK to strip implicit coercions from
                               2444                 :                :          * the COALESCE arguments?
                               2445                 :                :          */
 6994 tgl@sss.pgh.pa.us        2446                 :CBC         130 :         match = false;
                               2447   [ +  -  +  +  :            381 :         foreach(lc2, cur_ec->ec_members)
                                              +  + ]
                               2448                 :                :         {
                               2449                 :            254 :             coal_em = (EquivalenceMember *) lfirst(lc2);
                               2450                 :                : 
                               2451                 :                :             /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     2452         [ -  + ]:            254 :             Assert(!coal_em->em_is_child);
 6994 tgl@sss.pgh.pa.us        2453         [ +  + ]:            254 :             if (IsA(coal_em->em_expr, CoalesceExpr))
                               2454                 :                :             {
                               2455                 :              9 :                 CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
                               2456                 :                :                 Node       *cfirst;
                               2457                 :                :                 Node       *csecond;
                               2458                 :                : 
                               2459         [ -  + ]:              9 :                 if (list_length(cexpr->args) != 2)
 6994 tgl@sss.pgh.pa.us        2460                 :LBC         (6) :                     continue;
 6994 tgl@sss.pgh.pa.us        2461                 :CBC           9 :                 cfirst = (Node *) linitial(cexpr->args);
                               2462                 :              9 :                 csecond = (Node *) lsecond(cexpr->args);
                               2463                 :                : 
                               2464                 :                :                 /*
                               2465                 :                :                  * The COALESCE arguments will be marked as possibly nulled by
                               2466                 :                :                  * the full join, while we wish to generate clauses that apply
                               2467                 :                :                  * to the join's inputs.  So we must strip the join from the
                               2468                 :                :                  * nullingrels fields of cfirst/csecond before comparing them
                               2469                 :                :                  * to leftvar/rightvar.  (Perhaps with a less hokey
                               2470                 :                :                  * representation for FULL JOIN USING output columns, this
                               2471                 :                :                  * wouldn't be needed?)
                               2472                 :                :                  */
 1140                          2473                 :              9 :                 cfirst = remove_nulling_relids(cfirst, fjrelids, NULL);
                               2474                 :              9 :                 csecond = remove_nulling_relids(csecond, fjrelids, NULL);
                               2475                 :                : 
 6994                          2476   [ +  +  +  - ]:              9 :                 if (equal(leftvar, cfirst) && equal(rightvar, csecond))
                               2477                 :                :                 {
 1970 drowley@postgresql.o     2478                 :              3 :                     coal_idx = foreach_current_index(lc2);
 6994 tgl@sss.pgh.pa.us        2479                 :              3 :                     match = true;
                               2480                 :              3 :                     break;
                               2481                 :                :                 }
                               2482                 :                :             }
                               2483                 :                :         }
                               2484         [ +  + ]:            130 :         if (!match)
                               2485                 :            127 :             continue;           /* no match, so ignore this EC */
                               2486                 :                : 
                               2487                 :                :         /*
                               2488                 :                :          * Yes it does!  Try to generate clauses LEFTVAR = CONSTANT and
                               2489                 :                :          * RIGHTVAR = CONSTANT for each CONSTANT in the EC.  Note that we must
                               2490                 :                :          * succeed with at least one constant for each var before we can
                               2491                 :                :          * decide to throw away the outer-join clause.
                               2492                 :                :          */
                               2493                 :              3 :         matchleft = matchright = false;
                               2494   [ +  -  +  +  :              9 :         foreach(lc2, cur_ec->ec_members)
                                              +  + ]
                               2495                 :                :         {
                               2496                 :              6 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
                               2497                 :                :             Oid         eq_op;
                               2498                 :                :             RestrictInfo *newrinfo;
                               2499                 :                :             JoinDomain *jdomain;
                               2500                 :                : 
                               2501         [ +  + ]:              6 :             if (!cur_em->em_is_const)
                               2502                 :              3 :                 continue;       /* ignore non-const members */
                               2503                 :              3 :             eq_op = select_equality_operator(cur_ec,
                               2504                 :                :                                              left_type,
                               2505                 :                :                                              cur_em->em_datatype);
                               2506         [ +  - ]:              3 :             if (OidIsValid(eq_op))
                               2507                 :                :             {
 1879                          2508                 :              3 :                 newrinfo = build_implied_join_equality(root,
                               2509                 :                :                                                        eq_op,
                               2510                 :                :                                                        cur_ec->ec_collation,
                               2511                 :                :                                                        leftvar,
                               2512                 :                :                                                        cur_em->em_expr,
                               2513                 :                :                                                        bms_copy(left_relids),
                               2514                 :                :                                                        cur_ec->ec_min_security);
                               2515                 :                :                 /* This equality holds within the lefthand child JoinDomain */
 1140                          2516                 :              3 :                 jdomain = find_join_domain(root, sjinfo->syn_lefthand);
                               2517         [ +  - ]:              3 :                 if (process_equivalence(root, &newrinfo, jdomain))
 6994                          2518                 :              3 :                     matchleft = true;
                               2519                 :                :             }
                               2520                 :              3 :             eq_op = select_equality_operator(cur_ec,
                               2521                 :                :                                              right_type,
                               2522                 :                :                                              cur_em->em_datatype);
                               2523         [ +  - ]:              3 :             if (OidIsValid(eq_op))
                               2524                 :                :             {
 1879                          2525                 :              3 :                 newrinfo = build_implied_join_equality(root,
                               2526                 :                :                                                        eq_op,
                               2527                 :                :                                                        cur_ec->ec_collation,
                               2528                 :                :                                                        rightvar,
                               2529                 :                :                                                        cur_em->em_expr,
                               2530                 :                :                                                        bms_copy(right_relids),
                               2531                 :                :                                                        cur_ec->ec_min_security);
                               2532                 :                :                 /* This equality holds within the righthand child JoinDomain */
 1140                          2533                 :              3 :                 jdomain = find_join_domain(root, sjinfo->syn_righthand);
                               2534         [ +  - ]:              3 :                 if (process_equivalence(root, &newrinfo, jdomain))
 6994                          2535                 :              3 :                     matchright = true;
                               2536                 :                :             }
                               2537                 :                :         }
                               2538                 :                : 
                               2539                 :                :         /*
                               2540                 :                :          * If we were able to equate both vars to constants, we're done, and
                               2541                 :                :          * we can throw away the full-join clause as redundant.  Moreover, we
                               2542                 :                :          * can remove the COALESCE entry from the EC, since the added
                               2543                 :                :          * restrictions ensure it will always have the expected value. (We
                               2544                 :                :          * don't bother trying to update ec_relids or ec_sources.)
                               2545                 :                :          */
                               2546   [ +  -  +  - ]:              3 :         if (matchleft && matchright)
                               2547                 :                :         {
 1970 drowley@postgresql.o     2548                 :              3 :             cur_ec->ec_members = list_delete_nth_cell(cur_ec->ec_members, coal_idx);
 6640 tgl@sss.pgh.pa.us        2549                 :              3 :             return true;
                               2550                 :                :         }
                               2551                 :                : 
                               2552                 :                :         /*
                               2553                 :                :          * Otherwise, fall out of the search loop, since we know the COALESCE
                               2554                 :                :          * appears in at most one EC (XXX might stop being true if we allow
                               2555                 :                :          * stripping of coercions above?)
                               2556                 :                :          */
 6994 tgl@sss.pgh.pa.us        2557                 :UBC           0 :         break;
                               2558                 :                :     }
                               2559                 :                : 
 6640 tgl@sss.pgh.pa.us        2560                 :CBC         651 :     return false;               /* failed to make any deduction */
                               2561                 :                : }
                               2562                 :                : 
                               2563                 :                : /*
                               2564                 :                :  * rebuild_eclass_attr_needed
                               2565                 :                :  *    Put back attr_needed bits for Vars/PHVs needed for join eclasses.
                               2566                 :                :  *
                               2567                 :                :  * This is used to rebuild attr_needed/ph_needed sets after removal of a
                               2568                 :                :  * useless outer join.  It should match what
                               2569                 :                :  * generate_base_implied_equalities_no_const did, except that we call
                               2570                 :                :  * add_vars_to_attr_needed not add_vars_to_targetlist.
                               2571                 :                :  */
                               2572                 :                : void
  534                          2573                 :           6681 : rebuild_eclass_attr_needed(PlannerInfo *root)
                               2574                 :                : {
                               2575                 :                :     ListCell   *lc;
                               2576                 :                : 
                               2577   [ +  -  +  +  :          36503 :     foreach(lc, root->eq_classes)
                                              +  + ]
                               2578                 :                :     {
                               2579                 :          29822 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
                               2580                 :                : 
                               2581                 :                :         /*
                               2582                 :                :          * We don't expect any EC child members to exist at this point. Ensure
                               2583                 :                :          * that's the case, otherwise, we might be getting asked to do
                               2584                 :                :          * something this function hasn't been coded for.
                               2585                 :                :          */
  341 drowley@postgresql.o     2586         [ -  + ]:          29822 :         Assert(ec->ec_childmembers == NULL);
                               2587                 :                : 
                               2588                 :                :         /* Need do anything only for a multi-member, no-const EC. */
  534 tgl@sss.pgh.pa.us        2589   [ +  +  +  + ]:          29822 :         if (list_length(ec->ec_members) > 1 && !ec->ec_has_const)
                               2590                 :                :         {
                               2591                 :                :             ListCell   *lc2;
                               2592                 :                : 
                               2593   [ +  -  +  +  :           9948 :             foreach(lc2, ec->ec_members)
                                              +  + ]
                               2594                 :                :             {
                               2595                 :           6641 :                 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
                               2596                 :           6641 :                 List       *vars = pull_var_clause((Node *) cur_em->em_expr,
                               2597                 :                :                                                    PVC_RECURSE_AGGREGATES |
                               2598                 :                :                                                    PVC_RECURSE_WINDOWFUNCS |
                               2599                 :                :                                                    PVC_INCLUDE_PLACEHOLDERS);
                               2600                 :                : 
                               2601                 :           6641 :                 add_vars_to_attr_needed(root, vars, ec->ec_relids);
                               2602                 :           6641 :                 list_free(vars);
                               2603                 :                :             }
                               2604                 :                :         }
                               2605                 :                :     }
                               2606                 :           6681 : }
                               2607                 :                : 
                               2608                 :                : /*
                               2609                 :                :  * find_join_domain
                               2610                 :                :  *    Find the highest JoinDomain enclosed within the given relid set.
                               2611                 :                :  *
                               2612                 :                :  * (We could avoid this search at the cost of complicating APIs elsewhere,
                               2613                 :                :  * which doesn't seem worth it.)
                               2614                 :                :  */
                               2615                 :                : static JoinDomain *
 1140                          2616                 :           1063 : find_join_domain(PlannerInfo *root, Relids relids)
                               2617                 :                : {
                               2618                 :                :     ListCell   *lc;
                               2619                 :                : 
                               2620   [ +  -  +  -  :           2174 :     foreach(lc, root->join_domains)
                                              +  - ]
                               2621                 :                :     {
                               2622                 :           2174 :         JoinDomain *jdomain = (JoinDomain *) lfirst(lc);
                               2623                 :                : 
                               2624         [ +  + ]:           2174 :         if (bms_is_subset(jdomain->jd_relids, relids))
                               2625                 :           1063 :             return jdomain;
                               2626                 :                :     }
 1140 tgl@sss.pgh.pa.us        2627         [ #  # ]:UBC           0 :     elog(ERROR, "failed to find appropriate JoinDomain");
                               2628                 :                :     return NULL;                /* keep compiler quiet */
                               2629                 :                : }
                               2630                 :                : 
                               2631                 :                : 
                               2632                 :                : /*
                               2633                 :                :  * exprs_known_equal
                               2634                 :                :  *    Detect whether two expressions are known equal due to equivalence
                               2635                 :                :  *    relationships.
                               2636                 :                :  *
                               2637                 :                :  * If opfamily is given, the expressions must be known equal per the semantics
                               2638                 :                :  * of that opfamily (note it has to be a btree opfamily, since those are the
                               2639                 :                :  * only opfamilies equivclass.c deals with).  If opfamily is InvalidOid, we'll
                               2640                 :                :  * return true if they're equal according to any opfamily, which is fuzzy but
                               2641                 :                :  * OK for estimation purposes.
                               2642                 :                :  *
                               2643                 :                :  * Note: does not bother to check for "equal(item1, item2)"; caller must
                               2644                 :                :  * check that case if it's possible to pass identical items.
                               2645                 :                :  */
                               2646                 :                : bool
  593 rguo@postgresql.org      2647                 :CBC        3008 : exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2, Oid opfamily)
                               2648                 :                : {
                               2649                 :                :     ListCell   *lc1;
                               2650                 :                : 
 6994 tgl@sss.pgh.pa.us        2651   [ +  +  +  +  :          16566 :     foreach(lc1, root->eq_classes)
                                              +  + ]
                               2652                 :                :     {
                               2653                 :          13741 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
                               2654                 :          13741 :         bool        item1member = false;
                               2655                 :          13741 :         bool        item2member = false;
                               2656                 :                :         ListCell   *lc2;
                               2657                 :                : 
                               2658                 :                :         /* Never match to a volatile EC */
                               2659         [ -  + ]:          13741 :         if (ec->ec_has_volatile)
 6994 tgl@sss.pgh.pa.us        2660                 :UBC           0 :             continue;
                               2661                 :                : 
                               2662                 :                :         /*
                               2663                 :                :          * It's okay to consider ec_broken ECs here.  Brokenness just means we
                               2664                 :                :          * couldn't derive all the implied clauses we'd have liked to; it does
                               2665                 :                :          * not invalidate our knowledge that the members are equal.
                               2666                 :                :          */
                               2667                 :                : 
                               2668                 :                :         /* Ignore if this EC doesn't use specified opfamily */
  593 rguo@postgresql.org      2669         [ +  + ]:CBC       13741 :         if (OidIsValid(opfamily) &&
                               2670         [ +  + ]:            330 :             !list_member_oid(ec->ec_opfamilies, opfamily))
                               2671                 :            114 :             continue;
                               2672                 :                : 
                               2673                 :                :         /* Ignore children here */
 6994 tgl@sss.pgh.pa.us        2674   [ +  +  +  +  :          33445 :         foreach(lc2, ec->ec_members)
                                              +  + ]
                               2675                 :                :         {
                               2676                 :          20001 :             EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
                               2677                 :                : 
                               2678                 :                :             /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     2679         [ -  + ]:          20001 :             Assert(!em->em_is_child);
 6994 tgl@sss.pgh.pa.us        2680         [ +  + ]:          20001 :             if (equal(item1, em->em_expr))
                               2681                 :           1714 :                 item1member = true;
                               2682         [ +  + ]:          18287 :             else if (equal(item2, em->em_expr))
                               2683                 :           1889 :                 item2member = true;
                               2684                 :                :             /* Exit as soon as equality is proven */
                               2685   [ +  +  +  + ]:          20001 :             if (item1member && item2member)
                               2686                 :            183 :                 return true;
                               2687                 :                :         }
                               2688                 :                :     }
                               2689                 :           2825 :     return false;
                               2690                 :                : }
                               2691                 :                : 
                               2692                 :                : 
                               2693                 :                : /*
                               2694                 :                :  * match_eclasses_to_foreign_key_col
                               2695                 :                :  *    See whether a foreign key column match is proven by any eclass.
                               2696                 :                :  *
                               2697                 :                :  * If the referenced and referencing Vars of the fkey's colno'th column are
                               2698                 :                :  * known equal due to any eclass, return that eclass; otherwise return NULL.
                               2699                 :                :  * (In principle there might be more than one matching eclass if multiple
                               2700                 :                :  * collations are involved, but since collation doesn't matter for equality,
                               2701                 :                :  * we ignore that fine point here.)  This is much like exprs_known_equal,
                               2702                 :                :  * except for the format of the input.
                               2703                 :                :  *
                               2704                 :                :  * On success, we also set fkinfo->eclass[colno] to the matching eclass,
                               2705                 :                :  * and set fkinfo->fk_eclass_member[colno] to the eclass member for the
                               2706                 :                :  * referencing Var.
                               2707                 :                :  */
                               2708                 :                : EquivalenceClass *
 3557                          2709                 :           1212 : match_eclasses_to_foreign_key_col(PlannerInfo *root,
                               2710                 :                :                                   ForeignKeyOptInfo *fkinfo,
                               2711                 :                :                                   int colno)
                               2712                 :                : {
                               2713                 :           1212 :     Index       var1varno = fkinfo->con_relid;
                               2714                 :           1212 :     AttrNumber  var1attno = fkinfo->conkey[colno];
                               2715                 :           1212 :     Index       var2varno = fkinfo->ref_relid;
                               2716                 :           1212 :     AttrNumber  var2attno = fkinfo->confkey[colno];
                               2717                 :           1212 :     Oid         eqop = fkinfo->conpfeqop[colno];
 2429 drowley@postgresql.o     2718                 :           1212 :     RelOptInfo *rel1 = root->simple_rel_array[var1varno];
                               2719                 :           1212 :     RelOptInfo *rel2 = root->simple_rel_array[var2varno];
 3189 tgl@sss.pgh.pa.us        2720                 :           1212 :     List       *opfamilies = NIL;   /* compute only if needed */
                               2721                 :                :     Bitmapset  *matching_ecs;
                               2722                 :                :     int         i;
                               2723                 :                : 
                               2724                 :                :     /* Consider only eclasses mentioning both relations */
 2429 drowley@postgresql.o     2725         [ -  + ]:           1212 :     Assert(root->ec_merging_done);
                               2726   [ -  +  -  - ]:           1212 :     Assert(IS_SIMPLE_REL(rel1));
                               2727   [ -  +  -  - ]:           1212 :     Assert(IS_SIMPLE_REL(rel2));
                               2728                 :           1212 :     matching_ecs = bms_intersect(rel1->eclass_indexes,
                               2729                 :           1212 :                                  rel2->eclass_indexes);
                               2730                 :                : 
                               2731                 :           1212 :     i = -1;
                               2732         [ +  + ]:           1260 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
                               2733                 :                :     {
                               2734                 :            269 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
                               2735                 :                :                                                              i);
 1964 tgl@sss.pgh.pa.us        2736                 :            269 :         EquivalenceMember *item1_em = NULL;
                               2737                 :            269 :         EquivalenceMember *item2_em = NULL;
                               2738                 :                :         ListCell   *lc2;
                               2739                 :                : 
                               2740                 :                :         /* Never match to a volatile EC */
 3557                          2741         [ -  + ]:            269 :         if (ec->ec_has_volatile)
 3557 tgl@sss.pgh.pa.us        2742                 :UBC           0 :             continue;
                               2743                 :                : 
                               2744                 :                :         /*
                               2745                 :                :          * It's okay to consider "broken" ECs here, see exprs_known_equal.
                               2746                 :                :          * Ignore children here.
                               2747                 :                :          */
 3557 tgl@sss.pgh.pa.us        2748   [ +  -  +  +  :CBC         637 :         foreach(lc2, ec->ec_members)
                                              +  + ]
                               2749                 :                :         {
                               2750                 :            589 :             EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
                               2751                 :                :             Var        *var;
                               2752                 :                : 
                               2753                 :                :             /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     2754         [ -  + ]:            589 :             Assert(!em->em_is_child);
                               2755                 :                : 
                               2756                 :                :             /* EM must be a Var, possibly with RelabelType */
 3557 tgl@sss.pgh.pa.us        2757                 :            589 :             var = (Var *) em->em_expr;
                               2758   [ +  -  -  + ]:            589 :             while (var && IsA(var, RelabelType))
 3557 tgl@sss.pgh.pa.us        2759                 :UBC           0 :                 var = (Var *) ((RelabelType *) var)->arg;
 3557 tgl@sss.pgh.pa.us        2760   [ +  -  +  + ]:CBC         589 :             if (!(var && IsA(var, Var)))
                               2761                 :              3 :                 continue;
                               2762                 :                : 
                               2763                 :                :             /* Match? */
                               2764   [ +  +  +  + ]:            586 :             if (var->varno == var1varno && var->varattno == var1attno)
 1964                          2765                 :            221 :                 item1_em = em;
 3557                          2766   [ +  +  +  + ]:            365 :             else if (var->varno == var2varno && var->varattno == var2attno)
 1964                          2767                 :            221 :                 item2_em = em;
                               2768                 :                : 
                               2769                 :                :             /* Have we found both PK and FK column in this EC? */
                               2770   [ +  +  +  + ]:            586 :             if (item1_em && item2_em)
                               2771                 :                :             {
                               2772                 :                :                 /*
                               2773                 :                :                  * Succeed if eqop matches EC's opfamilies.  We could test
                               2774                 :                :                  * this before scanning the members, but it's probably cheaper
                               2775                 :                :                  * to test for member matches first.
                               2776                 :                :                  */
 3557                          2777         [ +  - ]:            221 :                 if (opfamilies == NIL)  /* compute if we didn't already */
                               2778                 :            221 :                     opfamilies = get_mergejoin_opfamilies(eqop);
                               2779         [ +  - ]:            221 :                 if (equal(opfamilies, ec->ec_opfamilies))
                               2780                 :                :                 {
 1964                          2781                 :            221 :                     fkinfo->eclass[colno] = ec;
                               2782                 :            221 :                     fkinfo->fk_eclass_member[colno] = item2_em;
 3557                          2783                 :            221 :                     return ec;
                               2784                 :                :                 }
                               2785                 :                :                 /* Otherwise, done with this EC, move on to the next */
 3557 tgl@sss.pgh.pa.us        2786                 :UBC           0 :                 break;
                               2787                 :                :             }
                               2788                 :                :         }
                               2789                 :                :     }
 3557 tgl@sss.pgh.pa.us        2790                 :CBC         991 :     return NULL;
                               2791                 :                : }
                               2792                 :                : 
                               2793                 :                : /*
                               2794                 :                :  * find_derived_clause_for_ec_member
                               2795                 :                :  *    Search for a previously-derived clause mentioning the given EM.
                               2796                 :                :  *
                               2797                 :                :  * The eclass should be an ec_has_const EC, of which the EM is a non-const
                               2798                 :                :  * member.  This should ensure there is just one derived clause mentioning
                               2799                 :                :  * the EM (and equating it to a constant).
                               2800                 :                :  * Returns NULL if no such clause can be found.
                               2801                 :                :  */
                               2802                 :                : RestrictInfo *
  345 amitlan@postgresql.o     2803                 :              3 : find_derived_clause_for_ec_member(PlannerInfo *root,
                               2804                 :                :                                   EquivalenceClass *ec,
                               2805                 :                :                                   EquivalenceMember *em)
                               2806                 :                : {
 1964 tgl@sss.pgh.pa.us        2807         [ -  + ]:              3 :     Assert(ec->ec_has_const);
                               2808         [ -  + ]:              3 :     Assert(!em->em_is_const);
                               2809                 :                : 
  345 amitlan@postgresql.o     2810                 :              3 :     return ec_search_derived_clause_for_ems(root, ec, em, NULL, NULL);
                               2811                 :                : }
                               2812                 :                : 
                               2813                 :                : 
                               2814                 :                : /*
                               2815                 :                :  * add_child_rel_equivalences
                               2816                 :                :  *    Search for EC members that reference the root parent of child_rel, and
                               2817                 :                :  *    add transformed members referencing the child_rel.
                               2818                 :                :  *
                               2819                 :                :  * Note that this function won't be called at all unless we have at least some
                               2820                 :                :  * reason to believe that the EC members it generates will be useful.
                               2821                 :                :  *
                               2822                 :                :  * parent_rel and child_rel could be derived from appinfo, but since the
                               2823                 :                :  * caller has already computed them, we might as well just pass them in.
                               2824                 :                :  *
                               2825                 :                :  * The passed-in AppendRelInfo is not used when the parent_rel is not a
                               2826                 :                :  * top-level baserel, since it shows the mapping from the parent_rel but
                               2827                 :                :  * we need to translate EC expressions that refer to the top-level parent.
                               2828                 :                :  * Using it is faster than using adjust_appendrel_attrs_multilevel(), though,
                               2829                 :                :  * so we prefer it when we can.
                               2830                 :                :  */
                               2831                 :                : void
 6994 tgl@sss.pgh.pa.us        2832                 :          19232 : add_child_rel_equivalences(PlannerInfo *root,
                               2833                 :                :                            AppendRelInfo *appinfo,
                               2834                 :                :                            RelOptInfo *parent_rel,
                               2835                 :                :                            RelOptInfo *child_rel)
                               2836                 :                : {
 2322                          2837                 :          19232 :     Relids      top_parent_relids = child_rel->top_parent_relids;
                               2838                 :          19232 :     Relids      child_relids = child_rel->relids;
                               2839                 :                :     int         i;
                               2840                 :                : 
                               2841                 :                :     /*
                               2842                 :                :      * EC merging should be complete already, so we can use the parent rel's
                               2843                 :                :      * eclass_indexes to avoid searching all of root->eq_classes.
                               2844                 :                :      */
 2429 drowley@postgresql.o     2845         [ -  + ]:          19232 :     Assert(root->ec_merging_done);
                               2846   [ +  +  -  + ]:          19232 :     Assert(IS_SIMPLE_REL(parent_rel));
                               2847                 :                : 
                               2848                 :          19232 :     i = -1;
                               2849         [ +  + ]:          56597 :     while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
                               2850                 :                :     {
                               2851                 :          37365 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
                               2852                 :                : 
                               2853                 :                :         /*
                               2854                 :                :          * If this EC contains a volatile expression, then generating child
                               2855                 :                :          * EMs would be downright dangerous, so skip it.  We rely on a
                               2856                 :                :          * volatile EC having only one EM.
                               2857                 :                :          */
 5126 tgl@sss.pgh.pa.us        2858         [ -  + ]:          37365 :         if (cur_ec->ec_has_volatile)
 6994 tgl@sss.pgh.pa.us        2859                 :UBC           0 :             continue;
                               2860                 :                : 
                               2861                 :                :         /* Sanity check eclass_indexes only contain ECs for parent_rel */
 2322 tgl@sss.pgh.pa.us        2862         [ -  + ]:CBC       37365 :         Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
                               2863                 :                : 
  341 drowley@postgresql.o     2864   [ +  -  +  +  :         125778 :         foreach_node(EquivalenceMember, cur_em, cur_ec->ec_members)
                                              +  + ]
                               2865                 :                :         {
 4370 tgl@sss.pgh.pa.us        2866         [ +  + ]:          51048 :             if (cur_em->em_is_const)
                               2867                 :           1719 :                 continue;       /* ignore consts here */
                               2868                 :                : 
                               2869                 :                :             /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     2870         [ -  + ]:          49329 :             Assert(!cur_em->em_is_child);
                               2871                 :                : 
                               2872                 :                :             /*
                               2873                 :                :              * Consider only members that reference and can be computed at
                               2874                 :                :              * child's topmost parent rel.  In particular we want to exclude
                               2875                 :                :              * parent-rel Vars that have nonempty varnullingrels.  Translating
                               2876                 :                :              * those might fail, if the transformed expression wouldn't be a
                               2877                 :                :              * simple Var; and in any case it wouldn't produce a member that
                               2878                 :                :              * has any use in creating plans for the child rel.
                               2879                 :                :              */
 1140 tgl@sss.pgh.pa.us        2880         [ +  + ]:          49329 :             if (bms_is_subset(cur_em->em_relids, top_parent_relids) &&
                               2881         [ +  - ]:          36228 :                 !bms_is_empty(cur_em->em_relids))
                               2882                 :                :             {
                               2883                 :                :                 /* OK, generate transformed child version */
                               2884                 :                :                 Expr       *child_expr;
                               2885                 :                :                 Relids      new_relids;
                               2886                 :                : 
 2467                          2887         [ +  + ]:          36228 :                 if (parent_rel->reloptkind == RELOPT_BASEREL)
                               2888                 :                :                 {
                               2889                 :                :                     /* Simple single-level transformation */
                               2890                 :                :                     child_expr = (Expr *)
                               2891                 :          31866 :                         adjust_appendrel_attrs(root,
                               2892                 :          31866 :                                                (Node *) cur_em->em_expr,
                               2893                 :                :                                                1, &appinfo);
                               2894                 :                :                 }
                               2895                 :                :                 else
                               2896                 :                :                 {
                               2897                 :                :                     /* Must do multi-level transformation */
                               2898                 :                :                     child_expr = (Expr *)
                               2899                 :           4362 :                         adjust_appendrel_attrs_multilevel(root,
                               2900                 :           4362 :                                                           (Node *) cur_em->em_expr,
                               2901                 :                :                                                           child_rel,
 1305                          2902                 :           4362 :                                                           child_rel->top_parent);
                               2903                 :                :                 }
                               2904                 :                : 
                               2905                 :                :                 /*
                               2906                 :                :                  * Transform em_relids to match.  Note we do *not* do
                               2907                 :                :                  * pull_varnos(child_expr) here, as for example the
                               2908                 :                :                  * transformation might have substituted a constant, but we
                               2909                 :                :                  * don't want the child member to be marked as constant.
                               2910                 :                :                  */
 5078                          2911                 :          36228 :                 new_relids = bms_difference(cur_em->em_relids,
                               2912                 :                :                                             top_parent_relids);
 2322                          2913                 :          36228 :                 new_relids = bms_add_members(new_relids, child_relids);
                               2914                 :                : 
  341 drowley@postgresql.o     2915                 :          36228 :                 add_child_eq_member(root,
                               2916                 :                :                                     cur_ec,
                               2917                 :                :                                     i,
                               2918                 :                :                                     child_expr,
                               2919                 :                :                                     new_relids,
                               2920                 :                :                                     cur_em->em_jdomain,
                               2921                 :                :                                     cur_em,
                               2922                 :                :                                     cur_em->em_datatype,
                               2923                 :                :                                     child_rel->relid);
                               2924                 :                :             }
                               2925                 :                :         }
                               2926                 :                :     }
 6994 tgl@sss.pgh.pa.us        2927                 :          19232 : }
                               2928                 :                : 
                               2929                 :                : /*
                               2930                 :                :  * add_child_join_rel_equivalences
                               2931                 :                :  *    Like add_child_rel_equivalences(), but for joinrels
                               2932                 :                :  *
                               2933                 :                :  * Here we find the ECs relevant to the top parent joinrel and add transformed
                               2934                 :                :  * member expressions that refer to this child joinrel.
                               2935                 :                :  *
                               2936                 :                :  * Note that this function won't be called at all unless we have at least some
                               2937                 :                :  * reason to believe that the EC members it generates will be useful.
                               2938                 :                :  */
                               2939                 :                : void
 2322                          2940                 :           8963 : add_child_join_rel_equivalences(PlannerInfo *root,
                               2941                 :                :                                 int nappinfos, AppendRelInfo **appinfos,
                               2942                 :                :                                 RelOptInfo *parent_joinrel,
                               2943                 :                :                                 RelOptInfo *child_joinrel)
                               2944                 :                : {
                               2945                 :           8963 :     Relids      top_parent_relids = child_joinrel->top_parent_relids;
                               2946                 :           8963 :     Relids      child_relids = child_joinrel->relids;
                               2947                 :                :     Bitmapset  *matching_ecs;
                               2948                 :                :     MemoryContext oldcontext;
                               2949                 :                :     int         i;
                               2950                 :                : 
                               2951   [ +  -  +  -  :           8963 :     Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
                                        +  +  -  + ]
                               2952                 :                : 
                               2953                 :                :     /* We need consider only ECs that mention the parent joinrel */
                               2954                 :           8963 :     matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
                               2955                 :                : 
                               2956                 :                :     /*
                               2957                 :                :      * If we're being called during GEQO join planning, we still have to
                               2958                 :                :      * create any new EC members in the main planner context, to avoid having
                               2959                 :                :      * a corrupt EC data structure after the GEQO context is reset.  This is
                               2960                 :                :      * problematic since we'll leak memory across repeated GEQO cycles.  For
                               2961                 :                :      * now, though, bloat is better than crash.  If it becomes a real issue
                               2962                 :                :      * we'll have to do something to avoid generating duplicate EC members.
                               2963                 :                :      */
 1986                          2964                 :           8963 :     oldcontext = MemoryContextSwitchTo(root->planner_cxt);
                               2965                 :                : 
 2322                          2966                 :           8963 :     i = -1;
                               2967         [ +  + ]:          23991 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
                               2968                 :                :     {
                               2969                 :          15028 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
                               2970                 :                : 
                               2971                 :                :         /*
                               2972                 :                :          * If this EC contains a volatile expression, then generating child
                               2973                 :                :          * EMs would be downright dangerous, so skip it.  We rely on a
                               2974                 :                :          * volatile EC having only one EM.
                               2975                 :                :          */
                               2976         [ -  + ]:          15028 :         if (cur_ec->ec_has_volatile)
 2322 tgl@sss.pgh.pa.us        2977                 :UBC           0 :             continue;
                               2978                 :                : 
                               2979                 :                :         /* Sanity check on get_eclass_indexes_for_relids result */
 2322 tgl@sss.pgh.pa.us        2980         [ -  + ]:CBC       15028 :         Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
                               2981                 :                : 
  341 drowley@postgresql.o     2982   [ +  -  +  +  :          55942 :         foreach_node(EquivalenceMember, cur_em, cur_ec->ec_members)
                                              +  + ]
                               2983                 :                :         {
 2322 tgl@sss.pgh.pa.us        2984         [ +  + ]:          25886 :             if (cur_em->em_is_const)
                               2985                 :           1170 :                 continue;       /* ignore consts here */
                               2986                 :                : 
                               2987                 :                :             /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     2988         [ -  + ]:          24716 :             Assert(!cur_em->em_is_child);
                               2989                 :                : 
                               2990                 :                :             /*
                               2991                 :                :              * We may ignore expressions that reference a single baserel,
                               2992                 :                :              * because add_child_rel_equivalences should have handled them.
                               2993                 :                :              */
 2322 tgl@sss.pgh.pa.us        2994         [ +  + ]:          24716 :             if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
                               2995                 :          23405 :                 continue;
                               2996                 :                : 
                               2997                 :                :             /* Does this member reference child's topmost parent rel? */
                               2998         [ +  - ]:           1311 :             if (bms_overlap(cur_em->em_relids, top_parent_relids))
                               2999                 :                :             {
                               3000                 :                :                 /* Yes, generate transformed child version */
                               3001                 :                :                 Expr       *child_expr;
                               3002                 :                :                 Relids      new_relids;
                               3003                 :                : 
                               3004         [ +  + ]:           1311 :                 if (parent_joinrel->reloptkind == RELOPT_JOINREL)
                               3005                 :                :                 {
                               3006                 :                :                     /* Simple single-level transformation */
                               3007                 :                :                     child_expr = (Expr *)
                               3008                 :           1263 :                         adjust_appendrel_attrs(root,
                               3009                 :           1263 :                                                (Node *) cur_em->em_expr,
                               3010                 :                :                                                nappinfos, appinfos);
                               3011                 :                :                 }
                               3012                 :                :                 else
                               3013                 :                :                 {
                               3014                 :                :                     /* Must do multi-level transformation */
                               3015         [ -  + ]:             48 :                     Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
                               3016                 :                :                     child_expr = (Expr *)
                               3017                 :             48 :                         adjust_appendrel_attrs_multilevel(root,
                               3018                 :             48 :                                                           (Node *) cur_em->em_expr,
                               3019                 :                :                                                           child_joinrel,
 1305                          3020                 :             48 :                                                           child_joinrel->top_parent);
                               3021                 :                :                 }
                               3022                 :                : 
                               3023                 :                :                 /*
                               3024                 :                :                  * Transform em_relids to match.  Note we do *not* do
                               3025                 :                :                  * pull_varnos(child_expr) here, as for example the
                               3026                 :                :                  * transformation might have substituted a constant, but we
                               3027                 :                :                  * don't want the child member to be marked as constant.
                               3028                 :                :                  */
 2322                          3029                 :           1311 :                 new_relids = bms_difference(cur_em->em_relids,
                               3030                 :                :                                             top_parent_relids);
                               3031                 :           1311 :                 new_relids = bms_add_members(new_relids, child_relids);
                               3032                 :                : 
                               3033                 :                :                 /*
                               3034                 :                :                  * Add new child member to the EquivalenceClass.  Because this
                               3035                 :                :                  * is a RELOPT_OTHER_JOINREL which has multiple component
                               3036                 :                :                  * relids, there is no ideal place to store these members in
                               3037                 :                :                  * the class.  Ordinarily, child members are stored in the
                               3038                 :                :                  * ec_childmembers[] array element corresponding to their
                               3039                 :                :                  * relid, however, here we have multiple component relids, so
                               3040                 :                :                  * there's no single ec_childmembers[] array element to store
                               3041                 :                :                  * this member.  So that we still correctly find this member
                               3042                 :                :                  * in loops iterating over an EquivalenceMemberIterator, we
                               3043                 :                :                  * opt to store the member in the ec_childmembers array in
                               3044                 :                :                  * only the first component relid slot of the array.  This
                               3045                 :                :                  * allows the member to be found, providing callers of
                               3046                 :                :                  * setup_eclass_member_iterator() specify all the component
                               3047                 :                :                  * relids for the RELOPT_OTHER_JOINREL, which they do.  If we
                               3048                 :                :                  * opted to store the member in each ec_childmembers[] element
                               3049                 :                :                  * for all the component relids, then that would just result
                               3050                 :                :                  * in eclass_member_iterator_next() finding the member
                               3051                 :                :                  * multiple times, which is a waste of effort.
                               3052                 :                :                  */
  341 drowley@postgresql.o     3053                 :           1311 :                 add_child_eq_member(root,
                               3054                 :                :                                     cur_ec,
                               3055                 :                :                                     -1,
                               3056                 :                :                                     child_expr,
                               3057                 :                :                                     new_relids,
                               3058                 :                :                                     cur_em->em_jdomain,
                               3059                 :                :                                     cur_em,
                               3060                 :                :                                     cur_em->em_datatype,
                               3061                 :           1311 :                                     bms_next_member(child_joinrel->relids, -1));
                               3062                 :                :             }
                               3063                 :                :         }
                               3064                 :                :     }
                               3065                 :                : 
 1986 tgl@sss.pgh.pa.us        3066                 :           8963 :     MemoryContextSwitchTo(oldcontext);
 2322                          3067                 :           8963 : }
                               3068                 :                : 
                               3069                 :                : /*
                               3070                 :                :  * add_setop_child_rel_equivalences
                               3071                 :                :  *      Add equivalence members for each non-resjunk target in 'child_tlist'
                               3072                 :                :  *      to the EquivalenceClass in the corresponding setop_pathkey's pk_eclass.
                               3073                 :                :  *
                               3074                 :                :  * 'root' is the PlannerInfo belonging to the top-level set operation.
                               3075                 :                :  * 'child_rel' is the RelOptInfo of the child relation we're adding
                               3076                 :                :  * EquivalenceMembers for.
                               3077                 :                :  * 'child_tlist' is the target list for the setop child relation.  The target
                               3078                 :                :  * list expressions are what we add as EquivalenceMembers.
                               3079                 :                :  * 'setop_pathkeys' is a list of PathKeys which must contain an entry for each
                               3080                 :                :  * non-resjunk target in 'child_tlist'.
                               3081                 :                :  */
                               3082                 :                : void
  663 rhaas@postgresql.org     3083                 :           6263 : add_setop_child_rel_equivalences(PlannerInfo *root, RelOptInfo *child_rel,
                               3084                 :                :                                  List *child_tlist, List *setop_pathkeys)
                               3085                 :                : {
                               3086                 :                :     ListCell   *lc;
                               3087                 :           6263 :     ListCell   *lc2 = list_head(setop_pathkeys);
                               3088                 :                : 
                               3089   [ +  -  +  +  :          25380 :     foreach(lc, child_tlist)
                                              +  + ]
                               3090                 :                :     {
                               3091                 :          19117 :         TargetEntry *tle = lfirst_node(TargetEntry, lc);
                               3092                 :                :         EquivalenceMember *parent_em;
                               3093                 :                :         PathKey    *pk;
                               3094                 :                : 
                               3095         [ -  + ]:          19117 :         if (tle->resjunk)
  663 rhaas@postgresql.org     3096                 :UBC           0 :             continue;
                               3097                 :                : 
  663 rhaas@postgresql.org     3098         [ -  + ]:CBC       19117 :         if (lc2 == NULL)
  663 rhaas@postgresql.org     3099         [ #  # ]:UBC           0 :             elog(ERROR, "too few pathkeys for set operation");
                               3100                 :                : 
  663 rhaas@postgresql.org     3101                 :CBC       19117 :         pk = lfirst_node(PathKey, lc2);
                               3102                 :          19117 :         parent_em = linitial(pk->pk_eclass->ec_members);
                               3103                 :                : 
                               3104                 :                :         /*
                               3105                 :                :          * We can safely pass the parent member as the first member in the
                               3106                 :                :          * ec_members list as this is added first in generate_union_paths,
                               3107                 :                :          * likewise, the JoinDomain can be that of the initial member of the
                               3108                 :                :          * Pathkey's EquivalenceClass.  We pass -1 for ec_index since we
                               3109                 :                :          * maintain the eclass_indexes for the child_rel after the loop.
                               3110                 :                :          */
  341 drowley@postgresql.o     3111                 :          19117 :         add_child_eq_member(root,
                               3112                 :                :                             pk->pk_eclass,
                               3113                 :                :                             -1,
                               3114                 :                :                             tle->expr,
                               3115                 :                :                             child_rel->relids,
                               3116                 :                :                             parent_em->em_jdomain,
                               3117                 :                :                             parent_em,
                               3118                 :          19117 :                             exprType((Node *) tle->expr),
                               3119                 :                :                             child_rel->relid);
                               3120                 :                : 
  663 rhaas@postgresql.org     3121                 :          19117 :         lc2 = lnext(setop_pathkeys, lc2);
                               3122                 :                :     }
                               3123                 :                : 
                               3124                 :                :     /*
                               3125                 :                :      * transformSetOperationStmt() ensures that the targetlist never contains
                               3126                 :                :      * any resjunk columns, so all eclasses that exist in 'root' must have
                               3127                 :                :      * received a new member in the loop above.  Add them to the child_rel's
                               3128                 :                :      * eclass_indexes.
                               3129                 :                :      */
                               3130                 :           6263 :     child_rel->eclass_indexes = bms_add_range(child_rel->eclass_indexes, 0,
                               3131                 :           6263 :                                               list_length(root->eq_classes) - 1);
                               3132                 :           6263 : }
                               3133                 :                : 
                               3134                 :                : /*
                               3135                 :                :  * setup_eclass_member_iterator
                               3136                 :                :  *    Setup an EquivalenceMemberIterator 'it' to iterate over all parent
                               3137                 :                :  *    EquivalenceMembers and child members belonging to the given 'ec'.
                               3138                 :                :  *
                               3139                 :                :  * This iterator returns:
                               3140                 :                :  *  - All parent members stored directly in ec_members for 'ec', and;
                               3141                 :                :  *  - Any child member added to the given ec by add_child_eq_member() where
                               3142                 :                :  *    the child_relid specified in the add_child_eq_member() call is a member
                               3143                 :                :  *    of the 'child_relids' parameter.
                               3144                 :                :  *
                               3145                 :                :  * Note:
                               3146                 :                :  * The given 'child_relids' must remain allocated and not be changed for the
                               3147                 :                :  * lifetime of the iterator.
                               3148                 :                :  *
                               3149                 :                :  * Parameters:
                               3150                 :                :  *  'it' is a pointer to the iterator to set up.  Normally stack allocated.
                               3151                 :                :  *  'ec' is the EquivalenceClass from which to iterate members for.
                               3152                 :                :  *  'child_relids' is the relids to return child members for.
                               3153                 :                :  */
                               3154                 :                : void
  341 drowley@postgresql.o     3155                 :        2886393 : setup_eclass_member_iterator(EquivalenceMemberIterator *it,
                               3156                 :                :                              EquivalenceClass *ec, Relids child_relids)
                               3157                 :                : {
                               3158                 :        2886393 :     it->ec = ec;
                               3159                 :                :     /* no need to set this if the class has no child members array set */
                               3160         [ +  + ]:        2886393 :     it->child_relids = ec->ec_childmembers != NULL ? child_relids : NULL;
                               3161                 :        2886393 :     it->current_relid = -1;
                               3162                 :        2886393 :     it->current_list = ec->ec_members;
                               3163                 :        2886393 :     it->current_cell = list_head(it->current_list);
                               3164                 :        2886393 : }
                               3165                 :                : 
                               3166                 :                : /*
                               3167                 :                :  * eclass_member_iterator_next
                               3168                 :                :  *    Get the next EquivalenceMember from the EquivalenceMemberIterator 'it',
                               3169                 :                :  *    as setup by setup_eclass_member_iterator().  NULL is returned if there
                               3170                 :                :  *    are no members left, after which callers must not call
                               3171                 :                :  *    eclass_member_iterator_next() again for the given iterator.
                               3172                 :                :  */
                               3173                 :                : EquivalenceMember *
                               3174                 :        6731932 : eclass_member_iterator_next(EquivalenceMemberIterator *it)
                               3175                 :                : {
                               3176         [ +  + ]:        6731932 :     while (it->current_list != NULL)
                               3177                 :                :     {
                               3178         [ +  + ]:        6720354 :         while (it->current_cell != NULL)
                               3179                 :                :         {
                               3180                 :                :             EquivalenceMember *em;
                               3181                 :                : 
                               3182                 :        4656903 :     nextcell:
                               3183                 :        4723527 :             em = lfirst_node(EquivalenceMember, it->current_cell);
                               3184                 :        4723527 :             it->current_cell = lnext(it->current_list, it->current_cell);
                               3185                 :        4723527 :             return em;
                               3186                 :                :         }
                               3187                 :                : 
                               3188                 :                :         /* Search for the next list to return members from */
                               3189         [ +  + ]:        2130359 :         while ((it->current_relid = bms_next_member(it->child_relids, it->current_relid)) > 0)
                               3190                 :                :         {
                               3191                 :                :             /*
                               3192                 :                :              * Be paranoid in case we're given relids above what we've sized
                               3193                 :                :              * the ec_childmembers array to.
                               3194                 :                :              */
                               3195         [ -  + ]:         133532 :             if (it->current_relid >= it->ec->ec_childmembers_size)
  341 drowley@postgresql.o     3196                 :UBC           0 :                 return NULL;
                               3197                 :                : 
  341 drowley@postgresql.o     3198                 :CBC      133532 :             it->current_list = it->ec->ec_childmembers[it->current_relid];
                               3199                 :                : 
                               3200                 :                :             /* If there are members in this list, use it. */
                               3201         [ +  + ]:         133532 :             if (it->current_list != NIL)
                               3202                 :                :             {
                               3203                 :                :                 /* point current_cell to the head of this list */
                               3204                 :          66624 :                 it->current_cell = list_head(it->current_list);
                               3205                 :          66624 :                 goto nextcell;
                               3206                 :                :             }
                               3207                 :                :         }
                               3208                 :        1996827 :         return NULL;
                               3209                 :                :     }
                               3210                 :                : 
                               3211                 :          11578 :     return NULL;
                               3212                 :                : }
                               3213                 :                : 
                               3214                 :                : /*
                               3215                 :                :  * generate_implied_equalities_for_column
                               3216                 :                :  *    Create EC-derived joinclauses usable with a specific column.
                               3217                 :                :  *
                               3218                 :                :  * This is used by indxpath.c to extract potentially indexable joinclauses
                               3219                 :                :  * from ECs, and can be used by foreign data wrappers for similar purposes.
                               3220                 :                :  * We assume that only expressions in Vars of a single table are of interest,
                               3221                 :                :  * but the caller provides a callback function to identify exactly which
                               3222                 :                :  * such expressions it would like to know about.
                               3223                 :                :  *
                               3224                 :                :  * We assume that any given table/index column could appear in only one EC.
                               3225                 :                :  * (This should be true in all but the most pathological cases, and if it
                               3226                 :                :  * isn't, we stop on the first match anyway.)  Therefore, what we return
                               3227                 :                :  * is a redundant list of clauses equating the table/index column to each of
                               3228                 :                :  * the other-relation values it is known to be equal to.  Any one of
                               3229                 :                :  * these clauses can be used to create a parameterized path, and there
                               3230                 :                :  * is no value in using more than one.  (But it *is* worthwhile to create
                               3231                 :                :  * a separate parameterized path for each one, since that leads to different
                               3232                 :                :  * join orders.)
                               3233                 :                :  *
                               3234                 :                :  * The caller can pass a Relids set of rels we aren't interested in joining
                               3235                 :                :  * to, so as to save the work of creating useless clauses.
                               3236                 :                :  */
                               3237                 :                : List *
 4742 tgl@sss.pgh.pa.us        3238                 :         366707 : generate_implied_equalities_for_column(PlannerInfo *root,
                               3239                 :                :                                        RelOptInfo *rel,
                               3240                 :                :                                        ec_matches_callback_type callback,
                               3241                 :                :                                        void *callback_arg,
                               3242                 :                :                                        Relids prohibited_rels)
                               3243                 :                : {
 6994                          3244                 :         366707 :     List       *result = NIL;
                               3245                 :         366707 :     bool        is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
                               3246                 :                :     Relids      parent_relids;
                               3247                 :                :     int         i;
                               3248                 :                : 
                               3249                 :                :     /* Should be OK to rely on eclass_indexes */
 2429 drowley@postgresql.o     3250         [ -  + ]:         366707 :     Assert(root->ec_merging_done);
                               3251                 :                : 
                               3252                 :                :     /* Indexes are available only on base or "other" member relations. */
 3268 rhaas@postgresql.org     3253   [ +  +  -  + ]:         366707 :     Assert(IS_SIMPLE_REL(rel));
                               3254                 :                : 
                               3255                 :                :     /* If it's a child rel, we'll need to know what its parent(s) are */
 5161 tgl@sss.pgh.pa.us        3256         [ +  + ]:         366707 :     if (is_child_rel)
 4183                          3257                 :           6819 :         parent_relids = find_childrel_parents(root, rel);
                               3258                 :                :     else
                               3259                 :         359888 :         parent_relids = NULL;   /* not used, but keep compiler quiet */
                               3260                 :                : 
 2429 drowley@postgresql.o     3261                 :         366707 :     i = -1;
                               3262         [ +  + ]:        1050346 :     while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
                               3263                 :                :     {
                               3264                 :         756865 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
                               3265                 :                :         EquivalenceMemberIterator it;
                               3266                 :                :         EquivalenceMember *cur_em;
                               3267                 :                :         ListCell   *lc2;
                               3268                 :                : 
                               3269                 :                :         /* Sanity check eclass_indexes only contain ECs for rel */
                               3270   [ +  +  -  + ]:         756865 :         Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
                               3271                 :                : 
                               3272                 :                :         /*
                               3273                 :                :          * Won't generate joinclauses if const or single-member (the latter
                               3274                 :                :          * test covers the volatile case too)
                               3275                 :                :          */
 6994 tgl@sss.pgh.pa.us        3276   [ +  +  +  + ]:         756865 :         if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
                               3277                 :         683410 :             continue;
                               3278                 :                : 
                               3279                 :                :         /*
                               3280                 :                :          * Scan members, looking for a match to the target column.  Note that
                               3281                 :                :          * child EC members are considered, but only when they belong to the
                               3282                 :                :          * target relation.  (Unlike regular members, the same expression
                               3283                 :                :          * could be a child member of more than one EC.  Therefore, it's
                               3284                 :                :          * potentially order-dependent which EC a child relation's target
                               3285                 :                :          * column gets matched to.  This is annoying but it only happens in
                               3286                 :                :          * corner cases, so for now we live with just reporting the first
                               3287                 :                :          * match.  See also get_eclass_for_sort_expr.)
                               3288                 :                :          */
  341 drowley@postgresql.o     3289                 :         367826 :         setup_eclass_member_iterator(&it, cur_ec, rel->relids);
                               3290         [ +  + ]:        1386543 :         while ((cur_em = eclass_member_iterator_next(&it)) != NULL)
                               3291                 :                :         {
 5161 tgl@sss.pgh.pa.us        3292   [ +  +  +  + ]:        1092758 :             if (bms_equal(cur_em->em_relids, rel->relids) &&
 4742                          3293                 :         368412 :                 callback(root, rel, cur_ec, cur_em, callback_arg))
 5161                          3294                 :          73455 :                 break;
                               3295                 :                :         }
                               3296                 :                : 
                               3297         [ +  + ]:         367826 :         if (!cur_em)
 6994                          3298                 :         294371 :             continue;
                               3299                 :                : 
                               3300                 :                :         /*
                               3301                 :                :          * Found our match.  Scan the other EC members and attempt to generate
                               3302                 :                :          * joinclauses.  Ignore children here.
                               3303                 :                :          */
                               3304   [ +  -  +  +  :         224797 :         foreach(lc2, cur_ec->ec_members)
                                              +  + ]
                               3305                 :                :         {
 5161                          3306                 :         151342 :             EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
                               3307                 :                :             Oid         eq_op;
                               3308                 :                :             RestrictInfo *rinfo;
                               3309                 :                : 
                               3310                 :                :             /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     3311         [ -  + ]:         151342 :             Assert(!other_em->em_is_child);
                               3312                 :                : 
                               3313                 :                :             /* Make sure it'll be a join to a different rel */
 5161 tgl@sss.pgh.pa.us        3314   [ +  +  +  + ]:         230876 :             if (other_em == cur_em ||
                               3315                 :          79534 :                 bms_overlap(other_em->em_relids, rel->relids))
 6994                          3316                 :          71992 :                 continue;
                               3317                 :                : 
                               3318                 :                :             /* Forget it if caller doesn't want joins to this rel */
 4945                          3319         [ +  + ]:          79350 :             if (bms_overlap(other_em->em_relids, prohibited_rels))
                               3320                 :             78 :                 continue;
                               3321                 :                : 
                               3322                 :                :             /*
                               3323                 :                :              * Also, if this is a child rel, avoid generating a useless join
                               3324                 :                :              * to its parent rel(s).
                               3325                 :                :              */
 5161                          3326   [ +  +  +  + ]:          82961 :             if (is_child_rel &&
 4183                          3327                 :           3689 :                 bms_overlap(parent_relids, other_em->em_relids))
 5161                          3328                 :           1689 :                 continue;
                               3329                 :                : 
                               3330                 :          77583 :             eq_op = select_equality_operator(cur_ec,
                               3331                 :                :                                              cur_em->em_datatype,
                               3332                 :                :                                              other_em->em_datatype);
                               3333         [ -  + ]:          77583 :             if (!OidIsValid(eq_op))
 5161 tgl@sss.pgh.pa.us        3334                 :UBC           0 :                 continue;
                               3335                 :                : 
                               3336                 :                :             /* set parent_ec to mark as redundant with other joinclauses */
 5161 tgl@sss.pgh.pa.us        3337                 :CBC       77583 :             rinfo = create_join_clause(root, cur_ec, eq_op,
                               3338                 :                :                                        cur_em, other_em,
                               3339                 :                :                                        cur_ec);
                               3340                 :                : 
                               3341                 :          77583 :             result = lappend(result, rinfo);
                               3342                 :                :         }
                               3343                 :                : 
                               3344                 :                :         /*
                               3345                 :                :          * If somehow we failed to create any join clauses, we might as well
                               3346                 :                :          * keep scanning the ECs for another match.  But if we did make any,
                               3347                 :                :          * we're done, because we don't want to return non-redundant clauses.
                               3348                 :                :          */
                               3349         [ +  + ]:          73455 :         if (result)
                               3350                 :          73226 :             break;
                               3351                 :                :     }
                               3352                 :                : 
 6994                          3353                 :         366707 :     return result;
                               3354                 :                : }
                               3355                 :                : 
                               3356                 :                : /*
                               3357                 :                :  * have_relevant_eclass_joinclause
                               3358                 :                :  *      Detect whether there is an EquivalenceClass that could produce
                               3359                 :                :  *      a joinclause involving the two given relations.
                               3360                 :                :  *
                               3361                 :                :  * This is essentially a very cut-down version of
                               3362                 :                :  * generate_join_implied_equalities().  Note it's OK to occasionally say "yes"
                               3363                 :                :  * incorrectly.  Hence we don't bother with details like whether the lack of a
                               3364                 :                :  * cross-type operator might prevent the clause from actually being generated.
                               3365                 :                :  * False negatives are not always fatal either: they will discourage, but not
                               3366                 :                :  * completely prevent, investigation of particular join pathways.
                               3367                 :                :  */
                               3368                 :                : bool
                               3369                 :         122793 : have_relevant_eclass_joinclause(PlannerInfo *root,
                               3370                 :                :                                 RelOptInfo *rel1, RelOptInfo *rel2)
                               3371                 :                : {
                               3372                 :                :     Bitmapset  *matching_ecs;
                               3373                 :                :     int         i;
                               3374                 :                : 
                               3375                 :                :     /*
                               3376                 :                :      * Examine only eclasses mentioning both rel1 and rel2.
                               3377                 :                :      *
                               3378                 :                :      * Note that we do not consider the possibility of an eclass generating
                               3379                 :                :      * "join" clauses that mention just one of the rels plus an outer join
                               3380                 :                :      * that could be formed from them.  Although such clauses must be
                               3381                 :                :      * correctly enforced when we form the outer join, they don't seem like
                               3382                 :                :      * sufficient reason to prioritize this join over other ones.  The join
                               3383                 :                :      * ordering rules will force the join to be made when necessary.
                               3384                 :                :      */
 2429 drowley@postgresql.o     3385                 :         122793 :     matching_ecs = get_common_eclass_indexes(root, rel1->relids,
                               3386                 :                :                                              rel2->relids);
                               3387                 :                : 
                               3388                 :         122793 :     i = -1;
                               3389         [ +  + ]:         122802 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
                               3390                 :                :     {
                               3391                 :         104185 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
                               3392                 :                :                                                              i);
                               3393                 :                : 
                               3394                 :                :         /*
                               3395                 :                :          * Sanity check that get_common_eclass_indexes gave only ECs
                               3396                 :                :          * containing both rels.
                               3397                 :                :          */
                               3398         [ -  + ]:         104185 :         Assert(bms_overlap(rel1->relids, ec->ec_relids));
                               3399         [ -  + ]:         104185 :         Assert(bms_overlap(rel2->relids, ec->ec_relids));
                               3400                 :                : 
                               3401                 :                :         /*
                               3402                 :                :          * Won't generate joinclauses if single-member (this test covers the
                               3403                 :                :          * volatile case too)
                               3404                 :                :          */
 6640 tgl@sss.pgh.pa.us        3405         [ +  + ]:         104185 :         if (list_length(ec->ec_members) <= 1)
 6994                          3406                 :              9 :             continue;
                               3407                 :                : 
                               3408                 :                :         /*
                               3409                 :                :          * We do not need to examine the individual members of the EC, because
                               3410                 :                :          * all that we care about is whether each rel overlaps the relids of
                               3411                 :                :          * at least one member, and get_common_eclass_indexes() and the single
                               3412                 :                :          * member check above are sufficient to prove that.  (As with
                               3413                 :                :          * have_relevant_joinclause(), it is not necessary that the EC be able
                               3414                 :                :          * to form a joinclause relating exactly the two given rels, only that
                               3415                 :                :          * it be able to form a joinclause mentioning both, and this will
                               3416                 :                :          * surely be true if both of them overlap ec_relids.)
                               3417                 :                :          *
                               3418                 :                :          * Note we don't test ec_broken; if we did, we'd need a separate code
                               3419                 :                :          * path to look through ec_sources.  Checking the membership anyway is
                               3420                 :                :          * OK as a possibly-overoptimistic heuristic.
                               3421                 :                :          *
                               3422                 :                :          * We don't test ec_has_const either, even though a const eclass won't
                               3423                 :                :          * generate real join clauses.  This is because if we had "WHERE a.x =
                               3424                 :                :          * b.y and a.x = 42", it is worth considering a join between a and b,
                               3425                 :                :          * since the join result is likely to be small even though it'll end
                               3426                 :                :          * up being an unqualified nestloop.
                               3427                 :                :          */
                               3428                 :                : 
 2429 drowley@postgresql.o     3429                 :         104176 :         return true;
                               3430                 :                :     }
                               3431                 :                : 
 6994 tgl@sss.pgh.pa.us        3432                 :          18617 :     return false;
                               3433                 :                : }
                               3434                 :                : 
                               3435                 :                : 
                               3436                 :                : /*
                               3437                 :                :  * has_relevant_eclass_joinclause
                               3438                 :                :  *      Detect whether there is an EquivalenceClass that could produce
                               3439                 :                :  *      a joinclause involving the given relation and anything else.
                               3440                 :                :  *
                               3441                 :                :  * This is the same as have_relevant_eclass_joinclause with the other rel
                               3442                 :                :  * implicitly defined as "everything else in the query".
                               3443                 :                :  */
                               3444                 :                : bool
                               3445                 :         129902 : has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
                               3446                 :                : {
                               3447                 :                :     Bitmapset  *matched_ecs;
                               3448                 :                :     int         i;
                               3449                 :                : 
                               3450                 :                :     /* Examine only eclasses mentioning rel1 */
 2429 drowley@postgresql.o     3451                 :         129902 :     matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
                               3452                 :                : 
                               3453                 :         129902 :     i = -1;
                               3454         [ +  + ]:         457883 :     while ((i = bms_next_member(matched_ecs, i)) >= 0)
                               3455                 :                :     {
                               3456                 :         377514 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
                               3457                 :                :                                                              i);
                               3458                 :                : 
                               3459                 :                :         /*
                               3460                 :                :          * Won't generate joinclauses if single-member (this test covers the
                               3461                 :                :          * volatile case too)
                               3462                 :                :          */
 6640 tgl@sss.pgh.pa.us        3463         [ +  + ]:         377514 :         if (list_length(ec->ec_members) <= 1)
 6994                          3464                 :         168016 :             continue;
                               3465                 :                : 
                               3466                 :                :         /*
                               3467                 :                :          * Per the comment in have_relevant_eclass_joinclause, it's sufficient
                               3468                 :                :          * to find an EC that mentions both this rel and some other rel.
                               3469                 :                :          */
 2429 drowley@postgresql.o     3470         [ +  + ]:         209498 :         if (!bms_is_subset(ec->ec_relids, rel1->relids))
 6994 tgl@sss.pgh.pa.us        3471                 :          49533 :             return true;
                               3472                 :                :     }
                               3473                 :                : 
                               3474                 :          80369 :     return false;
                               3475                 :                : }
                               3476                 :                : 
                               3477                 :                : 
                               3478                 :                : /*
                               3479                 :                :  * eclass_useful_for_merging
                               3480                 :                :  *    Detect whether the EC could produce any mergejoinable join clauses
                               3481                 :                :  *    against the specified relation.
                               3482                 :                :  *
                               3483                 :                :  * This is just a heuristic test and doesn't have to be exact; it's better
                               3484                 :                :  * to say "yes" incorrectly than "no".  Hence we don't bother with details
                               3485                 :                :  * like whether the lack of a cross-type operator might prevent the clause
                               3486                 :                :  * from actually being generated.
                               3487                 :                :  */
                               3488                 :                : bool
 3874                          3489                 :         479334 : eclass_useful_for_merging(PlannerInfo *root,
                               3490                 :                :                           EquivalenceClass *eclass,
                               3491                 :                :                           RelOptInfo *rel)
                               3492                 :                : {
                               3493                 :                :     Relids      relids;
                               3494                 :                :     ListCell   *lc;
                               3495                 :                : 
 6994                          3496         [ -  + ]:         479334 :     Assert(!eclass->ec_merged);
                               3497                 :                : 
                               3498                 :                :     /*
                               3499                 :                :      * Won't generate joinclauses if const or single-member (the latter test
                               3500                 :                :      * covers the volatile case too)
                               3501                 :                :      */
                               3502   [ +  +  +  + ]:         479334 :     if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
                               3503                 :          21916 :         return false;
                               3504                 :                : 
                               3505                 :                :     /*
                               3506                 :                :      * Note we don't test ec_broken; if we did, we'd need a separate code path
                               3507                 :                :      * to look through ec_sources.  Checking the members anyway is OK as a
                               3508                 :                :      * possibly-overoptimistic heuristic.
                               3509                 :                :      */
                               3510                 :                : 
                               3511                 :                :     /* If specified rel is a child, we must consider the topmost parent rel */
 3268 rhaas@postgresql.org     3512   [ +  +  +  +  :         457418 :     if (IS_OTHER_REL(rel))
                                              -  + ]
                               3513                 :                :     {
                               3514         [ -  + ]:           3101 :         Assert(!bms_is_empty(rel->top_parent_relids));
                               3515                 :           3101 :         relids = rel->top_parent_relids;
                               3516                 :                :     }
                               3517                 :                :     else
 3874 tgl@sss.pgh.pa.us        3518                 :         454317 :         relids = rel->relids;
                               3519                 :                : 
                               3520                 :                :     /* If rel already includes all members of eclass, no point in searching */
                               3521         [ +  + ]:         457418 :     if (bms_is_subset(eclass->ec_relids, relids))
 6994                          3522                 :         177960 :         return false;
                               3523                 :                : 
                               3524                 :                :     /*
                               3525                 :                :      * To join, we need a member not in the given rel.  Ignore children here.
                               3526                 :                :      */
                               3527   [ +  -  +  +  :         429270 :     foreach(lc, eclass->ec_members)
                                              +  + ]
                               3528                 :                :     {
                               3529                 :         428919 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
                               3530                 :                : 
                               3531                 :                :         /* Child members should not exist in ec_members */
  341 drowley@postgresql.o     3532         [ -  + ]:         428919 :         Assert(!cur_em->em_is_child);
                               3533                 :                : 
 3874 tgl@sss.pgh.pa.us        3534         [ +  + ]:         428919 :         if (!bms_overlap(cur_em->em_relids, relids))
 6994                          3535                 :         279107 :             return true;
                               3536                 :                :     }
                               3537                 :                : 
                               3538                 :            351 :     return false;
                               3539                 :                : }
                               3540                 :                : 
                               3541                 :                : 
                               3542                 :                : /*
                               3543                 :                :  * is_redundant_derived_clause
                               3544                 :                :  *      Test whether rinfo is derived from same EC as any clause in clauselist;
                               3545                 :                :  *      if so, it can be presumed to represent a condition that's redundant
                               3546                 :                :  *      with that member of the list.
                               3547                 :                :  */
                               3548                 :                : bool
 5078                          3549                 :             42 : is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
                               3550                 :                : {
                               3551                 :             42 :     EquivalenceClass *parent_ec = rinfo->parent_ec;
                               3552                 :                :     ListCell   *lc;
                               3553                 :                : 
                               3554                 :                :     /* Fail if it's not a potentially-redundant clause from some EC */
                               3555         [ +  - ]:             42 :     if (parent_ec == NULL)
                               3556                 :             42 :         return false;
                               3557                 :                : 
 5078 tgl@sss.pgh.pa.us        3558   [ #  #  #  #  :UBC           0 :     foreach(lc, clauselist)
                                              #  # ]
                               3559                 :                :     {
                               3560                 :              0 :         RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
                               3561                 :                : 
                               3562         [ #  # ]:              0 :         if (otherrinfo->parent_ec == parent_ec)
                               3563                 :              0 :             return true;
                               3564                 :                :     }
                               3565                 :                : 
                               3566                 :              0 :     return false;
                               3567                 :                : }
                               3568                 :                : 
                               3569                 :                : /*
                               3570                 :                :  * is_redundant_with_indexclauses
                               3571                 :                :  *      Test whether rinfo is redundant with any clause in the IndexClause
                               3572                 :                :  *      list.  Here, for convenience, we test both simple identity and
                               3573                 :                :  *      whether it is derived from the same EC as any member of the list.
                               3574                 :                :  */
                               3575                 :                : bool
 2591 tgl@sss.pgh.pa.us        3576                 :CBC      855592 : is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
                               3577                 :                : {
                               3578                 :         855592 :     EquivalenceClass *parent_ec = rinfo->parent_ec;
                               3579                 :                :     ListCell   *lc;
                               3580                 :                : 
                               3581   [ +  +  +  +  :        1183316 :     foreach(lc, indexclauses)
                                              +  + ]
                               3582                 :                :     {
                               3583                 :         874267 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
                               3584                 :         874267 :         RestrictInfo *otherrinfo = iclause->rinfo;
                               3585                 :                : 
                               3586                 :                :         /* If indexclause is lossy, it won't enforce the condition exactly */
                               3587         [ +  + ]:         874267 :         if (iclause->lossy)
                               3588                 :           3622 :             continue;
                               3589                 :                : 
                               3590                 :                :         /* Match if it's same clause (pointer equality should be enough) */
                               3591         [ +  + ]:         870645 :         if (rinfo == otherrinfo)
                               3592                 :         546543 :             return true;
                               3593                 :                :         /* Match if derived from same EC */
                               3594   [ +  +  +  + ]:         324303 :         if (parent_ec && otherrinfo->parent_ec == parent_ec)
                               3595                 :            201 :             return true;
                               3596                 :                : 
                               3597                 :                :         /*
                               3598                 :                :          * No need to look at the derived clauses in iclause->indexquals; they
                               3599                 :                :          * couldn't match if the parent clause didn't.
                               3600                 :                :          */
                               3601                 :                :     }
                               3602                 :                : 
                               3603                 :         309049 :     return false;
                               3604                 :                : }
                               3605                 :                : 
                               3606                 :                : /*
                               3607                 :                :  * get_eclass_indexes_for_relids
                               3608                 :                :  *      Build and return a Bitmapset containing the indexes into root's
                               3609                 :                :  *      eq_classes list for all eclasses that mention any of these relids
                               3610                 :                :  */
                               3611                 :                : static Bitmapset *
 2429 drowley@postgresql.o     3612                 :         681985 : get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
                               3613                 :                : {
                               3614                 :         681985 :     Bitmapset  *ec_indexes = NULL;
                               3615                 :         681985 :     int         i = -1;
                               3616                 :                : 
                               3617                 :                :     /* Should be OK to rely on eclass_indexes */
                               3618         [ -  + ]:         681985 :     Assert(root->ec_merging_done);
                               3619                 :                : 
                               3620         [ +  + ]:        2171239 :     while ((i = bms_next_member(relids, i)) > 0)
                               3621                 :                :     {
                               3622                 :        1489254 :         RelOptInfo *rel = root->simple_rel_array[i];
                               3623                 :                : 
                               3624                 :                :         /* ignore the RTE_GROUP RTE */
  551 rguo@postgresql.org      3625         [ -  + ]:        1489254 :         if (i == root->group_rtindex)
  551 rguo@postgresql.org      3626                 :UBC           0 :             continue;
                               3627                 :                : 
 1140 tgl@sss.pgh.pa.us        3628         [ +  + ]:CBC     1489254 :         if (rel == NULL)        /* must be an outer join */
                               3629                 :                :         {
                               3630         [ -  + ]:         200453 :             Assert(bms_is_member(i, root->outer_join_rels));
                               3631                 :         200453 :             continue;
                               3632                 :                :         }
                               3633                 :                : 
 2429 drowley@postgresql.o     3634                 :        1288801 :         ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
                               3635                 :                :     }
                               3636                 :         681985 :     return ec_indexes;
                               3637                 :                : }
                               3638                 :                : 
                               3639                 :                : /*
                               3640                 :                :  * get_common_eclass_indexes
                               3641                 :                :  *      Build and return a Bitmapset containing the indexes into root's
                               3642                 :                :  *      eq_classes list for all eclasses that mention rels in both
                               3643                 :                :  *      relids1 and relids2.
                               3644                 :                :  */
                               3645                 :                : static Bitmapset *
                               3646                 :         398090 : get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
                               3647                 :                : {
                               3648                 :                :     Bitmapset  *rel1ecs;
                               3649                 :                :     Bitmapset  *rel2ecs;
                               3650                 :                :     int         relid;
                               3651                 :                : 
                               3652                 :         398090 :     rel1ecs = get_eclass_indexes_for_relids(root, relids1);
                               3653                 :                : 
                               3654                 :                :     /*
                               3655                 :                :      * We can get away with just using the relation's eclass_indexes directly
                               3656                 :                :      * when relids2 is a singleton set.
                               3657                 :                :      */
                               3658         [ +  + ]:         398090 :     if (bms_get_singleton_member(relids2, &relid))
                               3659                 :         309684 :         rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
                               3660                 :                :     else
                               3661                 :          88406 :         rel2ecs = get_eclass_indexes_for_relids(root, relids2);
                               3662                 :                : 
                               3663                 :                :     /* Calculate and return the common EC indexes, recycling the left input. */
                               3664                 :         398090 :     return bms_int_members(rel1ecs, rel2ecs);
                               3665                 :                : }
                               3666                 :                : 
                               3667                 :                : /*
                               3668                 :                :  * ec_build_derives_hash
                               3669                 :                :  *    Construct the auxiliary hash table for derived clause lookups.
                               3670                 :                :  */
                               3671                 :                : static void
  345 amitlan@postgresql.o     3672                 :UBC           0 : ec_build_derives_hash(PlannerInfo *root, EquivalenceClass *ec)
                               3673                 :                : {
                               3674         [ #  # ]:              0 :     Assert(!ec->ec_derives_hash);
                               3675                 :                : 
                               3676                 :                :     /*
                               3677                 :                :      * Create the hash table.
                               3678                 :                :      *
                               3679                 :                :      * We pass list_length(ec->ec_derives_list) as the initial size.
                               3680                 :                :      * Simplehash will divide this by the fillfactor (typically 0.9) and round
                               3681                 :                :      * up to the next power of two, so this will usually give us at least 64
                               3682                 :                :      * buckets around the threshold. That avoids immediate resizing without
                               3683                 :                :      * hardcoding a specific size.
                               3684                 :                :      */
                               3685                 :              0 :     ec->ec_derives_hash = derives_create(root->planner_cxt,
                               3686                 :              0 :                                          list_length(ec->ec_derives_list),
                               3687                 :                :                                          NULL);
                               3688                 :                : 
                               3689   [ #  #  #  #  :              0 :     foreach_node(RestrictInfo, rinfo, ec->ec_derives_list)
                                              #  # ]
                               3690                 :              0 :         ec_add_clause_to_derives_hash(ec, rinfo);
                               3691                 :              0 : }
                               3692                 :                : 
                               3693                 :                : /*
                               3694                 :                :  * ec_add_derived_clause
                               3695                 :                :  *      Add a clause to the set of derived clauses for the given
                               3696                 :                :  *      EquivalenceClass. Always appends to ec_derives_list; also adds
                               3697                 :                :  *      to ec_derives_hash if it exists.
                               3698                 :                :  *
                               3699                 :                :  * Also asserts expected invariants of derived clauses.
                               3700                 :                :  */
                               3701                 :                : static void
  345 amitlan@postgresql.o     3702                 :CBC       69135 : ec_add_derived_clause(EquivalenceClass *ec, RestrictInfo *clause)
                               3703                 :                : {
                               3704                 :                :     /*
                               3705                 :                :      * Constant, if present, is always placed on the RHS; see
                               3706                 :                :      * generate_base_implied_equalities_const(). LHS is never a constant.
                               3707                 :                :      */
                               3708         [ -  + ]:          69135 :     Assert(!clause->left_em->em_is_const);
                               3709                 :                : 
                               3710                 :                :     /*
                               3711                 :                :      * Clauses containing a constant are never considered redundant, so
                               3712                 :                :      * parent_ec is not set.
                               3713                 :                :      */
                               3714   [ +  +  -  + ]:          69135 :     Assert(!clause->parent_ec || !clause->right_em->em_is_const);
                               3715                 :                : 
                               3716                 :          69135 :     ec->ec_derives_list = lappend(ec->ec_derives_list, clause);
                               3717         [ -  + ]:          69135 :     if (ec->ec_derives_hash)
  345 amitlan@postgresql.o     3718                 :UBC           0 :         ec_add_clause_to_derives_hash(ec, clause);
  345 amitlan@postgresql.o     3719                 :CBC       69135 : }
                               3720                 :                : 
                               3721                 :                : /*
                               3722                 :                :  * ec_add_derived_clauses
                               3723                 :                :  *      Add a list of clauses to the set of clauses derived from the given
                               3724                 :                :  *      EquivalenceClass; adding to the list and hash table if needed.
                               3725                 :                :  *
                               3726                 :                :  * This function is similar to ec_add_derived_clause() but optimized for adding
                               3727                 :                :  * multiple clauses at a time to the ec_derives_list.  The assertions from
                               3728                 :                :  * ec_add_derived_clause() are not repeated here, as the input clauses are
                               3729                 :                :  * assumed to have already been validated.
                               3730                 :                :  */
                               3731                 :                : static void
                               3732                 :             21 : ec_add_derived_clauses(EquivalenceClass *ec, List *clauses)
                               3733                 :                : {
                               3734                 :             21 :     ec->ec_derives_list = list_concat(ec->ec_derives_list, clauses);
                               3735         [ -  + ]:             21 :     if (ec->ec_derives_hash)
  345 amitlan@postgresql.o     3736   [ #  #  #  #  :UBC           0 :         foreach_node(RestrictInfo, rinfo, clauses)
                                              #  # ]
                               3737                 :              0 :             ec_add_clause_to_derives_hash(ec, rinfo);
  345 amitlan@postgresql.o     3738                 :CBC          21 : }
                               3739                 :                : 
                               3740                 :                : /*
                               3741                 :                :  * fill_ec_derives_key
                               3742                 :                :  *      Compute a canonical key for ec_derives_hash lookup or insertion.
                               3743                 :                :  *
                               3744                 :                :  * Derived clauses are looked up using a pair of EquivalenceMembers and a
                               3745                 :                :  * parent EquivalenceClass. To avoid storing or searching for both EM orderings,
                               3746                 :                :  * we canonicalize the key:
                               3747                 :                :  *
                               3748                 :                :  * - For clauses involving two non-constant EMs, em1 is set to the EM with lower
                               3749                 :                :  *   memory address and em2 is set to the other one.
                               3750                 :                :  * - For clauses involving a constant EM, the caller must pass the non-constant
                               3751                 :                :  *   EM as leftem and NULL as rightem; we then set em1 = NULL and em2 = leftem.
                               3752                 :                :  */
                               3753                 :                : static inline void
  345 amitlan@postgresql.o     3754                 :UBC           0 : fill_ec_derives_key(ECDerivesKey *key,
                               3755                 :                :                     EquivalenceMember *leftem,
                               3756                 :                :                     EquivalenceMember *rightem,
                               3757                 :                :                     EquivalenceClass *parent_ec)
                               3758                 :                : {
                               3759         [ #  # ]:              0 :     Assert(leftem);             /* Always required for lookup or insertion */
                               3760                 :                : 
                               3761         [ #  # ]:              0 :     if (rightem == NULL)
                               3762                 :                :     {
                               3763                 :              0 :         key->em1 = NULL;
                               3764                 :              0 :         key->em2 = leftem;
                               3765                 :                :     }
                               3766         [ #  # ]:              0 :     else if (leftem < rightem)
                               3767                 :                :     {
                               3768                 :              0 :         key->em1 = leftem;
                               3769                 :              0 :         key->em2 = rightem;
                               3770                 :                :     }
                               3771                 :                :     else
                               3772                 :                :     {
                               3773                 :              0 :         key->em1 = rightem;
                               3774                 :              0 :         key->em2 = leftem;
                               3775                 :                :     }
                               3776                 :              0 :     key->parent_ec = parent_ec;
                               3777                 :              0 : }
                               3778                 :                : 
                               3779                 :                : /*
                               3780                 :                :  * ec_add_clause_to_derives_hash
                               3781                 :                :  *      Add a derived clause to ec_derives_hash in the given EquivalenceClass.
                               3782                 :                :  *
                               3783                 :                :  * Each clause is associated with a canonicalized key. For constant-containing
                               3784                 :                :  * clauses, only the non-constant EM is used for lookup; see comments in
                               3785                 :                :  * fill_ec_derives_key().
                               3786                 :                :  */
                               3787                 :                : static void
                               3788                 :              0 : ec_add_clause_to_derives_hash(EquivalenceClass *ec, RestrictInfo *rinfo)
                               3789                 :                : {
                               3790                 :                :     ECDerivesKey key;
                               3791                 :                :     ECDerivesEntry *entry;
                               3792                 :                :     bool        found;
                               3793                 :                : 
                               3794                 :                :     /*
                               3795                 :                :      * Constants are always placed on the RHS; see
                               3796                 :                :      * generate_base_implied_equalities_const().
                               3797                 :                :      */
                               3798         [ #  # ]:              0 :     Assert(!rinfo->left_em->em_is_const);
                               3799                 :                : 
                               3800                 :                :     /*
                               3801                 :                :      * Clauses containing a constant are never considered redundant, so
                               3802                 :                :      * parent_ec is not set.
                               3803                 :                :      */
                               3804   [ #  #  #  # ]:              0 :     Assert(!rinfo->parent_ec || !rinfo->right_em->em_is_const);
                               3805                 :                : 
                               3806                 :                :     /*
                               3807                 :                :      * See fill_ec_derives_key() for details: we use a canonicalized key to
                               3808                 :                :      * avoid storing both EM orderings. For constant EMs, only the
                               3809                 :                :      * non-constant EM is included in the key.
                               3810                 :                :      */
                               3811                 :              0 :     fill_ec_derives_key(&key,
                               3812                 :                :                         rinfo->left_em,
                               3813         [ #  # ]:              0 :                         rinfo->right_em->em_is_const ? NULL : rinfo->right_em,
                               3814                 :                :                         rinfo->parent_ec);
                               3815                 :              0 :     entry = derives_insert(ec->ec_derives_hash, key, &found);
                               3816         [ #  # ]:              0 :     Assert(!found);
                               3817                 :              0 :     entry->rinfo = rinfo;
                               3818                 :              0 : }
                               3819                 :                : 
                               3820                 :                : /*
                               3821                 :                :  * ec_clear_derived_clauses
                               3822                 :                :  *      Reset ec_derives_list and ec_derives_hash.
                               3823                 :                :  *
                               3824                 :                :  * We destroy the hash table explicitly, since it may consume significant
                               3825                 :                :  * space. The list holds the same set of entries and can become equally large
                               3826                 :                :  * when thousands of partitions are involved, so we free it as well -- even
                               3827                 :                :  * though we do not typically free lists.
                               3828                 :                :  */
                               3829                 :                : void
  345 amitlan@postgresql.o     3830                 :CBC        9806 : ec_clear_derived_clauses(EquivalenceClass *ec)
                               3831                 :                : {
                               3832                 :           9806 :     list_free(ec->ec_derives_list);
                               3833                 :           9806 :     ec->ec_derives_list = NIL;
                               3834                 :                : 
                               3835         [ -  + ]:           9806 :     if (ec->ec_derives_hash)
                               3836                 :                :     {
  345 amitlan@postgresql.o     3837                 :UBC           0 :         derives_destroy(ec->ec_derives_hash);
                               3838                 :              0 :         ec->ec_derives_hash = NULL;
                               3839                 :                :     }
  345 amitlan@postgresql.o     3840                 :CBC        9806 : }
                               3841                 :                : 
                               3842                 :                : /*
                               3843                 :                :  * ec_search_clause_for_ems
                               3844                 :                :  *      Search for an existing RestrictInfo that equates the given pair
                               3845                 :                :  *      of EquivalenceMembers, either from ec_sources or ec_derives.
                               3846                 :                :  *
                               3847                 :                :  * Returns a clause with matching operands in either given order or commuted
                               3848                 :                :  * order. We used to require matching operator OIDs, but dropped that since any
                               3849                 :                :  * semantically different operator here would indicate a broken operator family.
                               3850                 :                :  *
                               3851                 :                :  * Returns NULL if no matching clause is found.
                               3852                 :                :  */
                               3853                 :                : static RestrictInfo *
                               3854                 :         301777 : ec_search_clause_for_ems(PlannerInfo *root, EquivalenceClass *ec,
                               3855                 :                :                          EquivalenceMember *leftem, EquivalenceMember *rightem,
                               3856                 :                :                          EquivalenceClass *parent_ec)
                               3857                 :                : {
                               3858                 :                :     /* Check original source clauses */
                               3859   [ +  -  +  +  :         955712 :     foreach_node(RestrictInfo, rinfo, ec->ec_sources)
                                              +  + ]
                               3860                 :                :     {
                               3861         [ +  + ]:         353820 :         if (rinfo->left_em == leftem &&
                               3862         [ +  + ]:         162733 :             rinfo->right_em == rightem &&
                               3863         [ +  + ]:         144746 :             rinfo->parent_ec == parent_ec)
                               3864                 :            831 :             return rinfo;
                               3865         [ +  + ]:         353766 :         if (rinfo->left_em == rightem &&
                               3866         [ +  + ]:         155113 :             rinfo->right_em == leftem &&
                               3867         [ +  + ]:         140302 :             rinfo->parent_ec == parent_ec)
                               3868                 :            777 :             return rinfo;
                               3869                 :                :     }
                               3870                 :                : 
                               3871                 :                :     /* Not found in ec_sources; search derived clauses */
                               3872                 :         300946 :     return ec_search_derived_clause_for_ems(root, ec, leftem, rightem,
                               3873                 :                :                                             parent_ec);
                               3874                 :                : }
                               3875                 :                : 
                               3876                 :                : /*
                               3877                 :                :  * ec_search_derived_clause_for_ems
                               3878                 :                :  *      Search for an existing derived clause between two EquivalenceMembers.
                               3879                 :                :  *
                               3880                 :                :  * If the number of derived clauses exceeds a threshold, switch to hash table
                               3881                 :                :  * lookup; otherwise, scan ec_derives_list linearly.
                               3882                 :                :  *
                               3883                 :                :  * Clauses involving constants are looked up by passing the non-constant EM
                               3884                 :                :  * as leftem and setting rightem to NULL. In that case, we expect to find a
                               3885                 :                :  * clause with a constant on the RHS.
                               3886                 :                :  *
                               3887                 :                :  * While searching the list, we compare each given EM with both sides of each
                               3888                 :                :  * clause. But for hash table lookups, we construct a canonicalized key and
                               3889                 :                :  * perform a single lookup.
                               3890                 :                :  */
                               3891                 :                : static RestrictInfo *
                               3892                 :         300949 : ec_search_derived_clause_for_ems(PlannerInfo *root, EquivalenceClass *ec,
                               3893                 :                :                                  EquivalenceMember *leftem,
                               3894                 :                :                                  EquivalenceMember *rightem,
                               3895                 :                :                                  EquivalenceClass *parent_ec)
                               3896                 :                : {
                               3897                 :                :     /* Switch to using hash lookup when list grows "too long". */
                               3898   [ +  -  -  + ]:         601898 :     if (!ec->ec_derives_hash &&
                               3899                 :         300949 :         list_length(ec->ec_derives_list) >= EC_DERIVES_HASH_THRESHOLD)
  345 amitlan@postgresql.o     3900                 :UBC           0 :         ec_build_derives_hash(root, ec);
                               3901                 :                : 
                               3902                 :                :     /* Perform hash table lookup if available */
  345 amitlan@postgresql.o     3903         [ -  + ]:CBC      300949 :     if (ec->ec_derives_hash)
                               3904                 :                :     {
                               3905                 :                :         ECDerivesKey key;
                               3906                 :                :         RestrictInfo *rinfo;
                               3907                 :                :         ECDerivesEntry *entry;
                               3908                 :                : 
  345 amitlan@postgresql.o     3909                 :UBC           0 :         fill_ec_derives_key(&key, leftem, rightem, parent_ec);
                               3910                 :              0 :         entry = derives_lookup(ec->ec_derives_hash, key);
                               3911         [ #  # ]:              0 :         if (entry)
                               3912                 :                :         {
                               3913                 :              0 :             rinfo = entry->rinfo;
                               3914         [ #  # ]:              0 :             Assert(rinfo);
                               3915   [ #  #  #  # ]:              0 :             Assert(rightem || rinfo->right_em->em_is_const);
                               3916                 :              0 :             return rinfo;
                               3917                 :                :         }
                               3918                 :                :     }
                               3919                 :                :     else
                               3920                 :                :     {
                               3921                 :                :         /* Fallback to linear search over ec_derives_list */
  345 amitlan@postgresql.o     3922   [ +  +  +  +  :CBC      424434 :         foreach_node(RestrictInfo, rinfo, ec->ec_derives_list)
                                              +  + ]
                               3923                 :                :         {
                               3924                 :                :             /* Handle special case: lookup by non-const EM alone */
                               3925         [ +  + ]:         323816 :             if (!rightem &&
                               3926         [ +  - ]:              3 :                 rinfo->left_em == leftem)
                               3927                 :                :             {
                               3928         [ -  + ]:              3 :                 Assert(rinfo->right_em->em_is_const);
                               3929                 :         250640 :                 return rinfo;
                               3930                 :                :             }
                               3931         [ +  + ]:         323813 :             if (rinfo->left_em == leftem &&
                               3932         [ +  + ]:         135682 :                 rinfo->right_em == rightem &&
                               3933         [ +  + ]:         123481 :                 rinfo->parent_ec == parent_ec)
                               3934                 :         123475 :                 return rinfo;
                               3935         [ +  + ]:         200338 :             if (rinfo->left_em == rightem &&
                               3936         [ +  + ]:         134112 :                 rinfo->right_em == leftem &&
                               3937         [ +  - ]:         127162 :                 rinfo->parent_ec == parent_ec)
                               3938                 :         127162 :                 return rinfo;
                               3939                 :                :         }
                               3940                 :                :     }
                               3941                 :                : 
                               3942                 :          50309 :     return NULL;
                               3943                 :                : }
        

Generated by: LCOV version 2.4-beta