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

Generated by: LCOV version 2.0-1