LCOV - code coverage report
Current view: top level - src/backend/utils/adt - selfuncs.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19beta1 Lines: 88.4 % 2589 2288
Test Date: 2026-06-11 04:16:26 Functions: 96.3 % 81 78
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * selfuncs.c
       4              :  *    Selectivity functions and index cost estimation functions for
       5              :  *    standard operators and index access methods.
       6              :  *
       7              :  *    Selectivity routines are registered in the pg_operator catalog
       8              :  *    in the "oprrest" and "oprjoin" attributes.
       9              :  *
      10              :  *    Index cost functions are located via the index AM's API struct,
      11              :  *    which is obtained from the handler function registered in pg_am.
      12              :  *
      13              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      14              :  * Portions Copyright (c) 1994, Regents of the University of California
      15              :  *
      16              :  *
      17              :  * IDENTIFICATION
      18              :  *    src/backend/utils/adt/selfuncs.c
      19              :  *
      20              :  *-------------------------------------------------------------------------
      21              :  */
      22              : 
      23              : /*----------
      24              :  * Operator selectivity estimation functions are called to estimate the
      25              :  * selectivity of WHERE clauses whose top-level operator is their operator.
      26              :  * We divide the problem into two cases:
      27              :  *      Restriction clause estimation: the clause involves vars of just
      28              :  *          one relation.
      29              :  *      Join clause estimation: the clause involves vars of multiple rels.
      30              :  * Join selectivity estimation is far more difficult and usually less accurate
      31              :  * than restriction estimation.
      32              :  *
      33              :  * When dealing with the inner scan of a nestloop join, we consider the
      34              :  * join's joinclauses as restriction clauses for the inner relation, and
      35              :  * treat vars of the outer relation as parameters (a/k/a constants of unknown
      36              :  * values).  So, restriction estimators need to be able to accept an argument
      37              :  * telling which relation is to be treated as the variable.
      38              :  *
      39              :  * The call convention for a restriction estimator (oprrest function) is
      40              :  *
      41              :  *      Selectivity oprrest (PlannerInfo *root,
      42              :  *                           Oid operator,
      43              :  *                           List *args,
      44              :  *                           int varRelid);
      45              :  *
      46              :  * root: general information about the query (rtable and RelOptInfo lists
      47              :  * are particularly important for the estimator).
      48              :  * operator: OID of the specific operator in question.
      49              :  * args: argument list from the operator clause.
      50              :  * varRelid: if not zero, the relid (rtable index) of the relation to
      51              :  * be treated as the variable relation.  May be zero if the args list
      52              :  * is known to contain vars of only one relation.
      53              :  *
      54              :  * This is represented at the SQL level (in pg_proc) as
      55              :  *
      56              :  *      float8 oprrest (internal, oid, internal, int4);
      57              :  *
      58              :  * The result is a selectivity, that is, a fraction (0 to 1) of the rows
      59              :  * of the relation that are expected to produce a TRUE result for the
      60              :  * given operator.
      61              :  *
      62              :  * The call convention for a join estimator (oprjoin function) is similar
      63              :  * except that varRelid is not needed, and instead join information is
      64              :  * supplied:
      65              :  *
      66              :  *      Selectivity oprjoin (PlannerInfo *root,
      67              :  *                           Oid operator,
      68              :  *                           List *args,
      69              :  *                           JoinType jointype,
      70              :  *                           SpecialJoinInfo *sjinfo);
      71              :  *
      72              :  *      float8 oprjoin (internal, oid, internal, int2, internal);
      73              :  *
      74              :  * (Before Postgres 8.4, join estimators had only the first four of these
      75              :  * parameters.  That signature is still allowed, but deprecated.)  The
      76              :  * relationship between jointype and sjinfo is explained in the comments for
      77              :  * clause_selectivity() --- the short version is that jointype is usually
      78              :  * best ignored in favor of examining sjinfo.
      79              :  *
      80              :  * Join selectivity for regular inner and outer joins is defined as the
      81              :  * fraction (0 to 1) of the cross product of the relations that is expected
      82              :  * to produce a TRUE result for the given operator.  For both semi and anti
      83              :  * joins, however, the selectivity is defined as the fraction of the left-hand
      84              :  * side relation's rows that are expected to have a match (ie, at least one
      85              :  * row with a TRUE result) in the right-hand side.
      86              :  *
      87              :  * For both oprrest and oprjoin functions, the operator's input collation OID
      88              :  * (if any) is passed using the standard fmgr mechanism, so that the estimator
      89              :  * function can fetch it with PG_GET_COLLATION().  Note, however, that all
      90              :  * statistics in pg_statistic are currently built using the relevant column's
      91              :  * collation.
      92              :  *----------
      93              :  */
      94              : 
      95              : #include "postgres.h"
      96              : 
      97              : #include <ctype.h>
      98              : #include <math.h>
      99              : 
     100              : #include "access/brin.h"
     101              : #include "access/brin_page.h"
     102              : #include "access/gin.h"
     103              : #include "access/table.h"
     104              : #include "access/tableam.h"
     105              : #include "access/visibilitymap.h"
     106              : #include "catalog/pg_collation.h"
     107              : #include "catalog/pg_operator.h"
     108              : #include "catalog/pg_statistic.h"
     109              : #include "catalog/pg_statistic_ext.h"
     110              : #include "executor/nodeAgg.h"
     111              : #include "miscadmin.h"
     112              : #include "nodes/makefuncs.h"
     113              : #include "nodes/nodeFuncs.h"
     114              : #include "optimizer/clauses.h"
     115              : #include "optimizer/cost.h"
     116              : #include "optimizer/optimizer.h"
     117              : #include "optimizer/pathnode.h"
     118              : #include "optimizer/paths.h"
     119              : #include "optimizer/plancat.h"
     120              : #include "parser/parse_clause.h"
     121              : #include "parser/parse_relation.h"
     122              : #include "parser/parsetree.h"
     123              : #include "rewrite/rewriteManip.h"
     124              : #include "statistics/statistics.h"
     125              : #include "storage/bufmgr.h"
     126              : #include "utils/acl.h"
     127              : #include "utils/array.h"
     128              : #include "utils/builtins.h"
     129              : #include "utils/date.h"
     130              : #include "utils/datum.h"
     131              : #include "utils/fmgroids.h"
     132              : #include "utils/index_selfuncs.h"
     133              : #include "utils/lsyscache.h"
     134              : #include "utils/memutils.h"
     135              : #include "utils/pg_locale.h"
     136              : #include "utils/rel.h"
     137              : #include "utils/selfuncs.h"
     138              : #include "utils/snapmgr.h"
     139              : #include "utils/spccache.h"
     140              : #include "utils/syscache.h"
     141              : #include "utils/timestamp.h"
     142              : #include "utils/typcache.h"
     143              : 
     144              : #define DEFAULT_PAGE_CPU_MULTIPLIER 50.0
     145              : 
     146              : /*
     147              :  * In production builds, switch to hash-based MCV matching when the lists are
     148              :  * large enough to amortize hash setup cost.  (This threshold is compared to
     149              :  * the sum of the lengths of the two MCV lists.  This is simplistic but seems
     150              :  * to work well enough.)  In debug builds, we use a smaller threshold so that
     151              :  * the regression tests cover both paths well.
     152              :  */
     153              : #ifndef USE_ASSERT_CHECKING
     154              : #define EQJOINSEL_MCV_HASH_THRESHOLD 200
     155              : #else
     156              : #define EQJOINSEL_MCV_HASH_THRESHOLD 20
     157              : #endif
     158              : 
     159              : /* Entries in the simplehash hash table used by eqjoinsel_find_matches */
     160              : typedef struct MCVHashEntry
     161              : {
     162              :     Datum       value;          /* the value represented by this entry */
     163              :     int         index;          /* its index in the relevant AttStatsSlot */
     164              :     uint32      hash;           /* hash code for the Datum */
     165              :     char        status;         /* status code used by simplehash.h */
     166              : } MCVHashEntry;
     167              : 
     168              : /* private_data for the simplehash hash table */
     169              : typedef struct MCVHashContext
     170              : {
     171              :     FunctionCallInfo equal_fcinfo;  /* the equality join operator */
     172              :     FunctionCallInfo hash_fcinfo;   /* the hash function to use */
     173              :     bool        op_is_reversed; /* equality compares hash type to probe type */
     174              :     bool        insert_mode;    /* doing inserts or lookups? */
     175              :     bool        hash_typbyval;  /* typbyval of hashed data type */
     176              :     int16       hash_typlen;    /* typlen of hashed data type */
     177              : } MCVHashContext;
     178              : 
     179              : /* forward reference */
     180              : typedef struct MCVHashTable_hash MCVHashTable_hash;
     181              : 
     182              : /* Hooks for plugins to get control when we ask for stats */
     183              : get_relation_stats_hook_type get_relation_stats_hook = NULL;
     184              : get_index_stats_hook_type get_index_stats_hook = NULL;
     185              : 
     186              : static double eqsel_internal(PG_FUNCTION_ARGS, bool negate);
     187              : static double eqjoinsel_inner(FmgrInfo *eqproc, Oid collation,
     188              :                               Oid hashLeft, Oid hashRight,
     189              :                               VariableStatData *vardata1, VariableStatData *vardata2,
     190              :                               double nd1, double nd2,
     191              :                               bool isdefault1, bool isdefault2,
     192              :                               AttStatsSlot *sslot1, AttStatsSlot *sslot2,
     193              :                               Form_pg_statistic stats1, Form_pg_statistic stats2,
     194              :                               bool have_mcvs1, bool have_mcvs2,
     195              :                               bool *hasmatch1, bool *hasmatch2,
     196              :                               int *p_nmatches);
     197              : static double eqjoinsel_semi(FmgrInfo *eqproc, Oid collation,
     198              :                              Oid hashLeft, Oid hashRight,
     199              :                              bool op_is_reversed,
     200              :                              VariableStatData *vardata1, VariableStatData *vardata2,
     201              :                              double nd1, double nd2,
     202              :                              bool isdefault1, bool isdefault2,
     203              :                              AttStatsSlot *sslot1, AttStatsSlot *sslot2,
     204              :                              Form_pg_statistic stats1, Form_pg_statistic stats2,
     205              :                              bool have_mcvs1, bool have_mcvs2,
     206              :                              bool *hasmatch1, bool *hasmatch2,
     207              :                              int *p_nmatches,
     208              :                              RelOptInfo *inner_rel);
     209              : static void eqjoinsel_find_matches(FmgrInfo *eqproc, Oid collation,
     210              :                                    Oid hashLeft, Oid hashRight,
     211              :                                    bool op_is_reversed,
     212              :                                    AttStatsSlot *sslot1, AttStatsSlot *sslot2,
     213              :                                    int nvalues1, int nvalues2,
     214              :                                    bool *hasmatch1, bool *hasmatch2,
     215              :                                    int *p_nmatches, double *p_matchprodfreq);
     216              : static uint32 hash_mcv(MCVHashTable_hash *tab, Datum key);
     217              : static bool mcvs_equal(MCVHashTable_hash *tab, Datum key0, Datum key1);
     218              : static bool estimate_multivariate_ndistinct(PlannerInfo *root,
     219              :                                             RelOptInfo *rel, List **varinfos, double *ndistinct);
     220              : static bool convert_to_scalar(Datum value, Oid valuetypid, Oid collid,
     221              :                               double *scaledvalue,
     222              :                               Datum lobound, Datum hibound, Oid boundstypid,
     223              :                               double *scaledlobound, double *scaledhibound);
     224              : static double convert_numeric_to_scalar(Datum value, Oid typid, bool *failure);
     225              : static void convert_string_to_scalar(char *value,
     226              :                                      double *scaledvalue,
     227              :                                      char *lobound,
     228              :                                      double *scaledlobound,
     229              :                                      char *hibound,
     230              :                                      double *scaledhibound);
     231              : static void convert_bytea_to_scalar(Datum value,
     232              :                                     double *scaledvalue,
     233              :                                     Datum lobound,
     234              :                                     double *scaledlobound,
     235              :                                     Datum hibound,
     236              :                                     double *scaledhibound);
     237              : static double convert_one_string_to_scalar(char *value,
     238              :                                            int rangelo, int rangehi);
     239              : static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
     240              :                                           int rangelo, int rangehi);
     241              : static char *convert_string_datum(Datum value, Oid typid, Oid collid,
     242              :                                   bool *failure);
     243              : static double convert_timevalue_to_scalar(Datum value, Oid typid,
     244              :                                           bool *failure);
     245              : static Node *strip_all_phvs_deep(PlannerInfo *root, Node *node);
     246              : static bool contain_placeholder_walker(Node *node, void *context);
     247              : static Node *strip_all_phvs_mutator(Node *node, void *context);
     248              : static void examine_simple_variable(PlannerInfo *root, Var *var,
     249              :                                     VariableStatData *vardata);
     250              : static void examine_indexcol_variable(PlannerInfo *root, IndexOptInfo *index,
     251              :                                       int indexcol, VariableStatData *vardata);
     252              : static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata,
     253              :                                Oid sortop, Oid collation,
     254              :                                Datum *min, Datum *max);
     255              : static void get_stats_slot_range(AttStatsSlot *sslot,
     256              :                                  Oid opfuncoid, FmgrInfo *opproc,
     257              :                                  Oid collation, int16 typLen, bool typByVal,
     258              :                                  Datum *min, Datum *max, bool *p_have_data);
     259              : static bool get_actual_variable_range(PlannerInfo *root,
     260              :                                       VariableStatData *vardata,
     261              :                                       Oid sortop, Oid collation,
     262              :                                       Datum *min, Datum *max);
     263              : static bool get_actual_variable_endpoint(Relation heapRel,
     264              :                                          Relation indexRel,
     265              :                                          ScanDirection indexscandir,
     266              :                                          ScanKey scankeys,
     267              :                                          int16 typLen,
     268              :                                          bool typByVal,
     269              :                                          TupleTableSlot *tableslot,
     270              :                                          MemoryContext outercontext,
     271              :                                          Datum *endpointDatum);
     272              : static RelOptInfo *find_join_input_rel(PlannerInfo *root, Relids relids);
     273              : static double btcost_correlation(IndexOptInfo *index,
     274              :                                  VariableStatData *vardata);
     275              : 
     276              : /* Define support routines for MCV hash tables */
     277              : #define SH_PREFIX               MCVHashTable
     278              : #define SH_ELEMENT_TYPE         MCVHashEntry
     279              : #define SH_KEY_TYPE             Datum
     280              : #define SH_KEY                  value
     281              : #define SH_HASH_KEY(tab,key)    hash_mcv(tab, key)
     282              : #define SH_EQUAL(tab,key0,key1) mcvs_equal(tab, key0, key1)
     283              : #define SH_SCOPE                static inline
     284              : #define SH_STORE_HASH
     285              : #define SH_GET_HASH(tab,ent)    (ent)->hash
     286              : #define SH_DEFINE
     287              : #define SH_DECLARE
     288              : #include "lib/simplehash.h"
     289              : 
     290              : 
     291              : /*
     292              :  *      eqsel           - Selectivity of "=" for any data types.
     293              :  *
     294              :  * Note: this routine is also used to estimate selectivity for some
     295              :  * operators that are not "=" but have comparable selectivity behavior,
     296              :  * such as "~=" (geometric approximate-match).  Even for "=", we must
     297              :  * keep in mind that the left and right datatypes may differ.
     298              :  */
     299              : Datum
     300       562198 : eqsel(PG_FUNCTION_ARGS)
     301              : {
     302       562198 :     PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, false));
     303              : }
     304              : 
     305              : /*
     306              :  * Common code for eqsel() and neqsel()
     307              :  */
     308              : static double
     309       594736 : eqsel_internal(PG_FUNCTION_ARGS, bool negate)
     310              : {
     311       594736 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
     312       594736 :     Oid         operator = PG_GETARG_OID(1);
     313       594736 :     List       *args = (List *) PG_GETARG_POINTER(2);
     314       594736 :     int         varRelid = PG_GETARG_INT32(3);
     315       594736 :     Oid         collation = PG_GET_COLLATION();
     316              :     VariableStatData vardata;
     317              :     Node       *other;
     318              :     bool        varonleft;
     319              :     double      selec;
     320              : 
     321              :     /*
     322              :      * When asked about <>, we do the estimation using the corresponding =
     323              :      * operator, then convert to <> via "1.0 - eq_selectivity - nullfrac".
     324              :      */
     325       594736 :     if (negate)
     326              :     {
     327        32538 :         operator = get_negator(operator);
     328        32538 :         if (!OidIsValid(operator))
     329              :         {
     330              :             /* Use default selectivity (should we raise an error instead?) */
     331            0 :             return 1.0 - DEFAULT_EQ_SEL;
     332              :         }
     333              :     }
     334              : 
     335              :     /*
     336              :      * If expression is not variable = something or something = variable, then
     337              :      * punt and return a default estimate.
     338              :      */
     339       594736 :     if (!get_restriction_variable(root, args, varRelid,
     340              :                                   &vardata, &other, &varonleft))
     341         3618 :         return negate ? (1.0 - DEFAULT_EQ_SEL) : DEFAULT_EQ_SEL;
     342              : 
     343              :     /*
     344              :      * We can do a lot better if the something is a constant.  (Note: the
     345              :      * Const might result from estimation rather than being a simple constant
     346              :      * in the query.)
     347              :      */
     348       591114 :     if (IsA(other, Const))
     349       223573 :         selec = var_eq_const(&vardata, operator, collation,
     350       223573 :                              ((Const *) other)->constvalue,
     351       223573 :                              ((Const *) other)->constisnull,
     352              :                              varonleft, negate);
     353              :     else
     354       367541 :         selec = var_eq_non_const(&vardata, operator, collation, other,
     355              :                                  varonleft, negate);
     356              : 
     357       591114 :     ReleaseVariableStats(vardata);
     358              : 
     359       591114 :     return selec;
     360              : }
     361              : 
     362              : /*
     363              :  * var_eq_const --- eqsel for var = const case
     364              :  *
     365              :  * This is exported so that some other estimation functions can use it.
     366              :  */
     367              : double
     368       253551 : var_eq_const(VariableStatData *vardata, Oid oproid, Oid collation,
     369              :              Datum constval, bool constisnull,
     370              :              bool varonleft, bool negate)
     371              : {
     372              :     double      selec;
     373       253551 :     double      nullfrac = 0.0;
     374              :     bool        isdefault;
     375              :     Oid         opfuncoid;
     376              : 
     377              :     /*
     378              :      * If the constant is NULL, assume operator is strict and return zero, ie,
     379              :      * operator will never return TRUE.  (It's zero even for a negator op.)
     380              :      */
     381       253551 :     if (constisnull)
     382          272 :         return 0.0;
     383              : 
     384              :     /*
     385              :      * Grab the nullfrac for use below.  Note we allow use of nullfrac
     386              :      * regardless of security check.
     387              :      */
     388       253279 :     if (HeapTupleIsValid(vardata->statsTuple))
     389              :     {
     390              :         Form_pg_statistic stats;
     391              : 
     392       180480 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
     393       180480 :         nullfrac = stats->stanullfrac;
     394              :     }
     395              : 
     396              :     /*
     397              :      * If we matched the var to a unique index, DISTINCT or GROUP-BY clause,
     398              :      * assume there is exactly one match regardless of anything else.  (This
     399              :      * is slightly bogus, since the index or clause's equality operator might
     400              :      * be different from ours, but it's much more likely to be right than
     401              :      * ignoring the information.)
     402              :      */
     403       253279 :     if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
     404              :     {
     405        51111 :         selec = 1.0 / vardata->rel->tuples;
     406              :     }
     407       340620 :     else if (HeapTupleIsValid(vardata->statsTuple) &&
     408       138452 :              statistic_proc_security_check(vardata,
     409       138452 :                                            (opfuncoid = get_opcode(oproid))))
     410       138452 :     {
     411              :         AttStatsSlot sslot;
     412       138452 :         bool        match = false;
     413              :         int         i;
     414              : 
     415              :         /*
     416              :          * Is the constant "=" to any of the column's most common values?
     417              :          * (Although the given operator may not really be "=", we will assume
     418              :          * that seeing whether it returns TRUE is an appropriate test.  If you
     419              :          * don't like this, maybe you shouldn't be using eqsel for your
     420              :          * operator...)
     421              :          */
     422       138452 :         if (get_attstatsslot(&sslot, vardata->statsTuple,
     423              :                              STATISTIC_KIND_MCV, InvalidOid,
     424              :                              ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
     425              :         {
     426       125289 :             LOCAL_FCINFO(fcinfo, 2);
     427              :             FmgrInfo    eqproc;
     428              : 
     429       125289 :             fmgr_info(opfuncoid, &eqproc);
     430              : 
     431              :             /*
     432              :              * Save a few cycles by setting up the fcinfo struct just once.
     433              :              * Using FunctionCallInvoke directly also avoids failure if the
     434              :              * eqproc returns NULL, though really equality functions should
     435              :              * never do that.
     436              :              */
     437       125289 :             InitFunctionCallInfoData(*fcinfo, &eqproc, 2, collation,
     438              :                                      NULL, NULL);
     439       125289 :             fcinfo->args[0].isnull = false;
     440       125289 :             fcinfo->args[1].isnull = false;
     441              :             /* be careful to apply operator right way 'round */
     442       125289 :             if (varonleft)
     443       125263 :                 fcinfo->args[1].value = constval;
     444              :             else
     445           26 :                 fcinfo->args[0].value = constval;
     446              : 
     447      2090443 :             for (i = 0; i < sslot.nvalues; i++)
     448              :             {
     449              :                 Datum       fresult;
     450              : 
     451      2029550 :                 if (varonleft)
     452      2029504 :                     fcinfo->args[0].value = sslot.values[i];
     453              :                 else
     454           46 :                     fcinfo->args[1].value = sslot.values[i];
     455      2029550 :                 fcinfo->isnull = false;
     456      2029550 :                 fresult = FunctionCallInvoke(fcinfo);
     457      2029550 :                 if (!fcinfo->isnull && DatumGetBool(fresult))
     458              :                 {
     459        64396 :                     match = true;
     460        64396 :                     break;
     461              :                 }
     462              :             }
     463              :         }
     464              :         else
     465              :         {
     466              :             /* no most-common-value info available */
     467        13163 :             i = 0;              /* keep compiler quiet */
     468              :         }
     469              : 
     470       138452 :         if (match)
     471              :         {
     472              :             /*
     473              :              * Constant is "=" to this common value.  We know selectivity
     474              :              * exactly (or as exactly as ANALYZE could calculate it, anyway).
     475              :              */
     476        64396 :             selec = sslot.numbers[i];
     477              :         }
     478              :         else
     479              :         {
     480              :             /*
     481              :              * Comparison is against a constant that is neither NULL nor any
     482              :              * of the common values.  Its selectivity cannot be more than
     483              :              * this:
     484              :              */
     485        74056 :             double      sumcommon = 0.0;
     486              :             double      otherdistinct;
     487              : 
     488      1771936 :             for (i = 0; i < sslot.nnumbers; i++)
     489      1697880 :                 sumcommon += sslot.numbers[i];
     490        74056 :             selec = 1.0 - sumcommon - nullfrac;
     491        74056 :             CLAMP_PROBABILITY(selec);
     492              : 
     493              :             /*
     494              :              * and in fact it's probably a good deal less. We approximate that
     495              :              * all the not-common values share this remaining fraction
     496              :              * equally, so we divide by the number of other distinct values.
     497              :              */
     498        74056 :             otherdistinct = get_variable_numdistinct(vardata, &isdefault) -
     499        74056 :                 sslot.nnumbers;
     500        74056 :             if (otherdistinct > 1)
     501        34580 :                 selec /= otherdistinct;
     502              : 
     503              :             /*
     504              :              * Another cross-check: selectivity shouldn't be estimated as more
     505              :              * than the least common "most common value".
     506              :              */
     507        74056 :             if (sslot.nnumbers > 0 && selec > sslot.numbers[sslot.nnumbers - 1])
     508            0 :                 selec = sslot.numbers[sslot.nnumbers - 1];
     509              :         }
     510              : 
     511       138452 :         free_attstatsslot(&sslot);
     512              :     }
     513              :     else
     514              :     {
     515              :         /*
     516              :          * No ANALYZE stats available, so make a guess using estimated number
     517              :          * of distinct values and assuming they are equally common. (The guess
     518              :          * is unlikely to be very good, but we do know a few special cases.)
     519              :          */
     520        63716 :         selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
     521              :     }
     522              : 
     523              :     /* now adjust if we wanted <> rather than = */
     524       253279 :     if (negate)
     525        25801 :         selec = 1.0 - selec - nullfrac;
     526              : 
     527              :     /* result should be in range, but make sure... */
     528       253279 :     CLAMP_PROBABILITY(selec);
     529              : 
     530       253279 :     return selec;
     531              : }
     532              : 
     533              : /*
     534              :  * var_eq_non_const --- eqsel for var = something-other-than-const case
     535              :  *
     536              :  * This is exported so that some other estimation functions can use it.
     537              :  */
     538              : double
     539       367541 : var_eq_non_const(VariableStatData *vardata, Oid oproid, Oid collation,
     540              :                  Node *other,
     541              :                  bool varonleft, bool negate)
     542              : {
     543              :     double      selec;
     544       367541 :     double      nullfrac = 0.0;
     545              :     bool        isdefault;
     546              : 
     547              :     /*
     548              :      * Grab the nullfrac for use below.
     549              :      */
     550       367541 :     if (HeapTupleIsValid(vardata->statsTuple))
     551              :     {
     552              :         Form_pg_statistic stats;
     553              : 
     554       219688 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
     555       219688 :         nullfrac = stats->stanullfrac;
     556              :     }
     557              : 
     558              :     /*
     559              :      * If we matched the var to a unique index, DISTINCT or GROUP-BY clause,
     560              :      * assume there is exactly one match regardless of anything else.  (This
     561              :      * is slightly bogus, since the index or clause's equality operator might
     562              :      * be different from ours, but it's much more likely to be right than
     563              :      * ignoring the information.)
     564              :      */
     565       367541 :     if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
     566              :     {
     567       129638 :         selec = 1.0 / vardata->rel->tuples;
     568              :     }
     569       237903 :     else if (HeapTupleIsValid(vardata->statsTuple))
     570              :     {
     571              :         double      ndistinct;
     572              :         AttStatsSlot sslot;
     573              : 
     574              :         /*
     575              :          * Search is for a value that we do not know a priori, but we will
     576              :          * assume it is not NULL.  Estimate the selectivity as non-null
     577              :          * fraction divided by number of distinct values, so that we get a
     578              :          * result averaged over all possible values whether common or
     579              :          * uncommon.  (Essentially, we are assuming that the not-yet-known
     580              :          * comparison value is equally likely to be any of the possible
     581              :          * values, regardless of their frequency in the table.  Is that a good
     582              :          * idea?)
     583              :          */
     584       110886 :         selec = 1.0 - nullfrac;
     585       110886 :         ndistinct = get_variable_numdistinct(vardata, &isdefault);
     586       110886 :         if (ndistinct > 1)
     587       108307 :             selec /= ndistinct;
     588              : 
     589              :         /*
     590              :          * Cross-check: selectivity should never be estimated as more than the
     591              :          * most common value's.
     592              :          */
     593       110886 :         if (get_attstatsslot(&sslot, vardata->statsTuple,
     594              :                              STATISTIC_KIND_MCV, InvalidOid,
     595              :                              ATTSTATSSLOT_NUMBERS))
     596              :         {
     597        95042 :             if (sslot.nnumbers > 0 && selec > sslot.numbers[0])
     598          483 :                 selec = sslot.numbers[0];
     599        95042 :             free_attstatsslot(&sslot);
     600              :         }
     601              :     }
     602              :     else
     603              :     {
     604              :         /*
     605              :          * No ANALYZE stats available, so make a guess using estimated number
     606              :          * of distinct values and assuming they are equally common. (The guess
     607              :          * is unlikely to be very good, but we do know a few special cases.)
     608              :          */
     609       127017 :         selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
     610              :     }
     611              : 
     612              :     /* now adjust if we wanted <> rather than = */
     613       367541 :     if (negate)
     614         5148 :         selec = 1.0 - selec - nullfrac;
     615              : 
     616              :     /* result should be in range, but make sure... */
     617       367541 :     CLAMP_PROBABILITY(selec);
     618              : 
     619       367541 :     return selec;
     620              : }
     621              : 
     622              : /*
     623              :  *      neqsel          - Selectivity of "!=" for any data types.
     624              :  *
     625              :  * This routine is also used for some operators that are not "!="
     626              :  * but have comparable selectivity behavior.  See above comments
     627              :  * for eqsel().
     628              :  */
     629              : Datum
     630        32538 : neqsel(PG_FUNCTION_ARGS)
     631              : {
     632        32538 :     PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, true));
     633              : }
     634              : 
     635              : /*
     636              :  *  scalarineqsel       - Selectivity of "<", "<=", ">", ">=" for scalars.
     637              :  *
     638              :  * This is the guts of scalarltsel/scalarlesel/scalargtsel/scalargesel.
     639              :  * The isgt and iseq flags distinguish which of the four cases apply.
     640              :  *
     641              :  * The caller has commuted the clause, if necessary, so that we can treat
     642              :  * the variable as being on the left.  The caller must also make sure that
     643              :  * the other side of the clause is a non-null Const, and dissect that into
     644              :  * a value and datatype.  (This definition simplifies some callers that
     645              :  * want to estimate against a computed value instead of a Const node.)
     646              :  *
     647              :  * This routine works for any datatype (or pair of datatypes) known to
     648              :  * convert_to_scalar().  If it is applied to some other datatype,
     649              :  * it will return an approximate estimate based on assuming that the constant
     650              :  * value falls in the middle of the bin identified by binary search.
     651              :  */
     652              : static double
     653       247709 : scalarineqsel(PlannerInfo *root, Oid operator, bool isgt, bool iseq,
     654              :               Oid collation,
     655              :               VariableStatData *vardata, Datum constval, Oid consttype)
     656              : {
     657              :     Form_pg_statistic stats;
     658              :     FmgrInfo    opproc;
     659              :     double      mcv_selec,
     660              :                 hist_selec,
     661              :                 sumcommon;
     662              :     double      selec;
     663              : 
     664       247709 :     if (!HeapTupleIsValid(vardata->statsTuple))
     665              :     {
     666              :         /*
     667              :          * No stats are available.  Typically this means we have to fall back
     668              :          * on the default estimate; but if the variable is CTID then we can
     669              :          * make an estimate based on comparing the constant to the table size.
     670              :          */
     671        21423 :         if (vardata->var && IsA(vardata->var, Var) &&
     672        17376 :             ((Var *) vardata->var)->varattno == SelfItemPointerAttributeNumber)
     673              :         {
     674              :             ItemPointer itemptr;
     675              :             double      block;
     676              :             double      density;
     677              : 
     678              :             /*
     679              :              * If the relation's empty, we're going to include all of it.
     680              :              * (This is mostly to avoid divide-by-zero below.)
     681              :              */
     682         1679 :             if (vardata->rel->pages == 0)
     683            0 :                 return 1.0;
     684              : 
     685         1679 :             itemptr = (ItemPointer) DatumGetPointer(constval);
     686         1679 :             block = ItemPointerGetBlockNumberNoCheck(itemptr);
     687              : 
     688              :             /*
     689              :              * Determine the average number of tuples per page (density).
     690              :              *
     691              :              * Since the last page will, on average, be only half full, we can
     692              :              * estimate it to have half as many tuples as earlier pages.  So
     693              :              * give it half the weight of a regular page.
     694              :              */
     695         1679 :             density = vardata->rel->tuples / (vardata->rel->pages - 0.5);
     696              : 
     697              :             /* If target is the last page, use half the density. */
     698         1679 :             if (block >= vardata->rel->pages - 1)
     699           31 :                 density *= 0.5;
     700              : 
     701              :             /*
     702              :              * Using the average tuples per page, calculate how far into the
     703              :              * page the itemptr is likely to be and adjust block accordingly,
     704              :              * by adding that fraction of a whole block (but never more than a
     705              :              * whole block, no matter how high the itemptr's offset is).  Here
     706              :              * we are ignoring the possibility of dead-tuple line pointers,
     707              :              * which is fairly bogus, but we lack the info to do better.
     708              :              */
     709         1679 :             if (density > 0.0)
     710              :             {
     711         1679 :                 OffsetNumber offset = ItemPointerGetOffsetNumberNoCheck(itemptr);
     712              : 
     713         1679 :                 block += Min(offset / density, 1.0);
     714              :             }
     715              : 
     716              :             /*
     717              :              * Convert relative block number to selectivity.  Again, the last
     718              :              * page has only half weight.
     719              :              */
     720         1679 :             selec = block / (vardata->rel->pages - 0.5);
     721              : 
     722              :             /*
     723              :              * The calculation so far gave us a selectivity for the "<=" case.
     724              :              * We'll have one fewer tuple for "<" and one additional tuple for
     725              :              * ">=", the latter of which we'll reverse the selectivity for
     726              :              * below, so we can simply subtract one tuple for both cases.  The
     727              :              * cases that need this adjustment can be identified by iseq being
     728              :              * equal to isgt.
     729              :              */
     730         1679 :             if (iseq == isgt && vardata->rel->tuples >= 1.0)
     731         1562 :                 selec -= (1.0 / vardata->rel->tuples);
     732              : 
     733              :             /* Finally, reverse the selectivity for the ">", ">=" cases. */
     734         1679 :             if (isgt)
     735         1547 :                 selec = 1.0 - selec;
     736              : 
     737         1679 :             CLAMP_PROBABILITY(selec);
     738         1679 :             return selec;
     739              :         }
     740              : 
     741              :         /* no stats available, so default result */
     742        19744 :         return DEFAULT_INEQ_SEL;
     743              :     }
     744       226286 :     stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
     745              : 
     746       226286 :     fmgr_info(get_opcode(operator), &opproc);
     747              : 
     748              :     /*
     749              :      * If we have most-common-values info, add up the fractions of the MCV
     750              :      * entries that satisfy MCV OP CONST.  These fractions contribute directly
     751              :      * to the result selectivity.  Also add up the total fraction represented
     752              :      * by MCV entries.
     753              :      */
     754       226286 :     mcv_selec = mcv_selectivity(vardata, &opproc, collation, constval, true,
     755              :                                 &sumcommon);
     756              : 
     757              :     /*
     758              :      * If there is a histogram, determine which bin the constant falls in, and
     759              :      * compute the resulting contribution to selectivity.
     760              :      */
     761       226286 :     hist_selec = ineq_histogram_selectivity(root, vardata,
     762              :                                             operator, &opproc, isgt, iseq,
     763              :                                             collation,
     764              :                                             constval, consttype);
     765              : 
     766              :     /*
     767              :      * Now merge the results from the MCV and histogram calculations,
     768              :      * realizing that the histogram covers only the non-null values that are
     769              :      * not listed in MCV.
     770              :      */
     771       226286 :     selec = 1.0 - stats->stanullfrac - sumcommon;
     772              : 
     773       226286 :     if (hist_selec >= 0.0)
     774       138170 :         selec *= hist_selec;
     775              :     else
     776              :     {
     777              :         /*
     778              :          * If no histogram but there are values not accounted for by MCV,
     779              :          * arbitrarily assume half of them will match.
     780              :          */
     781        88116 :         selec *= 0.5;
     782              :     }
     783              : 
     784       226286 :     selec += mcv_selec;
     785              : 
     786              :     /* result should be in range, but make sure... */
     787       226286 :     CLAMP_PROBABILITY(selec);
     788              : 
     789       226286 :     return selec;
     790              : }
     791              : 
     792              : /*
     793              :  *  mcv_selectivity         - Examine the MCV list for selectivity estimates
     794              :  *
     795              :  * Determine the fraction of the variable's MCV population that satisfies
     796              :  * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.  Also
     797              :  * compute the fraction of the total column population represented by the MCV
     798              :  * list.  This code will work for any boolean-returning predicate operator.
     799              :  *
     800              :  * The function result is the MCV selectivity, and the fraction of the
     801              :  * total population is returned into *sumcommonp.  Zeroes are returned
     802              :  * if there is no MCV list.
     803              :  */
     804              : double
     805       230693 : mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation,
     806              :                 Datum constval, bool varonleft,
     807              :                 double *sumcommonp)
     808              : {
     809              :     double      mcv_selec,
     810              :                 sumcommon;
     811              :     AttStatsSlot sslot;
     812              :     int         i;
     813              : 
     814       230693 :     mcv_selec = 0.0;
     815       230693 :     sumcommon = 0.0;
     816              : 
     817       459584 :     if (HeapTupleIsValid(vardata->statsTuple) &&
     818       457507 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
     819       228616 :         get_attstatsslot(&sslot, vardata->statsTuple,
     820              :                          STATISTIC_KIND_MCV, InvalidOid,
     821              :                          ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
     822              :     {
     823       128117 :         LOCAL_FCINFO(fcinfo, 2);
     824              : 
     825              :         /*
     826              :          * We invoke the opproc "by hand" so that we won't fail on NULL
     827              :          * results.  Such cases won't arise for normal comparison functions,
     828              :          * but generic_restriction_selectivity could perhaps be used with
     829              :          * operators that can return NULL.  A small side benefit is to not
     830              :          * need to re-initialize the fcinfo struct from scratch each time.
     831              :          */
     832       128117 :         InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
     833              :                                  NULL, NULL);
     834       128117 :         fcinfo->args[0].isnull = false;
     835       128117 :         fcinfo->args[1].isnull = false;
     836              :         /* be careful to apply operator right way 'round */
     837       128117 :         if (varonleft)
     838       128117 :             fcinfo->args[1].value = constval;
     839              :         else
     840            0 :             fcinfo->args[0].value = constval;
     841              : 
     842      3027354 :         for (i = 0; i < sslot.nvalues; i++)
     843              :         {
     844              :             Datum       fresult;
     845              : 
     846      2899237 :             if (varonleft)
     847      2899237 :                 fcinfo->args[0].value = sslot.values[i];
     848              :             else
     849            0 :                 fcinfo->args[1].value = sslot.values[i];
     850      2899237 :             fcinfo->isnull = false;
     851      2899237 :             fresult = FunctionCallInvoke(fcinfo);
     852      2899237 :             if (!fcinfo->isnull && DatumGetBool(fresult))
     853      1185871 :                 mcv_selec += sslot.numbers[i];
     854      2899237 :             sumcommon += sslot.numbers[i];
     855              :         }
     856       128117 :         free_attstatsslot(&sslot);
     857              :     }
     858              : 
     859       230693 :     *sumcommonp = sumcommon;
     860       230693 :     return mcv_selec;
     861              : }
     862              : 
     863              : /*
     864              :  *  histogram_selectivity   - Examine the histogram for selectivity estimates
     865              :  *
     866              :  * Determine the fraction of the variable's histogram entries that satisfy
     867              :  * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.
     868              :  *
     869              :  * This code will work for any boolean-returning predicate operator, whether
     870              :  * or not it has anything to do with the histogram sort operator.  We are
     871              :  * essentially using the histogram just as a representative sample.  However,
     872              :  * small histograms are unlikely to be all that representative, so the caller
     873              :  * should be prepared to fall back on some other estimation approach when the
     874              :  * histogram is missing or very small.  It may also be prudent to combine this
     875              :  * approach with another one when the histogram is small.
     876              :  *
     877              :  * If the actual histogram size is not at least min_hist_size, we won't bother
     878              :  * to do the calculation at all.  Also, if the n_skip parameter is > 0, we
     879              :  * ignore the first and last n_skip histogram elements, on the grounds that
     880              :  * they are outliers and hence not very representative.  Typical values for
     881              :  * these parameters are 10 and 1.
     882              :  *
     883              :  * The function result is the selectivity, or -1 if there is no histogram
     884              :  * or it's smaller than min_hist_size.
     885              :  *
     886              :  * The output parameter *hist_size receives the actual histogram size,
     887              :  * or zero if no histogram.  Callers may use this number to decide how
     888              :  * much faith to put in the function result.
     889              :  *
     890              :  * Note that the result disregards both the most-common-values (if any) and
     891              :  * null entries.  The caller is expected to combine this result with
     892              :  * statistics for those portions of the column population.  It may also be
     893              :  * prudent to clamp the result range, ie, disbelieve exact 0 or 1 outputs.
     894              :  */
     895              : double
     896         4407 : histogram_selectivity(VariableStatData *vardata,
     897              :                       FmgrInfo *opproc, Oid collation,
     898              :                       Datum constval, bool varonleft,
     899              :                       int min_hist_size, int n_skip,
     900              :                       int *hist_size)
     901              : {
     902              :     double      result;
     903              :     AttStatsSlot sslot;
     904              : 
     905              :     /* check sanity of parameters */
     906              :     Assert(n_skip >= 0);
     907              :     Assert(min_hist_size > 2 * n_skip);
     908              : 
     909         7012 :     if (HeapTupleIsValid(vardata->statsTuple) &&
     910         5205 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
     911         2600 :         get_attstatsslot(&sslot, vardata->statsTuple,
     912              :                          STATISTIC_KIND_HISTOGRAM, InvalidOid,
     913              :                          ATTSTATSSLOT_VALUES))
     914              :     {
     915         2522 :         *hist_size = sslot.nvalues;
     916         2522 :         if (sslot.nvalues >= min_hist_size)
     917              :         {
     918         1376 :             LOCAL_FCINFO(fcinfo, 2);
     919         1376 :             int         nmatch = 0;
     920              :             int         i;
     921              : 
     922              :             /*
     923              :              * We invoke the opproc "by hand" so that we won't fail on NULL
     924              :              * results.  Such cases won't arise for normal comparison
     925              :              * functions, but generic_restriction_selectivity could perhaps be
     926              :              * used with operators that can return NULL.  A small side benefit
     927              :              * is to not need to re-initialize the fcinfo struct from scratch
     928              :              * each time.
     929              :              */
     930         1376 :             InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
     931              :                                      NULL, NULL);
     932         1376 :             fcinfo->args[0].isnull = false;
     933         1376 :             fcinfo->args[1].isnull = false;
     934              :             /* be careful to apply operator right way 'round */
     935         1376 :             if (varonleft)
     936         1376 :                 fcinfo->args[1].value = constval;
     937              :             else
     938            0 :                 fcinfo->args[0].value = constval;
     939              : 
     940       115625 :             for (i = n_skip; i < sslot.nvalues - n_skip; i++)
     941              :             {
     942              :                 Datum       fresult;
     943              : 
     944       114249 :                 if (varonleft)
     945       114249 :                     fcinfo->args[0].value = sslot.values[i];
     946              :                 else
     947            0 :                     fcinfo->args[1].value = sslot.values[i];
     948       114249 :                 fcinfo->isnull = false;
     949       114249 :                 fresult = FunctionCallInvoke(fcinfo);
     950       114249 :                 if (!fcinfo->isnull && DatumGetBool(fresult))
     951         4397 :                     nmatch++;
     952              :             }
     953         1376 :             result = ((double) nmatch) / ((double) (sslot.nvalues - 2 * n_skip));
     954              :         }
     955              :         else
     956         1146 :             result = -1;
     957         2522 :         free_attstatsslot(&sslot);
     958              :     }
     959              :     else
     960              :     {
     961         1885 :         *hist_size = 0;
     962         1885 :         result = -1;
     963              :     }
     964              : 
     965         4407 :     return result;
     966              : }
     967              : 
     968              : /*
     969              :  *  generic_restriction_selectivity     - Selectivity for almost anything
     970              :  *
     971              :  * This function estimates selectivity for operators that we don't have any
     972              :  * special knowledge about, but are on data types that we collect standard
     973              :  * MCV and/or histogram statistics for.  (Additional assumptions are that
     974              :  * the operator is strict and immutable, or at least stable.)
     975              :  *
     976              :  * If we have "VAR OP CONST" or "CONST OP VAR", selectivity is estimated by
     977              :  * applying the operator to each element of the column's MCV and/or histogram
     978              :  * stats, and merging the results using the assumption that the histogram is
     979              :  * a reasonable random sample of the column's non-MCV population.  Note that
     980              :  * if the operator's semantics are related to the histogram ordering, this
     981              :  * might not be such a great assumption; other functions such as
     982              :  * scalarineqsel() are probably a better match in such cases.
     983              :  *
     984              :  * Otherwise, fall back to the default selectivity provided by the caller.
     985              :  */
     986              : double
     987          845 : generic_restriction_selectivity(PlannerInfo *root, Oid oproid, Oid collation,
     988              :                                 List *args, int varRelid,
     989              :                                 double default_selectivity)
     990              : {
     991              :     double      selec;
     992              :     VariableStatData vardata;
     993              :     Node       *other;
     994              :     bool        varonleft;
     995              : 
     996              :     /*
     997              :      * If expression is not variable OP something or something OP variable,
     998              :      * then punt and return the default estimate.
     999              :      */
    1000          845 :     if (!get_restriction_variable(root, args, varRelid,
    1001              :                                   &vardata, &other, &varonleft))
    1002            0 :         return default_selectivity;
    1003              : 
    1004              :     /*
    1005              :      * If the something is a NULL constant, assume operator is strict and
    1006              :      * return zero, ie, operator will never return TRUE.
    1007              :      */
    1008          845 :     if (IsA(other, Const) &&
    1009          845 :         ((Const *) other)->constisnull)
    1010              :     {
    1011            0 :         ReleaseVariableStats(vardata);
    1012            0 :         return 0.0;
    1013              :     }
    1014              : 
    1015          845 :     if (IsA(other, Const))
    1016              :     {
    1017              :         /* Variable is being compared to a known non-null constant */
    1018          845 :         Datum       constval = ((Const *) other)->constvalue;
    1019              :         FmgrInfo    opproc;
    1020              :         double      mcvsum;
    1021              :         double      mcvsel;
    1022              :         double      nullfrac;
    1023              :         int         hist_size;
    1024              : 
    1025          845 :         fmgr_info(get_opcode(oproid), &opproc);
    1026              : 
    1027              :         /*
    1028              :          * Calculate the selectivity for the column's most common values.
    1029              :          */
    1030          845 :         mcvsel = mcv_selectivity(&vardata, &opproc, collation,
    1031              :                                  constval, varonleft,
    1032              :                                  &mcvsum);
    1033              : 
    1034              :         /*
    1035              :          * If the histogram is large enough, see what fraction of it matches
    1036              :          * the query, and assume that's representative of the non-MCV
    1037              :          * population.  Otherwise use the default selectivity for the non-MCV
    1038              :          * population.
    1039              :          */
    1040          845 :         selec = histogram_selectivity(&vardata, &opproc, collation,
    1041              :                                       constval, varonleft,
    1042              :                                       10, 1, &hist_size);
    1043          845 :         if (selec < 0)
    1044              :         {
    1045              :             /* Nope, fall back on default */
    1046          845 :             selec = default_selectivity;
    1047              :         }
    1048            0 :         else if (hist_size < 100)
    1049              :         {
    1050              :             /*
    1051              :              * For histogram sizes from 10 to 100, we combine the histogram
    1052              :              * and default selectivities, putting increasingly more trust in
    1053              :              * the histogram for larger sizes.
    1054              :              */
    1055            0 :             double      hist_weight = hist_size / 100.0;
    1056              : 
    1057            0 :             selec = selec * hist_weight +
    1058            0 :                 default_selectivity * (1.0 - hist_weight);
    1059              :         }
    1060              : 
    1061              :         /* In any case, don't believe extremely small or large estimates. */
    1062          845 :         if (selec < 0.0001)
    1063            0 :             selec = 0.0001;
    1064          845 :         else if (selec > 0.9999)
    1065            0 :             selec = 0.9999;
    1066              : 
    1067              :         /* Don't forget to account for nulls. */
    1068          845 :         if (HeapTupleIsValid(vardata.statsTuple))
    1069           70 :             nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stanullfrac;
    1070              :         else
    1071          775 :             nullfrac = 0.0;
    1072              : 
    1073              :         /*
    1074              :          * Now merge the results from the MCV and histogram calculations,
    1075              :          * realizing that the histogram covers only the non-null values that
    1076              :          * are not listed in MCV.
    1077              :          */
    1078          845 :         selec *= 1.0 - nullfrac - mcvsum;
    1079          845 :         selec += mcvsel;
    1080              :     }
    1081              :     else
    1082              :     {
    1083              :         /* Comparison value is not constant, so we can't do anything */
    1084            0 :         selec = default_selectivity;
    1085              :     }
    1086              : 
    1087          845 :     ReleaseVariableStats(vardata);
    1088              : 
    1089              :     /* result should be in range, but make sure... */
    1090          845 :     CLAMP_PROBABILITY(selec);
    1091              : 
    1092          845 :     return selec;
    1093              : }
    1094              : 
    1095              : /*
    1096              :  *  ineq_histogram_selectivity  - Examine the histogram for scalarineqsel
    1097              :  *
    1098              :  * Determine the fraction of the variable's histogram population that
    1099              :  * satisfies the inequality condition, ie, VAR < (or <=, >, >=) CONST.
    1100              :  * The isgt and iseq flags distinguish which of the four cases apply.
    1101              :  *
    1102              :  * While opproc could be looked up from the operator OID, common callers
    1103              :  * also need to call it separately, so we make the caller pass both.
    1104              :  *
    1105              :  * Returns -1 if there is no histogram (valid results will always be >= 0).
    1106              :  *
    1107              :  * Note that the result disregards both the most-common-values (if any) and
    1108              :  * null entries.  The caller is expected to combine this result with
    1109              :  * statistics for those portions of the column population.
    1110              :  *
    1111              :  * This is exported so that some other estimation functions can use it.
    1112              :  */
    1113              : double
    1114       229572 : ineq_histogram_selectivity(PlannerInfo *root,
    1115              :                            VariableStatData *vardata,
    1116              :                            Oid opoid, FmgrInfo *opproc, bool isgt, bool iseq,
    1117              :                            Oid collation,
    1118              :                            Datum constval, Oid consttype)
    1119              : {
    1120              :     double      hist_selec;
    1121              :     AttStatsSlot sslot;
    1122              : 
    1123       229572 :     hist_selec = -1.0;
    1124              : 
    1125              :     /*
    1126              :      * Someday, ANALYZE might store more than one histogram per rel/att,
    1127              :      * corresponding to more than one possible sort ordering defined for the
    1128              :      * column type.  Right now, we know there is only one, so just grab it and
    1129              :      * see if it matches the query.
    1130              :      *
    1131              :      * Note that we can't use opoid as search argument; the staop appearing in
    1132              :      * pg_statistic will be for the relevant '<' operator, but what we have
    1133              :      * might be some other inequality operator such as '>='.  (Even if opoid
    1134              :      * is a '<' operator, it could be cross-type.)  Hence we must use
    1135              :      * comparison_ops_are_compatible() to see if the operators match.
    1136              :      */
    1137       458634 :     if (HeapTupleIsValid(vardata->statsTuple) &&
    1138       457854 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
    1139       228792 :         get_attstatsslot(&sslot, vardata->statsTuple,
    1140              :                          STATISTIC_KIND_HISTOGRAM, InvalidOid,
    1141              :                          ATTSTATSSLOT_VALUES))
    1142              :     {
    1143       140944 :         if (sslot.nvalues > 1 &&
    1144       281826 :             sslot.stacoll == collation &&
    1145       140882 :             comparison_ops_are_compatible(sslot.staop, opoid))
    1146       140792 :         {
    1147              :             /*
    1148              :              * Use binary search to find the desired location, namely the
    1149              :              * right end of the histogram bin containing the comparison value,
    1150              :              * which is the leftmost entry for which the comparison operator
    1151              :              * succeeds (if isgt) or fails (if !isgt).
    1152              :              *
    1153              :              * In this loop, we pay no attention to whether the operator iseq
    1154              :              * or not; that detail will be mopped up below.  (We cannot tell,
    1155              :              * anyway, whether the operator thinks the values are equal.)
    1156              :              *
    1157              :              * If the binary search accesses the first or last histogram
    1158              :              * entry, we try to replace that endpoint with the true column min
    1159              :              * or max as found by get_actual_variable_range().  This
    1160              :              * ameliorates misestimates when the min or max is moving as a
    1161              :              * result of changes since the last ANALYZE.  Note that this could
    1162              :              * result in effectively including MCVs into the histogram that
    1163              :              * weren't there before, but we don't try to correct for that.
    1164              :              */
    1165              :             double      histfrac;
    1166       140792 :             int         lobound = 0;    /* first possible slot to search */
    1167       140792 :             int         hibound = sslot.nvalues;    /* last+1 slot to search */
    1168       140792 :             bool        have_end = false;
    1169              : 
    1170              :             /*
    1171              :              * If there are only two histogram entries, we'll want up-to-date
    1172              :              * values for both.  (If there are more than two, we need at most
    1173              :              * one of them to be updated, so we deal with that within the
    1174              :              * loop.)
    1175              :              */
    1176       140792 :             if (sslot.nvalues == 2)
    1177         2339 :                 have_end = get_actual_variable_range(root,
    1178              :                                                      vardata,
    1179              :                                                      sslot.staop,
    1180              :                                                      collation,
    1181              :                                                      &sslot.values[0],
    1182         2339 :                                                      &sslot.values[1]);
    1183              : 
    1184       921910 :             while (lobound < hibound)
    1185              :             {
    1186       781118 :                 int         probe = (lobound + hibound) / 2;
    1187              :                 bool        ltcmp;
    1188              : 
    1189              :                 /*
    1190              :                  * If we find ourselves about to compare to the first or last
    1191              :                  * histogram entry, first try to replace it with the actual
    1192              :                  * current min or max (unless we already did so above).
    1193              :                  */
    1194       781118 :                 if (probe == 0 && sslot.nvalues > 2)
    1195        68768 :                     have_end = get_actual_variable_range(root,
    1196              :                                                          vardata,
    1197              :                                                          sslot.staop,
    1198              :                                                          collation,
    1199              :                                                          &sslot.values[0],
    1200              :                                                          NULL);
    1201       712350 :                 else if (probe == sslot.nvalues - 1 && sslot.nvalues > 2)
    1202        47987 :                     have_end = get_actual_variable_range(root,
    1203              :                                                          vardata,
    1204              :                                                          sslot.staop,
    1205              :                                                          collation,
    1206              :                                                          NULL,
    1207        47987 :                                                          &sslot.values[probe]);
    1208              : 
    1209       781118 :                 ltcmp = DatumGetBool(FunctionCall2Coll(opproc,
    1210              :                                                        collation,
    1211       781118 :                                                        sslot.values[probe],
    1212              :                                                        constval));
    1213       781118 :                 if (isgt)
    1214        50561 :                     ltcmp = !ltcmp;
    1215       781118 :                 if (ltcmp)
    1216       292115 :                     lobound = probe + 1;
    1217              :                 else
    1218       489003 :                     hibound = probe;
    1219              :             }
    1220              : 
    1221       140792 :             if (lobound <= 0)
    1222              :             {
    1223              :                 /*
    1224              :                  * Constant is below lower histogram boundary.  More
    1225              :                  * precisely, we have found that no entry in the histogram
    1226              :                  * satisfies the inequality clause (if !isgt) or they all do
    1227              :                  * (if isgt).  We estimate that that's true of the entire
    1228              :                  * table, so set histfrac to 0.0 (which we'll flip to 1.0
    1229              :                  * below, if isgt).
    1230              :                  */
    1231        60264 :                 histfrac = 0.0;
    1232              :             }
    1233        80528 :             else if (lobound >= sslot.nvalues)
    1234              :             {
    1235              :                 /*
    1236              :                  * Inverse case: constant is above upper histogram boundary.
    1237              :                  */
    1238        24228 :                 histfrac = 1.0;
    1239              :             }
    1240              :             else
    1241              :             {
    1242              :                 /* We have values[i-1] <= constant <= values[i]. */
    1243        56300 :                 int         i = lobound;
    1244        56300 :                 double      eq_selec = 0;
    1245              :                 double      val,
    1246              :                             high,
    1247              :                             low;
    1248              :                 double      binfrac;
    1249              : 
    1250              :                 /*
    1251              :                  * In the cases where we'll need it below, obtain an estimate
    1252              :                  * of the selectivity of "x = constval".  We use a calculation
    1253              :                  * similar to what var_eq_const() does for a non-MCV constant,
    1254              :                  * ie, estimate that all distinct non-MCV values occur equally
    1255              :                  * often.  But multiplication by "1.0 - sumcommon - nullfrac"
    1256              :                  * will be done by our caller, so we shouldn't do that here.
    1257              :                  * Therefore we can't try to clamp the estimate by reference
    1258              :                  * to the least common MCV; the result would be too small.
    1259              :                  *
    1260              :                  * Note: since this is effectively assuming that constval
    1261              :                  * isn't an MCV, it's logically dubious if constval in fact is
    1262              :                  * one.  But we have to apply *some* correction for equality,
    1263              :                  * and anyway we cannot tell if constval is an MCV, since we
    1264              :                  * don't have a suitable equality operator at hand.
    1265              :                  */
    1266        56300 :                 if (i == 1 || isgt == iseq)
    1267              :                 {
    1268              :                     double      otherdistinct;
    1269              :                     bool        isdefault;
    1270              :                     AttStatsSlot mcvslot;
    1271              : 
    1272              :                     /* Get estimated number of distinct values */
    1273        23825 :                     otherdistinct = get_variable_numdistinct(vardata,
    1274              :                                                              &isdefault);
    1275              : 
    1276              :                     /* Subtract off the number of known MCVs */
    1277        23825 :                     if (get_attstatsslot(&mcvslot, vardata->statsTuple,
    1278              :                                          STATISTIC_KIND_MCV, InvalidOid,
    1279              :                                          ATTSTATSSLOT_NUMBERS))
    1280              :                     {
    1281         2851 :                         otherdistinct -= mcvslot.nnumbers;
    1282         2851 :                         free_attstatsslot(&mcvslot);
    1283              :                     }
    1284              : 
    1285              :                     /* If result doesn't seem sane, leave eq_selec at 0 */
    1286        23825 :                     if (otherdistinct > 1)
    1287        23804 :                         eq_selec = 1.0 / otherdistinct;
    1288              :                 }
    1289              : 
    1290              :                 /*
    1291              :                  * Convert the constant and the two nearest bin boundary
    1292              :                  * values to a uniform comparison scale, and do a linear
    1293              :                  * interpolation within this bin.
    1294              :                  */
    1295        56300 :                 if (convert_to_scalar(constval, consttype, collation,
    1296              :                                       &val,
    1297        56300 :                                       sslot.values[i - 1], sslot.values[i],
    1298              :                                       vardata->vartype,
    1299              :                                       &low, &high))
    1300              :                 {
    1301        56300 :                     if (high <= low)
    1302              :                     {
    1303              :                         /* cope if bin boundaries appear identical */
    1304            0 :                         binfrac = 0.5;
    1305              :                     }
    1306        56300 :                     else if (val <= low)
    1307        11603 :                         binfrac = 0.0;
    1308        44697 :                     else if (val >= high)
    1309         2130 :                         binfrac = 1.0;
    1310              :                     else
    1311              :                     {
    1312        42567 :                         binfrac = (val - low) / (high - low);
    1313              : 
    1314              :                         /*
    1315              :                          * Watch out for the possibility that we got a NaN or
    1316              :                          * Infinity from the division.  This can happen
    1317              :                          * despite the previous checks, if for example "low"
    1318              :                          * is -Infinity.
    1319              :                          */
    1320        42567 :                         if (isnan(binfrac) ||
    1321        42567 :                             binfrac < 0.0 || binfrac > 1.0)
    1322            0 :                             binfrac = 0.5;
    1323              :                     }
    1324              :                 }
    1325              :                 else
    1326              :                 {
    1327              :                     /*
    1328              :                      * Ideally we'd produce an error here, on the grounds that
    1329              :                      * the given operator shouldn't have scalarXXsel
    1330              :                      * registered as its selectivity func unless we can deal
    1331              :                      * with its operand types.  But currently, all manner of
    1332              :                      * stuff is invoking scalarXXsel, so give a default
    1333              :                      * estimate until that can be fixed.
    1334              :                      */
    1335            0 :                     binfrac = 0.5;
    1336              :                 }
    1337              : 
    1338              :                 /*
    1339              :                  * Now, compute the overall selectivity across the values
    1340              :                  * represented by the histogram.  We have i-1 full bins and
    1341              :                  * binfrac partial bin below the constant.
    1342              :                  */
    1343        56300 :                 histfrac = (double) (i - 1) + binfrac;
    1344        56300 :                 histfrac /= (double) (sslot.nvalues - 1);
    1345              : 
    1346              :                 /*
    1347              :                  * At this point, histfrac is an estimate of the fraction of
    1348              :                  * the population represented by the histogram that satisfies
    1349              :                  * "x <= constval".  Somewhat remarkably, this statement is
    1350              :                  * true regardless of which operator we were doing the probes
    1351              :                  * with, so long as convert_to_scalar() delivers reasonable
    1352              :                  * results.  If the probe constant is equal to some histogram
    1353              :                  * entry, we would have considered the bin to the left of that
    1354              :                  * entry if probing with "<" or ">=", or the bin to the right
    1355              :                  * if probing with "<=" or ">"; but binfrac would have come
    1356              :                  * out as 1.0 in the first case and 0.0 in the second, leading
    1357              :                  * to the same histfrac in either case.  For probe constants
    1358              :                  * between histogram entries, we find the same bin and get the
    1359              :                  * same estimate with any operator.
    1360              :                  *
    1361              :                  * The fact that the estimate corresponds to "x <= constval"
    1362              :                  * and not "x < constval" is because of the way that ANALYZE
    1363              :                  * constructs the histogram: each entry is, effectively, the
    1364              :                  * rightmost value in its sample bucket.  So selectivity
    1365              :                  * values that are exact multiples of 1/(histogram_size-1)
    1366              :                  * should be understood as estimates including a histogram
    1367              :                  * entry plus everything to its left.
    1368              :                  *
    1369              :                  * However, that breaks down for the first histogram entry,
    1370              :                  * which necessarily is the leftmost value in its sample
    1371              :                  * bucket.  That means the first histogram bin is slightly
    1372              :                  * narrower than the rest, by an amount equal to eq_selec.
    1373              :                  * Another way to say that is that we want "x <= leftmost" to
    1374              :                  * be estimated as eq_selec not zero.  So, if we're dealing
    1375              :                  * with the first bin (i==1), rescale to make that true while
    1376              :                  * adjusting the rest of that bin linearly.
    1377              :                  */
    1378        56300 :                 if (i == 1)
    1379        10092 :                     histfrac += eq_selec * (1.0 - binfrac);
    1380              : 
    1381              :                 /*
    1382              :                  * "x <= constval" is good if we want an estimate for "<=" or
    1383              :                  * ">", but if we are estimating for "<" or ">=", we now need
    1384              :                  * to decrease the estimate by eq_selec.
    1385              :                  */
    1386        56300 :                 if (isgt == iseq)
    1387        18726 :                     histfrac -= eq_selec;
    1388              :             }
    1389              : 
    1390              :             /*
    1391              :              * Now the estimate is finished for "<" and "<=" cases.  If we are
    1392              :              * estimating for ">" or ">=", flip it.
    1393              :              */
    1394       140792 :             hist_selec = isgt ? (1.0 - histfrac) : histfrac;
    1395              : 
    1396              :             /*
    1397              :              * The histogram boundaries are only approximate to begin with,
    1398              :              * and may well be out of date anyway.  Therefore, don't believe
    1399              :              * extremely small or large selectivity estimates --- unless we
    1400              :              * got actual current endpoint values from the table, in which
    1401              :              * case just do the usual sanity clamp.  Somewhat arbitrarily, we
    1402              :              * set the cutoff for other cases at a hundredth of the histogram
    1403              :              * resolution.
    1404              :              */
    1405       140792 :             if (have_end)
    1406        79442 :                 CLAMP_PROBABILITY(hist_selec);
    1407              :             else
    1408              :             {
    1409        61350 :                 double      cutoff = 0.01 / (double) (sslot.nvalues - 1);
    1410              : 
    1411        61350 :                 if (hist_selec < cutoff)
    1412        20621 :                     hist_selec = cutoff;
    1413        40729 :                 else if (hist_selec > 1.0 - cutoff)
    1414        15144 :                     hist_selec = 1.0 - cutoff;
    1415              :             }
    1416              :         }
    1417          152 :         else if (sslot.nvalues > 1)
    1418              :         {
    1419              :             /*
    1420              :              * If we get here, we have a histogram but it's not sorted the way
    1421              :              * we want.  Do a brute-force search to see how many of the
    1422              :              * entries satisfy the comparison condition, and take that
    1423              :              * fraction as our estimate.  (This is identical to the inner loop
    1424              :              * of histogram_selectivity; maybe share code?)
    1425              :              */
    1426          152 :             LOCAL_FCINFO(fcinfo, 2);
    1427          152 :             int         nmatch = 0;
    1428              : 
    1429          152 :             InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
    1430              :                                      NULL, NULL);
    1431          152 :             fcinfo->args[0].isnull = false;
    1432          152 :             fcinfo->args[1].isnull = false;
    1433          152 :             fcinfo->args[1].value = constval;
    1434       801750 :             for (int i = 0; i < sslot.nvalues; i++)
    1435              :             {
    1436              :                 Datum       fresult;
    1437              : 
    1438       801598 :                 fcinfo->args[0].value = sslot.values[i];
    1439       801598 :                 fcinfo->isnull = false;
    1440       801598 :                 fresult = FunctionCallInvoke(fcinfo);
    1441       801598 :                 if (!fcinfo->isnull && DatumGetBool(fresult))
    1442         1614 :                     nmatch++;
    1443              :             }
    1444          152 :             hist_selec = ((double) nmatch) / ((double) sslot.nvalues);
    1445              : 
    1446              :             /*
    1447              :              * As above, clamp to a hundredth of the histogram resolution.
    1448              :              * This case is surely even less trustworthy than the normal one,
    1449              :              * so we shouldn't believe exact 0 or 1 selectivity.  (Maybe the
    1450              :              * clamp should be more restrictive in this case?)
    1451              :              */
    1452              :             {
    1453          152 :                 double      cutoff = 0.01 / (double) (sslot.nvalues - 1);
    1454              : 
    1455          152 :                 if (hist_selec < cutoff)
    1456           10 :                     hist_selec = cutoff;
    1457          142 :                 else if (hist_selec > 1.0 - cutoff)
    1458           10 :                     hist_selec = 1.0 - cutoff;
    1459              :             }
    1460              :         }
    1461              : 
    1462       140944 :         free_attstatsslot(&sslot);
    1463              :     }
    1464              : 
    1465       229572 :     return hist_selec;
    1466              : }
    1467              : 
    1468              : /*
    1469              :  * Common wrapper function for the selectivity estimators that simply
    1470              :  * invoke scalarineqsel().
    1471              :  */
    1472              : static Datum
    1473        39031 : scalarineqsel_wrapper(PG_FUNCTION_ARGS, bool isgt, bool iseq)
    1474              : {
    1475        39031 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
    1476        39031 :     Oid         operator = PG_GETARG_OID(1);
    1477        39031 :     List       *args = (List *) PG_GETARG_POINTER(2);
    1478        39031 :     int         varRelid = PG_GETARG_INT32(3);
    1479        39031 :     Oid         collation = PG_GET_COLLATION();
    1480              :     VariableStatData vardata;
    1481              :     Node       *other;
    1482              :     bool        varonleft;
    1483              :     Datum       constval;
    1484              :     Oid         consttype;
    1485              :     double      selec;
    1486              : 
    1487              :     /*
    1488              :      * If expression is not variable op something or something op variable,
    1489              :      * then punt and return a default estimate.
    1490              :      */
    1491        39031 :     if (!get_restriction_variable(root, args, varRelid,
    1492              :                                   &vardata, &other, &varonleft))
    1493          462 :         PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    1494              : 
    1495              :     /*
    1496              :      * Can't do anything useful if the something is not a constant, either.
    1497              :      */
    1498        38569 :     if (!IsA(other, Const))
    1499              :     {
    1500         2365 :         ReleaseVariableStats(vardata);
    1501         2365 :         PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    1502              :     }
    1503              : 
    1504              :     /*
    1505              :      * If the constant is NULL, assume operator is strict and return zero, ie,
    1506              :      * operator will never return TRUE.
    1507              :      */
    1508        36204 :     if (((Const *) other)->constisnull)
    1509              :     {
    1510           55 :         ReleaseVariableStats(vardata);
    1511           55 :         PG_RETURN_FLOAT8(0.0);
    1512              :     }
    1513        36149 :     constval = ((Const *) other)->constvalue;
    1514        36149 :     consttype = ((Const *) other)->consttype;
    1515              : 
    1516              :     /*
    1517              :      * Force the var to be on the left to simplify logic in scalarineqsel.
    1518              :      */
    1519        36149 :     if (!varonleft)
    1520              :     {
    1521          318 :         operator = get_commutator(operator);
    1522          318 :         if (!operator)
    1523              :         {
    1524              :             /* Use default selectivity (should we raise an error instead?) */
    1525            0 :             ReleaseVariableStats(vardata);
    1526            0 :             PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    1527              :         }
    1528          318 :         isgt = !isgt;
    1529              :     }
    1530              : 
    1531              :     /* The rest of the work is done by scalarineqsel(). */
    1532        36149 :     selec = scalarineqsel(root, operator, isgt, iseq, collation,
    1533              :                           &vardata, constval, consttype);
    1534              : 
    1535        36149 :     ReleaseVariableStats(vardata);
    1536              : 
    1537        36149 :     PG_RETURN_FLOAT8((float8) selec);
    1538              : }
    1539              : 
    1540              : /*
    1541              :  *      scalarltsel     - Selectivity of "<" for scalars.
    1542              :  */
    1543              : Datum
    1544        12221 : scalarltsel(PG_FUNCTION_ARGS)
    1545              : {
    1546        12221 :     return scalarineqsel_wrapper(fcinfo, false, false);
    1547              : }
    1548              : 
    1549              : /*
    1550              :  *      scalarlesel     - Selectivity of "<=" for scalars.
    1551              :  */
    1552              : Datum
    1553         3812 : scalarlesel(PG_FUNCTION_ARGS)
    1554              : {
    1555         3812 :     return scalarineqsel_wrapper(fcinfo, false, true);
    1556              : }
    1557              : 
    1558              : /*
    1559              :  *      scalargtsel     - Selectivity of ">" for scalars.
    1560              :  */
    1561              : Datum
    1562        12418 : scalargtsel(PG_FUNCTION_ARGS)
    1563              : {
    1564        12418 :     return scalarineqsel_wrapper(fcinfo, true, false);
    1565              : }
    1566              : 
    1567              : /*
    1568              :  *      scalargesel     - Selectivity of ">=" for scalars.
    1569              :  */
    1570              : Datum
    1571        10580 : scalargesel(PG_FUNCTION_ARGS)
    1572              : {
    1573        10580 :     return scalarineqsel_wrapper(fcinfo, true, true);
    1574              : }
    1575              : 
    1576              : /*
    1577              :  *      boolvarsel      - Selectivity of Boolean variable.
    1578              :  *
    1579              :  * This can actually be called on any boolean-valued expression.  If it
    1580              :  * involves only Vars of the specified relation, and if there are statistics
    1581              :  * about the Var or expression (the latter is possible if it's indexed) then
    1582              :  * we'll produce a real estimate; otherwise it's just a default.
    1583              :  */
    1584              : Selectivity
    1585        47673 : boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
    1586              : {
    1587              :     VariableStatData vardata;
    1588              :     double      selec;
    1589              : 
    1590        47673 :     examine_variable(root, arg, varRelid, &vardata);
    1591        47673 :     if (HeapTupleIsValid(vardata.statsTuple))
    1592              :     {
    1593              :         /*
    1594              :          * A boolean variable V is equivalent to the clause V = 't', so we
    1595              :          * compute the selectivity as if that is what we have.
    1596              :          */
    1597        22901 :         selec = var_eq_const(&vardata, BooleanEqualOperator, InvalidOid,
    1598              :                              BoolGetDatum(true), false, true, false);
    1599              :     }
    1600        24772 :     else if (is_funcclause(arg))
    1601              :     {
    1602              :         /*
    1603              :          * If we have no stats and it's a function call, estimate 0.3333333.
    1604              :          * This seems a pretty unprincipled choice, but Postgres has been
    1605              :          * using that estimate for function calls since 1992.  The hoariness
    1606              :          * of this behavior suggests that we should not be in too much hurry
    1607              :          * to use another value.
    1608              :          */
    1609        11456 :         selec = 0.3333333;
    1610              :     }
    1611              :     else
    1612              :     {
    1613              :         /* Otherwise, the default estimate is 0.5 */
    1614        13316 :         selec = 0.5;
    1615              :     }
    1616        47673 :     ReleaseVariableStats(vardata);
    1617        47673 :     return selec;
    1618              : }
    1619              : 
    1620              : /*
    1621              :  *      booltestsel     - Selectivity of BooleanTest Node.
    1622              :  */
    1623              : Selectivity
    1624          791 : booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
    1625              :             int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
    1626              : {
    1627              :     VariableStatData vardata;
    1628              :     double      selec;
    1629              : 
    1630          791 :     examine_variable(root, arg, varRelid, &vardata);
    1631              : 
    1632          791 :     if (HeapTupleIsValid(vardata.statsTuple))
    1633              :     {
    1634              :         Form_pg_statistic stats;
    1635              :         double      freq_null;
    1636              :         AttStatsSlot sslot;
    1637              : 
    1638           20 :         stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
    1639           20 :         freq_null = stats->stanullfrac;
    1640              : 
    1641           20 :         if (get_attstatsslot(&sslot, vardata.statsTuple,
    1642              :                              STATISTIC_KIND_MCV, InvalidOid,
    1643              :                              ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS)
    1644           10 :             && sslot.nnumbers > 0)
    1645           10 :         {
    1646              :             double      freq_true;
    1647              :             double      freq_false;
    1648              : 
    1649              :             /*
    1650              :              * Get first MCV frequency and derive frequency for true.
    1651              :              */
    1652           10 :             if (DatumGetBool(sslot.values[0]))
    1653            0 :                 freq_true = sslot.numbers[0];
    1654              :             else
    1655           10 :                 freq_true = 1.0 - sslot.numbers[0] - freq_null;
    1656              : 
    1657              :             /*
    1658              :              * Next derive frequency for false. Then use these as appropriate
    1659              :              * to derive frequency for each case.
    1660              :              */
    1661           10 :             freq_false = 1.0 - freq_true - freq_null;
    1662              : 
    1663           10 :             switch (booltesttype)
    1664              :             {
    1665            0 :                 case IS_UNKNOWN:
    1666              :                     /* select only NULL values */
    1667            0 :                     selec = freq_null;
    1668            0 :                     break;
    1669            0 :                 case IS_NOT_UNKNOWN:
    1670              :                     /* select non-NULL values */
    1671            0 :                     selec = 1.0 - freq_null;
    1672            0 :                     break;
    1673           10 :                 case IS_TRUE:
    1674              :                     /* select only TRUE values */
    1675           10 :                     selec = freq_true;
    1676           10 :                     break;
    1677            0 :                 case IS_NOT_TRUE:
    1678              :                     /* select non-TRUE values */
    1679            0 :                     selec = 1.0 - freq_true;
    1680            0 :                     break;
    1681            0 :                 case IS_FALSE:
    1682              :                     /* select only FALSE values */
    1683            0 :                     selec = freq_false;
    1684            0 :                     break;
    1685            0 :                 case IS_NOT_FALSE:
    1686              :                     /* select non-FALSE values */
    1687            0 :                     selec = 1.0 - freq_false;
    1688            0 :                     break;
    1689            0 :                 default:
    1690            0 :                     elog(ERROR, "unrecognized booltesttype: %d",
    1691              :                          (int) booltesttype);
    1692              :                     selec = 0.0;    /* Keep compiler quiet */
    1693              :                     break;
    1694              :             }
    1695              : 
    1696           10 :             free_attstatsslot(&sslot);
    1697              :         }
    1698              :         else
    1699              :         {
    1700              :             /*
    1701              :              * No most-common-value info available. Still have null fraction
    1702              :              * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
    1703              :              * for null fraction and assume a 50-50 split of TRUE and FALSE.
    1704              :              */
    1705           10 :             switch (booltesttype)
    1706              :             {
    1707           10 :                 case IS_UNKNOWN:
    1708              :                     /* select only NULL values */
    1709           10 :                     selec = freq_null;
    1710           10 :                     break;
    1711            0 :                 case IS_NOT_UNKNOWN:
    1712              :                     /* select non-NULL values */
    1713            0 :                     selec = 1.0 - freq_null;
    1714            0 :                     break;
    1715            0 :                 case IS_TRUE:
    1716              :                 case IS_FALSE:
    1717              :                     /* Assume we select half of the non-NULL values */
    1718            0 :                     selec = (1.0 - freq_null) / 2.0;
    1719            0 :                     break;
    1720            0 :                 case IS_NOT_TRUE:
    1721              :                 case IS_NOT_FALSE:
    1722              :                     /* Assume we select NULLs plus half of the non-NULLs */
    1723              :                     /* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
    1724            0 :                     selec = (freq_null + 1.0) / 2.0;
    1725            0 :                     break;
    1726            0 :                 default:
    1727            0 :                     elog(ERROR, "unrecognized booltesttype: %d",
    1728              :                          (int) booltesttype);
    1729              :                     selec = 0.0;    /* Keep compiler quiet */
    1730              :                     break;
    1731              :             }
    1732              :         }
    1733              :     }
    1734              :     else
    1735              :     {
    1736              :         /*
    1737              :          * If we can't get variable statistics for the argument, perhaps
    1738              :          * clause_selectivity can do something with it.  We ignore the
    1739              :          * possibility of a NULL value when using clause_selectivity, and just
    1740              :          * assume the value is either TRUE or FALSE.
    1741              :          */
    1742          771 :         switch (booltesttype)
    1743              :         {
    1744           40 :             case IS_UNKNOWN:
    1745           40 :                 selec = DEFAULT_UNK_SEL;
    1746           40 :                 break;
    1747           90 :             case IS_NOT_UNKNOWN:
    1748           90 :                 selec = DEFAULT_NOT_UNK_SEL;
    1749           90 :                 break;
    1750          220 :             case IS_TRUE:
    1751              :             case IS_NOT_FALSE:
    1752          220 :                 selec = (double) clause_selectivity(root, arg,
    1753              :                                                     varRelid,
    1754              :                                                     jointype, sjinfo);
    1755          220 :                 break;
    1756          421 :             case IS_FALSE:
    1757              :             case IS_NOT_TRUE:
    1758          421 :                 selec = 1.0 - (double) clause_selectivity(root, arg,
    1759              :                                                           varRelid,
    1760              :                                                           jointype, sjinfo);
    1761          421 :                 break;
    1762            0 :             default:
    1763            0 :                 elog(ERROR, "unrecognized booltesttype: %d",
    1764              :                      (int) booltesttype);
    1765              :                 selec = 0.0;    /* Keep compiler quiet */
    1766              :                 break;
    1767              :         }
    1768              :     }
    1769              : 
    1770          791 :     ReleaseVariableStats(vardata);
    1771              : 
    1772              :     /* result should be in range, but make sure... */
    1773          791 :     CLAMP_PROBABILITY(selec);
    1774              : 
    1775          791 :     return (Selectivity) selec;
    1776              : }
    1777              : 
    1778              : /*
    1779              :  *      nulltestsel     - Selectivity of NullTest Node.
    1780              :  */
    1781              : Selectivity
    1782        14260 : nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg,
    1783              :             int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
    1784              : {
    1785              :     VariableStatData vardata;
    1786              :     double      selec;
    1787              : 
    1788        14260 :     examine_variable(root, arg, varRelid, &vardata);
    1789              : 
    1790        14260 :     if (HeapTupleIsValid(vardata.statsTuple))
    1791              :     {
    1792              :         Form_pg_statistic stats;
    1793              :         double      freq_null;
    1794              : 
    1795         7764 :         stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
    1796         7764 :         freq_null = stats->stanullfrac;
    1797              : 
    1798         7764 :         switch (nulltesttype)
    1799              :         {
    1800         5976 :             case IS_NULL:
    1801              : 
    1802              :                 /*
    1803              :                  * Use freq_null directly.
    1804              :                  */
    1805         5976 :                 selec = freq_null;
    1806         5976 :                 break;
    1807         1788 :             case IS_NOT_NULL:
    1808              : 
    1809              :                 /*
    1810              :                  * Select not unknown (not null) values. Calculate from
    1811              :                  * freq_null.
    1812              :                  */
    1813         1788 :                 selec = 1.0 - freq_null;
    1814         1788 :                 break;
    1815            0 :             default:
    1816            0 :                 elog(ERROR, "unrecognized nulltesttype: %d",
    1817              :                      (int) nulltesttype);
    1818              :                 return (Selectivity) 0; /* keep compiler quiet */
    1819              :         }
    1820              :     }
    1821         6496 :     else if (vardata.var && IsA(vardata.var, Var) &&
    1822         5962 :              ((Var *) vardata.var)->varattno < 0)
    1823              :     {
    1824              :         /*
    1825              :          * There are no stats for system columns, but we know they are never
    1826              :          * NULL.
    1827              :          */
    1828           89 :         selec = (nulltesttype == IS_NULL) ? 0.0 : 1.0;
    1829              :     }
    1830              :     else
    1831              :     {
    1832              :         /*
    1833              :          * No ANALYZE stats available, so make a guess
    1834              :          */
    1835         6407 :         switch (nulltesttype)
    1836              :         {
    1837         1648 :             case IS_NULL:
    1838         1648 :                 selec = DEFAULT_UNK_SEL;
    1839         1648 :                 break;
    1840         4759 :             case IS_NOT_NULL:
    1841         4759 :                 selec = DEFAULT_NOT_UNK_SEL;
    1842         4759 :                 break;
    1843            0 :             default:
    1844            0 :                 elog(ERROR, "unrecognized nulltesttype: %d",
    1845              :                      (int) nulltesttype);
    1846              :                 return (Selectivity) 0; /* keep compiler quiet */
    1847              :         }
    1848              :     }
    1849              : 
    1850        14260 :     ReleaseVariableStats(vardata);
    1851              : 
    1852              :     /* result should be in range, but make sure... */
    1853        14260 :     CLAMP_PROBABILITY(selec);
    1854              : 
    1855        14260 :     return (Selectivity) selec;
    1856              : }
    1857              : 
    1858              : /*
    1859              :  * strip_array_coercion - strip binary-compatible relabeling from an array expr
    1860              :  *
    1861              :  * For array values, the parser normally generates ArrayCoerceExpr conversions,
    1862              :  * but it seems possible that RelabelType might show up.  Also, the planner
    1863              :  * is not currently tense about collapsing stacked ArrayCoerceExpr nodes,
    1864              :  * so we need to be ready to deal with more than one level.
    1865              :  */
    1866              : static Node *
    1867       105660 : strip_array_coercion(Node *node)
    1868              : {
    1869              :     for (;;)
    1870              :     {
    1871       105748 :         if (node && IsA(node, ArrayCoerceExpr))
    1872           88 :         {
    1873         1974 :             ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    1874              : 
    1875              :             /*
    1876              :              * If the per-element expression is just a RelabelType on top of
    1877              :              * CaseTestExpr, then we know it's a binary-compatible relabeling.
    1878              :              */
    1879         1974 :             if (IsA(acoerce->elemexpr, RelabelType) &&
    1880           88 :                 IsA(((RelabelType *) acoerce->elemexpr)->arg, CaseTestExpr))
    1881           88 :                 node = (Node *) acoerce->arg;
    1882              :             else
    1883              :                 break;
    1884              :         }
    1885       103774 :         else if (node && IsA(node, RelabelType))
    1886              :         {
    1887              :             /* We don't really expect this case, but may as well cope */
    1888            0 :             node = (Node *) ((RelabelType *) node)->arg;
    1889              :         }
    1890              :         else
    1891              :             break;
    1892              :     }
    1893       105660 :     return node;
    1894              : }
    1895              : 
    1896              : /*
    1897              :  *      scalararraysel      - Selectivity of ScalarArrayOpExpr Node.
    1898              :  */
    1899              : Selectivity
    1900        18276 : scalararraysel(PlannerInfo *root,
    1901              :                ScalarArrayOpExpr *clause,
    1902              :                bool is_join_clause,
    1903              :                int varRelid,
    1904              :                JoinType jointype,
    1905              :                SpecialJoinInfo *sjinfo)
    1906              : {
    1907        18276 :     Oid         operator = clause->opno;
    1908        18276 :     bool        useOr = clause->useOr;
    1909        18276 :     bool        isEquality = false;
    1910        18276 :     bool        isInequality = false;
    1911              :     Node       *leftop;
    1912              :     Node       *rightop;
    1913              :     Oid         nominal_element_type;
    1914              :     Oid         nominal_element_collation;
    1915              :     TypeCacheEntry *typentry;
    1916              :     RegProcedure oprsel;
    1917              :     FmgrInfo    oprselproc;
    1918              :     Selectivity s1;
    1919              :     Selectivity s1disjoint;
    1920              : 
    1921              :     /* First, deconstruct the expression */
    1922              :     Assert(list_length(clause->args) == 2);
    1923        18276 :     leftop = (Node *) linitial(clause->args);
    1924        18276 :     rightop = (Node *) lsecond(clause->args);
    1925              : 
    1926              :     /* aggressively reduce both sides to constants */
    1927        18276 :     leftop = estimate_expression_value(root, leftop);
    1928        18276 :     rightop = estimate_expression_value(root, rightop);
    1929              : 
    1930              :     /* get nominal (after relabeling) element type of rightop */
    1931        18276 :     nominal_element_type = get_base_element_type(exprType(rightop));
    1932        18276 :     if (!OidIsValid(nominal_element_type))
    1933            0 :         return (Selectivity) 0.5;   /* probably shouldn't happen */
    1934              :     /* get nominal collation, too, for generating constants */
    1935        18276 :     nominal_element_collation = exprCollation(rightop);
    1936              : 
    1937              :     /* look through any binary-compatible relabeling of rightop */
    1938        18276 :     rightop = strip_array_coercion(rightop);
    1939              : 
    1940              :     /*
    1941              :      * Detect whether the operator is the default equality or inequality
    1942              :      * operator of the array element type.
    1943              :      */
    1944        18276 :     typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
    1945        18276 :     if (OidIsValid(typentry->eq_opr))
    1946              :     {
    1947        18274 :         if (operator == typentry->eq_opr)
    1948        15905 :             isEquality = true;
    1949         2369 :         else if (get_negator(operator) == typentry->eq_opr)
    1950         1890 :             isInequality = true;
    1951              :     }
    1952              : 
    1953              :     /*
    1954              :      * If it is equality or inequality, we might be able to estimate this as a
    1955              :      * form of array containment; for instance "const = ANY(column)" can be
    1956              :      * treated as "ARRAY[const] <@ column".  scalararraysel_containment tries
    1957              :      * that, and returns the selectivity estimate if successful, or -1 if not.
    1958              :      */
    1959        18276 :     if ((isEquality || isInequality) && !is_join_clause)
    1960              :     {
    1961        17794 :         s1 = scalararraysel_containment(root, leftop, rightop,
    1962              :                                         nominal_element_type,
    1963              :                                         isEquality, useOr, varRelid);
    1964        17794 :         if (s1 >= 0.0)
    1965           95 :             return s1;
    1966              :     }
    1967              : 
    1968              :     /*
    1969              :      * Look up the underlying operator's selectivity estimator. Punt if it
    1970              :      * hasn't got one.
    1971              :      */
    1972        18181 :     if (is_join_clause)
    1973            1 :         oprsel = get_oprjoin(operator);
    1974              :     else
    1975        18180 :         oprsel = get_oprrest(operator);
    1976        18181 :     if (!oprsel)
    1977            2 :         return (Selectivity) 0.5;
    1978        18179 :     fmgr_info(oprsel, &oprselproc);
    1979              : 
    1980              :     /*
    1981              :      * In the array-containment check above, we must only believe that an
    1982              :      * operator is equality or inequality if it is the default btree equality
    1983              :      * operator (or its negator) for the element type, since those are the
    1984              :      * operators that array containment will use.  But in what follows, we can
    1985              :      * be a little laxer, and also believe that any operators using eqsel() or
    1986              :      * neqsel() as selectivity estimator act like equality or inequality.
    1987              :      */
    1988        18179 :     if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
    1989        15960 :         isEquality = true;
    1990         2219 :     else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
    1991         1819 :         isInequality = true;
    1992              : 
    1993              :     /*
    1994              :      * We consider three cases:
    1995              :      *
    1996              :      * 1. rightop is an Array constant: deconstruct the array, apply the
    1997              :      * operator's selectivity function for each array element, and merge the
    1998              :      * results in the same way that clausesel.c does for AND/OR combinations.
    1999              :      *
    2000              :      * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
    2001              :      * function for each element of the ARRAY[] construct, and merge.
    2002              :      *
    2003              :      * 3. otherwise, make a guess ...
    2004              :      */
    2005        18179 :     if (rightop && IsA(rightop, Const))
    2006        14654 :     {
    2007        14684 :         Datum       arraydatum = ((Const *) rightop)->constvalue;
    2008        14684 :         bool        arrayisnull = ((Const *) rightop)->constisnull;
    2009              :         ArrayType  *arrayval;
    2010              :         int16       elmlen;
    2011              :         bool        elmbyval;
    2012              :         char        elmalign;
    2013              :         int         num_elems;
    2014              :         Datum      *elem_values;
    2015              :         bool       *elem_nulls;
    2016              :         int         i;
    2017              : 
    2018        14684 :         if (arrayisnull)        /* qual can't succeed if null array */
    2019           30 :             return (Selectivity) 0.0;
    2020        14659 :         arrayval = DatumGetArrayTypeP(arraydatum);
    2021              : 
    2022              :         /*
    2023              :          * When the array contains a NULL constant, same as var_eq_const, we
    2024              :          * assume the operator is strict and nothing will match, thus return
    2025              :          * 0.0.
    2026              :          */
    2027        14659 :         if (!useOr && array_contains_nulls(arrayval))
    2028            5 :             return (Selectivity) 0.0;
    2029              : 
    2030        14654 :         get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
    2031              :                              &elmlen, &elmbyval, &elmalign);
    2032        14654 :         deconstruct_array(arrayval,
    2033              :                           ARR_ELEMTYPE(arrayval),
    2034              :                           elmlen, elmbyval, elmalign,
    2035              :                           &elem_values, &elem_nulls, &num_elems);
    2036              : 
    2037              :         /*
    2038              :          * For generic operators, we assume the probability of success is
    2039              :          * independent for each array element.  But for "= ANY" or "<> ALL",
    2040              :          * if the array elements are distinct (which'd typically be the case)
    2041              :          * then the probabilities are disjoint, and we should just sum them.
    2042              :          *
    2043              :          * If we were being really tense we would try to confirm that the
    2044              :          * elements are all distinct, but that would be expensive and it
    2045              :          * doesn't seem to be worth the cycles; it would amount to penalizing
    2046              :          * well-written queries in favor of poorly-written ones.  However, we
    2047              :          * do protect ourselves a little bit by checking whether the
    2048              :          * disjointness assumption leads to an impossible (out of range)
    2049              :          * probability; if so, we fall back to the normal calculation.
    2050              :          */
    2051        14654 :         s1 = s1disjoint = (useOr ? 0.0 : 1.0);
    2052              : 
    2053        57056 :         for (i = 0; i < num_elems; i++)
    2054              :         {
    2055              :             List       *args;
    2056              :             Selectivity s2;
    2057              : 
    2058        42402 :             args = list_make2(leftop,
    2059              :                               makeConst(nominal_element_type,
    2060              :                                         -1,
    2061              :                                         nominal_element_collation,
    2062              :                                         elmlen,
    2063              :                                         elem_values[i],
    2064              :                                         elem_nulls[i],
    2065              :                                         elmbyval));
    2066        42402 :             if (is_join_clause)
    2067            0 :                 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
    2068              :                                                       clause->inputcollid,
    2069              :                                                       PointerGetDatum(root),
    2070              :                                                       ObjectIdGetDatum(operator),
    2071              :                                                       PointerGetDatum(args),
    2072              :                                                       Int16GetDatum(jointype),
    2073              :                                                       PointerGetDatum(sjinfo)));
    2074              :             else
    2075        42402 :                 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
    2076              :                                                       clause->inputcollid,
    2077              :                                                       PointerGetDatum(root),
    2078              :                                                       ObjectIdGetDatum(operator),
    2079              :                                                       PointerGetDatum(args),
    2080              :                                                       Int32GetDatum(varRelid)));
    2081              : 
    2082        42402 :             if (useOr)
    2083              :             {
    2084        36568 :                 s1 = s1 + s2 - s1 * s2;
    2085        36568 :                 if (isEquality)
    2086        35698 :                     s1disjoint += s2;
    2087              :             }
    2088              :             else
    2089              :             {
    2090         5834 :                 s1 = s1 * s2;
    2091         5834 :                 if (isInequality)
    2092         5574 :                     s1disjoint += s2 - 1.0;
    2093              :             }
    2094              :         }
    2095              : 
    2096              :         /* accept disjoint-probability estimate if in range */
    2097        14654 :         if ((useOr ? isEquality : isInequality) &&
    2098        14149 :             s1disjoint >= 0.0 && s1disjoint <= 1.0)
    2099        14102 :             s1 = s1disjoint;
    2100              :     }
    2101         3495 :     else if (rightop && IsA(rightop, ArrayExpr) &&
    2102          296 :              !((ArrayExpr *) rightop)->multidims)
    2103          291 :     {
    2104          296 :         ArrayExpr  *arrayexpr = (ArrayExpr *) rightop;
    2105              :         int16       elmlen;
    2106              :         bool        elmbyval;
    2107              :         ListCell   *l;
    2108              : 
    2109          296 :         get_typlenbyval(arrayexpr->element_typeid,
    2110              :                         &elmlen, &elmbyval);
    2111              : 
    2112              :         /*
    2113              :          * We use the assumption of disjoint probabilities here too, although
    2114              :          * the odds of equal array elements are rather higher if the elements
    2115              :          * are not all constants (which they won't be, else constant folding
    2116              :          * would have reduced the ArrayExpr to a Const).  In this path it's
    2117              :          * critical to have the sanity check on the s1disjoint estimate.
    2118              :          */
    2119          296 :         s1 = s1disjoint = (useOr ? 0.0 : 1.0);
    2120              : 
    2121         1075 :         foreach(l, arrayexpr->elements)
    2122              :         {
    2123          784 :             Node       *elem = (Node *) lfirst(l);
    2124              :             List       *args;
    2125              :             Selectivity s2;
    2126              : 
    2127              :             /*
    2128              :              * When the array contains a NULL constant, same as var_eq_const,
    2129              :              * we assume the operator is strict and nothing will match, thus
    2130              :              * return 0.0.
    2131              :              */
    2132          784 :             if (!useOr && IsA(elem, Const) && ((Const *) elem)->constisnull)
    2133            5 :                 return (Selectivity) 0.0;
    2134              : 
    2135              :             /*
    2136              :              * Theoretically, if elem isn't of nominal_element_type we should
    2137              :              * insert a RelabelType, but it seems unlikely that any operator
    2138              :              * estimation function would really care ...
    2139              :              */
    2140          779 :             args = list_make2(leftop, elem);
    2141          779 :             if (is_join_clause)
    2142            3 :                 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
    2143              :                                                       clause->inputcollid,
    2144              :                                                       PointerGetDatum(root),
    2145              :                                                       ObjectIdGetDatum(operator),
    2146              :                                                       PointerGetDatum(args),
    2147              :                                                       Int16GetDatum(jointype),
    2148              :                                                       PointerGetDatum(sjinfo)));
    2149              :             else
    2150          776 :                 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
    2151              :                                                       clause->inputcollid,
    2152              :                                                       PointerGetDatum(root),
    2153              :                                                       ObjectIdGetDatum(operator),
    2154              :                                                       PointerGetDatum(args),
    2155              :                                                       Int32GetDatum(varRelid)));
    2156              : 
    2157          779 :             if (useOr)
    2158              :             {
    2159          759 :                 s1 = s1 + s2 - s1 * s2;
    2160          759 :                 if (isEquality)
    2161          759 :                     s1disjoint += s2;
    2162              :             }
    2163              :             else
    2164              :             {
    2165           20 :                 s1 = s1 * s2;
    2166           20 :                 if (isInequality)
    2167           20 :                     s1disjoint += s2 - 1.0;
    2168              :             }
    2169              :         }
    2170              : 
    2171              :         /* accept disjoint-probability estimate if in range */
    2172          291 :         if ((useOr ? isEquality : isInequality) &&
    2173          291 :             s1disjoint >= 0.0 && s1disjoint <= 1.0)
    2174          291 :             s1 = s1disjoint;
    2175              :     }
    2176              :     else
    2177              :     {
    2178              :         CaseTestExpr *dummyexpr;
    2179              :         List       *args;
    2180              :         Selectivity s2;
    2181              :         int         i;
    2182              : 
    2183              :         /*
    2184              :          * We need a dummy rightop to pass to the operator selectivity
    2185              :          * routine.  It can be pretty much anything that doesn't look like a
    2186              :          * constant; CaseTestExpr is a convenient choice.
    2187              :          */
    2188         3199 :         dummyexpr = makeNode(CaseTestExpr);
    2189         3199 :         dummyexpr->typeId = nominal_element_type;
    2190         3199 :         dummyexpr->typeMod = -1;
    2191         3199 :         dummyexpr->collation = clause->inputcollid;
    2192         3199 :         args = list_make2(leftop, dummyexpr);
    2193         3199 :         if (is_join_clause)
    2194            0 :             s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
    2195              :                                                   clause->inputcollid,
    2196              :                                                   PointerGetDatum(root),
    2197              :                                                   ObjectIdGetDatum(operator),
    2198              :                                                   PointerGetDatum(args),
    2199              :                                                   Int16GetDatum(jointype),
    2200              :                                                   PointerGetDatum(sjinfo)));
    2201              :         else
    2202         3199 :             s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
    2203              :                                                   clause->inputcollid,
    2204              :                                                   PointerGetDatum(root),
    2205              :                                                   ObjectIdGetDatum(operator),
    2206              :                                                   PointerGetDatum(args),
    2207              :                                                   Int32GetDatum(varRelid)));
    2208         3199 :         s1 = useOr ? 0.0 : 1.0;
    2209              : 
    2210              :         /*
    2211              :          * Arbitrarily assume 10 elements in the eventual array value (see
    2212              :          * also estimate_array_length).  We don't risk an assumption of
    2213              :          * disjoint probabilities here.
    2214              :          */
    2215        35189 :         for (i = 0; i < 10; i++)
    2216              :         {
    2217        31990 :             if (useOr)
    2218        31990 :                 s1 = s1 + s2 - s1 * s2;
    2219              :             else
    2220            0 :                 s1 = s1 * s2;
    2221              :         }
    2222              :     }
    2223              : 
    2224              :     /* result should be in range, but make sure... */
    2225        18144 :     CLAMP_PROBABILITY(s1);
    2226              : 
    2227        18144 :     return s1;
    2228              : }
    2229              : 
    2230              : /*
    2231              :  * Estimate number of elements in the array yielded by an expression.
    2232              :  *
    2233              :  * Note: the result is integral, but we use "double" to avoid overflow
    2234              :  * concerns.  Most callers will use it in double-type expressions anyway.
    2235              :  *
    2236              :  * Note: in some code paths root can be passed as NULL, resulting in
    2237              :  * slightly worse estimates.
    2238              :  */
    2239              : double
    2240        87384 : estimate_array_length(PlannerInfo *root, Node *arrayexpr)
    2241              : {
    2242              :     /* look through any binary-compatible relabeling of arrayexpr */
    2243        87384 :     arrayexpr = strip_array_coercion(arrayexpr);
    2244              : 
    2245        87384 :     if (arrayexpr && IsA(arrayexpr, Const))
    2246              :     {
    2247        38417 :         Datum       arraydatum = ((Const *) arrayexpr)->constvalue;
    2248        38417 :         bool        arrayisnull = ((Const *) arrayexpr)->constisnull;
    2249              :         ArrayType  *arrayval;
    2250              : 
    2251        38417 :         if (arrayisnull)
    2252           75 :             return 0;
    2253        38342 :         arrayval = DatumGetArrayTypeP(arraydatum);
    2254        38342 :         return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
    2255              :     }
    2256        48967 :     else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
    2257          512 :              !((ArrayExpr *) arrayexpr)->multidims)
    2258              :     {
    2259          512 :         return list_length(((ArrayExpr *) arrayexpr)->elements);
    2260              :     }
    2261        48455 :     else if (arrayexpr && root)
    2262              :     {
    2263              :         /* See if we can find any statistics about it */
    2264              :         VariableStatData vardata;
    2265              :         AttStatsSlot sslot;
    2266        48435 :         double      nelem = 0;
    2267              : 
    2268              :         /*
    2269              :          * Skip calling examine_variable for Var with varno 0, which has no
    2270              :          * valid relation entry and would error in find_base_rel.  Such a Var
    2271              :          * can appear when a nested set operation's output type doesn't match
    2272              :          * the parent's expected type, because recurse_set_operations builds a
    2273              :          * projection target list using generate_setop_tlist with varno 0, and
    2274              :          * if the required type coercion involves an ArrayCoerceExpr, we can
    2275              :          * be called on that Var.
    2276              :          */
    2277        48435 :         if (IsA(arrayexpr, Var) && ((Var *) arrayexpr)->varno == 0)
    2278         6421 :             return 10;          /* default guess, should match scalararraysel */
    2279              : 
    2280        48430 :         examine_variable(root, arrayexpr, 0, &vardata);
    2281        48430 :         if (HeapTupleIsValid(vardata.statsTuple))
    2282              :         {
    2283              :             /*
    2284              :              * Found stats, so use the average element count, which is stored
    2285              :              * in the last stanumbers element of the DECHIST statistics.
    2286              :              * Actually that is the average count of *distinct* elements;
    2287              :              * perhaps we should scale it up somewhat?
    2288              :              */
    2289         6511 :             if (get_attstatsslot(&sslot, vardata.statsTuple,
    2290              :                                  STATISTIC_KIND_DECHIST, InvalidOid,
    2291              :                                  ATTSTATSSLOT_NUMBERS))
    2292              :             {
    2293         6416 :                 if (sslot.nnumbers > 0)
    2294         6416 :                     nelem = clamp_row_est(sslot.numbers[sslot.nnumbers - 1]);
    2295         6416 :                 free_attstatsslot(&sslot);
    2296              :             }
    2297              :         }
    2298        48430 :         ReleaseVariableStats(vardata);
    2299              : 
    2300        48430 :         if (nelem > 0)
    2301         6416 :             return nelem;
    2302              :     }
    2303              : 
    2304              :     /* Else use a default guess --- this should match scalararraysel */
    2305        42034 :     return 10;
    2306              : }
    2307              : 
    2308              : /*
    2309              :  *      rowcomparesel       - Selectivity of RowCompareExpr Node.
    2310              :  *
    2311              :  * We estimate RowCompare selectivity by considering just the first (high
    2312              :  * order) columns, which makes it equivalent to an ordinary OpExpr.  While
    2313              :  * this estimate could be refined by considering additional columns, it
    2314              :  * seems unlikely that we could do a lot better without multi-column
    2315              :  * statistics.
    2316              :  */
    2317              : Selectivity
    2318          240 : rowcomparesel(PlannerInfo *root,
    2319              :               RowCompareExpr *clause,
    2320              :               int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
    2321              : {
    2322              :     Selectivity s1;
    2323          240 :     Oid         opno = linitial_oid(clause->opnos);
    2324          240 :     Oid         inputcollid = linitial_oid(clause->inputcollids);
    2325              :     List       *opargs;
    2326              :     bool        is_join_clause;
    2327              : 
    2328              :     /* Build equivalent arg list for single operator */
    2329          240 :     opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
    2330              : 
    2331              :     /*
    2332              :      * Decide if it's a join clause.  This should match clausesel.c's
    2333              :      * treat_as_join_clause(), except that we intentionally consider only the
    2334              :      * leading columns and not the rest of the clause.
    2335              :      */
    2336          240 :     if (varRelid != 0)
    2337              :     {
    2338              :         /*
    2339              :          * Caller is forcing restriction mode (eg, because we are examining an
    2340              :          * inner indexscan qual).
    2341              :          */
    2342           45 :         is_join_clause = false;
    2343              :     }
    2344          195 :     else if (sjinfo == NULL)
    2345              :     {
    2346              :         /*
    2347              :          * It must be a restriction clause, since it's being evaluated at a
    2348              :          * scan node.
    2349              :          */
    2350          175 :         is_join_clause = false;
    2351              :     }
    2352              :     else
    2353              :     {
    2354              :         /*
    2355              :          * Otherwise, it's a join if there's more than one base relation used.
    2356              :          */
    2357           20 :         is_join_clause = (NumRelids(root, (Node *) opargs) > 1);
    2358              :     }
    2359              : 
    2360          240 :     if (is_join_clause)
    2361              :     {
    2362              :         /* Estimate selectivity for a join clause. */
    2363           20 :         s1 = join_selectivity(root, opno,
    2364              :                               opargs,
    2365              :                               inputcollid,
    2366              :                               jointype,
    2367              :                               sjinfo);
    2368              :     }
    2369              :     else
    2370              :     {
    2371              :         /* Estimate selectivity for a restriction clause. */
    2372          220 :         s1 = restriction_selectivity(root, opno,
    2373              :                                      opargs,
    2374              :                                      inputcollid,
    2375              :                                      varRelid);
    2376              :     }
    2377              : 
    2378          240 :     return s1;
    2379              : }
    2380              : 
    2381              : /*
    2382              :  *      eqjoinsel       - Join selectivity of "="
    2383              :  */
    2384              : Datum
    2385       218790 : eqjoinsel(PG_FUNCTION_ARGS)
    2386              : {
    2387       218790 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
    2388       218790 :     Oid         operator = PG_GETARG_OID(1);
    2389       218790 :     List       *args = (List *) PG_GETARG_POINTER(2);
    2390              : 
    2391              : #ifdef NOT_USED
    2392              :     JoinType    jointype = (JoinType) PG_GETARG_INT16(3);
    2393              : #endif
    2394       218790 :     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
    2395       218790 :     Oid         collation = PG_GET_COLLATION();
    2396              :     double      selec;
    2397              :     double      selec_inner;
    2398              :     VariableStatData vardata1;
    2399              :     VariableStatData vardata2;
    2400              :     double      nd1;
    2401              :     double      nd2;
    2402              :     bool        isdefault1;
    2403              :     bool        isdefault2;
    2404              :     Oid         opfuncoid;
    2405              :     FmgrInfo    eqproc;
    2406       218790 :     Oid         hashLeft = InvalidOid;
    2407       218790 :     Oid         hashRight = InvalidOid;
    2408              :     AttStatsSlot sslot1;
    2409              :     AttStatsSlot sslot2;
    2410       218790 :     Form_pg_statistic stats1 = NULL;
    2411       218790 :     Form_pg_statistic stats2 = NULL;
    2412       218790 :     bool        have_mcvs1 = false;
    2413       218790 :     bool        have_mcvs2 = false;
    2414       218790 :     bool       *hasmatch1 = NULL;
    2415       218790 :     bool       *hasmatch2 = NULL;
    2416       218790 :     int         nmatches = 0;
    2417              :     bool        get_mcv_stats;
    2418              :     bool        join_is_reversed;
    2419              :     RelOptInfo *inner_rel;
    2420              : 
    2421       218790 :     get_join_variables(root, args, sjinfo,
    2422              :                        &vardata1, &vardata2, &join_is_reversed);
    2423              : 
    2424       218790 :     nd1 = get_variable_numdistinct(&vardata1, &isdefault1);
    2425       218790 :     nd2 = get_variable_numdistinct(&vardata2, &isdefault2);
    2426              : 
    2427       218790 :     opfuncoid = get_opcode(operator);
    2428              : 
    2429       218790 :     memset(&sslot1, 0, sizeof(sslot1));
    2430       218790 :     memset(&sslot2, 0, sizeof(sslot2));
    2431              : 
    2432              :     /*
    2433              :      * There is no use in fetching one side's MCVs if we lack MCVs for the
    2434              :      * other side, so do a quick check to verify that both stats exist.
    2435              :      */
    2436       587865 :     get_mcv_stats = (HeapTupleIsValid(vardata1.statsTuple) &&
    2437       262524 :                      HeapTupleIsValid(vardata2.statsTuple) &&
    2438       112239 :                      get_attstatsslot(&sslot1, vardata1.statsTuple,
    2439              :                                       STATISTIC_KIND_MCV, InvalidOid,
    2440       369075 :                                       0) &&
    2441        56257 :                      get_attstatsslot(&sslot2, vardata2.statsTuple,
    2442              :                                       STATISTIC_KIND_MCV, InvalidOid,
    2443              :                                       0));
    2444              : 
    2445       218790 :     if (HeapTupleIsValid(vardata1.statsTuple))
    2446              :     {
    2447              :         /* note we allow use of nullfrac regardless of security check */
    2448       150285 :         stats1 = (Form_pg_statistic) GETSTRUCT(vardata1.statsTuple);
    2449       172839 :         if (get_mcv_stats &&
    2450        22554 :             statistic_proc_security_check(&vardata1, opfuncoid))
    2451        22554 :             have_mcvs1 = get_attstatsslot(&sslot1, vardata1.statsTuple,
    2452              :                                           STATISTIC_KIND_MCV, InvalidOid,
    2453              :                                           ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
    2454              :     }
    2455              : 
    2456       218790 :     if (HeapTupleIsValid(vardata2.statsTuple))
    2457              :     {
    2458              :         /* note we allow use of nullfrac regardless of security check */
    2459       129643 :         stats2 = (Form_pg_statistic) GETSTRUCT(vardata2.statsTuple);
    2460       152197 :         if (get_mcv_stats &&
    2461        22554 :             statistic_proc_security_check(&vardata2, opfuncoid))
    2462        22554 :             have_mcvs2 = get_attstatsslot(&sslot2, vardata2.statsTuple,
    2463              :                                           STATISTIC_KIND_MCV, InvalidOid,
    2464              :                                           ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
    2465              :     }
    2466              : 
    2467              :     /* Prepare info usable by both eqjoinsel_inner and eqjoinsel_semi */
    2468       218790 :     if (have_mcvs1 && have_mcvs2)
    2469              :     {
    2470        22554 :         fmgr_info(opfuncoid, &eqproc);
    2471        22554 :         hasmatch1 = (bool *) palloc0(sslot1.nvalues * sizeof(bool));
    2472        22554 :         hasmatch2 = (bool *) palloc0(sslot2.nvalues * sizeof(bool));
    2473              : 
    2474              :         /*
    2475              :          * If the MCV lists are long enough to justify hashing, try to look up
    2476              :          * hash functions for the join operator.
    2477              :          */
    2478        22554 :         if ((sslot1.nvalues + sslot2.nvalues) >= EQJOINSEL_MCV_HASH_THRESHOLD)
    2479         1288 :             (void) get_op_hash_functions_ext(operator,
    2480         1288 :                                              exprType((Node *) linitial(args)),
    2481              :                                              &hashLeft, &hashRight);
    2482              :     }
    2483              :     else
    2484       196236 :         memset(&eqproc, 0, sizeof(eqproc)); /* silence uninit-var warnings */
    2485              : 
    2486              :     /* We need to compute the inner-join selectivity in all cases */
    2487       218790 :     selec_inner = eqjoinsel_inner(&eqproc, collation,
    2488              :                                   hashLeft, hashRight,
    2489              :                                   &vardata1, &vardata2,
    2490              :                                   nd1, nd2,
    2491              :                                   isdefault1, isdefault2,
    2492              :                                   &sslot1, &sslot2,
    2493              :                                   stats1, stats2,
    2494              :                                   have_mcvs1, have_mcvs2,
    2495              :                                   hasmatch1, hasmatch2,
    2496              :                                   &nmatches);
    2497              : 
    2498       218790 :     switch (sjinfo->jointype)
    2499              :     {
    2500       207382 :         case JOIN_INNER:
    2501              :         case JOIN_LEFT:
    2502              :         case JOIN_FULL:
    2503       207382 :             selec = selec_inner;
    2504       207382 :             break;
    2505        11408 :         case JOIN_SEMI:
    2506              :         case JOIN_ANTI:
    2507              : 
    2508              :             /*
    2509              :              * Look up the join's inner relation.  min_righthand is sufficient
    2510              :              * information because neither SEMI nor ANTI joins permit any
    2511              :              * reassociation into or out of their RHS, so the righthand will
    2512              :              * always be exactly that set of rels.
    2513              :              */
    2514        11408 :             inner_rel = find_join_input_rel(root, sjinfo->min_righthand);
    2515              : 
    2516        11408 :             if (!join_is_reversed)
    2517         5419 :                 selec = eqjoinsel_semi(&eqproc, collation,
    2518              :                                        hashLeft, hashRight,
    2519              :                                        false,
    2520              :                                        &vardata1, &vardata2,
    2521              :                                        nd1, nd2,
    2522              :                                        isdefault1, isdefault2,
    2523              :                                        &sslot1, &sslot2,
    2524              :                                        stats1, stats2,
    2525              :                                        have_mcvs1, have_mcvs2,
    2526              :                                        hasmatch1, hasmatch2,
    2527              :                                        &nmatches,
    2528              :                                        inner_rel);
    2529              :             else
    2530         5989 :                 selec = eqjoinsel_semi(&eqproc, collation,
    2531              :                                        hashLeft, hashRight,
    2532              :                                        true,
    2533              :                                        &vardata2, &vardata1,
    2534              :                                        nd2, nd1,
    2535              :                                        isdefault2, isdefault1,
    2536              :                                        &sslot2, &sslot1,
    2537              :                                        stats2, stats1,
    2538              :                                        have_mcvs2, have_mcvs1,
    2539              :                                        hasmatch2, hasmatch1,
    2540              :                                        &nmatches,
    2541              :                                        inner_rel);
    2542              : 
    2543              :             /*
    2544              :              * We should never estimate the output of a semijoin to be more
    2545              :              * rows than we estimate for an inner join with the same input
    2546              :              * rels and join condition; it's obviously impossible for that to
    2547              :              * happen.  The former estimate is N1 * Ssemi while the latter is
    2548              :              * N1 * N2 * Sinner, so we may clamp Ssemi <= N2 * Sinner.  Doing
    2549              :              * this is worthwhile because of the shakier estimation rules we
    2550              :              * use in eqjoinsel_semi, particularly in cases where it has to
    2551              :              * punt entirely.
    2552              :              */
    2553        11408 :             selec = Min(selec, inner_rel->rows * selec_inner);
    2554        11408 :             break;
    2555            0 :         default:
    2556              :             /* other values not expected here */
    2557            0 :             elog(ERROR, "unrecognized join type: %d",
    2558              :                  (int) sjinfo->jointype);
    2559              :             selec = 0;          /* keep compiler quiet */
    2560              :             break;
    2561              :     }
    2562              : 
    2563       218790 :     free_attstatsslot(&sslot1);
    2564       218790 :     free_attstatsslot(&sslot2);
    2565              : 
    2566       218790 :     ReleaseVariableStats(vardata1);
    2567       218790 :     ReleaseVariableStats(vardata2);
    2568              : 
    2569       218790 :     if (hasmatch1)
    2570        22554 :         pfree(hasmatch1);
    2571       218790 :     if (hasmatch2)
    2572        22554 :         pfree(hasmatch2);
    2573              : 
    2574       218790 :     CLAMP_PROBABILITY(selec);
    2575              : 
    2576       218790 :     PG_RETURN_FLOAT8((float8) selec);
    2577              : }
    2578              : 
    2579              : /*
    2580              :  * eqjoinsel_inner --- eqjoinsel for normal inner join
    2581              :  *
    2582              :  * In addition to computing the selectivity estimate, this will fill
    2583              :  * hasmatch1[], hasmatch2[], and *p_nmatches (if have_mcvs1 && have_mcvs2).
    2584              :  * We may be able to re-use that data in eqjoinsel_semi.
    2585              :  *
    2586              :  * We also use this for LEFT/FULL outer joins; it's not presently clear
    2587              :  * that it's worth trying to distinguish them here.
    2588              :  */
    2589              : static double
    2590       218790 : eqjoinsel_inner(FmgrInfo *eqproc, Oid collation,
    2591              :                 Oid hashLeft, Oid hashRight,
    2592              :                 VariableStatData *vardata1, VariableStatData *vardata2,
    2593              :                 double nd1, double nd2,
    2594              :                 bool isdefault1, bool isdefault2,
    2595              :                 AttStatsSlot *sslot1, AttStatsSlot *sslot2,
    2596              :                 Form_pg_statistic stats1, Form_pg_statistic stats2,
    2597              :                 bool have_mcvs1, bool have_mcvs2,
    2598              :                 bool *hasmatch1, bool *hasmatch2,
    2599              :                 int *p_nmatches)
    2600              : {
    2601              :     double      selec;
    2602              : 
    2603       218790 :     if (have_mcvs1 && have_mcvs2)
    2604        22554 :     {
    2605              :         /*
    2606              :          * We have most-common-value lists for both relations.  Run through
    2607              :          * the lists to see which MCVs actually join to each other with the
    2608              :          * given operator.  This allows us to determine the exact join
    2609              :          * selectivity for the portion of the relations represented by the MCV
    2610              :          * lists.  We still have to estimate for the remaining population, but
    2611              :          * in a skewed distribution this gives us a big leg up in accuracy.
    2612              :          * For motivation see the analysis in Y. Ioannidis and S.
    2613              :          * Christodoulakis, "On the propagation of errors in the size of join
    2614              :          * results", Technical Report 1018, Computer Science Dept., University
    2615              :          * of Wisconsin, Madison, March 1991 (available from ftp.cs.wisc.edu).
    2616              :          */
    2617        22554 :         double      nullfrac1 = stats1->stanullfrac;
    2618        22554 :         double      nullfrac2 = stats2->stanullfrac;
    2619              :         double      matchprodfreq,
    2620              :                     matchfreq1,
    2621              :                     matchfreq2,
    2622              :                     unmatchfreq1,
    2623              :                     unmatchfreq2,
    2624              :                     otherfreq1,
    2625              :                     otherfreq2,
    2626              :                     totalsel1,
    2627              :                     totalsel2;
    2628              :         int         i,
    2629              :                     nmatches;
    2630              : 
    2631              :         /* Fill the match arrays */
    2632        22554 :         eqjoinsel_find_matches(eqproc, collation,
    2633              :                                hashLeft, hashRight,
    2634              :                                false,
    2635              :                                sslot1, sslot2,
    2636              :                                sslot1->nvalues, sslot2->nvalues,
    2637              :                                hasmatch1, hasmatch2,
    2638              :                                p_nmatches, &matchprodfreq);
    2639        22554 :         nmatches = *p_nmatches;
    2640        22554 :         CLAMP_PROBABILITY(matchprodfreq);
    2641              : 
    2642              :         /* Sum up frequencies of matched and unmatched MCVs */
    2643        22554 :         matchfreq1 = unmatchfreq1 = 0.0;
    2644       526593 :         for (i = 0; i < sslot1->nvalues; i++)
    2645              :         {
    2646       504039 :             if (hasmatch1[i])
    2647       234753 :                 matchfreq1 += sslot1->numbers[i];
    2648              :             else
    2649       269286 :                 unmatchfreq1 += sslot1->numbers[i];
    2650              :         }
    2651        22554 :         CLAMP_PROBABILITY(matchfreq1);
    2652        22554 :         CLAMP_PROBABILITY(unmatchfreq1);
    2653        22554 :         matchfreq2 = unmatchfreq2 = 0.0;
    2654       411388 :         for (i = 0; i < sslot2->nvalues; i++)
    2655              :         {
    2656       388834 :             if (hasmatch2[i])
    2657       234753 :                 matchfreq2 += sslot2->numbers[i];
    2658              :             else
    2659       154081 :                 unmatchfreq2 += sslot2->numbers[i];
    2660              :         }
    2661        22554 :         CLAMP_PROBABILITY(matchfreq2);
    2662        22554 :         CLAMP_PROBABILITY(unmatchfreq2);
    2663              : 
    2664              :         /*
    2665              :          * Compute total frequency of non-null values that are not in the MCV
    2666              :          * lists.
    2667              :          */
    2668        22554 :         otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
    2669        22554 :         otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
    2670        22554 :         CLAMP_PROBABILITY(otherfreq1);
    2671        22554 :         CLAMP_PROBABILITY(otherfreq2);
    2672              : 
    2673              :         /*
    2674              :          * We can estimate the total selectivity from the point of view of
    2675              :          * relation 1 as: the known selectivity for matched MCVs, plus
    2676              :          * unmatched MCVs that are assumed to match against random members of
    2677              :          * relation 2's non-MCV population, plus non-MCV values that are
    2678              :          * assumed to match against random members of relation 2's unmatched
    2679              :          * MCVs plus non-MCV values.
    2680              :          */
    2681        22554 :         totalsel1 = matchprodfreq;
    2682        22554 :         if (nd2 > sslot2->nvalues)
    2683         4310 :             totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - sslot2->nvalues);
    2684        22554 :         if (nd2 > nmatches)
    2685         8235 :             totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
    2686         8235 :                 (nd2 - nmatches);
    2687              :         /* Same estimate from the point of view of relation 2. */
    2688        22554 :         totalsel2 = matchprodfreq;
    2689        22554 :         if (nd1 > sslot1->nvalues)
    2690         5018 :             totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - sslot1->nvalues);
    2691        22554 :         if (nd1 > nmatches)
    2692         7219 :             totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
    2693         7219 :                 (nd1 - nmatches);
    2694              : 
    2695              :         /*
    2696              :          * Use the smaller of the two estimates.  This can be justified in
    2697              :          * essentially the same terms as given below for the no-stats case: to
    2698              :          * a first approximation, we are estimating from the point of view of
    2699              :          * the relation with smaller nd.
    2700              :          */
    2701        22554 :         selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
    2702              :     }
    2703              :     else
    2704              :     {
    2705              :         /*
    2706              :          * We do not have MCV lists for both sides.  Estimate the join
    2707              :          * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). This
    2708              :          * is plausible if we assume that the join operator is strict and the
    2709              :          * non-null values are about equally distributed: a given non-null
    2710              :          * tuple of rel1 will join to either zero or N2*(1-nullfrac2)/nd2 rows
    2711              :          * of rel2, so total join rows are at most
    2712              :          * N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join selectivity of
    2713              :          * not more than (1-nullfrac1)*(1-nullfrac2)/nd2. By the same logic it
    2714              :          * is not more than (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression
    2715              :          * with MIN() is an upper bound.  Using the MIN() means we estimate
    2716              :          * from the point of view of the relation with smaller nd (since the
    2717              :          * larger nd is determining the MIN).  It is reasonable to assume that
    2718              :          * most tuples in this rel will have join partners, so the bound is
    2719              :          * probably reasonably tight and should be taken as-is.
    2720              :          *
    2721              :          * XXX Can we be smarter if we have an MCV list for just one side? It
    2722              :          * seems that if we assume equal distribution for the other side, we
    2723              :          * end up with the same answer anyway.
    2724              :          */
    2725       196236 :         double      nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
    2726       196236 :         double      nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
    2727              : 
    2728       196236 :         selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
    2729       196236 :         if (nd1 > nd2)
    2730        96311 :             selec /= nd1;
    2731              :         else
    2732        99925 :             selec /= nd2;
    2733              :     }
    2734              : 
    2735       218790 :     return selec;
    2736              : }
    2737              : 
    2738              : /*
    2739              :  * eqjoinsel_semi --- eqjoinsel for semi join
    2740              :  *
    2741              :  * (Also used for anti join, which we are supposed to estimate the same way.)
    2742              :  * Caller has ensured that vardata1 is the LHS variable; however, eqproc
    2743              :  * is for the original join operator, which might now need to have the inputs
    2744              :  * swapped in order to apply correctly.  Also, if have_mcvs1 && have_mcvs2
    2745              :  * then hasmatch1[], hasmatch2[], and *p_nmatches were filled by
    2746              :  * eqjoinsel_inner.
    2747              :  */
    2748              : static double
    2749        11408 : eqjoinsel_semi(FmgrInfo *eqproc, Oid collation,
    2750              :                Oid hashLeft, Oid hashRight,
    2751              :                bool op_is_reversed,
    2752              :                VariableStatData *vardata1, VariableStatData *vardata2,
    2753              :                double nd1, double nd2,
    2754              :                bool isdefault1, bool isdefault2,
    2755              :                AttStatsSlot *sslot1, AttStatsSlot *sslot2,
    2756              :                Form_pg_statistic stats1, Form_pg_statistic stats2,
    2757              :                bool have_mcvs1, bool have_mcvs2,
    2758              :                bool *hasmatch1, bool *hasmatch2,
    2759              :                int *p_nmatches,
    2760              :                RelOptInfo *inner_rel)
    2761              : {
    2762              :     double      selec;
    2763              : 
    2764              :     /*
    2765              :      * We clamp nd2 to be not more than what we estimate the inner relation's
    2766              :      * size to be.  This is intuitively somewhat reasonable since obviously
    2767              :      * there can't be more than that many distinct values coming from the
    2768              :      * inner rel.  The reason for the asymmetry (ie, that we don't clamp nd1
    2769              :      * likewise) is that this is the only pathway by which restriction clauses
    2770              :      * applied to the inner rel will affect the join result size estimate,
    2771              :      * since set_joinrel_size_estimates will multiply SEMI/ANTI selectivity by
    2772              :      * only the outer rel's size.  If we clamped nd1 we'd be double-counting
    2773              :      * the selectivity of outer-rel restrictions.
    2774              :      *
    2775              :      * We can apply this clamping both with respect to the base relation from
    2776              :      * which the join variable comes (if there is just one), and to the
    2777              :      * immediate inner input relation of the current join.
    2778              :      *
    2779              :      * If we clamp, we can treat nd2 as being a non-default estimate; it's not
    2780              :      * great, maybe, but it didn't come out of nowhere either.  This is most
    2781              :      * helpful when the inner relation is empty and consequently has no stats.
    2782              :      */
    2783        11408 :     if (vardata2->rel)
    2784              :     {
    2785        11403 :         if (nd2 >= vardata2->rel->rows)
    2786              :         {
    2787         9643 :             nd2 = vardata2->rel->rows;
    2788         9643 :             isdefault2 = false;
    2789              :         }
    2790              :     }
    2791        11408 :     if (nd2 >= inner_rel->rows)
    2792              :     {
    2793         9593 :         nd2 = inner_rel->rows;
    2794         9593 :         isdefault2 = false;
    2795              :     }
    2796              : 
    2797        11408 :     if (have_mcvs1 && have_mcvs2)
    2798          519 :     {
    2799              :         /*
    2800              :          * We have most-common-value lists for both relations.  Run through
    2801              :          * the lists to see which MCVs actually join to each other with the
    2802              :          * given operator.  This allows us to determine the exact join
    2803              :          * selectivity for the portion of the relations represented by the MCV
    2804              :          * lists.  We still have to estimate for the remaining population, but
    2805              :          * in a skewed distribution this gives us a big leg up in accuracy.
    2806              :          */
    2807          519 :         double      nullfrac1 = stats1->stanullfrac;
    2808              :         double      matchprodfreq,
    2809              :                     matchfreq1,
    2810              :                     uncertainfrac,
    2811              :                     uncertain;
    2812              :         int         i,
    2813              :                     nmatches,
    2814              :                     clamped_nvalues2;
    2815              : 
    2816              :         /*
    2817              :          * The clamping above could have resulted in nd2 being less than
    2818              :          * sslot2->nvalues; in which case, we assume that precisely the nd2
    2819              :          * most common values in the relation will appear in the join input,
    2820              :          * and so compare to only the first nd2 members of the MCV list.  Of
    2821              :          * course this is frequently wrong, but it's the best bet we can make.
    2822              :          */
    2823          519 :         clamped_nvalues2 = Min(sslot2->nvalues, nd2);
    2824              : 
    2825              :         /*
    2826              :          * If we did not set clamped_nvalues2 to less than sslot2->nvalues,
    2827              :          * then the hasmatch1[] and hasmatch2[] match flags computed by
    2828              :          * eqjoinsel_inner are still perfectly applicable, so we need not
    2829              :          * re-do the matching work.  Note that it does not matter if
    2830              :          * op_is_reversed: we'd get the same answers.
    2831              :          *
    2832              :          * If we did clamp, then a different set of sslot2 values is to be
    2833              :          * compared, so we have to re-do the matching.
    2834              :          */
    2835          519 :         if (clamped_nvalues2 != sslot2->nvalues)
    2836              :         {
    2837              :             /* Must re-zero the arrays */
    2838            0 :             memset(hasmatch1, 0, sslot1->nvalues * sizeof(bool));
    2839            0 :             memset(hasmatch2, 0, clamped_nvalues2 * sizeof(bool));
    2840              :             /* Re-fill the match arrays */
    2841            0 :             eqjoinsel_find_matches(eqproc, collation,
    2842              :                                    hashLeft, hashRight,
    2843              :                                    op_is_reversed,
    2844              :                                    sslot1, sslot2,
    2845              :                                    sslot1->nvalues, clamped_nvalues2,
    2846              :                                    hasmatch1, hasmatch2,
    2847              :                                    p_nmatches, &matchprodfreq);
    2848              :         }
    2849          519 :         nmatches = *p_nmatches;
    2850              : 
    2851              :         /* Sum up frequencies of matched MCVs */
    2852          519 :         matchfreq1 = 0.0;
    2853        11531 :         for (i = 0; i < sslot1->nvalues; i++)
    2854              :         {
    2855        11012 :             if (hasmatch1[i])
    2856         9529 :                 matchfreq1 += sslot1->numbers[i];
    2857              :         }
    2858          519 :         CLAMP_PROBABILITY(matchfreq1);
    2859              : 
    2860              :         /*
    2861              :          * Now we need to estimate the fraction of relation 1 that has at
    2862              :          * least one join partner.  We know for certain that the matched MCVs
    2863              :          * do, so that gives us a lower bound, but we're really in the dark
    2864              :          * about everything else.  Our crude approach is: if nd1 <= nd2 then
    2865              :          * assume all non-null rel1 rows have join partners, else assume for
    2866              :          * the uncertain rows that a fraction nd2/nd1 have join partners. We
    2867              :          * can discount the known-matched MCVs from the distinct-values counts
    2868              :          * before doing the division.
    2869              :          *
    2870              :          * Crude as the above is, it's completely useless if we don't have
    2871              :          * reliable ndistinct values for both sides.  Hence, if either nd1 or
    2872              :          * nd2 is default, punt and assume half of the uncertain rows have
    2873              :          * join partners.
    2874              :          */
    2875          519 :         if (!isdefault1 && !isdefault2)
    2876              :         {
    2877          519 :             nd1 -= nmatches;
    2878          519 :             nd2 -= nmatches;
    2879          519 :             if (nd1 <= nd2 || nd2 < 0)
    2880          489 :                 uncertainfrac = 1.0;
    2881              :             else
    2882           30 :                 uncertainfrac = nd2 / nd1;
    2883              :         }
    2884              :         else
    2885            0 :             uncertainfrac = 0.5;
    2886          519 :         uncertain = 1.0 - matchfreq1 - nullfrac1;
    2887          519 :         CLAMP_PROBABILITY(uncertain);
    2888          519 :         selec = matchfreq1 + uncertainfrac * uncertain;
    2889              :     }
    2890              :     else
    2891              :     {
    2892              :         /*
    2893              :          * Without MCV lists for both sides, we can only use the heuristic
    2894              :          * about nd1 vs nd2.
    2895              :          */
    2896        10889 :         double      nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
    2897              : 
    2898        10889 :         if (!isdefault1 && !isdefault2)
    2899              :         {
    2900         8075 :             if (nd1 <= nd2 || nd2 < 0)
    2901         4221 :                 selec = 1.0 - nullfrac1;
    2902              :             else
    2903         3854 :                 selec = (nd2 / nd1) * (1.0 - nullfrac1);
    2904              :         }
    2905              :         else
    2906         2814 :             selec = 0.5 * (1.0 - nullfrac1);
    2907              :     }
    2908              : 
    2909        11408 :     return selec;
    2910              : }
    2911              : 
    2912              : /*
    2913              :  * Identify matching MCVs for eqjoinsel_inner or eqjoinsel_semi.
    2914              :  *
    2915              :  * Inputs:
    2916              :  *  eqproc: FmgrInfo for equality function to use (might be reversed)
    2917              :  *  collation: OID of collation to use
    2918              :  *  hashLeft, hashRight: OIDs of hash functions associated with equality op,
    2919              :  *      or InvalidOid if we're not to use hashing
    2920              :  *  op_is_reversed: indicates that eqproc compares right type to left type
    2921              :  *  sslot1, sslot2: MCV values for the lefthand and righthand inputs
    2922              :  *  nvalues1, nvalues2: number of values to be considered (can be less than
    2923              :  *      sslotN->nvalues, but not more)
    2924              :  * Outputs:
    2925              :  *  hasmatch1[], hasmatch2[]: pre-zeroed arrays of lengths nvalues1, nvalues2;
    2926              :  *      entries are set to true if that MCV has a match on the other side
    2927              :  *  *p_nmatches: receives number of MCV pairs that match
    2928              :  *  *p_matchprodfreq: receives sum(sslot1->numbers[i] * sslot2->numbers[j])
    2929              :  *      for matching MCVs
    2930              :  *
    2931              :  * Note that hashLeft is for the eqproc's left-hand input type, hashRight
    2932              :  * for its right, regardless of op_is_reversed.
    2933              :  *
    2934              :  * Note we assume that each MCV will match at most one member of the other
    2935              :  * MCV list.  If the operator isn't really equality, there could be multiple
    2936              :  * matches --- but we don't look for them, both for speed and because the
    2937              :  * math wouldn't add up...
    2938              :  */
    2939              : static void
    2940        22554 : eqjoinsel_find_matches(FmgrInfo *eqproc, Oid collation,
    2941              :                        Oid hashLeft, Oid hashRight,
    2942              :                        bool op_is_reversed,
    2943              :                        AttStatsSlot *sslot1, AttStatsSlot *sslot2,
    2944              :                        int nvalues1, int nvalues2,
    2945              :                        bool *hasmatch1, bool *hasmatch2,
    2946              :                        int *p_nmatches, double *p_matchprodfreq)
    2947              : {
    2948        22554 :     LOCAL_FCINFO(fcinfo, 2);
    2949        22554 :     double      matchprodfreq = 0.0;
    2950        22554 :     int         nmatches = 0;
    2951              : 
    2952              :     /*
    2953              :      * Save a few cycles by setting up the fcinfo struct just once.  Using
    2954              :      * FunctionCallInvoke directly also avoids failure if the eqproc returns
    2955              :      * NULL, though really equality functions should never do that.
    2956              :      */
    2957        22554 :     InitFunctionCallInfoData(*fcinfo, eqproc, 2, collation,
    2958              :                              NULL, NULL);
    2959        22554 :     fcinfo->args[0].isnull = false;
    2960        22554 :     fcinfo->args[1].isnull = false;
    2961              : 
    2962        22554 :     if (OidIsValid(hashLeft) && OidIsValid(hashRight))
    2963         1288 :     {
    2964              :         /* Use a hash table to speed up the matching */
    2965         1288 :         LOCAL_FCINFO(hash_fcinfo, 1);
    2966              :         FmgrInfo    hash_proc;
    2967              :         MCVHashContext hashContext;
    2968              :         MCVHashTable_hash *hashTable;
    2969              :         AttStatsSlot *statsProbe;
    2970              :         AttStatsSlot *statsHash;
    2971              :         bool       *hasMatchProbe;
    2972              :         bool       *hasMatchHash;
    2973              :         int         nvaluesProbe;
    2974              :         int         nvaluesHash;
    2975              : 
    2976              :         /* Make sure we build the hash table on the smaller array. */
    2977         1288 :         if (sslot1->nvalues >= sslot2->nvalues)
    2978              :         {
    2979         1288 :             statsProbe = sslot1;
    2980         1288 :             statsHash = sslot2;
    2981         1288 :             hasMatchProbe = hasmatch1;
    2982         1288 :             hasMatchHash = hasmatch2;
    2983         1288 :             nvaluesProbe = nvalues1;
    2984         1288 :             nvaluesHash = nvalues2;
    2985              :         }
    2986              :         else
    2987              :         {
    2988              :             /* We'll have to reverse the direction of use of the operator. */
    2989            0 :             op_is_reversed = !op_is_reversed;
    2990            0 :             statsProbe = sslot2;
    2991            0 :             statsHash = sslot1;
    2992            0 :             hasMatchProbe = hasmatch2;
    2993            0 :             hasMatchHash = hasmatch1;
    2994            0 :             nvaluesProbe = nvalues2;
    2995            0 :             nvaluesHash = nvalues1;
    2996              :         }
    2997              : 
    2998              :         /*
    2999              :          * Build the hash table on the smaller array, using the appropriate
    3000              :          * hash function for its data type.
    3001              :          */
    3002         1288 :         fmgr_info(op_is_reversed ? hashLeft : hashRight, &hash_proc);
    3003         1288 :         InitFunctionCallInfoData(*hash_fcinfo, &hash_proc, 1, collation,
    3004              :                                  NULL, NULL);
    3005         1288 :         hash_fcinfo->args[0].isnull = false;
    3006              : 
    3007         1288 :         hashContext.equal_fcinfo = fcinfo;
    3008         1288 :         hashContext.hash_fcinfo = hash_fcinfo;
    3009         1288 :         hashContext.op_is_reversed = op_is_reversed;
    3010         1288 :         hashContext.insert_mode = true;
    3011         1288 :         get_typlenbyval(statsHash->valuetype,
    3012              :                         &hashContext.hash_typlen,
    3013              :                         &hashContext.hash_typbyval);
    3014              : 
    3015         1288 :         hashTable = MCVHashTable_create(CurrentMemoryContext,
    3016              :                                         nvaluesHash,
    3017              :                                         &hashContext);
    3018              : 
    3019       130088 :         for (int i = 0; i < nvaluesHash; i++)
    3020              :         {
    3021       128800 :             bool        found = false;
    3022       128800 :             MCVHashEntry *entry = MCVHashTable_insert(hashTable,
    3023       128800 :                                                       statsHash->values[i],
    3024              :                                                       &found);
    3025              : 
    3026              :             /*
    3027              :              * MCVHashTable_insert will only report "found" if the new value
    3028              :              * is equal to some previous one per datum_image_eq().  That
    3029              :              * probably shouldn't happen, since we're not expecting duplicates
    3030              :              * in the MCV list.  If we do find a dup, just ignore it, leaving
    3031              :              * the hash entry's index pointing at the first occurrence.  That
    3032              :              * matches the behavior that the non-hashed code path would have.
    3033              :              */
    3034       128800 :             if (likely(!found))
    3035       128800 :                 entry->index = i;
    3036              :         }
    3037              : 
    3038              :         /*
    3039              :          * Prepare to probe the hash table.  If the probe values are of a
    3040              :          * different data type, then we need to change hash functions.  (This
    3041              :          * code relies on the assumption that since we defined SH_STORE_HASH,
    3042              :          * simplehash.h will never need to compute hash values for existing
    3043              :          * hash table entries.)
    3044              :          */
    3045         1288 :         hashContext.insert_mode = false;
    3046         1288 :         if (hashLeft != hashRight)
    3047              :         {
    3048            0 :             fmgr_info(op_is_reversed ? hashRight : hashLeft, &hash_proc);
    3049              :             /* Resetting hash_fcinfo is probably unnecessary, but be safe */
    3050            0 :             InitFunctionCallInfoData(*hash_fcinfo, &hash_proc, 1, collation,
    3051              :                                      NULL, NULL);
    3052            0 :             hash_fcinfo->args[0].isnull = false;
    3053              :         }
    3054              : 
    3055              :         /* Look up each probe value in turn. */
    3056       130088 :         for (int i = 0; i < nvaluesProbe; i++)
    3057              :         {
    3058       128800 :             MCVHashEntry *entry = MCVHashTable_lookup(hashTable,
    3059       128800 :                                                       statsProbe->values[i]);
    3060              : 
    3061              :             /* As in the other code path, skip already-matched hash entries */
    3062       128800 :             if (entry != NULL && !hasMatchHash[entry->index])
    3063              :             {
    3064        54186 :                 hasMatchHash[entry->index] = hasMatchProbe[i] = true;
    3065        54186 :                 nmatches++;
    3066        54186 :                 matchprodfreq += statsHash->numbers[entry->index] * statsProbe->numbers[i];
    3067              :             }
    3068              :         }
    3069              : 
    3070         1288 :         MCVHashTable_destroy(hashTable);
    3071              :     }
    3072              :     else
    3073              :     {
    3074              :         /* We're not to use hashing, so do it the O(N^2) way */
    3075              :         int         index1,
    3076              :                     index2;
    3077              : 
    3078              :         /* Set up to supply the values in the order the operator expects */
    3079        21266 :         if (op_is_reversed)
    3080              :         {
    3081            0 :             index1 = 1;
    3082            0 :             index2 = 0;
    3083              :         }
    3084              :         else
    3085              :         {
    3086        21266 :             index1 = 0;
    3087        21266 :             index2 = 1;
    3088              :         }
    3089              : 
    3090       396505 :         for (int i = 0; i < nvalues1; i++)
    3091              :         {
    3092       375239 :             fcinfo->args[index1].value = sslot1->values[i];
    3093              : 
    3094      8000408 :             for (int j = 0; j < nvalues2; j++)
    3095              :             {
    3096              :                 Datum       fresult;
    3097              : 
    3098      7805736 :                 if (hasmatch2[j])
    3099      2471443 :                     continue;
    3100      5334293 :                 fcinfo->args[index2].value = sslot2->values[j];
    3101      5334293 :                 fcinfo->isnull = false;
    3102      5334293 :                 fresult = FunctionCallInvoke(fcinfo);
    3103      5334293 :                 if (!fcinfo->isnull && DatumGetBool(fresult))
    3104              :                 {
    3105       180567 :                     hasmatch1[i] = hasmatch2[j] = true;
    3106       180567 :                     matchprodfreq += sslot1->numbers[i] * sslot2->numbers[j];
    3107       180567 :                     nmatches++;
    3108       180567 :                     break;
    3109              :                 }
    3110              :             }
    3111              :         }
    3112              :     }
    3113              : 
    3114        22554 :     *p_nmatches = nmatches;
    3115        22554 :     *p_matchprodfreq = matchprodfreq;
    3116        22554 : }
    3117              : 
    3118              : /*
    3119              :  * Support functions for the hash tables used by eqjoinsel_find_matches
    3120              :  */
    3121              : static uint32
    3122       257600 : hash_mcv(MCVHashTable_hash *tab, Datum key)
    3123              : {
    3124       257600 :     MCVHashContext *context = (MCVHashContext *) tab->private_data;
    3125       257600 :     FunctionCallInfo fcinfo = context->hash_fcinfo;
    3126              :     Datum       fresult;
    3127              : 
    3128       257600 :     fcinfo->args[0].value = key;
    3129       257600 :     fcinfo->isnull = false;
    3130       257600 :     fresult = FunctionCallInvoke(fcinfo);
    3131              :     Assert(!fcinfo->isnull);
    3132       257600 :     return DatumGetUInt32(fresult);
    3133              : }
    3134              : 
    3135              : static bool
    3136        54186 : mcvs_equal(MCVHashTable_hash *tab, Datum key0, Datum key1)
    3137              : {
    3138        54186 :     MCVHashContext *context = (MCVHashContext *) tab->private_data;
    3139              : 
    3140        54186 :     if (context->insert_mode)
    3141              :     {
    3142              :         /*
    3143              :          * During the insertion step, any comparisons will be between two
    3144              :          * Datums of the hash table's data type, so if the given operator is
    3145              :          * cross-type it will be the wrong thing to use.  Fortunately, we can
    3146              :          * use datum_image_eq instead.  The MCV values should all be distinct
    3147              :          * anyway, so it's mostly pro-forma to compare them at all.
    3148              :          */
    3149            0 :         return datum_image_eq(key0, key1,
    3150            0 :                               context->hash_typbyval, context->hash_typlen);
    3151              :     }
    3152              :     else
    3153              :     {
    3154        54186 :         FunctionCallInfo fcinfo = context->equal_fcinfo;
    3155              :         Datum       fresult;
    3156              : 
    3157              :         /*
    3158              :          * Apply the operator the correct way around.  Although simplehash.h
    3159              :          * doesn't document this explicitly, during lookups key0 is from the
    3160              :          * hash table while key1 is the probe value, so we should compare them
    3161              :          * in that order only if op_is_reversed.
    3162              :          */
    3163        54186 :         if (context->op_is_reversed)
    3164              :         {
    3165            0 :             fcinfo->args[0].value = key0;
    3166            0 :             fcinfo->args[1].value = key1;
    3167              :         }
    3168              :         else
    3169              :         {
    3170        54186 :             fcinfo->args[0].value = key1;
    3171        54186 :             fcinfo->args[1].value = key0;
    3172              :         }
    3173        54186 :         fcinfo->isnull = false;
    3174        54186 :         fresult = FunctionCallInvoke(fcinfo);
    3175        54186 :         return (!fcinfo->isnull && DatumGetBool(fresult));
    3176              :     }
    3177              : }
    3178              : 
    3179              : /*
    3180              :  *      neqjoinsel      - Join selectivity of "!="
    3181              :  */
    3182              : Datum
    3183         2938 : neqjoinsel(PG_FUNCTION_ARGS)
    3184              : {
    3185         2938 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
    3186         2938 :     Oid         operator = PG_GETARG_OID(1);
    3187         2938 :     List       *args = (List *) PG_GETARG_POINTER(2);
    3188         2938 :     JoinType    jointype = (JoinType) PG_GETARG_INT16(3);
    3189         2938 :     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
    3190         2938 :     Oid         collation = PG_GET_COLLATION();
    3191              :     float8      result;
    3192              : 
    3193         2938 :     if (jointype == JOIN_SEMI || jointype == JOIN_ANTI)
    3194         1015 :     {
    3195              :         /*
    3196              :          * For semi-joins, if there is more than one distinct value in the RHS
    3197              :          * relation then every non-null LHS row must find a row to join since
    3198              :          * it can only be equal to one of them.  We'll assume that there is
    3199              :          * always more than one distinct RHS value for the sake of stability,
    3200              :          * though in theory we could have special cases for empty RHS
    3201              :          * (selectivity = 0) and single-distinct-value RHS (selectivity =
    3202              :          * fraction of LHS that has the same value as the single RHS value).
    3203              :          *
    3204              :          * For anti-joins, if we use the same assumption that there is more
    3205              :          * than one distinct key in the RHS relation, then every non-null LHS
    3206              :          * row must be suppressed by the anti-join.
    3207              :          *
    3208              :          * So either way, the selectivity estimate should be 1 - nullfrac.
    3209              :          */
    3210              :         VariableStatData leftvar;
    3211              :         VariableStatData rightvar;
    3212              :         bool        reversed;
    3213              :         HeapTuple   statsTuple;
    3214              :         double      nullfrac;
    3215              : 
    3216         1015 :         get_join_variables(root, args, sjinfo, &leftvar, &rightvar, &reversed);
    3217         1015 :         statsTuple = reversed ? rightvar.statsTuple : leftvar.statsTuple;
    3218         1015 :         if (HeapTupleIsValid(statsTuple))
    3219          838 :             nullfrac = ((Form_pg_statistic) GETSTRUCT(statsTuple))->stanullfrac;
    3220              :         else
    3221          177 :             nullfrac = 0.0;
    3222         1015 :         ReleaseVariableStats(leftvar);
    3223         1015 :         ReleaseVariableStats(rightvar);
    3224              : 
    3225         1015 :         result = 1.0 - nullfrac;
    3226              :     }
    3227              :     else
    3228              :     {
    3229              :         /*
    3230              :          * We want 1 - eqjoinsel() where the equality operator is the one
    3231              :          * associated with this != operator, that is, its negator.
    3232              :          */
    3233         1923 :         Oid         eqop = get_negator(operator);
    3234              : 
    3235         1923 :         if (eqop)
    3236              :         {
    3237              :             result =
    3238         1923 :                 DatumGetFloat8(DirectFunctionCall5Coll(eqjoinsel,
    3239              :                                                        collation,
    3240              :                                                        PointerGetDatum(root),
    3241              :                                                        ObjectIdGetDatum(eqop),
    3242              :                                                        PointerGetDatum(args),
    3243              :                                                        Int16GetDatum(jointype),
    3244              :                                                        PointerGetDatum(sjinfo)));
    3245              :         }
    3246              :         else
    3247              :         {
    3248              :             /* Use default selectivity (should we raise an error instead?) */
    3249            0 :             result = DEFAULT_EQ_SEL;
    3250              :         }
    3251         1923 :         result = 1.0 - result;
    3252              :     }
    3253              : 
    3254         2938 :     PG_RETURN_FLOAT8(result);
    3255              : }
    3256              : 
    3257              : /*
    3258              :  *      scalarltjoinsel - Join selectivity of "<" for scalars
    3259              :  */
    3260              : Datum
    3261          270 : scalarltjoinsel(PG_FUNCTION_ARGS)
    3262              : {
    3263          270 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    3264              : }
    3265              : 
    3266              : /*
    3267              :  *      scalarlejoinsel - Join selectivity of "<=" for scalars
    3268              :  */
    3269              : Datum
    3270          198 : scalarlejoinsel(PG_FUNCTION_ARGS)
    3271              : {
    3272          198 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    3273              : }
    3274              : 
    3275              : /*
    3276              :  *      scalargtjoinsel - Join selectivity of ">" for scalars
    3277              :  */
    3278              : Datum
    3279          240 : scalargtjoinsel(PG_FUNCTION_ARGS)
    3280              : {
    3281          240 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    3282              : }
    3283              : 
    3284              : /*
    3285              :  *      scalargejoinsel - Join selectivity of ">=" for scalars
    3286              :  */
    3287              : Datum
    3288          152 : scalargejoinsel(PG_FUNCTION_ARGS)
    3289              : {
    3290          152 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    3291              : }
    3292              : 
    3293              : 
    3294              : /*
    3295              :  * mergejoinscansel         - Scan selectivity of merge join.
    3296              :  *
    3297              :  * A merge join will stop as soon as it exhausts either input stream.
    3298              :  * Therefore, if we can estimate the ranges of both input variables,
    3299              :  * we can estimate how much of the input will actually be read.  This
    3300              :  * can have a considerable impact on the cost when using indexscans.
    3301              :  *
    3302              :  * Also, we can estimate how much of each input has to be read before the
    3303              :  * first join pair is found, which will affect the join's startup time.
    3304              :  *
    3305              :  * clause should be a clause already known to be mergejoinable.  opfamily,
    3306              :  * cmptype, and nulls_first specify the sort ordering being used.
    3307              :  *
    3308              :  * The outputs are:
    3309              :  *      *leftstart is set to the fraction of the left-hand variable expected
    3310              :  *       to be scanned before the first join pair is found (0 to 1).
    3311              :  *      *leftend is set to the fraction of the left-hand variable expected
    3312              :  *       to be scanned before the join terminates (0 to 1).
    3313              :  *      *rightstart, *rightend similarly for the right-hand variable.
    3314              :  */
    3315              : void
    3316       102851 : mergejoinscansel(PlannerInfo *root, Node *clause,
    3317              :                  Oid opfamily, CompareType cmptype, bool nulls_first,
    3318              :                  Selectivity *leftstart, Selectivity *leftend,
    3319              :                  Selectivity *rightstart, Selectivity *rightend)
    3320              : {
    3321              :     Node       *left,
    3322              :                *right;
    3323              :     VariableStatData leftvar,
    3324              :                 rightvar;
    3325              :     Oid         opmethod;
    3326              :     int         op_strategy;
    3327              :     Oid         op_lefttype;
    3328              :     Oid         op_righttype;
    3329              :     Oid         opno,
    3330              :                 collation,
    3331              :                 lsortop,
    3332              :                 rsortop,
    3333              :                 lstatop,
    3334              :                 rstatop,
    3335              :                 ltop,
    3336              :                 leop,
    3337              :                 revltop,
    3338              :                 revleop;
    3339              :     StrategyNumber ltstrat,
    3340              :                 lestrat,
    3341              :                 gtstrat,
    3342              :                 gestrat;
    3343              :     bool        isgt;
    3344              :     Datum       leftmin,
    3345              :                 leftmax,
    3346              :                 rightmin,
    3347              :                 rightmax;
    3348              :     double      selec;
    3349              : 
    3350              :     /* Set default results if we can't figure anything out. */
    3351              :     /* XXX should default "start" fraction be a bit more than 0? */
    3352       102851 :     *leftstart = *rightstart = 0.0;
    3353       102851 :     *leftend = *rightend = 1.0;
    3354              : 
    3355              :     /* Deconstruct the merge clause */
    3356       102851 :     if (!is_opclause(clause))
    3357            0 :         return;                 /* shouldn't happen */
    3358       102851 :     opno = ((OpExpr *) clause)->opno;
    3359       102851 :     collation = ((OpExpr *) clause)->inputcollid;
    3360       102851 :     left = get_leftop((Expr *) clause);
    3361       102851 :     right = get_rightop((Expr *) clause);
    3362       102851 :     if (!right)
    3363            0 :         return;                 /* shouldn't happen */
    3364              : 
    3365              :     /* Look for stats for the inputs */
    3366       102851 :     examine_variable(root, left, 0, &leftvar);
    3367       102851 :     examine_variable(root, right, 0, &rightvar);
    3368              : 
    3369       102851 :     opmethod = get_opfamily_method(opfamily);
    3370              : 
    3371              :     /* Extract the operator's declared left/right datatypes */
    3372       102851 :     get_op_opfamily_properties(opno, opfamily, false,
    3373              :                                &op_strategy,
    3374              :                                &op_lefttype,
    3375              :                                &op_righttype);
    3376              :     Assert(IndexAmTranslateStrategy(op_strategy, opmethod, opfamily, true) == COMPARE_EQ);
    3377              : 
    3378              :     /*
    3379              :      * Look up the various operators we need.  If we don't find them all, it
    3380              :      * probably means the opfamily is broken, but we just fail silently.
    3381              :      *
    3382              :      * Note: we expect that pg_statistic histograms will be sorted by the '<'
    3383              :      * operator, regardless of which sort direction we are considering.
    3384              :      */
    3385       102851 :     switch (cmptype)
    3386              :     {
    3387       102822 :         case COMPARE_LT:
    3388       102822 :             isgt = false;
    3389       102822 :             ltstrat = IndexAmTranslateCompareType(COMPARE_LT, opmethod, opfamily, true);
    3390       102822 :             lestrat = IndexAmTranslateCompareType(COMPARE_LE, opmethod, opfamily, true);
    3391       102822 :             if (op_lefttype == op_righttype)
    3392              :             {
    3393              :                 /* easy case */
    3394       101652 :                 ltop = get_opfamily_member(opfamily,
    3395              :                                            op_lefttype, op_righttype,
    3396              :                                            ltstrat);
    3397       101652 :                 leop = get_opfamily_member(opfamily,
    3398              :                                            op_lefttype, op_righttype,
    3399              :                                            lestrat);
    3400       101652 :                 lsortop = ltop;
    3401       101652 :                 rsortop = ltop;
    3402       101652 :                 lstatop = lsortop;
    3403       101652 :                 rstatop = rsortop;
    3404       101652 :                 revltop = ltop;
    3405       101652 :                 revleop = leop;
    3406              :             }
    3407              :             else
    3408              :             {
    3409         1170 :                 ltop = get_opfamily_member(opfamily,
    3410              :                                            op_lefttype, op_righttype,
    3411              :                                            ltstrat);
    3412         1170 :                 leop = get_opfamily_member(opfamily,
    3413              :                                            op_lefttype, op_righttype,
    3414              :                                            lestrat);
    3415         1170 :                 lsortop = get_opfamily_member(opfamily,
    3416              :                                               op_lefttype, op_lefttype,
    3417              :                                               ltstrat);
    3418         1170 :                 rsortop = get_opfamily_member(opfamily,
    3419              :                                               op_righttype, op_righttype,
    3420              :                                               ltstrat);
    3421         1170 :                 lstatop = lsortop;
    3422         1170 :                 rstatop = rsortop;
    3423         1170 :                 revltop = get_opfamily_member(opfamily,
    3424              :                                               op_righttype, op_lefttype,
    3425              :                                               ltstrat);
    3426         1170 :                 revleop = get_opfamily_member(opfamily,
    3427              :                                               op_righttype, op_lefttype,
    3428              :                                               lestrat);
    3429              :             }
    3430       102822 :             break;
    3431           29 :         case COMPARE_GT:
    3432              :             /* descending-order case */
    3433           29 :             isgt = true;
    3434           29 :             ltstrat = IndexAmTranslateCompareType(COMPARE_LT, opmethod, opfamily, true);
    3435           29 :             gtstrat = IndexAmTranslateCompareType(COMPARE_GT, opmethod, opfamily, true);
    3436           29 :             gestrat = IndexAmTranslateCompareType(COMPARE_GE, opmethod, opfamily, true);
    3437           29 :             if (op_lefttype == op_righttype)
    3438              :             {
    3439              :                 /* easy case */
    3440           29 :                 ltop = get_opfamily_member(opfamily,
    3441              :                                            op_lefttype, op_righttype,
    3442              :                                            gtstrat);
    3443           29 :                 leop = get_opfamily_member(opfamily,
    3444              :                                            op_lefttype, op_righttype,
    3445              :                                            gestrat);
    3446           29 :                 lsortop = ltop;
    3447           29 :                 rsortop = ltop;
    3448           29 :                 lstatop = get_opfamily_member(opfamily,
    3449              :                                               op_lefttype, op_lefttype,
    3450              :                                               ltstrat);
    3451           29 :                 rstatop = lstatop;
    3452           29 :                 revltop = ltop;
    3453           29 :                 revleop = leop;
    3454              :             }
    3455              :             else
    3456              :             {
    3457            0 :                 ltop = get_opfamily_member(opfamily,
    3458              :                                            op_lefttype, op_righttype,
    3459              :                                            gtstrat);
    3460            0 :                 leop = get_opfamily_member(opfamily,
    3461              :                                            op_lefttype, op_righttype,
    3462              :                                            gestrat);
    3463            0 :                 lsortop = get_opfamily_member(opfamily,
    3464              :                                               op_lefttype, op_lefttype,
    3465              :                                               gtstrat);
    3466            0 :                 rsortop = get_opfamily_member(opfamily,
    3467              :                                               op_righttype, op_righttype,
    3468              :                                               gtstrat);
    3469            0 :                 lstatop = get_opfamily_member(opfamily,
    3470              :                                               op_lefttype, op_lefttype,
    3471              :                                               ltstrat);
    3472            0 :                 rstatop = get_opfamily_member(opfamily,
    3473              :                                               op_righttype, op_righttype,
    3474              :                                               ltstrat);
    3475            0 :                 revltop = get_opfamily_member(opfamily,
    3476              :                                               op_righttype, op_lefttype,
    3477              :                                               gtstrat);
    3478            0 :                 revleop = get_opfamily_member(opfamily,
    3479              :                                               op_righttype, op_lefttype,
    3480              :                                               gestrat);
    3481              :             }
    3482           29 :             break;
    3483            0 :         default:
    3484            0 :             goto fail;          /* shouldn't get here */
    3485              :     }
    3486              : 
    3487       102851 :     if (!OidIsValid(lsortop) ||
    3488       102851 :         !OidIsValid(rsortop) ||
    3489       102851 :         !OidIsValid(lstatop) ||
    3490       102851 :         !OidIsValid(rstatop) ||
    3491       102841 :         !OidIsValid(ltop) ||
    3492       102841 :         !OidIsValid(leop) ||
    3493       102841 :         !OidIsValid(revltop) ||
    3494              :         !OidIsValid(revleop))
    3495           10 :         goto fail;              /* insufficient info in catalogs */
    3496              : 
    3497              :     /* Try to get ranges of both inputs */
    3498       102841 :     if (!isgt)
    3499              :     {
    3500       102812 :         if (!get_variable_range(root, &leftvar, lstatop, collation,
    3501              :                                 &leftmin, &leftmax))
    3502        32240 :             goto fail;          /* no range available from stats */
    3503        70572 :         if (!get_variable_range(root, &rightvar, rstatop, collation,
    3504              :                                 &rightmin, &rightmax))
    3505        17687 :             goto fail;          /* no range available from stats */
    3506              :     }
    3507              :     else
    3508              :     {
    3509              :         /* need to swap the max and min */
    3510           29 :         if (!get_variable_range(root, &leftvar, lstatop, collation,
    3511              :                                 &leftmax, &leftmin))
    3512           24 :             goto fail;          /* no range available from stats */
    3513            5 :         if (!get_variable_range(root, &rightvar, rstatop, collation,
    3514              :                                 &rightmax, &rightmin))
    3515            0 :             goto fail;          /* no range available from stats */
    3516              :     }
    3517              : 
    3518              :     /*
    3519              :      * Now, the fraction of the left variable that will be scanned is the
    3520              :      * fraction that's <= the right-side maximum value.  But only believe
    3521              :      * non-default estimates, else stick with our 1.0.
    3522              :      */
    3523        52890 :     selec = scalarineqsel(root, leop, isgt, true, collation, &leftvar,
    3524              :                           rightmax, op_righttype);
    3525        52890 :     if (selec != DEFAULT_INEQ_SEL)
    3526        52886 :         *leftend = selec;
    3527              : 
    3528              :     /* And similarly for the right variable. */
    3529        52890 :     selec = scalarineqsel(root, revleop, isgt, true, collation, &rightvar,
    3530              :                           leftmax, op_lefttype);
    3531        52890 :     if (selec != DEFAULT_INEQ_SEL)
    3532        52890 :         *rightend = selec;
    3533              : 
    3534              :     /*
    3535              :      * Only one of the two "end" fractions can really be less than 1.0;
    3536              :      * believe the smaller estimate and reset the other one to exactly 1.0. If
    3537              :      * we get exactly equal estimates (as can easily happen with self-joins),
    3538              :      * believe neither.
    3539              :      */
    3540        52890 :     if (*leftend > *rightend)
    3541        16396 :         *leftend = 1.0;
    3542        36494 :     else if (*leftend < *rightend)
    3543        20350 :         *rightend = 1.0;
    3544              :     else
    3545        16144 :         *leftend = *rightend = 1.0;
    3546              : 
    3547              :     /*
    3548              :      * Also, the fraction of the left variable that will be scanned before the
    3549              :      * first join pair is found is the fraction that's < the right-side
    3550              :      * minimum value.  But only believe non-default estimates, else stick with
    3551              :      * our own default.
    3552              :      */
    3553        52890 :     selec = scalarineqsel(root, ltop, isgt, false, collation, &leftvar,
    3554              :                           rightmin, op_righttype);
    3555        52890 :     if (selec != DEFAULT_INEQ_SEL)
    3556        52890 :         *leftstart = selec;
    3557              : 
    3558              :     /* And similarly for the right variable. */
    3559        52890 :     selec = scalarineqsel(root, revltop, isgt, false, collation, &rightvar,
    3560              :                           leftmin, op_lefttype);
    3561        52890 :     if (selec != DEFAULT_INEQ_SEL)
    3562        52890 :         *rightstart = selec;
    3563              : 
    3564              :     /*
    3565              :      * Only one of the two "start" fractions can really be more than zero;
    3566              :      * believe the larger estimate and reset the other one to exactly 0.0. If
    3567              :      * we get exactly equal estimates (as can easily happen with self-joins),
    3568              :      * believe neither.
    3569              :      */
    3570        52890 :     if (*leftstart < *rightstart)
    3571        10669 :         *leftstart = 0.0;
    3572        42221 :     else if (*leftstart > *rightstart)
    3573        15362 :         *rightstart = 0.0;
    3574              :     else
    3575        26859 :         *leftstart = *rightstart = 0.0;
    3576              : 
    3577              :     /*
    3578              :      * If the sort order is nulls-first, we're going to have to skip over any
    3579              :      * nulls too.  These would not have been counted by scalarineqsel, and we
    3580              :      * can safely add in this fraction regardless of whether we believe
    3581              :      * scalarineqsel's results or not.  But be sure to clamp the sum to 1.0!
    3582              :      */
    3583        52890 :     if (nulls_first)
    3584              :     {
    3585              :         Form_pg_statistic stats;
    3586              : 
    3587            5 :         if (HeapTupleIsValid(leftvar.statsTuple))
    3588              :         {
    3589            5 :             stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
    3590            5 :             *leftstart += stats->stanullfrac;
    3591            5 :             CLAMP_PROBABILITY(*leftstart);
    3592            5 :             *leftend += stats->stanullfrac;
    3593            5 :             CLAMP_PROBABILITY(*leftend);
    3594              :         }
    3595            5 :         if (HeapTupleIsValid(rightvar.statsTuple))
    3596              :         {
    3597            5 :             stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
    3598            5 :             *rightstart += stats->stanullfrac;
    3599            5 :             CLAMP_PROBABILITY(*rightstart);
    3600            5 :             *rightend += stats->stanullfrac;
    3601            5 :             CLAMP_PROBABILITY(*rightend);
    3602              :         }
    3603              :     }
    3604              : 
    3605              :     /* Disbelieve start >= end, just in case that can happen */
    3606        52890 :     if (*leftstart >= *leftend)
    3607              :     {
    3608          117 :         *leftstart = 0.0;
    3609          117 :         *leftend = 1.0;
    3610              :     }
    3611        52890 :     if (*rightstart >= *rightend)
    3612              :     {
    3613          784 :         *rightstart = 0.0;
    3614          784 :         *rightend = 1.0;
    3615              :     }
    3616              : 
    3617        52106 : fail:
    3618       102851 :     ReleaseVariableStats(leftvar);
    3619       102851 :     ReleaseVariableStats(rightvar);
    3620              : }
    3621              : 
    3622              : 
    3623              : /*
    3624              :  *  matchingsel -- generic matching-operator selectivity support
    3625              :  *
    3626              :  * Use these for any operators that (a) are on data types for which we collect
    3627              :  * standard statistics, and (b) have behavior for which the default estimate
    3628              :  * (twice DEFAULT_EQ_SEL) is sane.  Typically that is good for match-like
    3629              :  * operators.
    3630              :  */
    3631              : 
    3632              : Datum
    3633          845 : matchingsel(PG_FUNCTION_ARGS)
    3634              : {
    3635          845 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
    3636          845 :     Oid         operator = PG_GETARG_OID(1);
    3637          845 :     List       *args = (List *) PG_GETARG_POINTER(2);
    3638          845 :     int         varRelid = PG_GETARG_INT32(3);
    3639          845 :     Oid         collation = PG_GET_COLLATION();
    3640              :     double      selec;
    3641              : 
    3642              :     /* Use generic restriction selectivity logic. */
    3643          845 :     selec = generic_restriction_selectivity(root, operator, collation,
    3644              :                                             args, varRelid,
    3645              :                                             DEFAULT_MATCHING_SEL);
    3646              : 
    3647          845 :     PG_RETURN_FLOAT8((float8) selec);
    3648              : }
    3649              : 
    3650              : Datum
    3651            5 : matchingjoinsel(PG_FUNCTION_ARGS)
    3652              : {
    3653              :     /* Just punt, for the moment. */
    3654            5 :     PG_RETURN_FLOAT8(DEFAULT_MATCHING_SEL);
    3655              : }
    3656              : 
    3657              : 
    3658              : /*
    3659              :  * Helper routine for estimate_num_groups: add an item to a list of
    3660              :  * GroupVarInfos, but only if it's not known equal to any of the existing
    3661              :  * entries.
    3662              :  */
    3663              : typedef struct
    3664              : {
    3665              :     Node       *var;            /* might be an expression, not just a Var */
    3666              :     RelOptInfo *rel;            /* relation it belongs to */
    3667              :     double      ndistinct;      /* # distinct values */
    3668              :     bool        isdefault;      /* true if DEFAULT_NUM_DISTINCT was used */
    3669              : } GroupVarInfo;
    3670              : 
    3671              : static List *
    3672       282490 : add_unique_group_var(PlannerInfo *root, List *varinfos,
    3673              :                      Node *var, VariableStatData *vardata)
    3674              : {
    3675              :     GroupVarInfo *varinfo;
    3676              :     double      ndistinct;
    3677              :     bool        isdefault;
    3678              :     ListCell   *lc;
    3679              : 
    3680       282490 :     ndistinct = get_variable_numdistinct(vardata, &isdefault);
    3681              : 
    3682              :     /*
    3683              :      * The nullingrels bits within the var could cause the same var to be
    3684              :      * counted multiple times if it's marked with different nullingrels.  They
    3685              :      * could also prevent us from matching the var to the expressions in
    3686              :      * extended statistics (see estimate_multivariate_ndistinct).  So strip
    3687              :      * them out first.
    3688              :      */
    3689       282490 :     var = remove_nulling_relids(var, root->outer_join_rels, NULL);
    3690              : 
    3691       348626 :     foreach(lc, varinfos)
    3692              :     {
    3693        66915 :         varinfo = (GroupVarInfo *) lfirst(lc);
    3694              : 
    3695              :         /* Drop exact duplicates */
    3696        66915 :         if (equal(var, varinfo->var))
    3697          779 :             return varinfos;
    3698              : 
    3699              :         /*
    3700              :          * Drop known-equal vars, but only if they belong to different
    3701              :          * relations (see comments for estimate_num_groups).  We aren't too
    3702              :          * fussy about the semantics of "equal" here.
    3703              :          */
    3704        70520 :         if (vardata->rel != varinfo->rel &&
    3705         4190 :             exprs_known_equal(root, var, varinfo->var, InvalidOid))
    3706              :         {
    3707          210 :             if (varinfo->ndistinct <= ndistinct)
    3708              :             {
    3709              :                 /* Keep older item, forget new one */
    3710          194 :                 return varinfos;
    3711              :             }
    3712              :             else
    3713              :             {
    3714              :                 /* Delete the older item */
    3715           16 :                 varinfos = foreach_delete_current(varinfos, lc);
    3716              :             }
    3717              :         }
    3718              :     }
    3719              : 
    3720       281711 :     varinfo = palloc_object(GroupVarInfo);
    3721              : 
    3722       281711 :     varinfo->var = var;
    3723       281711 :     varinfo->rel = vardata->rel;
    3724       281711 :     varinfo->ndistinct = ndistinct;
    3725       281711 :     varinfo->isdefault = isdefault;
    3726       281711 :     varinfos = lappend(varinfos, varinfo);
    3727       281711 :     return varinfos;
    3728              : }
    3729              : 
    3730              : /*
    3731              :  * estimate_num_groups      - Estimate number of groups in a grouped query
    3732              :  *
    3733              :  * Given a query having a GROUP BY clause, estimate how many groups there
    3734              :  * will be --- ie, the number of distinct combinations of the GROUP BY
    3735              :  * expressions.
    3736              :  *
    3737              :  * This routine is also used to estimate the number of rows emitted by
    3738              :  * a DISTINCT filtering step; that is an isomorphic problem.  (Note:
    3739              :  * actually, we only use it for DISTINCT when there's no grouping or
    3740              :  * aggregation ahead of the DISTINCT.)
    3741              :  *
    3742              :  * Inputs:
    3743              :  *  root - the query
    3744              :  *  groupExprs - list of expressions being grouped by
    3745              :  *  input_rows - number of rows estimated to arrive at the group/unique
    3746              :  *      filter step
    3747              :  *  pgset - NULL, or a List** pointing to a grouping set to filter the
    3748              :  *      groupExprs against
    3749              :  *
    3750              :  * Outputs:
    3751              :  *  estinfo - When passed as non-NULL, the function will set bits in the
    3752              :  *      "flags" field in order to provide callers with additional information
    3753              :  *      about the estimation.  Currently, we only set the SELFLAG_USED_DEFAULT
    3754              :  *      bit if we used any default values in the estimation.
    3755              :  *
    3756              :  * Given the lack of any cross-correlation statistics in the system, it's
    3757              :  * impossible to do anything really trustworthy with GROUP BY conditions
    3758              :  * involving multiple Vars.  We should however avoid assuming the worst
    3759              :  * case (all possible cross-product terms actually appear as groups) since
    3760              :  * very often the grouped-by Vars are highly correlated.  Our current approach
    3761              :  * is as follows:
    3762              :  *  1.  Expressions yielding boolean are assumed to contribute two groups,
    3763              :  *      independently of their content, and are ignored in the subsequent
    3764              :  *      steps.  This is mainly because tests like "col IS NULL" break the
    3765              :  *      heuristic used in step 2 especially badly.
    3766              :  *  2.  Reduce the given expressions to a list of unique Vars used.  For
    3767              :  *      example, GROUP BY a, a + b is treated the same as GROUP BY a, b.
    3768              :  *      It is clearly correct not to count the same Var more than once.
    3769              :  *      It is also reasonable to treat f(x) the same as x: f() cannot
    3770              :  *      increase the number of distinct values (unless it is volatile,
    3771              :  *      which we consider unlikely for grouping), but it probably won't
    3772              :  *      reduce the number of distinct values much either.
    3773              :  *      As a special case, if a GROUP BY expression can be matched to an
    3774              :  *      expressional index for which we have statistics, then we treat the
    3775              :  *      whole expression as though it were just a Var.
    3776              :  *  3.  If the list contains Vars of different relations that are known equal
    3777              :  *      due to equivalence classes, then drop all but one of the Vars from each
    3778              :  *      known-equal set, keeping the one with smallest estimated # of values
    3779              :  *      (since the extra values of the others can't appear in joined rows).
    3780              :  *      Note the reason we only consider Vars of different relations is that
    3781              :  *      if we considered ones of the same rel, we'd be double-counting the
    3782              :  *      restriction selectivity of the equality in the next step.
    3783              :  *  4.  For Vars within a single source rel, we multiply together the numbers
    3784              :  *      of values, clamp to the number of rows in the rel (divided by 10 if
    3785              :  *      more than one Var), and then multiply by a factor based on the
    3786              :  *      selectivity of the restriction clauses for that rel.  When there's
    3787              :  *      more than one Var, the initial product is probably too high (it's the
    3788              :  *      worst case) but clamping to a fraction of the rel's rows seems to be a
    3789              :  *      helpful heuristic for not letting the estimate get out of hand.  (The
    3790              :  *      factor of 10 is derived from pre-Postgres-7.4 practice.)  The factor
    3791              :  *      we multiply by to adjust for the restriction selectivity assumes that
    3792              :  *      the restriction clauses are independent of the grouping, which may not
    3793              :  *      be a valid assumption, but it's hard to do better.
    3794              :  *  5.  If there are Vars from multiple rels, we repeat step 4 for each such
    3795              :  *      rel, and multiply the results together.
    3796              :  * Note that rels not containing grouped Vars are ignored completely, as are
    3797              :  * join clauses.  Such rels cannot increase the number of groups, and we
    3798              :  * assume such clauses do not reduce the number either (somewhat bogus,
    3799              :  * but we don't have the info to do better).
    3800              :  */
    3801              : double
    3802       245375 : estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows,
    3803              :                     List **pgset, EstimationInfo *estinfo)
    3804              : {
    3805       245375 :     List       *varinfos = NIL;
    3806       245375 :     double      srf_multiplier = 1.0;
    3807              :     double      numdistinct;
    3808              :     ListCell   *l;
    3809              :     int         i;
    3810              : 
    3811              :     /* Zero the estinfo output parameter, if non-NULL */
    3812       245375 :     if (estinfo != NULL)
    3813       204384 :         memset(estinfo, 0, sizeof(EstimationInfo));
    3814              : 
    3815              :     /*
    3816              :      * We don't ever want to return an estimate of zero groups, as that tends
    3817              :      * to lead to division-by-zero and other unpleasantness.  The input_rows
    3818              :      * estimate is usually already at least 1, but clamp it just in case it
    3819              :      * isn't.
    3820              :      */
    3821       245375 :     input_rows = clamp_row_est(input_rows);
    3822              : 
    3823              :     /*
    3824              :      * If no grouping columns, there's exactly one group.  (This can't happen
    3825              :      * for normal cases with GROUP BY or DISTINCT, but it is possible for
    3826              :      * corner cases with set operations.)
    3827              :      */
    3828       245375 :     if (groupExprs == NIL || (pgset && *pgset == NIL))
    3829          924 :         return 1.0;
    3830              : 
    3831              :     /*
    3832              :      * Count groups derived from boolean grouping expressions.  For other
    3833              :      * expressions, find the unique Vars used, treating an expression as a Var
    3834              :      * if we can find stats for it.  For each one, record the statistical
    3835              :      * estimate of number of distinct values (total in its table, without
    3836              :      * regard for filtering).
    3837              :      */
    3838       244451 :     numdistinct = 1.0;
    3839              : 
    3840       244451 :     i = 0;
    3841       526315 :     foreach(l, groupExprs)
    3842              :     {
    3843       281904 :         Node       *groupexpr = (Node *) lfirst(l);
    3844              :         double      this_srf_multiplier;
    3845              :         VariableStatData vardata;
    3846              :         List       *varshere;
    3847              :         ListCell   *l2;
    3848              : 
    3849              :         /* is expression in this grouping set? */
    3850       281904 :         if (pgset && !list_member_int(*pgset, i++))
    3851       222443 :             continue;
    3852              : 
    3853              :         /*
    3854              :          * Set-returning functions in grouping columns are a bit problematic.
    3855              :          * The code below will effectively ignore their SRF nature and come up
    3856              :          * with a numdistinct estimate as though they were scalar functions.
    3857              :          * We compensate by scaling up the end result by the largest SRF
    3858              :          * rowcount estimate.  (This will be an overestimate if the SRF
    3859              :          * produces multiple copies of any output value, but it seems best to
    3860              :          * assume the SRF's outputs are distinct.  In any case, it's probably
    3861              :          * pointless to worry too much about this without much better
    3862              :          * estimates for SRF output rowcounts than we have today.)
    3863              :          */
    3864       281236 :         this_srf_multiplier = expression_returns_set_rows(root, groupexpr);
    3865       281236 :         if (srf_multiplier < this_srf_multiplier)
    3866          130 :             srf_multiplier = this_srf_multiplier;
    3867              : 
    3868              :         /* Short-circuit for expressions returning boolean */
    3869       281236 :         if (exprType(groupexpr) == BOOLOID)
    3870              :         {
    3871          292 :             numdistinct *= 2.0;
    3872          292 :             continue;
    3873              :         }
    3874              : 
    3875              :         /*
    3876              :          * If examine_variable is able to deduce anything about the GROUP BY
    3877              :          * expression, treat it as a single variable even if it's really more
    3878              :          * complicated.
    3879              :          *
    3880              :          * XXX This has the consequence that if there's a statistics object on
    3881              :          * the expression, we don't split it into individual Vars. This
    3882              :          * affects our selection of statistics in
    3883              :          * estimate_multivariate_ndistinct, because it's probably better to
    3884              :          * use more accurate estimate for each expression and treat them as
    3885              :          * independent, than to combine estimates for the extracted variables
    3886              :          * when we don't know how that relates to the expressions.
    3887              :          */
    3888       280944 :         examine_variable(root, groupexpr, 0, &vardata);
    3889       280944 :         if (HeapTupleIsValid(vardata.statsTuple) || vardata.isunique)
    3890              :         {
    3891       220985 :             varinfos = add_unique_group_var(root, varinfos,
    3892              :                                             groupexpr, &vardata);
    3893       220985 :             ReleaseVariableStats(vardata);
    3894       220985 :             continue;
    3895              :         }
    3896        59959 :         ReleaseVariableStats(vardata);
    3897              : 
    3898              :         /*
    3899              :          * Else pull out the component Vars.  Handle PlaceHolderVars by
    3900              :          * recursing into their arguments (effectively assuming that the
    3901              :          * PlaceHolderVar doesn't change the number of groups, which boils
    3902              :          * down to ignoring the possible addition of nulls to the result set).
    3903              :          */
    3904        59959 :         varshere = pull_var_clause(groupexpr,
    3905              :                                    PVC_RECURSE_AGGREGATES |
    3906              :                                    PVC_RECURSE_WINDOWFUNCS |
    3907              :                                    PVC_RECURSE_PLACEHOLDERS);
    3908              : 
    3909              :         /*
    3910              :          * If we find any variable-free GROUP BY item, then either it is a
    3911              :          * constant (and we can ignore it) or it contains a volatile function;
    3912              :          * in the latter case we punt and assume that each input row will
    3913              :          * yield a distinct group.
    3914              :          */
    3915        59959 :         if (varshere == NIL)
    3916              :         {
    3917          538 :             if (contain_volatile_functions(groupexpr))
    3918           40 :                 return input_rows;
    3919          498 :             continue;
    3920              :         }
    3921              : 
    3922              :         /*
    3923              :          * Else add variables to varinfos list
    3924              :          */
    3925       120926 :         foreach(l2, varshere)
    3926              :         {
    3927        61505 :             Node       *var = (Node *) lfirst(l2);
    3928              : 
    3929        61505 :             examine_variable(root, var, 0, &vardata);
    3930        61505 :             varinfos = add_unique_group_var(root, varinfos, var, &vardata);
    3931        61505 :             ReleaseVariableStats(vardata);
    3932              :         }
    3933              :     }
    3934              : 
    3935              :     /*
    3936              :      * If now no Vars, we must have an all-constant or all-boolean GROUP BY
    3937              :      * list.
    3938              :      */
    3939       244411 :     if (varinfos == NIL)
    3940              :     {
    3941              :         /* Apply SRF multiplier as we would do in the long path */
    3942          385 :         numdistinct *= srf_multiplier;
    3943              :         /* Round off */
    3944          385 :         numdistinct = ceil(numdistinct);
    3945              :         /* Guard against out-of-range answers */
    3946          385 :         if (numdistinct > input_rows)
    3947           71 :             numdistinct = input_rows;
    3948          385 :         if (numdistinct < 1.0)
    3949            0 :             numdistinct = 1.0;
    3950          385 :         return numdistinct;
    3951              :     }
    3952              : 
    3953              :     /*
    3954              :      * Group Vars by relation and estimate total numdistinct.
    3955              :      *
    3956              :      * For each iteration of the outer loop, we process the frontmost Var in
    3957              :      * varinfos, plus all other Vars in the same relation.  We remove these
    3958              :      * Vars from the newvarinfos list for the next iteration. This is the
    3959              :      * easiest way to group Vars of same rel together.
    3960              :      */
    3961              :     do
    3962              :     {
    3963       246194 :         GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
    3964       246194 :         RelOptInfo *rel = varinfo1->rel;
    3965       246194 :         double      reldistinct = 1;
    3966       246194 :         double      relmaxndistinct = reldistinct;
    3967       246194 :         int         relvarcount = 0;
    3968       246194 :         List       *newvarinfos = NIL;
    3969       246194 :         List       *relvarinfos = NIL;
    3970              : 
    3971              :         /*
    3972              :          * Split the list of varinfos in two - one for the current rel, one
    3973              :          * for remaining Vars on other rels.
    3974              :          */
    3975       246194 :         relvarinfos = lappend(relvarinfos, varinfo1);
    3976       284850 :         for_each_from(l, varinfos, 1)
    3977              :         {
    3978        38656 :             GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
    3979              : 
    3980        38656 :             if (varinfo2->rel == varinfo1->rel)
    3981              :             {
    3982              :                 /* varinfos on current rel */
    3983        35501 :                 relvarinfos = lappend(relvarinfos, varinfo2);
    3984              :             }
    3985              :             else
    3986              :             {
    3987              :                 /* not time to process varinfo2 yet */
    3988         3155 :                 newvarinfos = lappend(newvarinfos, varinfo2);
    3989              :             }
    3990              :         }
    3991              : 
    3992              :         /*
    3993              :          * Get the numdistinct estimate for the Vars of this rel.  We
    3994              :          * iteratively search for multivariate n-distinct with maximum number
    3995              :          * of vars; assuming that each var group is independent of the others,
    3996              :          * we multiply them together.  Any remaining relvarinfos after no more
    3997              :          * multivariate matches are found are assumed independent too, so
    3998              :          * their individual ndistinct estimates are multiplied also.
    3999              :          *
    4000              :          * While iterating, count how many separate numdistinct values we
    4001              :          * apply.  We apply a fudge factor below, but only if we multiplied
    4002              :          * more than one such values.
    4003              :          */
    4004       492493 :         while (relvarinfos)
    4005              :         {
    4006              :             double      mvndistinct;
    4007              : 
    4008       246299 :             if (estimate_multivariate_ndistinct(root, rel, &relvarinfos,
    4009              :                                                 &mvndistinct))
    4010              :             {
    4011          345 :                 reldistinct *= mvndistinct;
    4012          345 :                 if (relmaxndistinct < mvndistinct)
    4013          335 :                     relmaxndistinct = mvndistinct;
    4014          345 :                 relvarcount++;
    4015              :             }
    4016              :             else
    4017              :             {
    4018       526919 :                 foreach(l, relvarinfos)
    4019              :                 {
    4020       280965 :                     GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
    4021              : 
    4022       280965 :                     reldistinct *= varinfo2->ndistinct;
    4023       280965 :                     if (relmaxndistinct < varinfo2->ndistinct)
    4024       249696 :                         relmaxndistinct = varinfo2->ndistinct;
    4025       280965 :                     relvarcount++;
    4026              : 
    4027              :                     /*
    4028              :                      * When varinfo2's isdefault is set then we'd better set
    4029              :                      * the SELFLAG_USED_DEFAULT bit in the EstimationInfo.
    4030              :                      */
    4031       280965 :                     if (estinfo != NULL && varinfo2->isdefault)
    4032        18073 :                         estinfo->flags |= SELFLAG_USED_DEFAULT;
    4033              :                 }
    4034              : 
    4035              :                 /* we're done with this relation */
    4036       245954 :                 relvarinfos = NIL;
    4037              :             }
    4038              :         }
    4039              : 
    4040              :         /*
    4041              :          * Sanity check --- don't divide by zero if empty relation.
    4042              :          */
    4043              :         Assert(IS_SIMPLE_REL(rel));
    4044       246194 :         if (rel->tuples > 0)
    4045              :         {
    4046              :             /*
    4047              :              * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
    4048              :              * fudge factor is because the Vars are probably correlated but we
    4049              :              * don't know by how much.  We should never clamp to less than the
    4050              :              * largest ndistinct value for any of the Vars, though, since
    4051              :              * there will surely be at least that many groups.
    4052              :              */
    4053       245315 :             double      clamp = rel->tuples;
    4054              : 
    4055       245315 :             if (relvarcount > 1)
    4056              :             {
    4057        30951 :                 clamp *= 0.1;
    4058        30951 :                 if (clamp < relmaxndistinct)
    4059              :                 {
    4060        28806 :                     clamp = relmaxndistinct;
    4061              :                     /* for sanity in case some ndistinct is too large: */
    4062        28806 :                     if (clamp > rel->tuples)
    4063           65 :                         clamp = rel->tuples;
    4064              :                 }
    4065              :             }
    4066       245315 :             if (reldistinct > clamp)
    4067        25266 :                 reldistinct = clamp;
    4068              : 
    4069              :             /*
    4070              :              * Update the estimate based on the restriction selectivity,
    4071              :              * guarding against division by zero when reldistinct is zero.
    4072              :              * Also skip this if we know that we are returning all rows.
    4073              :              */
    4074       245315 :             if (reldistinct > 0 && rel->rows < rel->tuples)
    4075              :             {
    4076              :                 /*
    4077              :                  * Given a table containing N rows with n distinct values in a
    4078              :                  * uniform distribution, if we select p rows at random then
    4079              :                  * the expected number of distinct values selected is
    4080              :                  *
    4081              :                  * n * (1 - product((N-N/n-i)/(N-i), i=0..p-1))
    4082              :                  *
    4083              :                  * = n * (1 - (N-N/n)! / (N-N/n-p)! * (N-p)! / N!)
    4084              :                  *
    4085              :                  * See "Approximating block accesses in database
    4086              :                  * organizations", S. B. Yao, Communications of the ACM,
    4087              :                  * Volume 20 Issue 4, April 1977 Pages 260-261.
    4088              :                  *
    4089              :                  * Alternatively, re-arranging the terms from the factorials,
    4090              :                  * this may be written as
    4091              :                  *
    4092              :                  * n * (1 - product((N-p-i)/(N-i), i=0..N/n-1))
    4093              :                  *
    4094              :                  * This form of the formula is more efficient to compute in
    4095              :                  * the common case where p is larger than N/n.  Additionally,
    4096              :                  * as pointed out by Dell'Era, if i << N for all terms in the
    4097              :                  * product, it can be approximated by
    4098              :                  *
    4099              :                  * n * (1 - ((N-p)/N)^(N/n))
    4100              :                  *
    4101              :                  * See "Expected distinct values when selecting from a bag
    4102              :                  * without replacement", Alberto Dell'Era,
    4103              :                  * http://www.adellera.it/investigations/distinct_balls/.
    4104              :                  *
    4105              :                  * The condition i << N is equivalent to n >> 1, so this is a
    4106              :                  * good approximation when the number of distinct values in
    4107              :                  * the table is large.  It turns out that this formula also
    4108              :                  * works well even when n is small.
    4109              :                  */
    4110        72876 :                 reldistinct *=
    4111        72876 :                     (1 - pow((rel->tuples - rel->rows) / rel->tuples,
    4112        72876 :                              rel->tuples / reldistinct));
    4113              :             }
    4114       245315 :             reldistinct = clamp_row_est(reldistinct);
    4115              : 
    4116              :             /*
    4117              :              * Update estimate of total distinct groups.
    4118              :              */
    4119       245315 :             numdistinct *= reldistinct;
    4120              :         }
    4121              : 
    4122       246194 :         varinfos = newvarinfos;
    4123       246194 :     } while (varinfos != NIL);
    4124              : 
    4125              :     /* Now we can account for the effects of any SRFs */
    4126       244026 :     numdistinct *= srf_multiplier;
    4127              : 
    4128              :     /* Round off */
    4129       244026 :     numdistinct = ceil(numdistinct);
    4130              : 
    4131              :     /* Guard against out-of-range answers */
    4132       244026 :     if (numdistinct > input_rows)
    4133        52690 :         numdistinct = input_rows;
    4134       244026 :     if (numdistinct < 1.0)
    4135            0 :         numdistinct = 1.0;
    4136              : 
    4137       244026 :     return numdistinct;
    4138              : }
    4139              : 
    4140              : /*
    4141              :  * Try to estimate the bucket size of the hash join inner side when the join
    4142              :  * condition contains two or more clauses by employing extended statistics.
    4143              :  *
    4144              :  * The main idea of this approach is that the distinct value generated by
    4145              :  * multivariate estimation on two or more columns would provide less bucket size
    4146              :  * than estimation on one separate column.
    4147              :  *
    4148              :  * IMPORTANT: It is crucial to synchronize the approach of combining different
    4149              :  * estimations with the caller's method.
    4150              :  *
    4151              :  * Return a list of clauses that didn't fetch any extended statistics.
    4152              :  */
    4153              : List *
    4154       346043 : estimate_multivariate_bucketsize(PlannerInfo *root, RelOptInfo *inner,
    4155              :                                  List *hashclauses,
    4156              :                                  Selectivity *innerbucketsize)
    4157              : {
    4158              :     List       *clauses;
    4159              :     List       *otherclauses;
    4160              :     double      ndistinct;
    4161              : 
    4162       346043 :     if (list_length(hashclauses) <= 1)
    4163              :     {
    4164              :         /*
    4165              :          * Nothing to do for a single clause.  Could we employ univariate
    4166              :          * extended stat here?
    4167              :          */
    4168       316509 :         return hashclauses;
    4169              :     }
    4170              : 
    4171              :     /* "clauses" is the list of hashclauses we've not dealt with yet */
    4172        29534 :     clauses = list_copy(hashclauses);
    4173              :     /* "otherclauses" holds clauses we are going to return to caller */
    4174        29534 :     otherclauses = NIL;
    4175              :     /* current estimate of ndistinct */
    4176        29534 :     ndistinct = 1.0;
    4177        59078 :     while (clauses != NIL)
    4178              :     {
    4179              :         ListCell   *lc;
    4180        29544 :         int         relid = -1;
    4181        29544 :         List       *varinfos = NIL;
    4182        29544 :         List       *origin_rinfos = NIL;
    4183              :         double      mvndistinct;
    4184              :         List       *origin_varinfos;
    4185        29544 :         int         group_relid = -1;
    4186        29544 :         RelOptInfo *group_rel = NULL;
    4187              :         ListCell   *lc1,
    4188              :                    *lc2;
    4189              : 
    4190              :         /*
    4191              :          * Find clauses, referencing the same single base relation and try to
    4192              :          * estimate such a group with extended statistics.  Create varinfo for
    4193              :          * an approved clause, push it to otherclauses, if it can't be
    4194              :          * estimated here or ignore to process at the next iteration.
    4195              :          */
    4196        90671 :         foreach(lc, clauses)
    4197              :         {
    4198        61127 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
    4199              :             Node       *expr;
    4200              :             Relids      relids;
    4201              :             GroupVarInfo *varinfo;
    4202              : 
    4203              :             /*
    4204              :              * Find the inner side of the join, which we need to estimate the
    4205              :              * number of buckets.  Use outer_is_left because the
    4206              :              * clause_sides_match_join routine has called on hash clauses.
    4207              :              */
    4208       122254 :             relids = rinfo->outer_is_left ?
    4209        61127 :                 rinfo->right_relids : rinfo->left_relids;
    4210       122254 :             expr = rinfo->outer_is_left ?
    4211        61127 :                 get_rightop(rinfo->clause) : get_leftop(rinfo->clause);
    4212              : 
    4213        61127 :             if (bms_get_singleton_member(relids, &relid) &&
    4214        59507 :                 root->simple_rel_array[relid]->statlist != NIL)
    4215           40 :             {
    4216           50 :                 bool        is_duplicate = false;
    4217              : 
    4218              :                 /*
    4219              :                  * This inner-side expression references only one relation.
    4220              :                  * Extended statistics on this clause can exist.
    4221              :                  */
    4222           50 :                 if (group_relid < 0)
    4223              :                 {
    4224           25 :                     RangeTblEntry *rte = root->simple_rte_array[relid];
    4225              : 
    4226           25 :                     if (!rte || (rte->relkind != RELKIND_RELATION &&
    4227            0 :                                  rte->relkind != RELKIND_MATVIEW &&
    4228            0 :                                  rte->relkind != RELKIND_FOREIGN_TABLE &&
    4229            0 :                                  rte->relkind != RELKIND_PARTITIONED_TABLE))
    4230              :                     {
    4231              :                         /* Extended statistics can't exist in principle */
    4232            0 :                         otherclauses = lappend(otherclauses, rinfo);
    4233            0 :                         clauses = foreach_delete_current(clauses, lc);
    4234            0 :                         continue;
    4235              :                     }
    4236              : 
    4237           25 :                     group_relid = relid;
    4238           25 :                     group_rel = root->simple_rel_array[relid];
    4239              :                 }
    4240           25 :                 else if (group_relid != relid)
    4241              :                 {
    4242              :                     /*
    4243              :                      * Being in the group forming state we don't need other
    4244              :                      * clauses.
    4245              :                      */
    4246            0 :                     continue;
    4247              :                 }
    4248              : 
    4249              :                 /*
    4250              :                  * We're going to add the new clause to the varinfos list.  We
    4251              :                  * might re-use add_unique_group_var(), but we don't do so for
    4252              :                  * two reasons.
    4253              :                  *
    4254              :                  * 1) We must keep the origin_rinfos list ordered exactly the
    4255              :                  * same way as varinfos.
    4256              :                  *
    4257              :                  * 2) add_unique_group_var() is designed for
    4258              :                  * estimate_num_groups(), where a larger number of groups is
    4259              :                  * worse.   While estimating the number of hash buckets, we
    4260              :                  * have the opposite: a lesser number of groups is worse.
    4261              :                  * Therefore, we don't have to remove "known equal" vars: the
    4262              :                  * removed var may valuably contribute to the multivariate
    4263              :                  * statistics to grow the number of groups.
    4264              :                  */
    4265              : 
    4266              :                 /*
    4267              :                  * Clear nullingrels to correctly match hash keys.  See
    4268              :                  * add_unique_group_var()'s comment for details.
    4269              :                  */
    4270           50 :                 expr = remove_nulling_relids(expr, root->outer_join_rels, NULL);
    4271              : 
    4272              :                 /*
    4273              :                  * Detect and exclude exact duplicates from the list of hash
    4274              :                  * keys (like add_unique_group_var does).
    4275              :                  */
    4276           70 :                 foreach(lc1, varinfos)
    4277              :                 {
    4278           30 :                     varinfo = (GroupVarInfo *) lfirst(lc1);
    4279              : 
    4280           30 :                     if (!equal(expr, varinfo->var))
    4281           20 :                         continue;
    4282              : 
    4283           10 :                     is_duplicate = true;
    4284           10 :                     break;
    4285              :                 }
    4286              : 
    4287           50 :                 if (is_duplicate)
    4288              :                 {
    4289              :                     /*
    4290              :                      * Skip exact duplicates. Adding them to the otherclauses
    4291              :                      * list also doesn't make sense.
    4292              :                      */
    4293           10 :                     continue;
    4294              :                 }
    4295              : 
    4296              :                 /*
    4297              :                  * Initialize GroupVarInfo.  We only use it to call
    4298              :                  * estimate_multivariate_ndistinct(), which doesn't care about
    4299              :                  * ndistinct and isdefault fields.  Thus, skip these fields.
    4300              :                  */
    4301           40 :                 varinfo = palloc0_object(GroupVarInfo);
    4302           40 :                 varinfo->var = expr;
    4303           40 :                 varinfo->rel = root->simple_rel_array[relid];
    4304           40 :                 varinfos = lappend(varinfos, varinfo);
    4305              : 
    4306              :                 /*
    4307              :                  * Remember the link to RestrictInfo for the case the clause
    4308              :                  * is failed to be estimated.
    4309              :                  */
    4310           40 :                 origin_rinfos = lappend(origin_rinfos, rinfo);
    4311              :             }
    4312              :             else
    4313              :             {
    4314              :                 /* This clause can't be estimated with extended statistics */
    4315        61077 :                 otherclauses = lappend(otherclauses, rinfo);
    4316              :             }
    4317              : 
    4318        61117 :             clauses = foreach_delete_current(clauses, lc);
    4319              :         }
    4320              : 
    4321        29544 :         if (list_length(varinfos) < 2)
    4322              :         {
    4323              :             /*
    4324              :              * Multivariate statistics doesn't apply to single columns except
    4325              :              * for expressions, but it has not been implemented yet.
    4326              :              */
    4327        29534 :             otherclauses = list_concat(otherclauses, origin_rinfos);
    4328        29534 :             list_free_deep(varinfos);
    4329        29534 :             list_free(origin_rinfos);
    4330        29534 :             continue;
    4331              :         }
    4332              : 
    4333              :         Assert(group_rel != NULL);
    4334              : 
    4335              :         /* Employ the extended statistics. */
    4336           10 :         origin_varinfos = varinfos;
    4337              :         for (;;)
    4338           10 :         {
    4339           20 :             bool        estimated = estimate_multivariate_ndistinct(root,
    4340              :                                                                     group_rel,
    4341              :                                                                     &varinfos,
    4342              :                                                                     &mvndistinct);
    4343              : 
    4344           20 :             if (!estimated)
    4345           10 :                 break;
    4346              : 
    4347              :             /*
    4348              :              * We've got an estimation.  Use ndistinct value in a consistent
    4349              :              * way - according to the caller's logic (see
    4350              :              * final_cost_hashjoin).
    4351              :              */
    4352           10 :             if (ndistinct < mvndistinct)
    4353           10 :                 ndistinct = mvndistinct;
    4354              :             Assert(ndistinct >= 1.0);
    4355              :         }
    4356              : 
    4357              :         Assert(list_length(origin_varinfos) == list_length(origin_rinfos));
    4358              : 
    4359              :         /* Collect unmatched clauses as otherclauses. */
    4360           35 :         forboth(lc1, origin_varinfos, lc2, origin_rinfos)
    4361              :         {
    4362           25 :             GroupVarInfo *vinfo = lfirst(lc1);
    4363              : 
    4364           25 :             if (!list_member_ptr(varinfos, vinfo))
    4365              :                 /* Already estimated */
    4366           25 :                 continue;
    4367              : 
    4368              :             /* Can't be estimated here - push to the returning list */
    4369            0 :             otherclauses = lappend(otherclauses, lfirst(lc2));
    4370              :         }
    4371              :     }
    4372              : 
    4373        29534 :     *innerbucketsize = 1.0 / ndistinct;
    4374        29534 :     return otherclauses;
    4375              : }
    4376              : 
    4377              : /*
    4378              :  * Estimate hash bucket statistics when the specified expression is used
    4379              :  * as a hash key for the given number of buckets.
    4380              :  *
    4381              :  * This attempts to determine two values:
    4382              :  *
    4383              :  * 1. The frequency of the most common value of the expression (returns
    4384              :  * zero into *mcv_freq if we can't get that).  This will be frequency
    4385              :  * relative to the entire underlying table.
    4386              :  *
    4387              :  * 2. The "bucketsize fraction", ie, average number of entries in a bucket
    4388              :  * divided by total number of tuples to be hashed.
    4389              :  *
    4390              :  * XXX This is really pretty bogus since we're effectively assuming that the
    4391              :  * distribution of hash keys will be the same after applying restriction
    4392              :  * clauses as it was in the underlying relation.  However, we are not nearly
    4393              :  * smart enough to figure out how the restrict clauses might change the
    4394              :  * distribution, so this will have to do for now.
    4395              :  *
    4396              :  * We are passed the number of buckets the executor will use for the given
    4397              :  * input relation.  If the data were perfectly distributed, with the same
    4398              :  * number of tuples going into each available bucket, then the bucketsize
    4399              :  * fraction would be 1/nbuckets.  But this happy state of affairs will occur
    4400              :  * only if (a) there are at least nbuckets distinct data values, and (b)
    4401              :  * we have a not-too-skewed data distribution.  Otherwise the buckets will
    4402              :  * be nonuniformly occupied.  If the other relation in the join has a key
    4403              :  * distribution similar to this one's, then the most-loaded buckets are
    4404              :  * exactly those that will be probed most often.  Therefore, the "average"
    4405              :  * bucket size for costing purposes should really be taken as something close
    4406              :  * to the "worst case" bucket size.  We try to estimate this by adjusting the
    4407              :  * fraction if there are too few distinct data values, and then clamping to
    4408              :  * at least the bucket size implied by the most common value's frequency.
    4409              :  *
    4410              :  * If no statistics are available, use a default estimate of 0.1.  This will
    4411              :  * discourage use of a hash rather strongly if the inner relation is large,
    4412              :  * which is what we want.  We do not want to hash unless we know that the
    4413              :  * inner rel is well-dispersed (or the alternatives seem much worse).
    4414              :  *
    4415              :  * The caller should also check that the mcv_freq is not so large that the
    4416              :  * most common value would by itself require an impractically large bucket.
    4417              :  * In a hash join, the executor can split buckets if they get too big, but
    4418              :  * obviously that doesn't help for a bucket that contains many duplicates of
    4419              :  * the same value.
    4420              :  */
    4421              : void
    4422       154770 : estimate_hash_bucket_stats(PlannerInfo *root, Node *hashkey, double nbuckets,
    4423              :                            Selectivity *mcv_freq,
    4424              :                            Selectivity *bucketsize_frac)
    4425              : {
    4426              :     VariableStatData vardata;
    4427              :     double      estfract,
    4428              :                 ndistinct;
    4429              :     bool        isdefault;
    4430              :     AttStatsSlot sslot;
    4431              : 
    4432       154770 :     examine_variable(root, hashkey, 0, &vardata);
    4433              : 
    4434              :     /* Initialize *mcv_freq to "unknown" */
    4435       154770 :     *mcv_freq = 0.0;
    4436              : 
    4437              :     /* Look up the frequency of the most common value, if available */
    4438       154770 :     if (HeapTupleIsValid(vardata.statsTuple))
    4439              :     {
    4440       100793 :         if (get_attstatsslot(&sslot, vardata.statsTuple,
    4441              :                              STATISTIC_KIND_MCV, InvalidOid,
    4442              :                              ATTSTATSSLOT_NUMBERS))
    4443              :         {
    4444              :             /*
    4445              :              * The first MCV stat is for the most common value.
    4446              :              */
    4447        59873 :             if (sslot.nnumbers > 0)
    4448        59873 :                 *mcv_freq = sslot.numbers[0];
    4449        59873 :             free_attstatsslot(&sslot);
    4450              :         }
    4451        40920 :         else if (get_attstatsslot(&sslot, vardata.statsTuple,
    4452              :                                   STATISTIC_KIND_HISTOGRAM, InvalidOid,
    4453              :                                   0))
    4454              :         {
    4455              :             /*
    4456              :              * If there are no recorded MCVs, but we do have a histogram, then
    4457              :              * assume that ANALYZE determined that the column is unique.
    4458              :              */
    4459        39444 :             if (vardata.rel && vardata.rel->tuples > 0)
    4460        39429 :                 *mcv_freq = 1.0 / vardata.rel->tuples;
    4461              :         }
    4462              :     }
    4463              : 
    4464              :     /* Get number of distinct values */
    4465       154770 :     ndistinct = get_variable_numdistinct(&vardata, &isdefault);
    4466              : 
    4467              :     /*
    4468              :      * If ndistinct isn't real, punt.  We normally return 0.1, but if the
    4469              :      * mcv_freq is known to be even higher than that, use it instead.
    4470              :      */
    4471       154770 :     if (isdefault)
    4472              :     {
    4473        24828 :         *bucketsize_frac = (Selectivity) Max(0.1, *mcv_freq);
    4474        24828 :         ReleaseVariableStats(vardata);
    4475        24828 :         return;
    4476              :     }
    4477              : 
    4478              :     /*
    4479              :      * Adjust ndistinct to account for restriction clauses.  Observe we are
    4480              :      * assuming that the data distribution is affected uniformly by the
    4481              :      * restriction clauses!
    4482              :      *
    4483              :      * XXX Possibly better way, but much more expensive: multiply by
    4484              :      * selectivity of rel's restriction clauses that mention the target Var.
    4485              :      */
    4486       129942 :     if (vardata.rel && vardata.rel->tuples > 0)
    4487              :     {
    4488       129915 :         ndistinct *= vardata.rel->rows / vardata.rel->tuples;
    4489       129915 :         ndistinct = clamp_row_est(ndistinct);
    4490              :     }
    4491              : 
    4492              :     /*
    4493              :      * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
    4494              :      * number of buckets is less than the expected number of distinct values;
    4495              :      * otherwise it is 1/ndistinct.
    4496              :      */
    4497       129942 :     if (ndistinct > nbuckets)
    4498           72 :         estfract = 1.0 / nbuckets;
    4499              :     else
    4500       129870 :         estfract = 1.0 / ndistinct;
    4501              : 
    4502              :     /*
    4503              :      * Clamp the bucketsize fraction to be not less than the MCV frequency,
    4504              :      * since whichever bucket the MCV values end up in will have at least that
    4505              :      * size.  This has no effect if *mcv_freq is still zero.
    4506              :      */
    4507       129942 :     estfract = Max(estfract, *mcv_freq);
    4508              : 
    4509       129942 :     *bucketsize_frac = (Selectivity) estfract;
    4510              : 
    4511       129942 :     ReleaseVariableStats(vardata);
    4512              : }
    4513              : 
    4514              : /*
    4515              :  * estimate_hashagg_tablesize
    4516              :  *    estimate the number of bytes that a hash aggregate hashtable will
    4517              :  *    require based on the agg_costs, path width and number of groups.
    4518              :  *
    4519              :  * We return the result as "double" to forestall any possible overflow
    4520              :  * problem in the multiplication by dNumGroups.
    4521              :  *
    4522              :  * XXX this may be over-estimating the size now that hashagg knows to omit
    4523              :  * unneeded columns from the hashtable.  Also for mixed-mode grouping sets,
    4524              :  * grouping columns not in the hashed set are counted here even though hashagg
    4525              :  * won't store them.  Is this a problem?
    4526              :  */
    4527              : double
    4528         2424 : estimate_hashagg_tablesize(PlannerInfo *root, Path *path,
    4529              :                            const AggClauseCosts *agg_costs, double dNumGroups)
    4530              : {
    4531              :     Size        hashentrysize;
    4532              : 
    4533         2424 :     hashentrysize = hash_agg_entry_size(list_length(root->aggtransinfos),
    4534         2424 :                                         path->pathtarget->width,
    4535         2424 :                                         agg_costs->transitionSpace);
    4536              : 
    4537              :     /*
    4538              :      * Note that this disregards the effect of fill-factor and growth policy
    4539              :      * of the hash table.  That's probably ok, given that the default
    4540              :      * fill-factor is relatively high.  It'd be hard to meaningfully factor in
    4541              :      * "double-in-size" growth policies here.
    4542              :      */
    4543         2424 :     return hashentrysize * dNumGroups;
    4544              : }
    4545              : 
    4546              : 
    4547              : /*-------------------------------------------------------------------------
    4548              :  *
    4549              :  * Support routines
    4550              :  *
    4551              :  *-------------------------------------------------------------------------
    4552              :  */
    4553              : 
    4554              : /*
    4555              :  * Find the best matching ndistinct extended statistics for the given list of
    4556              :  * GroupVarInfos.
    4557              :  *
    4558              :  * Callers must ensure that the given GroupVarInfos all belong to 'rel' and
    4559              :  * the GroupVarInfos list does not contain any duplicate Vars or expressions.
    4560              :  *
    4561              :  * When statistics are found that match > 1 of the given GroupVarInfo, the
    4562              :  * *ndistinct parameter is set according to the ndistinct estimate and a new
    4563              :  * list is built with the matching GroupVarInfos removed, which is output via
    4564              :  * the *varinfos parameter before returning true.  When no matching stats are
    4565              :  * found, false is returned and the *varinfos and *ndistinct parameters are
    4566              :  * left untouched.
    4567              :  */
    4568              : static bool
    4569       246319 : estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
    4570              :                                 List **varinfos, double *ndistinct)
    4571              : {
    4572              :     ListCell   *lc;
    4573              :     int         nmatches_vars;
    4574              :     int         nmatches_exprs;
    4575       246319 :     Oid         statOid = InvalidOid;
    4576              :     MVNDistinct *stats;
    4577       246319 :     StatisticExtInfo *matched_info = NULL;
    4578       246319 :     RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
    4579              : 
    4580              :     /* bail out immediately if the table has no extended statistics */
    4581       246319 :     if (!rel->statlist)
    4582       245848 :         return false;
    4583              : 
    4584              :     /* look for the ndistinct statistics object matching the most vars */
    4585          471 :     nmatches_vars = 0;          /* we require at least two matches */
    4586          471 :     nmatches_exprs = 0;
    4587         1875 :     foreach(lc, rel->statlist)
    4588              :     {
    4589              :         ListCell   *lc2;
    4590         1404 :         StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
    4591         1404 :         int         nshared_vars = 0;
    4592         1404 :         int         nshared_exprs = 0;
    4593              : 
    4594              :         /* skip statistics of other kinds */
    4595         1404 :         if (info->kind != STATS_EXT_NDISTINCT)
    4596          663 :             continue;
    4597              : 
    4598              :         /* skip statistics with mismatching stxdinherit value */
    4599          741 :         if (info->inherit != rte->inh)
    4600           25 :             continue;
    4601              : 
    4602              :         /*
    4603              :          * Determine how many expressions (and variables in non-matched
    4604              :          * expressions) match. We'll then use these numbers to pick the
    4605              :          * statistics object that best matches the clauses.
    4606              :          */
    4607         2267 :         foreach(lc2, *varinfos)
    4608              :         {
    4609              :             ListCell   *lc3;
    4610         1551 :             GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc2);
    4611              :             AttrNumber  attnum;
    4612              : 
    4613              :             Assert(varinfo->rel == rel);
    4614              : 
    4615              :             /* simple Var, search in statistics keys directly */
    4616         1551 :             if (IsA(varinfo->var, Var))
    4617              :             {
    4618         1246 :                 attnum = ((Var *) varinfo->var)->varattno;
    4619              : 
    4620              :                 /*
    4621              :                  * Ignore system attributes - we don't support statistics on
    4622              :                  * them, so can't match them (and it'd fail as the values are
    4623              :                  * negative).
    4624              :                  */
    4625         1246 :                 if (!AttrNumberIsForUserDefinedAttr(attnum))
    4626           10 :                     continue;
    4627              : 
    4628         1236 :                 if (bms_is_member(attnum, info->keys))
    4629          730 :                     nshared_vars++;
    4630              : 
    4631         1236 :                 continue;
    4632              :             }
    4633              : 
    4634              :             /* expression - see if it's in the statistics object */
    4635          550 :             foreach(lc3, info->exprs)
    4636              :             {
    4637          440 :                 Node       *expr = (Node *) lfirst(lc3);
    4638              : 
    4639          440 :                 if (equal(varinfo->var, expr))
    4640              :                 {
    4641          195 :                     nshared_exprs++;
    4642          195 :                     break;
    4643              :                 }
    4644              :             }
    4645              :         }
    4646              : 
    4647              :         /*
    4648              :          * The ndistinct extended statistics contain estimates for a minimum
    4649              :          * of pairs of columns which the statistics are defined on and
    4650              :          * certainly not single columns.  Here we skip unless we managed to
    4651              :          * match to at least two columns.
    4652              :          */
    4653          716 :         if (nshared_vars + nshared_exprs < 2)
    4654          331 :             continue;
    4655              : 
    4656              :         /*
    4657              :          * Check if these statistics are a better match than the previous best
    4658              :          * match and if so, take note of the StatisticExtInfo.
    4659              :          *
    4660              :          * The statslist is sorted by statOid, so the StatisticExtInfo we
    4661              :          * select as the best match is deterministic even when multiple sets
    4662              :          * of statistics match equally as well.
    4663              :          */
    4664          385 :         if ((nshared_exprs > nmatches_exprs) ||
    4665          295 :             (((nshared_exprs == nmatches_exprs)) && (nshared_vars > nmatches_vars)))
    4666              :         {
    4667          365 :             statOid = info->statOid;
    4668          365 :             nmatches_vars = nshared_vars;
    4669          365 :             nmatches_exprs = nshared_exprs;
    4670          365 :             matched_info = info;
    4671              :         }
    4672              :     }
    4673              : 
    4674              :     /* No match? */
    4675          471 :     if (statOid == InvalidOid)
    4676          116 :         return false;
    4677              : 
    4678              :     Assert(nmatches_vars + nmatches_exprs > 1);
    4679              : 
    4680          355 :     stats = statext_ndistinct_load(statOid, rte->inh);
    4681              : 
    4682              :     /*
    4683              :      * If we have a match, search it for the specific item that matches (there
    4684              :      * must be one), and construct the output values.
    4685              :      */
    4686          355 :     if (stats)
    4687              :     {
    4688              :         int         i;
    4689          355 :         List       *newlist = NIL;
    4690          355 :         MVNDistinctItem *item = NULL;
    4691              :         ListCell   *lc2;
    4692          355 :         Bitmapset  *matched = NULL;
    4693              :         AttrNumber  attnum_offset;
    4694              : 
    4695              :         /*
    4696              :          * How much we need to offset the attnums? If there are no
    4697              :          * expressions, no offset is needed. Otherwise offset enough to move
    4698              :          * the lowest one (which is equal to number of expressions) to 1.
    4699              :          */
    4700          355 :         if (matched_info->exprs)
    4701          125 :             attnum_offset = (list_length(matched_info->exprs) + 1);
    4702              :         else
    4703          230 :             attnum_offset = 0;
    4704              : 
    4705              :         /* see what actually matched */
    4706         1240 :         foreach(lc2, *varinfos)
    4707              :         {
    4708              :             ListCell   *lc3;
    4709              :             int         idx;
    4710          885 :             bool        found = false;
    4711              : 
    4712          885 :             GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc2);
    4713              : 
    4714              :             /*
    4715              :              * Process a simple Var expression, by matching it to keys
    4716              :              * directly. If there's a matching expression, we'll try matching
    4717              :              * it later.
    4718              :              */
    4719          885 :             if (IsA(varinfo->var, Var))
    4720              :             {
    4721          730 :                 AttrNumber  attnum = ((Var *) varinfo->var)->varattno;
    4722              : 
    4723              :                 /*
    4724              :                  * Ignore expressions on system attributes. Can't rely on the
    4725              :                  * bms check for negative values.
    4726              :                  */
    4727          730 :                 if (!AttrNumberIsForUserDefinedAttr(attnum))
    4728            5 :                     continue;
    4729              : 
    4730              :                 /* Is the variable covered by the statistics object? */
    4731          725 :                 if (!bms_is_member(attnum, matched_info->keys))
    4732          100 :                     continue;
    4733              : 
    4734          625 :                 attnum = attnum + attnum_offset;
    4735              : 
    4736              :                 /* ensure sufficient offset */
    4737              :                 Assert(AttrNumberIsForUserDefinedAttr(attnum));
    4738              : 
    4739          625 :                 matched = bms_add_member(matched, attnum);
    4740              : 
    4741          625 :                 found = true;
    4742              :             }
    4743              : 
    4744              :             /*
    4745              :              * XXX Maybe we should allow searching the expressions even if we
    4746              :              * found an attribute matching the expression? That would handle
    4747              :              * trivial expressions like "(a)" but it seems fairly useless.
    4748              :              */
    4749          780 :             if (found)
    4750          625 :                 continue;
    4751              : 
    4752              :             /* expression - see if it's in the statistics object */
    4753          155 :             idx = 0;
    4754          255 :             foreach(lc3, matched_info->exprs)
    4755              :             {
    4756          230 :                 Node       *expr = (Node *) lfirst(lc3);
    4757              : 
    4758          230 :                 if (equal(varinfo->var, expr))
    4759              :                 {
    4760          130 :                     AttrNumber  attnum = -(idx + 1);
    4761              : 
    4762          130 :                     attnum = attnum + attnum_offset;
    4763              : 
    4764              :                     /* ensure sufficient offset */
    4765              :                     Assert(AttrNumberIsForUserDefinedAttr(attnum));
    4766              : 
    4767          130 :                     matched = bms_add_member(matched, attnum);
    4768              : 
    4769              :                     /* there should be just one matching expression */
    4770          130 :                     break;
    4771              :                 }
    4772              : 
    4773          100 :                 idx++;
    4774              :             }
    4775              :         }
    4776              : 
    4777              :         /* Find the specific item that exactly matches the combination */
    4778          720 :         for (i = 0; i < stats->nitems; i++)
    4779              :         {
    4780              :             int         j;
    4781          720 :             MVNDistinctItem *tmpitem = &stats->items[i];
    4782              : 
    4783          720 :             if (tmpitem->nattributes != bms_num_members(matched))
    4784          135 :                 continue;
    4785              : 
    4786              :             /* assume it's the right item */
    4787          585 :             item = tmpitem;
    4788              : 
    4789              :             /* check that all item attributes/expressions fit the match */
    4790         1410 :             for (j = 0; j < tmpitem->nattributes; j++)
    4791              :             {
    4792         1055 :                 AttrNumber  attnum = tmpitem->attributes[j];
    4793              : 
    4794              :                 /*
    4795              :                  * Thanks to how we constructed the matched bitmap above, we
    4796              :                  * can just offset all attnums the same way.
    4797              :                  */
    4798         1055 :                 attnum = attnum + attnum_offset;
    4799              : 
    4800         1055 :                 if (!bms_is_member(attnum, matched))
    4801              :                 {
    4802              :                     /* nah, it's not this item */
    4803          230 :                     item = NULL;
    4804          230 :                     break;
    4805              :                 }
    4806              :             }
    4807              : 
    4808              :             /*
    4809              :              * If the item has all the matched attributes, we know it's the
    4810              :              * right one - there can't be a better one. matching more.
    4811              :              */
    4812          585 :             if (item)
    4813          355 :                 break;
    4814              :         }
    4815              : 
    4816              :         /*
    4817              :          * Make sure we found an item. There has to be one, because ndistinct
    4818              :          * statistics includes all combinations of attributes.
    4819              :          */
    4820          355 :         if (!item)
    4821            0 :             elog(ERROR, "corrupt MVNDistinct entry");
    4822              : 
    4823              :         /* Form the output varinfo list, keeping only unmatched ones */
    4824         1240 :         foreach(lc, *varinfos)
    4825              :         {
    4826          885 :             GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
    4827              :             ListCell   *lc3;
    4828          885 :             bool        found = false;
    4829              : 
    4830              :             /*
    4831              :              * Let's look at plain variables first, because it's the most
    4832              :              * common case and the check is quite cheap. We can simply get the
    4833              :              * attnum and check (with an offset) matched bitmap.
    4834              :              */
    4835          885 :             if (IsA(varinfo->var, Var))
    4836          725 :             {
    4837          730 :                 AttrNumber  attnum = ((Var *) varinfo->var)->varattno;
    4838              : 
    4839              :                 /*
    4840              :                  * If it's a system attribute, we're done. We don't support
    4841              :                  * extended statistics on system attributes, so it's clearly
    4842              :                  * not matched. Just keep the expression and continue.
    4843              :                  */
    4844          730 :                 if (!AttrNumberIsForUserDefinedAttr(attnum))
    4845              :                 {
    4846            5 :                     newlist = lappend(newlist, varinfo);
    4847            5 :                     continue;
    4848              :                 }
    4849              : 
    4850              :                 /* apply the same offset as above */
    4851          725 :                 attnum += attnum_offset;
    4852              : 
    4853              :                 /* if it's not matched, keep the varinfo */
    4854          725 :                 if (!bms_is_member(attnum, matched))
    4855          100 :                     newlist = lappend(newlist, varinfo);
    4856              : 
    4857              :                 /* The rest of the loop deals with complex expressions. */
    4858          725 :                 continue;
    4859              :             }
    4860              : 
    4861              :             /*
    4862              :              * Process complex expressions, not just simple Vars.
    4863              :              *
    4864              :              * First, we search for an exact match of an expression. If we
    4865              :              * find one, we can just discard the whole GroupVarInfo, with all
    4866              :              * the variables we extracted from it.
    4867              :              *
    4868              :              * Otherwise we inspect the individual vars, and try matching it
    4869              :              * to variables in the item.
    4870              :              */
    4871          255 :             foreach(lc3, matched_info->exprs)
    4872              :             {
    4873          230 :                 Node       *expr = (Node *) lfirst(lc3);
    4874              : 
    4875          230 :                 if (equal(varinfo->var, expr))
    4876              :                 {
    4877          130 :                     found = true;
    4878          130 :                     break;
    4879              :                 }
    4880              :             }
    4881              : 
    4882              :             /* found exact match, skip */
    4883          155 :             if (found)
    4884          130 :                 continue;
    4885              : 
    4886           25 :             newlist = lappend(newlist, varinfo);
    4887              :         }
    4888              : 
    4889          355 :         *varinfos = newlist;
    4890          355 :         *ndistinct = item->ndistinct;
    4891          355 :         return true;
    4892              :     }
    4893              : 
    4894            0 :     return false;
    4895              : }
    4896              : 
    4897              : /*
    4898              :  * convert_to_scalar
    4899              :  *    Convert non-NULL values of the indicated types to the comparison
    4900              :  *    scale needed by scalarineqsel().
    4901              :  *    Returns "true" if successful.
    4902              :  *
    4903              :  * XXX this routine is a hack: ideally we should look up the conversion
    4904              :  * subroutines in pg_type.
    4905              :  *
    4906              :  * All numeric datatypes are simply converted to their equivalent
    4907              :  * "double" values.  (NUMERIC values that are outside the range of "double"
    4908              :  * are clamped to +/- HUGE_VAL.)
    4909              :  *
    4910              :  * String datatypes are converted by convert_string_to_scalar(),
    4911              :  * which is explained below.  The reason why this routine deals with
    4912              :  * three values at a time, not just one, is that we need it for strings.
    4913              :  *
    4914              :  * The bytea datatype is just enough different from strings that it has
    4915              :  * to be treated separately.
    4916              :  *
    4917              :  * The several datatypes representing absolute times are all converted
    4918              :  * to Timestamp, which is actually an int64, and then we promote that to
    4919              :  * a double.  Note this will give correct results even for the "special"
    4920              :  * values of Timestamp, since those are chosen to compare correctly;
    4921              :  * see timestamp_cmp.
    4922              :  *
    4923              :  * The several datatypes representing relative times (intervals) are all
    4924              :  * converted to measurements expressed in seconds.
    4925              :  */
    4926              : static bool
    4927        56300 : convert_to_scalar(Datum value, Oid valuetypid, Oid collid, double *scaledvalue,
    4928              :                   Datum lobound, Datum hibound, Oid boundstypid,
    4929              :                   double *scaledlobound, double *scaledhibound)
    4930              : {
    4931        56300 :     bool        failure = false;
    4932              : 
    4933              :     /*
    4934              :      * Both the valuetypid and the boundstypid should exactly match the
    4935              :      * declared input type(s) of the operator we are invoked for.  However,
    4936              :      * extensions might try to use scalarineqsel as estimator for operators
    4937              :      * with input type(s) we don't handle here; in such cases, we want to
    4938              :      * return false, not fail.  In any case, we mustn't assume that valuetypid
    4939              :      * and boundstypid are identical.
    4940              :      *
    4941              :      * XXX The histogram we are interpolating between points of could belong
    4942              :      * to a column that's only binary-compatible with the declared type. In
    4943              :      * essence we are assuming that the semantics of binary-compatible types
    4944              :      * are enough alike that we can use a histogram generated with one type's
    4945              :      * operators to estimate selectivity for the other's.  This is outright
    4946              :      * wrong in some cases --- in particular signed versus unsigned
    4947              :      * interpretation could trip us up.  But it's useful enough in the
    4948              :      * majority of cases that we do it anyway.  Should think about more
    4949              :      * rigorous ways to do it.
    4950              :      */
    4951        56300 :     switch (valuetypid)
    4952              :     {
    4953              :             /*
    4954              :              * Built-in numeric types
    4955              :              */
    4956        51413 :         case BOOLOID:
    4957              :         case INT2OID:
    4958              :         case INT4OID:
    4959              :         case INT8OID:
    4960              :         case FLOAT4OID:
    4961              :         case FLOAT8OID:
    4962              :         case NUMERICOID:
    4963              :         case OIDOID:
    4964              :         case REGPROCOID:
    4965              :         case REGPROCEDUREOID:
    4966              :         case REGOPEROID:
    4967              :         case REGOPERATOROID:
    4968              :         case REGCLASSOID:
    4969              :         case REGTYPEOID:
    4970              :         case REGCOLLATIONOID:
    4971              :         case REGCONFIGOID:
    4972              :         case REGDICTIONARYOID:
    4973              :         case REGROLEOID:
    4974              :         case REGNAMESPACEOID:
    4975              :         case REGDATABASEOID:
    4976        51413 :             *scaledvalue = convert_numeric_to_scalar(value, valuetypid,
    4977              :                                                      &failure);
    4978        51413 :             *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid,
    4979              :                                                        &failure);
    4980        51413 :             *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid,
    4981              :                                                        &failure);
    4982        51413 :             return !failure;
    4983              : 
    4984              :             /*
    4985              :              * Built-in string types
    4986              :              */
    4987         4887 :         case CHAROID:
    4988              :         case BPCHAROID:
    4989              :         case VARCHAROID:
    4990              :         case TEXTOID:
    4991              :         case NAMEOID:
    4992              :             {
    4993         4887 :                 char       *valstr = convert_string_datum(value, valuetypid,
    4994              :                                                           collid, &failure);
    4995         4887 :                 char       *lostr = convert_string_datum(lobound, boundstypid,
    4996              :                                                          collid, &failure);
    4997         4887 :                 char       *histr = convert_string_datum(hibound, boundstypid,
    4998              :                                                          collid, &failure);
    4999              : 
    5000              :                 /*
    5001              :                  * Bail out if any of the values is not of string type.  We
    5002              :                  * might leak converted strings for the other value(s), but
    5003              :                  * that's not worth troubling over.
    5004              :                  */
    5005         4887 :                 if (failure)
    5006            0 :                     return false;
    5007              : 
    5008         4887 :                 convert_string_to_scalar(valstr, scaledvalue,
    5009              :                                          lostr, scaledlobound,
    5010              :                                          histr, scaledhibound);
    5011         4887 :                 pfree(valstr);
    5012         4887 :                 pfree(lostr);
    5013         4887 :                 pfree(histr);
    5014         4887 :                 return true;
    5015              :             }
    5016              : 
    5017              :             /*
    5018              :              * Built-in bytea type
    5019              :              */
    5020            0 :         case BYTEAOID:
    5021              :             {
    5022              :                 /* We only support bytea vs bytea comparison */
    5023            0 :                 if (boundstypid != BYTEAOID)
    5024            0 :                     return false;
    5025            0 :                 convert_bytea_to_scalar(value, scaledvalue,
    5026              :                                         lobound, scaledlobound,
    5027              :                                         hibound, scaledhibound);
    5028            0 :                 return true;
    5029              :             }
    5030              : 
    5031              :             /*
    5032              :              * Built-in time types
    5033              :              */
    5034            0 :         case TIMESTAMPOID:
    5035              :         case TIMESTAMPTZOID:
    5036              :         case DATEOID:
    5037              :         case INTERVALOID:
    5038              :         case TIMEOID:
    5039              :         case TIMETZOID:
    5040            0 :             *scaledvalue = convert_timevalue_to_scalar(value, valuetypid,
    5041              :                                                        &failure);
    5042            0 :             *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid,
    5043              :                                                          &failure);
    5044            0 :             *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid,
    5045              :                                                          &failure);
    5046            0 :             return !failure;
    5047              : 
    5048              :             /*
    5049              :              * Built-in network types
    5050              :              */
    5051            0 :         case INETOID:
    5052              :         case CIDROID:
    5053              :         case MACADDROID:
    5054              :         case MACADDR8OID:
    5055            0 :             *scaledvalue = convert_network_to_scalar(value, valuetypid,
    5056              :                                                      &failure);
    5057            0 :             *scaledlobound = convert_network_to_scalar(lobound, boundstypid,
    5058              :                                                        &failure);
    5059            0 :             *scaledhibound = convert_network_to_scalar(hibound, boundstypid,
    5060              :                                                        &failure);
    5061            0 :             return !failure;
    5062              :     }
    5063              :     /* Don't know how to convert */
    5064            0 :     *scaledvalue = *scaledlobound = *scaledhibound = 0;
    5065            0 :     return false;
    5066              : }
    5067              : 
    5068              : /*
    5069              :  * Do convert_to_scalar()'s work for any numeric data type.
    5070              :  *
    5071              :  * On failure (e.g., unsupported typid), set *failure to true;
    5072              :  * otherwise, that variable is not changed.
    5073              :  */
    5074              : static double
    5075       154239 : convert_numeric_to_scalar(Datum value, Oid typid, bool *failure)
    5076              : {
    5077       154239 :     switch (typid)
    5078              :     {
    5079            0 :         case BOOLOID:
    5080            0 :             return (double) DatumGetBool(value);
    5081           10 :         case INT2OID:
    5082           10 :             return (double) DatumGetInt16(value);
    5083        22169 :         case INT4OID:
    5084        22169 :             return (double) DatumGetInt32(value);
    5085            0 :         case INT8OID:
    5086            0 :             return (double) DatumGetInt64(value);
    5087            0 :         case FLOAT4OID:
    5088            0 :             return (double) DatumGetFloat4(value);
    5089           45 :         case FLOAT8OID:
    5090           45 :             return (double) DatumGetFloat8(value);
    5091            0 :         case NUMERICOID:
    5092              :             /* Note: out-of-range values will be clamped to +-HUGE_VAL */
    5093            0 :             return (double)
    5094            0 :                 DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
    5095              :                                                    value));
    5096       132015 :         case OIDOID:
    5097              :         case REGPROCOID:
    5098              :         case REGPROCEDUREOID:
    5099              :         case REGOPEROID:
    5100              :         case REGOPERATOROID:
    5101              :         case REGCLASSOID:
    5102              :         case REGTYPEOID:
    5103              :         case REGCOLLATIONOID:
    5104              :         case REGCONFIGOID:
    5105              :         case REGDICTIONARYOID:
    5106              :         case REGROLEOID:
    5107              :         case REGNAMESPACEOID:
    5108              :         case REGDATABASEOID:
    5109              :             /* we can treat OIDs as integers... */
    5110       132015 :             return (double) DatumGetObjectId(value);
    5111              :     }
    5112              : 
    5113            0 :     *failure = true;
    5114            0 :     return 0;
    5115              : }
    5116              : 
    5117              : /*
    5118              :  * Do convert_to_scalar()'s work for any character-string data type.
    5119              :  *
    5120              :  * String datatypes are converted to a scale that ranges from 0 to 1,
    5121              :  * where we visualize the bytes of the string as fractional digits.
    5122              :  *
    5123              :  * We do not want the base to be 256, however, since that tends to
    5124              :  * generate inflated selectivity estimates; few databases will have
    5125              :  * occurrences of all 256 possible byte values at each position.
    5126              :  * Instead, use the smallest and largest byte values seen in the bounds
    5127              :  * as the estimated range for each byte, after some fudging to deal with
    5128              :  * the fact that we probably aren't going to see the full range that way.
    5129              :  *
    5130              :  * An additional refinement is that we discard any common prefix of the
    5131              :  * three strings before computing the scaled values.  This allows us to
    5132              :  * "zoom in" when we encounter a narrow data range.  An example is a phone
    5133              :  * number database where all the values begin with the same area code.
    5134              :  * (Actually, the bounds will be adjacent histogram-bin-boundary values,
    5135              :  * so this is more likely to happen than you might think.)
    5136              :  */
    5137              : static void
    5138         4887 : convert_string_to_scalar(char *value,
    5139              :                          double *scaledvalue,
    5140              :                          char *lobound,
    5141              :                          double *scaledlobound,
    5142              :                          char *hibound,
    5143              :                          double *scaledhibound)
    5144              : {
    5145              :     int         rangelo,
    5146              :                 rangehi;
    5147              :     char       *sptr;
    5148              : 
    5149         4887 :     rangelo = rangehi = (unsigned char) hibound[0];
    5150        68846 :     for (sptr = lobound; *sptr; sptr++)
    5151              :     {
    5152        63959 :         if (rangelo > (unsigned char) *sptr)
    5153        12098 :             rangelo = (unsigned char) *sptr;
    5154        63959 :         if (rangehi < (unsigned char) *sptr)
    5155         6197 :             rangehi = (unsigned char) *sptr;
    5156              :     }
    5157        56313 :     for (sptr = hibound; *sptr; sptr++)
    5158              :     {
    5159        51426 :         if (rangelo > (unsigned char) *sptr)
    5160          545 :             rangelo = (unsigned char) *sptr;
    5161        51426 :         if (rangehi < (unsigned char) *sptr)
    5162         1862 :             rangehi = (unsigned char) *sptr;
    5163              :     }
    5164              :     /* If range includes any upper-case ASCII chars, make it include all */
    5165         4887 :     if (rangelo <= 'Z' && rangehi >= 'A')
    5166              :     {
    5167         1152 :         if (rangelo > 'A')
    5168          185 :             rangelo = 'A';
    5169         1152 :         if (rangehi < 'Z')
    5170          425 :             rangehi = 'Z';
    5171              :     }
    5172              :     /* Ditto lower-case */
    5173         4887 :     if (rangelo <= 'z' && rangehi >= 'a')
    5174              :     {
    5175         4447 :         if (rangelo > 'a')
    5176            5 :             rangelo = 'a';
    5177         4447 :         if (rangehi < 'z')
    5178         4399 :             rangehi = 'z';
    5179              :     }
    5180              :     /* Ditto digits */
    5181         4887 :     if (rangelo <= '9' && rangehi >= '0')
    5182              :     {
    5183          557 :         if (rangelo > '0')
    5184          484 :             rangelo = '0';
    5185          557 :         if (rangehi < '9')
    5186            9 :             rangehi = '9';
    5187              :     }
    5188              : 
    5189              :     /*
    5190              :      * If range includes less than 10 chars, assume we have not got enough
    5191              :      * data, and make it include regular ASCII set.
    5192              :      */
    5193         4887 :     if (rangehi - rangelo < 9)
    5194              :     {
    5195            0 :         rangelo = ' ';
    5196            0 :         rangehi = 127;
    5197              :     }
    5198              : 
    5199              :     /*
    5200              :      * Now strip any common prefix of the three strings.
    5201              :      */
    5202         9366 :     while (*lobound)
    5203              :     {
    5204         9366 :         if (*lobound != *hibound || *lobound != *value)
    5205              :             break;
    5206         4479 :         lobound++, hibound++, value++;
    5207              :     }
    5208              : 
    5209              :     /*
    5210              :      * Now we can do the conversions.
    5211              :      */
    5212         4887 :     *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
    5213         4887 :     *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
    5214         4887 :     *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
    5215         4887 : }
    5216              : 
    5217              : static double
    5218        14661 : convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
    5219              : {
    5220        14661 :     int         slen = strlen(value);
    5221              :     double      num,
    5222              :                 denom,
    5223              :                 base;
    5224              : 
    5225        14661 :     if (slen <= 0)
    5226            0 :         return 0.0;             /* empty string has scalar value 0 */
    5227              : 
    5228              :     /*
    5229              :      * There seems little point in considering more than a dozen bytes from
    5230              :      * the string.  Since base is at least 10, that will give us nominal
    5231              :      * resolution of at least 12 decimal digits, which is surely far more
    5232              :      * precision than this estimation technique has got anyway (especially in
    5233              :      * non-C locales).  Also, even with the maximum possible base of 256, this
    5234              :      * ensures denom cannot grow larger than 256^13 = 2.03e31, which will not
    5235              :      * overflow on any known machine.
    5236              :      */
    5237        14661 :     if (slen > 12)
    5238         4008 :         slen = 12;
    5239              : 
    5240              :     /* Convert initial characters to fraction */
    5241        14661 :     base = rangehi - rangelo + 1;
    5242        14661 :     num = 0.0;
    5243        14661 :     denom = base;
    5244       124860 :     while (slen-- > 0)
    5245              :     {
    5246       110199 :         int         ch = (unsigned char) *value++;
    5247              : 
    5248       110199 :         if (ch < rangelo)
    5249          180 :             ch = rangelo - 1;
    5250       110019 :         else if (ch > rangehi)
    5251            0 :             ch = rangehi + 1;
    5252       110199 :         num += ((double) (ch - rangelo)) / denom;
    5253       110199 :         denom *= base;
    5254              :     }
    5255              : 
    5256        14661 :     return num;
    5257              : }
    5258              : 
    5259              : /*
    5260              :  * Convert a string-type Datum into a palloc'd, null-terminated string.
    5261              :  *
    5262              :  * On failure (e.g., unsupported typid), set *failure to true;
    5263              :  * otherwise, that variable is not changed.  (We'll return NULL on failure.)
    5264              :  *
    5265              :  * When using a non-C locale, we must pass the string through pg_strxfrm()
    5266              :  * before continuing, so as to generate correct locale-specific results.
    5267              :  */
    5268              : static char *
    5269        14661 : convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure)
    5270              : {
    5271              :     char       *val;
    5272              :     pg_locale_t mylocale;
    5273              : 
    5274        14661 :     switch (typid)
    5275              :     {
    5276            0 :         case CHAROID:
    5277            0 :             val = (char *) palloc(2);
    5278            0 :             val[0] = DatumGetChar(value);
    5279            0 :             val[1] = '\0';
    5280            0 :             break;
    5281         4418 :         case BPCHAROID:
    5282              :         case VARCHAROID:
    5283              :         case TEXTOID:
    5284         4418 :             val = TextDatumGetCString(value);
    5285         4418 :             break;
    5286        10243 :         case NAMEOID:
    5287              :             {
    5288        10243 :                 NameData   *nm = (NameData *) DatumGetPointer(value);
    5289              : 
    5290        10243 :                 val = pstrdup(NameStr(*nm));
    5291        10243 :                 break;
    5292              :             }
    5293            0 :         default:
    5294            0 :             *failure = true;
    5295            0 :             return NULL;
    5296              :     }
    5297              : 
    5298        14661 :     mylocale = pg_newlocale_from_collation(collid);
    5299              : 
    5300        14661 :     if (!mylocale->collate_is_c)
    5301              :     {
    5302              :         char       *xfrmstr;
    5303              :         size_t      xfrmlen;
    5304              :         size_t      xfrmlen2 PG_USED_FOR_ASSERTS_ONLY;
    5305              : 
    5306              :         /*
    5307              :          * XXX: We could guess at a suitable output buffer size and only call
    5308              :          * pg_strxfrm() twice if our guess is too small.
    5309              :          *
    5310              :          * XXX: strxfrm doesn't support UTF-8 encoding on Win32, it can return
    5311              :          * bogus data or set an error. This is not really a problem unless it
    5312              :          * crashes since it will only give an estimation error and nothing
    5313              :          * fatal.
    5314              :          *
    5315              :          * XXX: we do not check pg_strxfrm_enabled(). On some platforms and in
    5316              :          * some cases, libc strxfrm() may return the wrong results, but that
    5317              :          * will only lead to an estimation error.
    5318              :          */
    5319           66 :         xfrmlen = pg_strxfrm(NULL, val, 0, mylocale);
    5320              : #ifdef WIN32
    5321              : 
    5322              :         /*
    5323              :          * On Windows, strxfrm returns INT_MAX when an error occurs. Instead
    5324              :          * of trying to allocate this much memory (and fail), just return the
    5325              :          * original string unmodified as if we were in the C locale.
    5326              :          */
    5327              :         if (xfrmlen == INT_MAX)
    5328              :             return val;
    5329              : #endif
    5330           66 :         xfrmstr = (char *) palloc(xfrmlen + 1);
    5331           66 :         xfrmlen2 = pg_strxfrm(xfrmstr, val, xfrmlen + 1, mylocale);
    5332              : 
    5333              :         /*
    5334              :          * Some systems (e.g., glibc) can return a smaller value from the
    5335              :          * second call than the first; thus the Assert must be <= not ==.
    5336              :          */
    5337              :         Assert(xfrmlen2 <= xfrmlen);
    5338           66 :         pfree(val);
    5339           66 :         val = xfrmstr;
    5340              :     }
    5341              : 
    5342        14661 :     return val;
    5343              : }
    5344              : 
    5345              : /*
    5346              :  * Do convert_to_scalar()'s work for any bytea data type.
    5347              :  *
    5348              :  * Very similar to convert_string_to_scalar except we can't assume
    5349              :  * null-termination and therefore pass explicit lengths around.
    5350              :  *
    5351              :  * Also, assumptions about likely "normal" ranges of characters have been
    5352              :  * removed - a data range of 0..255 is always used, for now.  (Perhaps
    5353              :  * someday we will add information about actual byte data range to
    5354              :  * pg_statistic.)
    5355              :  */
    5356              : static void
    5357            0 : convert_bytea_to_scalar(Datum value,
    5358              :                         double *scaledvalue,
    5359              :                         Datum lobound,
    5360              :                         double *scaledlobound,
    5361              :                         Datum hibound,
    5362              :                         double *scaledhibound)
    5363              : {
    5364            0 :     bytea      *valuep = DatumGetByteaPP(value);
    5365            0 :     bytea      *loboundp = DatumGetByteaPP(lobound);
    5366            0 :     bytea      *hiboundp = DatumGetByteaPP(hibound);
    5367              :     int         rangelo,
    5368              :                 rangehi,
    5369            0 :                 valuelen = VARSIZE_ANY_EXHDR(valuep),
    5370            0 :                 loboundlen = VARSIZE_ANY_EXHDR(loboundp),
    5371            0 :                 hiboundlen = VARSIZE_ANY_EXHDR(hiboundp),
    5372              :                 i,
    5373              :                 minlen;
    5374            0 :     unsigned char *valstr = (unsigned char *) VARDATA_ANY(valuep);
    5375            0 :     unsigned char *lostr = (unsigned char *) VARDATA_ANY(loboundp);
    5376            0 :     unsigned char *histr = (unsigned char *) VARDATA_ANY(hiboundp);
    5377              : 
    5378              :     /*
    5379              :      * Assume bytea data is uniformly distributed across all byte values.
    5380              :      */
    5381            0 :     rangelo = 0;
    5382            0 :     rangehi = 255;
    5383              : 
    5384              :     /*
    5385              :      * Now strip any common prefix of the three strings.
    5386              :      */
    5387            0 :     minlen = Min(Min(valuelen, loboundlen), hiboundlen);
    5388            0 :     for (i = 0; i < minlen; i++)
    5389              :     {
    5390            0 :         if (*lostr != *histr || *lostr != *valstr)
    5391              :             break;
    5392            0 :         lostr++, histr++, valstr++;
    5393            0 :         loboundlen--, hiboundlen--, valuelen--;
    5394              :     }
    5395              : 
    5396              :     /*
    5397              :      * Now we can do the conversions.
    5398              :      */
    5399            0 :     *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
    5400            0 :     *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
    5401            0 :     *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
    5402            0 : }
    5403              : 
    5404              : static double
    5405            0 : convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
    5406              :                             int rangelo, int rangehi)
    5407              : {
    5408              :     double      num,
    5409              :                 denom,
    5410              :                 base;
    5411              : 
    5412            0 :     if (valuelen <= 0)
    5413            0 :         return 0.0;             /* empty string has scalar value 0 */
    5414              : 
    5415              :     /*
    5416              :      * Since base is 256, need not consider more than about 10 chars (even
    5417              :      * this many seems like overkill)
    5418              :      */
    5419            0 :     if (valuelen > 10)
    5420            0 :         valuelen = 10;
    5421              : 
    5422              :     /* Convert initial characters to fraction */
    5423            0 :     base = rangehi - rangelo + 1;
    5424            0 :     num = 0.0;
    5425            0 :     denom = base;
    5426            0 :     while (valuelen-- > 0)
    5427              :     {
    5428            0 :         int         ch = *value++;
    5429              : 
    5430            0 :         if (ch < rangelo)
    5431            0 :             ch = rangelo - 1;
    5432            0 :         else if (ch > rangehi)
    5433            0 :             ch = rangehi + 1;
    5434            0 :         num += ((double) (ch - rangelo)) / denom;
    5435            0 :         denom *= base;
    5436              :     }
    5437              : 
    5438            0 :     return num;
    5439              : }
    5440              : 
    5441              : /*
    5442              :  * Do convert_to_scalar()'s work for any timevalue data type.
    5443              :  *
    5444              :  * On failure (e.g., unsupported typid), set *failure to true;
    5445              :  * otherwise, that variable is not changed.
    5446              :  */
    5447              : static double
    5448            0 : convert_timevalue_to_scalar(Datum value, Oid typid, bool *failure)
    5449              : {
    5450            0 :     switch (typid)
    5451              :     {
    5452            0 :         case TIMESTAMPOID:
    5453            0 :             return DatumGetTimestamp(value);
    5454            0 :         case TIMESTAMPTZOID:
    5455            0 :             return DatumGetTimestampTz(value);
    5456            0 :         case DATEOID:
    5457            0 :             return date2timestamp_no_overflow(DatumGetDateADT(value));
    5458            0 :         case INTERVALOID:
    5459              :             {
    5460            0 :                 Interval   *interval = DatumGetIntervalP(value);
    5461              : 
    5462              :                 /*
    5463              :                  * Convert the month part of Interval to days using assumed
    5464              :                  * average month length of 365.25/12.0 days.  Not too
    5465              :                  * accurate, but plenty good enough for our purposes.
    5466              :                  *
    5467              :                  * This also works for infinite intervals, which just have all
    5468              :                  * fields set to INT_MIN/INT_MAX, and so will produce a result
    5469              :                  * smaller/larger than any finite interval.
    5470              :                  */
    5471            0 :                 return interval->time + interval->day * (double) USECS_PER_DAY +
    5472            0 :                     interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * USECS_PER_DAY);
    5473              :             }
    5474            0 :         case TIMEOID:
    5475            0 :             return DatumGetTimeADT(value);
    5476            0 :         case TIMETZOID:
    5477              :             {
    5478            0 :                 TimeTzADT  *timetz = DatumGetTimeTzADTP(value);
    5479              : 
    5480              :                 /* use GMT-equivalent time */
    5481            0 :                 return (double) (timetz->time + (timetz->zone * 1000000.0));
    5482              :             }
    5483              :     }
    5484              : 
    5485            0 :     *failure = true;
    5486            0 :     return 0;
    5487              : }
    5488              : 
    5489              : 
    5490              : /*
    5491              :  * get_restriction_variable
    5492              :  *      Examine the args of a restriction clause to see if it's of the
    5493              :  *      form (variable op pseudoconstant) or (pseudoconstant op variable),
    5494              :  *      where "variable" could be either a Var or an expression in vars of a
    5495              :  *      single relation.  If so, extract information about the variable,
    5496              :  *      and also indicate which side it was on and the other argument.
    5497              :  *
    5498              :  * Inputs:
    5499              :  *  root: the planner info
    5500              :  *  args: clause argument list
    5501              :  *  varRelid: see specs for restriction selectivity functions
    5502              :  *
    5503              :  * Outputs: (these are valid only if true is returned)
    5504              :  *  *vardata: gets information about variable (see examine_variable)
    5505              :  *  *other: gets other clause argument, aggressively reduced to a constant
    5506              :  *  *varonleft: set true if variable is on the left, false if on the right
    5507              :  *
    5508              :  * Returns true if a variable is identified, otherwise false.
    5509              :  *
    5510              :  * Note: if there are Vars on both sides of the clause, we must fail, because
    5511              :  * callers are expecting that the other side will act like a pseudoconstant.
    5512              :  */
    5513              : bool
    5514       650165 : get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
    5515              :                          VariableStatData *vardata, Node **other,
    5516              :                          bool *varonleft)
    5517              : {
    5518              :     Node       *left,
    5519              :                *right;
    5520              :     VariableStatData rdata;
    5521              : 
    5522              :     /* Fail if not a binary opclause (probably shouldn't happen) */
    5523       650165 :     if (list_length(args) != 2)
    5524            0 :         return false;
    5525              : 
    5526       650165 :     left = (Node *) linitial(args);
    5527       650165 :     right = (Node *) lsecond(args);
    5528              : 
    5529              :     /*
    5530              :      * Examine both sides.  Note that when varRelid is nonzero, Vars of other
    5531              :      * relations will be treated as pseudoconstants.
    5532              :      */
    5533       650165 :     examine_variable(root, left, varRelid, vardata);
    5534       650165 :     examine_variable(root, right, varRelid, &rdata);
    5535              : 
    5536              :     /*
    5537              :      * If one side is a variable and the other not, we win.
    5538              :      */
    5539       650165 :     if (vardata->rel && rdata.rel == NULL)
    5540              :     {
    5541       578744 :         *varonleft = true;
    5542       578744 :         *other = estimate_expression_value(root, rdata.var);
    5543              :         /* Assume we need no ReleaseVariableStats(rdata) here */
    5544       578740 :         return true;
    5545              :     }
    5546              : 
    5547        71421 :     if (vardata->rel == NULL && rdata.rel)
    5548              :     {
    5549        67103 :         *varonleft = false;
    5550        67103 :         *other = estimate_expression_value(root, vardata->var);
    5551              :         /* Assume we need no ReleaseVariableStats(*vardata) here */
    5552        67103 :         *vardata = rdata;
    5553        67103 :         return true;
    5554              :     }
    5555              : 
    5556              :     /* Oops, clause has wrong structure (probably var op var) */
    5557         4318 :     ReleaseVariableStats(*vardata);
    5558         4318 :     ReleaseVariableStats(rdata);
    5559              : 
    5560         4318 :     return false;
    5561              : }
    5562              : 
    5563              : /*
    5564              :  * get_join_variables
    5565              :  *      Apply examine_variable() to each side of a join clause.
    5566              :  *      Also, attempt to identify whether the join clause has the same
    5567              :  *      or reversed sense compared to the SpecialJoinInfo.
    5568              :  *
    5569              :  * We consider the join clause "normal" if it is "lhs_var OP rhs_var",
    5570              :  * or "reversed" if it is "rhs_var OP lhs_var".  In complicated cases
    5571              :  * where we can't tell for sure, we default to assuming it's normal.
    5572              :  */
    5573              : void
    5574       219805 : get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo,
    5575              :                    VariableStatData *vardata1, VariableStatData *vardata2,
    5576              :                    bool *join_is_reversed)
    5577              : {
    5578              :     Node       *left,
    5579              :                *right;
    5580              : 
    5581       219805 :     if (list_length(args) != 2)
    5582            0 :         elog(ERROR, "join operator should take two arguments");
    5583              : 
    5584       219805 :     left = (Node *) linitial(args);
    5585       219805 :     right = (Node *) lsecond(args);
    5586              : 
    5587       219805 :     examine_variable(root, left, 0, vardata1);
    5588       219805 :     examine_variable(root, right, 0, vardata2);
    5589              : 
    5590       439399 :     if (vardata1->rel &&
    5591       219594 :         bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
    5592        71821 :         *join_is_reversed = true;   /* var1 is on RHS */
    5593       295757 :     else if (vardata2->rel &&
    5594       147773 :              bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
    5595          304 :         *join_is_reversed = true;   /* var2 is on LHS */
    5596              :     else
    5597       147680 :         *join_is_reversed = false;
    5598       219805 : }
    5599              : 
    5600              : /* statext_expressions_load copies the tuple, so just pfree it. */
    5601              : static void
    5602         1415 : ReleaseDummy(HeapTuple tuple)
    5603              : {
    5604         1415 :     pfree(tuple);
    5605         1415 : }
    5606              : 
    5607              : /*
    5608              :  * examine_variable
    5609              :  *      Try to look up statistical data about an expression.
    5610              :  *      Fill in a VariableStatData struct to describe the expression.
    5611              :  *
    5612              :  * Inputs:
    5613              :  *  root: the planner info
    5614              :  *  node: the expression tree to examine
    5615              :  *  varRelid: see specs for restriction selectivity functions
    5616              :  *
    5617              :  * Outputs: *vardata is filled as follows:
    5618              :  *  var: the input expression (with any phvs or binary relabeling stripped,
    5619              :  *      if it is or contains a variable; but otherwise unchanged)
    5620              :  *  rel: RelOptInfo for relation containing variable; NULL if expression
    5621              :  *      contains no Vars (NOTE this could point to a RelOptInfo of a
    5622              :  *      subquery, not one in the current query).
    5623              :  *  statsTuple: the pg_statistic entry for the variable, if one exists;
    5624              :  *      otherwise NULL.
    5625              :  *  freefunc: pointer to a function to release statsTuple with.
    5626              :  *  vartype: exposed type of the expression; this should always match
    5627              :  *      the declared input type of the operator we are estimating for.
    5628              :  *  atttype, atttypmod: actual type/typmod of the "var" expression.  This is
    5629              :  *      commonly the same as the exposed type of the variable argument,
    5630              :  *      but can be different in binary-compatible-type cases.
    5631              :  *  isunique: true if we were able to match the var to a unique index, a
    5632              :  *      single-column DISTINCT or GROUP-BY clause, implying its values are
    5633              :  *      unique for this query.  (Caution: this should be trusted for
    5634              :  *      statistical purposes only, since we do not check indimmediate nor
    5635              :  *      verify that the exact same definition of equality applies.)
    5636              :  *  acl_ok: true if current user has permission to read all table rows from
    5637              :  *      the column(s) underlying the pg_statistic entry.  This is consulted by
    5638              :  *      statistic_proc_security_check().
    5639              :  *
    5640              :  * Caller is responsible for doing ReleaseVariableStats() before exiting.
    5641              :  */
    5642              : void
    5643      2571809 : examine_variable(PlannerInfo *root, Node *node, int varRelid,
    5644              :                  VariableStatData *vardata)
    5645              : {
    5646              :     Node       *basenode;
    5647              :     Relids      varnos;
    5648              :     Relids      basevarnos;
    5649              :     RelOptInfo *onerel;
    5650              : 
    5651              :     /* Make sure we don't return dangling pointers in vardata */
    5652     18002663 :     MemSet(vardata, 0, sizeof(VariableStatData));
    5653              : 
    5654              :     /* Save the exposed type of the expression */
    5655      2571809 :     vardata->vartype = exprType(node);
    5656              : 
    5657              :     /*
    5658              :      * PlaceHolderVars are transparent for the purpose of statistics lookup;
    5659              :      * they do not alter the value distribution of the underlying expression.
    5660              :      * However, they can obscure the structure, preventing us from recognizing
    5661              :      * matches to base columns, index expressions, or extended statistics.  So
    5662              :      * strip them out first.
    5663              :      */
    5664      2571809 :     basenode = strip_all_phvs_deep(root, node);
    5665              : 
    5666              :     /*
    5667              :      * Look inside any binary-compatible relabeling.  We need to handle nested
    5668              :      * RelabelType nodes here, because the prior stripping of PlaceHolderVars
    5669              :      * may have brought separate RelabelTypes into adjacency.
    5670              :      */
    5671      2612268 :     while (IsA(basenode, RelabelType))
    5672        40459 :         basenode = (Node *) ((RelabelType *) basenode)->arg;
    5673              : 
    5674              :     /* Fast path for a simple Var */
    5675      2571809 :     if (IsA(basenode, Var) &&
    5676       675807 :         (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
    5677              :     {
    5678      1831936 :         Var        *var = (Var *) basenode;
    5679              : 
    5680              :         /* Set up result fields other than the stats tuple */
    5681      1831936 :         vardata->var = basenode; /* return Var without phvs or relabeling */
    5682      1831936 :         vardata->rel = find_base_rel(root, var->varno);
    5683      1831936 :         vardata->atttype = var->vartype;
    5684      1831936 :         vardata->atttypmod = var->vartypmod;
    5685      1831936 :         vardata->isunique = has_unique_index(vardata->rel, var->varattno);
    5686              : 
    5687              :         /* Try to locate some stats */
    5688      1831936 :         examine_simple_variable(root, var, vardata);
    5689              : 
    5690      1831936 :         return;
    5691              :     }
    5692              : 
    5693              :     /*
    5694              :      * Okay, it's a more complicated expression.  Determine variable
    5695              :      * membership.  Note that when varRelid isn't zero, only vars of that
    5696              :      * relation are considered "real" vars.
    5697              :      */
    5698       739873 :     varnos = pull_varnos(root, basenode);
    5699       739873 :     basevarnos = bms_difference(varnos, root->outer_join_rels);
    5700              : 
    5701       739873 :     onerel = NULL;
    5702              : 
    5703       739873 :     if (bms_is_empty(basevarnos))
    5704              :     {
    5705              :         /* No Vars at all ... must be pseudo-constant clause */
    5706              :     }
    5707              :     else
    5708              :     {
    5709              :         int         relid;
    5710              : 
    5711              :         /* Check if the expression is in vars of a single base relation */
    5712       394823 :         if (bms_get_singleton_member(basevarnos, &relid))
    5713              :         {
    5714       391149 :             if (varRelid == 0 || varRelid == relid)
    5715              :             {
    5716        54231 :                 onerel = find_base_rel(root, relid);
    5717        54231 :                 vardata->rel = onerel;
    5718        54231 :                 node = basenode;    /* strip any phvs or relabeling */
    5719              :             }
    5720              :             /* else treat it as a constant */
    5721              :         }
    5722              :         else
    5723              :         {
    5724              :             /* varnos has multiple relids */
    5725         3674 :             if (varRelid == 0)
    5726              :             {
    5727              :                 /* treat it as a variable of a join relation */
    5728         2688 :                 vardata->rel = find_join_rel(root, varnos);
    5729         2688 :                 node = basenode;    /* strip any phvs or relabeling */
    5730              :             }
    5731          986 :             else if (bms_is_member(varRelid, varnos))
    5732              :             {
    5733              :                 /* ignore the vars belonging to other relations */
    5734          891 :                 vardata->rel = find_base_rel(root, varRelid);
    5735          891 :                 node = basenode;    /* strip any phvs or relabeling */
    5736              :                 /* note: no point in expressional-index search here */
    5737              :             }
    5738              :             /* else treat it as a constant */
    5739              :         }
    5740              :     }
    5741              : 
    5742       739873 :     bms_free(basevarnos);
    5743              : 
    5744       739873 :     vardata->var = node;
    5745       739873 :     vardata->atttype = exprType(node);
    5746       739873 :     vardata->atttypmod = exprTypmod(node);
    5747              : 
    5748       739873 :     if (onerel)
    5749              :     {
    5750              :         /*
    5751              :          * We have an expression in vars of a single relation.  Try to match
    5752              :          * it to expressional index columns, in hopes of finding some
    5753              :          * statistics.
    5754              :          *
    5755              :          * Note that we consider all index columns including INCLUDE columns,
    5756              :          * since there could be stats for such columns.  But the test for
    5757              :          * uniqueness needs to be warier.
    5758              :          *
    5759              :          * XXX it's conceivable that there are multiple matches with different
    5760              :          * index opfamilies; if so, we need to pick one that matches the
    5761              :          * operator we are estimating for.  FIXME later.
    5762              :          */
    5763              :         ListCell   *ilist;
    5764              :         ListCell   *slist;
    5765              : 
    5766              :         /*
    5767              :          * The nullingrels bits within the expression could prevent us from
    5768              :          * matching it to expressional index columns or to the expressions in
    5769              :          * extended statistics.  So strip them out first.
    5770              :          */
    5771        54231 :         if (bms_overlap(varnos, root->outer_join_rels))
    5772         1577 :             node = remove_nulling_relids(node, root->outer_join_rels, NULL);
    5773              : 
    5774       119484 :         foreach(ilist, onerel->indexlist)
    5775              :         {
    5776        67630 :             IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
    5777              :             ListCell   *indexpr_item;
    5778              :             int         pos;
    5779              : 
    5780        67630 :             indexpr_item = list_head(index->indexprs);
    5781        67630 :             if (indexpr_item == NULL)
    5782        63678 :                 continue;       /* no expressions here... */
    5783              : 
    5784         5587 :             for (pos = 0; pos < index->ncolumns; pos++)
    5785              :             {
    5786         4012 :                 if (index->indexkeys[pos] == 0)
    5787              :                 {
    5788              :                     Node       *indexkey;
    5789              : 
    5790         3952 :                     if (indexpr_item == NULL)
    5791            0 :                         elog(ERROR, "too few entries in indexprs list");
    5792         3952 :                     indexkey = (Node *) lfirst(indexpr_item);
    5793         3952 :                     if (indexkey && IsA(indexkey, RelabelType))
    5794            0 :                         indexkey = (Node *) ((RelabelType *) indexkey)->arg;
    5795         3952 :                     if (equal(node, indexkey))
    5796              :                     {
    5797              :                         /*
    5798              :                          * Found a match ... is it a unique index? Tests here
    5799              :                          * should match has_unique_index().
    5800              :                          */
    5801         2907 :                         if (index->unique &&
    5802          365 :                             index->nkeycolumns == 1 &&
    5803          365 :                             pos == 0 &&
    5804          365 :                             (index->indpred == NIL || index->predOK))
    5805          365 :                             vardata->isunique = true;
    5806              : 
    5807              :                         /*
    5808              :                          * Has it got stats?  We only consider stats for
    5809              :                          * non-partial indexes, since partial indexes probably
    5810              :                          * don't reflect whole-relation statistics; the above
    5811              :                          * check for uniqueness is the only info we take from
    5812              :                          * a partial index.
    5813              :                          *
    5814              :                          * An index stats hook, however, must make its own
    5815              :                          * decisions about what to do with partial indexes.
    5816              :                          */
    5817         2907 :                         if (get_index_stats_hook &&
    5818            0 :                             (*get_index_stats_hook) (root, index->indexoid,
    5819            0 :                                                      pos + 1, vardata))
    5820              :                         {
    5821              :                             /*
    5822              :                              * The hook took control of acquiring a stats
    5823              :                              * tuple.  If it did supply a tuple, it'd better
    5824              :                              * have supplied a freefunc.
    5825              :                              */
    5826            0 :                             if (HeapTupleIsValid(vardata->statsTuple) &&
    5827            0 :                                 !vardata->freefunc)
    5828            0 :                                 elog(ERROR, "no function provided to release variable stats with");
    5829              :                         }
    5830         2907 :                         else if (index->indpred == NIL)
    5831              :                         {
    5832         2907 :                             vardata->statsTuple =
    5833         5814 :                                 SearchSysCache3(STATRELATTINH,
    5834              :                                                 ObjectIdGetDatum(index->indexoid),
    5835         2907 :                                                 Int16GetDatum(pos + 1),
    5836              :                                                 BoolGetDatum(false));
    5837         2907 :                             vardata->freefunc = ReleaseSysCache;
    5838              : 
    5839         2907 :                             if (HeapTupleIsValid(vardata->statsTuple))
    5840              :                             {
    5841              :                                 /*
    5842              :                                  * Test if user has permission to access all
    5843              :                                  * rows from the index's table.
    5844              :                                  *
    5845              :                                  * For simplicity, we insist on the whole
    5846              :                                  * table being selectable, rather than trying
    5847              :                                  * to identify which column(s) the index
    5848              :                                  * depends on.
    5849              :                                  *
    5850              :                                  * Note that for an inheritance child,
    5851              :                                  * permissions are checked on the inheritance
    5852              :                                  * root parent, and whole-table select
    5853              :                                  * privilege on the parent doesn't quite
    5854              :                                  * guarantee that the user could read all
    5855              :                                  * columns of the child.  But in practice it's
    5856              :                                  * unlikely that any interesting security
    5857              :                                  * violation could result from allowing access
    5858              :                                  * to the expression index's stats, so we
    5859              :                                  * allow it anyway.  See similar code in
    5860              :                                  * examine_simple_variable() for additional
    5861              :                                  * comments.
    5862              :                                  */
    5863         2377 :                                 vardata->acl_ok =
    5864         2377 :                                     all_rows_selectable(root,
    5865         2377 :                                                         index->rel->relid,
    5866              :                                                         NULL);
    5867              :                             }
    5868              :                             else
    5869              :                             {
    5870              :                                 /* suppress leakproofness checks later */
    5871          530 :                                 vardata->acl_ok = true;
    5872              :                             }
    5873              :                         }
    5874         2907 :                         if (vardata->statsTuple)
    5875         2377 :                             break;
    5876              :                     }
    5877         1575 :                     indexpr_item = lnext(index->indexprs, indexpr_item);
    5878              :                 }
    5879              :             }
    5880         3952 :             if (vardata->statsTuple)
    5881         2377 :                 break;
    5882              :         }
    5883              : 
    5884              :         /*
    5885              :          * Search extended statistics for one with a matching expression.
    5886              :          * There might be multiple ones, so just grab the first one. In the
    5887              :          * future, we might consider the statistics target (and pick the most
    5888              :          * accurate statistics) and maybe some other parameters.
    5889              :          */
    5890        57760 :         foreach(slist, onerel->statlist)
    5891              :         {
    5892         3774 :             StatisticExtInfo *info = (StatisticExtInfo *) lfirst(slist);
    5893         3774 :             RangeTblEntry *rte = planner_rt_fetch(onerel->relid, root);
    5894              :             ListCell   *expr_item;
    5895              :             int         pos;
    5896              : 
    5897              :             /*
    5898              :              * Stop once we've found statistics for the expression (either
    5899              :              * from extended stats, or for an index in the preceding loop).
    5900              :              */
    5901         3774 :             if (vardata->statsTuple)
    5902          245 :                 break;
    5903              : 
    5904              :             /* skip stats without per-expression stats */
    5905         3529 :             if (info->kind != STATS_EXT_EXPRESSIONS)
    5906         1808 :                 continue;
    5907              : 
    5908              :             /* skip stats with mismatching stxdinherit value */
    5909         1721 :             if (info->inherit != rte->inh)
    5910            5 :                 continue;
    5911              : 
    5912         1716 :             pos = 0;
    5913         2836 :             foreach(expr_item, info->exprs)
    5914              :             {
    5915         2536 :                 Node       *expr = (Node *) lfirst(expr_item);
    5916              : 
    5917              :                 Assert(expr);
    5918              : 
    5919              :                 /* strip RelabelType before comparing it */
    5920         2536 :                 if (expr && IsA(expr, RelabelType))
    5921            0 :                     expr = (Node *) ((RelabelType *) expr)->arg;
    5922              : 
    5923              :                 /* found a match, see if we can extract pg_statistic row */
    5924         2536 :                 if (equal(node, expr))
    5925              :                 {
    5926              :                     /*
    5927              :                      * XXX Not sure if we should cache the tuple somewhere.
    5928              :                      * Now we just create a new copy every time.
    5929              :                      */
    5930         1416 :                     vardata->statsTuple =
    5931         1416 :                         statext_expressions_load(info->statOid, rte->inh, pos);
    5932              : 
    5933              :                     /* Nothing to release if no data found */
    5934         1416 :                     if (vardata->statsTuple != NULL)
    5935              :                     {
    5936         1415 :                         vardata->freefunc = ReleaseDummy;
    5937              :                     }
    5938              : 
    5939              :                     /*
    5940              :                      * Test if user has permission to access all rows from the
    5941              :                      * table.
    5942              :                      *
    5943              :                      * For simplicity, we insist on the whole table being
    5944              :                      * selectable, rather than trying to identify which
    5945              :                      * column(s) the statistics object depends on.
    5946              :                      *
    5947              :                      * Note that for an inheritance child, permissions are
    5948              :                      * checked on the inheritance root parent, and whole-table
    5949              :                      * select privilege on the parent doesn't quite guarantee
    5950              :                      * that the user could read all columns of the child.  But
    5951              :                      * in practice it's unlikely that any interesting security
    5952              :                      * violation could result from allowing access to the
    5953              :                      * expression stats, so we allow it anyway.  See similar
    5954              :                      * code in examine_simple_variable() for additional
    5955              :                      * comments.
    5956              :                      */
    5957         1416 :                     vardata->acl_ok = all_rows_selectable(root,
    5958              :                                                           onerel->relid,
    5959              :                                                           NULL);
    5960              : 
    5961         1416 :                     break;
    5962              :                 }
    5963              : 
    5964         1120 :                 pos++;
    5965              :             }
    5966              :         }
    5967              :     }
    5968              : 
    5969       739873 :     bms_free(varnos);
    5970              : }
    5971              : 
    5972              : /*
    5973              :  * strip_all_phvs_deep
    5974              :  *      Deeply strip all PlaceHolderVars in an expression.
    5975              :  *
    5976              :  * As a performance optimization, we first use a lightweight walker to check
    5977              :  * for the presence of any PlaceHolderVars.  The expensive mutator is invoked
    5978              :  * only if a PlaceHolderVar is found, avoiding unnecessary memory allocation
    5979              :  * and tree copying in the common case where no PlaceHolderVars are present.
    5980              :  */
    5981              : static Node *
    5982      2571809 : strip_all_phvs_deep(PlannerInfo *root, Node *node)
    5983              : {
    5984              :     /* If there are no PHVs anywhere, we needn't work hard */
    5985      2571809 :     if (root->glob->lastPHId == 0)
    5986      2545786 :         return node;
    5987              : 
    5988        26023 :     if (!contain_placeholder_walker(node, NULL))
    5989        22345 :         return node;
    5990         3678 :     return strip_all_phvs_mutator(node, NULL);
    5991              : }
    5992              : 
    5993              : /*
    5994              :  * contain_placeholder_walker
    5995              :  *      Lightweight walker to check if an expression contains any
    5996              :  *      PlaceHolderVars
    5997              :  */
    5998              : static bool
    5999        29349 : contain_placeholder_walker(Node *node, void *context)
    6000              : {
    6001        29349 :     if (node == NULL)
    6002          109 :         return false;
    6003        29240 :     if (IsA(node, PlaceHolderVar))
    6004         3678 :         return true;
    6005              : 
    6006        25562 :     return expression_tree_walker(node, contain_placeholder_walker, context);
    6007              : }
    6008              : 
    6009              : /*
    6010              :  * strip_all_phvs_mutator
    6011              :  *      Mutator to deeply strip all PlaceHolderVars
    6012              :  */
    6013              : static Node *
    6014         9809 : strip_all_phvs_mutator(Node *node, void *context)
    6015              : {
    6016         9809 :     if (node == NULL)
    6017           34 :         return NULL;
    6018         9775 :     if (IsA(node, PlaceHolderVar))
    6019              :     {
    6020              :         /* Strip it and recurse into its contained expression */
    6021         3798 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    6022              : 
    6023         3798 :         return strip_all_phvs_mutator((Node *) phv->phexpr, context);
    6024              :     }
    6025              : 
    6026         5977 :     return expression_tree_mutator(node, strip_all_phvs_mutator, context);
    6027              : }
    6028              : 
    6029              : /*
    6030              :  * examine_simple_variable
    6031              :  *      Handle a simple Var for examine_variable
    6032              :  *
    6033              :  * This is split out as a subroutine so that we can recurse to deal with
    6034              :  * Vars referencing subqueries (either sub-SELECT-in-FROM or CTE style).
    6035              :  *
    6036              :  * We already filled in all the fields of *vardata except for the stats tuple.
    6037              :  */
    6038              : static void
    6039      1840040 : examine_simple_variable(PlannerInfo *root, Var *var,
    6040              :                         VariableStatData *vardata)
    6041              : {
    6042      1840040 :     RangeTblEntry *rte = root->simple_rte_array[var->varno];
    6043              : 
    6044              :     Assert(IsA(rte, RangeTblEntry));
    6045              : 
    6046      1840040 :     if (get_relation_stats_hook &&
    6047            0 :         (*get_relation_stats_hook) (root, rte, var->varattno, vardata))
    6048              :     {
    6049              :         /*
    6050              :          * The hook took control of acquiring a stats tuple.  If it did supply
    6051              :          * a tuple, it'd better have supplied a freefunc.
    6052              :          */
    6053            0 :         if (HeapTupleIsValid(vardata->statsTuple) &&
    6054            0 :             !vardata->freefunc)
    6055            0 :             elog(ERROR, "no function provided to release variable stats with");
    6056              :     }
    6057      1840040 :     else if (rte->rtekind == RTE_RELATION)
    6058              :     {
    6059              :         /*
    6060              :          * Plain table or parent of an inheritance appendrel, so look up the
    6061              :          * column in pg_statistic
    6062              :          */
    6063      1748020 :         vardata->statsTuple = SearchSysCache3(STATRELATTINH,
    6064              :                                               ObjectIdGetDatum(rte->relid),
    6065      1748020 :                                               Int16GetDatum(var->varattno),
    6066      1748020 :                                               BoolGetDatum(rte->inh));
    6067      1748020 :         vardata->freefunc = ReleaseSysCache;
    6068              : 
    6069      1748020 :         if (HeapTupleIsValid(vardata->statsTuple))
    6070              :         {
    6071              :             /*
    6072              :              * Test if user has permission to read all rows from this column.
    6073              :              *
    6074              :              * This requires that the user has the appropriate SELECT
    6075              :              * privileges and that there are no securityQuals from security
    6076              :              * barrier views or RLS policies.  If that's not the case, then we
    6077              :              * only permit leakproof functions to be passed pg_statistic data
    6078              :              * in vardata, otherwise the functions might reveal data that the
    6079              :              * user doesn't have permission to see --- see
    6080              :              * statistic_proc_security_check().
    6081              :              */
    6082      1153241 :             vardata->acl_ok =
    6083      1153241 :                 all_rows_selectable(root, var->varno,
    6084      1153241 :                                     bms_make_singleton(var->varattno - FirstLowInvalidHeapAttributeNumber));
    6085              :         }
    6086              :         else
    6087              :         {
    6088              :             /* suppress any possible leakproofness checks later */
    6089       594779 :             vardata->acl_ok = true;
    6090              :         }
    6091              :     }
    6092        92020 :     else if ((rte->rtekind == RTE_SUBQUERY && !rte->inh) ||
    6093        84205 :              (rte->rtekind == RTE_CTE && !rte->self_reference))
    6094              :     {
    6095              :         /*
    6096              :          * Plain subquery (not one that was converted to an appendrel) or
    6097              :          * non-recursive CTE.  In either case, we can try to find out what the
    6098              :          * Var refers to within the subquery.  We skip this for appendrel and
    6099              :          * recursive-CTE cases because any column stats we did find would
    6100              :          * likely not be very relevant.
    6101              :          */
    6102              :         PlannerInfo *subroot;
    6103              :         Query      *subquery;
    6104              :         List       *subtlist;
    6105              :         TargetEntry *ste;
    6106              : 
    6107              :         /*
    6108              :          * Punt if it's a whole-row var rather than a plain column reference.
    6109              :          */
    6110        15969 :         if (var->varattno == InvalidAttrNumber)
    6111            0 :             return;
    6112              : 
    6113              :         /*
    6114              :          * Otherwise, find the subquery's planner subroot.
    6115              :          */
    6116        15969 :         if (rte->rtekind == RTE_SUBQUERY)
    6117              :         {
    6118              :             RelOptInfo *rel;
    6119              : 
    6120              :             /*
    6121              :              * Fetch RelOptInfo for subquery.  Note that we don't change the
    6122              :              * rel returned in vardata, since caller expects it to be a rel of
    6123              :              * the caller's query level.  Because we might already be
    6124              :              * recursing, we can't use that rel pointer either, but have to
    6125              :              * look up the Var's rel afresh.
    6126              :              */
    6127         7815 :             rel = find_base_rel(root, var->varno);
    6128              : 
    6129         7815 :             subroot = rel->subroot;
    6130              :         }
    6131              :         else
    6132              :         {
    6133              :             /* CTE case is more difficult */
    6134              :             PlannerInfo *cteroot;
    6135              :             Index       levelsup;
    6136              :             int         ndx;
    6137              :             int         plan_id;
    6138              :             ListCell   *lc;
    6139              : 
    6140              :             /*
    6141              :              * Find the referenced CTE, and locate the subroot previously made
    6142              :              * for it.
    6143              :              */
    6144         8154 :             levelsup = rte->ctelevelsup;
    6145         8154 :             cteroot = root;
    6146        19208 :             while (levelsup-- > 0)
    6147              :             {
    6148        11054 :                 cteroot = cteroot->parent_root;
    6149        11054 :                 if (!cteroot)   /* shouldn't happen */
    6150            0 :                     elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
    6151              :             }
    6152              : 
    6153              :             /*
    6154              :              * Note: cte_plan_ids can be shorter than cteList, if we are still
    6155              :              * working on planning the CTEs (ie, this is a side-reference from
    6156              :              * another CTE).  So we mustn't use forboth here.
    6157              :              */
    6158         8154 :             ndx = 0;
    6159        11204 :             foreach(lc, cteroot->parse->cteList)
    6160              :             {
    6161        11204 :                 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
    6162              : 
    6163        11204 :                 if (strcmp(cte->ctename, rte->ctename) == 0)
    6164         8154 :                     break;
    6165         3050 :                 ndx++;
    6166              :             }
    6167         8154 :             if (lc == NULL)     /* shouldn't happen */
    6168            0 :                 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
    6169         8154 :             if (ndx >= list_length(cteroot->cte_plan_ids))
    6170            0 :                 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
    6171         8154 :             plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
    6172         8154 :             if (plan_id <= 0)
    6173            0 :                 elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
    6174         8154 :             subroot = list_nth(root->glob->subroots, plan_id - 1);
    6175              :         }
    6176              : 
    6177              :         /* If the subquery hasn't been planned yet, we have to punt */
    6178        15969 :         if (subroot == NULL)
    6179            0 :             return;
    6180              :         Assert(IsA(subroot, PlannerInfo));
    6181              : 
    6182              :         /*
    6183              :          * We must use the subquery parsetree as mangled by the planner, not
    6184              :          * the raw version from the RTE, because we need a Var that will refer
    6185              :          * to the subroot's live RelOptInfos.  For instance, if any subquery
    6186              :          * pullup happened during planning, Vars in the targetlist might have
    6187              :          * gotten replaced, and we need to see the replacement expressions.
    6188              :          */
    6189        15969 :         subquery = subroot->parse;
    6190              :         Assert(IsA(subquery, Query));
    6191              : 
    6192              :         /*
    6193              :          * Punt if subquery uses set operations or grouping sets, as these
    6194              :          * will mash underlying columns' stats beyond recognition.  (Set ops
    6195              :          * are particularly nasty; if we forged ahead, we would return stats
    6196              :          * relevant to only the leftmost subselect...)  DISTINCT is also
    6197              :          * problematic, but we check that later because there is a possibility
    6198              :          * of learning something even with it.
    6199              :          */
    6200        15969 :         if (subquery->setOperations ||
    6201        14391 :             subquery->groupingSets)
    6202         1655 :             return;
    6203              : 
    6204              :         /* Get the subquery output expression referenced by the upper Var */
    6205        14314 :         if (subquery->returningList)
    6206          179 :             subtlist = subquery->returningList;
    6207              :         else
    6208        14135 :             subtlist = subquery->targetList;
    6209        14314 :         ste = get_tle_by_resno(subtlist, var->varattno);
    6210        14314 :         if (ste == NULL || ste->resjunk)
    6211            0 :             elog(ERROR, "subquery %s does not have attribute %d",
    6212              :                  rte->eref->aliasname, var->varattno);
    6213        14314 :         var = (Var *) ste->expr;
    6214              : 
    6215              :         /*
    6216              :          * If subquery uses DISTINCT, we can't make use of any stats for the
    6217              :          * variable ... but, if it's the only DISTINCT column, we are entitled
    6218              :          * to consider it unique.  We do the test this way so that it works
    6219              :          * for cases involving DISTINCT ON.
    6220              :          */
    6221        14314 :         if (subquery->distinctClause)
    6222              :         {
    6223         1420 :             if (list_length(subquery->distinctClause) == 1 &&
    6224          475 :                 targetIsInSortList(ste, InvalidOid, subquery->distinctClause))
    6225          314 :                 vardata->isunique = true;
    6226              :             /* cannot go further */
    6227          945 :             return;
    6228              :         }
    6229              : 
    6230              :         /* The same idea as with DISTINCT clause works for a GROUP-BY too */
    6231        13369 :         if (subquery->groupClause)
    6232              :         {
    6233          947 :             if (list_length(subquery->groupClause) == 1 &&
    6234          402 :                 targetIsInSortList(ste, InvalidOid, subquery->groupClause))
    6235          322 :                 vardata->isunique = true;
    6236              :             /* cannot go further */
    6237          545 :             return;
    6238              :         }
    6239              : 
    6240              :         /*
    6241              :          * If the sub-query originated from a view with the security_barrier
    6242              :          * attribute, we must not look at the variable's statistics, though it
    6243              :          * seems all right to notice the existence of a DISTINCT clause. So
    6244              :          * stop here.
    6245              :          *
    6246              :          * This is probably a harsher restriction than necessary; it's
    6247              :          * certainly OK for the selectivity estimator (which is a C function,
    6248              :          * and therefore omnipotent anyway) to look at the statistics.  But
    6249              :          * many selectivity estimators will happily *invoke the operator
    6250              :          * function* to try to work out a good estimate - and that's not OK.
    6251              :          * So for now, don't dig down for stats.
    6252              :          */
    6253        12824 :         if (rte->security_barrier)
    6254          473 :             return;
    6255              : 
    6256              :         /* Can only handle a simple Var of subquery's query level */
    6257        12351 :         if (var && IsA(var, Var) &&
    6258         8104 :             var->varlevelsup == 0)
    6259              :         {
    6260              :             /*
    6261              :              * OK, recurse into the subquery.  Note that the original setting
    6262              :              * of vardata->isunique (which will surely be false) is left
    6263              :              * unchanged in this situation.  That's what we want, since even
    6264              :              * if the underlying column is unique, the subquery may have
    6265              :              * joined to other tables in a way that creates duplicates.
    6266              :              */
    6267         8104 :             examine_simple_variable(subroot, var, vardata);
    6268              :         }
    6269              :     }
    6270              :     else
    6271              :     {
    6272              :         /*
    6273              :          * Otherwise, the Var comes from a FUNCTION or VALUES RTE.  (We won't
    6274              :          * see RTE_JOIN here because join alias Vars have already been
    6275              :          * flattened.)  There's not much we can do with function outputs, but
    6276              :          * maybe someday try to be smarter about VALUES.
    6277              :          */
    6278              :     }
    6279              : }
    6280              : 
    6281              : /*
    6282              :  * all_rows_selectable
    6283              :  *      Test whether the user has permission to select all rows from a given
    6284              :  *      relation.
    6285              :  *
    6286              :  * Inputs:
    6287              :  *  root: the planner info
    6288              :  *  varno: the index of the relation (assumed to be an RTE_RELATION)
    6289              :  *  varattnos: the attributes for which permission is required, or NULL if
    6290              :  *      whole-table access is required
    6291              :  *
    6292              :  * Returns true if the user has the required select permissions, and there are
    6293              :  * no securityQuals from security barrier views or RLS policies.
    6294              :  *
    6295              :  * Note that if the relation is an inheritance child relation, securityQuals
    6296              :  * and access permissions are checked against the inheritance root parent (the
    6297              :  * relation actually mentioned in the query) --- see the comments in
    6298              :  * expand_single_inheritance_child() for an explanation of why it has to be
    6299              :  * done this way.
    6300              :  *
    6301              :  * If varattnos is non-NULL, its attribute numbers should be offset by
    6302              :  * FirstLowInvalidHeapAttributeNumber so that system attributes can be
    6303              :  * checked.  If varattnos is NULL, only table-level SELECT privileges are
    6304              :  * checked, not any column-level privileges.
    6305              :  *
    6306              :  * Note: if the relation is accessed via a view, this function actually tests
    6307              :  * whether the view owner has permission to select from the relation.  To
    6308              :  * ensure that the current user has permission, it is also necessary to check
    6309              :  * that the current user has permission to select from the view, which we do
    6310              :  * at planner-startup --- see subquery_planner().
    6311              :  *
    6312              :  * This is exported so that other estimation functions can use it.
    6313              :  */
    6314              : bool
    6315      1157244 : all_rows_selectable(PlannerInfo *root, Index varno, Bitmapset *varattnos)
    6316              : {
    6317      1157244 :     RelOptInfo *rel = find_base_rel_noerr(root, varno);
    6318      1157244 :     RangeTblEntry *rte = planner_rt_fetch(varno, root);
    6319              :     Oid         userid;
    6320              :     int         varattno;
    6321              : 
    6322              :     Assert(rte->rtekind == RTE_RELATION);
    6323              : 
    6324              :     /*
    6325              :      * Determine the user ID to use for privilege checks (either the current
    6326              :      * user or the view owner, if we're accessing the table via a view).
    6327              :      *
    6328              :      * Normally the relation will have an associated RelOptInfo from which we
    6329              :      * can find the userid, but it might not if it's a RETURNING Var for an
    6330              :      * INSERT target relation.  In that case use the RTEPermissionInfo
    6331              :      * associated with the RTE.
    6332              :      *
    6333              :      * If we navigate up to a parent relation, we keep using the same userid,
    6334              :      * since it's the same in all relations of a given inheritance tree.
    6335              :      */
    6336      1157244 :     if (rel)
    6337      1157211 :         userid = rel->userid;
    6338              :     else
    6339              :     {
    6340              :         RTEPermissionInfo *perminfo;
    6341              : 
    6342           33 :         perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
    6343           33 :         userid = perminfo->checkAsUser;
    6344              :     }
    6345      1157244 :     if (!OidIsValid(userid))
    6346      1026518 :         userid = GetUserId();
    6347              : 
    6348              :     /*
    6349              :      * Permissions and securityQuals must be checked on the table actually
    6350              :      * mentioned in the query, so if this is an inheritance child, navigate up
    6351              :      * to the inheritance root parent.  If the user can read the whole table
    6352              :      * or the required columns there, then they can read from the child table
    6353              :      * too.  For per-column checks, we must find out which of the root
    6354              :      * parent's attributes the child relation's attributes correspond to.
    6355              :      */
    6356      1157244 :     if (root->append_rel_array != NULL)
    6357              :     {
    6358              :         AppendRelInfo *appinfo;
    6359              : 
    6360       177728 :         appinfo = root->append_rel_array[varno];
    6361              : 
    6362              :         /*
    6363              :          * Partitions are mapped to their immediate parent, not the root
    6364              :          * parent, so must be ready to walk up multiple AppendRelInfos.  But
    6365              :          * stop if we hit a parent that is not RTE_RELATION --- that's a
    6366              :          * flattened UNION ALL subquery, not an inheritance parent.
    6367              :          */
    6368       334070 :         while (appinfo &&
    6369       156727 :                planner_rt_fetch(appinfo->parent_relid,
    6370       156727 :                                 root)->rtekind == RTE_RELATION)
    6371              :         {
    6372       156342 :             Bitmapset  *parent_varattnos = NULL;
    6373              : 
    6374              :             /*
    6375              :              * For each child attribute, find the corresponding parent
    6376              :              * attribute.  In rare cases, the attribute may be local to the
    6377              :              * child table, in which case, we've got to live with having no
    6378              :              * access to this column.
    6379              :              */
    6380       156342 :             varattno = -1;
    6381       310417 :             while ((varattno = bms_next_member(varattnos, varattno)) >= 0)
    6382              :             {
    6383              :                 AttrNumber  attno;
    6384              :                 AttrNumber  parent_attno;
    6385              : 
    6386       154075 :                 attno = varattno + FirstLowInvalidHeapAttributeNumber;
    6387              : 
    6388       154075 :                 if (attno == InvalidAttrNumber)
    6389              :                 {
    6390              :                     /*
    6391              :                      * Whole-row reference, so must map each column of the
    6392              :                      * child to the parent table.
    6393              :                      */
    6394           30 :                     for (attno = 1; attno <= appinfo->num_child_cols; attno++)
    6395              :                     {
    6396           20 :                         parent_attno = appinfo->parent_colnos[attno - 1];
    6397           20 :                         if (parent_attno == 0)
    6398            0 :                             return false;   /* attr is local to child */
    6399              :                         parent_varattnos =
    6400           20 :                             bms_add_member(parent_varattnos,
    6401              :                                            parent_attno - FirstLowInvalidHeapAttributeNumber);
    6402              :                     }
    6403              :                 }
    6404              :                 else
    6405              :                 {
    6406       154065 :                     if (attno < 0)
    6407              :                     {
    6408              :                         /* System attnos are the same in all tables */
    6409            0 :                         parent_attno = attno;
    6410              :                     }
    6411              :                     else
    6412              :                     {
    6413       154065 :                         if (attno > appinfo->num_child_cols)
    6414            0 :                             return false;   /* safety check */
    6415       154065 :                         parent_attno = appinfo->parent_colnos[attno - 1];
    6416       154065 :                         if (parent_attno == 0)
    6417            0 :                             return false;   /* attr is local to child */
    6418              :                     }
    6419              :                     parent_varattnos =
    6420       154065 :                         bms_add_member(parent_varattnos,
    6421              :                                        parent_attno - FirstLowInvalidHeapAttributeNumber);
    6422              :                 }
    6423              :             }
    6424              : 
    6425              :             /* If the parent is itself a child, continue up */
    6426       156342 :             varno = appinfo->parent_relid;
    6427       156342 :             varattnos = parent_varattnos;
    6428       156342 :             appinfo = root->append_rel_array[varno];
    6429              :         }
    6430              : 
    6431              :         /* Perform the access check on this parent rel */
    6432       177728 :         rte = planner_rt_fetch(varno, root);
    6433              :         Assert(rte->rtekind == RTE_RELATION);
    6434              :     }
    6435              : 
    6436              :     /*
    6437              :      * For all rows to be accessible, there must be no securityQuals from
    6438              :      * security barrier views or RLS policies.
    6439              :      */
    6440      1157244 :     if (rte->securityQuals != NIL)
    6441          690 :         return false;
    6442              : 
    6443              :     /*
    6444              :      * Test for table-level SELECT privilege.
    6445              :      *
    6446              :      * If varattnos is non-NULL, this is sufficient to give access to all
    6447              :      * requested attributes, even for a child table, since we have verified
    6448              :      * that all required child columns have matching parent columns.
    6449              :      *
    6450              :      * If varattnos is NULL (whole-table access requested), this doesn't
    6451              :      * necessarily guarantee that the user can read all columns of a child
    6452              :      * table, but we allow it anyway (see comments in examine_variable()) and
    6453              :      * don't bother checking any column privileges.
    6454              :      */
    6455      1156554 :     if (pg_class_aclcheck(rte->relid, userid, ACL_SELECT) == ACLCHECK_OK)
    6456      1156211 :         return true;
    6457              : 
    6458          343 :     if (varattnos == NULL)
    6459           10 :         return false;           /* whole-table access requested */
    6460              : 
    6461              :     /*
    6462              :      * Don't have table-level SELECT privilege, so check per-column
    6463              :      * privileges.
    6464              :      */
    6465          333 :     varattno = -1;
    6466          471 :     while ((varattno = bms_next_member(varattnos, varattno)) >= 0)
    6467              :     {
    6468          333 :         AttrNumber  attno = varattno + FirstLowInvalidHeapAttributeNumber;
    6469              : 
    6470          333 :         if (attno == InvalidAttrNumber)
    6471              :         {
    6472              :             /* Whole-row reference, so must have access to all columns */
    6473            5 :             if (pg_attribute_aclcheck_all(rte->relid, userid, ACL_SELECT,
    6474              :                                           ACLMASK_ALL) != ACLCHECK_OK)
    6475            5 :                 return false;
    6476              :         }
    6477              :         else
    6478              :         {
    6479          328 :             if (pg_attribute_aclcheck(rte->relid, attno, userid,
    6480              :                                       ACL_SELECT) != ACLCHECK_OK)
    6481          190 :                 return false;
    6482              :         }
    6483              :     }
    6484              : 
    6485              :     /* If we reach here, have all required column privileges */
    6486          138 :     return true;
    6487              : }
    6488              : 
    6489              : /*
    6490              :  * examine_indexcol_variable
    6491              :  *      Try to look up statistical data about an index column/expression.
    6492              :  *      Fill in a VariableStatData struct to describe the column.
    6493              :  *
    6494              :  * Inputs:
    6495              :  *  root: the planner info
    6496              :  *  index: the index whose column we're interested in
    6497              :  *  indexcol: 0-based index column number (subscripts index->indexkeys[])
    6498              :  *
    6499              :  * Outputs: *vardata is filled as follows:
    6500              :  *  var: the input expression (with any binary relabeling stripped, if
    6501              :  *      it is or contains a variable; but otherwise the type is preserved)
    6502              :  *  rel: RelOptInfo for table relation containing variable.
    6503              :  *  statsTuple: the pg_statistic entry for the variable, if one exists;
    6504              :  *      otherwise NULL.
    6505              :  *  freefunc: pointer to a function to release statsTuple with.
    6506              :  *
    6507              :  * Caller is responsible for doing ReleaseVariableStats() before exiting.
    6508              :  */
    6509              : static void
    6510       653238 : examine_indexcol_variable(PlannerInfo *root, IndexOptInfo *index,
    6511              :                           int indexcol, VariableStatData *vardata)
    6512              : {
    6513              :     AttrNumber  colnum;
    6514              :     Oid         relid;
    6515              : 
    6516       653238 :     if (index->indexkeys[indexcol] != 0)
    6517              :     {
    6518              :         /* Simple variable --- look to stats for the underlying table */
    6519       651405 :         RangeTblEntry *rte = planner_rt_fetch(index->rel->relid, root);
    6520              : 
    6521              :         Assert(rte->rtekind == RTE_RELATION);
    6522       651405 :         relid = rte->relid;
    6523              :         Assert(relid != InvalidOid);
    6524       651405 :         colnum = index->indexkeys[indexcol];
    6525       651405 :         vardata->rel = index->rel;
    6526              : 
    6527       651405 :         if (get_relation_stats_hook &&
    6528            0 :             (*get_relation_stats_hook) (root, rte, colnum, vardata))
    6529              :         {
    6530              :             /*
    6531              :              * The hook took control of acquiring a stats tuple.  If it did
    6532              :              * supply a tuple, it'd better have supplied a freefunc.
    6533              :              */
    6534            0 :             if (HeapTupleIsValid(vardata->statsTuple) &&
    6535            0 :                 !vardata->freefunc)
    6536            0 :                 elog(ERROR, "no function provided to release variable stats with");
    6537              :         }
    6538              :         else
    6539              :         {
    6540       651405 :             vardata->statsTuple = SearchSysCache3(STATRELATTINH,
    6541              :                                                   ObjectIdGetDatum(relid),
    6542              :                                                   Int16GetDatum(colnum),
    6543       651405 :                                                   BoolGetDatum(rte->inh));
    6544       651405 :             vardata->freefunc = ReleaseSysCache;
    6545              :         }
    6546              :     }
    6547              :     else
    6548              :     {
    6549              :         /* Expression --- maybe there are stats for the index itself */
    6550         1833 :         relid = index->indexoid;
    6551         1833 :         colnum = indexcol + 1;
    6552              : 
    6553         1833 :         if (get_index_stats_hook &&
    6554            0 :             (*get_index_stats_hook) (root, relid, colnum, vardata))
    6555              :         {
    6556              :             /*
    6557              :              * The hook took control of acquiring a stats tuple.  If it did
    6558              :              * supply a tuple, it'd better have supplied a freefunc.
    6559              :              */
    6560            0 :             if (HeapTupleIsValid(vardata->statsTuple) &&
    6561            0 :                 !vardata->freefunc)
    6562            0 :                 elog(ERROR, "no function provided to release variable stats with");
    6563              :         }
    6564              :         else
    6565              :         {
    6566         1833 :             vardata->statsTuple = SearchSysCache3(STATRELATTINH,
    6567              :                                                   ObjectIdGetDatum(relid),
    6568              :                                                   Int16GetDatum(colnum),
    6569              :                                                   BoolGetDatum(false));
    6570         1833 :             vardata->freefunc = ReleaseSysCache;
    6571              :         }
    6572              :     }
    6573       653238 : }
    6574              : 
    6575              : /*
    6576              :  * Check whether it is permitted to call func_oid passing some of the
    6577              :  * pg_statistic data in vardata.  We allow this if either of the following
    6578              :  * conditions is met: (1) the user has SELECT privileges on the table or
    6579              :  * column underlying the pg_statistic data and there are no securityQuals from
    6580              :  * security barrier views or RLS policies, or (2) the function is marked
    6581              :  * leakproof.
    6582              :  */
    6583              : bool
    6584       775401 : statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
    6585              : {
    6586       775401 :     if (vardata->acl_ok)
    6587       773890 :         return true;            /* have SELECT privs and no securityQuals */
    6588              : 
    6589         1511 :     if (!OidIsValid(func_oid))
    6590            0 :         return false;
    6591              : 
    6592         1511 :     if (get_func_leakproof(func_oid))
    6593          748 :         return true;
    6594              : 
    6595          763 :     ereport(DEBUG2,
    6596              :             (errmsg_internal("not using statistics because function \"%s\" is not leakproof",
    6597              :                              get_func_name(func_oid))));
    6598          763 :     return false;
    6599              : }
    6600              : 
    6601              : /*
    6602              :  * get_variable_numdistinct
    6603              :  *    Estimate the number of distinct values of a variable.
    6604              :  *
    6605              :  * vardata: results of examine_variable
    6606              :  * *isdefault: set to true if the result is a default rather than based on
    6607              :  * anything meaningful.
    6608              :  *
    6609              :  * NB: be careful to produce a positive integral result, since callers may
    6610              :  * compare the result to exact integer counts, or might divide by it.
    6611              :  */
    6612              : double
    6613      1303737 : get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
    6614              : {
    6615              :     double      stadistinct;
    6616      1303737 :     double      stanullfrac = 0.0;
    6617              :     double      ntuples;
    6618              : 
    6619      1303737 :     *isdefault = false;
    6620              : 
    6621              :     /*
    6622              :      * Determine the stadistinct value to use.  There are cases where we can
    6623              :      * get an estimate even without a pg_statistic entry, or can get a better
    6624              :      * value than is in pg_statistic.  Grab stanullfrac too if we can find it
    6625              :      * (otherwise, assume no nulls, for lack of any better idea).
    6626              :      */
    6627      1303737 :     if (HeapTupleIsValid(vardata->statsTuple))
    6628              :     {
    6629              :         /* Use the pg_statistic entry */
    6630              :         Form_pg_statistic stats;
    6631              : 
    6632       814179 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
    6633       814179 :         stadistinct = stats->stadistinct;
    6634       814179 :         stanullfrac = stats->stanullfrac;
    6635              :     }
    6636       489558 :     else if (vardata->vartype == BOOLOID)
    6637              :     {
    6638              :         /*
    6639              :          * Special-case boolean columns: presumably, two distinct values.
    6640              :          *
    6641              :          * Are there any other datatypes we should wire in special estimates
    6642              :          * for?
    6643              :          */
    6644          684 :         stadistinct = 2.0;
    6645              :     }
    6646       488874 :     else if (vardata->rel && vardata->rel->rtekind == RTE_VALUES)
    6647              :     {
    6648              :         /*
    6649              :          * If the Var represents a column of a VALUES RTE, assume it's unique.
    6650              :          * This could of course be very wrong, but it should tend to be true
    6651              :          * in well-written queries.  We could consider examining the VALUES'
    6652              :          * contents to get some real statistics; but that only works if the
    6653              :          * entries are all constants, and it would be pretty expensive anyway.
    6654              :          */
    6655         2999 :         stadistinct = -1.0;     /* unique (and all non null) */
    6656              :     }
    6657              :     else
    6658              :     {
    6659              :         /*
    6660              :          * We don't keep statistics for system columns, but in some cases we
    6661              :          * can infer distinctness anyway.
    6662              :          */
    6663       485875 :         if (vardata->var && IsA(vardata->var, Var))
    6664              :         {
    6665       454793 :             switch (((Var *) vardata->var)->varattno)
    6666              :             {
    6667          857 :                 case SelfItemPointerAttributeNumber:
    6668          857 :                     stadistinct = -1.0; /* unique (and all non null) */
    6669          857 :                     break;
    6670        18252 :                 case TableOidAttributeNumber:
    6671        18252 :                     stadistinct = 1.0;  /* only 1 value */
    6672        18252 :                     break;
    6673       435684 :                 default:
    6674       435684 :                     stadistinct = 0.0;  /* means "unknown" */
    6675       435684 :                     break;
    6676              :             }
    6677              :         }
    6678              :         else
    6679        31082 :             stadistinct = 0.0;  /* means "unknown" */
    6680              : 
    6681              :         /*
    6682              :          * XXX consider using estimate_num_groups on expressions?
    6683              :          */
    6684              :     }
    6685              : 
    6686              :     /*
    6687              :      * If there is a unique index, DISTINCT or GROUP-BY clause for the
    6688              :      * variable, assume it is unique no matter what pg_statistic says; the
    6689              :      * statistics could be out of date, or we might have found a partial
    6690              :      * unique index that proves the var is unique for this query.  However,
    6691              :      * we'd better still believe the null-fraction statistic.
    6692              :      */
    6693      1303737 :     if (vardata->isunique)
    6694       311802 :         stadistinct = -1.0 * (1.0 - stanullfrac);
    6695              : 
    6696              :     /*
    6697              :      * If we had an absolute estimate, use that.
    6698              :      */
    6699      1303737 :     if (stadistinct > 0.0)
    6700       316633 :         return clamp_row_est(stadistinct);
    6701              : 
    6702              :     /*
    6703              :      * Otherwise we need to get the relation size; punt if not available.
    6704              :      */
    6705       987104 :     if (vardata->rel == NULL)
    6706              :     {
    6707          591 :         *isdefault = true;
    6708          591 :         return DEFAULT_NUM_DISTINCT;
    6709              :     }
    6710       986513 :     ntuples = vardata->rel->tuples;
    6711       986513 :     if (ntuples <= 0.0)
    6712              :     {
    6713       111045 :         *isdefault = true;
    6714       111045 :         return DEFAULT_NUM_DISTINCT;
    6715              :     }
    6716              : 
    6717              :     /*
    6718              :      * If we had a relative estimate, use that.
    6719              :      */
    6720       875468 :     if (stadistinct < 0.0)
    6721       570345 :         return clamp_row_est(-stadistinct * ntuples);
    6722              : 
    6723              :     /*
    6724              :      * With no data, estimate ndistinct = ntuples if the table is small, else
    6725              :      * use default.  We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
    6726              :      * that the behavior isn't discontinuous.
    6727              :      */
    6728       305123 :     if (ntuples < DEFAULT_NUM_DISTINCT)
    6729       156012 :         return clamp_row_est(ntuples);
    6730              : 
    6731       149111 :     *isdefault = true;
    6732       149111 :     return DEFAULT_NUM_DISTINCT;
    6733              : }
    6734              : 
    6735              : /*
    6736              :  * get_variable_range
    6737              :  *      Estimate the minimum and maximum value of the specified variable.
    6738              :  *      If successful, store values in *min and *max, and return true.
    6739              :  *      If no data available, return false.
    6740              :  *
    6741              :  * sortop is the "<" comparison operator to use.  This should generally
    6742              :  * be "<" not ">", as only the former is likely to be found in pg_statistic.
    6743              :  * The collation must be specified too.
    6744              :  */
    6745              : static bool
    6746       173418 : get_variable_range(PlannerInfo *root, VariableStatData *vardata,
    6747              :                    Oid sortop, Oid collation,
    6748              :                    Datum *min, Datum *max)
    6749              : {
    6750       173418 :     Datum       tmin = 0;
    6751       173418 :     Datum       tmax = 0;
    6752       173418 :     bool        have_data = false;
    6753              :     int16       typLen;
    6754              :     bool        typByVal;
    6755              :     Oid         opfuncoid;
    6756              :     FmgrInfo    opproc;
    6757              :     AttStatsSlot sslot;
    6758              : 
    6759              :     /*
    6760              :      * XXX It's very tempting to try to use the actual column min and max, if
    6761              :      * we can get them relatively-cheaply with an index probe.  However, since
    6762              :      * this function is called many times during join planning, that could
    6763              :      * have unpleasant effects on planning speed.  Need more investigation
    6764              :      * before enabling this.
    6765              :      */
    6766              : #ifdef NOT_USED
    6767              :     if (get_actual_variable_range(root, vardata, sortop, collation, min, max))
    6768              :         return true;
    6769              : #endif
    6770              : 
    6771       173418 :     if (!HeapTupleIsValid(vardata->statsTuple))
    6772              :     {
    6773              :         /* no stats available, so default result */
    6774        48199 :         return false;
    6775              :     }
    6776              : 
    6777              :     /*
    6778              :      * If we can't apply the sortop to the stats data, just fail.  In
    6779              :      * principle, if there's a histogram and no MCVs, we could return the
    6780              :      * histogram endpoints without ever applying the sortop ... but it's
    6781              :      * probably not worth trying, because whatever the caller wants to do with
    6782              :      * the endpoints would likely fail the security check too.
    6783              :      */
    6784       125219 :     if (!statistic_proc_security_check(vardata,
    6785       125219 :                                        (opfuncoid = get_opcode(sortop))))
    6786            0 :         return false;
    6787              : 
    6788       125219 :     opproc.fn_oid = InvalidOid; /* mark this as not looked up yet */
    6789              : 
    6790       125219 :     get_typlenbyval(vardata->atttype, &typLen, &typByVal);
    6791              : 
    6792              :     /*
    6793              :      * If there is a histogram with the ordering we want, grab the first and
    6794              :      * last values.
    6795              :      */
    6796       125219 :     if (get_attstatsslot(&sslot, vardata->statsTuple,
    6797              :                          STATISTIC_KIND_HISTOGRAM, sortop,
    6798              :                          ATTSTATSSLOT_VALUES))
    6799              :     {
    6800        77741 :         if (sslot.stacoll == collation && sslot.nvalues > 0)
    6801              :         {
    6802        77741 :             tmin = datumCopy(sslot.values[0], typByVal, typLen);
    6803        77741 :             tmax = datumCopy(sslot.values[sslot.nvalues - 1], typByVal, typLen);
    6804        77741 :             have_data = true;
    6805              :         }
    6806        77741 :         free_attstatsslot(&sslot);
    6807              :     }
    6808              : 
    6809              :     /*
    6810              :      * Otherwise, if there is a histogram with some other ordering, scan it
    6811              :      * and get the min and max values according to the ordering we want.  This
    6812              :      * of course may not find values that are really extremal according to our
    6813              :      * ordering, but it beats ignoring available data.
    6814              :      */
    6815       172697 :     if (!have_data &&
    6816        47478 :         get_attstatsslot(&sslot, vardata->statsTuple,
    6817              :                          STATISTIC_KIND_HISTOGRAM, InvalidOid,
    6818              :                          ATTSTATSSLOT_VALUES))
    6819              :     {
    6820            0 :         get_stats_slot_range(&sslot, opfuncoid, &opproc,
    6821              :                              collation, typLen, typByVal,
    6822              :                              &tmin, &tmax, &have_data);
    6823            0 :         free_attstatsslot(&sslot);
    6824              :     }
    6825              : 
    6826              :     /*
    6827              :      * If we have most-common-values info, look for extreme MCVs.  This is
    6828              :      * needed even if we also have a histogram, since the histogram excludes
    6829              :      * the MCVs.  However, if we *only* have MCVs and no histogram, we should
    6830              :      * be pretty wary of deciding that that is a full representation of the
    6831              :      * data.  Proceed only if the MCVs represent the whole table (to within
    6832              :      * roundoff error).
    6833              :      */
    6834       125219 :     if (get_attstatsslot(&sslot, vardata->statsTuple,
    6835              :                          STATISTIC_KIND_MCV, InvalidOid,
    6836       125219 :                          have_data ? ATTSTATSSLOT_VALUES :
    6837              :                          (ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS)))
    6838              :     {
    6839        70730 :         bool        use_mcvs = have_data;
    6840              : 
    6841        70730 :         if (!have_data)
    6842              :         {
    6843        46552 :             double      sumcommon = 0.0;
    6844              :             double      nullfrac;
    6845              :             int         i;
    6846              : 
    6847       375526 :             for (i = 0; i < sslot.nnumbers; i++)
    6848       328974 :                 sumcommon += sslot.numbers[i];
    6849        46552 :             nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata->statsTuple))->stanullfrac;
    6850        46552 :             if (sumcommon + nullfrac > 0.99999)
    6851        45726 :                 use_mcvs = true;
    6852              :         }
    6853              : 
    6854        70730 :         if (use_mcvs)
    6855        69904 :             get_stats_slot_range(&sslot, opfuncoid, &opproc,
    6856              :                                  collation, typLen, typByVal,
    6857              :                                  &tmin, &tmax, &have_data);
    6858        70730 :         free_attstatsslot(&sslot);
    6859              :     }
    6860              : 
    6861       125219 :     *min = tmin;
    6862       125219 :     *max = tmax;
    6863       125219 :     return have_data;
    6864              : }
    6865              : 
    6866              : /*
    6867              :  * get_stats_slot_range: scan sslot for min/max values
    6868              :  *
    6869              :  * Subroutine for get_variable_range: update min/max/have_data according
    6870              :  * to what we find in the statistics array.
    6871              :  */
    6872              : static void
    6873        69904 : get_stats_slot_range(AttStatsSlot *sslot, Oid opfuncoid, FmgrInfo *opproc,
    6874              :                      Oid collation, int16 typLen, bool typByVal,
    6875              :                      Datum *min, Datum *max, bool *p_have_data)
    6876              : {
    6877        69904 :     Datum       tmin = *min;
    6878        69904 :     Datum       tmax = *max;
    6879        69904 :     bool        have_data = *p_have_data;
    6880        69904 :     bool        found_tmin = false;
    6881        69904 :     bool        found_tmax = false;
    6882              : 
    6883              :     /* Look up the comparison function, if we didn't already do so */
    6884        69904 :     if (opproc->fn_oid != opfuncoid)
    6885        69904 :         fmgr_info(opfuncoid, opproc);
    6886              : 
    6887              :     /* Scan all the slot's values */
    6888      1682602 :     for (int i = 0; i < sslot->nvalues; i++)
    6889              :     {
    6890      1612698 :         if (!have_data)
    6891              :         {
    6892        45726 :             tmin = tmax = sslot->values[i];
    6893        45726 :             found_tmin = found_tmax = true;
    6894        45726 :             *p_have_data = have_data = true;
    6895        45726 :             continue;
    6896              :         }
    6897      1566972 :         if (DatumGetBool(FunctionCall2Coll(opproc,
    6898              :                                            collation,
    6899      1566972 :                                            sslot->values[i], tmin)))
    6900              :         {
    6901        39539 :             tmin = sslot->values[i];
    6902        39539 :             found_tmin = true;
    6903              :         }
    6904      1566972 :         if (DatumGetBool(FunctionCall2Coll(opproc,
    6905              :                                            collation,
    6906      1566972 :                                            tmax, sslot->values[i])))
    6907              :         {
    6908       190928 :             tmax = sslot->values[i];
    6909       190928 :             found_tmax = true;
    6910              :         }
    6911              :     }
    6912              : 
    6913              :     /*
    6914              :      * Copy the slot's values, if we found new extreme values.
    6915              :      */
    6916        69904 :     if (found_tmin)
    6917        60510 :         *min = datumCopy(tmin, typByVal, typLen);
    6918        69904 :     if (found_tmax)
    6919        48256 :         *max = datumCopy(tmax, typByVal, typLen);
    6920        69904 : }
    6921              : 
    6922              : 
    6923              : /*
    6924              :  * get_actual_variable_range
    6925              :  *      Attempt to identify the current *actual* minimum and/or maximum
    6926              :  *      of the specified variable, by looking for a suitable btree index
    6927              :  *      and fetching its low and/or high values.
    6928              :  *      If successful, store values in *min and *max, and return true.
    6929              :  *      (Either pointer can be NULL if that endpoint isn't needed.)
    6930              :  *      If unsuccessful, return false.
    6931              :  *
    6932              :  * sortop is the "<" comparison operator to use.
    6933              :  * collation is the required collation.
    6934              :  */
    6935              : static bool
    6936       119094 : get_actual_variable_range(PlannerInfo *root, VariableStatData *vardata,
    6937              :                           Oid sortop, Oid collation,
    6938              :                           Datum *min, Datum *max)
    6939              : {
    6940       119094 :     bool        have_data = false;
    6941       119094 :     RelOptInfo *rel = vardata->rel;
    6942              :     RangeTblEntry *rte;
    6943              :     ListCell   *lc;
    6944              : 
    6945              :     /* No hope if no relation or it doesn't have indexes */
    6946       119094 :     if (rel == NULL || rel->indexlist == NIL)
    6947         8991 :         return false;
    6948              :     /* If it has indexes it must be a plain relation */
    6949       110103 :     rte = root->simple_rte_array[rel->relid];
    6950              :     Assert(rte->rtekind == RTE_RELATION);
    6951              : 
    6952              :     /* ignore partitioned tables.  Any indexes here are not real indexes */
    6953       110103 :     if (rte->relkind == RELKIND_PARTITIONED_TABLE)
    6954          560 :         return false;
    6955              : 
    6956              :     /* Search through the indexes to see if any match our problem */
    6957       214202 :     foreach(lc, rel->indexlist)
    6958              :     {
    6959       184101 :         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
    6960              :         ScanDirection indexscandir;
    6961              :         StrategyNumber strategy;
    6962              : 
    6963              :         /* Ignore non-ordering indexes */
    6964       184101 :         if (index->sortopfamily == NULL)
    6965            3 :             continue;
    6966              : 
    6967              :         /*
    6968              :          * Ignore partial indexes --- we only want stats that cover the entire
    6969              :          * relation.
    6970              :          */
    6971       184098 :         if (index->indpred != NIL)
    6972          240 :             continue;
    6973              : 
    6974              :         /*
    6975              :          * The index list might include hypothetical indexes inserted by a
    6976              :          * get_relation_info hook --- don't try to access them.
    6977              :          */
    6978       183858 :         if (index->hypothetical)
    6979            0 :             continue;
    6980              : 
    6981              :         /*
    6982              :          * get_actual_variable_endpoint uses the index-only-scan machinery, so
    6983              :          * ignore indexes that can't use it on their first column.
    6984              :          */
    6985       183858 :         if (!index->canreturn[0])
    6986            0 :             continue;
    6987              : 
    6988              :         /*
    6989              :          * The first index column must match the desired variable, sortop, and
    6990              :          * collation --- but we can use a descending-order index.
    6991              :          */
    6992       183858 :         if (collation != index->indexcollations[0])
    6993        24192 :             continue;           /* test first 'cause it's cheapest */
    6994       159666 :         if (!match_index_to_operand(vardata->var, 0, index))
    6995        80224 :             continue;
    6996        79442 :         strategy = get_op_opfamily_strategy(sortop, index->sortopfamily[0]);
    6997        79442 :         switch (IndexAmTranslateStrategy(strategy, index->relam, index->sortopfamily[0], true))
    6998              :         {
    6999        79442 :             case COMPARE_LT:
    7000        79442 :                 if (index->reverse_sort[0])
    7001            0 :                     indexscandir = BackwardScanDirection;
    7002              :                 else
    7003        79442 :                     indexscandir = ForwardScanDirection;
    7004        79442 :                 break;
    7005            0 :             case COMPARE_GT:
    7006            0 :                 if (index->reverse_sort[0])
    7007            0 :                     indexscandir = ForwardScanDirection;
    7008              :                 else
    7009            0 :                     indexscandir = BackwardScanDirection;
    7010            0 :                 break;
    7011            0 :             default:
    7012              :                 /* index doesn't match the sortop */
    7013            0 :                 continue;
    7014              :         }
    7015              : 
    7016              :         /*
    7017              :          * Found a suitable index to extract data from.  Set up some data that
    7018              :          * can be used by both invocations of get_actual_variable_endpoint.
    7019              :          */
    7020              :         {
    7021              :             MemoryContext tmpcontext;
    7022              :             MemoryContext oldcontext;
    7023              :             Relation    heapRel;
    7024              :             Relation    indexRel;
    7025              :             TupleTableSlot *slot;
    7026              :             int16       typLen;
    7027              :             bool        typByVal;
    7028              :             ScanKeyData scankeys[1];
    7029              : 
    7030              :             /* Make sure any cruft gets recycled when we're done */
    7031        79442 :             tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
    7032              :                                                "get_actual_variable_range workspace",
    7033              :                                                ALLOCSET_DEFAULT_SIZES);
    7034        79442 :             oldcontext = MemoryContextSwitchTo(tmpcontext);
    7035              : 
    7036              :             /*
    7037              :              * Open the table and index so we can read from them.  We should
    7038              :              * already have some type of lock on each.
    7039              :              */
    7040        79442 :             heapRel = table_open(rte->relid, NoLock);
    7041        79442 :             indexRel = index_open(index->indexoid, NoLock);
    7042              : 
    7043              :             /* build some stuff needed for indexscan execution */
    7044        79442 :             slot = table_slot_create(heapRel, NULL);
    7045        79442 :             get_typlenbyval(vardata->atttype, &typLen, &typByVal);
    7046              : 
    7047              :             /* set up an IS NOT NULL scan key so that we ignore nulls */
    7048        79442 :             ScanKeyEntryInitialize(&scankeys[0],
    7049              :                                    SK_ISNULL | SK_SEARCHNOTNULL,
    7050              :                                    1,   /* index col to scan */
    7051              :                                    InvalidStrategy, /* no strategy */
    7052              :                                    InvalidOid,  /* no strategy subtype */
    7053              :                                    InvalidOid,  /* no collation */
    7054              :                                    InvalidOid,  /* no reg proc for this */
    7055              :                                    (Datum) 0);  /* constant */
    7056              : 
    7057              :             /* If min is requested ... */
    7058        79442 :             if (min)
    7059              :             {
    7060        44573 :                 have_data = get_actual_variable_endpoint(heapRel,
    7061              :                                                          indexRel,
    7062              :                                                          indexscandir,
    7063              :                                                          scankeys,
    7064              :                                                          typLen,
    7065              :                                                          typByVal,
    7066              :                                                          slot,
    7067              :                                                          oldcontext,
    7068              :                                                          min);
    7069              :             }
    7070              :             else
    7071              :             {
    7072              :                 /* If min not requested, still want to fetch max */
    7073        34869 :                 have_data = true;
    7074              :             }
    7075              : 
    7076              :             /* If max is requested, and we didn't already fail ... */
    7077        79442 :             if (max && have_data)
    7078              :             {
    7079              :                 /* scan in the opposite direction; all else is the same */
    7080        36039 :                 have_data = get_actual_variable_endpoint(heapRel,
    7081              :                                                          indexRel,
    7082        36039 :                                                          -indexscandir,
    7083              :                                                          scankeys,
    7084              :                                                          typLen,
    7085              :                                                          typByVal,
    7086              :                                                          slot,
    7087              :                                                          oldcontext,
    7088              :                                                          max);
    7089              :             }
    7090              : 
    7091              :             /* Clean everything up */
    7092        79442 :             ExecDropSingleTupleTableSlot(slot);
    7093              : 
    7094        79442 :             index_close(indexRel, NoLock);
    7095        79442 :             table_close(heapRel, NoLock);
    7096              : 
    7097        79442 :             MemoryContextSwitchTo(oldcontext);
    7098        79442 :             MemoryContextDelete(tmpcontext);
    7099              : 
    7100              :             /* And we're done */
    7101        79442 :             break;
    7102              :         }
    7103              :     }
    7104              : 
    7105       109543 :     return have_data;
    7106              : }
    7107              : 
    7108              : /*
    7109              :  * Get one endpoint datum (min or max depending on indexscandir) from the
    7110              :  * specified index.  Return true if successful, false if not.
    7111              :  * On success, endpoint value is stored to *endpointDatum (and copied into
    7112              :  * outercontext).
    7113              :  *
    7114              :  * scankeys is a 1-element scankey array set up to reject nulls.
    7115              :  * typLen/typByVal describe the datatype of the index's first column.
    7116              :  * tableslot is a slot suitable to hold table tuples, in case we need
    7117              :  * to probe the heap.
    7118              :  * (We could compute these values locally, but that would mean computing them
    7119              :  * twice when get_actual_variable_range needs both the min and the max.)
    7120              :  *
    7121              :  * Failure occurs either when the index is empty, or we decide that it's
    7122              :  * taking too long to find a suitable tuple.
    7123              :  */
    7124              : static bool
    7125        80612 : get_actual_variable_endpoint(Relation heapRel,
    7126              :                              Relation indexRel,
    7127              :                              ScanDirection indexscandir,
    7128              :                              ScanKey scankeys,
    7129              :                              int16 typLen,
    7130              :                              bool typByVal,
    7131              :                              TupleTableSlot *tableslot,
    7132              :                              MemoryContext outercontext,
    7133              :                              Datum *endpointDatum)
    7134              : {
    7135        80612 :     bool        have_data = false;
    7136              :     SnapshotData SnapshotNonVacuumable;
    7137              :     IndexScanDesc index_scan;
    7138        80612 :     Buffer      vmbuffer = InvalidBuffer;
    7139        80612 :     BlockNumber last_heap_block = InvalidBlockNumber;
    7140        80612 :     int         n_visited_heap_pages = 0;
    7141              :     ItemPointer tid;
    7142              :     Datum       values[INDEX_MAX_KEYS];
    7143              :     bool        isnull[INDEX_MAX_KEYS];
    7144              :     MemoryContext oldcontext;
    7145              : 
    7146              :     /*
    7147              :      * We use the index-only-scan machinery for this.  With mostly-static
    7148              :      * tables that's a win because it avoids a heap visit.  It's also a win
    7149              :      * for dynamic data, but the reason is less obvious; read on for details.
    7150              :      *
    7151              :      * In principle, we should scan the index with our current active
    7152              :      * snapshot, which is the best approximation we've got to what the query
    7153              :      * will see when executed.  But that won't be exact if a new snap is taken
    7154              :      * before running the query, and it can be very expensive if a lot of
    7155              :      * recently-dead or uncommitted rows exist at the beginning or end of the
    7156              :      * index (because we'll laboriously fetch each one and reject it).
    7157              :      * Instead, we use SnapshotNonVacuumable.  That will accept recently-dead
    7158              :      * and uncommitted rows as well as normal visible rows.  On the other
    7159              :      * hand, it will reject known-dead rows, and thus not give a bogus answer
    7160              :      * when the extreme value has been deleted (unless the deletion was quite
    7161              :      * recent); that case motivates not using SnapshotAny here.
    7162              :      *
    7163              :      * A crucial point here is that SnapshotNonVacuumable, with
    7164              :      * GlobalVisTestFor(heapRel) as horizon, yields the inverse of the
    7165              :      * condition that the indexscan will use to decide that index entries are
    7166              :      * killable (see heap_hot_search_buffer()).  Therefore, if the snapshot
    7167              :      * rejects a tuple (or more precisely, all tuples of a HOT chain) and we
    7168              :      * have to continue scanning past it, we know that the indexscan will mark
    7169              :      * that index entry killed.  That means that the next
    7170              :      * get_actual_variable_endpoint() call will not have to re-consider that
    7171              :      * index entry.  In this way we avoid repetitive work when this function
    7172              :      * is used a lot during planning.
    7173              :      *
    7174              :      * But using SnapshotNonVacuumable creates a hazard of its own.  In a
    7175              :      * recently-created index, some index entries may point at "broken" HOT
    7176              :      * chains in which not all the tuple versions contain data matching the
    7177              :      * index entry.  The live tuple version(s) certainly do match the index,
    7178              :      * but SnapshotNonVacuumable can accept recently-dead tuple versions that
    7179              :      * don't match.  Hence, if we took data from the selected heap tuple, we
    7180              :      * might get a bogus answer that's not close to the index extremal value,
    7181              :      * or could even be NULL.  We avoid this hazard because we take the data
    7182              :      * from the index entry not the heap.
    7183              :      *
    7184              :      * Despite all this care, there are situations where we might find many
    7185              :      * non-visible tuples near the end of the index.  We don't want to expend
    7186              :      * a huge amount of time here, so we give up once we've read too many heap
    7187              :      * pages.  When we fail for that reason, the caller will end up using
    7188              :      * whatever extremal value is recorded in pg_statistic.
    7189              :      */
    7190        80612 :     InitNonVacuumableSnapshot(SnapshotNonVacuumable,
    7191              :                               GlobalVisTestFor(heapRel));
    7192              : 
    7193        80612 :     index_scan = index_beginscan(heapRel, indexRel,
    7194              :                                  &SnapshotNonVacuumable, NULL,
    7195              :                                  1, 0,
    7196              :                                  SO_NONE);
    7197              :     /* Set it up for index-only scan */
    7198        80612 :     index_scan->xs_want_itup = true;
    7199        80612 :     index_rescan(index_scan, scankeys, 1, NULL, 0);
    7200              : 
    7201              :     /* Fetch first/next tuple in specified direction */
    7202        94440 :     while ((tid = index_getnext_tid(index_scan, indexscandir)) != NULL)
    7203              :     {
    7204        94440 :         BlockNumber block = ItemPointerGetBlockNumber(tid);
    7205              : 
    7206        94440 :         if (!VM_ALL_VISIBLE(heapRel,
    7207              :                             block,
    7208              :                             &vmbuffer))
    7209              :         {
    7210              :             /* Rats, we have to visit the heap to check visibility */
    7211        63201 :             if (!index_fetch_heap(index_scan, tableslot))
    7212              :             {
    7213              :                 /*
    7214              :                  * No visible tuple for this index entry, so we need to
    7215              :                  * advance to the next entry.  Before doing so, count heap
    7216              :                  * page fetches and give up if we've done too many.
    7217              :                  *
    7218              :                  * We don't charge a page fetch if this is the same heap page
    7219              :                  * as the previous tuple.  This is on the conservative side,
    7220              :                  * since other recently-accessed pages are probably still in
    7221              :                  * buffers too; but it's good enough for this heuristic.
    7222              :                  */
    7223              : #define VISITED_PAGES_LIMIT 100
    7224              : 
    7225        13828 :                 if (block != last_heap_block)
    7226              :                 {
    7227         1891 :                     last_heap_block = block;
    7228         1891 :                     n_visited_heap_pages++;
    7229         1891 :                     if (n_visited_heap_pages > VISITED_PAGES_LIMIT)
    7230            0 :                         break;
    7231              :                 }
    7232              : 
    7233        13828 :                 continue;       /* no visible tuple, try next index entry */
    7234              :             }
    7235              : 
    7236              :             /* We don't actually need the heap tuple for anything */
    7237        49373 :             ExecClearTuple(tableslot);
    7238              : 
    7239              :             /*
    7240              :              * We don't care whether there's more than one visible tuple in
    7241              :              * the HOT chain; if any are visible, that's good enough.
    7242              :              */
    7243              :         }
    7244              : 
    7245              :         /*
    7246              :          * We expect that the index will return data in IndexTuple not
    7247              :          * HeapTuple format.
    7248              :          */
    7249        80612 :         if (!index_scan->xs_itup)
    7250            0 :             elog(ERROR, "no data returned for index-only scan");
    7251              : 
    7252              :         /*
    7253              :          * We do not yet support recheck here.
    7254              :          */
    7255        80612 :         if (index_scan->xs_recheck)
    7256            0 :             break;
    7257              : 
    7258              :         /* OK to deconstruct the index tuple */
    7259        80612 :         index_deform_tuple(index_scan->xs_itup,
    7260              :                            index_scan->xs_itupdesc,
    7261              :                            values, isnull);
    7262              : 
    7263              :         /* Shouldn't have got a null, but be careful */
    7264        80612 :         if (isnull[0])
    7265            0 :             elog(ERROR, "found unexpected null value in index \"%s\"",
    7266              :                  RelationGetRelationName(indexRel));
    7267              : 
    7268              :         /* Copy the index column value out to caller's context */
    7269        80612 :         oldcontext = MemoryContextSwitchTo(outercontext);
    7270        80612 :         *endpointDatum = datumCopy(values[0], typByVal, typLen);
    7271        80612 :         MemoryContextSwitchTo(oldcontext);
    7272        80612 :         have_data = true;
    7273        80612 :         break;
    7274              :     }
    7275              : 
    7276        80612 :     if (vmbuffer != InvalidBuffer)
    7277        73679 :         ReleaseBuffer(vmbuffer);
    7278        80612 :     index_endscan(index_scan);
    7279              : 
    7280        80612 :     return have_data;
    7281              : }
    7282              : 
    7283              : /*
    7284              :  * find_join_input_rel
    7285              :  *      Look up the input relation for a join.
    7286              :  *
    7287              :  * We assume that the input relation's RelOptInfo must have been constructed
    7288              :  * already.
    7289              :  */
    7290              : static RelOptInfo *
    7291        11408 : find_join_input_rel(PlannerInfo *root, Relids relids)
    7292              : {
    7293        11408 :     RelOptInfo *rel = NULL;
    7294              : 
    7295        11408 :     if (!bms_is_empty(relids))
    7296              :     {
    7297              :         int         relid;
    7298              : 
    7299        11408 :         if (bms_get_singleton_member(relids, &relid))
    7300        11132 :             rel = find_base_rel(root, relid);
    7301              :         else
    7302          276 :             rel = find_join_rel(root, relids);
    7303              :     }
    7304              : 
    7305        11408 :     if (rel == NULL)
    7306            0 :         elog(ERROR, "could not find RelOptInfo for given relids");
    7307              : 
    7308        11408 :     return rel;
    7309              : }
    7310              : 
    7311              : 
    7312              : /*-------------------------------------------------------------------------
    7313              :  *
    7314              :  * Index cost estimation functions
    7315              :  *
    7316              :  *-------------------------------------------------------------------------
    7317              :  */
    7318              : 
    7319              : /*
    7320              :  * Extract the actual indexquals (as RestrictInfos) from an IndexClause list
    7321              :  */
    7322              : List *
    7323       668612 : get_quals_from_indexclauses(List *indexclauses)
    7324              : {
    7325       668612 :     List       *result = NIL;
    7326              :     ListCell   *lc;
    7327              : 
    7328      1161057 :     foreach(lc, indexclauses)
    7329              :     {
    7330       492445 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
    7331              :         ListCell   *lc2;
    7332              : 
    7333       987321 :         foreach(lc2, iclause->indexquals)
    7334              :         {
    7335       494876 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
    7336              : 
    7337       494876 :             result = lappend(result, rinfo);
    7338              :         }
    7339              :     }
    7340       668612 :     return result;
    7341              : }
    7342              : 
    7343              : /*
    7344              :  * Compute the total evaluation cost of the comparison operands in a list
    7345              :  * of index qual expressions.  Since we know these will be evaluated just
    7346              :  * once per scan, there's no need to distinguish startup from per-row cost.
    7347              :  *
    7348              :  * This can be used either on the result of get_quals_from_indexclauses(),
    7349              :  * or directly on an indexorderbys list.  In both cases, we expect that the
    7350              :  * index key expression is on the left side of binary clauses.
    7351              :  */
    7352              : Cost
    7353      1326724 : index_other_operands_eval_cost(PlannerInfo *root, List *indexquals)
    7354              : {
    7355      1326724 :     Cost        qual_arg_cost = 0;
    7356              :     ListCell   *lc;
    7357              : 
    7358      1821949 :     foreach(lc, indexquals)
    7359              :     {
    7360       495225 :         Expr       *clause = (Expr *) lfirst(lc);
    7361              :         Node       *other_operand;
    7362              :         QualCost    index_qual_cost;
    7363              : 
    7364              :         /*
    7365              :          * Index quals will have RestrictInfos, indexorderbys won't.  Look
    7366              :          * through RestrictInfo if present.
    7367              :          */
    7368       495225 :         if (IsA(clause, RestrictInfo))
    7369       494866 :             clause = ((RestrictInfo *) clause)->clause;
    7370              : 
    7371       495225 :         if (IsA(clause, OpExpr))
    7372              :         {
    7373       479816 :             OpExpr     *op = (OpExpr *) clause;
    7374              : 
    7375       479816 :             other_operand = (Node *) lsecond(op->args);
    7376              :         }
    7377        15409 :         else if (IsA(clause, RowCompareExpr))
    7378              :         {
    7379          330 :             RowCompareExpr *rc = (RowCompareExpr *) clause;
    7380              : 
    7381          330 :             other_operand = (Node *) rc->rargs;
    7382              :         }
    7383        15079 :         else if (IsA(clause, ScalarArrayOpExpr))
    7384              :         {
    7385        12640 :             ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
    7386              : 
    7387        12640 :             other_operand = (Node *) lsecond(saop->args);
    7388              :         }
    7389         2439 :         else if (IsA(clause, NullTest))
    7390              :         {
    7391         2439 :             other_operand = NULL;
    7392              :         }
    7393              :         else
    7394              :         {
    7395            0 :             elog(ERROR, "unsupported indexqual type: %d",
    7396              :                  (int) nodeTag(clause));
    7397              :             other_operand = NULL;   /* keep compiler quiet */
    7398              :         }
    7399              : 
    7400       495225 :         cost_qual_eval_node(&index_qual_cost, other_operand, root);
    7401       495225 :         qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
    7402              :     }
    7403      1326724 :     return qual_arg_cost;
    7404              : }
    7405              : 
    7406              : /*
    7407              :  * Compute generic index access cost estimates.
    7408              :  *
    7409              :  * See struct GenericCosts in selfuncs.h for more info.
    7410              :  */
    7411              : void
    7412       658122 : genericcostestimate(PlannerInfo *root,
    7413              :                     IndexPath *path,
    7414              :                     double loop_count,
    7415              :                     GenericCosts *costs)
    7416              : {
    7417       658122 :     IndexOptInfo *index = path->indexinfo;
    7418       658122 :     List       *indexQuals = get_quals_from_indexclauses(path->indexclauses);
    7419       658122 :     List       *indexOrderBys = path->indexorderbys;
    7420              :     Cost        indexStartupCost;
    7421              :     Cost        indexTotalCost;
    7422              :     Selectivity indexSelectivity;
    7423              :     double      indexCorrelation;
    7424              :     double      numIndexPages;
    7425              :     double      numIndexTuples;
    7426              :     double      spc_random_page_cost;
    7427              :     double      num_sa_scans;
    7428              :     double      num_outer_scans;
    7429              :     double      num_scans;
    7430              :     double      qual_op_cost;
    7431              :     double      qual_arg_cost;
    7432              :     List       *selectivityQuals;
    7433              :     ListCell   *l;
    7434              : 
    7435              :     /*
    7436              :      * If the index is partial, AND the index predicate with the explicitly
    7437              :      * given indexquals to produce a more accurate idea of the index
    7438              :      * selectivity.
    7439              :      */
    7440       658122 :     selectivityQuals = add_predicate_to_index_quals(index, indexQuals);
    7441              : 
    7442              :     /*
    7443              :      * If caller didn't give us an estimate for ScalarArrayOpExpr index scans,
    7444              :      * just assume that the number of index descents is the number of distinct
    7445              :      * combinations of array elements from all of the scan's SAOP clauses.
    7446              :      */
    7447       658122 :     num_sa_scans = costs->num_sa_scans;
    7448       658122 :     if (num_sa_scans < 1)
    7449              :     {
    7450         6968 :         num_sa_scans = 1;
    7451        15246 :         foreach(l, indexQuals)
    7452              :         {
    7453         8278 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
    7454              : 
    7455         8278 :             if (IsA(rinfo->clause, ScalarArrayOpExpr))
    7456              :             {
    7457           46 :                 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
    7458           46 :                 double      alength = estimate_array_length(root, lsecond(saop->args));
    7459              : 
    7460           46 :                 if (alength > 1)
    7461           46 :                     num_sa_scans *= alength;
    7462              :             }
    7463              :         }
    7464              :     }
    7465              : 
    7466              :     /* Estimate the fraction of main-table tuples that will be visited */
    7467       658122 :     indexSelectivity = clauselist_selectivity(root, selectivityQuals,
    7468       658122 :                                               index->rel->relid,
    7469              :                                               JOIN_INNER,
    7470              :                                               NULL);
    7471              : 
    7472              :     /*
    7473              :      * If caller didn't give us an estimate, estimate the number of index
    7474              :      * tuples that will be visited.  We do it in this rather peculiar-looking
    7475              :      * way in order to get the right answer for partial indexes.
    7476              :      */
    7477       658122 :     numIndexTuples = costs->numIndexTuples;
    7478       658122 :     if (numIndexTuples <= 0.0)
    7479              :     {
    7480        84875 :         numIndexTuples = indexSelectivity * index->rel->tuples;
    7481              : 
    7482              :         /*
    7483              :          * The above calculation counts all the tuples visited across all
    7484              :          * scans induced by ScalarArrayOpExpr nodes.  We want to consider the
    7485              :          * average per-indexscan number, so adjust.  This is a handy place to
    7486              :          * round to integer, too.  (If caller supplied tuple estimate, it's
    7487              :          * responsible for handling these considerations.)
    7488              :          */
    7489        84875 :         numIndexTuples = rint(numIndexTuples / num_sa_scans);
    7490              :     }
    7491              : 
    7492              :     /*
    7493              :      * We can bound the number of tuples by the index size in any case. Also,
    7494              :      * always estimate at least one tuple is touched, even when
    7495              :      * indexSelectivity estimate is tiny.
    7496              :      */
    7497       658122 :     if (numIndexTuples > index->tuples)
    7498         8525 :         numIndexTuples = index->tuples;
    7499       658122 :     if (numIndexTuples < 1.0)
    7500        88574 :         numIndexTuples = 1.0;
    7501              : 
    7502              :     /*
    7503              :      * Estimate the number of index pages that will be retrieved.
    7504              :      *
    7505              :      * We use the simplistic method of taking a pro-rata fraction of the total
    7506              :      * number of index leaf pages.  We disregard any overhead such as index
    7507              :      * metapages or upper tree levels.
    7508              :      *
    7509              :      * In practice access to upper index levels is often nearly free because
    7510              :      * those tend to stay in cache under load; moreover, the cost involved is
    7511              :      * highly dependent on index type.  We therefore ignore such costs here
    7512              :      * and leave it to the caller to add a suitable charge if needed.
    7513              :      */
    7514       658122 :     if (index->pages > costs->numNonLeafPages && index->tuples > 1)
    7515       591029 :         numIndexPages =
    7516       591029 :             ceil(numIndexTuples * (index->pages - costs->numNonLeafPages)
    7517       591029 :                  / index->tuples);
    7518              :     else
    7519        67093 :         numIndexPages = 1.0;
    7520              : 
    7521              :     /* fetch estimated page cost for tablespace containing index */
    7522       658122 :     get_tablespace_page_costs(index->reltablespace,
    7523              :                               &spc_random_page_cost,
    7524              :                               NULL);
    7525              : 
    7526              :     /*
    7527              :      * Now compute the disk access costs.
    7528              :      *
    7529              :      * The above calculations are all per-index-scan.  However, if we are in a
    7530              :      * nestloop inner scan, we can expect the scan to be repeated (with
    7531              :      * different search keys) for each row of the outer relation.  Likewise,
    7532              :      * ScalarArrayOpExpr quals result in multiple index scans.  This creates
    7533              :      * the potential for cache effects to reduce the number of disk page
    7534              :      * fetches needed.  We want to estimate the average per-scan I/O cost in
    7535              :      * the presence of caching.
    7536              :      *
    7537              :      * We use the Mackert-Lohman formula (see costsize.c for details) to
    7538              :      * estimate the total number of page fetches that occur.  While this
    7539              :      * wasn't what it was designed for, it seems a reasonable model anyway.
    7540              :      * Note that we are counting pages not tuples anymore, so we take N = T =
    7541              :      * index size, as if there were one "tuple" per page.
    7542              :      */
    7543       658122 :     num_outer_scans = loop_count;
    7544       658122 :     num_scans = num_sa_scans * num_outer_scans;
    7545              : 
    7546       658122 :     if (num_scans > 1)
    7547              :     {
    7548              :         double      pages_fetched;
    7549              : 
    7550              :         /* total page fetches ignoring cache effects */
    7551        80658 :         pages_fetched = numIndexPages * num_scans;
    7552              : 
    7553              :         /* use Mackert and Lohman formula to adjust for cache effects */
    7554        80658 :         pages_fetched = index_pages_fetched(pages_fetched,
    7555              :                                             index->pages,
    7556        80658 :                                             (double) index->pages,
    7557              :                                             root);
    7558              : 
    7559              :         /*
    7560              :          * Now compute the total disk access cost, and then report a pro-rated
    7561              :          * share for each outer scan.  (Don't pro-rate for ScalarArrayOpExpr,
    7562              :          * since that's internal to the indexscan.)
    7563              :          */
    7564        80658 :         indexTotalCost = (pages_fetched * spc_random_page_cost)
    7565              :             / num_outer_scans;
    7566              :     }
    7567              :     else
    7568              :     {
    7569              :         /*
    7570              :          * For a single index scan, we just charge spc_random_page_cost per
    7571              :          * page touched.
    7572              :          */
    7573       577464 :         indexTotalCost = numIndexPages * spc_random_page_cost;
    7574              :     }
    7575              : 
    7576              :     /*
    7577              :      * CPU cost: any complex expressions in the indexquals will need to be
    7578              :      * evaluated once at the start of the scan to reduce them to runtime keys
    7579              :      * to pass to the index AM (see nodeIndexscan.c).  We model the per-tuple
    7580              :      * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
    7581              :      * indexqual operator.  Because we have numIndexTuples as a per-scan
    7582              :      * number, we have to multiply by num_sa_scans to get the correct result
    7583              :      * for ScalarArrayOpExpr cases.  Similarly add in costs for any index
    7584              :      * ORDER BY expressions.
    7585              :      *
    7586              :      * Note: this neglects the possible costs of rechecking lossy operators.
    7587              :      * Detecting that that might be needed seems more expensive than it's
    7588              :      * worth, though, considering all the other inaccuracies here ...
    7589              :      */
    7590       658122 :     qual_arg_cost = index_other_operands_eval_cost(root, indexQuals) +
    7591       658122 :         index_other_operands_eval_cost(root, indexOrderBys);
    7592       658122 :     qual_op_cost = cpu_operator_cost *
    7593       658122 :         (list_length(indexQuals) + list_length(indexOrderBys));
    7594              : 
    7595       658122 :     indexStartupCost = qual_arg_cost;
    7596       658122 :     indexTotalCost += qual_arg_cost;
    7597       658122 :     indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
    7598              : 
    7599              :     /*
    7600              :      * Generic assumption about index correlation: there isn't any.
    7601              :      */
    7602       658122 :     indexCorrelation = 0.0;
    7603              : 
    7604              :     /*
    7605              :      * Return everything to caller.
    7606              :      */
    7607       658122 :     costs->indexStartupCost = indexStartupCost;
    7608       658122 :     costs->indexTotalCost = indexTotalCost;
    7609       658122 :     costs->indexSelectivity = indexSelectivity;
    7610       658122 :     costs->indexCorrelation = indexCorrelation;
    7611       658122 :     costs->numIndexPages = numIndexPages;
    7612       658122 :     costs->numIndexTuples = numIndexTuples;
    7613       658122 :     costs->spc_random_page_cost = spc_random_page_cost;
    7614       658122 :     costs->num_sa_scans = num_sa_scans;
    7615       658122 : }
    7616              : 
    7617              : /*
    7618              :  * If the index is partial, add its predicate to the given qual list.
    7619              :  *
    7620              :  * ANDing the index predicate with the explicitly given indexquals produces
    7621              :  * a more accurate idea of the index's selectivity.  However, we need to be
    7622              :  * careful not to insert redundant clauses, because clauselist_selectivity()
    7623              :  * is easily fooled into computing a too-low selectivity estimate.  Our
    7624              :  * approach is to add only the predicate clause(s) that cannot be proven to
    7625              :  * be implied by the given indexquals.  This successfully handles cases such
    7626              :  * as a qual "x = 42" used with a partial index "WHERE x >= 40 AND x < 50".
    7627              :  * There are many other cases where we won't detect redundancy, leading to a
    7628              :  * too-low selectivity estimate, which will bias the system in favor of using
    7629              :  * partial indexes where possible.  That is not necessarily bad though.
    7630              :  *
    7631              :  * Note that indexQuals contains RestrictInfo nodes while the indpred
    7632              :  * does not, so the output list will be mixed.  This is OK for both
    7633              :  * predicate_implied_by() and clauselist_selectivity(), but might be
    7634              :  * problematic if the result were passed to other things.
    7635              :  */
    7636              : List *
    7637      1130623 : add_predicate_to_index_quals(IndexOptInfo *index, List *indexQuals)
    7638              : {
    7639      1130623 :     List       *predExtraQuals = NIL;
    7640              :     ListCell   *lc;
    7641              : 
    7642      1130623 :     if (index->indpred == NIL)
    7643      1129087 :         return indexQuals;
    7644              : 
    7645         3082 :     foreach(lc, index->indpred)
    7646              :     {
    7647         1546 :         Node       *predQual = (Node *) lfirst(lc);
    7648         1546 :         List       *oneQual = list_make1(predQual);
    7649              : 
    7650         1546 :         if (!predicate_implied_by(oneQual, indexQuals, false))
    7651         1370 :             predExtraQuals = list_concat(predExtraQuals, oneQual);
    7652              :     }
    7653         1536 :     return list_concat(predExtraQuals, indexQuals);
    7654              : }
    7655              : 
    7656              : /*
    7657              :  * Estimate correlation of btree index's first column.
    7658              :  *
    7659              :  * If we can get an estimate of the first column's ordering correlation C
    7660              :  * from pg_statistic, estimate the index correlation as C for a single-column
    7661              :  * index, or C * 0.75 for multiple columns.  The idea here is that multiple
    7662              :  * columns dilute the importance of the first column's ordering, but don't
    7663              :  * negate it entirely.
    7664              :  *
    7665              :  * We already filled in the stats tuple for *vardata when called.
    7666              :  */
    7667              : static double
    7668       426477 : btcost_correlation(IndexOptInfo *index, VariableStatData *vardata)
    7669              : {
    7670              :     Oid         sortop;
    7671              :     AttStatsSlot sslot;
    7672       426477 :     double      indexCorrelation = 0;
    7673              : 
    7674              :     Assert(HeapTupleIsValid(vardata->statsTuple));
    7675              : 
    7676       426477 :     sortop = get_opfamily_member(index->opfamily[0],
    7677       426477 :                                  index->opcintype[0],
    7678       426477 :                                  index->opcintype[0],
    7679              :                                  BTLessStrategyNumber);
    7680       852954 :     if (OidIsValid(sortop) &&
    7681       426477 :         get_attstatsslot(&sslot, vardata->statsTuple,
    7682              :                          STATISTIC_KIND_CORRELATION, sortop,
    7683              :                          ATTSTATSSLOT_NUMBERS))
    7684              :     {
    7685              :         double      varCorrelation;
    7686              : 
    7687              :         Assert(sslot.nnumbers == 1);
    7688       420951 :         varCorrelation = sslot.numbers[0];
    7689              : 
    7690       420951 :         if (index->reverse_sort[0])
    7691            0 :             varCorrelation = -varCorrelation;
    7692              : 
    7693       420951 :         if (index->nkeycolumns > 1)
    7694       146471 :             indexCorrelation = varCorrelation * 0.75;
    7695              :         else
    7696       274480 :             indexCorrelation = varCorrelation;
    7697              : 
    7698       420951 :         free_attstatsslot(&sslot);
    7699              :     }
    7700              : 
    7701       426477 :     return indexCorrelation;
    7702              : }
    7703              : 
    7704              : void
    7705       651154 : btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    7706              :                Cost *indexStartupCost, Cost *indexTotalCost,
    7707              :                Selectivity *indexSelectivity, double *indexCorrelation,
    7708              :                double *indexPages)
    7709              : {
    7710       651154 :     IndexOptInfo *index = path->indexinfo;
    7711       651154 :     GenericCosts costs = {0};
    7712       651154 :     VariableStatData vardata = {0};
    7713              :     double      numIndexTuples;
    7714              :     Cost        descentCost;
    7715              :     List       *indexBoundQuals;
    7716              :     List       *indexSkipQuals;
    7717              :     int         indexcol;
    7718              :     bool        eqQualHere;
    7719              :     bool        found_row_compare;
    7720              :     bool        found_array;
    7721              :     bool        found_is_null_op;
    7722       651154 :     bool        have_correlation = false;
    7723              :     double      num_sa_scans;
    7724       651154 :     double      correlation = 0.0;
    7725              :     ListCell   *lc;
    7726              : 
    7727              :     /*
    7728              :      * For a btree scan, only leading '=' quals plus inequality quals for the
    7729              :      * immediately next attribute contribute to index selectivity (these are
    7730              :      * the "boundary quals" that determine the starting and stopping points of
    7731              :      * the index scan).  Additional quals can suppress visits to the heap, so
    7732              :      * it's OK to count them in indexSelectivity, but they should not count
    7733              :      * for estimating numIndexTuples.  So we must examine the given indexquals
    7734              :      * to find out which ones count as boundary quals.  We rely on the
    7735              :      * knowledge that they are given in index column order.  Note that nbtree
    7736              :      * preprocessing can add skip arrays that act as leading '=' quals in the
    7737              :      * absence of ordinary input '=' quals, so in practice _most_ input quals
    7738              :      * are able to act as index bound quals (which we take into account here).
    7739              :      *
    7740              :      * For a RowCompareExpr, we consider only the first column, just as
    7741              :      * rowcomparesel() does.
    7742              :      *
    7743              :      * If there's a SAOP or skip array in the quals, we'll actually perform up
    7744              :      * to N index descents (not just one), but the underlying array key's
    7745              :      * operator can be considered to act the same as it normally does.
    7746              :      */
    7747       651154 :     indexBoundQuals = NIL;
    7748       651154 :     indexSkipQuals = NIL;
    7749       651154 :     indexcol = 0;
    7750       651154 :     eqQualHere = false;
    7751       651154 :     found_row_compare = false;
    7752       651154 :     found_array = false;
    7753       651154 :     found_is_null_op = false;
    7754       651154 :     num_sa_scans = 1;
    7755      1094650 :     foreach(lc, path->indexclauses)
    7756              :     {
    7757       471312 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
    7758              :         ListCell   *lc2;
    7759              : 
    7760       471312 :         if (indexcol < iclause->indexcol)
    7761              :         {
    7762        93471 :             double      num_sa_scans_prev_cols = num_sa_scans;
    7763              : 
    7764              :             /*
    7765              :              * Beginning of a new column's quals.
    7766              :              *
    7767              :              * Skip scans use skip arrays, which are ScalarArrayOp style
    7768              :              * arrays that generate their elements procedurally and on demand.
    7769              :              * Given a multi-column index on "(a, b)", and an SQL WHERE clause
    7770              :              * "WHERE b = 42", a skip scan will effectively use an indexqual
    7771              :              * "WHERE a = ANY('{every col a value}') AND b = 42".  (Obviously,
    7772              :              * the array on "a" must also return "IS NULL" matches, since our
    7773              :              * WHERE clause used no strict operator on "a").
    7774              :              *
    7775              :              * Here we consider how nbtree will backfill skip arrays for any
    7776              :              * index columns that lacked an '=' qual.  This maintains our
    7777              :              * num_sa_scans estimate, and determines if this new column (the
    7778              :              * "iclause->indexcol" column, not the prior "indexcol" column)
    7779              :              * can have its RestrictInfos/quals added to indexBoundQuals.
    7780              :              *
    7781              :              * We'll need to handle columns that have inequality quals, where
    7782              :              * the skip array generates values from a range constrained by the
    7783              :              * quals (not every possible value).  We've been maintaining
    7784              :              * indexSkipQuals to help with this; it will now contain all of
    7785              :              * the prior column's quals (that is, indexcol's quals) when they
    7786              :              * might be used for this.
    7787              :              */
    7788        93471 :             if (found_row_compare)
    7789              :             {
    7790              :                 /*
    7791              :                  * Skip arrays can't be added after a RowCompare input qual
    7792              :                  * due to limitations in nbtree
    7793              :                  */
    7794           20 :                 break;
    7795              :             }
    7796        93451 :             if (eqQualHere)
    7797              :             {
    7798              :                 /*
    7799              :                  * Don't need to add a skip array for an indexcol that already
    7800              :                  * has an '=' qual/equality constraint
    7801              :                  */
    7802        66128 :                 indexcol++;
    7803        66128 :                 indexSkipQuals = NIL;
    7804              :             }
    7805        93451 :             eqQualHere = false;
    7806              : 
    7807        95052 :             while (indexcol < iclause->indexcol)
    7808              :             {
    7809              :                 double      ndistinct;
    7810        29397 :                 bool        isdefault = true;
    7811              : 
    7812        29397 :                 found_array = true;
    7813              : 
    7814              :                 /*
    7815              :                  * A skipped attribute's ndistinct forms the basis of our
    7816              :                  * estimate of the total number of "array elements" used by
    7817              :                  * its skip array at runtime.  Look that up first.
    7818              :                  */
    7819        29397 :                 examine_indexcol_variable(root, index, indexcol, &vardata);
    7820        29397 :                 ndistinct = get_variable_numdistinct(&vardata, &isdefault);
    7821              : 
    7822        29397 :                 if (indexcol == 0)
    7823              :                 {
    7824              :                     /*
    7825              :                      * Get an estimate of the leading column's correlation in
    7826              :                      * passing (avoids rereading variable stats below)
    7827              :                      */
    7828        27313 :                     if (HeapTupleIsValid(vardata.statsTuple))
    7829        16014 :                         correlation = btcost_correlation(index, &vardata);
    7830        27313 :                     have_correlation = true;
    7831              :                 }
    7832              : 
    7833        29397 :                 ReleaseVariableStats(vardata);
    7834              : 
    7835              :                 /*
    7836              :                  * If ndistinct is a default estimate, conservatively assume
    7837              :                  * that no skipping will happen at runtime
    7838              :                  */
    7839        29397 :                 if (isdefault)
    7840              :                 {
    7841         8973 :                     num_sa_scans = num_sa_scans_prev_cols;
    7842        27796 :                     break;      /* done building indexBoundQuals */
    7843              :                 }
    7844              : 
    7845              :                 /*
    7846              :                  * Apply indexcol's indexSkipQuals selectivity to ndistinct
    7847              :                  */
    7848        20424 :                 if (indexSkipQuals != NIL)
    7849              :                 {
    7850              :                     List       *partialSkipQuals;
    7851              :                     Selectivity ndistinctfrac;
    7852              : 
    7853              :                     /*
    7854              :                      * If the index is partial, AND the index predicate with
    7855              :                      * the index-bound quals to produce a more accurate idea
    7856              :                      * of the number of distinct values for prior indexcol
    7857              :                      */
    7858          552 :                     partialSkipQuals = add_predicate_to_index_quals(index,
    7859              :                                                                     indexSkipQuals);
    7860              : 
    7861          552 :                     ndistinctfrac = clauselist_selectivity(root, partialSkipQuals,
    7862          552 :                                                            index->rel->relid,
    7863              :                                                            JOIN_INNER,
    7864              :                                                            NULL);
    7865              : 
    7866              :                     /*
    7867              :                      * If ndistinctfrac is selective (on its own), the scan is
    7868              :                      * unlikely to benefit from repositioning itself using
    7869              :                      * later quals.  Do not allow iclause->indexcol's quals to
    7870              :                      * be added to indexBoundQuals (it would increase descent
    7871              :                      * costs, without lowering numIndexTuples costs by much).
    7872              :                      */
    7873          552 :                     if (ndistinctfrac < DEFAULT_RANGE_INEQ_SEL)
    7874              :                     {
    7875          311 :                         num_sa_scans = num_sa_scans_prev_cols;
    7876          311 :                         break;  /* done building indexBoundQuals */
    7877              :                     }
    7878              : 
    7879              :                     /* Adjust ndistinct downward */
    7880          241 :                     ndistinct = rint(ndistinct * ndistinctfrac);
    7881          241 :                     ndistinct = Max(ndistinct, 1);
    7882              :                 }
    7883              : 
    7884              :                 /*
    7885              :                  * When there's no inequality quals, account for the need to
    7886              :                  * find an initial value by counting -inf/+inf as a value.
    7887              :                  *
    7888              :                  * We don't charge anything extra for possible next/prior key
    7889              :                  * index probes, which are sometimes used to find the next
    7890              :                  * valid skip array element (ahead of using the located
    7891              :                  * element value to relocate the scan to the next position
    7892              :                  * that might contain matching tuples).  It seems hard to do
    7893              :                  * better here.  Use of the skip support infrastructure often
    7894              :                  * avoids most next/prior key probes.  But even when it can't,
    7895              :                  * there's a decent chance that most individual next/prior key
    7896              :                  * probes will locate a leaf page whose key space overlaps all
    7897              :                  * of the scan's keys (even the lower-order keys) -- which
    7898              :                  * also avoids the need for a separate, extra index descent.
    7899              :                  * Note also that these probes are much cheaper than non-probe
    7900              :                  * primitive index scans: they're reliably very selective.
    7901              :                  */
    7902        20113 :                 if (indexSkipQuals == NIL)
    7903        19872 :                     ndistinct += 1;
    7904              : 
    7905              :                 /*
    7906              :                  * Update num_sa_scans estimate by multiplying by ndistinct.
    7907              :                  *
    7908              :                  * We make the pessimistic assumption that there is no
    7909              :                  * naturally occurring cross-column correlation.  This is
    7910              :                  * often wrong, but it seems best to err on the side of not
    7911              :                  * expecting skipping to be helpful...
    7912              :                  */
    7913        20113 :                 num_sa_scans *= ndistinct;
    7914              : 
    7915              :                 /*
    7916              :                  * ...but back out of adding this latest group of 1 or more
    7917              :                  * skip arrays when num_sa_scans exceeds the total number of
    7918              :                  * index pages (revert to num_sa_scans from before indexcol).
    7919              :                  * This causes a sharp discontinuity in cost (as a function of
    7920              :                  * the indexcol's ndistinct), but that is representative of
    7921              :                  * actual runtime costs.
    7922              :                  *
    7923              :                  * Note that skipping is helpful when each primitive index
    7924              :                  * scan only manages to skip over 1 or 2 irrelevant leaf pages
    7925              :                  * on average.  Skip arrays bring savings in CPU costs due to
    7926              :                  * the scan not needing to evaluate indexquals against every
    7927              :                  * tuple, which can greatly exceed any savings in I/O costs.
    7928              :                  * This test is a test of whether num_sa_scans implies that
    7929              :                  * we're past the point where the ability to skip ceases to
    7930              :                  * lower the scan's costs (even qual evaluation CPU costs).
    7931              :                  */
    7932        20113 :                 if (index->pages < num_sa_scans)
    7933              :                 {
    7934        18512 :                     num_sa_scans = num_sa_scans_prev_cols;
    7935        18512 :                     break;      /* done building indexBoundQuals */
    7936              :                 }
    7937              : 
    7938         1601 :                 indexcol++;
    7939         1601 :                 indexSkipQuals = NIL;
    7940              :             }
    7941              : 
    7942              :             /*
    7943              :              * Finished considering the need to add skip arrays to bridge an
    7944              :              * initial eqQualHere gap between the old and new index columns
    7945              :              * (or there was no initial eqQualHere gap in the first place).
    7946              :              *
    7947              :              * If an initial gap could not be bridged, then new column's quals
    7948              :              * (i.e. iclause->indexcol's quals) won't go into indexBoundQuals,
    7949              :              * and so won't affect our final numIndexTuples estimate.
    7950              :              */
    7951        93451 :             if (indexcol != iclause->indexcol)
    7952        27796 :                 break;          /* done building indexBoundQuals */
    7953              :         }
    7954              : 
    7955              :         Assert(indexcol == iclause->indexcol);
    7956              : 
    7957              :         /* Examine each indexqual associated with this index clause */
    7958       889297 :         foreach(lc2, iclause->indexquals)
    7959              :         {
    7960       445801 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
    7961       445801 :             Expr       *clause = rinfo->clause;
    7962       445801 :             Oid         clause_op = InvalidOid;
    7963              :             int         op_strategy;
    7964              : 
    7965       445801 :             if (IsA(clause, OpExpr))
    7966              :             {
    7967       431345 :                 OpExpr     *op = (OpExpr *) clause;
    7968              : 
    7969       431345 :                 clause_op = op->opno;
    7970              :             }
    7971        14456 :             else if (IsA(clause, RowCompareExpr))
    7972              :             {
    7973          330 :                 RowCompareExpr *rc = (RowCompareExpr *) clause;
    7974              : 
    7975          330 :                 clause_op = linitial_oid(rc->opnos);
    7976          330 :                 found_row_compare = true;
    7977              :             }
    7978        14126 :             else if (IsA(clause, ScalarArrayOpExpr))
    7979              :             {
    7980        12228 :                 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
    7981        12228 :                 Node       *other_operand = (Node *) lsecond(saop->args);
    7982        12228 :                 double      alength = estimate_array_length(root, other_operand);
    7983              : 
    7984        12228 :                 clause_op = saop->opno;
    7985        12228 :                 found_array = true;
    7986              :                 /* estimate SA descents by indexBoundQuals only */
    7987        12228 :                 if (alength > 1)
    7988        12004 :                     num_sa_scans *= alength;
    7989              :             }
    7990         1898 :             else if (IsA(clause, NullTest))
    7991              :             {
    7992         1898 :                 NullTest   *nt = (NullTest *) clause;
    7993              : 
    7994         1898 :                 if (nt->nulltesttype == IS_NULL)
    7995              :                 {
    7996          200 :                     found_is_null_op = true;
    7997              :                     /* IS NULL is like = for selectivity/skip scan purposes */
    7998          200 :                     eqQualHere = true;
    7999              :                 }
    8000              :             }
    8001              :             else
    8002            0 :                 elog(ERROR, "unsupported indexqual type: %d",
    8003              :                      (int) nodeTag(clause));
    8004              : 
    8005              :             /* check for equality operator */
    8006       445801 :             if (OidIsValid(clause_op))
    8007              :             {
    8008       443903 :                 op_strategy = get_op_opfamily_strategy(clause_op,
    8009       443903 :                                                        index->opfamily[indexcol]);
    8010              :                 Assert(op_strategy != 0);   /* not a member of opfamily?? */
    8011       443903 :                 if (op_strategy == BTEqualStrategyNumber)
    8012       420057 :                     eqQualHere = true;
    8013              :             }
    8014              : 
    8015       445801 :             indexBoundQuals = lappend(indexBoundQuals, rinfo);
    8016              : 
    8017              :             /*
    8018              :              * We apply inequality selectivities to estimate index descent
    8019              :              * costs with scans that use skip arrays.  Save this indexcol's
    8020              :              * RestrictInfos if it looks like they'll be needed for that.
    8021              :              */
    8022       445801 :             if (!eqQualHere && !found_row_compare &&
    8023        24647 :                 indexcol < index->nkeycolumns - 1)
    8024         4772 :                 indexSkipQuals = lappend(indexSkipQuals, rinfo);
    8025              :         }
    8026              :     }
    8027              : 
    8028              :     /*
    8029              :      * If index is unique and we found an '=' clause for each column, we can
    8030              :      * just assume numIndexTuples = 1 and skip the expensive
    8031              :      * clauselist_selectivity calculations.  However, an array or NullTest
    8032              :      * always invalidates that theory (even when eqQualHere has been set).
    8033              :      */
    8034       651154 :     if (index->unique &&
    8035       520558 :         indexcol == index->nkeycolumns - 1 &&
    8036       186236 :         eqQualHere &&
    8037       186236 :         !found_array &&
    8038       180791 :         !found_is_null_op)
    8039       180751 :         numIndexTuples = 1.0;
    8040              :     else
    8041              :     {
    8042              :         List       *selectivityQuals;
    8043              :         Selectivity btreeSelectivity;
    8044              : 
    8045              :         /*
    8046              :          * If the index is partial, AND the index predicate with the
    8047              :          * index-bound quals to produce a more accurate idea of the number of
    8048              :          * rows covered by the bound conditions.
    8049              :          */
    8050       470403 :         selectivityQuals = add_predicate_to_index_quals(index, indexBoundQuals);
    8051              : 
    8052       470403 :         btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
    8053       470403 :                                                   index->rel->relid,
    8054              :                                                   JOIN_INNER,
    8055              :                                                   NULL);
    8056       470403 :         numIndexTuples = btreeSelectivity * index->rel->tuples;
    8057              : 
    8058              :         /*
    8059              :          * btree automatically combines individual array element primitive
    8060              :          * index scans whenever the tuples covered by the next set of array
    8061              :          * keys are close to tuples covered by the current set.  That puts a
    8062              :          * natural ceiling on the worst case number of descents -- there
    8063              :          * cannot possibly be more than one descent per leaf page scanned.
    8064              :          *
    8065              :          * Clamp the number of descents to at most 1/3 the number of index
    8066              :          * pages.  This avoids implausibly high estimates with low selectivity
    8067              :          * paths, where scans usually require only one or two descents.  This
    8068              :          * is most likely to help when there are several SAOP clauses, where
    8069              :          * naively accepting the total number of distinct combinations of
    8070              :          * array elements as the number of descents would frequently lead to
    8071              :          * wild overestimates.
    8072              :          *
    8073              :          * We somewhat arbitrarily don't just make the cutoff the total number
    8074              :          * of leaf pages (we make it 1/3 the total number of pages instead) to
    8075              :          * give the btree code credit for its ability to continue on the leaf
    8076              :          * level with low selectivity scans.
    8077              :          *
    8078              :          * Note: num_sa_scans includes both ScalarArrayOp array elements and
    8079              :          * skip array elements whose qual affects our numIndexTuples estimate.
    8080              :          */
    8081       470403 :         num_sa_scans = Min(num_sa_scans, ceil(index->pages * 0.3333333));
    8082       470403 :         num_sa_scans = Max(num_sa_scans, 1);
    8083              : 
    8084              :         /*
    8085              :          * As in genericcostestimate(), we have to adjust for any array quals
    8086              :          * included in indexBoundQuals, and then round to integer.
    8087              :          *
    8088              :          * It is tempting to make genericcostestimate behave as if array
    8089              :          * clauses work in almost the same way as scalar operators during
    8090              :          * btree scans, making the top-level scan look like a continuous scan
    8091              :          * (as opposed to num_sa_scans-many primitive index scans).  After
    8092              :          * all, btree scans mostly work like that at runtime.  However, such a
    8093              :          * scheme would badly bias genericcostestimate's simplistic approach
    8094              :          * to calculating numIndexPages through prorating.
    8095              :          *
    8096              :          * Stick with the approach taken by non-native SAOP scans for now.
    8097              :          * genericcostestimate will use the Mackert-Lohman formula to
    8098              :          * compensate for repeat page fetches, even though that definitely
    8099              :          * won't happen during btree scans (not for leaf pages, at least).
    8100              :          * We're usually very pessimistic about the number of primitive index
    8101              :          * scans that will be required, but it's not clear how to do better.
    8102              :          */
    8103       470403 :         numIndexTuples = rint(numIndexTuples / num_sa_scans);
    8104              :     }
    8105              : 
    8106              :     /*
    8107              :      * Now do generic index cost estimation.
    8108              :      *
    8109              :      * While we expended effort to make realistic estimates of numIndexTuples
    8110              :      * and num_sa_scans, we are content to count only the btree metapage as
    8111              :      * non-leaf.  btree fanout is typically high enough that upper pages are
    8112              :      * few relative to leaf pages, so accounting for them would move the
    8113              :      * estimates at most a percent or two.  Given the uncertainty in just how
    8114              :      * many upper pages exist in a particular index, we'll skip trying to
    8115              :      * handle that.
    8116              :      */
    8117       651154 :     costs.numIndexTuples = numIndexTuples;
    8118       651154 :     costs.num_sa_scans = num_sa_scans;
    8119       651154 :     costs.numNonLeafPages = 1;
    8120              : 
    8121       651154 :     genericcostestimate(root, path, loop_count, &costs);
    8122              : 
    8123              :     /*
    8124              :      * Add a CPU-cost component to represent the costs of initial btree
    8125              :      * descent.  We don't charge any I/O cost for touching upper btree levels,
    8126              :      * since they tend to stay in cache, but we still have to do about log2(N)
    8127              :      * comparisons to descend a btree of N leaf tuples.  We charge one
    8128              :      * cpu_operator_cost per comparison.
    8129              :      *
    8130              :      * If there are SAOP or skip array keys, charge this once per estimated
    8131              :      * index descent.  The ones after the first one are not startup cost so
    8132              :      * far as the overall plan goes, so just add them to "total" cost.
    8133              :      */
    8134       651154 :     if (index->tuples > 1)        /* avoid computing log(0) */
    8135              :     {
    8136       589676 :         descentCost = ceil(log(index->tuples) / log(2.0)) * cpu_operator_cost;
    8137       589676 :         costs.indexStartupCost += descentCost;
    8138       589676 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
    8139              :     }
    8140              : 
    8141              :     /*
    8142              :      * Even though we're not charging I/O cost for touching upper btree pages,
    8143              :      * it's still reasonable to charge some CPU cost per page descended
    8144              :      * through.  Moreover, if we had no such charge at all, bloated indexes
    8145              :      * would appear to have the same search cost as unbloated ones, at least
    8146              :      * in cases where only a single leaf page is expected to be visited.  This
    8147              :      * cost is somewhat arbitrarily set at 50x cpu_operator_cost per page
    8148              :      * touched.  The number of such pages is btree tree height plus one (ie,
    8149              :      * we charge for the leaf page too).  As above, charge once per estimated
    8150              :      * SAOP/skip array descent.
    8151              :      */
    8152       651154 :     descentCost = (index->tree_height + 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
    8153       651154 :     costs.indexStartupCost += descentCost;
    8154       651154 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
    8155              : 
    8156       651154 :     if (!have_correlation)
    8157              :     {
    8158       623841 :         examine_indexcol_variable(root, index, 0, &vardata);
    8159       623841 :         if (HeapTupleIsValid(vardata.statsTuple))
    8160       410463 :             costs.indexCorrelation = btcost_correlation(index, &vardata);
    8161       623841 :         ReleaseVariableStats(vardata);
    8162              :     }
    8163              :     else
    8164              :     {
    8165              :         /* btcost_correlation already called earlier on */
    8166        27313 :         costs.indexCorrelation = correlation;
    8167              :     }
    8168              : 
    8169       651154 :     *indexStartupCost = costs.indexStartupCost;
    8170       651154 :     *indexTotalCost = costs.indexTotalCost;
    8171       651154 :     *indexSelectivity = costs.indexSelectivity;
    8172       651154 :     *indexCorrelation = costs.indexCorrelation;
    8173       651154 :     *indexPages = costs.numIndexPages;
    8174       651154 : }
    8175              : 
    8176              : void
    8177          308 : hashcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    8178              :                  Cost *indexStartupCost, Cost *indexTotalCost,
    8179              :                  Selectivity *indexSelectivity, double *indexCorrelation,
    8180              :                  double *indexPages)
    8181              : {
    8182          308 :     GenericCosts costs = {0};
    8183              : 
    8184              :     /* As in btcostestimate, count only the metapage as non-leaf */
    8185          308 :     costs.numNonLeafPages = 1;
    8186              : 
    8187          308 :     genericcostestimate(root, path, loop_count, &costs);
    8188              : 
    8189              :     /*
    8190              :      * A hash index has no descent costs as such, since the index AM can go
    8191              :      * directly to the target bucket after computing the hash value.  There
    8192              :      * are a couple of other hash-specific costs that we could conceivably add
    8193              :      * here, though:
    8194              :      *
    8195              :      * Ideally we'd charge spc_random_page_cost for each page in the target
    8196              :      * bucket, not just the numIndexPages pages that genericcostestimate
    8197              :      * thought we'd visit.  However in most cases we don't know which bucket
    8198              :      * that will be.  There's no point in considering the average bucket size
    8199              :      * because the hash AM makes sure that's always one page.
    8200              :      *
    8201              :      * Likewise, we could consider charging some CPU for each index tuple in
    8202              :      * the bucket, if we knew how many there were.  But the per-tuple cost is
    8203              :      * just a hash value comparison, not a general datatype-dependent
    8204              :      * comparison, so any such charge ought to be quite a bit less than
    8205              :      * cpu_operator_cost; which makes it probably not worth worrying about.
    8206              :      *
    8207              :      * A bigger issue is that chance hash-value collisions will result in
    8208              :      * wasted probes into the heap.  We don't currently attempt to model this
    8209              :      * cost on the grounds that it's rare, but maybe it's not rare enough.
    8210              :      * (Any fix for this ought to consider the generic lossy-operator problem,
    8211              :      * though; it's not entirely hash-specific.)
    8212              :      */
    8213              : 
    8214          308 :     *indexStartupCost = costs.indexStartupCost;
    8215          308 :     *indexTotalCost = costs.indexTotalCost;
    8216          308 :     *indexSelectivity = costs.indexSelectivity;
    8217          308 :     *indexCorrelation = costs.indexCorrelation;
    8218          308 :     *indexPages = costs.numIndexPages;
    8219          308 : }
    8220              : 
    8221              : void
    8222         4772 : gistcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    8223              :                  Cost *indexStartupCost, Cost *indexTotalCost,
    8224              :                  Selectivity *indexSelectivity, double *indexCorrelation,
    8225              :                  double *indexPages)
    8226              : {
    8227         4772 :     IndexOptInfo *index = path->indexinfo;
    8228         4772 :     GenericCosts costs = {0};
    8229              :     Cost        descentCost;
    8230              : 
    8231              :     /* GiST has no metapage, so we treat all pages as leaf pages */
    8232              : 
    8233         4772 :     genericcostestimate(root, path, loop_count, &costs);
    8234              : 
    8235              :     /*
    8236              :      * We model index descent costs similarly to those for btree, but to do
    8237              :      * that we first need an idea of the tree height.  We somewhat arbitrarily
    8238              :      * assume that the fanout is 100, meaning the tree height is at most
    8239              :      * log100(index->pages).
    8240              :      *
    8241              :      * Although this computation isn't really expensive enough to require
    8242              :      * caching, we might as well use index->tree_height to cache it.
    8243              :      */
    8244         4772 :     if (index->tree_height < 0) /* unknown? */
    8245              :     {
    8246         4745 :         if (index->pages > 1) /* avoid computing log(0) */
    8247         1956 :             index->tree_height = (int) (log(index->pages) / log(100.0));
    8248              :         else
    8249         2789 :             index->tree_height = 0;
    8250              :     }
    8251              : 
    8252              :     /*
    8253              :      * Add a CPU-cost component to represent the costs of initial descent. We
    8254              :      * just use log(N) here not log2(N) since the branching factor isn't
    8255              :      * necessarily two anyway.  As for btree, charge once per SA scan.
    8256              :      */
    8257         4772 :     if (index->tuples > 1)        /* avoid computing log(0) */
    8258              :     {
    8259         4772 :         descentCost = ceil(log(index->tuples)) * cpu_operator_cost;
    8260         4772 :         costs.indexStartupCost += descentCost;
    8261         4772 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
    8262              :     }
    8263              : 
    8264              :     /*
    8265              :      * Likewise add a per-page charge, calculated the same as for btrees.
    8266              :      */
    8267         4772 :     descentCost = (index->tree_height + 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
    8268         4772 :     costs.indexStartupCost += descentCost;
    8269         4772 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
    8270              : 
    8271         4772 :     *indexStartupCost = costs.indexStartupCost;
    8272         4772 :     *indexTotalCost = costs.indexTotalCost;
    8273         4772 :     *indexSelectivity = costs.indexSelectivity;
    8274         4772 :     *indexCorrelation = costs.indexCorrelation;
    8275         4772 :     *indexPages = costs.numIndexPages;
    8276         4772 : }
    8277              : 
    8278              : void
    8279         1482 : spgcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    8280              :                 Cost *indexStartupCost, Cost *indexTotalCost,
    8281              :                 Selectivity *indexSelectivity, double *indexCorrelation,
    8282              :                 double *indexPages)
    8283              : {
    8284         1482 :     IndexOptInfo *index = path->indexinfo;
    8285         1482 :     GenericCosts costs = {0};
    8286              :     Cost        descentCost;
    8287              : 
    8288              :     /* As in btcostestimate, count only the metapage as non-leaf */
    8289         1482 :     costs.numNonLeafPages = 1;
    8290              : 
    8291         1482 :     genericcostestimate(root, path, loop_count, &costs);
    8292              : 
    8293              :     /*
    8294              :      * We model index descent costs similarly to those for btree, but to do
    8295              :      * that we first need an idea of the tree height.  We somewhat arbitrarily
    8296              :      * assume that the fanout is 100, meaning the tree height is at most
    8297              :      * log100(index->pages).
    8298              :      *
    8299              :      * Although this computation isn't really expensive enough to require
    8300              :      * caching, we might as well use index->tree_height to cache it.
    8301              :      */
    8302         1482 :     if (index->tree_height < 0) /* unknown? */
    8303              :     {
    8304         1477 :         if (index->pages > 1) /* avoid computing log(0) */
    8305         1477 :             index->tree_height = (int) (log(index->pages) / log(100.0));
    8306              :         else
    8307            0 :             index->tree_height = 0;
    8308              :     }
    8309              : 
    8310              :     /*
    8311              :      * Add a CPU-cost component to represent the costs of initial descent. We
    8312              :      * just use log(N) here not log2(N) since the branching factor isn't
    8313              :      * necessarily two anyway.  As for btree, charge once per SA scan.
    8314              :      */
    8315         1482 :     if (index->tuples > 1)        /* avoid computing log(0) */
    8316              :     {
    8317         1482 :         descentCost = ceil(log(index->tuples)) * cpu_operator_cost;
    8318         1482 :         costs.indexStartupCost += descentCost;
    8319         1482 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
    8320              :     }
    8321              : 
    8322              :     /*
    8323              :      * Likewise add a per-page charge, calculated the same as for btrees.
    8324              :      */
    8325         1482 :     descentCost = (index->tree_height + 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
    8326         1482 :     costs.indexStartupCost += descentCost;
    8327         1482 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
    8328              : 
    8329         1482 :     *indexStartupCost = costs.indexStartupCost;
    8330         1482 :     *indexTotalCost = costs.indexTotalCost;
    8331         1482 :     *indexSelectivity = costs.indexSelectivity;
    8332         1482 :     *indexCorrelation = costs.indexCorrelation;
    8333         1482 :     *indexPages = costs.numIndexPages;
    8334         1482 : }
    8335              : 
    8336              : 
    8337              : /*
    8338              :  * Support routines for gincostestimate
    8339              :  */
    8340              : 
    8341              : typedef struct
    8342              : {
    8343              :     bool        attHasFullScan[INDEX_MAX_KEYS];
    8344              :     bool        attHasNormalScan[INDEX_MAX_KEYS];
    8345              :     double      partialEntries;
    8346              :     double      exactEntries;
    8347              :     double      searchEntries;
    8348              :     double      arrayScans;
    8349              : } GinQualCounts;
    8350              : 
    8351              : /*
    8352              :  * Estimate the number of index terms that need to be searched for while
    8353              :  * testing the given GIN query, and increment the counts in *counts
    8354              :  * appropriately.  If the query is unsatisfiable, return false.
    8355              :  */
    8356              : static bool
    8357         1714 : gincost_pattern(IndexOptInfo *index, int indexcol,
    8358              :                 Oid clause_op, Datum query,
    8359              :                 GinQualCounts *counts)
    8360              : {
    8361              :     FmgrInfo    flinfo;
    8362              :     Oid         extractProcOid;
    8363              :     Oid         collation;
    8364              :     int         strategy_op;
    8365              :     Oid         lefttype,
    8366              :                 righttype;
    8367         1714 :     int32       nentries = 0;
    8368         1714 :     bool       *partial_matches = NULL;
    8369         1714 :     Pointer    *extra_data = NULL;
    8370         1714 :     bool       *nullFlags = NULL;
    8371         1714 :     int32       searchMode = GIN_SEARCH_MODE_DEFAULT;
    8372              :     int32       i;
    8373              : 
    8374              :     Assert(indexcol < index->nkeycolumns);
    8375              : 
    8376              :     /*
    8377              :      * Get the operator's strategy number and declared input data types within
    8378              :      * the index opfamily.  (We don't need the latter, but we use
    8379              :      * get_op_opfamily_properties because it will throw error if it fails to
    8380              :      * find a matching pg_amop entry.)
    8381              :      */
    8382         1714 :     get_op_opfamily_properties(clause_op, index->opfamily[indexcol], false,
    8383              :                                &strategy_op, &lefttype, &righttype);
    8384              : 
    8385              :     /*
    8386              :      * GIN always uses the "default" support functions, which are those with
    8387              :      * lefttype == righttype == the opclass' opcintype (see
    8388              :      * IndexSupportInitialize in relcache.c).
    8389              :      */
    8390         1714 :     extractProcOid = get_opfamily_proc(index->opfamily[indexcol],
    8391         1714 :                                        index->opcintype[indexcol],
    8392         1714 :                                        index->opcintype[indexcol],
    8393              :                                        GIN_EXTRACTQUERY_PROC);
    8394              : 
    8395         1714 :     if (!OidIsValid(extractProcOid))
    8396              :     {
    8397              :         /* should not happen; throw same error as index_getprocinfo */
    8398            0 :         elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
    8399              :              GIN_EXTRACTQUERY_PROC, indexcol + 1,
    8400              :              get_rel_name(index->indexoid));
    8401              :     }
    8402              : 
    8403              :     /*
    8404              :      * Choose collation to pass to extractProc (should match initGinState).
    8405              :      */
    8406         1714 :     if (OidIsValid(index->indexcollations[indexcol]))
    8407          243 :         collation = index->indexcollations[indexcol];
    8408              :     else
    8409         1471 :         collation = DEFAULT_COLLATION_OID;
    8410              : 
    8411         1714 :     fmgr_info(extractProcOid, &flinfo);
    8412              : 
    8413         1714 :     set_fn_opclass_options(&flinfo, index->opclassoptions[indexcol]);
    8414              : 
    8415         1714 :     FunctionCall7Coll(&flinfo,
    8416              :                       collation,
    8417              :                       query,
    8418              :                       PointerGetDatum(&nentries),
    8419              :                       UInt16GetDatum(strategy_op),
    8420              :                       PointerGetDatum(&partial_matches),
    8421              :                       PointerGetDatum(&extra_data),
    8422              :                       PointerGetDatum(&nullFlags),
    8423              :                       PointerGetDatum(&searchMode));
    8424              : 
    8425         1714 :     if (nentries <= 0 && searchMode == GIN_SEARCH_MODE_DEFAULT)
    8426              :     {
    8427              :         /* No match is possible */
    8428           10 :         return false;
    8429              :     }
    8430              : 
    8431         5854 :     for (i = 0; i < nentries; i++)
    8432              :     {
    8433              :         /*
    8434              :          * For partial match we haven't any information to estimate number of
    8435              :          * matched entries in index, so, we just estimate it as 100
    8436              :          */
    8437         4150 :         if (partial_matches && partial_matches[i])
    8438          361 :             counts->partialEntries += 100;
    8439              :         else
    8440         3789 :             counts->exactEntries++;
    8441              : 
    8442         4150 :         counts->searchEntries++;
    8443              :     }
    8444              : 
    8445         1704 :     if (searchMode == GIN_SEARCH_MODE_DEFAULT)
    8446              :     {
    8447         1322 :         counts->attHasNormalScan[indexcol] = true;
    8448              :     }
    8449          382 :     else if (searchMode == GIN_SEARCH_MODE_INCLUDE_EMPTY)
    8450              :     {
    8451              :         /* Treat "include empty" like an exact-match item */
    8452           36 :         counts->attHasNormalScan[indexcol] = true;
    8453           36 :         counts->exactEntries++;
    8454           36 :         counts->searchEntries++;
    8455              :     }
    8456              :     else
    8457              :     {
    8458              :         /* It's GIN_SEARCH_MODE_ALL */
    8459          346 :         counts->attHasFullScan[indexcol] = true;
    8460              :     }
    8461              : 
    8462         1704 :     return true;
    8463              : }
    8464              : 
    8465              : /*
    8466              :  * Estimate the number of index terms that need to be searched for while
    8467              :  * testing the given GIN index clause, and increment the counts in *counts
    8468              :  * appropriately.  If the query is unsatisfiable, return false.
    8469              :  */
    8470              : static bool
    8471         1704 : gincost_opexpr(PlannerInfo *root,
    8472              :                IndexOptInfo *index,
    8473              :                int indexcol,
    8474              :                OpExpr *clause,
    8475              :                GinQualCounts *counts)
    8476              : {
    8477         1704 :     Oid         clause_op = clause->opno;
    8478         1704 :     Node       *operand = (Node *) lsecond(clause->args);
    8479              : 
    8480              :     /* aggressively reduce to a constant, and look through relabeling */
    8481         1704 :     operand = estimate_expression_value(root, operand);
    8482              : 
    8483         1704 :     if (IsA(operand, RelabelType))
    8484            0 :         operand = (Node *) ((RelabelType *) operand)->arg;
    8485              : 
    8486              :     /*
    8487              :      * It's impossible to call extractQuery method for unknown operand. So
    8488              :      * unless operand is a Const we can't do much; just assume there will be
    8489              :      * one ordinary search entry from the operand at runtime.
    8490              :      */
    8491         1704 :     if (!IsA(operand, Const))
    8492              :     {
    8493            0 :         counts->exactEntries++;
    8494            0 :         counts->searchEntries++;
    8495            0 :         return true;
    8496              :     }
    8497              : 
    8498              :     /* If Const is null, there can be no matches */
    8499         1704 :     if (((Const *) operand)->constisnull)
    8500            0 :         return false;
    8501              : 
    8502              :     /* Otherwise, apply extractQuery and get the actual term counts */
    8503         1704 :     return gincost_pattern(index, indexcol, clause_op,
    8504              :                            ((Const *) operand)->constvalue,
    8505              :                            counts);
    8506              : }
    8507              : 
    8508              : /*
    8509              :  * Estimate the number of index terms that need to be searched for while
    8510              :  * testing the given GIN index clause, and increment the counts in *counts
    8511              :  * appropriately.  If the query is unsatisfiable, return false.
    8512              :  *
    8513              :  * A ScalarArrayOpExpr will give rise to N separate indexscans at runtime,
    8514              :  * each of which involves one value from the RHS array, plus all the
    8515              :  * non-array quals (if any).  To model this, we average the counts across
    8516              :  * the RHS elements, and add the averages to the counts in *counts (which
    8517              :  * correspond to per-indexscan costs).  We also multiply counts->arrayScans
    8518              :  * by N, causing gincostestimate to scale up its estimates accordingly.
    8519              :  */
    8520              : static bool
    8521            5 : gincost_scalararrayopexpr(PlannerInfo *root,
    8522              :                           IndexOptInfo *index,
    8523              :                           int indexcol,
    8524              :                           ScalarArrayOpExpr *clause,
    8525              :                           double numIndexEntries,
    8526              :                           GinQualCounts *counts)
    8527              : {
    8528            5 :     Oid         clause_op = clause->opno;
    8529            5 :     Node       *rightop = (Node *) lsecond(clause->args);
    8530              :     ArrayType  *arrayval;
    8531              :     int16       elmlen;
    8532              :     bool        elmbyval;
    8533              :     char        elmalign;
    8534              :     int         numElems;
    8535              :     Datum      *elemValues;
    8536              :     bool       *elemNulls;
    8537              :     GinQualCounts arraycounts;
    8538            5 :     int         numPossible = 0;
    8539              :     int         i;
    8540              : 
    8541              :     Assert(clause->useOr);
    8542              : 
    8543              :     /* aggressively reduce to a constant, and look through relabeling */
    8544            5 :     rightop = estimate_expression_value(root, rightop);
    8545              : 
    8546            5 :     if (IsA(rightop, RelabelType))
    8547            0 :         rightop = (Node *) ((RelabelType *) rightop)->arg;
    8548              : 
    8549              :     /*
    8550              :      * It's impossible to call extractQuery method for unknown operand. So
    8551              :      * unless operand is a Const we can't do much; just assume there will be
    8552              :      * one ordinary search entry from each array entry at runtime, and fall
    8553              :      * back on a probably-bad estimate of the number of array entries.
    8554              :      */
    8555            5 :     if (!IsA(rightop, Const))
    8556              :     {
    8557            0 :         counts->exactEntries++;
    8558            0 :         counts->searchEntries++;
    8559            0 :         counts->arrayScans *= estimate_array_length(root, rightop);
    8560            0 :         return true;
    8561              :     }
    8562              : 
    8563              :     /* If Const is null, there can be no matches */
    8564            5 :     if (((Const *) rightop)->constisnull)
    8565            0 :         return false;
    8566              : 
    8567              :     /* Otherwise, extract the array elements and iterate over them */
    8568            5 :     arrayval = DatumGetArrayTypeP(((Const *) rightop)->constvalue);
    8569            5 :     get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
    8570              :                          &elmlen, &elmbyval, &elmalign);
    8571            5 :     deconstruct_array(arrayval,
    8572              :                       ARR_ELEMTYPE(arrayval),
    8573              :                       elmlen, elmbyval, elmalign,
    8574              :                       &elemValues, &elemNulls, &numElems);
    8575              : 
    8576            5 :     memset(&arraycounts, 0, sizeof(arraycounts));
    8577              : 
    8578           15 :     for (i = 0; i < numElems; i++)
    8579              :     {
    8580              :         GinQualCounts elemcounts;
    8581              : 
    8582              :         /* NULL can't match anything, so ignore, as the executor will */
    8583           10 :         if (elemNulls[i])
    8584            0 :             continue;
    8585              : 
    8586              :         /* Otherwise, apply extractQuery and get the actual term counts */
    8587           10 :         memset(&elemcounts, 0, sizeof(elemcounts));
    8588              : 
    8589           10 :         if (gincost_pattern(index, indexcol, clause_op, elemValues[i],
    8590              :                             &elemcounts))
    8591              :         {
    8592              :             /* We ignore array elements that are unsatisfiable patterns */
    8593           10 :             numPossible++;
    8594              : 
    8595           10 :             if (elemcounts.attHasFullScan[indexcol] &&
    8596            0 :                 !elemcounts.attHasNormalScan[indexcol])
    8597              :             {
    8598              :                 /*
    8599              :                  * Full index scan will be required.  We treat this as if
    8600              :                  * every key in the index had been listed in the query; is
    8601              :                  * that reasonable?
    8602              :                  */
    8603            0 :                 elemcounts.partialEntries = 0;
    8604            0 :                 elemcounts.exactEntries = numIndexEntries;
    8605            0 :                 elemcounts.searchEntries = numIndexEntries;
    8606              :             }
    8607           10 :             arraycounts.partialEntries += elemcounts.partialEntries;
    8608           10 :             arraycounts.exactEntries += elemcounts.exactEntries;
    8609           10 :             arraycounts.searchEntries += elemcounts.searchEntries;
    8610              :         }
    8611              :     }
    8612              : 
    8613            5 :     if (numPossible == 0)
    8614              :     {
    8615              :         /* No satisfiable patterns in the array */
    8616            0 :         return false;
    8617              :     }
    8618              : 
    8619              :     /*
    8620              :      * Now add the averages to the global counts.  This will give us an
    8621              :      * estimate of the average number of terms searched for in each indexscan,
    8622              :      * including contributions from both array and non-array quals.
    8623              :      */
    8624            5 :     counts->partialEntries += arraycounts.partialEntries / numPossible;
    8625            5 :     counts->exactEntries += arraycounts.exactEntries / numPossible;
    8626            5 :     counts->searchEntries += arraycounts.searchEntries / numPossible;
    8627              : 
    8628            5 :     counts->arrayScans *= numPossible;
    8629              : 
    8630            5 :     return true;
    8631              : }
    8632              : 
    8633              : /*
    8634              :  * GIN has search behavior completely different from other index types
    8635              :  */
    8636              : void
    8637         1546 : gincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    8638              :                 Cost *indexStartupCost, Cost *indexTotalCost,
    8639              :                 Selectivity *indexSelectivity, double *indexCorrelation,
    8640              :                 double *indexPages)
    8641              : {
    8642         1546 :     IndexOptInfo *index = path->indexinfo;
    8643         1546 :     List       *indexQuals = get_quals_from_indexclauses(path->indexclauses);
    8644              :     List       *selectivityQuals;
    8645         1546 :     double      numPages = index->pages,
    8646         1546 :                 numTuples = index->tuples;
    8647              :     double      numEntryPages,
    8648              :                 numDataPages,
    8649              :                 numPendingPages,
    8650              :                 numEntries;
    8651              :     GinQualCounts counts;
    8652              :     bool        matchPossible;
    8653              :     bool        fullIndexScan;
    8654              :     double      partialScale;
    8655              :     double      entryPagesFetched,
    8656              :                 dataPagesFetched,
    8657              :                 dataPagesFetchedBySel;
    8658              :     double      qual_op_cost,
    8659              :                 qual_arg_cost,
    8660              :                 spc_random_page_cost,
    8661              :                 outer_scans;
    8662              :     Cost        descentCost;
    8663              :     Relation    indexRel;
    8664              :     GinStatsData ginStats;
    8665              :     ListCell   *lc;
    8666              :     int         i;
    8667              : 
    8668              :     /*
    8669              :      * Obtain statistical information from the meta page, if possible.  Else
    8670              :      * set ginStats to zeroes, and we'll cope below.
    8671              :      */
    8672         1546 :     if (!index->hypothetical)
    8673              :     {
    8674              :         /* Lock should have already been obtained in plancat.c */
    8675         1546 :         indexRel = index_open(index->indexoid, NoLock);
    8676         1546 :         ginGetStats(indexRel, &ginStats);
    8677         1546 :         index_close(indexRel, NoLock);
    8678              :     }
    8679              :     else
    8680              :     {
    8681            0 :         memset(&ginStats, 0, sizeof(ginStats));
    8682              :     }
    8683              : 
    8684              :     /*
    8685              :      * Assuming we got valid (nonzero) stats at all, nPendingPages can be
    8686              :      * trusted, but the other fields are data as of the last VACUUM.  We can
    8687              :      * scale them up to account for growth since then, but that method only
    8688              :      * goes so far; in the worst case, the stats might be for a completely
    8689              :      * empty index, and scaling them will produce pretty bogus numbers.
    8690              :      * Somewhat arbitrarily, set the cutoff for doing scaling at 4X growth; if
    8691              :      * it's grown more than that, fall back to estimating things only from the
    8692              :      * assumed-accurate index size.  But we'll trust nPendingPages in any case
    8693              :      * so long as it's not clearly insane, ie, more than the index size.
    8694              :      */
    8695         1546 :     if (ginStats.nPendingPages < numPages)
    8696         1546 :         numPendingPages = ginStats.nPendingPages;
    8697              :     else
    8698            0 :         numPendingPages = 0;
    8699              : 
    8700         1546 :     if (numPages > 0 && ginStats.nTotalPages <= numPages &&
    8701         1546 :         ginStats.nTotalPages > numPages / 4 &&
    8702         1506 :         ginStats.nEntryPages > 0 && ginStats.nEntries > 0)
    8703         1292 :     {
    8704              :         /*
    8705              :          * OK, the stats seem close enough to sane to be trusted.  But we
    8706              :          * still need to scale them by the ratio numPages / nTotalPages to
    8707              :          * account for growth since the last VACUUM.
    8708              :          */
    8709         1292 :         double      scale = numPages / ginStats.nTotalPages;
    8710              : 
    8711         1292 :         numEntryPages = ceil(ginStats.nEntryPages * scale);
    8712         1292 :         numDataPages = ceil(ginStats.nDataPages * scale);
    8713         1292 :         numEntries = ceil(ginStats.nEntries * scale);
    8714              :         /* ensure we didn't round up too much */
    8715         1292 :         numEntryPages = Min(numEntryPages, numPages - numPendingPages);
    8716         1292 :         numDataPages = Min(numDataPages,
    8717              :                            numPages - numPendingPages - numEntryPages);
    8718              :     }
    8719              :     else
    8720              :     {
    8721              :         /*
    8722              :          * We might get here because it's a hypothetical index, or an index
    8723              :          * created pre-9.1 and never vacuumed since upgrading (in which case
    8724              :          * its stats would read as zeroes), or just because it's grown too
    8725              :          * much since the last VACUUM for us to put our faith in scaling.
    8726              :          *
    8727              :          * Invent some plausible internal statistics based on the index page
    8728              :          * count (and clamp that to at least 10 pages, just in case).  We
    8729              :          * estimate that 90% of the index is entry pages, and the rest is data
    8730              :          * pages.  Estimate 100 entries per entry page; this is rather bogus
    8731              :          * since it'll depend on the size of the keys, but it's more robust
    8732              :          * than trying to predict the number of entries per heap tuple.
    8733              :          */
    8734          254 :         numPages = Max(numPages, 10);
    8735          254 :         numEntryPages = floor((numPages - numPendingPages) * 0.90);
    8736          254 :         numDataPages = numPages - numPendingPages - numEntryPages;
    8737          254 :         numEntries = floor(numEntryPages * 100);
    8738              :     }
    8739              : 
    8740              :     /* In an empty index, numEntries could be zero.  Avoid divide-by-zero */
    8741         1546 :     if (numEntries < 1)
    8742            0 :         numEntries = 1;
    8743              : 
    8744              :     /*
    8745              :      * If the index is partial, AND the index predicate with the index-bound
    8746              :      * quals to produce a more accurate idea of the number of rows covered by
    8747              :      * the bound conditions.
    8748              :      */
    8749         1546 :     selectivityQuals = add_predicate_to_index_quals(index, indexQuals);
    8750              : 
    8751              :     /* Estimate the fraction of main-table tuples that will be visited */
    8752         3092 :     *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
    8753         1546 :                                                index->rel->relid,
    8754              :                                                JOIN_INNER,
    8755              :                                                NULL);
    8756              : 
    8757              :     /* fetch estimated page cost for tablespace containing index */
    8758         1546 :     get_tablespace_page_costs(index->reltablespace,
    8759              :                               &spc_random_page_cost,
    8760              :                               NULL);
    8761              : 
    8762              :     /*
    8763              :      * Generic assumption about index correlation: there isn't any.
    8764              :      */
    8765         1546 :     *indexCorrelation = 0.0;
    8766              : 
    8767              :     /*
    8768              :      * Examine quals to estimate number of search entries & partial matches
    8769              :      */
    8770         1546 :     memset(&counts, 0, sizeof(counts));
    8771         1546 :     counts.arrayScans = 1;
    8772         1546 :     matchPossible = true;
    8773              : 
    8774         3255 :     foreach(lc, path->indexclauses)
    8775              :     {
    8776         1709 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
    8777              :         ListCell   *lc2;
    8778              : 
    8779         3408 :         foreach(lc2, iclause->indexquals)
    8780              :         {
    8781         1709 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
    8782         1709 :             Expr       *clause = rinfo->clause;
    8783              : 
    8784         1709 :             if (IsA(clause, OpExpr))
    8785              :             {
    8786         1704 :                 matchPossible = gincost_opexpr(root,
    8787              :                                                index,
    8788         1704 :                                                iclause->indexcol,
    8789              :                                                (OpExpr *) clause,
    8790              :                                                &counts);
    8791         1704 :                 if (!matchPossible)
    8792           10 :                     break;
    8793              :             }
    8794            5 :             else if (IsA(clause, ScalarArrayOpExpr))
    8795              :             {
    8796            5 :                 matchPossible = gincost_scalararrayopexpr(root,
    8797              :                                                           index,
    8798            5 :                                                           iclause->indexcol,
    8799              :                                                           (ScalarArrayOpExpr *) clause,
    8800              :                                                           numEntries,
    8801              :                                                           &counts);
    8802            5 :                 if (!matchPossible)
    8803            0 :                     break;
    8804              :             }
    8805              :             else
    8806              :             {
    8807              :                 /* shouldn't be anything else for a GIN index */
    8808            0 :                 elog(ERROR, "unsupported GIN indexqual type: %d",
    8809              :                      (int) nodeTag(clause));
    8810              :             }
    8811              :         }
    8812              :     }
    8813              : 
    8814              :     /* Fall out if there were any provably-unsatisfiable quals */
    8815         1546 :     if (!matchPossible)
    8816              :     {
    8817           10 :         *indexStartupCost = 0;
    8818           10 :         *indexTotalCost = 0;
    8819           10 :         *indexSelectivity = 0;
    8820           10 :         return;
    8821              :     }
    8822              : 
    8823              :     /*
    8824              :      * If attribute has a full scan and at the same time doesn't have normal
    8825              :      * scan, then we'll have to scan all non-null entries of that attribute.
    8826              :      * Currently, we don't have per-attribute statistics for GIN.  Thus, we
    8827              :      * must assume the whole GIN index has to be scanned in this case.
    8828              :      */
    8829         1536 :     fullIndexScan = false;
    8830         2989 :     for (i = 0; i < index->nkeycolumns; i++)
    8831              :     {
    8832         1726 :         if (counts.attHasFullScan[i] && !counts.attHasNormalScan[i])
    8833              :         {
    8834          273 :             fullIndexScan = true;
    8835          273 :             break;
    8836              :         }
    8837              :     }
    8838              : 
    8839         1536 :     if (fullIndexScan || indexQuals == NIL)
    8840              :     {
    8841              :         /*
    8842              :          * Full index scan will be required.  We treat this as if every key in
    8843              :          * the index had been listed in the query; is that reasonable?
    8844              :          */
    8845          273 :         counts.partialEntries = 0;
    8846          273 :         counts.exactEntries = numEntries;
    8847          273 :         counts.searchEntries = numEntries;
    8848              :     }
    8849              : 
    8850              :     /* Will we have more than one iteration of a nestloop scan? */
    8851         1536 :     outer_scans = loop_count;
    8852              : 
    8853              :     /*
    8854              :      * Compute cost to begin scan, first of all, pay attention to pending
    8855              :      * list.
    8856              :      */
    8857         1536 :     entryPagesFetched = numPendingPages;
    8858              : 
    8859              :     /*
    8860              :      * Estimate number of entry pages read.  We need to do
    8861              :      * counts.searchEntries searches.  Use a power function as it should be,
    8862              :      * but tuples on leaf pages usually is much greater. Here we include all
    8863              :      * searches in entry tree, including search of first entry in partial
    8864              :      * match algorithm
    8865              :      */
    8866         1536 :     entryPagesFetched += ceil(counts.searchEntries * rint(pow(numEntryPages, 0.15)));
    8867              : 
    8868              :     /*
    8869              :      * Add an estimate of entry pages read by partial match algorithm. It's a
    8870              :      * scan over leaf pages in entry tree.  We haven't any useful stats here,
    8871              :      * so estimate it as proportion.  Because counts.partialEntries is really
    8872              :      * pretty bogus (see code above), it's possible that it is more than
    8873              :      * numEntries; clamp the proportion to ensure sanity.
    8874              :      */
    8875         1536 :     partialScale = counts.partialEntries / numEntries;
    8876         1536 :     partialScale = Min(partialScale, 1.0);
    8877              : 
    8878         1536 :     entryPagesFetched += ceil(numEntryPages * partialScale);
    8879              : 
    8880              :     /*
    8881              :      * Partial match algorithm reads all data pages before doing actual scan,
    8882              :      * so it's a startup cost.  Again, we haven't any useful stats here, so
    8883              :      * estimate it as proportion.
    8884              :      */
    8885         1536 :     dataPagesFetched = ceil(numDataPages * partialScale);
    8886              : 
    8887         1536 :     *indexStartupCost = 0;
    8888         1536 :     *indexTotalCost = 0;
    8889              : 
    8890              :     /*
    8891              :      * Add a CPU-cost component to represent the costs of initial entry btree
    8892              :      * descent.  We don't charge any I/O cost for touching upper btree levels,
    8893              :      * since they tend to stay in cache, but we still have to do about log2(N)
    8894              :      * comparisons to descend a btree of N leaf tuples.  We charge one
    8895              :      * cpu_operator_cost per comparison.
    8896              :      *
    8897              :      * If there are ScalarArrayOpExprs, charge this once per SA scan.  The
    8898              :      * ones after the first one are not startup cost so far as the overall
    8899              :      * plan is concerned, so add them only to "total" cost.
    8900              :      */
    8901         1536 :     if (numEntries > 1)          /* avoid computing log(0) */
    8902              :     {
    8903         1536 :         descentCost = ceil(log(numEntries) / log(2.0)) * cpu_operator_cost;
    8904         1536 :         *indexStartupCost += descentCost * counts.searchEntries;
    8905         1536 :         *indexTotalCost += counts.arrayScans * descentCost * counts.searchEntries;
    8906              :     }
    8907              : 
    8908              :     /*
    8909              :      * Add a cpu cost per entry-page fetched. This is not amortized over a
    8910              :      * loop.
    8911              :      */
    8912         1536 :     *indexStartupCost += entryPagesFetched * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
    8913         1536 :     *indexTotalCost += entryPagesFetched * counts.arrayScans * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
    8914              : 
    8915              :     /*
    8916              :      * Add a cpu cost per data-page fetched. This is also not amortized over a
    8917              :      * loop. Since those are the data pages from the partial match algorithm,
    8918              :      * charge them as startup cost.
    8919              :      */
    8920         1536 :     *indexStartupCost += DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost * dataPagesFetched;
    8921              : 
    8922              :     /*
    8923              :      * Since we add the startup cost to the total cost later on, remove the
    8924              :      * initial arrayscan from the total.
    8925              :      */
    8926         1536 :     *indexTotalCost += dataPagesFetched * (counts.arrayScans - 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
    8927              : 
    8928              :     /*
    8929              :      * Calculate cache effects if more than one scan due to nestloops or array
    8930              :      * quals.  The result is pro-rated per nestloop scan, but the array qual
    8931              :      * factor shouldn't be pro-rated (compare genericcostestimate).
    8932              :      */
    8933         1536 :     if (outer_scans > 1 || counts.arrayScans > 1)
    8934              :     {
    8935            5 :         entryPagesFetched *= outer_scans * counts.arrayScans;
    8936            5 :         entryPagesFetched = index_pages_fetched(entryPagesFetched,
    8937              :                                                 (BlockNumber) numEntryPages,
    8938              :                                                 numEntryPages, root);
    8939            5 :         entryPagesFetched /= outer_scans;
    8940            5 :         dataPagesFetched *= outer_scans * counts.arrayScans;
    8941            5 :         dataPagesFetched = index_pages_fetched(dataPagesFetched,
    8942              :                                                (BlockNumber) numDataPages,
    8943              :                                                numDataPages, root);
    8944            5 :         dataPagesFetched /= outer_scans;
    8945              :     }
    8946              : 
    8947              :     /*
    8948              :      * Here we use random page cost because logically-close pages could be far
    8949              :      * apart on disk.
    8950              :      */
    8951         1536 :     *indexStartupCost += (entryPagesFetched + dataPagesFetched) * spc_random_page_cost;
    8952              : 
    8953              :     /*
    8954              :      * Now compute the number of data pages fetched during the scan.
    8955              :      *
    8956              :      * We assume every entry to have the same number of items, and that there
    8957              :      * is no overlap between them. (XXX: tsvector and array opclasses collect
    8958              :      * statistics on the frequency of individual keys; it would be nice to use
    8959              :      * those here.)
    8960              :      */
    8961         1536 :     dataPagesFetched = ceil(numDataPages * counts.exactEntries / numEntries);
    8962              : 
    8963              :     /*
    8964              :      * If there is a lot of overlap among the entries, in particular if one of
    8965              :      * the entries is very frequent, the above calculation can grossly
    8966              :      * under-estimate.  As a simple cross-check, calculate a lower bound based
    8967              :      * on the overall selectivity of the quals.  At a minimum, we must read
    8968              :      * one item pointer for each matching entry.
    8969              :      *
    8970              :      * The width of each item pointer varies, based on the level of
    8971              :      * compression.  We don't have statistics on that, but an average of
    8972              :      * around 3 bytes per item is fairly typical.
    8973              :      */
    8974         1536 :     dataPagesFetchedBySel = ceil(*indexSelectivity *
    8975         1536 :                                  (numTuples / (BLCKSZ / 3)));
    8976         1536 :     if (dataPagesFetchedBySel > dataPagesFetched)
    8977         1243 :         dataPagesFetched = dataPagesFetchedBySel;
    8978              : 
    8979              :     /* Add one page cpu-cost to the startup cost */
    8980         1536 :     *indexStartupCost += DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost * counts.searchEntries;
    8981              : 
    8982              :     /*
    8983              :      * Add once again a CPU-cost for those data pages, before amortizing for
    8984              :      * cache.
    8985              :      */
    8986         1536 :     *indexTotalCost += dataPagesFetched * counts.arrayScans * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
    8987              : 
    8988              :     /* Account for cache effects, the same as above */
    8989         1536 :     if (outer_scans > 1 || counts.arrayScans > 1)
    8990              :     {
    8991            5 :         dataPagesFetched *= outer_scans * counts.arrayScans;
    8992            5 :         dataPagesFetched = index_pages_fetched(dataPagesFetched,
    8993              :                                                (BlockNumber) numDataPages,
    8994              :                                                numDataPages, root);
    8995            5 :         dataPagesFetched /= outer_scans;
    8996              :     }
    8997              : 
    8998              :     /* And apply random_page_cost as the cost per page */
    8999         1536 :     *indexTotalCost += *indexStartupCost +
    9000         1536 :         dataPagesFetched * spc_random_page_cost;
    9001              : 
    9002              :     /*
    9003              :      * Add on index qual eval costs, much as in genericcostestimate. We charge
    9004              :      * cpu but we can disregard indexorderbys, since GIN doesn't support
    9005              :      * those.
    9006              :      */
    9007         1536 :     qual_arg_cost = index_other_operands_eval_cost(root, indexQuals);
    9008         1536 :     qual_op_cost = cpu_operator_cost * list_length(indexQuals);
    9009              : 
    9010         1536 :     *indexStartupCost += qual_arg_cost;
    9011         1536 :     *indexTotalCost += qual_arg_cost;
    9012              : 
    9013              :     /*
    9014              :      * Add a cpu cost per search entry, corresponding to the actual visited
    9015              :      * entries.
    9016              :      */
    9017         1536 :     *indexTotalCost += (counts.searchEntries * counts.arrayScans) * (qual_op_cost);
    9018              :     /* Now add a cpu cost per tuple in the posting lists / trees */
    9019         1536 :     *indexTotalCost += (numTuples * *indexSelectivity) * (cpu_index_tuple_cost);
    9020         1536 :     *indexPages = dataPagesFetched;
    9021              : }
    9022              : 
    9023              : /*
    9024              :  * BRIN has search behavior completely different from other index types
    9025              :  */
    9026              : void
    9027         8944 : brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    9028              :                  Cost *indexStartupCost, Cost *indexTotalCost,
    9029              :                  Selectivity *indexSelectivity, double *indexCorrelation,
    9030              :                  double *indexPages)
    9031              : {
    9032         8944 :     IndexOptInfo *index = path->indexinfo;
    9033         8944 :     List       *indexQuals = get_quals_from_indexclauses(path->indexclauses);
    9034         8944 :     double      numPages = index->pages;
    9035         8944 :     RelOptInfo *baserel = index->rel;
    9036         8944 :     RangeTblEntry *rte = planner_rt_fetch(baserel->relid, root);
    9037              :     Cost        spc_seq_page_cost;
    9038              :     Cost        spc_random_page_cost;
    9039              :     double      qual_arg_cost;
    9040              :     double      qualSelectivity;
    9041              :     BrinStatsData statsData;
    9042              :     double      indexRanges;
    9043              :     double      minimalRanges;
    9044              :     double      estimatedRanges;
    9045              :     double      selec;
    9046              :     Relation    indexRel;
    9047              :     ListCell   *l;
    9048              :     VariableStatData vardata;
    9049              : 
    9050              :     Assert(rte->rtekind == RTE_RELATION);
    9051              : 
    9052              :     /* fetch estimated page cost for the tablespace containing the index */
    9053         8944 :     get_tablespace_page_costs(index->reltablespace,
    9054              :                               &spc_random_page_cost,
    9055              :                               &spc_seq_page_cost);
    9056              : 
    9057              :     /*
    9058              :      * Obtain some data from the index itself, if possible.  Otherwise invent
    9059              :      * some plausible internal statistics based on the relation page count.
    9060              :      */
    9061         8944 :     if (!index->hypothetical)
    9062              :     {
    9063              :         /*
    9064              :          * A lock should have already been obtained on the index in plancat.c.
    9065              :          */
    9066         8944 :         indexRel = index_open(index->indexoid, NoLock);
    9067         8944 :         brinGetStats(indexRel, &statsData);
    9068         8944 :         index_close(indexRel, NoLock);
    9069              : 
    9070              :         /* work out the actual number of ranges in the index */
    9071         8944 :         indexRanges = Max(ceil((double) baserel->pages /
    9072              :                                statsData.pagesPerRange), 1.0);
    9073              :     }
    9074              :     else
    9075              :     {
    9076              :         /*
    9077              :          * Assume default number of pages per range, and estimate the number
    9078              :          * of ranges based on that.
    9079              :          */
    9080            0 :         indexRanges = Max(ceil((double) baserel->pages /
    9081              :                                BRIN_DEFAULT_PAGES_PER_RANGE), 1.0);
    9082              : 
    9083            0 :         statsData.pagesPerRange = BRIN_DEFAULT_PAGES_PER_RANGE;
    9084            0 :         statsData.revmapNumPages = (indexRanges / REVMAP_PAGE_MAXITEMS) + 1;
    9085              :     }
    9086              : 
    9087              :     /*
    9088              :      * Compute index correlation
    9089              :      *
    9090              :      * Because we can use all index quals equally when scanning, we can use
    9091              :      * the largest correlation (in absolute value) among columns used by the
    9092              :      * query.  Start at zero, the worst possible case.  If we cannot find any
    9093              :      * correlation statistics, we will keep it as 0.
    9094              :      */
    9095         8944 :     *indexCorrelation = 0;
    9096              : 
    9097        17889 :     foreach(l, path->indexclauses)
    9098              :     {
    9099         8945 :         IndexClause *iclause = lfirst_node(IndexClause, l);
    9100         8945 :         AttrNumber  attnum = index->indexkeys[iclause->indexcol];
    9101              : 
    9102              :         /* attempt to lookup stats in relation for this index column */
    9103         8945 :         if (attnum != 0)
    9104              :         {
    9105              :             /* Simple variable -- look to stats for the underlying table */
    9106         8945 :             if (get_relation_stats_hook &&
    9107            0 :                 (*get_relation_stats_hook) (root, rte, attnum, &vardata))
    9108              :             {
    9109              :                 /*
    9110              :                  * The hook took control of acquiring a stats tuple.  If it
    9111              :                  * did supply a tuple, it'd better have supplied a freefunc.
    9112              :                  */
    9113            0 :                 if (HeapTupleIsValid(vardata.statsTuple) && !vardata.freefunc)
    9114            0 :                     elog(ERROR,
    9115              :                          "no function provided to release variable stats with");
    9116              :             }
    9117              :             else
    9118              :             {
    9119         8945 :                 vardata.statsTuple =
    9120         8945 :                     SearchSysCache3(STATRELATTINH,
    9121              :                                     ObjectIdGetDatum(rte->relid),
    9122              :                                     Int16GetDatum(attnum),
    9123              :                                     BoolGetDatum(false));
    9124         8945 :                 vardata.freefunc = ReleaseSysCache;
    9125              :             }
    9126              :         }
    9127              :         else
    9128              :         {
    9129              :             /*
    9130              :              * Looks like we've found an expression column in the index. Let's
    9131              :              * see if there's any stats for it.
    9132              :              */
    9133              : 
    9134              :             /* get the attnum from the 0-based index. */
    9135            0 :             attnum = iclause->indexcol + 1;
    9136              : 
    9137            0 :             if (get_index_stats_hook &&
    9138            0 :                 (*get_index_stats_hook) (root, index->indexoid, attnum, &vardata))
    9139              :             {
    9140              :                 /*
    9141              :                  * The hook took control of acquiring a stats tuple.  If it
    9142              :                  * did supply a tuple, it'd better have supplied a freefunc.
    9143              :                  */
    9144            0 :                 if (HeapTupleIsValid(vardata.statsTuple) &&
    9145            0 :                     !vardata.freefunc)
    9146            0 :                     elog(ERROR, "no function provided to release variable stats with");
    9147              :             }
    9148              :             else
    9149              :             {
    9150            0 :                 vardata.statsTuple = SearchSysCache3(STATRELATTINH,
    9151              :                                                      ObjectIdGetDatum(index->indexoid),
    9152              :                                                      Int16GetDatum(attnum),
    9153              :                                                      BoolGetDatum(false));
    9154            0 :                 vardata.freefunc = ReleaseSysCache;
    9155              :             }
    9156              :         }
    9157              : 
    9158         8945 :         if (HeapTupleIsValid(vardata.statsTuple))
    9159              :         {
    9160              :             AttStatsSlot sslot;
    9161              : 
    9162           33 :             if (get_attstatsslot(&sslot, vardata.statsTuple,
    9163              :                                  STATISTIC_KIND_CORRELATION, InvalidOid,
    9164              :                                  ATTSTATSSLOT_NUMBERS))
    9165              :             {
    9166           33 :                 double      varCorrelation = 0.0;
    9167              : 
    9168           33 :                 if (sslot.nnumbers > 0)
    9169           33 :                     varCorrelation = fabs(sslot.numbers[0]);
    9170              : 
    9171           33 :                 if (varCorrelation > *indexCorrelation)
    9172           33 :                     *indexCorrelation = varCorrelation;
    9173              : 
    9174           33 :                 free_attstatsslot(&sslot);
    9175              :             }
    9176              :         }
    9177              : 
    9178         8945 :         ReleaseVariableStats(vardata);
    9179              :     }
    9180              : 
    9181         8944 :     qualSelectivity = clauselist_selectivity(root, indexQuals,
    9182         8944 :                                              baserel->relid,
    9183              :                                              JOIN_INNER, NULL);
    9184              : 
    9185              :     /*
    9186              :      * Now calculate the minimum possible ranges we could match with if all of
    9187              :      * the rows were in the perfect order in the table's heap.
    9188              :      */
    9189         8944 :     minimalRanges = ceil(indexRanges * qualSelectivity);
    9190              : 
    9191              :     /*
    9192              :      * Now estimate the number of ranges that we'll touch by using the
    9193              :      * indexCorrelation from the stats. Careful not to divide by zero (note
    9194              :      * we're using the absolute value of the correlation).
    9195              :      */
    9196         8944 :     if (*indexCorrelation < 1.0e-10)
    9197         8911 :         estimatedRanges = indexRanges;
    9198              :     else
    9199           33 :         estimatedRanges = Min(minimalRanges / *indexCorrelation, indexRanges);
    9200              : 
    9201              :     /* we expect to visit this portion of the table */
    9202         8944 :     selec = estimatedRanges / indexRanges;
    9203              : 
    9204         8944 :     CLAMP_PROBABILITY(selec);
    9205              : 
    9206         8944 :     *indexSelectivity = selec;
    9207              : 
    9208              :     /*
    9209              :      * Compute the index qual costs, much as in genericcostestimate, to add to
    9210              :      * the index costs.  We can disregard indexorderbys, since BRIN doesn't
    9211              :      * support those.
    9212              :      */
    9213         8944 :     qual_arg_cost = index_other_operands_eval_cost(root, indexQuals);
    9214              : 
    9215              :     /*
    9216              :      * Compute the startup cost as the cost to read the whole revmap
    9217              :      * sequentially, including the cost to execute the index quals.
    9218              :      */
    9219         8944 :     *indexStartupCost =
    9220         8944 :         spc_seq_page_cost * statsData.revmapNumPages * loop_count;
    9221         8944 :     *indexStartupCost += qual_arg_cost;
    9222              : 
    9223              :     /*
    9224              :      * To read a BRIN index there might be a bit of back and forth over
    9225              :      * regular pages, as revmap might point to them out of sequential order;
    9226              :      * calculate the total cost as reading the whole index in random order.
    9227              :      */
    9228         8944 :     *indexTotalCost = *indexStartupCost +
    9229         8944 :         spc_random_page_cost * (numPages - statsData.revmapNumPages) * loop_count;
    9230              : 
    9231              :     /*
    9232              :      * Charge a small amount per range tuple which we expect to match to. This
    9233              :      * is meant to reflect the costs of manipulating the bitmap. The BRIN scan
    9234              :      * will set a bit for each page in the range when we find a matching
    9235              :      * range, so we must multiply the charge by the number of pages in the
    9236              :      * range.
    9237              :      */
    9238         8944 :     *indexTotalCost += 0.1 * cpu_operator_cost * estimatedRanges *
    9239         8944 :         statsData.pagesPerRange;
    9240              : 
    9241         8944 :     *indexPages = index->pages;
    9242         8944 : }
        

Generated by: LCOV version 2.0-1