LCOV - code coverage report
Current view: top level - src/backend/optimizer/path - equivclass.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 936 979 95.6 %
Date: 2024-04-24 15:11:43 Functions: 36 36 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-2024, 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 "rewrite/rewriteManip.h"
      33             : #include "utils/lsyscache.h"
      34             : 
      35             : 
      36             : static EquivalenceMember *add_eq_member(EquivalenceClass *ec,
      37             :                                         Expr *expr, Relids relids,
      38             :                                         JoinDomain *jdomain,
      39             :                                         EquivalenceMember *parent,
      40             :                                         Oid datatype);
      41             : static bool is_exprlist_member(Expr *node, List *exprs);
      42             : static void generate_base_implied_equalities_const(PlannerInfo *root,
      43             :                                                    EquivalenceClass *ec);
      44             : static void generate_base_implied_equalities_no_const(PlannerInfo *root,
      45             :                                                       EquivalenceClass *ec);
      46             : static void generate_base_implied_equalities_broken(PlannerInfo *root,
      47             :                                                     EquivalenceClass *ec);
      48             : static List *generate_join_implied_equalities_normal(PlannerInfo *root,
      49             :                                                      EquivalenceClass *ec,
      50             :                                                      Relids join_relids,
      51             :                                                      Relids outer_relids,
      52             :                                                      Relids inner_relids);
      53             : static List *generate_join_implied_equalities_broken(PlannerInfo *root,
      54             :                                                      EquivalenceClass *ec,
      55             :                                                      Relids nominal_join_relids,
      56             :                                                      Relids outer_relids,
      57             :                                                      Relids nominal_inner_relids,
      58             :                                                      RelOptInfo *inner_rel);
      59             : static Oid  select_equality_operator(EquivalenceClass *ec,
      60             :                                      Oid lefttype, Oid righttype);
      61             : static RestrictInfo *create_join_clause(PlannerInfo *root,
      62             :                                         EquivalenceClass *ec, Oid opno,
      63             :                                         EquivalenceMember *leftem,
      64             :                                         EquivalenceMember *rightem,
      65             :                                         EquivalenceClass *parent_ec);
      66             : static bool reconsider_outer_join_clause(PlannerInfo *root,
      67             :                                          OuterJoinClauseInfo *ojcinfo,
      68             :                                          bool outer_on_left);
      69             : static bool reconsider_full_join_clause(PlannerInfo *root,
      70             :                                         OuterJoinClauseInfo *ojcinfo);
      71             : static JoinDomain *find_join_domain(PlannerInfo *root, Relids relids);
      72             : static Bitmapset *get_eclass_indexes_for_relids(PlannerInfo *root,
      73             :                                                 Relids relids);
      74             : static Bitmapset *get_common_eclass_indexes(PlannerInfo *root, Relids relids1,
      75             :                                             Relids relids2);
      76             : 
      77             : 
      78             : /*
      79             :  * process_equivalence
      80             :  *    The given clause has a mergejoinable operator and is not an outer-join
      81             :  *    qualification, so its two sides can be considered equal
      82             :  *    anywhere they are both computable; moreover that equality can be
      83             :  *    extended transitively.  Record this knowledge in the EquivalenceClass
      84             :  *    data structure, if applicable.  Returns true if successful, false if not
      85             :  *    (in which case caller should treat the clause as ordinary, not an
      86             :  *    equivalence).
      87             :  *
      88             :  * In some cases, although we cannot convert a clause into EquivalenceClass
      89             :  * knowledge, we can still modify it to a more useful form than the original.
      90             :  * Then, *p_restrictinfo will be replaced by a new RestrictInfo, which is what
      91             :  * the caller should use for further processing.
      92             :  *
      93             :  * jdomain is the join domain within which the given clause was found.
      94             :  * This limits the applicability of deductions from the EquivalenceClass,
      95             :  * as described in optimizer/README.
      96             :  *
      97             :  * We 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      247770 : process_equivalence(PlannerInfo *root,
     119             :                     RestrictInfo **p_restrictinfo,
     120             :                     JoinDomain *jdomain)
     121             : {
     122      247770 :     RestrictInfo *restrictinfo = *p_restrictinfo;
     123      247770 :     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             :     List       *opfamilies;
     133             :     EquivalenceClass *ec1,
     134             :                *ec2;
     135             :     EquivalenceMember *em1,
     136             :                *em2;
     137             :     ListCell   *lc1;
     138             :     int         ec2_idx;
     139             : 
     140             :     /* Should not already be marked as having generated an eclass */
     141             :     Assert(restrictinfo->left_ec == NULL);
     142             :     Assert(restrictinfo->right_ec == NULL);
     143             : 
     144             :     /* Reject if it is potentially postponable by security considerations */
     145      247770 :     if (restrictinfo->security_level > 0 && !restrictinfo->leakproof)
     146         202 :         return false;
     147             : 
     148             :     /* Extract info from given clause */
     149             :     Assert(is_opclause(clause));
     150      247568 :     opno = ((OpExpr *) clause)->opno;
     151      247568 :     collation = ((OpExpr *) clause)->inputcollid;
     152      247568 :     item1 = (Expr *) get_leftop(clause);
     153      247568 :     item2 = (Expr *) get_rightop(clause);
     154      247568 :     item1_relids = restrictinfo->left_relids;
     155      247568 :     item2_relids = restrictinfo->right_relids;
     156             : 
     157             :     /*
     158             :      * Ensure both input expressions expose the desired collation (their types
     159             :      * should be OK already); see comments for canonicalize_ec_expression.
     160             :      */
     161      247568 :     item1 = canonicalize_ec_expression(item1,
     162             :                                        exprType((Node *) item1),
     163             :                                        collation);
     164      247568 :     item2 = canonicalize_ec_expression(item2,
     165             :                                        exprType((Node *) item2),
     166             :                                        collation);
     167             : 
     168             :     /*
     169             :      * Clauses of the form X=X cannot be translated into EquivalenceClasses.
     170             :      * We'd either end up with a single-entry EC, losing the knowledge that
     171             :      * the clause was present at all, or else make an EC with duplicate
     172             :      * entries, causing other issues.
     173             :      */
     174      247568 :     if (equal(item1, item2))
     175             :     {
     176             :         /*
     177             :          * If the operator is strict, then the clause can be treated as just
     178             :          * "X IS NOT NULL".  (Since we know we are considering a top-level
     179             :          * qual, we can ignore the difference between FALSE and NULL results.)
     180             :          * It's worth making the conversion because we'll typically get a much
     181             :          * better selectivity estimate than we would for X=X.
     182             :          *
     183             :          * If the operator is not strict, we can't be sure what it will do
     184             :          * with NULLs, so don't attempt to optimize it.
     185             :          */
     186          54 :         set_opfuncid((OpExpr *) clause);
     187          54 :         if (func_strict(((OpExpr *) clause)->opfuncid))
     188             :         {
     189          54 :             NullTest   *ntest = makeNode(NullTest);
     190             : 
     191          54 :             ntest->arg = item1;
     192          54 :             ntest->nulltesttype = IS_NOT_NULL;
     193          54 :             ntest->argisrow = false; /* correct even if composite arg */
     194          54 :             ntest->location = -1;
     195             : 
     196          54 :             *p_restrictinfo =
     197          54 :                 make_restrictinfo(root,
     198             :                                   (Expr *) ntest,
     199          54 :                                   restrictinfo->is_pushed_down,
     200          54 :                                   restrictinfo->has_clone,
     201          54 :                                   restrictinfo->is_clone,
     202          54 :                                   restrictinfo->pseudoconstant,
     203             :                                   restrictinfo->security_level,
     204             :                                   NULL,
     205             :                                   restrictinfo->incompatible_relids,
     206             :                                   restrictinfo->outer_relids);
     207             :         }
     208          54 :         return false;
     209             :     }
     210             : 
     211             :     /*
     212             :      * We use the declared input types of the operator, not exprType() of the
     213             :      * inputs, as the nominal datatypes for opfamily lookup.  This presumes
     214             :      * that btree operators are always registered with amoplefttype and
     215             :      * amoprighttype equal to their declared input types.  We will need this
     216             :      * info anyway to build EquivalenceMember nodes, and by extracting it now
     217             :      * we can use type comparisons to short-circuit some equal() tests.
     218             :      */
     219      247514 :     op_input_types(opno, &item1_type, &item2_type);
     220             : 
     221      247514 :     opfamilies = restrictinfo->mergeopfamilies;
     222             : 
     223             :     /*
     224             :      * Sweep through the existing EquivalenceClasses looking for matches to
     225             :      * item1 and item2.  These are the possible outcomes:
     226             :      *
     227             :      * 1. We find both in the same EC.  The equivalence is already known, so
     228             :      * there's nothing to do.
     229             :      *
     230             :      * 2. We find both in different ECs.  Merge the two ECs together.
     231             :      *
     232             :      * 3. We find just one.  Add the other to its EC.
     233             :      *
     234             :      * 4. We find neither.  Make a new, two-entry EC.
     235             :      *
     236             :      * Note: since all ECs are built through this process or the similar
     237             :      * search in get_eclass_for_sort_expr(), it's impossible that we'd match
     238             :      * an item in more than one existing nonvolatile EC.  So it's okay to stop
     239             :      * at the first match.
     240             :      */
     241      247514 :     ec1 = ec2 = NULL;
     242      247514 :     em1 = em2 = NULL;
     243      247514 :     ec2_idx = -1;
     244      393660 :     foreach(lc1, root->eq_classes)
     245             :     {
     246      146188 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
     247             :         ListCell   *lc2;
     248             : 
     249             :         /* Never match to a volatile EC */
     250      146188 :         if (cur_ec->ec_has_volatile)
     251           0 :             continue;
     252             : 
     253             :         /*
     254             :          * The collation has to match; check this first since it's cheaper
     255             :          * than the opfamily comparison.
     256             :          */
     257      146188 :         if (collation != cur_ec->ec_collation)
     258       11472 :             continue;
     259             : 
     260             :         /*
     261             :          * A "match" requires matching sets of btree opfamilies.  Use of
     262             :          * equal() for this test has implications discussed in the comments
     263             :          * for get_mergejoin_opfamilies().
     264             :          */
     265      134716 :         if (!equal(opfamilies, cur_ec->ec_opfamilies))
     266       34040 :             continue;
     267             : 
     268      304022 :         foreach(lc2, cur_ec->ec_members)
     269             :         {
     270      203388 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
     271             : 
     272             :             Assert(!cur_em->em_is_child);    /* no children yet */
     273             : 
     274             :             /*
     275             :              * Match constants only within the same JoinDomain (see
     276             :              * optimizer/README).
     277             :              */
     278      203388 :             if (cur_em->em_is_const && cur_em->em_jdomain != jdomain)
     279        3878 :                 continue;
     280             : 
     281      199510 :             if (!ec1 &&
     282      375836 :                 item1_type == cur_em->em_datatype &&
     283      187792 :                 equal(item1, cur_em->em_expr))
     284             :             {
     285       15384 :                 ec1 = cur_ec;
     286       15384 :                 em1 = cur_em;
     287       15384 :                 if (ec2)
     288          18 :                     break;
     289             :             }
     290             : 
     291      199492 :             if (!ec2 &&
     292      393816 :                 item2_type == cur_em->em_datatype &&
     293      196716 :                 equal(item2, cur_em->em_expr))
     294             :             {
     295        4706 :                 ec2 = cur_ec;
     296        4706 :                 ec2_idx = foreach_current_index(lc1);
     297        4706 :                 em2 = cur_em;
     298        4706 :                 if (ec1)
     299          24 :                     break;
     300             :             }
     301             :         }
     302             : 
     303      100676 :         if (ec1 && ec2)
     304          42 :             break;
     305             :     }
     306             : 
     307             :     /* Sweep finished, what did we find? */
     308             : 
     309      247514 :     if (ec1 && ec2)
     310             :     {
     311             :         /* If case 1, nothing to do, except add to sources */
     312          42 :         if (ec1 == ec2)
     313             :         {
     314          12 :             ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
     315          12 :             ec1->ec_min_security = Min(ec1->ec_min_security,
     316             :                                        restrictinfo->security_level);
     317          12 :             ec1->ec_max_security = Max(ec1->ec_max_security,
     318             :                                        restrictinfo->security_level);
     319             :             /* mark the RI as associated with this eclass */
     320          12 :             restrictinfo->left_ec = ec1;
     321          12 :             restrictinfo->right_ec = ec1;
     322             :             /* mark the RI as usable with this pair of EMs */
     323          12 :             restrictinfo->left_em = em1;
     324          12 :             restrictinfo->right_em = em2;
     325          12 :             return true;
     326             :         }
     327             : 
     328             :         /*
     329             :          * Case 2: need to merge ec1 and ec2.  This should never happen after
     330             :          * the ECs have reached canonical state; otherwise, pathkeys could be
     331             :          * rendered non-canonical by the merge, and relation eclass indexes
     332             :          * would get broken by removal of an eq_classes list entry.
     333             :          */
     334          30 :         if (root->ec_merging_done)
     335           0 :             elog(ERROR, "too late to merge equivalence classes");
     336             : 
     337             :         /*
     338             :          * We add ec2's items to ec1, then set ec2's ec_merged link to point
     339             :          * to ec1 and remove ec2 from the eq_classes list.  We cannot simply
     340             :          * delete ec2 because that could leave dangling pointers in existing
     341             :          * PathKeys.  We leave it behind with a link so that the merged EC can
     342             :          * be found.
     343             :          */
     344          30 :         ec1->ec_members = list_concat(ec1->ec_members, ec2->ec_members);
     345          30 :         ec1->ec_sources = list_concat(ec1->ec_sources, ec2->ec_sources);
     346          30 :         ec1->ec_derives = list_concat(ec1->ec_derives, ec2->ec_derives);
     347          30 :         ec1->ec_relids = bms_join(ec1->ec_relids, ec2->ec_relids);
     348          30 :         ec1->ec_has_const |= ec2->ec_has_const;
     349             :         /* can't need to set has_volatile */
     350          30 :         ec1->ec_min_security = Min(ec1->ec_min_security,
     351             :                                    ec2->ec_min_security);
     352          30 :         ec1->ec_max_security = Max(ec1->ec_max_security,
     353             :                                    ec2->ec_max_security);
     354          30 :         ec2->ec_merged = ec1;
     355          30 :         root->eq_classes = list_delete_nth_cell(root->eq_classes, ec2_idx);
     356             :         /* just to avoid debugging confusion w/ dangling pointers: */
     357          30 :         ec2->ec_members = NIL;
     358          30 :         ec2->ec_sources = NIL;
     359          30 :         ec2->ec_derives = NIL;
     360          30 :         ec2->ec_relids = NULL;
     361          30 :         ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
     362          30 :         ec1->ec_min_security = Min(ec1->ec_min_security,
     363             :                                    restrictinfo->security_level);
     364          30 :         ec1->ec_max_security = Max(ec1->ec_max_security,
     365             :                                    restrictinfo->security_level);
     366             :         /* mark the RI as associated with this eclass */
     367          30 :         restrictinfo->left_ec = ec1;
     368          30 :         restrictinfo->right_ec = ec1;
     369             :         /* mark the RI as usable with this pair of EMs */
     370          30 :         restrictinfo->left_em = em1;
     371          30 :         restrictinfo->right_em = em2;
     372             :     }
     373      247472 :     else if (ec1)
     374             :     {
     375             :         /* Case 3: add item2 to ec1 */
     376       15342 :         em2 = add_eq_member(ec1, item2, item2_relids,
     377             :                             jdomain, NULL, item2_type);
     378       15342 :         ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
     379       15342 :         ec1->ec_min_security = Min(ec1->ec_min_security,
     380             :                                    restrictinfo->security_level);
     381       15342 :         ec1->ec_max_security = Max(ec1->ec_max_security,
     382             :                                    restrictinfo->security_level);
     383             :         /* mark the RI as associated with this eclass */
     384       15342 :         restrictinfo->left_ec = ec1;
     385       15342 :         restrictinfo->right_ec = ec1;
     386             :         /* mark the RI as usable with this pair of EMs */
     387       15342 :         restrictinfo->left_em = em1;
     388       15342 :         restrictinfo->right_em = em2;
     389             :     }
     390      232130 :     else if (ec2)
     391             :     {
     392             :         /* Case 3: add item1 to ec2 */
     393        4664 :         em1 = add_eq_member(ec2, item1, item1_relids,
     394             :                             jdomain, NULL, item1_type);
     395        4664 :         ec2->ec_sources = lappend(ec2->ec_sources, restrictinfo);
     396        4664 :         ec2->ec_min_security = Min(ec2->ec_min_security,
     397             :                                    restrictinfo->security_level);
     398        4664 :         ec2->ec_max_security = Max(ec2->ec_max_security,
     399             :                                    restrictinfo->security_level);
     400             :         /* mark the RI as associated with this eclass */
     401        4664 :         restrictinfo->left_ec = ec2;
     402        4664 :         restrictinfo->right_ec = ec2;
     403             :         /* mark the RI as usable with this pair of EMs */
     404        4664 :         restrictinfo->left_em = em1;
     405        4664 :         restrictinfo->right_em = em2;
     406             :     }
     407             :     else
     408             :     {
     409             :         /* Case 4: make a new, two-entry EC */
     410      227466 :         EquivalenceClass *ec = makeNode(EquivalenceClass);
     411             : 
     412      227466 :         ec->ec_opfamilies = opfamilies;
     413      227466 :         ec->ec_collation = collation;
     414      227466 :         ec->ec_members = NIL;
     415      227466 :         ec->ec_sources = list_make1(restrictinfo);
     416      227466 :         ec->ec_derives = NIL;
     417      227466 :         ec->ec_relids = NULL;
     418      227466 :         ec->ec_has_const = false;
     419      227466 :         ec->ec_has_volatile = false;
     420      227466 :         ec->ec_broken = false;
     421      227466 :         ec->ec_sortref = 0;
     422      227466 :         ec->ec_min_security = restrictinfo->security_level;
     423      227466 :         ec->ec_max_security = restrictinfo->security_level;
     424      227466 :         ec->ec_merged = NULL;
     425      227466 :         em1 = add_eq_member(ec, item1, item1_relids,
     426             :                             jdomain, NULL, item1_type);
     427      227466 :         em2 = add_eq_member(ec, item2, item2_relids,
     428             :                             jdomain, NULL, item2_type);
     429             : 
     430      227466 :         root->eq_classes = lappend(root->eq_classes, ec);
     431             : 
     432             :         /* mark the RI as associated with this eclass */
     433      227466 :         restrictinfo->left_ec = ec;
     434      227466 :         restrictinfo->right_ec = ec;
     435             :         /* mark the RI as usable with this pair of EMs */
     436      227466 :         restrictinfo->left_em = em1;
     437      227466 :         restrictinfo->right_em = em2;
     438             :     }
     439             : 
     440      247502 :     return true;
     441             : }
     442             : 
     443             : /*
     444             :  * canonicalize_ec_expression
     445             :  *
     446             :  * This function ensures that the expression exposes the expected type and
     447             :  * collation, so that it will be equal() to other equivalence-class expressions
     448             :  * that it ought to be equal() to.
     449             :  *
     450             :  * The rule for datatypes is that the exposed type should match what it would
     451             :  * be for an input to an operator of the EC's opfamilies; which is usually
     452             :  * the declared input type of the operator, but in the case of polymorphic
     453             :  * operators no relabeling is wanted (compare the behavior of parse_coerce.c).
     454             :  * Expressions coming in from quals will generally have the right type
     455             :  * already, but expressions coming from indexkeys may not (because they are
     456             :  * represented without any explicit relabel in pg_index), and the same problem
     457             :  * occurs for sort expressions (because the parser is likewise cavalier about
     458             :  * putting relabels on them).  Such cases will be binary-compatible with the
     459             :  * real operators, so adding a RelabelType is sufficient.
     460             :  *
     461             :  * Also, the expression's exposed collation must match the EC's collation.
     462             :  * This is important because in comparisons like "foo < bar COLLATE baz",
     463             :  * only one of the expressions has the correct exposed collation as we receive
     464             :  * it from the parser.  Forcing both of them to have it ensures that all
     465             :  * variant spellings of such a construct behave the same.  Again, we can
     466             :  * stick on a RelabelType to force the right exposed collation.  (It might
     467             :  * work to not label the collation at all in EC members, but this is risky
     468             :  * since some parts of the system expect exprCollation() to deliver the
     469             :  * right answer for a sort key.)
     470             :  */
     471             : Expr *
     472     2223516 : canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
     473             : {
     474     2223516 :     Oid         expr_type = exprType((Node *) expr);
     475             : 
     476             :     /*
     477             :      * For a polymorphic-input-type opclass, just keep the same exposed type.
     478             :      * RECORD opclasses work like polymorphic-type ones for this purpose.
     479             :      */
     480     2223516 :     if (IsPolymorphicType(req_type) || req_type == RECORDOID)
     481        3452 :         req_type = expr_type;
     482             : 
     483             :     /*
     484             :      * No work if the expression exposes the right type/collation already.
     485             :      */
     486     4412384 :     if (expr_type != req_type ||
     487     2188868 :         exprCollation((Node *) expr) != req_collation)
     488             :     {
     489             :         /*
     490             :          * If we have to change the type of the expression, set typmod to -1,
     491             :          * since the new type may not have the same typmod interpretation.
     492             :          * When we only have to change collation, preserve the exposed typmod.
     493             :          */
     494             :         int32       req_typmod;
     495             : 
     496       35464 :         if (expr_type != req_type)
     497       34648 :             req_typmod = -1;
     498             :         else
     499         816 :             req_typmod = exprTypmod((Node *) expr);
     500             : 
     501             :         /*
     502             :          * Use applyRelabelType so that we preserve const-flatness.  This is
     503             :          * important since eval_const_expressions has already been applied.
     504             :          */
     505       35464 :         expr = (Expr *) applyRelabelType((Node *) expr,
     506             :                                          req_type, req_typmod, req_collation,
     507             :                                          COERCE_IMPLICIT_CAST, -1, false);
     508             :     }
     509             : 
     510     2223516 :     return expr;
     511             : }
     512             : 
     513             : /*
     514             :  * add_eq_member - build a new EquivalenceMember and add it to an EC
     515             :  */
     516             : static EquivalenceMember *
     517      756412 : add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids,
     518             :               JoinDomain *jdomain, EquivalenceMember *parent, Oid datatype)
     519             : {
     520      756412 :     EquivalenceMember *em = makeNode(EquivalenceMember);
     521             : 
     522      756412 :     em->em_expr = expr;
     523      756412 :     em->em_relids = relids;
     524      756412 :     em->em_is_const = false;
     525      756412 :     em->em_is_child = (parent != NULL);
     526      756412 :     em->em_datatype = datatype;
     527      756412 :     em->em_jdomain = jdomain;
     528      756412 :     em->em_parent = parent;
     529             : 
     530      756412 :     if (bms_is_empty(relids))
     531             :     {
     532             :         /*
     533             :          * No Vars, assume it's a pseudoconstant.  This is correct for entries
     534             :          * generated from process_equivalence(), because a WHERE clause can't
     535             :          * contain aggregates or SRFs, and non-volatility was checked before
     536             :          * process_equivalence() ever got called.  But
     537             :          * get_eclass_for_sort_expr() has to work harder.  We put the tests
     538             :          * there not here to save cycles in the equivalence case.
     539             :          */
     540             :         Assert(!parent);
     541      187944 :         em->em_is_const = true;
     542      187944 :         ec->ec_has_const = true;
     543             :         /* it can't affect ec_relids */
     544             :     }
     545      568468 :     else if (!parent)           /* child members don't add to ec_relids */
     546             :     {
     547      497310 :         ec->ec_relids = bms_add_members(ec->ec_relids, relids);
     548             :     }
     549      756412 :     ec->ec_members = lappend(ec->ec_members, em);
     550             : 
     551      756412 :     return em;
     552             : }
     553             : 
     554             : 
     555             : /*
     556             :  * get_eclass_for_sort_expr
     557             :  *    Given an expression and opfamily/collation info, find an existing
     558             :  *    equivalence class it is a member of; if none, optionally build a new
     559             :  *    single-member EquivalenceClass for it.
     560             :  *
     561             :  * sortref is the SortGroupRef of the originating SortGroupClause, if any,
     562             :  * or zero if not.  (It should never be zero if the expression is volatile!)
     563             :  *
     564             :  * If rel is not NULL, it identifies a specific relation we're considering
     565             :  * a path for, and indicates that child EC members for that relation can be
     566             :  * considered.  Otherwise child members are ignored.  (Note: since child EC
     567             :  * members aren't guaranteed unique, a non-NULL value means that there could
     568             :  * be more than one EC that matches the expression; if so it's order-dependent
     569             :  * which one you get.  This is annoying but it only happens in corner cases,
     570             :  * so for now we live with just reporting the first match.  See also
     571             :  * generate_implied_equalities_for_column and match_pathkeys_to_index.)
     572             :  *
     573             :  * If create_it is true, we'll build a new EquivalenceClass when there is no
     574             :  * match.  If create_it is false, we just return NULL when no match.
     575             :  *
     576             :  * This can be used safely both before and after EquivalenceClass merging;
     577             :  * since it never causes merging it does not invalidate any existing ECs
     578             :  * or PathKeys.  However, ECs added after path generation has begun are
     579             :  * of limited usefulness, so usually it's best to create them beforehand.
     580             :  *
     581             :  * Note: opfamilies must be chosen consistently with the way
     582             :  * process_equivalence() would do; that is, generated from a mergejoinable
     583             :  * equality operator.  Else we might fail to detect valid equivalences,
     584             :  * generating poor (but not incorrect) plans.
     585             :  */
     586             : EquivalenceClass *
     587     1655208 : get_eclass_for_sort_expr(PlannerInfo *root,
     588             :                          Expr *expr,
     589             :                          List *opfamilies,
     590             :                          Oid opcintype,
     591             :                          Oid collation,
     592             :                          Index sortref,
     593             :                          Relids rel,
     594             :                          bool create_it)
     595             : {
     596             :     JoinDomain *jdomain;
     597             :     Relids      expr_relids;
     598             :     EquivalenceClass *newec;
     599             :     EquivalenceMember *newem;
     600             :     ListCell   *lc1;
     601             :     MemoryContext oldcontext;
     602             : 
     603             :     /*
     604             :      * Ensure the expression exposes the correct type and collation.
     605             :      */
     606     1655208 :     expr = canonicalize_ec_expression(expr, opcintype, collation);
     607             : 
     608             :     /*
     609             :      * Since SortGroupClause nodes are top-level expressions (GROUP BY, ORDER
     610             :      * BY, etc), they can be presumed to belong to the top JoinDomain.
     611             :      */
     612     1655208 :     jdomain = linitial_node(JoinDomain, root->join_domains);
     613             : 
     614             :     /*
     615             :      * Scan through the existing EquivalenceClasses for a match
     616             :      */
     617     5434344 :     foreach(lc1, root->eq_classes)
     618             :     {
     619     4700890 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
     620             :         ListCell   *lc2;
     621             : 
     622             :         /*
     623             :          * Never match to a volatile EC, except when we are looking at another
     624             :          * reference to the same volatile SortGroupClause.
     625             :          */
     626     4700890 :         if (cur_ec->ec_has_volatile &&
     627          36 :             (sortref == 0 || sortref != cur_ec->ec_sortref))
     628         524 :             continue;
     629             : 
     630     4700366 :         if (collation != cur_ec->ec_collation)
     631     1282144 :             continue;
     632     3418222 :         if (!equal(opfamilies, cur_ec->ec_opfamilies))
     633      680578 :             continue;
     634             : 
     635     6210858 :         foreach(lc2, cur_ec->ec_members)
     636             :         {
     637     4394968 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
     638             : 
     639             :             /*
     640             :              * Ignore child members unless they match the request.
     641             :              */
     642     4394968 :             if (cur_em->em_is_child &&
     643      294056 :                 !bms_equal(cur_em->em_relids, rel))
     644      221640 :                 continue;
     645             : 
     646             :             /*
     647             :              * Match constants only within the same JoinDomain (see
     648             :              * optimizer/README).
     649             :              */
     650     4173328 :             if (cur_em->em_is_const && cur_em->em_jdomain != jdomain)
     651       75382 :                 continue;
     652             : 
     653     8166110 :             if (opcintype == cur_em->em_datatype &&
     654     4068164 :                 equal(expr, cur_em->em_expr))
     655             :             {
     656             :                 /*
     657             :                  * Match!
     658             :                  *
     659             :                  * Copy the sortref if it wasn't set yet.  That may happen if
     660             :                  * the ec was constructed from a WHERE clause, i.e. it doesn't
     661             :                  * have a target reference at all.
     662             :                  */
     663      921754 :                 if (cur_ec->ec_sortref == 0 && sortref > 0)
     664        8216 :                     cur_ec->ec_sortref = sortref;
     665      921754 :                 return cur_ec;
     666             :             }
     667             :         }
     668             :     }
     669             : 
     670             :     /* No match; does caller want a NULL result? */
     671      733454 :     if (!create_it)
     672      523138 :         return NULL;
     673             : 
     674             :     /*
     675             :      * OK, build a new single-member EC
     676             :      *
     677             :      * Here, we must be sure that we construct the EC in the right context.
     678             :      */
     679      210316 :     oldcontext = MemoryContextSwitchTo(root->planner_cxt);
     680             : 
     681      210316 :     newec = makeNode(EquivalenceClass);
     682      210316 :     newec->ec_opfamilies = list_copy(opfamilies);
     683      210316 :     newec->ec_collation = collation;
     684      210316 :     newec->ec_members = NIL;
     685      210316 :     newec->ec_sources = NIL;
     686      210316 :     newec->ec_derives = NIL;
     687      210316 :     newec->ec_relids = NULL;
     688      210316 :     newec->ec_has_const = false;
     689      210316 :     newec->ec_has_volatile = contain_volatile_functions((Node *) expr);
     690      210316 :     newec->ec_broken = false;
     691      210316 :     newec->ec_sortref = sortref;
     692      210316 :     newec->ec_min_security = UINT_MAX;
     693      210316 :     newec->ec_max_security = 0;
     694      210316 :     newec->ec_merged = NULL;
     695             : 
     696      210316 :     if (newec->ec_has_volatile && sortref == 0) /* should not happen */
     697           0 :         elog(ERROR, "volatile EquivalenceClass has no sortref");
     698             : 
     699             :     /*
     700             :      * Get the precise set of relids appearing in the expression.
     701             :      */
     702      210316 :     expr_relids = pull_varnos(root, (Node *) expr);
     703             : 
     704      210316 :     newem = add_eq_member(newec, copyObject(expr), expr_relids,
     705             :                           jdomain, NULL, opcintype);
     706             : 
     707             :     /*
     708             :      * add_eq_member doesn't check for volatile functions, set-returning
     709             :      * functions, aggregates, or window functions, but such could appear in
     710             :      * sort expressions; so we have to check whether its const-marking was
     711             :      * correct.
     712             :      */
     713      210316 :     if (newec->ec_has_const)
     714             :     {
     715       17514 :         if (newec->ec_has_volatile ||
     716       17242 :             expression_returns_set((Node *) expr) ||
     717       16916 :             contain_agg_clause((Node *) expr) ||
     718        8370 :             contain_window_function((Node *) expr))
     719             :         {
     720         454 :             newec->ec_has_const = false;
     721         454 :             newem->em_is_const = false;
     722             :         }
     723             :     }
     724             : 
     725      210316 :     root->eq_classes = lappend(root->eq_classes, newec);
     726             : 
     727             :     /*
     728             :      * If EC merging is already complete, we have to mop up by adding the new
     729             :      * EC to the eclass_indexes of the relation(s) mentioned in it.
     730             :      */
     731      210316 :     if (root->ec_merging_done)
     732             :     {
     733      117220 :         int         ec_index = list_length(root->eq_classes) - 1;
     734      117220 :         int         i = -1;
     735             : 
     736      225302 :         while ((i = bms_next_member(newec->ec_relids, i)) > 0)
     737             :         {
     738      108082 :             RelOptInfo *rel = root->simple_rel_array[i];
     739             : 
     740      108082 :             if (rel == NULL)    /* must be an outer join */
     741             :             {
     742             :                 Assert(bms_is_member(i, root->outer_join_rels));
     743        5794 :                 continue;
     744             :             }
     745             : 
     746             :             Assert(rel->reloptkind == RELOPT_BASEREL);
     747             : 
     748      102288 :             rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
     749             :                                                  ec_index);
     750             :         }
     751             :     }
     752             : 
     753      210316 :     MemoryContextSwitchTo(oldcontext);
     754             : 
     755      210316 :     return newec;
     756             : }
     757             : 
     758             : /*
     759             :  * find_ec_member_matching_expr
     760             :  *      Locate an EquivalenceClass member matching the given expr, if any;
     761             :  *      return NULL if no match.
     762             :  *
     763             :  * "Matching" is defined as "equal after stripping RelabelTypes".
     764             :  * This is used for identifying sort expressions, and we need to allow
     765             :  * binary-compatible relabeling for some cases involving binary-compatible
     766             :  * sort operators.
     767             :  *
     768             :  * Child EC members are ignored unless they belong to given 'relids'.
     769             :  */
     770             : EquivalenceMember *
     771      263540 : find_ec_member_matching_expr(EquivalenceClass *ec,
     772             :                              Expr *expr,
     773             :                              Relids relids)
     774             : {
     775             :     ListCell   *lc;
     776             : 
     777             :     /* We ignore binary-compatible relabeling on both ends */
     778      283134 :     while (expr && IsA(expr, RelabelType))
     779       19594 :         expr = ((RelabelType *) expr)->arg;
     780             : 
     781      571988 :     foreach(lc, ec->ec_members)
     782             :     {
     783      423928 :         EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
     784             :         Expr       *emexpr;
     785             : 
     786             :         /*
     787             :          * We shouldn't be trying to sort by an equivalence class that
     788             :          * contains a constant, so no need to consider such cases any further.
     789             :          */
     790      423928 :         if (em->em_is_const)
     791           0 :             continue;
     792             : 
     793             :         /*
     794             :          * Ignore child members unless they belong to the requested rel.
     795             :          */
     796      423928 :         if (em->em_is_child &&
     797      144024 :             !bms_is_subset(em->em_relids, relids))
     798      138326 :             continue;
     799             : 
     800             :         /*
     801             :          * Match if same expression (after stripping relabel).
     802             :          */
     803      285602 :         emexpr = em->em_expr;
     804      290428 :         while (emexpr && IsA(emexpr, RelabelType))
     805        4826 :             emexpr = ((RelabelType *) emexpr)->arg;
     806             : 
     807      285602 :         if (equal(emexpr, expr))
     808      115480 :             return em;
     809             :     }
     810             : 
     811      148060 :     return NULL;
     812             : }
     813             : 
     814             : /*
     815             :  * find_computable_ec_member
     816             :  *      Locate an EquivalenceClass member that can be computed from the
     817             :  *      expressions appearing in "exprs"; return NULL if no match.
     818             :  *
     819             :  * "exprs" can be either a list of bare expression trees, or a list of
     820             :  * TargetEntry nodes.  Either way, it should contain Vars and possibly
     821             :  * Aggrefs and WindowFuncs, which are matched to the corresponding elements
     822             :  * of the EquivalenceClass's expressions.
     823             :  *
     824             :  * Unlike find_ec_member_matching_expr, there's no special provision here
     825             :  * for binary-compatible relabeling.  This is intentional: if we have to
     826             :  * compute an expression in this way, setrefs.c is going to insist on exact
     827             :  * matches of Vars to the source tlist.
     828             :  *
     829             :  * Child EC members are ignored unless they belong to given 'relids'.
     830             :  * Also, non-parallel-safe expressions are ignored if 'require_parallel_safe'.
     831             :  *
     832             :  * Note: some callers pass root == NULL for notational reasons.  This is OK
     833             :  * when require_parallel_safe is false.
     834             :  */
     835             : EquivalenceMember *
     836        2396 : find_computable_ec_member(PlannerInfo *root,
     837             :                           EquivalenceClass *ec,
     838             :                           List *exprs,
     839             :                           Relids relids,
     840             :                           bool require_parallel_safe)
     841             : {
     842             :     ListCell   *lc;
     843             : 
     844        7510 :     foreach(lc, ec->ec_members)
     845             :     {
     846        5552 :         EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
     847             :         List       *exprvars;
     848             :         ListCell   *lc2;
     849             : 
     850             :         /*
     851             :          * We shouldn't be trying to sort by an equivalence class that
     852             :          * contains a constant, so no need to consider such cases any further.
     853             :          */
     854        5552 :         if (em->em_is_const)
     855           0 :             continue;
     856             : 
     857             :         /*
     858             :          * Ignore child members unless they belong to the requested rel.
     859             :          */
     860        5552 :         if (em->em_is_child &&
     861        2934 :             !bms_is_subset(em->em_relids, relids))
     862        2766 :             continue;
     863             : 
     864             :         /*
     865             :          * Match if all Vars and quasi-Vars are available in "exprs".
     866             :          */
     867        2786 :         exprvars = pull_var_clause((Node *) em->em_expr,
     868             :                                    PVC_INCLUDE_AGGREGATES |
     869             :                                    PVC_INCLUDE_WINDOWFUNCS |
     870             :                                    PVC_INCLUDE_PLACEHOLDERS);
     871        3376 :         foreach(lc2, exprvars)
     872             :         {
     873        2914 :             if (!is_exprlist_member(lfirst(lc2), exprs))
     874        2324 :                 break;
     875             :         }
     876        2786 :         list_free(exprvars);
     877        2786 :         if (lc2)
     878        2324 :             continue;           /* we hit a non-available Var */
     879             : 
     880             :         /*
     881             :          * If requested, reject expressions that are not parallel-safe.  We
     882             :          * check this last because it's a rather expensive test.
     883             :          */
     884         462 :         if (require_parallel_safe &&
     885         122 :             !is_parallel_safe(root, (Node *) em->em_expr))
     886          24 :             continue;
     887             : 
     888         438 :         return em;              /* found usable expression */
     889             :     }
     890             : 
     891        1958 :     return NULL;
     892             : }
     893             : 
     894             : /*
     895             :  * is_exprlist_member
     896             :  *    Subroutine for find_computable_ec_member: is "node" in "exprs"?
     897             :  *
     898             :  * Per the requirements of that function, "exprs" might or might not have
     899             :  * TargetEntry superstructure.
     900             :  */
     901             : static bool
     902        2914 : is_exprlist_member(Expr *node, List *exprs)
     903             : {
     904             :     ListCell   *lc;
     905             : 
     906        8124 :     foreach(lc, exprs)
     907             :     {
     908        5800 :         Expr       *expr = (Expr *) lfirst(lc);
     909             : 
     910        5800 :         if (expr && IsA(expr, TargetEntry))
     911        1172 :             expr = ((TargetEntry *) expr)->expr;
     912             : 
     913        5800 :         if (equal(node, expr))
     914         590 :             return true;
     915             :     }
     916        2324 :     return false;
     917             : }
     918             : 
     919             : /*
     920             :  * relation_can_be_sorted_early
     921             :  *      Can this relation be sorted on this EC before the final output step?
     922             :  *
     923             :  * To succeed, we must find an EC member that prepare_sort_from_pathkeys knows
     924             :  * how to sort on, given the rel's reltarget as input.  There are also a few
     925             :  * additional constraints based on the fact that the desired sort will be done
     926             :  * "early", within the scan/join part of the plan.  Also, non-parallel-safe
     927             :  * expressions are ignored if 'require_parallel_safe'.
     928             :  *
     929             :  * At some point we might want to return the identified EquivalenceMember,
     930             :  * but for now, callers only want to know if there is one.
     931             :  */
     932             : bool
     933        9994 : relation_can_be_sorted_early(PlannerInfo *root, RelOptInfo *rel,
     934             :                              EquivalenceClass *ec, bool require_parallel_safe)
     935             : {
     936        9994 :     PathTarget *target = rel->reltarget;
     937             :     EquivalenceMember *em;
     938             :     ListCell   *lc;
     939             : 
     940             :     /*
     941             :      * Reject volatile ECs immediately; such sorts must always be postponed.
     942             :      */
     943        9994 :     if (ec->ec_has_volatile)
     944          72 :         return false;
     945             : 
     946             :     /*
     947             :      * Try to find an EM directly matching some reltarget member.
     948             :      */
     949       20286 :     foreach(lc, target->exprs)
     950             :     {
     951       18230 :         Expr       *targetexpr = (Expr *) lfirst(lc);
     952             : 
     953       18230 :         em = find_ec_member_matching_expr(ec, targetexpr, rel->relids);
     954       18230 :         if (!em)
     955       10364 :             continue;
     956             : 
     957             :         /*
     958             :          * Reject expressions involving set-returning functions, as those
     959             :          * can't be computed early either.  (Note: this test and the following
     960             :          * one are effectively checking properties of targetexpr, so there's
     961             :          * no point in asking whether some other EC member would be better.)
     962             :          */
     963        7866 :         if (expression_returns_set((Node *) em->em_expr))
     964           0 :             continue;
     965             : 
     966             :         /*
     967             :          * If requested, reject expressions that are not parallel-safe.  We
     968             :          * check this last because it's a rather expensive test.
     969             :          */
     970        7866 :         if (require_parallel_safe &&
     971        7866 :             !is_parallel_safe(root, (Node *) em->em_expr))
     972           0 :             continue;
     973             : 
     974        7866 :         return true;
     975             :     }
     976             : 
     977             :     /*
     978             :      * Try to find an expression computable from the reltarget.
     979             :      */
     980        2056 :     em = find_computable_ec_member(root, ec, target->exprs, rel->relids,
     981             :                                    require_parallel_safe);
     982        2056 :     if (!em)
     983        1958 :         return false;
     984             : 
     985             :     /*
     986             :      * Reject expressions involving set-returning functions, as those can't be
     987             :      * computed early either.  (There's no point in looking for another EC
     988             :      * member in this case; since SRFs can't appear in WHERE, they cannot
     989             :      * belong to multi-member ECs.)
     990             :      */
     991          98 :     if (expression_returns_set((Node *) em->em_expr))
     992          12 :         return false;
     993             : 
     994          86 :     return true;
     995             : }
     996             : 
     997             : /*
     998             :  * generate_base_implied_equalities
     999             :  *    Generate any restriction clauses that we can deduce from equivalence
    1000             :  *    classes.
    1001             :  *
    1002             :  * When an EC contains pseudoconstants, our strategy is to generate
    1003             :  * "member = const1" clauses where const1 is the first constant member, for
    1004             :  * every other member (including other constants).  If we are able to do this
    1005             :  * then we don't need any "var = var" comparisons because we've successfully
    1006             :  * constrained all the vars at their points of creation.  If we fail to
    1007             :  * generate any of these clauses due to lack of cross-type operators, we fall
    1008             :  * back to the "ec_broken" strategy described below.  (XXX if there are
    1009             :  * multiple constants of different types, it's possible that we might succeed
    1010             :  * in forming all the required clauses if we started from a different const
    1011             :  * member; but this seems a sufficiently hokey corner case to not be worth
    1012             :  * spending lots of cycles on.)
    1013             :  *
    1014             :  * For ECs that contain no pseudoconstants, we generate derived clauses
    1015             :  * "member1 = member2" for each pair of members belonging to the same base
    1016             :  * relation (actually, if there are more than two for the same base relation,
    1017             :  * we only need enough clauses to link each to each other).  This provides
    1018             :  * the base case for the recursion: each row emitted by a base relation scan
    1019             :  * will constrain all computable members of the EC to be equal.  As each
    1020             :  * join path is formed, we'll add additional derived clauses on-the-fly
    1021             :  * to maintain this invariant (see generate_join_implied_equalities).
    1022             :  *
    1023             :  * If the opfamilies used by the EC do not provide complete sets of cross-type
    1024             :  * equality operators, it is possible that we will fail to generate a clause
    1025             :  * that must be generated to maintain the invariant.  (An example: given
    1026             :  * "WHERE a.x = b.y AND b.y = a.z", the scheme breaks down if we cannot
    1027             :  * generate "a.x = a.z" as a restriction clause for A.)  In this case we mark
    1028             :  * the EC "ec_broken" and fall back to regurgitating its original source
    1029             :  * RestrictInfos at appropriate times.  We do not try to retract any derived
    1030             :  * clauses already generated from the broken EC, so the resulting plan could
    1031             :  * be poor due to bad selectivity estimates caused by redundant clauses.  But
    1032             :  * the correct solution to that is to fix the opfamilies ...
    1033             :  *
    1034             :  * Equality clauses derived by this function are passed off to
    1035             :  * process_implied_equality (in plan/initsplan.c) to be inserted into the
    1036             :  * restrictinfo datastructures.  Note that this must be called after initial
    1037             :  * scanning of the quals and before Path construction begins.
    1038             :  *
    1039             :  * We make no attempt to avoid generating duplicate RestrictInfos here: we
    1040             :  * don't search ec_sources or ec_derives for matches.  It doesn't really
    1041             :  * seem worth the trouble to do so.
    1042             :  */
    1043             : void
    1044      276694 : generate_base_implied_equalities(PlannerInfo *root)
    1045             : {
    1046             :     int         ec_index;
    1047             :     ListCell   *lc;
    1048             : 
    1049             :     /*
    1050             :      * At this point, we're done absorbing knowledge of equivalences in the
    1051             :      * query, so no further EC merging should happen, and ECs remaining in the
    1052             :      * eq_classes list can be considered canonical.  (But note that it's still
    1053             :      * possible for new single-member ECs to be added through
    1054             :      * get_eclass_for_sort_expr().)
    1055             :      */
    1056      276694 :     root->ec_merging_done = true;
    1057             : 
    1058      276694 :     ec_index = 0;
    1059      597226 :     foreach(lc, root->eq_classes)
    1060             :     {
    1061      320532 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
    1062      320532 :         bool        can_generate_joinclause = false;
    1063             :         int         i;
    1064             : 
    1065             :         Assert(ec->ec_merged == NULL);   /* else shouldn't be in list */
    1066             :         Assert(!ec->ec_broken); /* not yet anyway... */
    1067             : 
    1068             :         /*
    1069             :          * Generate implied equalities that are restriction clauses.
    1070             :          * Single-member ECs won't generate any deductions, either here or at
    1071             :          * the join level.
    1072             :          */
    1073      320532 :         if (list_length(ec->ec_members) > 1)
    1074             :         {
    1075      229068 :             if (ec->ec_has_const)
    1076      178988 :                 generate_base_implied_equalities_const(root, ec);
    1077             :             else
    1078       50080 :                 generate_base_implied_equalities_no_const(root, ec);
    1079             : 
    1080             :             /* Recover if we failed to generate required derived clauses */
    1081      229068 :             if (ec->ec_broken)
    1082          30 :                 generate_base_implied_equalities_broken(root, ec);
    1083             : 
    1084             :             /* Detect whether this EC might generate join clauses */
    1085      229068 :             can_generate_joinclause =
    1086      229068 :                 (bms_membership(ec->ec_relids) == BMS_MULTIPLE);
    1087             :         }
    1088             : 
    1089             :         /*
    1090             :          * Mark the base rels cited in each eclass (which should all exist by
    1091             :          * now) with the eq_classes indexes of all eclasses mentioning them.
    1092             :          * This will let us avoid searching in subsequent lookups.  While
    1093             :          * we're at it, we can mark base rels that have pending eclass joins;
    1094             :          * this is a cheap version of has_relevant_eclass_joinclause().
    1095             :          */
    1096      320532 :         i = -1;
    1097      712242 :         while ((i = bms_next_member(ec->ec_relids, i)) > 0)
    1098             :         {
    1099      391710 :             RelOptInfo *rel = root->simple_rel_array[i];
    1100             : 
    1101      391710 :             if (rel == NULL)    /* must be an outer join */
    1102             :             {
    1103             :                 Assert(bms_is_member(i, root->outer_join_rels));
    1104        5064 :                 continue;
    1105             :             }
    1106             : 
    1107             :             Assert(rel->reloptkind == RELOPT_BASEREL);
    1108             : 
    1109      386646 :             rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
    1110             :                                                  ec_index);
    1111             : 
    1112      386646 :             if (can_generate_joinclause)
    1113      132072 :                 rel->has_eclass_joins = true;
    1114             :         }
    1115             : 
    1116      320532 :         ec_index++;
    1117             :     }
    1118      276694 : }
    1119             : 
    1120             : /*
    1121             :  * generate_base_implied_equalities when EC contains pseudoconstant(s)
    1122             :  */
    1123             : static void
    1124      178988 : generate_base_implied_equalities_const(PlannerInfo *root,
    1125             :                                        EquivalenceClass *ec)
    1126             : {
    1127      178988 :     EquivalenceMember *const_em = NULL;
    1128             :     ListCell   *lc;
    1129             : 
    1130             :     /*
    1131             :      * In the trivial case where we just had one "var = const" clause, push
    1132             :      * the original clause back into the main planner machinery.  There is
    1133             :      * nothing to be gained by doing it differently, and we save the effort to
    1134             :      * re-build and re-analyze an equality clause that will be exactly
    1135             :      * equivalent to the old one.
    1136             :      */
    1137      341202 :     if (list_length(ec->ec_members) == 2 &&
    1138      162214 :         list_length(ec->ec_sources) == 1)
    1139             :     {
    1140      162214 :         RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
    1141             : 
    1142      162214 :         distribute_restrictinfo_to_rels(root, restrictinfo);
    1143      162214 :         return;
    1144             :     }
    1145             : 
    1146             :     /*
    1147             :      * Find the constant member to use.  We prefer an actual constant to
    1148             :      * pseudo-constants (such as Params), because the constraint exclusion
    1149             :      * machinery might be able to exclude relations on the basis of generated
    1150             :      * "var = const" equalities, but "var = param" won't work for that.
    1151             :      */
    1152       37624 :     foreach(lc, ec->ec_members)
    1153             :     {
    1154       37574 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
    1155             : 
    1156       37574 :         if (cur_em->em_is_const)
    1157             :         {
    1158       16780 :             const_em = cur_em;
    1159       16780 :             if (IsA(cur_em->em_expr, Const))
    1160       16724 :                 break;
    1161             :         }
    1162             :     }
    1163             :     Assert(const_em != NULL);
    1164             : 
    1165             :     /* Generate a derived equality against each other member */
    1166       67216 :     foreach(lc, ec->ec_members)
    1167             :     {
    1168       50472 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
    1169             :         Oid         eq_op;
    1170             :         RestrictInfo *rinfo;
    1171             : 
    1172             :         Assert(!cur_em->em_is_child);    /* no children yet */
    1173       50472 :         if (cur_em == const_em)
    1174       16750 :             continue;
    1175       33722 :         eq_op = select_equality_operator(ec,
    1176             :                                          cur_em->em_datatype,
    1177             :                                          const_em->em_datatype);
    1178       33722 :         if (!OidIsValid(eq_op))
    1179             :         {
    1180             :             /* failed... */
    1181          30 :             ec->ec_broken = true;
    1182          30 :             break;
    1183             :         }
    1184             : 
    1185             :         /*
    1186             :          * We use the constant's em_jdomain as qualscope, so that if the
    1187             :          * generated clause is variable-free (i.e, both EMs are consts) it
    1188             :          * will be enforced at the join domain level.
    1189             :          */
    1190       33692 :         rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
    1191             :                                          cur_em->em_expr, const_em->em_expr,
    1192       33692 :                                          const_em->em_jdomain->jd_relids,
    1193             :                                          ec->ec_min_security,
    1194       33692 :                                          cur_em->em_is_const);
    1195             : 
    1196             :         /*
    1197             :          * If the clause didn't degenerate to a constant, fill in the correct
    1198             :          * markings for a mergejoinable clause, and save it in ec_derives. (We
    1199             :          * will not re-use such clauses directly, but selectivity estimation
    1200             :          * may consult the list later.  Note that this use of ec_derives does
    1201             :          * not overlap with its use for join clauses, since we never generate
    1202             :          * join clauses from an ec_has_const eclass.)
    1203             :          */
    1204       33692 :         if (rinfo && rinfo->mergeopfamilies)
    1205             :         {
    1206             :             /* it's not redundant, so don't set parent_ec */
    1207       33560 :             rinfo->left_ec = rinfo->right_ec = ec;
    1208       33560 :             rinfo->left_em = cur_em;
    1209       33560 :             rinfo->right_em = const_em;
    1210       33560 :             ec->ec_derives = lappend(ec->ec_derives, rinfo);
    1211             :         }
    1212             :     }
    1213             : }
    1214             : 
    1215             : /*
    1216             :  * generate_base_implied_equalities when EC contains no pseudoconstants
    1217             :  */
    1218             : static void
    1219       50080 : generate_base_implied_equalities_no_const(PlannerInfo *root,
    1220             :                                           EquivalenceClass *ec)
    1221             : {
    1222             :     EquivalenceMember **prev_ems;
    1223             :     ListCell   *lc;
    1224             : 
    1225             :     /*
    1226             :      * We scan the EC members once and track the last-seen member for each
    1227             :      * base relation.  When we see another member of the same base relation,
    1228             :      * we generate "prev_em = cur_em".  This results in the minimum number of
    1229             :      * derived clauses, but it's possible that it will fail when a different
    1230             :      * ordering would succeed.  XXX FIXME: use a UNION-FIND algorithm similar
    1231             :      * to the way we build merged ECs.  (Use a list-of-lists for each rel.)
    1232             :      */
    1233             :     prev_ems = (EquivalenceMember **)
    1234       50080 :         palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
    1235             : 
    1236      151708 :     foreach(lc, ec->ec_members)
    1237             :     {
    1238      101628 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
    1239             :         int         relid;
    1240             : 
    1241             :         Assert(!cur_em->em_is_child);    /* no children yet */
    1242      101628 :         if (!bms_get_singleton_member(cur_em->em_relids, &relid))
    1243         186 :             continue;
    1244             :         Assert(relid < root->simple_rel_array_size);
    1245             : 
    1246      101442 :         if (prev_ems[relid] != NULL)
    1247             :         {
    1248         310 :             EquivalenceMember *prev_em = prev_ems[relid];
    1249             :             Oid         eq_op;
    1250             :             RestrictInfo *rinfo;
    1251             : 
    1252         310 :             eq_op = select_equality_operator(ec,
    1253             :                                              prev_em->em_datatype,
    1254             :                                              cur_em->em_datatype);
    1255         310 :             if (!OidIsValid(eq_op))
    1256             :             {
    1257             :                 /* failed... */
    1258           0 :                 ec->ec_broken = true;
    1259           0 :                 break;
    1260             :             }
    1261             : 
    1262             :             /*
    1263             :              * The expressions aren't constants, so the passed qualscope will
    1264             :              * never be used to place the generated clause.  We just need to
    1265             :              * be sure it covers both expressions, which em_relids should do.
    1266             :              */
    1267         310 :             rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
    1268             :                                              prev_em->em_expr, cur_em->em_expr,
    1269             :                                              cur_em->em_relids,
    1270             :                                              ec->ec_min_security,
    1271             :                                              false);
    1272             : 
    1273             :             /*
    1274             :              * If the clause didn't degenerate to a constant, fill in the
    1275             :              * correct markings for a mergejoinable clause.  We don't put it
    1276             :              * in ec_derives however; we don't currently need to re-find such
    1277             :              * clauses, and we don't want to clutter that list with non-join
    1278             :              * clauses.
    1279             :              */
    1280         310 :             if (rinfo && rinfo->mergeopfamilies)
    1281             :             {
    1282             :                 /* it's not redundant, so don't set parent_ec */
    1283         310 :                 rinfo->left_ec = rinfo->right_ec = ec;
    1284         310 :                 rinfo->left_em = prev_em;
    1285         310 :                 rinfo->right_em = cur_em;
    1286             :             }
    1287             :         }
    1288      101442 :         prev_ems[relid] = cur_em;
    1289             :     }
    1290             : 
    1291       50080 :     pfree(prev_ems);
    1292             : 
    1293             :     /*
    1294             :      * We also have to make sure that all the Vars used in the member clauses
    1295             :      * will be available at any join node we might try to reference them at.
    1296             :      * For the moment we force all the Vars to be available at all join nodes
    1297             :      * for this eclass.  Perhaps this could be improved by doing some
    1298             :      * pre-analysis of which members we prefer to join, but it's no worse than
    1299             :      * what happened in the pre-8.3 code.
    1300             :      */
    1301      151708 :     foreach(lc, ec->ec_members)
    1302             :     {
    1303      101628 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
    1304      101628 :         List       *vars = pull_var_clause((Node *) cur_em->em_expr,
    1305             :                                            PVC_RECURSE_AGGREGATES |
    1306             :                                            PVC_RECURSE_WINDOWFUNCS |
    1307             :                                            PVC_INCLUDE_PLACEHOLDERS);
    1308             : 
    1309      101628 :         add_vars_to_targetlist(root, vars, ec->ec_relids);
    1310      101628 :         list_free(vars);
    1311             :     }
    1312       50080 : }
    1313             : 
    1314             : /*
    1315             :  * generate_base_implied_equalities cleanup after failure
    1316             :  *
    1317             :  * What we must do here is push any zero- or one-relation source RestrictInfos
    1318             :  * of the EC back into the main restrictinfo datastructures.  Multi-relation
    1319             :  * clauses will be regurgitated later by generate_join_implied_equalities().
    1320             :  * (We do it this way to maintain continuity with the case that ec_broken
    1321             :  * becomes set only after we've gone up a join level or two.)  However, for
    1322             :  * an EC that contains constants, we can adopt a simpler strategy and just
    1323             :  * throw back all the source RestrictInfos immediately; that works because
    1324             :  * we know that such an EC can't become broken later.  (This rule justifies
    1325             :  * ignoring ec_has_const ECs in generate_join_implied_equalities, even when
    1326             :  * they are broken.)
    1327             :  */
    1328             : static void
    1329          30 : generate_base_implied_equalities_broken(PlannerInfo *root,
    1330             :                                         EquivalenceClass *ec)
    1331             : {
    1332             :     ListCell   *lc;
    1333             : 
    1334          96 :     foreach(lc, ec->ec_sources)
    1335             :     {
    1336          66 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
    1337             : 
    1338          66 :         if (ec->ec_has_const ||
    1339           0 :             bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
    1340          66 :             distribute_restrictinfo_to_rels(root, restrictinfo);
    1341             :     }
    1342          30 : }
    1343             : 
    1344             : 
    1345             : /*
    1346             :  * generate_join_implied_equalities
    1347             :  *    Generate any join clauses that we can deduce from equivalence classes.
    1348             :  *
    1349             :  * At a join node, we must enforce restriction clauses sufficient to ensure
    1350             :  * that all equivalence-class members computable at that node are equal.
    1351             :  * Since the set of clauses to enforce can vary depending on which subset
    1352             :  * relations are the inputs, we have to compute this afresh for each join
    1353             :  * relation pair.  Hence a fresh List of RestrictInfo nodes is built and
    1354             :  * passed back on each call.
    1355             :  *
    1356             :  * In addition to its use at join nodes, this can be applied to generate
    1357             :  * eclass-based join clauses for use in a parameterized scan of a base rel.
    1358             :  * The reason for the asymmetry of specifying the inner rel as a RelOptInfo
    1359             :  * and the outer rel by Relids is that this usage occurs before we have
    1360             :  * built any join RelOptInfos.
    1361             :  *
    1362             :  * An annoying special case for parameterized scans is that the inner rel can
    1363             :  * be an appendrel child (an "other rel").  In this case we must generate
    1364             :  * appropriate clauses using child EC members.  add_child_rel_equivalences
    1365             :  * must already have been done for the child rel.
    1366             :  *
    1367             :  * The results are sufficient for use in merge, hash, and plain nestloop join
    1368             :  * methods.  We do not worry here about selecting clauses that are optimal
    1369             :  * for use in a parameterized indexscan.  indxpath.c makes its own selections
    1370             :  * of clauses to use, and if the ones we pick here are redundant with those,
    1371             :  * the extras will be eliminated at createplan time, using the parent_ec
    1372             :  * markers that we provide (see is_redundant_derived_clause()).
    1373             :  *
    1374             :  * Because the same join clauses are likely to be needed multiple times as
    1375             :  * we consider different join paths, we avoid generating multiple copies:
    1376             :  * whenever we select a particular pair of EquivalenceMembers to join,
    1377             :  * we check to see if the pair matches any original clause (in ec_sources)
    1378             :  * or previously-built clause (in ec_derives).  This saves memory and allows
    1379             :  * re-use of information cached in RestrictInfos.  We also avoid generating
    1380             :  * commutative duplicates, i.e. if the algorithm selects "a.x = b.y" but
    1381             :  * we already have "b.y = a.x", we return the existing clause.
    1382             :  *
    1383             :  * If we are considering an outer join, sjinfo is the associated OJ info,
    1384             :  * otherwise it can be NULL.
    1385             :  *
    1386             :  * join_relids should always equal bms_union(outer_relids, inner_rel->relids)
    1387             :  * plus whatever add_outer_joins_to_relids() would add.  We could simplify
    1388             :  * this function's API by computing it internally, but most callers have the
    1389             :  * value at hand anyway.
    1390             :  */
    1391             : List *
    1392      432066 : generate_join_implied_equalities(PlannerInfo *root,
    1393             :                                  Relids join_relids,
    1394             :                                  Relids outer_relids,
    1395             :                                  RelOptInfo *inner_rel,
    1396             :                                  SpecialJoinInfo *sjinfo)
    1397             : {
    1398      432066 :     List       *result = NIL;
    1399      432066 :     Relids      inner_relids = inner_rel->relids;
    1400             :     Relids      nominal_inner_relids;
    1401             :     Relids      nominal_join_relids;
    1402             :     Bitmapset  *matching_ecs;
    1403             :     int         i;
    1404             : 
    1405             :     /* If inner rel is a child, extra setup work is needed */
    1406      432066 :     if (IS_OTHER_REL(inner_rel))
    1407             :     {
    1408             :         Assert(!bms_is_empty(inner_rel->top_parent_relids));
    1409             : 
    1410             :         /* Fetch relid set for the topmost parent rel */
    1411        6832 :         nominal_inner_relids = inner_rel->top_parent_relids;
    1412             :         /* ECs will be marked with the parent's relid, not the child's */
    1413        6832 :         nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
    1414        6832 :         nominal_join_relids = add_outer_joins_to_relids(root,
    1415             :                                                         nominal_join_relids,
    1416             :                                                         sjinfo,
    1417             :                                                         NULL);
    1418             :     }
    1419             :     else
    1420             :     {
    1421      425234 :         nominal_inner_relids = inner_relids;
    1422      425234 :         nominal_join_relids = join_relids;
    1423             :     }
    1424             : 
    1425             :     /*
    1426             :      * Examine all potentially-relevant eclasses.
    1427             :      *
    1428             :      * If we are considering an outer join, we must include "join" clauses
    1429             :      * that mention either input rel plus the outer join's relid; these
    1430             :      * represent post-join filter clauses that have to be applied at this
    1431             :      * join.  We don't have infrastructure that would let us identify such
    1432             :      * eclasses cheaply, so just fall back to considering all eclasses
    1433             :      * mentioning anything in nominal_join_relids.
    1434             :      *
    1435             :      * At inner joins, we can be smarter: only consider eclasses mentioning
    1436             :      * both input rels.
    1437             :      */
    1438      432066 :     if (sjinfo && sjinfo->ojrelid != 0)
    1439       92962 :         matching_ecs = get_eclass_indexes_for_relids(root, nominal_join_relids);
    1440             :     else
    1441      339104 :         matching_ecs = get_common_eclass_indexes(root, nominal_inner_relids,
    1442             :                                                  outer_relids);
    1443             : 
    1444      432066 :     i = -1;
    1445     1263610 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
    1446             :     {
    1447      831544 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
    1448      831544 :         List       *sublist = NIL;
    1449             : 
    1450             :         /* ECs containing consts do not need any further enforcement */
    1451      831544 :         if (ec->ec_has_const)
    1452      116932 :             continue;
    1453             : 
    1454             :         /* Single-member ECs won't generate any deductions */
    1455      714612 :         if (list_length(ec->ec_members) <= 1)
    1456      428046 :             continue;
    1457             : 
    1458             :         /* Sanity check that this eclass overlaps the join */
    1459             :         Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
    1460             : 
    1461      286566 :         if (!ec->ec_broken)
    1462      286242 :             sublist = generate_join_implied_equalities_normal(root,
    1463             :                                                               ec,
    1464             :                                                               join_relids,
    1465             :                                                               outer_relids,
    1466             :                                                               inner_relids);
    1467             : 
    1468             :         /* Recover if we failed to generate required derived clauses */
    1469      286566 :         if (ec->ec_broken)
    1470         360 :             sublist = generate_join_implied_equalities_broken(root,
    1471             :                                                               ec,
    1472             :                                                               nominal_join_relids,
    1473             :                                                               outer_relids,
    1474             :                                                               nominal_inner_relids,
    1475             :                                                               inner_rel);
    1476             : 
    1477      286566 :         result = list_concat(result, sublist);
    1478             :     }
    1479             : 
    1480      432066 :     return result;
    1481             : }
    1482             : 
    1483             : /*
    1484             :  * generate_join_implied_equalities_for_ecs
    1485             :  *    As above, but consider only the listed ECs.
    1486             :  *
    1487             :  * For the sole current caller, we can assume sjinfo == NULL, that is we are
    1488             :  * not interested in outer-join filter clauses.  This might need to change
    1489             :  * in future.
    1490             :  */
    1491             : List *
    1492         890 : generate_join_implied_equalities_for_ecs(PlannerInfo *root,
    1493             :                                          List *eclasses,
    1494             :                                          Relids join_relids,
    1495             :                                          Relids outer_relids,
    1496             :                                          RelOptInfo *inner_rel)
    1497             : {
    1498         890 :     List       *result = NIL;
    1499         890 :     Relids      inner_relids = inner_rel->relids;
    1500             :     Relids      nominal_inner_relids;
    1501             :     Relids      nominal_join_relids;
    1502             :     ListCell   *lc;
    1503             : 
    1504             :     /* If inner rel is a child, extra setup work is needed */
    1505         890 :     if (IS_OTHER_REL(inner_rel))
    1506             :     {
    1507             :         Assert(!bms_is_empty(inner_rel->top_parent_relids));
    1508             : 
    1509             :         /* Fetch relid set for the topmost parent rel */
    1510           0 :         nominal_inner_relids = inner_rel->top_parent_relids;
    1511             :         /* ECs will be marked with the parent's relid, not the child's */
    1512           0 :         nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
    1513             :     }
    1514             :     else
    1515             :     {
    1516         890 :         nominal_inner_relids = inner_relids;
    1517         890 :         nominal_join_relids = join_relids;
    1518             :     }
    1519             : 
    1520        1828 :     foreach(lc, eclasses)
    1521             :     {
    1522         938 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
    1523         938 :         List       *sublist = NIL;
    1524             : 
    1525             :         /* ECs containing consts do not need any further enforcement */
    1526         938 :         if (ec->ec_has_const)
    1527           0 :             continue;
    1528             : 
    1529             :         /* Single-member ECs won't generate any deductions */
    1530         938 :         if (list_length(ec->ec_members) <= 1)
    1531           0 :             continue;
    1532             : 
    1533             :         /* We can quickly ignore any that don't overlap the join, too */
    1534         938 :         if (!bms_overlap(ec->ec_relids, nominal_join_relids))
    1535           0 :             continue;
    1536             : 
    1537         938 :         if (!ec->ec_broken)
    1538         938 :             sublist = generate_join_implied_equalities_normal(root,
    1539             :                                                               ec,
    1540             :                                                               join_relids,
    1541             :                                                               outer_relids,
    1542             :                                                               inner_relids);
    1543             : 
    1544             :         /* Recover if we failed to generate required derived clauses */
    1545         938 :         if (ec->ec_broken)
    1546           0 :             sublist = generate_join_implied_equalities_broken(root,
    1547             :                                                               ec,
    1548             :                                                               nominal_join_relids,
    1549             :                                                               outer_relids,
    1550             :                                                               nominal_inner_relids,
    1551             :                                                               inner_rel);
    1552             : 
    1553         938 :         result = list_concat(result, sublist);
    1554             :     }
    1555             : 
    1556         890 :     return result;
    1557             : }
    1558             : 
    1559             : /*
    1560             :  * generate_join_implied_equalities for a still-valid EC
    1561             :  */
    1562             : static List *
    1563      287180 : generate_join_implied_equalities_normal(PlannerInfo *root,
    1564             :                                         EquivalenceClass *ec,
    1565             :                                         Relids join_relids,
    1566             :                                         Relids outer_relids,
    1567             :                                         Relids inner_relids)
    1568             : {
    1569      287180 :     List       *result = NIL;
    1570      287180 :     List       *new_members = NIL;
    1571      287180 :     List       *outer_members = NIL;
    1572      287180 :     List       *inner_members = NIL;
    1573             :     ListCell   *lc1;
    1574             : 
    1575             :     /*
    1576             :      * First, scan the EC to identify member values that are computable at the
    1577             :      * outer rel, at the inner rel, or at this relation but not in either
    1578             :      * input rel.  The outer-rel members should already be enforced equal,
    1579             :      * likewise for the inner-rel members.  We'll need to create clauses to
    1580             :      * enforce that any newly computable members are all equal to each other
    1581             :      * as well as to at least one input member, plus enforce at least one
    1582             :      * outer-rel member equal to at least one inner-rel member.
    1583             :      */
    1584      988332 :     foreach(lc1, ec->ec_members)
    1585             :     {
    1586      701152 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
    1587             : 
    1588             :         /*
    1589             :          * We don't need to check explicitly for child EC members.  This test
    1590             :          * against join_relids will cause them to be ignored except when
    1591             :          * considering a child inner rel, which is what we want.
    1592             :          */
    1593      701152 :         if (!bms_is_subset(cur_em->em_relids, join_relids))
    1594      126374 :             continue;           /* not computable yet, or wrong child */
    1595             : 
    1596      574778 :         if (bms_is_subset(cur_em->em_relids, outer_relids))
    1597      331602 :             outer_members = lappend(outer_members, cur_em);
    1598      243176 :         else if (bms_is_subset(cur_em->em_relids, inner_relids))
    1599      241580 :             inner_members = lappend(inner_members, cur_em);
    1600             :         else
    1601        1596 :             new_members = lappend(new_members, cur_em);
    1602             :     }
    1603             : 
    1604             :     /*
    1605             :      * First, select the joinclause if needed.  We can equate any one outer
    1606             :      * member to any one inner member, but we have to find a datatype
    1607             :      * combination for which an opfamily member operator exists.  If we have
    1608             :      * choices, we prefer simple Var members (possibly with RelabelType) since
    1609             :      * these are (a) cheapest to compute at runtime and (b) most likely to
    1610             :      * have useful statistics. Also, prefer operators that are also
    1611             :      * hashjoinable.
    1612             :      */
    1613      287180 :     if (outer_members && inner_members)
    1614             :     {
    1615      229126 :         EquivalenceMember *best_outer_em = NULL;
    1616      229126 :         EquivalenceMember *best_inner_em = NULL;
    1617      229126 :         Oid         best_eq_op = InvalidOid;
    1618      229126 :         int         best_score = -1;
    1619             :         RestrictInfo *rinfo;
    1620             : 
    1621      242842 :         foreach(lc1, outer_members)
    1622             :         {
    1623      229198 :             EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
    1624             :             ListCell   *lc2;
    1625             : 
    1626      242926 :             foreach(lc2, inner_members)
    1627             :             {
    1628      229210 :                 EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
    1629             :                 Oid         eq_op;
    1630             :                 int         score;
    1631             : 
    1632      229210 :                 eq_op = select_equality_operator(ec,
    1633             :                                                  outer_em->em_datatype,
    1634             :                                                  inner_em->em_datatype);
    1635      229210 :                 if (!OidIsValid(eq_op))
    1636          36 :                     continue;
    1637      229174 :                 score = 0;
    1638      229174 :                 if (IsA(outer_em->em_expr, Var) ||
    1639       14874 :                     (IsA(outer_em->em_expr, RelabelType) &&
    1640        3346 :                      IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
    1641      217598 :                     score++;
    1642      229174 :                 if (IsA(inner_em->em_expr, Var) ||
    1643        5398 :                     (IsA(inner_em->em_expr, RelabelType) &&
    1644        2904 :                      IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
    1645      226662 :                     score++;
    1646      229174 :                 if (op_hashjoinable(eq_op,
    1647      229174 :                                     exprType((Node *) outer_em->em_expr)))
    1648      229096 :                     score++;
    1649      229174 :                 if (score > best_score)
    1650             :                 {
    1651      229090 :                     best_outer_em = outer_em;
    1652      229090 :                     best_inner_em = inner_em;
    1653      229090 :                     best_eq_op = eq_op;
    1654      229090 :                     best_score = score;
    1655      229090 :                     if (best_score == 3)
    1656      215482 :                         break;  /* no need to look further */
    1657             :                 }
    1658             :             }
    1659      229198 :             if (best_score == 3)
    1660      215482 :                 break;          /* no need to look further */
    1661             :         }
    1662      229126 :         if (best_score < 0)
    1663             :         {
    1664             :             /* failed... */
    1665          36 :             ec->ec_broken = true;
    1666          36 :             return NIL;
    1667             :         }
    1668             : 
    1669             :         /*
    1670             :          * Create clause, setting parent_ec to mark it as redundant with other
    1671             :          * joinclauses
    1672             :          */
    1673      229090 :         rinfo = create_join_clause(root, ec, best_eq_op,
    1674             :                                    best_outer_em, best_inner_em,
    1675             :                                    ec);
    1676             : 
    1677      229090 :         result = lappend(result, rinfo);
    1678             :     }
    1679             : 
    1680             :     /*
    1681             :      * Now deal with building restrictions for any expressions that involve
    1682             :      * Vars from both sides of the join.  We have to equate all of these to
    1683             :      * each other as well as to at least one old member (if any).
    1684             :      *
    1685             :      * XXX as in generate_base_implied_equalities_no_const, we could be a lot
    1686             :      * smarter here to avoid unnecessary failures in cross-type situations.
    1687             :      * For now, use the same left-to-right method used there.
    1688             :      */
    1689      287144 :     if (new_members)
    1690             :     {
    1691        1560 :         List       *old_members = list_concat(outer_members, inner_members);
    1692        1560 :         EquivalenceMember *prev_em = NULL;
    1693             :         RestrictInfo *rinfo;
    1694             : 
    1695             :         /* For now, arbitrarily take the first old_member as the one to use */
    1696        1560 :         if (old_members)
    1697        1098 :             new_members = lappend(new_members, linitial(old_members));
    1698             : 
    1699        4254 :         foreach(lc1, new_members)
    1700             :         {
    1701        2694 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
    1702             : 
    1703        2694 :             if (prev_em != NULL)
    1704             :             {
    1705             :                 Oid         eq_op;
    1706             : 
    1707        1134 :                 eq_op = select_equality_operator(ec,
    1708             :                                                  prev_em->em_datatype,
    1709             :                                                  cur_em->em_datatype);
    1710        1134 :                 if (!OidIsValid(eq_op))
    1711             :                 {
    1712             :                     /* failed... */
    1713           0 :                     ec->ec_broken = true;
    1714           0 :                     return NIL;
    1715             :                 }
    1716             :                 /* do NOT set parent_ec, this qual is not redundant! */
    1717        1134 :                 rinfo = create_join_clause(root, ec, eq_op,
    1718             :                                            prev_em, cur_em,
    1719             :                                            NULL);
    1720             : 
    1721        1134 :                 result = lappend(result, rinfo);
    1722             :             }
    1723        2694 :             prev_em = cur_em;
    1724             :         }
    1725             :     }
    1726             : 
    1727      287144 :     return result;
    1728             : }
    1729             : 
    1730             : /*
    1731             :  * generate_join_implied_equalities cleanup after failure
    1732             :  *
    1733             :  * Return any original RestrictInfos that are enforceable at this join.
    1734             :  *
    1735             :  * In the case of a child inner relation, we have to translate the
    1736             :  * original RestrictInfos from parent to child Vars.
    1737             :  */
    1738             : static List *
    1739         360 : generate_join_implied_equalities_broken(PlannerInfo *root,
    1740             :                                         EquivalenceClass *ec,
    1741             :                                         Relids nominal_join_relids,
    1742             :                                         Relids outer_relids,
    1743             :                                         Relids nominal_inner_relids,
    1744             :                                         RelOptInfo *inner_rel)
    1745             : {
    1746         360 :     List       *result = NIL;
    1747             :     ListCell   *lc;
    1748             : 
    1749         984 :     foreach(lc, ec->ec_sources)
    1750             :     {
    1751         624 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
    1752         624 :         Relids      clause_relids = restrictinfo->required_relids;
    1753             : 
    1754         624 :         if (bms_is_subset(clause_relids, nominal_join_relids) &&
    1755         336 :             !bms_is_subset(clause_relids, outer_relids) &&
    1756         312 :             !bms_is_subset(clause_relids, nominal_inner_relids))
    1757         312 :             result = lappend(result, restrictinfo);
    1758             :     }
    1759             : 
    1760             :     /*
    1761             :      * If we have to translate, just brute-force apply adjust_appendrel_attrs
    1762             :      * to all the RestrictInfos at once.  This will result in returning
    1763             :      * RestrictInfos that are not listed in ec_derives, but there shouldn't be
    1764             :      * any duplication, and it's a sufficiently narrow corner case that we
    1765             :      * shouldn't sweat too much over it anyway.
    1766             :      *
    1767             :      * Since inner_rel might be an indirect descendant of the baserel
    1768             :      * mentioned in the ec_sources clauses, we have to be prepared to apply
    1769             :      * multiple levels of Var translation.
    1770             :      */
    1771         360 :     if (IS_OTHER_REL(inner_rel) && result != NIL)
    1772         162 :         result = (List *) adjust_appendrel_attrs_multilevel(root,
    1773             :                                                             (Node *) result,
    1774             :                                                             inner_rel,
    1775         162 :                                                             inner_rel->top_parent);
    1776             : 
    1777         360 :     return result;
    1778             : }
    1779             : 
    1780             : 
    1781             : /*
    1782             :  * select_equality_operator
    1783             :  *    Select a suitable equality operator for comparing two EC members
    1784             :  *
    1785             :  * Returns InvalidOid if no operator can be found for this datatype combination
    1786             :  */
    1787             : static Oid
    1788      348906 : select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
    1789             : {
    1790             :     ListCell   *lc;
    1791             : 
    1792      348972 :     foreach(lc, ec->ec_opfamilies)
    1793             :     {
    1794      348906 :         Oid         opfamily = lfirst_oid(lc);
    1795             :         Oid         opno;
    1796             : 
    1797      348906 :         opno = get_opfamily_member(opfamily, lefttype, righttype,
    1798             :                                    BTEqualStrategyNumber);
    1799      348906 :         if (!OidIsValid(opno))
    1800          66 :             continue;
    1801             :         /* If no barrier quals in query, don't worry about leaky operators */
    1802      348840 :         if (ec->ec_max_security == 0)
    1803      348840 :             return opno;
    1804             :         /* Otherwise, insist that selected operators be leakproof */
    1805         428 :         if (get_func_leakproof(get_opcode(opno)))
    1806         428 :             return opno;
    1807             :     }
    1808          66 :     return InvalidOid;
    1809             : }
    1810             : 
    1811             : 
    1812             : /*
    1813             :  * create_join_clause
    1814             :  *    Find or make a RestrictInfo comparing the two given EC members
    1815             :  *    with the given operator (or, possibly, its commutator, because
    1816             :  *    the ordering of the operands in the result is not guaranteed).
    1817             :  *
    1818             :  * parent_ec is either equal to ec (if the clause is a potentially-redundant
    1819             :  * join clause) or NULL (if not).  We have to treat this as part of the
    1820             :  * match requirements --- it's possible that a clause comparing the same two
    1821             :  * EMs is a join clause in one join path and a restriction clause in another.
    1822             :  */
    1823             : static RestrictInfo *
    1824      316780 : create_join_clause(PlannerInfo *root,
    1825             :                    EquivalenceClass *ec, Oid opno,
    1826             :                    EquivalenceMember *leftem,
    1827             :                    EquivalenceMember *rightem,
    1828             :                    EquivalenceClass *parent_ec)
    1829             : {
    1830             :     RestrictInfo *rinfo;
    1831      316780 :     RestrictInfo *parent_rinfo = NULL;
    1832             :     ListCell   *lc;
    1833             :     MemoryContext oldcontext;
    1834             : 
    1835             :     /*
    1836             :      * Search to see if we already built a RestrictInfo for this pair of
    1837             :      * EquivalenceMembers.  We can use either original source clauses or
    1838             :      * previously-derived clauses, and a commutator clause is acceptable.
    1839             :      *
    1840             :      * We used to verify that opno matches, but that seems redundant: even if
    1841             :      * it's not identical, it'd better have the same effects, or the operator
    1842             :      * families we're using are broken.
    1843             :      */
    1844      687654 :     foreach(lc, ec->ec_sources)
    1845             :     {
    1846      371846 :         rinfo = (RestrictInfo *) lfirst(lc);
    1847      371846 :         if (rinfo->left_em == leftem &&
    1848      168980 :             rinfo->right_em == rightem &&
    1849      150158 :             rinfo->parent_ec == parent_ec)
    1850         972 :             return rinfo;
    1851      371744 :         if (rinfo->left_em == rightem &&
    1852      162282 :             rinfo->right_em == leftem &&
    1853      146988 :             rinfo->parent_ec == parent_ec)
    1854         870 :             return rinfo;
    1855             :     }
    1856             : 
    1857      407214 :     foreach(lc, ec->ec_derives)
    1858             :     {
    1859      350852 :         rinfo = (RestrictInfo *) lfirst(lc);
    1860      350852 :         if (rinfo->left_em == leftem &&
    1861      137290 :             rinfo->right_em == rightem &&
    1862      122528 :             rinfo->parent_ec == parent_ec)
    1863      259446 :             return rinfo;
    1864      228330 :         if (rinfo->left_em == rightem &&
    1865      145200 :             rinfo->right_em == leftem &&
    1866      136924 :             rinfo->parent_ec == parent_ec)
    1867      136924 :             return rinfo;
    1868             :     }
    1869             : 
    1870             :     /*
    1871             :      * Not there, so build it, in planner context so we can re-use it. (Not
    1872             :      * important in normal planning, but definitely so in GEQO.)
    1873             :      */
    1874       56362 :     oldcontext = MemoryContextSwitchTo(root->planner_cxt);
    1875             : 
    1876             :     /*
    1877             :      * If either EM is a child, recursively create the corresponding
    1878             :      * parent-to-parent clause, so that we can duplicate its rinfo_serial.
    1879             :      */
    1880       56362 :     if (leftem->em_is_child || rightem->em_is_child)
    1881             :     {
    1882        3694 :         EquivalenceMember *leftp = leftem->em_parent ? leftem->em_parent : leftem;
    1883        3694 :         EquivalenceMember *rightp = rightem->em_parent ? rightem->em_parent : rightem;
    1884             : 
    1885        3694 :         parent_rinfo = create_join_clause(root, ec, opno,
    1886             :                                           leftp, rightp,
    1887             :                                           parent_ec);
    1888             :     }
    1889             : 
    1890       56362 :     rinfo = build_implied_join_equality(root,
    1891             :                                         opno,
    1892             :                                         ec->ec_collation,
    1893             :                                         leftem->em_expr,
    1894             :                                         rightem->em_expr,
    1895       56362 :                                         bms_union(leftem->em_relids,
    1896       56362 :                                                   rightem->em_relids),
    1897             :                                         ec->ec_min_security);
    1898             : 
    1899             :     /*
    1900             :      * If either EM is a child, force the clause's clause_relids to include
    1901             :      * the relid(s) of the child rel.  In normal cases it would already, but
    1902             :      * not if we are considering appendrel child relations with pseudoconstant
    1903             :      * translated variables (i.e., UNION ALL sub-selects with constant output
    1904             :      * items).  We must do this so that join_clause_is_movable_into() will
    1905             :      * think that the clause should be evaluated at the correct place.
    1906             :      */
    1907       56362 :     if (leftem->em_is_child)
    1908        3190 :         rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
    1909        3190 :                                                leftem->em_relids);
    1910       56362 :     if (rightem->em_is_child)
    1911         504 :         rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
    1912         504 :                                                rightem->em_relids);
    1913             : 
    1914             :     /* If it's a child clause, copy the parent's rinfo_serial */
    1915       56362 :     if (parent_rinfo)
    1916        3694 :         rinfo->rinfo_serial = parent_rinfo->rinfo_serial;
    1917             : 
    1918             :     /* Mark the clause as redundant, or not */
    1919       56362 :     rinfo->parent_ec = parent_ec;
    1920             : 
    1921             :     /*
    1922             :      * We know the correct values for left_ec/right_ec, ie this particular EC,
    1923             :      * so we can just set them directly instead of forcing another lookup.
    1924             :      */
    1925       56362 :     rinfo->left_ec = ec;
    1926       56362 :     rinfo->right_ec = ec;
    1927             : 
    1928             :     /* Mark it as usable with these EMs */
    1929       56362 :     rinfo->left_em = leftem;
    1930       56362 :     rinfo->right_em = rightem;
    1931             :     /* and save it for possible re-use */
    1932       56362 :     ec->ec_derives = lappend(ec->ec_derives, rinfo);
    1933             : 
    1934       56362 :     MemoryContextSwitchTo(oldcontext);
    1935             : 
    1936       56362 :     return rinfo;
    1937             : }
    1938             : 
    1939             : 
    1940             : /*
    1941             :  * reconsider_outer_join_clauses
    1942             :  *    Re-examine any outer-join clauses that were set aside by
    1943             :  *    distribute_qual_to_rels(), and see if we can derive any
    1944             :  *    EquivalenceClasses from them.  Then, if they were not made
    1945             :  *    redundant, push them out into the regular join-clause lists.
    1946             :  *
    1947             :  * When we have mergejoinable clauses A = B that are outer-join clauses,
    1948             :  * we can't blindly combine them with other clauses A = C to deduce B = C,
    1949             :  * since in fact the "equality" A = B won't necessarily hold above the
    1950             :  * outer join (one of the variables might be NULL instead).  Nonetheless
    1951             :  * there are cases where we can add qual clauses using transitivity.
    1952             :  *
    1953             :  * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
    1954             :  * for which there is also an equivalence clause OUTERVAR = CONSTANT.
    1955             :  * It is safe and useful to push a clause INNERVAR = CONSTANT into the
    1956             :  * evaluation of the inner (nullable) relation, because any inner rows not
    1957             :  * meeting this condition will not contribute to the outer-join result anyway.
    1958             :  * (Any outer rows they could join to will be eliminated by the pushed-down
    1959             :  * equivalence clause.)
    1960             :  *
    1961             :  * Note that the above rule does not work for full outer joins; nor is it
    1962             :  * very interesting to consider cases where the generated equivalence clause
    1963             :  * would involve relations outside the outer join, since such clauses couldn't
    1964             :  * be pushed into the inner side's scan anyway.  So the restriction to
    1965             :  * outervar = pseudoconstant is not really giving up anything.
    1966             :  *
    1967             :  * For full-join cases, we can only do something useful if it's a FULL JOIN
    1968             :  * USING and a merged column has an equivalence MERGEDVAR = CONSTANT.
    1969             :  * By the time it gets here, the merged column will look like
    1970             :  *      COALESCE(LEFTVAR, RIGHTVAR)
    1971             :  * and we will have a full-join clause LEFTVAR = RIGHTVAR that we can match
    1972             :  * the COALESCE expression to. In this situation we can push LEFTVAR = CONSTANT
    1973             :  * and RIGHTVAR = CONSTANT into the input relations, since any rows not
    1974             :  * meeting these conditions cannot contribute to the join result.
    1975             :  *
    1976             :  * Again, there isn't any traction to be gained by trying to deal with
    1977             :  * clauses comparing a mergedvar to a non-pseudoconstant.  So we can make
    1978             :  * use of the EquivalenceClasses to search for matching variables that were
    1979             :  * equivalenced to constants.  The interesting outer-join clauses were
    1980             :  * accumulated for us by distribute_qual_to_rels.
    1981             :  *
    1982             :  * When we find one of these cases, we implement the changes we want by
    1983             :  * generating a new equivalence clause INNERVAR = CONSTANT (or LEFTVAR, etc)
    1984             :  * and pushing it into the EquivalenceClass structures.  This is because we
    1985             :  * may already know that INNERVAR is equivalenced to some other var(s), and
    1986             :  * we'd like the constant to propagate to them too.  Note that it would be
    1987             :  * unsafe to merge any existing EC for INNERVAR with the OUTERVAR's EC ---
    1988             :  * that could result in propagating constant restrictions from
    1989             :  * INNERVAR to OUTERVAR, which would be very wrong.
    1990             :  *
    1991             :  * It's possible that the INNERVAR is also an OUTERVAR for some other
    1992             :  * outer-join clause, in which case the process can be repeated.  So we repeat
    1993             :  * looping over the lists of clauses until no further deductions can be made.
    1994             :  * Whenever we do make a deduction, we remove the generating clause from the
    1995             :  * lists, since we don't want to make the same deduction twice.
    1996             :  *
    1997             :  * If we don't find any match for a set-aside outer join clause, we must
    1998             :  * throw it back into the regular joinclause processing by passing it to
    1999             :  * distribute_restrictinfo_to_rels().  If we do generate a derived clause,
    2000             :  * however, the outer-join clause is redundant.  We must still put some
    2001             :  * clause into the regular processing, because otherwise the join will be
    2002             :  * seen as a clauseless join and avoided during join order searching.
    2003             :  * We handle this by generating a constant-TRUE clause that is marked with
    2004             :  * the same required_relids etc as the removed outer-join clause, thus
    2005             :  * making it a join clause between the correct relations.
    2006             :  */
    2007             : void
    2008      278302 : reconsider_outer_join_clauses(PlannerInfo *root)
    2009             : {
    2010             :     bool        found;
    2011             :     ListCell   *cell;
    2012             : 
    2013             :     /* Outer loop repeats until we find no more deductions */
    2014             :     do
    2015             :     {
    2016      278302 :         found = false;
    2017             : 
    2018             :         /* Process the LEFT JOIN clauses */
    2019      310204 :         foreach(cell, root->left_join_clauses)
    2020             :         {
    2021       31902 :             OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
    2022             : 
    2023       31902 :             if (reconsider_outer_join_clause(root, ojcinfo, true))
    2024             :             {
    2025         696 :                 RestrictInfo *rinfo = ojcinfo->rinfo;
    2026             : 
    2027         696 :                 found = true;
    2028             :                 /* remove it from the list */
    2029         696 :                 root->left_join_clauses =
    2030         696 :                     foreach_delete_current(root->left_join_clauses, cell);
    2031             :                 /* throw back a dummy replacement clause (see notes above) */
    2032         696 :                 rinfo = make_restrictinfo(root,
    2033         696 :                                           (Expr *) makeBoolConst(true, false),
    2034         696 :                                           rinfo->is_pushed_down,
    2035         696 :                                           rinfo->has_clone,
    2036         696 :                                           rinfo->is_clone,
    2037             :                                           false,    /* pseudoconstant */
    2038             :                                           0,    /* security_level */
    2039             :                                           rinfo->required_relids,
    2040             :                                           rinfo->incompatible_relids,
    2041             :                                           rinfo->outer_relids);
    2042         696 :                 distribute_restrictinfo_to_rels(root, rinfo);
    2043             :             }
    2044             :         }
    2045             : 
    2046             :         /* Process the RIGHT JOIN clauses */
    2047      298524 :         foreach(cell, root->right_join_clauses)
    2048             :         {
    2049       20222 :             OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
    2050             : 
    2051       20222 :             if (reconsider_outer_join_clause(root, ojcinfo, false))
    2052             :             {
    2053         918 :                 RestrictInfo *rinfo = ojcinfo->rinfo;
    2054             : 
    2055         918 :                 found = true;
    2056             :                 /* remove it from the list */
    2057         918 :                 root->right_join_clauses =
    2058         918 :                     foreach_delete_current(root->right_join_clauses, cell);
    2059             :                 /* throw back a dummy replacement clause (see notes above) */
    2060         918 :                 rinfo = make_restrictinfo(root,
    2061         918 :                                           (Expr *) makeBoolConst(true, false),
    2062         918 :                                           rinfo->is_pushed_down,
    2063         918 :                                           rinfo->has_clone,
    2064         918 :                                           rinfo->is_clone,
    2065             :                                           false,    /* pseudoconstant */
    2066             :                                           0,    /* security_level */
    2067             :                                           rinfo->required_relids,
    2068             :                                           rinfo->incompatible_relids,
    2069             :                                           rinfo->outer_relids);
    2070         918 :                 distribute_restrictinfo_to_rels(root, rinfo);
    2071             :             }
    2072             :         }
    2073             : 
    2074             :         /* Process the FULL JOIN clauses */
    2075      279524 :         foreach(cell, root->full_join_clauses)
    2076             :         {
    2077        1222 :             OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
    2078             : 
    2079        1222 :             if (reconsider_full_join_clause(root, ojcinfo))
    2080             :             {
    2081           6 :                 RestrictInfo *rinfo = ojcinfo->rinfo;
    2082             : 
    2083           6 :                 found = true;
    2084             :                 /* remove it from the list */
    2085           6 :                 root->full_join_clauses =
    2086           6 :                     foreach_delete_current(root->full_join_clauses, cell);
    2087             :                 /* throw back a dummy replacement clause (see notes above) */
    2088           6 :                 rinfo = make_restrictinfo(root,
    2089           6 :                                           (Expr *) makeBoolConst(true, false),
    2090           6 :                                           rinfo->is_pushed_down,
    2091           6 :                                           rinfo->has_clone,
    2092           6 :                                           rinfo->is_clone,
    2093             :                                           false,    /* pseudoconstant */
    2094             :                                           0,    /* security_level */
    2095             :                                           rinfo->required_relids,
    2096             :                                           rinfo->incompatible_relids,
    2097             :                                           rinfo->outer_relids);
    2098           6 :                 distribute_restrictinfo_to_rels(root, rinfo);
    2099             :             }
    2100             :         }
    2101      278302 :     } while (found);
    2102             : 
    2103             :     /* Now, any remaining clauses have to be thrown back */
    2104      307378 :     foreach(cell, root->left_join_clauses)
    2105             :     {
    2106       30684 :         OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
    2107             : 
    2108       30684 :         distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
    2109             :     }
    2110      295032 :     foreach(cell, root->right_join_clauses)
    2111             :     {
    2112       18338 :         OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
    2113             : 
    2114       18338 :         distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
    2115             :     }
    2116      277910 :     foreach(cell, root->full_join_clauses)
    2117             :     {
    2118        1216 :         OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
    2119             : 
    2120        1216 :         distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
    2121             :     }
    2122      276694 : }
    2123             : 
    2124             : /*
    2125             :  * reconsider_outer_join_clauses for a single LEFT/RIGHT JOIN clause
    2126             :  *
    2127             :  * Returns true if we were able to propagate a constant through the clause.
    2128             :  */
    2129             : static bool
    2130       52124 : reconsider_outer_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo,
    2131             :                              bool outer_on_left)
    2132             : {
    2133       52124 :     RestrictInfo *rinfo = ojcinfo->rinfo;
    2134       52124 :     SpecialJoinInfo *sjinfo = ojcinfo->sjinfo;
    2135             :     Expr       *outervar,
    2136             :                *innervar;
    2137             :     Oid         opno,
    2138             :                 collation,
    2139             :                 left_type,
    2140             :                 right_type,
    2141             :                 inner_datatype;
    2142             :     Relids      inner_relids;
    2143             :     ListCell   *lc1;
    2144             : 
    2145             :     Assert(is_opclause(rinfo->clause));
    2146       52124 :     opno = ((OpExpr *) rinfo->clause)->opno;
    2147       52124 :     collation = ((OpExpr *) rinfo->clause)->inputcollid;
    2148             : 
    2149             :     /* Extract needed info from the clause */
    2150       52124 :     op_input_types(opno, &left_type, &right_type);
    2151       52124 :     if (outer_on_left)
    2152             :     {
    2153       31902 :         outervar = (Expr *) get_leftop(rinfo->clause);
    2154       31902 :         innervar = (Expr *) get_rightop(rinfo->clause);
    2155       31902 :         inner_datatype = right_type;
    2156       31902 :         inner_relids = rinfo->right_relids;
    2157             :     }
    2158             :     else
    2159             :     {
    2160       20222 :         outervar = (Expr *) get_rightop(rinfo->clause);
    2161       20222 :         innervar = (Expr *) get_leftop(rinfo->clause);
    2162       20222 :         inner_datatype = left_type;
    2163       20222 :         inner_relids = rinfo->left_relids;
    2164             :     }
    2165             : 
    2166             :     /* Scan EquivalenceClasses for a match to outervar */
    2167      333112 :     foreach(lc1, root->eq_classes)
    2168             :     {
    2169      282602 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
    2170             :         bool        match;
    2171             :         ListCell   *lc2;
    2172             : 
    2173             :         /* Ignore EC unless it contains pseudoconstants */
    2174      282602 :         if (!cur_ec->ec_has_const)
    2175      222304 :             continue;
    2176             :         /* Never match to a volatile EC */
    2177       60298 :         if (cur_ec->ec_has_volatile)
    2178           0 :             continue;
    2179             :         /* It has to match the outer-join clause as to semantics, too */
    2180       60298 :         if (collation != cur_ec->ec_collation)
    2181        2202 :             continue;
    2182       58096 :         if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
    2183       13576 :             continue;
    2184             :         /* Does it contain a match to outervar? */
    2185       44520 :         match = false;
    2186      137206 :         foreach(lc2, cur_ec->ec_members)
    2187             :         {
    2188       94300 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
    2189             : 
    2190             :             Assert(!cur_em->em_is_child);    /* no children yet */
    2191       94300 :             if (equal(outervar, cur_em->em_expr))
    2192             :             {
    2193        1614 :                 match = true;
    2194        1614 :                 break;
    2195             :             }
    2196             :         }
    2197       44520 :         if (!match)
    2198       42906 :             continue;           /* no match, so ignore this EC */
    2199             : 
    2200             :         /*
    2201             :          * Yes it does!  Try to generate a clause INNERVAR = CONSTANT for each
    2202             :          * CONSTANT in the EC.  Note that we must succeed with at least one
    2203             :          * constant before we can decide to throw away the outer-join clause.
    2204             :          */
    2205        1614 :         match = false;
    2206        5808 :         foreach(lc2, cur_ec->ec_members)
    2207             :         {
    2208        4194 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
    2209             :             Oid         eq_op;
    2210             :             RestrictInfo *newrinfo;
    2211             :             JoinDomain *jdomain;
    2212             : 
    2213        4194 :             if (!cur_em->em_is_const)
    2214        2538 :                 continue;       /* ignore non-const members */
    2215        1656 :             eq_op = select_equality_operator(cur_ec,
    2216             :                                              inner_datatype,
    2217             :                                              cur_em->em_datatype);
    2218        1656 :             if (!OidIsValid(eq_op))
    2219           0 :                 continue;       /* can't generate equality */
    2220        1656 :             newrinfo = build_implied_join_equality(root,
    2221             :                                                    eq_op,
    2222             :                                                    cur_ec->ec_collation,
    2223             :                                                    innervar,
    2224             :                                                    cur_em->em_expr,
    2225             :                                                    bms_copy(inner_relids),
    2226             :                                                    cur_ec->ec_min_security);
    2227             :             /* This equality holds within the OJ's child JoinDomain */
    2228        1656 :             jdomain = find_join_domain(root, sjinfo->syn_righthand);
    2229        1656 :             if (process_equivalence(root, &newrinfo, jdomain))
    2230        1656 :                 match = true;
    2231             :         }
    2232             : 
    2233             :         /*
    2234             :          * If we were able to equate INNERVAR to any constant, report success.
    2235             :          * Otherwise, fall out of the search loop, since we know the OUTERVAR
    2236             :          * appears in at most one EC.
    2237             :          */
    2238        1614 :         if (match)
    2239        1614 :             return true;
    2240             :         else
    2241           0 :             break;
    2242             :     }
    2243             : 
    2244       50510 :     return false;               /* failed to make any deduction */
    2245             : }
    2246             : 
    2247             : /*
    2248             :  * reconsider_outer_join_clauses for a single FULL JOIN clause
    2249             :  *
    2250             :  * Returns true if we were able to propagate a constant through the clause.
    2251             :  */
    2252             : static bool
    2253        1222 : reconsider_full_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo)
    2254             : {
    2255        1222 :     RestrictInfo *rinfo = ojcinfo->rinfo;
    2256        1222 :     SpecialJoinInfo *sjinfo = ojcinfo->sjinfo;
    2257        1222 :     Relids      fjrelids = bms_make_singleton(sjinfo->ojrelid);
    2258             :     Expr       *leftvar;
    2259             :     Expr       *rightvar;
    2260             :     Oid         opno,
    2261             :                 collation,
    2262             :                 left_type,
    2263             :                 right_type;
    2264             :     Relids      left_relids,
    2265             :                 right_relids;
    2266             :     ListCell   *lc1;
    2267             : 
    2268             :     /* Extract needed info from the clause */
    2269             :     Assert(is_opclause(rinfo->clause));
    2270        1222 :     opno = ((OpExpr *) rinfo->clause)->opno;
    2271        1222 :     collation = ((OpExpr *) rinfo->clause)->inputcollid;
    2272        1222 :     op_input_types(opno, &left_type, &right_type);
    2273        1222 :     leftvar = (Expr *) get_leftop(rinfo->clause);
    2274        1222 :     rightvar = (Expr *) get_rightop(rinfo->clause);
    2275        1222 :     left_relids = rinfo->left_relids;
    2276        1222 :     right_relids = rinfo->right_relids;
    2277             : 
    2278        6246 :     foreach(lc1, root->eq_classes)
    2279             :     {
    2280        5030 :         EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
    2281        5030 :         EquivalenceMember *coal_em = NULL;
    2282             :         bool        match;
    2283             :         bool        matchleft;
    2284             :         bool        matchright;
    2285             :         ListCell   *lc2;
    2286        5030 :         int         coal_idx = -1;
    2287             : 
    2288             :         /* Ignore EC unless it contains pseudoconstants */
    2289        5030 :         if (!cur_ec->ec_has_const)
    2290        4734 :             continue;
    2291             :         /* Never match to a volatile EC */
    2292         296 :         if (cur_ec->ec_has_volatile)
    2293           0 :             continue;
    2294             :         /* It has to match the outer-join clause as to semantics, too */
    2295         296 :         if (collation != cur_ec->ec_collation)
    2296          36 :             continue;
    2297         260 :         if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
    2298           0 :             continue;
    2299             : 
    2300             :         /*
    2301             :          * Does it contain a COALESCE(leftvar, rightvar) construct?
    2302             :          *
    2303             :          * We can assume the COALESCE() inputs are in the same order as the
    2304             :          * join clause, since both were automatically generated in the cases
    2305             :          * we care about.
    2306             :          *
    2307             :          * XXX currently this may fail to match in cross-type cases because
    2308             :          * the COALESCE will contain typecast operations while the join clause
    2309             :          * may not (if there is a cross-type mergejoin operator available for
    2310             :          * the two column types). Is it OK to strip implicit coercions from
    2311             :          * the COALESCE arguments?
    2312             :          */
    2313         260 :         match = false;
    2314         762 :         foreach(lc2, cur_ec->ec_members)
    2315             :         {
    2316         508 :             coal_em = (EquivalenceMember *) lfirst(lc2);
    2317             :             Assert(!coal_em->em_is_child);   /* no children yet */
    2318         508 :             if (IsA(coal_em->em_expr, CoalesceExpr))
    2319             :             {
    2320          18 :                 CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
    2321             :                 Node       *cfirst;
    2322             :                 Node       *csecond;
    2323             : 
    2324          18 :                 if (list_length(cexpr->args) != 2)
    2325          12 :                     continue;
    2326           6 :                 cfirst = (Node *) linitial(cexpr->args);
    2327           6 :                 csecond = (Node *) lsecond(cexpr->args);
    2328             : 
    2329             :                 /*
    2330             :                  * The COALESCE arguments will be marked as possibly nulled by
    2331             :                  * the full join, while we wish to generate clauses that apply
    2332             :                  * to the join's inputs.  So we must strip the join from the
    2333             :                  * nullingrels fields of cfirst/csecond before comparing them
    2334             :                  * to leftvar/rightvar.  (Perhaps with a less hokey
    2335             :                  * representation for FULL JOIN USING output columns, this
    2336             :                  * wouldn't be needed?)
    2337             :                  */
    2338           6 :                 cfirst = remove_nulling_relids(cfirst, fjrelids, NULL);
    2339           6 :                 csecond = remove_nulling_relids(csecond, fjrelids, NULL);
    2340             : 
    2341           6 :                 if (equal(leftvar, cfirst) && equal(rightvar, csecond))
    2342             :                 {
    2343           6 :                     coal_idx = foreach_current_index(lc2);
    2344           6 :                     match = true;
    2345           6 :                     break;
    2346             :                 }
    2347             :             }
    2348             :         }
    2349         260 :         if (!match)
    2350         254 :             continue;           /* no match, so ignore this EC */
    2351             : 
    2352             :         /*
    2353             :          * Yes it does!  Try to generate clauses LEFTVAR = CONSTANT and
    2354             :          * RIGHTVAR = CONSTANT for each CONSTANT in the EC.  Note that we must
    2355             :          * succeed with at least one constant for each var before we can
    2356             :          * decide to throw away the outer-join clause.
    2357             :          */
    2358           6 :         matchleft = matchright = false;
    2359          18 :         foreach(lc2, cur_ec->ec_members)
    2360             :         {
    2361          12 :             EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
    2362             :             Oid         eq_op;
    2363             :             RestrictInfo *newrinfo;
    2364             :             JoinDomain *jdomain;
    2365             : 
    2366          12 :             if (!cur_em->em_is_const)
    2367           6 :                 continue;       /* ignore non-const members */
    2368           6 :             eq_op = select_equality_operator(cur_ec,
    2369             :                                              left_type,
    2370             :                                              cur_em->em_datatype);
    2371           6 :             if (OidIsValid(eq_op))
    2372             :             {
    2373           6 :                 newrinfo = build_implied_join_equality(root,
    2374             :                                                        eq_op,
    2375             :                                                        cur_ec->ec_collation,
    2376             :                                                        leftvar,
    2377             :                                                        cur_em->em_expr,
    2378             :                                                        bms_copy(left_relids),
    2379             :                                                        cur_ec->ec_min_security);
    2380             :                 /* This equality holds within the lefthand child JoinDomain */
    2381           6 :                 jdomain = find_join_domain(root, sjinfo->syn_lefthand);
    2382           6 :                 if (process_equivalence(root, &newrinfo, jdomain))
    2383           6 :                     matchleft = true;
    2384             :             }
    2385           6 :             eq_op = select_equality_operator(cur_ec,
    2386             :                                              right_type,
    2387             :                                              cur_em->em_datatype);
    2388           6 :             if (OidIsValid(eq_op))
    2389             :             {
    2390           6 :                 newrinfo = build_implied_join_equality(root,
    2391             :                                                        eq_op,
    2392             :                                                        cur_ec->ec_collation,
    2393             :                                                        rightvar,
    2394             :                                                        cur_em->em_expr,
    2395             :                                                        bms_copy(right_relids),
    2396             :                                                        cur_ec->ec_min_security);
    2397             :                 /* This equality holds within the righthand child JoinDomain */
    2398           6 :                 jdomain = find_join_domain(root, sjinfo->syn_righthand);
    2399           6 :                 if (process_equivalence(root, &newrinfo, jdomain))
    2400           6 :                     matchright = true;
    2401             :             }
    2402             :         }
    2403             : 
    2404             :         /*
    2405             :          * If we were able to equate both vars to constants, we're done, and
    2406             :          * we can throw away the full-join clause as redundant.  Moreover, we
    2407             :          * can remove the COALESCE entry from the EC, since the added
    2408             :          * restrictions ensure it will always have the expected value. (We
    2409             :          * don't bother trying to update ec_relids or ec_sources.)
    2410             :          */
    2411           6 :         if (matchleft && matchright)
    2412             :         {
    2413           6 :             cur_ec->ec_members = list_delete_nth_cell(cur_ec->ec_members, coal_idx);
    2414           6 :             return true;
    2415             :         }
    2416             : 
    2417             :         /*
    2418             :          * Otherwise, fall out of the search loop, since we know the COALESCE
    2419             :          * appears in at most one EC (XXX might stop being true if we allow
    2420             :          * stripping of coercions above?)
    2421             :          */
    2422           0 :         break;
    2423             :     }
    2424             : 
    2425        1216 :     return false;               /* failed to make any deduction */
    2426             : }
    2427             : 
    2428             : /*
    2429             :  * find_join_domain
    2430             :  *    Find the highest JoinDomain enclosed within the given relid set.
    2431             :  *
    2432             :  * (We could avoid this search at the cost of complicating APIs elsewhere,
    2433             :  * which doesn't seem worth it.)
    2434             :  */
    2435             : static JoinDomain *
    2436        1668 : find_join_domain(PlannerInfo *root, Relids relids)
    2437             : {
    2438             :     ListCell   *lc;
    2439             : 
    2440        3426 :     foreach(lc, root->join_domains)
    2441             :     {
    2442        3426 :         JoinDomain *jdomain = (JoinDomain *) lfirst(lc);
    2443             : 
    2444        3426 :         if (bms_is_subset(jdomain->jd_relids, relids))
    2445        1668 :             return jdomain;
    2446             :     }
    2447           0 :     elog(ERROR, "failed to find appropriate JoinDomain");
    2448             :     return NULL;                /* keep compiler quiet */
    2449             : }
    2450             : 
    2451             : 
    2452             : /*
    2453             :  * exprs_known_equal
    2454             :  *    Detect whether two expressions are known equal due to equivalence
    2455             :  *    relationships.
    2456             :  *
    2457             :  * Actually, this only shows that the expressions are equal according
    2458             :  * to some opfamily's notion of equality --- but we only use it for
    2459             :  * selectivity estimation, so a fuzzy idea of equality is OK.
    2460             :  *
    2461             :  * Note: does not bother to check for "equal(item1, item2)"; caller must
    2462             :  * check that case if it's possible to pass identical items.
    2463             :  */
    2464             : bool
    2465        3240 : exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
    2466             : {
    2467             :     ListCell   *lc1;
    2468             : 
    2469       19744 :     foreach(lc1, root->eq_classes)
    2470             :     {
    2471       16624 :         EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
    2472       16624 :         bool        item1member = false;
    2473       16624 :         bool        item2member = false;
    2474             :         ListCell   *lc2;
    2475             : 
    2476             :         /* Never match to a volatile EC */
    2477       16624 :         if (ec->ec_has_volatile)
    2478           0 :             continue;
    2479             : 
    2480       53116 :         foreach(lc2, ec->ec_members)
    2481             :         {
    2482       36612 :             EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
    2483             : 
    2484       36612 :             if (em->em_is_child)
    2485       12756 :                 continue;       /* ignore children here */
    2486       23856 :             if (equal(item1, em->em_expr))
    2487        1618 :                 item1member = true;
    2488       22238 :             else if (equal(item2, em->em_expr))
    2489        1762 :                 item2member = true;
    2490             :             /* Exit as soon as equality is proven */
    2491       23856 :             if (item1member && item2member)
    2492         120 :                 return true;
    2493             :         }
    2494             :     }
    2495        3120 :     return false;
    2496             : }
    2497             : 
    2498             : 
    2499             : /*
    2500             :  * match_eclasses_to_foreign_key_col
    2501             :  *    See whether a foreign key column match is proven by any eclass.
    2502             :  *
    2503             :  * If the referenced and referencing Vars of the fkey's colno'th column are
    2504             :  * known equal due to any eclass, return that eclass; otherwise return NULL.
    2505             :  * (In principle there might be more than one matching eclass if multiple
    2506             :  * collations are involved, but since collation doesn't matter for equality,
    2507             :  * we ignore that fine point here.)  This is much like exprs_known_equal,
    2508             :  * except that we insist on the comparison operator matching the eclass, so
    2509             :  * that the result is definite not approximate.
    2510             :  *
    2511             :  * On success, we also set fkinfo->eclass[colno] to the matching eclass,
    2512             :  * and set fkinfo->fk_eclass_member[colno] to the eclass member for the
    2513             :  * referencing Var.
    2514             :  */
    2515             : EquivalenceClass *
    2516        2162 : match_eclasses_to_foreign_key_col(PlannerInfo *root,
    2517             :                                   ForeignKeyOptInfo *fkinfo,
    2518             :                                   int colno)
    2519             : {
    2520        2162 :     Index       var1varno = fkinfo->con_relid;
    2521        2162 :     AttrNumber  var1attno = fkinfo->conkey[colno];
    2522        2162 :     Index       var2varno = fkinfo->ref_relid;
    2523        2162 :     AttrNumber  var2attno = fkinfo->confkey[colno];
    2524        2162 :     Oid         eqop = fkinfo->conpfeqop[colno];
    2525        2162 :     RelOptInfo *rel1 = root->simple_rel_array[var1varno];
    2526        2162 :     RelOptInfo *rel2 = root->simple_rel_array[var2varno];
    2527        2162 :     List       *opfamilies = NIL;   /* compute only if needed */
    2528             :     Bitmapset  *matching_ecs;
    2529             :     int         i;
    2530             : 
    2531             :     /* Consider only eclasses mentioning both relations */
    2532             :     Assert(root->ec_merging_done);
    2533             :     Assert(IS_SIMPLE_REL(rel1));
    2534             :     Assert(IS_SIMPLE_REL(rel2));
    2535        2162 :     matching_ecs = bms_intersect(rel1->eclass_indexes,
    2536        2162 :                                  rel2->eclass_indexes);
    2537             : 
    2538        2162 :     i = -1;
    2539        2258 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
    2540             :     {
    2541         438 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
    2542             :                                                              i);
    2543         438 :         EquivalenceMember *item1_em = NULL;
    2544         438 :         EquivalenceMember *item2_em = NULL;
    2545             :         ListCell   *lc2;
    2546             : 
    2547             :         /* Never match to a volatile EC */
    2548         438 :         if (ec->ec_has_volatile)
    2549           0 :             continue;
    2550             :         /* Note: it seems okay to match to "broken" eclasses here */
    2551             : 
    2552        1074 :         foreach(lc2, ec->ec_members)
    2553             :         {
    2554         978 :             EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
    2555             :             Var        *var;
    2556             : 
    2557         978 :             if (em->em_is_child)
    2558           0 :                 continue;       /* ignore children here */
    2559             : 
    2560             :             /* EM must be a Var, possibly with RelabelType */
    2561         978 :             var = (Var *) em->em_expr;
    2562         978 :             while (var && IsA(var, RelabelType))
    2563           0 :                 var = (Var *) ((RelabelType *) var)->arg;
    2564         978 :             if (!(var && IsA(var, Var)))
    2565           6 :                 continue;
    2566             : 
    2567             :             /* Match? */
    2568         972 :             if (var->varno == var1varno && var->varattno == var1attno)
    2569         342 :                 item1_em = em;
    2570         630 :             else if (var->varno == var2varno && var->varattno == var2attno)
    2571         342 :                 item2_em = em;
    2572             : 
    2573             :             /* Have we found both PK and FK column in this EC? */
    2574         972 :             if (item1_em && item2_em)
    2575             :             {
    2576             :                 /*
    2577             :                  * Succeed if eqop matches EC's opfamilies.  We could test
    2578             :                  * this before scanning the members, but it's probably cheaper
    2579             :                  * to test for member matches first.
    2580             :                  */
    2581         342 :                 if (opfamilies == NIL)  /* compute if we didn't already */
    2582         342 :                     opfamilies = get_mergejoin_opfamilies(eqop);
    2583         342 :                 if (equal(opfamilies, ec->ec_opfamilies))
    2584             :                 {
    2585         342 :                     fkinfo->eclass[colno] = ec;
    2586         342 :                     fkinfo->fk_eclass_member[colno] = item2_em;
    2587         342 :                     return ec;
    2588             :                 }
    2589             :                 /* Otherwise, done with this EC, move on to the next */
    2590           0 :                 break;
    2591             :             }
    2592             :         }
    2593             :     }
    2594        1820 :     return NULL;
    2595             : }
    2596             : 
    2597             : /*
    2598             :  * find_derived_clause_for_ec_member
    2599             :  *    Search for a previously-derived clause mentioning the given EM.
    2600             :  *
    2601             :  * The eclass should be an ec_has_const EC, of which the EM is a non-const
    2602             :  * member.  This should ensure there is just one derived clause mentioning
    2603             :  * the EM (and equating it to a constant).
    2604             :  * Returns NULL if no such clause can be found.
    2605             :  */
    2606             : RestrictInfo *
    2607           6 : find_derived_clause_for_ec_member(EquivalenceClass *ec,
    2608             :                                   EquivalenceMember *em)
    2609             : {
    2610             :     ListCell   *lc;
    2611             : 
    2612             :     Assert(ec->ec_has_const);
    2613             :     Assert(!em->em_is_const);
    2614           6 :     foreach(lc, ec->ec_derives)
    2615             :     {
    2616           6 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    2617             : 
    2618             :         /*
    2619             :          * generate_base_implied_equalities_const will have put non-const
    2620             :          * members on the left side of derived clauses.
    2621             :          */
    2622           6 :         if (rinfo->left_em == em)
    2623           6 :             return rinfo;
    2624             :     }
    2625           0 :     return NULL;
    2626             : }
    2627             : 
    2628             : 
    2629             : /*
    2630             :  * add_child_rel_equivalences
    2631             :  *    Search for EC members that reference the root parent of child_rel, and
    2632             :  *    add transformed members referencing the child_rel.
    2633             :  *
    2634             :  * Note that this function won't be called at all unless we have at least some
    2635             :  * reason to believe that the EC members it generates will be useful.
    2636             :  *
    2637             :  * parent_rel and child_rel could be derived from appinfo, but since the
    2638             :  * caller has already computed them, we might as well just pass them in.
    2639             :  *
    2640             :  * The passed-in AppendRelInfo is not used when the parent_rel is not a
    2641             :  * top-level baserel, since it shows the mapping from the parent_rel but
    2642             :  * we need to translate EC expressions that refer to the top-level parent.
    2643             :  * Using it is faster than using adjust_appendrel_attrs_multilevel(), though,
    2644             :  * so we prefer it when we can.
    2645             :  */
    2646             : void
    2647       21618 : add_child_rel_equivalences(PlannerInfo *root,
    2648             :                            AppendRelInfo *appinfo,
    2649             :                            RelOptInfo *parent_rel,
    2650             :                            RelOptInfo *child_rel)
    2651             : {
    2652       21618 :     Relids      top_parent_relids = child_rel->top_parent_relids;
    2653       21618 :     Relids      child_relids = child_rel->relids;
    2654             :     int         i;
    2655             : 
    2656             :     /*
    2657             :      * EC merging should be complete already, so we can use the parent rel's
    2658             :      * eclass_indexes to avoid searching all of root->eq_classes.
    2659             :      */
    2660             :     Assert(root->ec_merging_done);
    2661             :     Assert(IS_SIMPLE_REL(parent_rel));
    2662             : 
    2663       21618 :     i = -1;
    2664       64088 :     while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
    2665             :     {
    2666       42470 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
    2667             :         int         num_members;
    2668             : 
    2669             :         /*
    2670             :          * If this EC contains a volatile expression, then generating child
    2671             :          * EMs would be downright dangerous, so skip it.  We rely on a
    2672             :          * volatile EC having only one EM.
    2673             :          */
    2674       42470 :         if (cur_ec->ec_has_volatile)
    2675           0 :             continue;
    2676             : 
    2677             :         /* Sanity check eclass_indexes only contain ECs for parent_rel */
    2678             :         Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
    2679             : 
    2680             :         /*
    2681             :          * We don't use foreach() here because there's no point in scanning
    2682             :          * newly-added child members, so we can stop after the last
    2683             :          * pre-existing EC member.
    2684             :          */
    2685       42470 :         num_members = list_length(cur_ec->ec_members);
    2686      198936 :         for (int pos = 0; pos < num_members; pos++)
    2687             :         {
    2688      156466 :             EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
    2689             : 
    2690      156466 :             if (cur_em->em_is_const)
    2691        3168 :                 continue;       /* ignore consts here */
    2692             : 
    2693             :             /*
    2694             :              * We consider only original EC members here, not
    2695             :              * already-transformed child members.  Otherwise, if some original
    2696             :              * member expression references more than one appendrel, we'd get
    2697             :              * an O(N^2) explosion of useless derived expressions for
    2698             :              * combinations of children.  (But add_child_join_rel_equivalences
    2699             :              * may add targeted combinations for partitionwise-join purposes.)
    2700             :              */
    2701      153298 :             if (cur_em->em_is_child)
    2702       97280 :                 continue;       /* ignore children here */
    2703             : 
    2704             :             /*
    2705             :              * Consider only members that reference and can be computed at
    2706             :              * child's topmost parent rel.  In particular we want to exclude
    2707             :              * parent-rel Vars that have nonempty varnullingrels.  Translating
    2708             :              * those might fail, if the transformed expression wouldn't be a
    2709             :              * simple Var; and in any case it wouldn't produce a member that
    2710             :              * has any use in creating plans for the child rel.
    2711             :              */
    2712       56018 :             if (bms_is_subset(cur_em->em_relids, top_parent_relids) &&
    2713       40034 :                 !bms_is_empty(cur_em->em_relids))
    2714             :             {
    2715             :                 /* OK, generate transformed child version */
    2716             :                 Expr       *child_expr;
    2717             :                 Relids      new_relids;
    2718             : 
    2719       40034 :                 if (parent_rel->reloptkind == RELOPT_BASEREL)
    2720             :                 {
    2721             :                     /* Simple single-level transformation */
    2722             :                     child_expr = (Expr *)
    2723       32504 :                         adjust_appendrel_attrs(root,
    2724       32504 :                                                (Node *) cur_em->em_expr,
    2725             :                                                1, &appinfo);
    2726             :                 }
    2727             :                 else
    2728             :                 {
    2729             :                     /* Must do multi-level transformation */
    2730             :                     child_expr = (Expr *)
    2731        7530 :                         adjust_appendrel_attrs_multilevel(root,
    2732        7530 :                                                           (Node *) cur_em->em_expr,
    2733             :                                                           child_rel,
    2734        7530 :                                                           child_rel->top_parent);
    2735             :                 }
    2736             : 
    2737             :                 /*
    2738             :                  * Transform em_relids to match.  Note we do *not* do
    2739             :                  * pull_varnos(child_expr) here, as for example the
    2740             :                  * transformation might have substituted a constant, but we
    2741             :                  * don't want the child member to be marked as constant.
    2742             :                  */
    2743       40034 :                 new_relids = bms_difference(cur_em->em_relids,
    2744             :                                             top_parent_relids);
    2745       40034 :                 new_relids = bms_add_members(new_relids, child_relids);
    2746             : 
    2747       40034 :                 (void) add_eq_member(cur_ec, child_expr, new_relids,
    2748             :                                      cur_em->em_jdomain,
    2749             :                                      cur_em, cur_em->em_datatype);
    2750             : 
    2751             :                 /* Record this EC index for the child rel */
    2752       40034 :                 child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
    2753             :             }
    2754             :         }
    2755             :     }
    2756       21618 : }
    2757             : 
    2758             : /*
    2759             :  * add_child_join_rel_equivalences
    2760             :  *    Like add_child_rel_equivalences(), but for joinrels
    2761             :  *
    2762             :  * Here we find the ECs relevant to the top parent joinrel and add transformed
    2763             :  * member expressions that refer to this child joinrel.
    2764             :  *
    2765             :  * Note that this function won't be called at all unless we have at least some
    2766             :  * reason to believe that the EC members it generates will be useful.
    2767             :  */
    2768             : void
    2769        4180 : add_child_join_rel_equivalences(PlannerInfo *root,
    2770             :                                 int nappinfos, AppendRelInfo **appinfos,
    2771             :                                 RelOptInfo *parent_joinrel,
    2772             :                                 RelOptInfo *child_joinrel)
    2773             : {
    2774        4180 :     Relids      top_parent_relids = child_joinrel->top_parent_relids;
    2775        4180 :     Relids      child_relids = child_joinrel->relids;
    2776             :     Bitmapset  *matching_ecs;
    2777             :     MemoryContext oldcontext;
    2778             :     int         i;
    2779             : 
    2780             :     Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
    2781             : 
    2782             :     /* We need consider only ECs that mention the parent joinrel */
    2783        4180 :     matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
    2784             : 
    2785             :     /*
    2786             :      * If we're being called during GEQO join planning, we still have to
    2787             :      * create any new EC members in the main planner context, to avoid having
    2788             :      * a corrupt EC data structure after the GEQO context is reset.  This is
    2789             :      * problematic since we'll leak memory across repeated GEQO cycles.  For
    2790             :      * now, though, bloat is better than crash.  If it becomes a real issue
    2791             :      * we'll have to do something to avoid generating duplicate EC members.
    2792             :      */
    2793        4180 :     oldcontext = MemoryContextSwitchTo(root->planner_cxt);
    2794             : 
    2795        4180 :     i = -1;
    2796       19686 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
    2797             :     {
    2798       15506 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
    2799             :         int         num_members;
    2800             : 
    2801             :         /*
    2802             :          * If this EC contains a volatile expression, then generating child
    2803             :          * EMs would be downright dangerous, so skip it.  We rely on a
    2804             :          * volatile EC having only one EM.
    2805             :          */
    2806       15506 :         if (cur_ec->ec_has_volatile)
    2807           0 :             continue;
    2808             : 
    2809             :         /* Sanity check on get_eclass_indexes_for_relids result */
    2810             :         Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
    2811             : 
    2812             :         /*
    2813             :          * We don't use foreach() here because there's no point in scanning
    2814             :          * newly-added child members, so we can stop after the last
    2815             :          * pre-existing EC member.
    2816             :          */
    2817       15506 :         num_members = list_length(cur_ec->ec_members);
    2818      103442 :         for (int pos = 0; pos < num_members; pos++)
    2819             :         {
    2820       87936 :             EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
    2821             : 
    2822       87936 :             if (cur_em->em_is_const)
    2823        2232 :                 continue;       /* ignore consts here */
    2824             : 
    2825             :             /*
    2826             :              * We consider only original EC members here, not
    2827             :              * already-transformed child members.
    2828             :              */
    2829       85704 :             if (cur_em->em_is_child)
    2830       66074 :                 continue;       /* ignore children here */
    2831             : 
    2832             :             /*
    2833             :              * We may ignore expressions that reference a single baserel,
    2834             :              * because add_child_rel_equivalences should have handled them.
    2835             :              */
    2836       19630 :             if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
    2837       17008 :                 continue;
    2838             : 
    2839             :             /* Does this member reference child's topmost parent rel? */
    2840        2622 :             if (bms_overlap(cur_em->em_relids, top_parent_relids))
    2841             :             {
    2842             :                 /* Yes, generate transformed child version */
    2843             :                 Expr       *child_expr;
    2844             :                 Relids      new_relids;
    2845             : 
    2846        2622 :                 if (parent_joinrel->reloptkind == RELOPT_JOINREL)
    2847             :                 {
    2848             :                     /* Simple single-level transformation */
    2849             :                     child_expr = (Expr *)
    2850        2526 :                         adjust_appendrel_attrs(root,
    2851        2526 :                                                (Node *) cur_em->em_expr,
    2852             :                                                nappinfos, appinfos);
    2853             :                 }
    2854             :                 else
    2855             :                 {
    2856             :                     /* Must do multi-level transformation */
    2857             :                     Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
    2858             :                     child_expr = (Expr *)
    2859          96 :                         adjust_appendrel_attrs_multilevel(root,
    2860          96 :                                                           (Node *) cur_em->em_expr,
    2861             :                                                           child_joinrel,
    2862          96 :                                                           child_joinrel->top_parent);
    2863             :                 }
    2864             : 
    2865             :                 /*
    2866             :                  * Transform em_relids to match.  Note we do *not* do
    2867             :                  * pull_varnos(child_expr) here, as for example the
    2868             :                  * transformation might have substituted a constant, but we
    2869             :                  * don't want the child member to be marked as constant.
    2870             :                  */
    2871        2622 :                 new_relids = bms_difference(cur_em->em_relids,
    2872             :                                             top_parent_relids);
    2873        2622 :                 new_relids = bms_add_members(new_relids, child_relids);
    2874             : 
    2875        2622 :                 (void) add_eq_member(cur_ec, child_expr, new_relids,
    2876             :                                      cur_em->em_jdomain,
    2877             :                                      cur_em, cur_em->em_datatype);
    2878             :             }
    2879             :         }
    2880             :     }
    2881             : 
    2882        4180 :     MemoryContextSwitchTo(oldcontext);
    2883        4180 : }
    2884             : 
    2885             : /*
    2886             :  * add_setop_child_rel_equivalences
    2887             :  *      Add equivalence members for each non-resjunk target in 'child_tlist'
    2888             :  *      to the EquivalenceClass in the corresponding setop_pathkey's pk_eclass.
    2889             :  *
    2890             :  * 'root' is the PlannerInfo belonging to the top-level set operation.
    2891             :  * 'child_rel' is the RelOptInfo of the child relation we're adding
    2892             :  * EquivalenceMembers for.
    2893             :  * 'child_tlist' is the target list for the setop child relation.  The target
    2894             :  * list expressions are what we add as EquivalenceMembers.
    2895             :  * 'setop_pathkeys' is a list of PathKeys which must contain an entry for each
    2896             :  * non-resjunk target in 'child_tlist'.
    2897             :  */
    2898             : void
    2899        9554 : add_setop_child_rel_equivalences(PlannerInfo *root, RelOptInfo *child_rel,
    2900             :                                  List *child_tlist, List *setop_pathkeys)
    2901             : {
    2902             :     ListCell   *lc;
    2903        9554 :     ListCell   *lc2 = list_head(setop_pathkeys);
    2904             : 
    2905       38056 :     foreach(lc, child_tlist)
    2906             :     {
    2907       28502 :         TargetEntry *tle = lfirst_node(TargetEntry, lc);
    2908             :         EquivalenceMember *parent_em;
    2909             :         PathKey    *pk;
    2910             : 
    2911       28502 :         if (tle->resjunk)
    2912           0 :             continue;
    2913             : 
    2914       28502 :         if (lc2 == NULL)
    2915           0 :             elog(ERROR, "too few pathkeys for set operation");
    2916             : 
    2917       28502 :         pk = lfirst_node(PathKey, lc2);
    2918       28502 :         parent_em = linitial(pk->pk_eclass->ec_members);
    2919             : 
    2920             :         /*
    2921             :          * We can safely pass the parent member as the first member in the
    2922             :          * ec_members list as this is added first in generate_union_paths,
    2923             :          * likewise, the JoinDomain can be that of the initial member of the
    2924             :          * Pathkey's EquivalenceClass.
    2925             :          */
    2926       28502 :         add_eq_member(pk->pk_eclass,
    2927             :                       tle->expr,
    2928             :                       child_rel->relids,
    2929             :                       parent_em->em_jdomain,
    2930             :                       parent_em,
    2931       28502 :                       exprType((Node *) tle->expr));
    2932             : 
    2933       28502 :         lc2 = lnext(setop_pathkeys, lc2);
    2934             :     }
    2935             : 
    2936             :     /*
    2937             :      * transformSetOperationStmt() ensures that the targetlist never contains
    2938             :      * any resjunk columns, so all eclasses that exist in 'root' must have
    2939             :      * received a new member in the loop above.  Add them to the child_rel's
    2940             :      * eclass_indexes.
    2941             :      */
    2942        9554 :     child_rel->eclass_indexes = bms_add_range(child_rel->eclass_indexes, 0,
    2943        9554 :                                               list_length(root->eq_classes) - 1);
    2944        9554 : }
    2945             : 
    2946             : 
    2947             : /*
    2948             :  * generate_implied_equalities_for_column
    2949             :  *    Create EC-derived joinclauses usable with a specific column.
    2950             :  *
    2951             :  * This is used by indxpath.c to extract potentially indexable joinclauses
    2952             :  * from ECs, and can be used by foreign data wrappers for similar purposes.
    2953             :  * We assume that only expressions in Vars of a single table are of interest,
    2954             :  * but the caller provides a callback function to identify exactly which
    2955             :  * such expressions it would like to know about.
    2956             :  *
    2957             :  * We assume that any given table/index column could appear in only one EC.
    2958             :  * (This should be true in all but the most pathological cases, and if it
    2959             :  * isn't, we stop on the first match anyway.)  Therefore, what we return
    2960             :  * is a redundant list of clauses equating the table/index column to each of
    2961             :  * the other-relation values it is known to be equal to.  Any one of
    2962             :  * these clauses can be used to create a parameterized path, and there
    2963             :  * is no value in using more than one.  (But it *is* worthwhile to create
    2964             :  * a separate parameterized path for each one, since that leads to different
    2965             :  * join orders.)
    2966             :  *
    2967             :  * The caller can pass a Relids set of rels we aren't interested in joining
    2968             :  * to, so as to save the work of creating useless clauses.
    2969             :  */
    2970             : List *
    2971      467236 : generate_implied_equalities_for_column(PlannerInfo *root,
    2972             :                                        RelOptInfo *rel,
    2973             :                                        ec_matches_callback_type callback,
    2974             :                                        void *callback_arg,
    2975             :                                        Relids prohibited_rels)
    2976             : {
    2977      467236 :     List       *result = NIL;
    2978      467236 :     bool        is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
    2979             :     Relids      parent_relids;
    2980             :     int         i;
    2981             : 
    2982             :     /* Should be OK to rely on eclass_indexes */
    2983             :     Assert(root->ec_merging_done);
    2984             : 
    2985             :     /* Indexes are available only on base or "other" member relations. */
    2986             :     Assert(IS_SIMPLE_REL(rel));
    2987             : 
    2988             :     /* If it's a child rel, we'll need to know what its parent(s) are */
    2989      467236 :     if (is_child_rel)
    2990       10526 :         parent_relids = find_childrel_parents(root, rel);
    2991             :     else
    2992      456710 :         parent_relids = NULL;   /* not used, but keep compiler quiet */
    2993             : 
    2994      467236 :     i = -1;
    2995     1274090 :     while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
    2996             :     {
    2997      886018 :         EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
    2998             :         EquivalenceMember *cur_em;
    2999             :         ListCell   *lc2;
    3000             : 
    3001             :         /* Sanity check eclass_indexes only contain ECs for rel */
    3002             :         Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
    3003             : 
    3004             :         /*
    3005             :          * Won't generate joinclauses if const or single-member (the latter
    3006             :          * test covers the volatile case too)
    3007             :          */
    3008      886018 :         if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
    3009      488144 :             continue;
    3010             : 
    3011             :         /*
    3012             :          * Scan members, looking for a match to the target column.  Note that
    3013             :          * child EC members are considered, but only when they belong to the
    3014             :          * target relation.  (Unlike regular members, the same expression
    3015             :          * could be a child member of more than one EC.  Therefore, it's
    3016             :          * potentially order-dependent which EC a child relation's target
    3017             :          * column gets matched to.  This is annoying but it only happens in
    3018             :          * corner cases, so for now we live with just reporting the first
    3019             :          * match.  See also get_eclass_for_sort_expr.)
    3020             :          */
    3021      397874 :         cur_em = NULL;
    3022     1189256 :         foreach(lc2, cur_ec->ec_members)
    3023             :         {
    3024      870994 :             cur_em = (EquivalenceMember *) lfirst(lc2);
    3025     1269746 :             if (bms_equal(cur_em->em_relids, rel->relids) &&
    3026      398752 :                 callback(root, rel, cur_ec, cur_em, callback_arg))
    3027       79612 :                 break;
    3028      791382 :             cur_em = NULL;
    3029             :         }
    3030             : 
    3031      397874 :         if (!cur_em)
    3032      318262 :             continue;
    3033             : 
    3034             :         /*
    3035             :          * Found our match.  Scan the other EC members and attempt to generate
    3036             :          * joinclauses.
    3037             :          */
    3038      263072 :         foreach(lc2, cur_ec->ec_members)
    3039             :         {
    3040      183460 :             EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
    3041             :             Oid         eq_op;
    3042             :             RestrictInfo *rinfo;
    3043             : 
    3044      183460 :             if (other_em->em_is_child)
    3045       20468 :                 continue;       /* ignore children here */
    3046             : 
    3047             :             /* Make sure it'll be a join to a different rel */
    3048      249236 :             if (other_em == cur_em ||
    3049       86244 :                 bms_overlap(other_em->em_relids, rel->relids))
    3050       77080 :                 continue;
    3051             : 
    3052             :             /* Forget it if caller doesn't want joins to this rel */
    3053       85912 :             if (bms_overlap(other_em->em_relids, prohibited_rels))
    3054         138 :                 continue;
    3055             : 
    3056             :             /*
    3057             :              * Also, if this is a child rel, avoid generating a useless join
    3058             :              * to its parent rel(s).
    3059             :              */
    3060       91960 :             if (is_child_rel &&
    3061        6186 :                 bms_overlap(parent_relids, other_em->em_relids))
    3062        2912 :                 continue;
    3063             : 
    3064       82862 :             eq_op = select_equality_operator(cur_ec,
    3065             :                                              cur_em->em_datatype,
    3066             :                                              other_em->em_datatype);
    3067       82862 :             if (!OidIsValid(eq_op))
    3068           0 :                 continue;
    3069             : 
    3070             :             /* set parent_ec to mark as redundant with other joinclauses */
    3071       82862 :             rinfo = create_join_clause(root, cur_ec, eq_op,
    3072             :                                        cur_em, other_em,
    3073             :                                        cur_ec);
    3074             : 
    3075       82862 :             result = lappend(result, rinfo);
    3076             :         }
    3077             : 
    3078             :         /*
    3079             :          * If somehow we failed to create any join clauses, we might as well
    3080             :          * keep scanning the ECs for another match.  But if we did make any,
    3081             :          * we're done, because we don't want to return non-redundant clauses.
    3082             :          */
    3083       79612 :         if (result)
    3084       79164 :             break;
    3085             :     }
    3086             : 
    3087      467236 :     return result;
    3088             : }
    3089             : 
    3090             : /*
    3091             :  * have_relevant_eclass_joinclause
    3092             :  *      Detect whether there is an EquivalenceClass that could produce
    3093             :  *      a joinclause involving the two given relations.
    3094             :  *
    3095             :  * This is essentially a very cut-down version of
    3096             :  * generate_join_implied_equalities().  Note it's OK to occasionally say "yes"
    3097             :  * incorrectly.  Hence we don't bother with details like whether the lack of a
    3098             :  * cross-type operator might prevent the clause from actually being generated.
    3099             :  * False negatives are not always fatal either: they will discourage, but not
    3100             :  * completely prevent, investigation of particular join pathways.
    3101             :  */
    3102             : bool
    3103      141852 : have_relevant_eclass_joinclause(PlannerInfo *root,
    3104             :                                 RelOptInfo *rel1, RelOptInfo *rel2)
    3105             : {
    3106             :     Bitmapset  *matching_ecs;
    3107             :     int         i;
    3108             : 
    3109             :     /*
    3110             :      * Examine only eclasses mentioning both rel1 and rel2.
    3111             :      *
    3112             :      * Note that we do not consider the possibility of an eclass generating
    3113             :      * "join" clauses that mention just one of the rels plus an outer join
    3114             :      * that could be formed from them.  Although such clauses must be
    3115             :      * correctly enforced when we form the outer join, they don't seem like
    3116             :      * sufficient reason to prioritize this join over other ones.  The join
    3117             :      * ordering rules will force the join to be made when necessary.
    3118             :      */
    3119      141852 :     matching_ecs = get_common_eclass_indexes(root, rel1->relids,
    3120             :                                              rel2->relids);
    3121             : 
    3122      141852 :     i = -1;
    3123      141852 :     while ((i = bms_next_member(matching_ecs, i)) >= 0)
    3124             :     {
    3125      118058 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
    3126             :                                                              i);
    3127             : 
    3128             :         /*
    3129             :          * Sanity check that get_common_eclass_indexes gave only ECs
    3130             :          * containing both rels.
    3131             :          */
    3132             :         Assert(bms_overlap(rel1->relids, ec->ec_relids));
    3133             :         Assert(bms_overlap(rel2->relids, ec->ec_relids));
    3134             : 
    3135             :         /*
    3136             :          * Won't generate joinclauses if single-member (this test covers the
    3137             :          * volatile case too)
    3138             :          */
    3139      118058 :         if (list_length(ec->ec_members) <= 1)
    3140           0 :             continue;
    3141             : 
    3142             :         /*
    3143             :          * We do not need to examine the individual members of the EC, because
    3144             :          * all that we care about is whether each rel overlaps the relids of
    3145             :          * at least one member, and get_common_eclass_indexes() and the single
    3146             :          * member check above are sufficient to prove that.  (As with
    3147             :          * have_relevant_joinclause(), it is not necessary that the EC be able
    3148             :          * to form a joinclause relating exactly the two given rels, only that
    3149             :          * it be able to form a joinclause mentioning both, and this will
    3150             :          * surely be true if both of them overlap ec_relids.)
    3151             :          *
    3152             :          * Note we don't test ec_broken; if we did, we'd need a separate code
    3153             :          * path to look through ec_sources.  Checking the membership anyway is
    3154             :          * OK as a possibly-overoptimistic heuristic.
    3155             :          *
    3156             :          * We don't test ec_has_const either, even though a const eclass won't
    3157             :          * generate real join clauses.  This is because if we had "WHERE a.x =
    3158             :          * b.y and a.x = 42", it is worth considering a join between a and b,
    3159             :          * since the join result is likely to be small even though it'll end
    3160             :          * up being an unqualified nestloop.
    3161             :          */
    3162             : 
    3163      118058 :         return true;
    3164             :     }
    3165             : 
    3166       23794 :     return false;
    3167             : }
    3168             : 
    3169             : 
    3170             : /*
    3171             :  * has_relevant_eclass_joinclause
    3172             :  *      Detect whether there is an EquivalenceClass that could produce
    3173             :  *      a joinclause involving the given relation and anything else.
    3174             :  *
    3175             :  * This is the same as have_relevant_eclass_joinclause with the other rel
    3176             :  * implicitly defined as "everything else in the query".
    3177             :  */
    3178             : bool
    3179      177424 : has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
    3180             : {
    3181             :     Bitmapset  *matched_ecs;
    3182             :     int         i;
    3183             : 
    3184             :     /* Examine only eclasses mentioning rel1 */
    3185      177424 :     matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
    3186             : 
    3187      177424 :     i = -1;
    3188      643158 :     while ((i = bms_next_member(matched_ecs, i)) >= 0)
    3189             :     {
    3190      524048 :         EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
    3191             :                                                              i);
    3192             : 
    3193             :         /*
    3194             :          * Won't generate joinclauses if single-member (this test covers the
    3195             :          * volatile case too)
    3196             :          */
    3197      524048 :         if (list_length(ec->ec_members) <= 1)
    3198      262790 :             continue;
    3199             : 
    3200             :         /*
    3201             :          * Per the comment in have_relevant_eclass_joinclause, it's sufficient
    3202             :          * to find an EC that mentions both this rel and some other rel.
    3203             :          */
    3204      261258 :         if (!bms_is_subset(ec->ec_relids, rel1->relids))
    3205       58314 :             return true;
    3206             :     }
    3207             : 
    3208      119110 :     return false;
    3209             : }
    3210             : 
    3211             : 
    3212             : /*
    3213             :  * eclass_useful_for_merging
    3214             :  *    Detect whether the EC could produce any mergejoinable join clauses
    3215             :  *    against the specified relation.
    3216             :  *
    3217             :  * This is just a heuristic test and doesn't have to be exact; it's better
    3218             :  * to say "yes" incorrectly than "no".  Hence we don't bother with details
    3219             :  * like whether the lack of a cross-type operator might prevent the clause
    3220             :  * from actually being generated.
    3221             :  */
    3222             : bool
    3223      610482 : eclass_useful_for_merging(PlannerInfo *root,
    3224             :                           EquivalenceClass *eclass,
    3225             :                           RelOptInfo *rel)
    3226             : {
    3227             :     Relids      relids;
    3228             :     ListCell   *lc;
    3229             : 
    3230             :     Assert(!eclass->ec_merged);
    3231             : 
    3232             :     /*
    3233             :      * Won't generate joinclauses if const or single-member (the latter test
    3234             :      * covers the volatile case too)
    3235             :      */
    3236      610482 :     if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
    3237       59370 :         return false;
    3238             : 
    3239             :     /*
    3240             :      * Note we don't test ec_broken; if we did, we'd need a separate code path
    3241             :      * to look through ec_sources.  Checking the members anyway is OK as a
    3242             :      * possibly-overoptimistic heuristic.
    3243             :      */
    3244             : 
    3245             :     /* If specified rel is a child, we must consider the topmost parent rel */
    3246      551112 :     if (IS_OTHER_REL(rel))
    3247             :     {
    3248             :         Assert(!bms_is_empty(rel->top_parent_relids));
    3249       10434 :         relids = rel->top_parent_relids;
    3250             :     }
    3251             :     else
    3252      540678 :         relids = rel->relids;
    3253             : 
    3254             :     /* If rel already includes all members of eclass, no point in searching */
    3255      551112 :     if (bms_is_subset(eclass->ec_relids, relids))
    3256      211708 :         return false;
    3257             : 
    3258             :     /* To join, we need a member not in the given rel */
    3259      527118 :     foreach(lc, eclass->ec_members)
    3260             :     {
    3261      526596 :         EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
    3262             : 
    3263      526596 :         if (cur_em->em_is_child)
    3264           0 :             continue;           /* ignore children here */
    3265             : 
    3266      526596 :         if (!bms_overlap(cur_em->em_relids, relids))
    3267      338882 :             return true;
    3268             :     }
    3269             : 
    3270         522 :     return false;
    3271             : }
    3272             : 
    3273             : 
    3274             : /*
    3275             :  * is_redundant_derived_clause
    3276             :  *      Test whether rinfo is derived from same EC as any clause in clauselist;
    3277             :  *      if so, it can be presumed to represent a condition that's redundant
    3278             :  *      with that member of the list.
    3279             :  */
    3280             : bool
    3281          72 : is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
    3282             : {
    3283          72 :     EquivalenceClass *parent_ec = rinfo->parent_ec;
    3284             :     ListCell   *lc;
    3285             : 
    3286             :     /* Fail if it's not a potentially-redundant clause from some EC */
    3287          72 :     if (parent_ec == NULL)
    3288          72 :         return false;
    3289             : 
    3290           0 :     foreach(lc, clauselist)
    3291             :     {
    3292           0 :         RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
    3293             : 
    3294           0 :         if (otherrinfo->parent_ec == parent_ec)
    3295           0 :             return true;
    3296             :     }
    3297             : 
    3298           0 :     return false;
    3299             : }
    3300             : 
    3301             : /*
    3302             :  * is_redundant_with_indexclauses
    3303             :  *      Test whether rinfo is redundant with any clause in the IndexClause
    3304             :  *      list.  Here, for convenience, we test both simple identity and
    3305             :  *      whether it is derived from the same EC as any member of the list.
    3306             :  */
    3307             : bool
    3308     1156074 : is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
    3309             : {
    3310     1156074 :     EquivalenceClass *parent_ec = rinfo->parent_ec;
    3311             :     ListCell   *lc;
    3312             : 
    3313     1591530 :     foreach(lc, indexclauses)
    3314             :     {
    3315     1192392 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
    3316     1192392 :         RestrictInfo *otherrinfo = iclause->rinfo;
    3317             : 
    3318             :         /* If indexclause is lossy, it won't enforce the condition exactly */
    3319     1192392 :         if (iclause->lossy)
    3320       36594 :             continue;
    3321             : 
    3322             :         /* Match if it's same clause (pointer equality should be enough) */
    3323     1155798 :         if (rinfo == otherrinfo)
    3324      756936 :             return true;
    3325             :         /* Match if derived from same EC */
    3326      399150 :         if (parent_ec && otherrinfo->parent_ec == parent_ec)
    3327         288 :             return true;
    3328             : 
    3329             :         /*
    3330             :          * No need to look at the derived clauses in iclause->indexquals; they
    3331             :          * couldn't match if the parent clause didn't.
    3332             :          */
    3333             :     }
    3334             : 
    3335      399138 :     return false;
    3336             : }
    3337             : 
    3338             : /*
    3339             :  * get_eclass_indexes_for_relids
    3340             :  *      Build and return a Bitmapset containing the indexes into root's
    3341             :  *      eq_classes list for all eclasses that mention any of these relids
    3342             :  */
    3343             : static Bitmapset *
    3344      857152 : get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
    3345             : {
    3346      857152 :     Bitmapset  *ec_indexes = NULL;
    3347      857152 :     int         i = -1;
    3348             : 
    3349             :     /* Should be OK to rely on eclass_indexes */
    3350             :     Assert(root->ec_merging_done);
    3351             : 
    3352     2790274 :     while ((i = bms_next_member(relids, i)) > 0)
    3353             :     {
    3354     1933122 :         RelOptInfo *rel = root->simple_rel_array[i];
    3355             : 
    3356     1933122 :         if (rel == NULL)        /* must be an outer join */
    3357             :         {
    3358             :             Assert(bms_is_member(i, root->outer_join_rels));
    3359      303970 :             continue;
    3360             :         }
    3361             : 
    3362     1629152 :         ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
    3363             :     }
    3364      857152 :     return ec_indexes;
    3365             : }
    3366             : 
    3367             : /*
    3368             :  * get_common_eclass_indexes
    3369             :  *      Build and return a Bitmapset containing the indexes into root's
    3370             :  *      eq_classes list for all eclasses that mention rels in both
    3371             :  *      relids1 and relids2.
    3372             :  */
    3373             : static Bitmapset *
    3374      480956 : get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
    3375             : {
    3376             :     Bitmapset  *rel1ecs;
    3377             :     Bitmapset  *rel2ecs;
    3378             :     int         relid;
    3379             : 
    3380      480956 :     rel1ecs = get_eclass_indexes_for_relids(root, relids1);
    3381             : 
    3382             :     /*
    3383             :      * We can get away with just using the relation's eclass_indexes directly
    3384             :      * when relids2 is a singleton set.
    3385             :      */
    3386      480956 :     if (bms_get_singleton_member(relids2, &relid))
    3387      379326 :         rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
    3388             :     else
    3389      101630 :         rel2ecs = get_eclass_indexes_for_relids(root, relids2);
    3390             : 
    3391             :     /* Calculate and return the common EC indexes, recycling the left input. */
    3392      480956 :     return bms_int_members(rel1ecs, rel2ecs);
    3393             : }

Generated by: LCOV version 1.14