LCOV - code coverage report
Current view: top level - src/backend/optimizer/path - equivclass.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 820 867 94.6 %
Date: 2020-06-03 09:06:53 Functions: 30 30 100.0 %
Legend: Lines: hit not hit

          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-2020, 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 "nodes/makefuncs.h"
      24             : #include "nodes/nodeFuncs.h"
      25             : #include "optimizer/appendinfo.h"
      26             : #include "optimizer/clauses.h"
      27             : #include "optimizer/optimizer.h"
      28             : #include "optimizer/pathnode.h"
      29             : #include "optimizer/paths.h"
      30             : #include "optimizer/planmain.h"
      31             : #include "optimizer/restrictinfo.h"
      32             : #include "utils/lsyscache.h"
      33             : 
      34             : 
      35             : static EquivalenceMember *add_eq_member(EquivalenceClass *ec,
      36             :                                         Expr *expr, Relids relids, Relids nullable_relids,
      37             :                                         bool is_child, Oid datatype);
      38             : static void generate_base_implied_equalities_const(PlannerInfo *root,
      39             :                                                    EquivalenceClass *ec);
      40             : static void generate_base_implied_equalities_no_const(PlannerInfo *root,
      41             :                                                       EquivalenceClass *ec);
      42             : static void generate_base_implied_equalities_broken(PlannerInfo *root,
      43             :                                                     EquivalenceClass *ec);
      44             : static List *generate_join_implied_equalities_normal(PlannerInfo *root,
      45             :                                                      EquivalenceClass *ec,
      46             :                                                      Relids join_relids,
      47             :                                                      Relids outer_relids,
      48             :                                                      Relids inner_relids);
      49             : static List *generate_join_implied_equalities_broken(PlannerInfo *root,
      50             :                                                      EquivalenceClass *ec,
      51             :                                                      Relids nominal_join_relids,
      52             :                                                      Relids outer_relids,
      53             :                                                      Relids nominal_inner_relids,
      54             :                                                      RelOptInfo *inner_rel);
      55             : static Oid  select_equality_operator(EquivalenceClass *ec,
      56             :                                      Oid lefttype, Oid righttype);
      57             : static RestrictInfo *create_join_clause(PlannerInfo *root,
      58             :                                         EquivalenceClass *ec, Oid opno,
      59             :                                         EquivalenceMember *leftem,
      60             :                                         EquivalenceMember *rightem,
      61             :                                         EquivalenceClass *parent_ec);
      62             : static bool reconsider_outer_join_clause(PlannerInfo *root,
      63             :                                          RestrictInfo *rinfo,
      64             :                                          bool outer_on_left);
      65             : static bool reconsider_full_join_clause(PlannerInfo *root,
      66             :                                         RestrictInfo *rinfo);
      67             : static Bitmapset *get_eclass_indexes_for_relids(PlannerInfo *root,
      68             :                                                 Relids relids);
      69             : static Bitmapset *get_common_eclass_indexes(PlannerInfo *root, Relids relids1,
      70             :                                             Relids relids2);
      71             : 
      72             : 
      73             : /*
      74             :  * process_equivalence
      75             :  *    The given clause has a mergejoinable operator and can be applied without
      76             :  *    any delay by an outer join, so its two sides can be considered equal
      77             :  *    anywhere they are both computable; moreover that equality can be
      78             :  *    extended transitively.  Record this knowledge in the EquivalenceClass
      79             :  *    data structure, if applicable.  Returns true if successful, false if not
      80             :  *    (in which case caller should treat the clause as ordinary, not an
      81             :  *    equivalence).
      82             :  *
      83             :  * In some cases, although we cannot convert a clause into EquivalenceClass
      84             :  * knowledge, we can still modify it to a more useful form than the original.
      85             :  * Then, *p_restrictinfo will be replaced by a new RestrictInfo, which is what
      86             :  * the caller should use for further processing.
      87             :  *
      88             :  * If below_outer_join is true, then the clause was found below the nullable
      89             :  * side of an outer join, so its sides might validly be both NULL rather than
      90             :  * strictly equal.  We can still deduce equalities in such cases, but we take
      91             :  * care to mark an EquivalenceClass if it came from any such clauses.  Also,
      92             :  * we have to check that both sides are either pseudo-constants or strict
      93             :  * functions of Vars, else they might not both go to NULL above the outer
      94             :  * join.  (This is the main reason why we need a failure return.  It's more
      95             :  * convenient to check this case here than at the call sites...)
      96             :  *
      97             :  * We also reject proposed equivalence clauses if they contain leaky functions
      98             :  * and have security_level above zero.  The EC evaluation rules require us to
      99             :  * apply certain tests at certain joining levels, and we can't tolerate
     100             :  * delaying any test on security_level grounds.  By rejecting candidate clauses
     101             :  * that might require security delays, we ensure it's safe to apply an EC
     102             :  * clause as soon as it's supposed to be applied.
     103             :  *
     104             :  * On success return, we have also initialized the clause's left_ec/right_ec
     105             :  * fields to point to the EquivalenceClass representing it.  This saves lookup
     106             :  * effort later.
     107             :  *
     108             :  * Note: constructing merged EquivalenceClasses is a standard UNION-FIND
     109             :  * problem, for which there exist better data structures than simple lists.
     110             :  * If this code ever proves to be a bottleneck then it could be sped up ---
     111             :  * but for now, simple is beautiful.
     112             :  *
     113             :  * Note: this is only called during planner startup, not during GEQO
     114             :  * exploration, so we need not worry about whether we're in the right
     115             :  * memory context.
     116             :  */
     117             : bool
     118      170714 : process_equivalence(PlannerInfo *root,
     119             :                     RestrictInfo **p_restrictinfo,
     120             :                     bool below_outer_join)
     121             : {
     122      170714 :     RestrictInfo *restrictinfo = *p_restrictinfo;
     123      170714 :     Expr       *clause = restrictinfo->clause;
     124             :     Oid         opno,
     125             :                 collation,
     126             :                 item1_type,
     127             :                 item2_type;
     128             :     Expr       *item1;
     129             :     Expr       *item2;
     130             :     Relids      item1_relids,
     131             :                 item2_relids,
     132             :                 item1_nullable_relids,
     133             :                 item2_nullable_relids;
     134             :     List       *opfamilies;
     135             :     EquivalenceClass *ec1,
     136             :                *ec2;
     137             :     EquivalenceMember *em1,
     138             :                *em2;
     139             :     ListCell   *lc1;
     140             : 
     141             :     /* Should not already be marked as having generated an eclass */
     142             :     Assert(restrictinfo->left_ec == NULL);
     143             :     Assert(restrictinfo->right_ec == NULL);
     144             : 
     145             :     /* Reject if it is potentially postponable by security considerations */
     146      170714 :     if (restrictinfo->security_level > 0 && !restrictinfo->leakproof)
     147         120 :         return false;
     148             : 
     149             :     /* Extract info from given clause */
     150             :     Assert(is_opclause(clause));
     151      170594 :     opno = ((OpExpr *) clause)->opno;
     152      170594 :     collation = ((OpExpr *) clause)->inputcollid;
     153      170594 :     item1 = (Expr *) get_leftop(clause);
     154      170594 :     item2 = (Expr *) get_rightop(clause);
     155      170594 :     item1_relids = restrictinfo->left_relids;
     156      170594 :     item2_relids = restrictinfo->right_relids;
     157             : 
     158             :     /*
     159             :      * Ensure both input expressions expose the desired collation (their types
     160             :      * should be OK already); see comments for canonicalize_ec_expression.
     161             :      */
     162      170594 :     item1 = canonicalize_ec_expression(item1,
     163             :                                        exprType((Node *) item1),
     164             :                                        collation);
     165      170594 :     item2 = canonicalize_ec_expression(item2,
     166             :                                        exprType((Node *) item2),
     167             :                                        collation);
     168             : 
     169             :     /*
     170             :      * Clauses of the form X=X cannot be translated into EquivalenceClasses.
     171             :      * We'd either end up with a single-entry EC, losing the knowledge that
     172             :      * the clause was present at all, or else make an EC with duplicate
     173             :      * entries, causing other issues.
     174             :      */
     175      170594 :     if (equal(item1, item2))
     176             :     {
     177             :         /*
     178             :          * If the operator is strict, then the clause can be treated as just
     179             :          * "X IS NOT NULL".  (Since we know we are considering a top-level
     180             :          * qual, we can ignore the difference between FALSE and NULL results.)
     181             :          * It's worth making the conversion because we'll typically get a much
     182             :          * better selectivity estimate than we would for X=X.
     183             :          *
     184             :          * If the operator is not strict, we can't be sure what it will do
     185             :          * with NULLs, so don't attempt to optimize it.
     186             :          */
     187          28 :         set_opfuncid((OpExpr *) clause);
     188          28 :         if (func_strict(((OpExpr *) clause)->opfuncid))
     189             :         {
     190          28 :             NullTest   *ntest = makeNode(NullTest);
     191             : 
     192          28 :             ntest->arg = item1;
     193          28 :             ntest->nulltesttype = IS_NOT_NULL;
     194          28 :             ntest->argisrow = false; /* correct even if composite arg */
     195          28 :             ntest->location = -1;
     196             : 
     197          28 :             *p_restrictinfo =
     198         112 :                 make_restrictinfo((Expr *) ntest,
     199          28 :                                   restrictinfo->is_pushed_down,
     200          28 :                                   restrictinfo->outerjoin_delayed,
     201          28 :                                   restrictinfo->pseudoconstant,
     202             :                                   restrictinfo->security_level,
     203             :                                   NULL,
     204             :                                   restrictinfo->outer_relids,
     205             :                                   restrictinfo->nullable_relids);
     206             :         }
     207          28 :         return false;
     208             :     }
     209             : 
     210             :     /*
     211             :      * If below outer join, check for strictness, else reject.
     212             :      */
     213      170566 :     if (below_outer_join)
     214             :     {
     215       45914 :         if (!bms_is_empty(item1_relids) &&
     216       22872 :             contain_nonstrict_functions((Node *) item1))
     217           0 :             return false;       /* LHS is non-strict but not constant */
     218       24038 :         if (!bms_is_empty(item2_relids) &&
     219         996 :             contain_nonstrict_functions((Node *) item2))
     220           0 :             return false;       /* RHS is non-strict but not constant */
     221             :     }
     222             : 
     223             :     /* Calculate nullable-relid sets for each side of the clause */
     224      170566 :     item1_nullable_relids = bms_intersect(item1_relids,
     225      170566 :                                           restrictinfo->nullable_relids);
     226      170566 :     item2_nullable_relids = bms_intersect(item2_relids,
     227      170566 :                                           restrictinfo->nullable_relids);
     228             : 
     229             :     /*
     230             :      * We use the declared input types of the operator, not exprType() of the
     231             :      * inputs, as the nominal datatypes for opfamily lookup.  This presumes
     232             :      * that btree operators are always registered with amoplefttype and
     233             :      * amoprighttype equal to their declared input types.  We will need this
     234             :      * info anyway to build EquivalenceMember nodes, and by extracting it now
     235             :      * we can use type comparisons to short-circuit some equal() tests.
     236             :      */
     237      170566 :     op_input_types(opno, &item1_type, &item2_type);
     238             : 
     239      170566 :     opfamilies = restrictinfo->mergeopfamilies;
     240             : 
     241             :     /*
     242             :      * Sweep through the existing EquivalenceClasses looking for matches to
     243             :      * item1 and item2.  These are the possible outcomes:
     244             :      *
     245             :      * 1. We find both in the same EC.  The equivalence is already known, so
     246             :      * there's nothing to do.
     247             :      *
     248             :      * 2. We find both in different ECs.  Merge the two ECs together.
     249             :      *
     250             :      * 3. We find just one.  Add the other to its EC.
     251             :      *
     252             :      * 4. We find neither.  Make a new, two-entry EC.
     253             :      *
     254             :      * Note: since all ECs are built through this process or the similar
     255             :      * search in get_eclass_for_sort_expr(), it's impossible that we'd match
     256             :      * an item in more than one existing nonvolatile EC.  So it's okay to stop
     257             :      * at the first match.
     258             :      */
     259      170566 :     ec1 = ec2 = NULL;
     260      170566 :     em1 = em2 = NULL;
     261      352130 :     foreach(lc1, root->eq_classes)
     262             :     {
     263      181984 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
     264             :         ListCell   *lc2;
     265             : 
     266             :         /* Never match to a volatile EC */
     267      181984 :         if (cur_ec->ec_has_volatile)
     268           0 :             continue;
     269             : 
     270             :         /*
     271             :          * The collation has to match; check this first since it's cheaper
     272             :          * than the opfamily comparison.
     273             :          */
     274      181984 :         if (collation != cur_ec->ec_collation)
     275        8204 :             continue;
     276             : 
     277             :         /*
     278             :          * A "match" requires matching sets of btree opfamilies.  Use of
     279             :          * equal() for this test has implications discussed in the comments
     280             :          * for get_mergejoin_opfamilies().
     281             :          */
     282      173780 :         if (!equal(opfamilies, cur_ec->ec_opfamilies))
     283       28914 :             continue;
     284             : 
     285      351862 :         foreach(lc2, cur_ec->ec_members)
     286             :         {
     287      207416 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
     288             : 
     289             :             Assert(!cur_em->em_is_child);    /* no children yet */
     290             : 
     291             :             /*
     292             :              * If below an outer join, don't match constants: they're not as
     293             :              * constant as they look.
     294             :              */
     295      207416 :             if ((below_outer_join || cur_ec->ec_below_outer_join) &&
     296       82496 :                 cur_em->em_is_const)
     297       15554 :                 continue;
     298             : 
     299      191862 :             if (!ec1 &&
     300      335388 :                 item1_type == cur_em->em_datatype &&
     301      167624 :                 equal(item1, cur_em->em_expr))
     302             :             {
     303       13820 :                 ec1 = cur_ec;
     304       13820 :                 em1 = cur_em;
     305       13820 :                 if (ec2)
     306         416 :                     break;
     307             :             }
     308             : 
     309      191446 :             if (!ec2 &&
     310      378868 :                 item2_type == cur_em->em_datatype &&
     311      189318 :                 equal(item2, cur_em->em_expr))
     312             :             {
     313        1982 :                 ec2 = cur_ec;
     314        1982 :                 em2 = cur_em;
     315        1982 :                 if (ec1)
     316           4 :                     break;
     317             :             }
     318             :         }
     319             : 
     320      144866 :         if (ec1 && ec2)
     321         420 :             break;
     322             :     }
     323             : 
     324             :     /* Sweep finished, what did we find? */
     325             : 
     326      170566 :     if (ec1 && ec2)
     327             :     {
     328             :         /* If case 1, nothing to do, except add to sources */
     329         420 :         if (ec1 == ec2)
     330             :         {
     331           0 :             ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
     332           0 :             ec1->ec_below_outer_join |= below_outer_join;
     333           0 :             ec1->ec_min_security = Min(ec1->ec_min_security,
     334             :                                        restrictinfo->security_level);
     335           0 :             ec1->ec_max_security = Max(ec1->ec_max_security,
     336             :                                        restrictinfo->security_level);
     337             :             /* mark the RI as associated with this eclass */
     338           0 :             restrictinfo->left_ec = ec1;
     339           0 :             restrictinfo->right_ec = ec1;
     340             :             /* mark the RI as usable with this pair of EMs */
     341           0 :             restrictinfo->left_em = em1;
     342           0 :             restrictinfo->right_em = em2;
     343           0 :             return true;
     344             :         }
     345             : 
     346             :         /*
     347             :          * Case 2: need to merge ec1 and ec2.  This should never happen after
     348             :          * the ECs have reached canonical state; otherwise, pathkeys could be
     349             :          * rendered non-canonical by the merge, and relation eclass indexes
     350             :          * would get broken by removal of an eq_classes list entry.
     351             :          */
     352         420 :         if (root->ec_merging_done)
     353           0 :             elog(ERROR, "too late to merge equivalence classes");
     354             : 
     355             :         /*
     356             :          * We add ec2's items to ec1, then set ec2's ec_merged link to point
     357             :          * to ec1 and remove ec2 from the eq_classes list.  We cannot simply
     358             :          * delete ec2 because that could leave dangling pointers in existing
     359             :          * PathKeys.  We leave it behind with a link so that the merged EC can
     360             :          * be found.
     361             :          */
     362         420 :         ec1->ec_members = list_concat(ec1->ec_members, ec2->ec_members);
     363         420 :         ec1->ec_sources = list_concat(ec1->ec_sources, ec2->ec_sources);
     364         420 :         ec1->ec_derives = list_concat(ec1->ec_derives, ec2->ec_derives);
     365         420 :         ec1->ec_relids = bms_join(ec1->ec_relids, ec2->ec_relids);
     366         420 :         ec1->ec_has_const |= ec2->ec_has_const;
     367             :         /* can't need to set has_volatile */
     368         420 :         ec1->ec_below_outer_join |= ec2->ec_below_outer_join;
     369         420 :         ec1->ec_min_security = Min(ec1->ec_min_security,
     370             :                                    ec2->ec_min_security);
     371         420 :         ec1->ec_max_security = Max(ec1->ec_max_security,
     372             :                                    ec2->ec_max_security);
     373         420 :         ec2->ec_merged = ec1;
     374         420 :         root->eq_classes = list_delete_ptr(root->eq_classes, ec2);
     375             :         /* just to avoid debugging confusion w/ dangling pointers: */
     376         420 :         ec2->ec_members = NIL;
     377         420 :         ec2->ec_sources = NIL;
     378         420 :         ec2->ec_derives = NIL;
     379         420 :         ec2->ec_relids = NULL;
     380         420 :         ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
     381         420 :         ec1->ec_below_outer_join |= below_outer_join;
     382         420 :         ec1->ec_min_security = Min(ec1->ec_min_security,
     383             :                                    restrictinfo->security_level);
     384         420 :         ec1->ec_max_security = Max(ec1->ec_max_security,
     385             :                                    restrictinfo->security_level);
     386             :         /* mark the RI as associated with this eclass */
     387         420 :         restrictinfo->left_ec = ec1;
     388         420 :         restrictinfo->right_ec = ec1;
     389             :         /* mark the RI as usable with this pair of EMs */
     390         420 :         restrictinfo->left_em = em1;
     391         420 :         restrictinfo->right_em = em2;
     392             :     }
     393      170146 :     else if (ec1)
     394             :     {
     395             :         /* Case 3: add item2 to ec1 */
     396       13400 :         em2 = add_eq_member(ec1, item2, item2_relids, item2_nullable_relids,
     397             :                             false, item2_type);
     398       13400 :         ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
     399       13400 :         ec1->ec_below_outer_join |= below_outer_join;
     400       13400 :         ec1->ec_min_security = Min(ec1->ec_min_security,
     401             :                                    restrictinfo->security_level);
     402       13400 :         ec1->ec_max_security = Max(ec1->ec_max_security,
     403             :                                    restrictinfo->security_level);
     404             :         /* mark the RI as associated with this eclass */
     405       13400 :         restrictinfo->left_ec = ec1;
     406       13400 :         restrictinfo->right_ec = ec1;
     407             :         /* mark the RI as usable with this pair of EMs */
     408       13400 :         restrictinfo->left_em = em1;
     409       13400 :         restrictinfo->right_em = em2;
     410             :     }
     411      156746 :     else if (ec2)
     412             :     {
     413             :         /* Case 3: add item1 to ec2 */
     414        1562 :         em1 = add_eq_member(ec2, item1, item1_relids, item1_nullable_relids,
     415             :                             false, item1_type);
     416        1562 :         ec2->ec_sources = lappend(ec2->ec_sources, restrictinfo);
     417        1562 :         ec2->ec_below_outer_join |= below_outer_join;
     418        1562 :         ec2->ec_min_security = Min(ec2->ec_min_security,
     419             :                                    restrictinfo->security_level);
     420        1562 :         ec2->ec_max_security = Max(ec2->ec_max_security,
     421             :                                    restrictinfo->security_level);
     422             :         /* mark the RI as associated with this eclass */
     423        1562 :         restrictinfo->left_ec = ec2;
     424        1562 :         restrictinfo->right_ec = ec2;
     425             :         /* mark the RI as usable with this pair of EMs */
     426        1562 :         restrictinfo->left_em = em1;
     427        1562 :         restrictinfo->right_em = em2;
     428             :     }
     429             :     else
     430             :     {
     431             :         /* Case 4: make a new, two-entry EC */
     432      155184 :         EquivalenceClass *ec = makeNode(EquivalenceClass);
     433             : 
     434      155184 :         ec->ec_opfamilies = opfamilies;
     435      155184 :         ec->ec_collation = collation;
     436      155184 :         ec->ec_members = NIL;
     437      155184 :         ec->ec_sources = list_make1(restrictinfo);
     438      155184 :         ec->ec_derives = NIL;
     439      155184 :         ec->ec_relids = NULL;
     440      155184 :         ec->ec_has_const = false;
     441      155184 :         ec->ec_has_volatile = false;
     442      155184 :         ec->ec_below_outer_join = below_outer_join;
     443      155184 :         ec->ec_broken = false;
     444      155184 :         ec->ec_sortref = 0;
     445      155184 :         ec->ec_min_security = restrictinfo->security_level;
     446      155184 :         ec->ec_max_security = restrictinfo->security_level;
     447      155184 :         ec->ec_merged = NULL;
     448      155184 :         em1 = add_eq_member(ec, item1, item1_relids, item1_nullable_relids,
     449             :                             false, item1_type);
     450      155184 :         em2 = add_eq_member(ec, item2, item2_relids, item2_nullable_relids,
     451             :                             false, item2_type);
     452             : 
     453      155184 :         root->eq_classes = lappend(root->eq_classes, ec);
     454             : 
     455             :         /* mark the RI as associated with this eclass */
     456      155184 :         restrictinfo->left_ec = ec;
     457      155184 :         restrictinfo->right_ec = ec;
     458             :         /* mark the RI as usable with this pair of EMs */
     459      155184 :         restrictinfo->left_em = em1;
     460      155184 :         restrictinfo->right_em = em2;
     461             :     }
     462             : 
     463      170566 :     return true;
     464             : }
     465             : 
     466             : /*
     467             :  * canonicalize_ec_expression
     468             :  *
     469             :  * This function ensures that the expression exposes the expected type and
     470             :  * collation, so that it will be equal() to other equivalence-class expressions
     471             :  * that it ought to be equal() to.
     472             :  *
     473             :  * The rule for datatypes is that the exposed type should match what it would
     474             :  * be for an input to an operator of the EC's opfamilies; which is usually
     475             :  * the declared input type of the operator, but in the case of polymorphic
     476             :  * operators no relabeling is wanted (compare the behavior of parse_coerce.c).
     477             :  * Expressions coming in from quals will generally have the right type
     478             :  * already, but expressions coming from indexkeys may not (because they are
     479             :  * represented without any explicit relabel in pg_index), and the same problem
     480             :  * occurs for sort expressions (because the parser is likewise cavalier about
     481             :  * putting relabels on them).  Such cases will be binary-compatible with the
     482             :  * real operators, so adding a RelabelType is sufficient.
     483             :  *
     484             :  * Also, the expression's exposed collation must match the EC's collation.
     485             :  * This is important because in comparisons like "foo < bar COLLATE baz",
     486             :  * only one of the expressions has the correct exposed collation as we receive
     487             :  * it from the parser.  Forcing both of them to have it ensures that all
     488             :  * variant spellings of such a construct behave the same.  Again, we can
     489             :  * stick on a RelabelType to force the right exposed collation.  (It might
     490             :  * work to not label the collation at all in EC members, but this is risky
     491             :  * since some parts of the system expect exprCollation() to deliver the
     492             :  * right answer for a sort key.)
     493             :  *
     494             :  * Note this code assumes that the expression has already been through
     495             :  * eval_const_expressions, so there are no CollateExprs and no redundant
     496             :  * RelabelTypes.
     497             :  */
     498             : Expr *
     499     1282690 : canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
     500             : {
     501     1282690 :     Oid         expr_type = exprType((Node *) expr);
     502             : 
     503             :     /*
     504             :      * For a polymorphic-input-type opclass, just keep the same exposed type.
     505             :      * RECORD opclasses work like polymorphic-type ones for this purpose.
     506             :      */
     507     1282690 :     if (IsPolymorphicType(req_type) || req_type == RECORDOID)
     508         634 :         req_type = expr_type;
     509             : 
     510             :     /*
     511             :      * No work if the expression exposes the right type/collation already.
     512             :      */
     513     2561428 :     if (expr_type != req_type ||
     514     1278738 :         exprCollation((Node *) expr) != req_collation)
     515             :     {
     516             :         /*
     517             :          * Strip any existing RelabelType, then add a new one if needed. This
     518             :          * is to preserve the invariant of no redundant RelabelTypes.
     519             :          *
     520             :          * If we have to change the exposed type of the stripped expression,
     521             :          * set typmod to -1 (since the new type may not have the same typmod
     522             :          * interpretation).  If we only have to change collation, preserve the
     523             :          * exposed typmod.
     524             :          */
     525        5750 :         while (expr && IsA(expr, RelabelType))
     526        1088 :             expr = (Expr *) ((RelabelType *) expr)->arg;
     527             : 
     528        4662 :         if (exprType((Node *) expr) != req_type)
     529        3294 :             expr = (Expr *) makeRelabelType(expr,
     530             :                                             req_type,
     531             :                                             -1,
     532             :                                             req_collation,
     533             :                                             COERCE_IMPLICIT_CAST);
     534        1368 :         else if (exprCollation((Node *) expr) != req_collation)
     535         720 :             expr = (Expr *) makeRelabelType(expr,
     536             :                                             req_type,
     537             :                                             exprTypmod((Node *) expr),
     538             :                                             req_collation,
     539             :                                             COERCE_IMPLICIT_CAST);
     540             :     }
     541             : 
     542     1282690 :     return expr;
     543             : }
     544             : 
     545             : /*
     546             :  * add_eq_member - build a new EquivalenceMember and add it to an EC
     547             :  */
     548             : static EquivalenceMember *
     549      481624 : add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids,
     550             :               Relids nullable_relids, bool is_child, Oid datatype)
     551             : {
     552      481624 :     EquivalenceMember *em = makeNode(EquivalenceMember);
     553             : 
     554      481624 :     em->em_expr = expr;
     555      481624 :     em->em_relids = relids;
     556      481624 :     em->em_nullable_relids = nullable_relids;
     557      481624 :     em->em_is_const = false;
     558      481624 :     em->em_is_child = is_child;
     559      481624 :     em->em_datatype = datatype;
     560             : 
     561      481624 :     if (bms_is_empty(relids))
     562             :     {
     563             :         /*
     564             :          * No Vars, assume it's a pseudoconstant.  This is correct for entries
     565             :          * generated from process_equivalence(), because a WHERE clause can't
     566             :          * contain aggregates or SRFs, and non-volatility was checked before
     567             :          * process_equivalence() ever got called.  But
     568             :          * get_eclass_for_sort_expr() has to work harder.  We put the tests
     569             :          * there not here to save cycles in the equivalence case.
     570             :          */
     571             :         Assert(!is_child);
     572      141434 :         em->em_is_const = true;
     573      141434 :         ec->ec_has_const = true;
     574             :         /* it can't affect ec_relids */
     575             :     }
     576      340190 :     else if (!is_child)         /* child members don't add to ec_relids */
     577             :     {
     578      312860 :         ec->ec_relids = bms_add_members(ec->ec_relids, relids);
     579             :     }
     580      481624 :     ec->ec_members = lappend(ec->ec_members, em);
     581             : 
     582      481624 :     return em;
     583             : }
     584             : 
     585             : 
     586             : /*
     587             :  * get_eclass_for_sort_expr
     588             :  *    Given an expression and opfamily/collation info, find an existing
     589             :  *    equivalence class it is a member of; if none, optionally build a new
     590             :  *    single-member EquivalenceClass for it.
     591             :  *
     592             :  * expr is the expression, and nullable_relids is the set of base relids
     593             :  * that are potentially nullable below it.  We actually only care about
     594             :  * the set of such relids that are used in the expression; but for caller
     595             :  * convenience, we perform that intersection step here.  The caller need
     596             :  * only be sure that nullable_relids doesn't omit any nullable rels that
     597             :  * might appear in the expr.
     598             :  *
     599             :  * sortref is the SortGroupRef of the originating SortGroupClause, if any,
     600             :  * or zero if not.  (It should never be zero if the expression is volatile!)
     601             :  *
     602             :  * If rel is not NULL, it identifies a specific relation we're considering
     603             :  * a path for, and indicates that child EC members for that relation can be
     604             :  * considered.  Otherwise child members are ignored.  (Note: since child EC
     605             :  * members aren't guaranteed unique, a non-NULL value means that there could
     606             :  * be more than one EC that matches the expression; if so it's order-dependent
     607             :  * which one you get.  This is annoying but it only happens in corner cases,
     608             :  * so for now we live with just reporting the first match.  See also
     609             :  * generate_implied_equalities_for_column and match_pathkeys_to_index.)
     610             :  *
     611             :  * If create_it is true, we'll build a new EquivalenceClass when there is no
     612             :  * match.  If create_it is false, we just return NULL when no match.
     613             :  *
     614             :  * This can be used safely both before and after EquivalenceClass merging;
     615             :  * since it never causes merging it does not invalidate any existing ECs
     616             :  * or PathKeys.  However, ECs added after path generation has begun are
     617             :  * of limited usefulness, so usually it's best to create them beforehand.
     618             :  *
     619             :  * Note: opfamilies must be chosen consistently with the way
     620             :  * process_equivalence() would do; that is, generated from a mergejoinable
     621             :  * equality operator.  Else we might fail to detect valid equivalences,
     622             :  * generating poor (but not incorrect) plans.
     623             :  */
     624             : EquivalenceClass *
     625      938932 : get_eclass_for_sort_expr(PlannerInfo *root,
     626             :                          Expr *expr,
     627             :                          Relids nullable_relids,
     628             :                          List *opfamilies,
     629             :                          Oid opcintype,
     630             :                          Oid collation,
     631             :                          Index sortref,
     632             :                          Relids rel,
     633             :                          bool create_it)
     634             : {
     635             :     Relids      expr_relids;
     636             :     EquivalenceClass *newec;
     637             :     EquivalenceMember *newem;
     638             :     ListCell   *lc1;
     639             :     MemoryContext oldcontext;
     640             : 
     641             :     /*
     642             :      * Ensure the expression exposes the correct type and collation.
     643             :      */
     644      938932 :     expr = canonicalize_ec_expression(expr, opcintype, collation);
     645             : 
     646             :     /*
     647             :      * Get the precise set of nullable relids appearing in the expression.
     648             :      */
     649      938932 :     expr_relids = pull_varnos((Node *) expr);
     650      938932 :     nullable_relids = bms_intersect(nullable_relids, expr_relids);
     651             : 
     652             :     /*
     653             :      * Scan through the existing EquivalenceClasses for a match
     654             :      */
     655     2988558 :     foreach(lc1, root->eq_classes)
     656             :     {
     657     2574730 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
     658             :         ListCell   *lc2;
     659             : 
     660             :         /*
     661             :          * Never match to a volatile EC, except when we are looking at another
     662             :          * reference to the same volatile SortGroupClause.
     663             :          */
     664     2574730 :         if (cur_ec->ec_has_volatile &&
     665          18 :             (sortref == 0 || sortref != cur_ec->ec_sortref))
     666         184 :             continue;
     667             : 
     668     2574546 :         if (collation != cur_ec->ec_collation)
     669      657616 :             continue;
     670     1916930 :         if (!equal(opfamilies, cur_ec->ec_opfamilies))
     671      362688 :             continue;
     672             : 
     673     3399288 :         foreach(lc2, cur_ec->ec_members)
     674             :         {
     675     2370150 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
     676             : 
     677             :             /*
     678             :              * Ignore child members unless they match the request.
     679             :              */
     680     2370150 :             if (cur_em->em_is_child &&
     681      126372 :                 !bms_equal(cur_em->em_relids, rel))
     682      103616 :                 continue;
     683             : 
     684             :             /*
     685             :              * If below an outer join, don't match constants: they're not as
     686             :              * constant as they look.
     687             :              */
     688     2266534 :             if (cur_ec->ec_below_outer_join &&
     689      200006 :                 cur_em->em_is_const)
     690       69474 :                 continue;
     691             : 
     692     4379542 :             if (opcintype == cur_em->em_datatype &&
     693     2182482 :                 equal(expr, cur_em->em_expr))
     694      525104 :                 return cur_ec;  /* Match! */
     695             :         }
     696             :     }
     697             : 
     698             :     /* No match; does caller want a NULL result? */
     699      413828 :     if (!create_it)
     700      284864 :         return NULL;
     701             : 
     702             :     /*
     703             :      * OK, build a new single-member EC
     704             :      *
     705             :      * Here, we must be sure that we construct the EC in the right context.
     706             :      */
     707      128964 :     oldcontext = MemoryContextSwitchTo(root->planner_cxt);
     708             : 
     709      128964 :     newec = makeNode(EquivalenceClass);
     710      128964 :     newec->ec_opfamilies = list_copy(opfamilies);
     711      128964 :     newec->ec_collation = collation;
     712      128964 :     newec->ec_members = NIL;
     713      128964 :     newec->ec_sources = NIL;
     714      128964 :     newec->ec_derives = NIL;
     715      128964 :     newec->ec_relids = NULL;
     716      128964 :     newec->ec_has_const = false;
     717      128964 :     newec->ec_has_volatile = contain_volatile_functions((Node *) expr);
     718      128964 :     newec->ec_below_outer_join = false;
     719      128964 :     newec->ec_broken = false;
     720      128964 :     newec->ec_sortref = sortref;
     721      128964 :     newec->ec_min_security = UINT_MAX;
     722      128964 :     newec->ec_max_security = 0;
     723      128964 :     newec->ec_merged = NULL;
     724             : 
     725      128964 :     if (newec->ec_has_volatile && sortref == 0) /* should not happen */
     726           0 :         elog(ERROR, "volatile EquivalenceClass has no sortref");
     727             : 
     728      128964 :     newem = add_eq_member(newec, copyObject(expr), expr_relids,
     729             :                           nullable_relids, false, opcintype);
     730             : 
     731             :     /*
     732             :      * add_eq_member doesn't check for volatile functions, set-returning
     733             :      * functions, aggregates, or window functions, but such could appear in
     734             :      * sort expressions; so we have to check whether its const-marking was
     735             :      * correct.
     736             :      */
     737      128964 :     if (newec->ec_has_const)
     738             :     {
     739        1594 :         if (newec->ec_has_volatile ||
     740        1480 :             expression_returns_set((Node *) expr) ||
     741        1276 :             contain_agg_clause((Node *) expr) ||
     742         580 :             contain_window_function((Node *) expr))
     743             :         {
     744         234 :             newec->ec_has_const = false;
     745         234 :             newem->em_is_const = false;
     746             :         }
     747             :     }
     748             : 
     749      128964 :     root->eq_classes = lappend(root->eq_classes, newec);
     750             : 
     751             :     /*
     752             :      * If EC merging is already complete, we have to mop up by adding the new
     753             :      * EC to the eclass_indexes of the relation(s) mentioned in it.
     754             :      */
     755      128964 :     if (root->ec_merging_done)
     756             :     {
     757       50130 :         int         ec_index = list_length(root->eq_classes) - 1;
     758       50130 :         int         i = -1;
     759             : 
     760       99398 :         while ((i = bms_next_member(newec->ec_relids, i)) > 0)
     761             :         {
     762       49268 :             RelOptInfo *rel = root->simple_rel_array[i];
     763             : 
     764             :             Assert(rel->reloptkind == RELOPT_BASEREL ||
     765             :                    rel->reloptkind == RELOPT_DEADREL);
     766             : 
     767       49268 :             rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
     768             :                                                  ec_index);
     769             :         }
     770             :     }
     771             : 
     772      128964 :     MemoryContextSwitchTo(oldcontext);
     773             : 
     774      128964 :     return newec;
     775             : }
     776             : 
     777             : /*
     778             :  * Find an equivalence class member expression, all of whose Vars, come from
     779             :  * the indicated relation.
     780             :  */
     781             : Expr *
     782        9122 : find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
     783             : {
     784             :     ListCell   *lc_em;
     785             : 
     786       19102 :     foreach(lc_em, ec->ec_members)
     787             :     {
     788       17642 :         EquivalenceMember *em = lfirst(lc_em);
     789             : 
     790       17642 :         if (bms_is_subset(em->em_relids, rel->relids) &&
     791        7670 :             !bms_is_empty(em->em_relids))
     792             :         {
     793             :             /*
     794             :              * If there is more than one equivalence member whose Vars are
     795             :              * taken entirely from this relation, we'll be content to choose
     796             :              * any one of those.
     797             :              */
     798        7662 :             return em->em_expr;
     799             :         }
     800             :     }
     801             : 
     802             :     /* We didn't find any suitable equivalence class expression */
     803        1460 :     return NULL;
     804             : }
     805             : 
     806             : /*
     807             :  * generate_base_implied_equalities
     808             :  *    Generate any restriction clauses that we can deduce from equivalence
     809             :  *    classes.
     810             :  *
     811             :  * When an EC contains pseudoconstants, our strategy is to generate
     812             :  * "member = const1" clauses where const1 is the first constant member, for
     813             :  * every other member (including other constants).  If we are able to do this
     814             :  * then we don't need any "var = var" comparisons because we've successfully
     815             :  * constrained all the vars at their points of creation.  If we fail to
     816             :  * generate any of these clauses due to lack of cross-type operators, we fall
     817             :  * back to the "ec_broken" strategy described below.  (XXX if there are
     818             :  * multiple constants of different types, it's possible that we might succeed
     819             :  * in forming all the required clauses if we started from a different const
     820             :  * member; but this seems a sufficiently hokey corner case to not be worth
     821             :  * spending lots of cycles on.)
     822             :  *
     823             :  * For ECs that contain no pseudoconstants, we generate derived clauses
     824             :  * "member1 = member2" for each pair of members belonging to the same base
     825             :  * relation (actually, if there are more than two for the same base relation,
     826             :  * we only need enough clauses to link each to each other).  This provides
     827             :  * the base case for the recursion: each row emitted by a base relation scan
     828             :  * will constrain all computable members of the EC to be equal.  As each
     829             :  * join path is formed, we'll add additional derived clauses on-the-fly
     830             :  * to maintain this invariant (see generate_join_implied_equalities).
     831             :  *
     832             :  * If the opfamilies used by the EC do not provide complete sets of cross-type
     833             :  * equality operators, it is possible that we will fail to generate a clause
     834             :  * that must be generated to maintain the invariant.  (An example: given
     835             :  * "WHERE a.x = b.y AND b.y = a.z", the scheme breaks down if we cannot
     836             :  * generate "a.x = a.z" as a restriction clause for A.)  In this case we mark
     837             :  * the EC "ec_broken" and fall back to regurgitating its original source
     838             :  * RestrictInfos at appropriate times.  We do not try to retract any derived
     839             :  * clauses already generated from the broken EC, so the resulting plan could
     840             :  * be poor due to bad selectivity estimates caused by redundant clauses.  But
     841             :  * the correct solution to that is to fix the opfamilies ...
     842             :  *
     843             :  * Equality clauses derived by this function are passed off to
     844             :  * process_implied_equality (in plan/initsplan.c) to be inserted into the
     845             :  * restrictinfo datastructures.  Note that this must be called after initial
     846             :  * scanning of the quals and before Path construction begins.
     847             :  *
     848             :  * We make no attempt to avoid generating duplicate RestrictInfos here: we
     849             :  * don't search ec_sources for matches, nor put the created RestrictInfos
     850             :  * into ec_derives.  Doing so would require some slightly ugly changes in
     851             :  * initsplan.c's API, and there's no real advantage, because the clauses
     852             :  * generated here can't duplicate anything we will generate for joins anyway.
     853             :  */
     854             : void
     855      202090 : generate_base_implied_equalities(PlannerInfo *root)
     856             : {
     857             :     int         ec_index;
     858             :     ListCell   *lc;
     859             : 
     860             :     /*
     861             :      * At this point, we're done absorbing knowledge of equivalences in the
     862             :      * query, so no further EC merging should happen, and ECs remaining in the
     863             :      * eq_classes list can be considered canonical.  (But note that it's still
     864             :      * possible for new single-member ECs to be added through
     865             :      * get_eclass_for_sort_expr().)
     866             :      */
     867      202090 :     root->ec_merging_done = true;
     868             : 
     869      202090 :     ec_index = 0;
     870      435704 :     foreach(lc, root->eq_classes)
     871             :     {
     872      233614 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
     873      233614 :         bool        can_generate_joinclause = false;
     874             :         int         i;
     875             : 
     876             :         Assert(ec->ec_merged == NULL);   /* else shouldn't be in list */
     877             :         Assert(!ec->ec_broken); /* not yet anyway... */
     878             : 
     879             :         /*
     880             :          * Generate implied equalities that are restriction clauses.
     881             :          * Single-member ECs won't generate any deductions, either here or at
     882             :          * the join level.
     883             :          */
     884      233614 :         if (list_length(ec->ec_members) > 1)
     885             :         {
     886      165092 :             if (ec->ec_has_const)
     887      140540 :                 generate_base_implied_equalities_const(root, ec);
     888             :             else
     889       24552 :                 generate_base_implied_equalities_no_const(root, ec);
     890             : 
     891             :             /* Recover if we failed to generate required derived clauses */
     892      165092 :             if (ec->ec_broken)
     893          20 :                 generate_base_implied_equalities_broken(root, ec);
     894             : 
     895             :             /* Detect whether this EC might generate join clauses */
     896      165092 :             can_generate_joinclause =
     897      165092 :                 (bms_membership(ec->ec_relids) == BMS_MULTIPLE);
     898             :         }
     899             : 
     900             :         /*
     901             :          * Mark the base rels cited in each eclass (which should all exist by
     902             :          * now) with the eq_classes indexes of all eclasses mentioning them.
     903             :          * This will let us avoid searching in subsequent lookups.  While
     904             :          * we're at it, we can mark base rels that have pending eclass joins;
     905             :          * this is a cheap version of has_relevant_eclass_joinclause().
     906             :          */
     907      233614 :         i = -1;
     908      495934 :         while ((i = bms_next_member(ec->ec_relids, i)) > 0)
     909             :         {
     910      262320 :             RelOptInfo *rel = root->simple_rel_array[i];
     911             : 
     912             :             Assert(rel->reloptkind == RELOPT_BASEREL);
     913             : 
     914      262320 :             rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
     915             :                                                  ec_index);
     916             : 
     917      262320 :             if (can_generate_joinclause)
     918       57536 :                 rel->has_eclass_joins = true;
     919             :         }
     920             : 
     921      233614 :         ec_index++;
     922             :     }
     923      202090 : }
     924             : 
     925             : /*
     926             :  * generate_base_implied_equalities when EC contains pseudoconstant(s)
     927             :  */
     928             : static void
     929      140540 : generate_base_implied_equalities_const(PlannerInfo *root,
     930             :                                        EquivalenceClass *ec)
     931             : {
     932      140540 :     EquivalenceMember *const_em = NULL;
     933             :     ListCell   *lc;
     934             : 
     935             :     /*
     936             :      * In the trivial case where we just had one "var = const" clause, push
     937             :      * the original clause back into the main planner machinery.  There is
     938             :      * nothing to be gained by doing it differently, and we save the effort to
     939             :      * re-build and re-analyze an equality clause that will be exactly
     940             :      * equivalent to the old one.
     941             :      */
     942      276214 :     if (list_length(ec->ec_members) == 2 &&
     943      135674 :         list_length(ec->ec_sources) == 1)
     944             :     {
     945      135674 :         RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
     946             : 
     947      135674 :         if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
     948             :         {
     949      135632 :             distribute_restrictinfo_to_rels(root, restrictinfo);
     950      135632 :             return;
     951             :         }
     952             :     }
     953             : 
     954             :     /*
     955             :      * Find the constant member to use.  We prefer an actual constant to
     956             :      * pseudo-constants (such as Params), because the constraint exclusion
     957             :      * machinery might be able to exclude relations on the basis of generated
     958             :      * "var = const" equalities, but "var = param" won't work for that.
     959             :      */
     960       12856 :     foreach(lc, ec->ec_members)
     961             :     {
     962       12838 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
     963             : 
     964       12838 :         if (cur_em->em_is_const)
     965             :         {
     966        4908 :             const_em = cur_em;
     967        4908 :             if (IsA(cur_em->em_expr, Const))
     968        4890 :                 break;
     969             :         }
     970             :     }
     971             :     Assert(const_em != NULL);
     972             : 
     973             :     /* Generate a derived equality against each other member */
     974       19630 :     foreach(lc, ec->ec_members)
     975             :     {
     976       14742 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
     977             :         Oid         eq_op;
     978             : 
     979             :         Assert(!cur_em->em_is_child);    /* no children yet */
     980       14742 :         if (cur_em == const_em)
     981        4892 :             continue;
     982        9850 :         eq_op = select_equality_operator(ec,
     983             :                                          cur_em->em_datatype,
     984             :                                          const_em->em_datatype);
     985        9850 :         if (!OidIsValid(eq_op))
     986             :         {
     987             :             /* failed... */
     988          20 :             ec->ec_broken = true;
     989          20 :             break;
     990             :         }
     991        9830 :         process_implied_equality(root, eq_op, ec->ec_collation,
     992             :                                  cur_em->em_expr, const_em->em_expr,
     993        9830 :                                  bms_copy(ec->ec_relids),
     994        9830 :                                  bms_union(cur_em->em_nullable_relids,
     995        9830 :                                            const_em->em_nullable_relids),
     996             :                                  ec->ec_min_security,
     997        9830 :                                  ec->ec_below_outer_join,
     998        9830 :                                  cur_em->em_is_const);
     999             :     }
    1000             : }
    1001             : 
    1002             : /*
    1003             :  * generate_base_implied_equalities when EC contains no pseudoconstants
    1004             :  */
    1005             : static void
    1006       24552 : generate_base_implied_equalities_no_const(PlannerInfo *root,
    1007             :                                           EquivalenceClass *ec)
    1008             : {
    1009             :     EquivalenceMember **prev_ems;
    1010             :     ListCell   *lc;
    1011             : 
    1012             :     /*
    1013             :      * We scan the EC members once and track the last-seen member for each
    1014             :      * base relation.  When we see another member of the same base relation,
    1015             :      * we generate "prev_em = cur_em".  This results in the minimum number of
    1016             :      * derived clauses, but it's possible that it will fail when a different
    1017             :      * ordering would succeed.  XXX FIXME: use a UNION-FIND algorithm similar
    1018             :      * to the way we build merged ECs.  (Use a list-of-lists for each rel.)
    1019             :      */
    1020             :     prev_ems = (EquivalenceMember **)
    1021       24552 :         palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
    1022             : 
    1023       74176 :     foreach(lc, ec->ec_members)
    1024             :     {
    1025       49624 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
    1026             :         int         relid;
    1027             : 
    1028             :         Assert(!cur_em->em_is_child);    /* no children yet */
    1029       49624 :         if (!bms_get_singleton_member(cur_em->em_relids, &relid))
    1030          40 :             continue;
    1031             :         Assert(relid < root->simple_rel_array_size);
    1032             : 
    1033       49584 :         if (prev_ems[relid] != NULL)
    1034             :         {
    1035         188 :             EquivalenceMember *prev_em = prev_ems[relid];
    1036             :             Oid         eq_op;
    1037             : 
    1038         188 :             eq_op = select_equality_operator(ec,
    1039             :                                              prev_em->em_datatype,
    1040             :                                              cur_em->em_datatype);
    1041         188 :             if (!OidIsValid(eq_op))
    1042             :             {
    1043             :                 /* failed... */
    1044           0 :                 ec->ec_broken = true;
    1045           0 :                 break;
    1046             :             }
    1047         188 :             process_implied_equality(root, eq_op, ec->ec_collation,
    1048             :                                      prev_em->em_expr, cur_em->em_expr,
    1049         188 :                                      bms_copy(ec->ec_relids),
    1050         188 :                                      bms_union(prev_em->em_nullable_relids,
    1051         188 :                                                cur_em->em_nullable_relids),
    1052             :                                      ec->ec_min_security,
    1053         188 :                                      ec->ec_below_outer_join,
    1054             :                                      false);
    1055             :         }
    1056       49584 :         prev_ems[relid] = cur_em;
    1057             :     }
    1058             : 
    1059       24552 :     pfree(prev_ems);
    1060             : 
    1061             :     /*
    1062             :      * We also have to make sure that all the Vars used in the member clauses
    1063             :      * will be available at any join node we might try to reference them at.
    1064             :      * For the moment we force all the Vars to be available at all join nodes
    1065             :      * for this eclass.  Perhaps this could be improved by doing some
    1066             :      * pre-analysis of which members we prefer to join, but it's no worse than
    1067             :      * what happened in the pre-8.3 code.
    1068             :      */
    1069       74176 :     foreach(lc, ec->ec_members)
    1070             :     {
    1071       49624 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
    1072       49624 :         List       *vars = pull_var_clause((Node *) cur_em->em_expr,
    1073             :                                            PVC_RECURSE_AGGREGATES |
    1074             :                                            PVC_RECURSE_WINDOWFUNCS |
    1075             :                                            PVC_INCLUDE_PLACEHOLDERS);
    1076             : 
    1077       49624 :         add_vars_to_targetlist(root, vars, ec->ec_relids, false);
    1078       49624 :         list_free(vars);
    1079             :     }
    1080       24552 : }
    1081             : 
    1082             : /*
    1083             :  * generate_base_implied_equalities cleanup after failure
    1084             :  *
    1085             :  * What we must do here is push any zero- or one-relation source RestrictInfos
    1086             :  * of the EC back into the main restrictinfo datastructures.  Multi-relation
    1087             :  * clauses will be regurgitated later by generate_join_implied_equalities().
    1088             :  * (We do it this way to maintain continuity with the case that ec_broken
    1089             :  * becomes set only after we've gone up a join level or two.)  However, for
    1090             :  * an EC that contains constants, we can adopt a simpler strategy and just
    1091             :  * throw back all the source RestrictInfos immediately; that works because
    1092             :  * we know that such an EC can't become broken later.  (This rule justifies
    1093             :  * ignoring ec_has_const ECs in generate_join_implied_equalities, even when
    1094             :  * they are broken.)
    1095             :  */
    1096             : static void
    1097          20 : generate_base_implied_equalities_broken(PlannerInfo *root,
    1098             :                                         EquivalenceClass *ec)
    1099             : {
    1100             :     ListCell   *lc;
    1101             : 
    1102          64 :     foreach(lc, ec->ec_sources)
    1103             :     {
    1104          44 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
    1105             : 
    1106          44 :         if (ec->ec_has_const ||
    1107           0 :             bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
    1108          44 :             distribute_restrictinfo_to_rels(root, restrictinfo);
    1109             :     }
    1110          20 : }
    1111             : 
    1112             : 
    1113             : /*
    1114             :  * generate_join_implied_equalities
    1115             :  *    Generate any join clauses that we can deduce from equivalence classes.
    1116             :  *
    1117             :  * At a join node, we must enforce restriction clauses sufficient to ensure
    1118             :  * that all equivalence-class members computable at that node are equal.
    1119             :  * Since the set of clauses to enforce can vary depending on which subset
    1120             :  * relations are the inputs, we have to compute this afresh for each join
    1121             :  * relation pair.  Hence a fresh List of RestrictInfo nodes is built and
    1122             :  * passed back on each call.
    1123             :  *
    1124             :  * In addition to its use at join nodes, this can be applied to generate
    1125             :  * eclass-based join clauses for use in a parameterized scan of a base rel.
    1126             :  * The reason for the asymmetry of specifying the inner rel as a RelOptInfo
    1127             :  * and the outer rel by Relids is that this usage occurs before we have
    1128             :  * built any join RelOptInfos.
    1129             :  *
    1130             :  * An annoying special case for parameterized scans is that the inner rel can
    1131             :  * be an appendrel child (an "other rel").  In this case we must generate
    1132             :  * appropriate clauses using child EC members.  add_child_rel_equivalences
    1133             :  * must already have been done for the child rel.
    1134             :  *
    1135             :  * The results are sufficient for use in merge, hash, and plain nestloop join
    1136             :  * methods.  We do not worry here about selecting clauses that are optimal
    1137             :  * for use in a parameterized indexscan.  indxpath.c makes its own selections
    1138             :  * of clauses to use, and if the ones we pick here are redundant with those,
    1139             :  * the extras will be eliminated at createplan time, using the parent_ec
    1140             :  * markers that we provide (see is_redundant_derived_clause()).
    1141             :  *
    1142             :  * Because the same join clauses are likely to be needed multiple times as
    1143             :  * we consider different join paths, we avoid generating multiple copies:
    1144             :  * whenever we select a particular pair of EquivalenceMembers to join,
    1145             :  * we check to see if the pair matches any original clause (in ec_sources)
    1146             :  * or previously-built clause (in ec_derives).  This saves memory and allows
    1147             :  * re-use of information cached in RestrictInfos.
    1148             :  *
    1149             :  * join_relids should always equal bms_union(outer_relids, inner_rel->relids).
    1150             :  * We could simplify this function's API by computing it internally, but in
    1151             :  * most current uses, the caller has the value at hand anyway.
    1152             :  */
    1153             : List *
    1154      228042 : generate_join_implied_equalities(PlannerInfo *root,
    1155             :                                  Relids join_relids,
    1156             :                                  Relids outer_relids,
    1157             :                                  RelOptInfo *inner_rel)
    1158             : {
    1159      228042 :     List       *result = NIL;
    1160      228042 :     Relids      inner_relids = inner_rel->relids;
    1161             :     Relids      nominal_inner_relids;
    1162             :     Relids      nominal_join_relids;
    1163             :     Bitmapset  *matching_ecs;
    1164             :     int         i;
    1165             : 
    1166             :     /* If inner rel is a child, extra setup work is needed */
    1167      228042 :     if (IS_OTHER_REL(inner_rel))
    1168             :     {
    1169             :         Assert(!bms_is_empty(inner_rel->top_parent_relids));
    1170             : 
    1171             :         /* Fetch relid set for the topmost parent rel */
    1172        4048 :         nominal_inner_relids = inner_rel->top_parent_relids;
    1173             :         /* ECs will be marked with the parent's relid, not the child's */
    1174        4048 :         nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
    1175             :     }
    1176             :     else
    1177             :     {
    1178      223994 :         nominal_inner_relids = inner_relids;
    1179      223994 :         nominal_join_relids = join_relids;
    1180             :     }
    1181             : 
    1182             :     /*
    1183             :      * Get all eclasses in common between inner_rel's relids and outer_relids
    1184             :      */
    1185      228042 :     matching_ecs = get_common_eclass_indexes(root, inner_rel->relids,
    1186             :                                              outer_relids);
    1187             : 
    1188      228042 :     i = -1;
    1189      356394 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
    1190             :     {
    1191      128352 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
    1192      128352 :         List       *sublist = NIL;
    1193             : 
    1194             :         /* ECs containing consts do not need any further enforcement */
    1195      128352 :         if (ec->ec_has_const)
    1196       19776 :             continue;
    1197             : 
    1198             :         /* Single-member ECs won't generate any deductions */
    1199      108576 :         if (list_length(ec->ec_members) <= 1)
    1200         630 :             continue;
    1201             : 
    1202             :         /* Sanity check that this eclass overlaps the join */
    1203             :         Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
    1204             : 
    1205      107946 :         if (!ec->ec_broken)
    1206      107774 :             sublist = generate_join_implied_equalities_normal(root,
    1207             :                                                               ec,
    1208             :                                                               join_relids,
    1209             :                                                               outer_relids,
    1210             :                                                               inner_relids);
    1211             : 
    1212             :         /* Recover if we failed to generate required derived clauses */
    1213      107946 :         if (ec->ec_broken)
    1214         192 :             sublist = generate_join_implied_equalities_broken(root,
    1215             :                                                               ec,
    1216             :                                                               nominal_join_relids,
    1217             :                                                               outer_relids,
    1218             :                                                               nominal_inner_relids,
    1219             :                                                               inner_rel);
    1220             : 
    1221      107946 :         result = list_concat(result, sublist);
    1222             :     }
    1223             : 
    1224      228042 :     return result;
    1225             : }
    1226             : 
    1227             : /*
    1228             :  * generate_join_implied_equalities_for_ecs
    1229             :  *    As above, but consider only the listed ECs.
    1230             :  */
    1231             : List *
    1232         822 : generate_join_implied_equalities_for_ecs(PlannerInfo *root,
    1233             :                                          List *eclasses,
    1234             :                                          Relids join_relids,
    1235             :                                          Relids outer_relids,
    1236             :                                          RelOptInfo *inner_rel)
    1237             : {
    1238         822 :     List       *result = NIL;
    1239         822 :     Relids      inner_relids = inner_rel->relids;
    1240             :     Relids      nominal_inner_relids;
    1241             :     Relids      nominal_join_relids;
    1242             :     ListCell   *lc;
    1243             : 
    1244             :     /* If inner rel is a child, extra setup work is needed */
    1245         822 :     if (IS_OTHER_REL(inner_rel))
    1246             :     {
    1247             :         Assert(!bms_is_empty(inner_rel->top_parent_relids));
    1248             : 
    1249             :         /* Fetch relid set for the topmost parent rel */
    1250           0 :         nominal_inner_relids = inner_rel->top_parent_relids;
    1251             :         /* ECs will be marked with the parent's relid, not the child's */
    1252           0 :         nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
    1253             :     }
    1254             :     else
    1255             :     {
    1256         822 :         nominal_inner_relids = inner_relids;
    1257         822 :         nominal_join_relids = join_relids;
    1258             :     }
    1259             : 
    1260        1664 :     foreach(lc, eclasses)
    1261             :     {
    1262         842 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
    1263         842 :         List       *sublist = NIL;
    1264             : 
    1265             :         /* ECs containing consts do not need any further enforcement */
    1266         842 :         if (ec->ec_has_const)
    1267           0 :             continue;
    1268             : 
    1269             :         /* Single-member ECs won't generate any deductions */
    1270         842 :         if (list_length(ec->ec_members) <= 1)
    1271           0 :             continue;
    1272             : 
    1273             :         /* We can quickly ignore any that don't overlap the join, too */
    1274         842 :         if (!bms_overlap(ec->ec_relids, nominal_join_relids))
    1275           0 :             continue;
    1276             : 
    1277         842 :         if (!ec->ec_broken)
    1278         842 :             sublist = generate_join_implied_equalities_normal(root,
    1279             :                                                               ec,
    1280             :                                                               join_relids,
    1281             :                                                               outer_relids,
    1282             :                                                               inner_relids);
    1283             : 
    1284             :         /* Recover if we failed to generate required derived clauses */
    1285         842 :         if (ec->ec_broken)
    1286           0 :             sublist = generate_join_implied_equalities_broken(root,
    1287             :                                                               ec,
    1288             :                                                               nominal_join_relids,
    1289             :                                                               outer_relids,
    1290             :                                                               nominal_inner_relids,
    1291             :                                                               inner_rel);
    1292             : 
    1293         842 :         result = list_concat(result, sublist);
    1294             :     }
    1295             : 
    1296         822 :     return result;
    1297             : }
    1298             : 
    1299             : /*
    1300             :  * generate_join_implied_equalities for a still-valid EC
    1301             :  */
    1302             : static List *
    1303      108616 : generate_join_implied_equalities_normal(PlannerInfo *root,
    1304             :                                         EquivalenceClass *ec,
    1305             :                                         Relids join_relids,
    1306             :                                         Relids outer_relids,
    1307             :                                         Relids inner_relids)
    1308             : {
    1309      108616 :     List       *result = NIL;
    1310      108616 :     List       *new_members = NIL;
    1311      108616 :     List       *outer_members = NIL;
    1312      108616 :     List       *inner_members = NIL;
    1313             :     ListCell   *lc1;
    1314             : 
    1315             :     /*
    1316             :      * First, scan the EC to identify member values that are computable at the
    1317             :      * outer rel, at the inner rel, or at this relation but not in either
    1318             :      * input rel.  The outer-rel members should already be enforced equal,
    1319             :      * likewise for the inner-rel members.  We'll need to create clauses to
    1320             :      * enforce that any newly computable members are all equal to each other
    1321             :      * as well as to at least one input member, plus enforce at least one
    1322             :      * outer-rel member equal to at least one inner-rel member.
    1323             :      */
    1324      371352 :     foreach(lc1, ec->ec_members)
    1325             :     {
    1326      262736 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
    1327             : 
    1328             :         /*
    1329             :          * We don't need to check explicitly for child EC members.  This test
    1330             :          * against join_relids will cause them to be ignored except when
    1331             :          * considering a child inner rel, which is what we want.
    1332             :          */
    1333      262736 :         if (!bms_is_subset(cur_em->em_relids, join_relids))
    1334       41858 :             continue;           /* not computable yet, or wrong child */
    1335             : 
    1336      220878 :         if (bms_is_subset(cur_em->em_relids, outer_relids))
    1337      111634 :             outer_members = lappend(outer_members, cur_em);
    1338      109244 :         else if (bms_is_subset(cur_em->em_relids, inner_relids))
    1339      108484 :             inner_members = lappend(inner_members, cur_em);
    1340             :         else
    1341         760 :             new_members = lappend(new_members, cur_em);
    1342             :     }
    1343             : 
    1344             :     /*
    1345             :      * First, select the joinclause if needed.  We can equate any one outer
    1346             :      * member to any one inner member, but we have to find a datatype
    1347             :      * combination for which an opfamily member operator exists.  If we have
    1348             :      * choices, we prefer simple Var members (possibly with RelabelType) since
    1349             :      * these are (a) cheapest to compute at runtime and (b) most likely to
    1350             :      * have useful statistics. Also, prefer operators that are also
    1351             :      * hashjoinable.
    1352             :      */
    1353      108616 :     if (outer_members && inner_members)
    1354             :     {
    1355      106850 :         EquivalenceMember *best_outer_em = NULL;
    1356      106850 :         EquivalenceMember *best_inner_em = NULL;
    1357      106850 :         Oid         best_eq_op = InvalidOid;
    1358      106850 :         int         best_score = -1;
    1359             :         RestrictInfo *rinfo;
    1360             : 
    1361      109712 :         foreach(lc1, outer_members)
    1362             :         {
    1363      106898 :             EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
    1364             :             ListCell   *lc2;
    1365             : 
    1366      109768 :             foreach(lc2, inner_members)
    1367             :             {
    1368      106906 :                 EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
    1369             :                 Oid         eq_op;
    1370             :                 int         score;
    1371             : 
    1372      106906 :                 eq_op = select_equality_operator(ec,
    1373             :                                                  outer_em->em_datatype,
    1374             :                                                  inner_em->em_datatype);
    1375      106906 :                 if (!OidIsValid(eq_op))
    1376          20 :                     continue;
    1377      106886 :                 score = 0;
    1378      106886 :                 if (IsA(outer_em->em_expr, Var) ||
    1379        3910 :                     (IsA(outer_em->em_expr, RelabelType) &&
    1380        2364 :                      IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
    1381      105308 :                     score++;
    1382      106886 :                 if (IsA(inner_em->em_expr, Var) ||
    1383        3992 :                     (IsA(inner_em->em_expr, RelabelType) &&
    1384        2486 :                      IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
    1385      105368 :                     score++;
    1386      106886 :                 if (op_hashjoinable(eq_op,
    1387      106886 :                                     exprType((Node *) outer_em->em_expr)))
    1388      106836 :                     score++;
    1389      106886 :                 if (score > best_score)
    1390             :                 {
    1391      106830 :                     best_outer_em = outer_em;
    1392      106830 :                     best_inner_em = inner_em;
    1393      106830 :                     best_eq_op = eq_op;
    1394      106830 :                     best_score = score;
    1395      106830 :                     if (best_score == 3)
    1396      104036 :                         break;  /* no need to look further */
    1397             :                 }
    1398             :             }
    1399      106898 :             if (best_score == 3)
    1400      104036 :                 break;          /* no need to look further */
    1401             :         }
    1402      106850 :         if (best_score < 0)
    1403             :         {
    1404             :             /* failed... */
    1405          20 :             ec->ec_broken = true;
    1406          20 :             return NIL;
    1407             :         }
    1408             : 
    1409             :         /*
    1410             :          * Create clause, setting parent_ec to mark it as redundant with other
    1411             :          * joinclauses
    1412             :          */
    1413      106830 :         rinfo = create_join_clause(root, ec, best_eq_op,
    1414             :                                    best_outer_em, best_inner_em,
    1415             :                                    ec);
    1416             : 
    1417      106830 :         result = lappend(result, rinfo);
    1418             :     }
    1419             : 
    1420             :     /*
    1421             :      * Now deal with building restrictions for any expressions that involve
    1422             :      * Vars from both sides of the join.  We have to equate all of these to
    1423             :      * each other as well as to at least one old member (if any).
    1424             :      *
    1425             :      * XXX as in generate_base_implied_equalities_no_const, we could be a lot
    1426             :      * smarter here to avoid unnecessary failures in cross-type situations.
    1427             :      * For now, use the same left-to-right method used there.
    1428             :      */
    1429      108596 :     if (new_members)
    1430             :     {
    1431         760 :         List       *old_members = list_concat(outer_members, inner_members);
    1432         760 :         EquivalenceMember *prev_em = NULL;
    1433             :         RestrictInfo *rinfo;
    1434             : 
    1435             :         /* For now, arbitrarily take the first old_member as the one to use */
    1436         760 :         if (old_members)
    1437         572 :             new_members = lappend(new_members, linitial(old_members));
    1438             : 
    1439        2092 :         foreach(lc1, new_members)
    1440             :         {
    1441        1332 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
    1442             : 
    1443        1332 :             if (prev_em != NULL)
    1444             :             {
    1445             :                 Oid         eq_op;
    1446             : 
    1447         572 :                 eq_op = select_equality_operator(ec,
    1448             :                                                  prev_em->em_datatype,
    1449             :                                                  cur_em->em_datatype);
    1450         572 :                 if (!OidIsValid(eq_op))
    1451             :                 {
    1452             :                     /* failed... */
    1453           0 :                     ec->ec_broken = true;
    1454           0 :                     return NIL;
    1455             :                 }
    1456             :                 /* do NOT set parent_ec, this qual is not redundant! */
    1457         572 :                 rinfo = create_join_clause(root, ec, eq_op,
    1458             :                                            prev_em, cur_em,
    1459             :                                            NULL);
    1460             : 
    1461         572 :                 result = lappend(result, rinfo);
    1462             :             }
    1463        1332 :             prev_em = cur_em;
    1464             :         }
    1465             :     }
    1466             : 
    1467      108596 :     return result;
    1468             : }
    1469             : 
    1470             : /*
    1471             :  * generate_join_implied_equalities cleanup after failure
    1472             :  *
    1473             :  * Return any original RestrictInfos that are enforceable at this join.
    1474             :  *
    1475             :  * In the case of a child inner relation, we have to translate the
    1476             :  * original RestrictInfos from parent to child Vars.
    1477             :  */
    1478             : static List *
    1479         192 : generate_join_implied_equalities_broken(PlannerInfo *root,
    1480             :                                         EquivalenceClass *ec,
    1481             :                                         Relids nominal_join_relids,
    1482             :                                         Relids outer_relids,
    1483             :                                         Relids nominal_inner_relids,
    1484             :                                         RelOptInfo *inner_rel)
    1485             : {
    1486         192 :     List       *result = NIL;
    1487             :     ListCell   *lc;
    1488             : 
    1489         528 :     foreach(lc, ec->ec_sources)
    1490             :     {
    1491         336 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
    1492         336 :         Relids      clause_relids = restrictinfo->required_relids;
    1493             : 
    1494         336 :         if (bms_is_subset(clause_relids, nominal_join_relids) &&
    1495         184 :             !bms_is_subset(clause_relids, outer_relids) &&
    1496         168 :             !bms_is_subset(clause_relids, nominal_inner_relids))
    1497         168 :             result = lappend(result, restrictinfo);
    1498             :     }
    1499             : 
    1500             :     /*
    1501             :      * If we have to translate, just brute-force apply adjust_appendrel_attrs
    1502             :      * to all the RestrictInfos at once.  This will result in returning
    1503             :      * RestrictInfos that are not listed in ec_derives, but there shouldn't be
    1504             :      * any duplication, and it's a sufficiently narrow corner case that we
    1505             :      * shouldn't sweat too much over it anyway.
    1506             :      *
    1507             :      * Since inner_rel might be an indirect descendant of the baserel
    1508             :      * mentioned in the ec_sources clauses, we have to be prepared to apply
    1509             :      * multiple levels of Var translation.
    1510             :      */
    1511         192 :     if (IS_OTHER_REL(inner_rel) && result != NIL)
    1512         108 :         result = (List *) adjust_appendrel_attrs_multilevel(root,
    1513             :                                                             (Node *) result,
    1514             :                                                             inner_rel->relids,
    1515             :                                                             inner_rel->top_parent_relids);
    1516             : 
    1517         192 :     return result;
    1518             : }
    1519             : 
    1520             : 
    1521             : /*
    1522             :  * select_equality_operator
    1523             :  *    Select a suitable equality operator for comparing two EC members
    1524             :  *
    1525             :  * Returns InvalidOid if no operator can be found for this datatype combination
    1526             :  */
    1527             : static Oid
    1528      165566 : select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
    1529             : {
    1530             :     ListCell   *lc;
    1531             : 
    1532      165606 :     foreach(lc, ec->ec_opfamilies)
    1533             :     {
    1534      165566 :         Oid         opfamily = lfirst_oid(lc);
    1535             :         Oid         opno;
    1536             : 
    1537      165566 :         opno = get_opfamily_member(opfamily, lefttype, righttype,
    1538             :                                    BTEqualStrategyNumber);
    1539      165566 :         if (!OidIsValid(opno))
    1540          40 :             continue;
    1541             :         /* If no barrier quals in query, don't worry about leaky operators */
    1542      165526 :         if (ec->ec_max_security == 0)
    1543      165526 :             return opno;
    1544             :         /* Otherwise, insist that selected operators be leakproof */
    1545         384 :         if (get_func_leakproof(get_opcode(opno)))
    1546         384 :             return opno;
    1547             :     }
    1548          40 :     return InvalidOid;
    1549             : }
    1550             : 
    1551             : 
    1552             : /*
    1553             :  * create_join_clause
    1554             :  *    Find or make a RestrictInfo comparing the two given EC members
    1555             :  *    with the given operator.
    1556             :  *
    1557             :  * parent_ec is either equal to ec (if the clause is a potentially-redundant
    1558             :  * join clause) or NULL (if not).  We have to treat this as part of the
    1559             :  * match requirements --- it's possible that a clause comparing the same two
    1560             :  * EMs is a join clause in one join path and a restriction clause in another.
    1561             :  */
    1562             : static RestrictInfo *
    1563      149570 : create_join_clause(PlannerInfo *root,
    1564             :                    EquivalenceClass *ec, Oid opno,
    1565             :                    EquivalenceMember *leftem,
    1566             :                    EquivalenceMember *rightem,
    1567             :                    EquivalenceClass *parent_ec)
    1568             : {
    1569             :     RestrictInfo *rinfo;
    1570             :     ListCell   *lc;
    1571             :     MemoryContext oldcontext;
    1572             : 
    1573             :     /*
    1574             :      * Search to see if we already built a RestrictInfo for this pair of
    1575             :      * EquivalenceMembers.  We can use either original source clauses or
    1576             :      * previously-derived clauses.  The check on opno is probably redundant,
    1577             :      * but be safe ...
    1578             :      */
    1579      313960 :     foreach(lc, ec->ec_sources)
    1580             :     {
    1581      164438 :         rinfo = (RestrictInfo *) lfirst(lc);
    1582      164438 :         if (rinfo->left_em == leftem &&
    1583       71958 :             rinfo->right_em == rightem &&
    1584       65928 :             rinfo->parent_ec == parent_ec &&
    1585          48 :             opno == ((OpExpr *) rinfo->clause)->opno)
    1586          48 :             return rinfo;
    1587             :     }
    1588             : 
    1589      297488 :     foreach(lc, ec->ec_derives)
    1590             :     {
    1591      245150 :         rinfo = (RestrictInfo *) lfirst(lc);
    1592      245150 :         if (rinfo->left_em == leftem &&
    1593      112490 :             rinfo->right_em == rightem &&
    1594       97616 :             rinfo->parent_ec == parent_ec &&
    1595       97184 :             opno == ((OpExpr *) rinfo->clause)->opno)
    1596       97184 :             return rinfo;
    1597             :     }
    1598             : 
    1599             :     /*
    1600             :      * Not there, so build it, in planner context so we can re-use it. (Not
    1601             :      * important in normal planning, but definitely so in GEQO.)
    1602             :      */
    1603       52338 :     oldcontext = MemoryContextSwitchTo(root->planner_cxt);
    1604             : 
    1605       52338 :     rinfo = build_implied_join_equality(opno,
    1606             :                                         ec->ec_collation,
    1607             :                                         leftem->em_expr,
    1608             :                                         rightem->em_expr,
    1609       52338 :                                         bms_union(leftem->em_relids,
    1610       52338 :                                                   rightem->em_relids),
    1611       52338 :                                         bms_union(leftem->em_nullable_relids,
    1612       52338 :                                                   rightem->em_nullable_relids),
    1613             :                                         ec->ec_min_security);
    1614             : 
    1615             :     /* Mark the clause as redundant, or not */
    1616       52338 :     rinfo->parent_ec = parent_ec;
    1617             : 
    1618             :     /*
    1619             :      * We know the correct values for left_ec/right_ec, ie this particular EC,
    1620             :      * so we can just set them directly instead of forcing another lookup.
    1621             :      */
    1622       52338 :     rinfo->left_ec = ec;
    1623       52338 :     rinfo->right_ec = ec;
    1624             : 
    1625             :     /* Mark it as usable with these EMs */
    1626       52338 :     rinfo->left_em = leftem;
    1627       52338 :     rinfo->right_em = rightem;
    1628             :     /* and save it for possible re-use */
    1629       52338 :     ec->ec_derives = lappend(ec->ec_derives, rinfo);
    1630             : 
    1631       52338 :     MemoryContextSwitchTo(oldcontext);
    1632             : 
    1633       52338 :     return rinfo;
    1634             : }
    1635             : 
    1636             : 
    1637             : /*
    1638             :  * reconsider_outer_join_clauses
    1639             :  *    Re-examine any outer-join clauses that were set aside by
    1640             :  *    distribute_qual_to_rels(), and see if we can derive any
    1641             :  *    EquivalenceClasses from them.  Then, if they were not made
    1642             :  *    redundant, push them out into the regular join-clause lists.
    1643             :  *
    1644             :  * When we have mergejoinable clauses A = B that are outer-join clauses,
    1645             :  * we can't blindly combine them with other clauses A = C to deduce B = C,
    1646             :  * since in fact the "equality" A = B won't necessarily hold above the
    1647             :  * outer join (one of the variables might be NULL instead).  Nonetheless
    1648             :  * there are cases where we can add qual clauses using transitivity.
    1649             :  *
    1650             :  * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
    1651             :  * for which there is also an equivalence clause OUTERVAR = CONSTANT.
    1652             :  * It is safe and useful to push a clause INNERVAR = CONSTANT into the
    1653             :  * evaluation of the inner (nullable) relation, because any inner rows not
    1654             :  * meeting this condition will not contribute to the outer-join result anyway.
    1655             :  * (Any outer rows they could join to will be eliminated by the pushed-down
    1656             :  * equivalence clause.)
    1657             :  *
    1658             :  * Note that the above rule does not work for full outer joins; nor is it
    1659             :  * very interesting to consider cases where the generated equivalence clause
    1660             :  * would involve relations outside the outer join, since such clauses couldn't
    1661             :  * be pushed into the inner side's scan anyway.  So the restriction to
    1662             :  * outervar = pseudoconstant is not really giving up anything.
    1663             :  *
    1664             :  * For full-join cases, we can only do something useful if it's a FULL JOIN
    1665             :  * USING and a merged column has an equivalence MERGEDVAR = CONSTANT.
    1666             :  * By the time it gets here, the merged column will look like
    1667             :  *      COALESCE(LEFTVAR, RIGHTVAR)
    1668             :  * and we will have a full-join clause LEFTVAR = RIGHTVAR that we can match
    1669             :  * the COALESCE expression to. In this situation we can push LEFTVAR = CONSTANT
    1670             :  * and RIGHTVAR = CONSTANT into the input relations, since any rows not
    1671             :  * meeting these conditions cannot contribute to the join result.
    1672             :  *
    1673             :  * Again, there isn't any traction to be gained by trying to deal with
    1674             :  * clauses comparing a mergedvar to a non-pseudoconstant.  So we can make
    1675             :  * use of the EquivalenceClasses to search for matching variables that were
    1676             :  * equivalenced to constants.  The interesting outer-join clauses were
    1677             :  * accumulated for us by distribute_qual_to_rels.
    1678             :  *
    1679             :  * When we find one of these cases, we implement the changes we want by
    1680             :  * generating a new equivalence clause INNERVAR = CONSTANT (or LEFTVAR, etc)
    1681             :  * and pushing it into the EquivalenceClass structures.  This is because we
    1682             :  * may already know that INNERVAR is equivalenced to some other var(s), and
    1683             :  * we'd like the constant to propagate to them too.  Note that it would be
    1684             :  * unsafe to merge any existing EC for INNERVAR with the OUTERVAR's EC ---
    1685             :  * that could result in propagating constant restrictions from
    1686             :  * INNERVAR to OUTERVAR, which would be very wrong.
    1687             :  *
    1688             :  * It's possible that the INNERVAR is also an OUTERVAR for some other
    1689             :  * outer-join clause, in which case the process can be repeated.  So we repeat
    1690             :  * looping over the lists of clauses until no further deductions can be made.
    1691             :  * Whenever we do make a deduction, we remove the generating clause from the
    1692             :  * lists, since we don't want to make the same deduction twice.
    1693             :  *
    1694             :  * If we don't find any match for a set-aside outer join clause, we must
    1695             :  * throw it back into the regular joinclause processing by passing it to
    1696             :  * distribute_restrictinfo_to_rels().  If we do generate a derived clause,
    1697             :  * however, the outer-join clause is redundant.  We still throw it back,
    1698             :  * because otherwise the join will be seen as a clauseless join and avoided
    1699             :  * during join order searching; but we mark it as redundant to keep from
    1700             :  * messing up the joinrel's size estimate.  (This behavior means that the
    1701             :  * API for this routine is uselessly complex: we could have just put all
    1702             :  * the clauses into the regular processing initially.  We keep it because
    1703             :  * someday we might want to do something else, such as inserting "dummy"
    1704             :  * joinclauses instead of real ones.)
    1705             :  *
    1706             :  * Outer join clauses that are marked outerjoin_delayed are special: this
    1707             :  * condition means that one or both VARs might go to null due to a lower
    1708             :  * outer join.  We can still push a constant through the clause, but only
    1709             :  * if its operator is strict; and we *have to* throw the clause back into
    1710             :  * regular joinclause processing.  By keeping the strict join clause,
    1711             :  * we ensure that any null-extended rows that are mistakenly generated due
    1712             :  * to suppressing rows not matching the constant will be rejected at the
    1713             :  * upper outer join.  (This doesn't work for full-join clauses.)
    1714             :  */
    1715             : void
    1716      207932 : reconsider_outer_join_clauses(PlannerInfo *root)
    1717             : {
    1718             :     bool        found;
    1719             :     ListCell   *cell;
    1720             : 
    1721             :     /* Outer loop repeats until we find no more deductions */
    1722             :     do
    1723             :     {
    1724      207932 :         found = false;
    1725             : 
    1726             :         /* Process the LEFT JOIN clauses */
    1727      241768 :         foreach(cell, root->left_join_clauses)
    1728             :         {
    1729       33836 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
    1730             : 
    1731       33836 :             if (reconsider_outer_join_clause(root, rinfo, true))
    1732             :             {
    1733        5438 :                 found = true;
    1734             :                 /* remove it from the list */
    1735        5438 :                 root->left_join_clauses =
    1736        5438 :                     foreach_delete_current(root->left_join_clauses, cell);
    1737             :                 /* we throw it back anyway (see notes above) */
    1738             :                 /* but the thrown-back clause has no extra selectivity */
    1739        5438 :                 rinfo->norm_selec = 2.0;
    1740        5438 :                 rinfo->outer_selec = 1.0;
    1741        5438 :                 distribute_restrictinfo_to_rels(root, rinfo);
    1742             :             }
    1743             :         }
    1744             : 
    1745             :         /* Process the RIGHT JOIN clauses */
    1746      219432 :         foreach(cell, root->right_join_clauses)
    1747             :         {
    1748       11500 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
    1749             : 
    1750       11500 :             if (reconsider_outer_join_clause(root, rinfo, false))
    1751             :             {
    1752         408 :                 found = true;
    1753             :                 /* remove it from the list */
    1754         408 :                 root->right_join_clauses =
    1755         408 :                     foreach_delete_current(root->right_join_clauses, cell);
    1756             :                 /* we throw it back anyway (see notes above) */
    1757             :                 /* but the thrown-back clause has no extra selectivity */
    1758         408 :                 rinfo->norm_selec = 2.0;
    1759         408 :                 rinfo->outer_selec = 1.0;
    1760         408 :                 distribute_restrictinfo_to_rels(root, rinfo);
    1761             :             }
    1762             :         }
    1763             : 
    1764             :         /* Process the FULL JOIN clauses */
    1765      208646 :         foreach(cell, root->full_join_clauses)
    1766             :         {
    1767         714 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
    1768             : 
    1769         714 :             if (reconsider_full_join_clause(root, rinfo))
    1770             :             {
    1771           4 :                 found = true;
    1772             :                 /* remove it from the list */
    1773           4 :                 root->full_join_clauses =
    1774           4 :                     foreach_delete_current(root->full_join_clauses, cell);
    1775             :                 /* we throw it back anyway (see notes above) */
    1776             :                 /* but the thrown-back clause has no extra selectivity */
    1777           4 :                 rinfo->norm_selec = 2.0;
    1778           4 :                 rinfo->outer_selec = 1.0;
    1779           4 :                 distribute_restrictinfo_to_rels(root, rinfo);
    1780             :             }
    1781             :         }
    1782      207932 :     } while (found);
    1783             : 
    1784             :     /* Now, any remaining clauses have to be thrown back */
    1785      228546 :     foreach(cell, root->left_join_clauses)
    1786             :     {
    1787       26456 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
    1788             : 
    1789       26456 :         distribute_restrictinfo_to_rels(root, rinfo);
    1790             :     }
    1791      211284 :     foreach(cell, root->right_join_clauses)
    1792             :     {
    1793        9194 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
    1794             : 
    1795        9194 :         distribute_restrictinfo_to_rels(root, rinfo);
    1796             :     }
    1797      202800 :     foreach(cell, root->full_join_clauses)
    1798             :     {
    1799         710 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
    1800             : 
    1801         710 :         distribute_restrictinfo_to_rels(root, rinfo);
    1802             :     }
    1803      202090 : }
    1804             : 
    1805             : /*
    1806             :  * reconsider_outer_join_clauses for a single LEFT/RIGHT JOIN clause
    1807             :  *
    1808             :  * Returns true if we were able to propagate a constant through the clause.
    1809             :  */
    1810             : static bool
    1811       45336 : reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo,
    1812             :                              bool outer_on_left)
    1813             : {
    1814             :     Expr       *outervar,
    1815             :                *innervar;
    1816             :     Oid         opno,
    1817             :                 collation,
    1818             :                 left_type,
    1819             :                 right_type,
    1820             :                 inner_datatype;
    1821             :     Relids      inner_relids,
    1822             :                 inner_nullable_relids;
    1823             :     ListCell   *lc1;
    1824             : 
    1825             :     Assert(is_opclause(rinfo->clause));
    1826       45336 :     opno = ((OpExpr *) rinfo->clause)->opno;
    1827       45336 :     collation = ((OpExpr *) rinfo->clause)->inputcollid;
    1828             : 
    1829             :     /* If clause is outerjoin_delayed, operator must be strict */
    1830       45336 :     if (rinfo->outerjoin_delayed && !op_strict(opno))
    1831           0 :         return false;
    1832             : 
    1833             :     /* Extract needed info from the clause */
    1834       45336 :     op_input_types(opno, &left_type, &right_type);
    1835       45336 :     if (outer_on_left)
    1836             :     {
    1837       33836 :         outervar = (Expr *) get_leftop(rinfo->clause);
    1838       33836 :         innervar = (Expr *) get_rightop(rinfo->clause);
    1839       33836 :         inner_datatype = right_type;
    1840       33836 :         inner_relids = rinfo->right_relids;
    1841             :     }
    1842             :     else
    1843             :     {
    1844       11500 :         outervar = (Expr *) get_rightop(rinfo->clause);
    1845       11500 :         innervar = (Expr *) get_leftop(rinfo->clause);
    1846       11500 :         inner_datatype = left_type;
    1847       11500 :         inner_relids = rinfo->left_relids;
    1848             :     }
    1849       45336 :     inner_nullable_relids = bms_intersect(inner_relids,
    1850       45336 :                                           rinfo->nullable_relids);
    1851             : 
    1852             :     /* Scan EquivalenceClasses for a match to outervar */
    1853      222032 :     foreach(lc1, root->eq_classes)
    1854             :     {
    1855      182542 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
    1856             :         bool        match;
    1857             :         ListCell   *lc2;
    1858             : 
    1859             :         /* Ignore EC unless it contains pseudoconstants */
    1860      182542 :         if (!cur_ec->ec_has_const)
    1861      124322 :             continue;
    1862             :         /* Never match to a volatile EC */
    1863       58220 :         if (cur_ec->ec_has_volatile)
    1864           0 :             continue;
    1865             :         /* It has to match the outer-join clause as to semantics, too */
    1866       58220 :         if (collation != cur_ec->ec_collation)
    1867        1294 :             continue;
    1868       56926 :         if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
    1869        9794 :             continue;
    1870             :         /* Does it contain a match to outervar? */
    1871       47132 :         match = false;
    1872      139036 :         foreach(lc2, cur_ec->ec_members)
    1873             :         {
    1874       97750 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
    1875             : 
    1876             :             Assert(!cur_em->em_is_child);    /* no children yet */
    1877       97750 :             if (equal(outervar, cur_em->em_expr))
    1878             :             {
    1879        5846 :                 match = true;
    1880        5846 :                 break;
    1881             :             }
    1882             :         }
    1883       47132 :         if (!match)
    1884       41286 :             continue;           /* no match, so ignore this EC */
    1885             : 
    1886             :         /*
    1887             :          * Yes it does!  Try to generate a clause INNERVAR = CONSTANT for each
    1888             :          * CONSTANT in the EC.  Note that we must succeed with at least one
    1889             :          * constant before we can decide to throw away the outer-join clause.
    1890             :          */
    1891        5846 :         match = false;
    1892       19928 :         foreach(lc2, cur_ec->ec_members)
    1893             :         {
    1894       14082 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
    1895             :             Oid         eq_op;
    1896             :             RestrictInfo *newrinfo;
    1897             : 
    1898       14082 :             if (!cur_em->em_is_const)
    1899        8208 :                 continue;       /* ignore non-const members */
    1900        5874 :             eq_op = select_equality_operator(cur_ec,
    1901             :                                              inner_datatype,
    1902             :                                              cur_em->em_datatype);
    1903        5874 :             if (!OidIsValid(eq_op))
    1904           0 :                 continue;       /* can't generate equality */
    1905        5874 :             newrinfo = build_implied_join_equality(eq_op,
    1906             :                                                    cur_ec->ec_collation,
    1907             :                                                    innervar,
    1908             :                                                    cur_em->em_expr,
    1909             :                                                    bms_copy(inner_relids),
    1910             :                                                    bms_copy(inner_nullable_relids),
    1911             :                                                    cur_ec->ec_min_security);
    1912        5874 :             if (process_equivalence(root, &newrinfo, true))
    1913        5874 :                 match = true;
    1914             :         }
    1915             : 
    1916             :         /*
    1917             :          * If we were able to equate INNERVAR to any constant, report success.
    1918             :          * Otherwise, fall out of the search loop, since we know the OUTERVAR
    1919             :          * appears in at most one EC.
    1920             :          */
    1921        5846 :         if (match)
    1922        5846 :             return true;
    1923             :         else
    1924           0 :             break;
    1925             :     }
    1926             : 
    1927       39490 :     return false;               /* failed to make any deduction */
    1928             : }
    1929             : 
    1930             : /*
    1931             :  * reconsider_outer_join_clauses for a single FULL JOIN clause
    1932             :  *
    1933             :  * Returns true if we were able to propagate a constant through the clause.
    1934             :  */
    1935             : static bool
    1936         714 : reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
    1937             : {
    1938             :     Expr       *leftvar;
    1939             :     Expr       *rightvar;
    1940             :     Oid         opno,
    1941             :                 collation,
    1942             :                 left_type,
    1943             :                 right_type;
    1944             :     Relids      left_relids,
    1945             :                 right_relids,
    1946             :                 left_nullable_relids,
    1947             :                 right_nullable_relids;
    1948             :     ListCell   *lc1;
    1949             : 
    1950             :     /* Can't use an outerjoin_delayed clause here */
    1951         714 :     if (rinfo->outerjoin_delayed)
    1952          38 :         return false;
    1953             : 
    1954             :     /* Extract needed info from the clause */
    1955             :     Assert(is_opclause(rinfo->clause));
    1956         676 :     opno = ((OpExpr *) rinfo->clause)->opno;
    1957         676 :     collation = ((OpExpr *) rinfo->clause)->inputcollid;
    1958         676 :     op_input_types(opno, &left_type, &right_type);
    1959         676 :     leftvar = (Expr *) get_leftop(rinfo->clause);
    1960         676 :     rightvar = (Expr *) get_rightop(rinfo->clause);
    1961         676 :     left_relids = rinfo->left_relids;
    1962         676 :     right_relids = rinfo->right_relids;
    1963         676 :     left_nullable_relids = bms_intersect(left_relids,
    1964         676 :                                          rinfo->nullable_relids);
    1965         676 :     right_nullable_relids = bms_intersect(right_relids,
    1966         676 :                                           rinfo->nullable_relids);
    1967             : 
    1968        3508 :     foreach(lc1, root->eq_classes)
    1969             :     {
    1970        2836 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
    1971        2836 :         EquivalenceMember *coal_em = NULL;
    1972             :         bool        match;
    1973             :         bool        matchleft;
    1974             :         bool        matchright;
    1975             :         ListCell   *lc2;
    1976             : 
    1977             :         /* Ignore EC unless it contains pseudoconstants */
    1978        2836 :         if (!cur_ec->ec_has_const)
    1979        2660 :             continue;
    1980             :         /* Never match to a volatile EC */
    1981         176 :         if (cur_ec->ec_has_volatile)
    1982           0 :             continue;
    1983             :         /* It has to match the outer-join clause as to semantics, too */
    1984         176 :         if (collation != cur_ec->ec_collation)
    1985          24 :             continue;
    1986         152 :         if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
    1987           0 :             continue;
    1988             : 
    1989             :         /*
    1990             :          * Does it contain a COALESCE(leftvar, rightvar) construct?
    1991             :          *
    1992             :          * We can assume the COALESCE() inputs are in the same order as the
    1993             :          * join clause, since both were automatically generated in the cases
    1994             :          * we care about.
    1995             :          *
    1996             :          * XXX currently this may fail to match in cross-type cases because
    1997             :          * the COALESCE will contain typecast operations while the join clause
    1998             :          * may not (if there is a cross-type mergejoin operator available for
    1999             :          * the two column types). Is it OK to strip implicit coercions from
    2000             :          * the COALESCE arguments?
    2001             :          */
    2002         152 :         match = false;
    2003         440 :         foreach(lc2, cur_ec->ec_members)
    2004             :         {
    2005         292 :             coal_em = (EquivalenceMember *) lfirst(lc2);
    2006             :             Assert(!coal_em->em_is_child);   /* no children yet */
    2007         292 :             if (IsA(coal_em->em_expr, CoalesceExpr))
    2008             :             {
    2009           4 :                 CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
    2010             :                 Node       *cfirst;
    2011             :                 Node       *csecond;
    2012             : 
    2013           4 :                 if (list_length(cexpr->args) != 2)
    2014           0 :                     continue;
    2015           4 :                 cfirst = (Node *) linitial(cexpr->args);
    2016           4 :                 csecond = (Node *) lsecond(cexpr->args);
    2017             : 
    2018           4 :                 if (equal(leftvar, cfirst) && equal(rightvar, csecond))
    2019             :                 {
    2020           4 :                     match = true;
    2021           4 :                     break;
    2022             :                 }
    2023             :             }
    2024             :         }
    2025         152 :         if (!match)
    2026         148 :             continue;           /* no match, so ignore this EC */
    2027             : 
    2028             :         /*
    2029             :          * Yes it does!  Try to generate clauses LEFTVAR = CONSTANT and
    2030             :          * RIGHTVAR = CONSTANT for each CONSTANT in the EC.  Note that we must
    2031             :          * succeed with at least one constant for each var before we can
    2032             :          * decide to throw away the outer-join clause.
    2033             :          */
    2034           4 :         matchleft = matchright = false;
    2035          12 :         foreach(lc2, cur_ec->ec_members)
    2036             :         {
    2037           8 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
    2038             :             Oid         eq_op;
    2039             :             RestrictInfo *newrinfo;
    2040             : 
    2041           8 :             if (!cur_em->em_is_const)
    2042           4 :                 continue;       /* ignore non-const members */
    2043           4 :             eq_op = select_equality_operator(cur_ec,
    2044             :                                              left_type,
    2045             :                                              cur_em->em_datatype);
    2046           4 :             if (OidIsValid(eq_op))
    2047             :             {
    2048           4 :                 newrinfo = build_implied_join_equality(eq_op,
    2049             :                                                        cur_ec->ec_collation,
    2050             :                                                        leftvar,
    2051             :                                                        cur_em->em_expr,
    2052             :                                                        bms_copy(left_relids),
    2053             :                                                        bms_copy(left_nullable_relids),
    2054             :                                                        cur_ec->ec_min_security);
    2055           4 :                 if (process_equivalence(root, &newrinfo, true))
    2056           4 :                     matchleft = true;
    2057             :             }
    2058           4 :             eq_op = select_equality_operator(cur_ec,
    2059             :                                              right_type,
    2060             :                                              cur_em->em_datatype);
    2061           4 :             if (OidIsValid(eq_op))
    2062             :             {
    2063           4 :                 newrinfo = build_implied_join_equality(eq_op,
    2064             :                                                        cur_ec->ec_collation,
    2065             :                                                        rightvar,
    2066             :                                                        cur_em->em_expr,
    2067             :                                                        bms_copy(right_relids),
    2068             :                                                        bms_copy(right_nullable_relids),
    2069             :                                                        cur_ec->ec_min_security);
    2070           4 :                 if (process_equivalence(root, &newrinfo, true))
    2071           4 :                     matchright = true;
    2072             :             }
    2073             :         }
    2074             : 
    2075             :         /*
    2076             :          * If we were able to equate both vars to constants, we're done, and
    2077             :          * we can throw away the full-join clause as redundant.  Moreover, we
    2078             :          * can remove the COALESCE entry from the EC, since the added
    2079             :          * restrictions ensure it will always have the expected value. (We
    2080             :          * don't bother trying to update ec_relids or ec_sources.)
    2081             :          */
    2082           4 :         if (matchleft && matchright)
    2083             :         {
    2084           4 :             cur_ec->ec_members = list_delete_ptr(cur_ec->ec_members, coal_em);
    2085           4 :             return true;
    2086             :         }
    2087             : 
    2088             :         /*
    2089             :          * Otherwise, fall out of the search loop, since we know the COALESCE
    2090             :          * appears in at most one EC (XXX might stop being true if we allow
    2091             :          * stripping of coercions above?)
    2092             :          */
    2093           0 :         break;
    2094             :     }
    2095             : 
    2096         672 :     return false;               /* failed to make any deduction */
    2097             : }
    2098             : 
    2099             : 
    2100             : /*
    2101             :  * exprs_known_equal
    2102             :  *    Detect whether two expressions are known equal due to equivalence
    2103             :  *    relationships.
    2104             :  *
    2105             :  * Actually, this only shows that the expressions are equal according
    2106             :  * to some opfamily's notion of equality --- but we only use it for
    2107             :  * selectivity estimation, so a fuzzy idea of equality is OK.
    2108             :  *
    2109             :  * Note: does not bother to check for "equal(item1, item2)"; caller must
    2110             :  * check that case if it's possible to pass identical items.
    2111             :  */
    2112             : bool
    2113         674 : exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
    2114             : {
    2115             :     ListCell   *lc1;
    2116             : 
    2117        4346 :     foreach(lc1, root->eq_classes)
    2118             :     {
    2119        3728 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
    2120        3728 :         bool        item1member = false;
    2121        3728 :         bool        item2member = false;
    2122             :         ListCell   *lc2;
    2123             : 
    2124             :         /* Never match to a volatile EC */
    2125        3728 :         if (ec->ec_has_volatile)
    2126           0 :             continue;
    2127             : 
    2128       14080 :         foreach(lc2, ec->ec_members)
    2129             :         {
    2130       10408 :             EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
    2131             : 
    2132       10408 :             if (em->em_is_child)
    2133        5632 :                 continue;       /* ignore children here */
    2134        4776 :             if (equal(item1, em->em_expr))
    2135         422 :                 item1member = true;
    2136        4354 :             else if (equal(item2, em->em_expr))
    2137         478 :                 item2member = true;
    2138             :             /* Exit as soon as equality is proven */
    2139        4776 :             if (item1member && item2member)
    2140          56 :                 return true;
    2141             :         }
    2142             :     }
    2143         618 :     return false;
    2144             : }
    2145             : 
    2146             : 
    2147             : /*
    2148             :  * match_eclasses_to_foreign_key_col
    2149             :  *    See whether a foreign key column match is proven by any eclass.
    2150             :  *
    2151             :  * If the referenced and referencing Vars of the fkey's colno'th column are
    2152             :  * known equal due to any eclass, return that eclass; otherwise return NULL.
    2153             :  * (In principle there might be more than one matching eclass if multiple
    2154             :  * collations are involved, but since collation doesn't matter for equality,
    2155             :  * we ignore that fine point here.)  This is much like exprs_known_equal,
    2156             :  * except that we insist on the comparison operator matching the eclass, so
    2157             :  * that the result is definite not approximate.
    2158             :  */
    2159             : EquivalenceClass *
    2160        1320 : match_eclasses_to_foreign_key_col(PlannerInfo *root,
    2161             :                                   ForeignKeyOptInfo *fkinfo,
    2162             :                                   int colno)
    2163             : {
    2164        1320 :     Index       var1varno = fkinfo->con_relid;
    2165        1320 :     AttrNumber  var1attno = fkinfo->conkey[colno];
    2166        1320 :     Index       var2varno = fkinfo->ref_relid;
    2167        1320 :     AttrNumber  var2attno = fkinfo->confkey[colno];
    2168        1320 :     Oid         eqop = fkinfo->conpfeqop[colno];
    2169        1320 :     RelOptInfo *rel1 = root->simple_rel_array[var1varno];
    2170        1320 :     RelOptInfo *rel2 = root->simple_rel_array[var2varno];
    2171        1320 :     List       *opfamilies = NIL;   /* compute only if needed */
    2172             :     Bitmapset  *matching_ecs;
    2173             :     int         i;
    2174             : 
    2175             :     /* Consider only eclasses mentioning both relations */
    2176             :     Assert(root->ec_merging_done);
    2177             :     Assert(IS_SIMPLE_REL(rel1));
    2178             :     Assert(IS_SIMPLE_REL(rel2));
    2179        1320 :     matching_ecs = bms_intersect(rel1->eclass_indexes,
    2180        1320 :                                  rel2->eclass_indexes);
    2181             : 
    2182        1320 :     i = -1;
    2183        1320 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
    2184             :     {
    2185         166 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
    2186             :                                                              i);
    2187         166 :         bool        item1member = false;
    2188         166 :         bool        item2member = false;
    2189             :         ListCell   *lc2;
    2190             : 
    2191             :         /* Never match to a volatile EC */
    2192         166 :         if (ec->ec_has_volatile)
    2193           0 :             continue;
    2194             :         /* Note: it seems okay to match to "broken" eclasses here */
    2195             : 
    2196         332 :         foreach(lc2, ec->ec_members)
    2197             :         {
    2198         332 :             EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
    2199             :             Var        *var;
    2200             : 
    2201         332 :             if (em->em_is_child)
    2202           0 :                 continue;       /* ignore children here */
    2203             : 
    2204             :             /* EM must be a Var, possibly with RelabelType */
    2205         332 :             var = (Var *) em->em_expr;
    2206         332 :             while (var && IsA(var, RelabelType))
    2207           0 :                 var = (Var *) ((RelabelType *) var)->arg;
    2208         332 :             if (!(var && IsA(var, Var)))
    2209           0 :                 continue;
    2210             : 
    2211             :             /* Match? */
    2212         332 :             if (var->varno == var1varno && var->varattno == var1attno)
    2213         166 :                 item1member = true;
    2214         166 :             else if (var->varno == var2varno && var->varattno == var2attno)
    2215         166 :                 item2member = true;
    2216             : 
    2217             :             /* Have we found both PK and FK column in this EC? */
    2218         332 :             if (item1member && item2member)
    2219             :             {
    2220             :                 /*
    2221             :                  * Succeed if eqop matches EC's opfamilies.  We could test
    2222             :                  * this before scanning the members, but it's probably cheaper
    2223             :                  * to test for member matches first.
    2224             :                  */
    2225         166 :                 if (opfamilies == NIL)  /* compute if we didn't already */
    2226         166 :                     opfamilies = get_mergejoin_opfamilies(eqop);
    2227         166 :                 if (equal(opfamilies, ec->ec_opfamilies))
    2228         166 :                     return ec;
    2229             :                 /* Otherwise, done with this EC, move on to the next */
    2230           0 :                 break;
    2231             :             }
    2232             :         }
    2233             :     }
    2234        1154 :     return NULL;
    2235             : }
    2236             : 
    2237             : 
    2238             : /*
    2239             :  * add_child_rel_equivalences
    2240             :  *    Search for EC members that reference the root parent of child_rel, and
    2241             :  *    add transformed members referencing the child_rel.
    2242             :  *
    2243             :  * Note that this function won't be called at all unless we have at least some
    2244             :  * reason to believe that the EC members it generates will be useful.
    2245             :  *
    2246             :  * parent_rel and child_rel could be derived from appinfo, but since the
    2247             :  * caller has already computed them, we might as well just pass them in.
    2248             :  *
    2249             :  * The passed-in AppendRelInfo is not used when the parent_rel is not a
    2250             :  * top-level baserel, since it shows the mapping from the parent_rel but
    2251             :  * we need to translate EC expressions that refer to the top-level parent.
    2252             :  * Using it is faster than using adjust_appendrel_attrs_multilevel(), though,
    2253             :  * so we prefer it when we can.
    2254             :  */
    2255             : void
    2256       15540 : add_child_rel_equivalences(PlannerInfo *root,
    2257             :                            AppendRelInfo *appinfo,
    2258             :                            RelOptInfo *parent_rel,
    2259             :                            RelOptInfo *child_rel)
    2260             : {
    2261       15540 :     Relids      top_parent_relids = child_rel->top_parent_relids;
    2262       15540 :     Relids      child_relids = child_rel->relids;
    2263             :     int         i;
    2264             : 
    2265             :     /*
    2266             :      * EC merging should be complete already, so we can use the parent rel's
    2267             :      * eclass_indexes to avoid searching all of root->eq_classes.
    2268             :      */
    2269             :     Assert(root->ec_merging_done);
    2270             :     Assert(IS_SIMPLE_REL(parent_rel));
    2271             : 
    2272       15540 :     i = -1;
    2273       42534 :     while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
    2274             :     {
    2275       26994 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
    2276             :         int         num_members;
    2277             : 
    2278             :         /*
    2279             :          * If this EC contains a volatile expression, then generating child
    2280             :          * EMs would be downright dangerous, so skip it.  We rely on a
    2281             :          * volatile EC having only one EM.
    2282             :          */
    2283       26994 :         if (cur_ec->ec_has_volatile)
    2284           0 :             continue;
    2285             : 
    2286             :         /* Sanity check eclass_indexes only contain ECs for parent_rel */
    2287             :         Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
    2288             : 
    2289             :         /*
    2290             :          * We don't use foreach() here because there's no point in scanning
    2291             :          * newly-added child members, so we can stop after the last
    2292             :          * pre-existing EC member.
    2293             :          */
    2294       26994 :         num_members = list_length(cur_ec->ec_members);
    2295      125826 :         for (int pos = 0; pos < num_members; pos++)
    2296             :         {
    2297       98832 :             EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
    2298             : 
    2299       98832 :             if (cur_em->em_is_const)
    2300        2212 :                 continue;       /* ignore consts here */
    2301             : 
    2302             :             /*
    2303             :              * We consider only original EC members here, not
    2304             :              * already-transformed child members.  Otherwise, if some original
    2305             :              * member expression references more than one appendrel, we'd get
    2306             :              * an O(N^2) explosion of useless derived expressions for
    2307             :              * combinations of children.  (But add_child_join_rel_equivalences
    2308             :              * may add targeted combinations for partitionwise-join purposes.)
    2309             :              */
    2310       96620 :             if (cur_em->em_is_child)
    2311       61522 :                 continue;       /* ignore children here */
    2312             : 
    2313             :             /* Does this member reference child's topmost parent rel? */
    2314       35098 :             if (bms_overlap(cur_em->em_relids, top_parent_relids))
    2315             :             {
    2316             :                 /* Yes, generate transformed child version */
    2317             :                 Expr       *child_expr;
    2318             :                 Relids      new_relids;
    2319             :                 Relids      new_nullable_relids;
    2320             : 
    2321       27018 :                 if (parent_rel->reloptkind == RELOPT_BASEREL)
    2322             :                 {
    2323             :                     /* Simple single-level transformation */
    2324             :                     child_expr = (Expr *)
    2325       22242 :                         adjust_appendrel_attrs(root,
    2326       22242 :                                                (Node *) cur_em->em_expr,
    2327             :                                                1, &appinfo);
    2328             :                 }
    2329             :                 else
    2330             :                 {
    2331             :                     /* Must do multi-level transformation */
    2332             :                     child_expr = (Expr *)
    2333        4776 :                         adjust_appendrel_attrs_multilevel(root,
    2334        4776 :                                                           (Node *) cur_em->em_expr,
    2335             :                                                           child_relids,
    2336             :                                                           top_parent_relids);
    2337             :                 }
    2338             : 
    2339             :                 /*
    2340             :                  * Transform em_relids to match.  Note we do *not* do
    2341             :                  * pull_varnos(child_expr) here, as for example the
    2342             :                  * transformation might have substituted a constant, but we
    2343             :                  * don't want the child member to be marked as constant.
    2344             :                  */
    2345       27018 :                 new_relids = bms_difference(cur_em->em_relids,
    2346             :                                             top_parent_relids);
    2347       27018 :                 new_relids = bms_add_members(new_relids, child_relids);
    2348             : 
    2349             :                 /*
    2350             :                  * And likewise for nullable_relids.  Note this code assumes
    2351             :                  * parent and child relids are singletons.
    2352             :                  */
    2353       27018 :                 new_nullable_relids = cur_em->em_nullable_relids;
    2354       27018 :                 if (bms_overlap(new_nullable_relids, top_parent_relids))
    2355             :                 {
    2356         576 :                     new_nullable_relids = bms_difference(new_nullable_relids,
    2357             :                                                          top_parent_relids);
    2358         576 :                     new_nullable_relids = bms_add_members(new_nullable_relids,
    2359             :                                                           child_relids);
    2360             :                 }
    2361             : 
    2362       27018 :                 (void) add_eq_member(cur_ec, child_expr,
    2363             :                                      new_relids, new_nullable_relids,
    2364             :                                      true, cur_em->em_datatype);
    2365             : 
    2366             :                 /* Record this EC index for the child rel */
    2367       27018 :                 child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
    2368             :             }
    2369             :         }
    2370             :     }
    2371       15540 : }
    2372             : 
    2373             : /*
    2374             :  * add_child_join_rel_equivalences
    2375             :  *    Like add_child_rel_equivalences(), but for joinrels
    2376             :  *
    2377             :  * Here we find the ECs relevant to the top parent joinrel and add transformed
    2378             :  * member expressions that refer to this child joinrel.
    2379             :  *
    2380             :  * Note that this function won't be called at all unless we have at least some
    2381             :  * reason to believe that the EC members it generates will be useful.
    2382             :  */
    2383             : void
    2384        2740 : add_child_join_rel_equivalences(PlannerInfo *root,
    2385             :                                 int nappinfos, AppendRelInfo **appinfos,
    2386             :                                 RelOptInfo *parent_joinrel,
    2387             :                                 RelOptInfo *child_joinrel)
    2388             : {
    2389        2740 :     Relids      top_parent_relids = child_joinrel->top_parent_relids;
    2390        2740 :     Relids      child_relids = child_joinrel->relids;
    2391             :     Bitmapset  *matching_ecs;
    2392             :     int         i;
    2393             : 
    2394             :     Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
    2395             : 
    2396             :     /* We need consider only ECs that mention the parent joinrel */
    2397        2740 :     matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
    2398             : 
    2399        2740 :     i = -1;
    2400       11720 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
    2401             :     {
    2402        8980 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
    2403             :         int         num_members;
    2404             : 
    2405             :         /*
    2406             :          * If this EC contains a volatile expression, then generating child
    2407             :          * EMs would be downright dangerous, so skip it.  We rely on a
    2408             :          * volatile EC having only one EM.
    2409             :          */
    2410        8980 :         if (cur_ec->ec_has_volatile)
    2411           0 :             continue;
    2412             : 
    2413             :         /* Sanity check on get_eclass_indexes_for_relids result */
    2414             :         Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
    2415             : 
    2416             :         /*
    2417             :          * We don't use foreach() here because there's no point in scanning
    2418             :          * newly-added child members, so we can stop after the last
    2419             :          * pre-existing EC member.
    2420             :          */
    2421        8980 :         num_members = list_length(cur_ec->ec_members);
    2422       65840 :         for (int pos = 0; pos < num_members; pos++)
    2423             :         {
    2424       56860 :             EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
    2425             : 
    2426       56860 :             if (cur_em->em_is_const)
    2427        1480 :                 continue;       /* ignore consts here */
    2428             : 
    2429             :             /*
    2430             :              * We consider only original EC members here, not
    2431             :              * already-transformed child members.
    2432             :              */
    2433       55380 :             if (cur_em->em_is_child)
    2434       43624 :                 continue;       /* ignore children here */
    2435             : 
    2436             :             /*
    2437             :              * We may ignore expressions that reference a single baserel,
    2438             :              * because add_child_rel_equivalences should have handled them.
    2439             :              */
    2440       11756 :             if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
    2441       11444 :                 continue;
    2442             : 
    2443             :             /* Does this member reference child's topmost parent rel? */
    2444         312 :             if (bms_overlap(cur_em->em_relids, top_parent_relids))
    2445             :             {
    2446             :                 /* Yes, generate transformed child version */
    2447             :                 Expr       *child_expr;
    2448             :                 Relids      new_relids;
    2449             :                 Relids      new_nullable_relids;
    2450             : 
    2451         312 :                 if (parent_joinrel->reloptkind == RELOPT_JOINREL)
    2452             :                 {
    2453             :                     /* Simple single-level transformation */
    2454             :                     child_expr = (Expr *)
    2455         312 :                         adjust_appendrel_attrs(root,
    2456         312 :                                                (Node *) cur_em->em_expr,
    2457             :                                                nappinfos, appinfos);
    2458             :                 }
    2459             :                 else
    2460             :                 {
    2461             :                     /* Must do multi-level transformation */
    2462             :                     Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
    2463             :                     child_expr = (Expr *)
    2464           0 :                         adjust_appendrel_attrs_multilevel(root,
    2465           0 :                                                           (Node *) cur_em->em_expr,
    2466             :                                                           child_relids,
    2467             :                                                           top_parent_relids);
    2468             :                 }
    2469             : 
    2470             :                 /*
    2471             :                  * Transform em_relids to match.  Note we do *not* do
    2472             :                  * pull_varnos(child_expr) here, as for example the
    2473             :                  * transformation might have substituted a constant, but we
    2474             :                  * don't want the child member to be marked as constant.
    2475             :                  */
    2476         312 :                 new_relids = bms_difference(cur_em->em_relids,
    2477             :                                             top_parent_relids);
    2478         312 :                 new_relids = bms_add_members(new_relids, child_relids);
    2479             : 
    2480             :                 /*
    2481             :                  * For nullable_relids, we must selectively replace parent
    2482             :                  * nullable relids with child ones.
    2483             :                  */
    2484         312 :                 new_nullable_relids = cur_em->em_nullable_relids;
    2485         312 :                 if (bms_overlap(new_nullable_relids, top_parent_relids))
    2486             :                     new_nullable_relids =
    2487         312 :                         adjust_child_relids_multilevel(root,
    2488             :                                                        new_nullable_relids,
    2489             :                                                        child_relids,
    2490             :                                                        top_parent_relids);
    2491             : 
    2492         312 :                 (void) add_eq_member(cur_ec, child_expr,
    2493             :                                      new_relids, new_nullable_relids,
    2494             :                                      true, cur_em->em_datatype);
    2495             :             }
    2496             :         }
    2497             :     }
    2498        2740 : }
    2499             : 
    2500             : 
    2501             : /*
    2502             :  * generate_implied_equalities_for_column
    2503             :  *    Create EC-derived joinclauses usable with a specific column.
    2504             :  *
    2505             :  * This is used by indxpath.c to extract potentially indexable joinclauses
    2506             :  * from ECs, and can be used by foreign data wrappers for similar purposes.
    2507             :  * We assume that only expressions in Vars of a single table are of interest,
    2508             :  * but the caller provides a callback function to identify exactly which
    2509             :  * such expressions it would like to know about.
    2510             :  *
    2511             :  * We assume that any given table/index column could appear in only one EC.
    2512             :  * (This should be true in all but the most pathological cases, and if it
    2513             :  * isn't, we stop on the first match anyway.)  Therefore, what we return
    2514             :  * is a redundant list of clauses equating the table/index column to each of
    2515             :  * the other-relation values it is known to be equal to.  Any one of
    2516             :  * these clauses can be used to create a parameterized path, and there
    2517             :  * is no value in using more than one.  (But it *is* worthwhile to create
    2518             :  * a separate parameterized path for each one, since that leads to different
    2519             :  * join orders.)
    2520             :  *
    2521             :  * The caller can pass a Relids set of rels we aren't interested in joining
    2522             :  * to, so as to save the work of creating useless clauses.
    2523             :  */
    2524             : List *
    2525      205884 : generate_implied_equalities_for_column(PlannerInfo *root,
    2526             :                                        RelOptInfo *rel,
    2527             :                                        ec_matches_callback_type callback,
    2528             :                                        void *callback_arg,
    2529             :                                        Relids prohibited_rels)
    2530             : {
    2531      205884 :     List       *result = NIL;
    2532      205884 :     bool        is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
    2533             :     Relids      parent_relids;
    2534             :     int         i;
    2535             : 
    2536             :     /* Should be OK to rely on eclass_indexes */
    2537             :     Assert(root->ec_merging_done);
    2538             : 
    2539             :     /* Indexes are available only on base or "other" member relations. */
    2540             :     Assert(IS_SIMPLE_REL(rel));
    2541             : 
    2542             :     /* If it's a child rel, we'll need to know what its parent(s) are */
    2543      205884 :     if (is_child_rel)
    2544        6936 :         parent_relids = find_childrel_parents(root, rel);
    2545             :     else
    2546      198948 :         parent_relids = NULL;   /* not used, but keep compiler quiet */
    2547             : 
    2548      205884 :     i = -1;
    2549      532280 :     while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
    2550             :     {
    2551      367014 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
    2552             :         EquivalenceMember *cur_em;
    2553             :         ListCell   *lc2;
    2554             : 
    2555             :         /* Sanity check eclass_indexes only contain ECs for rel */
    2556             :         Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
    2557             : 
    2558             :         /*
    2559             :          * Won't generate joinclauses if const or single-member (the latter
    2560             :          * test covers the volatile case too)
    2561             :          */
    2562      367014 :         if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
    2563      175100 :             continue;
    2564             : 
    2565             :         /*
    2566             :          * Scan members, looking for a match to the target column.  Note that
    2567             :          * child EC members are considered, but only when they belong to the
    2568             :          * target relation.  (Unlike regular members, the same expression
    2569             :          * could be a child member of more than one EC.  Therefore, it's
    2570             :          * potentially order-dependent which EC a child relation's target
    2571             :          * column gets matched to.  This is annoying but it only happens in
    2572             :          * corner cases, so for now we live with just reporting the first
    2573             :          * match.  See also get_eclass_for_sort_expr.)
    2574             :          */
    2575      191914 :         cur_em = NULL;
    2576      579374 :         foreach(lc2, cur_ec->ec_members)
    2577             :         {
    2578      428122 :             cur_em = (EquivalenceMember *) lfirst(lc2);
    2579      620036 :             if (bms_equal(cur_em->em_relids, rel->relids) &&
    2580      191914 :                 callback(root, rel, cur_ec, cur_em, callback_arg))
    2581       40662 :                 break;
    2582      387460 :             cur_em = NULL;
    2583             :         }
    2584             : 
    2585      191914 :         if (!cur_em)
    2586      151252 :             continue;
    2587             : 
    2588             :         /*
    2589             :          * Found our match.  Scan the other EC members and attempt to generate
    2590             :          * joinclauses.
    2591             :          */
    2592      135920 :         foreach(lc2, cur_ec->ec_members)
    2593             :         {
    2594       95258 :             EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
    2595             :             Oid         eq_op;
    2596             :             RestrictInfo *rinfo;
    2597             : 
    2598       95258 :             if (other_em->em_is_child)
    2599       12376 :                 continue;       /* ignore children here */
    2600             : 
    2601             :             /* Make sure it'll be a join to a different rel */
    2602      126866 :             if (other_em == cur_em ||
    2603       43984 :                 bms_overlap(other_em->em_relids, rel->relids))
    2604       38938 :                 continue;
    2605             : 
    2606             :             /* Forget it if caller doesn't want joins to this rel */
    2607       43944 :             if (bms_overlap(other_em->em_relids, prohibited_rels))
    2608           4 :                 continue;
    2609             : 
    2610             :             /*
    2611             :              * Also, if this is a child rel, avoid generating a useless join
    2612             :              * to its parent rel(s).
    2613             :              */
    2614       47836 :             if (is_child_rel &&
    2615        3896 :                 bms_overlap(parent_relids, other_em->em_relids))
    2616        1772 :                 continue;
    2617             : 
    2618       42168 :             eq_op = select_equality_operator(cur_ec,
    2619             :                                              cur_em->em_datatype,
    2620             :                                              other_em->em_datatype);
    2621       42168 :             if (!OidIsValid(eq_op))
    2622           0 :                 continue;
    2623             : 
    2624             :             /* set parent_ec to mark as redundant with other joinclauses */
    2625       42168 :             rinfo = create_join_clause(root, cur_ec, eq_op,
    2626             :                                        cur_em, other_em,
    2627             :                                        cur_ec);
    2628             : 
    2629       42168 :             result = lappend(result, rinfo);
    2630             :         }
    2631             : 
    2632             :         /*
    2633             :          * If somehow we failed to create any join clauses, we might as well
    2634             :          * keep scanning the ECs for another match.  But if we did make any,
    2635             :          * we're done, because we don't want to return non-redundant clauses.
    2636             :          */
    2637       40662 :         if (result)
    2638       40618 :             break;
    2639             :     }
    2640             : 
    2641      205884 :     return result;
    2642             : }
    2643             : 
    2644             : /*
    2645             :  * have_relevant_eclass_joinclause
    2646             :  *      Detect whether there is an EquivalenceClass that could produce
    2647             :  *      a joinclause involving the two given relations.
    2648             :  *
    2649             :  * This is essentially a very cut-down version of
    2650             :  * generate_join_implied_equalities().  Note it's OK to occasionally say "yes"
    2651             :  * incorrectly.  Hence we don't bother with details like whether the lack of a
    2652             :  * cross-type operator might prevent the clause from actually being generated.
    2653             :  */
    2654             : bool
    2655       70358 : have_relevant_eclass_joinclause(PlannerInfo *root,
    2656             :                                 RelOptInfo *rel1, RelOptInfo *rel2)
    2657             : {
    2658             :     Bitmapset  *matching_ecs;
    2659             :     int         i;
    2660             : 
    2661             :     /* Examine only eclasses mentioning both rel1 and rel2 */
    2662       70358 :     matching_ecs = get_common_eclass_indexes(root, rel1->relids,
    2663             :                                              rel2->relids);
    2664             : 
    2665       70358 :     i = -1;
    2666       70358 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
    2667             :     {
    2668       58748 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
    2669             :                                                              i);
    2670             : 
    2671             :         /*
    2672             :          * Sanity check that get_common_eclass_indexes gave only ECs
    2673             :          * containing both rels.
    2674             :          */
    2675             :         Assert(bms_overlap(rel1->relids, ec->ec_relids));
    2676             :         Assert(bms_overlap(rel2->relids, ec->ec_relids));
    2677             : 
    2678             :         /*
    2679             :          * Won't generate joinclauses if single-member (this test covers the
    2680             :          * volatile case too)
    2681             :          */
    2682       58748 :         if (list_length(ec->ec_members) <= 1)
    2683           0 :             continue;
    2684             : 
    2685             :         /*
    2686             :          * We do not need to examine the individual members of the EC, because
    2687             :          * all that we care about is whether each rel overlaps the relids of
    2688             :          * at least one member, and get_common_eclass_indexes() and the single
    2689             :          * member check above are sufficient to prove that.  (As with
    2690             :          * have_relevant_joinclause(), it is not necessary that the EC be able
    2691             :          * to form a joinclause relating exactly the two given rels, only that
    2692             :          * it be able to form a joinclause mentioning both, and this will
    2693             :          * surely be true if both of them overlap ec_relids.)
    2694             :          *
    2695             :          * Note we don't test ec_broken; if we did, we'd need a separate code
    2696             :          * path to look through ec_sources.  Checking the membership anyway is
    2697             :          * OK as a possibly-overoptimistic heuristic.
    2698             :          *
    2699             :          * We don't test ec_has_const either, even though a const eclass won't
    2700             :          * generate real join clauses.  This is because if we had "WHERE a.x =
    2701             :          * b.y and a.x = 42", it is worth considering a join between a and b,
    2702             :          * since the join result is likely to be small even though it'll end
    2703             :          * up being an unqualified nestloop.
    2704             :          */
    2705             : 
    2706       58748 :         return true;
    2707             :     }
    2708             : 
    2709       11610 :     return false;
    2710             : }
    2711             : 
    2712             : 
    2713             : /*
    2714             :  * has_relevant_eclass_joinclause
    2715             :  *      Detect whether there is an EquivalenceClass that could produce
    2716             :  *      a joinclause involving the given relation and anything else.
    2717             :  *
    2718             :  * This is the same as have_relevant_eclass_joinclause with the other rel
    2719             :  * implicitly defined as "everything else in the query".
    2720             :  */
    2721             : bool
    2722       99514 : has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
    2723             : {
    2724             :     Bitmapset  *matched_ecs;
    2725             :     int         i;
    2726             : 
    2727             :     /* Examine only eclasses mentioning rel1 */
    2728       99514 :     matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
    2729             : 
    2730       99514 :     i = -1;
    2731      334990 :     while ((i = bms_next_member(matched_ecs, i)) >= 0)
    2732             :     {
    2733      269482 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
    2734             :                                                              i);
    2735             : 
    2736             :         /*
    2737             :          * Won't generate joinclauses if single-member (this test covers the
    2738             :          * volatile case too)
    2739             :          */
    2740      269482 :         if (list_length(ec->ec_members) <= 1)
    2741      122084 :             continue;
    2742             : 
    2743             :         /*
    2744             :          * Per the comment in have_relevant_eclass_joinclause, it's sufficient
    2745             :          * to find an EC that mentions both this rel and some other rel.
    2746             :          */
    2747      147398 :         if (!bms_is_subset(ec->ec_relids, rel1->relids))
    2748       34006 :             return true;
    2749             :     }
    2750             : 
    2751       65508 :     return false;
    2752             : }
    2753             : 
    2754             : 
    2755             : /*
    2756             :  * eclass_useful_for_merging
    2757             :  *    Detect whether the EC could produce any mergejoinable join clauses
    2758             :  *    against the specified relation.
    2759             :  *
    2760             :  * This is just a heuristic test and doesn't have to be exact; it's better
    2761             :  * to say "yes" incorrectly than "no".  Hence we don't bother with details
    2762             :  * like whether the lack of a cross-type operator might prevent the clause
    2763             :  * from actually being generated.
    2764             :  */
    2765             : bool
    2766      294060 : eclass_useful_for_merging(PlannerInfo *root,
    2767             :                           EquivalenceClass *eclass,
    2768             :                           RelOptInfo *rel)
    2769             : {
    2770             :     Relids      relids;
    2771             :     ListCell   *lc;
    2772             : 
    2773             :     Assert(!eclass->ec_merged);
    2774             : 
    2775             :     /*
    2776             :      * Won't generate joinclauses if const or single-member (the latter test
    2777             :      * covers the volatile case too)
    2778             :      */
    2779      294060 :     if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
    2780       35172 :         return false;
    2781             : 
    2782             :     /*
    2783             :      * Note we don't test ec_broken; if we did, we'd need a separate code path
    2784             :      * to look through ec_sources.  Checking the members anyway is OK as a
    2785             :      * possibly-overoptimistic heuristic.
    2786             :      */
    2787             : 
    2788             :     /* If specified rel is a child, we must consider the topmost parent rel */
    2789      258888 :     if (IS_OTHER_REL(rel))
    2790             :     {
    2791             :         Assert(!bms_is_empty(rel->top_parent_relids));
    2792        6428 :         relids = rel->top_parent_relids;
    2793             :     }
    2794             :     else
    2795      252460 :         relids = rel->relids;
    2796             : 
    2797             :     /* If rel already includes all members of eclass, no point in searching */
    2798      258888 :     if (bms_is_subset(eclass->ec_relids, relids))
    2799      101088 :         return false;
    2800             : 
    2801             :     /* To join, we need a member not in the given rel */
    2802      232166 :     foreach(lc, eclass->ec_members)
    2803             :     {
    2804      231878 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
    2805             : 
    2806      231878 :         if (cur_em->em_is_child)
    2807           0 :             continue;           /* ignore children here */
    2808             : 
    2809      231878 :         if (!bms_overlap(cur_em->em_relids, relids))
    2810      157512 :             return true;
    2811             :     }
    2812             : 
    2813         288 :     return false;
    2814             : }
    2815             : 
    2816             : 
    2817             : /*
    2818             :  * is_redundant_derived_clause
    2819             :  *      Test whether rinfo is derived from same EC as any clause in clauselist;
    2820             :  *      if so, it can be presumed to represent a condition that's redundant
    2821             :  *      with that member of the list.
    2822             :  */
    2823             : bool
    2824          56 : is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
    2825             : {
    2826          56 :     EquivalenceClass *parent_ec = rinfo->parent_ec;
    2827             :     ListCell   *lc;
    2828             : 
    2829             :     /* Fail if it's not a potentially-redundant clause from some EC */
    2830          56 :     if (parent_ec == NULL)
    2831          48 :         return false;
    2832             : 
    2833           8 :     foreach(lc, clauselist)
    2834             :     {
    2835           8 :         RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
    2836             : 
    2837           8 :         if (otherrinfo->parent_ec == parent_ec)
    2838           8 :             return true;
    2839             :     }
    2840             : 
    2841           0 :     return false;
    2842             : }
    2843             : 
    2844             : /*
    2845             :  * is_redundant_with_indexclauses
    2846             :  *      Test whether rinfo is redundant with any clause in the IndexClause
    2847             :  *      list.  Here, for convenience, we test both simple identity and
    2848             :  *      whether it is derived from the same EC as any member of the list.
    2849             :  */
    2850             : bool
    2851      698236 : is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
    2852             : {
    2853      698236 :     EquivalenceClass *parent_ec = rinfo->parent_ec;
    2854             :     ListCell   *lc;
    2855             : 
    2856      941794 :     foreach(lc, indexclauses)
    2857             :     {
    2858      753654 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
    2859      753654 :         RestrictInfo *otherrinfo = iclause->rinfo;
    2860             : 
    2861             :         /* If indexclause is lossy, it won't enforce the condition exactly */
    2862      753654 :         if (iclause->lossy)
    2863       16076 :             continue;
    2864             : 
    2865             :         /* Match if it's same clause (pointer equality should be enough) */
    2866      737578 :         if (rinfo == otherrinfo)
    2867      510096 :             return true;
    2868             :         /* Match if derived from same EC */
    2869      341304 :         if (parent_ec && otherrinfo->parent_ec == parent_ec)
    2870      113822 :             return true;
    2871             : 
    2872             :         /*
    2873             :          * No need to look at the derived clauses in iclause->indexquals; they
    2874             :          * couldn't match if the parent clause didn't.
    2875             :          */
    2876             :     }
    2877             : 
    2878      188140 :     return false;
    2879             : }
    2880             : 
    2881             : /*
    2882             :  * get_eclass_indexes_for_relids
    2883             :  *      Build and return a Bitmapset containing the indexes into root's
    2884             :  *      eq_classes list for all eclasses that mention any of these relids
    2885             :  */
    2886             : static Bitmapset *
    2887      490598 : get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
    2888             : {
    2889      490598 :     Bitmapset  *ec_indexes = NULL;
    2890      490598 :     int         i = -1;
    2891             : 
    2892             :     /* Should be OK to rely on eclass_indexes */
    2893             :     Assert(root->ec_merging_done);
    2894             : 
    2895     1359536 :     while ((i = bms_next_member(relids, i)) > 0)
    2896             :     {
    2897      868938 :         RelOptInfo *rel = root->simple_rel_array[i];
    2898             : 
    2899      868938 :         ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
    2900             :     }
    2901      490598 :     return ec_indexes;
    2902             : }
    2903             : 
    2904             : /*
    2905             :  * get_common_eclass_indexes
    2906             :  *      Build and return a Bitmapset containing the indexes into root's
    2907             :  *      eq_classes list for all eclasses that mention rels in both
    2908             :  *      relids1 and relids2.
    2909             :  */
    2910             : static Bitmapset *
    2911      298400 : get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
    2912             : {
    2913             :     Bitmapset  *rel1ecs;
    2914             :     Bitmapset  *rel2ecs;
    2915             :     int         relid;
    2916             : 
    2917      298400 :     rel1ecs = get_eclass_indexes_for_relids(root, relids1);
    2918             : 
    2919             :     /*
    2920             :      * We can get away with just using the relation's eclass_indexes directly
    2921             :      * when relids2 is a singleton set.
    2922             :      */
    2923      298400 :     if (bms_get_singleton_member(relids2, &relid))
    2924      208456 :         rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
    2925             :     else
    2926       89944 :         rel2ecs = get_eclass_indexes_for_relids(root, relids2);
    2927             : 
    2928             :     /* Calculate and return the common EC indexes, recycling the left input. */
    2929      298400 :     return bms_int_members(rel1ecs, rel2ecs);
    2930             : }

Generated by: LCOV version 1.13