LCOV - code coverage report
Current view: top level - src/backend/utils/adt - selfuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL Lines: 1917 2350 81.6 %
Date: 2017-09-29 13:40:31 Functions: 73 91 80.2 %
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-2017, 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 database's default
      91             :  * collation.  Thus, in most cases where we are looking at statistics, we
      92             :  * should ignore the actual operator collation and use DEFAULT_COLLATION_OID.
      93             :  * We expect that the error induced by doing this is usually not large enough
      94             :  * to justify complicating matters.
      95             :  *----------
      96             :  */
      97             : 
      98             : #include "postgres.h"
      99             : 
     100             : #include <ctype.h>
     101             : #include <float.h>
     102             : #include <math.h>
     103             : 
     104             : #include "access/brin.h"
     105             : #include "access/gin.h"
     106             : #include "access/htup_details.h"
     107             : #include "access/sysattr.h"
     108             : #include "catalog/index.h"
     109             : #include "catalog/pg_am.h"
     110             : #include "catalog/pg_collation.h"
     111             : #include "catalog/pg_operator.h"
     112             : #include "catalog/pg_opfamily.h"
     113             : #include "catalog/pg_statistic.h"
     114             : #include "catalog/pg_statistic_ext.h"
     115             : #include "catalog/pg_type.h"
     116             : #include "executor/executor.h"
     117             : #include "mb/pg_wchar.h"
     118             : #include "miscadmin.h"
     119             : #include "nodes/makefuncs.h"
     120             : #include "nodes/nodeFuncs.h"
     121             : #include "optimizer/clauses.h"
     122             : #include "optimizer/cost.h"
     123             : #include "optimizer/pathnode.h"
     124             : #include "optimizer/paths.h"
     125             : #include "optimizer/plancat.h"
     126             : #include "optimizer/predtest.h"
     127             : #include "optimizer/restrictinfo.h"
     128             : #include "optimizer/var.h"
     129             : #include "parser/parse_clause.h"
     130             : #include "parser/parse_coerce.h"
     131             : #include "parser/parsetree.h"
     132             : #include "statistics/statistics.h"
     133             : #include "utils/acl.h"
     134             : #include "utils/builtins.h"
     135             : #include "utils/bytea.h"
     136             : #include "utils/date.h"
     137             : #include "utils/datum.h"
     138             : #include "utils/fmgroids.h"
     139             : #include "utils/index_selfuncs.h"
     140             : #include "utils/lsyscache.h"
     141             : #include "utils/nabstime.h"
     142             : #include "utils/pg_locale.h"
     143             : #include "utils/rel.h"
     144             : #include "utils/selfuncs.h"
     145             : #include "utils/spccache.h"
     146             : #include "utils/syscache.h"
     147             : #include "utils/timestamp.h"
     148             : #include "utils/tqual.h"
     149             : #include "utils/typcache.h"
     150             : #include "utils/varlena.h"
     151             : 
     152             : 
     153             : /* Hooks for plugins to get control when we ask for stats */
     154             : get_relation_stats_hook_type get_relation_stats_hook = NULL;
     155             : get_index_stats_hook_type get_index_stats_hook = NULL;
     156             : 
     157             : static double eqsel_internal(PG_FUNCTION_ARGS, bool negate);
     158             : static double var_eq_const(VariableStatData *vardata, Oid operator,
     159             :              Datum constval, bool constisnull,
     160             :              bool varonleft, bool negate);
     161             : static double var_eq_non_const(VariableStatData *vardata, Oid operator,
     162             :                  Node *other,
     163             :                  bool varonleft, bool negate);
     164             : static double ineq_histogram_selectivity(PlannerInfo *root,
     165             :                            VariableStatData *vardata,
     166             :                            FmgrInfo *opproc, bool isgt,
     167             :                            Datum constval, Oid consttype);
     168             : static double eqjoinsel_inner(Oid operator,
     169             :                 VariableStatData *vardata1, VariableStatData *vardata2);
     170             : static double eqjoinsel_semi(Oid operator,
     171             :                VariableStatData *vardata1, VariableStatData *vardata2,
     172             :                RelOptInfo *inner_rel);
     173             : static bool estimate_multivariate_ndistinct(PlannerInfo *root,
     174             :                                 RelOptInfo *rel, List **varinfos, double *ndistinct);
     175             : static bool convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
     176             :                   Datum lobound, Datum hibound, Oid boundstypid,
     177             :                   double *scaledlobound, double *scaledhibound);
     178             : static double convert_numeric_to_scalar(Datum value, Oid typid);
     179             : static void convert_string_to_scalar(char *value,
     180             :                          double *scaledvalue,
     181             :                          char *lobound,
     182             :                          double *scaledlobound,
     183             :                          char *hibound,
     184             :                          double *scaledhibound);
     185             : static void convert_bytea_to_scalar(Datum value,
     186             :                         double *scaledvalue,
     187             :                         Datum lobound,
     188             :                         double *scaledlobound,
     189             :                         Datum hibound,
     190             :                         double *scaledhibound);
     191             : static double convert_one_string_to_scalar(char *value,
     192             :                              int rangelo, int rangehi);
     193             : static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
     194             :                             int rangelo, int rangehi);
     195             : static char *convert_string_datum(Datum value, Oid typid);
     196             : static double convert_timevalue_to_scalar(Datum value, Oid typid);
     197             : static void examine_simple_variable(PlannerInfo *root, Var *var,
     198             :                         VariableStatData *vardata);
     199             : static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata,
     200             :                    Oid sortop, Datum *min, Datum *max);
     201             : static bool get_actual_variable_range(PlannerInfo *root,
     202             :                           VariableStatData *vardata,
     203             :                           Oid sortop,
     204             :                           Datum *min, Datum *max);
     205             : static RelOptInfo *find_join_input_rel(PlannerInfo *root, Relids relids);
     206             : static Selectivity prefix_selectivity(PlannerInfo *root,
     207             :                    VariableStatData *vardata,
     208             :                    Oid vartype, Oid opfamily, Const *prefixcon);
     209             : static Selectivity like_selectivity(const char *patt, int pattlen,
     210             :                  bool case_insensitive);
     211             : static Selectivity regex_selectivity(const char *patt, int pattlen,
     212             :                   bool case_insensitive,
     213             :                   int fixed_prefix_len);
     214             : static Datum string_to_datum(const char *str, Oid datatype);
     215             : static Const *string_to_const(const char *str, Oid datatype);
     216             : static Const *string_to_bytea_const(const char *str, size_t str_len);
     217             : static List *add_predicate_to_quals(IndexOptInfo *index, List *indexQuals);
     218             : 
     219             : 
     220             : /*
     221             :  *      eqsel           - Selectivity of "=" for any data types.
     222             :  *
     223             :  * Note: this routine is also used to estimate selectivity for some
     224             :  * operators that are not "=" but have comparable selectivity behavior,
     225             :  * such as "~=" (geometric approximate-match).  Even for "=", we must
     226             :  * keep in mind that the left and right datatypes may differ.
     227             :  */
     228             : Datum
     229       17346 : eqsel(PG_FUNCTION_ARGS)
     230             : {
     231       17346 :     PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, false));
     232             : }
     233             : 
     234             : /*
     235             :  * Common code for eqsel() and neqsel()
     236             :  */
     237             : static double
     238       18633 : eqsel_internal(PG_FUNCTION_ARGS, bool negate)
     239             : {
     240       18633 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
     241       18633 :     Oid         operator = PG_GETARG_OID(1);
     242       18633 :     List       *args = (List *) PG_GETARG_POINTER(2);
     243       18633 :     int         varRelid = PG_GETARG_INT32(3);
     244             :     VariableStatData vardata;
     245             :     Node       *other;
     246             :     bool        varonleft;
     247             :     double      selec;
     248             : 
     249             :     /*
     250             :      * When asked about <>, we do the estimation using the corresponding =
     251             :      * operator, then convert to <> via "1.0 - eq_selectivity - nullfrac".
     252             :      */
     253       18633 :     if (negate)
     254             :     {
     255        1287 :         operator = get_negator(operator);
     256        1287 :         if (!OidIsValid(operator))
     257             :         {
     258             :             /* Use default selectivity (should we raise an error instead?) */
     259           0 :             return 1.0 - DEFAULT_EQ_SEL;
     260             :         }
     261             :     }
     262             : 
     263             :     /*
     264             :      * If expression is not variable = something or something = variable, then
     265             :      * punt and return a default estimate.
     266             :      */
     267       18633 :     if (!get_restriction_variable(root, args, varRelid,
     268             :                                   &vardata, &other, &varonleft))
     269         141 :         return negate ? (1.0 - DEFAULT_EQ_SEL) : DEFAULT_EQ_SEL;
     270             : 
     271             :     /*
     272             :      * We can do a lot better if the something is a constant.  (Note: the
     273             :      * Const might result from estimation rather than being a simple constant
     274             :      * in the query.)
     275             :      */
     276       18492 :     if (IsA(other, Const))
     277       23808 :         selec = var_eq_const(&vardata, operator,
     278        7936 :                              ((Const *) other)->constvalue,
     279        7936 :                              ((Const *) other)->constisnull,
     280             :                              varonleft, negate);
     281             :     else
     282       10556 :         selec = var_eq_non_const(&vardata, operator, other,
     283             :                                  varonleft, negate);
     284             : 
     285       18492 :     ReleaseVariableStats(vardata);
     286             : 
     287       18492 :     return selec;
     288             : }
     289             : 
     290             : /*
     291             :  * var_eq_const --- eqsel for var = const case
     292             :  *
     293             :  * This is split out so that some other estimation functions can use it.
     294             :  */
     295             : static double
     296        9086 : var_eq_const(VariableStatData *vardata, Oid operator,
     297             :              Datum constval, bool constisnull,
     298             :              bool varonleft, bool negate)
     299             : {
     300             :     double      selec;
     301        9086 :     double      nullfrac = 0.0;
     302             :     bool        isdefault;
     303             :     Oid         opfuncoid;
     304             : 
     305             :     /*
     306             :      * If the constant is NULL, assume operator is strict and return zero, ie,
     307             :      * operator will never return TRUE.  (It's zero even for a negator op.)
     308             :      */
     309        9086 :     if (constisnull)
     310           3 :         return 0.0;
     311             : 
     312             :     /*
     313             :      * Grab the nullfrac for use below.  Note we allow use of nullfrac
     314             :      * regardless of security check.
     315             :      */
     316        9083 :     if (HeapTupleIsValid(vardata->statsTuple))
     317             :     {
     318             :         Form_pg_statistic stats;
     319             : 
     320        5034 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
     321        5034 :         nullfrac = stats->stanullfrac;
     322             :     }
     323             : 
     324             :     /*
     325             :      * If we matched the var to a unique index or DISTINCT clause, assume
     326             :      * there is exactly one match regardless of anything else.  (This is
     327             :      * slightly bogus, since the index or clause's equality operator might be
     328             :      * different from ours, but it's much more likely to be right than
     329             :      * ignoring the information.)
     330             :      */
     331        9083 :     if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
     332             :     {
     333        1464 :         selec = 1.0 / vardata->rel->tuples;
     334             :     }
     335       12331 :     else if (HeapTupleIsValid(vardata->statsTuple) &&
     336        4712 :              statistic_proc_security_check(vardata,
     337             :                                            (opfuncoid = get_opcode(operator))))
     338        4712 :     {
     339             :         AttStatsSlot sslot;
     340        4712 :         bool        match = false;
     341             :         int         i;
     342             : 
     343             :         /*
     344             :          * Is the constant "=" to any of the column's most common values?
     345             :          * (Although the given operator may not really be "=", we will assume
     346             :          * that seeing whether it returns TRUE is an appropriate test.  If you
     347             :          * don't like this, maybe you shouldn't be using eqsel for your
     348             :          * operator...)
     349             :          */
     350        4712 :         if (get_attstatsslot(&sslot, vardata->statsTuple,
     351             :                              STATISTIC_KIND_MCV, InvalidOid,
     352             :                              ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
     353             :         {
     354             :             FmgrInfo    eqproc;
     355             : 
     356        3519 :             fmgr_info(opfuncoid, &eqproc);
     357             : 
     358       44959 :             for (i = 0; i < sslot.nvalues; i++)
     359             :             {
     360             :                 /* be careful to apply operator right way 'round */
     361       42945 :                 if (varonleft)
     362       42945 :                     match = DatumGetBool(FunctionCall2Coll(&eqproc,
     363             :                                                            DEFAULT_COLLATION_OID,
     364             :                                                            sslot.values[i],
     365             :                                                            constval));
     366             :                 else
     367           0 :                     match = DatumGetBool(FunctionCall2Coll(&eqproc,
     368             :                                                            DEFAULT_COLLATION_OID,
     369             :                                                            constval,
     370             :                                                            sslot.values[i]));
     371       42945 :                 if (match)
     372        1505 :                     break;
     373             :             }
     374             :         }
     375             :         else
     376             :         {
     377             :             /* no most-common-value info available */
     378        1193 :             i = 0;              /* keep compiler quiet */
     379             :         }
     380             : 
     381        4712 :         if (match)
     382             :         {
     383             :             /*
     384             :              * Constant is "=" to this common value.  We know selectivity
     385             :              * exactly (or as exactly as ANALYZE could calculate it, anyway).
     386             :              */
     387        1505 :             selec = sslot.numbers[i];
     388             :         }
     389             :         else
     390             :         {
     391             :             /*
     392             :              * Comparison is against a constant that is neither NULL nor any
     393             :              * of the common values.  Its selectivity cannot be more than
     394             :              * this:
     395             :              */
     396        3207 :             double      sumcommon = 0.0;
     397             :             double      otherdistinct;
     398             : 
     399       42186 :             for (i = 0; i < sslot.nnumbers; i++)
     400       38979 :                 sumcommon += sslot.numbers[i];
     401        3207 :             selec = 1.0 - sumcommon - nullfrac;
     402        3207 :             CLAMP_PROBABILITY(selec);
     403             : 
     404             :             /*
     405             :              * and in fact it's probably a good deal less. We approximate that
     406             :              * all the not-common values share this remaining fraction
     407             :              * equally, so we divide by the number of other distinct values.
     408             :              */
     409        6414 :             otherdistinct = get_variable_numdistinct(vardata, &isdefault) -
     410        3207 :                 sslot.nnumbers;
     411        3207 :             if (otherdistinct > 1)
     412        2422 :                 selec /= otherdistinct;
     413             : 
     414             :             /*
     415             :              * Another cross-check: selectivity shouldn't be estimated as more
     416             :              * than the least common "most common value".
     417             :              */
     418        3207 :             if (sslot.nnumbers > 0 && selec > sslot.numbers[sslot.nnumbers - 1])
     419           0 :                 selec = sslot.numbers[sslot.nnumbers - 1];
     420             :         }
     421             : 
     422        4712 :         free_attstatsslot(&sslot);
     423             :     }
     424             :     else
     425             :     {
     426             :         /*
     427             :          * No ANALYZE stats available, so make a guess using estimated number
     428             :          * of distinct values and assuming they are equally common. (The guess
     429             :          * is unlikely to be very good, but we do know a few special cases.)
     430             :          */
     431        2907 :         selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
     432             :     }
     433             : 
     434             :     /* now adjust if we wanted <> rather than = */
     435        9083 :     if (negate)
     436         858 :         selec = 1.0 - selec - nullfrac;
     437             : 
     438             :     /* result should be in range, but make sure... */
     439        9083 :     CLAMP_PROBABILITY(selec);
     440             : 
     441        9083 :     return selec;
     442             : }
     443             : 
     444             : /*
     445             :  * var_eq_non_const --- eqsel for var = something-other-than-const case
     446             :  */
     447             : static double
     448       10556 : var_eq_non_const(VariableStatData *vardata, Oid operator,
     449             :                  Node *other,
     450             :                  bool varonleft, bool negate)
     451             : {
     452             :     double      selec;
     453       10556 :     double      nullfrac = 0.0;
     454             :     bool        isdefault;
     455             : 
     456             :     /*
     457             :      * Grab the nullfrac for use below.
     458             :      */
     459       10556 :     if (HeapTupleIsValid(vardata->statsTuple))
     460             :     {
     461             :         Form_pg_statistic stats;
     462             : 
     463        4296 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
     464        4296 :         nullfrac = stats->stanullfrac;
     465             :     }
     466             : 
     467             :     /*
     468             :      * If we matched the var to a unique index or DISTINCT clause, assume
     469             :      * there is exactly one match regardless of anything else.  (This is
     470             :      * slightly bogus, since the index or clause's equality operator might be
     471             :      * different from ours, but it's much more likely to be right than
     472             :      * ignoring the information.)
     473             :      */
     474       10556 :     if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
     475             :     {
     476        4996 :         selec = 1.0 / vardata->rel->tuples;
     477             :     }
     478        5560 :     else if (HeapTupleIsValid(vardata->statsTuple))
     479             :     {
     480             :         double      ndistinct;
     481             :         AttStatsSlot sslot;
     482             : 
     483             :         /*
     484             :          * Search is for a value that we do not know a priori, but we will
     485             :          * assume it is not NULL.  Estimate the selectivity as non-null
     486             :          * fraction divided by number of distinct values, so that we get a
     487             :          * result averaged over all possible values whether common or
     488             :          * uncommon.  (Essentially, we are assuming that the not-yet-known
     489             :          * comparison value is equally likely to be any of the possible
     490             :          * values, regardless of their frequency in the table.  Is that a good
     491             :          * idea?)
     492             :          */
     493        4108 :         selec = 1.0 - nullfrac;
     494        4108 :         ndistinct = get_variable_numdistinct(vardata, &isdefault);
     495        4108 :         if (ndistinct > 1)
     496        3806 :             selec /= ndistinct;
     497             : 
     498             :         /*
     499             :          * Cross-check: selectivity should never be estimated as more than the
     500             :          * most common value's.
     501             :          */
     502        4108 :         if (get_attstatsslot(&sslot, vardata->statsTuple,
     503             :                              STATISTIC_KIND_MCV, InvalidOid,
     504             :                              ATTSTATSSLOT_NUMBERS))
     505             :         {
     506        3365 :             if (sslot.nnumbers > 0 && selec > sslot.numbers[0])
     507          30 :                 selec = sslot.numbers[0];
     508        3365 :             free_attstatsslot(&sslot);
     509             :         }
     510             :     }
     511             :     else
     512             :     {
     513             :         /*
     514             :          * No ANALYZE stats available, so make a guess using estimated number
     515             :          * of distinct values and assuming they are equally common. (The guess
     516             :          * is unlikely to be very good, but we do know a few special cases.)
     517             :          */
     518        1452 :         selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
     519             :     }
     520             : 
     521             :     /* now adjust if we wanted <> rather than = */
     522       10556 :     if (negate)
     523         336 :         selec = 1.0 - selec - nullfrac;
     524             : 
     525             :     /* result should be in range, but make sure... */
     526       10556 :     CLAMP_PROBABILITY(selec);
     527             : 
     528       10556 :     return selec;
     529             : }
     530             : 
     531             : /*
     532             :  *      neqsel          - Selectivity of "!=" for any data types.
     533             :  *
     534             :  * This routine is also used for some operators that are not "!="
     535             :  * but have comparable selectivity behavior.  See above comments
     536             :  * for eqsel().
     537             :  */
     538             : Datum
     539        1287 : neqsel(PG_FUNCTION_ARGS)
     540             : {
     541        1287 :     PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, true));
     542             : }
     543             : 
     544             : /*
     545             :  *  scalarineqsel       - Selectivity of "<", "<=", ">", ">=" for scalars.
     546             :  *
     547             :  * This is the guts of both scalarltsel and scalargtsel.  The caller has
     548             :  * commuted the clause, if necessary, so that we can treat the variable as
     549             :  * being on the left.  The caller must also make sure that the other side
     550             :  * of the clause is a non-null Const, and dissect same into a value and
     551             :  * datatype.
     552             :  *
     553             :  * This routine works for any datatype (or pair of datatypes) known to
     554             :  * convert_to_scalar().  If it is applied to some other datatype,
     555             :  * it will return a default estimate.
     556             :  */
     557             : static double
     558        2698 : scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
     559             :               VariableStatData *vardata, Datum constval, Oid consttype)
     560             : {
     561             :     Form_pg_statistic stats;
     562             :     FmgrInfo    opproc;
     563             :     double      mcv_selec,
     564             :                 hist_selec,
     565             :                 sumcommon;
     566             :     double      selec;
     567             : 
     568        2698 :     if (!HeapTupleIsValid(vardata->statsTuple))
     569             :     {
     570             :         /* no stats available, so default result */
     571        1221 :         return DEFAULT_INEQ_SEL;
     572             :     }
     573        1477 :     stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
     574             : 
     575        1477 :     fmgr_info(get_opcode(operator), &opproc);
     576             : 
     577             :     /*
     578             :      * If we have most-common-values info, add up the fractions of the MCV
     579             :      * entries that satisfy MCV OP CONST.  These fractions contribute directly
     580             :      * to the result selectivity.  Also add up the total fraction represented
     581             :      * by MCV entries.
     582             :      */
     583        1477 :     mcv_selec = mcv_selectivity(vardata, &opproc, constval, true,
     584             :                                 &sumcommon);
     585             : 
     586             :     /*
     587             :      * If there is a histogram, determine which bin the constant falls in, and
     588             :      * compute the resulting contribution to selectivity.
     589             :      */
     590        1477 :     hist_selec = ineq_histogram_selectivity(root, vardata, &opproc, isgt,
     591             :                                             constval, consttype);
     592             : 
     593             :     /*
     594             :      * Now merge the results from the MCV and histogram calculations,
     595             :      * realizing that the histogram covers only the non-null values that are
     596             :      * not listed in MCV.
     597             :      */
     598        1477 :     selec = 1.0 - stats->stanullfrac - sumcommon;
     599             : 
     600        1477 :     if (hist_selec >= 0.0)
     601        1322 :         selec *= hist_selec;
     602             :     else
     603             :     {
     604             :         /*
     605             :          * If no histogram but there are values not accounted for by MCV,
     606             :          * arbitrarily assume half of them will match.
     607             :          */
     608         155 :         selec *= 0.5;
     609             :     }
     610             : 
     611        1477 :     selec += mcv_selec;
     612             : 
     613             :     /* result should be in range, but make sure... */
     614        1477 :     CLAMP_PROBABILITY(selec);
     615             : 
     616        1477 :     return selec;
     617             : }
     618             : 
     619             : /*
     620             :  *  mcv_selectivity         - Examine the MCV list for selectivity estimates
     621             :  *
     622             :  * Determine the fraction of the variable's MCV population that satisfies
     623             :  * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.  Also
     624             :  * compute the fraction of the total column population represented by the MCV
     625             :  * list.  This code will work for any boolean-returning predicate operator.
     626             :  *
     627             :  * The function result is the MCV selectivity, and the fraction of the
     628             :  * total population is returned into *sumcommonp.  Zeroes are returned
     629             :  * if there is no MCV list.
     630             :  */
     631             : double
     632        1662 : mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
     633             :                 Datum constval, bool varonleft,
     634             :                 double *sumcommonp)
     635             : {
     636             :     double      mcv_selec,
     637             :                 sumcommon;
     638             :     AttStatsSlot sslot;
     639             :     int         i;
     640             : 
     641        1662 :     mcv_selec = 0.0;
     642        1662 :     sumcommon = 0.0;
     643             : 
     644        3236 :     if (HeapTupleIsValid(vardata->statsTuple) &&
     645        3144 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
     646        1570 :         get_attstatsslot(&sslot, vardata->statsTuple,
     647             :                          STATISTIC_KIND_MCV, InvalidOid,
     648             :                          ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
     649             :     {
     650       37269 :         for (i = 0; i < sslot.nvalues; i++)
     651             :         {
     652       72820 :             if (varonleft ?
     653       36410 :                 DatumGetBool(FunctionCall2Coll(opproc,
     654             :                                                DEFAULT_COLLATION_OID,
     655             :                                                sslot.values[i],
     656             :                                                constval)) :
     657           0 :                 DatumGetBool(FunctionCall2Coll(opproc,
     658             :                                                DEFAULT_COLLATION_OID,
     659             :                                                constval,
     660             :                                                sslot.values[i])))
     661       15499 :                 mcv_selec += sslot.numbers[i];
     662       36410 :             sumcommon += sslot.numbers[i];
     663             :         }
     664         859 :         free_attstatsslot(&sslot);
     665             :     }
     666             : 
     667        1662 :     *sumcommonp = sumcommon;
     668        1662 :     return mcv_selec;
     669             : }
     670             : 
     671             : /*
     672             :  *  histogram_selectivity   - Examine the histogram for selectivity estimates
     673             :  *
     674             :  * Determine the fraction of the variable's histogram entries that satisfy
     675             :  * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.
     676             :  *
     677             :  * This code will work for any boolean-returning predicate operator, whether
     678             :  * or not it has anything to do with the histogram sort operator.  We are
     679             :  * essentially using the histogram just as a representative sample.  However,
     680             :  * small histograms are unlikely to be all that representative, so the caller
     681             :  * should be prepared to fall back on some other estimation approach when the
     682             :  * histogram is missing or very small.  It may also be prudent to combine this
     683             :  * approach with another one when the histogram is small.
     684             :  *
     685             :  * If the actual histogram size is not at least min_hist_size, we won't bother
     686             :  * to do the calculation at all.  Also, if the n_skip parameter is > 0, we
     687             :  * ignore the first and last n_skip histogram elements, on the grounds that
     688             :  * they are outliers and hence not very representative.  Typical values for
     689             :  * these parameters are 10 and 1.
     690             :  *
     691             :  * The function result is the selectivity, or -1 if there is no histogram
     692             :  * or it's smaller than min_hist_size.
     693             :  *
     694             :  * The output parameter *hist_size receives the actual histogram size,
     695             :  * or zero if no histogram.  Callers may use this number to decide how
     696             :  * much faith to put in the function result.
     697             :  *
     698             :  * Note that the result disregards both the most-common-values (if any) and
     699             :  * null entries.  The caller is expected to combine this result with
     700             :  * statistics for those portions of the column population.  It may also be
     701             :  * prudent to clamp the result range, ie, disbelieve exact 0 or 1 outputs.
     702             :  */
     703             : double
     704         185 : histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
     705             :                       Datum constval, bool varonleft,
     706             :                       int min_hist_size, int n_skip,
     707             :                       int *hist_size)
     708             : {
     709             :     double      result;
     710             :     AttStatsSlot sslot;
     711             : 
     712             :     /* check sanity of parameters */
     713         185 :     Assert(n_skip >= 0);
     714         185 :     Assert(min_hist_size > 2 * n_skip);
     715             : 
     716         282 :     if (HeapTupleIsValid(vardata->statsTuple) &&
     717         194 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
     718          97 :         get_attstatsslot(&sslot, vardata->statsTuple,
     719             :                          STATISTIC_KIND_HISTOGRAM, InvalidOid,
     720             :                          ATTSTATSSLOT_VALUES))
     721             :     {
     722          97 :         *hist_size = sslot.nvalues;
     723          97 :         if (sslot.nvalues >= min_hist_size)
     724             :         {
     725          66 :             int         nmatch = 0;
     726             :             int         i;
     727             : 
     728        6545 :             for (i = n_skip; i < sslot.nvalues - n_skip; i++)
     729             :             {
     730       12958 :                 if (varonleft ?
     731        6479 :                     DatumGetBool(FunctionCall2Coll(opproc,
     732             :                                                    DEFAULT_COLLATION_OID,
     733             :                                                    sslot.values[i],
     734             :                                                    constval)) :
     735           0 :                     DatumGetBool(FunctionCall2Coll(opproc,
     736             :                                                    DEFAULT_COLLATION_OID,
     737             :                                                    constval,
     738             :                                                    sslot.values[i])))
     739         251 :                     nmatch++;
     740             :             }
     741          66 :             result = ((double) nmatch) / ((double) (sslot.nvalues - 2 * n_skip));
     742             :         }
     743             :         else
     744          31 :             result = -1;
     745          97 :         free_attstatsslot(&sslot);
     746             :     }
     747             :     else
     748             :     {
     749          88 :         *hist_size = 0;
     750          88 :         result = -1;
     751             :     }
     752             : 
     753         185 :     return result;
     754             : }
     755             : 
     756             : /*
     757             :  *  ineq_histogram_selectivity  - Examine the histogram for scalarineqsel
     758             :  *
     759             :  * Determine the fraction of the variable's histogram population that
     760             :  * satisfies the inequality condition, ie, VAR < CONST or VAR > CONST.
     761             :  *
     762             :  * Returns -1 if there is no histogram (valid results will always be >= 0).
     763             :  *
     764             :  * Note that the result disregards both the most-common-values (if any) and
     765             :  * null entries.  The caller is expected to combine this result with
     766             :  * statistics for those portions of the column population.
     767             :  */
     768             : static double
     769        1591 : ineq_histogram_selectivity(PlannerInfo *root,
     770             :                            VariableStatData *vardata,
     771             :                            FmgrInfo *opproc, bool isgt,
     772             :                            Datum constval, Oid consttype)
     773             : {
     774             :     double      hist_selec;
     775             :     AttStatsSlot sslot;
     776             : 
     777        1591 :     hist_selec = -1.0;
     778             : 
     779             :     /*
     780             :      * Someday, ANALYZE might store more than one histogram per rel/att,
     781             :      * corresponding to more than one possible sort ordering defined for the
     782             :      * column type.  However, to make that work we will need to figure out
     783             :      * which staop to search for --- it's not necessarily the one we have at
     784             :      * hand!  (For example, we might have a '<=' operator rather than the '<'
     785             :      * operator that will appear in staop.)  For now, assume that whatever
     786             :      * appears in pg_statistic is sorted the same way our operator sorts, or
     787             :      * the reverse way if isgt is TRUE.
     788             :      */
     789        3132 :     if (HeapTupleIsValid(vardata->statsTuple) &&
     790        3078 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
     791        1537 :         get_attstatsslot(&sslot, vardata->statsTuple,
     792             :                          STATISTIC_KIND_HISTOGRAM, InvalidOid,
     793             :                          ATTSTATSSLOT_VALUES))
     794             :     {
     795        1386 :         if (sslot.nvalues > 1)
     796             :         {
     797             :             /*
     798             :              * Use binary search to find proper location, ie, the first slot
     799             :              * at which the comparison fails.  (If the given operator isn't
     800             :              * actually sort-compatible with the histogram, you'll get garbage
     801             :              * results ... but probably not any more garbage-y than you would
     802             :              * from the old linear search.)
     803             :              *
     804             :              * If the binary search accesses the first or last histogram
     805             :              * entry, we try to replace that endpoint with the true column min
     806             :              * or max as found by get_actual_variable_range().  This
     807             :              * ameliorates misestimates when the min or max is moving as a
     808             :              * result of changes since the last ANALYZE.  Note that this could
     809             :              * result in effectively including MCVs into the histogram that
     810             :              * weren't there before, but we don't try to correct for that.
     811             :              */
     812             :             double      histfrac;
     813        1386 :             int         lobound = 0;    /* first possible slot to search */
     814        1386 :             int         hibound = sslot.nvalues;    /* last+1 slot to search */
     815        1386 :             bool        have_end = false;
     816             : 
     817             :             /*
     818             :              * If there are only two histogram entries, we'll want up-to-date
     819             :              * values for both.  (If there are more than two, we need at most
     820             :              * one of them to be updated, so we deal with that within the
     821             :              * loop.)
     822             :              */
     823        1386 :             if (sslot.nvalues == 2)
     824          32 :                 have_end = get_actual_variable_range(root,
     825             :                                                      vardata,
     826             :                                                      sslot.staop,
     827             :                                                      &sslot.values[0],
     828          32 :                                                      &sslot.values[1]);
     829             : 
     830       10890 :             while (lobound < hibound)
     831             :             {
     832        8118 :                 int         probe = (lobound + hibound) / 2;
     833             :                 bool        ltcmp;
     834             : 
     835             :                 /*
     836             :                  * If we find ourselves about to compare to the first or last
     837             :                  * histogram entry, first try to replace it with the actual
     838             :                  * current min or max (unless we already did so above).
     839             :                  */
     840        8118 :                 if (probe == 0 && sslot.nvalues > 2)
     841         566 :                     have_end = get_actual_variable_range(root,
     842             :                                                          vardata,
     843             :                                                          sslot.staop,
     844             :                                                          &sslot.values[0],
     845             :                                                          NULL);
     846        7552 :                 else if (probe == sslot.nvalues - 1 && sslot.nvalues > 2)
     847         652 :                     have_end = get_actual_variable_range(root,
     848             :                                                          vardata,
     849             :                                                          sslot.staop,
     850             :                                                          NULL,
     851         652 :                                                          &sslot.values[probe]);
     852             : 
     853        8118 :                 ltcmp = DatumGetBool(FunctionCall2Coll(opproc,
     854             :                                                        DEFAULT_COLLATION_OID,
     855             :                                                        sslot.values[probe],
     856             :                                                        constval));
     857        8118 :                 if (isgt)
     858        2821 :                     ltcmp = !ltcmp;
     859        8118 :                 if (ltcmp)
     860        2782 :                     lobound = probe + 1;
     861             :                 else
     862        5336 :                     hibound = probe;
     863             :             }
     864             : 
     865        1386 :             if (lobound <= 0)
     866             :             {
     867             :                 /* Constant is below lower histogram boundary. */
     868         430 :                 histfrac = 0.0;
     869             :             }
     870         956 :             else if (lobound >= sslot.nvalues)
     871             :             {
     872             :                 /* Constant is above upper histogram boundary. */
     873         223 :                 histfrac = 1.0;
     874             :             }
     875             :             else
     876             :             {
     877         733 :                 int         i = lobound;
     878             :                 double      val,
     879             :                             high,
     880             :                             low;
     881             :                 double      binfrac;
     882             : 
     883             :                 /*
     884             :                  * We have values[i-1] <= constant <= values[i].
     885             :                  *
     886             :                  * Convert the constant and the two nearest bin boundary
     887             :                  * values to a uniform comparison scale, and do a linear
     888             :                  * interpolation within this bin.
     889             :                  */
     890        2199 :                 if (convert_to_scalar(constval, consttype, &val,
     891        1466 :                                       sslot.values[i - 1], sslot.values[i],
     892             :                                       vardata->vartype,
     893             :                                       &low, &high))
     894             :                 {
     895         732 :                     if (high <= low)
     896             :                     {
     897             :                         /* cope if bin boundaries appear identical */
     898          24 :                         binfrac = 0.5;
     899             :                     }
     900         708 :                     else if (val <= low)
     901          64 :                         binfrac = 0.0;
     902         644 :                     else if (val >= high)
     903          24 :                         binfrac = 1.0;
     904             :                     else
     905             :                     {
     906         620 :                         binfrac = (val - low) / (high - low);
     907             : 
     908             :                         /*
     909             :                          * Watch out for the possibility that we got a NaN or
     910             :                          * Infinity from the division.  This can happen
     911             :                          * despite the previous checks, if for example "low"
     912             :                          * is -Infinity.
     913             :                          */
     914         620 :                         if (isnan(binfrac) ||
     915         620 :                             binfrac < 0.0 || binfrac > 1.0)
     916           0 :                             binfrac = 0.5;
     917             :                     }
     918             :                 }
     919             :                 else
     920             :                 {
     921             :                     /*
     922             :                      * Ideally we'd produce an error here, on the grounds that
     923             :                      * the given operator shouldn't have scalarXXsel
     924             :                      * registered as its selectivity func unless we can deal
     925             :                      * with its operand types.  But currently, all manner of
     926             :                      * stuff is invoking scalarXXsel, so give a default
     927             :                      * estimate until that can be fixed.
     928             :                      */
     929           1 :                     binfrac = 0.5;
     930             :                 }
     931             : 
     932             :                 /*
     933             :                  * Now, compute the overall selectivity across the values
     934             :                  * represented by the histogram.  We have i-1 full bins and
     935             :                  * binfrac partial bin below the constant.
     936             :                  */
     937         733 :                 histfrac = (double) (i - 1) + binfrac;
     938         733 :                 histfrac /= (double) (sslot.nvalues - 1);
     939             :             }
     940             : 
     941             :             /*
     942             :              * Now histfrac = fraction of histogram entries below the
     943             :              * constant.
     944             :              *
     945             :              * Account for "<" vs ">"
     946             :              */
     947        1386 :             hist_selec = isgt ? (1.0 - histfrac) : histfrac;
     948             : 
     949             :             /*
     950             :              * The histogram boundaries are only approximate to begin with,
     951             :              * and may well be out of date anyway.  Therefore, don't believe
     952             :              * extremely small or large selectivity estimates --- unless we
     953             :              * got actual current endpoint values from the table.
     954             :              */
     955        1386 :             if (have_end)
     956         566 :                 CLAMP_PROBABILITY(hist_selec);
     957             :             else
     958             :             {
     959         820 :                 if (hist_selec < 0.0001)
     960         119 :                     hist_selec = 0.0001;
     961         701 :                 else if (hist_selec > 0.9999)
     962         223 :                     hist_selec = 0.9999;
     963             :             }
     964             :         }
     965             : 
     966        1386 :         free_attstatsslot(&sslot);
     967             :     }
     968             : 
     969        1591 :     return hist_selec;
     970             : }
     971             : 
     972             : /*
     973             :  *      scalarltsel     - Selectivity of "<" (also "<=") for scalars.
     974             :  */
     975             : Datum
     976        1005 : scalarltsel(PG_FUNCTION_ARGS)
     977             : {
     978        1005 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
     979        1005 :     Oid         operator = PG_GETARG_OID(1);
     980        1005 :     List       *args = (List *) PG_GETARG_POINTER(2);
     981        1005 :     int         varRelid = PG_GETARG_INT32(3);
     982             :     VariableStatData vardata;
     983             :     Node       *other;
     984             :     bool        varonleft;
     985             :     Datum       constval;
     986             :     Oid         consttype;
     987             :     bool        isgt;
     988             :     double      selec;
     989             : 
     990             :     /*
     991             :      * If expression is not variable op something or something op variable,
     992             :      * then punt and return a default estimate.
     993             :      */
     994        1005 :     if (!get_restriction_variable(root, args, varRelid,
     995             :                                   &vardata, &other, &varonleft))
     996          18 :         PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
     997             : 
     998             :     /*
     999             :      * Can't do anything useful if the something is not a constant, either.
    1000             :      */
    1001         987 :     if (!IsA(other, Const))
    1002             :     {
    1003          75 :         ReleaseVariableStats(vardata);
    1004          75 :         PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    1005             :     }
    1006             : 
    1007             :     /*
    1008             :      * If the constant is NULL, assume operator is strict and return zero, ie,
    1009             :      * operator will never return TRUE.
    1010             :      */
    1011         912 :     if (((Const *) other)->constisnull)
    1012             :     {
    1013           0 :         ReleaseVariableStats(vardata);
    1014           0 :         PG_RETURN_FLOAT8(0.0);
    1015             :     }
    1016         912 :     constval = ((Const *) other)->constvalue;
    1017         912 :     consttype = ((Const *) other)->consttype;
    1018             : 
    1019             :     /*
    1020             :      * Force the var to be on the left to simplify logic in scalarineqsel.
    1021             :      */
    1022         912 :     if (varonleft)
    1023             :     {
    1024             :         /* we have var < other */
    1025         904 :         isgt = false;
    1026             :     }
    1027             :     else
    1028             :     {
    1029             :         /* we have other < var, commute to make var > other */
    1030           8 :         operator = get_commutator(operator);
    1031           8 :         if (!operator)
    1032             :         {
    1033             :             /* Use default selectivity (should we raise an error instead?) */
    1034           0 :             ReleaseVariableStats(vardata);
    1035           0 :             PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    1036             :         }
    1037           8 :         isgt = true;
    1038             :     }
    1039             : 
    1040         912 :     selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
    1041             : 
    1042         912 :     ReleaseVariableStats(vardata);
    1043             : 
    1044         912 :     PG_RETURN_FLOAT8((float8) selec);
    1045             : }
    1046             : 
    1047             : /*
    1048             :  *      scalargtsel     - Selectivity of ">" (also ">=") for integers.
    1049             :  */
    1050             : Datum
    1051        1133 : scalargtsel(PG_FUNCTION_ARGS)
    1052             : {
    1053        1133 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
    1054        1133 :     Oid         operator = PG_GETARG_OID(1);
    1055        1133 :     List       *args = (List *) PG_GETARG_POINTER(2);
    1056        1133 :     int         varRelid = PG_GETARG_INT32(3);
    1057             :     VariableStatData vardata;
    1058             :     Node       *other;
    1059             :     bool        varonleft;
    1060             :     Datum       constval;
    1061             :     Oid         consttype;
    1062             :     bool        isgt;
    1063             :     double      selec;
    1064             : 
    1065             :     /*
    1066             :      * If expression is not variable op something or something op variable,
    1067             :      * then punt and return a default estimate.
    1068             :      */
    1069        1133 :     if (!get_restriction_variable(root, args, varRelid,
    1070             :                                   &vardata, &other, &varonleft))
    1071           3 :         PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    1072             : 
    1073             :     /*
    1074             :      * Can't do anything useful if the something is not a constant, either.
    1075             :      */
    1076        1130 :     if (!IsA(other, Const))
    1077             :     {
    1078          16 :         ReleaseVariableStats(vardata);
    1079          16 :         PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    1080             :     }
    1081             : 
    1082             :     /*
    1083             :      * If the constant is NULL, assume operator is strict and return zero, ie,
    1084             :      * operator will never return TRUE.
    1085             :      */
    1086        1114 :     if (((Const *) other)->constisnull)
    1087             :     {
    1088           0 :         ReleaseVariableStats(vardata);
    1089           0 :         PG_RETURN_FLOAT8(0.0);
    1090             :     }
    1091        1114 :     constval = ((Const *) other)->constvalue;
    1092        1114 :     consttype = ((Const *) other)->consttype;
    1093             : 
    1094             :     /*
    1095             :      * Force the var to be on the left to simplify logic in scalarineqsel.
    1096             :      */
    1097        1114 :     if (varonleft)
    1098             :     {
    1099             :         /* we have var > other */
    1100        1105 :         isgt = true;
    1101             :     }
    1102             :     else
    1103             :     {
    1104             :         /* we have other > var, commute to make var < other */
    1105           9 :         operator = get_commutator(operator);
    1106           9 :         if (!operator)
    1107             :         {
    1108             :             /* Use default selectivity (should we raise an error instead?) */
    1109           0 :             ReleaseVariableStats(vardata);
    1110           0 :             PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    1111             :         }
    1112           9 :         isgt = false;
    1113             :     }
    1114             : 
    1115        1114 :     selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
    1116             : 
    1117        1114 :     ReleaseVariableStats(vardata);
    1118             : 
    1119        1114 :     PG_RETURN_FLOAT8((float8) selec);
    1120             : }
    1121             : 
    1122             : /*
    1123             :  * patternsel           - Generic code for pattern-match selectivity.
    1124             :  */
    1125             : static double
    1126         461 : patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
    1127             : {
    1128         461 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
    1129         461 :     Oid         operator = PG_GETARG_OID(1);
    1130         461 :     List       *args = (List *) PG_GETARG_POINTER(2);
    1131         461 :     int         varRelid = PG_GETARG_INT32(3);
    1132         461 :     Oid         collation = PG_GET_COLLATION();
    1133             :     VariableStatData vardata;
    1134             :     Node       *other;
    1135             :     bool        varonleft;
    1136             :     Datum       constval;
    1137             :     Oid         consttype;
    1138             :     Oid         vartype;
    1139             :     Oid         opfamily;
    1140             :     Pattern_Prefix_Status pstatus;
    1141             :     Const      *patt;
    1142         461 :     Const      *prefix = NULL;
    1143         461 :     Selectivity rest_selec = 0;
    1144         461 :     double      nullfrac = 0.0;
    1145             :     double      result;
    1146             : 
    1147             :     /*
    1148             :      * If this is for a NOT LIKE or similar operator, get the corresponding
    1149             :      * positive-match operator and work with that.  Set result to the correct
    1150             :      * default estimate, too.
    1151             :      */
    1152         461 :     if (negate)
    1153             :     {
    1154          33 :         operator = get_negator(operator);
    1155          33 :         if (!OidIsValid(operator))
    1156           0 :             elog(ERROR, "patternsel called for operator without a negator");
    1157          33 :         result = 1.0 - DEFAULT_MATCH_SEL;
    1158             :     }
    1159             :     else
    1160             :     {
    1161         428 :         result = DEFAULT_MATCH_SEL;
    1162             :     }
    1163             : 
    1164             :     /*
    1165             :      * If expression is not variable op constant, then punt and return a
    1166             :      * default estimate.
    1167             :      */
    1168         461 :     if (!get_restriction_variable(root, args, varRelid,
    1169             :                                   &vardata, &other, &varonleft))
    1170           2 :         return result;
    1171         459 :     if (!varonleft || !IsA(other, Const))
    1172             :     {
    1173           0 :         ReleaseVariableStats(vardata);
    1174           0 :         return result;
    1175             :     }
    1176             : 
    1177             :     /*
    1178             :      * If the constant is NULL, assume operator is strict and return zero, ie,
    1179             :      * operator will never return TRUE.  (It's zero even for a negator op.)
    1180             :      */
    1181         459 :     if (((Const *) other)->constisnull)
    1182             :     {
    1183           0 :         ReleaseVariableStats(vardata);
    1184           0 :         return 0.0;
    1185             :     }
    1186         459 :     constval = ((Const *) other)->constvalue;
    1187         459 :     consttype = ((Const *) other)->consttype;
    1188             : 
    1189             :     /*
    1190             :      * The right-hand const is type text or bytea for all supported operators.
    1191             :      * We do not expect to see binary-compatible types here, since
    1192             :      * const-folding should have relabeled the const to exactly match the
    1193             :      * operator's declared type.
    1194             :      */
    1195         459 :     if (consttype != TEXTOID && consttype != BYTEAOID)
    1196             :     {
    1197           0 :         ReleaseVariableStats(vardata);
    1198           0 :         return result;
    1199             :     }
    1200             : 
    1201             :     /*
    1202             :      * Similarly, the exposed type of the left-hand side should be one of
    1203             :      * those we know.  (Do not look at vardata.atttype, which might be
    1204             :      * something binary-compatible but different.)  We can use it to choose
    1205             :      * the index opfamily from which we must draw the comparison operators.
    1206             :      *
    1207             :      * NOTE: It would be more correct to use the PATTERN opfamilies than the
    1208             :      * simple ones, but at the moment ANALYZE will not generate statistics for
    1209             :      * the PATTERN operators.  But our results are so approximate anyway that
    1210             :      * it probably hardly matters.
    1211             :      */
    1212         459 :     vartype = vardata.vartype;
    1213             : 
    1214         459 :     switch (vartype)
    1215             :     {
    1216             :         case TEXTOID:
    1217          79 :             opfamily = TEXT_BTREE_FAM_OID;
    1218          79 :             break;
    1219             :         case BPCHAROID:
    1220           6 :             opfamily = BPCHAR_BTREE_FAM_OID;
    1221           6 :             break;
    1222             :         case NAMEOID:
    1223         374 :             opfamily = NAME_BTREE_FAM_OID;
    1224         374 :             break;
    1225             :         case BYTEAOID:
    1226           0 :             opfamily = BYTEA_BTREE_FAM_OID;
    1227           0 :             break;
    1228             :         default:
    1229           0 :             ReleaseVariableStats(vardata);
    1230           0 :             return result;
    1231             :     }
    1232             : 
    1233             :     /*
    1234             :      * Grab the nullfrac for use below.
    1235             :      */
    1236         459 :     if (HeapTupleIsValid(vardata.statsTuple))
    1237             :     {
    1238             :         Form_pg_statistic stats;
    1239             : 
    1240         363 :         stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
    1241         363 :         nullfrac = stats->stanullfrac;
    1242             :     }
    1243             : 
    1244             :     /*
    1245             :      * Pull out any fixed prefix implied by the pattern, and estimate the
    1246             :      * fractional selectivity of the remainder of the pattern.  Unlike many of
    1247             :      * the other functions in this file, we use the pattern operator's actual
    1248             :      * collation for this step.  This is not because we expect the collation
    1249             :      * to make a big difference in the selectivity estimate (it seldom would),
    1250             :      * but because we want to be sure we cache compiled regexps under the
    1251             :      * right cache key, so that they can be re-used at runtime.
    1252             :      */
    1253         459 :     patt = (Const *) other;
    1254         459 :     pstatus = pattern_fixed_prefix(patt, ptype, collation,
    1255             :                                    &prefix, &rest_selec);
    1256             : 
    1257             :     /*
    1258             :      * If necessary, coerce the prefix constant to the right type.
    1259             :      */
    1260         459 :     if (prefix && prefix->consttype != vartype)
    1261             :     {
    1262             :         char       *prefixstr;
    1263             : 
    1264         362 :         switch (prefix->consttype)
    1265             :         {
    1266             :             case TEXTOID:
    1267         362 :                 prefixstr = TextDatumGetCString(prefix->constvalue);
    1268         362 :                 break;
    1269             :             case BYTEAOID:
    1270           0 :                 prefixstr = DatumGetCString(DirectFunctionCall1(byteaout,
    1271             :                                                                 prefix->constvalue));
    1272           0 :                 break;
    1273             :             default:
    1274           0 :                 elog(ERROR, "unrecognized consttype: %u",
    1275             :                      prefix->consttype);
    1276             :                 ReleaseVariableStats(vardata);
    1277             :                 return result;
    1278             :         }
    1279         362 :         prefix = string_to_const(prefixstr, vartype);
    1280         362 :         pfree(prefixstr);
    1281             :     }
    1282             : 
    1283         459 :     if (pstatus == Pattern_Prefix_Exact)
    1284             :     {
    1285             :         /*
    1286             :          * Pattern specifies an exact match, so pretend operator is '='
    1287             :          */
    1288         274 :         Oid         eqopr = get_opfamily_member(opfamily, vartype, vartype,
    1289             :                                                 BTEqualStrategyNumber);
    1290             : 
    1291         274 :         if (eqopr == InvalidOid)
    1292           0 :             elog(ERROR, "no = operator for opfamily %u", opfamily);
    1293         274 :         result = var_eq_const(&vardata, eqopr, prefix->constvalue,
    1294             :                               false, true, false);
    1295             :     }
    1296             :     else
    1297             :     {
    1298             :         /*
    1299             :          * Not exact-match pattern.  If we have a sufficiently large
    1300             :          * histogram, estimate selectivity for the histogram part of the
    1301             :          * population by counting matches in the histogram.  If not, estimate
    1302             :          * selectivity of the fixed prefix and remainder of pattern
    1303             :          * separately, then combine the two to get an estimate of the
    1304             :          * selectivity for the part of the column population represented by
    1305             :          * the histogram.  (For small histograms, we combine these
    1306             :          * approaches.)
    1307             :          *
    1308             :          * We then add up data for any most-common-values values; these are
    1309             :          * not in the histogram population, and we can get exact answers for
    1310             :          * them by applying the pattern operator, so there's no reason to
    1311             :          * approximate.  (If the MCVs cover a significant part of the total
    1312             :          * population, this gives us a big leg up in accuracy.)
    1313             :          */
    1314             :         Selectivity selec;
    1315             :         int         hist_size;
    1316             :         FmgrInfo    opproc;
    1317             :         double      mcv_selec,
    1318             :                     sumcommon;
    1319             : 
    1320             :         /* Try to use the histogram entries to get selectivity */
    1321         185 :         fmgr_info(get_opcode(operator), &opproc);
    1322             : 
    1323         185 :         selec = histogram_selectivity(&vardata, &opproc, constval, true,
    1324             :                                       10, 1, &hist_size);
    1325             : 
    1326             :         /* If not at least 100 entries, use the heuristic method */
    1327         185 :         if (hist_size < 100)
    1328             :         {
    1329             :             Selectivity heursel;
    1330             :             Selectivity prefixsel;
    1331             : 
    1332         120 :             if (pstatus == Pattern_Prefix_Partial)
    1333          82 :                 prefixsel = prefix_selectivity(root, &vardata, vartype,
    1334             :                                                opfamily, prefix);
    1335             :             else
    1336          38 :                 prefixsel = 1.0;
    1337         120 :             heursel = prefixsel * rest_selec;
    1338             : 
    1339         120 :             if (selec < 0)       /* fewer than 10 histogram entries? */
    1340         119 :                 selec = heursel;
    1341             :             else
    1342             :             {
    1343             :                 /*
    1344             :                  * For histogram sizes from 10 to 100, we combine the
    1345             :                  * histogram and heuristic selectivities, putting increasingly
    1346             :                  * more trust in the histogram for larger sizes.
    1347             :                  */
    1348           1 :                 double      hist_weight = hist_size / 100.0;
    1349             : 
    1350           1 :                 selec = selec * hist_weight + heursel * (1.0 - hist_weight);
    1351             :             }
    1352             :         }
    1353             : 
    1354             :         /* In any case, don't believe extremely small or large estimates. */
    1355         185 :         if (selec < 0.0001)
    1356         101 :             selec = 0.0001;
    1357          84 :         else if (selec > 0.9999)
    1358           4 :             selec = 0.9999;
    1359             : 
    1360             :         /*
    1361             :          * If we have most-common-values info, add up the fractions of the MCV
    1362             :          * entries that satisfy MCV OP PATTERN.  These fractions contribute
    1363             :          * directly to the result selectivity.  Also add up the total fraction
    1364             :          * represented by MCV entries.
    1365             :          */
    1366         185 :         mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
    1367             :                                     &sumcommon);
    1368             : 
    1369             :         /*
    1370             :          * Now merge the results from the MCV and histogram calculations,
    1371             :          * realizing that the histogram covers only the non-null values that
    1372             :          * are not listed in MCV.
    1373             :          */
    1374         185 :         selec *= 1.0 - nullfrac - sumcommon;
    1375         185 :         selec += mcv_selec;
    1376         185 :         result = selec;
    1377             :     }
    1378             : 
    1379             :     /* now adjust if we wanted not-match rather than match */
    1380         459 :     if (negate)
    1381          31 :         result = 1.0 - result - nullfrac;
    1382             : 
    1383             :     /* result should be in range, but make sure... */
    1384         459 :     CLAMP_PROBABILITY(result);
    1385             : 
    1386         459 :     if (prefix)
    1387             :     {
    1388         421 :         pfree(DatumGetPointer(prefix->constvalue));
    1389         421 :         pfree(prefix);
    1390             :     }
    1391             : 
    1392         459 :     ReleaseVariableStats(vardata);
    1393             : 
    1394         459 :     return result;
    1395             : }
    1396             : 
    1397             : /*
    1398             :  *      regexeqsel      - Selectivity of regular-expression pattern match.
    1399             :  */
    1400             : Datum
    1401         329 : regexeqsel(PG_FUNCTION_ARGS)
    1402             : {
    1403         329 :     PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex, false));
    1404             : }
    1405             : 
    1406             : /*
    1407             :  *      icregexeqsel    - Selectivity of case-insensitive regex match.
    1408             :  */
    1409             : Datum
    1410           0 : icregexeqsel(PG_FUNCTION_ARGS)
    1411             : {
    1412           0 :     PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex_IC, false));
    1413             : }
    1414             : 
    1415             : /*
    1416             :  *      likesel         - Selectivity of LIKE pattern match.
    1417             :  */
    1418             : Datum
    1419          99 : likesel(PG_FUNCTION_ARGS)
    1420             : {
    1421          99 :     PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like, false));
    1422             : }
    1423             : 
    1424             : /*
    1425             :  *      iclikesel           - Selectivity of ILIKE pattern match.
    1426             :  */
    1427             : Datum
    1428           0 : iclikesel(PG_FUNCTION_ARGS)
    1429             : {
    1430           0 :     PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like_IC, false));
    1431             : }
    1432             : 
    1433             : /*
    1434             :  *      regexnesel      - Selectivity of regular-expression pattern non-match.
    1435             :  */
    1436             : Datum
    1437          20 : regexnesel(PG_FUNCTION_ARGS)
    1438             : {
    1439          20 :     PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex, true));
    1440             : }
    1441             : 
    1442             : /*
    1443             :  *      icregexnesel    - Selectivity of case-insensitive regex non-match.
    1444             :  */
    1445             : Datum
    1446           2 : icregexnesel(PG_FUNCTION_ARGS)
    1447             : {
    1448           2 :     PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex_IC, true));
    1449             : }
    1450             : 
    1451             : /*
    1452             :  *      nlikesel        - Selectivity of LIKE pattern non-match.
    1453             :  */
    1454             : Datum
    1455          11 : nlikesel(PG_FUNCTION_ARGS)
    1456             : {
    1457          11 :     PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like, true));
    1458             : }
    1459             : 
    1460             : /*
    1461             :  *      icnlikesel      - Selectivity of ILIKE pattern non-match.
    1462             :  */
    1463             : Datum
    1464           0 : icnlikesel(PG_FUNCTION_ARGS)
    1465             : {
    1466           0 :     PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like_IC, true));
    1467             : }
    1468             : 
    1469             : /*
    1470             :  *      boolvarsel      - Selectivity of Boolean variable.
    1471             :  *
    1472             :  * This can actually be called on any boolean-valued expression.  If it
    1473             :  * involves only Vars of the specified relation, and if there are statistics
    1474             :  * about the Var or expression (the latter is possible if it's indexed) then
    1475             :  * we'll produce a real estimate; otherwise it's just a default.
    1476             :  */
    1477             : Selectivity
    1478        2255 : boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
    1479             : {
    1480             :     VariableStatData vardata;
    1481             :     double      selec;
    1482             : 
    1483        2255 :     examine_variable(root, arg, varRelid, &vardata);
    1484        2255 :     if (HeapTupleIsValid(vardata.statsTuple))
    1485             :     {
    1486             :         /*
    1487             :          * A boolean variable V is equivalent to the clause V = 't', so we
    1488             :          * compute the selectivity as if that is what we have.
    1489             :          */
    1490         844 :         selec = var_eq_const(&vardata, BooleanEqualOperator,
    1491             :                              BoolGetDatum(true), false, true, false);
    1492             :     }
    1493        1411 :     else if (is_funcclause(arg))
    1494             :     {
    1495             :         /*
    1496             :          * If we have no stats and it's a function call, estimate 0.3333333.
    1497             :          * This seems a pretty unprincipled choice, but Postgres has been
    1498             :          * using that estimate for function calls since 1992.  The hoariness
    1499             :          * of this behavior suggests that we should not be in too much hurry
    1500             :          * to use another value.
    1501             :          */
    1502        1091 :         selec = 0.3333333;
    1503             :     }
    1504             :     else
    1505             :     {
    1506             :         /* Otherwise, the default estimate is 0.5 */
    1507         320 :         selec = 0.5;
    1508             :     }
    1509        2255 :     ReleaseVariableStats(vardata);
    1510        2255 :     return selec;
    1511             : }
    1512             : 
    1513             : /*
    1514             :  *      booltestsel     - Selectivity of BooleanTest Node.
    1515             :  */
    1516             : Selectivity
    1517          20 : booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
    1518             :             int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
    1519             : {
    1520             :     VariableStatData vardata;
    1521             :     double      selec;
    1522             : 
    1523          20 :     examine_variable(root, arg, varRelid, &vardata);
    1524             : 
    1525          20 :     if (HeapTupleIsValid(vardata.statsTuple))
    1526             :     {
    1527             :         Form_pg_statistic stats;
    1528             :         double      freq_null;
    1529             :         AttStatsSlot sslot;
    1530             : 
    1531           0 :         stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
    1532           0 :         freq_null = stats->stanullfrac;
    1533             : 
    1534           0 :         if (get_attstatsslot(&sslot, vardata.statsTuple,
    1535             :                              STATISTIC_KIND_MCV, InvalidOid,
    1536             :                              ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS)
    1537           0 :             && sslot.nnumbers > 0)
    1538           0 :         {
    1539             :             double      freq_true;
    1540             :             double      freq_false;
    1541             : 
    1542             :             /*
    1543             :              * Get first MCV frequency and derive frequency for true.
    1544             :              */
    1545           0 :             if (DatumGetBool(sslot.values[0]))
    1546           0 :                 freq_true = sslot.numbers[0];
    1547             :             else
    1548           0 :                 freq_true = 1.0 - sslot.numbers[0] - freq_null;
    1549             : 
    1550             :             /*
    1551             :              * Next derive frequency for false. Then use these as appropriate
    1552             :              * to derive frequency for each case.
    1553             :              */
    1554           0 :             freq_false = 1.0 - freq_true - freq_null;
    1555             : 
    1556           0 :             switch (booltesttype)
    1557             :             {
    1558             :                 case IS_UNKNOWN:
    1559             :                     /* select only NULL values */
    1560           0 :                     selec = freq_null;
    1561           0 :                     break;
    1562             :                 case IS_NOT_UNKNOWN:
    1563             :                     /* select non-NULL values */
    1564           0 :                     selec = 1.0 - freq_null;
    1565           0 :                     break;
    1566             :                 case IS_TRUE:
    1567             :                     /* select only TRUE values */
    1568           0 :                     selec = freq_true;
    1569           0 :                     break;
    1570             :                 case IS_NOT_TRUE:
    1571             :                     /* select non-TRUE values */
    1572           0 :                     selec = 1.0 - freq_true;
    1573           0 :                     break;
    1574             :                 case IS_FALSE:
    1575             :                     /* select only FALSE values */
    1576           0 :                     selec = freq_false;
    1577           0 :                     break;
    1578             :                 case IS_NOT_FALSE:
    1579             :                     /* select non-FALSE values */
    1580           0 :                     selec = 1.0 - freq_false;
    1581           0 :                     break;
    1582             :                 default:
    1583           0 :                     elog(ERROR, "unrecognized booltesttype: %d",
    1584             :                          (int) booltesttype);
    1585             :                     selec = 0.0;    /* Keep compiler quiet */
    1586             :                     break;
    1587             :             }
    1588             : 
    1589           0 :             free_attstatsslot(&sslot);
    1590             :         }
    1591             :         else
    1592             :         {
    1593             :             /*
    1594             :              * No most-common-value info available. Still have null fraction
    1595             :              * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
    1596             :              * for null fraction and assume a 50-50 split of TRUE and FALSE.
    1597             :              */
    1598           0 :             switch (booltesttype)
    1599             :             {
    1600             :                 case IS_UNKNOWN:
    1601             :                     /* select only NULL values */
    1602           0 :                     selec = freq_null;
    1603           0 :                     break;
    1604             :                 case IS_NOT_UNKNOWN:
    1605             :                     /* select non-NULL values */
    1606           0 :                     selec = 1.0 - freq_null;
    1607           0 :                     break;
    1608             :                 case IS_TRUE:
    1609             :                 case IS_FALSE:
    1610             :                     /* Assume we select half of the non-NULL values */
    1611           0 :                     selec = (1.0 - freq_null) / 2.0;
    1612           0 :                     break;
    1613             :                 case IS_NOT_TRUE:
    1614             :                 case IS_NOT_FALSE:
    1615             :                     /* Assume we select NULLs plus half of the non-NULLs */
    1616             :                     /* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
    1617           0 :                     selec = (freq_null + 1.0) / 2.0;
    1618           0 :                     break;
    1619             :                 default:
    1620           0 :                     elog(ERROR, "unrecognized booltesttype: %d",
    1621             :                          (int) booltesttype);
    1622             :                     selec = 0.0;    /* Keep compiler quiet */
    1623             :                     break;
    1624             :             }
    1625             :         }
    1626             :     }
    1627             :     else
    1628             :     {
    1629             :         /*
    1630             :          * If we can't get variable statistics for the argument, perhaps
    1631             :          * clause_selectivity can do something with it.  We ignore the
    1632             :          * possibility of a NULL value when using clause_selectivity, and just
    1633             :          * assume the value is either TRUE or FALSE.
    1634             :          */
    1635          20 :         switch (booltesttype)
    1636             :         {
    1637             :             case IS_UNKNOWN:
    1638           0 :                 selec = DEFAULT_UNK_SEL;
    1639           0 :                 break;
    1640             :             case IS_NOT_UNKNOWN:
    1641           0 :                 selec = DEFAULT_NOT_UNK_SEL;
    1642           0 :                 break;
    1643             :             case IS_TRUE:
    1644             :             case IS_NOT_FALSE:
    1645           5 :                 selec = (double) clause_selectivity(root, arg,
    1646             :                                                     varRelid,
    1647             :                                                     jointype, sjinfo);
    1648           5 :                 break;
    1649             :             case IS_FALSE:
    1650             :             case IS_NOT_TRUE:
    1651          15 :                 selec = 1.0 - (double) clause_selectivity(root, arg,
    1652             :                                                           varRelid,
    1653             :                                                           jointype, sjinfo);
    1654          15 :                 break;
    1655             :             default:
    1656           0 :                 elog(ERROR, "unrecognized booltesttype: %d",
    1657             :                      (int) booltesttype);
    1658             :                 selec = 0.0;    /* Keep compiler quiet */
    1659             :                 break;
    1660             :         }
    1661             :     }
    1662             : 
    1663          20 :     ReleaseVariableStats(vardata);
    1664             : 
    1665             :     /* result should be in range, but make sure... */
    1666          20 :     CLAMP_PROBABILITY(selec);
    1667             : 
    1668          20 :     return (Selectivity) selec;
    1669             : }
    1670             : 
    1671             : /*
    1672             :  *      nulltestsel     - Selectivity of NullTest Node.
    1673             :  */
    1674             : Selectivity
    1675         887 : nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg,
    1676             :             int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
    1677             : {
    1678             :     VariableStatData vardata;
    1679             :     double      selec;
    1680             : 
    1681         887 :     examine_variable(root, arg, varRelid, &vardata);
    1682             : 
    1683         887 :     if (HeapTupleIsValid(vardata.statsTuple))
    1684             :     {
    1685             :         Form_pg_statistic stats;
    1686             :         double      freq_null;
    1687             : 
    1688         357 :         stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
    1689         357 :         freq_null = stats->stanullfrac;
    1690             : 
    1691         357 :         switch (nulltesttype)
    1692             :         {
    1693             :             case IS_NULL:
    1694             : 
    1695             :                 /*
    1696             :                  * Use freq_null directly.
    1697             :                  */
    1698         287 :                 selec = freq_null;
    1699         287 :                 break;
    1700             :             case IS_NOT_NULL:
    1701             : 
    1702             :                 /*
    1703             :                  * Select not unknown (not null) values. Calculate from
    1704             :                  * freq_null.
    1705             :                  */
    1706          70 :                 selec = 1.0 - freq_null;
    1707          70 :                 break;
    1708             :             default:
    1709           0 :                 elog(ERROR, "unrecognized nulltesttype: %d",
    1710             :                      (int) nulltesttype);
    1711             :                 return (Selectivity) 0; /* keep compiler quiet */
    1712             :         }
    1713             :     }
    1714             :     else
    1715             :     {
    1716             :         /*
    1717             :          * No ANALYZE stats available, so make a guess
    1718             :          */
    1719         530 :         switch (nulltesttype)
    1720             :         {
    1721             :             case IS_NULL:
    1722         217 :                 selec = DEFAULT_UNK_SEL;
    1723         217 :                 break;
    1724             :             case IS_NOT_NULL:
    1725         313 :                 selec = DEFAULT_NOT_UNK_SEL;
    1726         313 :                 break;
    1727             :             default:
    1728           0 :                 elog(ERROR, "unrecognized nulltesttype: %d",
    1729             :                      (int) nulltesttype);
    1730             :                 return (Selectivity) 0; /* keep compiler quiet */
    1731             :         }
    1732             :     }
    1733             : 
    1734         887 :     ReleaseVariableStats(vardata);
    1735             : 
    1736             :     /* result should be in range, but make sure... */
    1737         887 :     CLAMP_PROBABILITY(selec);
    1738             : 
    1739         887 :     return (Selectivity) selec;
    1740             : }
    1741             : 
    1742             : /*
    1743             :  * strip_array_coercion - strip binary-compatible relabeling from an array expr
    1744             :  *
    1745             :  * For array values, the parser normally generates ArrayCoerceExpr conversions,
    1746             :  * but it seems possible that RelabelType might show up.  Also, the planner
    1747             :  * is not currently tense about collapsing stacked ArrayCoerceExpr nodes,
    1748             :  * so we need to be ready to deal with more than one level.
    1749             :  */
    1750             : static Node *
    1751        1851 : strip_array_coercion(Node *node)
    1752             : {
    1753             :     for (;;)
    1754             :     {
    1755        1867 :         if (node && IsA(node, ArrayCoerceExpr) &&
    1756          16 :             ((ArrayCoerceExpr *) node)->elemfuncid == InvalidOid)
    1757             :         {
    1758          16 :             node = (Node *) ((ArrayCoerceExpr *) node)->arg;
    1759             :         }
    1760        1835 :         else if (node && IsA(node, RelabelType))
    1761             :         {
    1762             :             /* We don't really expect this case, but may as well cope */
    1763           0 :             node = (Node *) ((RelabelType *) node)->arg;
    1764             :         }
    1765             :         else
    1766             :             break;
    1767          16 :     }
    1768        1835 :     return node;
    1769             : }
    1770             : 
    1771             : /*
    1772             :  *      scalararraysel      - Selectivity of ScalarArrayOpExpr Node.
    1773             :  */
    1774             : Selectivity
    1775         498 : scalararraysel(PlannerInfo *root,
    1776             :                ScalarArrayOpExpr *clause,
    1777             :                bool is_join_clause,
    1778             :                int varRelid,
    1779             :                JoinType jointype,
    1780             :                SpecialJoinInfo *sjinfo)
    1781             : {
    1782         498 :     Oid         operator = clause->opno;
    1783         498 :     bool        useOr = clause->useOr;
    1784         498 :     bool        isEquality = false;
    1785         498 :     bool        isInequality = false;
    1786             :     Node       *leftop;
    1787             :     Node       *rightop;
    1788             :     Oid         nominal_element_type;
    1789             :     Oid         nominal_element_collation;
    1790             :     TypeCacheEntry *typentry;
    1791             :     RegProcedure oprsel;
    1792             :     FmgrInfo    oprselproc;
    1793             :     Selectivity s1;
    1794             :     Selectivity s1disjoint;
    1795             : 
    1796             :     /* First, deconstruct the expression */
    1797         498 :     Assert(list_length(clause->args) == 2);
    1798         498 :     leftop = (Node *) linitial(clause->args);
    1799         498 :     rightop = (Node *) lsecond(clause->args);
    1800             : 
    1801             :     /* aggressively reduce both sides to constants */
    1802         498 :     leftop = estimate_expression_value(root, leftop);
    1803         498 :     rightop = estimate_expression_value(root, rightop);
    1804             : 
    1805             :     /* get nominal (after relabeling) element type of rightop */
    1806         498 :     nominal_element_type = get_base_element_type(exprType(rightop));
    1807         498 :     if (!OidIsValid(nominal_element_type))
    1808           0 :         return (Selectivity) 0.5;   /* probably shouldn't happen */
    1809             :     /* get nominal collation, too, for generating constants */
    1810         498 :     nominal_element_collation = exprCollation(rightop);
    1811             : 
    1812             :     /* look through any binary-compatible relabeling of rightop */
    1813         498 :     rightop = strip_array_coercion(rightop);
    1814             : 
    1815             :     /*
    1816             :      * Detect whether the operator is the default equality or inequality
    1817             :      * operator of the array element type.
    1818             :      */
    1819         498 :     typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
    1820         498 :     if (OidIsValid(typentry->eq_opr))
    1821             :     {
    1822         498 :         if (operator == typentry->eq_opr)
    1823         451 :             isEquality = true;
    1824          47 :         else if (get_negator(operator) == typentry->eq_opr)
    1825          41 :             isInequality = true;
    1826             :     }
    1827             : 
    1828             :     /*
    1829             :      * If it is equality or inequality, we might be able to estimate this as a
    1830             :      * form of array containment; for instance "const = ANY(column)" can be
    1831             :      * treated as "ARRAY[const] <@ column".  scalararraysel_containment tries
    1832             :      * that, and returns the selectivity estimate if successful, or -1 if not.
    1833             :      */
    1834         498 :     if ((isEquality || isInequality) && !is_join_clause)
    1835             :     {
    1836         492 :         s1 = scalararraysel_containment(root, leftop, rightop,
    1837             :                                         nominal_element_type,
    1838             :                                         isEquality, useOr, varRelid);
    1839         492 :         if (s1 >= 0.0)
    1840           8 :             return s1;
    1841             :     }
    1842             : 
    1843             :     /*
    1844             :      * Look up the underlying operator's selectivity estimator. Punt if it
    1845             :      * hasn't got one.
    1846             :      */
    1847         490 :     if (is_join_clause)
    1848           0 :         oprsel = get_oprjoin(operator);
    1849             :     else
    1850         490 :         oprsel = get_oprrest(operator);
    1851         490 :     if (!oprsel)
    1852           0 :         return (Selectivity) 0.5;
    1853         490 :     fmgr_info(oprsel, &oprselproc);
    1854             : 
    1855             :     /*
    1856             :      * In the array-containment check above, we must only believe that an
    1857             :      * operator is equality or inequality if it is the default btree equality
    1858             :      * operator (or its negator) for the element type, since those are the
    1859             :      * operators that array containment will use.  But in what follows, we can
    1860             :      * be a little laxer, and also believe that any operators using eqsel() or
    1861             :      * neqsel() as selectivity estimator act like equality or inequality.
    1862             :      */
    1863         490 :     if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
    1864         449 :         isEquality = true;
    1865          41 :     else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
    1866          35 :         isInequality = true;
    1867             : 
    1868             :     /*
    1869             :      * We consider three cases:
    1870             :      *
    1871             :      * 1. rightop is an Array constant: deconstruct the array, apply the
    1872             :      * operator's selectivity function for each array element, and merge the
    1873             :      * results in the same way that clausesel.c does for AND/OR combinations.
    1874             :      *
    1875             :      * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
    1876             :      * function for each element of the ARRAY[] construct, and merge.
    1877             :      *
    1878             :      * 3. otherwise, make a guess ...
    1879             :      */
    1880         490 :     if (rightop && IsA(rightop, Const))
    1881         305 :     {
    1882         305 :         Datum       arraydatum = ((Const *) rightop)->constvalue;
    1883         305 :         bool        arrayisnull = ((Const *) rightop)->constisnull;
    1884             :         ArrayType  *arrayval;
    1885             :         int16       elmlen;
    1886             :         bool        elmbyval;
    1887             :         char        elmalign;
    1888             :         int         num_elems;
    1889             :         Datum      *elem_values;
    1890             :         bool       *elem_nulls;
    1891             :         int         i;
    1892             : 
    1893         305 :         if (arrayisnull)        /* qual can't succeed if null array */
    1894           0 :             return (Selectivity) 0.0;
    1895         305 :         arrayval = DatumGetArrayTypeP(arraydatum);
    1896         305 :         get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
    1897             :                              &elmlen, &elmbyval, &elmalign);
    1898         305 :         deconstruct_array(arrayval,
    1899             :                           ARR_ELEMTYPE(arrayval),
    1900             :                           elmlen, elmbyval, elmalign,
    1901             :                           &elem_values, &elem_nulls, &num_elems);
    1902             : 
    1903             :         /*
    1904             :          * For generic operators, we assume the probability of success is
    1905             :          * independent for each array element.  But for "= ANY" or "<> ALL",
    1906             :          * if the array elements are distinct (which'd typically be the case)
    1907             :          * then the probabilities are disjoint, and we should just sum them.
    1908             :          *
    1909             :          * If we were being really tense we would try to confirm that the
    1910             :          * elements are all distinct, but that would be expensive and it
    1911             :          * doesn't seem to be worth the cycles; it would amount to penalizing
    1912             :          * well-written queries in favor of poorly-written ones.  However, we
    1913             :          * do protect ourselves a little bit by checking whether the
    1914             :          * disjointness assumption leads to an impossible (out of range)
    1915             :          * probability; if so, we fall back to the normal calculation.
    1916             :          */
    1917         305 :         s1 = s1disjoint = (useOr ? 0.0 : 1.0);
    1918             : 
    1919        1232 :         for (i = 0; i < num_elems; i++)
    1920             :         {
    1921             :             List       *args;
    1922             :             Selectivity s2;
    1923             : 
    1924         927 :             args = list_make2(leftop,
    1925             :                               makeConst(nominal_element_type,
    1926             :                                         -1,
    1927             :                                         nominal_element_collation,
    1928             :                                         elmlen,
    1929             :                                         elem_values[i],
    1930             :                                         elem_nulls[i],
    1931             :                                         elmbyval));
    1932         927 :             if (is_join_clause)
    1933           0 :                 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
    1934             :                                                       clause->inputcollid,
    1935             :                                                       PointerGetDatum(root),
    1936             :                                                       ObjectIdGetDatum(operator),
    1937             :                                                       PointerGetDatum(args),
    1938             :                                                       Int16GetDatum(jointype),
    1939             :                                                       PointerGetDatum(sjinfo)));
    1940             :             else
    1941         927 :                 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
    1942             :                                                       clause->inputcollid,
    1943             :                                                       PointerGetDatum(root),
    1944             :                                                       ObjectIdGetDatum(operator),
    1945             :                                                       PointerGetDatum(args),
    1946             :                                                       Int32GetDatum(varRelid)));
    1947             : 
    1948         927 :             if (useOr)
    1949             :             {
    1950         829 :                 s1 = s1 + s2 - s1 * s2;
    1951         829 :                 if (isEquality)
    1952         813 :                     s1disjoint += s2;
    1953             :             }
    1954             :             else
    1955             :             {
    1956          98 :                 s1 = s1 * s2;
    1957          98 :                 if (isInequality)
    1958          98 :                     s1disjoint += s2 - 1.0;
    1959             :             }
    1960             :         }
    1961             : 
    1962             :         /* accept disjoint-probability estimate if in range */
    1963         305 :         if ((useOr ? isEquality : isInequality) &&
    1964         291 :             s1disjoint >= 0.0 && s1disjoint <= 1.0)
    1965         291 :             s1 = s1disjoint;
    1966             :     }
    1967         191 :     else if (rightop && IsA(rightop, ArrayExpr) &&
    1968           6 :              !((ArrayExpr *) rightop)->multidims)
    1969           6 :     {
    1970           6 :         ArrayExpr  *arrayexpr = (ArrayExpr *) rightop;
    1971             :         int16       elmlen;
    1972             :         bool        elmbyval;
    1973             :         ListCell   *l;
    1974             : 
    1975           6 :         get_typlenbyval(arrayexpr->element_typeid,
    1976             :                         &elmlen, &elmbyval);
    1977             : 
    1978             :         /*
    1979             :          * We use the assumption of disjoint probabilities here too, although
    1980             :          * the odds of equal array elements are rather higher if the elements
    1981             :          * are not all constants (which they won't be, else constant folding
    1982             :          * would have reduced the ArrayExpr to a Const).  In this path it's
    1983             :          * critical to have the sanity check on the s1disjoint estimate.
    1984             :          */
    1985           6 :         s1 = s1disjoint = (useOr ? 0.0 : 1.0);
    1986             : 
    1987          18 :         foreach(l, arrayexpr->elements)
    1988             :         {
    1989          12 :             Node       *elem = (Node *) lfirst(l);
    1990             :             List       *args;
    1991             :             Selectivity s2;
    1992             : 
    1993             :             /*
    1994             :              * Theoretically, if elem isn't of nominal_element_type we should
    1995             :              * insert a RelabelType, but it seems unlikely that any operator
    1996             :              * estimation function would really care ...
    1997             :              */
    1998          12 :             args = list_make2(leftop, elem);
    1999          12 :             if (is_join_clause)
    2000           0 :                 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
    2001             :                                                       clause->inputcollid,
    2002             :                                                       PointerGetDatum(root),
    2003             :                                                       ObjectIdGetDatum(operator),
    2004             :                                                       PointerGetDatum(args),
    2005             :                                                       Int16GetDatum(jointype),
    2006             :                                                       PointerGetDatum(sjinfo)));
    2007             :             else
    2008          12 :                 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
    2009             :                                                       clause->inputcollid,
    2010             :                                                       PointerGetDatum(root),
    2011             :                                                       ObjectIdGetDatum(operator),
    2012             :                                                       PointerGetDatum(args),
    2013             :                                                       Int32GetDatum(varRelid)));
    2014             : 
    2015          12 :             if (useOr)
    2016             :             {
    2017          12 :                 s1 = s1 + s2 - s1 * s2;
    2018          12 :                 if (isEquality)
    2019          12 :                     s1disjoint += s2;
    2020             :             }
    2021             :             else
    2022             :             {
    2023           0 :                 s1 = s1 * s2;
    2024           0 :                 if (isInequality)
    2025           0 :                     s1disjoint += s2 - 1.0;
    2026             :             }
    2027             :         }
    2028             : 
    2029             :         /* accept disjoint-probability estimate if in range */
    2030           6 :         if ((useOr ? isEquality : isInequality) &&
    2031           6 :             s1disjoint >= 0.0 && s1disjoint <= 1.0)
    2032           6 :             s1 = s1disjoint;
    2033             :     }
    2034             :     else
    2035             :     {
    2036             :         CaseTestExpr *dummyexpr;
    2037             :         List       *args;
    2038             :         Selectivity s2;
    2039             :         int         i;
    2040             : 
    2041             :         /*
    2042             :          * We need a dummy rightop to pass to the operator selectivity
    2043             :          * routine.  It can be pretty much anything that doesn't look like a
    2044             :          * constant; CaseTestExpr is a convenient choice.
    2045             :          */
    2046         179 :         dummyexpr = makeNode(CaseTestExpr);
    2047         179 :         dummyexpr->typeId = nominal_element_type;
    2048         179 :         dummyexpr->typeMod = -1;
    2049         179 :         dummyexpr->collation = clause->inputcollid;
    2050         179 :         args = list_make2(leftop, dummyexpr);
    2051         179 :         if (is_join_clause)
    2052           0 :             s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
    2053             :                                                   clause->inputcollid,
    2054             :                                                   PointerGetDatum(root),
    2055             :                                                   ObjectIdGetDatum(operator),
    2056             :                                                   PointerGetDatum(args),
    2057             :                                                   Int16GetDatum(jointype),
    2058             :                                                   PointerGetDatum(sjinfo)));
    2059             :         else
    2060         179 :             s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
    2061             :                                                   clause->inputcollid,
    2062             :                                                   PointerGetDatum(root),
    2063             :                                                   ObjectIdGetDatum(operator),
    2064             :                                                   PointerGetDatum(args),
    2065             :                                                   Int32GetDatum(varRelid)));
    2066         179 :         s1 = useOr ? 0.0 : 1.0;
    2067             : 
    2068             :         /*
    2069             :          * Arbitrarily assume 10 elements in the eventual array value (see
    2070             :          * also estimate_array_length).  We don't risk an assumption of
    2071             :          * disjoint probabilities here.
    2072             :          */
    2073        1969 :         for (i = 0; i < 10; i++)
    2074             :         {
    2075        1790 :             if (useOr)
    2076        1790 :                 s1 = s1 + s2 - s1 * s2;
    2077             :             else
    2078           0 :                 s1 = s1 * s2;
    2079             :         }
    2080             :     }
    2081             : 
    2082             :     /* result should be in range, but make sure... */
    2083         490 :     CLAMP_PROBABILITY(s1);
    2084             : 
    2085         490 :     return s1;
    2086             : }
    2087             : 
    2088             : /*
    2089             :  * Estimate number of elements in the array yielded by an expression.
    2090             :  *
    2091             :  * It's important that this agree with scalararraysel.
    2092             :  */
    2093             : int
    2094        1337 : estimate_array_length(Node *arrayexpr)
    2095             : {
    2096             :     /* look through any binary-compatible relabeling of arrayexpr */
    2097        1337 :     arrayexpr = strip_array_coercion(arrayexpr);
    2098             : 
    2099        1337 :     if (arrayexpr && IsA(arrayexpr, Const))
    2100             :     {
    2101         760 :         Datum       arraydatum = ((Const *) arrayexpr)->constvalue;
    2102         760 :         bool        arrayisnull = ((Const *) arrayexpr)->constisnull;
    2103             :         ArrayType  *arrayval;
    2104             : 
    2105         760 :         if (arrayisnull)
    2106           3 :             return 0;
    2107         757 :         arrayval = DatumGetArrayTypeP(arraydatum);
    2108         757 :         return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
    2109             :     }
    2110         594 :     else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
    2111          17 :              !((ArrayExpr *) arrayexpr)->multidims)
    2112             :     {
    2113          17 :         return list_length(((ArrayExpr *) arrayexpr)->elements);
    2114             :     }
    2115             :     else
    2116             :     {
    2117             :         /* default guess --- see also scalararraysel */
    2118         560 :         return 10;
    2119             :     }
    2120             : }
    2121             : 
    2122             : /*
    2123             :  *      rowcomparesel       - Selectivity of RowCompareExpr Node.
    2124             :  *
    2125             :  * We estimate RowCompare selectivity by considering just the first (high
    2126             :  * order) columns, which makes it equivalent to an ordinary OpExpr.  While
    2127             :  * this estimate could be refined by considering additional columns, it
    2128             :  * seems unlikely that we could do a lot better without multi-column
    2129             :  * statistics.
    2130             :  */
    2131             : Selectivity
    2132           9 : rowcomparesel(PlannerInfo *root,
    2133             :               RowCompareExpr *clause,
    2134             :               int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
    2135             : {
    2136             :     Selectivity s1;
    2137           9 :     Oid         opno = linitial_oid(clause->opnos);
    2138           9 :     Oid         inputcollid = linitial_oid(clause->inputcollids);
    2139             :     List       *opargs;
    2140             :     bool        is_join_clause;
    2141             : 
    2142             :     /* Build equivalent arg list for single operator */
    2143           9 :     opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
    2144             : 
    2145             :     /*
    2146             :      * Decide if it's a join clause.  This should match clausesel.c's
    2147             :      * treat_as_join_clause(), except that we intentionally consider only the
    2148             :      * leading columns and not the rest of the clause.
    2149             :      */
    2150           9 :     if (varRelid != 0)
    2151             :     {
    2152             :         /*
    2153             :          * Caller is forcing restriction mode (eg, because we are examining an
    2154             :          * inner indexscan qual).
    2155             :          */
    2156           1 :         is_join_clause = false;
    2157             :     }
    2158           8 :     else if (sjinfo == NULL)
    2159             :     {
    2160             :         /*
    2161             :          * It must be a restriction clause, since it's being evaluated at a
    2162             :          * scan node.
    2163             :          */
    2164           6 :         is_join_clause = false;
    2165             :     }
    2166             :     else
    2167             :     {
    2168             :         /*
    2169             :          * Otherwise, it's a join if there's more than one relation used.
    2170             :          */
    2171           2 :         is_join_clause = (NumRelids((Node *) opargs) > 1);
    2172             :     }
    2173             : 
    2174           9 :     if (is_join_clause)
    2175             :     {
    2176             :         /* Estimate selectivity for a join clause. */
    2177           2 :         s1 = join_selectivity(root, opno,
    2178             :                               opargs,
    2179             :                               inputcollid,
    2180             :                               jointype,
    2181             :                               sjinfo);
    2182             :     }
    2183             :     else
    2184             :     {
    2185             :         /* Estimate selectivity for a restriction clause. */
    2186           7 :         s1 = restriction_selectivity(root, opno,
    2187             :                                      opargs,
    2188             :                                      inputcollid,
    2189             :                                      varRelid);
    2190             :     }
    2191             : 
    2192           9 :     return s1;
    2193             : }
    2194             : 
    2195             : /*
    2196             :  *      eqjoinsel       - Join selectivity of "="
    2197             :  */
    2198             : Datum
    2199        6061 : eqjoinsel(PG_FUNCTION_ARGS)
    2200             : {
    2201        6061 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
    2202        6061 :     Oid         operator = PG_GETARG_OID(1);
    2203        6061 :     List       *args = (List *) PG_GETARG_POINTER(2);
    2204             : 
    2205             : #ifdef NOT_USED
    2206             :     JoinType    jointype = (JoinType) PG_GETARG_INT16(3);
    2207             : #endif
    2208        6061 :     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
    2209             :     double      selec;
    2210             :     VariableStatData vardata1;
    2211             :     VariableStatData vardata2;
    2212             :     bool        join_is_reversed;
    2213             :     RelOptInfo *inner_rel;
    2214             : 
    2215        6061 :     get_join_variables(root, args, sjinfo,
    2216             :                        &vardata1, &vardata2, &join_is_reversed);
    2217             : 
    2218        6061 :     switch (sjinfo->jointype)
    2219             :     {
    2220             :         case JOIN_INNER:
    2221             :         case JOIN_LEFT:
    2222             :         case JOIN_FULL:
    2223        5755 :             selec = eqjoinsel_inner(operator, &vardata1, &vardata2);
    2224        5755 :             break;
    2225             :         case JOIN_SEMI:
    2226             :         case JOIN_ANTI:
    2227             : 
    2228             :             /*
    2229             :              * Look up the join's inner relation.  min_righthand is sufficient
    2230             :              * information because neither SEMI nor ANTI joins permit any
    2231             :              * reassociation into or out of their RHS, so the righthand will
    2232             :              * always be exactly that set of rels.
    2233             :              */
    2234         306 :             inner_rel = find_join_input_rel(root, sjinfo->min_righthand);
    2235             : 
    2236         306 :             if (!join_is_reversed)
    2237          64 :                 selec = eqjoinsel_semi(operator, &vardata1, &vardata2,
    2238             :                                        inner_rel);
    2239             :             else
    2240         242 :                 selec = eqjoinsel_semi(get_commutator(operator),
    2241             :                                        &vardata2, &vardata1,
    2242             :                                        inner_rel);
    2243         306 :             break;
    2244             :         default:
    2245             :             /* other values not expected here */
    2246           0 :             elog(ERROR, "unrecognized join type: %d",
    2247             :                  (int) sjinfo->jointype);
    2248             :             selec = 0;          /* keep compiler quiet */
    2249             :             break;
    2250             :     }
    2251             : 
    2252        6061 :     ReleaseVariableStats(vardata1);
    2253        6061 :     ReleaseVariableStats(vardata2);
    2254             : 
    2255        6061 :     CLAMP_PROBABILITY(selec);
    2256             : 
    2257        6061 :     PG_RETURN_FLOAT8((float8) selec);
    2258             : }
    2259             : 
    2260             : /*
    2261             :  * eqjoinsel_inner --- eqjoinsel for normal inner join
    2262             :  *
    2263             :  * We also use this for LEFT/FULL outer joins; it's not presently clear
    2264             :  * that it's worth trying to distinguish them here.
    2265             :  */
    2266             : static double
    2267        5755 : eqjoinsel_inner(Oid operator,
    2268             :                 VariableStatData *vardata1, VariableStatData *vardata2)
    2269             : {
    2270             :     double      selec;
    2271             :     double      nd1;
    2272             :     double      nd2;
    2273             :     bool        isdefault1;
    2274             :     bool        isdefault2;
    2275             :     Oid         opfuncoid;
    2276        5755 :     Form_pg_statistic stats1 = NULL;
    2277        5755 :     Form_pg_statistic stats2 = NULL;
    2278        5755 :     bool        have_mcvs1 = false;
    2279        5755 :     bool        have_mcvs2 = false;
    2280             :     AttStatsSlot sslot1;
    2281             :     AttStatsSlot sslot2;
    2282             : 
    2283        5755 :     nd1 = get_variable_numdistinct(vardata1, &isdefault1);
    2284        5755 :     nd2 = get_variable_numdistinct(vardata2, &isdefault2);
    2285             : 
    2286        5755 :     opfuncoid = get_opcode(operator);
    2287             : 
    2288        5755 :     memset(&sslot1, 0, sizeof(sslot1));
    2289        5755 :     memset(&sslot2, 0, sizeof(sslot2));
    2290             : 
    2291        5755 :     if (HeapTupleIsValid(vardata1->statsTuple))
    2292             :     {
    2293             :         /* note we allow use of nullfrac regardless of security check */
    2294        1877 :         stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
    2295        1877 :         if (statistic_proc_security_check(vardata1, opfuncoid))
    2296        1877 :             have_mcvs1 = get_attstatsslot(&sslot1, vardata1->statsTuple,
    2297             :                                           STATISTIC_KIND_MCV, InvalidOid,
    2298             :                                           ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
    2299             :     }
    2300             : 
    2301        5755 :     if (HeapTupleIsValid(vardata2->statsTuple))
    2302             :     {
    2303             :         /* note we allow use of nullfrac regardless of security check */
    2304        2177 :         stats2 = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple);
    2305        2177 :         if (statistic_proc_security_check(vardata2, opfuncoid))
    2306        2177 :             have_mcvs2 = get_attstatsslot(&sslot2, vardata2->statsTuple,
    2307             :                                           STATISTIC_KIND_MCV, InvalidOid,
    2308             :                                           ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
    2309             :     }
    2310             : 
    2311        5755 :     if (have_mcvs1 && have_mcvs2)
    2312         158 :     {
    2313             :         /*
    2314             :          * We have most-common-value lists for both relations.  Run through
    2315             :          * the lists to see which MCVs actually join to each other with the
    2316             :          * given operator.  This allows us to determine the exact join
    2317             :          * selectivity for the portion of the relations represented by the MCV
    2318             :          * lists.  We still have to estimate for the remaining population, but
    2319             :          * in a skewed distribution this gives us a big leg up in accuracy.
    2320             :          * For motivation see the analysis in Y. Ioannidis and S.
    2321             :          * Christodoulakis, "On the propagation of errors in the size of join
    2322             :          * results", Technical Report 1018, Computer Science Dept., University
    2323             :          * of Wisconsin, Madison, March 1991 (available from ftp.cs.wisc.edu).
    2324             :          */
    2325             :         FmgrInfo    eqproc;
    2326             :         bool       *hasmatch1;
    2327             :         bool       *hasmatch2;
    2328         158 :         double      nullfrac1 = stats1->stanullfrac;
    2329         158 :         double      nullfrac2 = stats2->stanullfrac;
    2330             :         double      matchprodfreq,
    2331             :                     matchfreq1,
    2332             :                     matchfreq2,
    2333             :                     unmatchfreq1,
    2334             :                     unmatchfreq2,
    2335             :                     otherfreq1,
    2336             :                     otherfreq2,
    2337             :                     totalsel1,
    2338             :                     totalsel2;
    2339             :         int         i,
    2340             :                     nmatches;
    2341             : 
    2342         158 :         fmgr_info(opfuncoid, &eqproc);
    2343         158 :         hasmatch1 = (bool *) palloc0(sslot1.nvalues * sizeof(bool));
    2344         158 :         hasmatch2 = (bool *) palloc0(sslot2.nvalues * sizeof(bool));
    2345             : 
    2346             :         /*
    2347             :          * Note we assume that each MCV will match at most one member of the
    2348             :          * other MCV list.  If the operator isn't really equality, there could
    2349             :          * be multiple matches --- but we don't look for them, both for speed
    2350             :          * and because the math wouldn't add up...
    2351             :          */
    2352         158 :         matchprodfreq = 0.0;
    2353         158 :         nmatches = 0;
    2354        4096 :         for (i = 0; i < sslot1.nvalues; i++)
    2355             :         {
    2356             :             int         j;
    2357             : 
    2358      145988 :             for (j = 0; j < sslot2.nvalues; j++)
    2359             :             {
    2360      145554 :                 if (hasmatch2[j])
    2361      131606 :                     continue;
    2362       13948 :                 if (DatumGetBool(FunctionCall2Coll(&eqproc,
    2363             :                                                    DEFAULT_COLLATION_OID,
    2364             :                                                    sslot1.values[i],
    2365             :                                                    sslot2.values[j])))
    2366             :                 {
    2367        3504 :                     hasmatch1[i] = hasmatch2[j] = true;
    2368        3504 :                     matchprodfreq += sslot1.numbers[i] * sslot2.numbers[j];
    2369        3504 :                     nmatches++;
    2370        3504 :                     break;
    2371             :                 }
    2372             :             }
    2373             :         }
    2374         158 :         CLAMP_PROBABILITY(matchprodfreq);
    2375             :         /* Sum up frequencies of matched and unmatched MCVs */
    2376         158 :         matchfreq1 = unmatchfreq1 = 0.0;
    2377        4096 :         for (i = 0; i < sslot1.nvalues; i++)
    2378             :         {
    2379        3938 :             if (hasmatch1[i])
    2380        3504 :                 matchfreq1 += sslot1.numbers[i];
    2381             :             else
    2382         434 :                 unmatchfreq1 += sslot1.numbers[i];
    2383             :         }
    2384         158 :         CLAMP_PROBABILITY(matchfreq1);
    2385         158 :         CLAMP_PROBABILITY(unmatchfreq1);
    2386         158 :         matchfreq2 = unmatchfreq2 = 0.0;
    2387        4994 :         for (i = 0; i < sslot2.nvalues; i++)
    2388             :         {
    2389        4836 :             if (hasmatch2[i])
    2390        3504 :                 matchfreq2 += sslot2.numbers[i];
    2391             :             else
    2392        1332 :                 unmatchfreq2 += sslot2.numbers[i];
    2393             :         }
    2394         158 :         CLAMP_PROBABILITY(matchfreq2);
    2395         158 :         CLAMP_PROBABILITY(unmatchfreq2);
    2396         158 :         pfree(hasmatch1);
    2397         158 :         pfree(hasmatch2);
    2398             : 
    2399             :         /*
    2400             :          * Compute total frequency of non-null values that are not in the MCV
    2401             :          * lists.
    2402             :          */
    2403         158 :         otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
    2404         158 :         otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
    2405         158 :         CLAMP_PROBABILITY(otherfreq1);
    2406         158 :         CLAMP_PROBABILITY(otherfreq2);
    2407             : 
    2408             :         /*
    2409             :          * We can estimate the total selectivity from the point of view of
    2410             :          * relation 1 as: the known selectivity for matched MCVs, plus
    2411             :          * unmatched MCVs that are assumed to match against random members of
    2412             :          * relation 2's non-MCV population, plus non-MCV values that are
    2413             :          * assumed to match against random members of relation 2's unmatched
    2414             :          * MCVs plus non-MCV values.
    2415             :          */
    2416         158 :         totalsel1 = matchprodfreq;
    2417         158 :         if (nd2 > sslot2.nvalues)
    2418         105 :             totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - sslot2.nvalues);
    2419         158 :         if (nd2 > nmatches)
    2420         105 :             totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
    2421             :                 (nd2 - nmatches);
    2422             :         /* Same estimate from the point of view of relation 2. */
    2423         158 :         totalsel2 = matchprodfreq;
    2424         158 :         if (nd1 > sslot1.nvalues)
    2425         103 :             totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - sslot1.nvalues);
    2426         158 :         if (nd1 > nmatches)
    2427         105 :             totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
    2428             :                 (nd1 - nmatches);
    2429             : 
    2430             :         /*
    2431             :          * Use the smaller of the two estimates.  This can be justified in
    2432             :          * essentially the same terms as given below for the no-stats case: to
    2433             :          * a first approximation, we are estimating from the point of view of
    2434             :          * the relation with smaller nd.
    2435             :          */
    2436         158 :         selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
    2437             :     }
    2438             :     else
    2439             :     {
    2440             :         /*
    2441             :          * We do not have MCV lists for both sides.  Estimate the join
    2442             :          * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). This
    2443             :          * is plausible if we assume that the join operator is strict and the
    2444             :          * non-null values are about equally distributed: a given non-null
    2445             :          * tuple of rel1 will join to either zero or N2*(1-nullfrac2)/nd2 rows
    2446             :          * of rel2, so total join rows are at most
    2447             :          * N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join selectivity of
    2448             :          * not more than (1-nullfrac1)*(1-nullfrac2)/nd2. By the same logic it
    2449             :          * is not more than (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression
    2450             :          * with MIN() is an upper bound.  Using the MIN() means we estimate
    2451             :          * from the point of view of the relation with smaller nd (since the
    2452             :          * larger nd is determining the MIN).  It is reasonable to assume that
    2453             :          * most tuples in this rel will have join partners, so the bound is
    2454             :          * probably reasonably tight and should be taken as-is.
    2455             :          *
    2456             :          * XXX Can we be smarter if we have an MCV list for just one side? It
    2457             :          * seems that if we assume equal distribution for the other side, we
    2458             :          * end up with the same answer anyway.
    2459             :          */
    2460        5597 :         double      nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
    2461        5597 :         double      nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
    2462             : 
    2463        5597 :         selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
    2464        5597 :         if (nd1 > nd2)
    2465        2947 :             selec /= nd1;
    2466             :         else
    2467        2650 :             selec /= nd2;
    2468             :     }
    2469             : 
    2470        5755 :     free_attstatsslot(&sslot1);
    2471        5755 :     free_attstatsslot(&sslot2);
    2472             : 
    2473        5755 :     return selec;
    2474             : }
    2475             : 
    2476             : /*
    2477             :  * eqjoinsel_semi --- eqjoinsel for semi join
    2478             :  *
    2479             :  * (Also used for anti join, which we are supposed to estimate the same way.)
    2480             :  * Caller has ensured that vardata1 is the LHS variable.
    2481             :  * Unlike eqjoinsel_inner, we have to cope with operator being InvalidOid.
    2482             :  */
    2483             : static double
    2484         306 : eqjoinsel_semi(Oid operator,
    2485             :                VariableStatData *vardata1, VariableStatData *vardata2,
    2486             :                RelOptInfo *inner_rel)
    2487             : {
    2488             :     double      selec;
    2489             :     double      nd1;
    2490             :     double      nd2;
    2491             :     bool        isdefault1;
    2492             :     bool        isdefault2;
    2493             :     Oid         opfuncoid;
    2494         306 :     Form_pg_statistic stats1 = NULL;
    2495         306 :     bool        have_mcvs1 = false;
    2496         306 :     bool        have_mcvs2 = false;
    2497             :     AttStatsSlot sslot1;
    2498             :     AttStatsSlot sslot2;
    2499             : 
    2500         306 :     nd1 = get_variable_numdistinct(vardata1, &isdefault1);
    2501         306 :     nd2 = get_variable_numdistinct(vardata2, &isdefault2);
    2502             : 
    2503         306 :     opfuncoid = OidIsValid(operator) ? get_opcode(operator) : InvalidOid;
    2504             : 
    2505         306 :     memset(&sslot1, 0, sizeof(sslot1));
    2506         306 :     memset(&sslot2, 0, sizeof(sslot2));
    2507             : 
    2508             :     /*
    2509             :      * We clamp nd2 to be not more than what we estimate the inner relation's
    2510             :      * size to be.  This is intuitively somewhat reasonable since obviously
    2511             :      * there can't be more than that many distinct values coming from the
    2512             :      * inner rel.  The reason for the asymmetry (ie, that we don't clamp nd1
    2513             :      * likewise) is that this is the only pathway by which restriction clauses
    2514             :      * applied to the inner rel will affect the join result size estimate,
    2515             :      * since set_joinrel_size_estimates will multiply SEMI/ANTI selectivity by
    2516             :      * only the outer rel's size.  If we clamped nd1 we'd be double-counting
    2517             :      * the selectivity of outer-rel restrictions.
    2518             :      *
    2519             :      * We can apply this clamping both with respect to the base relation from
    2520             :      * which the join variable comes (if there is just one), and to the
    2521             :      * immediate inner input relation of the current join.
    2522             :      *
    2523             :      * If we clamp, we can treat nd2 as being a non-default estimate; it's not
    2524             :      * great, maybe, but it didn't come out of nowhere either.  This is most
    2525             :      * helpful when the inner relation is empty and consequently has no stats.
    2526             :      */
    2527         306 :     if (vardata2->rel)
    2528             :     {
    2529         306 :         if (nd2 >= vardata2->rel->rows)
    2530             :         {
    2531         256 :             nd2 = vardata2->rel->rows;
    2532         256 :             isdefault2 = false;
    2533             :         }
    2534             :     }
    2535         306 :     if (nd2 >= inner_rel->rows)
    2536             :     {
    2537         254 :         nd2 = inner_rel->rows;
    2538         254 :         isdefault2 = false;
    2539             :     }
    2540             : 
    2541         306 :     if (HeapTupleIsValid(vardata1->statsTuple))
    2542             :     {
    2543             :         /* note we allow use of nullfrac regardless of security check */
    2544         155 :         stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
    2545         155 :         if (statistic_proc_security_check(vardata1, opfuncoid))
    2546         155 :             have_mcvs1 = get_attstatsslot(&sslot1, vardata1->statsTuple,
    2547             :                                           STATISTIC_KIND_MCV, InvalidOid,
    2548             :                                           ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
    2549             :     }
    2550             : 
    2551         353 :     if (HeapTupleIsValid(vardata2->statsTuple) &&
    2552          47 :         statistic_proc_security_check(vardata2, opfuncoid))
    2553             :     {
    2554          47 :         have_mcvs2 = get_attstatsslot(&sslot2, vardata2->statsTuple,
    2555             :                                       STATISTIC_KIND_MCV, InvalidOid,
    2556             :                                       ATTSTATSSLOT_VALUES);
    2557             :         /* note: currently don't need stanumbers from RHS */
    2558             :     }
    2559             : 
    2560         306 :     if (have_mcvs1 && have_mcvs2 && OidIsValid(operator))
    2561           7 :     {
    2562             :         /*
    2563             :          * We have most-common-value lists for both relations.  Run through
    2564             :          * the lists to see which MCVs actually join to each other with the
    2565             :          * given operator.  This allows us to determine the exact join
    2566             :          * selectivity for the portion of the relations represented by the MCV
    2567             :          * lists.  We still have to estimate for the remaining population, but
    2568             :          * in a skewed distribution this gives us a big leg up in accuracy.
    2569             :          */
    2570             :         FmgrInfo    eqproc;
    2571             :         bool       *hasmatch1;
    2572             :         bool       *hasmatch2;
    2573           7 :         double      nullfrac1 = stats1->stanullfrac;
    2574             :         double      matchfreq1,
    2575             :                     uncertainfrac,
    2576             :                     uncertain;
    2577             :         int         i,
    2578             :                     nmatches,
    2579             :                     clamped_nvalues2;
    2580             : 
    2581             :         /*
    2582             :          * The clamping above could have resulted in nd2 being less than
    2583             :          * sslot2.nvalues; in which case, we assume that precisely the nd2
    2584             :          * most common values in the relation will appear in the join input,
    2585             :          * and so compare to only the first nd2 members of the MCV list.  Of
    2586             :          * course this is frequently wrong, but it's the best bet we can make.
    2587             :          */
    2588           7 :         clamped_nvalues2 = Min(sslot2.nvalues, nd2);
    2589             : 
    2590           7 :         fmgr_info(opfuncoid, &eqproc);
    2591           7 :         hasmatch1 = (bool *) palloc0(sslot1.nvalues * sizeof(bool));
    2592           7 :         hasmatch2 = (bool *) palloc0(clamped_nvalues2 * sizeof(bool));
    2593             : 
    2594             :         /*
    2595             :          * Note we assume that each MCV will match at most one member of the
    2596             :          * other MCV list.  If the operator isn't really equality, there could
    2597             :          * be multiple matches --- but we don't look for them, both for speed
    2598             :          * and because the math wouldn't add up...
    2599             :          */
    2600           7 :         nmatches = 0;
    2601         283 :         for (i = 0; i < sslot1.nvalues; i++)
    2602             :         {
    2603             :             int         j;
    2604             : 
    2605        8352 :             for (j = 0; j < clamped_nvalues2; j++)
    2606             :             {
    2607        8311 :                 if (hasmatch2[j])
    2608        6832 :                     continue;
    2609        1479 :                 if (DatumGetBool(FunctionCall2Coll(&eqproc,
    2610             :                                                    DEFAULT_COLLATION_OID,
    2611             :                                                    sslot1.values[i],
    2612             :                                                    sslot2.values[j])))
    2613             :                 {
    2614         235 :                     hasmatch1[i] = hasmatch2[j] = true;
    2615         235 :                     nmatches++;
    2616         235 :                     break;
    2617             :                 }
    2618             :             }
    2619             :         }
    2620             :         /* Sum up frequencies of matched MCVs */
    2621           7 :         matchfreq1 = 0.0;
    2622         283 :         for (i = 0; i < sslot1.nvalues; i++)
    2623             :         {
    2624         276 :             if (hasmatch1[i])
    2625         235 :                 matchfreq1 += sslot1.numbers[i];
    2626             :         }
    2627           7 :         CLAMP_PROBABILITY(matchfreq1);
    2628           7 :         pfree(hasmatch1);
    2629           7 :         pfree(hasmatch2);
    2630             : 
    2631             :         /*
    2632             :          * Now we need to estimate the fraction of relation 1 that has at
    2633             :          * least one join partner.  We know for certain that the matched MCVs
    2634             :          * do, so that gives us a lower bound, but we're really in the dark
    2635             :          * about everything else.  Our crude approach is: if nd1 <= nd2 then
    2636             :          * assume all non-null rel1 rows have join partners, else assume for
    2637             :          * the uncertain rows that a fraction nd2/nd1 have join partners. We
    2638             :          * can discount the known-matched MCVs from the distinct-values counts
    2639             :          * before doing the division.
    2640             :          *
    2641             :          * Crude as the above is, it's completely useless if we don't have
    2642             :          * reliable ndistinct values for both sides.  Hence, if either nd1 or
    2643             :          * nd2 is default, punt and assume half of the uncertain rows have
    2644             :          * join partners.
    2645             :          */
    2646           7 :         if (!isdefault1 && !isdefault2)
    2647             :         {
    2648           7 :             nd1 -= nmatches;
    2649           7 :             nd2 -= nmatches;
    2650          14 :             if (nd1 <= nd2 || nd2 < 0)
    2651           4 :                 uncertainfrac = 1.0;
    2652             :             else
    2653           3 :                 uncertainfrac = nd2 / nd1;
    2654             :         }
    2655             :         else
    2656           0 :             uncertainfrac = 0.5;
    2657           7 :         uncertain = 1.0 - matchfreq1 - nullfrac1;
    2658           7 :         CLAMP_PROBABILITY(uncertain);
    2659           7 :         selec = matchfreq1 + uncertainfrac * uncertain;
    2660             :     }
    2661             :     else
    2662             :     {
    2663             :         /*
    2664             :          * Without MCV lists for both sides, we can only use the heuristic
    2665             :          * about nd1 vs nd2.
    2666             :          */
    2667         299 :         double      nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
    2668             : 
    2669         299 :         if (!isdefault1 && !isdefault2)
    2670             :         {
    2671         394 :             if (nd1 <= nd2 || nd2 < 0)
    2672         166 :                 selec = 1.0 - nullfrac1;
    2673             :             else
    2674          31 :                 selec = (nd2 / nd1) * (1.0 - nullfrac1);
    2675             :         }
    2676             :         else
    2677         102 :             selec = 0.5 * (1.0 - nullfrac1);
    2678             :     }
    2679             : 
    2680         306 :     free_attstatsslot(&sslot1);
    2681         306 :     free_attstatsslot(&sslot2);
    2682             : 
    2683         306 :     return selec;
    2684             : }
    2685             : 
    2686             : /*
    2687             :  *      neqjoinsel      - Join selectivity of "!="
    2688             :  */
    2689             : Datum
    2690         153 : neqjoinsel(PG_FUNCTION_ARGS)
    2691             : {
    2692         153 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
    2693         153 :     Oid         operator = PG_GETARG_OID(1);
    2694         153 :     List       *args = (List *) PG_GETARG_POINTER(2);
    2695         153 :     JoinType    jointype = (JoinType) PG_GETARG_INT16(3);
    2696         153 :     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
    2697             :     Oid         eqop;
    2698             :     float8      result;
    2699             : 
    2700             :     /*
    2701             :      * We want 1 - eqjoinsel() where the equality operator is the one
    2702             :      * associated with this != operator, that is, its negator.
    2703             :      */
    2704         153 :     eqop = get_negator(operator);
    2705         153 :     if (eqop)
    2706             :     {
    2707         153 :         result = DatumGetFloat8(DirectFunctionCall5(eqjoinsel,
    2708             :                                                     PointerGetDatum(root),
    2709             :                                                     ObjectIdGetDatum(eqop),
    2710             :                                                     PointerGetDatum(args),
    2711             :                                                     Int16GetDatum(jointype),
    2712             :                                                     PointerGetDatum(sjinfo)));
    2713             :     }
    2714             :     else
    2715             :     {
    2716             :         /* Use default selectivity (should we raise an error instead?) */
    2717           0 :         result = DEFAULT_EQ_SEL;
    2718             :     }
    2719         153 :     result = 1.0 - result;
    2720         153 :     PG_RETURN_FLOAT8(result);
    2721             : }
    2722             : 
    2723             : /*
    2724             :  *      scalarltjoinsel - Join selectivity of "<" and "<=" for scalars
    2725             :  */
    2726             : Datum
    2727          38 : scalarltjoinsel(PG_FUNCTION_ARGS)
    2728             : {
    2729          38 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    2730             : }
    2731             : 
    2732             : /*
    2733             :  *      scalargtjoinsel - Join selectivity of ">" and ">=" for scalars
    2734             :  */
    2735             : Datum
    2736          20 : scalargtjoinsel(PG_FUNCTION_ARGS)
    2737             : {
    2738          20 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
    2739             : }
    2740             : 
    2741             : /*
    2742             :  * patternjoinsel       - Generic code for pattern-match join selectivity.
    2743             :  */
    2744             : static double
    2745           0 : patternjoinsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
    2746             : {
    2747             :     /* For the moment we just punt. */
    2748           0 :     return negate ? (1.0 - DEFAULT_MATCH_SEL) : DEFAULT_MATCH_SEL;
    2749             : }
    2750             : 
    2751             : /*
    2752             :  *      regexeqjoinsel  - Join selectivity of regular-expression pattern match.
    2753             :  */
    2754             : Datum
    2755           0 : regexeqjoinsel(PG_FUNCTION_ARGS)
    2756             : {
    2757           0 :     PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex, false));
    2758             : }
    2759             : 
    2760             : /*
    2761             :  *      icregexeqjoinsel    - Join selectivity of case-insensitive regex match.
    2762             :  */
    2763             : Datum
    2764           0 : icregexeqjoinsel(PG_FUNCTION_ARGS)
    2765             : {
    2766           0 :     PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex_IC, false));
    2767             : }
    2768             : 
    2769             : /*
    2770             :  *      likejoinsel         - Join selectivity of LIKE pattern match.
    2771             :  */
    2772             : Datum
    2773           0 : likejoinsel(PG_FUNCTION_ARGS)
    2774             : {
    2775           0 :     PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like, false));
    2776             : }
    2777             : 
    2778             : /*
    2779             :  *      iclikejoinsel           - Join selectivity of ILIKE pattern match.
    2780             :  */
    2781             : Datum
    2782           0 : iclikejoinsel(PG_FUNCTION_ARGS)
    2783             : {
    2784           0 :     PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like_IC, false));
    2785             : }
    2786             : 
    2787             : /*
    2788             :  *      regexnejoinsel  - Join selectivity of regex non-match.
    2789             :  */
    2790             : Datum
    2791           0 : regexnejoinsel(PG_FUNCTION_ARGS)
    2792             : {
    2793           0 :     PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex, true));
    2794             : }
    2795             : 
    2796             : /*
    2797             :  *      icregexnejoinsel    - Join selectivity of case-insensitive regex non-match.
    2798             :  */
    2799             : Datum
    2800           0 : icregexnejoinsel(PG_FUNCTION_ARGS)
    2801             : {
    2802           0 :     PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex_IC, true));
    2803             : }
    2804             : 
    2805             : /*
    2806             :  *      nlikejoinsel        - Join selectivity of LIKE pattern non-match.
    2807             :  */
    2808             : Datum
    2809           0 : nlikejoinsel(PG_FUNCTION_ARGS)
    2810             : {
    2811           0 :     PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like, true));
    2812             : }
    2813             : 
    2814             : /*
    2815             :  *      icnlikejoinsel      - Join selectivity of ILIKE pattern non-match.
    2816             :  */
    2817             : Datum
    2818           0 : icnlikejoinsel(PG_FUNCTION_ARGS)
    2819             : {
    2820           0 :     PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like_IC, true));
    2821             : }
    2822             : 
    2823             : /*
    2824             :  * mergejoinscansel         - Scan selectivity of merge join.
    2825             :  *
    2826             :  * A merge join will stop as soon as it exhausts either input stream.
    2827             :  * Therefore, if we can estimate the ranges of both input variables,
    2828             :  * we can estimate how much of the input will actually be read.  This
    2829             :  * can have a considerable impact on the cost when using indexscans.
    2830             :  *
    2831             :  * Also, we can estimate how much of each input has to be read before the
    2832             :  * first join pair is found, which will affect the join's startup time.
    2833             :  *
    2834             :  * clause should be a clause already known to be mergejoinable.  opfamily,
    2835             :  * strategy, and nulls_first specify the sort ordering being used.
    2836             :  *
    2837             :  * The outputs are:
    2838             :  *      *leftstart is set to the fraction of the left-hand variable expected
    2839             :  *       to be scanned before the first join pair is found (0 to 1).
    2840             :  *      *leftend is set to the fraction of the left-hand variable expected
    2841             :  *       to be scanned before the join terminates (0 to 1).
    2842             :  *      *rightstart, *rightend similarly for the right-hand variable.
    2843             :  */
    2844             : void
    2845        3186 : mergejoinscansel(PlannerInfo *root, Node *clause,
    2846             :                  Oid opfamily, int strategy, bool nulls_first,
    2847             :                  Selectivity *leftstart, Selectivity *leftend,
    2848             :                  Selectivity *rightstart, Selectivity *rightend)
    2849             : {
    2850             :     Node       *left,
    2851             :                *right;
    2852             :     VariableStatData leftvar,
    2853             :                 rightvar;
    2854             :     int         op_strategy;
    2855             :     Oid         op_lefttype;
    2856             :     Oid         op_righttype;
    2857             :     Oid         opno,
    2858             :                 lsortop,
    2859             :                 rsortop,
    2860             :                 lstatop,
    2861             :                 rstatop,
    2862             :                 ltop,
    2863             :                 leop,
    2864             :                 revltop,
    2865             :                 revleop;
    2866             :     bool        isgt;
    2867             :     Datum       leftmin,
    2868             :                 leftmax,
    2869             :                 rightmin,
    2870             :                 rightmax;
    2871             :     double      selec;
    2872             : 
    2873             :     /* Set default results if we can't figure anything out. */
    2874             :     /* XXX should default "start" fraction be a bit more than 0? */
    2875        3186 :     *leftstart = *rightstart = 0.0;
    2876        3186 :     *leftend = *rightend = 1.0;
    2877             : 
    2878             :     /* Deconstruct the merge clause */
    2879        3186 :     if (!is_opclause(clause))
    2880           0 :         return;                 /* shouldn't happen */
    2881        3186 :     opno = ((OpExpr *) clause)->opno;
    2882        3186 :     left = get_leftop((Expr *) clause);
    2883        3186 :     right = get_rightop((Expr *) clause);
    2884        3186 :     if (!right)
    2885           0 :         return;                 /* shouldn't happen */
    2886             : 
    2887             :     /* Look for stats for the inputs */
    2888        3186 :     examine_variable(root, left, 0, &leftvar);
    2889        3186 :     examine_variable(root, right, 0, &rightvar);
    2890             : 
    2891             :     /* Extract the operator's declared left/right datatypes */
    2892        3186 :     get_op_opfamily_properties(opno, opfamily, false,
    2893             :                                &op_strategy,
    2894             :                                &op_lefttype,
    2895             :                                &op_righttype);
    2896        3186 :     Assert(op_strategy == BTEqualStrategyNumber);
    2897             : 
    2898             :     /*
    2899             :      * Look up the various operators we need.  If we don't find them all, it
    2900             :      * probably means the opfamily is broken, but we just fail silently.
    2901             :      *
    2902             :      * Note: we expect that pg_statistic histograms will be sorted by the '<'
    2903             :      * operator, regardless of which sort direction we are considering.
    2904             :      */
    2905        3186 :     switch (strategy)
    2906             :     {
    2907             :         case BTLessStrategyNumber:
    2908        3186 :             isgt = false;
    2909        3186 :             if (op_lefttype == op_righttype)
    2910             :             {
    2911             :                 /* easy case */
    2912        3111 :                 ltop = get_opfamily_member(opfamily,
    2913             :                                            op_lefttype, op_righttype,
    2914             :                                            BTLessStrategyNumber);
    2915        3111 :                 leop = get_opfamily_member(opfamily,
    2916             :                                            op_lefttype, op_righttype,
    2917             :                                            BTLessEqualStrategyNumber);
    2918        3111 :                 lsortop = ltop;
    2919        3111 :                 rsortop = ltop;
    2920        3111 :                 lstatop = lsortop;
    2921        3111 :                 rstatop = rsortop;
    2922        3111 :                 revltop = ltop;
    2923        3111 :                 revleop = leop;
    2924             :             }
    2925             :             else
    2926             :             {
    2927          75 :                 ltop = get_opfamily_member(opfamily,
    2928             :                                            op_lefttype, op_righttype,
    2929             :                                            BTLessStrategyNumber);
    2930          75 :                 leop = get_opfamily_member(opfamily,
    2931             :                                            op_lefttype, op_righttype,
    2932             :                                            BTLessEqualStrategyNumber);
    2933          75 :                 lsortop = get_opfamily_member(opfamily,
    2934             :                                               op_lefttype, op_lefttype,
    2935             :                                               BTLessStrategyNumber);
    2936          75 :                 rsortop = get_opfamily_member(opfamily,
    2937             :                                               op_righttype, op_righttype,
    2938             :                                               BTLessStrategyNumber);
    2939          75 :                 lstatop = lsortop;
    2940          75 :                 rstatop = rsortop;
    2941          75 :                 revltop = get_opfamily_member(opfamily,
    2942             :                                               op_righttype, op_lefttype,
    2943             :                                               BTLessStrategyNumber);
    2944          75 :                 revleop = get_opfamily_member(opfamily,
    2945             :                                               op_righttype, op_lefttype,
    2946             :                                               BTLessEqualStrategyNumber);
    2947             :             }
    2948        3186 :             break;
    2949             :         case BTGreaterStrategyNumber:
    2950             :             /* descending-order case */
    2951           0 :             isgt = true;
    2952           0 :             if (op_lefttype == op_righttype)
    2953             :             {
    2954             :                 /* easy case */
    2955           0 :                 ltop = get_opfamily_member(opfamily,
    2956             :                                            op_lefttype, op_righttype,
    2957             :                                            BTGreaterStrategyNumber);
    2958           0 :                 leop = get_opfamily_member(opfamily,
    2959             :                                            op_lefttype, op_righttype,
    2960             :                                            BTGreaterEqualStrategyNumber);
    2961           0 :                 lsortop = ltop;
    2962           0 :                 rsortop = ltop;
    2963           0 :                 lstatop = get_opfamily_member(opfamily,
    2964             :                                               op_lefttype, op_lefttype,
    2965             :                                               BTLessStrategyNumber);
    2966           0 :                 rstatop = lstatop;
    2967           0 :                 revltop = ltop;
    2968           0 :                 revleop = leop;
    2969             :             }
    2970             :             else
    2971             :             {
    2972           0 :                 ltop = get_opfamily_member(opfamily,
    2973             :                                            op_lefttype, op_righttype,
    2974             :                                            BTGreaterStrategyNumber);
    2975           0 :                 leop = get_opfamily_member(opfamily,
    2976             :                                            op_lefttype, op_righttype,
    2977             :                                            BTGreaterEqualStrategyNumber);
    2978           0 :                 lsortop = get_opfamily_member(opfamily,
    2979             :                                               op_lefttype, op_lefttype,
    2980             :                                               BTGreaterStrategyNumber);
    2981           0 :                 rsortop = get_opfamily_member(opfamily,
    2982             :                                               op_righttype, op_righttype,
    2983             :                                               BTGreaterStrategyNumber);
    2984           0 :                 lstatop = get_opfamily_member(opfamily,
    2985             :                                               op_lefttype, op_lefttype,
    2986             :                                               BTLessStrategyNumber);
    2987           0 :                 rstatop = get_opfamily_member(opfamily,
    2988             :                                               op_righttype, op_righttype,
    2989             :                                               BTLessStrategyNumber);
    2990           0 :                 revltop = get_opfamily_member(opfamily,
    2991             :                                               op_righttype, op_lefttype,
    2992             :                                               BTGreaterStrategyNumber);
    2993           0 :                 revleop = get_opfamily_member(opfamily,
    2994             :                                               op_righttype, op_lefttype,
    2995             :                                               BTGreaterEqualStrategyNumber);
    2996             :             }
    2997           0 :             break;
    2998             :         default:
    2999           0 :             goto fail;          /* shouldn't get here */
    3000             :     }
    3001             : 
    3002        3186 :     if (!OidIsValid(lsortop) ||
    3003        3186 :         !OidIsValid(rsortop) ||
    3004        3186 :         !OidIsValid(lstatop) ||
    3005        3186 :         !OidIsValid(rstatop) ||
    3006        3184 :         !OidIsValid(ltop) ||
    3007        3184 :         !OidIsValid(leop) ||
    3008        3184 :         !OidIsValid(revltop) ||
    3009             :         !OidIsValid(revleop))
    3010             :         goto fail;              /* insufficient info in catalogs */
    3011             : 
    3012             :     /* Try to get ranges of both inputs */
    3013        3184 :     if (!isgt)
    3014             :     {
    3015        3184 :         if (!get_variable_range(root, &leftvar, lstatop,
    3016             :                                 &leftmin, &leftmax))
    3017        2214 :             goto fail;          /* no range available from stats */
    3018         970 :         if (!get_variable_range(root, &rightvar, rstatop,
    3019             :                                 &rightmin, &rightmax))
    3020         802 :             goto fail;          /* no range available from stats */
    3021             :     }
    3022             :     else
    3023             :     {
    3024             :         /* need to swap the max and min */
    3025           0 :         if (!get_variable_range(root, &leftvar, lstatop,
    3026             :                                 &leftmax, &leftmin))
    3027           0 :             goto fail;          /* no range available from stats */
    3028           0 :         if (!get_variable_range(root, &rightvar, rstatop,
    3029             :                                 &rightmax, &rightmin))
    3030           0 :             goto fail;          /* no range available from stats */
    3031             :     }
    3032             : 
    3033             :     /*
    3034             :      * Now, the fraction of the left variable that will be scanned is the
    3035             :      * fraction that's <= the right-side maximum value.  But only believe
    3036             :      * non-default estimates, else stick with our 1.0.
    3037             :      */
    3038         168 :     selec = scalarineqsel(root, leop, isgt, &leftvar,
    3039             :                           rightmax, op_righttype);
    3040         168 :     if (selec != DEFAULT_INEQ_SEL)
    3041         168 :         *leftend = selec;
    3042             : 
    3043             :     /* And similarly for the right variable. */
    3044         168 :     selec = scalarineqsel(root, revleop, isgt, &rightvar,
    3045             :                           leftmax, op_lefttype);
    3046         168 :     if (selec != DEFAULT_INEQ_SEL)
    3047         168 :         *rightend = selec;
    3048             : 
    3049             :     /*
    3050             :      * Only one of the two "end" fractions can really be less than 1.0;
    3051             :      * believe the smaller estimate and reset the other one to exactly 1.0. If
    3052             :      * we get exactly equal estimates (as can easily happen with self-joins),
    3053             :      * believe neither.
    3054             :      */
    3055         168 :     if (*leftend > *rightend)
    3056          76 :         *leftend = 1.0;
    3057          92 :     else if (*leftend < *rightend)
    3058          19 :         *rightend = 1.0;
    3059             :     else
    3060          73 :         *leftend = *rightend = 1.0;
    3061             : 
    3062             :     /*
    3063             :      * Also, the fraction of the left variable that will be scanned before the
    3064             :      * first join pair is found is the fraction that's < the right-side
    3065             :      * minimum value.  But only believe non-default estimates, else stick with
    3066             :      * our own default.
    3067             :      */
    3068         168 :     selec = scalarineqsel(root, ltop, isgt, &leftvar,
    3069             :                           rightmin, op_righttype);
    3070         168 :     if (selec != DEFAULT_INEQ_SEL)
    3071         168 :         *leftstart = selec;
    3072             : 
    3073             :     /* And similarly for the right variable. */
    3074         168 :     selec = scalarineqsel(root, revltop, isgt, &rightvar,
    3075             :                           leftmin, op_lefttype);
    3076         168 :     if (selec != DEFAULT_INEQ_SEL)
    3077         168 :         *rightstart = selec;
    3078             : 
    3079             :     /*
    3080             :      * Only one of the two "start" fractions can really be more than zero;
    3081             :      * believe the larger estimate and reset the other one to exactly 0.0. If
    3082             :      * we get exactly equal estimates (as can easily happen with self-joins),
    3083             :      * believe neither.
    3084             :      */
    3085         168 :     if (*leftstart < *rightstart)
    3086          40 :         *leftstart = 0.0;
    3087         128 :     else if (*leftstart > *rightstart)
    3088          46 :         *rightstart = 0.0;
    3089             :     else
    3090          82 :         *leftstart = *rightstart = 0.0;
    3091             : 
    3092             :     /*
    3093             :      * If the sort order is nulls-first, we're going to have to skip over any
    3094             :      * nulls too.  These would not have been counted by scalarineqsel, and we
    3095             :      * can safely add in this fraction regardless of whether we believe
    3096             :      * scalarineqsel's results or not.  But be sure to clamp the sum to 1.0!
    3097             :      */
    3098         168 :     if (nulls_first)
    3099             :     {
    3100             :         Form_pg_statistic stats;
    3101             : 
    3102           0 :         if (HeapTupleIsValid(leftvar.statsTuple))
    3103             :         {
    3104           0 :             stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
    3105           0 :             *leftstart += stats->stanullfrac;
    3106           0 :             CLAMP_PROBABILITY(*leftstart);
    3107           0 :             *leftend += stats->stanullfrac;
    3108           0 :             CLAMP_PROBABILITY(*leftend);
    3109             :         }
    3110           0 :         if (HeapTupleIsValid(rightvar.statsTuple))
    3111             :         {
    3112           0 :             stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
    3113           0 :             *rightstart += stats->stanullfrac;
    3114           0 :             CLAMP_PROBABILITY(*rightstart);
    3115           0 :             *rightend += stats->stanullfrac;
    3116           0 :             CLAMP_PROBABILITY(*rightend);
    3117             :         }
    3118             :     }
    3119             : 
    3120             :     /* Disbelieve start >= end, just in case that can happen */
    3121         168 :     if (*leftstart >= *leftend)
    3122             :     {
    3123          16 :         *leftstart = 0.0;
    3124          16 :         *leftend = 1.0;
    3125             :     }
    3126         168 :     if (*rightstart >= *rightend)
    3127             :     {
    3128          15 :         *rightstart = 0.0;
    3129          15 :         *rightend = 1.0;
    3130             :     }
    3131             : 
    3132             : fail:
    3133        3186 :     ReleaseVariableStats(leftvar);
    3134        3186 :     ReleaseVariableStats(rightvar);
    3135             : }
    3136             : 
    3137             : 
    3138             : /*
    3139             :  * Helper routine for estimate_num_groups: add an item to a list of
    3140             :  * GroupVarInfos, but only if it's not known equal to any of the existing
    3141             :  * entries.
    3142             :  */
    3143             : typedef struct
    3144             : {
    3145             :     Node       *var;            /* might be an expression, not just a Var */
    3146             :     RelOptInfo *rel;            /* relation it belongs to */
    3147             :     double      ndistinct;      /* # distinct values */
    3148             : } GroupVarInfo;
    3149             : 
    3150             : static List *
    3151        1111 : add_unique_group_var(PlannerInfo *root, List *varinfos,
    3152             :                      Node *var, VariableStatData *vardata)
    3153             : {
    3154             :     GroupVarInfo *varinfo;
    3155             :     double      ndistinct;
    3156             :     bool        isdefault;
    3157             :     ListCell   *lc;
    3158             : 
    3159        1111 :     ndistinct = get_variable_numdistinct(vardata, &isdefault);
    3160             : 
    3161             :     /* cannot use foreach here because of possible list_delete */
    3162        1111 :     lc = list_head(varinfos);
    3163        2516 :     while (lc)
    3164             :     {
    3165         303 :         varinfo = (GroupVarInfo *) lfirst(lc);
    3166             : 
    3167             :         /* must advance lc before list_delete possibly pfree's it */
    3168         303 :         lc = lnext(lc);
    3169             : 
    3170             :         /* Drop exact duplicates */
    3171         303 :         if (equal(var, varinfo->var))
    3172           6 :             return varinfos;
    3173             : 
    3174             :         /*
    3175             :          * Drop known-equal vars, but only if they belong to different
    3176             :          * relations (see comments for estimate_num_groups)
    3177             :          */
    3178         355 :         if (vardata->rel != varinfo->rel &&
    3179          58 :             exprs_known_equal(root, var, varinfo->var))
    3180             :         {
    3181           3 :             if (varinfo->ndistinct <= ndistinct)
    3182             :             {
    3183             :                 /* Keep older item, forget new one */
    3184           3 :                 return varinfos;
    3185             :             }
    3186             :             else
    3187             :             {
    3188             :                 /* Delete the older item */
    3189           0 :                 varinfos = list_delete_ptr(varinfos, varinfo);
    3190             :             }
    3191             :         }
    3192             :     }
    3193             : 
    3194        1102 :     varinfo = (GroupVarInfo *) palloc(sizeof(GroupVarInfo));
    3195             : 
    3196        1102 :     varinfo->var = var;
    3197        1102 :     varinfo->rel = vardata->rel;
    3198        1102 :     varinfo->ndistinct = ndistinct;
    3199        1102 :     varinfos = lappend(varinfos, varinfo);
    3200        1102 :     return varinfos;
    3201             : }
    3202             : 
    3203             : /*
    3204             :  * estimate_num_groups      - Estimate number of groups in a grouped query
    3205             :  *
    3206             :  * Given a query having a GROUP BY clause, estimate how many groups there
    3207             :  * will be --- ie, the number of distinct combinations of the GROUP BY
    3208             :  * expressions.
    3209             :  *
    3210             :  * This routine is also used to estimate the number of rows emitted by
    3211             :  * a DISTINCT filtering step; that is an isomorphic problem.  (Note:
    3212             :  * actually, we only use it for DISTINCT when there's no grouping or
    3213             :  * aggregation ahead of the DISTINCT.)
    3214             :  *
    3215             :  * Inputs:
    3216             :  *  root - the query
    3217             :  *  groupExprs - list of expressions being grouped by
    3218             :  *  input_rows - number of rows estimated to arrive at the group/unique
    3219             :  *      filter step
    3220             :  *  pgset - NULL, or a List** pointing to a grouping set to filter the
    3221             :  *      groupExprs against
    3222             :  *
    3223             :  * Given the lack of any cross-correlation statistics in the system, it's
    3224             :  * impossible to do anything really trustworthy with GROUP BY conditions
    3225             :  * involving multiple Vars.  We should however avoid assuming the worst
    3226             :  * case (all possible cross-product terms actually appear as groups) since
    3227             :  * very often the grouped-by Vars are highly correlated.  Our current approach
    3228             :  * is as follows:
    3229             :  *  1.  Expressions yielding boolean are assumed to contribute two groups,
    3230             :  *      independently of their content, and are ignored in the subsequent
    3231             :  *      steps.  This is mainly because tests like "col IS NULL" break the
    3232             :  *      heuristic used in step 2 especially badly.
    3233             :  *  2.  Reduce the given expressions to a list of unique Vars used.  For
    3234             :  *      example, GROUP BY a, a + b is treated the same as GROUP BY a, b.
    3235             :  *      It is clearly correct not to count the same Var more than once.
    3236             :  *      It is also reasonable to treat f(x) the same as x: f() cannot
    3237             :  *      increase the number of distinct values (unless it is volatile,
    3238             :  *      which we consider unlikely for grouping), but it probably won't
    3239             :  *      reduce the number of distinct values much either.
    3240             :  *      As a special case, if a GROUP BY expression can be matched to an
    3241             :  *      expressional index for which we have statistics, then we treat the
    3242             :  *      whole expression as though it were just a Var.
    3243             :  *  3.  If the list contains Vars of different relations that are known equal
    3244             :  *      due to equivalence classes, then drop all but one of the Vars from each
    3245             :  *      known-equal set, keeping the one with smallest estimated # of values
    3246             :  *      (since the extra values of the others can't appear in joined rows).
    3247             :  *      Note the reason we only consider Vars of different relations is that
    3248             :  *      if we considered ones of the same rel, we'd be double-counting the
    3249             :  *      restriction selectivity of the equality in the next step.
    3250             :  *  4.  For Vars within a single source rel, we multiply together the numbers
    3251             :  *      of values, clamp to the number of rows in the rel (divided by 10 if
    3252             :  *      more than one Var), and then multiply by a factor based on the
    3253             :  *      selectivity of the restriction clauses for that rel.  When there's
    3254             :  *      more than one Var, the initial product is probably too high (it's the
    3255             :  *      worst case) but clamping to a fraction of the rel's rows seems to be a
    3256             :  *      helpful heuristic for not letting the estimate get out of hand.  (The
    3257             :  *      factor of 10 is derived from pre-Postgres-7.4 practice.)  The factor
    3258             :  *      we multiply by to adjust for the restriction selectivity assumes that
    3259             :  *      the restriction clauses are independent of the grouping, which may not
    3260             :  *      be a valid assumption, but it's hard to do better.
    3261             :  *  5.  If there are Vars from multiple rels, we repeat step 4 for each such
    3262             :  *      rel, and multiply the results together.
    3263             :  * Note that rels not containing grouped Vars are ignored completely, as are
    3264             :  * join clauses.  Such rels cannot increase the number of groups, and we
    3265             :  * assume such clauses do not reduce the number either (somewhat bogus,
    3266             :  * but we don't have the info to do better).
    3267             :  */
    3268             : double
    3269         959 : estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows,
    3270             :                     List **pgset)
    3271             : {
    3272         959 :     List       *varinfos = NIL;
    3273             :     double      numdistinct;
    3274             :     ListCell   *l;
    3275             :     int         i;
    3276             : 
    3277             :     /*
    3278             :      * We don't ever want to return an estimate of zero groups, as that tends
    3279             :      * to lead to division-by-zero and other unpleasantness.  The input_rows
    3280             :      * estimate is usually already at least 1, but clamp it just in case it
    3281             :      * isn't.
    3282             :      */
    3283         959 :     input_rows = clamp_row_est(input_rows);
    3284             : 
    3285             :     /*
    3286             :      * If no grouping columns, there's exactly one group.  (This can't happen
    3287             :      * for normal cases with GROUP BY or DISTINCT, but it is possible for
    3288             :      * corner cases with set operations.)
    3289             :      */
    3290         959 :     if (groupExprs == NIL || (pgset && list_length(*pgset) < 1))
    3291          63 :         return 1.0;
    3292             : 
    3293             :     /*
    3294             :      * Count groups derived from boolean grouping expressions.  For other
    3295             :      * expressions, find the unique Vars used, treating an expression as a Var
    3296             :      * if we can find stats for it.  For each one, record the statistical
    3297             :      * estimate of number of distinct values (total in its table, without
    3298             :      * regard for filtering).
    3299             :      */
    3300         896 :     numdistinct = 1.0;
    3301             : 
    3302         896 :     i = 0;
    3303        2132 :     foreach(l, groupExprs)
    3304             :     {
    3305        1237 :         Node       *groupexpr = (Node *) lfirst(l);
    3306             :         VariableStatData vardata;
    3307             :         List       *varshere;
    3308             :         ListCell   *l2;
    3309             : 
    3310             :         /* is expression in this grouping set? */
    3311        1237 :         if (pgset && !list_member_int(*pgset, i++))
    3312         510 :             continue;
    3313             : 
    3314             :         /* Short-circuit for expressions returning boolean */
    3315        1174 :         if (exprType(groupexpr) == BOOLOID)
    3316             :         {
    3317           6 :             numdistinct *= 2.0;
    3318           6 :             continue;
    3319             :         }
    3320             : 
    3321             :         /*
    3322             :          * If examine_variable is able to deduce anything about the GROUP BY
    3323             :          * expression, treat it as a single variable even if it's really more
    3324             :          * complicated.
    3325             :          */
    3326        1168 :         examine_variable(root, groupexpr, 0, &vardata);
    3327        1168 :         if (HeapTupleIsValid(vardata.statsTuple) || vardata.isunique)
    3328             :         {
    3329         319 :             varinfos = add_unique_group_var(root, varinfos,
    3330             :                                             groupexpr, &vardata);
    3331         319 :             ReleaseVariableStats(vardata);
    3332         319 :             continue;
    3333             :         }
    3334         849 :         ReleaseVariableStats(vardata);
    3335             : 
    3336             :         /*
    3337             :          * Else pull out the component Vars.  Handle PlaceHolderVars by
    3338             :          * recursing into their arguments (effectively assuming that the
    3339             :          * PlaceHolderVar doesn't change the number of groups, which boils
    3340             :          * down to ignoring the possible addition of nulls to the result set).
    3341             :          */
    3342         849 :         varshere = pull_var_clause(groupexpr,
    3343             :                                    PVC_RECURSE_AGGREGATES |
    3344             :                                    PVC_RECURSE_WINDOWFUNCS |
    3345             :                                    PVC_RECURSE_PLACEHOLDERS);
    3346             : 
    3347             :         /*
    3348             :          * If we find any variable-free GROUP BY item, then either it is a
    3349             :          * constant (and we can ignore it) or it contains a volatile function;
    3350             :          * in the latter case we punt and assume that each input row will
    3351             :          * yield a distinct group.
    3352             :          */
    3353         849 :         if (varshere == NIL)
    3354             :         {
    3355          60 :             if (contain_volatile_functions(groupexpr))
    3356           1 :                 return input_rows;
    3357          59 :             continue;
    3358             :         }
    3359             : 
    3360             :         /*
    3361             :          * Else add variables to varinfos list
    3362             :          */
    3363        1581 :         foreach(l2, varshere)
    3364             :         {
    3365         792 :             Node       *var = (Node *) lfirst(l2);
    3366             : 
    3367         792 :             examine_variable(root, var, 0, &vardata);
    3368         792 :             varinfos = add_unique_group_var(root, varinfos, var, &vardata);
    3369         792 :             ReleaseVariableStats(vardata);
    3370             :         }
    3371             :     }
    3372             : 
    3373             :     /*
    3374             :      * If now no Vars, we must have an all-constant or all-boolean GROUP BY
    3375             :      * list.
    3376             :      */
    3377         895 :     if (varinfos == NIL)
    3378             :     {
    3379             :         /* Guard against out-of-range answers */
    3380          31 :         if (numdistinct > input_rows)
    3381           0 :             numdistinct = input_rows;
    3382          31 :         return numdistinct;
    3383             :     }
    3384             : 
    3385             :     /*
    3386             :      * Group Vars by relation and estimate total numdistinct.
    3387             :      *
    3388             :      * For each iteration of the outer loop, we process the frontmost Var in
    3389             :      * varinfos, plus all other Vars in the same relation.  We remove these
    3390             :      * Vars from the newvarinfos list for the next iteration. This is the
    3391             :      * easiest way to group Vars of same rel together.
    3392             :      */
    3393             :     do
    3394             :     {
    3395         897 :         GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
    3396         897 :         RelOptInfo *rel = varinfo1->rel;
    3397         897 :         double      reldistinct = 1;
    3398         897 :         double      relmaxndistinct = reldistinct;
    3399         897 :         int         relvarcount = 0;
    3400         897 :         List       *newvarinfos = NIL;
    3401         897 :         List       *relvarinfos = NIL;
    3402             : 
    3403             :         /*
    3404             :          * Split the list of varinfos in two - one for the current rel, one
    3405             :          * for remaining Vars on other rels.
    3406             :          */
    3407         897 :         relvarinfos = lcons(varinfo1, relvarinfos);
    3408        1142 :         for_each_cell(l, lnext(list_head(varinfos)))
    3409             :         {
    3410         245 :             GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
    3411             : 
    3412         245 :             if (varinfo2->rel == varinfo1->rel)
    3413             :             {
    3414             :                 /* varinfos on current rel */
    3415         205 :                 relvarinfos = lcons(varinfo2, relvarinfos);
    3416             :             }
    3417             :             else
    3418             :             {
    3419             :                 /* not time to process varinfo2 yet */
    3420          40 :                 newvarinfos = lcons(varinfo2, newvarinfos);
    3421             :             }
    3422             :         }
    3423             : 
    3424             :         /*
    3425             :          * Get the numdistinct estimate for the Vars of this rel.  We
    3426             :          * iteratively search for multivariate n-distinct with maximum number
    3427             :          * of vars; assuming that each var group is independent of the others,
    3428             :          * we multiply them together.  Any remaining relvarinfos after no more
    3429             :          * multivariate matches are found are assumed independent too, so
    3430             :          * their individual ndistinct estimates are multiplied also.
    3431             :          *
    3432             :          * While iterating, count how many separate numdistinct values we
    3433             :          * apply.  We apply a fudge factor below, but only if we multiplied
    3434             :          * more than one such values.
    3435             :          */
    3436        2695 :         while (relvarinfos)
    3437             :         {
    3438             :             double      mvndistinct;
    3439             : 
    3440         901 :             if (estimate_multivariate_ndistinct(root, rel, &relvarinfos,
    3441             :                                                 &mvndistinct))
    3442             :             {
    3443           9 :                 reldistinct *= mvndistinct;
    3444           9 :                 if (relmaxndistinct < mvndistinct)
    3445           9 :                     relmaxndistinct = mvndistinct;
    3446           9 :                 relvarcount++;
    3447             :             }
    3448             :             else
    3449             :             {
    3450        1972 :                 foreach(l, relvarinfos)
    3451             :                 {
    3452        1080 :                     GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
    3453             : 
    3454        1080 :                     reldistinct *= varinfo2->ndistinct;
    3455        1080 :                     if (relmaxndistinct < varinfo2->ndistinct)
    3456         894 :                         relmaxndistinct = varinfo2->ndistinct;
    3457        1080 :                     relvarcount++;
    3458             :                 }
    3459             : 
    3460             :                 /* we're done with this relation */
    3461         892 :                 relvarinfos = NIL;
    3462             :             }
    3463             :         }
    3464             : 
    3465             :         /*
    3466             :          * Sanity check --- don't divide by zero if empty relation.
    3467             :          */
    3468         897 :         Assert(IS_SIMPLE_REL(rel));
    3469         897 :         if (rel->tuples > 0)
    3470             :         {
    3471             :             /*
    3472             :              * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
    3473             :              * fudge factor is because the Vars are probably correlated but we
    3474             :              * don't know by how much.  We should never clamp to less than the
    3475             :              * largest ndistinct value for any of the Vars, though, since
    3476             :              * there will surely be at least that many groups.
    3477             :              */
    3478         897 :             double      clamp = rel->tuples;
    3479             : 
    3480         897 :             if (relvarcount > 1)
    3481             :             {
    3482         171 :                 clamp *= 0.1;
    3483         171 :                 if (clamp < relmaxndistinct)
    3484             :                 {
    3485         121 :                     clamp = relmaxndistinct;
    3486             :                     /* for sanity in case some ndistinct is too large: */
    3487         121 :                     if (clamp > rel->tuples)
    3488           0 :                         clamp = rel->tuples;
    3489             :                 }
    3490             :             }
    3491         897 :             if (reldistinct > clamp)
    3492         149 :                 reldistinct = clamp;
    3493             : 
    3494             :             /*
    3495             :              * Update the estimate based on the restriction selectivity,
    3496             :              * guarding against division by zero when reldistinct is zero.
    3497             :              * Also skip this if we know that we are returning all rows.
    3498             :              */
    3499         897 :             if (reldistinct > 0 && rel->rows < rel->tuples)
    3500             :             {
    3501             :                 /*
    3502             :                  * Given a table containing N rows with n distinct values in a
    3503             :                  * uniform distribution, if we select p rows at random then
    3504             :                  * the expected number of distinct values selected is
    3505             :                  *
    3506             :                  * n * (1 - product((N-N/n-i)/(N-i), i=0..p-1))
    3507             :                  *
    3508             :                  * = n * (1 - (N-N/n)! / (N-N/n-p)! * (N-p)! / N!)
    3509             :                  *
    3510             :                  * See "Approximating block accesses in database
    3511             :                  * organizations", S. B. Yao, Communications of the ACM,
    3512             :                  * Volume 20 Issue 4, April 1977 Pages 260-261.
    3513             :                  *
    3514             :                  * Alternatively, re-arranging the terms from the factorials,
    3515             :                  * this may be written as
    3516             :                  *
    3517             :                  * n * (1 - product((N-p-i)/(N-i), i=0..N/n-1))
    3518             :                  *
    3519             :                  * This form of the formula is more efficient to compute in
    3520             :                  * the common case where p is larger than N/n.  Additionally,
    3521             :                  * as pointed out by Dell'Era, if i << N for all terms in the
    3522             :                  * product, it can be approximated by
    3523             :                  *
    3524             :                  * n * (1 - ((N-p)/N)^(N/n))
    3525             :                  *
    3526             :                  * See "Expected distinct values when selecting from a bag
    3527             :                  * without replacement", Alberto Dell'Era,
    3528             :                  * http://www.adellera.it/investigations/distinct_balls/.
    3529             :                  *
    3530             :                  * The condition i << N is equivalent to n >> 1, so this is a
    3531             :                  * good approximation when the number of distinct values in
    3532             :                  * the table is large.  It turns out that this formula also
    3533             :                  * works well even when n is small.
    3534             :                  */
    3535         358 :                 reldistinct *=
    3536         358 :                     (1 - pow((rel->tuples - rel->rows) / rel->tuples,
    3537         716 :                              rel->tuples / reldistinct));
    3538             :             }
    3539         897 :             reldistinct = clamp_row_est(reldistinct);
    3540             : 
    3541             :             /*
    3542             :              * Update estimate of total distinct groups.
    3543             :              */
    3544         897 :             numdistinct *= reldistinct;
    3545             :         }
    3546             : 
    3547         897 :         varinfos = newvarinfos;
    3548         897 :     } while (varinfos != NIL);
    3549             : 
    3550         864 :     numdistinct = ceil(numdistinct);
    3551             : 
    3552             :     /* Guard against out-of-range answers */
    3553         864 :     if (numdistinct > input_rows)
    3554          52 :         numdistinct = input_rows;
    3555         864 :     if (numdistinct < 1.0)
    3556           0 :         numdistinct = 1.0;
    3557             : 
    3558         864 :     return numdistinct;
    3559             : }
    3560             : 
    3561             : /*
    3562             :  * Estimate hash bucket statistics when the specified expression is used
    3563             :  * as a hash key for the given number of buckets.
    3564             :  *
    3565             :  * This attempts to determine two values:
    3566             :  *
    3567             :  * 1. The frequency of the most common value of the expression (returns
    3568             :  * zero into *mcv_freq if we can't get that).
    3569             :  *
    3570             :  * 2. The "bucketsize fraction", ie, average number of entries in a bucket
    3571             :  * divided by total tuples in relation.
    3572             :  *
    3573             :  * XXX This is really pretty bogus since we're effectively assuming that the
    3574             :  * distribution of hash keys will be the same after applying restriction
    3575             :  * clauses as it was in the underlying relation.  However, we are not nearly
    3576             :  * smart enough to figure out how the restrict clauses might change the
    3577             :  * distribution, so this will have to do for now.
    3578             :  *
    3579             :  * We are passed the number of buckets the executor will use for the given
    3580             :  * input relation.  If the data were perfectly distributed, with the same
    3581             :  * number of tuples going into each available bucket, then the bucketsize
    3582             :  * fraction would be 1/nbuckets.  But this happy state of affairs will occur
    3583             :  * only if (a) there are at least nbuckets distinct data values, and (b)
    3584             :  * we have a not-too-skewed data distribution.  Otherwise the buckets will
    3585             :  * be nonuniformly occupied.  If the other relation in the join has a key
    3586             :  * distribution similar to this one's, then the most-loaded buckets are
    3587             :  * exactly those that will be probed most often.  Therefore, the "average"
    3588             :  * bucket size for costing purposes should really be taken as something close
    3589             :  * to the "worst case" bucket size.  We try to estimate this by adjusting the
    3590             :  * fraction if there are too few distinct data values, and then scaling up
    3591             :  * by the ratio of the most common value's frequency to the average frequency.
    3592             :  *
    3593             :  * If no statistics are available, use a default estimate of 0.1.  This will
    3594             :  * discourage use of a hash rather strongly if the inner relation is large,
    3595             :  * which is what we want.  We do not want to hash unless we know that the
    3596             :  * inner rel is well-dispersed (or the alternatives seem much worse).
    3597             :  *
    3598             :  * The caller should also check that the mcv_freq is not so large that the
    3599             :  * most common value would by itself require an impractically large bucket.
    3600             :  * In a hash join, the executor can split buckets if they get too big, but
    3601             :  * obviously that doesn't help for a bucket that contains many duplicates of
    3602             :  * the same value.
    3603             :  */
    3604             : void
    3605        4540 : estimate_hash_bucket_stats(PlannerInfo *root, Node *hashkey, double nbuckets,
    3606             :                            Selectivity *mcv_freq,
    3607             :                            Selectivity *bucketsize_frac)
    3608             : {
    3609             :     VariableStatData vardata;
    3610             :     double      estfract,
    3611             :                 ndistinct,
    3612             :                 stanullfrac,
    3613             :                 avgfreq;
    3614             :     bool        isdefault;
    3615             :     AttStatsSlot sslot;
    3616             : 
    3617        4540 :     examine_variable(root, hashkey, 0, &vardata);
    3618             : 
    3619             :     /* Look up the frequency of the most common value, if available */
    3620        4540 :     *mcv_freq = 0.0;
    3621             : 
    3622        4540 :     if (HeapTupleIsValid(vardata.statsTuple))
    3623             :     {
    3624        1543 :         if (get_attstatsslot(&sslot, vardata.statsTuple,
    3625             :                              STATISTIC_KIND_MCV, InvalidOid,
    3626             :                              ATTSTATSSLOT_NUMBERS))
    3627             :         {
    3628             :             /*
    3629             :              * The first MCV stat is for the most common value.
    3630             :              */
    3631        1289 :             if (sslot.nnumbers > 0)
    3632        1289 :                 *mcv_freq = sslot.numbers[0];
    3633        1289 :             free_attstatsslot(&sslot);
    3634             :         }
    3635             :     }
    3636             : 
    3637             :     /* Get number of distinct values */
    3638        4540 :     ndistinct = get_variable_numdistinct(&vardata, &isdefault);
    3639             : 
    3640             :     /*
    3641             :      * If ndistinct isn't real, punt.  We normally return 0.1, but if the
    3642             :      * mcv_freq is known to be even higher than that, use it instead.
    3643             :      */
    3644        4540 :     if (isdefault)
    3645             :     {
    3646        1004 :         *bucketsize_frac = (Selectivity) Max(0.1, *mcv_freq);
    3647        1004 :         ReleaseVariableStats(vardata);
    3648        5544 :         return;
    3649             :     }
    3650             : 
    3651             :     /* Get fraction that are null */
    3652        3536 :     if (HeapTupleIsValid(vardata.statsTuple))
    3653             :     {
    3654             :         Form_pg_statistic stats;
    3655             : 
    3656        1543 :         stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
    3657        1543 :         stanullfrac = stats->stanullfrac;
    3658             :     }
    3659             :     else
    3660        1993 :         stanullfrac = 0.0;
    3661             : 
    3662             :     /* Compute avg freq of all distinct data values in raw relation */
    3663        3536 :     avgfreq = (1.0 - stanullfrac) / ndistinct;
    3664             : 
    3665             :     /*
    3666             :      * Adjust ndistinct to account for restriction clauses.  Observe we are
    3667             :      * assuming that the data distribution is affected uniformly by the
    3668             :      * restriction clauses!
    3669             :      *
    3670             :      * XXX Possibly better way, but much more expensive: multiply by
    3671             :      * selectivity of rel's restriction clauses that mention the target Var.
    3672             :      */
    3673        3536 :     if (vardata.rel && vardata.rel->tuples > 0)
    3674             :     {
    3675        3536 :         ndistinct *= vardata.rel->rows / vardata.rel->tuples;
    3676        3536 :         ndistinct = clamp_row_est(ndistinct);
    3677             :     }
    3678             : 
    3679             :     /*
    3680             :      * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
    3681             :      * number of buckets is less than the expected number of distinct values;
    3682             :      * otherwise it is 1/ndistinct.
    3683             :      */
    3684        3536 :     if (ndistinct > nbuckets)
    3685           8 :         estfract = 1.0 / nbuckets;
    3686             :     else
    3687        3528 :         estfract = 1.0 / ndistinct;
    3688             : 
    3689             :     /*
    3690             :      * Adjust estimated bucketsize upward to account for skewed distribution.
    3691             :      */
    3692        3536 :     if (avgfreq > 0.0 && *mcv_freq > avgfreq)
    3693        1222 :         estfract *= *mcv_freq / avgfreq;
    3694             : 
    3695             :     /*
    3696             :      * Clamp bucketsize to sane range (the above adjustment could easily
    3697             :      * produce an out-of-range result).  We set the lower bound a little above
    3698             :      * zero, since zero isn't a very sane result.
    3699             :      */
    3700        3536 :     if (estfract < 1.0e-6)
    3701           0 :         estfract = 1.0e-6;
    3702        3536 :     else if (estfract > 1.0)
    3703         895 :         estfract = 1.0;
    3704             : 
    3705        3536 :     *bucketsize_frac = (Selectivity) estfract;
    3706             : 
    3707        3536 :     ReleaseVariableStats(vardata);
    3708             : }
    3709             : 
    3710             : 
    3711             : /*-------------------------------------------------------------------------
    3712             :  *
    3713             :  * Support routines
    3714             :  *
    3715             :  *-------------------------------------------------------------------------
    3716             :  */
    3717             : 
    3718             : /*
    3719             :  * Find applicable ndistinct statistics for the given list of VarInfos (which
    3720             :  * must all belong to the given rel), and update *ndistinct to the estimate of
    3721             :  * the MVNDistinctItem that best matches.  If a match it found, *varinfos is
    3722             :  * updated to remove the list of matched varinfos.
    3723             :  *
    3724             :  * Varinfos that aren't for simple Vars are ignored.
    3725             :  *
    3726             :  * Return TRUE if we're able to find a match, FALSE otherwise.
    3727             :  */
    3728             : static bool
    3729         901 : estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
    3730             :                                 List **varinfos, double *ndistinct)
    3731             : {
    3732             :     ListCell   *lc;
    3733         901 :     Bitmapset  *attnums = NULL;
    3734             :     int         nmatches;
    3735         901 :     Oid         statOid = InvalidOid;
    3736             :     MVNDistinct *stats;
    3737         901 :     Bitmapset  *matched = NULL;
    3738             : 
    3739             :     /* bail out immediately if the table has no extended statistics */
    3740         901 :     if (!rel->statlist)
    3741         887 :         return false;
    3742             : 
    3743             :     /* Determine the attnums we're looking for */
    3744          46 :     foreach(lc, *varinfos)
    3745             :     {
    3746          32 :         GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
    3747             : 
    3748          32 :         Assert(varinfo->rel == rel);
    3749             : 
    3750          32 :         if (IsA(varinfo->var, Var))
    3751             :         {
    3752          32 :             attnums = bms_add_member(attnums,
    3753          32 :                                      ((Var *) varinfo->var)->varattno);
    3754             :         }
    3755             :     }
    3756             : 
    3757             :     /* look for the ndistinct statistics matching the most vars */
    3758          14 :     nmatches = 1;               /* we require at least two matches */
    3759          35 :     foreach(lc, rel->statlist)
    3760             :     {
    3761          21 :         StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
    3762             :         Bitmapset  *shared;
    3763             :         int         nshared;
    3764             : 
    3765             :         /* skip statistics of other kinds */
    3766          21 :         if (info->kind != STATS_EXT_NDISTINCT)
    3767           7 :             continue;
    3768             : 
    3769             :         /* compute attnums shared by the vars and the statistics object */
    3770          14 :         shared = bms_intersect(info->keys, attnums);
    3771          14 :         nshared = bms_num_members(shared);
    3772             : 
    3773             :         /*
    3774             :          * Does this statistics object match more columns than the currently
    3775             :          * best object?  If so, use this one instead.
    3776             :          *
    3777             :          * XXX This should break ties using name of the object, or something
    3778             :          * like that, to make the outcome stable.
    3779             :          */
    3780          14 :         if (nshared > nmatches)
    3781             :         {
    3782           9 :             statOid = info->statOid;
    3783           9 :             nmatches = nshared;
    3784           9 :             matched = shared;
    3785             :         }
    3786             :     }
    3787             : 
    3788             :     /* No match? */
    3789          14 :     if (statOid == InvalidOid)
    3790           5 :         return false;
    3791           9 :     Assert(nmatches > 1 && matched != NULL);
    3792             : 
    3793           9 :     stats = statext_ndistinct_load(statOid);
    3794             : 
    3795             :     /*
    3796             :      * If we have a match, search it for the specific item that matches (there
    3797             :      * must be one), and construct the output values.
    3798             :      */
    3799           9 :     if (stats)
    3800             :     {
    3801             :         int         i;
    3802           9 :         List       *newlist = NIL;
    3803           9 :         MVNDistinctItem *item = NULL;
    3804             : 
    3805             :         /* Find the specific item that exactly matches the combination */
    3806          27 :         for (i = 0; i < stats->nitems; i++)
    3807             :         {
    3808          27 :             MVNDistinctItem *tmpitem = &stats->items[i];
    3809             : 
    3810          27 :             if (bms_subset_compare(tmpitem->attrs, matched) == BMS_EQUAL)
    3811             :             {
    3812           9 :                 item = tmpitem;
    3813           9 :                 break;
    3814             :             }
    3815             :         }
    3816             : 
    3817             :         /* make sure we found an item */
    3818           9 :         if (!item)
    3819           0 :             elog(ERROR, "corrupt MVNDistinct entry");
    3820             : 
    3821             :         /* Form the output varinfo list, keeping only unmatched ones */
    3822          35 :         foreach(lc, *varinfos)
    3823             :         {
    3824          26 :             GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
    3825             :             AttrNumber  attnum;
    3826             : 
    3827          26 :             if (!IsA(varinfo->var, Var))
    3828             :             {
    3829           0 :                 newlist = lappend(newlist, varinfo);
    3830           0 :                 continue;
    3831             :             }
    3832             : 
    3833          26 :             attnum = ((Var *) varinfo->var)->varattno;
    3834          26 :             if (!bms_is_member(attnum, matched))
    3835           4 :                 newlist = lappend(newlist, varinfo);
    3836             :         }
    3837             : 
    3838           9 :         *varinfos = newlist;
    3839           9 :         *ndistinct = item->ndistinct;
    3840           9 :         return true;
    3841             :     }
    3842             : 
    3843           0 :     return false;
    3844             : }
    3845             : 
    3846             : /*
    3847             :  * convert_to_scalar
    3848             :  *    Convert non-NULL values of the indicated types to the comparison
    3849             :  *    scale needed by scalarineqsel().
    3850             :  *    Returns "true" if successful.
    3851             :  *
    3852             :  * XXX this routine is a hack: ideally we should look up the conversion
    3853             :  * subroutines in pg_type.
    3854             :  *
    3855             :  * All numeric datatypes are simply converted to their equivalent
    3856             :  * "double" values.  (NUMERIC values that are outside the range of "double"
    3857             :  * are clamped to +/- HUGE_VAL.)
    3858             :  *
    3859             :  * String datatypes are converted by convert_string_to_scalar(),
    3860             :  * which is explained below.  The reason why this routine deals with
    3861             :  * three values at a time, not just one, is that we need it for strings.
    3862             :  *
    3863             :  * The bytea datatype is just enough different from strings that it has
    3864             :  * to be treated separately.
    3865             :  *
    3866             :  * The several datatypes representing absolute times are all converted
    3867             :  * to Timestamp, which is actually a double, and then we just use that
    3868             :  * double value.  Note this will give correct results even for the "special"
    3869             :  * values of Timestamp, since those are chosen to compare correctly;
    3870             :  * see timestamp_cmp.
    3871             :  *
    3872             :  * The several datatypes representing relative times (intervals) are all
    3873             :  * converted to measurements expressed in seconds.
    3874             :  */
    3875             : static bool
    3876         733 : convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
    3877             :                   Datum lobound, Datum hibound, Oid boundstypid,
    3878             :                   double *scaledlobound, double *scaledhibound)
    3879             : {
    3880             :     /*
    3881             :      * Both the valuetypid and the boundstypid should exactly match the
    3882             :      * declared input type(s) of the operator we are invoked for, so we just
    3883             :      * error out if either is not recognized.
    3884             :      *
    3885             :      * XXX The histogram we are interpolating between points of could belong
    3886             :      * to a column that's only binary-compatible with the declared type. In
    3887             :      * essence we are assuming that the semantics of binary-compatible types
    3888             :      * are enough alike that we can use a histogram generated with one type's
    3889             :      * operators to estimate selectivity for the other's.  This is outright
    3890             :      * wrong in some cases --- in particular signed versus unsigned
    3891             :      * interpretation could trip us up.  But it's useful enough in the
    3892             :      * majority of cases that we do it anyway.  Should think about more
    3893             :      * rigorous ways to do it.
    3894             :      */
    3895         733 :     switch (valuetypid)
    3896             :     {
    3897             :             /*
    3898             :              * Built-in numeric types
    3899             :              */
    3900             :         case BOOLOID:
    3901             :         case INT2OID:
    3902             :         case INT4OID:
    3903             :         case INT8OID:
    3904             :         case FLOAT4OID:
    3905             :         case FLOAT8OID:
    3906             :         case NUMERICOID:
    3907             :         case OIDOID:
    3908             :         case REGPROCOID:
    3909             :         case REGPROCEDUREOID:
    3910             :         case REGOPEROID:
    3911             :         case REGOPERATOROID:
    3912             :         case REGCLASSOID:
    3913             :         case REGTYPEOID:
    3914             :         case REGCONFIGOID:
    3915             :         case REGDICTIONARYOID:
    3916             :         case REGROLEOID:
    3917             :         case REGNAMESPACEOID:
    3918         387 :             *scaledvalue = convert_numeric_to_scalar(value, valuetypid);
    3919         387 :             *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid);
    3920         387 :             *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid);
    3921         387 :             return true;
    3922             : 
    3923             :             /*
    3924             :              * Built-in string types
    3925             :              */
    3926             :         case CHAROID:
    3927             :         case BPCHAROID:
    3928             :         case VARCHAROID:
    3929             :         case TEXTOID:
    3930             :         case NAMEOID:
    3931             :             {
    3932         345 :                 char       *valstr = convert_string_datum(value, valuetypid);
    3933         345 :                 char       *lostr = convert_string_datum(lobound, boundstypid);
    3934         345 :                 char       *histr = convert_string_datum(hibound, boundstypid);
    3935             : 
    3936         345 :                 convert_string_to_scalar(valstr, scaledvalue,
    3937             :                                          lostr, scaledlobound,
    3938             :                                          histr, scaledhibound);
    3939         345 :                 pfree(valstr);
    3940         345 :                 pfree(lostr);
    3941         345 :                 pfree(histr);
    3942         345 :                 return true;
    3943             :             }
    3944             : 
    3945             :             /*
    3946             :              * Built-in bytea type
    3947             :              */
    3948             :         case BYTEAOID:
    3949             :             {
    3950           0 :                 convert_bytea_to_scalar(value, scaledvalue,
    3951             :                                         lobound, scaledlobound,
    3952             :                                         hibound, scaledhibound);
    3953           0 :                 return true;
    3954             :             }
    3955             : 
    3956             :             /*
    3957             :              * Built-in time types
    3958             :              */
    3959             :         case TIMESTAMPOID:
    3960             :         case TIMESTAMPTZOID:
    3961             :         case ABSTIMEOID:
    3962             :         case DATEOID:
    3963             :         case INTERVALOID:
    3964             :         case RELTIMEOID:
    3965             :         case TINTERVALOID:
    3966             :         case TIMEOID:
    3967             :         case TIMETZOID:
    3968           0 :             *scaledvalue = convert_timevalue_to_scalar(value, valuetypid);
    3969           0 :             *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid);
    3970           0 :             *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid);
    3971           0 :             return true;
    3972             : 
    3973             :             /*
    3974             :              * Built-in network types
    3975             :              */
    3976             :         case INETOID:
    3977             :         case CIDROID:
    3978             :         case MACADDROID:
    3979             :         case MACADDR8OID:
    3980           0 :             *scaledvalue = convert_network_to_scalar(value, valuetypid);
    3981           0 :             *scaledlobound = convert_network_to_scalar(lobound, boundstypid);
    3982           0 :             *scaledhibound = convert_network_to_scalar(hibound, boundstypid);
    3983           0 :             return true;
    3984             :     }
    3985             :     /* Don't know how to convert */
    3986           1 :     *scaledvalue = *scaledlobound = *scaledhibound = 0;
    3987           1 :     return false;
    3988             : }
    3989             : 
    3990             : /*
    3991             :  * Do convert_to_scalar()'s work for any numeric data type.
    3992             :  */
    3993             : static double
    3994        1161 : convert_numeric_to_scalar(Datum value, Oid typid)
    3995             : {
    3996        1161 :     switch (typid)
    3997             :     {
    3998             :         case BOOLOID:
    3999           0 :             return (double) DatumGetBool(value);
    4000             :         case INT2OID:
    4001         338 :             return (double) DatumGetInt16(value);
    4002             :         case INT4OID:
    4003         667 :             return (double) DatumGetInt32(value);
    4004             :         case INT8OID:
    4005           0 :             return (double) DatumGetInt64(value);
    4006             :         case FLOAT4OID:
    4007           0 :             return (double) DatumGetFloat4(value);
    4008             :         case FLOAT8OID:
    4009           6 :             return (double) DatumGetFloat8(value);
    4010             :         case NUMERICOID:
    4011             :             /* Note: out-of-range values will be clamped to +-HUGE_VAL */
    4012           0 :             return (double)
    4013           0 :                 DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
    4014             :                                                    value));
    4015             :         case OIDOID:
    4016             :         case REGPROCOID:
    4017             :         case REGPROCEDUREOID:
    4018             :         case REGOPEROID:
    4019             :         case REGOPERATOROID:
    4020             :         case REGCLASSOID:
    4021             :         case REGTYPEOID:
    4022             :         case REGCONFIGOID:
    4023             :         case REGDICTIONARYOID:
    4024             :         case REGROLEOID:
    4025             :         case REGNAMESPACEOID:
    4026             :             /* we can treat OIDs as integers... */
    4027         150 :             return (double) DatumGetObjectId(value);
    4028             :     }
    4029             : 
    4030             :     /*
    4031             :      * Can't get here unless someone tries to use scalarltsel/scalargtsel on
    4032             :      * an operator with one numeric and one non-numeric operand.
    4033             :      */
    4034           0 :     elog(ERROR, "unsupported type: %u", typid);
    4035             :     return 0;
    4036             : }
    4037             : 
    4038             : /*
    4039             :  * Do convert_to_scalar()'s work for any character-string data type.
    4040             :  *
    4041             :  * String datatypes are converted to a scale that ranges from 0 to 1,
    4042             :  * where we visualize the bytes of the string as fractional digits.
    4043             :  *
    4044             :  * We do not want the base to be 256, however, since that tends to
    4045             :  * generate inflated selectivity estimates; few databases will have
    4046             :  * occurrences of all 256 possible byte values at each position.
    4047             :  * Instead, use the smallest and largest byte values seen in the bounds
    4048             :  * as the estimated range for each byte, after some fudging to deal with
    4049             :  * the fact that we probably aren't going to see the full range that way.
    4050             :  *
    4051             :  * An additional refinement is that we discard any common prefix of the
    4052             :  * three strings before computing the scaled values.  This allows us to
    4053             :  * "zoom in" when we encounter a narrow data range.  An example is a phone
    4054             :  * number database where all the values begin with the same area code.
    4055             :  * (Actually, the bounds will be adjacent histogram-bin-boundary values,
    4056             :  * so this is more likely to happen than you might think.)
    4057             :  */
    4058             : static void
    4059         345 : convert_string_to_scalar(char *value,
    4060             :                          double *scaledvalue,
    4061             :                          char *lobound,
    4062             :                          double *scaledlobound,
    4063             :                          char *hibound,
    4064             :                          double *scaledhibound)
    4065             : {
    4066             :     int         rangelo,
    4067             :                 rangehi;
    4068             :     char       *sptr;
    4069             : 
    4070         345 :     rangelo = rangehi = (unsigned char) hibound[0];
    4071       13189 :     for (sptr = lobound; *sptr; sptr++)
    4072             :     {
    4073       12844 :         if (rangelo > (unsigned char) *sptr)
    4074        1012 :             rangelo = (unsigned char) *sptr;
    4075       12844 :         if (rangehi < (unsigned char) *sptr)
    4076         543 :             rangehi = (unsigned char) *sptr;
    4077             :     }
    4078       10914 :     for (sptr = hibound; *sptr; sptr++)
    4079             :     {
    4080       10569 :         if (rangelo > (unsigned char) *sptr)
    4081           0 :             rangelo = (unsigned char) *sptr;
    4082       10569 :         if (rangehi < (unsigned char) *sptr)
    4083          78 :             rangehi = (unsigned char) *sptr;
    4084             :     }
    4085             :     /* If range includes any upper-case ASCII chars, make it include all */
    4086         345 :     if (rangelo <= 'Z' && rangehi >= 'A')
    4087             :     {
    4088           0 :         if (rangelo > 'A')
    4089           0 :             rangelo = 'A';
    4090           0 :         if (rangehi < 'Z')
    4091           0 :             rangehi = 'Z';
    4092             :     }
    4093             :     /* Ditto lower-case */
    4094         345 :     if (rangelo <= 'z' && rangehi >= 'a')
    4095             :     {
    4096           0 :         if (rangelo > 'a')
    4097           0 :             rangelo = 'a';
    4098           0 :         if (rangehi < 'z')
    4099           0 :             rangehi = 'z';
    4100             :     }
    4101             :     /* Ditto digits */
    4102         345 :     if (rangelo <= '9' && rangehi >= '0')
    4103             :     {
    4104         229 :         if (rangelo > '0')
    4105           0 :             rangelo = '0';
    4106         229 :         if (rangehi < '9')
    4107         229 :             rangehi = '9';
    4108             :     }
    4109             : 
    4110             :     /*
    4111             :      * If range includes less than 10 chars, assume we have not got enough
    4112             :      * data, and make it include regular ASCII set.
    4113             :      */
    4114         345 :     if (rangehi - rangelo < 9)
    4115             :     {
    4116           0 :         rangelo = ' ';
    4117           0 :         rangehi = 127;
    4118             :     }
    4119             : 
    4120             :     /*
    4121             :      * Now strip any common prefix of the three strings.
    4122             :      */
    4123         812 :     while (*lobound)
    4124             :     {
    4125         467 :         if (*lobound != *hibound || *lobound != *value)
    4126             :             break;
    4127         122 :         lobound++, hibound++, value++;
    4128             :     }
    4129             : 
    4130             :     /*
    4131             :      * Now we can do the conversions.
    4132             :      */
    4133         345 :     *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
    4134         345 :     *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
    4135         345 :     *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
    4136         345 : }
    4137             : 
    4138             : static double
    4139        1035 : convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
    4140             : {
    4141        1035 :     int         slen = strlen(value);
    4142             :     double      num,
    4143             :                 denom,
    4144             :                 base;
    4145             : 
    4146        1035 :     if (slen <= 0)
    4147           0 :         return 0.0;             /* empty string has scalar value 0 */
    4148             : 
    4149             :     /*
    4150             :      * There seems little point in considering more than a dozen bytes from
    4151             :      * the string.  Since base is at least 10, that will give us nominal
    4152             :      * resolution of at least 12 decimal digits, which is surely far more
    4153             :      * precision than this estimation technique has got anyway (especially in
    4154             :      * non-C locales).  Also, even with the maximum possible base of 256, this
    4155             :      * ensures denom cannot grow larger than 256^13 = 2.03e31, which will not
    4156             :      * overflow on any known machine.
    4157             :      */
    4158        1035 :     if (slen > 12)
    4159         838 :         slen = 12;
    4160             : 
    4161             :     /* Convert initial characters to fraction */
    4162        1035 :     base = rangehi - rangelo + 1;
    4163        1035 :     num = 0.0;
    4164        1035 :     denom = base;
    4165       13697 :     while (slen-- > 0)
    4166             :     {
    4167       11627 :         int         ch = (unsigned char) *value++;
    4168             : 
    4169       11627 :         if (ch < rangelo)
    4170           0 :             ch = rangelo - 1;
    4171       11627 :         else if (ch > rangehi)
    4172           0 :             ch = rangehi + 1;
    4173       11627 :         num += ((double) (ch - rangelo)) / denom;
    4174       11627 :         denom *= base;
    4175             :     }
    4176             : 
    4177        1035 :     return num;
    4178             : }
    4179             : 
    4180             : /*
    4181             :  * Convert a string-type Datum into a palloc'd, null-terminated string.
    4182             :  *
    4183             :  * When using a non-C locale, we must pass the string through strxfrm()
    4184             :  * before continuing, so as to generate correct locale-specific results.
    4185             :  */
    4186             : static char *
    4187        1035 : convert_string_datum(Datum value, Oid typid)
    4188             : {
    4189             :     char       *val;
    4190             : 
    4191        1035 :     switch (typid)
    4192             :     {
    4193             :         case CHAROID:
    4194           0 :             val = (char *) palloc(2);
    4195           0 :             val[0] = DatumGetChar(value);
    4196           0 :             val[1] = '\0';
    4197           0 :             break;
    4198             :         case BPCHAROID:
    4199             :         case VARCHAROID:
    4200             :         case TEXTOID:
    4201          18 :             val = TextDatumGetCString(value);
    4202          18 :             break;
    4203             :         case NAMEOID:
    4204             :             {
    4205        1017 :                 NameData   *nm = (NameData *) DatumGetPointer(value);
    4206             : 
    4207        1017 :                 val = pstrdup(NameStr(*nm));
    4208        1017 :                 break;
    4209             :             }
    4210             :         default:
    4211             : 
    4212             :             /*
    4213             :              * Can't get here unless someone tries to use scalarltsel on an
    4214             :              * operator with one string and one non-string operand.
    4215             :              */
    4216           0 :             elog(ERROR, "unsupported type: %u", typid);
    4217             :             return NULL;
    4218             :     }
    4219             : 
    4220        1035 :     if (!lc_collate_is_c(DEFAULT_COLLATION_OID))
    4221             :     {
    4222             :         char       *xfrmstr;
    4223             :         size_t      xfrmlen;
    4224             :         size_t      xfrmlen2 PG_USED_FOR_ASSERTS_ONLY;
    4225             : 
    4226             :         /*
    4227             :          * XXX: We could guess at a suitable output buffer size and only call
    4228             :          * strxfrm twice if our guess is too small.
    4229             :          *
    4230             :          * XXX: strxfrm doesn't support UTF-8 encoding on Win32, it can return
    4231             :          * bogus data or set an error. This is not really a problem unless it
    4232             :          * crashes since it will only give an estimation error and nothing
    4233             :          * fatal.
    4234             :          */
    4235             : #if _MSC_VER == 1400            /* VS.Net 2005 */
    4236             : 
    4237             :         /*
    4238             :          *
    4239             :          * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99694
    4240             :          */
    4241             :         {
    4242             :             char        x[1];
    4243             : 
    4244             :             xfrmlen = strxfrm(x, val, 0);
    4245             :         }
    4246             : #else
    4247        1035 :         xfrmlen = strxfrm(NULL, val, 0);
    4248             : #endif
    4249             : #ifdef WIN32
    4250             : 
    4251             :         /*
    4252             :          * On Windows, strxfrm returns INT_MAX when an error occurs. Instead
    4253             :          * of trying to allocate this much memory (and fail), just return the
    4254             :          * original string unmodified as if we were in the C locale.
    4255             :          */
    4256             :         if (xfrmlen == INT_MAX)
    4257             :             return val;
    4258             : #endif
    4259        1035 :         xfrmstr = (char *) palloc(xfrmlen + 1);
    4260        1035 :         xfrmlen2 = strxfrm(xfrmstr, val, xfrmlen + 1);
    4261             : 
    4262             :         /*
    4263             :          * Some systems (e.g., glibc) can return a smaller value from the
    4264             :          * second call than the first; thus the Assert must be <= not ==.
    4265             :          */
    4266        1035 :         Assert(xfrmlen2 <= xfrmlen);
    4267        1035 :         pfree(val);
    4268        1035 :         val = xfrmstr;
    4269             :     }
    4270             : 
    4271        1035 :     return val;
    4272             : }
    4273             : 
    4274             : /*
    4275             :  * Do convert_to_scalar()'s work for any bytea data type.
    4276             :  *
    4277             :  * Very similar to convert_string_to_scalar except we can't assume
    4278             :  * null-termination and therefore pass explicit lengths around.
    4279             :  *
    4280             :  * Also, assumptions about likely "normal" ranges of characters have been
    4281             :  * removed - a data range of 0..255 is always used, for now.  (Perhaps
    4282             :  * someday we will add information about actual byte data range to
    4283             :  * pg_statistic.)
    4284             :  */
    4285             : static void
    4286           0 : convert_bytea_to_scalar(Datum value,
    4287             :                         double *scaledvalue,
    4288             :                         Datum lobound,
    4289             :                         double *scaledlobound,
    4290             :                         Datum hibound,
    4291             :                         double *scaledhibound)
    4292             : {
    4293             :     int         rangelo,
    4294             :                 rangehi,
    4295           0 :                 valuelen = VARSIZE(DatumGetPointer(value)) - VARHDRSZ,
    4296           0 :                 loboundlen = VARSIZE(DatumGetPointer(lobound)) - VARHDRSZ,
    4297           0 :                 hiboundlen = VARSIZE(DatumGetPointer(hibound)) - VARHDRSZ,
    4298             :                 i,
    4299             :                 minlen;
    4300           0 :     unsigned char *valstr = (unsigned char *) VARDATA(DatumGetPointer(value)),
    4301           0 :                *lostr = (unsigned char *) VARDATA(DatumGetPointer(lobound)),
    4302           0 :                *histr = (unsigned char *) VARDATA(DatumGetPointer(hibound));
    4303             : 
    4304             :     /*
    4305             :      * Assume bytea data is uniformly distributed across all byte values.
    4306             :      */
    4307           0 :     rangelo = 0;
    4308           0 :     rangehi = 255;
    4309             : 
    4310             :     /*
    4311             :      * Now strip any common prefix of the three strings.
    4312             :      */
    4313           0 :     minlen = Min(Min(valuelen, loboundlen), hiboundlen);
    4314           0 :     for (i = 0; i < minlen; i++)
    4315             :     {
    4316           0 :         if (*lostr != *histr || *lostr != *valstr)
    4317             :             break;
    4318           0 :         lostr++, histr++, valstr++;
    4319           0 :         loboundlen--, hiboundlen--, valuelen--;
    4320             :     }
    4321             : 
    4322             :     /*
    4323             :      * Now we can do the conversions.
    4324             :      */
    4325           0 :     *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
    4326           0 :     *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
    4327           0 :     *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
    4328           0 : }
    4329             : 
    4330             : static double
    4331           0 : convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
    4332             :                             int rangelo, int rangehi)
    4333             : {
    4334             :     double      num,
    4335             :                 denom,
    4336             :                 base;
    4337             : 
    4338           0 :     if (valuelen <= 0)
    4339           0 :         return 0.0;             /* empty string has scalar value 0 */
    4340             : 
    4341             :     /*
    4342             :      * Since base is 256, need not consider more than about 10 chars (even
    4343             :      * this many seems like overkill)
    4344             :      */
    4345           0 :     if (valuelen > 10)
    4346           0 :         valuelen = 10;
    4347             : 
    4348             :     /* Convert initial characters to fraction */
    4349           0 :     base = rangehi - rangelo + 1;
    4350           0 :     num = 0.0;
    4351           0 :     denom = base;
    4352           0 :     while (valuelen-- > 0)
    4353             :     {
    4354           0 :         int         ch = *value++;
    4355             : 
    4356           0 :         if (ch < rangelo)
    4357           0 :             ch = rangelo - 1;
    4358           0 :         else if (ch > rangehi)
    4359           0 :             ch = rangehi + 1;
    4360           0 :         num += ((double) (ch - rangelo)) / denom;
    4361           0 :         denom *= base;
    4362             :     }
    4363             : 
    4364           0 :     return num;
    4365             : }
    4366             : 
    4367             : /*
    4368             :  * Do convert_to_scalar()'s work for any timevalue data type.
    4369             :  */
    4370             : static double
    4371           0 : convert_timevalue_to_scalar(Datum value, Oid typid)
    4372             : {
    4373           0 :     switch (typid)
    4374             :     {
    4375             :         case TIMESTAMPOID:
    4376           0 :             return DatumGetTimestamp(value);
    4377             :         case TIMESTAMPTZOID:
    4378           0 :             return DatumGetTimestampTz(value);
    4379             :         case ABSTIMEOID:
    4380           0 :             return DatumGetTimestamp(DirectFunctionCall1(abstime_timestamp,
    4381             :                                                          value));
    4382             :         case DATEOID:
    4383           0 :             return date2timestamp_no_overflow(DatumGetDateADT(value));
    4384             :         case INTERVALOID:
    4385             :             {
    4386           0 :                 Interval   *interval = DatumGetIntervalP(value);
    4387             : 
    4388             :                 /*
    4389             :                  * Convert the month part of Interval to days using assumed
    4390             :                  * average month length of 365.25/12.0 days.  Not too
    4391             :                  * accurate, but plenty good enough for our purposes.
    4392             :                  */
    4393           0 :                 return interval->time + interval->day * (double) USECS_PER_DAY +
    4394           0 :                     interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * USECS_PER_DAY);
    4395             :             }
    4396             :         case RELTIMEOID:
    4397           0 :             return (DatumGetRelativeTime(value) * 1000000.0);
    4398             :         case TINTERVALOID:
    4399             :             {
    4400           0 :                 TimeInterval tinterval = DatumGetTimeInterval(value);
    4401             : 
    4402           0 :                 if (tinterval->status != 0)
    4403           0 :                     return ((tinterval->data[1] - tinterval->data[0]) * 1000000.0);
    4404           0 :                 return 0;       /* for lack of a better idea */
    4405             :             }
    4406             :         case TIMEOID:
    4407           0 :             return DatumGetTimeADT(value);
    4408             :         case TIMETZOID:
    4409             :             {
    4410           0 :                 TimeTzADT  *timetz = DatumGetTimeTzADTP(value);
    4411             : 
    4412             :                 /* use GMT-equivalent time */
    4413           0 :                 return (double) (timetz->time + (timetz->zone * 1000000.0));
    4414             :             }
    4415             :     }
    4416             : 
    4417             :     /*
    4418             :      * Can't get here unless someone tries to use scalarltsel/scalargtsel on
    4419             :      * an operator with one timevalue and one non-timevalue operand.
    4420             :      */
    4421           0 :     elog(ERROR, "unsupported type: %u", typid);
    4422             :     return 0;
    4423             : }
    4424             : 
    4425             : 
    4426             : /*
    4427             :  * get_restriction_variable
    4428             :  *      Examine the args of a restriction clause to see if it's of the
    4429             :  *      form (variable op pseudoconstant) or (pseudoconstant op variable),
    4430             :  *      where "variable" could be either a Var or an expression in vars of a
    4431             :  *      single relation.  If so, extract information about the variable,
    4432             :  *      and also indicate which side it was on and the other argument.
    4433             :  *
    4434             :  * Inputs:
    4435             :  *  root: the planner info
    4436             :  *  args: clause argument list
    4437             :  *  varRelid: see specs for restriction selectivity functions
    4438             :  *
    4439             :  * Outputs: (these are valid only if TRUE is returned)
    4440             :  *  *vardata: gets information about variable (see examine_variable)
    4441             :  *  *other: gets other clause argument, aggressively reduced to a constant
    4442             :  *  *varonleft: set TRUE if variable is on the left, FALSE if on the right
    4443             :  *
    4444             :  * Returns TRUE if a variable is identified, otherwise FALSE.
    4445             :  *
    4446             :  * Note: if there are Vars on both sides of the clause, we must fail, because
    4447             :  * callers are expecting that the other side will act like a pseudoconstant.
    4448             :  */
    4449             : bool
    4450       21628 : get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
    4451             :                          VariableStatData *vardata, Node **other,
    4452             :                          bool *varonleft)
    4453             : {
    4454             :     Node       *left,
    4455             :                *right;
    4456             :     VariableStatData rdata;
    4457             : 
    4458             :     /* Fail if not a binary opclause (probably shouldn't happen) */
    4459       21628 :     if (list_length(args) != 2)
    4460           0 :         return false;
    4461             : 
    4462       21628 :     left = (Node *) linitial(args);
    4463       21628 :     right = (Node *) lsecond(args);
    4464             : 
    4465             :     /*
    4466             :      * Examine both sides.  Note that when varRelid is nonzero, Vars of other
    4467             :      * relations will be treated as pseudoconstants.
    4468             :      */
    4469       21628 :     examine_variable(root, left, varRelid, vardata);
    4470       21628 :     examine_variable(root, right, varRelid, &rdata);
    4471             : 
    4472             :     /*
    4473             :      * If one side is a variable and the other not, we win.
    4474             :      */
    4475       21628 :     if (vardata->rel && rdata.rel == NULL)
    4476             :     {
    4477       17528 :         *varonleft = true;
    4478       17528 :         *other = estimate_expression_value(root, rdata.var);
    4479             :         /* Assume we need no ReleaseVariableStats(rdata) here */
    4480       17528 :         return true;
    4481             :     }
    4482             : 
    4483        4100 :     if (vardata->rel == NULL && rdata.rel)
    4484             :     {
    4485        3936 :         *varonleft = false;
    4486        3936 :         *other = estimate_expression_value(root, vardata->var);
    4487             :         /* Assume we need no ReleaseVariableStats(*vardata) here */
    4488        3936 :         *vardata = rdata;
    4489        3936 :         return true;
    4490             :     }
    4491             : 
    4492             :     /* Oops, clause has wrong structure (probably var op var) */
    4493         164 :     ReleaseVariableStats(*vardata);
    4494         164 :     ReleaseVariableStats(rdata);
    4495             : 
    4496         164 :     return false;
    4497             : }
    4498             : 
    4499             : /*
    4500             :  * get_join_variables
    4501             :  *      Apply examine_variable() to each side of a join clause.
    4502             :  *      Also, attempt to identify whether the join clause has the same
    4503             :  *      or reversed sense compared to the SpecialJoinInfo.
    4504             :  *
    4505             :  * We consider the join clause "normal" if it is "lhs_var OP rhs_var",
    4506             :  * or "reversed" if it is "rhs_var OP lhs_var".  In complicated cases
    4507             :  * where we can't tell for sure, we default to assuming it's normal.
    4508             :  */
    4509             : void
    4510        6061 : get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo,
    4511             :                    VariableStatData *vardata1, VariableStatData *vardata2,
    4512             :                    bool *join_is_reversed)
    4513             : {
    4514             :     Node       *left,
    4515             :                *right;
    4516             : 
    4517        6061 :     if (list_length(args) != 2)
    4518           0 :         elog(ERROR, "join operator should take two arguments");
    4519             : 
    4520        6061 :     left = (Node *) linitial(args);
    4521        6061 :     right = (Node *) lsecond(args);
    4522             : 
    4523        6061 :     examine_variable(root, left, 0, vardata1);
    4524        6061 :     examine_variable(root, right, 0, vardata2);
    4525             : 
    4526       12109 :     if (vardata1->rel &&
    4527        6048 :         bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
    4528        1425 :         *join_is_reversed = true;   /* var1 is on RHS */
    4529        9264 :     else if (vardata2->rel &&
    4530        4628 :              bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
    4531          20 :         *join_is_reversed = true;   /* var2 is on LHS */
    4532             :     else
    4533        4616 :         *join_is_reversed = false;
    4534        6061 : }
    4535             : 
    4536             : /*
    4537             :  * examine_variable
    4538             :  *      Try to look up statistical data about an expression.
    4539             :  *      Fill in a VariableStatData struct to describe the expression.
    4540             :  *
    4541             :  * Inputs:
    4542             :  *  root: the planner info
    4543             :  *  node: the expression tree to examine
    4544             :  *  varRelid: see specs for restriction selectivity functions
    4545             :  *
    4546             :  * Outputs: *vardata is filled as follows:
    4547             :  *  var: the input expression (with any binary relabeling stripped, if
    4548             :  *      it is or contains a variable; but otherwise the type is preserved)
    4549             :  *  rel: RelOptInfo for relation containing variable; NULL if expression
    4550             :  *      contains no Vars (NOTE this could point to a RelOptInfo of a
    4551             :  *      subquery, not one in the current query).
    4552             :  *  statsTuple: the pg_statistic entry for the variable, if one exists;
    4553             :  *      otherwise NULL.
    4554             :  *  freefunc: pointer to a function to release statsTuple with.
    4555             :  *  vartype: exposed type of the expression; this should always match
    4556             :  *      the declared input type of the operator we are estimating for.
    4557             :  *  atttype, atttypmod: actual type/typmod of the "var" expression.  This is
    4558             :  *      commonly the same as the exposed type of the variable argument,
    4559             :  *      but can be different in binary-compatible-type cases.
    4560             :  *  isunique: TRUE if we were able to match the var to a unique index or a
    4561             :  *      single-column DISTINCT clause, implying its values are unique for
    4562             :  *      this query.  (Caution: this should be trusted for statistical
    4563             :  *      purposes only, since we do not check indimmediate nor verify that
    4564             :  *      the exact same definition of equality applies.)
    4565             :  *  acl_ok: TRUE if current user has permission to read the column(s)
    4566             :  *      underlying the pg_statistic entry.  This is consulted by
    4567             :  *      statistic_proc_security_check().
    4568             :  *
    4569             :  * Caller is responsible for doing ReleaseVariableStats() before exiting.
    4570             :  */
    4571             : void
    4572       71904 : examine_variable(PlannerInfo *root, Node *node, int varRelid,
    4573             :                  VariableStatData *vardata)
    4574             : {
    4575             :     Node       *basenode;
    4576             :     Relids      varnos;
    4577             :     RelOptInfo *onerel;
    4578             : 
    4579             :     /* Make sure we don't return dangling pointers in vardata */
    4580       71904 :     MemSet(vardata, 0, sizeof(VariableStatData));
    4581             : 
    4582             :     /* Save the exposed type of the expression */
    4583       71904 :     vardata->vartype = exprType(node);
    4584             : 
    4585             :     /* Look inside any binary-compatible relabeling */
    4586             : 
    4587       71904 :     if (IsA(node, RelabelType))
    4588        1197 :         basenode = (Node *) ((RelabelType *) node)->arg;
    4589             :     else
    4590       70707 :         basenode = node;
    4591             : 
    4592             :     /* Fast path for a simple Var */
    4593             : 
    4594       71904 :     if (IsA(basenode, Var) &&
    4595       17633 :         (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
    4596             :     {
    4597       47370 :         Var        *var = (Var *) basenode;
    4598             : 
    4599             :         /* Set up result fields other than the stats tuple */
    4600       47370 :         vardata->var = basenode; /* return Var without relabeling */
    4601       47370 :         vardata->rel = find_base_rel(root, var->varno);
    4602       47370 :         vardata->atttype = var->vartype;
    4603       47370 :         vardata->atttypmod = var->vartypmod;
    4604       47370 :         vardata->isunique = has_unique_index(vardata->rel, var->varattno);
    4605             : 
    4606             :         /* Try to locate some stats */
    4607       47370 :         examine_simple_variable(root, var, vardata);
    4608             : 
    4609      119274 :         return;
    4610             :     }
    4611             : 
    4612             :     /*
    4613             :      * Okay, it's a more complicated expression.  Determine variable
    4614             :      * membership.  Note that when varRelid isn't zero, only vars of that
    4615             :      * relation are considered "real" vars.
    4616             :      */
    4617       24534 :     varnos = pull_varnos(basenode);
    4618             : 
    4619       24534 :     onerel = NULL;
    4620             : 
    4621       24534 :     switch (bms_membership(varnos))
    4622             :     {
    4623             :         case BMS_EMPTY_SET:
    4624             :             /* No Vars at all ... must be pseudo-constant clause */
    4625       13877 :             break;
    4626             :         case BMS_SINGLETON:
    4627       10343 :             if (varRelid == 0 || bms_is_member(varRelid, varnos))
    4628             :             {
    4629        2097 :                 onerel = find_base_rel(root,
    4630             :                                        (varRelid ? varRelid : bms_singleton_member(varnos)));
    4631        2097 :                 vardata->rel = onerel;
    4632        2097 :                 node = basenode;    /* strip any relabeling */
    4633             :             }
    4634             :             /* else treat it as a constant */
    4635       10343 :             break;
    4636             :         case BMS_MULTIPLE:
    4637         314 :             if (varRelid == 0)
    4638             :             {
    4639             :                 /* treat it as a variable of a join relation */
    4640         224 :                 vardata->rel = find_join_rel(root, varnos);
    4641         224 :                 node = basenode;    /* strip any relabeling */
    4642             :             }
    4643          90 :             else if (bms_is_member(varRelid, varnos))
    4644             :             {
    4645             :                 /* ignore the vars belonging to other relations */
    4646          67 :                 vardata->rel = find_base_rel(root, varRelid);
    4647          67 :                 node = basenode;    /* strip any relabeling */
    4648             :                 /* note: no point in expressional-index search here */
    4649             :             }
    4650             :             /* else treat it as a constant */
    4651         314 :             break;
    4652             :     }
    4653             : 
    4654       24534 :     bms_free(varnos);
    4655             : 
    4656       24534 :     vardata->var = node;
    4657       24534 :     vardata->atttype = exprType(node);
    4658       24534 :     vardata->atttypmod = exprTypmod(node);
    4659             : 
    4660       24534 :     if (onerel)
    4661             :     {
    4662             :         /*
    4663             :          * We have an expression in vars of a single relation.  Try to match
    4664             :          * it to expressional index columns, in hopes of finding some
    4665             :          * statistics.
    4666             :          *
    4667             :          * XXX it's conceivable that there are multiple matches with different
    4668             :          * index opfamilies; if so, we need to pick one that matches the
    4669             :          * operator we are estimating for.  FIXME later.
    4670             :          */
    4671             :         ListCell   *ilist;
    4672             : 
    4673        4994 :         foreach(ilist, onerel->indexlist)
    4674             :         {
    4675        2911 :             IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
    4676             :             ListCell   *indexpr_item;
    4677             :             int         pos;
    4678             : 
    4679        2911 :             indexpr_item = list_head(index->indexprs);
    4680        2911 :             if (indexpr_item == NULL)
    4681        2625 :                 continue;       /* no expressions here... */
    4682             : 
    4683         558 :             for (pos = 0; pos < index->ncolumns; pos++)
    4684             :             {
    4685         286 :                 if (index->indexkeys[pos] == 0)
    4686             :                 {
    4687             :                     Node       *indexkey;
    4688             : 
    4689         286 :                     if (indexpr_item == NULL)
    4690           0 :                         elog(ERROR, "too few entries in indexprs list");
    4691         286 :                     indexkey = (Node *) lfirst(indexpr_item);
    4692         286 :                     if (indexkey && IsA(indexkey, RelabelType))
    4693           0 :                         indexkey = (Node *) ((RelabelType *) indexkey)->arg;
    4694         286 :                     if (equal(node, indexkey))
    4695             :                     {
    4696             :                         /*
    4697             :                          * Found a match ... is it a unique index? Tests here
    4698             :                          * should match has_unique_index().
    4699             :                          */
    4700         158 :                         if (index->unique &&
    4701         130 :                             index->ncolumns == 1 &&
    4702          65 :                             (index->indpred == NIL || index->predOK))
    4703          65 :                             vardata->isunique = true;
    4704             : 
    4705             :                         /*
    4706             :                          * Has it got stats?  We only consider stats for
    4707             :                          * non-partial indexes, since partial indexes probably
    4708             :                          * don't reflect whole-relation statistics; the above
    4709             :                          * check for uniqueness is the only info we take from
    4710             :                          * a partial index.
    4711             :                          *
    4712             :                          * An index stats hook, however, must make its own
    4713             :                          * decisions about what to do with partial indexes.
    4714             :                          */
    4715          93 :                         if (get_index_stats_hook &&
    4716           0 :                             (*get_index_stats_hook) (root, index->indexoid,
    4717             :                                                      pos + 1, vardata))
    4718             :                         {
    4719             :                             /*
    4720             :                              * The hook took control of acquiring a stats
    4721             :                              * tuple.  If it did supply a tuple, it'd better
    4722             :                              * have supplied a freefunc.
    4723             :                              */
    4724           0 :                             if (HeapTupleIsValid(vardata->statsTuple) &&
    4725           0 :                                 !vardata->freefunc)
    4726           0 :                                 elog(ERROR, "no function provided to release variable stats with");
    4727             :                         }
    4728          93 :                         else if (index->indpred == NIL)
    4729             :                         {
    4730          93 :                             vardata->statsTuple =
    4731          93 :                                 SearchSysCache3(STATRELATTINH,
    4732             :                                                 ObjectIdGetDatum(index->indexoid),
    4733             :                                                 Int16GetDatum(pos + 1),
    4734             :                                                 BoolGetDatum(false));
    4735          93 :                             vardata->freefunc = ReleaseSysCache;
    4736             : 
    4737          93 :                             if (HeapTupleIsValid(vardata->statsTuple))
    4738             :                             {
    4739             :                                 /* Get index's table for permission check */
    4740             :                                 RangeTblEntry *rte;
    4741             : 
    4742          14 :                                 rte = planner_rt_fetch(index->rel->relid, root);
    4743          14 :                                 Assert(rte->rtekind == RTE_RELATION);
    4744             : 
    4745             :                                 /*
    4746             :                                  * For simplicity, we insist on the whole
    4747             :                                  * table being selectable, rather than trying
    4748             :                                  * to identify which column(s) the index
    4749             :                                  * depends on.
    4750             :                                  */
    4751          14 :                                 vardata->acl_ok =
    4752          14 :                                     (pg_class_aclcheck(rte->relid, GetUserId(),
    4753          14 :                                                        ACL_SELECT) == ACLCHECK_OK);
    4754             :                             }
    4755             :                             else
    4756             :                             {
    4757             :                                 /* suppress leakproofness checks later */
    4758          79 :                                 vardata->acl_ok = true;
    4759             :                             }
    4760             :                         }
    4761          93 :                         if (vardata->statsTuple)
    4762          14 :                             break;
    4763             :                     }
    4764         272 :                     indexpr_item = lnext(indexpr_item);
    4765             :                 }
    4766             :             }
    4767         286 :             if (vardata->statsTuple)
    4768          14 :                 break;
    4769             :         }
    4770             :     }
    4771             : }
    4772             : 
    4773             : /*
    4774             :  * examine_simple_variable
    4775             :  *      Handle a simple Var for examine_variable
    4776             :  *
    4777             :  * This is split out as a subroutine so that we can recurse to deal with
    4778             :  * Vars referencing subqueries.
    4779             :  *
    4780             :  * We already filled in all the fields of *vardata except for the stats tuple.
    4781             :  */
    4782             : static void
    4783       47482 : examine_simple_variable(PlannerInfo *root, Var *var,
    4784             :                         VariableStatData *vardata)
    4785             : {
    4786       47482 :     RangeTblEntry *rte = root->simple_rte_array[var->varno];
    4787             : 
    4788       47482 :     Assert(IsA(rte, RangeTblEntry));
    4789             : 
    4790       47482 :     if (get_relation_stats_hook &&
    4791           0 :         (*get_relation_stats_hook) (root, rte, var->varattno, vardata))
    4792             :     {
    4793             :         /*
    4794             :          * The hook took control of acquiring a stats tuple.  If it did supply
    4795             :          * a tuple, it'd better have supplied a freefunc.
    4796             :          */
    4797           0 :         if (HeapTupleIsValid(vardata->statsTuple) &&
    4798           0 :             !vardata->freefunc)
    4799           0 :             elog(ERROR, "no function provided to release variable stats with");
    4800             :     }
    4801       47482 :     else if (rte->rtekind == RTE_RELATION)
    4802             :     {
    4803             :         /*
    4804             :          * Plain table or parent of an inheritance appendrel, so look up the
    4805             :          * column in pg_statistic
    4806             :          */
    4807       45238 :         vardata->statsTuple = SearchSysCache3(STATRELATTINH,
    4808             :                                               ObjectIdGetDatum(rte->relid),
    4809             :                                               Int16GetDatum(var->varattno),
    4810             :                                               BoolGetDatum(rte->inh));
    4811       45238 :         vardata->freefunc = ReleaseSysCache;
    4812             : 
    4813       45238 :         if (HeapTupleIsValid(vardata->statsTuple))
    4814             :         {
    4815             :             /* check if user has permission to read this column */
    4816       19024 :             vardata->acl_ok =
    4817       19024 :                 (pg_class_aclcheck(rte->relid, GetUserId(),
    4818       19069 :                                    ACL_SELECT) == ACLCHECK_OK) ||
    4819          45 :                 (pg_attribute_aclcheck(rte->relid, var->varattno, GetUserId(),
    4820             :                                        ACL_SELECT) == ACLCHECK_OK);
    4821             :         }
    4822             :         else
    4823             :         {
    4824             :             /* suppress any possible leakproofness checks later */
    4825       26214 :             vardata->acl_ok = true;
    4826             :         }
    4827             :     }
    4828        2244 :     else if (rte->rtekind == RTE_SUBQUERY && !rte->inh)
    4829             :     {
    4830             :         /*
    4831             :          * Plain subquery (not one that was converted to an appendrel).
    4832             :          */
    4833         661 :         Query      *subquery = rte->subquery;
    4834             :         RelOptInfo *rel;
    4835             :         TargetEntry *ste;
    4836             : 
    4837             :         /*
    4838             :          * Punt if it's a whole-row var rather than a plain column reference.
    4839             :          */
    4840         661 :         if (var->varattno == InvalidAttrNumber)
    4841           0 :             return;
    4842             : 
    4843             :         /*
    4844             :          * Punt if subquery uses set operations or GROUP BY, as these will
    4845             :          * mash underlying columns' stats beyond recognition.  (Set ops are
    4846             :          * particularly nasty; if we forged ahead, we would return stats
    4847             :          * relevant to only the leftmost subselect...)  DISTINCT is also
    4848             :          * problematic, but we check that later because there is a possibility
    4849             :          * of learning something even with it.
    4850             :          */
    4851        1308 :         if (subquery->setOperations ||
    4852         647 :             subquery->groupClause)
    4853          64 :             return;
    4854             : 
    4855             :         /*
    4856             :          * OK, fetch RelOptInfo for subquery.  Note that we don't change the
    4857             :          * rel returned in vardata, since caller expects it to be a rel of the
    4858             :          * caller's query level.  Because we might already be recursing, we
    4859             :          * can't use that rel pointer either, but have to look up the Var's
    4860             :          * rel afresh.
    4861             :          */
    4862         597 :         rel = find_base_rel(root, var->varno);
    4863             : 
    4864             :         /* If the subquery hasn't been planned yet, we have to punt */
    4865         597 :         if (rel->subroot == NULL)
    4866           0 :             return;
    4867         597 :         Assert(IsA(rel->subroot, PlannerInfo));
    4868             : 
    4869             :         /*
    4870             :          * Switch our attention to the subquery as mangled by the planner. It
    4871             :          * was okay to look at the pre-planning version for the tests above,
    4872             :          * but now we need a Var that will refer to the subroot's live
    4873             :          * RelOptInfos.  For instance, if any subquery pullup happened during
    4874             :          * planning, Vars in the targetlist might have gotten replaced, and we
    4875             :          * need to see the replacement expressions.
    4876             :          */
    4877         597 :         subquery = rel->subroot->parse;
    4878         597 :         Assert(IsA(subquery, Query));
    4879             : 
    4880             :         /* Get the subquery output expression referenced by the upper Var */
    4881         597 :         ste = get_tle_by_resno(subquery->targetList, var->varattno);
    4882         597 :         if (ste == NULL || ste->resjunk)
    4883           0 :             elog(ERROR, "subquery %s does not have attribute %d",
    4884             :                  rte->eref->aliasname, var->varattno);
    4885         597 :         var = (Var *) ste->expr;
    4886             : 
    4887             :         /*
    4888             :          * If subquery uses DISTINCT, we can't make use of any stats for the
    4889             :          * variable ... but, if it's the only DISTINCT column, we are entitled
    4890             :          * to consider it unique.  We do the test this way so that it works
    4891             :          * for cases involving DISTINCT ON.
    4892             :          */
    4893         597 :         if (subquery->distinctClause)
    4894             :         {
    4895          50 :             if (list_length(subquery->distinctClause) == 1 &&
    4896          17 :                 targetIsInSortList(ste, InvalidOid, subquery->distinctClause))
    4897          17 :                 vardata->isunique = true;
    4898             :             /* cannot go further */
    4899          33 :             return;
    4900             :         }
    4901             : 
    4902             :         /*
    4903             :          * If the sub-query originated from a view with the security_barrier
    4904             :          * attribute, we must not look at the variable's statistics, though it
    4905             :          * seems all right to notice the existence of a DISTINCT clause. So
    4906             :          * stop here.
    4907             :          *
    4908             :          * This is probably a harsher restriction than necessary; it's
    4909             :          * certainly OK for the selectivity estimator (which is a C function,
    4910             :          * and therefore omnipotent anyway) to look at the statistics.  But
    4911             :          * many selectivity estimators will happily *invoke the operator
    4912             :          * function* to try to work out a good estimate - and that's not OK.
    4913             :          * So for now, don't dig down for stats.
    4914             :          */
    4915         564 :         if (rte->security_barrier)
    4916          17 :             return;
    4917             : 
    4918             :         /* Can only handle a simple Var of subquery's query level */
    4919         659 :         if (var && IsA(var, Var) &&
    4920         112 :             var->varlevelsup == 0)
    4921             :         {
    4922             :             /*
    4923             :              * OK, recurse into the subquery.  Note that the original setting
    4924             :              * of vardata->isunique (which will surely be false) is left
    4925             :              * unchanged in this situation.  That's what we want, since even
    4926             :              * if the underlying column is unique, the subquery may have
    4927             :              * joined to other tables in a way that creates duplicates.
    4928             :              */
    4929         112 :             examine_simple_variable(rel->subroot, var, vardata);
    4930             :         }
    4931             :     }
    4932             :     else
    4933             :     {
    4934             :         /*
    4935             :          * Otherwise, the Var comes from a FUNCTION, VALUES, or CTE RTE.  (We
    4936             :          * won't see RTE_JOIN here because join alias Vars have already been
    4937             :          * flattened.)  There's not much we can do with function outputs, but
    4938             :          * maybe someday try to be smarter about VALUES and/or CTEs.
    4939             :          */
    4940             :     }
    4941             : }
    4942             : 
    4943             : /*
    4944             :  * Check whether it is permitted to call func_oid passing some of the
    4945             :  * pg_statistic data in vardata.  We allow this either if the user has SELECT
    4946             :  * privileges on the table or column underlying the pg_statistic data or if
    4947             :  * the function is marked leak-proof.
    4948             :  */
    4949             : bool
    4950       13597 : statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
    4951             : {
    4952       13597 :     if (vardata->acl_ok)
    4953       13577 :         return true;
    4954             : 
    4955          20 :     if (!OidIsValid(func_oid))
    4956           0 :         return false;
    4957             : 
    4958          20 :     if (get_func_leakproof(func_oid))
    4959          12 :         return true;
    4960             : 
    4961           8 :     ereport(DEBUG2,
    4962             :             (errmsg_internal("not using statistics because function \"%s\" is not leak-proof",
    4963             :                              get_func_name(func_oid))));
    4964           8 :     return false;
    4965             : }
    4966             : 
    4967             : /*
    4968             :  * get_variable_numdistinct
    4969             :  *    Estimate the number of distinct values of a variable.
    4970             :  *
    4971             :  * vardata: results of examine_variable
    4972             :  * *isdefault: set to TRUE if the result is a default rather than based on
    4973             :  * anything meaningful.
    4974             :  *
    4975             :  * NB: be careful to produce a positive integral result, since callers may
    4976             :  * compare the result to exact integer counts, or might divide by it.
    4977             :  */
    4978             : double
    4979       29447 : get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
    4980             : {
    4981             :     double      stadistinct;
    4982       29447 :     double      stanullfrac = 0.0;
    4983             :     double      ntuples;
    4984             : 
    4985       29447 :     *isdefault = false;
    4986             : 
    4987             :     /*
    4988             :      * Determine the stadistinct value to use.  There are cases where we can
    4989             :      * get an estimate even without a pg_statistic entry, or can get a better
    4990             :      * value than is in pg_statistic.  Grab stanullfrac too if we can find it
    4991             :      * (otherwise, assume no nulls, for lack of any better idea).
    4992             :      */
    4993       29447 :     if (HeapTupleIsValid(vardata->statsTuple))
    4994             :     {
    4995             :         /* Use the pg_statistic entry */
    4996             :         Form_pg_statistic stats;
    4997             : 
    4998       13442 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
    4999       13442 :         stadistinct = stats->stadistinct;
    5000       13442 :         stanullfrac = stats->stanullfrac;
    5001             :     }
    5002       16005 :     else if (vardata->vartype == BOOLOID)
    5003             :     {
    5004             :         /*
    5005             :          * Special-case boolean columns: presumably, two distinct values.
    5006             :          *
    5007             :          * Are there any other datatypes we should wire in special estimates
    5008             :          * for?
    5009             :          */
    5010          20 :         stadistinct = 2.0;
    5011             :     }
    5012       15985 :     else if (vardata->rel && vardata->rel->rtekind == RTE_VALUES)
    5013             :     {
    5014             :         /*
    5015             :          * If the Var represents a column of a VALUES RTE, assume it's unique.
    5016             :          * This could of course be very wrong, but it should tend to be true
    5017             :          * in well-written queries.  We could consider examining the VALUES'
    5018             :          * contents to get some real statistics; but that only works if the
    5019             :          * entries are all constants, and it would be pretty expensive anyway.
    5020             :          */
    5021         171 :         stadistinct = -1.0;     /* unique (and all non null) */
    5022             :     }
    5023             :     else
    5024             :     {
    5025             :         /*
    5026             :          * We don't keep statistics for system columns, but in some cases we
    5027             :          * can infer distinctness anyway.
    5028             :          */
    5029       15814 :         if (vardata->var && IsA(vardata->var, Var))
    5030             :         {
    5031       15094 :             switch (((Var *) vardata->var)->varattno)
    5032             :             {
    5033             :                 case ObjectIdAttributeNumber:
    5034             :                 case SelfItemPointerAttributeNumber:
    5035        5373 :                     stadistinct = -1.0; /* unique (and all non null) */
    5036        5373 :                     break;
    5037             :                 case TableOidAttributeNumber:
    5038         207 :                     stadistinct = 1.0;  /* only 1 value */
    5039         207 :                     break;
    5040             :                 default:
    5041        9514 :                     stadistinct = 0.0;  /* means "unknown" */
    5042        9514 :                     break;
    5043             :             }
    5044       15094 :         }
    5045             :         else
    5046         720 :             stadistinct = 0.0;  /* means "unknown" */
    5047             : 
    5048             :         /*
    5049             :          * XXX consider using estimate_num_groups on expressions?
    5050             :          */
    5051             :     }
    5052             : 
    5053             :     /*
    5054             :      * If there is a unique index or DISTINCT clause for the variable, assume
    5055             :      * it is unique no matter what pg_statistic says; the statistics could be
    5056             :      * out of date, or we might have found a partial unique index that proves
    5057             :      * the var is unique for this query.  However, we'd better still believe
    5058             :      * the null-fraction statistic.
    5059             :      */
    5060       29447 :     if (vardata->isunique)
    5061        6289 :         stadistinct = -1.0 * (1.0 - stanullfrac);
    5062             : 
    5063             :     /*
    5064             :      * If we had an absolute estimate, use that.
    5065             :      */
    5066       29447 :     if (stadistinct > 0.0)
    5067        5749 :         return clamp_row_est(stadistinct);
    5068             : 
    5069             :     /*
    5070             :      * Otherwise we need to get the relation size; punt if not available.
    5071             :      */
    5072       23698 :     if (vardata->rel == NULL)
    5073             :     {
    5074          21 :         *isdefault = true;
    5075          21 :         return DEFAULT_NUM_DISTINCT;
    5076             :     }
    5077       23677 :     ntuples = vardata->rel->tuples;
    5078       23677 :     if (ntuples <= 0.0)
    5079             :     {
    5080          79 :         *isdefault = true;
    5081          79 :         return DEFAULT_NUM_DISTINCT;
    5082             :     }
    5083             : 
    5084             :     /*
    5085             :      * If we had a relative estimate, use that.
    5086             :      */
    5087       23598 :     if (stadistinct < 0.0)
    5088       13929 :         return clamp_row_est(-stadistinct * ntuples);
    5089             : 
    5090             :     /*
    5091             :      * With no data, estimate ndistinct = ntuples if the table is small, else
    5092             :      * use default.  We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
    5093             :      * that the behavior isn't discontinuous.
    5094             :      */
    5095        9669 :     if (ntuples < DEFAULT_NUM_DISTINCT)
    5096        2424 :         return clamp_row_est(ntuples);
    5097             : 
    5098        7245 :     *isdefault = true;
    5099        7245 :     return DEFAULT_NUM_DISTINCT;
    5100             : }
    5101             : 
    5102             : /*
    5103             :  * get_variable_range
    5104             :  *      Estimate the minimum and maximum value of the specified variable.
    5105             :  *      If successful, store values in *min and *max, and return TRUE.
    5106             :  *      If no data available, return FALSE.
    5107             :  *
    5108             :  * sortop is the "<" comparison operator to use.  This should generally
    5109             :  * be "<" not ">", as only the former is likely to be found in pg_statistic.
    5110             :  */
    5111             : static bool
    5112        4154 : get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop,
    5113             :                    Datum *min, Datum *max)
    5114             : {
    5115        4154 :     Datum       tmin = 0;
    5116        4154 :     Datum       tmax = 0;
    5117        4154 :     bool        have_data = false;
    5118             :     int16       typLen;
    5119             :     bool        typByVal;
    5120             :     Oid         opfuncoid;
    5121             :     AttStatsSlot sslot;
    5122             :     int         i;
    5123             : 
    5124             :     /*
    5125             :      * XXX It's very tempting to try to use the actual column min and max, if
    5126             :      * we can get them relatively-cheaply with an index probe.  However, since
    5127             :      * this function is called many times during join planning, that could
    5128             :      * have unpleasant effects on planning speed.  Need more investigation
    5129             :      * before enabling this.
    5130             :      */
    5131             : #ifdef NOT_USED
    5132             :     if (get_actual_variable_range(root, vardata, sortop, min, max))
    5133             :         return true;
    5134             : #endif
    5135             : 
    5136        4154 :     if (!HeapTupleIsValid(vardata->statsTuple))
    5137             :     {
    5138             :         /* no stats available, so default result */
    5139        3010 :         return false;
    5140             :     }
    5141             : 
    5142             :     /*
    5143             :      * If we can't apply the sortop to the stats data, just fail.  In
    5144             :      * principle, if there's a histogram and no MCVs, we could return the
    5145             :      * histogram endpoints without ever applying the sortop ... but it's
    5146             :      * probably not worth trying, because whatever the caller wants to do with
    5147             :      * the endpoints would likely fail the security check too.
    5148             :      */
    5149        1144 :     if (!statistic_proc_security_check(vardata,
    5150             :                                        (opfuncoid = get_opcode(sortop))))
    5151           0 :         return false;
    5152             : 
    5153        1144 :     get_typlenbyval(vardata->atttype, &typLen, &typByVal);
    5154             : 
    5155             :     /*
    5156             :      * If there is a histogram, grab the first and last values.
    5157             :      *
    5158             :      * If there is a histogram that is sorted with some other operator than
    5159             :      * the one we want, fail --- this suggests that there is data we can't
    5160             :      * use.
    5161             :      */
    5162        1144 :     if (get_attstatsslot(&sslot, vardata->statsTuple,
    5163             :                          STATISTIC_KIND_HISTOGRAM, sortop,
    5164             :                          ATTSTATSSLOT_VALUES))
    5165             :     {
    5166         828 :         if (sslot.nvalues > 0)
    5167             :         {
    5168         828 :             tmin = datumCopy(sslot.values[0], typByVal, typLen);
    5169         828 :             tmax = datumCopy(sslot.values[sslot.nvalues - 1], typByVal, typLen);
    5170         828 :             have_data = true;
    5171             :         }
    5172         828 :         free_attstatsslot(&sslot);
    5173             :     }
    5174         316 :     else if (get_attstatsslot(&sslot, vardata->statsTuple,
    5175             :                               STATISTIC_KIND_HISTOGRAM, InvalidOid,
    5176             :                               0))
    5177             :     {
    5178           0 :         free_attstatsslot(&sslot);
    5179           0 :         return false;
    5180             :     }
    5181             : 
    5182             :     /*
    5183             :      * If we have most-common-values info, look for extreme MCVs.  This is
    5184             :      * needed even if we also have a histogram, since the histogram excludes
    5185             :      * the MCVs.  However, usually the MCVs will not be the extreme values, so
    5186             :      * avoid unnecessary data copying.
    5187             :      */
    5188        1144 :     if (get_attstatsslot(&sslot, vardata->statsTuple,
    5189             :                          STATISTIC_KIND_MCV, InvalidOid,
    5190             :                          ATTSTATSSLOT_VALUES))
    5191             :     {
    5192         834 :         bool        tmin_is_mcv = false;
    5193         834 :         bool        tmax_is_mcv = false;
    5194             :         FmgrInfo    opproc;
    5195             : 
    5196         834 :         fmgr_info(opfuncoid, &opproc);
    5197             : 
    5198       15441 :         for (i = 0; i < sslot.nvalues; i++)
    5199             :         {
    5200       14607 :             if (!have_data)
    5201             :             {
    5202         310 :                 tmin = tmax = sslot.values[i];
    5203         310 :                 tmin_is_mcv = tmax_is_mcv = have_data = true;
    5204         310 :                 continue;
    5205             :             }
    5206       14297 :             if (DatumGetBool(FunctionCall2Coll(&opproc,
    5207             :                                                DEFAULT_COLLATION_OID,
    5208             :                                                sslot.values[i], tmin)))
    5209             :             {
    5210         459 :                 tmin = sslot.values[i];
    5211         459 :                 tmin_is_mcv = true;
    5212             :             }
    5213       14297 :             if (DatumGetBool(FunctionCall2Coll(&opproc,
    5214             :                                                DEFAULT_COLLATION_OID,
    5215             :                                                tmax, sslot.values[i])))
    5216             :             {
    5217        1740 :                 tmax = sslot.values[i];
    5218        1740 :                 tmax_is_mcv = true;
    5219             :             }
    5220             :         }
    5221         834 :         if (tmin_is_mcv)
    5222         713 :             tmin = datumCopy(tmin, typByVal, typLen);
    5223         834 :         if (tmax_is_mcv)
    5224         357 :             tmax = datumCopy(tmax, typByVal, typLen);
    5225         834 :         free_attstatsslot(&sslot);
    5226             :     }
    5227             : 
    5228        1144 :     *min = tmin;
    5229        1144 :     *max = tmax;
    5230        1144 :     return have_data;
    5231             : }
    5232             : 
    5233             : 
    5234             : /*
    5235             :  * get_actual_variable_range
    5236             :  *      Attempt to identify the current *actual* minimum and/or maximum
    5237             :  *      of the specified variable, by looking for a suitable btree index
    5238             :  *      and fetching its low and/or high values.
    5239             :  *      If successful, store values in *min and *max, and return TRUE.
    5240             :  *      (Either pointer can be NULL if that endpoint isn't needed.)
    5241             :  *      If no data available, return FALSE.
    5242             :  *
    5243             :  * sortop is the "<" comparison operator to use.
    5244             :  */
    5245             : static bool
    5246         924 : get_actual_variable_range(PlannerInfo *root, VariableStatData *vardata,
    5247             :                           Oid sortop,
    5248             :                           Datum *min, Datum *max)
    5249             : {
    5250         924 :     bool        have_data = false;
    5251         924 :     RelOptInfo *rel = vardata->rel;
    5252             :     RangeTblEntry *rte;
    5253             :     ListCell   *lc;
    5254             : 
    5255             :     /* No hope if no relation or it doesn't have indexes */
    5256         924 :     if (rel == NULL || rel->indexlist == NIL)
    5257          11 :         return false;
    5258             :     /* If it has indexes it must be a plain relation */
    5259         913 :     rte = root->simple_rte_array[rel->relid];
    5260         913 :     Assert(rte->rtekind == RTE_RELATION);
    5261             : 
    5262             :     /* Search through the indexes to see if any match our problem */
    5263        2335 :     foreach(lc, rel->indexlist)
    5264             :     {
    5265        1988 :         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
    5266             :         ScanDirection indexscandir;
    5267             : 
    5268             :         /* Ignore non-btree indexes */
    5269        1988 :         if (index->relam != BTREE_AM_OID)
    5270           0 :             continue;
    5271             : 
    5272             :         /*
    5273             :          * Ignore partial indexes --- we only want stats that cover the entire
    5274             :          * relation.
    5275             :          */
    5276        1988 :         if (index->indpred != NIL)
    5277          17 :             continue;
    5278             : 
    5279             :         /*
    5280             :          * The index list might include hypothetical indexes inserted by a
    5281             :          * get_relation_info hook --- don't try to access them.
    5282             :          */
    5283        1971 :         if (index->hypothetical)
    5284           0 :             continue;
    5285             : 
    5286             :         /*
    5287             :          * The first index column must match the desired variable and sort
    5288             :          * operator --- but we can use a descending-order index.
    5289             :          */
    5290        1971 :         if (!match_index_to_operand(vardata->var, 0, index))
    5291        1405 :             continue;
    5292         566 :         switch (get_op_opfamily_strategy(sortop, index->sortopfamily[0]))
    5293             :         {
    5294             :             case BTLessStrategyNumber:
    5295         566 :                 if (index->reverse_sort[0])
    5296           0 :                     indexscandir = BackwardScanDirection;
    5297             :                 else
    5298         566 :                     indexscandir = ForwardScanDirection;
    5299         566 :                 break;
    5300             :             case BTGreaterStrategyNumber:
    5301           0 :                 if (index->reverse_sort[0])
    5302           0 :                     indexscandir = ForwardScanDirection;
    5303             :                 else
    5304           0 :                     indexscandir = BackwardScanDirection;
    5305           0 :                 break;
    5306             :             default:
    5307             :                 /* index doesn't match the sortop */
    5308           0 :                 continue;
    5309             :         }
    5310             : 
    5311             :         /*
    5312             :          * Found a suitable index to extract data from.  We'll need an EState
    5313             :          * and a bunch of other infrastructure.
    5314             :          */
    5315             :         {
    5316             :             EState     *estate;
    5317             :             ExprContext *econtext;
    5318             :             MemoryContext tmpcontext;
    5319             :             MemoryContext oldcontext;
    5320             :             Relation    heapRel;
    5321             :             Relation    indexRel;
    5322             :             IndexInfo  *indexInfo;
    5323             :             TupleTableSlot *slot;
    5324             :             int16       typLen;
    5325             :             bool        typByVal;
    5326             :             ScanKeyData scankeys[1];
    5327             :             IndexScanDesc index_scan;
    5328             :             HeapTuple   tup;
    5329             :             Datum       values[INDEX_MAX_KEYS];
    5330             :             bool        isnull[INDEX_MAX_KEYS];
    5331             :             SnapshotData SnapshotDirty;
    5332             : 
    5333         566 :             estate = CreateExecutorState();
    5334         566 :             econtext = GetPerTupleExprContext(estate);
    5335             :             /* Make sure any cruft is generated in the econtext's memory */
    5336         566 :             tmpcontext = econtext->ecxt_per_tuple_memory;
    5337         566 :             oldcontext = MemoryContextSwitchTo(tmpcontext);
    5338             : 
    5339             :             /*
    5340             :              * Open the table and index so we can read from them.  We should
    5341             :              * already have at least AccessShareLock on the table, but not
    5342             :              * necessarily on the index.
    5343             :              */
    5344         566 :             heapRel = heap_open(rte->relid, NoLock);
    5345         566 :             indexRel = index_open(index->indexoid, AccessShareLock);
    5346             : 
    5347             :             /* extract index key information from the index's pg_index info */
    5348         566 :             indexInfo = BuildIndexInfo(indexRel);
    5349             : 
    5350             :             /* some other stuff */
    5351         566 :             slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRel));
    5352         566 :             econtext->ecxt_scantuple = slot;
    5353         566 :             get_typlenbyval(vardata->atttype, &typLen, &typByVal);
    5354         566 :             InitDirtySnapshot(SnapshotDirty);
    5355             : 
    5356             :             /* set up an IS NOT NULL scan key so that we ignore nulls */
    5357         566 :             ScanKeyEntryInitialize(&scankeys[0],
    5358             :                                    SK_ISNULL | SK_SEARCHNOTNULL,
    5359             :                                    1,   /* index col to scan */
    5360             :                                    InvalidStrategy, /* no strategy */
    5361             :                                    InvalidOid,  /* no strategy subtype */
    5362             :                                    InvalidOid,  /* no collation */
    5363             :                                    InvalidOid,  /* no reg proc for this */
    5364             :                                    (Datum) 0);  /* constant */
    5365             : 
    5366         566 :             have_data = true;
    5367             : 
    5368             :             /* If min is requested ... */
    5369         566 :             if (min)
    5370             :             {
    5371             :                 /*
    5372             :                  * In principle, we should scan the index with our current
    5373             :                  * active snapshot, which is the best approximation we've got
    5374             :                  * to what the query will see when executed.  But that won't
    5375             :                  * be exact if a new snap is taken before running the query,
    5376             :                  * and it can be very expensive if a lot of uncommitted rows
    5377             :                  * exist at the end of the index (because we'll laboriously
    5378             :                  * fetch each one and reject it).  What seems like a good
    5379             :                  * compromise is to use SnapshotDirty.  That will accept
    5380             :                  * uncommitted rows, and thus avoid fetching multiple heap
    5381             :                  * tuples in this scenario.  On the other hand, it will reject
    5382             :                  * known-dead rows, and thus not give a bogus answer when the
    5383             :                  * extreme value has been deleted; that case motivates not
    5384             :                  * using SnapshotAny here.
    5385             :                  */
    5386         334 :                 index_scan = index_beginscan(heapRel, indexRel, &SnapshotDirty,
    5387             :                                              1, 0);
    5388         334 :                 index_rescan(index_scan, scankeys, 1, NULL, 0);
    5389             : 
    5390             :                 /* Fetch first tuple in sortop's direction */
    5391         334 :                 if ((tup = index_getnext(index_scan,
    5392             :                                          indexscandir)) != NULL)
    5393             :                 {
    5394             :                     /* Extract the index column values from the heap tuple */
    5395         334 :                     ExecStoreTuple(tup, slot, InvalidBuffer, false);
    5396         334 :                     FormIndexDatum(indexInfo, slot, estate,
    5397             :                                    values, isnull);
    5398             : 
    5399             :                     /* Shouldn't have got a null, but be careful */
    5400         334 :                     if (isnull[0])
    5401           0 :                         elog(ERROR, "found unexpected null value in index \"%s\"",
    5402             :                              RelationGetRelationName(indexRel));
    5403             : 
    5404             :                     /* Copy the index column value out to caller's context */
    5405         334 :                     MemoryContextSwitchTo(oldcontext);
    5406         334 :                     *min = datumCopy(values[0], typByVal, typLen);
    5407         334 :                     MemoryContextSwitchTo(tmpcontext);
    5408             :                 }
    5409             :                 else
    5410           0 :                     have_data = false;
    5411             : 
    5412         334 :                 index_endscan(index_scan);
    5413             :             }
    5414             : 
    5415             :             /* If max is requested, and we didn't find the index is empty */
    5416         566 :             if (max && have_data)
    5417             :             {
    5418         256 :                 index_scan = index_beginscan(heapRel, indexRel, &SnapshotDirty,
    5419             :                                              1, 0);
    5420         256 :                 index_rescan(index_scan, scankeys, 1, NULL, 0);
    5421             : 
    5422             :                 /* Fetch first tuple in reverse direction */
    5423         256 :                 if ((tup = index_getnext(index_scan,
    5424         256 :                                          -indexscandir)) != NULL)
    5425             :                 {
    5426             :                     /* Extract the index column values from the heap tuple */
    5427         256 :                     ExecStoreTuple(tup, slot, InvalidBuffer, false);
    5428         256 :                     FormIndexDatum(indexInfo, slot, estate,
    5429             :                                    values, isnull);
    5430             : 
    5431             :                     /* Shouldn't have got a null, but be careful */
    5432         256 :                     if (isnull[0])
    5433           0 :                         elog(ERROR, "found unexpected null value in index \"%s\"",
    5434             :                              RelationGetRelationName(indexRel));
    5435             : 
    5436             :                     /* Copy the index column value out to caller's context */
    5437         256 :                     MemoryContextSwitchTo(oldcontext);
    5438         256 :                     *max = datumCopy(values[0], typByVal, typLen);
    5439         256 :                     MemoryContextSwitchTo(tmpcontext);
    5440             :                 }
    5441             :                 else
    5442           0 :                     have_data = false;
    5443             : 
    5444         256 :                 index_endscan(index_scan);
    5445             :             }
    5446             : 
    5447             :             /* Clean everything up */
    5448         566 :             ExecDropSingleTupleTableSlot(slot);
    5449             : 
    5450         566 :             index_close(indexRel, AccessShareLock);
    5451         566 :             heap_close(heapRel, NoLock);
    5452             : 
    5453         566 :             MemoryContextSwitchTo(oldcontext);
    5454         566 :             FreeExecutorState(estate);
    5455             : 
    5456             :             /* And we're done */
    5457         566 :             break;
    5458             :         }
    5459             :     }
    5460             : 
    5461         913 :     return have_data;
    5462             : }
    5463             : 
    5464             : /*
    5465             :  * find_join_input_rel
    5466             :  *      Look up the input relation for a join.
    5467             :  *
    5468             :  * We assume that the input relation's RelOptInfo must have been constructed
    5469             :  * already.
    5470             :  */
    5471             : static RelOptInfo *
    5472         306 : find_join_input_rel(PlannerInfo *root, Relids relids)
    5473             : {
    5474         306 :     RelOptInfo *rel = NULL;
    5475             : 
    5476         306 :     switch (bms_membership(relids))
    5477             :     {
    5478             :         case BMS_EMPTY_SET:
    5479             :             /* should not happen */
    5480           0 :             break;
    5481             :         case BMS_SINGLETON:
    5482         296 :             rel = find_base_rel(root, bms_singleton_member(relids));
    5483         296 :             break;
    5484             :         case BMS_MULTIPLE:
    5485          10 :             rel = find_join_rel(root, relids);
    5486          10 :             break;
    5487             :     }
    5488             : 
    5489         306 :     if (rel == NULL)
    5490           0 :         elog(ERROR, "could not find RelOptInfo for given relids");
    5491             : 
    5492         306 :     return rel;
    5493             : }
    5494             : 
    5495             : 
    5496             : /*-------------------------------------------------------------------------
    5497             :  *
    5498             :  * Pattern analysis functions
    5499             :  *
    5500             :  * These routines support analysis of LIKE and regular-expression patterns
    5501             :  * by the planner/optimizer.  It's important that they agree with the
    5502             :  * regular-expression code in backend/regex/ and the LIKE code in
    5503             :  * backend/utils/adt/like.c.  Also, the computation of the fixed prefix
    5504             :  * must be conservative: if we report a string longer than the true fixed
    5505             :  * prefix, the query may produce actually wrong answers, rather than just
    5506             :  * getting a bad selectivity estimate!
    5507             :  *
    5508             :  * Note that the prefix-analysis functions are called from
    5509             :  * backend/optimizer/path/indxpath.c as well as from routines in this file.
    5510             :  *
    5511             :  *-------------------------------------------------------------------------
    5512             :  */
    5513             : 
    5514             : /*
    5515             :  * Check whether char is a letter (and, hence, subject to case-folding)
    5516             :  *
    5517             :  * In multibyte character sets or with ICU, we can't use isalpha, and it does not seem
    5518             :  * worth trying to convert to wchar_t to use iswalpha.  Instead, just assume
    5519             :  * any multibyte char is potentially case-varying.
    5520             :  */
    5521             : static int
    5522           0 : pattern_char_isalpha(char c, bool is_multibyte,
    5523             :                      pg_locale_t locale, bool locale_is_c)
    5524             : {
    5525           0 :     if (locale_is_c)
    5526           0 :         return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
    5527           0 :     else if (is_multibyte && IS_HIGHBIT_SET(c))
    5528           0 :         return true;
    5529           0 :     else if (locale && locale->provider == COLLPROVIDER_ICU)
    5530           0 :         return IS_HIGHBIT_SET(c) ? true : false;
    5531             : #ifdef HAVE_LOCALE_T
    5532           0 :     else if (locale && locale->provider == COLLPROVIDER_LIBC)
    5533           0 :         return isalpha_l((unsigned char) c, locale->info.lt);
    5534             : #endif
    5535             :     else
    5536           0 :         return isalpha((unsigned char) c);
    5537             : }
    5538             : 
    5539             : /*
    5540             :  * Extract the fixed prefix, if any, for a pattern.
    5541             :  *
    5542             :  * *prefix is set to a palloc'd prefix string (in the form of a Const node),
    5543             :  *  or to NULL if no fixed prefix exists for the pattern.
    5544             :  * If rest_selec is not NULL, *rest_selec is set to an estimate of the
    5545             :  *  selectivity of the remainder of the pattern (without any fixed prefix).
    5546             :  * The prefix Const has the same type (TEXT or BYTEA) as the input pattern.
    5547             :  *
    5548             :  * The return value distinguishes no fixed prefix, a partial prefix,
    5549             :  * or an exact-match-only pattern.
    5550             :  */
    5551             : 
    5552             : static Pattern_Prefix_Status
    5553         241 : like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
    5554             :                   Const **prefix_const, Selectivity *rest_selec)
    5555             : {
    5556             :     char       *match;
    5557             :     char       *patt;
    5558             :     int         pattlen;
    5559         241 :     Oid         typeid = patt_const->consttype;
    5560             :     int         pos,
    5561             :                 match_pos;
    5562         241 :     bool        is_multibyte = (pg_database_encoding_max_length() > 1);
    5563         241 :     pg_locale_t locale = 0;
    5564         241 :     bool        locale_is_c = false;
    5565             : 
    5566             :     /* the right-hand const is type text or bytea */
    5567         241 :     Assert(typeid == BYTEAOID || typeid == TEXTOID);
    5568             : 
    5569         241 :     if (case_insensitive)
    5570             :     {
    5571           0 :         if (typeid == BYTEAOID)
    5572           0 :             ereport(ERROR,
    5573             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    5574             :                      errmsg("case insensitive matching not supported on type bytea")));
    5575             : 
    5576             :         /* If case-insensitive, we need locale info */
    5577           0 :         if (lc_ctype_is_c(collation))
    5578           0 :             locale_is_c = true;
    5579           0 :         else if (collation != DEFAULT_COLLATION_OID)
    5580             :         {
    5581           0 :             if (!OidIsValid(collation))
    5582             :             {
    5583             :                 /*
    5584             :                  * This typically means that the parser could not resolve a
    5585             :                  * conflict of implicit collations, so report it that way.
    5586             :                  */
    5587           0 :                 ereport(ERROR,
    5588             :                         (errcode(ERRCODE_INDETERMINATE_COLLATION),
    5589             :                          errmsg("could not determine which collation to use for ILIKE"),
    5590             :                          errhint("Use the COLLATE clause to set the collation explicitly.")));
    5591             :             }
    5592           0 :             locale = pg_newlocale_from_collation(collation);
    5593             :         }
    5594             :     }
    5595             : 
    5596         241 :     if (typeid != BYTEAOID)
    5597             :     {
    5598         241 :         patt = TextDatumGetCString(patt_const->constvalue);
    5599         241 :         pattlen = strlen(patt);
    5600             :     }
    5601             :     else
    5602             :     {
    5603           0 :         bytea      *bstr = DatumGetByteaPP(patt_const->constvalue);
    5604             : 
    5605           0 :         pattlen = VARSIZE_ANY_EXHDR(bstr);
    5606           0 :         patt = (char *) palloc(pattlen);
    5607           0 :         memcpy(patt, VARDATA_ANY(bstr), pattlen);
    5608           0 :         Assert((Pointer) bstr == DatumGetPointer(patt_const->constvalue));
    5609             :     }
    5610             : 
    5611         241 :     match = palloc(pattlen + 1);
    5612         241 :     match_pos = 0;
    5613        1486 :     for (pos = 0; pos < pattlen; pos++)
    5614             :     {
    5615             :         /* % and _ are wildcard characters in LIKE */
    5616        2884 :         if (patt[pos] == '%' ||
    5617        1403 :             patt[pos] == '_')
    5618             :             break;
    5619             : 
    5620             :         /* Backslash escapes the next character */
    5621        1245 :         if (patt[pos] == '\\')
    5622             :         {
    5623          32 :             pos++;
    5624          32 :             if (pos >= pattlen)
    5625           0 :                 break;
    5626             :         }
    5627             : 
    5628             :         /* Stop if case-varying character (it's sort of a wildcard) */
    5629        1245 :         if (case_insensitive &&
    5630           0 :             pattern_char_isalpha(patt[pos], is_multibyte, locale, locale_is_c))
    5631           0 :             break;
    5632             : 
    5633        1245 :         match[match_pos++] = patt[pos];
    5634             :     }
    5635             : 
    5636         241 :     match[match_pos] = '\0';
    5637             : 
    5638         241 :     if (typeid != BYTEAOID)
    5639         241 :         *prefix_const = string_to_const(match, typeid);
    5640             :     else
    5641           0 :         *prefix_const = string_to_bytea_const(match, match_pos);
    5642             : 
    5643         241 :     if (rest_selec != NULL)
    5644         110 :         *rest_selec = like_selectivity(&patt[pos], pattlen - pos,
    5645             :                                        case_insensitive);
    5646             : 
    5647         241 :     pfree(patt);
    5648         241 :     pfree(match);
    5649             : 
    5650             :     /* in LIKE, an empty pattern is an exact match! */
    5651         241 :     if (pos == pattlen)
    5652           5 :         return Pattern_Prefix_Exact;    /* reached end of pattern, so exact */
    5653             : 
    5654         236 :     if (match_pos > 0)
    5655         214 :         return Pattern_Prefix_Partial;
    5656             : 
    5657          22 :     return Pattern_Prefix_None;
    5658             : }
    5659             : 
    5660             : static Pattern_Prefix_Status
    5661        1256 : regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
    5662             :                    Const **prefix_const, Selectivity *rest_selec)
    5663             : {
    5664        1256 :     Oid         typeid = patt_const->consttype;
    5665             :     char       *prefix;
    5666             :     bool        exact;
    5667             : 
    5668             :     /*
    5669             :      * Should be unnecessary, there are no bytea regex operators defined. As
    5670             :      * such, it should be noted that the rest of this function has *not* been
    5671             :      * made safe for binary (possibly NULL containing) strings.
    5672             :      */
    5673        1256 :     if (typeid == BYTEAOID)
    5674           0 :         ereport(ERROR,
    5675             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    5676             :                  errmsg("regular-expression matching not supported on type bytea")));
    5677             : 
    5678             :     /* Use the regexp machinery to extract the prefix, if any */
    5679        1256 :     prefix = regexp_fixed_prefix(DatumGetTextPP(patt_const->constvalue),
    5680             :                                  case_insensitive, collation,
    5681             :                                  &exact);
    5682             : 
    5683        1256 :     if (prefix == NULL)
    5684             :     {
    5685          46 :         *prefix_const = NULL;
    5686             : 
    5687          46 :         if (rest_selec != NULL)
    5688             :         {
    5689          38 :             char       *patt = TextDatumGetCString(patt_const->constvalue);
    5690             : 
    5691          38 :             *rest_selec = regex_selectivity(patt, strlen(patt),
    5692             :                                             case_insensitive,
    5693             :                                             0);
    5694          38 :             pfree(patt);
    5695             :         }
    5696             : 
    5697          46 :         return Pattern_Prefix_None;
    5698             :     }
    5699             : 
    5700        1210 :     *prefix_const = string_to_const(prefix, typeid);
    5701             : 
    5702        1210 :     if (rest_selec != NULL)
    5703             :     {
    5704         311 :         if (exact)
    5705             :         {
    5706             :             /* Exact match, so there's no additional selectivity */
    5707         273 :             *rest_selec = 1.0;
    5708             :         }
    5709             :         else
    5710             :         {
    5711          38 :             char       *patt = TextDatumGetCString(patt_const->constvalue);
    5712             : 
    5713          38 :             *rest_selec = regex_selectivity(patt, strlen(patt),
    5714             :                                             case_insensitive,
    5715          38 :                                             strlen(prefix));
    5716          38 :             pfree(patt);
    5717             :         }
    5718             :     }
    5719             : 
    5720        1210 :     pfree(prefix);
    5721             : 
    5722        1210 :     if (exact)
    5723        1112 :         return Pattern_Prefix_Exact;    /* pattern specifies exact match */
    5724             :     else
    5725          98 :         return Pattern_Prefix_Partial;
    5726             : }
    5727             : 
    5728             : Pattern_Prefix_Status
    5729        1497 : pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation,
    5730             :                      Const **prefix, Selectivity *rest_selec)
    5731             : {
    5732             :     Pattern_Prefix_Status result;
    5733             : 
    5734        1497 :     switch (ptype)
    5735             :     {
    5736             :         case Pattern_Type_Like:
    5737         241 :             result = like_fixed_prefix(patt, false, collation,
    5738             :                                        prefix, rest_selec);
    5739         241 :             break;
    5740             :         case Pattern_Type_Like_IC:
    5741           0 :             result = like_fixed_prefix(patt, true, collation,
    5742             :                                        prefix, rest_selec);
    5743           0 :             break;
    5744             :         case Pattern_Type_Regex:
    5745        1256 :             result = regex_fixed_prefix(patt, false, collation,
    5746             :                                         prefix, rest_selec);
    5747        1256 :             break;
    5748             :         case Pattern_Type_Regex_IC:
    5749           0 :             result = regex_fixed_prefix(patt, true, collation,
    5750             :                                         prefix, rest_selec);
    5751           0 :             break;
    5752             :         default:
    5753           0 :             elog(ERROR, "unrecognized ptype: %d", (int) ptype);
    5754             :             result = Pattern_Prefix_None;   /* keep compiler quiet */
    5755             :             break;
    5756             :     }
    5757        1497 :     return result;
    5758             : }
    5759             : 
    5760             : /*
    5761             :  * Estimate the selectivity of a fixed prefix for a pattern match.
    5762             :  *
    5763             :  * A fixed prefix "foo" is estimated as the selectivity of the expression
    5764             :  * "variable >= 'foo' AND variable < 'fop'" (see also indxpath.c).
    5765             :  *
    5766             :  * The selectivity estimate is with respect to the portion of the column
    5767             :  * population represented by the histogram --- the caller must fold this
    5768             :  * together with info about MCVs and NULLs.
    5769             :  *
    5770             :  * We use the >= and < operators from the specified btree opfamily to do the
    5771             :  * estimation.  The given variable and Const must be of the associated
    5772             :  * datatype.
    5773             :  *
    5774             :  * XXX Note: we make use of the upper bound to estimate operator selectivity
    5775             :  * even if the locale is such that we cannot rely on the upper-bound string.
    5776             :  * The selectivity only needs to be approximately right anyway, so it seems
    5777             :  * more useful to use the upper-bound code than not.
    5778             :  */
    5779             : static Selectivity
    5780          82 : prefix_selectivity(PlannerInfo *root, VariableStatData *vardata,
    5781             :                    Oid vartype, Oid opfamily, Const *prefixcon)
    5782             : {
    5783             :     Selectivity prefixsel;
    5784             :     Oid         cmpopr;
    5785             :     FmgrInfo    opproc;
    5786             :     Const      *greaterstrcon;
    5787             :     Selectivity eq_sel;
    5788             : 
    5789          82 :     cmpopr = get_opfamily_member(opfamily, vartype, vartype,
    5790             :                                  BTGreaterEqualStrategyNumber);
    5791          82 :     if (cmpopr == InvalidOid)
    5792           0 :         elog(ERROR, "no >= operator for opfamily %u", opfamily);
    5793          82 :     fmgr_info(get_opcode(cmpopr), &opproc);
    5794             : 
    5795          82 :     prefixsel = ineq_histogram_selectivity(root, vardata, &opproc, true,
    5796             :                                            prefixcon->constvalue,
    5797             :                                            prefixcon->consttype);
    5798             : 
    5799          82 :     if (prefixsel < 0.0)
    5800             :     {
    5801             :         /* No histogram is present ... return a suitable default estimate */
    5802          50 :         return DEFAULT_MATCH_SEL;
    5803             :     }
    5804             : 
    5805             :     /*-------
    5806             :      * If we can create a string larger than the prefix, say
    5807             :      *  "x < greaterstr".
    5808             :      *-------
    5809             :      */
    5810          32 :     cmpopr = get_opfamily_member(opfamily, vartype, vartype,
    5811             :                                  BTLessStrategyNumber);
    5812          32 :     if (cmpopr == InvalidOid)
    5813           0 :         elog(ERROR, "no < operator for opfamily %u", opfamily);
    5814          32 :     fmgr_info(get_opcode(cmpopr), &opproc);
    5815          32 :     greaterstrcon = make_greater_string(prefixcon, &opproc,
    5816             :                                         DEFAULT_COLLATION_OID);
    5817          32 :     if (greaterstrcon)
    5818             :     {
    5819             :         Selectivity topsel;
    5820             : 
    5821          32 :         topsel = ineq_histogram_selectivity(root, vardata, &opproc, false,
    5822             :                                             greaterstrcon->constvalue,
    5823             :                                             greaterstrcon->consttype);
    5824             : 
    5825             :         /* ineq_histogram_selectivity worked before, it shouldn't fail now */
    5826          32 :         Assert(topsel >= 0.0);
    5827             : 
    5828             :         /*
    5829             :          * Merge the two selectivities in the same way as for a range query
    5830             :          * (see clauselist_selectivity()).  Note that we don't need to worry
    5831             :          * about double-exclusion of nulls, since ineq_histogram_selectivity
    5832             :          * doesn't count those anyway.
    5833             :          */
    5834          32 :         prefixsel = topsel + prefixsel - 1.0;
    5835             :     }
    5836             : 
    5837             :     /*
    5838             :      * If the prefix is long then the two bounding values might be too close
    5839             :      * together for the histogram to distinguish them usefully, resulting in a
    5840             :      * zero estimate (plus or minus roundoff error). To avoid returning a
    5841             :      * ridiculously small estimate, compute the estimated selectivity for
    5842             :      * "variable = 'foo'", and clamp to that. (Obviously, the resultant
    5843             :      * estimate should be at least that.)
    5844             :      *
    5845             :      * We apply this even if we couldn't make a greater string.  That case
    5846             :      * suggests that the prefix is near the maximum possible, and thus
    5847             :      * probably off the end of the histogram, and thus we probably got a very
    5848             :      * small estimate from the >= condition; so we still need to clamp.
    5849             :      */
    5850          32 :     cmpopr = get_opfamily_member(opfamily, vartype, vartype,
    5851             :                                  BTEqualStrategyNumber);
    5852          32 :     if (cmpopr == InvalidOid)
    5853           0 :         elog(ERROR, "no = operator for opfamily %u", opfamily);
    5854          32 :     eq_sel = var_eq_const(vardata, cmpopr, prefixcon->constvalue,
    5855             :                           false, true, false);
    5856             : 
    5857          32 :     prefixsel = Max(prefixsel, eq_sel);
    5858             : 
    5859          32 :     return prefixsel;
    5860             : }
    5861             : 
    5862             : 
    5863             : /*
    5864             :  * Estimate the selectivity of a pattern of the specified type.
    5865             :  * Note that any fixed prefix of the pattern will have been removed already,
    5866             :  * so actually we may be looking at just a fragment of the pattern.
    5867             :  *
    5868             :  * For now, we use a very simplistic approach: fixed characters reduce the
    5869             :  * selectivity a good deal, character ranges reduce it a little,
    5870             :  * wildcards (such as % for LIKE or .* for regex) increase it.
    5871             :  */
    5872             : 
    5873             : #define FIXED_CHAR_SEL  0.20    /* about 1/5 */
    5874             : #define CHAR_RANGE_SEL  0.25
    5875             : #define ANY_CHAR_SEL    0.9     /* not 1, since it won't match end-of-string */
    5876             : #define FULL_WILDCARD_SEL 5.0
    5877             : #define PARTIAL_WILDCARD_SEL 2.0
    5878             : 
    5879             : static Selectivity
    5880         110 : like_selectivity(const char *patt, int pattlen, bool case_insensitive)
    5881             : {
    5882         110 :     Selectivity sel = 1.0;
    5883             :     int         pos;
    5884             : 
    5885             :     /* Skip any leading wildcard; it's already factored into initial sel */
    5886         219 :     for (pos = 0; pos < pattlen; pos++)
    5887             :     {
    5888         176 :         if (patt[pos] != '%' && patt[pos] != '_')
    5889          67 :             break;
    5890             :     }
    5891             : 
    5892         461 :     for (; pos < pattlen; pos++)
    5893             :     {
    5894             :         /* % and _ are wildcard characters in LIKE */
    5895         351 :         if (patt[pos] == '%')
    5896          65 :             sel *= FULL_WILDCARD_SEL;
    5897         286 :         else if (patt[pos] == '_')
    5898           8 :             sel *= ANY_CHAR_SEL;
    5899         278 :         else if (patt[pos] == '\\')
    5900             :         {
    5901             :             /* Backslash quotes the next character */
    5902           6 :             pos++;
    5903           6 :             if (pos >= pattlen)
    5904           0 :                 break;
    5905           6 :             sel *= FIXED_CHAR_SEL;
    5906             :         }
    5907             :         else
    5908         272 :             sel *= FIXED_CHAR_SEL;
    5909             :     }
    5910             :     /* Could get sel > 1 if multiple wildcards */
    5911         110 :     if (sel > 1.0)
    5912           0 :         sel = 1.0;
    5913         110 :     return sel;
    5914             : }
    5915             : 
    5916             : static Selectivity
    5917          83 : regex_selectivity_sub(const char *patt, int pattlen, bool case_insensitive)
    5918             : {
    5919          83 :     Selectivity sel = 1.0;
    5920          83 :     int         paren_depth = 0;
    5921          83 :     int         paren_pos = 0;  /* dummy init to keep compiler quiet */
    5922             :     int         pos;
    5923             : 
    5924         659 :     for (pos = 0; pos < pattlen; pos++)
    5925             :     {
    5926         577 :         if (patt[pos] == '(')
    5927             :         {
    5928           7 :             if (paren_depth == 0)
    5929           6 :                 paren_pos = pos;    /* remember start of parenthesized item */
    5930           7 :             paren_depth++;
    5931             :         }
    5932         570 :         else if (patt[pos] == ')' && paren_depth > 0)
    5933             :         {
    5934           7 :             paren_depth--;
    5935          14 :             if (paren_depth == 0)
    5936          12 :                 sel *= regex_selectivity_sub(patt + (paren_pos + 1),
    5937           6 :                                              pos - (paren_pos + 1),
    5938             :                                              case_insensitive);
    5939             :         }
    5940         563 :         else if (patt[pos] == '|' && paren_depth == 0)
    5941             :         {
    5942             :             /*
    5943             :              * If unquoted | is present at paren level 0 in pattern, we have
    5944             :              * multiple alternatives; sum their probabilities.
    5945             :              */
    5946           2 :             sel += regex_selectivity_sub(patt + (pos + 1),
    5947           1 :                                          pattlen - (pos + 1),
    5948             :                                          case_insensitive);
    5949           1 :             break;              /* rest of pattern is now processed */
    5950             :         }
    5951         562 :         else if (patt[pos] == '[')
    5952             :         {
    5953           4 :             bool        negclass = false;
    5954             : 
    5955           4 :             if (patt[++pos] == '^')
    5956             :             {
    5957           0 :                 negclass = true;
    5958           0 :                 pos++;
    5959             :             }
    5960           4 :             if (patt[pos] == ']')   /* ']' at start of class is not special */
    5961           0 :                 pos++;
    5962          20 :             while (pos < pattlen && patt[pos] != ']')
    5963          12 :                 pos++;
    5964           4 :             if (paren_depth == 0)
    5965           4 :                 sel *= (negclass ? (1.0 - CHAR_RANGE_SEL) : CHAR_RANGE_SEL);
    5966             :         }
    5967         558 :         else if (patt[pos] == '.')
    5968             :         {
    5969          27 :             if (paren_depth == 0)
    5970          26 :                 sel *= ANY_CHAR_SEL;
    5971             :         }
    5972        1046 :         else if (patt[pos] == '*' ||
    5973        1026 :                  patt[pos] == '?' ||
    5974         511 :                  patt[pos] == '+')
    5975             :         {
    5976             :             /* Ought to be smarter about quantifiers... */
    5977          42 :             if (paren_depth == 0)
    5978          18 :                 sel *= PARTIAL_WILDCARD_SEL;
    5979             :         }
    5980         510 :         else if (patt[pos] == '{')
    5981             :         {
    5982           0 :             while (pos < pattlen && patt[pos] != '}')
    5983           0 :                 pos++;
    5984           0 :             if (paren_depth == 0)
    5985           0 :                 sel *= PARTIAL_WILDCARD_SEL;
    5986             :         }
    5987         510 :         else if (patt[pos] == '\\')
    5988             :         {
    5989             :             /* backslash quotes the next character */
    5990           8 :             pos++;
    5991           8 :             if (pos >= pattlen)
    5992           0 :                 break;
    5993           8 :             if (paren_depth == 0)
    5994           4 :                 sel *= FIXED_CHAR_SEL;
    5995             :         }
    5996             :         else
    5997             :         {
    5998         502 :             if (paren_depth == 0)
    5999         483 :                 sel *= FIXED_CHAR_SEL;
    6000             :         }
    6001             :     }
    6002             :     /* Could get sel > 1 if multiple wildcards */
    6003          83 :     if (sel > 1.0)
    6004           2 :         sel = 1.0;
    6005          83 :     return sel;
    6006             : }
    6007             : 
    6008             : static Selectivity
    6009          76 : regex_selectivity(const char *patt, int pattlen, bool case_insensitive,
    6010             :                   int fixed_prefix_len)
    6011             : {
    6012             :     Selectivity sel;
    6013             : 
    6014             :     /* If patt doesn't end with $, consider it to have a trailing wildcard */
    6015          76 :     if (pattlen > 0 && patt[pattlen - 1] == '$' &&
    6016           1 :         (pattlen == 1 || patt[pattlen - 2] != '\\'))
    6017             :     {
    6018             :         /* has trailing $ */
    6019           1 :         sel = regex_selectivity_sub(patt, pattlen - 1, case_insensitive);
    6020             :     }
    6021             :     else
    6022             :     {
    6023             :         /* no trailing $ */
    6024          75 :         sel = regex_selectivity_sub(patt, pattlen, case_insensitive);
    6025          75 :         sel *= FULL_WILDCARD_SEL;
    6026             :     }
    6027             : 
    6028             :     /* If there's a fixed prefix, discount its selectivity */
    6029          76 :     if (fixed_prefix_len > 0)
    6030          38 :         sel /= pow(FIXED_CHAR_SEL, fixed_prefix_len);
    6031             : 
    6032             :     /* Make sure result stays in range */
    6033          76 :     CLAMP_PROBABILITY(sel);
    6034          76 :     return sel;
    6035             : }
    6036             : 
    6037             : 
    6038             : /*
    6039             :  * For bytea, the increment function need only increment the current byte
    6040             :  * (there are no multibyte characters to worry about).
    6041             :  */
    6042             : static bool
    6043           0 : byte_increment(unsigned char *ptr, int len)
    6044             : {
    6045           0 :     if (*ptr >= 255)
    6046           0 :         return false;
    6047           0 :     (*ptr)++;
    6048           0 :     return true;
    6049             : }
    6050             : 
    6051             : /*
    6052             :  * Try to generate a string greater than the given string or any
    6053             :  * string it is a prefix of.  If successful, return a palloc'd string
    6054             :  * in the form of a Const node; else return NULL.
    6055             :  *
    6056             :  * The caller must provide the appropriate "less than" comparison function
    6057             :  * for testing the strings, along with the collation to use.
    6058             :  *
    6059             :  * The key requirement here is that given a prefix string, say "foo",
    6060             :  * we must be able to generate another string "fop" that is greater than
    6061             :  * all strings "foobar" starting with "foo".  We can test that we have
    6062             :  * generated a string greater than the prefix string, but in non-C collations
    6063             :  * that is not a bulletproof guarantee that an extension of the string might
    6064             :  * not sort after it; an example is that "foo " is less than "foo!", but it
    6065             :  * is not clear that a "dictionary" sort ordering will consider "foo!" less
    6066             :  * than "foo bar".  CAUTION: Therefore, this function should be used only for
    6067             :  * estimation purposes when working in a non-C collation.
    6068             :  *
    6069             :  * To try to catch most cases where an extended string might otherwise sort
    6070             :  * before the result value, we determine which of the strings "Z", "z", "y",
    6071             :  * and "9" is seen as largest by the collation, and append that to the given
    6072             :  * prefix before trying to find a string that compares as larger.
    6073             :  *
    6074             :  * To search for a greater string, we repeatedly "increment" the rightmost
    6075             :  * character, using an encoding-specific character incrementer function.
    6076             :  * When it's no longer possible to increment the last character, we truncate
    6077             :  * off that character and start incrementing the next-to-rightmost.
    6078             :  * For example, if "z" were the last character in the sort order, then we
    6079             :  * could produce "foo" as a string greater than "fonz".
    6080             :  *
    6081             :  * This could be rather slow in the worst case, but in most cases we
    6082             :  * won't have to try more than one or two strings before succeeding.
    6083             :  *
    6084             :  * Note that it's important for the character incrementer not to be too anal
    6085             :  * about producing every possible character code, since in some cases the only
    6086             :  * way to get a larger string is to increment a previous character position.
    6087             :  * So we don't want to spend too much time trying every possible character
    6088             :  * code at the last position.  A good rule of thumb is to be sure that we
    6089             :  * don't try more than 256*K values for a K-byte character (and definitely
    6090             :  * not 256^K, which is what an exhaustive search would approach).
    6091             :  */
    6092             : Const *
    6093         151 : make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation)
    6094             : {
    6095         151 :     Oid         datatype = str_const->consttype;
    6096             :     char       *workstr;
    6097             :     int         len;
    6098             :     Datum       cmpstr;
    6099         151 :     text       *cmptxt = NULL;
    6100             :     mbcharacter_incrementer charinc;
    6101             : 
    6102             :     /*
    6103             :      * Get a modifiable copy of the prefix string in C-string format, and set
    6104             :      * up the string we will compare to as a Datum.  In C locale this can just
    6105             :      * be the given prefix string, otherwise we need to add a suffix.  Types
    6106             :      * NAME and BYTEA sort bytewise so they don't need a suffix either.
    6107             :      */
    6108         151 :     if (datatype == NAMEOID)
    6109             :     {
    6110         151 :         workstr = DatumGetCString(DirectFunctionCall1(nameout,
    6111             :                                                       str_const->constvalue));
    6112         151 :         len = strlen(workstr);
    6113         151 :         cmpstr = str_const->constvalue;
    6114             :     }
    6115           0 :     else if (datatype == BYTEAOID)
    6116             :     {
    6117           0 :         bytea      *bstr = DatumGetByteaPP(str_const->constvalue);
    6118             : 
    6119           0 :         len = VARSIZE_ANY_EXHDR(bstr);
    6120           0 :         workstr = (char *) palloc(len);
    6121           0 :         memcpy(workstr, VARDATA_ANY(bstr), len);
    6122           0 :         Assert((Pointer) bstr == DatumGetPointer(str_const->constvalue));
    6123           0 :         cmpstr = str_const->constvalue;
    6124             :     }
    6125             :     else
    6126             :     {
    6127           0 :         workstr = TextDatumGetCString(str_const->constvalue);
    6128           0 :         len = strlen(workstr);
    6129           0 :         if (lc_collate_is_c(collation) || len == 0)
    6130           0 :             cmpstr = str_const->constvalue;
    6131             :         else
    6132             :         {
    6133             :             /* If first time through, determine the suffix to use */
    6134             :             static char suffixchar = 0;
    6135             :             static Oid  suffixcollation = 0;
    6136             : 
    6137           0 :             if (!suffixchar || suffixcollation != collation)
    6138             :             {
    6139             :                 char       *best;
    6140             : 
    6141           0 :                 best = "Z";
    6142           0 :                 if (varstr_cmp(best, 1, "z", 1, collation) < 0)
    6143           0 :                     best = "z";
    6144           0 :                 if (varstr_cmp(best, 1, "y", 1, collation) < 0)
    6145           0 :                     best = "y";
    6146           0 :                 if (varstr_cmp(best, 1, "9", 1, collation) < 0)
    6147           0 :                     best = "9";
    6148           0 :                 suffixchar = *best;
    6149           0 :                 suffixcollation = collation;
    6150             :             }
    6151             : 
    6152             :             /* And build the string to compare to */
    6153           0 :             cmptxt = (text *) palloc(VARHDRSZ + len + 1);
    6154           0 :             SET_VARSIZE(cmptxt, VARHDRSZ + len + 1);
    6155           0 :             memcpy(VARDATA(cmptxt), workstr, len);
    6156           0 :             *(VARDATA(cmptxt) + len) = suffixchar;
    6157           0 :             cmpstr = PointerGetDatum(cmptxt);
    6158             :         }
    6159             :     }
    6160             : 
    6161             :     /* Select appropriate character-incrementer function */
    6162         151 :     if (datatype == BYTEAOID)
    6163           0 :         charinc = byte_increment;
    6164             :     else
    6165         151 :         charinc = pg_database_encoding_character_incrementer();
    6166             : 
    6167             :     /* And search ... */
    6168         302 :     while (len > 0)
    6169             :     {
    6170             :         int         charlen;
    6171             :         unsigned char *lastchar;
    6172             : 
    6173             :         /* Identify the last character --- for bytea, just the last byte */
    6174         151 :         if (datatype == BYTEAOID)
    6175           0 :             charlen = 1;
    6176             :         else
    6177         151 :             charlen = len - pg_mbcliplen(workstr, len, len - 1);
    6178         151 :         lastchar = (unsigned char *) (workstr + len - charlen);
    6179             : 
    6180             :         /*
    6181             :          * Try to generate a larger string by incrementing the last character
    6182             :          * (for BYTEA, we treat each byte as a character).
    6183             :          *
    6184             :          * Note: the incrementer function is expected to return true if it's
    6185             :          * generated a valid-per-the-encoding new character, otherwise false.
    6186             :          * The contents of the character on false return are unspecified.
    6187             :          */
    6188         302 :         while (charinc(lastchar, charlen))
    6189             :         {
    6190             :             Const      *workstr_const;
    6191             : 
    6192         151 :             if (datatype == BYTEAOID)
    6193           0 :                 workstr_const = string_to_bytea_const(workstr, len);
    6194             :             else
    6195         151 :                 workstr_const = string_to_const(workstr, datatype);
    6196             : 
    6197         151 :             if (DatumGetBool(FunctionCall2Coll(ltproc,
    6198             :                                                collation,
    6199             :                                                cmpstr,
    6200             :                                                workstr_const->constvalue)))
    6201             :             {
    6202             :                 /* Successfully made a string larger than cmpstr */
    6203         151 :                 if (cmptxt)
    6204           0 :                     pfree(cmptxt);
    6205         151 :                 pfree(workstr);
    6206         151 :                 return workstr_const;
    6207             :             }
    6208             : 
    6209             :             /* No good, release unusable value and try again */
    6210           0 :             pfree(DatumGetPointer(workstr_const->constvalue));
    6211           0 :             pfree(workstr_const);
    6212             :         }
    6213             : 
    6214             :         /*
    6215             :          * No luck here, so truncate off the last character and try to
    6216             :          * increment the next one.
    6217             :          */
    6218           0 :         len -= charlen;
    6219           0 :         workstr[len] = '\0';
    6220             :     }
    6221             : 
    6222             :     /* Failed... */
    6223           0 :     if (cmptxt)
    6224           0 :         pfree(cmptxt);
    6225           0 :     pfree(workstr);
    6226             : 
    6227           0 :     return NULL;
    6228             : }
    6229             : 
    6230             : /*
    6231             :  * Generate a Datum of the appropriate type from a C string.
    6232             :  * Note that all of the supported types are pass-by-ref, so the
    6233             :  * returned value should be pfree'd if no longer needed.
    6234             :  */
    6235             : static Datum
    6236        1964 : string_to_datum(const char *str, Oid datatype)
    6237             : {
    6238        1964 :     Assert(str != NULL);
    6239             : 
    6240             :     /*
    6241             :      * We cheat a little by assuming that CStringGetTextDatum() will do for
    6242             :      * bpchar and varchar constants too...
    6243             :      */
    6244        1964 :     if (datatype == NAMEOID)
    6245         513 :         return DirectFunctionCall1(namein, CStringGetDatum(str));
    6246        1451 :     else if (datatype == BYTEAOID)
    6247           0 :         return DirectFunctionCall1(byteain, CStringGetDatum(str));
    6248             :     else
    6249        1451 :         return CStringGetTextDatum(str);
    6250             : }
    6251             : 
    6252             : /*
    6253             :  * Generate a Const node of the appropriate type from a C string.
    6254             :  */
    6255             : static Const *
    6256        1964 : string_to_const(const char *str, Oid datatype)
    6257             : {
    6258        1964 :     Datum       conval = string_to_datum(str, datatype);
    6259             :     Oid         collation;
    6260             :     int         constlen;
    6261             : 
    6262             :     /*
    6263             :      * We only need to support a few datatypes here, so hard-wire properties
    6264             :      * instead of incurring the expense of catalog lookups.
    6265             :      */
    6266        1964 :     switch (datatype)
    6267             :     {
    6268             :         case TEXTOID:
    6269             :         case VARCHAROID:
    6270             :         case BPCHAROID:
    6271        1451 :             collation = DEFAULT_COLLATION_OID;
    6272        1451 :             constlen = -1;
    6273        1451 :             break;
    6274             : 
    6275             :         case NAMEOID:
    6276         513 :             collation = InvalidOid;
    6277         513 :             constlen = NAMEDATALEN;
    6278         513 :             break;
    6279             : 
    6280             :         case BYTEAOID:
    6281           0 :             collation = InvalidOid;
    6282           0 :             constlen = -1;
    6283           0 :             break;
    6284             : 
    6285             :         default:
    6286           0 :             elog(ERROR, "unexpected datatype in string_to_const: %u",
    6287             :                  datatype);
    6288             :             return NULL;
    6289             :     }
    6290             : 
    6291        1964 :     return makeConst(datatype, -1, collation, constlen,
    6292             :                      conval, false, false);
    6293             : }
    6294             : 
    6295             : /*
    6296             :  * Generate a Const node of bytea type from a binary C string and a length.
    6297             :  */
    6298             : static Const *
    6299           0 : string_to_bytea_const(const char *str, size_t str_len)
    6300             : {
    6301           0 :     bytea      *bstr = palloc(VARHDRSZ + str_len);
    6302             :     Datum       conval;
    6303             : 
    6304           0 :     memcpy(VARDATA(bstr), str, str_len);
    6305           0 :     SET_VARSIZE(bstr, VARHDRSZ + str_len);
    6306           0 :     conval = PointerGetDatum(bstr);
    6307             : 
    6308           0 :     return makeConst(BYTEAOID, -1, InvalidOid, -1, conval, false, false);
    6309             : }
    6310             : 
    6311             : /*-------------------------------------------------------------------------
    6312             :  *
    6313             :  * Index cost estimation functions
    6314             :  *
    6315             :  *-------------------------------------------------------------------------
    6316             :  */
    6317             : 
    6318             : List *
    6319       21949 : deconstruct_indexquals(IndexPath *path)
    6320             : {
    6321       21949 :     List       *result = NIL;
    6322       21949 :     IndexOptInfo *index = path->indexinfo;
    6323             :     ListCell   *lcc,
    6324             :                *lci;
    6325             : 
    6326       39110 :     forboth(lcc, path->indexquals, lci, path->indexqualcols)
    6327             :     {
    6328       17161 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, lcc);
    6329       17161 :         int         indexcol = lfirst_int(lci);
    6330             :         Expr       *clause;
    6331             :         Node       *leftop,
    6332             :                    *rightop;
    6333             :         IndexQualInfo *qinfo;
    6334             : 
    6335       17161 :         clause = rinfo->clause;
    6336             : 
    6337       17161 :         qinfo = (IndexQualInfo *) palloc(sizeof(IndexQualInfo));
    6338       17161 :         qinfo->rinfo = rinfo;
    6339       17161 :         qinfo->indexcol = indexcol;
    6340             : 
    6341       17161 :         if (IsA(clause, OpExpr))
    6342             :         {
    6343       16446 :             qinfo->clause_op = ((OpExpr *) clause)->opno;
    6344       16446 :             leftop = get_leftop(clause);
    6345       16446 :             rightop = get_rightop(clause);
    6346       16446 :             if (match_index_to_operand(leftop, indexcol, index))
    6347             :             {
    6348       15642 :                 qinfo->varonleft = true;
    6349       15642 :                 qinfo->other_operand = rightop;
    6350             :             }
    6351             :             else
    6352             :             {
    6353         804 :                 Assert(match_index_to_operand(rightop, indexcol, index));
    6354         804 :                 qinfo->varonleft = false;
    6355         804 :                 qinfo->other_operand = leftop;
    6356             :             }
    6357             :         }
    6358         715 :         else if (IsA(clause, RowCompareExpr))
    6359             :         {
    6360           6 :             RowCompareExpr *rc = (RowCompareExpr *) clause;
    6361             : 
    6362           6 :             qinfo->clause_op = linitial_oid(rc->opnos);
    6363             :             /* Examine only first columns to determine left/right sides */
    6364           6 :             if (match_index_to_operand((Node *) linitial(rc->largs),
    6365             :                                        indexcol, index))
    6366             :             {
    6367           6 :                 qinfo->varonleft = true;
    6368           6 :                 qinfo->other_operand = (Node *) rc->rargs;
    6369             :             }
    6370             :             else
    6371             :             {
    6372           0 :                 Assert(match_index_to_operand((Node *) linitial(rc->rargs),
    6373             :                                               indexcol, index));
    6374           0 :                 qinfo->varonleft = false;
    6375           0 :                 qinfo->other_operand = (Node *) rc->largs;
    6376             :             }
    6377             :         }
    6378         709 :         else if (IsA(clause, ScalarArrayOpExpr))
    6379             :         {
    6380         302 :             ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
    6381             : 
    6382         302 :             qinfo->clause_op = saop->opno;
    6383             :             /* index column is always on the left in this case */
    6384         302 :             Assert(match_index_to_operand((Node *) linitial(saop->args),
    6385             :                                           indexcol, index));
    6386         302 :             qinfo->varonleft = true;
    6387         302 :             qinfo->other_operand = (Node *) lsecond(saop->args);
    6388             :         }
    6389         407 :         else if (IsA(clause, NullTest))
    6390             :         {
    6391         407 :             qinfo->clause_op = InvalidOid;
    6392         407 :             Assert(match_index_to_operand((Node *) ((NullTest *) clause)->arg,
    6393             :                                           indexcol, index));
    6394         407 :             qinfo->varonleft = true;
    6395         407 :             qinfo->other_operand = NULL;
    6396             :         }
    6397             :         else
    6398             :         {
    6399           0 :             elog(ERROR, "unsupported indexqual type: %d",
    6400             :                  (int) nodeTag(clause));
    6401             :         }
    6402             : 
    6403       17161 :         result = lappend(result, qinfo);
    6404             :     }
    6405       21949 :     return result;
    6406             : }
    6407             : 
    6408             : /*
    6409             :  * Simple function to compute the total eval cost of the "other operands"
    6410             :  * in an IndexQualInfo list.  Since we know these will be evaluated just
    6411             :  * once per scan, there's no need to distinguish startup from per-row cost.
    6412             :  */
    6413             : static Cost
    6414       21947 : other_operands_eval_cost(PlannerInfo *root, List *qinfos)
    6415             : {
    6416       21947 :     Cost        qual_arg_cost = 0;
    6417             :     ListCell   *lc;
    6418             : 
    6419       39106 :     foreach(lc, qinfos)
    6420             :     {
    6421       17159 :         IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(lc);
    6422             :         QualCost    index_qual_cost;
    6423             : 
    6424       17159 :         cost_qual_eval_node(&index_qual_cost, qinfo->other_operand, root);
    6425       17159 :         qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
    6426             :     }
    6427       21947 :     return qual_arg_cost;
    6428             : }
    6429             : 
    6430             : /*
    6431             :  * Get other-operand eval cost for an index orderby list.
    6432             :  *
    6433             :  * Index orderby expressions aren't represented as RestrictInfos (since they
    6434             :  * aren't boolean, usually).  So we can't apply deconstruct_indexquals to
    6435             :  * them.  However, they are much simpler to deal with since they are always
    6436             :  * OpExprs and the index column is always on the left.
    6437             :  */
    6438             : static Cost
    6439       21947 : orderby_operands_eval_cost(PlannerInfo *root, IndexPath *path)
    6440             : {
    6441       21947 :     Cost        qual_arg_cost = 0;
    6442             :     ListCell   *lc;
    6443             : 
    6444       21970 :     foreach(lc, path->indexorderbys)
    6445             :     {
    6446          23 :         Expr       *clause = (Expr *) lfirst(lc);
    6447             :         Node       *other_operand;
    6448             :         QualCost    index_qual_cost;
    6449             : 
    6450          23 :         if (IsA(clause, OpExpr))
    6451             :         {
    6452          23 :             other_operand = get_rightop(clause);
    6453             :         }
    6454             :         else
    6455             :         {
    6456           0 :             elog(ERROR, "unsupported indexorderby type: %d",
    6457             :                  (int) nodeTag(clause));
    6458             :             other_operand = NULL;   /* keep compiler quiet */
    6459             :         }
    6460             : 
    6461          23 :         cost_qual_eval_node(&index_qual_cost, other_operand, root);
    6462          23 :         qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
    6463             :     }
    6464       21947 :     return qual_arg_cost;
    6465             : }
    6466             : 
    6467             : void
    6468       20870 : genericcostestimate(PlannerInfo *root,
    6469             :                     IndexPath *path,
    6470             :                     double loop_count,
    6471             :                     List *qinfos,
    6472             :                     GenericCosts *costs)
    6473             : {
    6474       20870 :     IndexOptInfo *index = path->indexinfo;
    6475       20870 :     List       *indexQuals = path->indexquals;
    6476       20870 :     List       *indexOrderBys = path->indexorderbys;
    6477             :     Cost        indexStartupCost;
    6478             :     Cost        indexTotalCost;
    6479             :     Selectivity indexSelectivity;
    6480             :     double      indexCorrelation;
    6481             :     double      numIndexPages;
    6482             :     double      numIndexTuples;
    6483             :     double      spc_random_page_cost;
    6484             :     double      num_sa_scans;
    6485             :     double      num_outer_scans;
    6486             :     double      num_scans;
    6487             :     double      qual_op_cost;
    6488             :     double      qual_arg_cost;
    6489             :     List       *selectivityQuals;
    6490             :     ListCell   *l;
    6491             : 
    6492             :     /*
    6493             :      * If the index is partial, AND the index predicate with the explicitly
    6494             :      * given indexquals to produce a more accurate idea of the index
    6495             :      * selectivity.
    6496             :      */
    6497       20870 :     selectivityQuals = add_predicate_to_quals(index, indexQuals);
    6498             : 
    6499             :     /*
    6500             :      * Check for ScalarArrayOpExpr index quals, and estimate the number of
    6501             :      * index scans that will be performed.
    6502             :      */
    6503       20870 :     num_sa_scans = 1;
    6504       36950 :     foreach(l, indexQuals)
    6505             :     {
    6506       16080 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
    6507             : 
    6508       16080 :         if (IsA(rinfo->clause, ScalarArrayOpExpr))
    6509             :         {
    6510         301 :             ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
    6511         301 :             int         alength = estimate_array_length(lsecond(saop->args));
    6512             : 
    6513         301 :             if (alength > 1)
    6514         301 :                 num_sa_scans *= alength;
    6515             :         }
    6516             :     }
    6517             : 
    6518             :     /* Estimate the fraction of main-table tuples that will be visited */
    6519       20870 :     indexSelectivity = clauselist_selectivity(root, selectivityQuals,
    6520       20870 :                                               index->rel->relid,
    6521             :                                               JOIN_INNER,
    6522             :                                               NULL);
    6523             : 
    6524             :     /*
    6525             :      * If caller didn't give us an estimate, estimate the number of index
    6526             :      * tuples that will be visited.  We do it in this rather peculiar-looking
    6527             :      * way in order to get the right answer for partial indexes.
    6528             :      */
    6529       20870 :     numIndexTuples = costs->numIndexTuples;
    6530       20870 :     if (numIndexTuples <= 0.0)
    6531             :     {
    6532         851 :         numIndexTuples = indexSelectivity * index->rel->tuples;
    6533             : 
    6534             :         /*
    6535             :          * The above calculation counts all the tuples visited across all
    6536             :          * scans induced by ScalarArrayOpExpr nodes.  We want to consider the
    6537             :          * average per-indexscan number, so adjust.  This is a handy place to
    6538             :          * round to integer, too.  (If caller supplied tuple estimate, it's
    6539             :          * responsible for handling these considerations.)
    6540             :          */
    6541         851 :         numIndexTuples = rint(numIndexTuples / num_sa_scans);
    6542             :     }
    6543             : 
    6544             :     /*
    6545             :      * We can bound the number of tuples by the index size in any case. Also,
    6546             :      * always estimate at least one tuple is touched, even when
    6547             :      * indexSelectivity estimate is tiny.
    6548             :      */
    6549       20870 :     if (numIndexTuples > index->tuples)
    6550          40 :         numIndexTuples = index->tuples;
    6551       20870 :     if (numIndexTuples < 1.0)
    6552         494 :         numIndexTuples = 1.0;
    6553             : 
    6554             :     /*
    6555             :      * Estimate the number of index pages that will be retrieved.
    6556             :      *
    6557             :      * We use the simplistic method of taking a pro-rata fraction of the total
    6558             :      * number of index pages.  In effect, this counts only leaf pages and not
    6559             :      * any overhead such as index metapage or upper tree levels.
    6560             :      *
    6561             :      * In practice access to upper index levels is often nearly free because
    6562             :      * those tend to stay in cache under load; moreover, the cost involved is
    6563             :      * highly dependent on index type.  We therefore ignore such costs here
    6564             :      * and leave it to the caller to add a suitable charge if needed.
    6565             :      */
    6566       20870 :     if (index->pages > 1 && index->tuples > 1)
    6567       19532 :         numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
    6568             :     else
    6569        1338 :         numIndexPages = 1.0;
    6570             : 
    6571             :     /* fetch estimated page cost for tablespace containing index */
    6572       20870 :     get_tablespace_page_costs(index->reltablespace,
    6573             :                               &spc_random_page_cost,
    6574             :                               NULL);
    6575             : 
    6576             :     /*
    6577             :      * Now compute the disk access costs.
    6578             :      *
    6579             :      * The above calculations are all per-index-scan.  However, if we are in a
    6580             :      * nestloop inner scan, we can expect the scan to be repeated (with
    6581             :      * different search keys) for each row of the outer relation.  Likewise,
    6582             :      * ScalarArrayOpExpr quals result in multiple index scans.  This creates
    6583             :      * the potential for cache effects to reduce the number of disk page
    6584             :      * fetches needed.  We want to estimate the average per-scan I/O cost in
    6585             :      * the presence of caching.
    6586             :      *
    6587             :      * We use the Mackert-Lohman formula (see costsize.c for details) to
    6588             :      * estimate the total number of page fetches that occur.  While this
    6589             :      * wasn't what it was designed for, it seems a reasonable model anyway.
    6590             :      * Note that we are counting pages not tuples anymore, so we take N = T =
    6591             :      * index size, as if there were one "tuple" per page.
    6592             :      */
    6593       20870 :     num_outer_scans = loop_count;
    6594       20870 :     num_scans = num_sa_scans * num_outer_scans;
    6595             : 
    6596       20870 :     if (num_scans > 1)
    6597             :     {
    6598             :         double      pages_fetched;
    6599             : 
    6600             :         /* total page fetches ignoring cache effects */
    6601        2730 :         pages_fetched = numIndexPages * num_scans;
    6602             : 
    6603             :         /* use Mackert and Lohman formula to adjust for cache effects */
    6604        2730 :         pages_fetched = index_pages_fetched(pages_fetched,
    6605             :                                             index->pages,
    6606        2730 :                                             (double) index->pages,
    6607             :                                             root);
    6608             : 
    6609             :         /*
    6610             :          * Now compute the total disk access cost, and then report a pro-rated
    6611             :          * share for each outer scan.  (Don't pro-rate for ScalarArrayOpExpr,
    6612             :          * since that's internal to the indexscan.)
    6613             :          */
    6614        2730 :         indexTotalCost = (pages_fetched * spc_random_page_cost)
    6615             :             / num_outer_scans;
    6616             :     }
    6617             :     else
    6618             :     {
    6619             :         /*
    6620             :          * For a single index scan, we just charge spc_random_page_cost per
    6621             :          * page touched.
    6622             :          */
    6623       18140 :         indexTotalCost = numIndexPages * spc_random_page_cost;
    6624             :     }
    6625             : 
    6626             :     /*
    6627             :      * CPU cost: any complex expressions in the indexquals will need to be
    6628             :      * evaluated once at the start of the scan to reduce them to runtime keys
    6629             :      * to pass to the index AM (see nodeIndexscan.c).  We model the per-tuple
    6630             :      * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
    6631             :      * indexqual operator.  Because we have numIndexTuples as a per-scan
    6632             :      * number, we have to multiply by num_sa_scans to get the correct result
    6633             :      * for ScalarArrayOpExpr cases.  Similarly add in costs for any index
    6634             :      * ORDER BY expressions.
    6635             :      *
    6636             :      * Note: this neglects the possible costs of rechecking lossy operators.
    6637             :      * Detecting that that might be needed seems more expensive than it's
    6638             :      * worth, though, considering all the other inaccuracies here ...
    6639             :      */
    6640       41740 :     qual_arg_cost = other_operands_eval_cost(root, qinfos) +
    6641       20870 :         orderby_operands_eval_cost(root, path);
    6642       41740 :     qual_op_cost = cpu_operator_cost *
    6643       20870 :         (list_length(indexQuals) + list_length(indexOrderBys));
    6644             : 
    6645       20870 :     indexStartupCost = qual_arg_cost;
    6646       20870 :     indexTotalCost += qual_arg_cost;
    6647       20870 :     indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
    6648             : 
    6649             :     /*
    6650             :      * Generic assumption about index correlation: there isn't any.
    6651             :      */
    6652       20870 :     indexCorrelation = 0.0;
    6653             : 
    6654             :     /*
    6655             :      * Return everything to caller.
    6656             :      */
    6657       20870 :     costs->indexStartupCost = indexStartupCost;
    6658       20870 :     costs->indexTotalCost = indexTotalCost;
    6659       20870 :     costs->indexSelectivity = indexSelectivity;
    6660       20870 :     costs->indexCorrelation = indexCorrelation;
    6661       20870 :     costs->numIndexPages = numIndexPages;
    6662       20870 :     costs->numIndexTuples = numIndexTuples;
    6663       20870 :     costs->spc_random_page_cost = spc_random_page_cost;
    6664       20870 :     costs->num_sa_scans = num_sa_scans;
    6665       20870 : }
    6666             : 
    6667             : /*
    6668             :  * If the index is partial, add its predicate to the given qual list.
    6669             :  *
    6670             :  * ANDing the index predicate with the explicitly given indexquals produces
    6671             :  * a more accurate idea of the index's selectivity.  However, we need to be
    6672             :  * careful not to insert redundant clauses, because clauselist_selectivity()
    6673             :  * is easily fooled into computing a too-low selectivity estimate.  Our
    6674             :  * approach is to add only the predicate clause(s) that cannot be proven to
    6675             :  * be implied by the given indexquals.  This successfully handles cases such
    6676             :  * as a qual "x = 42" used with a partial index "WHERE x >= 40 AND x < 50".
    6677             :  * There are many other cases where we won't detect redundancy, leading to a
    6678             :  * too-low selectivity estimate, which will bias the system in favor of using
    6679             :  * partial indexes where possible.  That is not necessarily bad though.
    6680             :  *
    6681             :  * Note that indexQuals contains RestrictInfo nodes while the indpred
    6682             :  * does not, so the output list will be mixed.  This is OK for both
    6683             :  * predicate_implied_by() and clauselist_selectivity(), but might be
    6684             :  * problematic if the result were passed to other things.
    6685             :  */
    6686             : static List *
    6687       35538 : add_predicate_to_quals(IndexOptInfo *index, List *indexQuals)
    6688             : {
    6689       35538 :     List       *predExtraQuals = NIL;
    6690             :     ListCell   *lc;
    6691             : 
    6692       35538 :     if (index->indpred == NIL)
    6693       35368 :         return indexQuals;
    6694             : 
    6695         342 :     foreach(lc, index->indpred)
    6696             :     {
    6697         172 :         Node       *predQual = (Node *) lfirst(lc);
    6698         172 :         List       *oneQual = list_make1(predQual);
    6699             : 
    6700         172 :         if (!predicate_implied_by(oneQual, indexQuals, false))
    6701         138 :             predExtraQuals = list_concat(predExtraQuals, oneQual);
    6702             :     }
    6703             :     /* list_concat avoids modifying the passed-in indexQuals list */
    6704         170 :     return list_concat(predExtraQuals, indexQuals);
    6705             : }
    6706             : 
    6707             : 
    6708             : void
    6709       20450 : btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    6710             :                Cost *indexStartupCost, Cost *indexTotalCost,
    6711             :                Selectivity *indexSelectivity, double *indexCorrelation,
    6712             :                double *indexPages)
    6713             : {
    6714       20450 :     IndexOptInfo *index = path->indexinfo;
    6715             :     List       *qinfos;
    6716             :     GenericCosts costs;
    6717             :     Oid         relid;
    6718             :     AttrNumber  colnum;
    6719             :     VariableStatData vardata;
    6720             :     double      numIndexTuples;
    6721             :     Cost        descentCost;
    6722             :     List       *indexBoundQuals;
    6723             :     int         indexcol;
    6724             :     bool        eqQualHere;
    6725             :     bool        found_saop;
    6726             :     bool        found_is_null_op;
    6727             :     double      num_sa_scans;
    6728             :     ListCell   *lc;
    6729             : 
    6730             :     /* Do preliminary analysis of indexquals */
    6731       20450 :     qinfos = deconstruct_indexquals(path);
    6732             : 
    6733             :     /*
    6734             :      * For a btree scan, only leading '=' quals plus inequality quals for the
    6735             :      * immediately next attribute contribute to index selectivity (these are
    6736             :      * the "boundary quals" that determine the starting and stopping points of
    6737             :      * the index scan).  Additional quals can suppress visits to the heap, so
    6738             :      * it's OK to count them in indexSelectivity, but they should not count
    6739             :      * for estimating numIndexTuples.  So we must examine the given indexquals
    6740             :      * to find out which ones count as boundary quals.  We rely on the
    6741             :      * knowledge that they are given in index column order.
    6742             :      *
    6743             :      * For a RowCompareExpr, we consider only the first column, just as
    6744             :      * rowcomparesel() does.
    6745             :      *
    6746             :      * If there's a ScalarArrayOpExpr in the quals, we'll actually perform N
    6747             :      * index scans not one, but the ScalarArrayOpExpr's operator can be
    6748             :      * considered to act the same as it normally does.
    6749             :      */
    6750       20450 :     indexBoundQuals = NIL;
    6751       20450 :     indexcol = 0;
    6752       20450 :     eqQualHere = false;
    6753       20450 :     found_saop = false;
    6754       20450 :     found_is_null_op = false;
    6755       20450 :     num_sa_scans = 1;
    6756       35584 :     foreach(lc, qinfos)
    6757             :     {
    6758       15653 :         IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(lc);
    6759       15653 :         RestrictInfo *rinfo = qinfo->rinfo;
    6760       15653 :         Expr       *clause = rinfo->clause;
    6761             :         Oid         clause_op;
    6762             :         int         op_strategy;
    6763             : 
    6764       15653 :         if (indexcol != qinfo->indexcol)
    6765             :         {
    6766             :             /* Beginning of a new column's quals */
    6767        2526 :             if (!eqQualHere)
    6768         496 :                 break;          /* done if no '=' qual for indexcol */
    6769        2030 :             eqQualHere = false;
    6770        2030 :             indexcol++;
    6771        2030 :             if (indexcol != qinfo->indexcol)
    6772          23 :                 break;          /* no quals at all for indexcol */
    6773             :         }
    6774             : 
    6775       15134 :         if (IsA(clause, ScalarArrayOpExpr))
    6776             :         {
    6777         278 :             int         alength = estimate_array_length(qinfo->other_operand);
    6778             : 
    6779         278 :             found_saop = true;
    6780             :             /* count up number of SA scans induced by indexBoundQuals only */
    6781         278 :             if (alength > 1)
    6782         278 :                 num_sa_scans *= alength;
    6783             :         }
    6784       14856 :         else if (IsA(clause, NullTest))
    6785             :         {
    6786         333 :             NullTest   *nt = (NullTest *) clause;
    6787             : 
    6788         333 :             if (nt->nulltesttype == IS_NULL)
    6789             :             {
    6790          16 :                 found_is_null_op = true;
    6791             :                 /* IS NULL is like = for selectivity determination purposes */
    6792          16 :                 eqQualHere = true;
    6793             :             }
    6794             :         }
    6795             : 
    6796             :         /*
    6797             :          * We would need to commute the clause_op if not varonleft, except
    6798             :          * that we only care if it's equality or not, so that refinement is
    6799             :          * unnecessary.
    6800             :          */
    6801       15134 :         clause_op = qinfo->clause_op;
    6802             : 
    6803             :         /* check for equality operator */
    6804       15134 :         if (OidIsValid(clause_op))
    6805             :         {
    6806       14801 :             op_strategy = get_op_opfamily_strategy(clause_op,
    6807       14801 :                                                    index->opfamily[indexcol]);
    6808       14801 :             Assert(op_strategy != 0);   /* not a member of opfamily?? */
    6809       14801 :             if (op_strategy == BTEqualStrategyNumber)
    6810       13585 :                 eqQualHere = true;
    6811             :         }
    6812             : 
    6813       15134 :         indexBoundQuals = lappend(indexBoundQuals, rinfo);
    6814             :     }
    6815             : 
    6816             :     /*
    6817             :      * If index is unique and we found an '=' clause for each column, we can
    6818             :      * just assume numIndexTuples = 1 and skip the expensive
    6819             :      * clauselist_selectivity calculations.  However, a ScalarArrayOp or
    6820             :      * NullTest invalidates that theory, even though it sets eqQualHere.
    6821             :      */
    6822       37138 :     if (index->unique &&
    6823       28465 :         indexcol == index->ncolumns - 1 &&
    6824        6028 :         eqQualHere &&
    6825        5790 :         !found_saop &&
    6826             :         !found_is_null_op)
    6827        5782 :         numIndexTuples = 1.0;
    6828             :     else
    6829             :     {
    6830             :         List       *selectivityQuals;
    6831             :         Selectivity btreeSelectivity;
    6832             : 
    6833             :         /*
    6834             :          * If the index is partial, AND the index predicate with the
    6835             :          * index-bound quals to produce a more accurate idea of the number of
    6836             :          * rows covered by the bound conditions.
    6837             :          */
    6838       14668 :         selectivityQuals = add_predicate_to_quals(index, indexBoundQuals);
    6839             : 
    6840       14668 :         btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
    6841       14668 :                                                   index->rel->relid,
    6842             :                                                   JOIN_INNER,
    6843             :                                                   NULL);
    6844       14668 :         numIndexTuples = btreeSelectivity * index->rel->tuples;
    6845             : 
    6846             :         /*
    6847             :          * As in genericcostestimate(), we have to adjust for any
    6848             :          * ScalarArrayOpExpr quals included in indexBoundQuals, and then round
    6849             :          * to integer.
    6850             :          */
    6851       14668 :         numIndexTuples = rint(numIndexTuples / num_sa_scans);
    6852             :     }
    6853             : 
    6854             :     /*
    6855             :      * Now do generic index cost estimation.
    6856             :      */
    6857       20450 :     MemSet(&costs, 0, sizeof(costs));
    6858       20450 :     costs.numIndexTuples = numIndexTuples;
    6859             : 
    6860       20450 :     genericcostestimate(root, path, loop_count, qinfos, &costs);
    6861             : 
    6862             :     /*
    6863             :      * Add a CPU-cost component to represent the costs of initial btree
    6864             :      * descent.  We don't charge any I/O cost for touching upper btree levels,
    6865             :      * since they tend to stay in cache, but we still have to do about log2(N)
    6866             :      * comparisons to descend a btree of N leaf tuples.  We charge one
    6867             :      * cpu_operator_cost per comparison.
    6868             :      *
    6869             :      * If there are ScalarArrayOpExprs, charge this once per SA scan.  The
    6870             :      * ones after the first one are not startup cost so far as the overall
    6871             :      * plan is concerned, so add them only to "total" cost.
    6872             :      */
    6873       20450 :     if (index->tuples > 1)        /* avoid computing log(0) */
    6874             :     {
    6875       20343 :         descentCost = ceil(log(index->tuples) / log(2.0)) * cpu_operator_cost;
    6876       20343 :         costs.indexStartupCost += descentCost;
    6877       20343 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
    6878             :     }
    6879             : 
    6880             :     /*
    6881             :      * Even though we're not charging I/O cost for touching upper btree pages,
    6882             :      * it's still reasonable to charge some CPU cost per page descended
    6883             :      * through.  Moreover, if we had no such charge at all, bloated indexes
    6884             :      * would appear to have the same search cost as unbloated ones, at least
    6885             :      * in cases where only a single leaf page is expected to be visited.  This
    6886             :      * cost is somewhat arbitrarily set at 50x cpu_operator_cost per page
    6887             :      * touched.  The number of such pages is btree tree height plus one (ie,
    6888             :      * we charge for the leaf page too).  As above, charge once per SA scan.
    6889             :      */
    6890       20450 :     descentCost = (index->tree_height + 1) * 50.0 * cpu_operator_cost;
    6891       20450 :     costs.indexStartupCost += descentCost;
    6892       20450 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
    6893             : 
    6894             :     /*
    6895             :      * If we can get an estimate of the first column's ordering correlation C
    6896             :      * from pg_statistic, estimate the index correlation as C for a
    6897             :      * single-column index, or C * 0.75 for multiple columns. (The idea here
    6898             :      * is that multiple columns dilute the importance of the first column's
    6899             :      * ordering, but don't negate it entirely.  Before 8.0 we divided the
    6900             :      * correlation by the number of columns, but that seems too strong.)
    6901             :      */
    6902       20450 :     MemSet(&vardata, 0, sizeof(vardata));
    6903             : 
    6904       20450 :     if (index->indexkeys[0] != 0)
    6905             :     {
    6906             :         /* Simple variable --- look to stats for the underlying table */
    6907       20323 :         RangeTblEntry *rte = planner_rt_fetch(index->rel->relid, root);
    6908             : 
    6909       20323 :         Assert(rte->rtekind == RTE_RELATION);
    6910       20323 :         relid = rte->relid;
    6911       20323 :         Assert(relid != InvalidOid);
    6912       20323 :         colnum = index->indexkeys[0];
    6913             : 
    6914       20323 :         if (get_relation_stats_hook &&
    6915           0 :             (*get_relation_stats_hook) (root, rte, colnum, &vardata))
    6916             :         {
    6917             :             /*
    6918             :              * The hook took control of acquiring a stats tuple.  If it did
    6919             :              * supply a tuple, it'd better have supplied a freefunc.
    6920             :              */
    6921           0 :             if (HeapTupleIsValid(vardata.statsTuple) &&
    6922           0 :                 !vardata.freefunc)
    6923           0 :                 elog(ERROR, "no function provided to release variable stats with");
    6924             :         }
    6925             :         else
    6926             :         {
    6927       20323 :             vardata.statsTuple = SearchSysCache3(STATRELATTINH,
    6928             :                                                  ObjectIdGetDatum(relid),
    6929             :                                                  Int16GetDatum(colnum),
    6930             :                                                  BoolGetDatum(rte->inh));
    6931       20323 :             vardata.freefunc = ReleaseSysCache;
    6932             :         }
    6933             :     }
    6934             :     else
    6935             :     {
    6936             :         /* Expression --- maybe there are stats for the index itself */
    6937         127 :         relid = index->indexoid;
    6938         127 :         colnum = 1;
    6939             : 
    6940         127 :         if (get_index_stats_hook &&
    6941           0 :             (*get_index_stats_hook) (root, relid, colnum, &vardata))
    6942             :         {
    6943             :             /*
    6944             :              * The hook took control of acquiring a stats tuple.  If it did
    6945             :              * supply a tuple, it'd better have supplied a freefunc.
    6946             :              */
    6947           0 :             if (HeapTupleIsValid(vardata.statsTuple) &&
    6948           0 :                 !vardata.freefunc)
    6949           0 :                 elog(ERROR, "no function provided to release variable stats with");
    6950             :         }
    6951             :         else
    6952             :         {
    6953         127 :             vardata.statsTuple = SearchSysCache3(STATRELATTINH,
    6954             :                                                  ObjectIdGetDatum(relid),
    6955             :                                                  Int16GetDatum(colnum),
    6956             :                                                  BoolGetDatum(false));
    6957         127 :             vardata.freefunc = ReleaseSysCache;
    6958             :         }
    6959             :     }
    6960             : 
    6961       20450 :     if (HeapTupleIsValid(vardata.statsTuple))
    6962             :     {
    6963             :         Oid         sortop;
    6964             :         AttStatsSlot sslot;
    6965             : 
    6966       15924 :         sortop = get_opfamily_member(index->opfamily[0],
    6967        7962 :                                      index->opcintype[0],
    6968        7962 :                                      index->opcintype[0],
    6969             :                                      BTLessStrategyNumber);
    6970       15924 :         if (OidIsValid(sortop) &&
    6971        7962 :             get_attstatsslot(&sslot, vardata.statsTuple,
    6972             :                              STATISTIC_KIND_CORRELATION, sortop,
    6973             :                              ATTSTATSSLOT_NUMBERS))
    6974             :         {
    6975             :             double      varCorrelation;
    6976             : 
    6977        7927 :             Assert(sslot.nnumbers == 1);
    6978        7927 :             varCorrelation = sslot.numbers[0];
    6979             : 
    6980        7927 :             if (index->reverse_sort[0])
    6981           0 :                 varCorrelation = -varCorrelation;
    6982             : 
    6983        7927 :             if (index->ncolumns > 1)
    6984        5001 :                 costs.indexCorrelation = varCorrelation * 0.75;
    6985             :             else
    6986        2926 :                 costs.indexCorrelation = varCorrelation;
    6987             : 
    6988        7927 :             free_attstatsslot(&sslot);
    6989             :         }
    6990             :     }
    6991             : 
    6992       20450 :     ReleaseVariableStats(vardata);
    6993             : 
    6994       20450 :     *indexStartupCost = costs.indexStartupCost;
    6995       20450 :     *indexTotalCost = costs.indexTotalCost;
    6996       20450 :     *indexSelectivity = costs.indexSelectivity;
    6997       20450 :     *indexCorrelation = costs.indexCorrelation;
    6998       20450 :     *indexPages = costs.numIndexPages;
    6999       20450 : }
    7000             : 
    7001             : void
    7002          26 : hashcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    7003             :                  Cost *indexStartupCost, Cost *indexTotalCost,
    7004             :                  Selectivity *indexSelectivity, double *indexCorrelation,
    7005             :                  double *indexPages)
    7006             : {
    7007             :     List       *qinfos;
    7008             :     GenericCosts costs;
    7009             : 
    7010             :     /* Do preliminary analysis of indexquals */
    7011          26 :     qinfos = deconstruct_indexquals(path);
    7012             : 
    7013          26 :     MemSet(&costs, 0, sizeof(costs));
    7014             : 
    7015          26 :     genericcostestimate(root, path, loop_count, qinfos, &costs);
    7016             : 
    7017             :     /*
    7018             :      * A hash index has no descent costs as such, since the index AM can go
    7019             :      * directly to the target bucket after computing the hash value.  There
    7020             :      * are a couple of other hash-specific costs that we could conceivably add
    7021             :      * here, though:
    7022             :      *
    7023             :      * Ideally we'd charge spc_random_page_cost for each page in the target
    7024             :      * bucket, not just the numIndexPages pages that genericcostestimate
    7025             :      * thought we'd visit.  However in most cases we don't know which bucket
    7026             :      * that will be.  There's no point in considering the average bucket size
    7027             :      * because the hash AM makes sure that's always one page.
    7028             :      *
    7029             :      * Likewise, we could consider charging some CPU for each index tuple in
    7030             :      * the bucket, if we knew how many there were.  But the per-tuple cost is
    7031             :      * just a hash value comparison, not a general datatype-dependent
    7032             :      * comparison, so any such charge ought to be quite a bit less than
    7033             :      * cpu_operator_cost; which makes it probably not worth worrying about.
    7034             :      *
    7035             :      * A bigger issue is that chance hash-value collisions will result in
    7036             :      * wasted probes into the heap.  We don't currently attempt to model this
    7037             :      * cost on the grounds that it's rare, but maybe it's not rare enough.
    7038             :      * (Any fix for this ought to consider the generic lossy-operator problem,
    7039             :      * though; it's not entirely hash-specific.)
    7040             :      */
    7041             : 
    7042          26 :     *indexStartupCost = costs.indexStartupCost;
    7043          26 :     *indexTotalCost = costs.indexTotalCost;
    7044          26 :     *indexSelectivity = costs.indexSelectivity;
    7045          26 :     *indexCorrelation = costs.indexCorrelation;
    7046          26 :     *indexPages = costs.numIndexPages;
    7047          26 : }
    7048             : 
    7049             : void
    7050         165 : gistcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    7051             :                  Cost *indexStartupCost, Cost *indexTotalCost,
    7052             :                  Selectivity *indexSelectivity, double *indexCorrelation,
    7053             :                  double *indexPages)
    7054             : {
    7055         165 :     IndexOptInfo *index = path->indexinfo;
    7056             :     List       *qinfos;
    7057             :     GenericCosts costs;
    7058             :     Cost        descentCost;
    7059             : 
    7060             :     /* Do preliminary analysis of indexquals */
    7061         165 :     qinfos = deconstruct_indexquals(path);
    7062             : 
    7063         165 :     MemSet(&costs, 0, sizeof(costs));
    7064             : 
    7065         165 :     genericcostestimate(root, path, loop_count, qinfos, &costs);
    7066             : 
    7067             :     /*
    7068             :      * We model index descent costs similarly to those for btree, but to do
    7069             :      * that we first need an idea of the tree height.  We somewhat arbitrarily
    7070             :      * assume that the fanout is 100, meaning the tree height is at most
    7071             :      * log100(index->pages).
    7072             :      *
    7073             :      * Although this computation isn't really expensive enough to require
    7074             :      * caching, we might as well use index->tree_height to cache it.
    7075             :      */
    7076         165 :     if (index->tree_height < 0) /* unknown? */
    7077             :     {
    7078         165 :         if (index->pages > 1) /* avoid computing log(0) */
    7079         100 :             index->tree_height = (int) (log(index->pages) / log(100.0));
    7080             :         else
    7081          65 :             index->tree_height = 0;
    7082             :     }
    7083             : 
    7084             :     /*
    7085             :      * Add a CPU-cost component to represent the costs of initial descent. We
    7086             :      * just use log(N) here not log2(N) since the branching factor isn't
    7087             :      * necessarily two anyway.  As for btree, charge once per SA scan.
    7088             :      */
    7089         165 :     if (index->tuples > 1)        /* avoid computing log(0) */
    7090             :     {
    7091         165 :         descentCost = ceil(log(index->tuples)) * cpu_operator_cost;
    7092         165 :         costs.indexStartupCost += descentCost;
    7093         165 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
    7094             :     }
    7095             : 
    7096             :     /*
    7097             :      * Likewise add a per-page charge, calculated the same as for btrees.
    7098             :      */
    7099         165 :     descentCost = (index->tree_height + 1) * 50.0 * cpu_operator_cost;
    7100         165 :     costs.indexStartupCost += descentCost;
    7101         165 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
    7102             : 
    7103         165 :     *indexStartupCost = costs.indexStartupCost;
    7104         165 :     *indexTotalCost = costs.indexTotalCost;
    7105         165 :     *indexSelectivity = costs.indexSelectivity;
    7106         165 :     *indexCorrelation = costs.indexCorrelation;
    7107         165 :     *indexPages = costs.numIndexPages;
    7108         165 : }
    7109             : 
    7110             : void
    7111         229 : spgcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    7112             :                 Cost *indexStartupCost, Cost *indexTotalCost,
    7113             :                 Selectivity *indexSelectivity, double *indexCorrelation,
    7114             :                 double *indexPages)
    7115             : {
    7116         229 :     IndexOptInfo *index = path->indexinfo;
    7117             :     List       *qinfos;
    7118             :     GenericCosts costs;
    7119             :     Cost        descentCost;
    7120             : 
    7121             :     /* Do preliminary analysis of indexquals */
    7122         229 :     qinfos = deconstruct_indexquals(path);
    7123             : 
    7124         229 :     MemSet(&costs, 0, sizeof(costs));
    7125             : 
    7126         229 :     genericcostestimate(root, path, loop_count, qinfos, &costs);
    7127             : 
    7128             :     /*
    7129             :      * We model index descent costs similarly to those for btree, but to do
    7130             :      * that we first need an idea of the tree height.  We somewhat arbitrarily
    7131             :      * assume that the fanout is 100, meaning the tree height is at most
    7132             :      * log100(index->pages).
    7133             :      *
    7134             :      * Although this computation isn't really expensive enough to require
    7135             :      * caching, we might as well use index->tree_height to cache it.
    7136             :      */
    7137         229 :     if (index->tree_height < 0) /* unknown? */
    7138             :     {
    7139         229 :         if (index->pages > 1) /* avoid computing log(0) */
    7140         229 :             index->tree_height = (int) (log(index->pages) / log(100.0));
    7141             :         else
    7142           0 :             index->tree_height = 0;
    7143             :     }
    7144             : 
    7145             :     /*
    7146             :      * Add a CPU-cost component to represent the costs of initial descent. We
    7147             :      * just use log(N) here not log2(N) since the branching factor isn't
    7148             :      * necessarily two anyway.  As for btree, charge once per SA scan.
    7149             :      */
    7150         229 :     if (index->tuples > 1)        /* avoid computing log(0) */
    7151             :     {
    7152         229 :         descentCost = ceil(log(index->tuples)) * cpu_operator_cost;
    7153         229 :         costs.indexStartupCost += descentCost;
    7154         229 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
    7155             :     }
    7156             : 
    7157             :     /*
    7158             :      * Likewise add a per-page charge, calculated the same as for btrees.
    7159             :      */
    7160         229 :     descentCost = (index->tree_height + 1) * 50.0 * cpu_operator_cost;
    7161         229 :     costs.indexStartupCost += descentCost;
    7162         229 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
    7163             : 
    7164         229 :     *indexStartupCost = costs.indexStartupCost;
    7165         229 :     *indexTotalCost = costs.indexTotalCost;
    7166         229 :     *indexSelectivity = costs.indexSelectivity;
    7167         229 :     *indexCorrelation = costs.indexCorrelation;
    7168         229 :     *indexPages = costs.numIndexPages;
    7169         229 : }
    7170             : 
    7171             : 
    7172             : /*
    7173             :  * Support routines for gincostestimate
    7174             :  */
    7175             : 
    7176             : typedef struct
    7177             : {
    7178             :     bool        haveFullScan;
    7179             :     double      partialEntries;
    7180             :     double      exactEntries;
    7181             :     double      searchEntries;
    7182             :     double      arrayScans;
    7183             : } GinQualCounts;
    7184             : 
    7185             : /*
    7186             :  * Estimate the number of index terms that need to be searched for while
    7187             :  * testing the given GIN query, and increment the counts in *counts
    7188             :  * appropriately.  If the query is unsatisfiable, return false.
    7189             :  */
    7190             : static bool
    7191          87 : gincost_pattern(IndexOptInfo *index, int indexcol,
    7192             :                 Oid clause_op, Datum query,
    7193             :                 GinQualCounts *counts)
    7194             : {
    7195             :     Oid         extractProcOid;
    7196             :     Oid         collation;
    7197             :     int         strategy_op;
    7198             :     Oid         lefttype,
    7199             :                 righttype;
    7200          87 :     int32       nentries = 0;
    7201          87 :     bool       *partial_matches = NULL;
    7202          87 :     Pointer    *extra_data = NULL;
    7203          87 :     bool       *nullFlags = NULL;
    7204          87 :     int32       searchMode = GIN_SEARCH_MODE_DEFAULT;
    7205             :     int32       i;
    7206             : 
    7207             :     /*
    7208             :      * Get the operator's strategy number and declared input data types within
    7209             :      * the index opfamily.  (We don't need the latter, but we use
    7210             :      * get_op_opfamily_properties because it will throw error if it fails to
    7211             :      * find a matching pg_amop entry.)
    7212             :      */
    7213          87 :     get_op_opfamily_properties(clause_op, index->opfamily[indexcol], false,
    7214             :                                &strategy_op, &lefttype, &righttype);
    7215             : 
    7216             :     /*
    7217             :      * GIN always uses the "default" support functions, which are those with
    7218             :      * lefttype == righttype == the opclass' opcintype (see
    7219             :      * IndexSupportInitialize in relcache.c).
    7220             :      */
    7221         174 :     extractProcOid = get_opfamily_proc(index->opfamily[indexcol],
    7222          87 :                                        index->opcintype[indexcol],
    7223          87 :                                        index->opcintype[indexcol],
    7224             :                                        GIN_EXTRACTQUERY_PROC);
    7225             : 
    7226          87 :     if (!OidIsValid(extractProcOid))
    7227             :     {
    7228             :         /* should not happen; throw same error as index_getprocinfo */
    7229           0 :         elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
    7230             :              GIN_EXTRACTQUERY_PROC, indexcol + 1,
    7231             :              get_rel_name(index->indexoid));
    7232             :     }
    7233             : 
    7234             :     /*
    7235             :      * Choose collation to pass to extractProc (should match initGinState).
    7236             :      */
    7237          87 :     if (OidIsValid(index->indexcollations[indexcol]))
    7238          18 :         collation = index->indexcollations[indexcol];
    7239             :     else
    7240          69 :         collation = DEFAULT_COLLATION_OID;
    7241             : 
    7242         174 :     OidFunctionCall7Coll(extractProcOid,
    7243             :                          collation,
    7244             :                          query,
    7245             :                          PointerGetDatum(&nentries),
    7246          87 :                          UInt16GetDatum(strategy_op),
    7247             :                          PointerGetDatum(&partial_matches),
    7248             :                          PointerGetDatum(&extra_data),
    7249             :                          PointerGetDatum(&nullFlags),
    7250             :                          PointerGetDatum(&searchMode));
    7251             : 
    7252          87 :     if (nentries <= 0 && searchMode == GIN_SEARCH_MODE_DEFAULT)
    7253             :     {
    7254             :         /* No match is possible */
    7255           2 :         return false;
    7256             :     }
    7257             : 
    7258         204 :     for (i = 0; i < nentries; i++)
    7259             :     {
    7260             :         /*
    7261             :          * For partial match we haven't any information to estimate number of
    7262             :          * matched entries in index, so, we just estimate it as 100
    7263             :          */
    7264         119 :         if (partial_matches && partial_matches[i])
    7265           7 :             counts->partialEntries += 100;
    7266             :         else
    7267         112 :             counts->exactEntries++;
    7268             : 
    7269         119 :         counts->searchEntries++;
    7270             :     }
    7271             : 
    7272          85 :     if (searchMode == GIN_SEARCH_MODE_INCLUDE_EMPTY)
    7273             :     {
    7274             :         /* Treat "include empty" like an exact-match item */
    7275           7 :         counts->exactEntries++;
    7276           7 :         counts->searchEntries++;
    7277             :     }
    7278          78 :     else if (searchMode != GIN_SEARCH_MODE_DEFAULT)
    7279             :     {
    7280             :         /* It's GIN_SEARCH_MODE_ALL */
    7281           5 :         counts->haveFullScan = true;
    7282             :     }
    7283             : 
    7284          85 :     return true;
    7285             : }
    7286             : 
    7287             : /*
    7288             :  * Estimate the number of index terms that need to be searched for while
    7289             :  * testing the given GIN index clause, and increment the counts in *counts
    7290             :  * appropriately.  If the query is unsatisfiable, return false.
    7291             :  */
    7292             : static bool
    7293          85 : gincost_opexpr(PlannerInfo *root,
    7294             :                IndexOptInfo *index,
    7295             :                IndexQualInfo *qinfo,
    7296             :                GinQualCounts *counts)
    7297             : {
    7298          85 :     int         indexcol = qinfo->indexcol;
    7299          85 :     Oid         clause_op = qinfo->clause_op;
    7300          85 :     Node       *operand = qinfo->other_operand;
    7301             : 
    7302          85 :     if (!qinfo->varonleft)
    7303             :     {
    7304             :         /* must commute the operator */
    7305           5 :         clause_op = get_commutator(clause_op);
    7306             :     }
    7307             : 
    7308             :     /* aggressively reduce to a constant, and look through relabeling */
    7309          85 :     operand = estimate_expression_value(root, operand);
    7310             : 
    7311          85 :     if (IsA(operand, RelabelType))
    7312           0 :         operand = (Node *) ((RelabelType *) operand)->arg;
    7313             : 
    7314             :     /*
    7315             :      * It's impossible to call extractQuery method for unknown operand. So
    7316             :      * unless operand is a Const we can't do much; just assume there will be
    7317             :      * one ordinary search entry from the operand at runtime.
    7318             :      */
    7319          85 :     if (!IsA(operand, Const))
    7320             :     {
    7321           0 :         counts->exactEntries++;
    7322           0 :         counts->searchEntries++;
    7323           0 :         return true;
    7324             :     }
    7325             : 
    7326             :     /* If Const is null, there can be no matches */
    7327          85 :     if (((Const *) operand)->constisnull)
    7328           0 :         return false;
    7329             : 
    7330             :     /* Otherwise, apply extractQuery and get the actual term counts */
    7331          85 :     return gincost_pattern(index, indexcol, clause_op,
    7332             :                            ((Const *) operand)->constvalue,
    7333             :                            counts);
    7334             : }
    7335             : 
    7336             : /*
    7337             :  * Estimate the number of index terms that need to be searched for while
    7338             :  * testing the given GIN index clause, and increment the counts in *counts
    7339             :  * appropriately.  If the query is unsatisfiable, return false.
    7340             :  *
    7341             :  * A ScalarArrayOpExpr will give rise to N separate indexscans at runtime,
    7342             :  * each of which involves one value from the RHS array, plus all the
    7343             :  * non-array quals (if any).  To model this, we average the counts across
    7344             :  * the RHS elements, and add the averages to the counts in *counts (which
    7345             :  * correspond to per-indexscan costs).  We also multiply counts->arrayScans
    7346             :  * by N, causing gincostestimate to scale up its estimates accordingly.
    7347             :  */
    7348             : static bool
    7349           1 : gincost_scalararrayopexpr(PlannerInfo *root,
    7350             :                           IndexOptInfo *index,
    7351             :                           IndexQualInfo *qinfo,
    7352             :                           double numIndexEntries,
    7353             :                           GinQualCounts *counts)
    7354             : {
    7355           1 :     int         indexcol = qinfo->indexcol;
    7356           1 :     Oid         clause_op = qinfo->clause_op;
    7357           1 :     Node       *rightop = qinfo->other_operand;
    7358             :     ArrayType  *arrayval;
    7359             :     int16       elmlen;
    7360             :     bool        elmbyval;
    7361             :     char        elmalign;
    7362             :     int         numElems;
    7363             :     Datum      *elemValues;
    7364             :     bool       *elemNulls;
    7365             :     GinQualCounts arraycounts;
    7366           1 :     int         numPossible = 0;
    7367             :     int         i;
    7368             : 
    7369           1 :     Assert(((ScalarArrayOpExpr *) qinfo->rinfo->clause)->useOr);
    7370             : 
    7371             :     /* aggressively reduce to a constant, and look through relabeling */
    7372           1 :     rightop = estimate_expression_value(root, rightop);
    7373             : 
    7374           1 :     if (IsA(rightop, RelabelType))
    7375           0 :         rightop = (Node *) ((RelabelType *) rightop)->arg;
    7376             : 
    7377             :     /*
    7378             :      * It's impossible to call extractQuery method for unknown operand. So
    7379             :      * unless operand is a Const we can't do much; just assume there will be
    7380             :      * one ordinary search entry from each array entry at runtime, and fall
    7381             :      * back on a probably-bad estimate of the number of array entries.
    7382             :      */
    7383           1 :     if (!IsA(rightop, Const))
    7384             :     {
    7385           0 :         counts->exactEntries++;
    7386           0 :         counts->searchEntries++;
    7387           0 :         counts->arrayScans *= estimate_array_length(rightop);
    7388           0 :         return true;
    7389             :     }
    7390             : 
    7391             :     /* If Const is null, there can be no matches */
    7392           1 :     if (((Const *) rightop)->constisnull)
    7393           0 :         return false;
    7394             : 
    7395             :     /* Otherwise, extract the array elements and iterate over them */
    7396           1 :     arrayval = DatumGetArrayTypeP(((Const *) rightop)->constvalue);
    7397           1 :     get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
    7398             :                          &elmlen, &elmbyval, &elmalign);
    7399           1 :     deconstruct_array(arrayval,
    7400             :                       ARR_ELEMTYPE(arrayval),
    7401             :                       elmlen, elmbyval, elmalign,
    7402             :                       &elemValues, &elemNulls, &numElems);
    7403             : 
    7404           1 :     memset(&arraycounts, 0, sizeof(arraycounts));
    7405             : 
    7406           3 :     for (i = 0; i < numElems; i++)
    7407             :     {
    7408             :         GinQualCounts elemcounts;
    7409             : 
    7410             :         /* NULL can't match anything, so ignore, as the executor will */
    7411           2 :         if (elemNulls[i])
    7412           0 :             continue;
    7413             : 
    7414             :         /* Otherwise, apply extractQuery and get the actual term counts */
    7415           2 :         memset(&elemcounts, 0, sizeof(elemcounts));
    7416             : 
    7417           2 :         if (gincost_pattern(index, indexcol, clause_op, elemValues[i],
    7418             :                             &elemcounts))
    7419             :         {
    7420             :             /* We ignore array elements that are unsatisfiable patterns */
    7421           2 :             numPossible++;
    7422             : 
    7423           2 :             if (elemcounts.haveFullScan)
    7424             :             {
    7425             :                 /*
    7426             :                  * Full index scan will be required.  We treat this as if
    7427             :                  * every key in the index had been listed in the query; is
    7428             :                  * that reasonable?
    7429             :                  */
    7430           0 :                 elemcounts.partialEntries = 0;
    7431           0 :                 elemcounts.exactEntries = numIndexEntries;
    7432           0 :                 elemcounts.searchEntries = numIndexEntries;
    7433             :             }
    7434           2 :             arraycounts.partialEntries += elemcounts.partialEntries;
    7435           2 :             arraycounts.exactEntries += elemcounts.exactEntries;
    7436           2 :             arraycounts.searchEntries += elemcounts.searchEntries;
    7437             :         }
    7438             :     }
    7439             : 
    7440           1 :     if (numPossible == 0)
    7441             :     {
    7442             :         /* No satisfiable patterns in the array */
    7443           0 :         return false;
    7444             :     }
    7445             : 
    7446             :     /*
    7447             :      * Now add the averages to the global counts.  This will give us an
    7448             :      * estimate of the average number of terms searched for in each indexscan,
    7449             :      * including contributions from both array and non-array quals.
    7450             :      */
    7451           1 :     counts->partialEntries += arraycounts.partialEntries / numPossible;
    7452           1 :     counts->exactEntries += arraycounts.exactEntries / numPossible;
    7453           1 :     counts->searchEntries += arraycounts.searchEntries / numPossible;
    7454             : 
    7455           1 :     counts->arrayScans *= numPossible;
    7456             : 
    7457           1 :     return true;
    7458             : }
    7459             : 
    7460             : /*
    7461             :  * GIN has search behavior completely different from other index types
    7462             :  */
    7463             : void
    7464          84 : gincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    7465             :                 Cost *indexStartupCost, Cost *indexTotalCost,
    7466             :                 Selectivity *indexSelectivity, double *indexCorrelation,
    7467             :                 double *indexPages)
    7468             : {
    7469          84 :     IndexOptInfo *index = path->indexinfo;
    7470          84 :     List       *indexQuals = path->indexquals;
    7471          84 :     List       *indexOrderBys = path->indexorderbys;
    7472             :     List       *qinfos;
    7473             :     ListCell   *l;
    7474             :     List       *selectivityQuals;
    7475          84 :     double      numPages = index->pages,
    7476          84 :                 numTuples = index->tuples;
    7477             :     double      numEntryPages,
    7478             :                 numDataPages,
    7479             :                 numPendingPages,
    7480             :                 numEntries;
    7481             :     GinQualCounts counts;
    7482             :     bool        matchPossible;
    7483             :     double      partialScale;
    7484             :     double      entryPagesFetched,
    7485             :                 dataPagesFetched,
    7486             :                 dataPagesFetchedBySel;
    7487             :     double      qual_op_cost,
    7488             :                 qual_arg_cost,
    7489             :                 spc_random_page_cost,
    7490             :                 outer_scans;
    7491             :     Relation    indexRel;
    7492             :     GinStatsData ginStats;
    7493             : 
    7494             :     /* Do preliminary analysis of indexquals */
    7495          84 :     qinfos = deconstruct_indexquals(path);
    7496             : 
    7497             :     /*
    7498             :      * Obtain statistical information from the meta page, if possible.  Else
    7499             :      * set ginStats to zeroes, and we'll cope below.
    7500             :      */
    7501          84 :     if (!index->hypothetical)
    7502             :     {
    7503          84 :         indexRel = index_open(index->indexoid, AccessShareLock);
    7504          84 :         ginGetStats(indexRel, &ginStats);
    7505          84 :         index_close(indexRel, AccessShareLock);
    7506             :     }
    7507             :     else
    7508             :     {
    7509           0 :         memset(&ginStats, 0, sizeof(ginStats));
    7510             :     }
    7511             : 
    7512             :     /*
    7513             :      * Assuming we got valid (nonzero) stats at all, nPendingPages can be
    7514             :      * trusted, but the other fields are data as of the last VACUUM.  We can
    7515             :      * scale them up to account for growth since then, but that method only
    7516             :      * goes so far; in the worst case, the stats might be for a completely
    7517             :      * empty index, and scaling them will produce pretty bogus numbers.
    7518             :      * Somewhat arbitrarily, set the cutoff for doing scaling at 4X growth; if
    7519             :      * it's grown more than that, fall back to estimating things only from the
    7520             :      * assumed-accurate index size.  But we'll trust nPendingPages in any case
    7521             :      * so long as it's not clearly insane, ie, more than the index size.
    7522             :      */
    7523          84 :     if (ginStats.nPendingPages < numPages)
    7524          84 :         numPendingPages = ginStats.nPendingPages;
    7525             :     else
    7526           0 :         numPendingPages = 0;
    7527             : 
    7528         168 :     if (numPages > 0 && ginStats.nTotalPages <= numPages &&
    7529         168 :         ginStats.nTotalPages > numPages / 4 &&
    7530         168 :         ginStats.nEntryPages > 0 && ginStats.nEntries > 0)
    7531          79 :     {
    7532             :         /*
    7533             :          * OK, the stats seem close enough to sane to be trusted.  But we
    7534             :          * still need to scale them by the ratio numPages / nTotalPages to
    7535             :          * account for growth since the last VACUUM.
    7536             :          */
    7537          79 :         double      scale = numPages / ginStats.nTotalPages;
    7538             : 
    7539          79 :         numEntryPages = ceil(ginStats.nEntryPages * scale);
    7540          79 :         numDataPages = ceil(ginStats.nDataPages * scale);
    7541          79 :         numEntries = ceil(ginStats.nEntries * scale);
    7542             :         /* ensure we didn't round up too much */
    7543          79 :         numEntryPages = Min(numEntryPages, numPages - numPendingPages);
    7544          79 :         numDataPages = Min(numDataPages,
    7545             :                            numPages - numPendingPages - numEntryPages);
    7546             :     }
    7547             :     else
    7548             :     {
    7549             :         /*
    7550             :          * We might get here because it's a hypothetical index, or an index
    7551             :          * created pre-9.1 and never vacuumed since upgrading (in which case
    7552             :          * its stats would read as zeroes), or just because it's grown too
    7553             :          * much since the last VACUUM for us to put our faith in scaling.
    7554             :          *
    7555             :          * Invent some plausible internal statistics based on the index page
    7556             :          * count (and clamp that to at least 10 pages, just in case).  We
    7557             :          * estimate that 90% of the index is entry pages, and the rest is data
    7558             :          * pages.  Estimate 100 entries per entry page; this is rather bogus
    7559             :          * since it'll depend on the size of the keys, but it's more robust
    7560             :          * than trying to predict the number of entries per heap tuple.
    7561             :          */
    7562           5 :         numPages = Max(numPages, 10);
    7563           5 :         numEntryPages = floor((numPages - numPendingPages) * 0.90);
    7564           5 :         numDataPages = numPages - numPendingPages - numEntryPages;
    7565           5 :         numEntries = floor(numEntryPages * 100);
    7566             :     }
    7567             : 
    7568             :     /* In an empty index, numEntries could be zero.  Avoid divide-by-zero */
    7569          84 :     if (numEntries < 1)
    7570           0 :         numEntries = 1;
    7571             : 
    7572             :     /*
    7573             :      * Include predicate in selectivityQuals (should match
    7574             :      * genericcostestimate)
    7575             :      */
    7576          84 :     if (index->indpred != NIL)
    7577             :     {
    7578           0 :         List       *predExtraQuals = NIL;
    7579             : 
    7580           0 :         foreach(l, index->indpred)
    7581             :         {
    7582           0 :             Node       *predQual = (Node *) lfirst(l);
    7583           0 :             List       *oneQual = list_make1(predQual);
    7584             : 
    7585           0 :             if (!predicate_implied_by(oneQual, indexQuals, false))
    7586           0 :                 predExtraQuals = list_concat(predExtraQuals, oneQual);
    7587             :         }
    7588             :         /* list_concat avoids modifying the passed-in indexQuals list */
    7589           0 :         selectivityQuals = list_concat(predExtraQuals, indexQuals);
    7590             :     }
    7591             :     else
    7592          84 :         selectivityQuals = indexQuals;
    7593             : 
    7594             :     /* Estimate the fraction of main-table tuples that will be visited */
    7595          84 :     *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
    7596          84 :                                                index->rel->relid,
    7597             :                                                JOIN_INNER,
    7598             :                                                NULL);
    7599             : 
    7600             :     /* fetch estimated page cost for tablespace containing index */
    7601          84 :     get_tablespace_page_costs(index->reltablespace,
    7602             :                               &spc_random_page_cost,
    7603             :                               NULL);
    7604             : 
    7605             :     /*
    7606             :      * Generic assumption about index correlation: there isn't any.
    7607             :      */
    7608          84 :     *indexCorrelation = 0.0;
    7609             : 
    7610             :     /*
    7611             :      * Examine quals to estimate number of search entries & partial matches
    7612             :      */
    7613          84 :     memset(&counts, 0, sizeof(counts));
    7614          84 :     counts.arrayScans = 1;
    7615          84 :     matchPossible = true;
    7616             : 
    7617         168 :     foreach(l, qinfos)
    7618             :     {
    7619          86 :         IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(l);
    7620          86 :         Expr       *clause = qinfo->rinfo->clause;
    7621             : 
    7622          86 :         if (IsA(clause, OpExpr))
    7623             :         {
    7624          85 :             matchPossible = gincost_opexpr(root,
    7625             :                                            index,
    7626             :                                            qinfo,
    7627             :                                            &counts);
    7628          85 :             if (!matchPossible)
    7629           2 :                 break;
    7630             :         }
    7631           1 :         else if (IsA(clause, ScalarArrayOpExpr))
    7632             :         {
    7633           1 :             matchPossible = gincost_scalararrayopexpr(root,
    7634             :                                                       index,
    7635             :                                                       qinfo,
    7636             :                                                       numEntries,
    7637             :                                                       &counts);
    7638           1 :             if (!matchPossible)
    7639           0 :                 break;
    7640             :         }
    7641             :         else
    7642             :         {
    7643             :             /* shouldn't be anything else for a GIN index */
    7644           0 :             elog(ERROR, "unsupported GIN indexqual type: %d",
    7645             :                  (int) nodeTag(clause));
    7646             :         }
    7647             :     }
    7648             : 
    7649             :     /* Fall out if there were any provably-unsatisfiable quals */
    7650          84 :     if (!matchPossible)
    7651             :     {
    7652           2 :         *indexStartupCost = 0;
    7653           2 :         *indexTotalCost = 0;
    7654           2 :         *indexSelectivity = 0;
    7655          86 :         return;
    7656             :     }
    7657             : 
    7658          82 :     if (counts.haveFullScan || indexQuals == NIL)
    7659             :     {
    7660             :         /*
    7661             :          * Full index scan will be required.  We treat this as if every key in
    7662             :          * the index had been listed in the query; is that reasonable?
    7663             :          */
    7664           5 :         counts.partialEntries = 0;
    7665           5 :         counts.exactEntries = numEntries;
    7666           5 :         counts.searchEntries = numEntries;
    7667             :     }
    7668             : 
    7669             :     /* Will we have more than one iteration of a nestloop scan? */
    7670          82 :     outer_scans = loop_count;
    7671             : 
    7672             :     /*
    7673             :      * Compute cost to begin scan, first of all, pay attention to pending
    7674             :      * list.
    7675             :      */
    7676          82 :     entryPagesFetched = numPendingPages;
    7677             : 
    7678             :     /*
    7679             :      * Estimate number of entry pages read.  We need to do
    7680             :      * counts.searchEntries searches.  Use a power function as it should be,
    7681             :      * but tuples on leaf pages usually is much greater. Here we include all
    7682             :      * searches in entry tree, including search of first entry in partial
    7683             :      * match algorithm
    7684             :      */
    7685          82 :     entryPagesFetched += ceil(counts.searchEntries * rint(pow(numEntryPages, 0.15)));
    7686             : 
    7687             :     /*
    7688             :      * Add an estimate of entry pages read by partial match algorithm. It's a
    7689             :      * scan over leaf pages in entry tree.  We haven't any useful stats here,
    7690             :      * so estimate it as proportion.  Because counts.partialEntries is really
    7691             :      * pretty bogus (see code above), it's possible that it is more than
    7692             :      * numEntries; clamp the proportion to ensure sanity.
    7693             :      */
    7694          82 :     partialScale = counts.partialEntries / numEntries;
    7695          82 :     partialScale = Min(partialScale, 1.0);
    7696             : 
    7697          82 :     entryPagesFetched += ceil(numEntryPages * partialScale);
    7698             : 
    7699             :     /*
    7700             :      * Partial match algorithm reads all data pages before doing actual scan,
    7701             :      * so it's a startup cost.  Again, we haven't any useful stats here, so
    7702             :      * estimate it as proportion.
    7703             :      */
    7704          82 :     dataPagesFetched = ceil(numDataPages * partialScale);
    7705             : 
    7706             :     /*
    7707             :      * Calculate cache effects if more than one scan due to nestloops or array
    7708             :      * quals.  The result is pro-rated per nestloop scan, but the array qual
    7709             :      * factor shouldn't be pro-rated (compare genericcostestimate).
    7710             :      */
    7711          82 :     if (outer_scans > 1 || counts.arrayScans > 1)
    7712             :     {
    7713           1 :         entryPagesFetched *= outer_scans * counts.arrayScans;
    7714           1 :         entryPagesFetched = index_pages_fetched(entryPagesFetched,
    7715             :                                                 (BlockNumber) numEntryPages,
    7716             :                                                 numEntryPages, root);
    7717           1 :         entryPagesFetched /= outer_scans;
    7718           1 :         dataPagesFetched *= outer_scans * counts.arrayScans;
    7719           1 :         dataPagesFetched = index_pages_fetched(dataPagesFetched,
    7720             :                                                (BlockNumber) numDataPages,
    7721             :                                                numDataPages, root);
    7722           1 :         dataPagesFetched /= outer_scans;
    7723             :     }
    7724             : 
    7725             :     /*
    7726             :      * Here we use random page cost because logically-close pages could be far
    7727             :      * apart on disk.
    7728             :      */
    7729          82 :     *indexStartupCost = (entryPagesFetched + dataPagesFetched) * spc_random_page_cost;
    7730             : 
    7731             :     /*
    7732             :      * Now compute the number of data pages fetched during the scan.
    7733             :      *
    7734             :      * We assume every entry to have the same number of items, and that there
    7735             :      * is no overlap between them. (XXX: tsvector and array opclasses collect
    7736             :      * statistics on the frequency of individual keys; it would be nice to use
    7737             :      * those here.)
    7738             :      */
    7739          82 :     dataPagesFetched = ceil(numDataPages * counts.exactEntries / numEntries);
    7740             : 
    7741             :     /*
    7742             :      * If there is a lot of overlap among the entries, in particular if one of
    7743             :      * the entries is very frequent, the above calculation can grossly
    7744             :      * under-estimate.  As a simple cross-check, calculate a lower bound based
    7745             :      * on the overall selectivity of the quals.  At a minimum, we must read
    7746             :      * one item pointer for each matching entry.
    7747             :      *
    7748             :      * The width of each item pointer varies, based on the level of
    7749             :      * compression.  We don't have statistics on that, but an average of
    7750             :      * around 3 bytes per item is fairly typical.
    7751             :      */
    7752          82 :     dataPagesFetchedBySel = ceil(*indexSelectivity *
    7753             :                                  (numTuples / (BLCKSZ / 3)));
    7754          82 :     if (dataPagesFetchedBySel > dataPagesFetched)
    7755          79 :         dataPagesFetched = dataPagesFetchedBySel;
    7756             : 
    7757             :     /* Account for cache effects, the same as above */
    7758          82 :     if (outer_scans > 1 || counts.arrayScans > 1)
    7759             :     {
    7760           1 :         dataPagesFetched *= outer_scans * counts.arrayScans;
    7761           1 :         dataPagesFetched = index_pages_fetched(dataPagesFetched,
    7762             :                                                (BlockNumber) numDataPages,
    7763             :                                                numDataPages, root);
    7764           1 :         dataPagesFetched /= outer_scans;
    7765             :     }
    7766             : 
    7767             :     /* And apply random_page_cost as the cost per page */
    7768          82 :     *indexTotalCost = *indexStartupCost +
    7769             :         dataPagesFetched * spc_random_page_cost;
    7770             : 
    7771             :     /*
    7772             :      * Add on index qual eval costs, much as in genericcostestimate
    7773             :      */
    7774         164 :     qual_arg_cost = other_operands_eval_cost(root, qinfos) +
    7775          82 :         orderby_operands_eval_cost(root, path);
    7776         164 :     qual_op_cost = cpu_operator_cost *
    7777          82 :         (list_length(indexQuals) + list_length(indexOrderBys));
    7778             : 
    7779          82 :     *indexStartupCost += qual_arg_cost;
    7780          82 :     *indexTotalCost += qual_arg_cost;
    7781          82 :     *indexTotalCost += (numTuples * *indexSelectivity) * (cpu_index_tuple_cost + qual_op_cost);
    7782          82 :     *indexPages = dataPagesFetched;
    7783             : }
    7784             : 
    7785             : /*
    7786             :  * BRIN has search behavior completely different from other index types
    7787             :  */
    7788             : void
    7789         995 : brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
    7790             :                  Cost *indexStartupCost, Cost *indexTotalCost,
    7791             :                  Selectivity *indexSelectivity, double *indexCorrelation,
    7792             :                  double *indexPages)
    7793             : {
    7794         995 :     IndexOptInfo *index = path->indexinfo;
    7795         995 :     List       *indexQuals = path->indexquals;
    7796         995 :     double      numPages = index->pages;
    7797         995 :     RelOptInfo *baserel = index->rel;
    7798         995 :     RangeTblEntry *rte = planner_rt_fetch(baserel->relid, root);
    7799             :     List       *qinfos;
    7800             :     Cost        spc_seq_page_cost;
    7801             :     Cost        spc_random_page_cost;
    7802             :     double      qual_arg_cost;
    7803             :     double      qualSelectivity;
    7804             :     BrinStatsData statsData;
    7805             :     double      indexRanges;
    7806             :     double      minimalRanges;
    7807             :     double      estimatedRanges;
    7808             :     double      selec;
    7809             :     Relation    indexRel;
    7810             :     ListCell   *l;
    7811             :     VariableStatData vardata;
    7812             : 
    7813         995 :     Assert(rte->rtekind == RTE_RELATION);
    7814             : 
    7815             :     /* fetch estimated page cost for the tablespace containing the index */
    7816         995 :     get_tablespace_page_costs(index->reltablespace,
    7817             :                               &spc_random_page_cost,
    7818             :                               &spc_seq_page_cost);
    7819             : 
    7820             :     /*
    7821             :      * Obtain some data from the index itself.
    7822             :      */
    7823         995 :     indexRel = index_open(index->indexoid, AccessShareLock);
    7824         995 :     brinGetStats(indexRel, &statsData);
    7825         995 :     index_close(indexRel, AccessShareLock);
    7826             : 
    7827             :     /*
    7828             :      * Compute index correlation
    7829             :      *
    7830             :      * Because we can use all index quals equally when scanning, we can use
    7831             :      * the largest correlation (in absolute value) among columns used by the
    7832             :      * query.  Start at zero, the worst possible case.  If we cannot find any
    7833             :      * correlation statistics, we will keep it as 0.
    7834             :      */
    7835         995 :     *indexCorrelation = 0;
    7836             : 
    7837         995 :     qinfos = deconstruct_indexquals(path);
    7838        1990 :     foreach(l, qinfos)
    7839             :     {
    7840         995 :         IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(l);
    7841         995 :         AttrNumber  attnum = index->indexkeys[qinfo->indexcol];
    7842             : 
    7843             :         /* attempt to lookup stats in relation for this index column */
    7844         995 :         if (attnum != 0)
    7845             :         {
    7846             :             /* Simple variable -- look to stats for the underlying table */
    7847         995 :             if (get_relation_stats_hook &&
    7848           0 :                 (*get_relation_stats_hook) (root, rte, attnum, &vardata))
    7849             :             {
    7850             :                 /*
    7851             :                  * The hook took control of acquiring a stats tuple.  If it
    7852             :                  * did supply a tuple, it'd better have supplied a freefunc.
    7853             :                  */
    7854           0 :                 if (HeapTupleIsValid(vardata.statsTuple) && !vardata.freefunc)
    7855           0 :                     elog(ERROR,
    7856             :                          "no function provided to release variable stats with");
    7857             :             }
    7858             :             else
    7859             :             {
    7860         995 :                 vardata.statsTuple =
    7861         995 :                     SearchSysCache3(STATRELATTINH,
    7862             :                                     ObjectIdGetDatum(rte->relid),
    7863             :                                     Int16GetDatum(attnum),
    7864             :                                     BoolGetDatum(false));
    7865         995 :                 vardata.freefunc = ReleaseSysCache;
    7866             :             }
    7867             :         }
    7868             :         else
    7869             :         {
    7870             :             /*
    7871             :              * Looks like we've found an expression column in the index. Let's
    7872             :              * see if there's any stats for it.
    7873             :              */
    7874             : 
    7875             :             /* get the attnum from the 0-based index. */
    7876           0 :             attnum = qinfo->indexcol + 1;
    7877             : 
    7878           0 :             if (get_index_stats_hook &&
    7879           0 :                 (*get_index_stats_hook) (root, index->indexoid, attnum, &vardata))
    7880             :             {
    7881             :                 /*
    7882             :                  * The hook took control of acquiring a stats tuple.  If it
    7883             :                  * did supply a tuple, it'd better have supplied a freefunc.
    7884             :                  */
    7885           0 :                 if (HeapTupleIsValid(vardata.statsTuple) &&
    7886           0 :                     !vardata.freefunc)
    7887           0 :                     elog(ERROR, "no function provided to release variable stats with");
    7888             :             }
    7889             :             else
    7890             :             {
    7891           0 :                 vardata.statsTuple = SearchSysCache3(STATRELATTINH,
    7892             :                                                      ObjectIdGetDatum(index->indexoid),
    7893             :                                                      Int16GetDatum(attnum),
    7894             :                                                      BoolGetDatum(false));
    7895           0 :                 vardata.freefunc = ReleaseSysCache;
    7896             :             }
    7897             :         }
    7898             : 
    7899         995 :         if (HeapTupleIsValid(vardata.statsTuple))
    7900             :         {
    7901             :             AttStatsSlot sslot;
    7902             : 
    7903           2 :             if (get_attstatsslot(&sslot, vardata.statsTuple,
    7904             :                                  STATISTIC_KIND_CORRELATION, InvalidOid,
    7905             :                                  ATTSTATSSLOT_NUMBERS))
    7906             :             {
    7907           2 :                 double      varCorrelation = 0.0;
    7908             : 
    7909           2 :                 if (sslot.nnumbers > 0)
    7910           2 :                     varCorrelation = Abs(sslot.numbers[0]);
    7911             : 
    7912           2 :                 if (varCorrelation > *indexCorrelation)
    7913           2 :                     *indexCorrelation = varCorrelation;
    7914             : 
    7915           2 :                 free_attstatsslot(&sslot);
    7916             :             }
    7917             :         }
    7918             : 
    7919         995 :         ReleaseVariableStats(vardata);
    7920             :     }
    7921             : 
    7922         995 :     qualSelectivity = clauselist_selectivity(root, indexQuals,
    7923         995 :                                              baserel->relid,
    7924             :                                              JOIN_INNER, NULL);
    7925             : 
    7926             :     /* work out the actual number of ranges in the index */
    7927         995 :     indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange),
    7928             :                       1.0);
    7929             : 
    7930             :     /*
    7931             :      * Now calculate the minimum possible ranges we could match with if all of
    7932             :      * the rows were in the perfect order in the table's heap.
    7933             :      */
    7934         995 :     minimalRanges = ceil(indexRanges * qualSelectivity);
    7935             : 
    7936             :     /*
    7937             :      * Now estimate the number of ranges that we'll touch by using the
    7938             :      * indexCorrelation from the stats. Careful not to divide by zero (note
    7939             :      * we're using the absolute value of the correlation).
    7940             :      */
    7941         995 :     if (*indexCorrelation < 1.0e-10)
    7942         993 :         estimatedRanges = indexRanges;
    7943             :     else
    7944           2 :         estimatedRanges = Min(minimalRanges / *indexCorrelation, indexRanges);
    7945             : 
    7946             :     /* we expect to visit this portion of the table */
    7947         995 :     selec = estimatedRanges / indexRanges;
    7948             : 
    7949         995 :     CLAMP_PROBABILITY(selec);
    7950             : 
    7951         995 :     *indexSelectivity = selec;
    7952             : 
    7953             :     /*
    7954             :      * Compute the index qual costs, much as in genericcostestimate, to add to
    7955             :      * the index costs.
    7956             :      */
    7957        1990 :     qual_arg_cost = other_operands_eval_cost(root, qinfos) +
    7958         995 :         orderby_operands_eval_cost(root, path);
    7959             : 
    7960             :     /*
    7961             :      * Compute the startup cost as the cost to read the whole revmap
    7962             :      * sequentially, including the cost to execute the index quals.
    7963             :      */
    7964        1990 :     *indexStartupCost =
    7965         995 :         spc_seq_page_cost * statsData.revmapNumPages * loop_count;
    7966         995 :     *indexStartupCost += qual_arg_cost;
    7967             : 
    7968             :     /*
    7969             :      * To read a BRIN index there might be a bit of back and forth over
    7970             :      * regular pages, as revmap might point to them out of sequential order;
    7971             :      * calculate the total cost as reading the whole index in random order.
    7972             :      */
    7973        1990 :     *indexTotalCost = *indexStartupCost +
    7974         995 :         spc_random_page_cost * (numPages - statsData.revmapNumPages) * loop_count;
    7975             : 
    7976             :     /*
    7977             :      * Charge a small amount per range tuple which we expect to match to. This
    7978             :      * is meant to reflect the costs of manipulating the bitmap. The BRIN scan
    7979             :      * will set a bit for each page in the range when we find a matching
    7980             :      * range, so we must multiply the charge by the number of pages in the
    7981             :      * range.
    7982             :      */
    7983        1990 :     *indexTotalCost += 0.1 * cpu_operator_cost * estimatedRanges *
    7984         995 :         statsData.pagesPerRange;
    7985             : 
    7986         995 :     *indexPages = index->pages;
    7987         995 : }

Generated by: LCOV version 1.11