LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - selfuncs.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC EUB DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 89.0 % 2618 2329 7 282 23 139 2167 1 71
Current Date: 2026-05-05 10:23:31 +0900 Functions: 96.3 % 81 78 3 16 62 2
Baseline: lcov-20260505-025707-baseline Branches: 76.6 % 2037 1560 13 1 463 14 71 1475 36 6 36
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 2 2 2
(30,360] days: 94.9 % 235 223 7 5 139 84
(360..) days: 88.4 % 2381 2104 277 23 2081
Function coverage date bins:
(30,360] days: 100.0 % 9 9 5 4
(360..) days: 95.8 % 72 69 3 11 58
Branch coverage date bins:
(7,30] days: 100.0 % 4 4 4
(30,360] days: 84.0 % 150 126 13 11 71 55
(360..) days: 74.5 % 1919 1430 1 452 14 1416 36

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * selfuncs.c
                                  4                 :                :  *    Selectivity functions and index cost estimation functions for
                                  5                 :                :  *    standard operators and index access methods.
                                  6                 :                :  *
                                  7                 :                :  *    Selectivity routines are registered in the pg_operator catalog
                                  8                 :                :  *    in the "oprrest" and "oprjoin" attributes.
                                  9                 :                :  *
                                 10                 :                :  *    Index cost functions are located via the index AM's API struct,
                                 11                 :                :  *    which is obtained from the handler function registered in pg_am.
                                 12                 :                :  *
                                 13                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 14                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 15                 :                :  *
                                 16                 :                :  *
                                 17                 :                :  * IDENTIFICATION
                                 18                 :                :  *    src/backend/utils/adt/selfuncs.c
                                 19                 :                :  *
                                 20                 :                :  *-------------------------------------------------------------------------
                                 21                 :                :  */
                                 22                 :                : 
                                 23                 :                : /*----------
                                 24                 :                :  * Operator selectivity estimation functions are called to estimate the
                                 25                 :                :  * selectivity of WHERE clauses whose top-level operator is their operator.
                                 26                 :                :  * We divide the problem into two cases:
                                 27                 :                :  *      Restriction clause estimation: the clause involves vars of just
                                 28                 :                :  *          one relation.
                                 29                 :                :  *      Join clause estimation: the clause involves vars of multiple rels.
                                 30                 :                :  * Join selectivity estimation is far more difficult and usually less accurate
                                 31                 :                :  * than restriction estimation.
                                 32                 :                :  *
                                 33                 :                :  * When dealing with the inner scan of a nestloop join, we consider the
                                 34                 :                :  * join's joinclauses as restriction clauses for the inner relation, and
                                 35                 :                :  * treat vars of the outer relation as parameters (a/k/a constants of unknown
                                 36                 :                :  * values).  So, restriction estimators need to be able to accept an argument
                                 37                 :                :  * telling which relation is to be treated as the variable.
                                 38                 :                :  *
                                 39                 :                :  * The call convention for a restriction estimator (oprrest function) is
                                 40                 :                :  *
                                 41                 :                :  *      Selectivity oprrest (PlannerInfo *root,
                                 42                 :                :  *                           Oid operator,
                                 43                 :                :  *                           List *args,
                                 44                 :                :  *                           int varRelid);
                                 45                 :                :  *
                                 46                 :                :  * root: general information about the query (rtable and RelOptInfo lists
                                 47                 :                :  * are particularly important for the estimator).
                                 48                 :                :  * operator: OID of the specific operator in question.
                                 49                 :                :  * args: argument list from the operator clause.
                                 50                 :                :  * varRelid: if not zero, the relid (rtable index) of the relation to
                                 51                 :                :  * be treated as the variable relation.  May be zero if the args list
                                 52                 :                :  * is known to contain vars of only one relation.
                                 53                 :                :  *
                                 54                 :                :  * This is represented at the SQL level (in pg_proc) as
                                 55                 :                :  *
                                 56                 :                :  *      float8 oprrest (internal, oid, internal, int4);
                                 57                 :                :  *
                                 58                 :                :  * The result is a selectivity, that is, a fraction (0 to 1) of the rows
                                 59                 :                :  * of the relation that are expected to produce a TRUE result for the
                                 60                 :                :  * given operator.
                                 61                 :                :  *
                                 62                 :                :  * The call convention for a join estimator (oprjoin function) is similar
                                 63                 :                :  * except that varRelid is not needed, and instead join information is
                                 64                 :                :  * supplied:
                                 65                 :                :  *
                                 66                 :                :  *      Selectivity oprjoin (PlannerInfo *root,
                                 67                 :                :  *                           Oid operator,
                                 68                 :                :  *                           List *args,
                                 69                 :                :  *                           JoinType jointype,
                                 70                 :                :  *                           SpecialJoinInfo *sjinfo);
                                 71                 :                :  *
                                 72                 :                :  *      float8 oprjoin (internal, oid, internal, int2, internal);
                                 73                 :                :  *
                                 74                 :                :  * (Before Postgres 8.4, join estimators had only the first four of these
                                 75                 :                :  * parameters.  That signature is still allowed, but deprecated.)  The
                                 76                 :                :  * relationship between jointype and sjinfo is explained in the comments for
                                 77                 :                :  * clause_selectivity() --- the short version is that jointype is usually
                                 78                 :                :  * best ignored in favor of examining sjinfo.
                                 79                 :                :  *
                                 80                 :                :  * Join selectivity for regular inner and outer joins is defined as the
                                 81                 :                :  * fraction (0 to 1) of the cross product of the relations that is expected
                                 82                 :                :  * to produce a TRUE result for the given operator.  For both semi and anti
                                 83                 :                :  * joins, however, the selectivity is defined as the fraction of the left-hand
                                 84                 :                :  * side relation's rows that are expected to have a match (ie, at least one
                                 85                 :                :  * row with a TRUE result) in the right-hand side.
                                 86                 :                :  *
                                 87                 :                :  * For both oprrest and oprjoin functions, the operator's input collation OID
                                 88                 :                :  * (if any) is passed using the standard fmgr mechanism, so that the estimator
                                 89                 :                :  * function can fetch it with PG_GET_COLLATION().  Note, however, that all
                                 90                 :                :  * statistics in pg_statistic are currently built using the relevant column's
                                 91                 :                :  * collation.
                                 92                 :                :  *----------
                                 93                 :                :  */
                                 94                 :                : 
                                 95                 :                : #include "postgres.h"
                                 96                 :                : 
                                 97                 :                : #include <ctype.h>
                                 98                 :                : #include <math.h>
                                 99                 :                : 
                                100                 :                : #include "access/brin.h"
                                101                 :                : #include "access/brin_page.h"
                                102                 :                : #include "access/gin.h"
                                103                 :                : #include "access/table.h"
                                104                 :                : #include "access/tableam.h"
                                105                 :                : #include "access/visibilitymap.h"
                                106                 :                : #include "catalog/pg_collation.h"
                                107                 :                : #include "catalog/pg_operator.h"
                                108                 :                : #include "catalog/pg_statistic.h"
                                109                 :                : #include "catalog/pg_statistic_ext.h"
                                110                 :                : #include "executor/nodeAgg.h"
                                111                 :                : #include "miscadmin.h"
                                112                 :                : #include "nodes/makefuncs.h"
                                113                 :                : #include "nodes/nodeFuncs.h"
                                114                 :                : #include "optimizer/clauses.h"
                                115                 :                : #include "optimizer/cost.h"
                                116                 :                : #include "optimizer/optimizer.h"
                                117                 :                : #include "optimizer/pathnode.h"
                                118                 :                : #include "optimizer/paths.h"
                                119                 :                : #include "optimizer/plancat.h"
                                120                 :                : #include "parser/parse_clause.h"
                                121                 :                : #include "parser/parse_relation.h"
                                122                 :                : #include "parser/parsetree.h"
                                123                 :                : #include "rewrite/rewriteManip.h"
                                124                 :                : #include "statistics/statistics.h"
                                125                 :                : #include "storage/bufmgr.h"
                                126                 :                : #include "utils/acl.h"
                                127                 :                : #include "utils/array.h"
                                128                 :                : #include "utils/builtins.h"
                                129                 :                : #include "utils/date.h"
                                130                 :                : #include "utils/datum.h"
                                131                 :                : #include "utils/fmgroids.h"
                                132                 :                : #include "utils/index_selfuncs.h"
                                133                 :                : #include "utils/lsyscache.h"
                                134                 :                : #include "utils/memutils.h"
                                135                 :                : #include "utils/pg_locale.h"
                                136                 :                : #include "utils/rel.h"
                                137                 :                : #include "utils/selfuncs.h"
                                138                 :                : #include "utils/snapmgr.h"
                                139                 :                : #include "utils/spccache.h"
                                140                 :                : #include "utils/syscache.h"
                                141                 :                : #include "utils/timestamp.h"
                                142                 :                : #include "utils/typcache.h"
                                143                 :                : 
                                144                 :                : #define DEFAULT_PAGE_CPU_MULTIPLIER 50.0
                                145                 :                : 
                                146                 :                : /*
                                147                 :                :  * In production builds, switch to hash-based MCV matching when the lists are
                                148                 :                :  * large enough to amortize hash setup cost.  (This threshold is compared to
                                149                 :                :  * the sum of the lengths of the two MCV lists.  This is simplistic but seems
                                150                 :                :  * to work well enough.)  In debug builds, we use a smaller threshold so that
                                151                 :                :  * the regression tests cover both paths well.
                                152                 :                :  */
                                153                 :                : #ifndef USE_ASSERT_CHECKING
                                154                 :                : #define EQJOINSEL_MCV_HASH_THRESHOLD 200
                                155                 :                : #else
                                156                 :                : #define EQJOINSEL_MCV_HASH_THRESHOLD 20
                                157                 :                : #endif
                                158                 :                : 
                                159                 :                : /* Entries in the simplehash hash table used by eqjoinsel_find_matches */
                                160                 :                : typedef struct MCVHashEntry
                                161                 :                : {
                                162                 :                :     Datum       value;          /* the value represented by this entry */
                                163                 :                :     int         index;          /* its index in the relevant AttStatsSlot */
                                164                 :                :     uint32      hash;           /* hash code for the Datum */
                                165                 :                :     char        status;         /* status code used by simplehash.h */
                                166                 :                : } MCVHashEntry;
                                167                 :                : 
                                168                 :                : /* private_data for the simplehash hash table */
                                169                 :                : typedef struct MCVHashContext
                                170                 :                : {
                                171                 :                :     FunctionCallInfo equal_fcinfo;  /* the equality join operator */
                                172                 :                :     FunctionCallInfo hash_fcinfo;   /* the hash function to use */
                                173                 :                :     bool        op_is_reversed; /* equality compares hash type to probe type */
                                174                 :                :     bool        insert_mode;    /* doing inserts or lookups? */
                                175                 :                :     bool        hash_typbyval;  /* typbyval of hashed data type */
                                176                 :                :     int16       hash_typlen;    /* typlen of hashed data type */
                                177                 :                : } MCVHashContext;
                                178                 :                : 
                                179                 :                : /* forward reference */
                                180                 :                : typedef struct MCVHashTable_hash MCVHashTable_hash;
                                181                 :                : 
                                182                 :                : /* Hooks for plugins to get control when we ask for stats */
                                183                 :                : get_relation_stats_hook_type get_relation_stats_hook = NULL;
                                184                 :                : get_index_stats_hook_type get_index_stats_hook = NULL;
                                185                 :                : 
                                186                 :                : static double eqsel_internal(PG_FUNCTION_ARGS, bool negate);
                                187                 :                : static double eqjoinsel_inner(FmgrInfo *eqproc, Oid collation,
                                188                 :                :                               Oid hashLeft, Oid hashRight,
                                189                 :                :                               VariableStatData *vardata1, VariableStatData *vardata2,
                                190                 :                :                               double nd1, double nd2,
                                191                 :                :                               bool isdefault1, bool isdefault2,
                                192                 :                :                               AttStatsSlot *sslot1, AttStatsSlot *sslot2,
                                193                 :                :                               Form_pg_statistic stats1, Form_pg_statistic stats2,
                                194                 :                :                               bool have_mcvs1, bool have_mcvs2,
                                195                 :                :                               bool *hasmatch1, bool *hasmatch2,
                                196                 :                :                               int *p_nmatches);
                                197                 :                : static double eqjoinsel_semi(FmgrInfo *eqproc, Oid collation,
                                198                 :                :                              Oid hashLeft, Oid hashRight,
                                199                 :                :                              bool op_is_reversed,
                                200                 :                :                              VariableStatData *vardata1, VariableStatData *vardata2,
                                201                 :                :                              double nd1, double nd2,
                                202                 :                :                              bool isdefault1, bool isdefault2,
                                203                 :                :                              AttStatsSlot *sslot1, AttStatsSlot *sslot2,
                                204                 :                :                              Form_pg_statistic stats1, Form_pg_statistic stats2,
                                205                 :                :                              bool have_mcvs1, bool have_mcvs2,
                                206                 :                :                              bool *hasmatch1, bool *hasmatch2,
                                207                 :                :                              int *p_nmatches,
                                208                 :                :                              RelOptInfo *inner_rel);
                                209                 :                : static void eqjoinsel_find_matches(FmgrInfo *eqproc, Oid collation,
                                210                 :                :                                    Oid hashLeft, Oid hashRight,
                                211                 :                :                                    bool op_is_reversed,
                                212                 :                :                                    AttStatsSlot *sslot1, AttStatsSlot *sslot2,
                                213                 :                :                                    int nvalues1, int nvalues2,
                                214                 :                :                                    bool *hasmatch1, bool *hasmatch2,
                                215                 :                :                                    int *p_nmatches, double *p_matchprodfreq);
                                216                 :                : static uint32 hash_mcv(MCVHashTable_hash *tab, Datum key);
                                217                 :                : static bool mcvs_equal(MCVHashTable_hash *tab, Datum key0, Datum key1);
                                218                 :                : static bool estimate_multivariate_ndistinct(PlannerInfo *root,
                                219                 :                :                                             RelOptInfo *rel, List **varinfos, double *ndistinct);
                                220                 :                : static bool convert_to_scalar(Datum value, Oid valuetypid, Oid collid,
                                221                 :                :                               double *scaledvalue,
                                222                 :                :                               Datum lobound, Datum hibound, Oid boundstypid,
                                223                 :                :                               double *scaledlobound, double *scaledhibound);
                                224                 :                : static double convert_numeric_to_scalar(Datum value, Oid typid, bool *failure);
                                225                 :                : static void convert_string_to_scalar(char *value,
                                226                 :                :                                      double *scaledvalue,
                                227                 :                :                                      char *lobound,
                                228                 :                :                                      double *scaledlobound,
                                229                 :                :                                      char *hibound,
                                230                 :                :                                      double *scaledhibound);
                                231                 :                : static void convert_bytea_to_scalar(Datum value,
                                232                 :                :                                     double *scaledvalue,
                                233                 :                :                                     Datum lobound,
                                234                 :                :                                     double *scaledlobound,
                                235                 :                :                                     Datum hibound,
                                236                 :                :                                     double *scaledhibound);
                                237                 :                : static double convert_one_string_to_scalar(char *value,
                                238                 :                :                                            int rangelo, int rangehi);
                                239                 :                : static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
                                240                 :                :                                           int rangelo, int rangehi);
                                241                 :                : static char *convert_string_datum(Datum value, Oid typid, Oid collid,
                                242                 :                :                                   bool *failure);
                                243                 :                : static double convert_timevalue_to_scalar(Datum value, Oid typid,
                                244                 :                :                                           bool *failure);
                                245                 :                : static Node *strip_all_phvs_deep(PlannerInfo *root, Node *node);
                                246                 :                : static bool contain_placeholder_walker(Node *node, void *context);
                                247                 :                : static Node *strip_all_phvs_mutator(Node *node, void *context);
                                248                 :                : static void examine_simple_variable(PlannerInfo *root, Var *var,
                                249                 :                :                                     VariableStatData *vardata);
                                250                 :                : static void examine_indexcol_variable(PlannerInfo *root, IndexOptInfo *index,
                                251                 :                :                                       int indexcol, VariableStatData *vardata);
                                252                 :                : static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata,
                                253                 :                :                                Oid sortop, Oid collation,
                                254                 :                :                                Datum *min, Datum *max);
                                255                 :                : static void get_stats_slot_range(AttStatsSlot *sslot,
                                256                 :                :                                  Oid opfuncoid, FmgrInfo *opproc,
                                257                 :                :                                  Oid collation, int16 typLen, bool typByVal,
                                258                 :                :                                  Datum *min, Datum *max, bool *p_have_data);
                                259                 :                : static bool get_actual_variable_range(PlannerInfo *root,
                                260                 :                :                                       VariableStatData *vardata,
                                261                 :                :                                       Oid sortop, Oid collation,
                                262                 :                :                                       Datum *min, Datum *max);
                                263                 :                : static bool get_actual_variable_endpoint(Relation heapRel,
                                264                 :                :                                          Relation indexRel,
                                265                 :                :                                          ScanDirection indexscandir,
                                266                 :                :                                          ScanKey scankeys,
                                267                 :                :                                          int16 typLen,
                                268                 :                :                                          bool typByVal,
                                269                 :                :                                          TupleTableSlot *tableslot,
                                270                 :                :                                          MemoryContext outercontext,
                                271                 :                :                                          Datum *endpointDatum);
                                272                 :                : static RelOptInfo *find_join_input_rel(PlannerInfo *root, Relids relids);
                                273                 :                : static double btcost_correlation(IndexOptInfo *index,
                                274                 :                :                                  VariableStatData *vardata);
                                275                 :                : 
                                276                 :                : /* Define support routines for MCV hash tables */
                                277                 :                : #define SH_PREFIX               MCVHashTable
                                278                 :                : #define SH_ELEMENT_TYPE         MCVHashEntry
                                279                 :                : #define SH_KEY_TYPE             Datum
                                280                 :                : #define SH_KEY                  value
                                281                 :                : #define SH_HASH_KEY(tab,key)    hash_mcv(tab, key)
                                282                 :                : #define SH_EQUAL(tab,key0,key1) mcvs_equal(tab, key0, key1)
                                283                 :                : #define SH_SCOPE                static inline
                                284                 :                : #define SH_STORE_HASH
                                285                 :                : #define SH_GET_HASH(tab,ent)    (ent)->hash
                                286                 :                : #define SH_DEFINE
                                287                 :                : #define SH_DECLARE
                                288                 :                : #include "lib/simplehash.h"
                                289                 :                : 
                                290                 :                : 
                                291                 :                : /*
                                292                 :                :  *      eqsel           - Selectivity of "=" for any data types.
                                293                 :                :  *
                                294                 :                :  * Note: this routine is also used to estimate selectivity for some
                                295                 :                :  * operators that are not "=" but have comparable selectivity behavior,
                                296                 :                :  * such as "~=" (geometric approximate-match).  Even for "=", we must
                                297                 :                :  * keep in mind that the left and right datatypes may differ.
                                298                 :                :  */
                                299                 :                : Datum
 9465 tgl@sss.pgh.pa.us         300                 :CBC      563147 : eqsel(PG_FUNCTION_ARGS)
                                301                 :                : {
 3258                           302                 :         563147 :     PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, false));
                                303                 :                : }
                                304                 :                : 
                                305                 :                : /*
                                306                 :                :  * Common code for eqsel() and neqsel()
                                307                 :                :  */
                                308                 :                : static double
                                309                 :         595595 : eqsel_internal(PG_FUNCTION_ARGS, bool negate)
                                310                 :                : {
 7639                           311                 :         595595 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
 9116                           312                 :         595595 :     Oid         operator = PG_GETARG_OID(1);
                                313                 :         595595 :     List       *args = (List *) PG_GETARG_POINTER(2);
                                314                 :         595595 :     int         varRelid = PG_GETARG_INT32(3);
 2160                           315                 :         595595 :     Oid         collation = PG_GET_COLLATION();
                                316                 :                :     VariableStatData vardata;
                                317                 :                :     Node       *other;
                                318                 :                :     bool        varonleft;
                                319                 :                :     double      selec;
                                320                 :                : 
                                321                 :                :     /*
                                322                 :                :      * When asked about <>, we do the estimation using the corresponding =
                                323                 :                :      * operator, then convert to <> via "1.0 - eq_selectivity - nullfrac".
                                324                 :                :      */
 3258                           325         [ +  + ]:         595595 :     if (negate)
                                326                 :                :     {
                                327                 :          32448 :         operator = get_negator(operator);
                                328         [ -  + ]:          32448 :         if (!OidIsValid(operator))
                                329                 :                :         {
                                330                 :                :             /* Use default selectivity (should we raise an error instead?) */
 3258 tgl@sss.pgh.pa.us         331                 :UBC           0 :             return 1.0 - DEFAULT_EQ_SEL;
                                332                 :                :         }
                                333                 :                :     }
                                334                 :                : 
                                335                 :                :     /*
                                336                 :                :      * If expression is not variable = something or something = variable, then
                                337                 :                :      * punt and return a default estimate.
                                338                 :                :      */
 8113 tgl@sss.pgh.pa.us         339         [ +  + ]:CBC      595595 :     if (!get_restriction_variable(root, args, varRelid,
                                340                 :                :                                   &vardata, &other, &varonleft))
 3258                           341         [ +  + ]:           3626 :         return negate ? (1.0 - DEFAULT_EQ_SEL) : DEFAULT_EQ_SEL;
                                342                 :                : 
                                343                 :                :     /*
                                344                 :                :      * We can do a lot better if the something is a constant.  (Note: the
                                345                 :                :      * Const might result from estimation rather than being a simple constant
                                346                 :                :      * in the query.)
                                347                 :                :      */
 6632                           348         [ +  + ]:         591965 :     if (IsA(other, Const))
 2160                           349                 :         224405 :         selec = var_eq_const(&vardata, operator, collation,
 6632                           350                 :         224405 :                              ((Const *) other)->constvalue,
                                351                 :         224405 :                              ((Const *) other)->constisnull,
                                352                 :                :                              varonleft, negate);
                                353                 :                :     else
 2160                           354                 :         367560 :         selec = var_eq_non_const(&vardata, operator, collation, other,
                                355                 :                :                                  varonleft, negate);
                                356                 :                : 
 6632                           357         [ +  + ]:         591965 :     ReleaseVariableStats(vardata);
                                358                 :                : 
 3258                           359                 :         591965 :     return selec;
                                360                 :                : }
                                361                 :                : 
                                362                 :                : /*
                                363                 :                :  * var_eq_const --- eqsel for var = const case
                                364                 :                :  *
                                365                 :                :  * This is exported so that some other estimation functions can use it.
                                366                 :                :  */
                                367                 :                : double
 1323 pg@bowt.ie                368                 :         255523 : var_eq_const(VariableStatData *vardata, Oid oproid, Oid collation,
                                369                 :                :              Datum constval, bool constisnull,
                                370                 :                :              bool varonleft, bool negate)
                                371                 :                : {
                                372                 :                :     double      selec;
 3258 tgl@sss.pgh.pa.us         373                 :         255523 :     double      nullfrac = 0.0;
                                374                 :                :     bool        isdefault;
                                375                 :                :     Oid         opfuncoid;
                                376                 :                : 
                                377                 :                :     /*
                                378                 :                :      * If the constant is NULL, assume operator is strict and return zero, ie,
                                379                 :                :      * operator will never return TRUE.  (It's zero even for a negator op.)
                                380                 :                :      */
 6632                           381         [ +  + ]:         255523 :     if (constisnull)
                                382                 :            270 :         return 0.0;
                                383                 :                : 
                                384                 :                :     /*
                                385                 :                :      * Grab the nullfrac for use below.  Note we allow use of nullfrac
                                386                 :                :      * regardless of security check.
                                387                 :                :      */
 3258                           388         [ +  + ]:         255253 :     if (HeapTupleIsValid(vardata->statsTuple))
                                389                 :                :     {
                                390                 :                :         Form_pg_statistic stats;
                                391                 :                : 
                                392                 :         189036 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
                                393                 :         189036 :         nullfrac = stats->stanullfrac;
                                394                 :                :     }
                                395                 :                : 
                                396                 :                :     /*
                                397                 :                :      * If we matched the var to a unique index, DISTINCT or GROUP-BY clause,
                                398                 :                :      * assume there is exactly one match regardless of anything else.  (This
                                399                 :                :      * is slightly bogus, since the index or clause's equality operator might
                                400                 :                :      * be different from ours, but it's much more likely to be right than
                                401                 :                :      * ignoring the information.)
                                402                 :                :      */
 6288                           403   [ +  +  +  -  :         255253 :     if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
                                              +  + ]
                                404                 :                :     {
 3258                           405                 :          52471 :         selec = 1.0 / vardata->rel->tuples;
                                406                 :                :     }
                                407   [ +  +  +  - ]:         348301 :     else if (HeapTupleIsValid(vardata->statsTuple) &&
                                408                 :         145519 :              statistic_proc_security_check(vardata,
 1323 pg@bowt.ie                409                 :         145519 :                                            (opfuncoid = get_opcode(oproid))))
 9774 tgl@sss.pgh.pa.us         410                 :         145519 :     {
                                411                 :                :         AttStatsSlot sslot;
 6632                           412                 :         145519 :         bool        match = false;
                                413                 :                :         int         i;
                                414                 :                : 
                                415                 :                :         /*
                                416                 :                :          * Is the constant "=" to any of the column's most common values?
                                417                 :                :          * (Although the given operator may not really be "=", we will assume
                                418                 :                :          * that seeing whether it returns TRUE is an appropriate test.  If you
                                419                 :                :          * don't like this, maybe you shouldn't be using eqsel for your
                                420                 :                :          * operator...)
                                421                 :                :          */
 3279                           422         [ +  + ]:         145519 :         if (get_attstatsslot(&sslot, vardata->statsTuple,
                                423                 :                :                              STATISTIC_KIND_MCV, InvalidOid,
                                424                 :                :                              ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
                                425                 :                :         {
 2205                           426                 :         134321 :             LOCAL_FCINFO(fcinfo, 2);
                                427                 :                :             FmgrInfo    eqproc;
                                428                 :                : 
 3287 peter_e@gmx.net           429                 :         134321 :             fmgr_info(opfuncoid, &eqproc);
                                430                 :                : 
                                431                 :                :             /*
                                432                 :                :              * Save a few cycles by setting up the fcinfo struct just once.
                                433                 :                :              * Using FunctionCallInvoke directly also avoids failure if the
                                434                 :                :              * eqproc returns NULL, though really equality functions should
                                435                 :                :              * never do that.
                                436                 :                :              */
 2160 tgl@sss.pgh.pa.us         437                 :         134321 :             InitFunctionCallInfoData(*fcinfo, &eqproc, 2, collation,
                                438                 :                :                                      NULL, NULL);
 2205                           439                 :         134321 :             fcinfo->args[0].isnull = false;
                                440                 :         134321 :             fcinfo->args[1].isnull = false;
                                441                 :                :             /* be careful to apply operator right way 'round */
                                442         [ +  + ]:         134321 :             if (varonleft)
                                443                 :         134294 :                 fcinfo->args[1].value = constval;
                                444                 :                :             else
                                445                 :             27 :                 fcinfo->args[0].value = constval;
                                446                 :                : 
 3279                           447         [ +  + ]:        2339349 :             for (i = 0; i < sslot.nvalues; i++)
                                448                 :                :             {
                                449                 :                :                 Datum       fresult;
                                450                 :                : 
 6632                           451         [ +  + ]:        2276146 :                 if (varonleft)
 2205                           452                 :        2276075 :                     fcinfo->args[0].value = sslot.values[i];
                                453                 :                :                 else
                                454                 :             71 :                     fcinfo->args[1].value = sslot.values[i];
                                455                 :        2276146 :                 fcinfo->isnull = false;
                                456                 :        2276146 :                 fresult = FunctionCallInvoke(fcinfo);
                                457   [ +  -  +  + ]:        2276146 :                 if (!fcinfo->isnull && DatumGetBool(fresult))
                                458                 :                :                 {
                                459                 :          71118 :                     match = true;
 6632                           460                 :          71118 :                     break;
                                461                 :                :                 }
                                462                 :                :             }
                                463                 :                :         }
                                464                 :                :         else
                                465                 :                :         {
                                466                 :                :             /* no most-common-value info available */
 3279                           467                 :          11198 :             i = 0;              /* keep compiler quiet */
                                468                 :                :         }
                                469                 :                : 
 6632                           470         [ +  + ]:         145519 :         if (match)
                                471                 :                :         {
                                472                 :                :             /*
                                473                 :                :              * Constant is "=" to this common value.  We know selectivity
                                474                 :                :              * exactly (or as exactly as ANALYZE could calculate it, anyway).
                                475                 :                :              */
 3279                           476                 :          71118 :             selec = sslot.numbers[i];
                                477                 :                :         }
                                478                 :                :         else
                                479                 :                :         {
                                480                 :                :             /*
                                481                 :                :              * Comparison is against a constant that is neither NULL nor any
                                482                 :                :              * of the common values.  Its selectivity cannot be more than
                                483                 :                :              * this:
                                484                 :                :              */
 6632                           485                 :          74401 :             double      sumcommon = 0.0;
                                486                 :                :             double      otherdistinct;
                                487                 :                : 
 3279                           488         [ +  + ]:        1987733 :             for (i = 0; i < sslot.nnumbers; i++)
                                489                 :        1913332 :                 sumcommon += sslot.numbers[i];
 3258                           490                 :          74401 :             selec = 1.0 - sumcommon - nullfrac;
 6632                           491   [ +  +  -  + ]:          74401 :             CLAMP_PROBABILITY(selec);
                                492                 :                : 
                                493                 :                :             /*
                                494                 :                :              * and in fact it's probably a good deal less. We approximate that
                                495                 :                :              * all the not-common values share this remaining fraction
                                496                 :                :              * equally, so we divide by the number of other distinct values.
                                497                 :                :              */
 3279                           498                 :          74401 :             otherdistinct = get_variable_numdistinct(vardata, &isdefault) -
                                499                 :          74401 :                 sslot.nnumbers;
 6632                           500         [ +  + ]:          74401 :             if (otherdistinct > 1)
                                501                 :          36663 :                 selec /= otherdistinct;
                                502                 :                : 
                                503                 :                :             /*
                                504                 :                :              * Another cross-check: selectivity shouldn't be estimated as more
                                505                 :                :              * than the least common "most common value".
                                506                 :                :              */
 3279                           507   [ +  +  -  + ]:          74401 :             if (sslot.nnumbers > 0 && selec > sslot.numbers[sslot.nnumbers - 1])
 3279 tgl@sss.pgh.pa.us         508                 :UBC           0 :                 selec = sslot.numbers[sslot.nnumbers - 1];
                                509                 :                :         }
                                510                 :                : 
 3279 tgl@sss.pgh.pa.us         511                 :CBC      145519 :         free_attstatsslot(&sslot);
                                512                 :                :     }
                                513                 :                :     else
                                514                 :                :     {
                                515                 :                :         /*
                                516                 :                :          * No ANALYZE stats available, so make a guess using estimated number
                                517                 :                :          * of distinct values and assuming they are equally common. (The guess
                                518                 :                :          * is unlikely to be very good, but we do know a few special cases.)
                                519                 :                :          */
 5357                           520                 :          57263 :         selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
                                521                 :                :     }
                                522                 :                : 
                                523                 :                :     /* now adjust if we wanted <> rather than = */
 3258                           524         [ +  + ]:         255253 :     if (negate)
                                525                 :          25695 :         selec = 1.0 - selec - nullfrac;
                                526                 :                : 
                                527                 :                :     /* result should be in range, but make sure... */
 6632                           528   [ -  +  -  + ]:         255253 :     CLAMP_PROBABILITY(selec);
                                529                 :                : 
                                530                 :         255253 :     return selec;
                                531                 :                : }
                                532                 :                : 
                                533                 :                : /*
                                534                 :                :  * var_eq_non_const --- eqsel for var = something-other-than-const case
                                535                 :                :  *
                                536                 :                :  * This is exported so that some other estimation functions can use it.
                                537                 :                :  */
                                538                 :                : double
 1323 pg@bowt.ie                539                 :         367560 : var_eq_non_const(VariableStatData *vardata, Oid oproid, Oid collation,
                                540                 :                :                  Node *other,
                                541                 :                :                  bool varonleft, bool negate)
                                542                 :                : {
                                543                 :                :     double      selec;
 3258 tgl@sss.pgh.pa.us         544                 :         367560 :     double      nullfrac = 0.0;
                                545                 :                :     bool        isdefault;
                                546                 :                : 
                                547                 :                :     /*
                                548                 :                :      * Grab the nullfrac for use below.
                                549                 :                :      */
                                550         [ +  + ]:         367560 :     if (HeapTupleIsValid(vardata->statsTuple))
                                551                 :                :     {
                                552                 :                :         Form_pg_statistic stats;
                                553                 :                : 
                                554                 :         242335 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
                                555                 :         242335 :         nullfrac = stats->stanullfrac;
                                556                 :                :     }
                                557                 :                : 
                                558                 :                :     /*
                                559                 :                :      * If we matched the var to a unique index, DISTINCT or GROUP-BY clause,
                                560                 :                :      * assume there is exactly one match regardless of anything else.  (This
                                561                 :                :      * is slightly bogus, since the index or clause's equality operator might
                                562                 :                :      * be different from ours, but it's much more likely to be right than
                                563                 :                :      * ignoring the information.)
                                564                 :                :      */
 6288                           565   [ +  +  +  -  :         367560 :     if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
                                              +  + ]
                                566                 :                :     {
 3258                           567                 :         129629 :         selec = 1.0 / vardata->rel->tuples;
                                568                 :                :     }
                                569         [ +  + ]:         237931 :     else if (HeapTupleIsValid(vardata->statsTuple))
                                570                 :                :     {
                                571                 :                :         double      ndistinct;
                                572                 :                :         AttStatsSlot sslot;
                                573                 :                : 
                                574                 :                :         /*
                                575                 :                :          * Search is for a value that we do not know a priori, but we will
                                576                 :                :          * assume it is not NULL.  Estimate the selectivity as non-null
                                577                 :                :          * fraction divided by number of distinct values, so that we get a
                                578                 :                :          * result averaged over all possible values whether common or
                                579                 :                :          * uncommon.  (Essentially, we are assuming that the not-yet-known
                                580                 :                :          * comparison value is equally likely to be any of the possible
                                581                 :                :          * values, regardless of their frequency in the table.  Is that a good
                                582                 :                :          * idea?)
                                583                 :                :          */
                                584                 :         130518 :         selec = 1.0 - nullfrac;
 5357                           585                 :         130518 :         ndistinct = get_variable_numdistinct(vardata, &isdefault);
 6632                           586         [ +  + ]:         130518 :         if (ndistinct > 1)
                                587                 :         127906 :             selec /= ndistinct;
                                588                 :                : 
                                589                 :                :         /*
                                590                 :                :          * Cross-check: selectivity should never be estimated as more than the
                                591                 :                :          * most common value's.
                                592                 :                :          */
 3279                           593         [ +  + ]:         130518 :         if (get_attstatsslot(&sslot, vardata->statsTuple,
                                594                 :                :                              STATISTIC_KIND_MCV, InvalidOid,
                                595                 :                :                              ATTSTATSSLOT_NUMBERS))
                                596                 :                :         {
                                597   [ +  -  +  + ]:         114707 :             if (sslot.nnumbers > 0 && selec > sslot.numbers[0])
                                598                 :            483 :                 selec = sslot.numbers[0];
                                599                 :         114707 :             free_attstatsslot(&sslot);
                                600                 :                :         }
                                601                 :                :     }
                                602                 :                :     else
                                603                 :                :     {
                                604                 :                :         /*
                                605                 :                :          * No ANALYZE stats available, so make a guess using estimated number
                                606                 :                :          * of distinct values and assuming they are equally common. (The guess
                                607                 :                :          * is unlikely to be very good, but we do know a few special cases.)
                                608                 :                :          */
 5357                           609                 :         107413 :         selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
                                610                 :                :     }
                                611                 :                : 
                                612                 :                :     /* now adjust if we wanted <> rather than = */
 3258                           613         [ +  + ]:         367560 :     if (negate)
                                614                 :           5160 :         selec = 1.0 - selec - nullfrac;
                                615                 :                : 
                                616                 :                :     /* result should be in range, but make sure... */
 8888                           617   [ -  +  -  + ]:         367560 :     CLAMP_PROBABILITY(selec);
                                618                 :                : 
 6632                           619                 :         367560 :     return selec;
                                620                 :                : }
                                621                 :                : 
                                622                 :                : /*
                                623                 :                :  *      neqsel          - Selectivity of "!=" for any data types.
                                624                 :                :  *
                                625                 :                :  * This routine is also used for some operators that are not "!="
                                626                 :                :  * but have comparable selectivity behavior.  See above comments
                                627                 :                :  * for eqsel().
                                628                 :                :  */
                                629                 :                : Datum
 9465                           630                 :          32448 : neqsel(PG_FUNCTION_ARGS)
                                631                 :                : {
 3258                           632                 :          32448 :     PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, true));
                                633                 :                : }
                                634                 :                : 
                                635                 :                : /*
                                636                 :                :  *  scalarineqsel       - Selectivity of "<", "<=", ">", ">=" for scalars.
                                637                 :                :  *
                                638                 :                :  * This is the guts of scalarltsel/scalarlesel/scalargtsel/scalargesel.
                                639                 :                :  * The isgt and iseq flags distinguish which of the four cases apply.
                                640                 :                :  *
                                641                 :                :  * The caller has commuted the clause, if necessary, so that we can treat
                                642                 :                :  * the variable as being on the left.  The caller must also make sure that
                                643                 :                :  * the other side of the clause is a non-null Const, and dissect that into
                                644                 :                :  * a value and datatype.  (This definition simplifies some callers that
                                645                 :                :  * want to estimate against a computed value instead of a Const node.)
                                646                 :                :  *
                                647                 :                :  * This routine works for any datatype (or pair of datatypes) known to
                                648                 :                :  * convert_to_scalar().  If it is applied to some other datatype,
                                649                 :                :  * it will return an approximate estimate based on assuming that the constant
                                650                 :                :  * value falls in the middle of the bin identified by binary search.
                                651                 :                :  */
                                652                 :                : static double
 3156                           653                 :         252684 : scalarineqsel(PlannerInfo *root, Oid operator, bool isgt, bool iseq,
                                654                 :                :               Oid collation,
                                655                 :                :               VariableStatData *vardata, Datum constval, Oid consttype)
                                656                 :                : {
                                657                 :                :     Form_pg_statistic stats;
                                658                 :                :     FmgrInfo    opproc;
                                659                 :                :     double      mcv_selec,
                                660                 :                :                 hist_selec,
                                661                 :                :                 sumcommon;
                                662                 :                :     double      selec;
                                663                 :                : 
 8113                           664         [ +  + ]:         252684 :     if (!HeapTupleIsValid(vardata->statsTuple))
                                665                 :                :     {
                                666                 :                :         /*
                                667                 :                :          * No stats are available.  Typically this means we have to fall back
                                668                 :                :          * on the default estimate; but if the variable is CTID then we can
                                669                 :                :          * make an estimate based on comparing the constant to the table size.
                                670                 :                :          */
 2598                           671   [ +  -  +  + ]:          21439 :         if (vardata->var && IsA(vardata->var, Var) &&
                                672         [ +  + ]:          17403 :             ((Var *) vardata->var)->varattno == SelfItemPointerAttributeNumber)
                                673                 :                :         {
                                674                 :                :             ItemPointer itemptr;
                                675                 :                :             double      block;
                                676                 :                :             double      density;
                                677                 :                : 
                                678                 :                :             /*
                                679                 :                :              * If the relation's empty, we're going to include all of it.
                                680                 :                :              * (This is mostly to avoid divide-by-zero below.)
                                681                 :                :              */
                                682         [ -  + ]:           1680 :             if (vardata->rel->pages == 0)
 2598 tgl@sss.pgh.pa.us         683                 :UBC           0 :                 return 1.0;
                                684                 :                : 
 2598 tgl@sss.pgh.pa.us         685                 :CBC        1680 :             itemptr = (ItemPointer) DatumGetPointer(constval);
                                686                 :           1680 :             block = ItemPointerGetBlockNumberNoCheck(itemptr);
                                687                 :                : 
                                688                 :                :             /*
                                689                 :                :              * Determine the average number of tuples per page (density).
                                690                 :                :              *
                                691                 :                :              * Since the last page will, on average, be only half full, we can
                                692                 :                :              * estimate it to have half as many tuples as earlier pages.  So
                                693                 :                :              * give it half the weight of a regular page.
                                694                 :                :              */
                                695                 :           1680 :             density = vardata->rel->tuples / (vardata->rel->pages - 0.5);
                                696                 :                : 
                                697                 :                :             /* If target is the last page, use half the density. */
                                698         [ +  + ]:           1680 :             if (block >= vardata->rel->pages - 1)
                                699                 :             25 :                 density *= 0.5;
                                700                 :                : 
                                701                 :                :             /*
                                702                 :                :              * Using the average tuples per page, calculate how far into the
                                703                 :                :              * page the itemptr is likely to be and adjust block accordingly,
                                704                 :                :              * by adding that fraction of a whole block (but never more than a
                                705                 :                :              * whole block, no matter how high the itemptr's offset is).  Here
                                706                 :                :              * we are ignoring the possibility of dead-tuple line pointers,
                                707                 :                :              * which is fairly bogus, but we lack the info to do better.
                                708                 :                :              */
                                709         [ +  - ]:           1680 :             if (density > 0.0)
                                710                 :                :             {
                                711                 :           1680 :                 OffsetNumber offset = ItemPointerGetOffsetNumberNoCheck(itemptr);
                                712                 :                : 
                                713         [ +  + ]:           1680 :                 block += Min(offset / density, 1.0);
                                714                 :                :             }
                                715                 :                : 
                                716                 :                :             /*
                                717                 :                :              * Convert relative block number to selectivity.  Again, the last
                                718                 :                :              * page has only half weight.
                                719                 :                :              */
                                720                 :           1680 :             selec = block / (vardata->rel->pages - 0.5);
                                721                 :                : 
                                722                 :                :             /*
                                723                 :                :              * The calculation so far gave us a selectivity for the "<=" case.
                                724                 :                :              * We'll have one fewer tuple for "<" and one additional tuple for
                                725                 :                :              * ">=", the latter of which we'll reverse the selectivity for
                                726                 :                :              * below, so we can simply subtract one tuple for both cases.  The
                                727                 :                :              * cases that need this adjustment can be identified by iseq being
                                728                 :                :              * equal to isgt.
                                729                 :                :              */
                                730   [ +  +  +  - ]:           1680 :             if (iseq == isgt && vardata->rel->tuples >= 1.0)
                                731                 :           1563 :                 selec -= (1.0 / vardata->rel->tuples);
                                732                 :                : 
                                733                 :                :             /* Finally, reverse the selectivity for the ">", ">=" cases. */
                                734         [ +  + ]:           1680 :             if (isgt)
                                735                 :           1547 :                 selec = 1.0 - selec;
                                736                 :                : 
                                737   [ +  +  -  + ]:           1680 :             CLAMP_PROBABILITY(selec);
                                738                 :           1680 :             return selec;
                                739                 :                :         }
                                740                 :                : 
                                741                 :                :         /* no stats available, so default result */
 9116                           742                 :          19759 :         return DEFAULT_INEQ_SEL;
                                743                 :                :     }
 8113                           744                 :         231245 :     stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
                                745                 :                : 
 9116                           746                 :         231245 :     fmgr_info(get_opcode(operator), &opproc);
                                747                 :                : 
                                748                 :                :     /*
                                749                 :                :      * If we have most-common-values info, add up the fractions of the MCV
                                750                 :                :      * entries that satisfy MCV OP CONST.  These fractions contribute directly
                                751                 :                :      * to the result selectivity.  Also add up the total fraction represented
                                752                 :                :      * by MCV entries.
                                753                 :                :      */
 2160                           754                 :         231245 :     mcv_selec = mcv_selectivity(vardata, &opproc, collation, constval, true,
                                755                 :                :                                 &sumcommon);
                                756                 :                : 
                                757                 :                :     /*
                                758                 :                :      * If there is a histogram, determine which bin the constant falls in, and
                                759                 :                :      * compute the resulting contribution to selectivity.
                                760                 :                :      */
 3156                           761                 :         231245 :     hist_selec = ineq_histogram_selectivity(root, vardata,
                                762                 :                :                                             operator, &opproc, isgt, iseq,
                                763                 :                :                                             collation,
                                764                 :                :                                             constval, consttype);
                                765                 :                : 
                                766                 :                :     /*
                                767                 :                :      * Now merge the results from the MCV and histogram calculations,
                                768                 :                :      * realizing that the histogram covers only the non-null values that are
                                769                 :                :      * not listed in MCV.
                                770                 :                :      */
 7420                           771                 :         231245 :     selec = 1.0 - stats->stanullfrac - sumcommon;
                                772                 :                : 
 5965                           773         [ +  + ]:         231245 :     if (hist_selec >= 0.0)
 7420                           774                 :         146207 :         selec *= hist_selec;
                                775                 :                :     else
                                776                 :                :     {
                                777                 :                :         /*
                                778                 :                :          * If no histogram but there are values not accounted for by MCV,
                                779                 :                :          * arbitrarily assume half of them will match.
                                780                 :                :          */
                                781                 :          85038 :         selec *= 0.5;
                                782                 :                :     }
                                783                 :                : 
                                784                 :         231245 :     selec += mcv_selec;
                                785                 :                : 
                                786                 :                :     /* result should be in range, but make sure... */
                                787   [ +  +  +  + ]:         231245 :     CLAMP_PROBABILITY(selec);
                                788                 :                : 
                                789                 :         231245 :     return selec;
                                790                 :                : }
                                791                 :                : 
                                792                 :                : /*
                                793                 :                :  *  mcv_selectivity         - Examine the MCV list for selectivity estimates
                                794                 :                :  *
                                795                 :                :  * Determine the fraction of the variable's MCV population that satisfies
                                796                 :                :  * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.  Also
                                797                 :                :  * compute the fraction of the total column population represented by the MCV
                                798                 :                :  * list.  This code will work for any boolean-returning predicate operator.
                                799                 :                :  *
                                800                 :                :  * The function result is the MCV selectivity, and the fraction of the
                                801                 :                :  * total population is returned into *sumcommonp.  Zeroes are returned
                                802                 :                :  * if there is no MCV list.
                                803                 :                :  */
                                804                 :                : double
 2160                           805                 :         235647 : mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation,
                                806                 :                :                 Datum constval, bool varonleft,
                                807                 :                :                 double *sumcommonp)
                                808                 :                : {
                                809                 :                :     double      mcv_selec,
                                810                 :                :                 sumcommon;
                                811                 :                :     AttStatsSlot sslot;
                                812                 :                :     int         i;
                                813                 :                : 
 9129                           814                 :         235647 :     mcv_selec = 0.0;
                                815                 :         235647 :     sumcommon = 0.0;
                                816                 :                : 
 7420                           817   [ +  +  +  + ]:         469499 :     if (HeapTupleIsValid(vardata->statsTuple) &&
 3287 peter_e@gmx.net           818         [ +  + ]:         467429 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
 3279 tgl@sss.pgh.pa.us         819                 :         233577 :         get_attstatsslot(&sslot, vardata->statsTuple,
                                820                 :                :                          STATISTIC_KIND_MCV, InvalidOid,
                                821                 :                :                          ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
                                822                 :                :     {
 2205                           823                 :         132053 :         LOCAL_FCINFO(fcinfo, 2);
                                824                 :                : 
                                825                 :                :         /*
                                826                 :                :          * We invoke the opproc "by hand" so that we won't fail on NULL
                                827                 :                :          * results.  Such cases won't arise for normal comparison functions,
                                828                 :                :          * but generic_restriction_selectivity could perhaps be used with
                                829                 :                :          * operators that can return NULL.  A small side benefit is to not
                                830                 :                :          * need to re-initialize the fcinfo struct from scratch each time.
                                831                 :                :          */
 2160                           832                 :         132053 :         InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
                                833                 :                :                                  NULL, NULL);
 2205                           834                 :         132053 :         fcinfo->args[0].isnull = false;
                                835                 :         132053 :         fcinfo->args[1].isnull = false;
                                836                 :                :         /* be careful to apply operator right way 'round */
                                837         [ +  - ]:         132053 :         if (varonleft)
                                838                 :         132053 :             fcinfo->args[1].value = constval;
                                839                 :                :         else
 2205 tgl@sss.pgh.pa.us         840                 :UBC           0 :             fcinfo->args[0].value = constval;
                                841                 :                : 
 3279 tgl@sss.pgh.pa.us         842         [ +  + ]:CBC     3297594 :         for (i = 0; i < sslot.nvalues; i++)
                                843                 :                :         {
                                844                 :                :             Datum       fresult;
                                845                 :                : 
 2205                           846         [ +  - ]:        3165541 :             if (varonleft)
                                847                 :        3165541 :                 fcinfo->args[0].value = sslot.values[i];
                                848                 :                :             else
 2205 tgl@sss.pgh.pa.us         849                 :UBC           0 :                 fcinfo->args[1].value = sslot.values[i];
 2205 tgl@sss.pgh.pa.us         850                 :CBC     3165541 :             fcinfo->isnull = false;
                                851                 :        3165541 :             fresult = FunctionCallInvoke(fcinfo);
                                852   [ +  -  +  + ]:        3165541 :             if (!fcinfo->isnull && DatumGetBool(fresult))
 3279                           853                 :        1329110 :                 mcv_selec += sslot.numbers[i];
                                854                 :        3165541 :             sumcommon += sslot.numbers[i];
                                855                 :                :         }
                                856                 :         132053 :         free_attstatsslot(&sslot);
                                857                 :                :     }
                                858                 :                : 
 7420                           859                 :         235647 :     *sumcommonp = sumcommon;
                                860                 :         235647 :     return mcv_selec;
                                861                 :                : }
                                862                 :                : 
                                863                 :                : /*
                                864                 :                :  *  histogram_selectivity   - Examine the histogram for selectivity estimates
                                865                 :                :  *
                                866                 :                :  * Determine the fraction of the variable's histogram entries that satisfy
                                867                 :                :  * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.
                                868                 :                :  *
                                869                 :                :  * This code will work for any boolean-returning predicate operator, whether
                                870                 :                :  * or not it has anything to do with the histogram sort operator.  We are
                                871                 :                :  * essentially using the histogram just as a representative sample.  However,
                                872                 :                :  * small histograms are unlikely to be all that representative, so the caller
                                873                 :                :  * should be prepared to fall back on some other estimation approach when the
                                874                 :                :  * histogram is missing or very small.  It may also be prudent to combine this
                                875                 :                :  * approach with another one when the histogram is small.
                                876                 :                :  *
                                877                 :                :  * If the actual histogram size is not at least min_hist_size, we won't bother
                                878                 :                :  * to do the calculation at all.  Also, if the n_skip parameter is > 0, we
                                879                 :                :  * ignore the first and last n_skip histogram elements, on the grounds that
                                880                 :                :  * they are outliers and hence not very representative.  Typical values for
                                881                 :                :  * these parameters are 10 and 1.
                                882                 :                :  *
                                883                 :                :  * The function result is the selectivity, or -1 if there is no histogram
                                884                 :                :  * or it's smaller than min_hist_size.
                                885                 :                :  *
                                886                 :                :  * The output parameter *hist_size receives the actual histogram size,
                                887                 :                :  * or zero if no histogram.  Callers may use this number to decide how
                                888                 :                :  * much faith to put in the function result.
                                889                 :                :  *
                                890                 :                :  * Note that the result disregards both the most-common-values (if any) and
                                891                 :                :  * null entries.  The caller is expected to combine this result with
                                892                 :                :  * statistics for those portions of the column population.  It may also be
                                893                 :                :  * prudent to clamp the result range, ie, disbelieve exact 0 or 1 outputs.
                                894                 :                :  */
                                895                 :                : double
 2160                           896                 :           4402 : histogram_selectivity(VariableStatData *vardata,
                                897                 :                :                       FmgrInfo *opproc, Oid collation,
                                898                 :                :                       Datum constval, bool varonleft,
                                899                 :                :                       int min_hist_size, int n_skip,
                                900                 :                :                       int *hist_size)
                                901                 :                : {
                                902                 :                :     double      result;
                                903                 :                :     AttStatsSlot sslot;
                                904                 :                : 
                                905                 :                :     /* check sanity of parameters */
 7167                           906         [ -  + ]:           4402 :     Assert(n_skip >= 0);
                                907         [ -  + ]:           4402 :     Assert(min_hist_size > 2 * n_skip);
                                908                 :                : 
                                909   [ +  +  +  + ]:           7009 :     if (HeapTupleIsValid(vardata->statsTuple) &&
 3287 peter_e@gmx.net           910         [ +  + ]:           5209 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
 3279 tgl@sss.pgh.pa.us         911                 :           2602 :         get_attstatsslot(&sslot, vardata->statsTuple,
                                912                 :                :                          STATISTIC_KIND_HISTOGRAM, InvalidOid,
                                913                 :                :                          ATTSTATSSLOT_VALUES))
                                914                 :                :     {
                                915                 :           2524 :         *hist_size = sslot.nvalues;
                                916         [ +  + ]:           2524 :         if (sslot.nvalues >= min_hist_size)
                                917                 :                :         {
 2205                           918                 :           1475 :             LOCAL_FCINFO(fcinfo, 2);
 7167                           919                 :           1475 :             int         nmatch = 0;
                                920                 :                :             int         i;
                                921                 :                : 
                                922                 :                :             /*
                                923                 :                :              * We invoke the opproc "by hand" so that we won't fail on NULL
                                924                 :                :              * results.  Such cases won't arise for normal comparison
                                925                 :                :              * functions, but generic_restriction_selectivity could perhaps be
                                926                 :                :              * used with operators that can return NULL.  A small side benefit
                                927                 :                :              * is to not need to re-initialize the fcinfo struct from scratch
                                928                 :                :              * each time.
                                929                 :                :              */
 2160                           930                 :           1475 :             InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
                                931                 :                :                                      NULL, NULL);
 2205                           932                 :           1475 :             fcinfo->args[0].isnull = false;
                                933                 :           1475 :             fcinfo->args[1].isnull = false;
                                934                 :                :             /* be careful to apply operator right way 'round */
                                935         [ +  - ]:           1475 :             if (varonleft)
                                936                 :           1475 :                 fcinfo->args[1].value = constval;
                                937                 :                :             else
 2205 tgl@sss.pgh.pa.us         938                 :UBC           0 :                 fcinfo->args[0].value = constval;
                                939                 :                : 
 3279 tgl@sss.pgh.pa.us         940         [ +  + ]:CBC      124350 :             for (i = n_skip; i < sslot.nvalues - n_skip; i++)
                                941                 :                :             {
                                942                 :                :                 Datum       fresult;
                                943                 :                : 
 2205                           944         [ +  - ]:         122875 :                 if (varonleft)
                                945                 :         122875 :                     fcinfo->args[0].value = sslot.values[i];
                                946                 :                :                 else
 2205 tgl@sss.pgh.pa.us         947                 :UBC           0 :                     fcinfo->args[1].value = sslot.values[i];
 2205 tgl@sss.pgh.pa.us         948                 :CBC      122875 :                 fcinfo->isnull = false;
                                949                 :         122875 :                 fresult = FunctionCallInvoke(fcinfo);
                                950   [ +  -  +  + ]:         122875 :                 if (!fcinfo->isnull && DatumGetBool(fresult))
 7167                           951                 :           8126 :                     nmatch++;
                                952                 :                :             }
 3279                           953                 :           1475 :             result = ((double) nmatch) / ((double) (sslot.nvalues - 2 * n_skip));
                                954                 :                :         }
                                955                 :                :         else
 7167                           956                 :           1049 :             result = -1;
 3279                           957                 :           2524 :         free_attstatsslot(&sslot);
                                958                 :                :     }
                                959                 :                :     else
                                960                 :                :     {
 6631                           961                 :           1878 :         *hist_size = 0;
 7167                           962                 :           1878 :         result = -1;
                                963                 :                :     }
                                964                 :                : 
                                965                 :           4402 :     return result;
                                966                 :                : }
                                967                 :                : 
                                968                 :                : /*
                                969                 :                :  *  generic_restriction_selectivity     - Selectivity for almost anything
                                970                 :                :  *
                                971                 :                :  * This function estimates selectivity for operators that we don't have any
                                972                 :                :  * special knowledge about, but are on data types that we collect standard
                                973                 :                :  * MCV and/or histogram statistics for.  (Additional assumptions are that
                                974                 :                :  * the operator is strict and immutable, or at least stable.)
                                975                 :                :  *
                                976                 :                :  * If we have "VAR OP CONST" or "CONST OP VAR", selectivity is estimated by
                                977                 :                :  * applying the operator to each element of the column's MCV and/or histogram
                                978                 :                :  * stats, and merging the results using the assumption that the histogram is
                                979                 :                :  * a reasonable random sample of the column's non-MCV population.  Note that
                                980                 :                :  * if the operator's semantics are related to the histogram ordering, this
                                981                 :                :  * might not be such a great assumption; other functions such as
                                982                 :                :  * scalarineqsel() are probably a better match in such cases.
                                983                 :                :  *
                                984                 :                :  * Otherwise, fall back to the default selectivity provided by the caller.
                                985                 :                :  */
                                986                 :                : double
 2160                           987                 :            845 : generic_restriction_selectivity(PlannerInfo *root, Oid oproid, Oid collation,
                                988                 :                :                                 List *args, int varRelid,
                                989                 :                :                                 double default_selectivity)
                                990                 :                : {
                                991                 :                :     double      selec;
                                992                 :                :     VariableStatData vardata;
                                993                 :                :     Node       *other;
                                994                 :                :     bool        varonleft;
                                995                 :                : 
                                996                 :                :     /*
                                997                 :                :      * If expression is not variable OP something or something OP variable,
                                998                 :                :      * then punt and return the default estimate.
                                999                 :                :      */
 2225                          1000         [ -  + ]:            845 :     if (!get_restriction_variable(root, args, varRelid,
                               1001                 :                :                                   &vardata, &other, &varonleft))
 2225 tgl@sss.pgh.pa.us        1002                 :UBC           0 :         return default_selectivity;
                               1003                 :                : 
                               1004                 :                :     /*
                               1005                 :                :      * If the something is a NULL constant, assume operator is strict and
                               1006                 :                :      * return zero, ie, operator will never return TRUE.
                               1007                 :                :      */
 2225 tgl@sss.pgh.pa.us        1008         [ +  - ]:CBC         845 :     if (IsA(other, Const) &&
                               1009         [ -  + ]:            845 :         ((Const *) other)->constisnull)
                               1010                 :                :     {
 2225 tgl@sss.pgh.pa.us        1011         [ #  # ]:UBC           0 :         ReleaseVariableStats(vardata);
                               1012                 :              0 :         return 0.0;
                               1013                 :                :     }
                               1014                 :                : 
 2225 tgl@sss.pgh.pa.us        1015         [ +  - ]:CBC         845 :     if (IsA(other, Const))
                               1016                 :                :     {
                               1017                 :                :         /* Variable is being compared to a known non-null constant */
                               1018                 :            845 :         Datum       constval = ((Const *) other)->constvalue;
                               1019                 :                :         FmgrInfo    opproc;
                               1020                 :                :         double      mcvsum;
                               1021                 :                :         double      mcvsel;
                               1022                 :                :         double      nullfrac;
                               1023                 :                :         int         hist_size;
                               1024                 :                : 
 2205                          1025                 :            845 :         fmgr_info(get_opcode(oproid), &opproc);
                               1026                 :                : 
                               1027                 :                :         /*
                               1028                 :                :          * Calculate the selectivity for the column's most common values.
                               1029                 :                :          */
 2160                          1030                 :            845 :         mcvsel = mcv_selectivity(&vardata, &opproc, collation,
                               1031                 :                :                                  constval, varonleft,
                               1032                 :                :                                  &mcvsum);
                               1033                 :                : 
                               1034                 :                :         /*
                               1035                 :                :          * If the histogram is large enough, see what fraction of it matches
                               1036                 :                :          * the query, and assume that's representative of the non-MCV
                               1037                 :                :          * population.  Otherwise use the default selectivity for the non-MCV
                               1038                 :                :          * population.
                               1039                 :                :          */
                               1040                 :            845 :         selec = histogram_selectivity(&vardata, &opproc, collation,
                               1041                 :                :                                       constval, varonleft,
                               1042                 :                :                                       10, 1, &hist_size);
 2225                          1043         [ +  - ]:            845 :         if (selec < 0)
                               1044                 :                :         {
                               1045                 :                :             /* Nope, fall back on default */
                               1046                 :            845 :             selec = default_selectivity;
                               1047                 :                :         }
 2225 tgl@sss.pgh.pa.us        1048         [ #  # ]:UBC           0 :         else if (hist_size < 100)
                               1049                 :                :         {
                               1050                 :                :             /*
                               1051                 :                :              * For histogram sizes from 10 to 100, we combine the histogram
                               1052                 :                :              * and default selectivities, putting increasingly more trust in
                               1053                 :                :              * the histogram for larger sizes.
                               1054                 :                :              */
                               1055                 :              0 :             double      hist_weight = hist_size / 100.0;
                               1056                 :                : 
                               1057                 :              0 :             selec = selec * hist_weight +
                               1058                 :              0 :                 default_selectivity * (1.0 - hist_weight);
                               1059                 :                :         }
                               1060                 :                : 
                               1061                 :                :         /* In any case, don't believe extremely small or large estimates. */
 2225 tgl@sss.pgh.pa.us        1062         [ -  + ]:CBC         845 :         if (selec < 0.0001)
 2225 tgl@sss.pgh.pa.us        1063                 :UBC           0 :             selec = 0.0001;
 2225 tgl@sss.pgh.pa.us        1064         [ -  + ]:CBC         845 :         else if (selec > 0.9999)
 2225 tgl@sss.pgh.pa.us        1065                 :UBC           0 :             selec = 0.9999;
                               1066                 :                : 
                               1067                 :                :         /* Don't forget to account for nulls. */
 2225 tgl@sss.pgh.pa.us        1068         [ +  + ]:CBC         845 :         if (HeapTupleIsValid(vardata.statsTuple))
                               1069                 :             70 :             nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stanullfrac;
                               1070                 :                :         else
                               1071                 :            775 :             nullfrac = 0.0;
                               1072                 :                : 
                               1073                 :                :         /*
                               1074                 :                :          * Now merge the results from the MCV and histogram calculations,
                               1075                 :                :          * realizing that the histogram covers only the non-null values that
                               1076                 :                :          * are not listed in MCV.
                               1077                 :                :          */
                               1078                 :            845 :         selec *= 1.0 - nullfrac - mcvsum;
                               1079                 :            845 :         selec += mcvsel;
                               1080                 :                :     }
                               1081                 :                :     else
                               1082                 :                :     {
                               1083                 :                :         /* Comparison value is not constant, so we can't do anything */
 2225 tgl@sss.pgh.pa.us        1084                 :UBC           0 :         selec = default_selectivity;
                               1085                 :                :     }
                               1086                 :                : 
 2225 tgl@sss.pgh.pa.us        1087         [ +  + ]:CBC         845 :     ReleaseVariableStats(vardata);
                               1088                 :                : 
                               1089                 :                :     /* result should be in range, but make sure... */
                               1090   [ -  +  -  + ]:            845 :     CLAMP_PROBABILITY(selec);
                               1091                 :                : 
                               1092                 :            845 :     return selec;
                               1093                 :                : }
                               1094                 :                : 
                               1095                 :                : /*
                               1096                 :                :  *  ineq_histogram_selectivity  - Examine the histogram for scalarineqsel
                               1097                 :                :  *
                               1098                 :                :  * Determine the fraction of the variable's histogram population that
                               1099                 :                :  * satisfies the inequality condition, ie, VAR < (or <=, >, >=) CONST.
                               1100                 :                :  * The isgt and iseq flags distinguish which of the four cases apply.
                               1101                 :                :  *
                               1102                 :                :  * While opproc could be looked up from the operator OID, common callers
                               1103                 :                :  * also need to call it separately, so we make the caller pass both.
                               1104                 :                :  *
                               1105                 :                :  * Returns -1 if there is no histogram (valid results will always be >= 0).
                               1106                 :                :  *
                               1107                 :                :  * Note that the result disregards both the most-common-values (if any) and
                               1108                 :                :  * null entries.  The caller is expected to combine this result with
                               1109                 :                :  * statistics for those portions of the column population.
                               1110                 :                :  *
                               1111                 :                :  * This is exported so that some other estimation functions can use it.
                               1112                 :                :  */
                               1113                 :                : double
 5965                          1114                 :         234454 : ineq_histogram_selectivity(PlannerInfo *root,
                               1115                 :                :                            VariableStatData *vardata,
                               1116                 :                :                            Oid opoid, FmgrInfo *opproc, bool isgt, bool iseq,
                               1117                 :                :                            Oid collation,
                               1118                 :                :                            Datum constval, Oid consttype)
                               1119                 :                : {
                               1120                 :                :     double      hist_selec;
                               1121                 :                :     AttStatsSlot sslot;
                               1122                 :                : 
                               1123                 :         234454 :     hist_selec = -1.0;
                               1124                 :                : 
                               1125                 :                :     /*
                               1126                 :                :      * Someday, ANALYZE might store more than one histogram per rel/att,
                               1127                 :                :      * corresponding to more than one possible sort ordering defined for the
                               1128                 :                :      * column type.  Right now, we know there is only one, so just grab it and
                               1129                 :                :      * see if it matches the query.
                               1130                 :                :      *
                               1131                 :                :      * Note that we can't use opoid as search argument; the staop appearing in
                               1132                 :                :      * pg_statistic will be for the relevant '<' operator, but what we have
                               1133                 :                :      * might be some other inequality operator such as '>='.  (Even if opoid
                               1134                 :                :      * is a '<' operator, it could be cross-type.)  Hence we must use
                               1135                 :                :      * comparison_ops_are_compatible() to see if the operators match.
                               1136                 :                :      */
 7420                          1137   [ +  +  +  + ]:         468401 :     if (HeapTupleIsValid(vardata->statsTuple) &&
 3287 peter_e@gmx.net          1138         [ +  + ]:         467624 :         statistic_proc_security_check(vardata, opproc->fn_oid) &&
 3279 tgl@sss.pgh.pa.us        1139                 :         233677 :         get_attstatsslot(&sslot, vardata->statsTuple,
                               1140                 :                :                          STATISTIC_KIND_HISTOGRAM, InvalidOid,
                               1141                 :                :                          ATTSTATSSLOT_VALUES))
                               1142                 :                :     {
 2160                          1143         [ +  - ]:         148907 :         if (sslot.nvalues > 1 &&
                               1144   [ +  +  +  + ]:         297746 :             sslot.stacoll == collation &&
                               1145                 :         148839 :             comparison_ops_are_compatible(sslot.staop, opoid))
 9773                          1146                 :         148749 :         {
                               1147                 :                :             /*
                               1148                 :                :              * Use binary search to find the desired location, namely the
                               1149                 :                :              * right end of the histogram bin containing the comparison value,
                               1150                 :                :              * which is the leftmost entry for which the comparison operator
                               1151                 :                :              * succeeds (if isgt) or fails (if !isgt).
                               1152                 :                :              *
                               1153                 :                :              * In this loop, we pay no attention to whether the operator iseq
                               1154                 :                :              * or not; that detail will be mopped up below.  (We cannot tell,
                               1155                 :                :              * anyway, whether the operator thinks the values are equal.)
                               1156                 :                :              *
                               1157                 :                :              * If the binary search accesses the first or last histogram
                               1158                 :                :              * entry, we try to replace that endpoint with the true column min
                               1159                 :                :              * or max as found by get_actual_variable_range().  This
                               1160                 :                :              * ameliorates misestimates when the min or max is moving as a
                               1161                 :                :              * result of changes since the last ANALYZE.  Note that this could
                               1162                 :                :              * result in effectively including MCVs into the histogram that
                               1163                 :                :              * weren't there before, but we don't try to correct for that.
                               1164                 :                :              */
                               1165                 :                :             double      histfrac;
 7153 bruce@momjian.us         1166                 :         148749 :             int         lobound = 0;    /* first possible slot to search */
 3240 tgl@sss.pgh.pa.us        1167                 :         148749 :             int         hibound = sslot.nvalues;    /* last+1 slot to search */
 5965                          1168                 :         148749 :             bool        have_end = false;
                               1169                 :                : 
                               1170                 :                :             /*
                               1171                 :                :              * If there are only two histogram entries, we'll want up-to-date
                               1172                 :                :              * values for both.  (If there are more than two, we need at most
                               1173                 :                :              * one of them to be updated, so we deal with that within the
                               1174                 :                :              * loop.)
                               1175                 :                :              */
 3279                          1176         [ +  + ]:         148749 :             if (sslot.nvalues == 2)
 5965                          1177                 :           4839 :                 have_end = get_actual_variable_range(root,
                               1178                 :                :                                                      vardata,
                               1179                 :                :                                                      sslot.staop,
                               1180                 :                :                                                      collation,
                               1181                 :                :                                                      &sslot.values[0],
 3279                          1182                 :           4839 :                                                      &sslot.values[1]);
                               1183                 :                : 
 7167                          1184         [ +  + ]:         996310 :             while (lobound < hibound)
                               1185                 :                :             {
 7153 bruce@momjian.us         1186                 :         847561 :                 int         probe = (lobound + hibound) / 2;
                               1187                 :                :                 bool        ltcmp;
                               1188                 :                : 
                               1189                 :                :                 /*
                               1190                 :                :                  * If we find ourselves about to compare to the first or last
                               1191                 :                :                  * histogram entry, first try to replace it with the actual
                               1192                 :                :                  * current min or max (unless we already did so above).
                               1193                 :                :                  */
 3279 tgl@sss.pgh.pa.us        1194   [ +  +  +  + ]:         847561 :                 if (probe == 0 && sslot.nvalues > 2)
 5965                          1195                 :          70012 :                     have_end = get_actual_variable_range(root,
                               1196                 :                :                                                          vardata,
                               1197                 :                :                                                          sslot.staop,
                               1198                 :                :                                                          collation,
                               1199                 :                :                                                          &sslot.values[0],
                               1200                 :                :                                                          NULL);
 3279                          1201   [ +  +  +  + ]:         777549 :                 else if (probe == sslot.nvalues - 1 && sslot.nvalues > 2)
 5965                          1202                 :          50290 :                     have_end = get_actual_variable_range(root,
                               1203                 :                :                                                          vardata,
                               1204                 :                :                                                          sslot.staop,
                               1205                 :                :                                                          collation,
                               1206                 :                :                                                          NULL,
 3240                          1207                 :          50290 :                                                          &sslot.values[probe]);
                               1208                 :                : 
 5502                          1209                 :         847561 :                 ltcmp = DatumGetBool(FunctionCall2Coll(opproc,
                               1210                 :                :                                                        collation,
 3279                          1211                 :         847561 :                                                        sslot.values[probe],
                               1212                 :                :                                                        constval));
 7167                          1213         [ +  + ]:         847561 :                 if (isgt)
                               1214                 :          51947 :                     ltcmp = !ltcmp;
                               1215         [ +  + ]:         847561 :                 if (ltcmp)
                               1216                 :         326404 :                     lobound = probe + 1;
                               1217                 :                :                 else
                               1218                 :         521157 :                     hibound = probe;
                               1219                 :                :             }
                               1220                 :                : 
                               1221         [ +  + ]:         148749 :             if (lobound <= 0)
                               1222                 :                :             {
                               1223                 :                :                 /*
                               1224                 :                :                  * Constant is below lower histogram boundary.  More
                               1225                 :                :                  * precisely, we have found that no entry in the histogram
                               1226                 :                :                  * satisfies the inequality clause (if !isgt) or they all do
                               1227                 :                :                  * (if isgt).  We estimate that that's true of the entire
                               1228                 :                :                  * table, so set histfrac to 0.0 (which we'll flip to 1.0
                               1229                 :                :                  * below, if isgt).
                               1230                 :                :                  */
 9129                          1231                 :          62050 :                 histfrac = 0.0;
                               1232                 :                :             }
 3279                          1233         [ +  + ]:          86699 :             else if (lobound >= sslot.nvalues)
                               1234                 :                :             {
                               1235                 :                :                 /*
                               1236                 :                :                  * Inverse case: constant is above upper histogram boundary.
                               1237                 :                :                  */
 7167                          1238                 :          25849 :                 histfrac = 1.0;
                               1239                 :                :             }
                               1240                 :                :             else
                               1241                 :                :             {
                               1242                 :                :                 /* We have values[i-1] <= constant <= values[i]. */
                               1243                 :          60850 :                 int         i = lobound;
 3156                          1244                 :          60850 :                 double      eq_selec = 0;
                               1245                 :                :                 double      val,
                               1246                 :                :                             high,
                               1247                 :                :                             low;
                               1248                 :                :                 double      binfrac;
                               1249                 :                : 
                               1250                 :                :                 /*
                               1251                 :                :                  * In the cases where we'll need it below, obtain an estimate
                               1252                 :                :                  * of the selectivity of "x = constval".  We use a calculation
                               1253                 :                :                  * similar to what var_eq_const() does for a non-MCV constant,
                               1254                 :                :                  * ie, estimate that all distinct non-MCV values occur equally
                               1255                 :                :                  * often.  But multiplication by "1.0 - sumcommon - nullfrac"
                               1256                 :                :                  * will be done by our caller, so we shouldn't do that here.
                               1257                 :                :                  * Therefore we can't try to clamp the estimate by reference
                               1258                 :                :                  * to the least common MCV; the result would be too small.
                               1259                 :                :                  *
                               1260                 :                :                  * Note: since this is effectively assuming that constval
                               1261                 :                :                  * isn't an MCV, it's logically dubious if constval in fact is
                               1262                 :                :                  * one.  But we have to apply *some* correction for equality,
                               1263                 :                :                  * and anyway we cannot tell if constval is an MCV, since we
                               1264                 :                :                  * don't have a suitable equality operator at hand.
                               1265                 :                :                  */
                               1266   [ +  +  +  + ]:          60850 :                 if (i == 1 || isgt == iseq)
                               1267                 :                :                 {
                               1268                 :                :                     double      otherdistinct;
                               1269                 :                :                     bool        isdefault;
                               1270                 :                :                     AttStatsSlot mcvslot;
                               1271                 :                : 
                               1272                 :                :                     /* Get estimated number of distinct values */
                               1273                 :          26122 :                     otherdistinct = get_variable_numdistinct(vardata,
                               1274                 :                :                                                              &isdefault);
                               1275                 :                : 
                               1276                 :                :                     /* Subtract off the number of known MCVs */
                               1277         [ +  + ]:          26122 :                     if (get_attstatsslot(&mcvslot, vardata->statsTuple,
                               1278                 :                :                                          STATISTIC_KIND_MCV, InvalidOid,
                               1279                 :                :                                          ATTSTATSSLOT_NUMBERS))
                               1280                 :                :                     {
                               1281                 :           3804 :                         otherdistinct -= mcvslot.nnumbers;
                               1282                 :           3804 :                         free_attstatsslot(&mcvslot);
                               1283                 :                :                     }
                               1284                 :                : 
                               1285                 :                :                     /* If result doesn't seem sane, leave eq_selec at 0 */
                               1286         [ +  + ]:          26122 :                     if (otherdistinct > 1)
                               1287                 :          26101 :                         eq_selec = 1.0 / otherdistinct;
                               1288                 :                :                 }
                               1289                 :                : 
                               1290                 :                :                 /*
                               1291                 :                :                  * Convert the constant and the two nearest bin boundary
                               1292                 :                :                  * values to a uniform comparison scale, and do a linear
                               1293                 :                :                  * interpolation within this bin.
                               1294                 :                :                  */
 2160                          1295         [ +  - ]:          60850 :                 if (convert_to_scalar(constval, consttype, collation,
                               1296                 :                :                                       &val,
 3279                          1297                 :          60850 :                                       sslot.values[i - 1], sslot.values[i],
                               1298                 :                :                                       vardata->vartype,
                               1299                 :                :                                       &low, &high))
                               1300                 :                :                 {
 7167                          1301         [ -  + ]:          60850 :                     if (high <= low)
                               1302                 :                :                     {
                               1303                 :                :                         /* cope if bin boundaries appear identical */
 7167 tgl@sss.pgh.pa.us        1304                 :UBC           0 :                         binfrac = 0.5;
                               1305                 :                :                     }
 7167 tgl@sss.pgh.pa.us        1306         [ +  + ]:CBC       60850 :                     else if (val <= low)
                               1307                 :          10734 :                         binfrac = 0.0;
                               1308         [ +  + ]:          50116 :                     else if (val >= high)
                               1309                 :           1998 :                         binfrac = 1.0;
                               1310                 :                :                     else
                               1311                 :                :                     {
                               1312                 :          48118 :                         binfrac = (val - low) / (high - low);
                               1313                 :                : 
                               1314                 :                :                         /*
                               1315                 :                :                          * Watch out for the possibility that we got a NaN or
                               1316                 :                :                          * Infinity from the division.  This can happen
                               1317                 :                :                          * despite the previous checks, if for example "low"
                               1318                 :                :                          * is -Infinity.
                               1319                 :                :                          */
                               1320   [ +  -  +  - ]:          48118 :                         if (isnan(binfrac) ||
                               1321         [ -  + ]:          48118 :                             binfrac < 0.0 || binfrac > 1.0)
 7167 tgl@sss.pgh.pa.us        1322                 :UBC           0 :                             binfrac = 0.5;
                               1323                 :                :                     }
                               1324                 :                :                 }
                               1325                 :                :                 else
                               1326                 :                :                 {
                               1327                 :                :                     /*
                               1328                 :                :                      * Ideally we'd produce an error here, on the grounds that
                               1329                 :                :                      * the given operator shouldn't have scalarXXsel
                               1330                 :                :                      * registered as its selectivity func unless we can deal
                               1331                 :                :                      * with its operand types.  But currently, all manner of
                               1332                 :                :                      * stuff is invoking scalarXXsel, so give a default
                               1333                 :                :                      * estimate until that can be fixed.
                               1334                 :                :                      */
                               1335                 :              0 :                     binfrac = 0.5;
                               1336                 :                :                 }
                               1337                 :                : 
                               1338                 :                :                 /*
                               1339                 :                :                  * Now, compute the overall selectivity across the values
                               1340                 :                :                  * represented by the histogram.  We have i-1 full bins and
                               1341                 :                :                  * binfrac partial bin below the constant.
                               1342                 :                :                  */
 7167 tgl@sss.pgh.pa.us        1343                 :CBC       60850 :                 histfrac = (double) (i - 1) + binfrac;
 3279                          1344                 :          60850 :                 histfrac /= (double) (sslot.nvalues - 1);
                               1345                 :                : 
                               1346                 :                :                 /*
                               1347                 :                :                  * At this point, histfrac is an estimate of the fraction of
                               1348                 :                :                  * the population represented by the histogram that satisfies
                               1349                 :                :                  * "x <= constval".  Somewhat remarkably, this statement is
                               1350                 :                :                  * true regardless of which operator we were doing the probes
                               1351                 :                :                  * with, so long as convert_to_scalar() delivers reasonable
                               1352                 :                :                  * results.  If the probe constant is equal to some histogram
                               1353                 :                :                  * entry, we would have considered the bin to the left of that
                               1354                 :                :                  * entry if probing with "<" or ">=", or the bin to the right
                               1355                 :                :                  * if probing with "<=" or ">"; but binfrac would have come
                               1356                 :                :                  * out as 1.0 in the first case and 0.0 in the second, leading
                               1357                 :                :                  * to the same histfrac in either case.  For probe constants
                               1358                 :                :                  * between histogram entries, we find the same bin and get the
                               1359                 :                :                  * same estimate with any operator.
                               1360                 :                :                  *
                               1361                 :                :                  * The fact that the estimate corresponds to "x <= constval"
                               1362                 :                :                  * and not "x < constval" is because of the way that ANALYZE
                               1363                 :                :                  * constructs the histogram: each entry is, effectively, the
                               1364                 :                :                  * rightmost value in its sample bucket.  So selectivity
                               1365                 :                :                  * values that are exact multiples of 1/(histogram_size-1)
                               1366                 :                :                  * should be understood as estimates including a histogram
                               1367                 :                :                  * entry plus everything to its left.
                               1368                 :                :                  *
                               1369                 :                :                  * However, that breaks down for the first histogram entry,
                               1370                 :                :                  * which necessarily is the leftmost value in its sample
                               1371                 :                :                  * bucket.  That means the first histogram bin is slightly
                               1372                 :                :                  * narrower than the rest, by an amount equal to eq_selec.
                               1373                 :                :                  * Another way to say that is that we want "x <= leftmost" to
                               1374                 :                :                  * be estimated as eq_selec not zero.  So, if we're dealing
                               1375                 :                :                  * with the first bin (i==1), rescale to make that true while
                               1376                 :                :                  * adjusting the rest of that bin linearly.
                               1377                 :                :                  */
 3156                          1378         [ +  + ]:          60850 :                 if (i == 1)
                               1379                 :          10774 :                     histfrac += eq_selec * (1.0 - binfrac);
                               1380                 :                : 
                               1381                 :                :                 /*
                               1382                 :                :                  * "x <= constval" is good if we want an estimate for "<=" or
                               1383                 :                :                  * ">", but if we are estimating for "<" or ">=", we now need
                               1384                 :                :                  * to decrease the estimate by eq_selec.
                               1385                 :                :                  */
                               1386         [ +  + ]:          60850 :                 if (isgt == iseq)
                               1387                 :          21025 :                     histfrac -= eq_selec;
                               1388                 :                :             }
                               1389                 :                : 
                               1390                 :                :             /*
                               1391                 :                :              * Now the estimate is finished for "<" and "<=" cases.  If we are
                               1392                 :                :              * estimating for ">" or ">=", flip it.
                               1393                 :                :              */
 9129                          1394         [ +  + ]:         148749 :             hist_selec = isgt ? (1.0 - histfrac) : histfrac;
                               1395                 :                : 
                               1396                 :                :             /*
                               1397                 :                :              * The histogram boundaries are only approximate to begin with,
                               1398                 :                :              * and may well be out of date anyway.  Therefore, don't believe
                               1399                 :                :              * extremely small or large selectivity estimates --- unless we
                               1400                 :                :              * got actual current endpoint values from the table, in which
                               1401                 :                :              * case just do the usual sanity clamp.  Somewhat arbitrarily, we
                               1402                 :                :              * set the cutoff for other cases at a hundredth of the histogram
                               1403                 :                :              * resolution.
                               1404                 :                :              */
 5965                          1405         [ +  + ]:         148749 :             if (have_end)
                               1406   [ +  +  -  + ]:          82863 :                 CLAMP_PROBABILITY(hist_selec);
                               1407                 :                :             else
                               1408                 :                :             {
 3156                          1409                 :          65886 :                 double      cutoff = 0.01 / (double) (sslot.nvalues - 1);
                               1410                 :                : 
                               1411         [ +  + ]:          65886 :                 if (hist_selec < cutoff)
                               1412                 :          21504 :                     hist_selec = cutoff;
                               1413         [ +  + ]:          44382 :                 else if (hist_selec > 1.0 - cutoff)
                               1414                 :          16491 :                     hist_selec = 1.0 - cutoff;
                               1415                 :                :             }
                               1416                 :                :         }
 2160                          1417         [ +  - ]:            158 :         else if (sslot.nvalues > 1)
                               1418                 :                :         {
                               1419                 :                :             /*
                               1420                 :                :              * If we get here, we have a histogram but it's not sorted the way
                               1421                 :                :              * we want.  Do a brute-force search to see how many of the
                               1422                 :                :              * entries satisfy the comparison condition, and take that
                               1423                 :                :              * fraction as our estimate.  (This is identical to the inner loop
                               1424                 :                :              * of histogram_selectivity; maybe share code?)
                               1425                 :                :              */
                               1426                 :            158 :             LOCAL_FCINFO(fcinfo, 2);
                               1427                 :            158 :             int         nmatch = 0;
                               1428                 :                : 
                               1429                 :            158 :             InitFunctionCallInfoData(*fcinfo, opproc, 2, collation,
                               1430                 :                :                                      NULL, NULL);
                               1431                 :            158 :             fcinfo->args[0].isnull = false;
                               1432                 :            158 :             fcinfo->args[1].isnull = false;
                               1433                 :            158 :             fcinfo->args[1].value = constval;
                               1434         [ +  + ]:         802994 :             for (int i = 0; i < sslot.nvalues; i++)
                               1435                 :                :             {
                               1436                 :                :                 Datum       fresult;
                               1437                 :                : 
                               1438                 :         802836 :                 fcinfo->args[0].value = sslot.values[i];
                               1439                 :         802836 :                 fcinfo->isnull = false;
                               1440                 :         802836 :                 fresult = FunctionCallInvoke(fcinfo);
                               1441   [ +  -  +  + ]:         802836 :                 if (!fcinfo->isnull && DatumGetBool(fresult))
                               1442                 :           2507 :                     nmatch++;
                               1443                 :                :             }
                               1444                 :            158 :             hist_selec = ((double) nmatch) / ((double) sslot.nvalues);
                               1445                 :                : 
                               1446                 :                :             /*
                               1447                 :                :              * As above, clamp to a hundredth of the histogram resolution.
                               1448                 :                :              * This case is surely even less trustworthy than the normal one,
                               1449                 :                :              * so we shouldn't believe exact 0 or 1 selectivity.  (Maybe the
                               1450                 :                :              * clamp should be more restrictive in this case?)
                               1451                 :                :              */
                               1452                 :                :             {
                               1453                 :            158 :                 double      cutoff = 0.01 / (double) (sslot.nvalues - 1);
                               1454                 :                : 
                               1455         [ +  + ]:            158 :                 if (hist_selec < cutoff)
 2160 tgl@sss.pgh.pa.us        1456                 :GBC           3 :                     hist_selec = cutoff;
 2160 tgl@sss.pgh.pa.us        1457         [ +  + ]:CBC         155 :                 else if (hist_selec > 1.0 - cutoff)
 2160 tgl@sss.pgh.pa.us        1458                 :GBC           3 :                     hist_selec = 1.0 - cutoff;
                               1459                 :                :             }
                               1460                 :                :         }
                               1461                 :                : 
 3279 tgl@sss.pgh.pa.us        1462                 :CBC      148907 :         free_attstatsslot(&sslot);
                               1463                 :                :     }
                               1464                 :                : 
 7420                          1465                 :         234454 :     return hist_selec;
                               1466                 :                : }
                               1467                 :                : 
                               1468                 :                : /*
                               1469                 :                :  * Common wrapper function for the selectivity estimators that simply
                               1470                 :                :  * invoke scalarineqsel().
                               1471                 :                :  */
                               1472                 :                : static Datum
 3156                          1473                 :          39042 : scalarineqsel_wrapper(PG_FUNCTION_ARGS, bool isgt, bool iseq)
                               1474                 :                : {
 7639                          1475                 :          39042 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
 9116                          1476                 :          39042 :     Oid         operator = PG_GETARG_OID(1);
                               1477                 :          39042 :     List       *args = (List *) PG_GETARG_POINTER(2);
                               1478                 :          39042 :     int         varRelid = PG_GETARG_INT32(3);
 2160                          1479                 :          39042 :     Oid         collation = PG_GET_COLLATION();
                               1480                 :                :     VariableStatData vardata;
                               1481                 :                :     Node       *other;
                               1482                 :                :     bool        varonleft;
                               1483                 :                :     Datum       constval;
                               1484                 :                :     Oid         consttype;
                               1485                 :                :     double      selec;
                               1486                 :                : 
                               1487                 :                :     /*
                               1488                 :                :      * If expression is not variable op something or something op variable,
                               1489                 :                :      * then punt and return a default estimate.
                               1490                 :                :      */
 8113                          1491         [ +  + ]:          39042 :     if (!get_restriction_variable(root, args, varRelid,
                               1492                 :                :                                   &vardata, &other, &varonleft))
 9116                          1493                 :            462 :         PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
                               1494                 :                : 
                               1495                 :                :     /*
                               1496                 :                :      * Can't do anything useful if the something is not a constant, either.
                               1497                 :                :      */
 8831                          1498         [ +  + ]:          38580 :     if (!IsA(other, Const))
                               1499                 :                :     {
 8113                          1500         [ +  + ]:           2365 :         ReleaseVariableStats(vardata);
 8831                          1501                 :           2365 :         PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
                               1502                 :                :     }
                               1503                 :                : 
                               1504                 :                :     /*
                               1505                 :                :      * If the constant is NULL, assume operator is strict and return zero, ie,
                               1506                 :                :      * operator will never return TRUE.
                               1507                 :                :      */
                               1508         [ +  + ]:          36215 :     if (((Const *) other)->constisnull)
                               1509                 :                :     {
 8113                          1510         [ +  + ]:             55 :         ReleaseVariableStats(vardata);
 8831                          1511                 :             55 :         PG_RETURN_FLOAT8(0.0);
                               1512                 :                :     }
                               1513                 :          36160 :     constval = ((Const *) other)->constvalue;
                               1514                 :          36160 :     consttype = ((Const *) other)->consttype;
                               1515                 :                : 
                               1516                 :                :     /*
                               1517                 :                :      * Force the var to be on the left to simplify logic in scalarineqsel.
                               1518                 :                :      */
 3156                          1519         [ +  + ]:          36160 :     if (!varonleft)
                               1520                 :                :     {
 9116                          1521                 :            318 :         operator = get_commutator(operator);
                               1522         [ -  + ]:            318 :         if (!operator)
                               1523                 :                :         {
                               1524                 :                :             /* Use default selectivity (should we raise an error instead?) */
 8113 tgl@sss.pgh.pa.us        1525         [ #  # ]:UBC           0 :             ReleaseVariableStats(vardata);
 9116                          1526                 :              0 :             PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
                               1527                 :                :         }
 3156 tgl@sss.pgh.pa.us        1528                 :CBC         318 :         isgt = !isgt;
                               1529                 :                :     }
                               1530                 :                : 
                               1531                 :                :     /* The rest of the work is done by scalarineqsel(). */
 2160                          1532                 :          36160 :     selec = scalarineqsel(root, operator, isgt, iseq, collation,
                               1533                 :                :                           &vardata, constval, consttype);
                               1534                 :                : 
 8113                          1535         [ +  + ]:          36160 :     ReleaseVariableStats(vardata);
                               1536                 :                : 
 9129                          1537                 :          36160 :     PG_RETURN_FLOAT8((float8) selec);
                               1538                 :                : }
                               1539                 :                : 
                               1540                 :                : /*
                               1541                 :                :  *      scalarltsel     - Selectivity of "<" for scalars.
                               1542                 :                :  */
                               1543                 :                : Datum
 3156                          1544                 :          12240 : scalarltsel(PG_FUNCTION_ARGS)
                               1545                 :                : {
                               1546                 :          12240 :     return scalarineqsel_wrapper(fcinfo, false, false);
                               1547                 :                : }
                               1548                 :                : 
                               1549                 :                : /*
                               1550                 :                :  *      scalarlesel     - Selectivity of "<=" for scalars.
                               1551                 :                :  */
                               1552                 :                : Datum
                               1553                 :           3816 : scalarlesel(PG_FUNCTION_ARGS)
                               1554                 :                : {
                               1555                 :           3816 :     return scalarineqsel_wrapper(fcinfo, false, true);
                               1556                 :                : }
                               1557                 :                : 
                               1558                 :                : /*
                               1559                 :                :  *      scalargtsel     - Selectivity of ">" for scalars.
                               1560                 :                :  */
                               1561                 :                : Datum
                               1562                 :          12400 : scalargtsel(PG_FUNCTION_ARGS)
                               1563                 :                : {
                               1564                 :          12400 :     return scalarineqsel_wrapper(fcinfo, true, false);
                               1565                 :                : }
                               1566                 :                : 
                               1567                 :                : /*
                               1568                 :                :  *      scalargesel     - Selectivity of ">=" for scalars.
                               1569                 :                :  */
                               1570                 :                : Datum
                               1571                 :          10586 : scalargesel(PG_FUNCTION_ARGS)
                               1572                 :                : {
                               1573                 :          10586 :     return scalarineqsel_wrapper(fcinfo, true, true);
                               1574                 :                : }
                               1575                 :                : 
                               1576                 :                : /*
                               1577                 :                :  *      boolvarsel      - Selectivity of Boolean variable.
                               1578                 :                :  *
                               1579                 :                :  * This can actually be called on any boolean-valued expression.  If it
                               1580                 :                :  * involves only Vars of the specified relation, and if there are statistics
                               1581                 :                :  * about the Var or expression (the latter is possible if it's indexed) then
                               1582                 :                :  * we'll produce a real estimate; otherwise it's just a default.
                               1583                 :                :  */
                               1584                 :                : Selectivity
 3876                          1585                 :          46971 : boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
                               1586                 :                : {
                               1587                 :                :     VariableStatData vardata;
                               1588                 :                :     double      selec;
                               1589                 :                : 
                               1590                 :          46971 :     examine_variable(root, arg, varRelid, &vardata);
                               1591         [ +  + ]:          46971 :     if (HeapTupleIsValid(vardata.statsTuple))
                               1592                 :                :     {
                               1593                 :                :         /*
                               1594                 :                :          * A boolean variable V is equivalent to the clause V = 't', so we
                               1595                 :                :          * compute the selectivity as if that is what we have.
                               1596                 :                :          */
 2160                          1597                 :          24080 :         selec = var_eq_const(&vardata, BooleanEqualOperator, InvalidOid,
                               1598                 :                :                              BoolGetDatum(true), false, true, false);
                               1599                 :                :     }
  227 tgl@sss.pgh.pa.us        1600         [ +  + ]:GNC       22891 :     else if (is_funcclause(arg))
                               1601                 :                :     {
                               1602                 :                :         /*
                               1603                 :                :          * If we have no stats and it's a function call, estimate 0.3333333.
                               1604                 :                :          * This seems a pretty unprincipled choice, but Postgres has been
                               1605                 :                :          * using that estimate for function calls since 1992.  The hoariness
                               1606                 :                :          * of this behavior suggests that we should not be in too much hurry
                               1607                 :                :          * to use another value.
                               1608                 :                :          */
                               1609                 :          11379 :         selec = 0.3333333;
                               1610                 :                :     }
                               1611                 :                :     else
                               1612                 :                :     {
                               1613                 :                :         /* Otherwise, the default estimate is 0.5 */
 3876 tgl@sss.pgh.pa.us        1614                 :CBC       11512 :         selec = 0.5;
                               1615                 :                :     }
                               1616         [ +  + ]:          46971 :     ReleaseVariableStats(vardata);
                               1617                 :          46971 :     return selec;
                               1618                 :                : }
                               1619                 :                : 
                               1620                 :                : /*
                               1621                 :                :  *      booltestsel     - Selectivity of BooleanTest Node.
                               1622                 :                :  */
                               1623                 :                : Selectivity
 7639                          1624                 :            791 : booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
                               1625                 :                :             int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
                               1626                 :                : {
                               1627                 :                :     VariableStatData vardata;
                               1628                 :                :     double      selec;
                               1629                 :                : 
 8113                          1630                 :            791 :     examine_variable(root, arg, varRelid, &vardata);
                               1631                 :                : 
                               1632         [ +  + ]:            791 :     if (HeapTupleIsValid(vardata.statsTuple))
                               1633                 :                :     {
                               1634                 :                :         Form_pg_statistic stats;
                               1635                 :                :         double      freq_null;
                               1636                 :                :         AttStatsSlot sslot;
                               1637                 :                : 
 8113 tgl@sss.pgh.pa.us        1638                 :GBC          20 :         stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
 9080                          1639                 :             20 :         freq_null = stats->stanullfrac;
                               1640                 :                : 
 3279                          1641         [ +  + ]:             20 :         if (get_attstatsslot(&sslot, vardata.statsTuple,
                               1642                 :                :                              STATISTIC_KIND_MCV, InvalidOid,
                               1643                 :                :                              ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS)
                               1644         [ +  - ]:             10 :             && sslot.nnumbers > 0)
 9080                          1645                 :             10 :         {
                               1646                 :                :             double      freq_true;
                               1647                 :                :             double      freq_false;
                               1648                 :                : 
                               1649                 :                :             /*
                               1650                 :                :              * Get first MCV frequency and derive frequency for true.
                               1651                 :                :              */
 3279                          1652         [ -  + ]:             10 :             if (DatumGetBool(sslot.values[0]))
 3279 tgl@sss.pgh.pa.us        1653                 :UBC           0 :                 freq_true = sslot.numbers[0];
                               1654                 :                :             else
 3279 tgl@sss.pgh.pa.us        1655                 :GBC          10 :                 freq_true = 1.0 - sslot.numbers[0] - freq_null;
                               1656                 :                : 
                               1657                 :                :             /*
                               1658                 :                :              * Next derive frequency for false. Then use these as appropriate
                               1659                 :                :              * to derive frequency for each case.
                               1660                 :                :              */
 9080                          1661                 :             10 :             freq_false = 1.0 - freq_true - freq_null;
                               1662                 :                : 
 8599                          1663   [ -  -  +  -  :             10 :             switch (booltesttype)
                                           -  -  - ]
                               1664                 :                :             {
 8958 bruce@momjian.us         1665                 :UBC           0 :                 case IS_UNKNOWN:
                               1666                 :                :                     /* select only NULL values */
 9080 tgl@sss.pgh.pa.us        1667                 :              0 :                     selec = freq_null;
                               1668                 :              0 :                     break;
 8958 bruce@momjian.us         1669                 :              0 :                 case IS_NOT_UNKNOWN:
                               1670                 :                :                     /* select non-NULL values */
 9080 tgl@sss.pgh.pa.us        1671                 :              0 :                     selec = 1.0 - freq_null;
                               1672                 :              0 :                     break;
 8958 bruce@momjian.us         1673                 :GBC          10 :                 case IS_TRUE:
                               1674                 :                :                     /* select only TRUE values */
 9080 tgl@sss.pgh.pa.us        1675                 :             10 :                     selec = freq_true;
                               1676                 :             10 :                     break;
 8958 bruce@momjian.us         1677                 :UBC           0 :                 case IS_NOT_TRUE:
                               1678                 :                :                     /* select non-TRUE values */
 9080 tgl@sss.pgh.pa.us        1679                 :              0 :                     selec = 1.0 - freq_true;
                               1680                 :              0 :                     break;
 8958 bruce@momjian.us         1681                 :              0 :                 case IS_FALSE:
                               1682                 :                :                     /* select only FALSE values */
 9080 tgl@sss.pgh.pa.us        1683                 :              0 :                     selec = freq_false;
                               1684                 :              0 :                     break;
 8958 bruce@momjian.us         1685                 :              0 :                 case IS_NOT_FALSE:
                               1686                 :                :                     /* select non-FALSE values */
 9080 tgl@sss.pgh.pa.us        1687                 :              0 :                     selec = 1.0 - freq_false;
                               1688                 :              0 :                     break;
 8958 bruce@momjian.us         1689                 :              0 :                 default:
 8318 tgl@sss.pgh.pa.us        1690         [ #  # ]:              0 :                     elog(ERROR, "unrecognized booltesttype: %d",
                               1691                 :                :                          (int) booltesttype);
                               1692                 :                :                     selec = 0.0;    /* Keep compiler quiet */
                               1693                 :                :                     break;
                               1694                 :                :             }
                               1695                 :                : 
 3279 tgl@sss.pgh.pa.us        1696                 :GBC          10 :             free_attstatsslot(&sslot);
                               1697                 :                :         }
                               1698                 :                :         else
                               1699                 :                :         {
                               1700                 :                :             /*
                               1701                 :                :              * No most-common-value info available. Still have null fraction
                               1702                 :                :              * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
                               1703                 :                :              * for null fraction and assume a 50-50 split of TRUE and FALSE.
                               1704                 :                :              */
 8599                          1705   [ +  -  -  -  :             10 :             switch (booltesttype)
                                                 - ]
                               1706                 :                :             {
 8958 bruce@momjian.us         1707                 :             10 :                 case IS_UNKNOWN:
                               1708                 :                :                     /* select only NULL values */
 9080 tgl@sss.pgh.pa.us        1709                 :             10 :                     selec = freq_null;
                               1710                 :             10 :                     break;
 8958 bruce@momjian.us         1711                 :UBC           0 :                 case IS_NOT_UNKNOWN:
                               1712                 :                :                     /* select non-NULL values */
 9080 tgl@sss.pgh.pa.us        1713                 :              0 :                     selec = 1.0 - freq_null;
                               1714                 :              0 :                     break;
 8958 bruce@momjian.us         1715                 :              0 :                 case IS_TRUE:
                               1716                 :                :                 case IS_FALSE:
                               1717                 :                :                     /* Assume we select half of the non-NULL values */
 9080 tgl@sss.pgh.pa.us        1718                 :              0 :                     selec = (1.0 - freq_null) / 2.0;
                               1719                 :              0 :                     break;
 4668                          1720                 :              0 :                 case IS_NOT_TRUE:
                               1721                 :                :                 case IS_NOT_FALSE:
                               1722                 :                :                     /* Assume we select NULLs plus half of the non-NULLs */
                               1723                 :                :                     /* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
                               1724                 :              0 :                     selec = (freq_null + 1.0) / 2.0;
                               1725                 :              0 :                     break;
 8958 bruce@momjian.us         1726                 :              0 :                 default:
 8318 tgl@sss.pgh.pa.us        1727         [ #  # ]:              0 :                     elog(ERROR, "unrecognized booltesttype: %d",
                               1728                 :                :                          (int) booltesttype);
                               1729                 :                :                     selec = 0.0;    /* Keep compiler quiet */
                               1730                 :                :                     break;
                               1731                 :                :             }
                               1732                 :                :         }
                               1733                 :                :     }
                               1734                 :                :     else
                               1735                 :                :     {
                               1736                 :                :         /*
                               1737                 :                :          * If we can't get variable statistics for the argument, perhaps
                               1738                 :                :          * clause_selectivity can do something with it.  We ignore the
                               1739                 :                :          * possibility of a NULL value when using clause_selectivity, and just
                               1740                 :                :          * assume the value is either TRUE or FALSE.
                               1741                 :                :          */
 8599 tgl@sss.pgh.pa.us        1742   [ +  +  +  +  :CBC         771 :         switch (booltesttype)
                                                 - ]
                               1743                 :                :         {
 9080                          1744                 :             40 :             case IS_UNKNOWN:
                               1745                 :             40 :                 selec = DEFAULT_UNK_SEL;
                               1746                 :             40 :                 break;
                               1747                 :             90 :             case IS_NOT_UNKNOWN:
                               1748                 :             90 :                 selec = DEFAULT_NOT_UNK_SEL;
                               1749                 :             90 :                 break;
                               1750                 :            220 :             case IS_TRUE:
                               1751                 :                :             case IS_NOT_FALSE:
 8113                          1752                 :            220 :                 selec = (double) clause_selectivity(root, arg,
                               1753                 :                :                                                     varRelid,
                               1754                 :                :                                                     jointype, sjinfo);
                               1755                 :            220 :                 break;
                               1756                 :            421 :             case IS_FALSE:
                               1757                 :                :             case IS_NOT_TRUE:
                               1758                 :            421 :                 selec = 1.0 - (double) clause_selectivity(root, arg,
                               1759                 :                :                                                           varRelid,
                               1760                 :                :                                                           jointype, sjinfo);
 9080                          1761                 :            421 :                 break;
 9080 tgl@sss.pgh.pa.us        1762                 :UBC           0 :             default:
 8318                          1763         [ #  # ]:              0 :                 elog(ERROR, "unrecognized booltesttype: %d",
                               1764                 :                :                      (int) booltesttype);
                               1765                 :                :                 selec = 0.0;    /* Keep compiler quiet */
                               1766                 :                :                 break;
                               1767                 :                :         }
                               1768                 :                :     }
                               1769                 :                : 
 8113 tgl@sss.pgh.pa.us        1770         [ +  + ]:CBC         791 :     ReleaseVariableStats(vardata);
                               1771                 :                : 
                               1772                 :                :     /* result should be in range, but make sure... */
 8888                          1773   [ -  +  -  + ]:            791 :     CLAMP_PROBABILITY(selec);
                               1774                 :                : 
 9080                          1775                 :            791 :     return (Selectivity) selec;
                               1776                 :                : }
                               1777                 :                : 
                               1778                 :                : /*
                               1779                 :                :  *      nulltestsel     - Selectivity of NullTest Node.
                               1780                 :                :  */
                               1781                 :                : Selectivity
 6473                          1782                 :          14453 : nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg,
                               1783                 :                :             int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
                               1784                 :                : {
                               1785                 :                :     VariableStatData vardata;
                               1786                 :                :     double      selec;
                               1787                 :                : 
 8113                          1788                 :          14453 :     examine_variable(root, arg, varRelid, &vardata);
                               1789                 :                : 
                               1790         [ +  + ]:          14453 :     if (HeapTupleIsValid(vardata.statsTuple))
                               1791                 :                :     {
                               1792                 :                :         Form_pg_statistic stats;
                               1793                 :                :         double      freq_null;
                               1794                 :                : 
                               1795                 :           7786 :         stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
 9080                          1796                 :           7786 :         freq_null = stats->stanullfrac;
                               1797                 :                : 
 8599                          1798      [ +  +  - ]:           7786 :         switch (nulltesttype)
                               1799                 :                :         {
 8958 bruce@momjian.us         1800                 :           5976 :             case IS_NULL:
                               1801                 :                : 
                               1802                 :                :                 /*
                               1803                 :                :                  * Use freq_null directly.
                               1804                 :                :                  */
 9080 tgl@sss.pgh.pa.us        1805                 :           5976 :                 selec = freq_null;
                               1806                 :           5976 :                 break;
 8958 bruce@momjian.us         1807                 :           1810 :             case IS_NOT_NULL:
                               1808                 :                : 
                               1809                 :                :                 /*
                               1810                 :                :                  * Select not unknown (not null) values. Calculate from
                               1811                 :                :                  * freq_null.
                               1812                 :                :                  */
 9080 tgl@sss.pgh.pa.us        1813                 :           1810 :                 selec = 1.0 - freq_null;
                               1814                 :           1810 :                 break;
 8958 bruce@momjian.us         1815                 :UBC           0 :             default:
 8318 tgl@sss.pgh.pa.us        1816         [ #  # ]:              0 :                 elog(ERROR, "unrecognized nulltesttype: %d",
                               1817                 :                :                      (int) nulltesttype);
                               1818                 :                :                 return (Selectivity) 0; /* keep compiler quiet */
                               1819                 :                :         }
                               1820                 :                :     }
 2657 tgl@sss.pgh.pa.us        1821   [ +  -  +  + ]:CBC        6667 :     else if (vardata.var && IsA(vardata.var, Var) &&
                               1822         [ +  + ]:           6137 :              ((Var *) vardata.var)->varattno < 0)
                               1823                 :                :     {
                               1824                 :                :         /*
                               1825                 :                :          * There are no stats for system columns, but we know they are never
                               1826                 :                :          * NULL.
                               1827                 :                :          */
                               1828         [ +  - ]:             89 :         selec = (nulltesttype == IS_NULL) ? 0.0 : 1.0;
                               1829                 :                :     }
                               1830                 :                :     else
                               1831                 :                :     {
                               1832                 :                :         /*
                               1833                 :                :          * No ANALYZE stats available, so make a guess
                               1834                 :                :          */
 8113                          1835      [ +  +  - ]:           6578 :         switch (nulltesttype)
                               1836                 :                :         {
                               1837                 :           1648 :             case IS_NULL:
                               1838                 :           1648 :                 selec = DEFAULT_UNK_SEL;
                               1839                 :           1648 :                 break;
                               1840                 :           4930 :             case IS_NOT_NULL:
                               1841                 :           4930 :                 selec = DEFAULT_NOT_UNK_SEL;
                               1842                 :           4930 :                 break;
 8113 tgl@sss.pgh.pa.us        1843                 :UBC           0 :             default:
                               1844         [ #  # ]:              0 :                 elog(ERROR, "unrecognized nulltesttype: %d",
                               1845                 :                :                      (int) nulltesttype);
                               1846                 :                :                 return (Selectivity) 0; /* keep compiler quiet */
                               1847                 :                :         }
                               1848                 :                :     }
                               1849                 :                : 
 8113 tgl@sss.pgh.pa.us        1850         [ +  + ]:CBC       14453 :     ReleaseVariableStats(vardata);
                               1851                 :                : 
                               1852                 :                :     /* result should be in range, but make sure... */
 8888                          1853   [ -  +  -  + ]:          14453 :     CLAMP_PROBABILITY(selec);
                               1854                 :                : 
 9080                          1855                 :          14453 :     return (Selectivity) selec;
                               1856                 :                : }
                               1857                 :                : 
                               1858                 :                : /*
                               1859                 :                :  * strip_array_coercion - strip binary-compatible relabeling from an array expr
                               1860                 :                :  *
                               1861                 :                :  * For array values, the parser normally generates ArrayCoerceExpr conversions,
                               1862                 :                :  * but it seems possible that RelabelType might show up.  Also, the planner
                               1863                 :                :  * is not currently tense about collapsing stacked ArrayCoerceExpr nodes,
                               1864                 :                :  * so we need to be ready to deal with more than one level.
                               1865                 :                :  */
                               1866                 :                : static Node *
 7037                          1867                 :         106231 : strip_array_coercion(Node *node)
                               1868                 :                : {
                               1869                 :                :     for (;;)
                               1870                 :                :     {
 3139                          1871   [ +  -  +  + ]:         106319 :         if (node && IsA(node, ArrayCoerceExpr))
 7037                          1872                 :             88 :         {
 3139                          1873                 :           1968 :             ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
                               1874                 :                : 
                               1875                 :                :             /*
                               1876                 :                :              * If the per-element expression is just a RelabelType on top of
                               1877                 :                :              * CaseTestExpr, then we know it's a binary-compatible relabeling.
                               1878                 :                :              */
                               1879         [ +  + ]:           1968 :             if (IsA(acoerce->elemexpr, RelabelType) &&
                               1880         [ +  - ]:             88 :                 IsA(((RelabelType *) acoerce->elemexpr)->arg, CaseTestExpr))
                               1881                 :             88 :                 node = (Node *) acoerce->arg;
                               1882                 :                :             else
                               1883                 :                :                 break;
                               1884                 :                :         }
 6979                          1885   [ +  -  -  + ]:         104351 :         else if (node && IsA(node, RelabelType))
                               1886                 :                :         {
                               1887                 :                :             /* We don't really expect this case, but may as well cope */
 6979 tgl@sss.pgh.pa.us        1888                 :UBC           0 :             node = (Node *) ((RelabelType *) node)->arg;
                               1889                 :                :         }
                               1890                 :                :         else
                               1891                 :                :             break;
                               1892                 :                :     }
 7037 tgl@sss.pgh.pa.us        1893                 :CBC      106231 :     return node;
                               1894                 :                : }
                               1895                 :                : 
                               1896                 :                : /*
                               1897                 :                :  *      scalararraysel      - Selectivity of ScalarArrayOpExpr Node.
                               1898                 :                :  */
                               1899                 :                : Selectivity
 7466                          1900                 :          18468 : scalararraysel(PlannerInfo *root,
                               1901                 :                :                ScalarArrayOpExpr *clause,
                               1902                 :                :                bool is_join_clause,
                               1903                 :                :                int varRelid,
                               1904                 :                :                JoinType jointype,
                               1905                 :                :                SpecialJoinInfo *sjinfo)
                               1906                 :                : {
                               1907                 :          18468 :     Oid         operator = clause->opno;
                               1908                 :          18468 :     bool        useOr = clause->useOr;
 5176                          1909                 :          18468 :     bool        isEquality = false;
                               1910                 :          18468 :     bool        isInequality = false;
                               1911                 :                :     Node       *leftop;
                               1912                 :                :     Node       *rightop;
                               1913                 :                :     Oid         nominal_element_type;
                               1914                 :                :     Oid         nominal_element_collation;
                               1915                 :                :     TypeCacheEntry *typentry;
                               1916                 :                :     RegProcedure oprsel;
                               1917                 :                :     FmgrInfo    oprselproc;
                               1918                 :                :     Selectivity s1;
                               1919                 :                :     Selectivity s1disjoint;
                               1920                 :                : 
                               1921                 :                :     /* First, deconstruct the expression */
 7037                          1922         [ -  + ]:          18468 :     Assert(list_length(clause->args) == 2);
                               1923                 :          18468 :     leftop = (Node *) linitial(clause->args);
                               1924                 :          18468 :     rightop = (Node *) lsecond(clause->args);
                               1925                 :                : 
                               1926                 :                :     /* aggressively reduce both sides to constants */
 4456                          1927                 :          18468 :     leftop = estimate_expression_value(root, leftop);
                               1928                 :          18468 :     rightop = estimate_expression_value(root, rightop);
                               1929                 :                : 
                               1930                 :                :     /* get nominal (after relabeling) element type of rightop */
 5675                          1931                 :          18468 :     nominal_element_type = get_base_element_type(exprType(rightop));
 7037                          1932         [ -  + ]:          18468 :     if (!OidIsValid(nominal_element_type))
 3240 tgl@sss.pgh.pa.us        1933                 :UBC           0 :         return (Selectivity) 0.5;   /* probably shouldn't happen */
                               1934                 :                :     /* get nominal collation, too, for generating constants */
 5520 tgl@sss.pgh.pa.us        1935                 :CBC       18468 :     nominal_element_collation = exprCollation(rightop);
                               1936                 :                : 
                               1937                 :                :     /* look through any binary-compatible relabeling of rightop */
 7037                          1938                 :          18468 :     rightop = strip_array_coercion(rightop);
                               1939                 :                : 
                               1940                 :                :     /*
                               1941                 :                :      * Detect whether the operator is the default equality or inequality
                               1942                 :                :      * operator of the array element type.
                               1943                 :                :      */
 5176                          1944                 :          18468 :     typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
                               1945         [ +  + ]:          18468 :     if (OidIsValid(typentry->eq_opr))
                               1946                 :                :     {
                               1947         [ +  + ]:          18466 :         if (operator == typentry->eq_opr)
                               1948                 :          16000 :             isEquality = true;
                               1949         [ +  + ]:           2466 :         else if (get_negator(operator) == typentry->eq_opr)
                               1950                 :           1988 :             isInequality = true;
                               1951                 :                :     }
                               1952                 :                : 
                               1953                 :                :     /*
                               1954                 :                :      * If it is equality or inequality, we might be able to estimate this as a
                               1955                 :                :      * form of array containment; for instance "const = ANY(column)" can be
                               1956                 :                :      * treated as "ARRAY[const] <@ column".  scalararraysel_containment tries
                               1957                 :                :      * that, and returns the selectivity estimate if successful, or -1 if not.
                               1958                 :                :      */
                               1959   [ +  +  +  +  :          18468 :     if ((isEquality || isInequality) && !is_join_clause)
                                              +  + ]
                               1960                 :                :     {
                               1961                 :          17987 :         s1 = scalararraysel_containment(root, leftop, rightop,
                               1962                 :                :                                         nominal_element_type,
                               1963                 :                :                                         isEquality, useOr, varRelid);
                               1964         [ +  + ]:          17987 :         if (s1 >= 0.0)
                               1965                 :             94 :             return s1;
                               1966                 :                :     }
                               1967                 :                : 
                               1968                 :                :     /*
                               1969                 :                :      * Look up the underlying operator's selectivity estimator. Punt if it
                               1970                 :                :      * hasn't got one.
                               1971                 :                :      */
                               1972         [ +  + ]:          18374 :     if (is_join_clause)
                               1973                 :              1 :         oprsel = get_oprjoin(operator);
                               1974                 :                :     else
                               1975                 :          18373 :         oprsel = get_oprrest(operator);
                               1976         [ +  + ]:          18374 :     if (!oprsel)
                               1977                 :              2 :         return (Selectivity) 0.5;
                               1978                 :          18372 :     fmgr_info(oprsel, &oprselproc);
                               1979                 :                : 
                               1980                 :                :     /*
                               1981                 :                :      * In the array-containment check above, we must only believe that an
                               1982                 :                :      * operator is equality or inequality if it is the default btree equality
                               1983                 :                :      * operator (or its negator) for the element type, since those are the
                               1984                 :                :      * operators that array containment will use.  But in what follows, we can
                               1985                 :                :      * be a little laxer, and also believe that any operators using eqsel() or
                               1986                 :                :      * neqsel() as selectivity estimator act like equality or inequality.
                               1987                 :                :      */
 5172                          1988   [ +  +  +  + ]:          18372 :     if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
                               1989                 :          16055 :         isEquality = true;
                               1990   [ +  +  -  + ]:           2317 :     else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
                               1991                 :           1917 :         isInequality = true;
                               1992                 :                : 
                               1993                 :                :     /*
                               1994                 :                :      * We consider three cases:
                               1995                 :                :      *
                               1996                 :                :      * 1. rightop is an Array constant: deconstruct the array, apply the
                               1997                 :                :      * operator's selectivity function for each array element, and merge the
                               1998                 :                :      * results in the same way that clausesel.c does for AND/OR combinations.
                               1999                 :                :      *
                               2000                 :                :      * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
                               2001                 :                :      * function for each element of the ARRAY[] construct, and merge.
                               2002                 :                :      *
                               2003                 :                :      * 3. otherwise, make a guess ...
                               2004                 :                :      */
 7466                          2005   [ +  -  +  + ]:          18372 :     if (rightop && IsA(rightop, Const))
                               2006                 :          14825 :     {
                               2007                 :          14855 :         Datum       arraydatum = ((Const *) rightop)->constvalue;
                               2008                 :          14855 :         bool        arrayisnull = ((Const *) rightop)->constisnull;
                               2009                 :                :         ArrayType  *arrayval;
                               2010                 :                :         int16       elmlen;
                               2011                 :                :         bool        elmbyval;
                               2012                 :                :         char        elmalign;
                               2013                 :                :         int         num_elems;
                               2014                 :                :         Datum      *elem_values;
                               2015                 :                :         bool       *elem_nulls;
                               2016                 :                :         int         i;
                               2017                 :                : 
                               2018         [ +  + ]:          14855 :         if (arrayisnull)        /* qual can't succeed if null array */
                               2019                 :             30 :             return (Selectivity) 0.0;
                               2020                 :          14830 :         arrayval = DatumGetArrayTypeP(arraydatum);
                               2021                 :                : 
                               2022                 :                :         /*
                               2023                 :                :          * When the array contains a NULL constant, same as var_eq_const, we
                               2024                 :                :          * assume the operator is strict and nothing will match, thus return
                               2025                 :                :          * 0.0.
                               2026                 :                :          */
   47 drowley@postgresql.o     2027   [ +  +  +  + ]:GNC       14830 :         if (!useOr && array_contains_nulls(arrayval))
                               2028                 :              5 :             return (Selectivity) 0.0;
                               2029                 :                : 
 7466 tgl@sss.pgh.pa.us        2030                 :CBC       14825 :         get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
                               2031                 :                :                              &elmlen, &elmbyval, &elmalign);
                               2032                 :          14825 :         deconstruct_array(arrayval,
                               2033                 :                :                           ARR_ELEMTYPE(arrayval),
                               2034                 :                :                           elmlen, elmbyval, elmalign,
                               2035                 :                :                           &elem_values, &elem_nulls, &num_elems);
                               2036                 :                : 
                               2037                 :                :         /*
                               2038                 :                :          * For generic operators, we assume the probability of success is
                               2039                 :                :          * independent for each array element.  But for "= ANY" or "<> ALL",
                               2040                 :                :          * if the array elements are distinct (which'd typically be the case)
                               2041                 :                :          * then the probabilities are disjoint, and we should just sum them.
                               2042                 :                :          *
                               2043                 :                :          * If we were being really tense we would try to confirm that the
                               2044                 :                :          * elements are all distinct, but that would be expensive and it
                               2045                 :                :          * doesn't seem to be worth the cycles; it would amount to penalizing
                               2046                 :                :          * well-written queries in favor of poorly-written ones.  However, we
                               2047                 :                :          * do protect ourselves a little bit by checking whether the
                               2048                 :                :          * disjointness assumption leads to an impossible (out of range)
                               2049                 :                :          * probability; if so, we fall back to the normal calculation.
                               2050                 :                :          */
 5172                          2051         [ +  + ]:          14825 :         s1 = s1disjoint = (useOr ? 0.0 : 1.0);
                               2052                 :                : 
 7466                          2053         [ +  + ]:          57605 :         for (i = 0; i < num_elems; i++)
                               2054                 :                :         {
                               2055                 :                :             List       *args;
                               2056                 :                :             Selectivity s2;
                               2057                 :                : 
                               2058                 :          42780 :             args = list_make2(leftop,
                               2059                 :                :                               makeConst(nominal_element_type,
                               2060                 :                :                                         -1,
                               2061                 :                :                                         nominal_element_collation,
                               2062                 :                :                                         elmlen,
                               2063                 :                :                                         elem_values[i],
                               2064                 :                :                                         elem_nulls[i],
                               2065                 :                :                                         elmbyval));
 6471                          2066         [ -  + ]:          42780 :             if (is_join_clause)
 5049 tgl@sss.pgh.pa.us        2067                 :UBC           0 :                 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
                               2068                 :                :                                                       clause->inputcollid,
                               2069                 :                :                                                       PointerGetDatum(root),
                               2070                 :                :                                                       ObjectIdGetDatum(operator),
                               2071                 :                :                                                       PointerGetDatum(args),
                               2072                 :                :                                                       Int16GetDatum(jointype),
                               2073                 :                :                                                       PointerGetDatum(sjinfo)));
                               2074                 :                :             else
 5049 tgl@sss.pgh.pa.us        2075                 :CBC       42780 :                 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
                               2076                 :                :                                                       clause->inputcollid,
                               2077                 :                :                                                       PointerGetDatum(root),
                               2078                 :                :                                                       ObjectIdGetDatum(operator),
                               2079                 :                :                                                       PointerGetDatum(args),
                               2080                 :                :                                                       Int32GetDatum(varRelid)));
                               2081                 :                : 
 7466                          2082         [ +  + ]:          42780 :             if (useOr)
                               2083                 :                :             {
                               2084                 :          36744 :                 s1 = s1 + s2 - s1 * s2;
 5172                          2085         [ +  + ]:          36744 :                 if (isEquality)
                               2086                 :          35874 :                     s1disjoint += s2;
                               2087                 :                :             }
                               2088                 :                :             else
                               2089                 :                :             {
 7466                          2090                 :           6036 :                 s1 = s1 * s2;
 5172                          2091         [ +  + ]:           6036 :                 if (isInequality)
                               2092                 :           5776 :                     s1disjoint += s2 - 1.0;
                               2093                 :                :             }
                               2094                 :                :         }
                               2095                 :                : 
                               2096                 :                :         /* accept disjoint-probability estimate if in range */
                               2097   [ +  +  +  +  :          14825 :         if ((useOr ? isEquality : isInequality) &&
                                              +  + ]
                               2098         [ +  + ]:          14319 :             s1disjoint >= 0.0 && s1disjoint <= 1.0)
                               2099                 :          14292 :             s1 = s1disjoint;
                               2100                 :                :     }
 7466                          2101   [ +  -  +  + ]:           3517 :     else if (rightop && IsA(rightop, ArrayExpr) &&
                               2102         [ +  - ]:            314 :              !((ArrayExpr *) rightop)->multidims)
                               2103                 :            309 :     {
                               2104                 :            314 :         ArrayExpr  *arrayexpr = (ArrayExpr *) rightop;
                               2105                 :                :         int16       elmlen;
                               2106                 :                :         bool        elmbyval;
                               2107                 :                :         ListCell   *l;
                               2108                 :                : 
                               2109                 :            314 :         get_typlenbyval(arrayexpr->element_typeid,
                               2110                 :                :                         &elmlen, &elmbyval);
                               2111                 :                : 
                               2112                 :                :         /*
                               2113                 :                :          * We use the assumption of disjoint probabilities here too, although
                               2114                 :                :          * the odds of equal array elements are rather higher if the elements
                               2115                 :                :          * are not all constants (which they won't be, else constant folding
                               2116                 :                :          * would have reduced the ArrayExpr to a Const).  In this path it's
                               2117                 :                :          * critical to have the sanity check on the s1disjoint estimate.
                               2118                 :                :          */
 5172                          2119         [ +  + ]:            314 :         s1 = s1disjoint = (useOr ? 0.0 : 1.0);
                               2120                 :                : 
 7466                          2121   [ +  -  +  +  :           1147 :         foreach(l, arrayexpr->elements)
                                              +  + ]
                               2122                 :                :         {
 7037                          2123                 :            838 :             Node       *elem = (Node *) lfirst(l);
                               2124                 :                :             List       *args;
                               2125                 :                :             Selectivity s2;
                               2126                 :                : 
                               2127                 :                :             /*
                               2128                 :                :              * When the array contains a NULL constant, same as var_eq_const,
                               2129                 :                :              * we assume the operator is strict and nothing will match, thus
                               2130                 :                :              * return 0.0.
                               2131                 :                :              */
   47 drowley@postgresql.o     2132   [ +  +  +  +  :GNC         838 :             if (!useOr && IsA(elem, Const) && ((Const *) elem)->constisnull)
                                              +  + ]
                               2133                 :              5 :                 return (Selectivity) 0.0;
                               2134                 :                : 
                               2135                 :                :             /*
                               2136                 :                :              * Theoretically, if elem isn't of nominal_element_type we should
                               2137                 :                :              * insert a RelabelType, but it seems unlikely that any operator
                               2138                 :                :              * estimation function would really care ...
                               2139                 :                :              */
 7037 tgl@sss.pgh.pa.us        2140                 :CBC         833 :             args = list_make2(leftop, elem);
 6471                          2141         [ +  + ]:            833 :             if (is_join_clause)
 5049                          2142                 :              3 :                 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
                               2143                 :                :                                                       clause->inputcollid,
                               2144                 :                :                                                       PointerGetDatum(root),
                               2145                 :                :                                                       ObjectIdGetDatum(operator),
                               2146                 :                :                                                       PointerGetDatum(args),
                               2147                 :                :                                                       Int16GetDatum(jointype),
                               2148                 :                :                                                       PointerGetDatum(sjinfo)));
                               2149                 :                :             else
                               2150                 :            830 :                 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
                               2151                 :                :                                                       clause->inputcollid,
                               2152                 :                :                                                       PointerGetDatum(root),
                               2153                 :                :                                                       ObjectIdGetDatum(operator),
                               2154                 :                :                                                       PointerGetDatum(args),
                               2155                 :                :                                                       Int32GetDatum(varRelid)));
                               2156                 :                : 
 7466                          2157         [ +  + ]:            833 :             if (useOr)
                               2158                 :                :             {
                               2159                 :            813 :                 s1 = s1 + s2 - s1 * s2;
 5172                          2160         [ +  - ]:            813 :                 if (isEquality)
                               2161                 :            813 :                     s1disjoint += s2;
                               2162                 :                :             }
                               2163                 :                :             else
                               2164                 :                :             {
 7466 tgl@sss.pgh.pa.us        2165                 :GBC          20 :                 s1 = s1 * s2;
 5172                          2166         [ +  - ]:             20 :                 if (isInequality)
                               2167                 :             20 :                     s1disjoint += s2 - 1.0;
                               2168                 :                :             }
                               2169                 :                :         }
                               2170                 :                : 
                               2171                 :                :         /* accept disjoint-probability estimate if in range */
 5172 tgl@sss.pgh.pa.us        2172   [ +  -  +  -  :CBC         309 :         if ((useOr ? isEquality : isInequality) &&
                                              +  - ]
                               2173         [ +  - ]:            309 :             s1disjoint >= 0.0 && s1disjoint <= 1.0)
                               2174                 :            309 :             s1 = s1disjoint;
                               2175                 :                :     }
                               2176                 :                :     else
                               2177                 :                :     {
                               2178                 :                :         CaseTestExpr *dummyexpr;
                               2179                 :                :         List       *args;
                               2180                 :                :         Selectivity s2;
                               2181                 :                :         int         i;
                               2182                 :                : 
                               2183                 :                :         /*
                               2184                 :                :          * We need a dummy rightop to pass to the operator selectivity
                               2185                 :                :          * routine.  It can be pretty much anything that doesn't look like a
                               2186                 :                :          * constant; CaseTestExpr is a convenient choice.
                               2187                 :                :          */
 7466                          2188                 :           3203 :         dummyexpr = makeNode(CaseTestExpr);
 7037                          2189                 :           3203 :         dummyexpr->typeId = nominal_element_type;
 7466                          2190                 :           3203 :         dummyexpr->typeMod = -1;
 5526                          2191                 :           3203 :         dummyexpr->collation = clause->inputcollid;
 7466                          2192                 :           3203 :         args = list_make2(leftop, dummyexpr);
 6471                          2193         [ -  + ]:           3203 :         if (is_join_clause)
 5049 tgl@sss.pgh.pa.us        2194                 :UBC           0 :             s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
                               2195                 :                :                                                   clause->inputcollid,
                               2196                 :                :                                                   PointerGetDatum(root),
                               2197                 :                :                                                   ObjectIdGetDatum(operator),
                               2198                 :                :                                                   PointerGetDatum(args),
                               2199                 :                :                                                   Int16GetDatum(jointype),
                               2200                 :                :                                                   PointerGetDatum(sjinfo)));
                               2201                 :                :         else
 5049 tgl@sss.pgh.pa.us        2202                 :CBC        3203 :             s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
                               2203                 :                :                                                   clause->inputcollid,
                               2204                 :                :                                                   PointerGetDatum(root),
                               2205                 :                :                                                   ObjectIdGetDatum(operator),
                               2206                 :                :                                                   PointerGetDatum(args),
                               2207                 :                :                                                   Int32GetDatum(varRelid)));
 7466                          2208         [ +  - ]:           3203 :         s1 = useOr ? 0.0 : 1.0;
                               2209                 :                : 
                               2210                 :                :         /*
                               2211                 :                :          * Arbitrarily assume 10 elements in the eventual array value (see
                               2212                 :                :          * also estimate_array_length).  We don't risk an assumption of
                               2213                 :                :          * disjoint probabilities here.
                               2214                 :                :          */
                               2215         [ +  + ]:          35233 :         for (i = 0; i < 10; i++)
                               2216                 :                :         {
                               2217         [ +  - ]:          32030 :             if (useOr)
                               2218                 :          32030 :                 s1 = s1 + s2 - s1 * s2;
                               2219                 :                :             else
 7466 tgl@sss.pgh.pa.us        2220                 :UBC           0 :                 s1 = s1 * s2;
                               2221                 :                :         }
                               2222                 :                :     }
                               2223                 :                : 
                               2224                 :                :     /* result should be in range, but make sure... */
 7466 tgl@sss.pgh.pa.us        2225   [ -  +  -  + ]:CBC       18337 :     CLAMP_PROBABILITY(s1);
                               2226                 :                : 
                               2227                 :          18337 :     return s1;
                               2228                 :                : }
                               2229                 :                : 
                               2230                 :                : /*
                               2231                 :                :  * Estimate number of elements in the array yielded by an expression.
                               2232                 :                :  *
                               2233                 :                :  * Note: the result is integral, but we use "double" to avoid overflow
                               2234                 :                :  * concerns.  Most callers will use it in double-type expressions anyway.
                               2235                 :                :  *
                               2236                 :                :  * Note: in some code paths root can be passed as NULL, resulting in
                               2237                 :                :  * slightly worse estimates.
                               2238                 :                :  */
                               2239                 :                : double
  852                          2240                 :          87763 : estimate_array_length(PlannerInfo *root, Node *arrayexpr)
                               2241                 :                : {
                               2242                 :                :     /* look through any binary-compatible relabeling of arrayexpr */
 7037                          2243                 :          87763 :     arrayexpr = strip_array_coercion(arrayexpr);
                               2244                 :                : 
 7248                          2245   [ +  -  +  + ]:          87763 :     if (arrayexpr && IsA(arrayexpr, Const))
                               2246                 :                :     {
                               2247                 :          38740 :         Datum       arraydatum = ((Const *) arrayexpr)->constvalue;
                               2248                 :          38740 :         bool        arrayisnull = ((Const *) arrayexpr)->constisnull;
                               2249                 :                :         ArrayType  *arrayval;
                               2250                 :                : 
                               2251         [ +  + ]:          38740 :         if (arrayisnull)
                               2252                 :             75 :             return 0;
                               2253                 :          38665 :         arrayval = DatumGetArrayTypeP(arraydatum);
                               2254                 :          38665 :         return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
                               2255                 :                :     }
                               2256   [ +  -  +  + ]:          49023 :     else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
                               2257         [ +  - ]:            548 :              !((ArrayExpr *) arrayexpr)->multidims)
                               2258                 :                :     {
                               2259                 :            548 :         return list_length(((ArrayExpr *) arrayexpr)->elements);
                               2260                 :                :     }
  573                          2261   [ +  -  +  + ]:          48475 :     else if (arrayexpr && root)
                               2262                 :                :     {
                               2263                 :                :         /* See if we can find any statistics about it */
                               2264                 :                :         VariableStatData vardata;
                               2265                 :                :         AttStatsSlot sslot;
  852                          2266                 :          48455 :         double      nelem = 0;
                               2267                 :                : 
                               2268                 :                :         /*
                               2269                 :                :          * Skip calling examine_variable for Var with varno 0, which has no
                               2270                 :                :          * valid relation entry and would error in find_base_rel.  Such a Var
                               2271                 :                :          * can appear when a nested set operation's output type doesn't match
                               2272                 :                :          * the parent's expected type, because recurse_set_operations builds a
                               2273                 :                :          * projection target list using generate_setop_tlist with varno 0, and
                               2274                 :                :          * if the required type coercion involves an ArrayCoerceExpr, we can
                               2275                 :                :          * be called on that Var.
                               2276                 :                :          */
   24 rguo@postgresql.org      2277   [ +  +  +  + ]:          48455 :         if (IsA(arrayexpr, Var) && ((Var *) arrayexpr)->varno == 0)
                               2278                 :          11693 :             return 10;          /* default guess, should match scalararraysel */
                               2279                 :                : 
  852 tgl@sss.pgh.pa.us        2280                 :          48450 :         examine_variable(root, arrayexpr, 0, &vardata);
                               2281         [ +  + ]:          48450 :         if (HeapTupleIsValid(vardata.statsTuple))
                               2282                 :                :         {
                               2283                 :                :             /*
                               2284                 :                :              * Found stats, so use the average element count, which is stored
                               2285                 :                :              * in the last stanumbers element of the DECHIST statistics.
                               2286                 :                :              * Actually that is the average count of *distinct* elements;
                               2287                 :                :              * perhaps we should scale it up somewhat?
                               2288                 :                :              */
                               2289         [ +  + ]:          11783 :             if (get_attstatsslot(&sslot, vardata.statsTuple,
                               2290                 :                :                                  STATISTIC_KIND_DECHIST, InvalidOid,
                               2291                 :                :                                  ATTSTATSSLOT_NUMBERS))
                               2292                 :                :             {
                               2293         [ +  - ]:          11688 :                 if (sslot.nnumbers > 0)
                               2294                 :          11688 :                     nelem = clamp_row_est(sslot.numbers[sslot.nnumbers - 1]);
                               2295                 :          11688 :                 free_attstatsslot(&sslot);
                               2296                 :                :             }
                               2297                 :                :         }
                               2298         [ +  + ]:          48450 :         ReleaseVariableStats(vardata);
                               2299                 :                : 
                               2300         [ +  + ]:          48450 :         if (nelem > 0)
                               2301                 :          11688 :             return nelem;
                               2302                 :                :     }
                               2303                 :                : 
                               2304                 :                :     /* Else use a default guess --- this should match scalararraysel */
                               2305                 :          36782 :     return 10;
                               2306                 :                : }
                               2307                 :                : 
                               2308                 :                : /*
                               2309                 :                :  *      rowcomparesel       - Selectivity of RowCompareExpr Node.
                               2310                 :                :  *
                               2311                 :                :  * We estimate RowCompare selectivity by considering just the first (high
                               2312                 :                :  * order) columns, which makes it equivalent to an ordinary OpExpr.  While
                               2313                 :                :  * this estimate could be refined by considering additional columns, it
                               2314                 :                :  * seems unlikely that we could do a lot better without multi-column
                               2315                 :                :  * statistics.
                               2316                 :                :  */
                               2317                 :                : Selectivity
 7416                          2318                 :            240 : rowcomparesel(PlannerInfo *root,
                               2319                 :                :               RowCompareExpr *clause,
                               2320                 :                :               int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
                               2321                 :                : {
                               2322                 :                :     Selectivity s1;
                               2323                 :            240 :     Oid         opno = linitial_oid(clause->opnos);
 5049                          2324                 :            240 :     Oid         inputcollid = linitial_oid(clause->inputcollids);
                               2325                 :                :     List       *opargs;
                               2326                 :                :     bool        is_join_clause;
                               2327                 :                : 
                               2328                 :                :     /* Build equivalent arg list for single operator */
 7416                          2329                 :            240 :     opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
                               2330                 :                : 
                               2331                 :                :     /*
                               2332                 :                :      * Decide if it's a join clause.  This should match clausesel.c's
                               2333                 :                :      * treat_as_join_clause(), except that we intentionally consider only the
                               2334                 :                :      * leading columns and not the rest of the clause.
                               2335                 :                :      */
                               2336         [ +  + ]:            240 :     if (varRelid != 0)
                               2337                 :                :     {
                               2338                 :                :         /*
                               2339                 :                :          * Caller is forcing restriction mode (eg, because we are examining an
                               2340                 :                :          * inner indexscan qual).
                               2341                 :                :          */
 6471                          2342                 :             45 :         is_join_clause = false;
                               2343                 :                :     }
                               2344         [ +  + ]:            195 :     else if (sjinfo == NULL)
                               2345                 :                :     {
                               2346                 :                :         /*
                               2347                 :                :          * It must be a restriction clause, since it's being evaluated at a
                               2348                 :                :          * scan node.
                               2349                 :                :          */
 7416                          2350                 :            175 :         is_join_clause = false;
                               2351                 :                :     }
                               2352                 :                :     else
                               2353                 :                :     {
                               2354                 :                :         /*
                               2355                 :                :          * Otherwise, it's a join if there's more than one base relation used.
                               2356                 :                :          */
 1930                          2357                 :             20 :         is_join_clause = (NumRelids(root, (Node *) opargs) > 1);
                               2358                 :                :     }
                               2359                 :                : 
 7416                          2360         [ +  + ]:            240 :     if (is_join_clause)
                               2361                 :                :     {
                               2362                 :                :         /* Estimate selectivity for a join clause. */
                               2363                 :             20 :         s1 = join_selectivity(root, opno,
                               2364                 :                :                               opargs,
                               2365                 :                :                               inputcollid,
                               2366                 :                :                               jointype,
                               2367                 :                :                               sjinfo);
                               2368                 :                :     }
                               2369                 :                :     else
                               2370                 :                :     {
                               2371                 :                :         /* Estimate selectivity for a restriction clause. */
                               2372                 :            220 :         s1 = restriction_selectivity(root, opno,
                               2373                 :                :                                      opargs,
                               2374                 :                :                                      inputcollid,
                               2375                 :                :                                      varRelid);
                               2376                 :                :     }
                               2377                 :                : 
                               2378                 :            240 :     return s1;
                               2379                 :                : }
                               2380                 :                : 
                               2381                 :                : /*
                               2382                 :                :  *      eqjoinsel       - Join selectivity of "="
                               2383                 :                :  */
                               2384                 :                : Datum
 9465                          2385                 :         218813 : eqjoinsel(PG_FUNCTION_ARGS)
                               2386                 :                : {
 7639                          2387                 :         218813 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
 9116                          2388                 :         218813 :     Oid         operator = PG_GETARG_OID(1);
                               2389                 :         218813 :     List       *args = (List *) PG_GETARG_POINTER(2);
                               2390                 :                : 
                               2391                 :                : #ifdef NOT_USED
                               2392                 :                :     JoinType    jointype = (JoinType) PG_GETARG_INT16(3);
                               2393                 :                : #endif
 6471                          2394                 :         218813 :     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
 2160                          2395                 :         218813 :     Oid         collation = PG_GET_COLLATION();
                               2396                 :                :     double      selec;
                               2397                 :                :     double      selec_inner;
                               2398                 :                :     VariableStatData vardata1;
                               2399                 :                :     VariableStatData vardata2;
                               2400                 :                :     double      nd1;
                               2401                 :                :     double      nd2;
                               2402                 :                :     bool        isdefault1;
                               2403                 :                :     bool        isdefault2;
                               2404                 :                :     Oid         opfuncoid;
                               2405                 :                :     FmgrInfo    eqproc;
  167 tgl@sss.pgh.pa.us        2406                 :GNC      218813 :     Oid         hashLeft = InvalidOid;
                               2407                 :         218813 :     Oid         hashRight = InvalidOid;
                               2408                 :                :     AttStatsSlot sslot1;
                               2409                 :                :     AttStatsSlot sslot2;
 2720 tgl@sss.pgh.pa.us        2410                 :CBC      218813 :     Form_pg_statistic stats1 = NULL;
                               2411                 :         218813 :     Form_pg_statistic stats2 = NULL;
                               2412                 :         218813 :     bool        have_mcvs1 = false;
                               2413                 :         218813 :     bool        have_mcvs2 = false;
  167 tgl@sss.pgh.pa.us        2414                 :GNC      218813 :     bool       *hasmatch1 = NULL;
                               2415                 :         218813 :     bool       *hasmatch2 = NULL;
                               2416                 :         218813 :     int         nmatches = 0;
                               2417                 :                :     bool        get_mcv_stats;
                               2418                 :                :     bool        join_is_reversed;
                               2419                 :                :     RelOptInfo *inner_rel;
                               2420                 :                : 
 6471 tgl@sss.pgh.pa.us        2421                 :CBC      218813 :     get_join_variables(root, args, sjinfo,
                               2422                 :                :                        &vardata1, &vardata2, &join_is_reversed);
                               2423                 :                : 
 2720                          2424                 :         218813 :     nd1 = get_variable_numdistinct(&vardata1, &isdefault1);
                               2425                 :         218813 :     nd2 = get_variable_numdistinct(&vardata2, &isdefault2);
                               2426                 :                : 
                               2427                 :         218813 :     opfuncoid = get_opcode(operator);
                               2428                 :                : 
                               2429                 :         218813 :     memset(&sslot1, 0, sizeof(sslot1));
                               2430                 :         218813 :     memset(&sslot2, 0, sizeof(sslot2));
                               2431                 :                : 
                               2432                 :                :     /*
                               2433                 :                :      * There is no use in fetching one side's MCVs if we lack MCVs for the
                               2434                 :                :      * other side, so do a quick check to verify that both stats exist.
                               2435                 :                :      */
 1264                          2436                 :         593211 :     get_mcv_stats = (HeapTupleIsValid(vardata1.statsTuple) &&
                               2437   [ +  +  +  + ]:         277414 :                      HeapTupleIsValid(vardata2.statsTuple) &&
                               2438                 :         121829 :                      get_attstatsslot(&sslot1, vardata1.statsTuple,
                               2439                 :                :                                       STATISTIC_KIND_MCV, InvalidOid,
                               2440   [ +  +  +  + ]:         374398 :                                       0) &&
                               2441                 :          58359 :                      get_attstatsslot(&sslot2, vardata2.statsTuple,
                               2442                 :                :                                       STATISTIC_KIND_MCV, InvalidOid,
                               2443                 :                :                                       0));
                               2444                 :                : 
 2720                          2445         [ +  + ]:         218813 :     if (HeapTupleIsValid(vardata1.statsTuple))
                               2446                 :                :     {
                               2447                 :                :         /* note we allow use of nullfrac regardless of security check */
                               2448                 :         155585 :         stats1 = (Form_pg_statistic) GETSTRUCT(vardata1.statsTuple);
 1264                          2449   [ +  +  +  - ]:         179173 :         if (get_mcv_stats &&
                               2450                 :          23588 :             statistic_proc_security_check(&vardata1, opfuncoid))
 2720                          2451                 :          23588 :             have_mcvs1 = get_attstatsslot(&sslot1, vardata1.statsTuple,
                               2452                 :                :                                           STATISTIC_KIND_MCV, InvalidOid,
                               2453                 :                :                                           ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
                               2454                 :                :     }
                               2455                 :                : 
                               2456         [ +  + ]:         218813 :     if (HeapTupleIsValid(vardata2.statsTuple))
                               2457                 :                :     {
                               2458                 :                :         /* note we allow use of nullfrac regardless of security check */
                               2459                 :         138089 :         stats2 = (Form_pg_statistic) GETSTRUCT(vardata2.statsTuple);
 1264                          2460   [ +  +  +  - ]:         161677 :         if (get_mcv_stats &&
                               2461                 :          23588 :             statistic_proc_security_check(&vardata2, opfuncoid))
 2720                          2462                 :          23588 :             have_mcvs2 = get_attstatsslot(&sslot2, vardata2.statsTuple,
                               2463                 :                :                                           STATISTIC_KIND_MCV, InvalidOid,
                               2464                 :                :                                           ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
                               2465                 :                :     }
                               2466                 :                : 
                               2467                 :                :     /* Prepare info usable by both eqjoinsel_inner and eqjoinsel_semi */
  167 tgl@sss.pgh.pa.us        2468   [ +  +  +  - ]:GNC      218813 :     if (have_mcvs1 && have_mcvs2)
                               2469                 :                :     {
                               2470                 :          23588 :         fmgr_info(opfuncoid, &eqproc);
                               2471                 :          23588 :         hasmatch1 = (bool *) palloc0(sslot1.nvalues * sizeof(bool));
                               2472                 :          23588 :         hasmatch2 = (bool *) palloc0(sslot2.nvalues * sizeof(bool));
                               2473                 :                : 
                               2474                 :                :         /*
                               2475                 :                :          * If the MCV lists are long enough to justify hashing, try to look up
                               2476                 :                :          * hash functions for the join operator.
                               2477                 :                :          */
                               2478         [ +  + ]:          23588 :         if ((sslot1.nvalues + sslot2.nvalues) >= EQJOINSEL_MCV_HASH_THRESHOLD)
                               2479                 :           9737 :             (void) get_op_hash_functions(operator, &hashLeft, &hashRight);
                               2480                 :                :     }
                               2481                 :                :     else
                               2482                 :         195225 :         memset(&eqproc, 0, sizeof(eqproc)); /* silence uninit-var warnings */
                               2483                 :                : 
                               2484                 :                :     /* We need to compute the inner-join selectivity in all cases */
                               2485                 :         218813 :     selec_inner = eqjoinsel_inner(&eqproc, collation,
                               2486                 :                :                                   hashLeft, hashRight,
                               2487                 :                :                                   &vardata1, &vardata2,
                               2488                 :                :                                   nd1, nd2,
                               2489                 :                :                                   isdefault1, isdefault2,
                               2490                 :                :                                   &sslot1, &sslot2,
                               2491                 :                :                                   stats1, stats2,
                               2492                 :                :                                   have_mcvs1, have_mcvs2,
                               2493                 :                :                                   hasmatch1, hasmatch2,
                               2494                 :                :                                   &nmatches);
                               2495                 :                : 
 6471 tgl@sss.pgh.pa.us        2496      [ +  +  - ]:CBC      218813 :     switch (sjinfo->jointype)
                               2497                 :                :     {
                               2498                 :         207419 :         case JOIN_INNER:
                               2499                 :                :         case JOIN_LEFT:
                               2500                 :                :         case JOIN_FULL:
 2720                          2501                 :         207419 :             selec = selec_inner;
 6471                          2502                 :         207419 :             break;
                               2503                 :          11394 :         case JOIN_SEMI:
                               2504                 :                :         case JOIN_ANTI:
                               2505                 :                : 
                               2506                 :                :             /*
                               2507                 :                :              * Look up the join's inner relation.  min_righthand is sufficient
                               2508                 :                :              * information because neither SEMI nor ANTI joins permit any
                               2509                 :                :              * reassociation into or out of their RHS, so the righthand will
                               2510                 :                :              * always be exactly that set of rels.
                               2511                 :                :              */
 5360                          2512                 :          11394 :             inner_rel = find_join_input_rel(root, sjinfo->min_righthand);
                               2513                 :                : 
 6471                          2514         [ +  + ]:          11394 :             if (!join_is_reversed)
  167 tgl@sss.pgh.pa.us        2515                 :GNC        5413 :                 selec = eqjoinsel_semi(&eqproc, collation,
                               2516                 :                :                                        hashLeft, hashRight,
                               2517                 :                :                                        false,
                               2518                 :                :                                        &vardata1, &vardata2,
                               2519                 :                :                                        nd1, nd2,
                               2520                 :                :                                        isdefault1, isdefault2,
                               2521                 :                :                                        &sslot1, &sslot2,
                               2522                 :                :                                        stats1, stats2,
                               2523                 :                :                                        have_mcvs1, have_mcvs2,
                               2524                 :                :                                        hasmatch1, hasmatch2,
                               2525                 :                :                                        &nmatches,
                               2526                 :                :                                        inner_rel);
                               2527                 :                :             else
                               2528                 :           5981 :                 selec = eqjoinsel_semi(&eqproc, collation,
                               2529                 :                :                                        hashLeft, hashRight,
                               2530                 :                :                                        true,
                               2531                 :                :                                        &vardata2, &vardata1,
                               2532                 :                :                                        nd2, nd1,
                               2533                 :                :                                        isdefault2, isdefault1,
                               2534                 :                :                                        &sslot2, &sslot1,
                               2535                 :                :                                        stats2, stats1,
                               2536                 :                :                                        have_mcvs2, have_mcvs1,
                               2537                 :                :                                        hasmatch2, hasmatch1,
                               2538                 :                :                                        &nmatches,
                               2539                 :                :                                        inner_rel);
                               2540                 :                : 
                               2541                 :                :             /*
                               2542                 :                :              * We should never estimate the output of a semijoin to be more
                               2543                 :                :              * rows than we estimate for an inner join with the same input
                               2544                 :                :              * rels and join condition; it's obviously impossible for that to
                               2545                 :                :              * happen.  The former estimate is N1 * Ssemi while the latter is
                               2546                 :                :              * N1 * N2 * Sinner, so we may clamp Ssemi <= N2 * Sinner.  Doing
                               2547                 :                :              * this is worthwhile because of the shakier estimation rules we
                               2548                 :                :              * use in eqjoinsel_semi, particularly in cases where it has to
                               2549                 :                :              * punt entirely.
                               2550                 :                :              */
 2720 tgl@sss.pgh.pa.us        2551         [ +  + ]:CBC       11394 :             selec = Min(selec, inner_rel->rows * selec_inner);
 6471                          2552                 :          11394 :             break;
 6471 tgl@sss.pgh.pa.us        2553                 :UBC           0 :         default:
                               2554                 :                :             /* other values not expected here */
                               2555         [ #  # ]:              0 :             elog(ERROR, "unrecognized join type: %d",
                               2556                 :                :                  (int) sjinfo->jointype);
                               2557                 :                :             selec = 0;          /* keep compiler quiet */
                               2558                 :                :             break;
                               2559                 :                :     }
                               2560                 :                : 
 2720 tgl@sss.pgh.pa.us        2561                 :CBC      218813 :     free_attstatsslot(&sslot1);
                               2562                 :         218813 :     free_attstatsslot(&sslot2);
                               2563                 :                : 
 6471                          2564         [ +  + ]:         218813 :     ReleaseVariableStats(vardata1);
                               2565         [ +  + ]:         218813 :     ReleaseVariableStats(vardata2);
                               2566                 :                : 
  167 tgl@sss.pgh.pa.us        2567         [ +  + ]:GNC      218813 :     if (hasmatch1)
                               2568                 :          23588 :         pfree(hasmatch1);
                               2569         [ +  + ]:         218813 :     if (hasmatch2)
                               2570                 :          23588 :         pfree(hasmatch2);
                               2571                 :                : 
 6471 tgl@sss.pgh.pa.us        2572   [ -  +  -  + ]:CBC      218813 :     CLAMP_PROBABILITY(selec);
                               2573                 :                : 
                               2574                 :         218813 :     PG_RETURN_FLOAT8((float8) selec);
                               2575                 :                : }
                               2576                 :                : 
                               2577                 :                : /*
                               2578                 :                :  * eqjoinsel_inner --- eqjoinsel for normal inner join
                               2579                 :                :  *
                               2580                 :                :  * In addition to computing the selectivity estimate, this will fill
                               2581                 :                :  * hasmatch1[], hasmatch2[], and *p_nmatches (if have_mcvs1 && have_mcvs2).
                               2582                 :                :  * We may be able to re-use that data in eqjoinsel_semi.
                               2583                 :                :  *
                               2584                 :                :  * We also use this for LEFT/FULL outer joins; it's not presently clear
                               2585                 :                :  * that it's worth trying to distinguish them here.
                               2586                 :                :  */
                               2587                 :                : static double
  167 tgl@sss.pgh.pa.us        2588                 :GNC      218813 : eqjoinsel_inner(FmgrInfo *eqproc, Oid collation,
                               2589                 :                :                 Oid hashLeft, Oid hashRight,
                               2590                 :                :                 VariableStatData *vardata1, VariableStatData *vardata2,
                               2591                 :                :                 double nd1, double nd2,
                               2592                 :                :                 bool isdefault1, bool isdefault2,
                               2593                 :                :                 AttStatsSlot *sslot1, AttStatsSlot *sslot2,
                               2594                 :                :                 Form_pg_statistic stats1, Form_pg_statistic stats2,
                               2595                 :                :                 bool have_mcvs1, bool have_mcvs2,
                               2596                 :                :                 bool *hasmatch1, bool *hasmatch2,
                               2597                 :                :                 int *p_nmatches)
                               2598                 :                : {
                               2599                 :                :     double      selec;
                               2600                 :                : 
 8113 tgl@sss.pgh.pa.us        2601   [ +  +  +  - ]:CBC      218813 :     if (have_mcvs1 && have_mcvs2)
10467 bruce@momjian.us         2602                 :          23588 :     {
                               2603                 :                :         /*
                               2604                 :                :          * We have most-common-value lists for both relations.  Run through
                               2605                 :                :          * the lists to see which MCVs actually join to each other with the
                               2606                 :                :          * given operator.  This allows us to determine the exact join
                               2607                 :                :          * selectivity for the portion of the relations represented by the MCV
                               2608                 :                :          * lists.  We still have to estimate for the remaining population, but
                               2609                 :                :          * in a skewed distribution this gives us a big leg up in accuracy.
                               2610                 :                :          * For motivation see the analysis in Y. Ioannidis and S.
                               2611                 :                :          * Christodoulakis, "On the propagation of errors in the size of join
                               2612                 :                :          * results", Technical Report 1018, Computer Science Dept., University
                               2613                 :                :          * of Wisconsin, Madison, March 1991 (available from ftp.cs.wisc.edu).
                               2614                 :                :          */
 8113 tgl@sss.pgh.pa.us        2615                 :          23588 :         double      nullfrac1 = stats1->stanullfrac;
                               2616                 :          23588 :         double      nullfrac2 = stats2->stanullfrac;
                               2617                 :                :         double      matchprodfreq,
                               2618                 :                :                     matchfreq1,
                               2619                 :                :                     matchfreq2,
                               2620                 :                :                     unmatchfreq1,
                               2621                 :                :                     unmatchfreq2,
                               2622                 :                :                     otherfreq1,
                               2623                 :                :                     otherfreq2,
                               2624                 :                :                     totalsel1,
                               2625                 :                :                     totalsel2;
                               2626                 :                :         int         i,
                               2627                 :                :                     nmatches;
                               2628                 :                : 
                               2629                 :                :         /* Fill the match arrays */
  167 tgl@sss.pgh.pa.us        2630                 :GNC       23588 :         eqjoinsel_find_matches(eqproc, collation,
                               2631                 :                :                                hashLeft, hashRight,
                               2632                 :                :                                false,
                               2633                 :                :                                sslot1, sslot2,
                               2634                 :                :                                sslot1->nvalues, sslot2->nvalues,
                               2635                 :                :                                hasmatch1, hasmatch2,
                               2636                 :                :                                p_nmatches, &matchprodfreq);
                               2637                 :          23588 :         nmatches = *p_nmatches;
 8113 tgl@sss.pgh.pa.us        2638   [ -  +  -  + ]:CBC       23588 :         CLAMP_PROBABILITY(matchprodfreq);
                               2639                 :                : 
                               2640                 :                :         /* Sum up frequencies of matched and unmatched MCVs */
                               2641                 :          23588 :         matchfreq1 = unmatchfreq1 = 0.0;
 2720                          2642         [ +  + ]:         537528 :         for (i = 0; i < sslot1->nvalues; i++)
                               2643                 :                :         {
 8113                          2644         [ +  + ]:         513940 :             if (hasmatch1[i])
 2720                          2645                 :         232059 :                 matchfreq1 += sslot1->numbers[i];
                               2646                 :                :             else
                               2647                 :         281881 :                 unmatchfreq1 += sslot1->numbers[i];
                               2648                 :                :         }
 8113                          2649   [ -  +  +  + ]:          23588 :         CLAMP_PROBABILITY(matchfreq1);
                               2650   [ -  +  -  + ]:          23588 :         CLAMP_PROBABILITY(unmatchfreq1);
                               2651                 :          23588 :         matchfreq2 = unmatchfreq2 = 0.0;
 2720                          2652         [ +  + ]:         436124 :         for (i = 0; i < sslot2->nvalues; i++)
                               2653                 :                :         {
 8113                          2654         [ +  + ]:         412536 :             if (hasmatch2[i])
 2720                          2655                 :         232059 :                 matchfreq2 += sslot2->numbers[i];
                               2656                 :                :             else
                               2657                 :         180477 :                 unmatchfreq2 += sslot2->numbers[i];
                               2658                 :                :         }
 8113                          2659   [ -  +  +  + ]:          23588 :         CLAMP_PROBABILITY(matchfreq2);
                               2660   [ -  +  -  + ]:          23588 :         CLAMP_PROBABILITY(unmatchfreq2);
                               2661                 :                : 
                               2662                 :                :         /*
                               2663                 :                :          * Compute total frequency of non-null values that are not in the MCV
                               2664                 :                :          * lists.
                               2665                 :                :          */
                               2666                 :          23588 :         otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
                               2667                 :          23588 :         otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
                               2668   [ +  +  -  + ]:          23588 :         CLAMP_PROBABILITY(otherfreq1);
                               2669   [ +  +  -  + ]:          23588 :         CLAMP_PROBABILITY(otherfreq2);
                               2670                 :                : 
                               2671                 :                :         /*
                               2672                 :                :          * We can estimate the total selectivity from the point of view of
                               2673                 :                :          * relation 1 as: the known selectivity for matched MCVs, plus
                               2674                 :                :          * unmatched MCVs that are assumed to match against random members of
                               2675                 :                :          * relation 2's non-MCV population, plus non-MCV values that are
                               2676                 :                :          * assumed to match against random members of relation 2's unmatched
                               2677                 :                :          * MCVs plus non-MCV values.
                               2678                 :                :          */
                               2679                 :          23588 :         totalsel1 = matchprodfreq;
 2720                          2680         [ +  + ]:          23588 :         if (nd2 > sslot2->nvalues)
                               2681                 :           5330 :             totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - sslot2->nvalues);
 8113                          2682         [ +  + ]:          23588 :         if (nd2 > nmatches)
                               2683                 :           9265 :             totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
                               2684                 :           9265 :                 (nd2 - nmatches);
                               2685                 :                :         /* Same estimate from the point of view of relation 2. */
                               2686                 :          23588 :         totalsel2 = matchprodfreq;
 2720                          2687         [ +  + ]:          23588 :         if (nd1 > sslot1->nvalues)
                               2688                 :           5092 :             totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - sslot1->nvalues);
 8113                          2689         [ +  + ]:          23588 :         if (nd1 > nmatches)
                               2690                 :           8249 :             totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
                               2691                 :           8249 :                 (nd1 - nmatches);
                               2692                 :                : 
                               2693                 :                :         /*
                               2694                 :                :          * Use the smaller of the two estimates.  This can be justified in
                               2695                 :                :          * essentially the same terms as given below for the no-stats case: to
                               2696                 :                :          * a first approximation, we are estimating from the point of view of
                               2697                 :                :          * the relation with smaller nd.
                               2698                 :                :          */
                               2699         [ +  + ]:          23588 :         selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
                               2700                 :                :     }
                               2701                 :                :     else
                               2702                 :                :     {
                               2703                 :                :         /*
                               2704                 :                :          * We do not have MCV lists for both sides.  Estimate the join
                               2705                 :                :          * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). This
                               2706                 :                :          * is plausible if we assume that the join operator is strict and the
                               2707                 :                :          * non-null values are about equally distributed: a given non-null
                               2708                 :                :          * tuple of rel1 will join to either zero or N2*(1-nullfrac2)/nd2 rows
                               2709                 :                :          * of rel2, so total join rows are at most
                               2710                 :                :          * N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join selectivity of
                               2711                 :                :          * not more than (1-nullfrac1)*(1-nullfrac2)/nd2. By the same logic it
                               2712                 :                :          * is not more than (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression
                               2713                 :                :          * with MIN() is an upper bound.  Using the MIN() means we estimate
                               2714                 :                :          * from the point of view of the relation with smaller nd (since the
                               2715                 :                :          * larger nd is determining the MIN).  It is reasonable to assume that
                               2716                 :                :          * most tuples in this rel will have join partners, so the bound is
                               2717                 :                :          * probably reasonably tight and should be taken as-is.
                               2718                 :                :          *
                               2719                 :                :          * XXX Can we be smarter if we have an MCV list for just one side? It
                               2720                 :                :          * seems that if we assume equal distribution for the other side, we
                               2721                 :                :          * end up with the same answer anyway.
                               2722                 :                :          */
                               2723         [ +  + ]:         195225 :         double      nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
                               2724         [ +  + ]:         195225 :         double      nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
                               2725                 :                : 
                               2726                 :         195225 :         selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
                               2727         [ +  + ]:         195225 :         if (nd1 > nd2)
                               2728                 :         103422 :             selec /= nd1;
                               2729                 :                :         else
                               2730                 :          91803 :             selec /= nd2;
                               2731                 :                :     }
                               2732                 :                : 
 6471                          2733                 :         218813 :     return selec;
                               2734                 :                : }
                               2735                 :                : 
                               2736                 :                : /*
                               2737                 :                :  * eqjoinsel_semi --- eqjoinsel for semi join
                               2738                 :                :  *
                               2739                 :                :  * (Also used for anti join, which we are supposed to estimate the same way.)
                               2740                 :                :  * Caller has ensured that vardata1 is the LHS variable; however, eqproc
                               2741                 :                :  * is for the original join operator, which might now need to have the inputs
                               2742                 :                :  * swapped in order to apply correctly.  Also, if have_mcvs1 && have_mcvs2
                               2743                 :                :  * then hasmatch1[], hasmatch2[], and *p_nmatches were filled by
                               2744                 :                :  * eqjoinsel_inner.
                               2745                 :                :  */
                               2746                 :                : static double
  167 tgl@sss.pgh.pa.us        2747                 :GNC       11394 : eqjoinsel_semi(FmgrInfo *eqproc, Oid collation,
                               2748                 :                :                Oid hashLeft, Oid hashRight,
                               2749                 :                :                bool op_is_reversed,
                               2750                 :                :                VariableStatData *vardata1, VariableStatData *vardata2,
                               2751                 :                :                double nd1, double nd2,
                               2752                 :                :                bool isdefault1, bool isdefault2,
                               2753                 :                :                AttStatsSlot *sslot1, AttStatsSlot *sslot2,
                               2754                 :                :                Form_pg_statistic stats1, Form_pg_statistic stats2,
                               2755                 :                :                bool have_mcvs1, bool have_mcvs2,
                               2756                 :                :                bool *hasmatch1, bool *hasmatch2,
                               2757                 :                :                int *p_nmatches,
                               2758                 :                :                RelOptInfo *inner_rel)
                               2759                 :                : {
                               2760                 :                :     double      selec;
                               2761                 :                : 
                               2762                 :                :     /*
                               2763                 :                :      * We clamp nd2 to be not more than what we estimate the inner relation's
                               2764                 :                :      * size to be.  This is intuitively somewhat reasonable since obviously
                               2765                 :                :      * there can't be more than that many distinct values coming from the
                               2766                 :                :      * inner rel.  The reason for the asymmetry (ie, that we don't clamp nd1
                               2767                 :                :      * likewise) is that this is the only pathway by which restriction clauses
                               2768                 :                :      * applied to the inner rel will affect the join result size estimate,
                               2769                 :                :      * since set_joinrel_size_estimates will multiply SEMI/ANTI selectivity by
                               2770                 :                :      * only the outer rel's size.  If we clamped nd1 we'd be double-counting
                               2771                 :                :      * the selectivity of outer-rel restrictions.
                               2772                 :                :      *
                               2773                 :                :      * We can apply this clamping both with respect to the base relation from
                               2774                 :                :      * which the join variable comes (if there is just one), and to the
                               2775                 :                :      * immediate inner input relation of the current join.
                               2776                 :                :      *
                               2777                 :                :      * If we clamp, we can treat nd2 as being a non-default estimate; it's not
                               2778                 :                :      * great, maybe, but it didn't come out of nowhere either.  This is most
                               2779                 :                :      * helpful when the inner relation is empty and consequently has no stats.
                               2780                 :                :      */
 5360 tgl@sss.pgh.pa.us        2781         [ +  + ]:CBC       11394 :     if (vardata2->rel)
                               2782                 :                :     {
 3444                          2783         [ +  + ]:          11389 :         if (nd2 >= vardata2->rel->rows)
                               2784                 :                :         {
                               2785                 :           9637 :             nd2 = vardata2->rel->rows;
                               2786                 :           9637 :             isdefault2 = false;
                               2787                 :                :         }
                               2788                 :                :     }
                               2789         [ +  + ]:          11394 :     if (nd2 >= inner_rel->rows)
                               2790                 :                :     {
                               2791                 :           9591 :         nd2 = inner_rel->rows;
                               2792                 :           9591 :         isdefault2 = false;
                               2793                 :                :     }
                               2794                 :                : 
  167 tgl@sss.pgh.pa.us        2795   [ +  +  +  - ]:GNC       11394 :     if (have_mcvs1 && have_mcvs2)
 6471 tgl@sss.pgh.pa.us        2796                 :CBC         525 :     {
                               2797                 :                :         /*
                               2798                 :                :          * We have most-common-value lists for both relations.  Run through
                               2799                 :                :          * the lists to see which MCVs actually join to each other with the
                               2800                 :                :          * given operator.  This allows us to determine the exact join
                               2801                 :                :          * selectivity for the portion of the relations represented by the MCV
                               2802                 :                :          * lists.  We still have to estimate for the remaining population, but
                               2803                 :                :          * in a skewed distribution this gives us a big leg up in accuracy.
                               2804                 :                :          */
                               2805                 :            525 :         double      nullfrac1 = stats1->stanullfrac;
                               2806                 :                :         double      matchprodfreq,
                               2807                 :                :                     matchfreq1,
                               2808                 :                :                     uncertainfrac,
                               2809                 :                :                     uncertain;
                               2810                 :                :         int         i,
                               2811                 :                :                     nmatches,
                               2812                 :                :                     clamped_nvalues2;
                               2813                 :                : 
                               2814                 :                :         /*
                               2815                 :                :          * The clamping above could have resulted in nd2 being less than
                               2816                 :                :          * sslot2->nvalues; in which case, we assume that precisely the nd2
                               2817                 :                :          * most common values in the relation will appear in the join input,
                               2818                 :                :          * and so compare to only the first nd2 members of the MCV list.  Of
                               2819                 :                :          * course this is frequently wrong, but it's the best bet we can make.
                               2820                 :                :          */
 2720                          2821         [ +  + ]:            525 :         clamped_nvalues2 = Min(sslot2->nvalues, nd2);
                               2822                 :                : 
                               2823                 :                :         /*
                               2824                 :                :          * If we did not set clamped_nvalues2 to less than sslot2->nvalues,
                               2825                 :                :          * then the hasmatch1[] and hasmatch2[] match flags computed by
                               2826                 :                :          * eqjoinsel_inner are still perfectly applicable, so we need not
                               2827                 :                :          * re-do the matching work.  Note that it does not matter if
                               2828                 :                :          * op_is_reversed: we'd get the same answers.
                               2829                 :                :          *
                               2830                 :                :          * If we did clamp, then a different set of sslot2 values is to be
                               2831                 :                :          * compared, so we have to re-do the matching.
                               2832                 :                :          */
  167 tgl@sss.pgh.pa.us        2833         [ -  + ]:GNC         525 :         if (clamped_nvalues2 != sslot2->nvalues)
                               2834                 :                :         {
                               2835                 :                :             /* Must re-zero the arrays */
  167 tgl@sss.pgh.pa.us        2836                 :UNC           0 :             memset(hasmatch1, 0, sslot1->nvalues * sizeof(bool));
                               2837                 :              0 :             memset(hasmatch2, 0, clamped_nvalues2 * sizeof(bool));
                               2838                 :                :             /* Re-fill the match arrays */
                               2839                 :              0 :             eqjoinsel_find_matches(eqproc, collation,
                               2840                 :                :                                    hashLeft, hashRight,
                               2841                 :                :                                    op_is_reversed,
                               2842                 :                :                                    sslot1, sslot2,
                               2843                 :                :                                    sslot1->nvalues, clamped_nvalues2,
                               2844                 :                :                                    hasmatch1, hasmatch2,
                               2845                 :                :                                    p_nmatches, &matchprodfreq);
                               2846                 :                :         }
  167 tgl@sss.pgh.pa.us        2847                 :GNC         525 :         nmatches = *p_nmatches;
                               2848                 :                : 
                               2849                 :                :         /* Sum up frequencies of matched MCVs */
 6471 tgl@sss.pgh.pa.us        2850                 :CBC         525 :         matchfreq1 = 0.0;
 2720                          2851         [ +  + ]:          11907 :         for (i = 0; i < sslot1->nvalues; i++)
                               2852                 :                :         {
 6471                          2853         [ +  + ]:          11382 :             if (hasmatch1[i])
 2720                          2854                 :           9490 :                 matchfreq1 += sslot1->numbers[i];
                               2855                 :                :         }
 6471                          2856   [ -  +  +  + ]:            525 :         CLAMP_PROBABILITY(matchfreq1);
                               2857                 :                : 
                               2858                 :                :         /*
                               2859                 :                :          * Now we need to estimate the fraction of relation 1 that has at
                               2860                 :                :          * least one join partner.  We know for certain that the matched MCVs
                               2861                 :                :          * do, so that gives us a lower bound, but we're really in the dark
                               2862                 :                :          * about everything else.  Our crude approach is: if nd1 <= nd2 then
                               2863                 :                :          * assume all non-null rel1 rows have join partners, else assume for
                               2864                 :                :          * the uncertain rows that a fraction nd2/nd1 have join partners. We
                               2865                 :                :          * can discount the known-matched MCVs from the distinct-values counts
                               2866                 :                :          * before doing the division.
                               2867                 :                :          *
                               2868                 :                :          * Crude as the above is, it's completely useless if we don't have
                               2869                 :                :          * reliable ndistinct values for both sides.  Hence, if either nd1 or
                               2870                 :                :          * nd2 is default, punt and assume half of the uncertain rows have
                               2871                 :                :          * join partners.
                               2872                 :                :          */
 5357                          2873   [ +  -  +  - ]:            525 :         if (!isdefault1 && !isdefault2)
                               2874                 :                :         {
 5502                          2875                 :            525 :             nd1 -= nmatches;
                               2876                 :            525 :             nd2 -= nmatches;
 5360                          2877   [ +  +  -  + ]:            525 :             if (nd1 <= nd2 || nd2 < 0)
 5502                          2878                 :            495 :                 uncertainfrac = 1.0;
                               2879                 :                :             else
                               2880                 :             30 :                 uncertainfrac = nd2 / nd1;
                               2881                 :                :         }
                               2882                 :                :         else
 5502 tgl@sss.pgh.pa.us        2883                 :UBC           0 :             uncertainfrac = 0.5;
 5502 tgl@sss.pgh.pa.us        2884                 :CBC         525 :         uncertain = 1.0 - matchfreq1 - nullfrac1;
                               2885   [ -  +  -  + ]:            525 :         CLAMP_PROBABILITY(uncertain);
                               2886                 :            525 :         selec = matchfreq1 + uncertainfrac * uncertain;
                               2887                 :                :     }
                               2888                 :                :     else
                               2889                 :                :     {
                               2890                 :                :         /*
                               2891                 :                :          * Without MCV lists for both sides, we can only use the heuristic
                               2892                 :                :          * about nd1 vs nd2.
                               2893                 :                :          */
 6471                          2894         [ +  + ]:          10869 :         double      nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
                               2895                 :                : 
 5357                          2896   [ +  +  +  + ]:          10869 :         if (!isdefault1 && !isdefault2)
                               2897                 :                :         {
 5360                          2898   [ +  +  -  + ]:           8068 :             if (nd1 <= nd2 || nd2 < 0)
 5502                          2899                 :           3728 :                 selec = 1.0 - nullfrac1;
                               2900                 :                :             else
                               2901                 :           4340 :                 selec = (nd2 / nd1) * (1.0 - nullfrac1);
                               2902                 :                :         }
                               2903                 :                :         else
                               2904                 :           2801 :             selec = 0.5 * (1.0 - nullfrac1);
                               2905                 :                :     }
                               2906                 :                : 
 6471                          2907                 :          11394 :     return selec;
                               2908                 :                : }
                               2909                 :                : 
                               2910                 :                : /*
                               2911                 :                :  * Identify matching MCVs for eqjoinsel_inner or eqjoinsel_semi.
                               2912                 :                :  *
                               2913                 :                :  * Inputs:
                               2914                 :                :  *  eqproc: FmgrInfo for equality function to use (might be reversed)
                               2915                 :                :  *  collation: OID of collation to use
                               2916                 :                :  *  hashLeft, hashRight: OIDs of hash functions associated with equality op,
                               2917                 :                :  *      or InvalidOid if we're not to use hashing
                               2918                 :                :  *  op_is_reversed: indicates that eqproc compares right type to left type
                               2919                 :                :  *  sslot1, sslot2: MCV values for the lefthand and righthand inputs
                               2920                 :                :  *  nvalues1, nvalues2: number of values to be considered (can be less than
                               2921                 :                :  *      sslotN->nvalues, but not more)
                               2922                 :                :  * Outputs:
                               2923                 :                :  *  hasmatch1[], hasmatch2[]: pre-zeroed arrays of lengths nvalues1, nvalues2;
                               2924                 :                :  *      entries are set to true if that MCV has a match on the other side
                               2925                 :                :  *  *p_nmatches: receives number of MCV pairs that match
                               2926                 :                :  *  *p_matchprodfreq: receives sum(sslot1->numbers[i] * sslot2->numbers[j])
                               2927                 :                :  *      for matching MCVs
                               2928                 :                :  *
                               2929                 :                :  * Note that hashLeft is for the eqproc's left-hand input type, hashRight
                               2930                 :                :  * for its right, regardless of op_is_reversed.
                               2931                 :                :  *
                               2932                 :                :  * Note we assume that each MCV will match at most one member of the other
                               2933                 :                :  * MCV list.  If the operator isn't really equality, there could be multiple
                               2934                 :                :  * matches --- but we don't look for them, both for speed and because the
                               2935                 :                :  * math wouldn't add up...
                               2936                 :                :  */
                               2937                 :                : static void
  167 tgl@sss.pgh.pa.us        2938                 :GNC       23588 : eqjoinsel_find_matches(FmgrInfo *eqproc, Oid collation,
                               2939                 :                :                        Oid hashLeft, Oid hashRight,
                               2940                 :                :                        bool op_is_reversed,
                               2941                 :                :                        AttStatsSlot *sslot1, AttStatsSlot *sslot2,
                               2942                 :                :                        int nvalues1, int nvalues2,
                               2943                 :                :                        bool *hasmatch1, bool *hasmatch2,
                               2944                 :                :                        int *p_nmatches, double *p_matchprodfreq)
                               2945                 :                : {
                               2946                 :          23588 :     LOCAL_FCINFO(fcinfo, 2);
                               2947                 :          23588 :     double      matchprodfreq = 0.0;
                               2948                 :          23588 :     int         nmatches = 0;
                               2949                 :                : 
                               2950                 :                :     /*
                               2951                 :                :      * Save a few cycles by setting up the fcinfo struct just once.  Using
                               2952                 :                :      * FunctionCallInvoke directly also avoids failure if the eqproc returns
                               2953                 :                :      * NULL, though really equality functions should never do that.
                               2954                 :                :      */
                               2955                 :          23588 :     InitFunctionCallInfoData(*fcinfo, eqproc, 2, collation,
                               2956                 :                :                              NULL, NULL);
                               2957                 :          23588 :     fcinfo->args[0].isnull = false;
                               2958                 :          23588 :     fcinfo->args[1].isnull = false;
                               2959                 :                : 
                               2960   [ +  +  +  - ]:          23588 :     if (OidIsValid(hashLeft) && OidIsValid(hashRight))
                               2961                 :           9737 :     {
                               2962                 :                :         /* Use a hash table to speed up the matching */
                               2963                 :           9737 :         LOCAL_FCINFO(hash_fcinfo, 1);
                               2964                 :                :         FmgrInfo    hash_proc;
                               2965                 :                :         MCVHashContext hashContext;
                               2966                 :                :         MCVHashTable_hash *hashTable;
                               2967                 :                :         AttStatsSlot *statsProbe;
                               2968                 :                :         AttStatsSlot *statsHash;
                               2969                 :                :         bool       *hasMatchProbe;
                               2970                 :                :         bool       *hasMatchHash;
                               2971                 :                :         int         nvaluesProbe;
                               2972                 :                :         int         nvaluesHash;
                               2973                 :                : 
                               2974                 :                :         /* Make sure we build the hash table on the smaller array. */
                               2975         [ +  + ]:           9737 :         if (sslot1->nvalues >= sslot2->nvalues)
                               2976                 :                :         {
                               2977                 :           8224 :             statsProbe = sslot1;
                               2978                 :           8224 :             statsHash = sslot2;
                               2979                 :           8224 :             hasMatchProbe = hasmatch1;
                               2980                 :           8224 :             hasMatchHash = hasmatch2;
                               2981                 :           8224 :             nvaluesProbe = nvalues1;
                               2982                 :           8224 :             nvaluesHash = nvalues2;
                               2983                 :                :         }
                               2984                 :                :         else
                               2985                 :                :         {
                               2986                 :                :             /* We'll have to reverse the direction of use of the operator. */
                               2987                 :           1513 :             op_is_reversed = !op_is_reversed;
                               2988                 :           1513 :             statsProbe = sslot2;
                               2989                 :           1513 :             statsHash = sslot1;
                               2990                 :           1513 :             hasMatchProbe = hasmatch2;
                               2991                 :           1513 :             hasMatchHash = hasmatch1;
                               2992                 :           1513 :             nvaluesProbe = nvalues2;
                               2993                 :           1513 :             nvaluesHash = nvalues1;
                               2994                 :                :         }
                               2995                 :                : 
                               2996                 :                :         /*
                               2997                 :                :          * Build the hash table on the smaller array, using the appropriate
                               2998                 :                :          * hash function for its data type.
                               2999                 :                :          */
                               3000         [ +  + ]:           9737 :         fmgr_info(op_is_reversed ? hashLeft : hashRight, &hash_proc);
                               3001                 :           9737 :         InitFunctionCallInfoData(*hash_fcinfo, &hash_proc, 1, collation,
                               3002                 :                :                                  NULL, NULL);
                               3003                 :           9737 :         hash_fcinfo->args[0].isnull = false;
                               3004                 :                : 
                               3005                 :           9737 :         hashContext.equal_fcinfo = fcinfo;
                               3006                 :           9737 :         hashContext.hash_fcinfo = hash_fcinfo;
                               3007                 :           9737 :         hashContext.op_is_reversed = op_is_reversed;
                               3008                 :           9737 :         hashContext.insert_mode = true;
                               3009                 :           9737 :         get_typlenbyval(statsHash->valuetype,
                               3010                 :                :                         &hashContext.hash_typlen,
                               3011                 :                :                         &hashContext.hash_typbyval);
                               3012                 :                : 
                               3013                 :           9737 :         hashTable = MCVHashTable_create(CurrentMemoryContext,
                               3014                 :                :                                         nvaluesHash,
                               3015                 :                :                                         &hashContext);
                               3016                 :                : 
                               3017         [ +  + ]:         313126 :         for (int i = 0; i < nvaluesHash; i++)
                               3018                 :                :         {
                               3019                 :         303389 :             bool        found = false;
                               3020                 :         303389 :             MCVHashEntry *entry = MCVHashTable_insert(hashTable,
                               3021                 :         303389 :                                                       statsHash->values[i],
                               3022                 :                :                                                       &found);
                               3023                 :                : 
                               3024                 :                :             /*
                               3025                 :                :              * MCVHashTable_insert will only report "found" if the new value
                               3026                 :                :              * is equal to some previous one per datum_image_eq().  That
                               3027                 :                :              * probably shouldn't happen, since we're not expecting duplicates
                               3028                 :                :              * in the MCV list.  If we do find a dup, just ignore it, leaving
                               3029                 :                :              * the hash entry's index pointing at the first occurrence.  That
                               3030                 :                :              * matches the behavior that the non-hashed code path would have.
                               3031                 :                :              */
                               3032         [ +  - ]:         303389 :             if (likely(!found))
                               3033                 :         303389 :                 entry->index = i;
                               3034                 :                :         }
                               3035                 :                : 
                               3036                 :                :         /*
                               3037                 :                :          * Prepare to probe the hash table.  If the probe values are of a
                               3038                 :                :          * different data type, then we need to change hash functions.  (This
                               3039                 :                :          * code relies on the assumption that since we defined SH_STORE_HASH,
                               3040                 :                :          * simplehash.h will never need to compute hash values for existing
                               3041                 :                :          * hash table entries.)
                               3042                 :                :          */
                               3043                 :           9737 :         hashContext.insert_mode = false;
                               3044         [ +  + ]:           9737 :         if (hashLeft != hashRight)
                               3045                 :                :         {
                               3046         [ +  + ]:           1264 :             fmgr_info(op_is_reversed ? hashRight : hashLeft, &hash_proc);
                               3047                 :                :             /* Resetting hash_fcinfo is probably unnecessary, but be safe */
                               3048                 :           1264 :             InitFunctionCallInfoData(*hash_fcinfo, &hash_proc, 1, collation,
                               3049                 :                :                                      NULL, NULL);
                               3050                 :           1264 :             hash_fcinfo->args[0].isnull = false;
                               3051                 :                :         }
                               3052                 :                : 
                               3053                 :                :         /* Look up each probe value in turn. */
                               3054         [ +  + ]:         501477 :         for (int i = 0; i < nvaluesProbe; i++)
                               3055                 :                :         {
                               3056                 :         491740 :             MCVHashEntry *entry = MCVHashTable_lookup(hashTable,
                               3057                 :         491740 :                                                       statsProbe->values[i]);
                               3058                 :                : 
                               3059                 :                :             /* As in the other code path, skip already-matched hash entries */
                               3060   [ +  +  +  - ]:         491740 :             if (entry != NULL && !hasMatchHash[entry->index])
                               3061                 :                :             {
                               3062                 :         179954 :                 hasMatchHash[entry->index] = hasMatchProbe[i] = true;
                               3063                 :         179954 :                 nmatches++;
                               3064                 :         179954 :                 matchprodfreq += statsHash->numbers[entry->index] * statsProbe->numbers[i];
                               3065                 :                :             }
                               3066                 :                :         }
                               3067                 :                : 
                               3068                 :           9737 :         MCVHashTable_destroy(hashTable);
                               3069                 :                :     }
                               3070                 :                :     else
                               3071                 :                :     {
                               3072                 :                :         /* We're not to use hashing, so do it the O(N^2) way */
                               3073                 :                :         int         index1,
                               3074                 :                :                     index2;
                               3075                 :                : 
                               3076                 :                :         /* Set up to supply the values in the order the operator expects */
                               3077         [ -  + ]:          13851 :         if (op_is_reversed)
                               3078                 :                :         {
  167 tgl@sss.pgh.pa.us        3079                 :UNC           0 :             index1 = 1;
                               3080                 :              0 :             index2 = 0;
                               3081                 :                :         }
                               3082                 :                :         else
                               3083                 :                :         {
  167 tgl@sss.pgh.pa.us        3084                 :GNC       13851 :             index1 = 0;
                               3085                 :          13851 :             index2 = 1;
                               3086                 :                :         }
                               3087                 :                : 
                               3088         [ +  + ]:          70243 :         for (int i = 0; i < nvalues1; i++)
                               3089                 :                :         {
                               3090                 :          56392 :             fcinfo->args[index1].value = sslot1->values[i];
                               3091                 :                : 
                               3092         [ +  + ]:         200926 :             for (int j = 0; j < nvalues2; j++)
                               3093                 :                :             {
                               3094                 :                :                 Datum       fresult;
                               3095                 :                : 
                               3096         [ +  + ]:         196639 :                 if (hasmatch2[j])
                               3097                 :          95118 :                     continue;
                               3098                 :         101521 :                 fcinfo->args[index2].value = sslot2->values[j];
                               3099                 :         101521 :                 fcinfo->isnull = false;
                               3100                 :         101521 :                 fresult = FunctionCallInvoke(fcinfo);
                               3101   [ +  -  +  + ]:         101521 :                 if (!fcinfo->isnull && DatumGetBool(fresult))
                               3102                 :                :                 {
                               3103                 :          52105 :                     hasmatch1[i] = hasmatch2[j] = true;
                               3104                 :          52105 :                     matchprodfreq += sslot1->numbers[i] * sslot2->numbers[j];
                               3105                 :          52105 :                     nmatches++;
                               3106                 :          52105 :                     break;
                               3107                 :                :                 }
                               3108                 :                :             }
                               3109                 :                :         }
                               3110                 :                :     }
                               3111                 :                : 
                               3112                 :          23588 :     *p_nmatches = nmatches;
                               3113                 :          23588 :     *p_matchprodfreq = matchprodfreq;
                               3114                 :          23588 : }
                               3115                 :                : 
                               3116                 :                : /*
                               3117                 :                :  * Support functions for the hash tables used by eqjoinsel_find_matches
                               3118                 :                :  */
                               3119                 :                : static uint32
                               3120                 :         795129 : hash_mcv(MCVHashTable_hash *tab, Datum key)
                               3121                 :                : {
                               3122                 :         795129 :     MCVHashContext *context = (MCVHashContext *) tab->private_data;
                               3123                 :         795129 :     FunctionCallInfo fcinfo = context->hash_fcinfo;
                               3124                 :                :     Datum       fresult;
                               3125                 :                : 
                               3126                 :         795129 :     fcinfo->args[0].value = key;
                               3127                 :         795129 :     fcinfo->isnull = false;
                               3128                 :         795129 :     fresult = FunctionCallInvoke(fcinfo);
                               3129         [ -  + ]:         795129 :     Assert(!fcinfo->isnull);
                               3130                 :         795129 :     return DatumGetUInt32(fresult);
                               3131                 :                : }
                               3132                 :                : 
                               3133                 :                : static bool
                               3134                 :         179954 : mcvs_equal(MCVHashTable_hash *tab, Datum key0, Datum key1)
                               3135                 :                : {
                               3136                 :         179954 :     MCVHashContext *context = (MCVHashContext *) tab->private_data;
                               3137                 :                : 
                               3138         [ -  + ]:         179954 :     if (context->insert_mode)
                               3139                 :                :     {
                               3140                 :                :         /*
                               3141                 :                :          * During the insertion step, any comparisons will be between two
                               3142                 :                :          * Datums of the hash table's data type, so if the given operator is
                               3143                 :                :          * cross-type it will be the wrong thing to use.  Fortunately, we can
                               3144                 :                :          * use datum_image_eq instead.  The MCV values should all be distinct
                               3145                 :                :          * anyway, so it's mostly pro-forma to compare them at all.
                               3146                 :                :          */
  167 tgl@sss.pgh.pa.us        3147                 :UNC           0 :         return datum_image_eq(key0, key1,
                               3148                 :              0 :                               context->hash_typbyval, context->hash_typlen);
                               3149                 :                :     }
                               3150                 :                :     else
                               3151                 :                :     {
  167 tgl@sss.pgh.pa.us        3152                 :GNC      179954 :         FunctionCallInfo fcinfo = context->equal_fcinfo;
                               3153                 :                :         Datum       fresult;
                               3154                 :                : 
                               3155                 :                :         /*
                               3156                 :                :          * Apply the operator the correct way around.  Although simplehash.h
                               3157                 :                :          * doesn't document this explicitly, during lookups key0 is from the
                               3158                 :                :          * hash table while key1 is the probe value, so we should compare them
                               3159                 :                :          * in that order only if op_is_reversed.
                               3160                 :                :          */
                               3161         [ +  + ]:         179954 :         if (context->op_is_reversed)
                               3162                 :                :         {
                               3163                 :          45959 :             fcinfo->args[0].value = key0;
                               3164                 :          45959 :             fcinfo->args[1].value = key1;
                               3165                 :                :         }
                               3166                 :                :         else
                               3167                 :                :         {
                               3168                 :         133995 :             fcinfo->args[0].value = key1;
                               3169                 :         133995 :             fcinfo->args[1].value = key0;
                               3170                 :                :         }
                               3171                 :         179954 :         fcinfo->isnull = false;
                               3172                 :         179954 :         fresult = FunctionCallInvoke(fcinfo);
                               3173   [ +  -  +  - ]:         179954 :         return (!fcinfo->isnull && DatumGetBool(fresult));
                               3174                 :                :     }
                               3175                 :                : }
                               3176                 :                : 
                               3177                 :                : /*
                               3178                 :                :  *      neqjoinsel      - Join selectivity of "!="
                               3179                 :                :  */
                               3180                 :                : Datum
 9465 tgl@sss.pgh.pa.us        3181                 :CBC        2952 : neqjoinsel(PG_FUNCTION_ARGS)
                               3182                 :                : {
 7639                          3183                 :           2952 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
 9109                          3184                 :           2952 :     Oid         operator = PG_GETARG_OID(1);
                               3185                 :           2952 :     List       *args = (List *) PG_GETARG_POINTER(2);
 8498                          3186                 :           2952 :     JoinType    jointype = (JoinType) PG_GETARG_INT16(3);
 6471                          3187                 :           2952 :     SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
 2114                          3188                 :           2952 :     Oid         collation = PG_GET_COLLATION();
                               3189                 :                :     float8      result;
                               3190                 :                : 
 3079                          3191   [ +  +  -  + ]:           2952 :     if (jointype == JOIN_SEMI || jointype == JOIN_ANTI)
 9109                          3192                 :           1019 :     {
                               3193                 :                :         /*
                               3194                 :                :          * For semi-joins, if there is more than one distinct value in the RHS
                               3195                 :                :          * relation then every non-null LHS row must find a row to join since
                               3196                 :                :          * it can only be equal to one of them.  We'll assume that there is
                               3197                 :                :          * always more than one distinct RHS value for the sake of stability,
                               3198                 :                :          * though in theory we could have special cases for empty RHS
                               3199                 :                :          * (selectivity = 0) and single-distinct-value RHS (selectivity =
                               3200                 :                :          * fraction of LHS that has the same value as the single RHS value).
                               3201                 :                :          *
                               3202                 :                :          * For anti-joins, if we use the same assumption that there is more
                               3203                 :                :          * than one distinct key in the RHS relation, then every non-null LHS
                               3204                 :                :          * row must be suppressed by the anti-join.
                               3205                 :                :          *
                               3206                 :                :          * So either way, the selectivity estimate should be 1 - nullfrac.
                               3207                 :                :          */
                               3208                 :                :         VariableStatData leftvar;
                               3209                 :                :         VariableStatData rightvar;
                               3210                 :                :         bool        reversed;
                               3211                 :                :         HeapTuple   statsTuple;
                               3212                 :                :         double      nullfrac;
                               3213                 :                : 
 3079                          3214                 :           1019 :         get_join_variables(root, args, sjinfo, &leftvar, &rightvar, &reversed);
                               3215         [ +  + ]:           1019 :         statsTuple = reversed ? rightvar.statsTuple : leftvar.statsTuple;
                               3216         [ +  + ]:           1019 :         if (HeapTupleIsValid(statsTuple))
                               3217                 :            842 :             nullfrac = ((Form_pg_statistic) GETSTRUCT(statsTuple))->stanullfrac;
                               3218                 :                :         else
                               3219                 :            177 :             nullfrac = 0.0;
                               3220         [ +  + ]:           1019 :         ReleaseVariableStats(leftvar);
                               3221         [ +  + ]:           1019 :         ReleaseVariableStats(rightvar);
                               3222                 :                : 
                               3223                 :           1019 :         result = 1.0 - nullfrac;
                               3224                 :                :     }
                               3225                 :                :     else
                               3226                 :                :     {
                               3227                 :                :         /*
                               3228                 :                :          * We want 1 - eqjoinsel() where the equality operator is the one
                               3229                 :                :          * associated with this != operator, that is, its negator.
                               3230                 :                :          */
                               3231                 :           1933 :         Oid         eqop = get_negator(operator);
                               3232                 :                : 
                               3233         [ +  - ]:           1933 :         if (eqop)
                               3234                 :                :         {
                               3235                 :                :             result =
 2114                          3236                 :           1933 :                 DatumGetFloat8(DirectFunctionCall5Coll(eqjoinsel,
                               3237                 :                :                                                        collation,
                               3238                 :                :                                                        PointerGetDatum(root),
                               3239                 :                :                                                        ObjectIdGetDatum(eqop),
                               3240                 :                :                                                        PointerGetDatum(args),
                               3241                 :                :                                                        Int16GetDatum(jointype),
                               3242                 :                :                                                        PointerGetDatum(sjinfo)));
                               3243                 :                :         }
                               3244                 :                :         else
                               3245                 :                :         {
                               3246                 :                :             /* Use default selectivity (should we raise an error instead?) */
 3079 tgl@sss.pgh.pa.us        3247                 :UBC           0 :             result = DEFAULT_EQ_SEL;
                               3248                 :                :         }
 3079 tgl@sss.pgh.pa.us        3249                 :CBC        1933 :         result = 1.0 - result;
                               3250                 :                :     }
                               3251                 :                : 
 9465                          3252                 :           2952 :     PG_RETURN_FLOAT8(result);
                               3253                 :                : }
                               3254                 :                : 
                               3255                 :                : /*
                               3256                 :                :  *      scalarltjoinsel - Join selectivity of "<" for scalars
                               3257                 :                :  */
                               3258                 :                : Datum
                               3259                 :            270 : scalarltjoinsel(PG_FUNCTION_ARGS)
                               3260                 :                : {
                               3261                 :            270 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
                               3262                 :                : }
                               3263                 :                : 
                               3264                 :                : /*
                               3265                 :                :  *      scalarlejoinsel - Join selectivity of "<=" for scalars
                               3266                 :                :  */
                               3267                 :                : Datum
 3156                          3268                 :            198 : scalarlejoinsel(PG_FUNCTION_ARGS)
                               3269                 :                : {
                               3270                 :            198 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
                               3271                 :                : }
                               3272                 :                : 
                               3273                 :                : /*
                               3274                 :                :  *      scalargtjoinsel - Join selectivity of ">" for scalars
                               3275                 :                :  */
                               3276                 :                : Datum
 9465                          3277                 :            240 : scalargtjoinsel(PG_FUNCTION_ARGS)
                               3278                 :                : {
                               3279                 :            240 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
                               3280                 :                : }
                               3281                 :                : 
                               3282                 :                : /*
                               3283                 :                :  *      scalargejoinsel - Join selectivity of ">=" for scalars
                               3284                 :                :  */
                               3285                 :                : Datum
 3156                          3286                 :            152 : scalargejoinsel(PG_FUNCTION_ARGS)
                               3287                 :                : {
                               3288                 :            152 :     PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
                               3289                 :                : }
                               3290                 :                : 
                               3291                 :                : 
                               3292                 :                : /*
                               3293                 :                :  * mergejoinscansel         - Scan selectivity of merge join.
                               3294                 :                :  *
                               3295                 :                :  * A merge join will stop as soon as it exhausts either input stream.
                               3296                 :                :  * Therefore, if we can estimate the ranges of both input variables,
                               3297                 :                :  * we can estimate how much of the input will actually be read.  This
                               3298                 :                :  * can have a considerable impact on the cost when using indexscans.
                               3299                 :                :  *
                               3300                 :                :  * Also, we can estimate how much of each input has to be read before the
                               3301                 :                :  * first join pair is found, which will affect the join's startup time.
                               3302                 :                :  *
                               3303                 :                :  * clause should be a clause already known to be mergejoinable.  opfamily,
                               3304                 :                :  * cmptype, and nulls_first specify the sort ordering being used.
                               3305                 :                :  *
                               3306                 :                :  * The outputs are:
                               3307                 :                :  *      *leftstart is set to the fraction of the left-hand variable expected
                               3308                 :                :  *       to be scanned before the first join pair is found (0 to 1).
                               3309                 :                :  *      *leftend is set to the fraction of the left-hand variable expected
                               3310                 :                :  *       to be scanned before the join terminates (0 to 1).
                               3311                 :                :  *      *rightstart, *rightend similarly for the right-hand variable.
                               3312                 :                :  */
                               3313                 :                : void
 2637                          3314                 :         102901 : mergejoinscansel(PlannerInfo *root, Node *clause,
                               3315                 :                :                  Oid opfamily, CompareType cmptype, bool nulls_first,
                               3316                 :                :                  Selectivity *leftstart, Selectivity *leftend,
                               3317                 :                :                  Selectivity *rightstart, Selectivity *rightend)
                               3318                 :                : {
                               3319                 :                :     Node       *left,
                               3320                 :                :                *right;
                               3321                 :                :     VariableStatData leftvar,
                               3322                 :                :                 rightvar;
                               3323                 :                :     Oid         opmethod;
                               3324                 :                :     int         op_strategy;
                               3325                 :                :     Oid         op_lefttype;
                               3326                 :                :     Oid         op_righttype;
                               3327                 :                :     Oid         opno,
                               3328                 :                :                 collation,
                               3329                 :                :                 lsortop,
                               3330                 :                :                 rsortop,
                               3331                 :                :                 lstatop,
                               3332                 :                :                 rstatop,
                               3333                 :                :                 ltop,
                               3334                 :                :                 leop,
                               3335                 :                :                 revltop,
                               3336                 :                :                 revleop;
                               3337                 :                :     StrategyNumber ltstrat,
                               3338                 :                :                 lestrat,
                               3339                 :                :                 gtstrat,
                               3340                 :                :                 gestrat;
                               3341                 :                :     bool        isgt;
                               3342                 :                :     Datum       leftmin,
                               3343                 :                :                 leftmax,
                               3344                 :                :                 rightmin,
                               3345                 :                :                 rightmax;
                               3346                 :                :     double      selec;
                               3347                 :                : 
                               3348                 :                :     /* Set default results if we can't figure anything out. */
                               3349                 :                :     /* XXX should default "start" fraction be a bit more than 0? */
 6723                          3350                 :         102901 :     *leftstart = *rightstart = 0.0;
                               3351                 :         102901 :     *leftend = *rightend = 1.0;
                               3352                 :                : 
                               3353                 :                :     /* Deconstruct the merge clause */
 8831                          3354         [ -  + ]:         102901 :     if (!is_opclause(clause))
 8831 tgl@sss.pgh.pa.us        3355                 :UBC           0 :         return;                 /* shouldn't happen */
 8545 tgl@sss.pgh.pa.us        3356                 :CBC      102901 :     opno = ((OpExpr *) clause)->opno;
 2160                          3357                 :         102901 :     collation = ((OpExpr *) clause)->inputcollid;
 8113                          3358                 :         102901 :     left = get_leftop((Expr *) clause);
                               3359                 :         102901 :     right = get_rightop((Expr *) clause);
 8831                          3360         [ -  + ]:         102901 :     if (!right)
 8831 tgl@sss.pgh.pa.us        3361                 :UBC           0 :         return;                 /* shouldn't happen */
                               3362                 :                : 
                               3363                 :                :     /* Look for stats for the inputs */
 8113 tgl@sss.pgh.pa.us        3364                 :CBC      102901 :     examine_variable(root, left, 0, &leftvar);
                               3365                 :         102901 :     examine_variable(root, right, 0, &rightvar);
                               3366                 :                : 
  394 peter@eisentraut.org     3367                 :         102901 :     opmethod = get_opfamily_method(opfamily);
                               3368                 :                : 
                               3369                 :                :     /* Extract the operator's declared left/right datatypes */
 5633 tgl@sss.pgh.pa.us        3370                 :         102901 :     get_op_opfamily_properties(opno, opfamily, false,
                               3371                 :                :                                &op_strategy,
                               3372                 :                :                                &op_lefttype,
                               3373                 :                :                                &op_righttype);
  394 peter@eisentraut.org     3374         [ -  + ]:         102901 :     Assert(IndexAmTranslateStrategy(op_strategy, opmethod, opfamily, true) == COMPARE_EQ);
                               3375                 :                : 
                               3376                 :                :     /*
                               3377                 :                :      * Look up the various operators we need.  If we don't find them all, it
                               3378                 :                :      * probably means the opfamily is broken, but we just fail silently.
                               3379                 :                :      *
                               3380                 :                :      * Note: we expect that pg_statistic histograms will be sorted by the '<'
                               3381                 :                :      * operator, regardless of which sort direction we are considering.
                               3382                 :                :      */
                               3383      [ +  +  - ]:         102901 :     switch (cmptype)
                               3384                 :                :     {
                               3385                 :         102872 :         case COMPARE_LT:
 6723 tgl@sss.pgh.pa.us        3386                 :         102872 :             isgt = false;
  394 peter@eisentraut.org     3387                 :         102872 :             ltstrat = IndexAmTranslateCompareType(COMPARE_LT, opmethod, opfamily, true);
                               3388                 :         102872 :             lestrat = IndexAmTranslateCompareType(COMPARE_LE, opmethod, opfamily, true);
 6723 tgl@sss.pgh.pa.us        3389         [ +  + ]:         102872 :             if (op_lefttype == op_righttype)
                               3390                 :                :             {
                               3391                 :                :                 /* easy case */
                               3392                 :         101697 :                 ltop = get_opfamily_member(opfamily,
                               3393                 :                :                                            op_lefttype, op_righttype,
                               3394                 :                :                                            ltstrat);
                               3395                 :         101697 :                 leop = get_opfamily_member(opfamily,
                               3396                 :                :                                            op_lefttype, op_righttype,
                               3397                 :                :                                            lestrat);
                               3398                 :         101697 :                 lsortop = ltop;
                               3399                 :         101697 :                 rsortop = ltop;
                               3400                 :         101697 :                 lstatop = lsortop;
                               3401                 :         101697 :                 rstatop = rsortop;
                               3402                 :         101697 :                 revltop = ltop;
                               3403                 :         101697 :                 revleop = leop;
                               3404                 :                :             }
                               3405                 :                :             else
                               3406                 :                :             {
                               3407                 :           1175 :                 ltop = get_opfamily_member(opfamily,
                               3408                 :                :                                            op_lefttype, op_righttype,
                               3409                 :                :                                            ltstrat);
                               3410                 :           1175 :                 leop = get_opfamily_member(opfamily,
                               3411                 :                :                                            op_lefttype, op_righttype,
                               3412                 :                :                                            lestrat);
                               3413                 :           1175 :                 lsortop = get_opfamily_member(opfamily,
                               3414                 :                :                                               op_lefttype, op_lefttype,
                               3415                 :                :                                               ltstrat);
                               3416                 :           1175 :                 rsortop = get_opfamily_member(opfamily,
                               3417                 :                :                                               op_righttype, op_righttype,
                               3418                 :                :                                               ltstrat);
                               3419                 :           1175 :                 lstatop = lsortop;
                               3420                 :           1175 :                 rstatop = rsortop;
                               3421                 :           1175 :                 revltop = get_opfamily_member(opfamily,
                               3422                 :                :                                               op_righttype, op_lefttype,
                               3423                 :                :                                               ltstrat);
                               3424                 :           1175 :                 revleop = get_opfamily_member(opfamily,
                               3425                 :                :                                               op_righttype, op_lefttype,
                               3426                 :                :                                               lestrat);
                               3427                 :                :             }
 7073                          3428                 :         102872 :             break;
  394 peter@eisentraut.org     3429                 :             29 :         case COMPARE_GT:
                               3430                 :                :             /* descending-order case */
 6723 tgl@sss.pgh.pa.us        3431                 :             29 :             isgt = true;
  394 peter@eisentraut.org     3432                 :             29 :             ltstrat = IndexAmTranslateCompareType(COMPARE_LT, opmethod, opfamily, true);
                               3433                 :             29 :             gtstrat = IndexAmTranslateCompareType(COMPARE_GT, opmethod, opfamily, true);
                               3434                 :             29 :             gestrat = IndexAmTranslateCompareType(COMPARE_GE, opmethod, opfamily, true);
 6723 tgl@sss.pgh.pa.us        3435         [ +  - ]:             29 :             if (op_lefttype == op_righttype)
                               3436                 :                :             {
                               3437                 :                :                 /* easy case */
                               3438                 :             29 :                 ltop = get_opfamily_member(opfamily,
                               3439                 :                :                                            op_lefttype, op_righttype,
                               3440                 :                :                                            gtstrat);
                               3441                 :             29 :                 leop = get_opfamily_member(opfamily,
                               3442                 :                :                                            op_lefttype, op_righttype,
                               3443                 :                :                                            gestrat);
                               3444                 :             29 :                 lsortop = ltop;
                               3445                 :             29 :                 rsortop = ltop;
                               3446                 :             29 :                 lstatop = get_opfamily_member(opfamily,
                               3447                 :                :                                               op_lefttype, op_lefttype,
                               3448                 :                :                                               ltstrat);
                               3449                 :             29 :                 rstatop = lstatop;
                               3450                 :             29 :                 revltop = ltop;
                               3451                 :             29 :                 revleop = leop;
                               3452                 :                :             }
                               3453                 :                :             else
                               3454                 :                :             {
 6723 tgl@sss.pgh.pa.us        3455                 :UBC           0 :                 ltop = get_opfamily_member(opfamily,
                               3456                 :                :                                            op_lefttype, op_righttype,
                               3457                 :                :                                            gtstrat);
                               3458                 :              0 :                 leop = get_opfamily_member(opfamily,
                               3459                 :                :                                            op_lefttype, op_righttype,
                               3460                 :                :                                            gestrat);
                               3461                 :              0 :                 lsortop = get_opfamily_member(opfamily,
                               3462                 :                :                                               op_lefttype, op_lefttype,
                               3463                 :                :                                               gtstrat);
                               3464                 :              0 :                 rsortop = get_opfamily_member(opfamily,
                               3465                 :                :                                               op_righttype, op_righttype,
                               3466                 :                :                                               gtstrat);
                               3467                 :              0 :                 lstatop = get_opfamily_member(opfamily,
                               3468                 :                :                                               op_lefttype, op_lefttype,
                               3469                 :                :                                               ltstrat);
                               3470                 :              0 :                 rstatop = get_opfamily_member(opfamily,
                               3471                 :                :                                               op_righttype, op_righttype,
                               3472                 :                :                                               ltstrat);
                               3473                 :              0 :                 revltop = get_opfamily_member(opfamily,
                               3474                 :                :                                               op_righttype, op_lefttype,
                               3475                 :                :                                               gtstrat);
                               3476                 :              0 :                 revleop = get_opfamily_member(opfamily,
                               3477                 :                :                                               op_righttype, op_lefttype,
                               3478                 :                :                                               gestrat);
                               3479                 :                :             }
 7073 tgl@sss.pgh.pa.us        3480                 :CBC          29 :             break;
 7073 tgl@sss.pgh.pa.us        3481                 :UBC           0 :         default:
                               3482                 :              0 :             goto fail;          /* shouldn't get here */
                               3483                 :                :     }
                               3484                 :                : 
 7073 tgl@sss.pgh.pa.us        3485   [ +  -  +  - ]:CBC      102901 :     if (!OidIsValid(lsortop) ||
                               3486         [ +  - ]:         102901 :         !OidIsValid(rsortop) ||
 6723                          3487         [ +  - ]:         102901 :         !OidIsValid(lstatop) ||
                               3488         [ +  + ]:         102901 :         !OidIsValid(rstatop) ||
                               3489         [ +  - ]:         102891 :         !OidIsValid(ltop) ||
 7073                          3490         [ +  - ]:         102891 :         !OidIsValid(leop) ||
 6723                          3491         [ -  + ]:         102891 :         !OidIsValid(revltop) ||
                               3492                 :                :         !OidIsValid(revleop))
 7073                          3493                 :             10 :         goto fail;              /* insufficient info in catalogs */
                               3494                 :                : 
                               3495                 :                :     /* Try to get ranges of both inputs */
 6723                          3496         [ +  + ]:         102891 :     if (!isgt)
                               3497                 :                :     {
 2160                          3498         [ +  + ]:         102862 :         if (!get_variable_range(root, &leftvar, lstatop, collation,
                               3499                 :                :                                 &leftmin, &leftmax))
 6723                          3500                 :          30876 :             goto fail;          /* no range available from stats */
 2160                          3501         [ +  + ]:          71986 :         if (!get_variable_range(root, &rightvar, rstatop, collation,
                               3502                 :                :                                 &rightmin, &rightmax))
 6723                          3503                 :          17860 :             goto fail;          /* no range available from stats */
                               3504                 :                :     }
                               3505                 :                :     else
                               3506                 :                :     {
                               3507                 :                :         /* need to swap the max and min */
 2160                          3508         [ +  + ]:             29 :         if (!get_variable_range(root, &leftvar, lstatop, collation,
                               3509                 :                :                                 &leftmax, &leftmin))
 6723                          3510                 :             24 :             goto fail;          /* no range available from stats */
 2160                          3511         [ -  + ]:              5 :         if (!get_variable_range(root, &rightvar, rstatop, collation,
                               3512                 :                :                                 &rightmax, &rightmin))
 6723 tgl@sss.pgh.pa.us        3513                 :UBC           0 :             goto fail;          /* no range available from stats */
                               3514                 :                :     }
                               3515                 :                : 
                               3516                 :                :     /*
                               3517                 :                :      * Now, the fraction of the left variable that will be scanned is the
                               3518                 :                :      * fraction that's <= the right-side maximum value.  But only believe
                               3519                 :                :      * non-default estimates, else stick with our 1.0.
                               3520                 :                :      */
 2160 tgl@sss.pgh.pa.us        3521                 :CBC       54131 :     selec = scalarineqsel(root, leop, isgt, true, collation, &leftvar,
                               3522                 :                :                           rightmax, op_righttype);
 8831                          3523         [ +  + ]:          54131 :     if (selec != DEFAULT_INEQ_SEL)
 6723                          3524                 :          54127 :         *leftend = selec;
                               3525                 :                : 
                               3526                 :                :     /* And similarly for the right variable. */
 2160                          3527                 :          54131 :     selec = scalarineqsel(root, revleop, isgt, true, collation, &rightvar,
                               3528                 :                :                           leftmax, op_lefttype);
 8831                          3529         [ +  - ]:          54131 :     if (selec != DEFAULT_INEQ_SEL)
 6723                          3530                 :          54131 :         *rightend = selec;
                               3531                 :                : 
                               3532                 :                :     /*
                               3533                 :                :      * Only one of the two "end" fractions can really be less than 1.0;
                               3534                 :                :      * believe the smaller estimate and reset the other one to exactly 1.0. If
                               3535                 :                :      * we get exactly equal estimates (as can easily happen with self-joins),
                               3536                 :                :      * believe neither.
                               3537                 :                :      */
                               3538         [ +  + ]:          54131 :     if (*leftend > *rightend)
                               3539                 :          16567 :         *leftend = 1.0;
                               3540         [ +  + ]:          37564 :     else if (*leftend < *rightend)
                               3541                 :          20597 :         *rightend = 1.0;
                               3542                 :                :     else
                               3543                 :          16967 :         *leftend = *rightend = 1.0;
                               3544                 :                : 
                               3545                 :                :     /*
                               3546                 :                :      * Also, the fraction of the left variable that will be scanned before the
                               3547                 :                :      * first join pair is found is the fraction that's < the right-side
                               3548                 :                :      * minimum value.  But only believe non-default estimates, else stick with
                               3549                 :                :      * our own default.
                               3550                 :                :      */
 2160                          3551                 :          54131 :     selec = scalarineqsel(root, ltop, isgt, false, collation, &leftvar,
                               3552                 :                :                           rightmin, op_righttype);
 6723                          3553         [ +  - ]:          54131 :     if (selec != DEFAULT_INEQ_SEL)
                               3554                 :          54131 :         *leftstart = selec;
                               3555                 :                : 
                               3556                 :                :     /* And similarly for the right variable. */
 2160                          3557                 :          54131 :     selec = scalarineqsel(root, revltop, isgt, false, collation, &rightvar,
                               3558                 :                :                           leftmin, op_lefttype);
 6723                          3559         [ +  - ]:          54131 :     if (selec != DEFAULT_INEQ_SEL)
                               3560                 :          54131 :         *rightstart = selec;
                               3561                 :                : 
                               3562                 :                :     /*
                               3563                 :                :      * Only one of the two "start" fractions can really be more than zero;
                               3564                 :                :      * believe the larger estimate and reset the other one to exactly 0.0. If
                               3565                 :                :      * we get exactly equal estimates (as can easily happen with self-joins),
                               3566                 :                :      * believe neither.
                               3567                 :                :      */
                               3568         [ +  + ]:          54131 :     if (*leftstart < *rightstart)
                               3569                 :          11573 :         *leftstart = 0.0;
                               3570         [ +  + ]:          42558 :     else if (*leftstart > *rightstart)
                               3571                 :          17473 :         *rightstart = 0.0;
                               3572                 :                :     else
                               3573                 :          25085 :         *leftstart = *rightstart = 0.0;
                               3574                 :                : 
                               3575                 :                :     /*
                               3576                 :                :      * If the sort order is nulls-first, we're going to have to skip over any
                               3577                 :                :      * nulls too.  These would not have been counted by scalarineqsel, and we
                               3578                 :                :      * can safely add in this fraction regardless of whether we believe
                               3579                 :                :      * scalarineqsel's results or not.  But be sure to clamp the sum to 1.0!
                               3580                 :                :      */
                               3581         [ +  + ]:          54131 :     if (nulls_first)
                               3582                 :                :     {
                               3583                 :                :         Form_pg_statistic stats;
                               3584                 :                : 
                               3585         [ +  - ]:              5 :         if (HeapTupleIsValid(leftvar.statsTuple))
                               3586                 :                :         {
                               3587                 :              5 :             stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
                               3588                 :              5 :             *leftstart += stats->stanullfrac;
                               3589   [ -  +  -  + ]:              5 :             CLAMP_PROBABILITY(*leftstart);
                               3590                 :              5 :             *leftend += stats->stanullfrac;
                               3591   [ -  +  -  + ]:              5 :             CLAMP_PROBABILITY(*leftend);
                               3592                 :                :         }
                               3593         [ +  - ]:              5 :         if (HeapTupleIsValid(rightvar.statsTuple))
                               3594                 :                :         {
 7043                          3595                 :              5 :             stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
 6723                          3596                 :              5 :             *rightstart += stats->stanullfrac;
                               3597   [ -  +  -  + ]:              5 :             CLAMP_PROBABILITY(*rightstart);
                               3598                 :              5 :             *rightend += stats->stanullfrac;
                               3599   [ -  +  -  + ]:              5 :             CLAMP_PROBABILITY(*rightend);
                               3600                 :                :         }
                               3601                 :                :     }
                               3602                 :                : 
                               3603                 :                :     /* Disbelieve start >= end, just in case that can happen */
                               3604         [ +  + ]:          54131 :     if (*leftstart >= *leftend)
                               3605                 :                :     {
                               3606                 :             97 :         *leftstart = 0.0;
                               3607                 :             97 :         *leftend = 1.0;
                               3608                 :                :     }
                               3609         [ +  + ]:          54131 :     if (*rightstart >= *rightend)
                               3610                 :                :     {
                               3611                 :            770 :         *rightstart = 0.0;
                               3612                 :            770 :         *rightend = 1.0;
                               3613                 :                :     }
                               3614                 :                : 
 8113                          3615                 :          53361 : fail:
                               3616         [ +  + ]:         102901 :     ReleaseVariableStats(leftvar);
                               3617         [ +  + ]:         102901 :     ReleaseVariableStats(rightvar);
                               3618                 :                : }
                               3619                 :                : 
                               3620                 :                : 
                               3621                 :                : /*
                               3622                 :                :  *  matchingsel -- generic matching-operator selectivity support
                               3623                 :                :  *
                               3624                 :                :  * Use these for any operators that (a) are on data types for which we collect
                               3625                 :                :  * standard statistics, and (b) have behavior for which the default estimate
                               3626                 :                :  * (twice DEFAULT_EQ_SEL) is sane.  Typically that is good for match-like
                               3627                 :                :  * operators.
                               3628                 :                :  */
                               3629                 :                : 
                               3630                 :                : Datum
 2225                          3631                 :            845 : matchingsel(PG_FUNCTION_ARGS)
                               3632                 :                : {
                               3633                 :            845 :     PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
                               3634                 :            845 :     Oid         operator = PG_GETARG_OID(1);
                               3635                 :            845 :     List       *args = (List *) PG_GETARG_POINTER(2);
                               3636                 :            845 :     int         varRelid = PG_GETARG_INT32(3);
 2160                          3637                 :            845 :     Oid         collation = PG_GET_COLLATION();
                               3638                 :                :     double      selec;
                               3639                 :                : 
                               3640                 :                :     /* Use generic restriction selectivity logic. */
                               3641                 :            845 :     selec = generic_restriction_selectivity(root, operator, collation,
                               3642                 :                :                                             args, varRelid,
                               3643                 :                :                                             DEFAULT_MATCHING_SEL);
                               3644                 :                : 
 2225                          3645                 :            845 :     PG_RETURN_FLOAT8((float8) selec);
                               3646                 :                : }
                               3647                 :                : 
                               3648                 :                : Datum
                               3649                 :              5 : matchingjoinsel(PG_FUNCTION_ARGS)
                               3650                 :                : {
                               3651                 :                :     /* Just punt, for the moment. */
                               3652                 :              5 :     PG_RETURN_FLOAT8(DEFAULT_MATCHING_SEL);
                               3653                 :                : }
                               3654                 :                : 
                               3655                 :                : 
                               3656                 :                : /*
                               3657                 :                :  * Helper routine for estimate_num_groups: add an item to a list of
                               3658                 :                :  * GroupVarInfos, but only if it's not known equal to any of the existing
                               3659                 :                :  * entries.
                               3660                 :                :  */
                               3661                 :                : typedef struct
                               3662                 :                : {
                               3663                 :                :     Node       *var;            /* might be an expression, not just a Var */
                               3664                 :                :     RelOptInfo *rel;            /* relation it belongs to */
                               3665                 :                :     double      ndistinct;      /* # distinct values */
                               3666                 :                :     bool        isdefault;      /* true if DEFAULT_NUM_DISTINCT was used */
                               3667                 :                : } GroupVarInfo;
                               3668                 :                : 
                               3669                 :                : static List *
 7639                          3670                 :         281067 : add_unique_group_var(PlannerInfo *root, List *varinfos,
                               3671                 :                :                      Node *var, VariableStatData *vardata)
                               3672                 :                : {
                               3673                 :                :     GroupVarInfo *varinfo;
                               3674                 :                :     double      ndistinct;
                               3675                 :                :     bool        isdefault;
                               3676                 :                :     ListCell   *lc;
                               3677                 :                : 
 5357                          3678                 :         281067 :     ndistinct = get_variable_numdistinct(vardata, &isdefault);
                               3679                 :                : 
                               3680                 :                :     /*
                               3681                 :                :      * The nullingrels bits within the var could cause the same var to be
                               3682                 :                :      * counted multiple times if it's marked with different nullingrels.  They
                               3683                 :                :      * could also prevent us from matching the var to the expressions in
                               3684                 :                :      * extended statistics (see estimate_multivariate_ndistinct).  So strip
                               3685                 :                :      * them out first.
                               3686                 :                :      */
  488 rguo@postgresql.org      3687                 :         281067 :     var = remove_nulling_relids(var, root->outer_join_rels, NULL);
                               3688                 :                : 
 2486 tgl@sss.pgh.pa.us        3689   [ +  +  +  +  :         347056 :     foreach(lc, varinfos)
                                              +  + ]
                               3690                 :                :     {
 7899                          3691                 :          66765 :         varinfo = (GroupVarInfo *) lfirst(lc);
                               3692                 :                : 
                               3693                 :                :         /* Drop exact duplicates */
                               3694         [ +  + ]:          66765 :         if (equal(var, varinfo->var))
                               3695                 :            776 :             return varinfos;
                               3696                 :                : 
                               3697                 :                :         /*
                               3698                 :                :          * Drop known-equal vars, but only if they belong to different
                               3699                 :                :          * relations (see comments for estimate_num_groups).  We aren't too
                               3700                 :                :          * fussy about the semantics of "equal" here.
                               3701                 :                :          */
                               3702   [ +  +  +  + ]:          70307 :         if (vardata->rel != varinfo->rel &&
  644 rguo@postgresql.org      3703                 :           4124 :             exprs_known_equal(root, var, varinfo->var, InvalidOid))
                               3704                 :                :         {
 7899 tgl@sss.pgh.pa.us        3705         [ +  + ]:            210 :             if (varinfo->ndistinct <= ndistinct)
                               3706                 :                :             {
                               3707                 :                :                 /* Keep older item, forget new one */
                               3708                 :            194 :                 return varinfos;
                               3709                 :                :             }
                               3710                 :                :             else
                               3711                 :                :             {
                               3712                 :                :                 /* Delete the older item */
 2486                          3713                 :             16 :                 varinfos = foreach_delete_current(varinfos, lc);
                               3714                 :                :             }
                               3715                 :                :         }
                               3716                 :                :     }
                               3717                 :                : 
  146 michael@paquier.xyz      3718                 :GNC      280291 :     varinfo = palloc_object(GroupVarInfo);
                               3719                 :                : 
 7899 tgl@sss.pgh.pa.us        3720                 :CBC      280291 :     varinfo->var = var;
                               3721                 :         280291 :     varinfo->rel = vardata->rel;
                               3722                 :         280291 :     varinfo->ndistinct = ndistinct;
 1862 drowley@postgresql.o     3723                 :         280291 :     varinfo->isdefault = isdefault;
 7899 tgl@sss.pgh.pa.us        3724                 :         280291 :     varinfos = lappend(varinfos, varinfo);
                               3725                 :         280291 :     return varinfos;
                               3726                 :                : }
                               3727                 :                : 
                               3728                 :                : /*
                               3729                 :                :  * estimate_num_groups      - Estimate number of groups in a grouped query
                               3730                 :                :  *
                               3731                 :                :  * Given a query having a GROUP BY clause, estimate how many groups there
                               3732                 :                :  * will be --- ie, the number of distinct combinations of the GROUP BY
                               3733                 :                :  * expressions.
                               3734                 :                :  *
                               3735                 :                :  * This routine is also used to estimate the number of rows emitted by
                               3736                 :                :  * a DISTINCT filtering step; that is an isomorphic problem.  (Note:
                               3737                 :                :  * actually, we only use it for DISTINCT when there's no grouping or
                               3738                 :                :  * aggregation ahead of the DISTINCT.)
                               3739                 :                :  *
                               3740                 :                :  * Inputs:
                               3741                 :                :  *  root - the query
                               3742                 :                :  *  groupExprs - list of expressions being grouped by
                               3743                 :                :  *  input_rows - number of rows estimated to arrive at the group/unique
                               3744                 :                :  *      filter step
                               3745                 :                :  *  pgset - NULL, or a List** pointing to a grouping set to filter the
                               3746                 :                :  *      groupExprs against
                               3747                 :                :  *
                               3748                 :                :  * Outputs:
                               3749                 :                :  *  estinfo - When passed as non-NULL, the function will set bits in the
                               3750                 :                :  *      "flags" field in order to provide callers with additional information
                               3751                 :                :  *      about the estimation.  Currently, we only set the SELFLAG_USED_DEFAULT
                               3752                 :                :  *      bit if we used any default values in the estimation.
                               3753                 :                :  *
                               3754                 :                :  * Given the lack of any cross-correlation statistics in the system, it's
                               3755                 :                :  * impossible to do anything really trustworthy with GROUP BY conditions
                               3756                 :                :  * involving multiple Vars.  We should however avoid assuming the worst
                               3757                 :                :  * case (all possible cross-product terms actually appear as groups) since
                               3758                 :                :  * very often the grouped-by Vars are highly correlated.  Our current approach
                               3759                 :                :  * is as follows:
                               3760                 :                :  *  1.  Expressions yielding boolean are assumed to contribute two groups,
                               3761                 :                :  *      independently of their content, and are ignored in the subsequent
                               3762                 :                :  *      steps.  This is mainly because tests like "col IS NULL" break the
                               3763                 :                :  *      heuristic used in step 2 especially badly.
                               3764                 :                :  *  2.  Reduce the given expressions to a list of unique Vars used.  For
                               3765                 :                :  *      example, GROUP BY a, a + b is treated the same as GROUP BY a, b.
                               3766                 :                :  *      It is clearly correct not to count the same Var more than once.
                               3767                 :                :  *      It is also reasonable to treat f(x) the same as x: f() cannot
                               3768                 :                :  *      increase the number of distinct values (unless it is volatile,
                               3769                 :                :  *      which we consider unlikely for grouping), but it probably won't
                               3770                 :                :  *      reduce the number of distinct values much either.
                               3771                 :                :  *      As a special case, if a GROUP BY expression can be matched to an
                               3772                 :                :  *      expressional index for which we have statistics, then we treat the
                               3773                 :                :  *      whole expression as though it were just a Var.
                               3774                 :                :  *  3.  If the list contains Vars of different relations that are known equal
                               3775                 :                :  *      due to equivalence classes, then drop all but one of the Vars from each
                               3776                 :                :  *      known-equal set, keeping the one with smallest estimated # of values
                               3777                 :                :  *      (since the extra values of the others can't appear in joined rows).
                               3778                 :                :  *      Note the reason we only consider Vars of different relations is that
                               3779                 :                :  *      if we considered ones of the same rel, we'd be double-counting the
                               3780                 :                :  *      restriction selectivity of the equality in the next step.
                               3781                 :                :  *  4.  For Vars within a single source rel, we multiply together the numbers
                               3782                 :                :  *      of values, clamp to the number of rows in the rel (divided by 10 if
                               3783                 :                :  *      more than one Var), and then multiply by a factor based on the
                               3784                 :                :  *      selectivity of the restriction clauses for that rel.  When there's
                               3785                 :                :  *      more than one Var, the initial product is probably too high (it's the
                               3786                 :                :  *      worst case) but clamping to a fraction of the rel's rows seems to be a
                               3787                 :                :  *      helpful heuristic for not letting the estimate get out of hand.  (The
                               3788                 :                :  *      factor of 10 is derived from pre-Postgres-7.4 practice.)  The factor
                               3789                 :                :  *      we multiply by to adjust for the restriction selectivity assumes that
                               3790                 :                :  *      the restriction clauses are independent of the grouping, which may not
                               3791                 :                :  *      be a valid assumption, but it's hard to do better.
                               3792                 :                :  *  5.  If there are Vars from multiple rels, we repeat step 4 for each such
                               3793                 :                :  *      rel, and multiply the results together.
                               3794                 :                :  * Note that rels not containing grouped Vars are ignored completely, as are
                               3795                 :                :  * join clauses.  Such rels cannot increase the number of groups, and we
                               3796                 :                :  * assume such clauses do not reduce the number either (somewhat bogus,
                               3797                 :                :  * but we don't have the info to do better).
                               3798                 :                :  */
                               3799                 :                : double
 4007 andres@anarazel.de       3800                 :         243848 : estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows,
                               3801                 :                :                     List **pgset, EstimationInfo *estinfo)
                               3802                 :                : {
 1310 tgl@sss.pgh.pa.us        3803                 :         243848 :     List       *varinfos = NIL;
 3083                          3804                 :         243848 :     double      srf_multiplier = 1.0;
                               3805                 :                :     double      numdistinct;
                               3806                 :                :     ListCell   *l;
                               3807                 :                :     int         i;
                               3808                 :                : 
                               3809                 :                :     /* Zero the estinfo output parameter, if non-NULL */
 1862 drowley@postgresql.o     3810         [ +  + ]:         243848 :     if (estinfo != NULL)
                               3811                 :         203204 :         memset(estinfo, 0, sizeof(EstimationInfo));
                               3812                 :                : 
                               3813                 :                :     /*
                               3814                 :                :      * We don't ever want to return an estimate of zero groups, as that tends
                               3815                 :                :      * to lead to division-by-zero and other unpleasantness.  The input_rows
                               3816                 :                :      * estimate is usually already at least 1, but clamp it just in case it
                               3817                 :                :      * isn't.
                               3818                 :                :      */
 4743 tgl@sss.pgh.pa.us        3819                 :         243848 :     input_rows = clamp_row_est(input_rows);
                               3820                 :                : 
                               3821                 :                :     /*
                               3822                 :                :      * If no grouping columns, there's exactly one group.  (This can't happen
                               3823                 :                :      * for normal cases with GROUP BY or DISTINCT, but it is possible for
                               3824                 :                :      * corner cases with set operations.)
                               3825                 :                :      */
 1357                          3826   [ +  +  +  +  :         243848 :     if (groupExprs == NIL || (pgset && *pgset == NIL))
                                              +  + ]
 5139                          3827                 :            923 :         return 1.0;
                               3828                 :                : 
                               3829                 :                :     /*
                               3830                 :                :      * Count groups derived from boolean grouping expressions.  For other
                               3831                 :                :      * expressions, find the unique Vars used, treating an expression as a Var
                               3832                 :                :      * if we can find stats for it.  For each one, record the statistical
                               3833                 :                :      * estimate of number of distinct values (total in its table, without
                               3834                 :                :      * regard for filtering).
                               3835                 :                :      */
 6511                          3836                 :         242925 :     numdistinct = 1.0;
                               3837                 :                : 
 1310                          3838                 :         242925 :     i = 0;
 8506                          3839   [ +  -  +  +  :         523375 :     foreach(l, groupExprs)
                                              +  + ]
                               3840                 :                :     {
                               3841                 :         280490 :         Node       *groupexpr = (Node *) lfirst(l);
                               3842                 :                :         double      this_srf_multiplier;
                               3843                 :                :         VariableStatData vardata;
                               3844                 :                :         List       *varshere;
                               3845                 :                :         ListCell   *l2;
                               3846                 :                : 
                               3847                 :                :         /* is expression in this grouping set? */
 4007 andres@anarazel.de       3848   [ +  +  +  + ]:         280490 :         if (pgset && !list_member_int(*pgset, i++))
                               3849                 :         222919 :             continue;
                               3850                 :                : 
                               3851                 :                :         /*
                               3852                 :                :          * Set-returning functions in grouping columns are a bit problematic.
                               3853                 :                :          * The code below will effectively ignore their SRF nature and come up
                               3854                 :                :          * with a numdistinct estimate as though they were scalar functions.
                               3855                 :                :          * We compensate by scaling up the end result by the largest SRF
                               3856                 :                :          * rowcount estimate.  (This will be an overestimate if the SRF
                               3857                 :                :          * produces multiple copies of any output value, but it seems best to
                               3858                 :                :          * assume the SRF's outputs are distinct.  In any case, it's probably
                               3859                 :                :          * pointless to worry too much about this without much better
                               3860                 :                :          * estimates for SRF output rowcounts than we have today.)
                               3861                 :                :          */
 2642 tgl@sss.pgh.pa.us        3862                 :         279822 :         this_srf_multiplier = expression_returns_set_rows(root, groupexpr);
 3083                          3863         [ +  + ]:         279822 :         if (srf_multiplier < this_srf_multiplier)
                               3864                 :            130 :             srf_multiplier = this_srf_multiplier;
                               3865                 :                : 
                               3866                 :                :         /* Short-circuit for expressions returning boolean */
 6511                          3867         [ +  + ]:         279822 :         if (exprType(groupexpr) == BOOLOID)
                               3868                 :                :         {
                               3869                 :            292 :             numdistinct *= 2.0;
                               3870                 :            292 :             continue;
                               3871                 :                :         }
                               3872                 :                : 
                               3873                 :                :         /*
                               3874                 :                :          * If examine_variable is able to deduce anything about the GROUP BY
                               3875                 :                :          * expression, treat it as a single variable even if it's really more
                               3876                 :                :          * complicated.
                               3877                 :                :          *
                               3878                 :                :          * XXX This has the consequence that if there's a statistics object on
                               3879                 :                :          * the expression, we don't split it into individual Vars. This
                               3880                 :                :          * affects our selection of statistics in
                               3881                 :                :          * estimate_multivariate_ndistinct, because it's probably better to
                               3882                 :                :          * use more accurate estimate for each expression and treat them as
                               3883                 :                :          * independent, than to combine estimates for the extracted variables
                               3884                 :                :          * when we don't know how that relates to the expressions.
                               3885                 :                :          */
 7899                          3886                 :         279530 :         examine_variable(root, groupexpr, 0, &vardata);
 6428                          3887   [ +  +  +  + ]:         279530 :         if (HeapTupleIsValid(vardata.statsTuple) || vardata.isunique)
                               3888                 :                :         {
 7899                          3889                 :         221461 :             varinfos = add_unique_group_var(root, varinfos,
                               3890                 :                :                                             groupexpr, &vardata);
                               3891         [ +  + ]:         221461 :             ReleaseVariableStats(vardata);
                               3892                 :         221461 :             continue;
                               3893                 :                :         }
                               3894         [ -  + ]:          58069 :         ReleaseVariableStats(vardata);
                               3895                 :                : 
                               3896                 :                :         /*
                               3897                 :                :          * Else pull out the component Vars.  Handle PlaceHolderVars by
                               3898                 :                :          * recursing into their arguments (effectively assuming that the
                               3899                 :                :          * PlaceHolderVar doesn't change the number of groups, which boils
                               3900                 :                :          * down to ignoring the possible addition of nulls to the result set).
                               3901                 :                :          */
 5411                          3902                 :          58069 :         varshere = pull_var_clause(groupexpr,
                               3903                 :                :                                    PVC_RECURSE_AGGREGATES |
                               3904                 :                :                                    PVC_RECURSE_WINDOWFUNCS |
                               3905                 :                :                                    PVC_RECURSE_PLACEHOLDERS);
                               3906                 :                : 
                               3907                 :                :         /*
                               3908                 :                :          * If we find any variable-free GROUP BY item, then either it is a
                               3909                 :                :          * constant (and we can ignore it) or it contains a volatile function;
                               3910                 :                :          * in the latter case we punt and assume that each input row will
                               3911                 :                :          * yield a distinct group.
                               3912                 :                :          */
 8568                          3913         [ +  + ]:          58069 :         if (varshere == NIL)
                               3914                 :                :         {
                               3915         [ +  + ]:            538 :             if (contain_volatile_functions(groupexpr))
                               3916                 :             40 :                 return input_rows;
                               3917                 :            498 :             continue;
                               3918                 :                :         }
                               3919                 :                : 
                               3920                 :                :         /*
                               3921                 :                :          * Else add variables to varinfos list
                               3922                 :                :          */
 7899                          3923   [ +  -  +  +  :         117137 :         foreach(l2, varshere)
                                              +  + ]
                               3924                 :                :         {
                               3925                 :          59606 :             Node       *var = (Node *) lfirst(l2);
                               3926                 :                : 
                               3927                 :          59606 :             examine_variable(root, var, 0, &vardata);
                               3928                 :          59606 :             varinfos = add_unique_group_var(root, varinfos, var, &vardata);
                               3929         [ +  + ]:          59606 :             ReleaseVariableStats(vardata);
                               3930                 :                :         }
                               3931                 :                :     }
                               3932                 :                : 
                               3933                 :                :     /*
                               3934                 :                :      * If now no Vars, we must have an all-constant or all-boolean GROUP BY
                               3935                 :                :      * list.
                               3936                 :                :      */
                               3937         [ +  + ]:         242885 :     if (varinfos == NIL)
                               3938                 :                :     {
                               3939                 :                :         /* Apply SRF multiplier as we would do in the long path */
 3083                          3940                 :            385 :         numdistinct *= srf_multiplier;
                               3941                 :                :         /* Round off */
                               3942                 :            385 :         numdistinct = ceil(numdistinct);
                               3943                 :                :         /* Guard against out-of-range answers */
 6511                          3944         [ +  + ]:            385 :         if (numdistinct > input_rows)
                               3945                 :             71 :             numdistinct = input_rows;
 3083                          3946         [ -  + ]:            385 :         if (numdistinct < 1.0)
 3083 tgl@sss.pgh.pa.us        3947                 :UBC           0 :             numdistinct = 1.0;
 6511 tgl@sss.pgh.pa.us        3948                 :CBC         385 :         return numdistinct;
                               3949                 :                :     }
                               3950                 :                : 
                               3951                 :                :     /*
                               3952                 :                :      * Group Vars by relation and estimate total numdistinct.
                               3953                 :                :      *
                               3954                 :                :      * For each iteration of the outer loop, we process the frontmost Var in
                               3955                 :                :      * varinfos, plus all other Vars in the same relation.  We remove these
                               3956                 :                :      * Vars from the newvarinfos list for the next iteration. This is the
                               3957                 :                :      * easiest way to group Vars of same rel together.
                               3958                 :                :      */
                               3959                 :                :     do
                               3960                 :                :     {
 7899                          3961                 :         244665 :         GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
                               3962                 :         244665 :         RelOptInfo *rel = varinfo1->rel;
 3329 alvherre@alvh.no-ip.     3963                 :         244665 :         double      reldistinct = 1;
 7763 tgl@sss.pgh.pa.us        3964                 :         244665 :         double      relmaxndistinct = reldistinct;
 3326 alvherre@alvh.no-ip.     3965                 :         244665 :         int         relvarcount = 0;
 8310 bruce@momjian.us         3966                 :         244665 :         List       *newvarinfos = NIL;
 3329 alvherre@alvh.no-ip.     3967                 :         244665 :         List       *relvarinfos = NIL;
                               3968                 :                : 
                               3969                 :                :         /*
                               3970                 :                :          * Split the list of varinfos in two - one for the current rel, one
                               3971                 :                :          * for remaining Vars on other rels.
                               3972                 :                :          */
 2484 tgl@sss.pgh.pa.us        3973                 :         244665 :         relvarinfos = lappend(relvarinfos, varinfo1);
 2045                          3974   [ +  -  +  +  :         283418 :         for_each_from(l, varinfos, 1)
                                              +  + ]
                               3975                 :                :         {
 7899                          3976                 :          38753 :             GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
                               3977                 :                : 
                               3978         [ +  + ]:          38753 :             if (varinfo2->rel == varinfo1->rel)
                               3979                 :                :             {
                               3980                 :                :                 /* varinfos on current rel */
 2484                          3981                 :          35610 :                 relvarinfos = lappend(relvarinfos, varinfo2);
                               3982                 :                :             }
                               3983                 :                :             else
                               3984                 :                :             {
                               3985                 :                :                 /* not time to process varinfo2 yet */
                               3986                 :           3143 :                 newvarinfos = lappend(newvarinfos, varinfo2);
                               3987                 :                :             }
                               3988                 :                :         }
                               3989                 :                : 
                               3990                 :                :         /*
                               3991                 :                :          * Get the numdistinct estimate for the Vars of this rel.  We
                               3992                 :                :          * iteratively search for multivariate n-distinct with maximum number
                               3993                 :                :          * of vars; assuming that each var group is independent of the others,
                               3994                 :                :          * we multiply them together.  Any remaining relvarinfos after no more
                               3995                 :                :          * multivariate matches are found are assumed independent too, so
                               3996                 :                :          * their individual ndistinct estimates are multiplied also.
                               3997                 :                :          *
                               3998                 :                :          * While iterating, count how many separate numdistinct values we
                               3999                 :                :          * apply.  We apply a fudge factor below, but only if we multiplied
                               4000                 :                :          * more than one such values.
                               4001                 :                :          */
 3329 alvherre@alvh.no-ip.     4002         [ +  + ]:         489435 :         while (relvarinfos)
                               4003                 :                :         {
                               4004                 :                :             double      mvndistinct;
                               4005                 :                : 
                               4006         [ +  + ]:         244770 :             if (estimate_multivariate_ndistinct(root, rel, &relvarinfos,
                               4007                 :                :                                                 &mvndistinct))
                               4008                 :                :             {
                               4009                 :            345 :                 reldistinct *= mvndistinct;
                               4010         [ +  + ]:            345 :                 if (relmaxndistinct < mvndistinct)
                               4011                 :            335 :                     relmaxndistinct = mvndistinct;
 3326                          4012                 :            345 :                 relvarcount++;
                               4013                 :                :             }
                               4014                 :                :             else
                               4015                 :                :             {
 3275 bruce@momjian.us         4016   [ +  -  +  +  :         523970 :                 foreach(l, relvarinfos)
                                              +  + ]
                               4017                 :                :                 {
 3329 alvherre@alvh.no-ip.     4018                 :         279545 :                     GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
                               4019                 :                : 
                               4020                 :         279545 :                     reldistinct *= varinfo2->ndistinct;
                               4021         [ +  + ]:         279545 :                     if (relmaxndistinct < varinfo2->ndistinct)
                               4022                 :         248326 :                         relmaxndistinct = varinfo2->ndistinct;
                               4023                 :         279545 :                     relvarcount++;
                               4024                 :                : 
                               4025                 :                :                     /*
                               4026                 :                :                      * When varinfo2's isdefault is set then we'd better set
                               4027                 :                :                      * the SELFLAG_USED_DEFAULT bit in the EstimationInfo.
                               4028                 :                :                      */
 1862 drowley@postgresql.o     4029   [ +  +  +  + ]:         279545 :                     if (estinfo != NULL && varinfo2->isdefault)
                               4030                 :          16908 :                         estinfo->flags |= SELFLAG_USED_DEFAULT;
                               4031                 :                :                 }
                               4032                 :                : 
                               4033                 :                :                 /* we're done with this relation */
 3329 alvherre@alvh.no-ip.     4034                 :         244425 :                 relvarinfos = NIL;
                               4035                 :                :             }
                               4036                 :                :         }
                               4037                 :                : 
                               4038                 :                :         /*
                               4039                 :                :          * Sanity check --- don't divide by zero if empty relation.
                               4040                 :                :          */
 3319 rhaas@postgresql.org     4041   [ +  +  -  + ]:         244665 :         Assert(IS_SIMPLE_REL(rel));
 8237 tgl@sss.pgh.pa.us        4042         [ +  + ]:         244665 :         if (rel->tuples > 0)
                               4043                 :                :         {
                               4044                 :                :             /*
                               4045                 :                :              * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
                               4046                 :                :              * fudge factor is because the Vars are probably correlated but we
                               4047                 :                :              * don't know by how much.  We should never clamp to less than the
                               4048                 :                :              * largest ndistinct value for any of the Vars, though, since
                               4049                 :                :              * there will surely be at least that many groups.
                               4050                 :                :              */
 7767                          4051                 :         243800 :             double      clamp = rel->tuples;
                               4052                 :                : 
                               4053         [ +  + ]:         243800 :             if (relvarcount > 1)
                               4054                 :                :             {
                               4055                 :          31151 :                 clamp *= 0.1;
 7763                          4056         [ +  + ]:          31151 :                 if (clamp < relmaxndistinct)
                               4057                 :                :                 {
                               4058                 :          29009 :                     clamp = relmaxndistinct;
                               4059                 :                :                     /* for sanity in case some ndistinct is too large: */
                               4060         [ +  + ]:          29009 :                     if (clamp > rel->tuples)
                               4061                 :             65 :                         clamp = rel->tuples;
                               4062                 :                :                 }
                               4063                 :                :             }
 7767                          4064         [ +  + ]:         243800 :             if (reldistinct > clamp)
                               4065                 :          25438 :                 reldistinct = clamp;
                               4066                 :                : 
                               4067                 :                :             /*
                               4068                 :                :              * Update the estimate based on the restriction selectivity,
                               4069                 :                :              * guarding against division by zero when reldistinct is zero.
                               4070                 :                :              * Also skip this if we know that we are returning all rows.
                               4071                 :                :              */
 3683 dean.a.rasheed@gmail     4072   [ +  -  +  + ]:         243800 :             if (reldistinct > 0 && rel->rows < rel->tuples)
                               4073                 :                :             {
                               4074                 :                :                 /*
                               4075                 :                :                  * Given a table containing N rows with n distinct values in a
                               4076                 :                :                  * uniform distribution, if we select p rows at random then
                               4077                 :                :                  * the expected number of distinct values selected is
                               4078                 :                :                  *
                               4079                 :                :                  * n * (1 - product((N-N/n-i)/(N-i), i=0..p-1))
                               4080                 :                :                  *
                               4081                 :                :                  * = n * (1 - (N-N/n)! / (N-N/n-p)! * (N-p)! / N!)
                               4082                 :                :                  *
                               4083                 :                :                  * See "Approximating block accesses in database
                               4084                 :                :                  * organizations", S. B. Yao, Communications of the ACM,
                               4085                 :                :                  * Volume 20 Issue 4, April 1977 Pages 260-261.
                               4086                 :                :                  *
                               4087                 :                :                  * Alternatively, re-arranging the terms from the factorials,
                               4088                 :                :                  * this may be written as
                               4089                 :                :                  *
                               4090                 :                :                  * n * (1 - product((N-p-i)/(N-i), i=0..N/n-1))
                               4091                 :                :                  *
                               4092                 :                :                  * This form of the formula is more efficient to compute in
                               4093                 :                :                  * the common case where p is larger than N/n.  Additionally,
                               4094                 :                :                  * as pointed out by Dell'Era, if i << N for all terms in the
                               4095                 :                :                  * product, it can be approximated by
                               4096                 :                :                  *
                               4097                 :                :                  * n * (1 - ((N-p)/N)^(N/n))
                               4098                 :                :                  *
                               4099                 :                :                  * See "Expected distinct values when selecting from a bag
                               4100                 :                :                  * without replacement", Alberto Dell'Era,
                               4101                 :                :                  * http://www.adellera.it/investigations/distinct_balls/.
                               4102                 :                :                  *
                               4103                 :                :                  * The condition i << N is equivalent to n >> 1, so this is a
                               4104                 :                :                  * good approximation when the number of distinct values in
                               4105                 :                :                  * the table is large.  It turns out that this formula also
                               4106                 :                :                  * works well even when n is small.
                               4107                 :                :                  */
                               4108                 :          75282 :                 reldistinct *=
                               4109                 :          75282 :                     (1 - pow((rel->tuples - rel->rows) / rel->tuples,
                               4110                 :          75282 :                              rel->tuples / reldistinct));
                               4111                 :                :             }
                               4112                 :         243800 :             reldistinct = clamp_row_est(reldistinct);
                               4113                 :                : 
                               4114                 :                :             /*
                               4115                 :                :              * Update estimate of total distinct groups.
                               4116                 :                :              */
 8237 tgl@sss.pgh.pa.us        4117                 :         243800 :             numdistinct *= reldistinct;
                               4118                 :                :         }
                               4119                 :                : 
 8568                          4120                 :         244665 :         varinfos = newvarinfos;
                               4121         [ +  + ]:         244665 :     } while (varinfos != NIL);
                               4122                 :                : 
                               4123                 :                :     /* Now we can account for the effects of any SRFs */
 3083                          4124                 :         242500 :     numdistinct *= srf_multiplier;
                               4125                 :                : 
                               4126                 :                :     /* Round off */
 8499                          4127                 :         242500 :     numdistinct = ceil(numdistinct);
                               4128                 :                : 
                               4129                 :                :     /* Guard against out-of-range answers */
 8568                          4130         [ +  + ]:         242500 :     if (numdistinct > input_rows)
                               4131                 :          52691 :         numdistinct = input_rows;
                               4132         [ -  + ]:         242500 :     if (numdistinct < 1.0)
 8568 tgl@sss.pgh.pa.us        4133                 :UBC           0 :         numdistinct = 1.0;
                               4134                 :                : 
 8568 tgl@sss.pgh.pa.us        4135                 :CBC      242500 :     return numdistinct;
                               4136                 :                : }
                               4137                 :                : 
                               4138                 :                : /*
                               4139                 :                :  * Try to estimate the bucket size of the hash join inner side when the join
                               4140                 :                :  * condition contains two or more clauses by employing extended statistics.
                               4141                 :                :  *
                               4142                 :                :  * The main idea of this approach is that the distinct value generated by
                               4143                 :                :  * multivariate estimation on two or more columns would provide less bucket size
                               4144                 :                :  * than estimation on one separate column.
                               4145                 :                :  *
                               4146                 :                :  * IMPORTANT: It is crucial to synchronize the approach of combining different
                               4147                 :                :  * estimations with the caller's method.
                               4148                 :                :  *
                               4149                 :                :  * Return a list of clauses that didn't fetch any extended statistics.
                               4150                 :                :  */
                               4151                 :                : List *
  421 akorotkov@postgresql     4152                 :         348681 : estimate_multivariate_bucketsize(PlannerInfo *root, RelOptInfo *inner,
                               4153                 :                :                                  List *hashclauses,
                               4154                 :                :                                  Selectivity *innerbucketsize)
                               4155                 :                : {
                               4156                 :                :     List       *clauses;
                               4157                 :                :     List       *otherclauses;
                               4158                 :                :     double      ndistinct;
                               4159                 :                : 
                               4160         [ +  + ]:         348681 :     if (list_length(hashclauses) <= 1)
                               4161                 :                :     {
                               4162                 :                :         /*
                               4163                 :                :          * Nothing to do for a single clause.  Could we employ univariate
                               4164                 :                :          * extended stat here?
                               4165                 :                :          */
                               4166                 :         318297 :         return hashclauses;
                               4167                 :                :     }
                               4168                 :                : 
                               4169                 :                :     /* "clauses" is the list of hashclauses we've not dealt with yet */
  290 tgl@sss.pgh.pa.us        4170                 :GNC       30384 :     clauses = list_copy(hashclauses);
                               4171                 :                :     /* "otherclauses" holds clauses we are going to return to caller */
                               4172                 :          30384 :     otherclauses = NIL;
                               4173                 :                :     /* current estimate of ndistinct */
                               4174                 :          30384 :     ndistinct = 1.0;
  421 akorotkov@postgresql     4175         [ +  + ]:CBC       60778 :     while (clauses != NIL)
                               4176                 :                :     {
                               4177                 :                :         ListCell   *lc;
                               4178                 :          30394 :         int         relid = -1;
                               4179                 :          30394 :         List       *varinfos = NIL;
                               4180                 :          30394 :         List       *origin_rinfos = NIL;
                               4181                 :                :         double      mvndistinct;
                               4182                 :                :         List       *origin_varinfos;
                               4183                 :          30394 :         int         group_relid = -1;
                               4184                 :          30394 :         RelOptInfo *group_rel = NULL;
                               4185                 :                :         ListCell   *lc1,
                               4186                 :                :                    *lc2;
                               4187                 :                : 
                               4188                 :                :         /*
                               4189                 :                :          * Find clauses, referencing the same single base relation and try to
                               4190                 :                :          * estimate such a group with extended statistics.  Create varinfo for
                               4191                 :                :          * an approved clause, push it to otherclauses, if it can't be
                               4192                 :                :          * estimated here or ignore to process at the next iteration.
                               4193                 :                :          */
                               4194   [ +  +  +  +  :          93209 :         foreach(lc, clauses)
                                              +  + ]
                               4195                 :                :         {
                               4196                 :          62815 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
                               4197                 :                :             Node       *expr;
                               4198                 :                :             Relids      relids;
                               4199                 :                :             GroupVarInfo *varinfo;
                               4200                 :                : 
                               4201                 :                :             /*
                               4202                 :                :              * Find the inner side of the join, which we need to estimate the
                               4203                 :                :              * number of buckets.  Use outer_is_left because the
                               4204                 :                :              * clause_sides_match_join routine has called on hash clauses.
                               4205                 :                :              */
                               4206                 :         125630 :             relids = rinfo->outer_is_left ?
                               4207         [ +  + ]:          62815 :                 rinfo->right_relids : rinfo->left_relids;
                               4208                 :         125630 :             expr = rinfo->outer_is_left ?
                               4209         [ +  + ]:          62815 :                 get_rightop(rinfo->clause) : get_leftop(rinfo->clause);
                               4210                 :                : 
                               4211         [ +  + ]:          62815 :             if (bms_get_singleton_member(relids, &relid) &&
                               4212         [ +  + ]:          61195 :                 root->simple_rel_array[relid]->statlist != NIL)
                               4213                 :             40 :             {
  377                          4214                 :             50 :                 bool        is_duplicate = false;
                               4215                 :                : 
                               4216                 :                :                 /*
                               4217                 :                :                  * This inner-side expression references only one relation.
                               4218                 :                :                  * Extended statistics on this clause can exist.
                               4219                 :                :                  */
  421                          4220         [ +  + ]:             50 :                 if (group_relid < 0)
                               4221                 :                :                 {
                               4222                 :             25 :                     RangeTblEntry *rte = root->simple_rte_array[relid];
                               4223                 :                : 
                               4224   [ +  -  -  + ]:             25 :                     if (!rte || (rte->relkind != RELKIND_RELATION &&
  421 akorotkov@postgresql     4225         [ #  # ]:UBC           0 :                                  rte->relkind != RELKIND_MATVIEW &&
                               4226         [ #  # ]:              0 :                                  rte->relkind != RELKIND_FOREIGN_TABLE &&
                               4227         [ #  # ]:              0 :                                  rte->relkind != RELKIND_PARTITIONED_TABLE))
                               4228                 :                :                     {
                               4229                 :                :                         /* Extended statistics can't exist in principle */
                               4230                 :              0 :                         otherclauses = lappend(otherclauses, rinfo);
                               4231                 :              0 :                         clauses = foreach_delete_current(clauses, lc);
                               4232                 :              0 :                         continue;
                               4233                 :                :                     }
                               4234                 :                : 
  421 akorotkov@postgresql     4235                 :CBC          25 :                     group_relid = relid;
                               4236                 :             25 :                     group_rel = root->simple_rel_array[relid];
                               4237                 :                :                 }
                               4238         [ -  + ]:             25 :                 else if (group_relid != relid)
                               4239                 :                :                 {
                               4240                 :                :                     /*
                               4241                 :                :                      * Being in the group forming state we don't need other
                               4242                 :                :                      * clauses.
                               4243                 :                :                      */
  421 akorotkov@postgresql     4244                 :UBC           0 :                     continue;
                               4245                 :                :                 }
                               4246                 :                : 
                               4247                 :                :                 /*
                               4248                 :                :                  * We're going to add the new clause to the varinfos list.  We
                               4249                 :                :                  * might re-use add_unique_group_var(), but we don't do so for
                               4250                 :                :                  * two reasons.
                               4251                 :                :                  *
                               4252                 :                :                  * 1) We must keep the origin_rinfos list ordered exactly the
                               4253                 :                :                  * same way as varinfos.
                               4254                 :                :                  *
                               4255                 :                :                  * 2) add_unique_group_var() is designed for
                               4256                 :                :                  * estimate_num_groups(), where a larger number of groups is
                               4257                 :                :                  * worse.   While estimating the number of hash buckets, we
                               4258                 :                :                  * have the opposite: a lesser number of groups is worse.
                               4259                 :                :                  * Therefore, we don't have to remove "known equal" vars: the
                               4260                 :                :                  * removed var may valuably contribute to the multivariate
                               4261                 :                :                  * statistics to grow the number of groups.
                               4262                 :                :                  */
                               4263                 :                : 
                               4264                 :                :                 /*
                               4265                 :                :                  * Clear nullingrels to correctly match hash keys.  See
                               4266                 :                :                  * add_unique_group_var()'s comment for details.
                               4267                 :                :                  */
  377 akorotkov@postgresql     4268                 :CBC          50 :                 expr = remove_nulling_relids(expr, root->outer_join_rels, NULL);
                               4269                 :                : 
                               4270                 :                :                 /*
                               4271                 :                :                  * Detect and exclude exact duplicates from the list of hash
                               4272                 :                :                  * keys (like add_unique_group_var does).
                               4273                 :                :                  */
                               4274   [ +  +  +  +  :             70 :                 foreach(lc1, varinfos)
                                              +  + ]
                               4275                 :                :                 {
                               4276                 :             30 :                     varinfo = (GroupVarInfo *) lfirst(lc1);
                               4277                 :                : 
                               4278         [ +  + ]:             30 :                     if (!equal(expr, varinfo->var))
                               4279                 :             20 :                         continue;
                               4280                 :                : 
                               4281                 :             10 :                     is_duplicate = true;
                               4282                 :             10 :                     break;
                               4283                 :                :                 }
                               4284                 :                : 
                               4285         [ +  + ]:             50 :                 if (is_duplicate)
                               4286                 :                :                 {
                               4287                 :                :                     /*
                               4288                 :                :                      * Skip exact duplicates. Adding them to the otherclauses
                               4289                 :                :                      * list also doesn't make sense.
                               4290                 :                :                      */
                               4291                 :             10 :                     continue;
                               4292                 :                :                 }
                               4293                 :                : 
                               4294                 :                :                 /*
                               4295                 :                :                  * Initialize GroupVarInfo.  We only use it to call
                               4296                 :                :                  * estimate_multivariate_ndistinct(), which doesn't care about
                               4297                 :                :                  * ndistinct and isdefault fields.  Thus, skip these fields.
                               4298                 :                :                  */
  146 michael@paquier.xyz      4299                 :GNC          40 :                 varinfo = palloc0_object(GroupVarInfo);
  421 akorotkov@postgresql     4300                 :CBC          40 :                 varinfo->var = expr;
                               4301                 :             40 :                 varinfo->rel = root->simple_rel_array[relid];
                               4302                 :             40 :                 varinfos = lappend(varinfos, varinfo);
                               4303                 :                : 
                               4304                 :                :                 /*
                               4305                 :                :                  * Remember the link to RestrictInfo for the case the clause
                               4306                 :                :                  * is failed to be estimated.
                               4307                 :                :                  */
                               4308                 :             40 :                 origin_rinfos = lappend(origin_rinfos, rinfo);
                               4309                 :                :             }
                               4310                 :                :             else
                               4311                 :                :             {
                               4312                 :                :                 /* This clause can't be estimated with extended statistics */
                               4313                 :          62765 :                 otherclauses = lappend(otherclauses, rinfo);
                               4314                 :                :             }
                               4315                 :                : 
                               4316                 :          62805 :             clauses = foreach_delete_current(clauses, lc);
                               4317                 :                :         }
                               4318                 :                : 
                               4319         [ +  + ]:          30394 :         if (list_length(varinfos) < 2)
                               4320                 :                :         {
                               4321                 :                :             /*
                               4322                 :                :              * Multivariate statistics doesn't apply to single columns except
                               4323                 :                :              * for expressions, but it has not been implemented yet.
                               4324                 :                :              */
                               4325                 :          30384 :             otherclauses = list_concat(otherclauses, origin_rinfos);
                               4326                 :          30384 :             list_free_deep(varinfos);
                               4327                 :          30384 :             list_free(origin_rinfos);
                               4328                 :          30384 :             continue;
                               4329                 :                :         }
                               4330                 :                : 
                               4331         [ -  + ]:             10 :         Assert(group_rel != NULL);
                               4332                 :                : 
                               4333                 :                :         /* Employ the extended statistics. */
                               4334                 :             10 :         origin_varinfos = varinfos;
                               4335                 :                :         for (;;)
                               4336                 :             10 :         {
                               4337                 :             20 :             bool        estimated = estimate_multivariate_ndistinct(root,
                               4338                 :                :                                                                     group_rel,
                               4339                 :                :                                                                     &varinfos,
                               4340                 :                :                                                                     &mvndistinct);
                               4341                 :                : 
                               4342         [ +  + ]:             20 :             if (!estimated)
                               4343                 :             10 :                 break;
                               4344                 :                : 
                               4345                 :                :             /*
                               4346                 :                :              * We've got an estimation.  Use ndistinct value in a consistent
                               4347                 :                :              * way - according to the caller's logic (see
                               4348                 :                :              * final_cost_hashjoin).
                               4349                 :                :              */
                               4350         [ +  - ]:             10 :             if (ndistinct < mvndistinct)
                               4351                 :             10 :                 ndistinct = mvndistinct;
                               4352         [ -  + ]:             10 :             Assert(ndistinct >= 1.0);
                               4353                 :                :         }
                               4354                 :                : 
                               4355         [ -  + ]:             10 :         Assert(list_length(origin_varinfos) == list_length(origin_rinfos));
                               4356                 :                : 
                               4357                 :                :         /* Collect unmatched clauses as otherclauses. */
                               4358   [ +  -  +  +  :             35 :         forboth(lc1, origin_varinfos, lc2, origin_rinfos)
                                     +  -  +  +  +  
                                        +  +  -  +  
                                                 + ]
                               4359                 :                :         {
                               4360                 :             25 :             GroupVarInfo *vinfo = lfirst(lc1);
                               4361                 :                : 
                               4362         [ +  - ]:             25 :             if (!list_member_ptr(varinfos, vinfo))
                               4363                 :                :                 /* Already estimated */
                               4364                 :             25 :                 continue;
                               4365                 :                : 
                               4366                 :                :             /* Can't be estimated here - push to the returning list */
  421 akorotkov@postgresql     4367                 :UBC           0 :             otherclauses = lappend(otherclauses, lfirst(lc2));
                               4368                 :                :         }
                               4369                 :                :     }
                               4370                 :                : 
  421 akorotkov@postgresql     4371                 :CBC       30384 :     *innerbucketsize = 1.0 / ndistinct;
                               4372                 :          30384 :     return otherclauses;
                               4373                 :                : }
                               4374                 :                : 
                               4375                 :                : /*
                               4376                 :                :  * Estimate hash bucket statistics when the specified expression is used
                               4377                 :                :  * as a hash key for the given number of buckets.
                               4378                 :                :  *
                               4379                 :                :  * This attempts to determine two values:
                               4380                 :                :  *
                               4381                 :                :  * 1. The frequency of the most common value of the expression (returns
                               4382                 :                :  * zero into *mcv_freq if we can't get that).  This will be frequency
                               4383                 :                :  * relative to the entire underlying table.
                               4384                 :                :  *
                               4385                 :                :  * 2. The "bucketsize fraction", ie, average number of entries in a bucket
                               4386                 :                :  * divided by total number of tuples to be hashed.
                               4387                 :                :  *
                               4388                 :                :  * XXX This is really pretty bogus since we're effectively assuming that the
                               4389                 :                :  * distribution of hash keys will be the same after applying restriction
                               4390                 :                :  * clauses as it was in the underlying relation.  However, we are not nearly
                               4391                 :                :  * smart enough to figure out how the restrict clauses might change the
                               4392                 :                :  * distribution, so this will have to do for now.
                               4393                 :                :  *
                               4394                 :                :  * We are passed the number of buckets the executor will use for the given
                               4395                 :                :  * input relation.  If the data were perfectly distributed, with the same
                               4396                 :                :  * number of tuples going into each available bucket, then the bucketsize
                               4397                 :                :  * fraction would be 1/nbuckets.  But this happy state of affairs will occur
                               4398                 :                :  * only if (a) there are at least nbuckets distinct data values, and (b)
                               4399                 :                :  * we have a not-too-skewed data distribution.  Otherwise the buckets will
                               4400                 :                :  * be nonuniformly occupied.  If the other relation in the join has a key
                               4401                 :                :  * distribution similar to this one's, then the most-loaded buckets are
                               4402                 :                :  * exactly those that will be probed most often.  Therefore, the "average"
                               4403                 :                :  * bucket size for costing purposes should really be taken as something close
                               4404                 :                :  * to the "worst case" bucket size.  We try to estimate this by adjusting the
                               4405                 :                :  * fraction if there are too few distinct data values, and then clamping to
                               4406                 :                :  * at least the bucket size implied by the most common value's frequency.
                               4407                 :                :  *
                               4408                 :                :  * If no statistics are available, use a default estimate of 0.1.  This will
                               4409                 :                :  * discourage use of a hash rather strongly if the inner relation is large,
                               4410                 :                :  * which is what we want.  We do not want to hash unless we know that the
                               4411                 :                :  * inner rel is well-dispersed (or the alternatives seem much worse).
                               4412                 :                :  *
                               4413                 :                :  * The caller should also check that the mcv_freq is not so large that the
                               4414                 :                :  * most common value would by itself require an impractically large bucket.
                               4415                 :                :  * In a hash join, the executor can split buckets if they get too big, but
                               4416                 :                :  * obviously that doesn't help for a bucket that contains many duplicates of
                               4417                 :                :  * the same value.
                               4418                 :                :  */
                               4419                 :                : void
 3185 tgl@sss.pgh.pa.us        4420                 :         155604 : estimate_hash_bucket_stats(PlannerInfo *root, Node *hashkey, double nbuckets,
                               4421                 :                :                            Selectivity *mcv_freq,
                               4422                 :                :                            Selectivity *bucketsize_frac)
                               4423                 :                : {
                               4424                 :                :     VariableStatData vardata;
                               4425                 :                :     double      estfract,
                               4426                 :                :                 ndistinct;
                               4427                 :                :     bool        isdefault;
                               4428                 :                :     AttStatsSlot sslot;
                               4429                 :                : 
 8113                          4430                 :         155604 :     examine_variable(root, hashkey, 0, &vardata);
                               4431                 :                : 
                               4432                 :                :     /* Initialize *mcv_freq to "unknown" */
 3185                          4433                 :         155604 :     *mcv_freq = 0.0;
                               4434                 :                : 
                               4435                 :                :     /* Look up the frequency of the most common value, if available */
                               4436         [ +  + ]:         155604 :     if (HeapTupleIsValid(vardata.statsTuple))
                               4437                 :                :     {
                               4438         [ +  + ]:         105850 :         if (get_attstatsslot(&sslot, vardata.statsTuple,
                               4439                 :                :                              STATISTIC_KIND_MCV, InvalidOid,
                               4440                 :                :                              ATTSTATSSLOT_NUMBERS))
                               4441                 :                :         {
                               4442                 :                :             /*
                               4443                 :                :              * The first MCV stat is for the most common value.
                               4444                 :                :              */
                               4445         [ +  - ]:          63469 :             if (sslot.nnumbers > 0)
                               4446                 :          63469 :                 *mcv_freq = sslot.numbers[0];
                               4447                 :          63469 :             free_attstatsslot(&sslot);
                               4448                 :                :         }
  127 tgl@sss.pgh.pa.us        4449         [ +  + ]:GNC       42381 :         else if (get_attstatsslot(&sslot, vardata.statsTuple,
                               4450                 :                :                                   STATISTIC_KIND_HISTOGRAM, InvalidOid,
                               4451                 :                :                                   0))
                               4452                 :                :         {
                               4453                 :                :             /*
                               4454                 :                :              * If there are no recorded MCVs, but we do have a histogram, then
                               4455                 :                :              * assume that ANALYZE determined that the column is unique.
                               4456                 :                :              */
   65                          4457   [ +  -  +  + ]:          40886 :             if (vardata.rel && vardata.rel->tuples > 0)
                               4458                 :          40871 :                 *mcv_freq = 1.0 / vardata.rel->tuples;
                               4459                 :                :         }
                               4460                 :                :     }
                               4461                 :                : 
                               4462                 :                :     /* Get number of distinct values */
 5357 tgl@sss.pgh.pa.us        4463                 :CBC      155604 :     ndistinct = get_variable_numdistinct(&vardata, &isdefault);
                               4464                 :                : 
                               4465                 :                :     /*
                               4466                 :                :      * If ndistinct isn't real, punt.  We normally return 0.1, but if the
                               4467                 :                :      * mcv_freq is known to be even higher than that, use it instead.
                               4468                 :                :      */
                               4469         [ +  + ]:         155604 :     if (isdefault)
                               4470                 :                :     {
 3185                          4471         [ +  - ]:          24409 :         *bucketsize_frac = (Selectivity) Max(0.1, *mcv_freq);
 5357                          4472         [ +  + ]:          24409 :         ReleaseVariableStats(vardata);
 3185                          4473                 :          24409 :         return;
                               4474                 :                :     }
                               4475                 :                : 
                               4476                 :                :     /*
                               4477                 :                :      * Adjust ndistinct to account for restriction clauses.  Observe we are
                               4478                 :                :      * assuming that the data distribution is affected uniformly by the
                               4479                 :                :      * restriction clauses!
                               4480                 :                :      *
                               4481                 :                :      * XXX Possibly better way, but much more expensive: multiply by
                               4482                 :                :      * selectivity of rel's restriction clauses that mention the target Var.
                               4483                 :                :      */
 3691                          4484   [ +  -  +  + ]:         131195 :     if (vardata.rel && vardata.rel->tuples > 0)
                               4485                 :                :     {
 8113                          4486                 :         131165 :         ndistinct *= vardata.rel->rows / vardata.rel->tuples;
 3691                          4487                 :         131165 :         ndistinct = clamp_row_est(ndistinct);
                               4488                 :                :     }
                               4489                 :                : 
                               4490                 :                :     /*
                               4491                 :                :      * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
                               4492                 :                :      * number of buckets is less than the expected number of distinct values;
                               4493                 :                :      * otherwise it is 1/ndistinct.
                               4494                 :                :      */
 7730                          4495         [ +  + ]:         131195 :     if (ndistinct > nbuckets)
                               4496                 :             75 :         estfract = 1.0 / nbuckets;
                               4497                 :                :     else
 8113                          4498                 :         131120 :         estfract = 1.0 / ndistinct;
                               4499                 :                : 
                               4500                 :                :     /*
                               4501                 :                :      * Clamp the bucketsize fraction to be not less than the MCV frequency,
                               4502                 :                :      * since whichever bucket the MCV values end up in will have at least that
                               4503                 :                :      * size.  This has no effect if *mcv_freq is still zero.
                               4504                 :                :      */
   62 tgl@sss.pgh.pa.us        4505         [ +  + ]:GNC      131195 :     estfract = Max(estfract, *mcv_freq);
                               4506                 :                : 
 3185 tgl@sss.pgh.pa.us        4507                 :CBC      131195 :     *bucketsize_frac = (Selectivity) estfract;
                               4508                 :                : 
                               4509         [ +  + ]:         131195 :     ReleaseVariableStats(vardata);
                               4510                 :                : }
                               4511                 :                : 
                               4512                 :                : /*
                               4513                 :                :  * estimate_hashagg_tablesize
                               4514                 :                :  *    estimate the number of bytes that a hash aggregate hashtable will
                               4515                 :                :  *    require based on the agg_costs, path width and number of groups.
                               4516                 :                :  *
                               4517                 :                :  * We return the result as "double" to forestall any possible overflow
                               4518                 :                :  * problem in the multiplication by dNumGroups.
                               4519                 :                :  *
                               4520                 :                :  * XXX this may be over-estimating the size now that hashagg knows to omit
                               4521                 :                :  * unneeded columns from the hashtable.  Also for mixed-mode grouping sets,
                               4522                 :                :  * grouping columns not in the hashed set are counted here even though hashagg
                               4523                 :                :  * won't store them.  Is this a problem?
                               4524                 :                :  */
                               4525                 :                : double
 1988 heikki.linnakangas@i     4526                 :           2424 : estimate_hashagg_tablesize(PlannerInfo *root, Path *path,
                               4527                 :                :                            const AggClauseCosts *agg_costs, double dNumGroups)
                               4528                 :                : {
                               4529                 :                :     Size        hashentrysize;
                               4530                 :                : 
                               4531                 :           2424 :     hashentrysize = hash_agg_entry_size(list_length(root->aggtransinfos),
                               4532                 :           2424 :                                         path->pathtarget->width,
                               4533                 :           2424 :                                         agg_costs->transitionSpace);
                               4534                 :                : 
                               4535                 :                :     /*
                               4536                 :                :      * Note that this disregards the effect of fill-factor and growth policy
                               4537                 :                :      * of the hash table.  That's probably ok, given that the default
                               4538                 :                :      * fill-factor is relatively high.  It'd be hard to meaningfully factor in
                               4539                 :                :      * "double-in-size" growth policies here.
                               4540                 :                :      */
 2630 tgl@sss.pgh.pa.us        4541                 :           2424 :     return hashentrysize * dNumGroups;
                               4542                 :                : }
                               4543                 :                : 
                               4544                 :                : 
                               4545                 :                : /*-------------------------------------------------------------------------
                               4546                 :                :  *
                               4547                 :                :  * Support routines
                               4548                 :                :  *
                               4549                 :                :  *-------------------------------------------------------------------------
                               4550                 :                :  */
                               4551                 :                : 
                               4552                 :                : /*
                               4553                 :                :  * Find the best matching ndistinct extended statistics for the given list of
                               4554                 :                :  * GroupVarInfos.
                               4555                 :                :  *
                               4556                 :                :  * Callers must ensure that the given GroupVarInfos all belong to 'rel' and
                               4557                 :                :  * the GroupVarInfos list does not contain any duplicate Vars or expressions.
                               4558                 :                :  *
                               4559                 :                :  * When statistics are found that match > 1 of the given GroupVarInfo, the
                               4560                 :                :  * *ndistinct parameter is set according to the ndistinct estimate and a new
                               4561                 :                :  * list is built with the matching GroupVarInfos removed, which is output via
                               4562                 :                :  * the *varinfos parameter before returning true.  When no matching stats are
                               4563                 :                :  * found, false is returned and the *varinfos and *ndistinct parameters are
                               4564                 :                :  * left untouched.
                               4565                 :                :  */
                               4566                 :                : static bool
 3329 alvherre@alvh.no-ip.     4567                 :         244790 : estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
                               4568                 :                :                                 List **varinfos, double *ndistinct)
                               4569                 :                : {
                               4570                 :                :     ListCell   *lc;
                               4571                 :                :     int         nmatches_vars;
                               4572                 :                :     int         nmatches_exprs;
                               4573                 :         244790 :     Oid         statOid = InvalidOid;
                               4574                 :                :     MVNDistinct *stats;
 1866 tomas.vondra@postgre     4575                 :         244790 :     StatisticExtInfo *matched_info = NULL;
 1281 tgl@sss.pgh.pa.us        4576         [ +  - ]:         244790 :     RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
                               4577                 :                : 
                               4578                 :                :     /* bail out immediately if the table has no extended statistics */
 3329 alvherre@alvh.no-ip.     4579         [ +  + ]:         244790 :     if (!rel->statlist)
                               4580                 :         244319 :         return false;
                               4581                 :                : 
                               4582                 :                :     /* look for the ndistinct statistics object matching the most vars */
 1866 tomas.vondra@postgre     4583                 :            471 :     nmatches_vars = 0;          /* we require at least two matches */
                               4584                 :            471 :     nmatches_exprs = 0;
 3329 alvherre@alvh.no-ip.     4585   [ +  -  +  +  :           1875 :     foreach(lc, rel->statlist)
                                              +  + ]
                               4586                 :                :     {
                               4587                 :                :         ListCell   *lc2;
                               4588                 :           1404 :         StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
 1866 tomas.vondra@postgre     4589                 :           1404 :         int         nshared_vars = 0;
                               4590                 :           1404 :         int         nshared_exprs = 0;
                               4591                 :                : 
                               4592                 :                :         /* skip statistics of other kinds */
 3329 alvherre@alvh.no-ip.     4593         [ +  + ]:           1404 :         if (info->kind != STATS_EXT_NDISTINCT)
                               4594                 :            663 :             continue;
                               4595                 :                : 
                               4596                 :                :         /* skip statistics with mismatching stxdinherit value */
 1281 tgl@sss.pgh.pa.us        4597         [ +  + ]:            741 :         if (info->inherit != rte->inh)
                               4598                 :             25 :             continue;
                               4599                 :                : 
                               4600                 :                :         /*
                               4601                 :                :          * Determine how many expressions (and variables in non-matched
                               4602                 :                :          * expressions) match. We'll then use these numbers to pick the
                               4603                 :                :          * statistics object that best matches the clauses.
                               4604                 :                :          */
 1866 tomas.vondra@postgre     4605   [ +  +  +  +  :           2267 :         foreach(lc2, *varinfos)
                                              +  + ]
                               4606                 :                :         {
                               4607                 :                :             ListCell   *lc3;
                               4608                 :           1551 :             GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc2);
                               4609                 :                :             AttrNumber  attnum;
                               4610                 :                : 
                               4611         [ -  + ]:           1551 :             Assert(varinfo->rel == rel);
                               4612                 :                : 
                               4613                 :                :             /* simple Var, search in statistics keys directly */
                               4614         [ +  + ]:           1551 :             if (IsA(varinfo->var, Var))
                               4615                 :                :             {
                               4616                 :           1246 :                 attnum = ((Var *) varinfo->var)->varattno;
                               4617                 :                : 
                               4618                 :                :                 /*
                               4619                 :                :                  * Ignore system attributes - we don't support statistics on
                               4620                 :                :                  * them, so can't match them (and it'd fail as the values are
                               4621                 :                :                  * negative).
                               4622                 :                :                  */
                               4623         [ +  + ]:           1246 :                 if (!AttrNumberIsForUserDefinedAttr(attnum))
                               4624                 :             10 :                     continue;
                               4625                 :                : 
                               4626         [ +  + ]:           1236 :                 if (bms_is_member(attnum, info->keys))
                               4627                 :            730 :                     nshared_vars++;
                               4628                 :                : 
                               4629                 :           1236 :                 continue;
                               4630                 :                :             }
                               4631                 :                : 
                               4632                 :                :             /* expression - see if it's in the statistics object */
                               4633   [ +  +  +  +  :            550 :             foreach(lc3, info->exprs)
                                              +  + ]
                               4634                 :                :             {
                               4635                 :            440 :                 Node       *expr = (Node *) lfirst(lc3);
                               4636                 :                : 
                               4637         [ +  + ]:            440 :                 if (equal(varinfo->var, expr))
                               4638                 :                :                 {
                               4639                 :            195 :                     nshared_exprs++;
                               4640                 :            195 :                     break;
                               4641                 :                :                 }
                               4642                 :                :             }
                               4643                 :                :         }
                               4644                 :                : 
                               4645                 :                :         /*
                               4646                 :                :          * The ndistinct extended statistics contain estimates for a minimum
                               4647                 :                :          * of pairs of columns which the statistics are defined on and
                               4648                 :                :          * certainly not single columns.  Here we skip unless we managed to
                               4649                 :                :          * match to at least two columns.
                               4650                 :                :          */
                               4651         [ +  + ]:            716 :         if (nshared_vars + nshared_exprs < 2)
                               4652                 :            331 :             continue;
                               4653                 :                : 
                               4654                 :                :         /*
                               4655                 :                :          * Check if these statistics are a better match than the previous best
                               4656                 :                :          * match and if so, take note of the StatisticExtInfo.
                               4657                 :                :          *
                               4658                 :                :          * The statslist is sorted by statOid, so the StatisticExtInfo we
                               4659                 :                :          * select as the best match is deterministic even when multiple sets
                               4660                 :                :          * of statistics match equally as well.
                               4661                 :                :          */
                               4662   [ +  +  +  - ]:            385 :         if ((nshared_exprs > nmatches_exprs) ||
                               4663         [ +  + ]:            295 :             (((nshared_exprs == nmatches_exprs)) && (nshared_vars > nmatches_vars)))
                               4664                 :                :         {
 3329 alvherre@alvh.no-ip.     4665                 :            365 :             statOid = info->statOid;
 1866 tomas.vondra@postgre     4666                 :            365 :             nmatches_vars = nshared_vars;
                               4667                 :            365 :             nmatches_exprs = nshared_exprs;
                               4668                 :            365 :             matched_info = info;
                               4669                 :                :         }
                               4670                 :                :     }
                               4671                 :                : 
                               4672                 :                :     /* No match? */
 3329 alvherre@alvh.no-ip.     4673         [ +  + ]:            471 :     if (statOid == InvalidOid)
                               4674                 :            116 :         return false;
                               4675                 :                : 
 1866 tomas.vondra@postgre     4676         [ -  + ]:            355 :     Assert(nmatches_vars + nmatches_exprs > 1);
                               4677                 :                : 
 1570                          4678                 :            355 :     stats = statext_ndistinct_load(statOid, rte->inh);
                               4679                 :                : 
                               4680                 :                :     /*
                               4681                 :                :      * If we have a match, search it for the specific item that matches (there
                               4682                 :                :      * must be one), and construct the output values.
                               4683                 :                :      */
 3329 alvherre@alvh.no-ip.     4684         [ +  - ]:            355 :     if (stats)
                               4685                 :                :     {
                               4686                 :                :         int         i;
 3275 bruce@momjian.us         4687                 :            355 :         List       *newlist = NIL;
 3329 alvherre@alvh.no-ip.     4688                 :            355 :         MVNDistinctItem *item = NULL;
                               4689                 :                :         ListCell   *lc2;
 1866 tomas.vondra@postgre     4690                 :            355 :         Bitmapset  *matched = NULL;
                               4691                 :                :         AttrNumber  attnum_offset;
                               4692                 :                : 
                               4693                 :                :         /*
                               4694                 :                :          * How much we need to offset the attnums? If there are no
                               4695                 :                :          * expressions, no offset is needed. Otherwise offset enough to move
                               4696                 :                :          * the lowest one (which is equal to number of expressions) to 1.
                               4697                 :                :          */
                               4698         [ +  + ]:            355 :         if (matched_info->exprs)
                               4699                 :            125 :             attnum_offset = (list_length(matched_info->exprs) + 1);
                               4700                 :                :         else
                               4701                 :            230 :             attnum_offset = 0;
                               4702                 :                : 
                               4703                 :                :         /* see what actually matched */
                               4704   [ +  -  +  +  :           1240 :         foreach(lc2, *varinfos)
                                              +  + ]
                               4705                 :                :         {
                               4706                 :                :             ListCell   *lc3;
                               4707                 :                :             int         idx;
                               4708                 :            885 :             bool        found = false;
                               4709                 :                : 
                               4710                 :            885 :             GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc2);
                               4711                 :                : 
                               4712                 :                :             /*
                               4713                 :                :              * Process a simple Var expression, by matching it to keys
                               4714                 :                :              * directly. If there's a matching expression, we'll try matching
                               4715                 :                :              * it later.
                               4716                 :                :              */
                               4717         [ +  + ]:            885 :             if (IsA(varinfo->var, Var))
                               4718                 :                :             {
                               4719                 :            730 :                 AttrNumber  attnum = ((Var *) varinfo->var)->varattno;
                               4720                 :                : 
                               4721                 :                :                 /*
                               4722                 :                :                  * Ignore expressions on system attributes. Can't rely on the
                               4723                 :                :                  * bms check for negative values.
                               4724                 :                :                  */
                               4725         [ +  + ]:            730 :                 if (!AttrNumberIsForUserDefinedAttr(attnum))
                               4726                 :              5 :                     continue;
                               4727                 :                : 
                               4728                 :                :                 /* Is the variable covered by the statistics object? */
                               4729         [ +  + ]:            725 :                 if (!bms_is_member(attnum, matched_info->keys))
                               4730                 :            100 :                     continue;
                               4731                 :                : 
                               4732                 :            625 :                 attnum = attnum + attnum_offset;
                               4733                 :                : 
                               4734                 :                :                 /* ensure sufficient offset */
                               4735         [ -  + ]:            625 :                 Assert(AttrNumberIsForUserDefinedAttr(attnum));
                               4736                 :                : 
                               4737                 :            625 :                 matched = bms_add_member(matched, attnum);
                               4738                 :                : 
                               4739                 :            625 :                 found = true;
                               4740                 :                :             }
                               4741                 :                : 
                               4742                 :                :             /*
                               4743                 :                :              * XXX Maybe we should allow searching the expressions even if we
                               4744                 :                :              * found an attribute matching the expression? That would handle
                               4745                 :                :              * trivial expressions like "(a)" but it seems fairly useless.
                               4746                 :                :              */
                               4747         [ +  + ]:            780 :             if (found)
                               4748                 :            625 :                 continue;
                               4749                 :                : 
                               4750                 :                :             /* expression - see if it's in the statistics object */
                               4751                 :            155 :             idx = 0;
                               4752   [ +  +  +  +  :            255 :             foreach(lc3, matched_info->exprs)
                                              +  + ]
                               4753                 :                :             {
                               4754                 :            230 :                 Node       *expr = (Node *) lfirst(lc3);
                               4755                 :                : 
                               4756         [ +  + ]:            230 :                 if (equal(varinfo->var, expr))
                               4757                 :                :                 {
                               4758                 :            130 :                     AttrNumber  attnum = -(idx + 1);
                               4759                 :                : 
                               4760                 :            130 :                     attnum = attnum + attnum_offset;
                               4761                 :                : 
                               4762                 :                :                     /* ensure sufficient offset */
                               4763         [ -  + ]:            130 :                     Assert(AttrNumberIsForUserDefinedAttr(attnum));
                               4764                 :                : 
                               4765                 :            130 :                     matched = bms_add_member(matched, attnum);
                               4766                 :                : 
                               4767                 :                :                     /* there should be just one matching expression */
                               4768                 :            130 :                     break;
                               4769                 :                :                 }
                               4770                 :                : 
                               4771                 :            100 :                 idx++;
                               4772                 :                :             }
                               4773                 :                :         }
                               4774                 :                : 
                               4775                 :                :         /* Find the specific item that exactly matches the combination */
 3329 alvherre@alvh.no-ip.     4776         [ +  - ]:            720 :         for (i = 0; i < stats->nitems; i++)
                               4777                 :                :         {
                               4778                 :                :             int         j;
                               4779                 :            720 :             MVNDistinctItem *tmpitem = &stats->items[i];
                               4780                 :                : 
 1866 tomas.vondra@postgre     4781         [ +  + ]:            720 :             if (tmpitem->nattributes != bms_num_members(matched))
                               4782                 :            135 :                 continue;
                               4783                 :                : 
                               4784                 :                :             /* assume it's the right item */
                               4785                 :            585 :             item = tmpitem;
                               4786                 :                : 
                               4787                 :                :             /* check that all item attributes/expressions fit the match */
                               4788         [ +  + ]:           1410 :             for (j = 0; j < tmpitem->nattributes; j++)
                               4789                 :                :             {
                               4790                 :           1055 :                 AttrNumber  attnum = tmpitem->attributes[j];
                               4791                 :                : 
                               4792                 :                :                 /*
                               4793                 :                :                  * Thanks to how we constructed the matched bitmap above, we
                               4794                 :                :                  * can just offset all attnums the same way.
                               4795                 :                :                  */
                               4796                 :           1055 :                 attnum = attnum + attnum_offset;
                               4797                 :                : 
                               4798         [ +  + ]:           1055 :                 if (!bms_is_member(attnum, matched))
                               4799                 :                :                 {
                               4800                 :                :                     /* nah, it's not this item */
                               4801                 :            230 :                     item = NULL;
                               4802                 :            230 :                     break;
                               4803                 :                :                 }
                               4804                 :                :             }
                               4805                 :                : 
                               4806                 :                :             /*
                               4807                 :                :              * If the item has all the matched attributes, we know it's the
                               4808                 :                :              * right one - there can't be a better one. matching more.
                               4809                 :                :              */
                               4810         [ +  + ]:            585 :             if (item)
                               4811                 :            355 :                 break;
                               4812                 :                :         }
                               4813                 :                : 
                               4814                 :                :         /*
                               4815                 :                :          * Make sure we found an item. There has to be one, because ndistinct
                               4816                 :                :          * statistics includes all combinations of attributes.
                               4817                 :                :          */
 3329 alvherre@alvh.no-ip.     4818         [ -  + ]:            355 :         if (!item)
 3329 alvherre@alvh.no-ip.     4819         [ #  # ]:UBC           0 :             elog(ERROR, "corrupt MVNDistinct entry");
                               4820                 :                : 
                               4821                 :                :         /* Form the output varinfo list, keeping only unmatched ones */
 3329 alvherre@alvh.no-ip.     4822   [ +  -  +  +  :CBC        1240 :         foreach(lc, *varinfos)
                                              +  + ]
                               4823                 :                :         {
                               4824                 :            885 :             GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
                               4825                 :                :             ListCell   *lc3;
 1866 tomas.vondra@postgre     4826                 :            885 :             bool        found = false;
                               4827                 :                : 
                               4828                 :                :             /*
                               4829                 :                :              * Let's look at plain variables first, because it's the most
                               4830                 :                :              * common case and the check is quite cheap. We can simply get the
                               4831                 :                :              * attnum and check (with an offset) matched bitmap.
                               4832                 :                :              */
                               4833         [ +  + ]:            885 :             if (IsA(varinfo->var, Var))
 3329 alvherre@alvh.no-ip.     4834                 :            725 :             {
 1866 tomas.vondra@postgre     4835                 :            730 :                 AttrNumber  attnum = ((Var *) varinfo->var)->varattno;
                               4836                 :                : 
                               4837                 :                :                 /*
                               4838                 :                :                  * If it's a system attribute, we're done. We don't support
                               4839                 :                :                  * extended statistics on system attributes, so it's clearly
                               4840                 :                :                  * not matched. Just keep the expression and continue.
                               4841                 :                :                  */
                               4842         [ +  + ]:            730 :                 if (!AttrNumberIsForUserDefinedAttr(attnum))
                               4843                 :                :                 {
                               4844                 :              5 :                     newlist = lappend(newlist, varinfo);
                               4845                 :              5 :                     continue;
                               4846                 :                :                 }
                               4847                 :                : 
                               4848                 :                :                 /* apply the same offset as above */
                               4849                 :            725 :                 attnum += attnum_offset;
                               4850                 :                : 
                               4851                 :                :                 /* if it's not matched, keep the varinfo */
                               4852         [ +  + ]:            725 :                 if (!bms_is_member(attnum, matched))
                               4853                 :            100 :                     newlist = lappend(newlist, varinfo);
                               4854                 :                : 
                               4855                 :                :                 /* The rest of the loop deals with complex expressions. */
 3329 alvherre@alvh.no-ip.     4856                 :            725 :                 continue;
                               4857                 :                :             }
                               4858                 :                : 
                               4859                 :                :             /*
                               4860                 :                :              * Process complex expressions, not just simple Vars.
                               4861                 :                :              *
                               4862                 :                :              * First, we search for an exact match of an expression. If we
                               4863                 :                :              * find one, we can just discard the whole GroupVarInfo, with all
                               4864                 :                :              * the variables we extracted from it.
                               4865                 :                :              *
                               4866                 :                :              * Otherwise we inspect the individual vars, and try matching it
                               4867                 :                :              * to variables in the item.
                               4868                 :                :              */
 1866 tomas.vondra@postgre     4869   [ +  +  +  +  :            255 :             foreach(lc3, matched_info->exprs)
                                              +  + ]
                               4870                 :                :             {
                               4871                 :            230 :                 Node       *expr = (Node *) lfirst(lc3);
                               4872                 :                : 
                               4873         [ +  + ]:            230 :                 if (equal(varinfo->var, expr))
                               4874                 :                :                 {
                               4875                 :            130 :                     found = true;
                               4876                 :            130 :                     break;
                               4877                 :                :                 }
                               4878                 :                :             }
                               4879                 :                : 
                               4880                 :                :             /* found exact match, skip */
                               4881         [ +  + ]:            155 :             if (found)
 2362                          4882                 :            130 :                 continue;
                               4883                 :                : 
 1866                          4884                 :             25 :             newlist = lappend(newlist, varinfo);
                               4885                 :                :         }
                               4886                 :                : 
 3329 alvherre@alvh.no-ip.     4887                 :            355 :         *varinfos = newlist;
                               4888                 :            355 :         *ndistinct = item->ndistinct;
                               4889                 :            355 :         return true;
                               4890                 :                :     }
                               4891                 :                : 
 3329 alvherre@alvh.no-ip.     4892                 :UBC           0 :     return false;
                               4893                 :                : }
                               4894                 :                : 
                               4895                 :                : /*
                               4896                 :                :  * convert_to_scalar
                               4897                 :                :  *    Convert non-NULL values of the indicated types to the comparison
                               4898                 :                :  *    scale needed by scalarineqsel().
                               4899                 :                :  *    Returns "true" if successful.
                               4900                 :                :  *
                               4901                 :                :  * XXX this routine is a hack: ideally we should look up the conversion
                               4902                 :                :  * subroutines in pg_type.
                               4903                 :                :  *
                               4904                 :                :  * All numeric datatypes are simply converted to their equivalent
                               4905                 :                :  * "double" values.  (NUMERIC values that are outside the range of "double"
                               4906                 :                :  * are clamped to +/- HUGE_VAL.)
                               4907                 :                :  *
                               4908                 :                :  * String datatypes are converted by convert_string_to_scalar(),
                               4909                 :                :  * which is explained below.  The reason why this routine deals with
                               4910                 :                :  * three values at a time, not just one, is that we need it for strings.
                               4911                 :                :  *
                               4912                 :                :  * The bytea datatype is just enough different from strings that it has
                               4913                 :                :  * to be treated separately.
                               4914                 :                :  *
                               4915                 :                :  * The several datatypes representing absolute times are all converted
                               4916                 :                :  * to Timestamp, which is actually an int64, and then we promote that to
                               4917                 :                :  * a double.  Note this will give correct results even for the "special"
                               4918                 :                :  * values of Timestamp, since those are chosen to compare correctly;
                               4919                 :                :  * see timestamp_cmp.
                               4920                 :                :  *
                               4921                 :                :  * The several datatypes representing relative times (intervals) are all
                               4922                 :                :  * converted to measurements expressed in seconds.
                               4923                 :                :  */
                               4924                 :                : static bool
 2699 tgl@sss.pgh.pa.us        4925                 :CBC       60850 : convert_to_scalar(Datum value, Oid valuetypid, Oid collid, double *scaledvalue,
                               4926                 :                :                   Datum lobound, Datum hibound, Oid boundstypid,
                               4927                 :                :                   double *scaledlobound, double *scaledhibound)
                               4928                 :                : {
 2985                          4929                 :          60850 :     bool        failure = false;
                               4930                 :                : 
                               4931                 :                :     /*
                               4932                 :                :      * Both the valuetypid and the boundstypid should exactly match the
                               4933                 :                :      * declared input type(s) of the operator we are invoked for.  However,
                               4934                 :                :      * extensions might try to use scalarineqsel as estimator for operators
                               4935                 :                :      * with input type(s) we don't handle here; in such cases, we want to
                               4936                 :                :      * return false, not fail.  In any case, we mustn't assume that valuetypid
                               4937                 :                :      * and boundstypid are identical.
                               4938                 :                :      *
                               4939                 :                :      * XXX The histogram we are interpolating between points of could belong
                               4940                 :                :      * to a column that's only binary-compatible with the declared type. In
                               4941                 :                :      * essence we are assuming that the semantics of binary-compatible types
                               4942                 :                :      * are enough alike that we can use a histogram generated with one type's
                               4943                 :                :      * operators to estimate selectivity for the other's.  This is outright
                               4944                 :                :      * wrong in some cases --- in particular signed versus unsigned
                               4945                 :                :      * interpretation could trip us up.  But it's useful enough in the
                               4946                 :                :      * majority of cases that we do it anyway.  Should think about more
                               4947                 :                :      * rigorous ways to do it.
                               4948                 :                :      */
 9515                          4949   [ +  +  -  -  :          60850 :     switch (valuetypid)
                                              -  - ]
                               4950                 :                :     {
                               4951                 :                :             /*
                               4952                 :                :              * Built-in numeric types
                               4953                 :                :              */
 9116                          4954                 :          55965 :         case BOOLOID:
                               4955                 :                :         case INT2OID:
                               4956                 :                :         case INT4OID:
                               4957                 :                :         case INT8OID:
                               4958                 :                :         case FLOAT4OID:
                               4959                 :                :         case FLOAT8OID:
                               4960                 :                :         case NUMERICOID:
                               4961                 :                :         case OIDOID:
                               4962                 :                :         case REGPROCOID:
                               4963                 :                :         case REGPROCEDUREOID:
                               4964                 :                :         case REGOPEROID:
                               4965                 :                :         case REGOPERATOROID:
                               4966                 :                :         case REGCLASSOID:
                               4967                 :                :         case REGTYPEOID:
                               4968                 :                :         case REGCOLLATIONOID:
                               4969                 :                :         case REGCONFIGOID:
                               4970                 :                :         case REGDICTIONARYOID:
                               4971                 :                :         case REGROLEOID:
                               4972                 :                :         case REGNAMESPACEOID:
                               4973                 :                :         case REGDATABASEOID:
 2985                          4974                 :          55965 :             *scaledvalue = convert_numeric_to_scalar(value, valuetypid,
                               4975                 :                :                                                      &failure);
                               4976                 :          55965 :             *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid,
                               4977                 :                :                                                        &failure);
                               4978                 :          55965 :             *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid,
                               4979                 :                :                                                        &failure);
                               4980                 :          55965 :             return !failure;
                               4981                 :                : 
                               4982                 :                :             /*
                               4983                 :                :              * Built-in string types
                               4984                 :                :              */
 9598                          4985                 :           4885 :         case CHAROID:
                               4986                 :                :         case BPCHAROID:
                               4987                 :                :         case VARCHAROID:
                               4988                 :                :         case TEXTOID:
                               4989                 :                :         case NAMEOID:
                               4990                 :                :             {
 2985                          4991                 :           4885 :                 char       *valstr = convert_string_datum(value, valuetypid,
                               4992                 :                :                                                           collid, &failure);
                               4993                 :           4885 :                 char       *lostr = convert_string_datum(lobound, boundstypid,
                               4994                 :                :                                                          collid, &failure);
                               4995                 :           4885 :                 char       *histr = convert_string_datum(hibound, boundstypid,
                               4996                 :                :                                                          collid, &failure);
                               4997                 :                : 
                               4998                 :                :                 /*
                               4999                 :                :                  * Bail out if any of the values is not of string type.  We
                               5000                 :                :                  * might leak converted strings for the other value(s), but
                               5001                 :                :                  * that's not worth troubling over.
                               5002                 :                :                  */
                               5003         [ -  + ]:           4885 :                 if (failure)
 2985 tgl@sss.pgh.pa.us        5004                 :UBC           0 :                     return false;
                               5005                 :                : 
 9175 bruce@momjian.us         5006                 :CBC        4885 :                 convert_string_to_scalar(valstr, scaledvalue,
                               5007                 :                :                                          lostr, scaledlobound,
                               5008                 :                :                                          histr, scaledhibound);
                               5009                 :           4885 :                 pfree(valstr);
                               5010                 :           4885 :                 pfree(lostr);
                               5011                 :           4885 :                 pfree(histr);
                               5012                 :           4885 :                 return true;
                               5013                 :                :             }
                               5014                 :                : 
                               5015                 :                :             /*
                               5016                 :                :              * Built-in bytea type
                               5017                 :                :              */
 9031 tgl@sss.pgh.pa.us        5018                 :UBC           0 :         case BYTEAOID:
                               5019                 :                :             {
                               5020                 :                :                 /* We only support bytea vs bytea comparison */
 2985                          5021         [ #  # ]:              0 :                 if (boundstypid != BYTEAOID)
                               5022                 :              0 :                     return false;
 9031                          5023                 :              0 :                 convert_bytea_to_scalar(value, scaledvalue,
                               5024                 :                :                                         lobound, scaledlobound,
                               5025                 :                :                                         hibound, scaledhibound);
                               5026                 :              0 :                 return true;
                               5027                 :                :             }
                               5028                 :                : 
                               5029                 :                :             /*
                               5030                 :                :              * Built-in time types
                               5031                 :                :              */
 9565                          5032                 :              0 :         case TIMESTAMPOID:
                               5033                 :                :         case TIMESTAMPTZOID:
                               5034                 :                :         case DATEOID:
                               5035                 :                :         case INTERVALOID:
                               5036                 :                :         case TIMEOID:
                               5037                 :                :         case TIMETZOID:
 2985                          5038                 :              0 :             *scaledvalue = convert_timevalue_to_scalar(value, valuetypid,
                               5039                 :                :                                                        &failure);
                               5040                 :              0 :             *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid,
                               5041                 :                :                                                          &failure);
                               5042                 :              0 :             *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid,
                               5043                 :                :                                                          &failure);
                               5044                 :              0 :             return !failure;
                               5045                 :                : 
                               5046                 :                :             /*
                               5047                 :                :              * Built-in network types
                               5048                 :                :              */
 9096                          5049                 :              0 :         case INETOID:
                               5050                 :                :         case CIDROID:
                               5051                 :                :         case MACADDROID:
                               5052                 :                :         case MACADDR8OID:
 2985                          5053                 :              0 :             *scaledvalue = convert_network_to_scalar(value, valuetypid,
                               5054                 :                :                                                      &failure);
                               5055                 :              0 :             *scaledlobound = convert_network_to_scalar(lobound, boundstypid,
                               5056                 :                :                                                        &failure);
                               5057                 :              0 :             *scaledhibound = convert_network_to_scalar(hibound, boundstypid,
                               5058                 :                :                                                        &failure);
                               5059                 :              0 :             return !failure;
                               5060                 :                :     }
                               5061                 :                :     /* Don't know how to convert */
 7528                          5062                 :              0 :     *scaledvalue = *scaledlobound = *scaledhibound = 0;
 9773                          5063                 :              0 :     return false;
                               5064                 :                : }
                               5065                 :                : 
                               5066                 :                : /*
                               5067                 :                :  * Do convert_to_scalar()'s work for any numeric data type.
                               5068                 :                :  *
                               5069                 :                :  * On failure (e.g., unsupported typid), set *failure to true;
                               5070                 :                :  * otherwise, that variable is not changed.
                               5071                 :                :  */
                               5072                 :                : static double
 2985 tgl@sss.pgh.pa.us        5073                 :CBC      167895 : convert_numeric_to_scalar(Datum value, Oid typid, bool *failure)
                               5074                 :                : {
 9515                          5075   [ -  +  +  -  :         167895 :     switch (typid)
                                        -  +  -  +  
                                                 - ]
                               5076                 :                :     {
 9129 tgl@sss.pgh.pa.us        5077                 :UBC           0 :         case BOOLOID:
 9473                          5078                 :              0 :             return (double) DatumGetBool(value);
 9515 tgl@sss.pgh.pa.us        5079                 :CBC          10 :         case INT2OID:
                               5080                 :             10 :             return (double) DatumGetInt16(value);
                               5081                 :          22121 :         case INT4OID:
                               5082                 :          22121 :             return (double) DatumGetInt32(value);
 9515 tgl@sss.pgh.pa.us        5083                 :UBC           0 :         case INT8OID:
 9473                          5084                 :              0 :             return (double) DatumGetInt64(value);
 9515                          5085                 :              0 :         case FLOAT4OID:
 9473                          5086                 :              0 :             return (double) DatumGetFloat4(value);
 9515 tgl@sss.pgh.pa.us        5087                 :CBC          45 :         case FLOAT8OID:
 9473                          5088                 :             45 :             return (double) DatumGetFloat8(value);
 9515 tgl@sss.pgh.pa.us        5089                 :UBC           0 :         case NUMERICOID:
                               5090                 :                :             /* Note: out-of-range values will be clamped to +-HUGE_VAL */
 8970                          5091                 :              0 :             return (double)
                               5092                 :              0 :                 DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
                               5093                 :                :                                                    value));
 9515 tgl@sss.pgh.pa.us        5094                 :CBC      145719 :         case OIDOID:
                               5095                 :                :         case REGPROCOID:
                               5096                 :                :         case REGPROCEDUREOID:
                               5097                 :                :         case REGOPEROID:
                               5098                 :                :         case REGOPERATOROID:
                               5099                 :                :         case REGCLASSOID:
                               5100                 :                :         case REGTYPEOID:
                               5101                 :                :         case REGCOLLATIONOID:
                               5102                 :                :         case REGCONFIGOID:
                               5103                 :                :         case REGDICTIONARYOID:
                               5104                 :                :         case REGROLEOID:
                               5105                 :                :         case REGNAMESPACEOID:
                               5106                 :                :         case REGDATABASEOID:
                               5107                 :                :             /* we can treat OIDs as integers... */
                               5108                 :         145719 :             return (double) DatumGetObjectId(value);
                               5109                 :                :     }
                               5110                 :                : 
 2985 tgl@sss.pgh.pa.us        5111                 :UBC           0 :     *failure = true;
 9515                          5112                 :              0 :     return 0;
                               5113                 :                : }
                               5114                 :                : 
                               5115                 :                : /*
                               5116                 :                :  * Do convert_to_scalar()'s work for any character-string data type.
                               5117                 :                :  *
                               5118                 :                :  * String datatypes are converted to a scale that ranges from 0 to 1,
                               5119                 :                :  * where we visualize the bytes of the string as fractional digits.
                               5120                 :                :  *
                               5121                 :                :  * We do not want the base to be 256, however, since that tends to
                               5122                 :                :  * generate inflated selectivity estimates; few databases will have
                               5123                 :                :  * occurrences of all 256 possible byte values at each position.
                               5124                 :                :  * Instead, use the smallest and largest byte values seen in the bounds
                               5125                 :                :  * as the estimated range for each byte, after some fudging to deal with
                               5126                 :                :  * the fact that we probably aren't going to see the full range that way.
                               5127                 :                :  *
                               5128                 :                :  * An additional refinement is that we discard any common prefix of the
                               5129                 :                :  * three strings before computing the scaled values.  This allows us to
                               5130                 :                :  * "zoom in" when we encounter a narrow data range.  An example is a phone
                               5131                 :                :  * number database where all the values begin with the same area code.
                               5132                 :                :  * (Actually, the bounds will be adjacent histogram-bin-boundary values,
                               5133                 :                :  * so this is more likely to happen than you might think.)
                               5134                 :                :  */
                               5135                 :                : static void
 7528 tgl@sss.pgh.pa.us        5136                 :CBC        4885 : convert_string_to_scalar(char *value,
                               5137                 :                :                          double *scaledvalue,
                               5138                 :                :                          char *lobound,
                               5139                 :                :                          double *scaledlobound,
                               5140                 :                :                          char *hibound,
                               5141                 :                :                          double *scaledhibound)
                               5142                 :                : {
                               5143                 :                :     int         rangelo,
                               5144                 :                :                 rangehi;
                               5145                 :                :     char       *sptr;
                               5146                 :                : 
                               5147                 :           4885 :     rangelo = rangehi = (unsigned char) hibound[0];
 9515                          5148         [ +  + ]:          71302 :     for (sptr = lobound; *sptr; sptr++)
                               5149                 :                :     {
 7528                          5150         [ +  + ]:          66417 :         if (rangelo > (unsigned char) *sptr)
                               5151                 :          12325 :             rangelo = (unsigned char) *sptr;
                               5152         [ +  + ]:          66417 :         if (rangehi < (unsigned char) *sptr)
                               5153                 :           6425 :             rangehi = (unsigned char) *sptr;
                               5154                 :                :     }
 9515                          5155         [ +  + ]:          52064 :     for (sptr = hibound; *sptr; sptr++)
                               5156                 :                :     {
 7528                          5157         [ +  + ]:          47179 :         if (rangelo > (unsigned char) *sptr)
                               5158                 :            471 :             rangelo = (unsigned char) *sptr;
                               5159         [ +  + ]:          47179 :         if (rangehi < (unsigned char) *sptr)
                               5160                 :           1926 :             rangehi = (unsigned char) *sptr;
                               5161                 :                :     }
                               5162                 :                :     /* If range includes any upper-case ASCII chars, make it include all */
 9515                          5163   [ +  +  +  + ]:           4885 :     if (rangelo <= 'Z' && rangehi >= 'A')
                               5164                 :                :     {
                               5165         [ +  + ]:           1397 :         if (rangelo > 'A')
                               5166                 :            185 :             rangelo = 'A';
                               5167         [ +  + ]:           1397 :         if (rangehi < 'Z')
                               5168                 :            425 :             rangehi = 'Z';
                               5169                 :                :     }
                               5170                 :                :     /* Ditto lower-case */
                               5171   [ +  -  +  + ]:           4885 :     if (rangelo <= 'z' && rangehi >= 'a')
                               5172                 :                :     {
                               5173         [ +  + ]:           4426 :         if (rangelo > 'a')
                               5174                 :              1 :             rangelo = 'a';
                               5175         [ +  + ]:           4426 :         if (rangehi < 'z')
                               5176                 :           4392 :             rangehi = 'z';
                               5177                 :                :     }
                               5178                 :                :     /* Ditto digits */
                               5179   [ +  +  +  - ]:           4885 :     if (rangelo <= '9' && rangehi >= '0')
                               5180                 :                :     {
                               5181         [ +  + ]:            821 :         if (rangelo > '0')
                               5182                 :            748 :             rangelo = '0';
                               5183         [ +  + ]:            821 :         if (rangehi < '9')
                               5184                 :             24 :             rangehi = '9';
                               5185                 :                :     }
                               5186                 :                : 
                               5187                 :                :     /*
                               5188                 :                :      * If range includes less than 10 chars, assume we have not got enough
                               5189                 :                :      * data, and make it include regular ASCII set.
                               5190                 :                :      */
                               5191         [ -  + ]:           4885 :     if (rangehi - rangelo < 9)
                               5192                 :                :     {
 9515 tgl@sss.pgh.pa.us        5193                 :UBC           0 :         rangelo = ' ';
                               5194                 :              0 :         rangehi = 127;
                               5195                 :                :     }
                               5196                 :                : 
                               5197                 :                :     /*
                               5198                 :                :      * Now strip any common prefix of the three strings.
                               5199                 :                :      */
 9515 tgl@sss.pgh.pa.us        5200         [ +  - ]:CBC        9667 :     while (*lobound)
                               5201                 :                :     {
                               5202   [ +  +  +  - ]:           9667 :         if (*lobound != *hibound || *lobound != *value)
                               5203                 :                :             break;
                               5204                 :           4782 :         lobound++, hibound++, value++;
                               5205                 :                :     }
                               5206                 :                : 
                               5207                 :                :     /*
                               5208                 :                :      * Now we can do the conversions.
                               5209                 :                :      */
                               5210                 :           4885 :     *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
                               5211                 :           4885 :     *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
                               5212                 :           4885 :     *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
                               5213                 :           4885 : }
                               5214                 :                : 
                               5215                 :                : static double
 7528                          5216                 :          14655 : convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
                               5217                 :                : {
                               5218                 :          14655 :     int         slen = strlen(value);
                               5219                 :                :     double      num,
                               5220                 :                :                 denom,
                               5221                 :                :                 base;
                               5222                 :                : 
 9515                          5223         [ -  + ]:          14655 :     if (slen <= 0)
 9515 tgl@sss.pgh.pa.us        5224                 :UBC           0 :         return 0.0;             /* empty string has scalar value 0 */
                               5225                 :                : 
                               5226                 :                :     /*
                               5227                 :                :      * There seems little point in considering more than a dozen bytes from
                               5228                 :                :      * the string.  Since base is at least 10, that will give us nominal
                               5229                 :                :      * resolution of at least 12 decimal digits, which is surely far more
                               5230                 :                :      * precision than this estimation technique has got anyway (especially in
                               5231                 :                :      * non-C locales).  Also, even with the maximum possible base of 256, this
                               5232                 :                :      * ensures denom cannot grow larger than 256^13 = 2.03e31, which will not
                               5233                 :                :      * overflow on any known machine.
                               5234                 :                :      */
 3908 tgl@sss.pgh.pa.us        5235         [ +  + ]:CBC       14655 :     if (slen > 12)
                               5236                 :           3827 :         slen = 12;
                               5237                 :                : 
                               5238                 :                :     /* Convert initial characters to fraction */
 9515                          5239                 :          14655 :     base = rangehi - rangelo + 1;
                               5240                 :          14655 :     num = 0.0;
                               5241                 :          14655 :     denom = base;
                               5242         [ +  + ]:         121709 :     while (slen-- > 0)
                               5243                 :                :     {
 7528                          5244                 :         107054 :         int         ch = (unsigned char) *value++;
                               5245                 :                : 
 9515                          5246         [ +  + ]:         107054 :         if (ch < rangelo)
 9175 bruce@momjian.us         5247                 :             68 :             ch = rangelo - 1;
 9515 tgl@sss.pgh.pa.us        5248         [ -  + ]:         106986 :         else if (ch > rangehi)
 9175 bruce@momjian.us         5249                 :UBC           0 :             ch = rangehi + 1;
 9515 tgl@sss.pgh.pa.us        5250                 :CBC      107054 :         num += ((double) (ch - rangelo)) / denom;
                               5251                 :         107054 :         denom *= base;
                               5252                 :                :     }
                               5253                 :                : 
                               5254                 :          14655 :     return num;
                               5255                 :                : }
                               5256                 :                : 
                               5257                 :                : /*
                               5258                 :                :  * Convert a string-type Datum into a palloc'd, null-terminated string.
                               5259                 :                :  *
                               5260                 :                :  * On failure (e.g., unsupported typid), set *failure to true;
                               5261                 :                :  * otherwise, that variable is not changed.  (We'll return NULL on failure.)
                               5262                 :                :  *
                               5263                 :                :  * When using a non-C locale, we must pass the string through pg_strxfrm()
                               5264                 :                :  * before continuing, so as to generate correct locale-specific results.
                               5265                 :                :  */
                               5266                 :                : static char *
 2699                          5267                 :          14655 : convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure)
                               5268                 :                : {
                               5269                 :                :     char       *val;
                               5270                 :                :     pg_locale_t mylocale;
                               5271                 :                : 
 9515                          5272   [ -  +  +  - ]:          14655 :     switch (typid)
                               5273                 :                :     {
 9515 tgl@sss.pgh.pa.us        5274                 :UBC           0 :         case CHAROID:
                               5275                 :              0 :             val = (char *) palloc(2);
                               5276                 :              0 :             val[0] = DatumGetChar(value);
                               5277                 :              0 :             val[1] = '\0';
                               5278                 :              0 :             break;
 9515 tgl@sss.pgh.pa.us        5279                 :CBC        4416 :         case BPCHAROID:
                               5280                 :                :         case VARCHAROID:
                               5281                 :                :         case TEXTOID:
 6615                          5282                 :           4416 :             val = TextDatumGetCString(value);
                               5283                 :           4416 :             break;
 9515                          5284                 :          10239 :         case NAMEOID:
                               5285                 :                :             {
 9175 bruce@momjian.us         5286                 :          10239 :                 NameData   *nm = (NameData *) DatumGetPointer(value);
                               5287                 :                : 
                               5288                 :          10239 :                 val = pstrdup(NameStr(*nm));
                               5289                 :          10239 :                 break;
                               5290                 :                :             }
 9515 tgl@sss.pgh.pa.us        5291                 :UBC           0 :         default:
 2985                          5292                 :              0 :             *failure = true;
 9515                          5293                 :              0 :             return NULL;
                               5294                 :                :     }
                               5295                 :                : 
  608 jdavis@postgresql.or     5296                 :CBC       14655 :     mylocale = pg_newlocale_from_collation(collid);
                               5297                 :                : 
                               5298         [ +  + ]:          14655 :     if (!mylocale->collate_is_c)
                               5299                 :                :     {
                               5300                 :                :         char       *xfrmstr;
                               5301                 :                :         size_t      xfrmlen;
                               5302                 :                :         size_t      xfrmlen2 PG_USED_FOR_ASSERTS_ONLY;
                               5303                 :                : 
                               5304                 :                :         /*
                               5305                 :                :          * XXX: We could guess at a suitable output buffer size and only call
                               5306                 :                :          * pg_strxfrm() twice if our guess is too small.
                               5307                 :                :          *
                               5308                 :                :          * XXX: strxfrm doesn't support UTF-8 encoding on Win32, it can return
                               5309                 :                :          * bogus data or set an error. This is not really a problem unless it
                               5310                 :                :          * crashes since it will only give an estimation error and nothing
                               5311                 :                :          * fatal.
                               5312                 :                :          *
                               5313                 :                :          * XXX: we do not check pg_strxfrm_enabled(). On some platforms and in
                               5314                 :                :          * some cases, libc strxfrm() may return the wrong results, but that
                               5315                 :                :          * will only lead to an estimation error.
                               5316                 :                :          */
  637                          5317                 :             66 :         xfrmlen = pg_strxfrm(NULL, val, 0, mylocale);
                               5318                 :                : #ifdef WIN32
                               5319                 :                : 
                               5320                 :                :         /*
                               5321                 :                :          * On Windows, strxfrm returns INT_MAX when an error occurs. Instead
                               5322                 :                :          * of trying to allocate this much memory (and fail), just return the
                               5323                 :                :          * original string unmodified as if we were in the C locale.
                               5324                 :                :          */
                               5325                 :                :         if (xfrmlen == INT_MAX)
                               5326                 :                :             return val;
                               5327                 :                : #endif
 8328 tgl@sss.pgh.pa.us        5328                 :             66 :         xfrmstr = (char *) palloc(xfrmlen + 1);
  637 jdavis@postgresql.or     5329                 :             66 :         xfrmlen2 = pg_strxfrm(xfrmstr, val, xfrmlen + 1, mylocale);
                               5330                 :                : 
                               5331                 :                :         /*
                               5332                 :                :          * Some systems (e.g., glibc) can return a smaller value from the
                               5333                 :                :          * second call than the first; thus the Assert must be <= not ==.
                               5334                 :                :          */
 8328 tgl@sss.pgh.pa.us        5335         [ -  + ]:             66 :         Assert(xfrmlen2 <= xfrmlen);
 8798 peter_e@gmx.net          5336                 :             66 :         pfree(val);
                               5337                 :             66 :         val = xfrmstr;
                               5338                 :                :     }
                               5339                 :                : 
 7528 tgl@sss.pgh.pa.us        5340                 :          14655 :     return val;
                               5341                 :                : }
                               5342                 :                : 
                               5343                 :                : /*
                               5344                 :                :  * Do convert_to_scalar()'s work for any bytea data type.
                               5345                 :                :  *
                               5346                 :                :  * Very similar to convert_string_to_scalar except we can't assume
                               5347                 :                :  * null-termination and therefore pass explicit lengths around.
                               5348                 :                :  *
                               5349                 :                :  * Also, assumptions about likely "normal" ranges of characters have been
                               5350                 :                :  * removed - a data range of 0..255 is always used, for now.  (Perhaps
                               5351                 :                :  * someday we will add information about actual byte data range to
                               5352                 :                :  * pg_statistic.)
                               5353                 :                :  */
                               5354                 :                : static void
 9031 tgl@sss.pgh.pa.us        5355                 :UBC           0 : convert_bytea_to_scalar(Datum value,
                               5356                 :                :                         double *scaledvalue,
                               5357                 :                :                         Datum lobound,
                               5358                 :                :                         double *scaledlobound,
                               5359                 :                :                         Datum hibound,
                               5360                 :                :                         double *scaledhibound)
                               5361                 :                : {
 2985                          5362                 :              0 :     bytea      *valuep = DatumGetByteaPP(value);
                               5363                 :              0 :     bytea      *loboundp = DatumGetByteaPP(lobound);
                               5364                 :              0 :     bytea      *hiboundp = DatumGetByteaPP(hibound);
                               5365                 :                :     int         rangelo,
                               5366                 :                :                 rangehi,
                               5367   [ #  #  #  #  :              0 :                 valuelen = VARSIZE_ANY_EXHDR(valuep),
                                     #  #  #  #  #  
                                                 # ]
                               5368   [ #  #  #  #  :              0 :                 loboundlen = VARSIZE_ANY_EXHDR(loboundp),
                                     #  #  #  #  #  
                                                 # ]
                               5369   [ #  #  #  #  :              0 :                 hiboundlen = VARSIZE_ANY_EXHDR(hiboundp),
                                     #  #  #  #  #  
                                                 # ]
                               5370                 :                :                 i,
                               5371                 :                :                 minlen;
                               5372         [ #  # ]:              0 :     unsigned char *valstr = (unsigned char *) VARDATA_ANY(valuep);
                               5373         [ #  # ]:              0 :     unsigned char *lostr = (unsigned char *) VARDATA_ANY(loboundp);
                               5374         [ #  # ]:              0 :     unsigned char *histr = (unsigned char *) VARDATA_ANY(hiboundp);
                               5375                 :                : 
                               5376                 :                :     /*
                               5377                 :                :      * Assume bytea data is uniformly distributed across all byte values.
                               5378                 :                :      */
 9031                          5379                 :              0 :     rangelo = 0;
                               5380                 :              0 :     rangehi = 255;
                               5381                 :                : 
                               5382                 :                :     /*
                               5383                 :                :      * Now strip any common prefix of the three strings.
                               5384                 :                :      */
                               5385                 :              0 :     minlen = Min(Min(valuelen, loboundlen), hiboundlen);
                               5386         [ #  # ]:              0 :     for (i = 0; i < minlen; i++)
                               5387                 :                :     {
                               5388   [ #  #  #  # ]:              0 :         if (*lostr != *histr || *lostr != *valstr)
                               5389                 :                :             break;
                               5390                 :              0 :         lostr++, histr++, valstr++;
                               5391                 :              0 :         loboundlen--, hiboundlen--, valuelen--;
                               5392                 :                :     }
                               5393                 :                : 
                               5394                 :                :     /*
                               5395                 :                :      * Now we can do the conversions.
                               5396                 :                :      */
                               5397                 :              0 :     *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
                               5398                 :              0 :     *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
                               5399                 :              0 :     *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
                               5400                 :              0 : }
                               5401                 :                : 
                               5402                 :                : static double
                               5403                 :              0 : convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
                               5404                 :                :                             int rangelo, int rangehi)
                               5405                 :                : {
                               5406                 :                :     double      num,
                               5407                 :                :                 denom,
                               5408                 :                :                 base;
                               5409                 :                : 
                               5410         [ #  # ]:              0 :     if (valuelen <= 0)
                               5411                 :              0 :         return 0.0;             /* empty string has scalar value 0 */
                               5412                 :                : 
                               5413                 :                :     /*
                               5414                 :                :      * Since base is 256, need not consider more than about 10 chars (even
                               5415                 :                :      * this many seems like overkill)
                               5416                 :                :      */
                               5417         [ #  # ]:              0 :     if (valuelen > 10)
                               5418                 :              0 :         valuelen = 10;
                               5419                 :                : 
                               5420                 :                :     /* Convert initial characters to fraction */
                               5421                 :              0 :     base = rangehi - rangelo + 1;
                               5422                 :              0 :     num = 0.0;
                               5423                 :              0 :     denom = base;
                               5424         [ #  # ]:              0 :     while (valuelen-- > 0)
                               5425                 :                :     {
                               5426                 :              0 :         int         ch = *value++;
                               5427                 :                : 
                               5428         [ #  # ]:              0 :         if (ch < rangelo)
                               5429                 :              0 :             ch = rangelo - 1;
                               5430         [ #  # ]:              0 :         else if (ch > rangehi)
                               5431                 :              0 :             ch = rangehi + 1;
                               5432                 :              0 :         num += ((double) (ch - rangelo)) / denom;
                               5433                 :              0 :         denom *= base;
                               5434                 :                :     }
                               5435                 :                : 
                               5436                 :              0 :     return num;
                               5437                 :                : }
                               5438                 :                : 
                               5439                 :                : /*
                               5440                 :                :  * Do convert_to_scalar()'s work for any timevalue data type.
                               5441                 :                :  *
                               5442                 :                :  * On failure (e.g., unsupported typid), set *failure to true;
                               5443                 :                :  * otherwise, that variable is not changed.
                               5444                 :                :  */
                               5445                 :                : static double
 2985                          5446                 :              0 : convert_timevalue_to_scalar(Datum value, Oid typid, bool *failure)
                               5447                 :                : {
 9515                          5448   [ #  #  #  #  :              0 :     switch (typid)
                                           #  #  # ]
                               5449                 :                :     {
 9116                          5450                 :              0 :         case TIMESTAMPOID:
 9461                          5451                 :              0 :             return DatumGetTimestamp(value);
 8980                          5452                 :              0 :         case TIMESTAMPTZOID:
                               5453                 :              0 :             return DatumGetTimestampTz(value);
 9515                          5454                 :              0 :         case DATEOID:
 5607                          5455                 :              0 :             return date2timestamp_no_overflow(DatumGetDateADT(value));
 9515                          5456                 :              0 :         case INTERVALOID:
                               5457                 :                :             {
 9175 bruce@momjian.us         5458                 :              0 :                 Interval   *interval = DatumGetIntervalP(value);
                               5459                 :                : 
                               5460                 :                :                 /*
                               5461                 :                :                  * Convert the month part of Interval to days using assumed
                               5462                 :                :                  * average month length of 365.25/12.0 days.  Not too
                               5463                 :                :                  * accurate, but plenty good enough for our purposes.
                               5464                 :                :                  *
                               5465                 :                :                  * This also works for infinite intervals, which just have all
                               5466                 :                :                  * fields set to INT_MIN/INT_MAX, and so will produce a result
                               5467                 :                :                  * smaller/larger than any finite interval.
                               5468                 :                :                  */
 7507                          5469                 :              0 :                 return interval->time + interval->day * (double) USECS_PER_DAY +
                               5470                 :              0 :                     interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * USECS_PER_DAY);
                               5471                 :                :             }
 9515 tgl@sss.pgh.pa.us        5472                 :              0 :         case TIMEOID:
 9461                          5473                 :              0 :             return DatumGetTimeADT(value);
 9096                          5474                 :              0 :         case TIMETZOID:
                               5475                 :                :             {
                               5476                 :              0 :                 TimeTzADT  *timetz = DatumGetTimeTzADTP(value);
                               5477                 :                : 
                               5478                 :                :                 /* use GMT-equivalent time */
 8780 lockhart@fourpalms.o     5479                 :              0 :                 return (double) (timetz->time + (timetz->zone * 1000000.0));
                               5480                 :                :             }
                               5481                 :                :     }
                               5482                 :                : 
 2985 tgl@sss.pgh.pa.us        5483                 :              0 :     *failure = true;
 9515                          5484                 :              0 :     return 0;
                               5485                 :                : }
                               5486                 :                : 
                               5487                 :                : 
                               5488                 :                : /*
                               5489                 :                :  * get_restriction_variable
                               5490                 :                :  *      Examine the args of a restriction clause to see if it's of the
                               5491                 :                :  *      form (variable op pseudoconstant) or (pseudoconstant op variable),
                               5492                 :                :  *      where "variable" could be either a Var or an expression in vars of a
                               5493                 :                :  *      single relation.  If so, extract information about the variable,
                               5494                 :                :  *      and also indicate which side it was on and the other argument.
                               5495                 :                :  *
                               5496                 :                :  * Inputs:
                               5497                 :                :  *  root: the planner info
                               5498                 :                :  *  args: clause argument list
                               5499                 :                :  *  varRelid: see specs for restriction selectivity functions
                               5500                 :                :  *
                               5501                 :                :  * Outputs: (these are valid only if true is returned)
                               5502                 :                :  *  *vardata: gets information about variable (see examine_variable)
                               5503                 :                :  *  *other: gets other clause argument, aggressively reduced to a constant
                               5504                 :                :  *  *varonleft: set true if variable is on the left, false if on the right
                               5505                 :                :  *
                               5506                 :                :  * Returns true if a variable is identified, otherwise false.
                               5507                 :                :  *
                               5508                 :                :  * Note: if there are Vars on both sides of the clause, we must fail, because
                               5509                 :                :  * callers are expecting that the other side will act like a pseudoconstant.
                               5510                 :                :  */
                               5511                 :                : bool
 7639 tgl@sss.pgh.pa.us        5512                 :CBC      650958 : get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
                               5513                 :                :                          VariableStatData *vardata, Node **other,
                               5514                 :                :                          bool *varonleft)
                               5515                 :                : {
                               5516                 :                :     Node       *left,
                               5517                 :                :                *right;
                               5518                 :                :     VariableStatData rdata;
                               5519                 :                : 
                               5520                 :                :     /* Fail if not a binary opclause (probably shouldn't happen) */
 8010 neilc@samurai.com        5521         [ -  + ]:         650958 :     if (list_length(args) != 2)
 8113 tgl@sss.pgh.pa.us        5522                 :UBC           0 :         return false;
                               5523                 :                : 
 8014 neilc@samurai.com        5524                 :CBC      650958 :     left = (Node *) linitial(args);
 8113 tgl@sss.pgh.pa.us        5525                 :         650958 :     right = (Node *) lsecond(args);
                               5526                 :                : 
                               5527                 :                :     /*
                               5528                 :                :      * Examine both sides.  Note that when varRelid is nonzero, Vars of other
                               5529                 :                :      * relations will be treated as pseudoconstants.
                               5530                 :                :      */
                               5531                 :         650958 :     examine_variable(root, left, varRelid, vardata);
                               5532                 :         650958 :     examine_variable(root, right, varRelid, &rdata);
                               5533                 :                : 
                               5534                 :                :     /*
                               5535                 :                :      * If one side is a variable and the other not, we win.
                               5536                 :                :      */
                               5537   [ +  +  +  + ]:         650958 :     if (vardata->rel && rdata.rel == NULL)
                               5538                 :                :     {
                               5539                 :         579501 :         *varonleft = true;
 7015                          5540                 :         579501 :         *other = estimate_expression_value(root, rdata.var);
                               5541                 :                :         /* Assume we need no ReleaseVariableStats(rdata) here */
 8113                          5542                 :         579497 :         return true;
                               5543                 :                :     }
                               5544                 :                : 
                               5545   [ +  +  +  + ]:          71457 :     if (vardata->rel == NULL && rdata.rel)
                               5546                 :                :     {
                               5547                 :          67131 :         *varonleft = false;
 7015                          5548                 :          67131 :         *other = estimate_expression_value(root, vardata->var);
                               5549                 :                :         /* Assume we need no ReleaseVariableStats(*vardata) here */
 8113                          5550                 :          67131 :         *vardata = rdata;
                               5551                 :          67131 :         return true;
                               5552                 :                :     }
                               5553                 :                : 
                               5554                 :                :     /* Oops, clause has wrong structure (probably var op var) */
                               5555         [ +  + ]:           4326 :     ReleaseVariableStats(*vardata);
                               5556         [ +  + ]:           4326 :     ReleaseVariableStats(rdata);
                               5557                 :                : 
                               5558                 :           4326 :     return false;
                               5559                 :                : }
                               5560                 :                : 
                               5561                 :                : /*
                               5562                 :                :  * get_join_variables
                               5563                 :                :  *      Apply examine_variable() to each side of a join clause.
                               5564                 :                :  *      Also, attempt to identify whether the join clause has the same
                               5565                 :                :  *      or reversed sense compared to the SpecialJoinInfo.
                               5566                 :                :  *
                               5567                 :                :  * We consider the join clause "normal" if it is "lhs_var OP rhs_var",
                               5568                 :                :  * or "reversed" if it is "rhs_var OP lhs_var".  In complicated cases
                               5569                 :                :  * where we can't tell for sure, we default to assuming it's normal.
                               5570                 :                :  */
                               5571                 :                : void
 6471                          5572                 :         219832 : get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo,
                               5573                 :                :                    VariableStatData *vardata1, VariableStatData *vardata2,
                               5574                 :                :                    bool *join_is_reversed)
                               5575                 :                : {
                               5576                 :                :     Node       *left,
                               5577                 :                :                *right;
                               5578                 :                : 
 8010 neilc@samurai.com        5579         [ -  + ]:         219832 :     if (list_length(args) != 2)
 8113 tgl@sss.pgh.pa.us        5580         [ #  # ]:UBC           0 :         elog(ERROR, "join operator should take two arguments");
                               5581                 :                : 
 8014 neilc@samurai.com        5582                 :CBC      219832 :     left = (Node *) linitial(args);
 9116 tgl@sss.pgh.pa.us        5583                 :         219832 :     right = (Node *) lsecond(args);
                               5584                 :                : 
 8113                          5585                 :         219832 :     examine_variable(root, left, 0, vardata1);
                               5586                 :         219832 :     examine_variable(root, right, 0, vardata2);
                               5587                 :                : 
 6471                          5588   [ +  +  +  + ]:         439453 :     if (vardata1->rel &&
                               5589                 :         219621 :         bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
 3240                          5590                 :          71828 :         *join_is_reversed = true;   /* var1 is on RHS */
 6471                          5591   [ +  +  +  + ]:         295797 :     else if (vardata2->rel &&
                               5592                 :         147793 :              bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
 3240                          5593                 :            304 :         *join_is_reversed = true;   /* var2 is on LHS */
                               5594                 :                :     else
 6471                          5595                 :         147700 :         *join_is_reversed = false;
 8113                          5596                 :         219832 : }
                               5597                 :                : 
                               5598                 :                : /* statext_expressions_load copies the tuple, so just pfree it. */
                               5599                 :                : static void
 1866 tomas.vondra@postgre     5600                 :           1415 : ReleaseDummy(HeapTuple tuple)
                               5601                 :                : {
                               5602                 :           1415 :     pfree(tuple);
                               5603                 :           1415 : }
                               5604                 :                : 
                               5605                 :                : /*
                               5606                 :                :  * examine_variable
                               5607                 :                :  *      Try to look up statistical data about an expression.
                               5608                 :                :  *      Fill in a VariableStatData struct to describe the expression.
                               5609                 :                :  *
                               5610                 :                :  * Inputs:
                               5611                 :                :  *  root: the planner info
                               5612                 :                :  *  node: the expression tree to examine
                               5613                 :                :  *  varRelid: see specs for restriction selectivity functions
                               5614                 :                :  *
                               5615                 :                :  * Outputs: *vardata is filled as follows:
                               5616                 :                :  *  var: the input expression (with any phvs or binary relabeling stripped,
                               5617                 :                :  *      if it is or contains a variable; but otherwise unchanged)
                               5618                 :                :  *  rel: RelOptInfo for relation containing variable; NULL if expression
                               5619                 :                :  *      contains no Vars (NOTE this could point to a RelOptInfo of a
                               5620                 :                :  *      subquery, not one in the current query).
                               5621                 :                :  *  statsTuple: the pg_statistic entry for the variable, if one exists;
                               5622                 :                :  *      otherwise NULL.
                               5623                 :                :  *  freefunc: pointer to a function to release statsTuple with.
                               5624                 :                :  *  vartype: exposed type of the expression; this should always match
                               5625                 :                :  *      the declared input type of the operator we are estimating for.
                               5626                 :                :  *  atttype, atttypmod: actual type/typmod of the "var" expression.  This is
                               5627                 :                :  *      commonly the same as the exposed type of the variable argument,
                               5628                 :                :  *      but can be different in binary-compatible-type cases.
                               5629                 :                :  *  isunique: true if we were able to match the var to a unique index, a
                               5630                 :                :  *      single-column DISTINCT or GROUP-BY clause, implying its values are
                               5631                 :                :  *      unique for this query.  (Caution: this should be trusted for
                               5632                 :                :  *      statistical purposes only, since we do not check indimmediate nor
                               5633                 :                :  *      verify that the exact same definition of equality applies.)
                               5634                 :                :  *  acl_ok: true if current user has permission to read all table rows from
                               5635                 :                :  *      the column(s) underlying the pg_statistic entry.  This is consulted by
                               5636                 :                :  *      statistic_proc_security_check().
                               5637                 :                :  *
                               5638                 :                :  * Caller is responsible for doing ReleaseVariableStats() before exiting.
                               5639                 :                :  */
                               5640                 :                : void
 7639 tgl@sss.pgh.pa.us        5641                 :        2570774 : examine_variable(PlannerInfo *root, Node *node, int varRelid,
                               5642                 :                :                  VariableStatData *vardata)
                               5643                 :                : {
                               5644                 :                :     Node       *basenode;
                               5645                 :                :     Relids      varnos;
                               5646                 :                :     Relids      basevarnos;
                               5647                 :                :     RelOptInfo *onerel;
                               5648                 :                : 
                               5649                 :                :     /* Make sure we don't return dangling pointers in vardata */
 8113                          5650   [ +  -  +  -  :       17995418 :     MemSet(vardata, 0, sizeof(VariableStatData));
                                     +  -  +  -  +  
                                                 + ]
                               5651                 :                : 
                               5652                 :                :     /* Save the exposed type of the expression */
 7704                          5653                 :        2570774 :     vardata->vartype = exprType(node);
                               5654                 :                : 
                               5655                 :                :     /*
                               5656                 :                :      * PlaceHolderVars are transparent for the purpose of statistics lookup;
                               5657                 :                :      * they do not alter the value distribution of the underlying expression.
                               5658                 :                :      * However, they can obscure the structure, preventing us from recognizing
                               5659                 :                :      * matches to base columns, index expressions, or extended statistics.  So
                               5660                 :                :      * strip them out first.
                               5661                 :                :      */
  127 rguo@postgresql.org      5662                 :        2570774 :     basenode = strip_all_phvs_deep(root, node);
                               5663                 :                : 
                               5664                 :                :     /*
                               5665                 :                :      * Look inside any binary-compatible relabeling.  We need to handle nested
                               5666                 :                :      * RelabelType nodes here, because the prior stripping of PlaceHolderVars
                               5667                 :                :      * may have brought separate RelabelTypes into adjacency.
                               5668                 :                :      */
                               5669         [ +  + ]:        2611517 :     while (IsA(basenode, RelabelType))
                               5670                 :          40743 :         basenode = (Node *) ((RelabelType *) basenode)->arg;
                               5671                 :                : 
                               5672                 :                :     /* Fast path for a simple Var */
 7710 tgl@sss.pgh.pa.us        5673   [ +  +  +  + ]:        2570774 :     if (IsA(basenode, Var) &&
                               5674         [ +  + ]:         675788 :         (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
                               5675                 :                :     {
                               5676                 :        1830049 :         Var        *var = (Var *) basenode;
                               5677                 :                : 
                               5678                 :                :         /* Set up result fields other than the stats tuple */
  127 rguo@postgresql.org      5679                 :        1830049 :         vardata->var = basenode; /* return Var without phvs or relabeling */
 8113 tgl@sss.pgh.pa.us        5680                 :        1830049 :         vardata->rel = find_base_rel(root, var->varno);
                               5681                 :        1830049 :         vardata->atttype = var->vartype;
                               5682                 :        1830049 :         vardata->atttypmod = var->vartypmod;
 6288                          5683                 :        1830049 :         vardata->isunique = has_unique_index(vardata->rel, var->varattno);
                               5684                 :                : 
                               5685                 :                :         /* Try to locate some stats */
 5357                          5686                 :        1830049 :         examine_simple_variable(root, var, vardata);
                               5687                 :                : 
 8113                          5688                 :        1830049 :         return;
                               5689                 :                :     }
                               5690                 :                : 
                               5691                 :                :     /*
                               5692                 :                :      * Okay, it's a more complicated expression.  Determine variable
                               5693                 :                :      * membership.  Note that when varRelid isn't zero, only vars of that
                               5694                 :                :      * relation are considered "real" vars.
                               5695                 :                :      */
 1930                          5696                 :         740725 :     varnos = pull_varnos(root, basenode);
  488 rguo@postgresql.org      5697                 :         740725 :     basevarnos = bms_difference(varnos, root->outer_join_rels);
                               5698                 :                : 
 8113 tgl@sss.pgh.pa.us        5699                 :         740725 :     onerel = NULL;
                               5700                 :                : 
  488 rguo@postgresql.org      5701         [ +  + ]:         740725 :     if (bms_is_empty(basevarnos))
                               5702                 :                :     {
                               5703                 :                :         /* No Vars at all ... must be pseudo-constant clause */
                               5704                 :                :     }
                               5705                 :                :     else
                               5706                 :                :     {
                               5707                 :                :         int         relid;
                               5708                 :                : 
                               5709                 :                :         /* Check if the expression is in vars of a single base relation */
                               5710         [ +  + ]:         394675 :         if (bms_get_singleton_member(basevarnos, &relid))
                               5711                 :                :         {
  889 drowley@postgresql.o     5712   [ +  +  +  + ]:         391056 :             if (varRelid == 0 || varRelid == relid)
                               5713                 :                :             {
                               5714                 :          54135 :                 onerel = find_base_rel(root, relid);
 8113 tgl@sss.pgh.pa.us        5715                 :          54135 :                 vardata->rel = onerel;
  127 rguo@postgresql.org      5716                 :          54135 :                 node = basenode;    /* strip any phvs or relabeling */
                               5717                 :                :             }
                               5718                 :                :             /* else treat it as a constant */
                               5719                 :                :         }
                               5720                 :                :         else
                               5721                 :                :         {
                               5722                 :                :             /* varnos has multiple relids */
 8113 tgl@sss.pgh.pa.us        5723         [ +  + ]:           3619 :             if (varRelid == 0)
                               5724                 :                :             {
                               5725                 :                :                 /* treat it as a variable of a join relation */
                               5726                 :           2659 :                 vardata->rel = find_join_rel(root, varnos);
  127 rguo@postgresql.org      5727                 :           2659 :                 node = basenode;    /* strip any phvs or relabeling */
                               5728                 :                :             }
 8113 tgl@sss.pgh.pa.us        5729         [ +  + ]:            960 :             else if (bms_is_member(varRelid, varnos))
                               5730                 :                :             {
                               5731                 :                :                 /* ignore the vars belonging to other relations */
                               5732                 :            865 :                 vardata->rel = find_base_rel(root, varRelid);
  127 rguo@postgresql.org      5733                 :            865 :                 node = basenode;    /* strip any phvs or relabeling */
                               5734                 :                :                 /* note: no point in expressional-index search here */
                               5735                 :                :             }
                               5736                 :                :             /* else treat it as a constant */
                               5737                 :                :         }
                               5738                 :                :     }
                               5739                 :                : 
  488                          5740                 :         740725 :     bms_free(basevarnos);
                               5741                 :                : 
 7710 tgl@sss.pgh.pa.us        5742                 :         740725 :     vardata->var = node;
 8113                          5743                 :         740725 :     vardata->atttype = exprType(node);
                               5744                 :         740725 :     vardata->atttypmod = exprTypmod(node);
                               5745                 :                : 
                               5746         [ +  + ]:         740725 :     if (onerel)
                               5747                 :                :     {
                               5748                 :                :         /*
                               5749                 :                :          * We have an expression in vars of a single relation.  Try to match
                               5750                 :                :          * it to expressional index columns, in hopes of finding some
                               5751                 :                :          * statistics.
                               5752                 :                :          *
                               5753                 :                :          * Note that we consider all index columns including INCLUDE columns,
                               5754                 :                :          * since there could be stats for such columns.  But the test for
                               5755                 :                :          * uniqueness needs to be warier.
                               5756                 :                :          *
                               5757                 :                :          * XXX it's conceivable that there are multiple matches with different
                               5758                 :                :          * index opfamilies; if so, we need to pick one that matches the
                               5759                 :                :          * operator we are estimating for.  FIXME later.
                               5760                 :                :          */
                               5761                 :                :         ListCell   *ilist;
                               5762                 :                :         ListCell   *slist;
                               5763                 :                : 
                               5764                 :                :         /*
                               5765                 :                :          * The nullingrels bits within the expression could prevent us from
                               5766                 :                :          * matching it to expressional index columns or to the expressions in
                               5767                 :                :          * extended statistics.  So strip them out first.
                               5768                 :                :          */
  488 rguo@postgresql.org      5769         [ +  + ]:          54135 :         if (bms_overlap(varnos, root->outer_join_rels))
                               5770                 :           1565 :             node = remove_nulling_relids(node, root->outer_join_rels, NULL);
                               5771                 :                : 
 8113 tgl@sss.pgh.pa.us        5772   [ +  +  +  +  :         119249 :         foreach(ilist, onerel->indexlist)
                                              +  + ]
                               5773                 :                :         {
                               5774                 :          67491 :             IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
                               5775                 :                :             ListCell   *indexpr_item;
                               5776                 :                :             int         pos;
                               5777                 :                : 
 8014 neilc@samurai.com        5778                 :          67491 :             indexpr_item = list_head(index->indexprs);
                               5779         [ +  + ]:          67491 :             if (indexpr_item == NULL)
 8113 tgl@sss.pgh.pa.us        5780                 :          63539 :                 continue;       /* no expressions here... */
                               5781                 :                : 
                               5782         [ +  + ]:           5587 :             for (pos = 0; pos < index->ncolumns; pos++)
                               5783                 :                :             {
                               5784         [ +  + ]:           4012 :                 if (index->indexkeys[pos] == 0)
                               5785                 :                :                 {
                               5786                 :                :                     Node       *indexkey;
                               5787                 :                : 
 8014 neilc@samurai.com        5788         [ -  + ]:           3952 :                     if (indexpr_item == NULL)
 8113 tgl@sss.pgh.pa.us        5789         [ #  # ]:UBC           0 :                         elog(ERROR, "too few entries in indexprs list");
 8014 neilc@samurai.com        5790                 :CBC        3952 :                     indexkey = (Node *) lfirst(indexpr_item);
 8113 tgl@sss.pgh.pa.us        5791   [ +  -  -  + ]:           3952 :                     if (indexkey && IsA(indexkey, RelabelType))
 8113 tgl@sss.pgh.pa.us        5792                 :UBC           0 :                         indexkey = (Node *) ((RelabelType *) indexkey)->arg;
 8113 tgl@sss.pgh.pa.us        5793         [ +  + ]:CBC        3952 :                     if (equal(node, indexkey))
                               5794                 :                :                     {
                               5795                 :                :                         /*
                               5796                 :                :                          * Found a match ... is it a unique index? Tests here
                               5797                 :                :                          * should match has_unique_index().
                               5798                 :                :                          */
                               5799         [ +  + ]:           2907 :                         if (index->unique &&
 2950 teodor@sigaev.ru         5800   [ +  -  +  - ]:            365 :                             index->nkeycolumns == 1 &&
 2639 tgl@sss.pgh.pa.us        5801                 :            365 :                             pos == 0 &&
 6288                          5802   [ -  +  -  - ]:            365 :                             (index->indpred == NIL || index->predOK))
 8113                          5803                 :            365 :                             vardata->isunique = true;
                               5804                 :                : 
                               5805                 :                :                         /*
                               5806                 :                :                          * Has it got stats?  We only consider stats for
                               5807                 :                :                          * non-partial indexes, since partial indexes probably
                               5808                 :                :                          * don't reflect whole-relation statistics; the above
                               5809                 :                :                          * check for uniqueness is the only info we take from
                               5810                 :                :                          * a partial index.
                               5811                 :                :                          *
                               5812                 :                :                          * An index stats hook, however, must make its own
                               5813                 :                :                          * decisions about what to do with partial indexes.
                               5814                 :                :                          */
 6428                          5815   [ -  +  -  - ]:           2907 :                         if (get_index_stats_hook &&
 6428 tgl@sss.pgh.pa.us        5816                 :UBC           0 :                             (*get_index_stats_hook) (root, index->indexoid,
                               5817                 :              0 :                                                      pos + 1, vardata))
                               5818                 :                :                         {
                               5819                 :                :                             /*
                               5820                 :                :                              * The hook took control of acquiring a stats
                               5821                 :                :                              * tuple.  If it did supply a tuple, it'd better
                               5822                 :                :                              * have supplied a freefunc.
                               5823                 :                :                              */
                               5824         [ #  # ]:              0 :                             if (HeapTupleIsValid(vardata->statsTuple) &&
                               5825         [ #  # ]:              0 :                                 !vardata->freefunc)
                               5826         [ #  # ]:              0 :                                 elog(ERROR, "no function provided to release variable stats with");
                               5827                 :                :                         }
 6288 tgl@sss.pgh.pa.us        5828         [ +  - ]:CBC        2907 :                         else if (index->indpred == NIL)
                               5829                 :                :                         {
 6428                          5830                 :           2907 :                             vardata->statsTuple =
 5924 rhaas@postgresql.org     5831                 :           5814 :                                 SearchSysCache3(STATRELATTINH,
                               5832                 :                :                                                 ObjectIdGetDatum(index->indexoid),
 5912 bruce@momjian.us         5833                 :           2907 :                                                 Int16GetDatum(pos + 1),
                               5834                 :                :                                                 BoolGetDatum(false));
 6428 tgl@sss.pgh.pa.us        5835                 :           2907 :                             vardata->freefunc = ReleaseSysCache;
                               5836                 :                : 
 3287 peter_e@gmx.net          5837         [ +  + ]:           2907 :                             if (HeapTupleIsValid(vardata->statsTuple))
                               5838                 :                :                             {
                               5839                 :                :                                 /*
                               5840                 :                :                                  * Test if user has permission to access all
                               5841                 :                :                                  * rows from the index's table.
                               5842                 :                :                                  *
                               5843                 :                :                                  * For simplicity, we insist on the whole
                               5844                 :                :                                  * table being selectable, rather than trying
                               5845                 :                :                                  * to identify which column(s) the index
                               5846                 :                :                                  * depends on.
                               5847                 :                :                                  *
                               5848                 :                :                                  * Note that for an inheritance child,
                               5849                 :                :                                  * permissions are checked on the inheritance
                               5850                 :                :                                  * root parent, and whole-table select
                               5851                 :                :                                  * privilege on the parent doesn't quite
                               5852                 :                :                                  * guarantee that the user could read all
                               5853                 :                :                                  * columns of the child.  But in practice it's
                               5854                 :                :                                  * unlikely that any interesting security
                               5855                 :                :                                  * violation could result from allowing access
                               5856                 :                :                                  * to the expression index's stats, so we
                               5857                 :                :                                  * allow it anyway.  See similar code in
                               5858                 :                :                                  * examine_simple_variable() for additional
                               5859                 :                :                                  * comments.
                               5860                 :                :                                  */
                               5861                 :           2377 :                                 vardata->acl_ok =
  267 dean.a.rasheed@gmail     5862                 :           2377 :                                     all_rows_selectable(root,
                               5863                 :           2377 :                                                         index->rel->relid,
                               5864                 :                :                                                         NULL);
                               5865                 :                :                             }
                               5866                 :                :                             else
                               5867                 :                :                             {
                               5868                 :                :                                 /* suppress leakproofness checks later */
 3287 peter_e@gmx.net          5869                 :            530 :                                 vardata->acl_ok = true;
                               5870                 :                :                             }
                               5871                 :                :                         }
 8113 tgl@sss.pgh.pa.us        5872         [ +  + ]:           2907 :                         if (vardata->statsTuple)
                               5873                 :           2377 :                             break;
                               5874                 :                :                     }
 2486                          5875                 :           1575 :                     indexpr_item = lnext(index->indexprs, indexpr_item);
                               5876                 :                :                 }
                               5877                 :                :             }
 8113                          5878         [ +  + ]:           3952 :             if (vardata->statsTuple)
                               5879                 :           2377 :                 break;
                               5880                 :                :         }
                               5881                 :                : 
                               5882                 :                :         /*
                               5883                 :                :          * Search extended statistics for one with a matching expression.
                               5884                 :                :          * There might be multiple ones, so just grab the first one. In the
                               5885                 :                :          * future, we might consider the statistics target (and pick the most
                               5886                 :                :          * accurate statistics) and maybe some other parameters.
                               5887                 :                :          */
 1866 tomas.vondra@postgre     5888   [ +  +  +  +  :          57664 :         foreach(slist, onerel->statlist)
                                              +  + ]
                               5889                 :                :         {
                               5890                 :           3774 :             StatisticExtInfo *info = (StatisticExtInfo *) lfirst(slist);
 1454 tgl@sss.pgh.pa.us        5891         [ +  - ]:           3774 :             RangeTblEntry *rte = planner_rt_fetch(onerel->relid, root);
                               5892                 :                :             ListCell   *expr_item;
                               5893                 :                :             int         pos;
                               5894                 :                : 
                               5895                 :                :             /*
                               5896                 :                :              * Stop once we've found statistics for the expression (either
                               5897                 :                :              * from extended stats, or for an index in the preceding loop).
                               5898                 :                :              */
 1866 tomas.vondra@postgre     5899         [ +  + ]:           3774 :             if (vardata->statsTuple)
                               5900                 :            245 :                 break;
                               5901                 :                : 
                               5902                 :                :             /* skip stats without per-expression stats */
                               5903         [ +  + ]:           3529 :             if (info->kind != STATS_EXT_EXPRESSIONS)
                               5904                 :           1808 :                 continue;
                               5905                 :                : 
                               5906                 :                :             /* skip stats with mismatching stxdinherit value */
 1281 tgl@sss.pgh.pa.us        5907         [ +  + ]:           1721 :             if (info->inherit != rte->inh)
                               5908                 :              5 :                 continue;
                               5909                 :                : 
 1866 tomas.vondra@postgre     5910                 :           1716 :             pos = 0;
                               5911   [ +  -  +  +  :           2836 :             foreach(expr_item, info->exprs)
                                              +  + ]
                               5912                 :                :             {
                               5913                 :           2536 :                 Node       *expr = (Node *) lfirst(expr_item);
                               5914                 :                : 
                               5915         [ -  + ]:           2536 :                 Assert(expr);
                               5916                 :                : 
                               5917                 :                :                 /* strip RelabelType before comparing it */
                               5918   [ +  -  -  + ]:           2536 :                 if (expr && IsA(expr, RelabelType))
 1866 tomas.vondra@postgre     5919                 :UBC           0 :                     expr = (Node *) ((RelabelType *) expr)->arg;
                               5920                 :                : 
                               5921                 :                :                 /* found a match, see if we can extract pg_statistic row */
 1866 tomas.vondra@postgre     5922         [ +  + ]:CBC        2536 :                 if (equal(node, expr))
                               5923                 :                :                 {
                               5924                 :                :                     /*
                               5925                 :                :                      * XXX Not sure if we should cache the tuple somewhere.
                               5926                 :                :                      * Now we just create a new copy every time.
                               5927                 :                :                      */
 1570                          5928                 :           1416 :                     vardata->statsTuple =
                               5929                 :           1416 :                         statext_expressions_load(info->statOid, rte->inh, pos);
                               5930                 :                : 
                               5931                 :                :                     /* Nothing to release if no data found */
   64 michael@paquier.xyz      5932         [ +  + ]:           1416 :                     if (vardata->statsTuple != NULL)
                               5933                 :                :                     {
                               5934                 :           1415 :                         vardata->freefunc = ReleaseDummy;
                               5935                 :                :                     }
                               5936                 :                : 
                               5937                 :                :                     /*
                               5938                 :                :                      * Test if user has permission to access all rows from the
                               5939                 :                :                      * table.
                               5940                 :                :                      *
                               5941                 :                :                      * For simplicity, we insist on the whole table being
                               5942                 :                :                      * selectable, rather than trying to identify which
                               5943                 :                :                      * column(s) the statistics object depends on.
                               5944                 :                :                      *
                               5945                 :                :                      * Note that for an inheritance child, permissions are
                               5946                 :                :                      * checked on the inheritance root parent, and whole-table
                               5947                 :                :                      * select privilege on the parent doesn't quite guarantee
                               5948                 :                :                      * that the user could read all columns of the child.  But
                               5949                 :                :                      * in practice it's unlikely that any interesting security
                               5950                 :                :                      * violation could result from allowing access to the
                               5951                 :                :                      * expression stats, so we allow it anyway.  See similar
                               5952                 :                :                      * code in examine_simple_variable() for additional
                               5953                 :                :                      * comments.
                               5954                 :                :                      */
  267 dean.a.rasheed@gmail     5955                 :           1416 :                     vardata->acl_ok = all_rows_selectable(root,
                               5956                 :                :                                                           onerel->relid,
                               5957                 :                :                                                           NULL);
                               5958                 :                : 
 1866 tomas.vondra@postgre     5959                 :           1416 :                     break;
                               5960                 :                :                 }
                               5961                 :                : 
                               5962                 :           1120 :                 pos++;
                               5963                 :                :             }
                               5964                 :                :         }
                               5965                 :                :     }
                               5966                 :                : 
  488 rguo@postgresql.org      5967                 :         740725 :     bms_free(varnos);
                               5968                 :                : }
                               5969                 :                : 
                               5970                 :                : /*
                               5971                 :                :  * strip_all_phvs_deep
                               5972                 :                :  *      Deeply strip all PlaceHolderVars in an expression.
                               5973                 :                : 
                               5974                 :                :  * As a performance optimization, we first use a lightweight walker to check
                               5975                 :                :  * for the presence of any PlaceHolderVars.  The expensive mutator is invoked
                               5976                 :                :  * only if a PlaceHolderVar is found, avoiding unnecessary memory allocation
                               5977                 :                :  * and tree copying in the common case where no PlaceHolderVars are present.
                               5978                 :                :  */
                               5979                 :                : static Node *
  127                          5980                 :        2570774 : strip_all_phvs_deep(PlannerInfo *root, Node *node)
                               5981                 :                : {
                               5982                 :                :     /* If there are no PHVs anywhere, we needn't work hard */
                               5983         [ +  + ]:        2570774 :     if (root->glob->lastPHId == 0)
                               5984                 :        2544750 :         return node;
                               5985                 :                : 
                               5986         [ +  + ]:          26024 :     if (!contain_placeholder_walker(node, NULL))
                               5987                 :          22346 :         return node;
                               5988                 :           3678 :     return strip_all_phvs_mutator(node, NULL);
                               5989                 :                : }
                               5990                 :                : 
                               5991                 :                : /*
                               5992                 :                :  * contain_placeholder_walker
                               5993                 :                :  *      Lightweight walker to check if an expression contains any
                               5994                 :                :  *      PlaceHolderVars
                               5995                 :                :  */
                               5996                 :                : static bool
                               5997                 :          29350 : contain_placeholder_walker(Node *node, void *context)
                               5998                 :                : {
                               5999         [ +  + ]:          29350 :     if (node == NULL)
                               6000                 :            109 :         return false;
                               6001         [ +  + ]:          29241 :     if (IsA(node, PlaceHolderVar))
                               6002                 :           3678 :         return true;
                               6003                 :                : 
                               6004                 :          25563 :     return expression_tree_walker(node, contain_placeholder_walker, context);
                               6005                 :                : }
                               6006                 :                : 
                               6007                 :                : /*
                               6008                 :                :  * strip_all_phvs_mutator
                               6009                 :                :  *      Mutator to deeply strip all PlaceHolderVars
                               6010                 :                :  */
                               6011                 :                : static Node *
                               6012                 :           9809 : strip_all_phvs_mutator(Node *node, void *context)
                               6013                 :                : {
                               6014         [ +  + ]:           9809 :     if (node == NULL)
                               6015                 :             34 :         return NULL;
                               6016         [ +  + ]:           9775 :     if (IsA(node, PlaceHolderVar))
                               6017                 :                :     {
                               6018                 :                :         /* Strip it and recurse into its contained expression */
                               6019                 :           3798 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
                               6020                 :                : 
                               6021                 :           3798 :         return strip_all_phvs_mutator((Node *) phv->phexpr, context);
                               6022                 :                :     }
                               6023                 :                : 
                               6024                 :           5977 :     return expression_tree_mutator(node, strip_all_phvs_mutator, context);
                               6025                 :                : }
                               6026                 :                : 
                               6027                 :                : /*
                               6028                 :                :  * examine_simple_variable
                               6029                 :                :  *      Handle a simple Var for examine_variable
                               6030                 :                :  *
                               6031                 :                :  * This is split out as a subroutine so that we can recurse to deal with
                               6032                 :                :  * Vars referencing subqueries (either sub-SELECT-in-FROM or CTE style).
                               6033                 :                :  *
                               6034                 :                :  * We already filled in all the fields of *vardata except for the stats tuple.
                               6035                 :                :  */
                               6036                 :                : static void
 5357 tgl@sss.pgh.pa.us        6037                 :        1838143 : examine_simple_variable(PlannerInfo *root, Var *var,
                               6038                 :                :                         VariableStatData *vardata)
                               6039                 :                : {
                               6040                 :        1838143 :     RangeTblEntry *rte = root->simple_rte_array[var->varno];
                               6041                 :                : 
                               6042         [ -  + ]:        1838143 :     Assert(IsA(rte, RangeTblEntry));
                               6043                 :                : 
                               6044   [ -  +  -  - ]:        1838143 :     if (get_relation_stats_hook &&
 5357 tgl@sss.pgh.pa.us        6045                 :UBC           0 :         (*get_relation_stats_hook) (root, rte, var->varattno, vardata))
                               6046                 :                :     {
                               6047                 :                :         /*
                               6048                 :                :          * The hook took control of acquiring a stats tuple.  If it did supply
                               6049                 :                :          * a tuple, it'd better have supplied a freefunc.
                               6050                 :                :          */
                               6051         [ #  # ]:              0 :         if (HeapTupleIsValid(vardata->statsTuple) &&
                               6052         [ #  # ]:              0 :             !vardata->freefunc)
                               6053         [ #  # ]:              0 :             elog(ERROR, "no function provided to release variable stats with");
                               6054                 :                :     }
 5357 tgl@sss.pgh.pa.us        6055         [ +  + ]:CBC     1838143 :     else if (rte->rtekind == RTE_RELATION)
                               6056                 :                :     {
                               6057                 :                :         /*
                               6058                 :                :          * Plain table or parent of an inheritance appendrel, so look up the
                               6059                 :                :          * column in pg_statistic
                               6060                 :                :          */
                               6061                 :        1745336 :         vardata->statsTuple = SearchSysCache3(STATRELATTINH,
                               6062                 :                :                                               ObjectIdGetDatum(rte->relid),
                               6063                 :        1745336 :                                               Int16GetDatum(var->varattno),
                               6064                 :        1745336 :                                               BoolGetDatum(rte->inh));
                               6065                 :        1745336 :         vardata->freefunc = ReleaseSysCache;
                               6066                 :                : 
 3287 peter_e@gmx.net          6067         [ +  + ]:        1745336 :         if (HeapTupleIsValid(vardata->statsTuple))
                               6068                 :                :         {
                               6069                 :                :             /*
                               6070                 :                :              * Test if user has permission to read all rows from this column.
                               6071                 :                :              *
                               6072                 :                :              * This requires that the user has the appropriate SELECT
                               6073                 :                :              * privileges and that there are no securityQuals from security
                               6074                 :                :              * barrier views or RLS policies.  If that's not the case, then we
                               6075                 :                :              * only permit leakproof functions to be passed pg_statistic data
                               6076                 :                :              * in vardata, otherwise the functions might reveal data that the
                               6077                 :                :              * user doesn't have permission to see --- see
                               6078                 :                :              * statistic_proc_security_check().
                               6079                 :                :              */
                               6080                 :        1218783 :             vardata->acl_ok =
  267 dean.a.rasheed@gmail     6081                 :        1218783 :                 all_rows_selectable(root, var->varno,
                               6082                 :        1218783 :                                     bms_make_singleton(var->varattno - FirstLowInvalidHeapAttributeNumber));
                               6083                 :                :         }
                               6084                 :                :         else
                               6085                 :                :         {
                               6086                 :                :             /* suppress any possible leakproofness checks later */
 3287 peter_e@gmx.net          6087                 :         526553 :             vardata->acl_ok = true;
                               6088                 :                :         }
                               6089                 :                :     }
  900 tgl@sss.pgh.pa.us        6090   [ +  +  +  + ]:          92807 :     else if ((rte->rtekind == RTE_SUBQUERY && !rte->inh) ||
                               6091   [ +  +  +  + ]:          85040 :              (rte->rtekind == RTE_CTE && !rte->self_reference))
                               6092                 :                :     {
                               6093                 :                :         /*
                               6094                 :                :          * Plain subquery (not one that was converted to an appendrel) or
                               6095                 :                :          * non-recursive CTE.  In either case, we can try to find out what the
                               6096                 :                :          * Var refers to within the subquery.  We skip this for appendrel and
                               6097                 :                :          * recursive-CTE cases because any column stats we did find would
                               6098                 :                :          * likely not be very relevant.
                               6099                 :                :          */
                               6100                 :                :         PlannerInfo *subroot;
                               6101                 :                :         Query      *subquery;
                               6102                 :                :         List       *subtlist;
                               6103                 :                :         TargetEntry *ste;
                               6104                 :                : 
                               6105                 :                :         /*
                               6106                 :                :          * Punt if it's a whole-row var rather than a plain column reference.
                               6107                 :                :          */
 4558                          6108         [ -  + ]:          15738 :         if (var->varattno == InvalidAttrNumber)
 4558 tgl@sss.pgh.pa.us        6109                 :UBC           0 :             return;
                               6110                 :                : 
                               6111                 :                :         /*
                               6112                 :                :          * Otherwise, find the subquery's planner subroot.
                               6113                 :                :          */
  900 tgl@sss.pgh.pa.us        6114         [ +  + ]:CBC       15738 :         if (rte->rtekind == RTE_SUBQUERY)
                               6115                 :                :         {
                               6116                 :                :             RelOptInfo *rel;
                               6117                 :                : 
                               6118                 :                :             /*
                               6119                 :                :              * Fetch RelOptInfo for subquery.  Note that we don't change the
                               6120                 :                :              * rel returned in vardata, since caller expects it to be a rel of
                               6121                 :                :              * the caller's query level.  Because we might already be
                               6122                 :                :              * recursing, we can't use that rel pointer either, but have to
                               6123                 :                :              * look up the Var's rel afresh.
                               6124                 :                :              */
                               6125                 :           7767 :             rel = find_base_rel(root, var->varno);
                               6126                 :                : 
                               6127                 :           7767 :             subroot = rel->subroot;
                               6128                 :                :         }
                               6129                 :                :         else
                               6130                 :                :         {
                               6131                 :                :             /* CTE case is more difficult */
                               6132                 :                :             PlannerInfo *cteroot;
                               6133                 :                :             Index       levelsup;
                               6134                 :                :             int         ndx;
                               6135                 :                :             int         plan_id;
                               6136                 :                :             ListCell   *lc;
                               6137                 :                : 
                               6138                 :                :             /*
                               6139                 :                :              * Find the referenced CTE, and locate the subroot previously made
                               6140                 :                :              * for it.
                               6141                 :                :              */
                               6142                 :           7971 :             levelsup = rte->ctelevelsup;
                               6143                 :           7971 :             cteroot = root;
                               6144         [ +  + ]:          19011 :             while (levelsup-- > 0)
                               6145                 :                :             {
                               6146                 :          11040 :                 cteroot = cteroot->parent_root;
                               6147         [ -  + ]:          11040 :                 if (!cteroot)   /* shouldn't happen */
  900 tgl@sss.pgh.pa.us        6148         [ #  # ]:UBC           0 :                     elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
                               6149                 :                :             }
                               6150                 :                : 
                               6151                 :                :             /*
                               6152                 :                :              * Note: cte_plan_ids can be shorter than cteList, if we are still
                               6153                 :                :              * working on planning the CTEs (ie, this is a side-reference from
                               6154                 :                :              * another CTE).  So we mustn't use forboth here.
                               6155                 :                :              */
  900 tgl@sss.pgh.pa.us        6156                 :CBC        7971 :             ndx = 0;
                               6157   [ +  -  +  -  :          11017 :             foreach(lc, cteroot->parse->cteList)
                                              +  - ]
                               6158                 :                :             {
                               6159                 :          11017 :                 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
                               6160                 :                : 
                               6161         [ +  + ]:          11017 :                 if (strcmp(cte->ctename, rte->ctename) == 0)
                               6162                 :           7971 :                     break;
                               6163                 :           3046 :                 ndx++;
                               6164                 :                :             }
                               6165         [ -  + ]:           7971 :             if (lc == NULL)     /* shouldn't happen */
  900 tgl@sss.pgh.pa.us        6166         [ #  # ]:UBC           0 :                 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
  900 tgl@sss.pgh.pa.us        6167         [ -  + ]:CBC        7971 :             if (ndx >= list_length(cteroot->cte_plan_ids))
  900 tgl@sss.pgh.pa.us        6168         [ #  # ]:UBC           0 :                 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
  900 tgl@sss.pgh.pa.us        6169                 :CBC        7971 :             plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
                               6170         [ -  + ]:           7971 :             if (plan_id <= 0)
  900 tgl@sss.pgh.pa.us        6171         [ #  # ]:UBC           0 :                 elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
  900 tgl@sss.pgh.pa.us        6172                 :CBC        7971 :             subroot = list_nth(root->glob->subroots, plan_id - 1);
                               6173                 :                :         }
                               6174                 :                : 
                               6175                 :                :         /* If the subquery hasn't been planned yet, we have to punt */
                               6176         [ -  + ]:          15738 :         if (subroot == NULL)
  900 tgl@sss.pgh.pa.us        6177                 :UBC           0 :             return;
  900 tgl@sss.pgh.pa.us        6178         [ -  + ]:CBC       15738 :         Assert(IsA(subroot, PlannerInfo));
                               6179                 :                : 
                               6180                 :                :         /*
                               6181                 :                :          * We must use the subquery parsetree as mangled by the planner, not
                               6182                 :                :          * the raw version from the RTE, because we need a Var that will refer
                               6183                 :                :          * to the subroot's live RelOptInfos.  For instance, if any subquery
                               6184                 :                :          * pullup happened during planning, Vars in the targetlist might have
                               6185                 :                :          * gotten replaced, and we need to see the replacement expressions.
                               6186                 :                :          */
                               6187                 :          15738 :         subquery = subroot->parse;
                               6188         [ -  + ]:          15738 :         Assert(IsA(subquery, Query));
                               6189                 :                : 
                               6190                 :                :         /*
                               6191                 :                :          * Punt if subquery uses set operations or grouping sets, as these
                               6192                 :                :          * will mash underlying columns' stats beyond recognition.  (Set ops
                               6193                 :                :          * are particularly nasty; if we forged ahead, we would return stats
                               6194                 :                :          * relevant to only the leftmost subselect...)  DISTINCT is also
                               6195                 :                :          * problematic, but we check that later because there is a possibility
                               6196                 :                :          * of learning something even with it.
                               6197                 :                :          */
 5192                          6198         [ +  + ]:          15738 :         if (subquery->setOperations ||
 1799                          6199         [ +  + ]:          14328 :             subquery->groupingSets)
 5248 rhaas@postgresql.org     6200                 :           1487 :             return;
                               6201                 :                : 
                               6202                 :                :         /* Get the subquery output expression referenced by the upper Var */
  900 tgl@sss.pgh.pa.us        6203         [ +  + ]:          14251 :         if (subquery->returningList)
                               6204                 :            179 :             subtlist = subquery->returningList;
                               6205                 :                :         else
                               6206                 :          14072 :             subtlist = subquery->targetList;
                               6207                 :          14251 :         ste = get_tle_by_resno(subtlist, var->varattno);
 5357                          6208   [ +  -  -  + ]:          14251 :         if (ste == NULL || ste->resjunk)
 5357 tgl@sss.pgh.pa.us        6209         [ #  # ]:UBC           0 :             elog(ERROR, "subquery %s does not have attribute %d",
                               6210                 :                :                  rte->eref->aliasname, var->varattno);
 5357 tgl@sss.pgh.pa.us        6211                 :CBC       14251 :         var = (Var *) ste->expr;
                               6212                 :                : 
                               6213                 :                :         /*
                               6214                 :                :          * If subquery uses DISTINCT, we can't make use of any stats for the
                               6215                 :                :          * variable ... but, if it's the only DISTINCT column, we are entitled
                               6216                 :                :          * to consider it unique.  We do the test this way so that it works
                               6217                 :                :          * for cases involving DISTINCT ON.
                               6218                 :                :          */
 5192                          6219         [ +  + ]:          14251 :         if (subquery->distinctClause)
                               6220                 :                :         {
                               6221   [ +  +  +  + ]:           1338 :             if (list_length(subquery->distinctClause) == 1 &&
                               6222                 :            464 :                 targetIsInSortList(ste, InvalidOid, subquery->distinctClause))
                               6223                 :            310 :                 vardata->isunique = true;
                               6224                 :                :             /* cannot go further */
                               6225                 :            874 :             return;
                               6226                 :                :         }
                               6227                 :                : 
                               6228                 :                :         /* The same idea as with DISTINCT clause works for a GROUP-BY too */
  440 akorotkov@postgresql     6229         [ +  + ]:          13377 :         if (subquery->groupClause)
                               6230                 :                :         {
                               6231   [ +  +  +  + ]:            947 :             if (list_length(subquery->groupClause) == 1 &&
                               6232                 :            402 :                 targetIsInSortList(ste, InvalidOid, subquery->groupClause))
                               6233                 :            322 :                 vardata->isunique = true;
                               6234                 :                :             /* cannot go further */
                               6235                 :            545 :             return;
                               6236                 :                :         }
                               6237                 :                : 
                               6238                 :                :         /*
                               6239                 :                :          * If the sub-query originated from a view with the security_barrier
                               6240                 :                :          * attribute, we must not look at the variable's statistics, though it
                               6241                 :                :          * seems all right to notice the existence of a DISTINCT clause. So
                               6242                 :                :          * stop here.
                               6243                 :                :          *
                               6244                 :                :          * This is probably a harsher restriction than necessary; it's
                               6245                 :                :          * certainly OK for the selectivity estimator (which is a C function,
                               6246                 :                :          * and therefore omnipotent anyway) to look at the statistics.  But
                               6247                 :                :          * many selectivity estimators will happily *invoke the operator
                               6248                 :                :          * function* to try to work out a good estimate - and that's not OK.
                               6249                 :                :          * So for now, don't dig down for stats.
                               6250                 :                :          */
 5192 tgl@sss.pgh.pa.us        6251         [ +  + ]:          12832 :         if (rte->security_barrier)
                               6252                 :            480 :             return;
                               6253                 :                : 
                               6254                 :                :         /* Can only handle a simple Var of subquery's query level */
 5357                          6255   [ +  -  +  + ]:          12352 :         if (var && IsA(var, Var) &&
                               6256         [ +  - ]:           8094 :             var->varlevelsup == 0)
                               6257                 :                :         {
                               6258                 :                :             /*
                               6259                 :                :              * OK, recurse into the subquery.  Note that the original setting
                               6260                 :                :              * of vardata->isunique (which will surely be false) is left
                               6261                 :                :              * unchanged in this situation.  That's what we want, since even
                               6262                 :                :              * if the underlying column is unique, the subquery may have
                               6263                 :                :              * joined to other tables in a way that creates duplicates.
                               6264                 :                :              */
  900                          6265                 :           8094 :             examine_simple_variable(subroot, var, vardata);
                               6266                 :                :         }
                               6267                 :                :     }
                               6268                 :                :     else
                               6269                 :                :     {
                               6270                 :                :         /*
                               6271                 :                :          * Otherwise, the Var comes from a FUNCTION or VALUES RTE.  (We won't
                               6272                 :                :          * see RTE_JOIN here because join alias Vars have already been
                               6273                 :                :          * flattened.)  There's not much we can do with function outputs, but
                               6274                 :                :          * maybe someday try to be smarter about VALUES.
                               6275                 :                :          */
                               6276                 :                :     }
                               6277                 :                : }
                               6278                 :                : 
                               6279                 :                : /*
                               6280                 :                :  * all_rows_selectable
                               6281                 :                :  *      Test whether the user has permission to select all rows from a given
                               6282                 :                :  *      relation.
                               6283                 :                :  *
                               6284                 :                :  * Inputs:
                               6285                 :                :  *  root: the planner info
                               6286                 :                :  *  varno: the index of the relation (assumed to be an RTE_RELATION)
                               6287                 :                :  *  varattnos: the attributes for which permission is required, or NULL if
                               6288                 :                :  *      whole-table access is required
                               6289                 :                :  *
                               6290                 :                :  * Returns true if the user has the required select permissions, and there are
                               6291                 :                :  * no securityQuals from security barrier views or RLS policies.
                               6292                 :                :  *
                               6293                 :                :  * Note that if the relation is an inheritance child relation, securityQuals
                               6294                 :                :  * and access permissions are checked against the inheritance root parent (the
                               6295                 :                :  * relation actually mentioned in the query) --- see the comments in
                               6296                 :                :  * expand_single_inheritance_child() for an explanation of why it has to be
                               6297                 :                :  * done this way.
                               6298                 :                :  *
                               6299                 :                :  * If varattnos is non-NULL, its attribute numbers should be offset by
                               6300                 :                :  * FirstLowInvalidHeapAttributeNumber so that system attributes can be
                               6301                 :                :  * checked.  If varattnos is NULL, only table-level SELECT privileges are
                               6302                 :                :  * checked, not any column-level privileges.
                               6303                 :                :  *
                               6304                 :                :  * Note: if the relation is accessed via a view, this function actually tests
                               6305                 :                :  * whether the view owner has permission to select from the relation.  To
                               6306                 :                :  * ensure that the current user has permission, it is also necessary to check
                               6307                 :                :  * that the current user has permission to select from the view, which we do
                               6308                 :                :  * at planner-startup --- see subquery_planner().
                               6309                 :                :  *
                               6310                 :                :  * This is exported so that other estimation functions can use it.
                               6311                 :                :  */
                               6312                 :                : bool
  267 dean.a.rasheed@gmail     6313                 :        1222786 : all_rows_selectable(PlannerInfo *root, Index varno, Bitmapset *varattnos)
                               6314                 :                : {
                               6315                 :        1222786 :     RelOptInfo *rel = find_base_rel_noerr(root, varno);
                               6316         [ +  - ]:        1222786 :     RangeTblEntry *rte = planner_rt_fetch(varno, root);
                               6317                 :                :     Oid         userid;
                               6318                 :                :     int         varattno;
                               6319                 :                : 
                               6320         [ -  + ]:        1222786 :     Assert(rte->rtekind == RTE_RELATION);
                               6321                 :                : 
                               6322                 :                :     /*
                               6323                 :                :      * Determine the user ID to use for privilege checks (either the current
                               6324                 :                :      * user or the view owner, if we're accessing the table via a view).
                               6325                 :                :      *
                               6326                 :                :      * Normally the relation will have an associated RelOptInfo from which we
                               6327                 :                :      * can find the userid, but it might not if it's a RETURNING Var for an
                               6328                 :                :      * INSERT target relation.  In that case use the RTEPermissionInfo
                               6329                 :                :      * associated with the RTE.
                               6330                 :                :      *
                               6331                 :                :      * If we navigate up to a parent relation, we keep using the same userid,
                               6332                 :                :      * since it's the same in all relations of a given inheritance tree.
                               6333                 :                :      */
                               6334         [ +  + ]:        1222786 :     if (rel)
                               6335                 :        1222753 :         userid = rel->userid;
                               6336                 :                :     else
                               6337                 :                :     {
                               6338                 :                :         RTEPermissionInfo *perminfo;
                               6339                 :                : 
                               6340                 :             33 :         perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
                               6341                 :             33 :         userid = perminfo->checkAsUser;
                               6342                 :                :     }
                               6343         [ +  + ]:        1222786 :     if (!OidIsValid(userid))
                               6344                 :        1091226 :         userid = GetUserId();
                               6345                 :                : 
                               6346                 :                :     /*
                               6347                 :                :      * Permissions and securityQuals must be checked on the table actually
                               6348                 :                :      * mentioned in the query, so if this is an inheritance child, navigate up
                               6349                 :                :      * to the inheritance root parent.  If the user can read the whole table
                               6350                 :                :      * or the required columns there, then they can read from the child table
                               6351                 :                :      * too.  For per-column checks, we must find out which of the root
                               6352                 :                :      * parent's attributes the child relation's attributes correspond to.
                               6353                 :                :      */
                               6354         [ +  + ]:        1222786 :     if (root->append_rel_array != NULL)
                               6355                 :                :     {
                               6356                 :                :         AppendRelInfo *appinfo;
                               6357                 :                : 
                               6358                 :         177850 :         appinfo = root->append_rel_array[varno];
                               6359                 :                : 
                               6360                 :                :         /*
                               6361                 :                :          * Partitions are mapped to their immediate parent, not the root
                               6362                 :                :          * parent, so must be ready to walk up multiple AppendRelInfos.  But
                               6363                 :                :          * stop if we hit a parent that is not RTE_RELATION --- that's a
                               6364                 :                :          * flattened UNION ALL subquery, not an inheritance parent.
                               6365                 :                :          */
                               6366         [ +  + ]:         334192 :         while (appinfo &&
                               6367         [ +  - ]:         156727 :                planner_rt_fetch(appinfo->parent_relid,
                               6368         [ +  + ]:         156727 :                                 root)->rtekind == RTE_RELATION)
                               6369                 :                :         {
                               6370                 :         156342 :             Bitmapset  *parent_varattnos = NULL;
                               6371                 :                : 
                               6372                 :                :             /*
                               6373                 :                :              * For each child attribute, find the corresponding parent
                               6374                 :                :              * attribute.  In rare cases, the attribute may be local to the
                               6375                 :                :              * child table, in which case, we've got to live with having no
                               6376                 :                :              * access to this column.
                               6377                 :                :              */
                               6378                 :         156342 :             varattno = -1;
                               6379         [ +  + ]:         310417 :             while ((varattno = bms_next_member(varattnos, varattno)) >= 0)
                               6380                 :                :             {
                               6381                 :                :                 AttrNumber  attno;
                               6382                 :                :                 AttrNumber  parent_attno;
                               6383                 :                : 
                               6384                 :         154075 :                 attno = varattno + FirstLowInvalidHeapAttributeNumber;
                               6385                 :                : 
                               6386         [ +  + ]:         154075 :                 if (attno == InvalidAttrNumber)
                               6387                 :                :                 {
                               6388                 :                :                     /*
                               6389                 :                :                      * Whole-row reference, so must map each column of the
                               6390                 :                :                      * child to the parent table.
                               6391                 :                :                      */
                               6392         [ +  + ]:             30 :                     for (attno = 1; attno <= appinfo->num_child_cols; attno++)
                               6393                 :                :                     {
                               6394                 :             20 :                         parent_attno = appinfo->parent_colnos[attno - 1];
                               6395         [ -  + ]:             20 :                         if (parent_attno == 0)
  267 dean.a.rasheed@gmail     6396                 :UBC           0 :                             return false;   /* attr is local to child */
                               6397                 :                :                         parent_varattnos =
  267 dean.a.rasheed@gmail     6398                 :CBC          20 :                             bms_add_member(parent_varattnos,
                               6399                 :                :                                            parent_attno - FirstLowInvalidHeapAttributeNumber);
                               6400                 :                :                     }
                               6401                 :                :                 }
                               6402                 :                :                 else
                               6403                 :                :                 {
                               6404         [ -  + ]:         154065 :                     if (attno < 0)
                               6405                 :                :                     {
                               6406                 :                :                         /* System attnos are the same in all tables */
  267 dean.a.rasheed@gmail     6407                 :UBC           0 :                         parent_attno = attno;
                               6408                 :                :                     }
                               6409                 :                :                     else
                               6410                 :                :                     {
  267 dean.a.rasheed@gmail     6411         [ -  + ]:CBC      154065 :                         if (attno > appinfo->num_child_cols)
  267 dean.a.rasheed@gmail     6412                 :UBC           0 :                             return false;   /* safety check */
  267 dean.a.rasheed@gmail     6413                 :CBC      154065 :                         parent_attno = appinfo->parent_colnos[attno - 1];
                               6414         [ -  + ]:         154065 :                         if (parent_attno == 0)
  267 dean.a.rasheed@gmail     6415                 :UBC           0 :                             return false;   /* attr is local to child */
                               6416                 :                :                     }
                               6417                 :                :                     parent_varattnos =
  267 dean.a.rasheed@gmail     6418                 :CBC      154065 :                         bms_add_member(parent_varattnos,
                               6419                 :                :                                        parent_attno - FirstLowInvalidHeapAttributeNumber);
                               6420                 :                :                 }
                               6421                 :                :             }
                               6422                 :                : 
                               6423                 :                :             /* If the parent is itself a child, continue up */
                               6424                 :         156342 :             varno = appinfo->parent_relid;
                               6425                 :         156342 :             varattnos = parent_varattnos;
                               6426                 :         156342 :             appinfo = root->append_rel_array[varno];
                               6427                 :                :         }
                               6428                 :                : 
                               6429                 :                :         /* Perform the access check on this parent rel */
                               6430         [ +  - ]:         177850 :         rte = planner_rt_fetch(varno, root);
                               6431         [ -  + ]:         177850 :         Assert(rte->rtekind == RTE_RELATION);
                               6432                 :                :     }
                               6433                 :                : 
                               6434                 :                :     /*
                               6435                 :                :      * For all rows to be accessible, there must be no securityQuals from
                               6436                 :                :      * security barrier views or RLS policies.
                               6437                 :                :      */
                               6438         [ +  + ]:        1222786 :     if (rte->securityQuals != NIL)
                               6439                 :            690 :         return false;
                               6440                 :                : 
                               6441                 :                :     /*
                               6442                 :                :      * Test for table-level SELECT privilege.
                               6443                 :                :      *
                               6444                 :                :      * If varattnos is non-NULL, this is sufficient to give access to all
                               6445                 :                :      * requested attributes, even for a child table, since we have verified
                               6446                 :                :      * that all required child columns have matching parent columns.
                               6447                 :                :      *
                               6448                 :                :      * If varattnos is NULL (whole-table access requested), this doesn't
                               6449                 :                :      * necessarily guarantee that the user can read all columns of a child
                               6450                 :                :      * table, but we allow it anyway (see comments in examine_variable()) and
                               6451                 :                :      * don't bother checking any column privileges.
                               6452                 :                :      */
                               6453         [ +  + ]:        1222096 :     if (pg_class_aclcheck(rte->relid, userid, ACL_SELECT) == ACLCHECK_OK)
                               6454                 :        1221753 :         return true;
                               6455                 :                : 
                               6456         [ +  + ]:            343 :     if (varattnos == NULL)
                               6457                 :             10 :         return false;           /* whole-table access requested */
                               6458                 :                : 
                               6459                 :                :     /*
                               6460                 :                :      * Don't have table-level SELECT privilege, so check per-column
                               6461                 :                :      * privileges.
                               6462                 :                :      */
                               6463                 :            333 :     varattno = -1;
                               6464         [ +  + ]:            471 :     while ((varattno = bms_next_member(varattnos, varattno)) >= 0)
                               6465                 :                :     {
                               6466                 :            333 :         AttrNumber  attno = varattno + FirstLowInvalidHeapAttributeNumber;
                               6467                 :                : 
                               6468         [ +  + ]:            333 :         if (attno == InvalidAttrNumber)
                               6469                 :                :         {
                               6470                 :                :             /* Whole-row reference, so must have access to all columns */
                               6471         [ +  - ]:              5 :             if (pg_attribute_aclcheck_all(rte->relid, userid, ACL_SELECT,
                               6472                 :                :                                           ACLMASK_ALL) != ACLCHECK_OK)
                               6473                 :              5 :                 return false;
                               6474                 :                :         }
                               6475                 :                :         else
                               6476                 :                :         {
                               6477         [ +  + ]:            328 :             if (pg_attribute_aclcheck(rte->relid, attno, userid,
                               6478                 :                :                                       ACL_SELECT) != ACLCHECK_OK)
                               6479                 :            190 :                 return false;
                               6480                 :                :         }
                               6481                 :                :     }
                               6482                 :                : 
                               6483                 :                :     /* If we reach here, have all required column privileges */
                               6484                 :            138 :     return true;
                               6485                 :                : }
                               6486                 :                : 
                               6487                 :                : /*
                               6488                 :                :  * examine_indexcol_variable
                               6489                 :                :  *      Try to look up statistical data about an index column/expression.
                               6490                 :                :  *      Fill in a VariableStatData struct to describe the column.
                               6491                 :                :  *
                               6492                 :                :  * Inputs:
                               6493                 :                :  *  root: the planner info
                               6494                 :                :  *  index: the index whose column we're interested in
                               6495                 :                :  *  indexcol: 0-based index column number (subscripts index->indexkeys[])
                               6496                 :                :  *
                               6497                 :                :  * Outputs: *vardata is filled as follows:
                               6498                 :                :  *  var: the input expression (with any binary relabeling stripped, if
                               6499                 :                :  *      it is or contains a variable; but otherwise the type is preserved)
                               6500                 :                :  *  rel: RelOptInfo for table relation containing variable.
                               6501                 :                :  *  statsTuple: the pg_statistic entry for the variable, if one exists;
                               6502                 :                :  *      otherwise NULL.
                               6503                 :                :  *  freefunc: pointer to a function to release statsTuple with.
                               6504                 :                :  *
                               6505                 :                :  * Caller is responsible for doing ReleaseVariableStats() before exiting.
                               6506                 :                :  */
                               6507                 :                : static void
  396 pg@bowt.ie               6508                 :         654424 : examine_indexcol_variable(PlannerInfo *root, IndexOptInfo *index,
                               6509                 :                :                           int indexcol, VariableStatData *vardata)
                               6510                 :                : {
                               6511                 :                :     AttrNumber  colnum;
                               6512                 :                :     Oid         relid;
                               6513                 :                : 
                               6514         [ +  + ]:         654424 :     if (index->indexkeys[indexcol] != 0)
                               6515                 :                :     {
                               6516                 :                :         /* Simple variable --- look to stats for the underlying table */
                               6517         [ +  - ]:         652591 :         RangeTblEntry *rte = planner_rt_fetch(index->rel->relid, root);
                               6518                 :                : 
                               6519         [ -  + ]:         652591 :         Assert(rte->rtekind == RTE_RELATION);
                               6520                 :         652591 :         relid = rte->relid;
                               6521         [ -  + ]:         652591 :         Assert(relid != InvalidOid);
                               6522                 :         652591 :         colnum = index->indexkeys[indexcol];
                               6523                 :         652591 :         vardata->rel = index->rel;
                               6524                 :                : 
                               6525   [ -  +  -  - ]:         652591 :         if (get_relation_stats_hook &&
  396 pg@bowt.ie               6526                 :UBC           0 :             (*get_relation_stats_hook) (root, rte, colnum, vardata))
                               6527                 :                :         {
                               6528                 :                :             /*
                               6529                 :                :              * The hook took control of acquiring a stats tuple.  If it did
                               6530                 :                :              * supply a tuple, it'd better have supplied a freefunc.
                               6531                 :                :              */
                               6532         [ #  # ]:              0 :             if (HeapTupleIsValid(vardata->statsTuple) &&
                               6533         [ #  # ]:              0 :                 !vardata->freefunc)
                               6534         [ #  # ]:              0 :                 elog(ERROR, "no function provided to release variable stats with");
                               6535                 :                :         }
                               6536                 :                :         else
                               6537                 :                :         {
  396 pg@bowt.ie               6538                 :CBC      652591 :             vardata->statsTuple = SearchSysCache3(STATRELATTINH,
                               6539                 :                :                                                   ObjectIdGetDatum(relid),
                               6540                 :                :                                                   Int16GetDatum(colnum),
                               6541                 :         652591 :                                                   BoolGetDatum(rte->inh));
                               6542                 :         652591 :             vardata->freefunc = ReleaseSysCache;
                               6543                 :                :         }
                               6544                 :                :     }
                               6545                 :                :     else
                               6546                 :                :     {
                               6547                 :                :         /* Expression --- maybe there are stats for the index itself */
                               6548                 :           1833 :         relid = index->indexoid;
                               6549                 :           1833 :         colnum = indexcol + 1;
                               6550                 :                : 
                               6551   [ -  +  -  - ]:           1833 :         if (get_index_stats_hook &&
  396 pg@bowt.ie               6552                 :UBC           0 :             (*get_index_stats_hook) (root, relid, colnum, vardata))
                               6553                 :                :         {
                               6554                 :                :             /*
                               6555                 :                :              * The hook took control of acquiring a stats tuple.  If it did
                               6556                 :                :              * supply a tuple, it'd better have supplied a freefunc.
                               6557                 :                :              */
                               6558         [ #  # ]:              0 :             if (HeapTupleIsValid(vardata->statsTuple) &&
                               6559         [ #  # ]:              0 :                 !vardata->freefunc)
                               6560         [ #  # ]:              0 :                 elog(ERROR, "no function provided to release variable stats with");
                               6561                 :                :         }
                               6562                 :                :         else
                               6563                 :                :         {
  396 pg@bowt.ie               6564                 :CBC        1833 :             vardata->statsTuple = SearchSysCache3(STATRELATTINH,
                               6565                 :                :                                                   ObjectIdGetDatum(relid),
                               6566                 :                :                                                   Int16GetDatum(colnum),
                               6567                 :                :                                                   BoolGetDatum(false));
                               6568                 :           1833 :             vardata->freefunc = ReleaseSysCache;
                               6569                 :                :         }
                               6570                 :                :     }
                               6571                 :         654424 : }
                               6572                 :                : 
                               6573                 :                : /*
                               6574                 :                :  * Check whether it is permitted to call func_oid passing some of the
                               6575                 :                :  * pg_statistic data in vardata.  We allow this if either of the following
                               6576                 :                :  * conditions is met: (1) the user has SELECT privileges on the table or
                               6577                 :                :  * column underlying the pg_statistic data and there are no securityQuals from
                               6578                 :                :  * security barrier views or RLS policies, or (2) the function is marked
                               6579                 :                :  * leakproof.
                               6580                 :                :  */
                               6581                 :                : bool
 3287 peter_e@gmx.net          6582                 :         799564 : statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
                               6583                 :                : {
                               6584         [ +  + ]:         799564 :     if (vardata->acl_ok)
  267 dean.a.rasheed@gmail     6585                 :         798053 :         return true;            /* have SELECT privs and no securityQuals */
                               6586                 :                : 
 3287 peter_e@gmx.net          6587         [ -  + ]:           1511 :     if (!OidIsValid(func_oid))
 3287 peter_e@gmx.net          6588                 :UBC           0 :         return false;
                               6589                 :                : 
 3287 peter_e@gmx.net          6590         [ +  + ]:CBC        1511 :     if (get_func_leakproof(func_oid))
                               6591                 :            748 :         return true;
                               6592                 :                : 
                               6593         [ -  + ]:            763 :     ereport(DEBUG2,
                               6594                 :                :             (errmsg_internal("not using statistics because function \"%s\" is not leakproof",
                               6595                 :                :                              get_func_name(func_oid))));
                               6596                 :            763 :     return false;
                               6597                 :                : }
                               6598                 :                : 
                               6599                 :                : /*
                               6600                 :                :  * get_variable_numdistinct
                               6601                 :                :  *    Estimate the number of distinct values of a variable.
                               6602                 :                :  *
                               6603                 :                :  * vardata: results of examine_variable
                               6604                 :                :  * *isdefault: set to true if the result is a default rather than based on
                               6605                 :                :  * anything meaningful.
                               6606                 :                :  *
                               6607                 :                :  * NB: be careful to produce a positive integral result, since callers may
                               6608                 :                :  * compare the result to exact integer counts, or might divide by it.
                               6609                 :                :  */
                               6610                 :                : double
 5357 tgl@sss.pgh.pa.us        6611                 :        1299400 : get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
                               6612                 :                : {
                               6613                 :                :     double      stadistinct;
 3558                          6614                 :        1299400 :     double      stanullfrac = 0.0;
                               6615                 :                :     double      ntuples;
                               6616                 :                : 
 5357                          6617                 :        1299400 :     *isdefault = false;
                               6618                 :                : 
                               6619                 :                :     /*
                               6620                 :                :      * Determine the stadistinct value to use.  There are cases where we can
                               6621                 :                :      * get an estimate even without a pg_statistic entry, or can get a better
                               6622                 :                :      * value than is in pg_statistic.  Grab stanullfrac too if we can find it
                               6623                 :                :      * (otherwise, assume no nulls, for lack of any better idea).
                               6624                 :                :      */
 8113                          6625         [ +  + ]:        1299400 :     if (HeapTupleIsValid(vardata->statsTuple))
                               6626                 :                :     {
                               6627                 :                :         /* Use the pg_statistic entry */
                               6628                 :                :         Form_pg_statistic stats;
                               6629                 :                : 
                               6630                 :         860477 :         stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
                               6631                 :         860477 :         stadistinct = stats->stadistinct;
 3558                          6632                 :         860477 :         stanullfrac = stats->stanullfrac;
                               6633                 :                :     }
 7704                          6634         [ +  + ]:         438923 :     else if (vardata->vartype == BOOLOID)
                               6635                 :                :     {
                               6636                 :                :         /*
                               6637                 :                :          * Special-case boolean columns: presumably, two distinct values.
                               6638                 :                :          *
                               6639                 :                :          * Are there any other datatypes we should wire in special estimates
                               6640                 :                :          * for?
                               6641                 :                :          */
 8113                          6642                 :            665 :         stadistinct = 2.0;
                               6643                 :                :     }
 3184                          6644   [ +  +  +  + ]:         438258 :     else if (vardata->rel && vardata->rel->rtekind == RTE_VALUES)
                               6645                 :                :     {
                               6646                 :                :         /*
                               6647                 :                :          * If the Var represents a column of a VALUES RTE, assume it's unique.
                               6648                 :                :          * This could of course be very wrong, but it should tend to be true
                               6649                 :                :          * in well-written queries.  We could consider examining the VALUES'
                               6650                 :                :          * contents to get some real statistics; but that only works if the
                               6651                 :                :          * entries are all constants, and it would be pretty expensive anyway.
                               6652                 :                :          */
                               6653                 :           2999 :         stadistinct = -1.0;     /* unique (and all non null) */
                               6654                 :                :     }
                               6655                 :                :     else
                               6656                 :                :     {
                               6657                 :                :         /*
                               6658                 :                :          * We don't keep statistics for system columns, but in some cases we
                               6659                 :                :          * can infer distinctness anyway.
                               6660                 :                :          */
 8113                          6661   [ +  +  +  + ]:         435259 :         if (vardata->var && IsA(vardata->var, Var))
                               6662                 :                :         {
                               6663      [ +  +  + ]:         404861 :             switch (((Var *) vardata->var)->varattno)
                               6664                 :                :             {
                               6665                 :            865 :                 case SelfItemPointerAttributeNumber:
 3558                          6666                 :            865 :                     stadistinct = -1.0; /* unique (and all non null) */
 8113                          6667                 :            865 :                     break;
                               6668                 :          18378 :                 case TableOidAttributeNumber:
 7919 bruce@momjian.us         6669                 :          18378 :                     stadistinct = 1.0;  /* only 1 value */
 8113 tgl@sss.pgh.pa.us        6670                 :          18378 :                     break;
                               6671                 :         385618 :                 default:
 7919 bruce@momjian.us         6672                 :         385618 :                     stadistinct = 0.0;  /* means "unknown" */
 8113 tgl@sss.pgh.pa.us        6673                 :         385618 :                     break;
                               6674                 :                :             }
                               6675                 :                :         }
                               6676                 :                :         else
 7919 bruce@momjian.us         6677                 :          30398 :             stadistinct = 0.0;  /* means "unknown" */
                               6678                 :                : 
                               6679                 :                :         /*
                               6680                 :                :          * XXX consider using estimate_num_groups on expressions?
                               6681                 :                :          */
                               6682                 :                :     }
                               6683                 :                : 
                               6684                 :                :     /*
                               6685                 :                :      * If there is a unique index, DISTINCT or GROUP-BY clause for the
                               6686                 :                :      * variable, assume it is unique no matter what pg_statistic says; the
                               6687                 :                :      * statistics could be out of date, or we might have found a partial
                               6688                 :                :      * unique index that proves the var is unique for this query.  However,
                               6689                 :                :      * we'd better still believe the null-fraction statistic.
                               6690                 :                :      */
 6288 tgl@sss.pgh.pa.us        6691         [ +  + ]:        1299400 :     if (vardata->isunique)
 3558                          6692                 :         312149 :         stadistinct = -1.0 * (1.0 - stanullfrac);
                               6693                 :                : 
                               6694                 :                :     /*
                               6695                 :                :      * If we had an absolute estimate, use that.
                               6696                 :                :      */
 8113                          6697         [ +  + ]:        1299400 :     if (stadistinct > 0.0)
 3932                          6698                 :         304817 :         return clamp_row_est(stadistinct);
                               6699                 :                : 
                               6700                 :                :     /*
                               6701                 :                :      * Otherwise we need to get the relation size; punt if not available.
                               6702                 :                :      */
 8113                          6703         [ +  + ]:         994583 :     if (vardata->rel == NULL)
                               6704                 :                :     {
 5357                          6705                 :            591 :         *isdefault = true;
 8113                          6706                 :            591 :         return DEFAULT_NUM_DISTINCT;
                               6707                 :                :     }
                               6708                 :         993992 :     ntuples = vardata->rel->tuples;
                               6709         [ +  + ]:         993992 :     if (ntuples <= 0.0)
                               6710                 :                :     {
 5357                          6711                 :         110950 :         *isdefault = true;
 8113                          6712                 :         110950 :         return DEFAULT_NUM_DISTINCT;
                               6713                 :                :     }
                               6714                 :                : 
                               6715                 :                :     /*
                               6716                 :                :      * If we had a relative estimate, use that.
                               6717                 :                :      */
                               6718         [ +  + ]:         883042 :     if (stadistinct < 0.0)
 3932                          6719                 :         619692 :         return clamp_row_est(-stadistinct * ntuples);
                               6720                 :                : 
                               6721                 :                :     /*
                               6722                 :                :      * With no data, estimate ndistinct = ntuples if the table is small, else
                               6723                 :                :      * use default.  We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
                               6724                 :                :      * that the behavior isn't discontinuous.
                               6725                 :                :      */
 8113                          6726         [ +  + ]:         263350 :     if (ntuples < DEFAULT_NUM_DISTINCT)
 3932                          6727                 :         124223 :         return clamp_row_est(ntuples);
                               6728                 :                : 
 5357                          6729                 :         139127 :     *isdefault = true;
 8113                          6730                 :         139127 :     return DEFAULT_NUM_DISTINCT;
                               6731                 :                : }
                               6732                 :                : 
                               6733                 :                : /*
                               6734                 :                :  * get_variable_range
                               6735                 :                :  *      Estimate the minimum and maximum value of the specified variable.
                               6736                 :                :  *      If successful, store values in *min and *max, and return true.
                               6737                 :                :  *      If no data available, return false.
                               6738                 :                :  *
                               6739                 :                :  * sortop is the "<" comparison operator to use.  This should generally
                               6740                 :                :  * be "<" not ">", as only the former is likely to be found in pg_statistic.
                               6741                 :                :  * The collation must be specified too.
                               6742                 :                :  */
                               6743                 :                : static bool
 2160                          6744                 :         174882 : get_variable_range(PlannerInfo *root, VariableStatData *vardata,
                               6745                 :                :                    Oid sortop, Oid collation,
                               6746                 :                :                    Datum *min, Datum *max)
                               6747                 :                : {
 6723                          6748                 :         174882 :     Datum       tmin = 0;
 8113                          6749                 :         174882 :     Datum       tmax = 0;
 6723                          6750                 :         174882 :     bool        have_data = false;
                               6751                 :                :     int16       typLen;
                               6752                 :                :     bool        typByVal;
                               6753                 :                :     Oid         opfuncoid;
                               6754                 :                :     FmgrInfo    opproc;
                               6755                 :                :     AttStatsSlot sslot;
                               6756                 :                : 
                               6757                 :                :     /*
                               6758                 :                :      * XXX It's very tempting to try to use the actual column min and max, if
                               6759                 :                :      * we can get them relatively-cheaply with an index probe.  However, since
                               6760                 :                :      * this function is called many times during join planning, that could
                               6761                 :                :      * have unpleasant effects on planning speed.  Need more investigation
                               6762                 :                :      * before enabling this.
                               6763                 :                :      */
                               6764                 :                : #ifdef NOT_USED
                               6765                 :                :     if (get_actual_variable_range(root, vardata, sortop, collation, min, max))
                               6766                 :                :         return true;
                               6767                 :                : #endif
                               6768                 :                : 
 8113                          6769         [ +  + ]:         174882 :     if (!HeapTupleIsValid(vardata->statsTuple))
                               6770                 :                :     {
                               6771                 :                :         /* no stats available, so default result */
                               6772                 :          44343 :         return false;
                               6773                 :                :     }
                               6774                 :                : 
                               6775                 :                :     /*
                               6776                 :                :      * If we can't apply the sortop to the stats data, just fail.  In
                               6777                 :                :      * principle, if there's a histogram and no MCVs, we could return the
                               6778                 :                :      * histogram endpoints without ever applying the sortop ... but it's
                               6779                 :                :      * probably not worth trying, because whatever the caller wants to do with
                               6780                 :                :      * the endpoints would likely fail the security check too.
                               6781                 :                :      */
 3287 peter_e@gmx.net          6782         [ -  + ]:         130539 :     if (!statistic_proc_security_check(vardata,
                               6783                 :         130539 :                                        (opfuncoid = get_opcode(sortop))))
 3287 peter_e@gmx.net          6784                 :UBC           0 :         return false;
                               6785                 :                : 
 2160 tgl@sss.pgh.pa.us        6786                 :CBC      130539 :     opproc.fn_oid = InvalidOid; /* mark this as not looked up yet */
                               6787                 :                : 
 8113                          6788                 :         130539 :     get_typlenbyval(vardata->atttype, &typLen, &typByVal);
                               6789                 :                : 
                               6790                 :                :     /*
                               6791                 :                :      * If there is a histogram with the ordering we want, grab the first and
                               6792                 :                :      * last values.
                               6793                 :                :      */
 3279                          6794         [ +  + ]:         130539 :     if (get_attstatsslot(&sslot, vardata->statsTuple,
                               6795                 :                :                          STATISTIC_KIND_HISTOGRAM, sortop,
                               6796                 :                :                          ATTSTATSSLOT_VALUES))
                               6797                 :                :     {
 2160                          6798   [ +  -  +  - ]:          82940 :         if (sslot.stacoll == collation && sslot.nvalues > 0)
                               6799                 :                :         {
 3279                          6800                 :          82940 :             tmin = datumCopy(sslot.values[0], typByVal, typLen);
                               6801                 :          82940 :             tmax = datumCopy(sslot.values[sslot.nvalues - 1], typByVal, typLen);
 6723                          6802                 :          82940 :             have_data = true;
                               6803                 :                :         }
 3279                          6804                 :          82940 :         free_attstatsslot(&sslot);
                               6805                 :                :     }
                               6806                 :                : 
                               6807                 :                :     /*
                               6808                 :                :      * Otherwise, if there is a histogram with some other ordering, scan it
                               6809                 :                :      * and get the min and max values according to the ordering we want.  This
                               6810                 :                :      * of course may not find values that are really extremal according to our
                               6811                 :                :      * ordering, but it beats ignoring available data.
                               6812                 :                :      */
 2160                          6813   [ +  +  -  + ]:         178138 :     if (!have_data &&
                               6814                 :          47599 :         get_attstatsslot(&sslot, vardata->statsTuple,
                               6815                 :                :                          STATISTIC_KIND_HISTOGRAM, InvalidOid,
                               6816                 :                :                          ATTSTATSSLOT_VALUES))
                               6817                 :                :     {
 2160 tgl@sss.pgh.pa.us        6818                 :UBC           0 :         get_stats_slot_range(&sslot, opfuncoid, &opproc,
                               6819                 :                :                              collation, typLen, typByVal,
                               6820                 :                :                              &tmin, &tmax, &have_data);
 3279                          6821                 :              0 :         free_attstatsslot(&sslot);
                               6822                 :                :     }
                               6823                 :                : 
                               6824                 :                :     /*
                               6825                 :                :      * If we have most-common-values info, look for extreme MCVs.  This is
                               6826                 :                :      * needed even if we also have a histogram, since the histogram excludes
                               6827                 :                :      * the MCVs.  However, if we *only* have MCVs and no histogram, we should
                               6828                 :                :      * be pretty wary of deciding that that is a full representation of the
                               6829                 :                :      * data.  Proceed only if the MCVs represent the whole table (to within
                               6830                 :                :      * roundoff error).
                               6831                 :                :      */
 3279 tgl@sss.pgh.pa.us        6832         [ +  + ]:CBC      130539 :     if (get_attstatsslot(&sslot, vardata->statsTuple,
                               6833                 :                :                          STATISTIC_KIND_MCV, InvalidOid,
 1677                          6834         [ +  + ]:         130539 :                          have_data ? ATTSTATSSLOT_VALUES :
                               6835                 :                :                          (ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS)))
                               6836                 :                :     {
                               6837                 :          75093 :         bool        use_mcvs = have_data;
                               6838                 :                : 
                               6839         [ +  + ]:          75093 :         if (!have_data)
                               6840                 :                :         {
                               6841                 :          46660 :             double      sumcommon = 0.0;
                               6842                 :                :             double      nullfrac;
                               6843                 :                :             int         i;
                               6844                 :                : 
                               6845         [ +  + ]:         370145 :             for (i = 0; i < sslot.nnumbers; i++)
                               6846                 :         323485 :                 sumcommon += sslot.numbers[i];
                               6847                 :          46660 :             nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata->statsTuple))->stanullfrac;
                               6848         [ +  + ]:          46660 :             if (sumcommon + nullfrac > 0.99999)
                               6849                 :          43182 :                 use_mcvs = true;
                               6850                 :                :         }
                               6851                 :                : 
                               6852         [ +  + ]:          75093 :         if (use_mcvs)
                               6853                 :          71615 :             get_stats_slot_range(&sslot, opfuncoid, &opproc,
                               6854                 :                :                                  collation, typLen, typByVal,
                               6855                 :                :                                  &tmin, &tmax, &have_data);
 3279                          6856                 :          75093 :         free_attstatsslot(&sslot);
                               6857                 :                :     }
                               6858                 :                : 
 6723                          6859                 :         130539 :     *min = tmin;
 8113                          6860                 :         130539 :     *max = tmax;
 6723                          6861                 :         130539 :     return have_data;
                               6862                 :                : }
                               6863                 :                : 
                               6864                 :                : /*
                               6865                 :                :  * get_stats_slot_range: scan sslot for min/max values
                               6866                 :                :  *
                               6867                 :                :  * Subroutine for get_variable_range: update min/max/have_data according
                               6868                 :                :  * to what we find in the statistics array.
                               6869                 :                :  */
                               6870                 :                : static void
 2160                          6871                 :          71615 : get_stats_slot_range(AttStatsSlot *sslot, Oid opfuncoid, FmgrInfo *opproc,
                               6872                 :                :                      Oid collation, int16 typLen, bool typByVal,
                               6873                 :                :                      Datum *min, Datum *max, bool *p_have_data)
                               6874                 :                : {
                               6875                 :          71615 :     Datum       tmin = *min;
                               6876                 :          71615 :     Datum       tmax = *max;
                               6877                 :          71615 :     bool        have_data = *p_have_data;
                               6878                 :          71615 :     bool        found_tmin = false;
                               6879                 :          71615 :     bool        found_tmax = false;
                               6880                 :                : 
                               6881                 :                :     /* Look up the comparison function, if we didn't already do so */
                               6882         [ +  - ]:          71615 :     if (opproc->fn_oid != opfuncoid)
                               6883                 :          71615 :         fmgr_info(opfuncoid, opproc);
                               6884                 :                : 
                               6885                 :                :     /* Scan all the slot's values */
                               6886         [ +  + ]:        1829695 :     for (int i = 0; i < sslot->nvalues; i++)
                               6887                 :                :     {
                               6888         [ +  + ]:        1758080 :         if (!have_data)
                               6889                 :                :         {
                               6890                 :          43182 :             tmin = tmax = sslot->values[i];
                               6891                 :          43182 :             found_tmin = found_tmax = true;
                               6892                 :          43182 :             *p_have_data = have_data = true;
                               6893                 :          43182 :             continue;
                               6894                 :                :         }
                               6895         [ +  + ]:        1714898 :         if (DatumGetBool(FunctionCall2Coll(opproc,
                               6896                 :                :                                            collation,
                               6897                 :        1714898 :                                            sslot->values[i], tmin)))
                               6898                 :                :         {
                               6899                 :          44829 :             tmin = sslot->values[i];
                               6900                 :          44829 :             found_tmin = true;
                               6901                 :                :         }
                               6902         [ +  + ]:        1714898 :         if (DatumGetBool(FunctionCall2Coll(opproc,
                               6903                 :                :                                            collation,
                               6904                 :        1714898 :                                            tmax, sslot->values[i])))
                               6905                 :                :         {
                               6906                 :         195365 :             tmax = sslot->values[i];
                               6907                 :         195365 :             found_tmax = true;
                               6908                 :                :         }
                               6909                 :                :     }
                               6910                 :                : 
                               6911                 :                :     /*
                               6912                 :                :      * Copy the slot's values, if we found new extreme values.
                               6913                 :                :      */
                               6914         [ +  + ]:          71615 :     if (found_tmin)
                               6915                 :          61296 :         *min = datumCopy(tmin, typByVal, typLen);
                               6916         [ +  + ]:          71615 :     if (found_tmax)
                               6917                 :          47651 :         *max = datumCopy(tmax, typByVal, typLen);
                               6918                 :          71615 : }
                               6919                 :                : 
                               6920                 :                : 
                               6921                 :                : /*
                               6922                 :                :  * get_actual_variable_range
                               6923                 :                :  *      Attempt to identify the current *actual* minimum and/or maximum
                               6924                 :                :  *      of the specified variable, by looking for a suitable btree index
                               6925                 :                :  *      and fetching its low and/or high values.
                               6926                 :                :  *      If successful, store values in *min and *max, and return true.
                               6927                 :                :  *      (Either pointer can be NULL if that endpoint isn't needed.)
                               6928                 :                :  *      If unsuccessful, return false.
                               6929                 :                :  *
                               6930                 :                :  * sortop is the "<" comparison operator to use.
                               6931                 :                :  * collation is the required collation.
                               6932                 :                :  */
                               6933                 :                : static bool
 5965                          6934                 :         125141 : get_actual_variable_range(PlannerInfo *root, VariableStatData *vardata,
                               6935                 :                :                           Oid sortop, Oid collation,
                               6936                 :                :                           Datum *min, Datum *max)
                               6937                 :                : {
                               6938                 :         125141 :     bool        have_data = false;
                               6939                 :         125141 :     RelOptInfo *rel = vardata->rel;
                               6940                 :                :     RangeTblEntry *rte;
                               6941                 :                :     ListCell   *lc;
                               6942                 :                : 
                               6943                 :                :     /* No hope if no relation or it doesn't have indexes */
                               6944   [ +  -  +  + ]:         125141 :     if (rel == NULL || rel->indexlist == NIL)
                               6945                 :           8975 :         return false;
                               6946                 :                :     /* If it has indexes it must be a plain relation */
                               6947                 :         116166 :     rte = root->simple_rte_array[rel->relid];
                               6948         [ -  + ]:         116166 :     Assert(rte->rtekind == RTE_RELATION);
                               6949                 :                : 
                               6950                 :                :     /* ignore partitioned tables.  Any indexes here are not real indexes */
 1212 drowley@postgresql.o     6951         [ +  + ]:         116166 :     if (rte->relkind == RELKIND_PARTITIONED_TABLE)
                               6952                 :            560 :         return false;
                               6953                 :                : 
                               6954                 :                :     /* Search through the indexes to see if any match our problem */
 5965 tgl@sss.pgh.pa.us        6955   [ +  -  +  +  :         234355 :     foreach(lc, rel->indexlist)
                                              +  + ]
                               6956                 :                :     {
                               6957                 :         201612 :         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
                               6958                 :                :         ScanDirection indexscandir;
                               6959                 :                :         StrategyNumber strategy;
                               6960                 :                : 
                               6961                 :                :         /* Ignore non-ordering indexes */
  396 peter@eisentraut.org     6962         [ +  + ]:         201612 :         if (index->sortopfamily == NULL)
  396 peter@eisentraut.org     6963                 :GBC           3 :             continue;
                               6964                 :                : 
                               6965                 :                :         /*
                               6966                 :                :          * Ignore partial indexes --- we only want stats that cover the entire
                               6967                 :                :          * relation.
                               6968                 :                :          */
 5965 tgl@sss.pgh.pa.us        6969         [ +  + ]:CBC      201609 :         if (index->indpred != NIL)
                               6970                 :            240 :             continue;
                               6971                 :                : 
                               6972                 :                :         /*
                               6973                 :                :          * The index list might include hypothetical indexes inserted by a
                               6974                 :                :          * get_relation_info hook --- don't try to access them.
                               6975                 :                :          */
 5557                          6976         [ -  + ]:         201369 :         if (index->hypothetical)
 5965 tgl@sss.pgh.pa.us        6977                 :UBC           0 :             continue;
                               6978                 :                : 
                               6979                 :                :         /*
                               6980                 :                :          * get_actual_variable_endpoint uses the index-only-scan machinery, so
                               6981                 :                :          * ignore indexes that can't use it on their first column.
                               6982                 :                :          */
  189 peter@eisentraut.org     6983         [ -  + ]:CBC      201369 :         if (!index->canreturn[0])
  189 peter@eisentraut.org     6984                 :UBC           0 :             continue;
                               6985                 :                : 
                               6986                 :                :         /*
                               6987                 :                :          * The first index column must match the desired variable, sortop, and
                               6988                 :                :          * collation --- but we can use a descending-order index.
                               6989                 :                :          */
 2160 tgl@sss.pgh.pa.us        6990         [ +  + ]:CBC      201369 :         if (collation != index->indexcollations[0])
                               6991                 :          28984 :             continue;           /* test first 'cause it's cheapest */
 5965                          6992         [ +  + ]:         172385 :         if (!match_index_to_operand(vardata->var, 0, index))
                               6993                 :          89522 :             continue;
  396 peter@eisentraut.org     6994                 :          82863 :         strategy = get_op_opfamily_strategy(sortop, index->sortopfamily[0]);
                               6995      [ +  -  - ]:          82863 :         switch (IndexAmTranslateStrategy(strategy, index->relam, index->sortopfamily[0], true))
                               6996                 :                :         {
                               6997                 :          82863 :             case COMPARE_LT:
 5636 tgl@sss.pgh.pa.us        6998         [ -  + ]:          82863 :                 if (index->reverse_sort[0])
 5636 tgl@sss.pgh.pa.us        6999                 :UBC           0 :                     indexscandir = BackwardScanDirection;
                               7000                 :                :                 else
 5636 tgl@sss.pgh.pa.us        7001                 :CBC       82863 :                     indexscandir = ForwardScanDirection;
                               7002                 :          82863 :                 break;
  396 peter@eisentraut.org     7003                 :UBC           0 :             case COMPARE_GT:
 5636 tgl@sss.pgh.pa.us        7004         [ #  # ]:              0 :                 if (index->reverse_sort[0])
                               7005                 :              0 :                     indexscandir = ForwardScanDirection;
                               7006                 :                :                 else
                               7007                 :              0 :                     indexscandir = BackwardScanDirection;
                               7008                 :              0 :                 break;
                               7009                 :              0 :             default:
                               7010                 :                :                 /* index doesn't match the sortop */
                               7011                 :              0 :                 continue;
                               7012                 :                :         }
                               7013                 :                : 
                               7014                 :                :         /*
                               7015                 :                :          * Found a suitable index to extract data from.  Set up some data that
                               7016                 :                :          * can be used by both invocations of get_actual_variable_endpoint.
                               7017                 :                :          */
                               7018                 :                :         {
                               7019                 :                :             MemoryContext tmpcontext;
                               7020                 :                :             MemoryContext oldcontext;
                               7021                 :                :             Relation    heapRel;
                               7022                 :                :             Relation    indexRel;
                               7023                 :                :             TupleTableSlot *slot;
                               7024                 :                :             int16       typLen;
                               7025                 :                :             bool        typByVal;
                               7026                 :                :             ScanKeyData scankeys[1];
                               7027                 :                : 
                               7028                 :                :             /* Make sure any cruft gets recycled when we're done */
 2489 tgl@sss.pgh.pa.us        7029                 :CBC       82863 :             tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
                               7030                 :                :                                                "get_actual_variable_range workspace",
                               7031                 :                :                                                ALLOCSET_DEFAULT_SIZES);
 5965                          7032                 :          82863 :             oldcontext = MemoryContextSwitchTo(tmpcontext);
                               7033                 :                : 
                               7034                 :                :             /*
                               7035                 :                :              * Open the table and index so we can read from them.  We should
                               7036                 :                :              * already have some type of lock on each.
                               7037                 :                :              */
 2661 andres@anarazel.de       7038                 :          82863 :             heapRel = table_open(rte->relid, NoLock);
 2588 tgl@sss.pgh.pa.us        7039                 :          82863 :             indexRel = index_open(index->indexoid, NoLock);
                               7040                 :                : 
                               7041                 :                :             /* build some stuff needed for indexscan execution */
 2612 andres@anarazel.de       7042                 :          82863 :             slot = table_slot_create(heapRel, NULL);
 5965 tgl@sss.pgh.pa.us        7043                 :          82863 :             get_typlenbyval(vardata->atttype, &typLen, &typByVal);
                               7044                 :                : 
                               7045                 :                :             /* set up an IS NOT NULL scan key so that we ignore nulls */
                               7046                 :          82863 :             ScanKeyEntryInitialize(&scankeys[0],
                               7047                 :                :                                    SK_ISNULL | SK_SEARCHNOTNULL,
                               7048                 :                :                                    1,   /* index col to scan */
                               7049                 :                :                                    InvalidStrategy, /* no strategy */
                               7050                 :                :                                    InvalidOid,  /* no strategy subtype */
                               7051                 :                :                                    InvalidOid,  /* no collation */
                               7052                 :                :                                    InvalidOid,  /* no reg proc for this */
                               7053                 :                :                                    (Datum) 0);  /* constant */
                               7054                 :                : 
                               7055                 :                :             /* If min is requested ... */
                               7056         [ +  + ]:          82863 :             if (min)
                               7057                 :                :             {
 2489                          7058                 :          45771 :                 have_data = get_actual_variable_endpoint(heapRel,
                               7059                 :                :                                                          indexRel,
                               7060                 :                :                                                          indexscandir,
                               7061                 :                :                                                          scankeys,
                               7062                 :                :                                                          typLen,
                               7063                 :                :                                                          typByVal,
                               7064                 :                :                                                          slot,
                               7065                 :                :                                                          oldcontext,
                               7066                 :                :                                                          min);
                               7067                 :                :             }
                               7068                 :                :             else
                               7069                 :                :             {
                               7070                 :                :                 /* If min not requested, still want to fetch max */
                               7071                 :          37092 :                 have_data = true;
                               7072                 :                :             }
                               7073                 :                : 
                               7074                 :                :             /* If max is requested, and we didn't already fail ... */
 5965                          7075   [ +  +  +  - ]:          82863 :             if (max && have_data)
                               7076                 :                :             {
                               7077                 :                :                 /* scan in the opposite direction; all else is the same */
 2489                          7078                 :          38210 :                 have_data = get_actual_variable_endpoint(heapRel,
                               7079                 :                :                                                          indexRel,
                               7080                 :          38210 :                                                          -indexscandir,
                               7081                 :                :                                                          scankeys,
                               7082                 :                :                                                          typLen,
                               7083                 :                :                                                          typByVal,
                               7084                 :                :                                                          slot,
                               7085                 :                :                                                          oldcontext,
                               7086                 :                :                                                          max);
                               7087                 :                :             }
                               7088                 :                : 
                               7089                 :                :             /* Clean everything up */
 5965                          7090                 :          82863 :             ExecDropSingleTupleTableSlot(slot);
                               7091                 :                : 
 2588                          7092                 :          82863 :             index_close(indexRel, NoLock);
 2661 andres@anarazel.de       7093                 :          82863 :             table_close(heapRel, NoLock);
                               7094                 :                : 
 5965 tgl@sss.pgh.pa.us        7095                 :          82863 :             MemoryContextSwitchTo(oldcontext);
 2489                          7096                 :          82863 :             MemoryContextDelete(tmpcontext);
                               7097                 :                : 
                               7098                 :                :             /* And we're done */
 5965                          7099                 :          82863 :             break;
                               7100                 :                :         }
                               7101                 :                :     }
                               7102                 :                : 
                               7103                 :         115606 :     return have_data;
                               7104                 :                : }
                               7105                 :                : 
                               7106                 :                : /*
                               7107                 :                :  * Get one endpoint datum (min or max depending on indexscandir) from the
                               7108                 :                :  * specified index.  Return true if successful, false if not.
                               7109                 :                :  * On success, endpoint value is stored to *endpointDatum (and copied into
                               7110                 :                :  * outercontext).
                               7111                 :                :  *
                               7112                 :                :  * scankeys is a 1-element scankey array set up to reject nulls.
                               7113                 :                :  * typLen/typByVal describe the datatype of the index's first column.
                               7114                 :                :  * tableslot is a slot suitable to hold table tuples, in case we need
                               7115                 :                :  * to probe the heap.
                               7116                 :                :  * (We could compute these values locally, but that would mean computing them
                               7117                 :                :  * twice when get_actual_variable_range needs both the min and the max.)
                               7118                 :                :  *
                               7119                 :                :  * Failure occurs either when the index is empty, or we decide that it's
                               7120                 :                :  * taking too long to find a suitable tuple.
                               7121                 :                :  */
                               7122                 :                : static bool
 2489                          7123                 :          83981 : get_actual_variable_endpoint(Relation heapRel,
                               7124                 :                :                              Relation indexRel,
                               7125                 :                :                              ScanDirection indexscandir,
                               7126                 :                :                              ScanKey scankeys,
                               7127                 :                :                              int16 typLen,
                               7128                 :                :                              bool typByVal,
                               7129                 :                :                              TupleTableSlot *tableslot,
                               7130                 :                :                              MemoryContext outercontext,
                               7131                 :                :                              Datum *endpointDatum)
                               7132                 :                : {
                               7133                 :          83981 :     bool        have_data = false;
                               7134                 :                :     SnapshotData SnapshotNonVacuumable;
                               7135                 :                :     IndexScanDesc index_scan;
                               7136                 :          83981 :     Buffer      vmbuffer = InvalidBuffer;
 1260                          7137                 :          83981 :     BlockNumber last_heap_block = InvalidBlockNumber;
                               7138                 :          83981 :     int         n_visited_heap_pages = 0;
                               7139                 :                :     ItemPointer tid;
                               7140                 :                :     Datum       values[INDEX_MAX_KEYS];
                               7141                 :                :     bool        isnull[INDEX_MAX_KEYS];
                               7142                 :                :     MemoryContext oldcontext;
                               7143                 :                : 
                               7144                 :                :     /*
                               7145                 :                :      * We use the index-only-scan machinery for this.  With mostly-static
                               7146                 :                :      * tables that's a win because it avoids a heap visit.  It's also a win
                               7147                 :                :      * for dynamic data, but the reason is less obvious; read on for details.
                               7148                 :                :      *
                               7149                 :                :      * In principle, we should scan the index with our current active
                               7150                 :                :      * snapshot, which is the best approximation we've got to what the query
                               7151                 :                :      * will see when executed.  But that won't be exact if a new snap is taken
                               7152                 :                :      * before running the query, and it can be very expensive if a lot of
                               7153                 :                :      * recently-dead or uncommitted rows exist at the beginning or end of the
                               7154                 :                :      * index (because we'll laboriously fetch each one and reject it).
                               7155                 :                :      * Instead, we use SnapshotNonVacuumable.  That will accept recently-dead
                               7156                 :                :      * and uncommitted rows as well as normal visible rows.  On the other
                               7157                 :                :      * hand, it will reject known-dead rows, and thus not give a bogus answer
                               7158                 :                :      * when the extreme value has been deleted (unless the deletion was quite
                               7159                 :                :      * recent); that case motivates not using SnapshotAny here.
                               7160                 :                :      *
                               7161                 :                :      * A crucial point here is that SnapshotNonVacuumable, with
                               7162                 :                :      * GlobalVisTestFor(heapRel) as horizon, yields the inverse of the
                               7163                 :                :      * condition that the indexscan will use to decide that index entries are
                               7164                 :                :      * killable (see heap_hot_search_buffer()).  Therefore, if the snapshot
                               7165                 :                :      * rejects a tuple (or more precisely, all tuples of a HOT chain) and we
                               7166                 :                :      * have to continue scanning past it, we know that the indexscan will mark
                               7167                 :                :      * that index entry killed.  That means that the next
                               7168                 :                :      * get_actual_variable_endpoint() call will not have to re-consider that
                               7169                 :                :      * index entry.  In this way we avoid repetitive work when this function
                               7170                 :                :      * is used a lot during planning.
                               7171                 :                :      *
                               7172                 :                :      * But using SnapshotNonVacuumable creates a hazard of its own.  In a
                               7173                 :                :      * recently-created index, some index entries may point at "broken" HOT
                               7174                 :                :      * chains in which not all the tuple versions contain data matching the
                               7175                 :                :      * index entry.  The live tuple version(s) certainly do match the index,
                               7176                 :                :      * but SnapshotNonVacuumable can accept recently-dead tuple versions that
                               7177                 :                :      * don't match.  Hence, if we took data from the selected heap tuple, we
                               7178                 :                :      * might get a bogus answer that's not close to the index extremal value,
                               7179                 :                :      * or could even be NULL.  We avoid this hazard because we take the data
                               7180                 :                :      * from the index entry not the heap.
                               7181                 :                :      *
                               7182                 :                :      * Despite all this care, there are situations where we might find many
                               7183                 :                :      * non-visible tuples near the end of the index.  We don't want to expend
                               7184                 :                :      * a huge amount of time here, so we give up once we've read too many heap
                               7185                 :                :      * pages.  When we fail for that reason, the caller will end up using
                               7186                 :                :      * whatever extremal value is recorded in pg_statistic.
                               7187                 :                :      */
 2092 andres@anarazel.de       7188                 :          83981 :     InitNonVacuumableSnapshot(SnapshotNonVacuumable,
                               7189                 :                :                               GlobalVisTestFor(heapRel));
                               7190                 :                : 
 2489 tgl@sss.pgh.pa.us        7191                 :          83981 :     index_scan = index_beginscan(heapRel, indexRel,
                               7192                 :                :                                  &SnapshotNonVacuumable, NULL,
                               7193                 :                :                                  1, 0,
                               7194                 :                :                                  SO_NONE);
                               7195                 :                :     /* Set it up for index-only scan */
                               7196                 :          83981 :     index_scan->xs_want_itup = true;
                               7197                 :          83981 :     index_rescan(index_scan, scankeys, 1, NULL, 0);
                               7198                 :                : 
                               7199                 :                :     /* Fetch first/next tuple in specified direction */
                               7200         [ +  - ]:         102011 :     while ((tid = index_getnext_tid(index_scan, indexscandir)) != NULL)
                               7201                 :                :     {
 1260                          7202                 :         102011 :         BlockNumber block = ItemPointerGetBlockNumber(tid);
                               7203                 :                : 
 2489                          7204         [ +  + ]:         102011 :         if (!VM_ALL_VISIBLE(heapRel,
                               7205                 :                :                             block,
                               7206                 :                :                             &vmbuffer))
                               7207                 :                :         {
                               7208                 :                :             /* Rats, we have to visit the heap to check visibility */
                               7209         [ +  + ]:          69929 :             if (!index_fetch_heap(index_scan, tableslot))
                               7210                 :                :             {
                               7211                 :                :                 /*
                               7212                 :                :                  * No visible tuple for this index entry, so we need to
                               7213                 :                :                  * advance to the next entry.  Before doing so, count heap
                               7214                 :                :                  * page fetches and give up if we've done too many.
                               7215                 :                :                  *
                               7216                 :                :                  * We don't charge a page fetch if this is the same heap page
                               7217                 :                :                  * as the previous tuple.  This is on the conservative side,
                               7218                 :                :                  * since other recently-accessed pages are probably still in
                               7219                 :                :                  * buffers too; but it's good enough for this heuristic.
                               7220                 :                :                  */
                               7221                 :                : #define VISITED_PAGES_LIMIT 100
                               7222                 :                : 
 1260                          7223         [ +  + ]:          18030 :                 if (block != last_heap_block)
                               7224                 :                :                 {
                               7225                 :           1825 :                     last_heap_block = block;
                               7226                 :           1825 :                     n_visited_heap_pages++;
                               7227         [ -  + ]:           1825 :                     if (n_visited_heap_pages > VISITED_PAGES_LIMIT)
 1260 tgl@sss.pgh.pa.us        7228                 :UBC           0 :                         break;
                               7229                 :                :                 }
                               7230                 :                : 
 2489 tgl@sss.pgh.pa.us        7231                 :CBC       18030 :                 continue;       /* no visible tuple, try next index entry */
                               7232                 :                :             }
                               7233                 :                : 
                               7234                 :                :             /* We don't actually need the heap tuple for anything */
                               7235                 :          51899 :             ExecClearTuple(tableslot);
                               7236                 :                : 
                               7237                 :                :             /*
                               7238                 :                :              * We don't care whether there's more than one visible tuple in
                               7239                 :                :              * the HOT chain; if any are visible, that's good enough.
                               7240                 :                :              */
                               7241                 :                :         }
                               7242                 :                : 
                               7243                 :                :         /*
                               7244                 :                :          * We expect that the index will return data in IndexTuple not
                               7245                 :                :          * HeapTuple format.
                               7246                 :                :          */
                               7247         [ -  + ]:          83981 :         if (!index_scan->xs_itup)
 2489 tgl@sss.pgh.pa.us        7248         [ #  # ]:UBC           0 :             elog(ERROR, "no data returned for index-only scan");
                               7249                 :                : 
                               7250                 :                :         /*
                               7251                 :                :          * We do not yet support recheck here.
                               7252                 :                :          */
 2489 tgl@sss.pgh.pa.us        7253         [ -  + ]:CBC       83981 :         if (index_scan->xs_recheck)
  396 peter@eisentraut.org     7254                 :UBC           0 :             break;
                               7255                 :                : 
                               7256                 :                :         /* OK to deconstruct the index tuple */
 2489 tgl@sss.pgh.pa.us        7257                 :CBC       83981 :         index_deform_tuple(index_scan->xs_itup,
                               7258                 :                :                            index_scan->xs_itupdesc,
                               7259                 :                :                            values, isnull);
                               7260                 :                : 
                               7261                 :                :         /* Shouldn't have got a null, but be careful */
                               7262         [ -  + ]:          83981 :         if (isnull[0])
 2489 tgl@sss.pgh.pa.us        7263         [ #  # ]:UBC           0 :             elog(ERROR, "found unexpected null value in index \"%s\"",
                               7264                 :                :                  RelationGetRelationName(indexRel));
                               7265                 :                : 
                               7266                 :                :         /* Copy the index column value out to caller's context */
 2489 tgl@sss.pgh.pa.us        7267                 :CBC       83981 :         oldcontext = MemoryContextSwitchTo(outercontext);
                               7268                 :          83981 :         *endpointDatum = datumCopy(values[0], typByVal, typLen);
                               7269                 :          83981 :         MemoryContextSwitchTo(oldcontext);
                               7270                 :          83981 :         have_data = true;
                               7271                 :          83981 :         break;
                               7272                 :                :     }
                               7273                 :                : 
                               7274         [ +  + ]:          83981 :     if (vmbuffer != InvalidBuffer)
                               7275                 :          77023 :         ReleaseBuffer(vmbuffer);
                               7276                 :          83981 :     index_endscan(index_scan);
                               7277                 :                : 
                               7278                 :          83981 :     return have_data;
                               7279                 :                : }
                               7280                 :                : 
                               7281                 :                : /*
                               7282                 :                :  * find_join_input_rel
                               7283                 :                :  *      Look up the input relation for a join.
                               7284                 :                :  *
                               7285                 :                :  * We assume that the input relation's RelOptInfo must have been constructed
                               7286                 :                :  * already.
                               7287                 :                :  */
                               7288                 :                : static RelOptInfo *
 5361                          7289                 :          11394 : find_join_input_rel(PlannerInfo *root, Relids relids)
                               7290                 :                : {
                               7291                 :          11394 :     RelOptInfo *rel = NULL;
                               7292                 :                : 
  889 drowley@postgresql.o     7293         [ +  - ]:          11394 :     if (!bms_is_empty(relids))
                               7294                 :                :     {
                               7295                 :                :         int         relid;
                               7296                 :                : 
                               7297         [ +  + ]:          11394 :         if (bms_get_singleton_member(relids, &relid))
                               7298                 :          11118 :             rel = find_base_rel(root, relid);
                               7299                 :                :         else
 5361 tgl@sss.pgh.pa.us        7300                 :            276 :             rel = find_join_rel(root, relids);
                               7301                 :                :     }
                               7302                 :                : 
                               7303         [ -  + ]:          11394 :     if (rel == NULL)
 5361 tgl@sss.pgh.pa.us        7304         [ #  # ]:UBC           0 :         elog(ERROR, "could not find RelOptInfo for given relids");
                               7305                 :                : 
 5361 tgl@sss.pgh.pa.us        7306                 :CBC       11394 :     return rel;
                               7307                 :                : }
                               7308                 :                : 
                               7309                 :                : 
                               7310                 :                : /*-------------------------------------------------------------------------
                               7311                 :                :  *
                               7312                 :                :  * Index cost estimation functions
                               7313                 :                :  *
                               7314                 :                :  *-------------------------------------------------------------------------
                               7315                 :                :  */
                               7316                 :                : 
                               7317                 :                : /*
                               7318                 :                :  * Extract the actual indexquals (as RestrictInfos) from an IndexClause list
                               7319                 :                :  */
                               7320                 :                : List *
 2636                          7321                 :         669775 : get_quals_from_indexclauses(List *indexclauses)
                               7322                 :                : {
 2642                          7323                 :         669775 :     List       *result = NIL;
                               7324                 :                :     ListCell   *lc;
                               7325                 :                : 
                               7326   [ +  +  +  +  :        1163239 :     foreach(lc, indexclauses)
                                              +  + ]
                               7327                 :                :     {
                               7328                 :         493464 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
                               7329                 :                :         ListCell   *lc2;
                               7330                 :                : 
 2637                          7331   [ +  -  +  +  :         989351 :         foreach(lc2, iclause->indexquals)
                                              +  + ]
                               7332                 :                :         {
                               7333                 :         495887 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
                               7334                 :                : 
                               7335                 :         495887 :             result = lappend(result, rinfo);
                               7336                 :                :         }
                               7337                 :                :     }
 2642                          7338                 :         669775 :     return result;
                               7339                 :                : }
                               7340                 :                : 
                               7341                 :                : /*
                               7342                 :                :  * Compute the total evaluation cost of the comparison operands in a list
                               7343                 :                :  * of index qual expressions.  Since we know these will be evaluated just
                               7344                 :                :  * once per scan, there's no need to distinguish startup from per-row cost.
                               7345                 :                :  *
                               7346                 :                :  * This can be used either on the result of get_quals_from_indexclauses(),
                               7347                 :                :  * or directly on an indexorderbys list.  In both cases, we expect that the
                               7348                 :                :  * index key expression is on the left side of binary clauses.
                               7349                 :                :  */
                               7350                 :                : Cost
 2636                          7351                 :        1329050 : index_other_operands_eval_cost(PlannerInfo *root, List *indexquals)
                               7352                 :                : {
 4081                          7353                 :        1329050 :     Cost        qual_arg_cost = 0;
                               7354                 :                :     ListCell   *lc;
                               7355                 :                : 
 2636                          7356   [ +  +  +  +  :        1825286 :     foreach(lc, indexquals)
                                              +  + ]
                               7357                 :                :     {
 4081                          7358                 :         496236 :         Expr       *clause = (Expr *) lfirst(lc);
                               7359                 :                :         Node       *other_operand;
                               7360                 :                :         QualCost    index_qual_cost;
                               7361                 :                : 
                               7362                 :                :         /*
                               7363                 :                :          * Index quals will have RestrictInfos, indexorderbys won't.  Look
                               7364                 :                :          * through RestrictInfo if present.
                               7365                 :                :          */
 2636                          7366         [ +  + ]:         496236 :         if (IsA(clause, RestrictInfo))
                               7367                 :         495877 :             clause = ((RestrictInfo *) clause)->clause;
                               7368                 :                : 
 4081                          7369         [ +  + ]:         496236 :         if (IsA(clause, OpExpr))
                               7370                 :                :         {
 2636                          7371                 :         480825 :             OpExpr     *op = (OpExpr *) clause;
                               7372                 :                : 
                               7373                 :         480825 :             other_operand = (Node *) lsecond(op->args);
                               7374                 :                :         }
                               7375         [ +  + ]:          15411 :         else if (IsA(clause, RowCompareExpr))
                               7376                 :                :         {
                               7377                 :            330 :             RowCompareExpr *rc = (RowCompareExpr *) clause;
                               7378                 :                : 
                               7379                 :            330 :             other_operand = (Node *) rc->rargs;
                               7380                 :                :         }
                               7381         [ +  + ]:          15081 :         else if (IsA(clause, ScalarArrayOpExpr))
                               7382                 :                :         {
                               7383                 :          12642 :             ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
                               7384                 :                : 
                               7385                 :          12642 :             other_operand = (Node *) lsecond(saop->args);
                               7386                 :                :         }
                               7387         [ +  - ]:           2439 :         else if (IsA(clause, NullTest))
                               7388                 :                :         {
                               7389                 :           2439 :             other_operand = NULL;
                               7390                 :                :         }
                               7391                 :                :         else
                               7392                 :                :         {
 2636 tgl@sss.pgh.pa.us        7393         [ #  # ]:UBC           0 :             elog(ERROR, "unsupported indexqual type: %d",
                               7394                 :                :                  (int) nodeTag(clause));
                               7395                 :                :             other_operand = NULL;   /* keep compiler quiet */
                               7396                 :                :         }
                               7397                 :                : 
 4081 tgl@sss.pgh.pa.us        7398                 :CBC      496236 :         cost_qual_eval_node(&index_qual_cost, other_operand, root);
                               7399                 :         496236 :         qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
                               7400                 :                :     }
                               7401                 :        1329050 :     return qual_arg_cost;
                               7402                 :                : }
                               7403                 :                : 
                               7404                 :                : /*
                               7405                 :                :  * Compute generic index access cost estimates.
                               7406                 :                :  *
                               7407                 :                :  * See struct GenericCosts in selfuncs.h for more info.
                               7408                 :                :  */
                               7409                 :                : void
 7639                          7410                 :         659285 : genericcostestimate(PlannerInfo *root,
                               7411                 :                :                     IndexPath *path,
                               7412                 :                :                     double loop_count,
                               7413                 :                :                     GenericCosts *costs)
                               7414                 :                : {
 5246                          7415                 :         659285 :     IndexOptInfo *index = path->indexinfo;
 2636                          7416                 :         659285 :     List       *indexQuals = get_quals_from_indexclauses(path->indexclauses);
 5246                          7417                 :         659285 :     List       *indexOrderBys = path->indexorderbys;
                               7418                 :                :     Cost        indexStartupCost;
                               7419                 :                :     Cost        indexTotalCost;
                               7420                 :                :     Selectivity indexSelectivity;
                               7421                 :                :     double      indexCorrelation;
                               7422                 :                :     double      numIndexPages;
                               7423                 :                :     double      numIndexTuples;
                               7424                 :                :     double      spc_random_page_cost;
                               7425                 :                :     double      num_sa_scans;
                               7426                 :                :     double      num_outer_scans;
                               7427                 :                :     double      num_scans;
                               7428                 :                :     double      qual_op_cost;
                               7429                 :                :     double      qual_arg_cost;
                               7430                 :                :     List       *selectivityQuals;
                               7431                 :                :     ListCell   *l;
                               7432                 :                : 
                               7433                 :                :     /*
                               7434                 :                :      * If the index is partial, AND the index predicate with the explicitly
                               7435                 :                :      * given indexquals to produce a more accurate idea of the index
                               7436                 :                :      * selectivity.
                               7437                 :                :      */
 2636                          7438                 :         659285 :     selectivityQuals = add_predicate_to_index_quals(index, indexQuals);
                               7439                 :                : 
                               7440                 :                :     /*
                               7441                 :                :      * If caller didn't give us an estimate for ScalarArrayOpExpr index scans,
                               7442                 :                :      * just assume that the number of index descents is the number of distinct
                               7443                 :                :      * combinations of array elements from all of the scan's SAOP clauses.
                               7444                 :                :      */
  759 pg@bowt.ie               7445                 :         659285 :     num_sa_scans = costs->num_sa_scans;
                               7446         [ +  + ]:         659285 :     if (num_sa_scans < 1)
                               7447                 :                :     {
                               7448                 :           6942 :         num_sa_scans = 1;
                               7449   [ +  +  +  +  :          15192 :         foreach(l, indexQuals)
                                              +  + ]
                               7450                 :                :         {
                               7451                 :           8250 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
                               7452                 :                : 
                               7453         [ +  + ]:           8250 :             if (IsA(rinfo->clause, ScalarArrayOpExpr))
                               7454                 :                :             {
                               7455                 :             46 :                 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
                               7456                 :             46 :                 double      alength = estimate_array_length(root, lsecond(saop->args));
                               7457                 :                : 
                               7458         [ +  - ]:             46 :                 if (alength > 1)
                               7459                 :             46 :                     num_sa_scans *= alength;
                               7460                 :                :             }
                               7461                 :                :         }
                               7462                 :                :     }
                               7463                 :                : 
                               7464                 :                :     /* Estimate the fraction of main-table tuples that will be visited */
 4862 tgl@sss.pgh.pa.us        7465                 :         659285 :     indexSelectivity = clauselist_selectivity(root, selectivityQuals,
                               7466                 :         659285 :                                               index->rel->relid,
                               7467                 :                :                                               JOIN_INNER,
                               7468                 :                :                                               NULL);
                               7469                 :                : 
                               7470                 :                :     /*
                               7471                 :                :      * If caller didn't give us an estimate, estimate the number of index
                               7472                 :                :      * tuples that will be visited.  We do it in this rather peculiar-looking
                               7473                 :                :      * way in order to get the right answer for partial indexes.
                               7474                 :                :      */
                               7475                 :         659285 :     numIndexTuples = costs->numIndexTuples;
 7631                          7476         [ +  + ]:         659285 :     if (numIndexTuples <= 0.0)
                               7477                 :                :     {
 4862                          7478                 :          87238 :         numIndexTuples = indexSelectivity * index->rel->tuples;
                               7479                 :                : 
                               7480                 :                :         /*
                               7481                 :                :          * The above calculation counts all the tuples visited across all
                               7482                 :                :          * scans induced by ScalarArrayOpExpr nodes.  We want to consider the
                               7483                 :                :          * average per-indexscan number, so adjust.  This is a handy place to
                               7484                 :                :          * round to integer, too.  (If caller supplied tuple estimate, it's
                               7485                 :                :          * responsible for handling these considerations.)
                               7486                 :                :          */
 7081                          7487                 :          87238 :         numIndexTuples = rint(numIndexTuples / num_sa_scans);
                               7488                 :                :     }
                               7489                 :                : 
                               7490                 :                :     /*
                               7491                 :                :      * We can bound the number of tuples by the index size in any case. Also,
                               7492                 :                :      * always estimate at least one tuple is touched, even when
                               7493                 :                :      * indexSelectivity estimate is tiny.
                               7494                 :                :      */
 7631                          7495         [ +  + ]:         659285 :     if (numIndexTuples > index->tuples)
                               7496                 :           8440 :         numIndexTuples = index->tuples;
 9522                          7497         [ +  + ]:         659285 :     if (numIndexTuples < 1.0)
                               7498                 :          90862 :         numIndexTuples = 1.0;
                               7499                 :                : 
                               7500                 :                :     /*
                               7501                 :                :      * Estimate the number of index pages that will be retrieved.
                               7502                 :                :      *
                               7503                 :                :      * We use the simplistic method of taking a pro-rata fraction of the total
                               7504                 :                :      * number of index leaf pages.  We disregard any overhead such as index
                               7505                 :                :      * metapages or upper tree levels.
                               7506                 :                :      *
                               7507                 :                :      * In practice access to upper index levels is often nearly free because
                               7508                 :                :      * those tend to stay in cache under load; moreover, the cost involved is
                               7509                 :                :      * highly dependent on index type.  We therefore ignore such costs here
                               7510                 :                :      * and leave it to the caller to add a suitable charge if needed.
                               7511                 :                :      */
   46 tgl@sss.pgh.pa.us        7512   [ +  +  +  + ]:GNC      659285 :     if (index->pages > costs->numNonLeafPages && index->tuples > 1)
                               7513                 :         592624 :         numIndexPages =
                               7514                 :         592624 :             ceil(numIndexTuples * (index->pages - costs->numNonLeafPages)
                               7515                 :         592624 :                  / index->tuples);
                               7516                 :                :     else
 9522 tgl@sss.pgh.pa.us        7517                 :CBC       66661 :         numIndexPages = 1.0;
                               7518                 :                : 
                               7519                 :                :     /* fetch estimated page cost for tablespace containing index */
 5964 rhaas@postgresql.org     7520                 :         659285 :     get_tablespace_page_costs(index->reltablespace,
                               7521                 :                :                               &spc_random_page_cost,
                               7522                 :                :                               NULL);
                               7523                 :                : 
                               7524                 :                :     /*
                               7525                 :                :      * Now compute the disk access costs.
                               7526                 :                :      *
                               7527                 :                :      * The above calculations are all per-index-scan.  However, if we are in a
                               7528                 :                :      * nestloop inner scan, we can expect the scan to be repeated (with
                               7529                 :                :      * different search keys) for each row of the outer relation.  Likewise,
                               7530                 :                :      * ScalarArrayOpExpr quals result in multiple index scans.  This creates
                               7531                 :                :      * the potential for cache effects to reduce the number of disk page
                               7532                 :                :      * fetches needed.  We want to estimate the average per-scan I/O cost in
                               7533                 :                :      * the presence of caching.
                               7534                 :                :      *
                               7535                 :                :      * We use the Mackert-Lohman formula (see costsize.c for details) to
                               7536                 :                :      * estimate the total number of page fetches that occur.  While this
                               7537                 :                :      * wasn't what it was designed for, it seems a reasonable model anyway.
                               7538                 :                :      * Note that we are counting pages not tuples anymore, so we take N = T =
                               7539                 :                :      * index size, as if there were one "tuple" per page.
                               7540                 :                :      */
 5212 tgl@sss.pgh.pa.us        7541                 :         659285 :     num_outer_scans = loop_count;
                               7542                 :         659285 :     num_scans = num_sa_scans * num_outer_scans;
                               7543                 :                : 
 7248                          7544         [ +  + ]:         659285 :     if (num_scans > 1)
                               7545                 :                :     {
                               7546                 :                :         double      pages_fetched;
                               7547                 :                : 
                               7548                 :                :         /* total page fetches ignoring cache effects */
 7273                          7549                 :          79869 :         pages_fetched = numIndexPages * num_scans;
                               7550                 :                : 
                               7551                 :                :         /* use Mackert and Lohman formula to adjust for cache effects */
                               7552                 :          79869 :         pages_fetched = index_pages_fetched(pages_fetched,
                               7553                 :                :                                             index->pages,
 7168                          7554                 :          79869 :                                             (double) index->pages,
                               7555                 :                :                                             root);
                               7556                 :                : 
                               7557                 :                :         /*
                               7558                 :                :          * Now compute the total disk access cost, and then report a pro-rated
                               7559                 :                :          * share for each outer scan.  (Don't pro-rate for ScalarArrayOpExpr,
                               7560                 :                :          * since that's internal to the indexscan.)
                               7561                 :                :          */
 4862                          7562                 :          79869 :         indexTotalCost = (pages_fetched * spc_random_page_cost)
                               7563                 :                :             / num_outer_scans;
                               7564                 :                :     }
                               7565                 :                :     else
                               7566                 :                :     {
                               7567                 :                :         /*
                               7568                 :                :          * For a single index scan, we just charge spc_random_page_cost per
                               7569                 :                :          * page touched.
                               7570                 :                :          */
                               7571                 :         579416 :         indexTotalCost = numIndexPages * spc_random_page_cost;
                               7572                 :                :     }
                               7573                 :                : 
                               7574                 :                :     /*
                               7575                 :                :      * CPU cost: any complex expressions in the indexquals will need to be
                               7576                 :                :      * evaluated once at the start of the scan to reduce them to runtime keys
                               7577                 :                :      * to pass to the index AM (see nodeIndexscan.c).  We model the per-tuple
                               7578                 :                :      * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
                               7579                 :                :      * indexqual operator.  Because we have numIndexTuples as a per-scan
                               7580                 :                :      * number, we have to multiply by num_sa_scans to get the correct result
                               7581                 :                :      * for ScalarArrayOpExpr cases.  Similarly add in costs for any index
                               7582                 :                :      * ORDER BY expressions.
                               7583                 :                :      *
                               7584                 :                :      * Note: this neglects the possible costs of rechecking lossy operators.
                               7585                 :                :      * Detecting that that might be needed seems more expensive than it's
                               7586                 :                :      * worth, though, considering all the other inaccuracies here ...
                               7587                 :                :      */
 2636                          7588                 :         659285 :     qual_arg_cost = index_other_operands_eval_cost(root, indexQuals) +
                               7589                 :         659285 :         index_other_operands_eval_cost(root, indexOrderBys);
 5633                          7590                 :         659285 :     qual_op_cost = cpu_operator_cost *
                               7591                 :         659285 :         (list_length(indexQuals) + list_length(indexOrderBys));
                               7592                 :                : 
 4862                          7593                 :         659285 :     indexStartupCost = qual_arg_cost;
                               7594                 :         659285 :     indexTotalCost += qual_arg_cost;
                               7595                 :         659285 :     indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
                               7596                 :                : 
                               7597                 :                :     /*
                               7598                 :                :      * Generic assumption about index correlation: there isn't any.
                               7599                 :                :      */
                               7600                 :         659285 :     indexCorrelation = 0.0;
                               7601                 :                : 
                               7602                 :                :     /*
                               7603                 :                :      * Return everything to caller.
                               7604                 :                :      */
                               7605                 :         659285 :     costs->indexStartupCost = indexStartupCost;
                               7606                 :         659285 :     costs->indexTotalCost = indexTotalCost;
                               7607                 :         659285 :     costs->indexSelectivity = indexSelectivity;
                               7608                 :         659285 :     costs->indexCorrelation = indexCorrelation;
                               7609                 :         659285 :     costs->numIndexPages = numIndexPages;
                               7610                 :         659285 :     costs->numIndexTuples = numIndexTuples;
                               7611                 :         659285 :     costs->spc_random_page_cost = spc_random_page_cost;
                               7612                 :         659285 :     costs->num_sa_scans = num_sa_scans;
                               7613                 :         659285 : }
                               7614                 :                : 
                               7615                 :                : /*
                               7616                 :                :  * If the index is partial, add its predicate to the given qual list.
                               7617                 :                :  *
                               7618                 :                :  * ANDing the index predicate with the explicitly given indexquals produces
                               7619                 :                :  * a more accurate idea of the index's selectivity.  However, we need to be
                               7620                 :                :  * careful not to insert redundant clauses, because clauselist_selectivity()
                               7621                 :                :  * is easily fooled into computing a too-low selectivity estimate.  Our
                               7622                 :                :  * approach is to add only the predicate clause(s) that cannot be proven to
                               7623                 :                :  * be implied by the given indexquals.  This successfully handles cases such
                               7624                 :                :  * as a qual "x = 42" used with a partial index "WHERE x >= 40 AND x < 50".
                               7625                 :                :  * There are many other cases where we won't detect redundancy, leading to a
                               7626                 :                :  * too-low selectivity estimate, which will bias the system in favor of using
                               7627                 :                :  * partial indexes where possible.  That is not necessarily bad though.
                               7628                 :                :  *
                               7629                 :                :  * Note that indexQuals contains RestrictInfo nodes while the indpred
                               7630                 :                :  * does not, so the output list will be mixed.  This is OK for both
                               7631                 :                :  * predicate_implied_by() and clauselist_selectivity(), but might be
                               7632                 :                :  * problematic if the result were passed to other things.
                               7633                 :                :  */
                               7634                 :                : List *
 2636                          7635                 :        1130220 : add_predicate_to_index_quals(IndexOptInfo *index, List *indexQuals)
                               7636                 :                : {
 4862                          7637                 :        1130220 :     List       *predExtraQuals = NIL;
                               7638                 :                :     ListCell   *lc;
                               7639                 :                : 
                               7640         [ +  + ]:        1130220 :     if (index->indpred == NIL)
                               7641                 :        1128683 :         return indexQuals;
                               7642                 :                : 
                               7643   [ +  -  +  +  :           3084 :     foreach(lc, index->indpred)
                                              +  + ]
                               7644                 :                :     {
                               7645                 :           1547 :         Node       *predQual = (Node *) lfirst(lc);
                               7646                 :           1547 :         List       *oneQual = list_make1(predQual);
                               7647                 :                : 
 3247 rhaas@postgresql.org     7648         [ +  + ]:           1547 :         if (!predicate_implied_by(oneQual, indexQuals, false))
 4862 tgl@sss.pgh.pa.us        7649                 :           1370 :             predExtraQuals = list_concat(predExtraQuals, oneQual);
                               7650                 :                :     }
                               7651                 :           1537 :     return list_concat(predExtraQuals, indexQuals);
                               7652                 :                : }
                               7653                 :                : 
                               7654                 :                : /*
                               7655                 :                :  * Estimate correlation of btree index's first column.
                               7656                 :                :  *
                               7657                 :                :  * If we can get an estimate of the first column's ordering correlation C
                               7658                 :                :  * from pg_statistic, estimate the index correlation as C for a single-column
                               7659                 :                :  * index, or C * 0.75 for multiple columns.  The idea here is that multiple
                               7660                 :                :  * columns dilute the importance of the first column's ordering, but don't
                               7661                 :                :  * negate it entirely.
                               7662                 :                :  *
                               7663                 :                :  * We already filled in the stats tuple for *vardata when called.
                               7664                 :                :  */
                               7665                 :                : static double
  396 pg@bowt.ie               7666                 :         464930 : btcost_correlation(IndexOptInfo *index, VariableStatData *vardata)
                               7667                 :                : {
                               7668                 :                :     Oid         sortop;
                               7669                 :                :     AttStatsSlot sslot;
                               7670                 :         464930 :     double      indexCorrelation = 0;
                               7671                 :                : 
                               7672         [ -  + ]:         464930 :     Assert(HeapTupleIsValid(vardata->statsTuple));
                               7673                 :                : 
                               7674                 :         464930 :     sortop = get_opfamily_member(index->opfamily[0],
                               7675                 :         464930 :                                  index->opcintype[0],
                               7676                 :         464930 :                                  index->opcintype[0],
                               7677                 :                :                                  BTLessStrategyNumber);
                               7678   [ +  -  +  + ]:         929860 :     if (OidIsValid(sortop) &&
                               7679                 :         464930 :         get_attstatsslot(&sslot, vardata->statsTuple,
                               7680                 :                :                          STATISTIC_KIND_CORRELATION, sortop,
                               7681                 :                :                          ATTSTATSSLOT_NUMBERS))
                               7682                 :                :     {
                               7683                 :                :         double      varCorrelation;
                               7684                 :                : 
                               7685         [ -  + ]:         459360 :         Assert(sslot.nnumbers == 1);
                               7686                 :         459360 :         varCorrelation = sslot.numbers[0];
                               7687                 :                : 
                               7688         [ -  + ]:         459360 :         if (index->reverse_sort[0])
  396 pg@bowt.ie               7689                 :UBC           0 :             varCorrelation = -varCorrelation;
                               7690                 :                : 
  396 pg@bowt.ie               7691         [ +  + ]:CBC      459360 :         if (index->nkeycolumns > 1)
                               7692                 :         161739 :             indexCorrelation = varCorrelation * 0.75;
                               7693                 :                :         else
                               7694                 :         297621 :             indexCorrelation = varCorrelation;
                               7695                 :                : 
                               7696                 :         459360 :         free_attstatsslot(&sslot);
                               7697                 :                :     }
                               7698                 :                : 
                               7699                 :         464930 :     return indexCorrelation;
                               7700                 :                : }
                               7701                 :                : 
                               7702                 :                : void
 3761 tgl@sss.pgh.pa.us        7703                 :         652343 : btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
                               7704                 :                :                Cost *indexStartupCost, Cost *indexTotalCost,
                               7705                 :                :                Selectivity *indexSelectivity, double *indexCorrelation,
                               7706                 :                :                double *indexPages)
                               7707                 :                : {
 5246                          7708                 :         652343 :     IndexOptInfo *index = path->indexinfo;
 1389 peter@eisentraut.org     7709                 :         652343 :     GenericCosts costs = {0};
                               7710                 :         652343 :     VariableStatData vardata = {0};
                               7711                 :                :     double      numIndexTuples;
                               7712                 :                :     Cost        descentCost;
                               7713                 :                :     List       *indexBoundQuals;
                               7714                 :                :     List       *indexSkipQuals;
                               7715                 :                :     int         indexcol;
                               7716                 :                :     bool        eqQualHere;
                               7717                 :                :     bool        found_row_compare;
                               7718                 :                :     bool        found_array;
                               7719                 :                :     bool        found_is_null_op;
  396 pg@bowt.ie               7720                 :         652343 :     bool        have_correlation = false;
                               7721                 :                :     double      num_sa_scans;
                               7722                 :         652343 :     double      correlation = 0.0;
                               7723                 :                :     ListCell   *lc;
                               7724                 :                : 
                               7725                 :                :     /*
                               7726                 :                :      * For a btree scan, only leading '=' quals plus inequality quals for the
                               7727                 :                :      * immediately next attribute contribute to index selectivity (these are
                               7728                 :                :      * the "boundary quals" that determine the starting and stopping points of
                               7729                 :                :      * the index scan).  Additional quals can suppress visits to the heap, so
                               7730                 :                :      * it's OK to count them in indexSelectivity, but they should not count
                               7731                 :                :      * for estimating numIndexTuples.  So we must examine the given indexquals
                               7732                 :                :      * to find out which ones count as boundary quals.  We rely on the
                               7733                 :                :      * knowledge that they are given in index column order.  Note that nbtree
                               7734                 :                :      * preprocessing can add skip arrays that act as leading '=' quals in the
                               7735                 :                :      * absence of ordinary input '=' quals, so in practice _most_ input quals
                               7736                 :                :      * are able to act as index bound quals (which we take into account here).
                               7737                 :                :      *
                               7738                 :                :      * For a RowCompareExpr, we consider only the first column, just as
                               7739                 :                :      * rowcomparesel() does.
                               7740                 :                :      *
                               7741                 :                :      * If there's a SAOP or skip array in the quals, we'll actually perform up
                               7742                 :                :      * to N index descents (not just one), but the underlying array key's
                               7743                 :                :      * operator can be considered to act the same as it normally does.
                               7744                 :                :      */
 7631 tgl@sss.pgh.pa.us        7745                 :         652343 :     indexBoundQuals = NIL;
  396 pg@bowt.ie               7746                 :         652343 :     indexSkipQuals = NIL;
 5246 tgl@sss.pgh.pa.us        7747                 :         652343 :     indexcol = 0;
 7631                          7748                 :         652343 :     eqQualHere = false;
  396 pg@bowt.ie               7749                 :         652343 :     found_row_compare = false;
                               7750                 :         652343 :     found_array = false;
 5968 tgl@sss.pgh.pa.us        7751                 :         652343 :     found_is_null_op = false;
 7081                          7752                 :         652343 :     num_sa_scans = 1;
 2636                          7753   [ +  +  +  +  :        1096915 :     foreach(lc, path->indexclauses)
                                              +  + ]
                               7754                 :                :     {
                               7755                 :         472343 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
                               7756                 :                :         ListCell   *lc2;
                               7757                 :                : 
  396 pg@bowt.ie               7758         [ +  + ]:         472343 :         if (indexcol < iclause->indexcol)
                               7759                 :                :         {
                               7760                 :          93283 :             double      num_sa_scans_prev_cols = num_sa_scans;
                               7761                 :                : 
                               7762                 :                :             /*
                               7763                 :                :              * Beginning of a new column's quals.
                               7764                 :                :              *
                               7765                 :                :              * Skip scans use skip arrays, which are ScalarArrayOp style
                               7766                 :                :              * arrays that generate their elements procedurally and on demand.
                               7767                 :                :              * Given a multi-column index on "(a, b)", and an SQL WHERE clause
                               7768                 :                :              * "WHERE b = 42", a skip scan will effectively use an indexqual
                               7769                 :                :              * "WHERE a = ANY('{every col a value}') AND b = 42".  (Obviously,
                               7770                 :                :              * the array on "a" must also return "IS NULL" matches, since our
                               7771                 :                :              * WHERE clause used no strict operator on "a").
                               7772                 :                :              *
                               7773                 :                :              * Here we consider how nbtree will backfill skip arrays for any
                               7774                 :                :              * index columns that lacked an '=' qual.  This maintains our
                               7775                 :                :              * num_sa_scans estimate, and determines if this new column (the
                               7776                 :                :              * "iclause->indexcol" column, not the prior "indexcol" column)
                               7777                 :                :              * can have its RestrictInfos/quals added to indexBoundQuals.
                               7778                 :                :              *
                               7779                 :                :              * We'll need to handle columns that have inequality quals, where
                               7780                 :                :              * the skip array generates values from a range constrained by the
                               7781                 :                :              * quals (not every possible value).  We've been maintaining
                               7782                 :                :              * indexSkipQuals to help with this; it will now contain all of
                               7783                 :                :              * the prior column's quals (that is, indexcol's quals) when they
                               7784                 :                :              * might be used for this.
                               7785                 :                :              */
                               7786         [ +  + ]:          93283 :             if (found_row_compare)
                               7787                 :                :             {
                               7788                 :                :                 /*
                               7789                 :                :                  * Skip arrays can't be added after a RowCompare input qual
                               7790                 :                :                  * due to limitations in nbtree
                               7791                 :                :                  */
                               7792                 :             20 :                 break;
                               7793                 :                :             }
                               7794         [ +  + ]:          93263 :             if (eqQualHere)
                               7795                 :                :             {
                               7796                 :                :                 /*
                               7797                 :                :                  * Don't need to add a skip array for an indexcol that already
                               7798                 :                :                  * has an '=' qual/equality constraint
                               7799                 :                :                  */
                               7800                 :          65948 :                 indexcol++;
                               7801                 :          65948 :                 indexSkipQuals = NIL;
                               7802                 :                :             }
 5246 tgl@sss.pgh.pa.us        7803                 :          93263 :             eqQualHere = false;
                               7804                 :                : 
  396 pg@bowt.ie               7805         [ +  + ]:          94898 :             while (indexcol < iclause->indexcol)
                               7806                 :                :             {
                               7807                 :                :                 double      ndistinct;
                               7808                 :          29386 :                 bool        isdefault = true;
                               7809                 :                : 
                               7810                 :          29386 :                 found_array = true;
                               7811                 :                : 
                               7812                 :                :                 /*
                               7813                 :                :                  * A skipped attribute's ndistinct forms the basis of our
                               7814                 :                :                  * estimate of the total number of "array elements" used by
                               7815                 :                :                  * its skip array at runtime.  Look that up first.
                               7816                 :                :                  */
                               7817                 :          29386 :                 examine_indexcol_variable(root, index, indexcol, &vardata);
                               7818                 :          29386 :                 ndistinct = get_variable_numdistinct(&vardata, &isdefault);
                               7819                 :                : 
                               7820         [ +  + ]:          29386 :                 if (indexcol == 0)
                               7821                 :                :                 {
                               7822                 :                :                     /*
                               7823                 :                :                      * Get an estimate of the leading column's correlation in
                               7824                 :                :                      * passing (avoids rereading variable stats below)
                               7825                 :                :                      */
                               7826         [ +  + ]:          27305 :                     if (HeapTupleIsValid(vardata.statsTuple))
                               7827                 :          16657 :                         correlation = btcost_correlation(index, &vardata);
                               7828                 :          27305 :                     have_correlation = true;
                               7829                 :                :                 }
                               7830                 :                : 
                               7831         [ +  + ]:          29386 :                 ReleaseVariableStats(vardata);
                               7832                 :                : 
                               7833                 :                :                 /*
                               7834                 :                :                  * If ndistinct is a default estimate, conservatively assume
                               7835                 :                :                  * that no skipping will happen at runtime
                               7836                 :                :                  */
                               7837         [ +  + ]:          29386 :                 if (isdefault)
                               7838                 :                :                 {
                               7839                 :           8479 :                     num_sa_scans = num_sa_scans_prev_cols;
                               7840                 :          27751 :                     break;      /* done building indexBoundQuals */
                               7841                 :                :                 }
                               7842                 :                : 
                               7843                 :                :                 /*
                               7844                 :                :                  * Apply indexcol's indexSkipQuals selectivity to ndistinct
                               7845                 :                :                  */
                               7846         [ +  + ]:          20907 :                 if (indexSkipQuals != NIL)
                               7847                 :                :                 {
                               7848                 :                :                     List       *partialSkipQuals;
                               7849                 :                :                     Selectivity ndistinctfrac;
                               7850                 :                : 
                               7851                 :                :                     /*
                               7852                 :                :                      * If the index is partial, AND the index predicate with
                               7853                 :                :                      * the index-bound quals to produce a more accurate idea
                               7854                 :                :                      * of the number of distinct values for prior indexcol
                               7855                 :                :                      */
                               7856                 :            552 :                     partialSkipQuals = add_predicate_to_index_quals(index,
                               7857                 :                :                                                                     indexSkipQuals);
                               7858                 :                : 
                               7859                 :            552 :                     ndistinctfrac = clauselist_selectivity(root, partialSkipQuals,
                               7860                 :            552 :                                                            index->rel->relid,
                               7861                 :                :                                                            JOIN_INNER,
                               7862                 :                :                                                            NULL);
                               7863                 :                : 
                               7864                 :                :                     /*
                               7865                 :                :                      * If ndistinctfrac is selective (on its own), the scan is
                               7866                 :                :                      * unlikely to benefit from repositioning itself using
                               7867                 :                :                      * later quals.  Do not allow iclause->indexcol's quals to
                               7868                 :                :                      * be added to indexBoundQuals (it would increase descent
                               7869                 :                :                      * costs, without lowering numIndexTuples costs by much).
                               7870                 :                :                      */
                               7871         [ +  + ]:            552 :                     if (ndistinctfrac < DEFAULT_RANGE_INEQ_SEL)
                               7872                 :                :                     {
                               7873                 :            311 :                         num_sa_scans = num_sa_scans_prev_cols;
                               7874                 :            311 :                         break;  /* done building indexBoundQuals */
                               7875                 :                :                     }
                               7876                 :                : 
                               7877                 :                :                     /* Adjust ndistinct downward */
                               7878                 :            241 :                     ndistinct = rint(ndistinct * ndistinctfrac);
                               7879         [ +  - ]:            241 :                     ndistinct = Max(ndistinct, 1);
                               7880                 :                :                 }
                               7881                 :                : 
                               7882                 :                :                 /*
                               7883                 :                :                  * When there's no inequality quals, account for the need to
                               7884                 :                :                  * find an initial value by counting -inf/+inf as a value.
                               7885                 :                :                  *
                               7886                 :                :                  * We don't charge anything extra for possible next/prior key
                               7887                 :                :                  * index probes, which are sometimes used to find the next
                               7888                 :                :                  * valid skip array element (ahead of using the located
                               7889                 :                :                  * element value to relocate the scan to the next position
                               7890                 :                :                  * that might contain matching tuples).  It seems hard to do
                               7891                 :                :                  * better here.  Use of the skip support infrastructure often
                               7892                 :                :                  * avoids most next/prior key probes.  But even when it can't,
                               7893                 :                :                  * there's a decent chance that most individual next/prior key
                               7894                 :                :                  * probes will locate a leaf page whose key space overlaps all
                               7895                 :                :                  * of the scan's keys (even the lower-order keys) -- which
                               7896                 :                :                  * also avoids the need for a separate, extra index descent.
                               7897                 :                :                  * Note also that these probes are much cheaper than non-probe
                               7898                 :                :                  * primitive index scans: they're reliably very selective.
                               7899                 :                :                  */
                               7900         [ +  + ]:          20596 :                 if (indexSkipQuals == NIL)
                               7901                 :          20355 :                     ndistinct += 1;
                               7902                 :                : 
                               7903                 :                :                 /*
                               7904                 :                :                  * Update num_sa_scans estimate by multiplying by ndistinct.
                               7905                 :                :                  *
                               7906                 :                :                  * We make the pessimistic assumption that there is no
                               7907                 :                :                  * naturally occurring cross-column correlation.  This is
                               7908                 :                :                  * often wrong, but it seems best to err on the side of not
                               7909                 :                :                  * expecting skipping to be helpful...
                               7910                 :                :                  */
                               7911                 :          20596 :                 num_sa_scans *= ndistinct;
                               7912                 :                : 
                               7913                 :                :                 /*
                               7914                 :                :                  * ...but back out of adding this latest group of 1 or more
                               7915                 :                :                  * skip arrays when num_sa_scans exceeds the total number of
                               7916                 :                :                  * index pages (revert to num_sa_scans from before indexcol).
                               7917                 :                :                  * This causes a sharp discontinuity in cost (as a function of
                               7918                 :                :                  * the indexcol's ndistinct), but that is representative of
                               7919                 :                :                  * actual runtime costs.
                               7920                 :                :                  *
                               7921                 :                :                  * Note that skipping is helpful when each primitive index
                               7922                 :                :                  * scan only manages to skip over 1 or 2 irrelevant leaf pages
                               7923                 :                :                  * on average.  Skip arrays bring savings in CPU costs due to
                               7924                 :                :                  * the scan not needing to evaluate indexquals against every
                               7925                 :                :                  * tuple, which can greatly exceed any savings in I/O costs.
                               7926                 :                :                  * This test is a test of whether num_sa_scans implies that
                               7927                 :                :                  * we're past the point where the ability to skip ceases to
                               7928                 :                :                  * lower the scan's costs (even qual evaluation CPU costs).
                               7929                 :                :                  */
                               7930         [ +  + ]:          20596 :                 if (index->pages < num_sa_scans)
                               7931                 :                :                 {
                               7932                 :          18961 :                     num_sa_scans = num_sa_scans_prev_cols;
                               7933                 :          18961 :                     break;      /* done building indexBoundQuals */
                               7934                 :                :                 }
                               7935                 :                : 
                               7936                 :           1635 :                 indexcol++;
                               7937                 :           1635 :                 indexSkipQuals = NIL;
                               7938                 :                :             }
                               7939                 :                : 
                               7940                 :                :             /*
                               7941                 :                :              * Finished considering the need to add skip arrays to bridge an
                               7942                 :                :              * initial eqQualHere gap between the old and new index columns
                               7943                 :                :              * (or there was no initial eqQualHere gap in the first place).
                               7944                 :                :              *
                               7945                 :                :              * If an initial gap could not be bridged, then new column's quals
                               7946                 :                :              * (i.e. iclause->indexcol's quals) won't go into indexBoundQuals,
                               7947                 :                :              * and so won't affect our final numIndexTuples estimate.
                               7948                 :                :              */
 2636 tgl@sss.pgh.pa.us        7949         [ +  + ]:          93263 :             if (indexcol != iclause->indexcol)
  396 pg@bowt.ie               7950                 :          27751 :                 break;          /* done building indexBoundQuals */
                               7951                 :                :         }
                               7952                 :                : 
                               7953         [ -  + ]:         444572 :         Assert(indexcol == iclause->indexcol);
                               7954                 :                : 
                               7955                 :                :         /* Examine each indexqual associated with this index clause */
 2636 tgl@sss.pgh.pa.us        7956   [ +  -  +  +  :         891445 :         foreach(lc2, iclause->indexquals)
                                              +  + ]
                               7957                 :                :         {
                               7958                 :         446873 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
                               7959                 :         446873 :             Expr       *clause = rinfo->clause;
                               7960                 :         446873 :             Oid         clause_op = InvalidOid;
                               7961                 :                :             int         op_strategy;
                               7962                 :                : 
                               7963         [ +  + ]:         446873 :             if (IsA(clause, OpExpr))
                               7964                 :                :             {
                               7965                 :         432413 :                 OpExpr     *op = (OpExpr *) clause;
                               7966                 :                : 
                               7967                 :         432413 :                 clause_op = op->opno;
                               7968                 :                :             }
                               7969         [ +  + ]:          14460 :             else if (IsA(clause, RowCompareExpr))
                               7970                 :                :             {
                               7971                 :            330 :                 RowCompareExpr *rc = (RowCompareExpr *) clause;
                               7972                 :                : 
                               7973                 :            330 :                 clause_op = linitial_oid(rc->opnos);
  396 pg@bowt.ie               7974                 :            330 :                 found_row_compare = true;
                               7975                 :                :             }
 2636 tgl@sss.pgh.pa.us        7976         [ +  + ]:          14130 :             else if (IsA(clause, ScalarArrayOpExpr))
                               7977                 :                :             {
                               7978                 :          12232 :                 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
                               7979                 :          12232 :                 Node       *other_operand = (Node *) lsecond(saop->args);
  852                          7980                 :          12232 :                 double      alength = estimate_array_length(root, other_operand);
                               7981                 :                : 
 2636                          7982                 :          12232 :                 clause_op = saop->opno;
  396 pg@bowt.ie               7983                 :          12232 :                 found_array = true;
                               7984                 :                :                 /* estimate SA descents by indexBoundQuals only */
 2636 tgl@sss.pgh.pa.us        7985         [ +  + ]:          12232 :                 if (alength > 1)
                               7986                 :          12008 :                     num_sa_scans *= alength;
                               7987                 :                :             }
                               7988         [ +  - ]:           1898 :             else if (IsA(clause, NullTest))
                               7989                 :                :             {
                               7990                 :           1898 :                 NullTest   *nt = (NullTest *) clause;
                               7991                 :                : 
                               7992         [ +  + ]:           1898 :                 if (nt->nulltesttype == IS_NULL)
                               7993                 :                :                 {
                               7994                 :            200 :                     found_is_null_op = true;
                               7995                 :                :                     /* IS NULL is like = for selectivity/skip scan purposes */
                               7996                 :            200 :                     eqQualHere = true;
                               7997                 :                :                 }
                               7998                 :                :             }
                               7999                 :                :             else
 2636 tgl@sss.pgh.pa.us        8000         [ #  # ]:UBC           0 :                 elog(ERROR, "unsupported indexqual type: %d",
                               8001                 :                :                      (int) nodeTag(clause));
                               8002                 :                : 
                               8003                 :                :             /* check for equality operator */
 2636 tgl@sss.pgh.pa.us        8004         [ +  + ]:CBC      446873 :             if (OidIsValid(clause_op))
                               8005                 :                :             {
                               8006                 :         444975 :                 op_strategy = get_op_opfamily_strategy(clause_op,
                               8007                 :         444975 :                                                        index->opfamily[indexcol]);
                               8008         [ -  + ]:         444975 :                 Assert(op_strategy != 0);   /* not a member of opfamily?? */
                               8009         [ +  + ]:         444975 :                 if (op_strategy == BTEqualStrategyNumber)
                               8010                 :         421150 :                     eqQualHere = true;
                               8011                 :                :             }
                               8012                 :                : 
                               8013                 :         446873 :             indexBoundQuals = lappend(indexBoundQuals, rinfo);
                               8014                 :                : 
                               8015                 :                :             /*
                               8016                 :                :              * We apply inequality selectivities to estimate index descent
                               8017                 :                :              * costs with scans that use skip arrays.  Save this indexcol's
                               8018                 :                :              * RestrictInfos if it looks like they'll be needed for that.
                               8019                 :                :              */
  396 pg@bowt.ie               8020   [ +  +  +  + ]:         446873 :             if (!eqQualHere && !found_row_compare &&
                               8021         [ +  + ]:          24626 :                 indexcol < index->nkeycolumns - 1)
                               8022                 :           4768 :                 indexSkipQuals = lappend(indexSkipQuals, rinfo);
                               8023                 :                :         }
                               8024                 :                :     }
                               8025                 :                : 
                               8026                 :                :     /*
                               8027                 :                :      * If index is unique and we found an '=' clause for each column, we can
                               8028                 :                :      * just assume numIndexTuples = 1 and skip the expensive
                               8029                 :                :      * clauselist_selectivity calculations.  However, an array or NullTest
                               8030                 :                :      * always invalidates that theory (even when eqQualHere has been set).
                               8031                 :                :      */
 7466 tgl@sss.pgh.pa.us        8032         [ +  + ]:         652343 :     if (index->unique &&
 2950 teodor@sigaev.ru         8033   [ +  +  +  + ]:         521694 :         indexcol == index->nkeycolumns - 1 &&
 7466 tgl@sss.pgh.pa.us        8034                 :         188987 :         eqQualHere &&
  396 pg@bowt.ie               8035         [ +  + ]:         188987 :         !found_array &&
 5968 tgl@sss.pgh.pa.us        8036         [ +  + ]:         183546 :         !found_is_null_op)
 7631                          8037                 :         183506 :         numIndexTuples = 1.0;
                               8038                 :                :     else
                               8039                 :                :     {
                               8040                 :                :         List       *selectivityQuals;
                               8041                 :                :         Selectivity btreeSelectivity;
                               8042                 :                : 
                               8043                 :                :         /*
                               8044                 :                :          * If the index is partial, AND the index predicate with the
                               8045                 :                :          * index-bound quals to produce a more accurate idea of the number of
                               8046                 :                :          * rows covered by the bound conditions.
                               8047                 :                :          */
 2636                          8048                 :         468837 :         selectivityQuals = add_predicate_to_index_quals(index, indexBoundQuals);
                               8049                 :                : 
 5210                          8050                 :         468837 :         btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
 7631                          8051                 :         468837 :                                                   index->rel->relid,
                               8052                 :                :                                                   JOIN_INNER,
                               8053                 :                :                                                   NULL);
                               8054                 :         468837 :         numIndexTuples = btreeSelectivity * index->rel->tuples;
                               8055                 :                : 
                               8056                 :                :         /*
                               8057                 :                :          * btree automatically combines individual array element primitive
                               8058                 :                :          * index scans whenever the tuples covered by the next set of array
                               8059                 :                :          * keys are close to tuples covered by the current set.  That puts a
                               8060                 :                :          * natural ceiling on the worst case number of descents -- there
                               8061                 :                :          * cannot possibly be more than one descent per leaf page scanned.
                               8062                 :                :          *
                               8063                 :                :          * Clamp the number of descents to at most 1/3 the number of index
                               8064                 :                :          * pages.  This avoids implausibly high estimates with low selectivity
                               8065                 :                :          * paths, where scans usually require only one or two descents.  This
                               8066                 :                :          * is most likely to help when there are several SAOP clauses, where
                               8067                 :                :          * naively accepting the total number of distinct combinations of
                               8068                 :                :          * array elements as the number of descents would frequently lead to
                               8069                 :                :          * wild overestimates.
                               8070                 :                :          *
                               8071                 :                :          * We somewhat arbitrarily don't just make the cutoff the total number
                               8072                 :                :          * of leaf pages (we make it 1/3 the total number of pages instead) to
                               8073                 :                :          * give the btree code credit for its ability to continue on the leaf
                               8074                 :                :          * level with low selectivity scans.
                               8075                 :                :          *
                               8076                 :                :          * Note: num_sa_scans includes both ScalarArrayOp array elements and
                               8077                 :                :          * skip array elements whose qual affects our numIndexTuples estimate.
                               8078                 :                :          */
  759 pg@bowt.ie               8079         [ +  + ]:         468837 :         num_sa_scans = Min(num_sa_scans, ceil(index->pages * 0.3333333));
                               8080         [ +  + ]:         468837 :         num_sa_scans = Max(num_sa_scans, 1);
                               8081                 :                : 
                               8082                 :                :         /*
                               8083                 :                :          * As in genericcostestimate(), we have to adjust for any array quals
                               8084                 :                :          * included in indexBoundQuals, and then round to integer.
                               8085                 :                :          *
                               8086                 :                :          * It is tempting to make genericcostestimate behave as if array
                               8087                 :                :          * clauses work in almost the same way as scalar operators during
                               8088                 :                :          * btree scans, making the top-level scan look like a continuous scan
                               8089                 :                :          * (as opposed to num_sa_scans-many primitive index scans).  After
                               8090                 :                :          * all, btree scans mostly work like that at runtime.  However, such a
                               8091                 :                :          * scheme would badly bias genericcostestimate's simplistic approach
                               8092                 :                :          * to calculating numIndexPages through prorating.
                               8093                 :                :          *
                               8094                 :                :          * Stick with the approach taken by non-native SAOP scans for now.
                               8095                 :                :          * genericcostestimate will use the Mackert-Lohman formula to
                               8096                 :                :          * compensate for repeat page fetches, even though that definitely
                               8097                 :                :          * won't happen during btree scans (not for leaf pages, at least).
                               8098                 :                :          * We're usually very pessimistic about the number of primitive index
                               8099                 :                :          * scans that will be required, but it's not clear how to do better.
                               8100                 :                :          */
 7081 tgl@sss.pgh.pa.us        8101                 :         468837 :         numIndexTuples = rint(numIndexTuples / num_sa_scans);
                               8102                 :                :     }
                               8103                 :                : 
                               8104                 :                :     /*
                               8105                 :                :      * Now do generic index cost estimation.
                               8106                 :                :      *
                               8107                 :                :      * While we expended effort to make realistic estimates of numIndexTuples
                               8108                 :                :      * and num_sa_scans, we are content to count only the btree metapage as
                               8109                 :                :      * non-leaf.  btree fanout is typically high enough that upper pages are
                               8110                 :                :      * few relative to leaf pages, so accounting for them would move the
                               8111                 :                :      * estimates at most a percent or two.  Given the uncertainty in just how
                               8112                 :                :      * many upper pages exist in a particular index, we'll skip trying to
                               8113                 :                :      * handle that.
                               8114                 :                :      */
 4862                          8115                 :         652343 :     costs.numIndexTuples = numIndexTuples;
  759 pg@bowt.ie               8116                 :         652343 :     costs.num_sa_scans = num_sa_scans;
   46 tgl@sss.pgh.pa.us        8117                 :GNC      652343 :     costs.numNonLeafPages = 1;
                               8118                 :                : 
 2636 tgl@sss.pgh.pa.us        8119                 :CBC      652343 :     genericcostestimate(root, path, loop_count, &costs);
                               8120                 :                : 
                               8121                 :                :     /*
                               8122                 :                :      * Add a CPU-cost component to represent the costs of initial btree
                               8123                 :                :      * descent.  We don't charge any I/O cost for touching upper btree levels,
                               8124                 :                :      * since they tend to stay in cache, but we still have to do about log2(N)
                               8125                 :                :      * comparisons to descend a btree of N leaf tuples.  We charge one
                               8126                 :                :      * cpu_operator_cost per comparison.
                               8127                 :                :      *
                               8128                 :                :      * If there are SAOP or skip array keys, charge this once per estimated
                               8129                 :                :      * index descent.  The ones after the first one are not startup cost so
                               8130                 :                :      * far as the overall plan goes, so just add them to "total" cost.
                               8131                 :                :      */
 4862                          8132         [ +  + ]:         652343 :     if (index->tuples > 1)        /* avoid computing log(0) */
                               8133                 :                :     {
                               8134                 :         591293 :         descentCost = ceil(log(index->tuples) / log(2.0)) * cpu_operator_cost;
                               8135                 :         591293 :         costs.indexStartupCost += descentCost;
                               8136                 :         591293 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
                               8137                 :                :     }
                               8138                 :                : 
                               8139                 :                :     /*
                               8140                 :                :      * Even though we're not charging I/O cost for touching upper btree pages,
                               8141                 :                :      * it's still reasonable to charge some CPU cost per page descended
                               8142                 :                :      * through.  Moreover, if we had no such charge at all, bloated indexes
                               8143                 :                :      * would appear to have the same search cost as unbloated ones, at least
                               8144                 :                :      * in cases where only a single leaf page is expected to be visited.  This
                               8145                 :                :      * cost is somewhat arbitrarily set at 50x cpu_operator_cost per page
                               8146                 :                :      * touched.  The number of such pages is btree tree height plus one (ie,
                               8147                 :                :      * we charge for the leaf page too).  As above, charge once per estimated
                               8148                 :                :      * SAOP/skip array descent.
                               8149                 :                :      */
 1213 akorotkov@postgresql     8150                 :         652343 :     descentCost = (index->tree_height + 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
 4862 tgl@sss.pgh.pa.us        8151                 :         652343 :     costs.indexStartupCost += descentCost;
                               8152                 :         652343 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
                               8153                 :                : 
  396 pg@bowt.ie               8154         [ +  + ]:         652343 :     if (!have_correlation)
                               8155                 :                :     {
                               8156                 :         625038 :         examine_indexcol_variable(root, index, 0, &vardata);
                               8157         [ +  + ]:         625038 :         if (HeapTupleIsValid(vardata.statsTuple))
                               8158                 :         448273 :             costs.indexCorrelation = btcost_correlation(index, &vardata);
                               8159         [ +  + ]:         625038 :         ReleaseVariableStats(vardata);
                               8160                 :                :     }
                               8161                 :                :     else
                               8162                 :                :     {
                               8163                 :                :         /* btcost_correlation already called earlier on */
                               8164                 :          27305 :         costs.indexCorrelation = correlation;
                               8165                 :                :     }
                               8166                 :                : 
 4862 tgl@sss.pgh.pa.us        8167                 :         652343 :     *indexStartupCost = costs.indexStartupCost;
                               8168                 :         652343 :     *indexTotalCost = costs.indexTotalCost;
                               8169                 :         652343 :     *indexSelectivity = costs.indexSelectivity;
                               8170                 :         652343 :     *indexCorrelation = costs.indexCorrelation;
 3366 rhaas@postgresql.org     8171                 :         652343 :     *indexPages = costs.numIndexPages;
10892 scrappy@hub.org          8172                 :         652343 : }
                               8173                 :                : 
                               8174                 :                : void
 3761 tgl@sss.pgh.pa.us        8175                 :            308 : hashcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
                               8176                 :                :                  Cost *indexStartupCost, Cost *indexTotalCost,
                               8177                 :                :                  Selectivity *indexSelectivity, double *indexCorrelation,
                               8178                 :                :                  double *indexPages)
                               8179                 :                : {
 1389 peter@eisentraut.org     8180                 :            308 :     GenericCosts costs = {0};
                               8181                 :                : 
                               8182                 :                :     /* As in btcostestimate, count only the metapage as non-leaf */
   46 tgl@sss.pgh.pa.us        8183                 :GNC         308 :     costs.numNonLeafPages = 1;
                               8184                 :                : 
 2636 tgl@sss.pgh.pa.us        8185                 :CBC         308 :     genericcostestimate(root, path, loop_count, &costs);
                               8186                 :                : 
                               8187                 :                :     /*
                               8188                 :                :      * A hash index has no descent costs as such, since the index AM can go
                               8189                 :                :      * directly to the target bucket after computing the hash value.  There
                               8190                 :                :      * are a couple of other hash-specific costs that we could conceivably add
                               8191                 :                :      * here, though:
                               8192                 :                :      *
                               8193                 :                :      * Ideally we'd charge spc_random_page_cost for each page in the target
                               8194                 :                :      * bucket, not just the numIndexPages pages that genericcostestimate
                               8195                 :                :      * thought we'd visit.  However in most cases we don't know which bucket
                               8196                 :                :      * that will be.  There's no point in considering the average bucket size
                               8197                 :                :      * because the hash AM makes sure that's always one page.
                               8198                 :                :      *
                               8199                 :                :      * Likewise, we could consider charging some CPU for each index tuple in
                               8200                 :                :      * the bucket, if we knew how many there were.  But the per-tuple cost is
                               8201                 :                :      * just a hash value comparison, not a general datatype-dependent
                               8202                 :                :      * comparison, so any such charge ought to be quite a bit less than
                               8203                 :                :      * cpu_operator_cost; which makes it probably not worth worrying about.
                               8204                 :                :      *
                               8205                 :                :      * A bigger issue is that chance hash-value collisions will result in
                               8206                 :                :      * wasted probes into the heap.  We don't currently attempt to model this
                               8207                 :                :      * cost on the grounds that it's rare, but maybe it's not rare enough.
                               8208                 :                :      * (Any fix for this ought to consider the generic lossy-operator problem,
                               8209                 :                :      * though; it's not entirely hash-specific.)
                               8210                 :                :      */
                               8211                 :                : 
 4862                          8212                 :            308 :     *indexStartupCost = costs.indexStartupCost;
                               8213                 :            308 :     *indexTotalCost = costs.indexTotalCost;
                               8214                 :            308 :     *indexSelectivity = costs.indexSelectivity;
                               8215                 :            308 :     *indexCorrelation = costs.indexCorrelation;
 3366 rhaas@postgresql.org     8216                 :            308 :     *indexPages = costs.numIndexPages;
10844 scrappy@hub.org          8217                 :            308 : }
                               8218                 :                : 
                               8219                 :                : void
 3761 tgl@sss.pgh.pa.us        8220                 :           4746 : gistcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
                               8221                 :                :                  Cost *indexStartupCost, Cost *indexTotalCost,
                               8222                 :                :                  Selectivity *indexSelectivity, double *indexCorrelation,
                               8223                 :                :                  double *indexPages)
                               8224                 :                : {
 4862                          8225                 :           4746 :     IndexOptInfo *index = path->indexinfo;
 1389 peter@eisentraut.org     8226                 :           4746 :     GenericCosts costs = {0};
                               8227                 :                :     Cost        descentCost;
                               8228                 :                : 
                               8229                 :                :     /* GiST has no metapage, so we treat all pages as leaf pages */
                               8230                 :                : 
 2636 tgl@sss.pgh.pa.us        8231                 :           4746 :     genericcostestimate(root, path, loop_count, &costs);
                               8232                 :                : 
                               8233                 :                :     /*
                               8234                 :                :      * We model index descent costs similarly to those for btree, but to do
                               8235                 :                :      * that we first need an idea of the tree height.  We somewhat arbitrarily
                               8236                 :                :      * assume that the fanout is 100, meaning the tree height is at most
                               8237                 :                :      * log100(index->pages).
                               8238                 :                :      *
                               8239                 :                :      * Although this computation isn't really expensive enough to require
                               8240                 :                :      * caching, we might as well use index->tree_height to cache it.
                               8241                 :                :      */
 4724 bruce@momjian.us         8242         [ +  + ]:           4746 :     if (index->tree_height < 0) /* unknown? */
                               8243                 :                :     {
 4862 tgl@sss.pgh.pa.us        8244         [ +  + ]:           4719 :         if (index->pages > 1) /* avoid computing log(0) */
                               8245                 :           1956 :             index->tree_height = (int) (log(index->pages) / log(100.0));
                               8246                 :                :         else
                               8247                 :           2763 :             index->tree_height = 0;
                               8248                 :                :     }
                               8249                 :                : 
                               8250                 :                :     /*
                               8251                 :                :      * Add a CPU-cost component to represent the costs of initial descent. We
                               8252                 :                :      * just use log(N) here not log2(N) since the branching factor isn't
                               8253                 :                :      * necessarily two anyway.  As for btree, charge once per SA scan.
                               8254                 :                :      */
                               8255         [ +  - ]:           4746 :     if (index->tuples > 1)        /* avoid computing log(0) */
                               8256                 :                :     {
                               8257                 :           4746 :         descentCost = ceil(log(index->tuples)) * cpu_operator_cost;
                               8258                 :           4746 :         costs.indexStartupCost += descentCost;
                               8259                 :           4746 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
                               8260                 :                :     }
                               8261                 :                : 
                               8262                 :                :     /*
                               8263                 :                :      * Likewise add a per-page charge, calculated the same as for btrees.
                               8264                 :                :      */
 1213 akorotkov@postgresql     8265                 :           4746 :     descentCost = (index->tree_height + 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
 4862 tgl@sss.pgh.pa.us        8266                 :           4746 :     costs.indexStartupCost += descentCost;
                               8267                 :           4746 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
                               8268                 :                : 
                               8269                 :           4746 :     *indexStartupCost = costs.indexStartupCost;
                               8270                 :           4746 :     *indexTotalCost = costs.indexTotalCost;
                               8271                 :           4746 :     *indexSelectivity = costs.indexSelectivity;
                               8272                 :           4746 :     *indexCorrelation = costs.indexCorrelation;
 3366 rhaas@postgresql.org     8273                 :           4746 :     *indexPages = costs.numIndexPages;
10844 scrappy@hub.org          8274                 :           4746 : }
                               8275                 :                : 
                               8276                 :                : void
 3761 tgl@sss.pgh.pa.us        8277                 :           1482 : spgcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
                               8278                 :                :                 Cost *indexStartupCost, Cost *indexTotalCost,
                               8279                 :                :                 Selectivity *indexSelectivity, double *indexCorrelation,
                               8280                 :                :                 double *indexPages)
                               8281                 :                : {
 4862                          8282                 :           1482 :     IndexOptInfo *index = path->indexinfo;
 1389 peter@eisentraut.org     8283                 :           1482 :     GenericCosts costs = {0};
                               8284                 :                :     Cost        descentCost;
                               8285                 :                : 
                               8286                 :                :     /* As in btcostestimate, count only the metapage as non-leaf */
   46 tgl@sss.pgh.pa.us        8287                 :GNC        1482 :     costs.numNonLeafPages = 1;
                               8288                 :                : 
 2636 tgl@sss.pgh.pa.us        8289                 :CBC        1482 :     genericcostestimate(root, path, loop_count, &costs);
                               8290                 :                : 
                               8291                 :                :     /*
                               8292                 :                :      * We model index descent costs similarly to those for btree, but to do
                               8293                 :                :      * that we first need an idea of the tree height.  We somewhat arbitrarily
                               8294                 :                :      * assume that the fanout is 100, meaning the tree height is at most
                               8295                 :                :      * log100(index->pages).
                               8296                 :                :      *
                               8297                 :                :      * Although this computation isn't really expensive enough to require
                               8298                 :                :      * caching, we might as well use index->tree_height to cache it.
                               8299                 :                :      */
 4724 bruce@momjian.us         8300         [ +  + ]:           1482 :     if (index->tree_height < 0) /* unknown? */
                               8301                 :                :     {
 4862 tgl@sss.pgh.pa.us        8302         [ +  - ]:           1477 :         if (index->pages > 1) /* avoid computing log(0) */
                               8303                 :           1477 :             index->tree_height = (int) (log(index->pages) / log(100.0));
                               8304                 :                :         else
 4862 tgl@sss.pgh.pa.us        8305                 :UBC           0 :             index->tree_height = 0;
                               8306                 :                :     }
                               8307                 :                : 
                               8308                 :                :     /*
                               8309                 :                :      * Add a CPU-cost component to represent the costs of initial descent. We
                               8310                 :                :      * just use log(N) here not log2(N) since the branching factor isn't
                               8311                 :                :      * necessarily two anyway.  As for btree, charge once per SA scan.
                               8312                 :                :      */
 4862 tgl@sss.pgh.pa.us        8313         [ +  - ]:CBC        1482 :     if (index->tuples > 1)        /* avoid computing log(0) */
                               8314                 :                :     {
                               8315                 :           1482 :         descentCost = ceil(log(index->tuples)) * cpu_operator_cost;
                               8316                 :           1482 :         costs.indexStartupCost += descentCost;
                               8317                 :           1482 :         costs.indexTotalCost += costs.num_sa_scans * descentCost;
                               8318                 :                :     }
                               8319                 :                : 
                               8320                 :                :     /*
                               8321                 :                :      * Likewise add a per-page charge, calculated the same as for btrees.
                               8322                 :                :      */
 1213 akorotkov@postgresql     8323                 :           1482 :     descentCost = (index->tree_height + 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
 4862 tgl@sss.pgh.pa.us        8324                 :           1482 :     costs.indexStartupCost += descentCost;
                               8325                 :           1482 :     costs.indexTotalCost += costs.num_sa_scans * descentCost;
                               8326                 :                : 
                               8327                 :           1482 :     *indexStartupCost = costs.indexStartupCost;
                               8328                 :           1482 :     *indexTotalCost = costs.indexTotalCost;
                               8329                 :           1482 :     *indexSelectivity = costs.indexSelectivity;
                               8330                 :           1482 :     *indexCorrelation = costs.indexCorrelation;
 3366 rhaas@postgresql.org     8331                 :           1482 :     *indexPages = costs.numIndexPages;
 5253 tgl@sss.pgh.pa.us        8332                 :           1482 : }
                               8333                 :                : 
                               8334                 :                : 
                               8335                 :                : /*
                               8336                 :                :  * Support routines for gincostestimate
                               8337                 :                :  */
                               8338                 :                : 
                               8339                 :                : typedef struct
                               8340                 :                : {
                               8341                 :                :     bool        attHasFullScan[INDEX_MAX_KEYS];
                               8342                 :                :     bool        attHasNormalScan[INDEX_MAX_KEYS];
                               8343                 :                :     double      partialEntries;
                               8344                 :                :     double      exactEntries;
                               8345                 :                :     double      searchEntries;
                               8346                 :                :     double      arrayScans;
                               8347                 :                : } GinQualCounts;
                               8348                 :                : 
                               8349                 :                : /*
                               8350                 :                :  * Estimate the number of index terms that need to be searched for while
                               8351                 :                :  * testing the given GIN query, and increment the counts in *counts
                               8352                 :                :  * appropriately.  If the query is unsatisfiable, return false.
                               8353                 :                :  */
                               8354                 :                : static bool
 5250                          8355                 :           1714 : gincost_pattern(IndexOptInfo *index, int indexcol,
                               8356                 :                :                 Oid clause_op, Datum query,
                               8357                 :                :                 GinQualCounts *counts)
                               8358                 :                : {
                               8359                 :                :     FmgrInfo    flinfo;
                               8360                 :                :     Oid         extractProcOid;
                               8361                 :                :     Oid         collation;
                               8362                 :                :     int         strategy_op;
                               8363                 :                :     Oid         lefttype,
                               8364                 :                :                 righttype;
                               8365                 :           1714 :     int32       nentries = 0;
                               8366                 :           1714 :     bool       *partial_matches = NULL;
                               8367                 :           1714 :     Pointer    *extra_data = NULL;
                               8368                 :           1714 :     bool       *nullFlags = NULL;
                               8369                 :           1714 :     int32       searchMode = GIN_SEARCH_MODE_DEFAULT;
                               8370                 :                :     int32       i;
                               8371                 :                : 
 2945 teodor@sigaev.ru         8372         [ -  + ]:           1714 :     Assert(indexcol < index->nkeycolumns);
                               8373                 :                : 
                               8374                 :                :     /*
                               8375                 :                :      * Get the operator's strategy number and declared input data types within
                               8376                 :                :      * the index opfamily.  (We don't need the latter, but we use
                               8377                 :                :      * get_op_opfamily_properties because it will throw error if it fails to
                               8378                 :                :      * find a matching pg_amop entry.)
                               8379                 :                :      */
 5250 tgl@sss.pgh.pa.us        8380                 :           1714 :     get_op_opfamily_properties(clause_op, index->opfamily[indexcol], false,
                               8381                 :                :                                &strategy_op, &lefttype, &righttype);
                               8382                 :                : 
                               8383                 :                :     /*
                               8384                 :                :      * GIN always uses the "default" support functions, which are those with
                               8385                 :                :      * lefttype == righttype == the opclass' opcintype (see
                               8386                 :                :      * IndexSupportInitialize in relcache.c).
                               8387                 :                :      */
                               8388                 :           1714 :     extractProcOid = get_opfamily_proc(index->opfamily[indexcol],
                               8389                 :           1714 :                                        index->opcintype[indexcol],
                               8390                 :           1714 :                                        index->opcintype[indexcol],
                               8391                 :                :                                        GIN_EXTRACTQUERY_PROC);
                               8392                 :                : 
                               8393         [ -  + ]:           1714 :     if (!OidIsValid(extractProcOid))
                               8394                 :                :     {
                               8395                 :                :         /* should not happen; throw same error as index_getprocinfo */
 5250 tgl@sss.pgh.pa.us        8396         [ #  # ]:UBC           0 :         elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
                               8397                 :                :              GIN_EXTRACTQUERY_PROC, indexcol + 1,
                               8398                 :                :              get_rel_name(index->indexoid));
                               8399                 :                :     }
                               8400                 :                : 
                               8401                 :                :     /*
                               8402                 :                :      * Choose collation to pass to extractProc (should match initGinState).
                               8403                 :                :      */
 4774 tgl@sss.pgh.pa.us        8404         [ +  + ]:CBC        1714 :     if (OidIsValid(index->indexcollations[indexcol]))
                               8405                 :            243 :         collation = index->indexcollations[indexcol];
                               8406                 :                :     else
                               8407                 :           1471 :         collation = DEFAULT_COLLATION_OID;
                               8408                 :                : 
 2227 akorotkov@postgresql     8409                 :           1714 :     fmgr_info(extractProcOid, &flinfo);
                               8410                 :                : 
                               8411                 :           1714 :     set_fn_opclass_options(&flinfo, index->opclassoptions[indexcol]);
                               8412                 :                : 
                               8413                 :           1714 :     FunctionCall7Coll(&flinfo,
                               8414                 :                :                       collation,
                               8415                 :                :                       query,
                               8416                 :                :                       PointerGetDatum(&nentries),
                               8417                 :                :                       UInt16GetDatum(strategy_op),
                               8418                 :                :                       PointerGetDatum(&partial_matches),
                               8419                 :                :                       PointerGetDatum(&extra_data),
                               8420                 :                :                       PointerGetDatum(&nullFlags),
                               8421                 :                :                       PointerGetDatum(&searchMode));
                               8422                 :                : 
 5250 tgl@sss.pgh.pa.us        8423   [ +  +  +  + ]:           1714 :     if (nentries <= 0 && searchMode == GIN_SEARCH_MODE_DEFAULT)
                               8424                 :                :     {
                               8425                 :                :         /* No match is possible */
                               8426                 :             10 :         return false;
                               8427                 :                :     }
                               8428                 :                : 
                               8429         [ +  + ]:           5854 :     for (i = 0; i < nentries; i++)
                               8430                 :                :     {
                               8431                 :                :         /*
                               8432                 :                :          * For partial match we haven't any information to estimate number of
                               8433                 :                :          * matched entries in index, so, we just estimate it as 100
                               8434                 :                :          */
                               8435   [ +  +  +  + ]:           4150 :         if (partial_matches && partial_matches[i])
                               8436                 :            361 :             counts->partialEntries += 100;
                               8437                 :                :         else
                               8438                 :           3789 :             counts->exactEntries++;
                               8439                 :                : 
                               8440                 :           4150 :         counts->searchEntries++;
                               8441                 :                :     }
                               8442                 :                : 
 2299 akorotkov@postgresql     8443         [ +  + ]:           1704 :     if (searchMode == GIN_SEARCH_MODE_DEFAULT)
                               8444                 :                :     {
                               8445                 :           1322 :         counts->attHasNormalScan[indexcol] = true;
                               8446                 :                :     }
                               8447         [ +  + ]:            382 :     else if (searchMode == GIN_SEARCH_MODE_INCLUDE_EMPTY)
                               8448                 :                :     {
                               8449                 :                :         /* Treat "include empty" like an exact-match item */
                               8450                 :             36 :         counts->attHasNormalScan[indexcol] = true;
 5250 tgl@sss.pgh.pa.us        8451                 :             36 :         counts->exactEntries++;
                               8452                 :             36 :         counts->searchEntries++;
                               8453                 :                :     }
                               8454                 :                :     else
                               8455                 :                :     {
                               8456                 :                :         /* It's GIN_SEARCH_MODE_ALL */
 2299 akorotkov@postgresql     8457                 :            346 :         counts->attHasFullScan[indexcol] = true;
                               8458                 :                :     }
                               8459                 :                : 
 5250 tgl@sss.pgh.pa.us        8460                 :           1704 :     return true;
                               8461                 :                : }
                               8462                 :                : 
                               8463                 :                : /*
                               8464                 :                :  * Estimate the number of index terms that need to be searched for while
                               8465                 :                :  * testing the given GIN index clause, and increment the counts in *counts
                               8466                 :                :  * appropriately.  If the query is unsatisfiable, return false.
                               8467                 :                :  */
                               8468                 :                : static bool
 4081                          8469                 :           1704 : gincost_opexpr(PlannerInfo *root,
                               8470                 :                :                IndexOptInfo *index,
                               8471                 :                :                int indexcol,
                               8472                 :                :                OpExpr *clause,
                               8473                 :                :                GinQualCounts *counts)
                               8474                 :                : {
 2636                          8475                 :           1704 :     Oid         clause_op = clause->opno;
                               8476                 :           1704 :     Node       *operand = (Node *) lsecond(clause->args);
                               8477                 :                : 
                               8478                 :                :     /* aggressively reduce to a constant, and look through relabeling */
 4456                          8479                 :           1704 :     operand = estimate_expression_value(root, operand);
                               8480                 :                : 
 5250                          8481         [ -  + ]:           1704 :     if (IsA(operand, RelabelType))
 5250 tgl@sss.pgh.pa.us        8482                 :UBC           0 :         operand = (Node *) ((RelabelType *) operand)->arg;
                               8483                 :                : 
                               8484                 :                :     /*
                               8485                 :                :      * It's impossible to call extractQuery method for unknown operand. So
                               8486                 :                :      * unless operand is a Const we can't do much; just assume there will be
                               8487                 :                :      * one ordinary search entry from the operand at runtime.
                               8488                 :                :      */
 5250 tgl@sss.pgh.pa.us        8489         [ -  + ]:CBC        1704 :     if (!IsA(operand, Const))
                               8490                 :                :     {
 5250 tgl@sss.pgh.pa.us        8491                 :UBC           0 :         counts->exactEntries++;
                               8492                 :              0 :         counts->searchEntries++;
                               8493                 :              0 :         return true;
                               8494                 :                :     }
                               8495                 :                : 
                               8496                 :                :     /* If Const is null, there can be no matches */
 5250 tgl@sss.pgh.pa.us        8497         [ -  + ]:CBC        1704 :     if (((Const *) operand)->constisnull)
 5250 tgl@sss.pgh.pa.us        8498                 :UBC           0 :         return false;
                               8499                 :                : 
                               8500                 :                :     /* Otherwise, apply extractQuery and get the actual term counts */
 5250 tgl@sss.pgh.pa.us        8501                 :CBC        1704 :     return gincost_pattern(index, indexcol, clause_op,
                               8502                 :                :                            ((Const *) operand)->constvalue,
                               8503                 :                :                            counts);
                               8504                 :                : }
                               8505                 :                : 
                               8506                 :                : /*
                               8507                 :                :  * Estimate the number of index terms that need to be searched for while
                               8508                 :                :  * testing the given GIN index clause, and increment the counts in *counts
                               8509                 :                :  * appropriately.  If the query is unsatisfiable, return false.
                               8510                 :                :  *
                               8511                 :                :  * A ScalarArrayOpExpr will give rise to N separate indexscans at runtime,
                               8512                 :                :  * each of which involves one value from the RHS array, plus all the
                               8513                 :                :  * non-array quals (if any).  To model this, we average the counts across
                               8514                 :                :  * the RHS elements, and add the averages to the counts in *counts (which
                               8515                 :                :  * correspond to per-indexscan costs).  We also multiply counts->arrayScans
                               8516                 :                :  * by N, causing gincostestimate to scale up its estimates accordingly.
                               8517                 :                :  */
                               8518                 :                : static bool
 4456                          8519                 :              5 : gincost_scalararrayopexpr(PlannerInfo *root,
                               8520                 :                :                           IndexOptInfo *index,
                               8521                 :                :                           int indexcol,
                               8522                 :                :                           ScalarArrayOpExpr *clause,
                               8523                 :                :                           double numIndexEntries,
                               8524                 :                :                           GinQualCounts *counts)
                               8525                 :                : {
 2636                          8526                 :              5 :     Oid         clause_op = clause->opno;
                               8527                 :              5 :     Node       *rightop = (Node *) lsecond(clause->args);
                               8528                 :                :     ArrayType  *arrayval;
                               8529                 :                :     int16       elmlen;
                               8530                 :                :     bool        elmbyval;
                               8531                 :                :     char        elmalign;
                               8532                 :                :     int         numElems;
                               8533                 :                :     Datum      *elemValues;
                               8534                 :                :     bool       *elemNulls;
                               8535                 :                :     GinQualCounts arraycounts;
 5250                          8536                 :              5 :     int         numPossible = 0;
                               8537                 :                :     int         i;
                               8538                 :                : 
 2636                          8539         [ -  + ]:              5 :     Assert(clause->useOr);
                               8540                 :                : 
                               8541                 :                :     /* aggressively reduce to a constant, and look through relabeling */
 4456                          8542                 :              5 :     rightop = estimate_expression_value(root, rightop);
                               8543                 :                : 
 5250                          8544         [ -  + ]:              5 :     if (IsA(rightop, RelabelType))
 5250 tgl@sss.pgh.pa.us        8545                 :UBC           0 :         rightop = (Node *) ((RelabelType *) rightop)->arg;
                               8546                 :                : 
                               8547                 :                :     /*
                               8548                 :                :      * It's impossible to call extractQuery method for unknown operand. So
                               8549                 :                :      * unless operand is a Const we can't do much; just assume there will be
                               8550                 :                :      * one ordinary search entry from each array entry at runtime, and fall
                               8551                 :                :      * back on a probably-bad estimate of the number of array entries.
                               8552                 :                :      */
 5250 tgl@sss.pgh.pa.us        8553         [ -  + ]:CBC           5 :     if (!IsA(rightop, Const))
                               8554                 :                :     {
 5250 tgl@sss.pgh.pa.us        8555                 :UBC           0 :         counts->exactEntries++;
                               8556                 :              0 :         counts->searchEntries++;
  852                          8557                 :              0 :         counts->arrayScans *= estimate_array_length(root, rightop);
 5250                          8558                 :              0 :         return true;
                               8559                 :                :     }
                               8560                 :                : 
                               8561                 :                :     /* If Const is null, there can be no matches */
 5250 tgl@sss.pgh.pa.us        8562         [ -  + ]:CBC           5 :     if (((Const *) rightop)->constisnull)
 5250 tgl@sss.pgh.pa.us        8563                 :UBC           0 :         return false;
                               8564                 :                : 
                               8565                 :                :     /* Otherwise, extract the array elements and iterate over them */
 5250 tgl@sss.pgh.pa.us        8566                 :CBC           5 :     arrayval = DatumGetArrayTypeP(((Const *) rightop)->constvalue);
                               8567                 :              5 :     get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
                               8568                 :                :                          &elmlen, &elmbyval, &elmalign);
                               8569                 :              5 :     deconstruct_array(arrayval,
                               8570                 :                :                       ARR_ELEMTYPE(arrayval),
                               8571                 :                :                       elmlen, elmbyval, elmalign,
                               8572                 :                :                       &elemValues, &elemNulls, &numElems);
                               8573                 :                : 
                               8574                 :              5 :     memset(&arraycounts, 0, sizeof(arraycounts));
                               8575                 :                : 
                               8576         [ +  + ]:             15 :     for (i = 0; i < numElems; i++)
                               8577                 :                :     {
                               8578                 :                :         GinQualCounts elemcounts;
                               8579                 :                : 
                               8580                 :                :         /* NULL can't match anything, so ignore, as the executor will */
                               8581         [ -  + ]:             10 :         if (elemNulls[i])
 5250 tgl@sss.pgh.pa.us        8582                 :UBC           0 :             continue;
                               8583                 :                : 
                               8584                 :                :         /* Otherwise, apply extractQuery and get the actual term counts */
 5250 tgl@sss.pgh.pa.us        8585                 :CBC          10 :         memset(&elemcounts, 0, sizeof(elemcounts));
                               8586                 :                : 
                               8587         [ +  - ]:             10 :         if (gincost_pattern(index, indexcol, clause_op, elemValues[i],
                               8588                 :                :                             &elemcounts))
                               8589                 :                :         {
                               8590                 :                :             /* We ignore array elements that are unsatisfiable patterns */
                               8591                 :             10 :             numPossible++;
                               8592                 :                : 
 2299 akorotkov@postgresql     8593         [ -  + ]:             10 :             if (elemcounts.attHasFullScan[indexcol] &&
 2299 akorotkov@postgresql     8594         [ #  # ]:UBC           0 :                 !elemcounts.attHasNormalScan[indexcol])
                               8595                 :                :             {
                               8596                 :                :                 /*
                               8597                 :                :                  * Full index scan will be required.  We treat this as if
                               8598                 :                :                  * every key in the index had been listed in the query; is
                               8599                 :                :                  * that reasonable?
                               8600                 :                :                  */
 5250 tgl@sss.pgh.pa.us        8601                 :              0 :                 elemcounts.partialEntries = 0;
                               8602                 :              0 :                 elemcounts.exactEntries = numIndexEntries;
                               8603                 :              0 :                 elemcounts.searchEntries = numIndexEntries;
                               8604                 :                :             }
 5250 tgl@sss.pgh.pa.us        8605                 :CBC          10 :             arraycounts.partialEntries += elemcounts.partialEntries;
                               8606                 :             10 :             arraycounts.exactEntries += elemcounts.exactEntries;
                               8607                 :             10 :             arraycounts.searchEntries += elemcounts.searchEntries;
                               8608                 :                :         }
                               8609                 :                :     }
                               8610                 :                : 
                               8611         [ -  + ]:              5 :     if (numPossible == 0)
                               8612                 :                :     {
                               8613                 :                :         /* No satisfiable patterns in the array */
 5250 tgl@sss.pgh.pa.us        8614                 :UBC           0 :         return false;
                               8615                 :                :     }
                               8616                 :                : 
                               8617                 :                :     /*
                               8618                 :                :      * Now add the averages to the global counts.  This will give us an
                               8619                 :                :      * estimate of the average number of terms searched for in each indexscan,
                               8620                 :                :      * including contributions from both array and non-array quals.
                               8621                 :                :      */
 5250 tgl@sss.pgh.pa.us        8622                 :CBC           5 :     counts->partialEntries += arraycounts.partialEntries / numPossible;
                               8623                 :              5 :     counts->exactEntries += arraycounts.exactEntries / numPossible;
                               8624                 :              5 :     counts->searchEntries += arraycounts.searchEntries / numPossible;
                               8625                 :                : 
                               8626                 :              5 :     counts->arrayScans *= numPossible;
                               8627                 :                : 
                               8628                 :              5 :     return true;
                               8629                 :                : }
                               8630                 :                : 
                               8631                 :                : /*
                               8632                 :                :  * GIN has search behavior completely different from other index types
                               8633                 :                :  */
                               8634                 :                : void
 3761                          8635                 :           1546 : gincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
                               8636                 :                :                 Cost *indexStartupCost, Cost *indexTotalCost,
                               8637                 :                :                 Selectivity *indexSelectivity, double *indexCorrelation,
                               8638                 :                :                 double *indexPages)
                               8639                 :                : {
 5246                          8640                 :           1546 :     IndexOptInfo *index = path->indexinfo;
 2636                          8641                 :           1546 :     List       *indexQuals = get_quals_from_indexclauses(path->indexclauses);
                               8642                 :                :     List       *selectivityQuals;
 5504 bruce@momjian.us         8643                 :           1546 :     double      numPages = index->pages,
                               8644                 :           1546 :                 numTuples = index->tuples;
                               8645                 :                :     double      numEntryPages,
                               8646                 :                :                 numDataPages,
                               8647                 :                :                 numPendingPages,
                               8648                 :                :                 numEntries;
                               8649                 :                :     GinQualCounts counts;
                               8650                 :                :     bool        matchPossible;
                               8651                 :                :     bool        fullIndexScan;
                               8652                 :                :     double      partialScale;
                               8653                 :                :     double      entryPagesFetched,
                               8654                 :                :                 dataPagesFetched,
                               8655                 :                :                 dataPagesFetchedBySel;
                               8656                 :                :     double      qual_op_cost,
                               8657                 :                :                 qual_arg_cost,
                               8658                 :                :                 spc_random_page_cost,
                               8659                 :                :                 outer_scans;
                               8660                 :                :     Cost        descentCost;
                               8661                 :                :     Relation    indexRel;
                               8662                 :                :     GinStatsData ginStats;
                               8663                 :                :     ListCell   *lc;
                               8664                 :                :     int         i;
                               8665                 :                : 
                               8666                 :                :     /*
                               8667                 :                :      * Obtain statistical information from the meta page, if possible.  Else
                               8668                 :                :      * set ginStats to zeroes, and we'll cope below.
                               8669                 :                :      */
 3808 tgl@sss.pgh.pa.us        8670         [ +  - ]:           1546 :     if (!index->hypothetical)
                               8671                 :                :     {
                               8672                 :                :         /* Lock should have already been obtained in plancat.c */
 2588                          8673                 :           1546 :         indexRel = index_open(index->indexoid, NoLock);
 3808                          8674                 :           1546 :         ginGetStats(indexRel, &ginStats);
 2588                          8675                 :           1546 :         index_close(indexRel, NoLock);
                               8676                 :                :     }
                               8677                 :                :     else
                               8678                 :                :     {
 3808 tgl@sss.pgh.pa.us        8679                 :UBC           0 :         memset(&ginStats, 0, sizeof(ginStats));
                               8680                 :                :     }
                               8681                 :                : 
                               8682                 :                :     /*
                               8683                 :                :      * Assuming we got valid (nonzero) stats at all, nPendingPages can be
                               8684                 :                :      * trusted, but the other fields are data as of the last VACUUM.  We can
                               8685                 :                :      * scale them up to account for growth since then, but that method only
                               8686                 :                :      * goes so far; in the worst case, the stats might be for a completely
                               8687                 :                :      * empty index, and scaling them will produce pretty bogus numbers.
                               8688                 :                :      * Somewhat arbitrarily, set the cutoff for doing scaling at 4X growth; if
                               8689                 :                :      * it's grown more than that, fall back to estimating things only from the
                               8690                 :                :      * assumed-accurate index size.  But we'll trust nPendingPages in any case
                               8691                 :                :      * so long as it's not clearly insane, ie, more than the index size.
                               8692                 :                :      */
 3777 tgl@sss.pgh.pa.us        8693         [ +  - ]:CBC        1546 :     if (ginStats.nPendingPages < numPages)
                               8694                 :           1546 :         numPendingPages = ginStats.nPendingPages;
                               8695                 :                :     else
 3777 tgl@sss.pgh.pa.us        8696                 :UBC           0 :         numPendingPages = 0;
                               8697                 :                : 
 3777 tgl@sss.pgh.pa.us        8698   [ +  -  +  - ]:CBC        1546 :     if (numPages > 0 && ginStats.nTotalPages <= numPages &&
                               8699         [ +  + ]:           1546 :         ginStats.nTotalPages > numPages / 4 &&
                               8700   [ +  -  +  + ]:           1506 :         ginStats.nEntryPages > 0 && ginStats.nEntries > 0)
 3808                          8701                 :           1292 :     {
                               8702                 :                :         /*
                               8703                 :                :          * OK, the stats seem close enough to sane to be trusted.  But we
                               8704                 :                :          * still need to scale them by the ratio numPages / nTotalPages to
                               8705                 :                :          * account for growth since the last VACUUM.
                               8706                 :                :          */
 5504 bruce@momjian.us         8707                 :           1292 :         double      scale = numPages / ginStats.nTotalPages;
                               8708                 :                : 
 3777 tgl@sss.pgh.pa.us        8709                 :           1292 :         numEntryPages = ceil(ginStats.nEntryPages * scale);
                               8710                 :           1292 :         numDataPages = ceil(ginStats.nDataPages * scale);
                               8711                 :           1292 :         numEntries = ceil(ginStats.nEntries * scale);
                               8712                 :                :         /* ensure we didn't round up too much */
                               8713         [ +  + ]:           1292 :         numEntryPages = Min(numEntryPages, numPages - numPendingPages);
                               8714         [ +  + ]:           1292 :         numDataPages = Min(numDataPages,
                               8715                 :                :                            numPages - numPendingPages - numEntryPages);
                               8716                 :                :     }
                               8717                 :                :     else
                               8718                 :                :     {
                               8719                 :                :         /*
                               8720                 :                :          * We might get here because it's a hypothetical index, or an index
                               8721                 :                :          * created pre-9.1 and never vacuumed since upgrading (in which case
                               8722                 :                :          * its stats would read as zeroes), or just because it's grown too
                               8723                 :                :          * much since the last VACUUM for us to put our faith in scaling.
                               8724                 :                :          *
                               8725                 :                :          * Invent some plausible internal statistics based on the index page
                               8726                 :                :          * count (and clamp that to at least 10 pages, just in case).  We
                               8727                 :                :          * estimate that 90% of the index is entry pages, and the rest is data
                               8728                 :                :          * pages.  Estimate 100 entries per entry page; this is rather bogus
                               8729                 :                :          * since it'll depend on the size of the keys, but it's more robust
                               8730                 :                :          * than trying to predict the number of entries per heap tuple.
                               8731                 :                :          */
 3808                          8732         [ +  + ]:            254 :         numPages = Max(numPages, 10);
 3777                          8733                 :            254 :         numEntryPages = floor((numPages - numPendingPages) * 0.90);
                               8734                 :            254 :         numDataPages = numPages - numPendingPages - numEntryPages;
 3808                          8735                 :            254 :         numEntries = floor(numEntryPages * 100);
                               8736                 :                :     }
                               8737                 :                : 
                               8738                 :                :     /* In an empty index, numEntries could be zero.  Avoid divide-by-zero */
 5493                          8739         [ -  + ]:           1546 :     if (numEntries < 1)
 5493 tgl@sss.pgh.pa.us        8740                 :UBC           0 :         numEntries = 1;
                               8741                 :                : 
                               8742                 :                :     /*
                               8743                 :                :      * If the index is partial, AND the index predicate with the index-bound
                               8744                 :                :      * quals to produce a more accurate idea of the number of rows covered by
                               8745                 :                :      * the bound conditions.
                               8746                 :                :      */
 2636 tgl@sss.pgh.pa.us        8747                 :CBC        1546 :     selectivityQuals = add_predicate_to_index_quals(index, indexQuals);
                               8748                 :                : 
                               8749                 :                :     /* Estimate the fraction of main-table tuples that will be visited */
 5679                          8750                 :           3092 :     *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
 5504 bruce@momjian.us         8751                 :           1546 :                                                index->rel->relid,
                               8752                 :                :                                                JOIN_INNER,
                               8753                 :                :                                                NULL);
                               8754                 :                : 
                               8755                 :                :     /* fetch estimated page cost for tablespace containing index */
                               8756                 :           1546 :     get_tablespace_page_costs(index->reltablespace,
                               8757                 :                :                               &spc_random_page_cost,
                               8758                 :                :                               NULL);
                               8759                 :                : 
                               8760                 :                :     /*
                               8761                 :                :      * Generic assumption about index correlation: there isn't any.
                               8762                 :                :      */
 5679 tgl@sss.pgh.pa.us        8763                 :           1546 :     *indexCorrelation = 0.0;
                               8764                 :                : 
                               8765                 :                :     /*
                               8766                 :                :      * Examine quals to estimate number of search entries & partial matches
                               8767                 :                :      */
 5250                          8768                 :           1546 :     memset(&counts, 0, sizeof(counts));
                               8769                 :           1546 :     counts.arrayScans = 1;
                               8770                 :           1546 :     matchPossible = true;
                               8771                 :                : 
 2636                          8772   [ +  -  +  +  :           3255 :     foreach(lc, path->indexclauses)
                                              +  + ]
                               8773                 :                :     {
                               8774                 :           1709 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
                               8775                 :                :         ListCell   *lc2;
                               8776                 :                : 
                               8777   [ +  -  +  +  :           3408 :         foreach(lc2, iclause->indexquals)
                                              +  + ]
                               8778                 :                :         {
                               8779                 :           1709 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
                               8780                 :           1709 :             Expr       *clause = rinfo->clause;
                               8781                 :                : 
                               8782         [ +  + ]:           1709 :             if (IsA(clause, OpExpr))
                               8783                 :                :             {
                               8784                 :           1704 :                 matchPossible = gincost_opexpr(root,
                               8785                 :                :                                                index,
                               8786                 :           1704 :                                                iclause->indexcol,
                               8787                 :                :                                                (OpExpr *) clause,
                               8788                 :                :                                                &counts);
                               8789         [ +  + ]:           1704 :                 if (!matchPossible)
                               8790                 :             10 :                     break;
                               8791                 :                :             }
                               8792         [ +  - ]:              5 :             else if (IsA(clause, ScalarArrayOpExpr))
                               8793                 :                :             {
                               8794                 :              5 :                 matchPossible = gincost_scalararrayopexpr(root,
                               8795                 :                :                                                           index,
                               8796                 :              5 :                                                           iclause->indexcol,
                               8797                 :                :                                                           (ScalarArrayOpExpr *) clause,
                               8798                 :                :                                                           numEntries,
                               8799                 :                :                                                           &counts);
                               8800         [ -  + ]:              5 :                 if (!matchPossible)
 2636 tgl@sss.pgh.pa.us        8801                 :UBC           0 :                     break;
                               8802                 :                :             }
                               8803                 :                :             else
                               8804                 :                :             {
                               8805                 :                :                 /* shouldn't be anything else for a GIN index */
                               8806         [ #  # ]:              0 :                 elog(ERROR, "unsupported GIN indexqual type: %d",
                               8807                 :                :                      (int) nodeTag(clause));
                               8808                 :                :             }
                               8809                 :                :         }
                               8810                 :                :     }
                               8811                 :                : 
                               8812                 :                :     /* Fall out if there were any provably-unsatisfiable quals */
 5250 tgl@sss.pgh.pa.us        8813         [ +  + ]:CBC        1546 :     if (!matchPossible)
                               8814                 :                :     {
                               8815                 :             10 :         *indexStartupCost = 0;
                               8816                 :             10 :         *indexTotalCost = 0;
                               8817                 :             10 :         *indexSelectivity = 0;
 3761                          8818                 :             10 :         return;
                               8819                 :                :     }
                               8820                 :                : 
                               8821                 :                :     /*
                               8822                 :                :      * If attribute has a full scan and at the same time doesn't have normal
                               8823                 :                :      * scan, then we'll have to scan all non-null entries of that attribute.
                               8824                 :                :      * Currently, we don't have per-attribute statistics for GIN.  Thus, we
                               8825                 :                :      * must assume the whole GIN index has to be scanned in this case.
                               8826                 :                :      */
 2299 akorotkov@postgresql     8827                 :           1536 :     fullIndexScan = false;
                               8828         [ +  + ]:           2989 :     for (i = 0; i < index->nkeycolumns; i++)
                               8829                 :                :     {
                               8830   [ +  +  +  + ]:           1726 :         if (counts.attHasFullScan[i] && !counts.attHasNormalScan[i])
                               8831                 :                :         {
                               8832                 :            273 :             fullIndexScan = true;
                               8833                 :            273 :             break;
                               8834                 :                :         }
                               8835                 :                :     }
                               8836                 :                : 
                               8837   [ +  +  -  + ]:           1536 :     if (fullIndexScan || indexQuals == NIL)
                               8838                 :                :     {
                               8839                 :                :         /*
                               8840                 :                :          * Full index scan will be required.  We treat this as if every key in
                               8841                 :                :          * the index had been listed in the query; is that reasonable?
                               8842                 :                :          */
 5250 tgl@sss.pgh.pa.us        8843                 :            273 :         counts.partialEntries = 0;
                               8844                 :            273 :         counts.exactEntries = numEntries;
                               8845                 :            273 :         counts.searchEntries = numEntries;
                               8846                 :                :     }
                               8847                 :                : 
                               8848                 :                :     /* Will we have more than one iteration of a nestloop scan? */
 5212                          8849                 :           1536 :     outer_scans = loop_count;
                               8850                 :                : 
                               8851                 :                :     /*
                               8852                 :                :      * Compute cost to begin scan, first of all, pay attention to pending
                               8853                 :                :      * list.
                               8854                 :                :      */
 5679                          8855                 :           1536 :     entryPagesFetched = numPendingPages;
                               8856                 :                : 
                               8857                 :                :     /*
                               8858                 :                :      * Estimate number of entry pages read.  We need to do
                               8859                 :                :      * counts.searchEntries searches.  Use a power function as it should be,
                               8860                 :                :      * but tuples on leaf pages usually is much greater. Here we include all
                               8861                 :                :      * searches in entry tree, including search of first entry in partial
                               8862                 :                :      * match algorithm
                               8863                 :                :      */
 5250                          8864                 :           1536 :     entryPagesFetched += ceil(counts.searchEntries * rint(pow(numEntryPages, 0.15)));
                               8865                 :                : 
                               8866                 :                :     /*
                               8867                 :                :      * Add an estimate of entry pages read by partial match algorithm. It's a
                               8868                 :                :      * scan over leaf pages in entry tree.  We haven't any useful stats here,
                               8869                 :                :      * so estimate it as proportion.  Because counts.partialEntries is really
                               8870                 :                :      * pretty bogus (see code above), it's possible that it is more than
                               8871                 :                :      * numEntries; clamp the proportion to ensure sanity.
                               8872                 :                :      */
 3777                          8873                 :           1536 :     partialScale = counts.partialEntries / numEntries;
                               8874         [ +  + ]:           1536 :     partialScale = Min(partialScale, 1.0);
                               8875                 :                : 
                               8876                 :           1536 :     entryPagesFetched += ceil(numEntryPages * partialScale);
                               8877                 :                : 
                               8878                 :                :     /*
                               8879                 :                :      * Partial match algorithm reads all data pages before doing actual scan,
                               8880                 :                :      * so it's a startup cost.  Again, we haven't any useful stats here, so
                               8881                 :                :      * estimate it as proportion.
                               8882                 :                :      */
                               8883                 :           1536 :     dataPagesFetched = ceil(numDataPages * partialScale);
                               8884                 :                : 
 1213 akorotkov@postgresql     8885                 :           1536 :     *indexStartupCost = 0;
                               8886                 :           1536 :     *indexTotalCost = 0;
                               8887                 :                : 
                               8888                 :                :     /*
                               8889                 :                :      * Add a CPU-cost component to represent the costs of initial entry btree
                               8890                 :                :      * descent.  We don't charge any I/O cost for touching upper btree levels,
                               8891                 :                :      * since they tend to stay in cache, but we still have to do about log2(N)
                               8892                 :                :      * comparisons to descend a btree of N leaf tuples.  We charge one
                               8893                 :                :      * cpu_operator_cost per comparison.
                               8894                 :                :      *
                               8895                 :                :      * If there are ScalarArrayOpExprs, charge this once per SA scan.  The
                               8896                 :                :      * ones after the first one are not startup cost so far as the overall
                               8897                 :                :      * plan is concerned, so add them only to "total" cost.
                               8898                 :                :      */
                               8899         [ +  - ]:           1536 :     if (numEntries > 1)          /* avoid computing log(0) */
                               8900                 :                :     {
                               8901                 :           1536 :         descentCost = ceil(log(numEntries) / log(2.0)) * cpu_operator_cost;
                               8902                 :           1536 :         *indexStartupCost += descentCost * counts.searchEntries;
                               8903                 :           1536 :         *indexTotalCost += counts.arrayScans * descentCost * counts.searchEntries;
                               8904                 :                :     }
                               8905                 :                : 
                               8906                 :                :     /*
                               8907                 :                :      * Add a cpu cost per entry-page fetched. This is not amortized over a
                               8908                 :                :      * loop.
                               8909                 :                :      */
                               8910                 :           1536 :     *indexStartupCost += entryPagesFetched * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
                               8911                 :           1536 :     *indexTotalCost += entryPagesFetched * counts.arrayScans * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
                               8912                 :                : 
                               8913                 :                :     /*
                               8914                 :                :      * Add a cpu cost per data-page fetched. This is also not amortized over a
                               8915                 :                :      * loop. Since those are the data pages from the partial match algorithm,
                               8916                 :                :      * charge them as startup cost.
                               8917                 :                :      */
                               8918                 :           1536 :     *indexStartupCost += DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost * dataPagesFetched;
                               8919                 :                : 
                               8920                 :                :     /*
                               8921                 :                :      * Since we add the startup cost to the total cost later on, remove the
                               8922                 :                :      * initial arrayscan from the total.
                               8923                 :                :      */
                               8924                 :           1536 :     *indexTotalCost += dataPagesFetched * (counts.arrayScans - 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
                               8925                 :                : 
                               8926                 :                :     /*
                               8927                 :                :      * Calculate cache effects if more than one scan due to nestloops or array
                               8928                 :                :      * quals.  The result is pro-rated per nestloop scan, but the array qual
                               8929                 :                :      * factor shouldn't be pro-rated (compare genericcostestimate).
                               8930                 :                :      */
 5250 tgl@sss.pgh.pa.us        8931   [ +  -  +  + ]:           1536 :     if (outer_scans > 1 || counts.arrayScans > 1)
                               8932                 :                :     {
                               8933                 :              5 :         entryPagesFetched *= outer_scans * counts.arrayScans;
 5679                          8934                 :              5 :         entryPagesFetched = index_pages_fetched(entryPagesFetched,
                               8935                 :                :                                                 (BlockNumber) numEntryPages,
                               8936                 :                :                                                 numEntryPages, root);
 5250                          8937                 :              5 :         entryPagesFetched /= outer_scans;
                               8938                 :              5 :         dataPagesFetched *= outer_scans * counts.arrayScans;
 5679                          8939                 :              5 :         dataPagesFetched = index_pages_fetched(dataPagesFetched,
                               8940                 :                :                                                (BlockNumber) numDataPages,
                               8941                 :                :                                                numDataPages, root);
 5250                          8942                 :              5 :         dataPagesFetched /= outer_scans;
                               8943                 :                :     }
                               8944                 :                : 
                               8945                 :                :     /*
                               8946                 :                :      * Here we use random page cost because logically-close pages could be far
                               8947                 :                :      * apart on disk.
                               8948                 :                :      */
 1213 akorotkov@postgresql     8949                 :           1536 :     *indexStartupCost += (entryPagesFetched + dataPagesFetched) * spc_random_page_cost;
                               8950                 :                : 
                               8951                 :                :     /*
                               8952                 :                :      * Now compute the number of data pages fetched during the scan.
                               8953                 :                :      *
                               8954                 :                :      * We assume every entry to have the same number of items, and that there
                               8955                 :                :      * is no overlap between them. (XXX: tsvector and array opclasses collect
                               8956                 :                :      * statistics on the frequency of individual keys; it would be nice to use
                               8957                 :                :      * those here.)
                               8958                 :                :      */
 5250 tgl@sss.pgh.pa.us        8959                 :           1536 :     dataPagesFetched = ceil(numDataPages * counts.exactEntries / numEntries);
                               8960                 :                : 
                               8961                 :                :     /*
                               8962                 :                :      * If there is a lot of overlap among the entries, in particular if one of
                               8963                 :                :      * the entries is very frequent, the above calculation can grossly
                               8964                 :                :      * under-estimate.  As a simple cross-check, calculate a lower bound based
                               8965                 :                :      * on the overall selectivity of the quals.  At a minimum, we must read
                               8966                 :                :      * one item pointer for each matching entry.
                               8967                 :                :      *
                               8968                 :                :      * The width of each item pointer varies, based on the level of
                               8969                 :                :      * compression.  We don't have statistics on that, but an average of
                               8970                 :                :      * around 3 bytes per item is fairly typical.
                               8971                 :                :      */
 5679                          8972                 :           1536 :     dataPagesFetchedBySel = ceil(*indexSelectivity *
 4437 heikki.linnakangas@i     8973                 :           1536 :                                  (numTuples / (BLCKSZ / 3)));
 5679 tgl@sss.pgh.pa.us        8974         [ +  + ]:           1536 :     if (dataPagesFetchedBySel > dataPagesFetched)
                               8975                 :           1243 :         dataPagesFetched = dataPagesFetchedBySel;
                               8976                 :                : 
                               8977                 :                :     /* Add one page cpu-cost to the startup cost */
 1213 akorotkov@postgresql     8978                 :           1536 :     *indexStartupCost += DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost * counts.searchEntries;
                               8979                 :                : 
                               8980                 :                :     /*
                               8981                 :                :      * Add once again a CPU-cost for those data pages, before amortizing for
                               8982                 :                :      * cache.
                               8983                 :                :      */
                               8984                 :           1536 :     *indexTotalCost += dataPagesFetched * counts.arrayScans * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
                               8985                 :                : 
                               8986                 :                :     /* Account for cache effects, the same as above */
 5250 tgl@sss.pgh.pa.us        8987   [ +  -  +  + ]:           1536 :     if (outer_scans > 1 || counts.arrayScans > 1)
                               8988                 :                :     {
                               8989                 :              5 :         dataPagesFetched *= outer_scans * counts.arrayScans;
 5679                          8990                 :              5 :         dataPagesFetched = index_pages_fetched(dataPagesFetched,
                               8991                 :                :                                                (BlockNumber) numDataPages,
                               8992                 :                :                                                numDataPages, root);
 5250                          8993                 :              5 :         dataPagesFetched /= outer_scans;
                               8994                 :                :     }
                               8995                 :                : 
                               8996                 :                :     /* And apply random_page_cost as the cost per page */
 1213 akorotkov@postgresql     8997                 :           1536 :     *indexTotalCost += *indexStartupCost +
 5679 tgl@sss.pgh.pa.us        8998                 :           1536 :         dataPagesFetched * spc_random_page_cost;
                               8999                 :                : 
                               9000                 :                :     /*
                               9001                 :                :      * Add on index qual eval costs, much as in genericcostestimate. We charge
                               9002                 :                :      * cpu but we can disregard indexorderbys, since GIN doesn't support
                               9003                 :                :      * those.
                               9004                 :                :      */
 2636                          9005                 :           1536 :     qual_arg_cost = index_other_operands_eval_cost(root, indexQuals);
                               9006                 :           1536 :     qual_op_cost = cpu_operator_cost * list_length(indexQuals);
                               9007                 :                : 
 5679                          9008                 :           1536 :     *indexStartupCost += qual_arg_cost;
                               9009                 :           1536 :     *indexTotalCost += qual_arg_cost;
                               9010                 :                : 
                               9011                 :                :     /*
                               9012                 :                :      * Add a cpu cost per search entry, corresponding to the actual visited
                               9013                 :                :      * entries.
                               9014                 :                :      */
 1213 akorotkov@postgresql     9015                 :           1536 :     *indexTotalCost += (counts.searchEntries * counts.arrayScans) * (qual_op_cost);
                               9016                 :                :     /* Now add a cpu cost per tuple in the posting lists / trees */
                               9017                 :           1536 :     *indexTotalCost += (numTuples * *indexSelectivity) * (cpu_index_tuple_cost);
 3366 rhaas@postgresql.org     9018                 :           1536 :     *indexPages = dataPagesFetched;
                               9019                 :                : }
                               9020                 :                : 
                               9021                 :                : /*
                               9022                 :                :  * BRIN has search behavior completely different from other index types
                               9023                 :                :  */
                               9024                 :                : void
 3761 tgl@sss.pgh.pa.us        9025                 :           8944 : brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
                               9026                 :                :                  Cost *indexStartupCost, Cost *indexTotalCost,
                               9027                 :                :                  Selectivity *indexSelectivity, double *indexCorrelation,
                               9028                 :                :                  double *indexPages)
                               9029                 :                : {
 4197 alvherre@alvh.no-ip.     9030                 :           8944 :     IndexOptInfo *index = path->indexinfo;
 2636 tgl@sss.pgh.pa.us        9031                 :           8944 :     List       *indexQuals = get_quals_from_indexclauses(path->indexclauses);
 4197 alvherre@alvh.no-ip.     9032                 :           8944 :     double      numPages = index->pages;
 3316                          9033                 :           8944 :     RelOptInfo *baserel = index->rel;
                               9034         [ +  - ]:           8944 :     RangeTblEntry *rte = planner_rt_fetch(baserel->relid, root);
                               9035                 :                :     Cost        spc_seq_page_cost;
                               9036                 :                :     Cost        spc_random_page_cost;
                               9037                 :                :     double      qual_arg_cost;
                               9038                 :                :     double      qualSelectivity;
                               9039                 :                :     BrinStatsData statsData;
                               9040                 :                :     double      indexRanges;
                               9041                 :                :     double      minimalRanges;
                               9042                 :                :     double      estimatedRanges;
                               9043                 :                :     double      selec;
                               9044                 :                :     Relation    indexRel;
                               9045                 :                :     ListCell   *l;
                               9046                 :                :     VariableStatData vardata;
                               9047                 :                : 
                               9048         [ -  + ]:           8944 :     Assert(rte->rtekind == RTE_RELATION);
                               9049                 :                : 
                               9050                 :                :     /* fetch estimated page cost for the tablespace containing the index */
 4197                          9051                 :           8944 :     get_tablespace_page_costs(index->reltablespace,
                               9052                 :                :                               &spc_random_page_cost,
                               9053                 :                :                               &spc_seq_page_cost);
                               9054                 :                : 
                               9055                 :                :     /*
                               9056                 :                :      * Obtain some data from the index itself, if possible.  Otherwise invent
                               9057                 :                :      * some plausible internal statistics based on the relation page count.
                               9058                 :                :      */
 2357 michael@paquier.xyz      9059         [ +  - ]:           8944 :     if (!index->hypothetical)
                               9060                 :                :     {
                               9061                 :                :         /*
                               9062                 :                :          * A lock should have already been obtained on the index in plancat.c.
                               9063                 :                :          */
                               9064                 :           8944 :         indexRel = index_open(index->indexoid, NoLock);
                               9065                 :           8944 :         brinGetStats(indexRel, &statsData);
                               9066                 :           8944 :         index_close(indexRel, NoLock);
                               9067                 :                : 
                               9068                 :                :         /* work out the actual number of ranges in the index */
                               9069         [ +  + ]:           8944 :         indexRanges = Max(ceil((double) baserel->pages /
                               9070                 :                :                                statsData.pagesPerRange), 1.0);
                               9071                 :                :     }
                               9072                 :                :     else
                               9073                 :                :     {
                               9074                 :                :         /*
                               9075                 :                :          * Assume default number of pages per range, and estimate the number
                               9076                 :                :          * of ranges based on that.
                               9077                 :                :          */
 2357 michael@paquier.xyz      9078         [ #  # ]:UBC           0 :         indexRanges = Max(ceil((double) baserel->pages /
                               9079                 :                :                                BRIN_DEFAULT_PAGES_PER_RANGE), 1.0);
                               9080                 :                : 
                               9081                 :              0 :         statsData.pagesPerRange = BRIN_DEFAULT_PAGES_PER_RANGE;
                               9082                 :              0 :         statsData.revmapNumPages = (indexRanges / REVMAP_PAGE_MAXITEMS) + 1;
                               9083                 :                :     }
                               9084                 :                : 
                               9085                 :                :     /*
                               9086                 :                :      * Compute index correlation
                               9087                 :                :      *
                               9088                 :                :      * Because we can use all index quals equally when scanning, we can use
                               9089                 :                :      * the largest correlation (in absolute value) among columns used by the
                               9090                 :                :      * query.  Start at zero, the worst possible case.  If we cannot find any
                               9091                 :                :      * correlation statistics, we will keep it as 0.
                               9092                 :                :      */
 3316 alvherre@alvh.no-ip.     9093                 :CBC        8944 :     *indexCorrelation = 0;
                               9094                 :                : 
 2636 tgl@sss.pgh.pa.us        9095   [ +  -  +  +  :          17889 :     foreach(l, path->indexclauses)
                                              +  + ]
                               9096                 :                :     {
                               9097                 :           8945 :         IndexClause *iclause = lfirst_node(IndexClause, l);
                               9098                 :           8945 :         AttrNumber  attnum = index->indexkeys[iclause->indexcol];
                               9099                 :                : 
                               9100                 :                :         /* attempt to lookup stats in relation for this index column */
 3316 alvherre@alvh.no-ip.     9101         [ +  - ]:           8945 :         if (attnum != 0)
                               9102                 :                :         {
                               9103                 :                :             /* Simple variable -- look to stats for the underlying table */
                               9104   [ -  +  -  - ]:           8945 :             if (get_relation_stats_hook &&
 3316 alvherre@alvh.no-ip.     9105                 :UBC           0 :                 (*get_relation_stats_hook) (root, rte, attnum, &vardata))
                               9106                 :                :             {
                               9107                 :                :                 /*
                               9108                 :                :                  * The hook took control of acquiring a stats tuple.  If it
                               9109                 :                :                  * did supply a tuple, it'd better have supplied a freefunc.
                               9110                 :                :                  */
                               9111   [ #  #  #  # ]:              0 :                 if (HeapTupleIsValid(vardata.statsTuple) && !vardata.freefunc)
                               9112         [ #  # ]:              0 :                     elog(ERROR,
                               9113                 :                :                          "no function provided to release variable stats with");
                               9114                 :                :             }
                               9115                 :                :             else
                               9116                 :                :             {
 3316 alvherre@alvh.no-ip.     9117                 :CBC        8945 :                 vardata.statsTuple =
                               9118                 :           8945 :                     SearchSysCache3(STATRELATTINH,
                               9119                 :                :                                     ObjectIdGetDatum(rte->relid),
                               9120                 :                :                                     Int16GetDatum(attnum),
                               9121                 :                :                                     BoolGetDatum(false));
                               9122                 :           8945 :                 vardata.freefunc = ReleaseSysCache;
                               9123                 :                :             }
                               9124                 :                :         }
                               9125                 :                :         else
                               9126                 :                :         {
                               9127                 :                :             /*
                               9128                 :                :              * Looks like we've found an expression column in the index. Let's
                               9129                 :                :              * see if there's any stats for it.
                               9130                 :                :              */
                               9131                 :                : 
                               9132                 :                :             /* get the attnum from the 0-based index. */
 2636 tgl@sss.pgh.pa.us        9133                 :UBC           0 :             attnum = iclause->indexcol + 1;
                               9134                 :                : 
 3316 alvherre@alvh.no-ip.     9135   [ #  #  #  # ]:              0 :             if (get_index_stats_hook &&
 3240 tgl@sss.pgh.pa.us        9136                 :              0 :                 (*get_index_stats_hook) (root, index->indexoid, attnum, &vardata))
                               9137                 :                :             {
                               9138                 :                :                 /*
                               9139                 :                :                  * The hook took control of acquiring a stats tuple.  If it
                               9140                 :                :                  * did supply a tuple, it'd better have supplied a freefunc.
                               9141                 :                :                  */
 3316 alvherre@alvh.no-ip.     9142         [ #  # ]:              0 :                 if (HeapTupleIsValid(vardata.statsTuple) &&
                               9143         [ #  # ]:              0 :                     !vardata.freefunc)
                               9144         [ #  # ]:              0 :                     elog(ERROR, "no function provided to release variable stats with");
                               9145                 :                :             }
                               9146                 :                :             else
                               9147                 :                :             {
                               9148                 :              0 :                 vardata.statsTuple = SearchSysCache3(STATRELATTINH,
                               9149                 :                :                                                      ObjectIdGetDatum(index->indexoid),
                               9150                 :                :                                                      Int16GetDatum(attnum),
                               9151                 :                :                                                      BoolGetDatum(false));
                               9152                 :              0 :                 vardata.freefunc = ReleaseSysCache;
                               9153                 :                :             }
                               9154                 :                :         }
                               9155                 :                : 
 3316 alvherre@alvh.no-ip.     9156         [ +  + ]:CBC        8945 :         if (HeapTupleIsValid(vardata.statsTuple))
                               9157                 :                :         {
                               9158                 :                :             AttStatsSlot sslot;
                               9159                 :                : 
 3279 tgl@sss.pgh.pa.us        9160         [ +  - ]:             33 :             if (get_attstatsslot(&sslot, vardata.statsTuple,
                               9161                 :                :                                  STATISTIC_KIND_CORRELATION, InvalidOid,
                               9162                 :                :                                  ATTSTATSSLOT_NUMBERS))
                               9163                 :                :             {
 3316 alvherre@alvh.no-ip.     9164                 :             33 :                 double      varCorrelation = 0.0;
                               9165                 :                : 
 3279 tgl@sss.pgh.pa.us        9166         [ +  - ]:             33 :                 if (sslot.nnumbers > 0)
 1306 peter@eisentraut.org     9167                 :             33 :                     varCorrelation = fabs(sslot.numbers[0]);
                               9168                 :                : 
 3316 alvherre@alvh.no-ip.     9169         [ +  - ]:             33 :                 if (varCorrelation > *indexCorrelation)
                               9170                 :             33 :                     *indexCorrelation = varCorrelation;
                               9171                 :                : 
 3279 tgl@sss.pgh.pa.us        9172                 :             33 :                 free_attstatsslot(&sslot);
                               9173                 :                :             }
                               9174                 :                :         }
                               9175                 :                : 
 3316 alvherre@alvh.no-ip.     9176         [ +  + ]:           8945 :         ReleaseVariableStats(vardata);
                               9177                 :                :     }
                               9178                 :                : 
                               9179                 :           8944 :     qualSelectivity = clauselist_selectivity(root, indexQuals,
                               9180                 :           8944 :                                              baserel->relid,
                               9181                 :                :                                              JOIN_INNER, NULL);
                               9182                 :                : 
                               9183                 :                :     /*
                               9184                 :                :      * Now calculate the minimum possible ranges we could match with if all of
                               9185                 :                :      * the rows were in the perfect order in the table's heap.
                               9186                 :                :      */
                               9187                 :           8944 :     minimalRanges = ceil(indexRanges * qualSelectivity);
                               9188                 :                : 
                               9189                 :                :     /*
                               9190                 :                :      * Now estimate the number of ranges that we'll touch by using the
                               9191                 :                :      * indexCorrelation from the stats. Careful not to divide by zero (note
                               9192                 :                :      * we're using the absolute value of the correlation).
                               9193                 :                :      */
                               9194         [ +  + ]:           8944 :     if (*indexCorrelation < 1.0e-10)
                               9195                 :           8911 :         estimatedRanges = indexRanges;
                               9196                 :                :     else
                               9197         [ +  + ]:             33 :         estimatedRanges = Min(minimalRanges / *indexCorrelation, indexRanges);
                               9198                 :                : 
                               9199                 :                :     /* we expect to visit this portion of the table */
                               9200                 :           8944 :     selec = estimatedRanges / indexRanges;
                               9201                 :                : 
                               9202   [ -  +  -  + ]:           8944 :     CLAMP_PROBABILITY(selec);
                               9203                 :                : 
                               9204                 :           8944 :     *indexSelectivity = selec;
                               9205                 :                : 
                               9206                 :                :     /*
                               9207                 :                :      * Compute the index qual costs, much as in genericcostestimate, to add to
                               9208                 :                :      * the index costs.  We can disregard indexorderbys, since BRIN doesn't
                               9209                 :                :      * support those.
                               9210                 :                :      */
 2636 tgl@sss.pgh.pa.us        9211                 :           8944 :     qual_arg_cost = index_other_operands_eval_cost(root, indexQuals);
                               9212                 :                : 
                               9213                 :                :     /*
                               9214                 :                :      * Compute the startup cost as the cost to read the whole revmap
                               9215                 :                :      * sequentially, including the cost to execute the index quals.
                               9216                 :                :      */
 3316 alvherre@alvh.no-ip.     9217                 :           8944 :     *indexStartupCost =
                               9218                 :           8944 :         spc_seq_page_cost * statsData.revmapNumPages * loop_count;
 4197                          9219                 :           8944 :     *indexStartupCost += qual_arg_cost;
                               9220                 :                : 
                               9221                 :                :     /*
                               9222                 :                :      * To read a BRIN index there might be a bit of back and forth over
                               9223                 :                :      * regular pages, as revmap might point to them out of sequential order;
                               9224                 :                :      * calculate the total cost as reading the whole index in random order.
                               9225                 :                :      */
 3316                          9226                 :           8944 :     *indexTotalCost = *indexStartupCost +
                               9227                 :           8944 :         spc_random_page_cost * (numPages - statsData.revmapNumPages) * loop_count;
                               9228                 :                : 
                               9229                 :                :     /*
                               9230                 :                :      * Charge a small amount per range tuple which we expect to match to. This
                               9231                 :                :      * is meant to reflect the costs of manipulating the bitmap. The BRIN scan
                               9232                 :                :      * will set a bit for each page in the range when we find a matching
                               9233                 :                :      * range, so we must multiply the charge by the number of pages in the
                               9234                 :                :      * range.
                               9235                 :                :      */
                               9236                 :           8944 :     *indexTotalCost += 0.1 * cpu_operator_cost * estimatedRanges *
                               9237                 :           8944 :         statsData.pagesPerRange;
                               9238                 :                : 
                               9239                 :           8944 :     *indexPages = index->pages;
 4197                          9240                 :           8944 : }
        

Generated by: LCOV version 2.5.0-beta