LCOV - code coverage report
Current view: top level - src/backend/statistics - extended_stats.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 728 789 92.3 %
Date: 2025-01-18 04:15:08 Functions: 32 33 97.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * extended_stats.c
       4             :  *    POSTGRES extended statistics
       5             :  *
       6             :  * Generic code supporting statistics objects created via CREATE STATISTICS.
       7             :  *
       8             :  *
       9             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
      10             :  * Portions Copyright (c) 1994, Regents of the University of California
      11             :  *
      12             :  * IDENTIFICATION
      13             :  *    src/backend/statistics/extended_stats.c
      14             :  *
      15             :  *-------------------------------------------------------------------------
      16             :  */
      17             : #include "postgres.h"
      18             : 
      19             : #include "access/detoast.h"
      20             : #include "access/genam.h"
      21             : #include "access/htup_details.h"
      22             : #include "access/table.h"
      23             : #include "catalog/indexing.h"
      24             : #include "catalog/pg_statistic_ext.h"
      25             : #include "catalog/pg_statistic_ext_data.h"
      26             : #include "commands/defrem.h"
      27             : #include "commands/progress.h"
      28             : #include "executor/executor.h"
      29             : #include "miscadmin.h"
      30             : #include "nodes/nodeFuncs.h"
      31             : #include "optimizer/optimizer.h"
      32             : #include "parser/parsetree.h"
      33             : #include "pgstat.h"
      34             : #include "postmaster/autovacuum.h"
      35             : #include "statistics/extended_stats_internal.h"
      36             : #include "statistics/statistics.h"
      37             : #include "utils/acl.h"
      38             : #include "utils/array.h"
      39             : #include "utils/attoptcache.h"
      40             : #include "utils/builtins.h"
      41             : #include "utils/datum.h"
      42             : #include "utils/fmgroids.h"
      43             : #include "utils/lsyscache.h"
      44             : #include "utils/memutils.h"
      45             : #include "utils/rel.h"
      46             : #include "utils/selfuncs.h"
      47             : #include "utils/syscache.h"
      48             : 
      49             : /*
      50             :  * To avoid consuming too much memory during analysis and/or too much space
      51             :  * in the resulting pg_statistic rows, we ignore varlena datums that are wider
      52             :  * than WIDTH_THRESHOLD (after detoasting!).  This is legitimate for MCV
      53             :  * and distinct-value calculations since a wide value is unlikely to be
      54             :  * duplicated at all, much less be a most-common value.  For the same reason,
      55             :  * ignoring wide values will not affect our estimates of histogram bin
      56             :  * boundaries very much.
      57             :  */
      58             : #define WIDTH_THRESHOLD  1024
      59             : 
      60             : /*
      61             :  * Used internally to refer to an individual statistics object, i.e.,
      62             :  * a pg_statistic_ext entry.
      63             :  */
      64             : typedef struct StatExtEntry
      65             : {
      66             :     Oid         statOid;        /* OID of pg_statistic_ext entry */
      67             :     char       *schema;         /* statistics object's schema */
      68             :     char       *name;           /* statistics object's name */
      69             :     Bitmapset  *columns;        /* attribute numbers covered by the object */
      70             :     List       *types;          /* 'char' list of enabled statistics kinds */
      71             :     int         stattarget;     /* statistics target (-1 for default) */
      72             :     List       *exprs;          /* expressions */
      73             : } StatExtEntry;
      74             : 
      75             : 
      76             : static List *fetch_statentries_for_relation(Relation pg_statext, Oid relid);
      77             : static VacAttrStats **lookup_var_attr_stats(Bitmapset *attrs, List *exprs,
      78             :                                             int nvacatts, VacAttrStats **vacatts);
      79             : static void statext_store(Oid statOid, bool inh,
      80             :                           MVNDistinct *ndistinct, MVDependencies *dependencies,
      81             :                           MCVList *mcv, Datum exprs, VacAttrStats **stats);
      82             : static int  statext_compute_stattarget(int stattarget,
      83             :                                        int nattrs, VacAttrStats **stats);
      84             : 
      85             : /* Information needed to analyze a single simple expression. */
      86             : typedef struct AnlExprData
      87             : {
      88             :     Node       *expr;           /* expression to analyze */
      89             :     VacAttrStats *vacattrstat;  /* statistics attrs to analyze */
      90             : } AnlExprData;
      91             : 
      92             : static void compute_expr_stats(Relation onerel, AnlExprData *exprdata,
      93             :                                int nexprs, HeapTuple *rows, int numrows);
      94             : static Datum serialize_expr_stats(AnlExprData *exprdata, int nexprs);
      95             : static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull);
      96             : static AnlExprData *build_expr_data(List *exprs, int stattarget);
      97             : 
      98             : static StatsBuildData *make_build_data(Relation rel, StatExtEntry *stat,
      99             :                                        int numrows, HeapTuple *rows,
     100             :                                        VacAttrStats **stats, int stattarget);
     101             : 
     102             : 
     103             : /*
     104             :  * Compute requested extended stats, using the rows sampled for the plain
     105             :  * (single-column) stats.
     106             :  *
     107             :  * This fetches a list of stats types from pg_statistic_ext, computes the
     108             :  * requested stats, and serializes them back into the catalog.
     109             :  */
     110             : void
     111        9356 : BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows,
     112             :                            int numrows, HeapTuple *rows,
     113             :                            int natts, VacAttrStats **vacattrstats)
     114             : {
     115             :     Relation    pg_stext;
     116             :     ListCell   *lc;
     117             :     List       *statslist;
     118             :     MemoryContext cxt;
     119             :     MemoryContext oldcxt;
     120             :     int64       ext_cnt;
     121             : 
     122             :     /* Do nothing if there are no columns to analyze. */
     123        9356 :     if (!natts)
     124          18 :         return;
     125             : 
     126             :     /* the list of stats has to be allocated outside the memory context */
     127        9338 :     pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
     128        9338 :     statslist = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
     129             : 
     130             :     /* memory context for building each statistics object */
     131        9338 :     cxt = AllocSetContextCreate(CurrentMemoryContext,
     132             :                                 "BuildRelationExtStatistics",
     133             :                                 ALLOCSET_DEFAULT_SIZES);
     134        9338 :     oldcxt = MemoryContextSwitchTo(cxt);
     135             : 
     136             :     /* report this phase */
     137        9338 :     if (statslist != NIL)
     138             :     {
     139         288 :         const int   index[] = {
     140             :             PROGRESS_ANALYZE_PHASE,
     141             :             PROGRESS_ANALYZE_EXT_STATS_TOTAL
     142             :         };
     143         576 :         const int64 val[] = {
     144             :             PROGRESS_ANALYZE_PHASE_COMPUTE_EXT_STATS,
     145         288 :             list_length(statslist)
     146             :         };
     147             : 
     148         288 :         pgstat_progress_update_multi_param(2, index, val);
     149             :     }
     150             : 
     151        9338 :     ext_cnt = 0;
     152        9734 :     foreach(lc, statslist)
     153             :     {
     154         396 :         StatExtEntry *stat = (StatExtEntry *) lfirst(lc);
     155         396 :         MVNDistinct *ndistinct = NULL;
     156         396 :         MVDependencies *dependencies = NULL;
     157         396 :         MCVList    *mcv = NULL;
     158         396 :         Datum       exprstats = (Datum) 0;
     159             :         VacAttrStats **stats;
     160             :         ListCell   *lc2;
     161             :         int         stattarget;
     162             :         StatsBuildData *data;
     163             : 
     164             :         /*
     165             :          * Check if we can build these stats based on the column analyzed. If
     166             :          * not, report this fact (except in autovacuum) and move on.
     167             :          */
     168         396 :         stats = lookup_var_attr_stats(stat->columns, stat->exprs,
     169             :                                       natts, vacattrstats);
     170         396 :         if (!stats)
     171             :         {
     172          12 :             if (!AmAutoVacuumWorkerProcess())
     173          12 :                 ereport(WARNING,
     174             :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     175             :                          errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",
     176             :                                 stat->schema, stat->name,
     177             :                                 get_namespace_name(onerel->rd_rel->relnamespace),
     178             :                                 RelationGetRelationName(onerel)),
     179             :                          errtable(onerel)));
     180          12 :             continue;
     181             :         }
     182             : 
     183             :         /* compute statistics target for this statistics object */
     184         384 :         stattarget = statext_compute_stattarget(stat->stattarget,
     185         384 :                                                 bms_num_members(stat->columns),
     186             :                                                 stats);
     187             : 
     188             :         /*
     189             :          * Don't rebuild statistics objects with statistics target set to 0
     190             :          * (we just leave the existing values around, just like we do for
     191             :          * regular per-column statistics).
     192             :          */
     193         384 :         if (stattarget == 0)
     194           6 :             continue;
     195             : 
     196             :         /* evaluate expressions (if the statistics object has any) */
     197         378 :         data = make_build_data(onerel, stat, numrows, rows, stats, stattarget);
     198             : 
     199             :         /* compute statistic of each requested type */
     200        1044 :         foreach(lc2, stat->types)
     201             :         {
     202         666 :             char        t = (char) lfirst_int(lc2);
     203             : 
     204         666 :             if (t == STATS_EXT_NDISTINCT)
     205         174 :                 ndistinct = statext_ndistinct_build(totalrows, data);
     206         492 :             else if (t == STATS_EXT_DEPENDENCIES)
     207         132 :                 dependencies = statext_dependencies_build(data);
     208         360 :             else if (t == STATS_EXT_MCV)
     209         204 :                 mcv = statext_mcv_build(data, totalrows, stattarget);
     210         156 :             else if (t == STATS_EXT_EXPRESSIONS)
     211             :             {
     212             :                 AnlExprData *exprdata;
     213             :                 int         nexprs;
     214             : 
     215             :                 /* should not happen, thanks to checks when defining stats */
     216         156 :                 if (!stat->exprs)
     217           0 :                     elog(ERROR, "requested expression stats, but there are no expressions");
     218             : 
     219         156 :                 exprdata = build_expr_data(stat->exprs, stattarget);
     220         156 :                 nexprs = list_length(stat->exprs);
     221             : 
     222         156 :                 compute_expr_stats(onerel, exprdata, nexprs, rows, numrows);
     223             : 
     224         156 :                 exprstats = serialize_expr_stats(exprdata, nexprs);
     225             :             }
     226             :         }
     227             : 
     228             :         /* store the statistics in the catalog */
     229         378 :         statext_store(stat->statOid, inh,
     230             :                       ndistinct, dependencies, mcv, exprstats, stats);
     231             : 
     232             :         /* for reporting progress */
     233         378 :         pgstat_progress_update_param(PROGRESS_ANALYZE_EXT_STATS_COMPUTED,
     234             :                                      ++ext_cnt);
     235             : 
     236             :         /* free the data used for building this statistics object */
     237         378 :         MemoryContextReset(cxt);
     238             :     }
     239             : 
     240        9338 :     MemoryContextSwitchTo(oldcxt);
     241        9338 :     MemoryContextDelete(cxt);
     242             : 
     243        9338 :     list_free(statslist);
     244             : 
     245        9338 :     table_close(pg_stext, RowExclusiveLock);
     246             : }
     247             : 
     248             : /*
     249             :  * ComputeExtStatisticsRows
     250             :  *      Compute number of rows required by extended statistics on a table.
     251             :  *
     252             :  * Computes number of rows we need to sample to build extended statistics on a
     253             :  * table. This only looks at statistics we can actually build - for example
     254             :  * when analyzing only some of the columns, this will skip statistics objects
     255             :  * that would require additional columns.
     256             :  *
     257             :  * See statext_compute_stattarget for details about how we compute the
     258             :  * statistics target for a statistics object (from the object target,
     259             :  * attribute targets and default statistics target).
     260             :  */
     261             : int
     262       13662 : ComputeExtStatisticsRows(Relation onerel,
     263             :                          int natts, VacAttrStats **vacattrstats)
     264             : {
     265             :     Relation    pg_stext;
     266             :     ListCell   *lc;
     267             :     List       *lstats;
     268             :     MemoryContext cxt;
     269             :     MemoryContext oldcxt;
     270       13662 :     int         result = 0;
     271             : 
     272             :     /* If there are no columns to analyze, just return 0. */
     273       13662 :     if (!natts)
     274          36 :         return 0;
     275             : 
     276       13626 :     cxt = AllocSetContextCreate(CurrentMemoryContext,
     277             :                                 "ComputeExtStatisticsRows",
     278             :                                 ALLOCSET_DEFAULT_SIZES);
     279       13626 :     oldcxt = MemoryContextSwitchTo(cxt);
     280             : 
     281       13626 :     pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
     282       13626 :     lstats = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
     283             : 
     284       14022 :     foreach(lc, lstats)
     285             :     {
     286         396 :         StatExtEntry *stat = (StatExtEntry *) lfirst(lc);
     287             :         int         stattarget;
     288             :         VacAttrStats **stats;
     289         396 :         int         nattrs = bms_num_members(stat->columns);
     290             : 
     291             :         /*
     292             :          * Check if we can build this statistics object based on the columns
     293             :          * analyzed. If not, ignore it (don't report anything, we'll do that
     294             :          * during the actual build BuildRelationExtStatistics).
     295             :          */
     296         396 :         stats = lookup_var_attr_stats(stat->columns, stat->exprs,
     297             :                                       natts, vacattrstats);
     298             : 
     299         396 :         if (!stats)
     300          12 :             continue;
     301             : 
     302             :         /*
     303             :          * Compute statistics target, based on what's set for the statistic
     304             :          * object itself, and for its attributes.
     305             :          */
     306         384 :         stattarget = statext_compute_stattarget(stat->stattarget,
     307             :                                                 nattrs, stats);
     308             : 
     309             :         /* Use the largest value for all statistics objects. */
     310         384 :         if (stattarget > result)
     311         270 :             result = stattarget;
     312             :     }
     313             : 
     314       13626 :     table_close(pg_stext, RowExclusiveLock);
     315             : 
     316       13626 :     MemoryContextSwitchTo(oldcxt);
     317       13626 :     MemoryContextDelete(cxt);
     318             : 
     319             :     /* compute sample size based on the statistics target */
     320       13626 :     return (300 * result);
     321             : }
     322             : 
     323             : /*
     324             :  * statext_compute_stattarget
     325             :  *      compute statistics target for an extended statistic
     326             :  *
     327             :  * When computing target for extended statistics objects, we consider three
     328             :  * places where the target may be set - the statistics object itself,
     329             :  * attributes the statistics object is defined on, and then the default
     330             :  * statistics target.
     331             :  *
     332             :  * First we look at what's set for the statistics object itself, using the
     333             :  * ALTER STATISTICS ... SET STATISTICS command. If we find a valid value
     334             :  * there (i.e. not -1) we're done. Otherwise we look at targets set for any
     335             :  * of the attributes the statistic is defined on, and if there are columns
     336             :  * with defined target, we use the maximum value. We do this mostly for
     337             :  * backwards compatibility, because this is what we did before having
     338             :  * statistics target for extended statistics.
     339             :  *
     340             :  * And finally, if we still don't have a statistics target, we use the value
     341             :  * set in default_statistics_target.
     342             :  */
     343             : static int
     344         768 : statext_compute_stattarget(int stattarget, int nattrs, VacAttrStats **stats)
     345             : {
     346             :     int         i;
     347             : 
     348             :     /*
     349             :      * If there's statistics target set for the statistics object, use it. It
     350             :      * may be set to 0 which disables building of that statistic.
     351             :      */
     352         768 :     if (stattarget >= 0)
     353          12 :         return stattarget;
     354             : 
     355             :     /*
     356             :      * The target for the statistics object is set to -1, in which case we
     357             :      * look at the maximum target set for any of the attributes the object is
     358             :      * defined on.
     359             :      */
     360        2076 :     for (i = 0; i < nattrs; i++)
     361             :     {
     362             :         /* keep the maximum statistics target */
     363        1320 :         if (stats[i]->attstattarget > stattarget)
     364         576 :             stattarget = stats[i]->attstattarget;
     365             :     }
     366             : 
     367             :     /*
     368             :      * If the value is still negative (so neither the statistics object nor
     369             :      * any of the columns have custom statistics target set), use the global
     370             :      * default target.
     371             :      */
     372         756 :     if (stattarget < 0)
     373         180 :         stattarget = default_statistics_target;
     374             : 
     375             :     /* As this point we should have a valid statistics target. */
     376             :     Assert((stattarget >= 0) && (stattarget <= MAX_STATISTICS_TARGET));
     377             : 
     378         756 :     return stattarget;
     379             : }
     380             : 
     381             : /*
     382             :  * statext_is_kind_built
     383             :  *      Is this stat kind built in the given pg_statistic_ext_data tuple?
     384             :  */
     385             : bool
     386        7512 : statext_is_kind_built(HeapTuple htup, char type)
     387             : {
     388             :     AttrNumber  attnum;
     389             : 
     390        7512 :     switch (type)
     391             :     {
     392        1878 :         case STATS_EXT_NDISTINCT:
     393        1878 :             attnum = Anum_pg_statistic_ext_data_stxdndistinct;
     394        1878 :             break;
     395             : 
     396        1878 :         case STATS_EXT_DEPENDENCIES:
     397        1878 :             attnum = Anum_pg_statistic_ext_data_stxddependencies;
     398        1878 :             break;
     399             : 
     400        1878 :         case STATS_EXT_MCV:
     401        1878 :             attnum = Anum_pg_statistic_ext_data_stxdmcv;
     402        1878 :             break;
     403             : 
     404        1878 :         case STATS_EXT_EXPRESSIONS:
     405        1878 :             attnum = Anum_pg_statistic_ext_data_stxdexpr;
     406        1878 :             break;
     407             : 
     408           0 :         default:
     409           0 :             elog(ERROR, "unexpected statistics type requested: %d", type);
     410             :     }
     411             : 
     412        7512 :     return !heap_attisnull(htup, attnum, NULL);
     413             : }
     414             : 
     415             : /*
     416             :  * Return a list (of StatExtEntry) of statistics objects for the given relation.
     417             :  */
     418             : static List *
     419       22964 : fetch_statentries_for_relation(Relation pg_statext, Oid relid)
     420             : {
     421             :     SysScanDesc scan;
     422             :     ScanKeyData skey;
     423             :     HeapTuple   htup;
     424       22964 :     List       *result = NIL;
     425             : 
     426             :     /*
     427             :      * Prepare to scan pg_statistic_ext for entries having stxrelid = this
     428             :      * rel.
     429             :      */
     430       22964 :     ScanKeyInit(&skey,
     431             :                 Anum_pg_statistic_ext_stxrelid,
     432             :                 BTEqualStrategyNumber, F_OIDEQ,
     433             :                 ObjectIdGetDatum(relid));
     434             : 
     435       22964 :     scan = systable_beginscan(pg_statext, StatisticExtRelidIndexId, true,
     436             :                               NULL, 1, &skey);
     437             : 
     438       23756 :     while (HeapTupleIsValid(htup = systable_getnext(scan)))
     439             :     {
     440             :         StatExtEntry *entry;
     441             :         Datum       datum;
     442             :         bool        isnull;
     443             :         int         i;
     444             :         ArrayType  *arr;
     445             :         char       *enabled;
     446             :         Form_pg_statistic_ext staForm;
     447         792 :         List       *exprs = NIL;
     448             : 
     449         792 :         entry = palloc0(sizeof(StatExtEntry));
     450         792 :         staForm = (Form_pg_statistic_ext) GETSTRUCT(htup);
     451         792 :         entry->statOid = staForm->oid;
     452         792 :         entry->schema = get_namespace_name(staForm->stxnamespace);
     453         792 :         entry->name = pstrdup(NameStr(staForm->stxname));
     454        2184 :         for (i = 0; i < staForm->stxkeys.dim1; i++)
     455             :         {
     456        1392 :             entry->columns = bms_add_member(entry->columns,
     457        1392 :                                             staForm->stxkeys.values[i]);
     458             :         }
     459             : 
     460         792 :         datum = SysCacheGetAttr(STATEXTOID, htup, Anum_pg_statistic_ext_stxstattarget, &isnull);
     461         792 :         entry->stattarget = isnull ? -1 : DatumGetInt16(datum);
     462             : 
     463             :         /* decode the stxkind char array into a list of chars */
     464         792 :         datum = SysCacheGetAttrNotNull(STATEXTOID, htup,
     465             :                                        Anum_pg_statistic_ext_stxkind);
     466         792 :         arr = DatumGetArrayTypeP(datum);
     467         792 :         if (ARR_NDIM(arr) != 1 ||
     468         792 :             ARR_HASNULL(arr) ||
     469         792 :             ARR_ELEMTYPE(arr) != CHAROID)
     470           0 :             elog(ERROR, "stxkind is not a 1-D char array");
     471         792 :         enabled = (char *) ARR_DATA_PTR(arr);
     472        2232 :         for (i = 0; i < ARR_DIMS(arr)[0]; i++)
     473             :         {
     474             :             Assert((enabled[i] == STATS_EXT_NDISTINCT) ||
     475             :                    (enabled[i] == STATS_EXT_DEPENDENCIES) ||
     476             :                    (enabled[i] == STATS_EXT_MCV) ||
     477             :                    (enabled[i] == STATS_EXT_EXPRESSIONS));
     478        1440 :             entry->types = lappend_int(entry->types, (int) enabled[i]);
     479             :         }
     480             : 
     481             :         /* decode expression (if any) */
     482         792 :         datum = SysCacheGetAttr(STATEXTOID, htup,
     483             :                                 Anum_pg_statistic_ext_stxexprs, &isnull);
     484             : 
     485         792 :         if (!isnull)
     486             :         {
     487             :             char       *exprsString;
     488             : 
     489         312 :             exprsString = TextDatumGetCString(datum);
     490         312 :             exprs = (List *) stringToNode(exprsString);
     491             : 
     492         312 :             pfree(exprsString);
     493             : 
     494             :             /*
     495             :              * Run the expressions through eval_const_expressions. This is not
     496             :              * just an optimization, but is necessary, because the planner
     497             :              * will be comparing them to similarly-processed qual clauses, and
     498             :              * may fail to detect valid matches without this.  We must not use
     499             :              * canonicalize_qual, however, since these aren't qual
     500             :              * expressions.
     501             :              */
     502         312 :             exprs = (List *) eval_const_expressions(NULL, (Node *) exprs);
     503             : 
     504             :             /* May as well fix opfuncids too */
     505         312 :             fix_opfuncids((Node *) exprs);
     506             :         }
     507             : 
     508         792 :         entry->exprs = exprs;
     509             : 
     510         792 :         result = lappend(result, entry);
     511             :     }
     512             : 
     513       22964 :     systable_endscan(scan);
     514             : 
     515       22964 :     return result;
     516             : }
     517             : 
     518             : /*
     519             :  * examine_attribute -- pre-analysis of a single column
     520             :  *
     521             :  * Determine whether the column is analyzable; if so, create and initialize
     522             :  * a VacAttrStats struct for it.  If not, return NULL.
     523             :  */
     524             : static VacAttrStats *
     525         600 : examine_attribute(Node *expr)
     526             : {
     527             :     HeapTuple   typtuple;
     528             :     VacAttrStats *stats;
     529             :     int         i;
     530             :     bool        ok;
     531             : 
     532             :     /*
     533             :      * Create the VacAttrStats struct.
     534             :      */
     535         600 :     stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
     536         600 :     stats->attstattarget = -1;
     537             : 
     538             :     /*
     539             :      * When analyzing an expression, believe the expression tree's type not
     540             :      * the column datatype --- the latter might be the opckeytype storage type
     541             :      * of the opclass, which is not interesting for our purposes.  (Note: if
     542             :      * we did anything with non-expression statistics columns, we'd need to
     543             :      * figure out where to get the correct type info from, but for now that's
     544             :      * not a problem.)  It's not clear whether anyone will care about the
     545             :      * typmod, but we store that too just in case.
     546             :      */
     547         600 :     stats->attrtypid = exprType(expr);
     548         600 :     stats->attrtypmod = exprTypmod(expr);
     549         600 :     stats->attrcollid = exprCollation(expr);
     550             : 
     551         600 :     typtuple = SearchSysCacheCopy1(TYPEOID,
     552             :                                    ObjectIdGetDatum(stats->attrtypid));
     553         600 :     if (!HeapTupleIsValid(typtuple))
     554           0 :         elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
     555         600 :     stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
     556             : 
     557             :     /*
     558             :      * We don't actually analyze individual attributes, so no need to set the
     559             :      * memory context.
     560             :      */
     561         600 :     stats->anl_context = NULL;
     562         600 :     stats->tupattnum = InvalidAttrNumber;
     563             : 
     564             :     /*
     565             :      * The fields describing the stats->stavalues[n] element types default to
     566             :      * the type of the data being analyzed, but the type-specific typanalyze
     567             :      * function can change them if it wants to store something else.
     568             :      */
     569        3600 :     for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
     570             :     {
     571        3000 :         stats->statypid[i] = stats->attrtypid;
     572        3000 :         stats->statyplen[i] = stats->attrtype->typlen;
     573        3000 :         stats->statypbyval[i] = stats->attrtype->typbyval;
     574        3000 :         stats->statypalign[i] = stats->attrtype->typalign;
     575             :     }
     576             : 
     577             :     /*
     578             :      * Call the type-specific typanalyze function.  If none is specified, use
     579             :      * std_typanalyze().
     580             :      */
     581         600 :     if (OidIsValid(stats->attrtype->typanalyze))
     582           0 :         ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
     583             :                                            PointerGetDatum(stats)));
     584             :     else
     585         600 :         ok = std_typanalyze(stats);
     586             : 
     587         600 :     if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
     588             :     {
     589           0 :         heap_freetuple(typtuple);
     590           0 :         pfree(stats);
     591           0 :         return NULL;
     592             :     }
     593             : 
     594         600 :     return stats;
     595             : }
     596             : 
     597             : /*
     598             :  * examine_expression -- pre-analysis of a single expression
     599             :  *
     600             :  * Determine whether the expression is analyzable; if so, create and initialize
     601             :  * a VacAttrStats struct for it.  If not, return NULL.
     602             :  */
     603             : static VacAttrStats *
     604         600 : examine_expression(Node *expr, int stattarget)
     605             : {
     606             :     HeapTuple   typtuple;
     607             :     VacAttrStats *stats;
     608             :     int         i;
     609             :     bool        ok;
     610             : 
     611             :     Assert(expr != NULL);
     612             : 
     613             :     /*
     614             :      * Create the VacAttrStats struct.
     615             :      */
     616         600 :     stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
     617             : 
     618             :     /*
     619             :      * We can't have statistics target specified for the expression, so we
     620             :      * could use either the default_statistics_target, or the target computed
     621             :      * for the extended statistics. The second option seems more reasonable.
     622             :      */
     623         600 :     stats->attstattarget = stattarget;
     624             : 
     625             :     /*
     626             :      * When analyzing an expression, believe the expression tree's type.
     627             :      */
     628         600 :     stats->attrtypid = exprType(expr);
     629         600 :     stats->attrtypmod = exprTypmod(expr);
     630             : 
     631             :     /*
     632             :      * We don't allow collation to be specified in CREATE STATISTICS, so we
     633             :      * have to use the collation specified for the expression. It's possible
     634             :      * to specify the collation in the expression "(col COLLATE "en_US")" in
     635             :      * which case exprCollation() does the right thing.
     636             :      */
     637         600 :     stats->attrcollid = exprCollation(expr);
     638             : 
     639         600 :     typtuple = SearchSysCacheCopy1(TYPEOID,
     640             :                                    ObjectIdGetDatum(stats->attrtypid));
     641         600 :     if (!HeapTupleIsValid(typtuple))
     642           0 :         elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
     643             : 
     644         600 :     stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
     645         600 :     stats->anl_context = CurrentMemoryContext;   /* XXX should be using
     646             :                                                  * something else? */
     647         600 :     stats->tupattnum = InvalidAttrNumber;
     648             : 
     649             :     /*
     650             :      * The fields describing the stats->stavalues[n] element types default to
     651             :      * the type of the data being analyzed, but the type-specific typanalyze
     652             :      * function can change them if it wants to store something else.
     653             :      */
     654        3600 :     for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
     655             :     {
     656        3000 :         stats->statypid[i] = stats->attrtypid;
     657        3000 :         stats->statyplen[i] = stats->attrtype->typlen;
     658        3000 :         stats->statypbyval[i] = stats->attrtype->typbyval;
     659        3000 :         stats->statypalign[i] = stats->attrtype->typalign;
     660             :     }
     661             : 
     662             :     /*
     663             :      * Call the type-specific typanalyze function.  If none is specified, use
     664             :      * std_typanalyze().
     665             :      */
     666         600 :     if (OidIsValid(stats->attrtype->typanalyze))
     667           0 :         ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
     668             :                                            PointerGetDatum(stats)));
     669             :     else
     670         600 :         ok = std_typanalyze(stats);
     671             : 
     672         600 :     if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
     673             :     {
     674           0 :         heap_freetuple(typtuple);
     675           0 :         pfree(stats);
     676           0 :         return NULL;
     677             :     }
     678             : 
     679         600 :     return stats;
     680             : }
     681             : 
     682             : /*
     683             :  * Using 'vacatts' of size 'nvacatts' as input data, return a newly-built
     684             :  * VacAttrStats array which includes only the items corresponding to
     685             :  * attributes indicated by 'attrs'.  If we don't have all of the per-column
     686             :  * stats available to compute the extended stats, then we return NULL to
     687             :  * indicate to the caller that the stats should not be built.
     688             :  */
     689             : static VacAttrStats **
     690         792 : lookup_var_attr_stats(Bitmapset *attrs, List *exprs,
     691             :                       int nvacatts, VacAttrStats **vacatts)
     692             : {
     693         792 :     int         i = 0;
     694         792 :     int         x = -1;
     695             :     int         natts;
     696             :     VacAttrStats **stats;
     697             :     ListCell   *lc;
     698             : 
     699         792 :     natts = bms_num_members(attrs) + list_length(exprs);
     700             : 
     701         792 :     stats = (VacAttrStats **) palloc(natts * sizeof(VacAttrStats *));
     702             : 
     703             :     /* lookup VacAttrStats info for the requested columns (same attnum) */
     704        2148 :     while ((x = bms_next_member(attrs, x)) >= 0)
     705             :     {
     706             :         int         j;
     707             : 
     708        1380 :         stats[i] = NULL;
     709        4248 :         for (j = 0; j < nvacatts; j++)
     710             :         {
     711        4224 :             if (x == vacatts[j]->tupattnum)
     712             :             {
     713        1356 :                 stats[i] = vacatts[j];
     714        1356 :                 break;
     715             :             }
     716             :         }
     717             : 
     718        1380 :         if (!stats[i])
     719             :         {
     720             :             /*
     721             :              * Looks like stats were not gathered for one of the columns
     722             :              * required. We'll be unable to build the extended stats without
     723             :              * this column.
     724             :              */
     725          24 :             pfree(stats);
     726          24 :             return NULL;
     727             :         }
     728             : 
     729        1356 :         i++;
     730             :     }
     731             : 
     732             :     /* also add info for expressions */
     733        1368 :     foreach(lc, exprs)
     734             :     {
     735         600 :         Node       *expr = (Node *) lfirst(lc);
     736             : 
     737         600 :         stats[i] = examine_attribute(expr);
     738             : 
     739             :         /*
     740             :          * XXX We need tuple descriptor later, and we just grab it from
     741             :          * stats[0]->tupDesc (see e.g. statext_mcv_build). But as coded
     742             :          * examine_attribute does not set that, so just grab it from the first
     743             :          * vacatts element.
     744             :          */
     745         600 :         stats[i]->tupDesc = vacatts[0]->tupDesc;
     746             : 
     747         600 :         i++;
     748             :     }
     749             : 
     750         768 :     return stats;
     751             : }
     752             : 
     753             : /*
     754             :  * statext_store
     755             :  *  Serializes the statistics and stores them into the pg_statistic_ext_data
     756             :  *  tuple.
     757             :  */
     758             : static void
     759         378 : statext_store(Oid statOid, bool inh,
     760             :               MVNDistinct *ndistinct, MVDependencies *dependencies,
     761             :               MCVList *mcv, Datum exprs, VacAttrStats **stats)
     762             : {
     763             :     Relation    pg_stextdata;
     764             :     HeapTuple   stup;
     765             :     Datum       values[Natts_pg_statistic_ext_data];
     766             :     bool        nulls[Natts_pg_statistic_ext_data];
     767             : 
     768         378 :     pg_stextdata = table_open(StatisticExtDataRelationId, RowExclusiveLock);
     769             : 
     770         378 :     memset(nulls, true, sizeof(nulls));
     771         378 :     memset(values, 0, sizeof(values));
     772             : 
     773             :     /* basic info */
     774         378 :     values[Anum_pg_statistic_ext_data_stxoid - 1] = ObjectIdGetDatum(statOid);
     775         378 :     nulls[Anum_pg_statistic_ext_data_stxoid - 1] = false;
     776             : 
     777         378 :     values[Anum_pg_statistic_ext_data_stxdinherit - 1] = BoolGetDatum(inh);
     778         378 :     nulls[Anum_pg_statistic_ext_data_stxdinherit - 1] = false;
     779             : 
     780             :     /*
     781             :      * Construct a new pg_statistic_ext_data tuple, replacing the calculated
     782             :      * stats.
     783             :      */
     784         378 :     if (ndistinct != NULL)
     785             :     {
     786         174 :         bytea      *data = statext_ndistinct_serialize(ndistinct);
     787             : 
     788         174 :         nulls[Anum_pg_statistic_ext_data_stxdndistinct - 1] = (data == NULL);
     789         174 :         values[Anum_pg_statistic_ext_data_stxdndistinct - 1] = PointerGetDatum(data);
     790             :     }
     791             : 
     792         378 :     if (dependencies != NULL)
     793             :     {
     794         114 :         bytea      *data = statext_dependencies_serialize(dependencies);
     795             : 
     796         114 :         nulls[Anum_pg_statistic_ext_data_stxddependencies - 1] = (data == NULL);
     797         114 :         values[Anum_pg_statistic_ext_data_stxddependencies - 1] = PointerGetDatum(data);
     798             :     }
     799         378 :     if (mcv != NULL)
     800             :     {
     801         204 :         bytea      *data = statext_mcv_serialize(mcv, stats);
     802             : 
     803         204 :         nulls[Anum_pg_statistic_ext_data_stxdmcv - 1] = (data == NULL);
     804         204 :         values[Anum_pg_statistic_ext_data_stxdmcv - 1] = PointerGetDatum(data);
     805             :     }
     806         378 :     if (exprs != (Datum) 0)
     807             :     {
     808         156 :         nulls[Anum_pg_statistic_ext_data_stxdexpr - 1] = false;
     809         156 :         values[Anum_pg_statistic_ext_data_stxdexpr - 1] = exprs;
     810             :     }
     811             : 
     812             :     /*
     813             :      * Delete the old tuple if it exists, and insert a new one. It's easier
     814             :      * than trying to update or insert, based on various conditions.
     815             :      */
     816         378 :     RemoveStatisticsDataById(statOid, inh);
     817             : 
     818             :     /* form and insert a new tuple */
     819         378 :     stup = heap_form_tuple(RelationGetDescr(pg_stextdata), values, nulls);
     820         378 :     CatalogTupleInsert(pg_stextdata, stup);
     821             : 
     822         378 :     heap_freetuple(stup);
     823             : 
     824         378 :     table_close(pg_stextdata, RowExclusiveLock);
     825         378 : }
     826             : 
     827             : /* initialize multi-dimensional sort */
     828             : MultiSortSupport
     829        1326 : multi_sort_init(int ndims)
     830             : {
     831             :     MultiSortSupport mss;
     832             : 
     833             :     Assert(ndims >= 2);
     834             : 
     835        1326 :     mss = (MultiSortSupport) palloc0(offsetof(MultiSortSupportData, ssup)
     836        1326 :                                      + sizeof(SortSupportData) * ndims);
     837             : 
     838        1326 :     mss->ndims = ndims;
     839             : 
     840        1326 :     return mss;
     841             : }
     842             : 
     843             : /*
     844             :  * Prepare sort support info using the given sort operator and collation
     845             :  * at the position 'sortdim'
     846             :  */
     847             : void
     848        3126 : multi_sort_add_dimension(MultiSortSupport mss, int sortdim,
     849             :                          Oid oper, Oid collation)
     850             : {
     851        3126 :     SortSupport ssup = &mss->ssup[sortdim];
     852             : 
     853        3126 :     ssup->ssup_cxt = CurrentMemoryContext;
     854        3126 :     ssup->ssup_collation = collation;
     855        3126 :     ssup->ssup_nulls_first = false;
     856             : 
     857        3126 :     PrepareSortSupportFromOrderingOp(oper, ssup);
     858        3126 : }
     859             : 
     860             : /* compare all the dimensions in the selected order */
     861             : int
     862    15300210 : multi_sort_compare(const void *a, const void *b, void *arg)
     863             : {
     864    15300210 :     MultiSortSupport mss = (MultiSortSupport) arg;
     865    15300210 :     SortItem   *ia = (SortItem *) a;
     866    15300210 :     SortItem   *ib = (SortItem *) b;
     867             :     int         i;
     868             : 
     869    27722880 :     for (i = 0; i < mss->ndims; i++)
     870             :     {
     871             :         int         compare;
     872             : 
     873    24105600 :         compare = ApplySortComparator(ia->values[i], ia->isnull[i],
     874    24105600 :                                       ib->values[i], ib->isnull[i],
     875    24105600 :                                       &mss->ssup[i]);
     876             : 
     877    24105600 :         if (compare != 0)
     878    11682930 :             return compare;
     879             :     }
     880             : 
     881             :     /* equal by default */
     882     3617280 :     return 0;
     883             : }
     884             : 
     885             : /* compare selected dimension */
     886             : int
     887     1472598 : multi_sort_compare_dim(int dim, const SortItem *a, const SortItem *b,
     888             :                        MultiSortSupport mss)
     889             : {
     890     2945196 :     return ApplySortComparator(a->values[dim], a->isnull[dim],
     891     1472598 :                                b->values[dim], b->isnull[dim],
     892     1472598 :                                &mss->ssup[dim]);
     893             : }
     894             : 
     895             : int
     896     1504266 : multi_sort_compare_dims(int start, int end,
     897             :                         const SortItem *a, const SortItem *b,
     898             :                         MultiSortSupport mss)
     899             : {
     900             :     int         dim;
     901             : 
     902     3403398 :     for (dim = start; dim <= end; dim++)
     903             :     {
     904     1930800 :         int         r = ApplySortComparator(a->values[dim], a->isnull[dim],
     905     1930800 :                                             b->values[dim], b->isnull[dim],
     906     1930800 :                                             &mss->ssup[dim]);
     907             : 
     908     1930800 :         if (r != 0)
     909       31668 :             return r;
     910             :     }
     911             : 
     912     1472598 :     return 0;
     913             : }
     914             : 
     915             : int
     916      198762 : compare_scalars_simple(const void *a, const void *b, void *arg)
     917             : {
     918      198762 :     return compare_datums_simple(*(Datum *) a,
     919             :                                  *(Datum *) b,
     920             :                                  (SortSupport) arg);
     921             : }
     922             : 
     923             : int
     924      224874 : compare_datums_simple(Datum a, Datum b, SortSupport ssup)
     925             : {
     926      224874 :     return ApplySortComparator(a, false, b, false, ssup);
     927             : }
     928             : 
     929             : /*
     930             :  * build_attnums_array
     931             :  *      Transforms a bitmap into an array of AttrNumber values.
     932             :  *
     933             :  * This is used for extended statistics only, so all the attributes must be
     934             :  * user-defined. That means offsetting by FirstLowInvalidHeapAttributeNumber
     935             :  * is not necessary here (and when querying the bitmap).
     936             :  */
     937             : AttrNumber *
     938           0 : build_attnums_array(Bitmapset *attrs, int nexprs, int *numattrs)
     939             : {
     940             :     int         i,
     941             :                 j;
     942             :     AttrNumber *attnums;
     943           0 :     int         num = bms_num_members(attrs);
     944             : 
     945           0 :     if (numattrs)
     946           0 :         *numattrs = num;
     947             : 
     948             :     /* build attnums from the bitmapset */
     949           0 :     attnums = (AttrNumber *) palloc(sizeof(AttrNumber) * num);
     950           0 :     i = 0;
     951           0 :     j = -1;
     952           0 :     while ((j = bms_next_member(attrs, j)) >= 0)
     953             :     {
     954           0 :         int         attnum = (j - nexprs);
     955             : 
     956             :         /*
     957             :          * Make sure the bitmap contains only user-defined attributes. As
     958             :          * bitmaps can't contain negative values, this can be violated in two
     959             :          * ways. Firstly, the bitmap might contain 0 as a member, and secondly
     960             :          * the integer value might be larger than MaxAttrNumber.
     961             :          */
     962             :         Assert(AttributeNumberIsValid(attnum));
     963             :         Assert(attnum <= MaxAttrNumber);
     964             :         Assert(attnum >= (-nexprs));
     965             : 
     966           0 :         attnums[i++] = (AttrNumber) attnum;
     967             : 
     968             :         /* protect against overflows */
     969             :         Assert(i <= num);
     970             :     }
     971             : 
     972           0 :     return attnums;
     973             : }
     974             : 
     975             : /*
     976             :  * build_sorted_items
     977             :  *      build a sorted array of SortItem with values from rows
     978             :  *
     979             :  * Note: All the memory is allocated in a single chunk, so that the caller
     980             :  * can simply pfree the return value to release all of it.
     981             :  */
     982             : SortItem *
     983         804 : build_sorted_items(StatsBuildData *data, int *nitems,
     984             :                    MultiSortSupport mss,
     985             :                    int numattrs, AttrNumber *attnums)
     986             : {
     987             :     int         i,
     988             :                 j,
     989             :                 len,
     990             :                 nrows;
     991         804 :     int         nvalues = data->numrows * numattrs;
     992             : 
     993             :     SortItem   *items;
     994             :     Datum      *values;
     995             :     bool       *isnull;
     996             :     char       *ptr;
     997             :     int        *typlen;
     998             : 
     999             :     /* Compute the total amount of memory we need (both items and values). */
    1000         804 :     len = data->numrows * sizeof(SortItem) + nvalues * (sizeof(Datum) + sizeof(bool));
    1001             : 
    1002             :     /* Allocate the memory and split it into the pieces. */
    1003         804 :     ptr = palloc0(len);
    1004             : 
    1005             :     /* items to sort */
    1006         804 :     items = (SortItem *) ptr;
    1007         804 :     ptr += data->numrows * sizeof(SortItem);
    1008             : 
    1009             :     /* values and null flags */
    1010         804 :     values = (Datum *) ptr;
    1011         804 :     ptr += nvalues * sizeof(Datum);
    1012             : 
    1013         804 :     isnull = (bool *) ptr;
    1014         804 :     ptr += nvalues * sizeof(bool);
    1015             : 
    1016             :     /* make sure we consumed the whole buffer exactly */
    1017             :     Assert((ptr - (char *) items) == len);
    1018             : 
    1019             :     /* fix the pointers to Datum and bool arrays */
    1020         804 :     nrows = 0;
    1021     1988724 :     for (i = 0; i < data->numrows; i++)
    1022             :     {
    1023     1987920 :         items[nrows].values = &values[nrows * numattrs];
    1024     1987920 :         items[nrows].isnull = &isnull[nrows * numattrs];
    1025             : 
    1026     1987920 :         nrows++;
    1027             :     }
    1028             : 
    1029             :     /* build a local cache of typlen for all attributes */
    1030         804 :     typlen = (int *) palloc(sizeof(int) * data->nattnums);
    1031        2994 :     for (i = 0; i < data->nattnums; i++)
    1032        2190 :         typlen[i] = get_typlen(data->stats[i]->attrtypid);
    1033             : 
    1034         804 :     nrows = 0;
    1035     1988724 :     for (i = 0; i < data->numrows; i++)
    1036             :     {
    1037     1987920 :         bool        toowide = false;
    1038             : 
    1039             :         /* load the values/null flags from sample rows */
    1040     6840336 :         for (j = 0; j < numattrs; j++)
    1041             :         {
    1042             :             Datum       value;
    1043             :             bool        isnull;
    1044             :             int         attlen;
    1045     4852416 :             AttrNumber  attnum = attnums[j];
    1046             : 
    1047             :             int         idx;
    1048             : 
    1049             :             /* match attnum to the pre-calculated data */
    1050     9565452 :             for (idx = 0; idx < data->nattnums; idx++)
    1051             :             {
    1052     9565452 :                 if (attnum == data->attnums[idx])
    1053     4852416 :                     break;
    1054             :             }
    1055             : 
    1056             :             Assert(idx < data->nattnums);
    1057             : 
    1058     4852416 :             value = data->values[idx][i];
    1059     4852416 :             isnull = data->nulls[idx][i];
    1060     4852416 :             attlen = typlen[idx];
    1061             : 
    1062             :             /*
    1063             :              * If this is a varlena value, check if it's too wide and if yes
    1064             :              * then skip the whole item. Otherwise detoast the value.
    1065             :              *
    1066             :              * XXX It may happen that we've already detoasted some preceding
    1067             :              * values for the current item. We don't bother to cleanup those
    1068             :              * on the assumption that those are small (below WIDTH_THRESHOLD)
    1069             :              * and will be discarded at the end of analyze.
    1070             :              */
    1071     4852416 :             if ((!isnull) && (attlen == -1))
    1072             :             {
    1073     1480308 :                 if (toast_raw_datum_size(value) > WIDTH_THRESHOLD)
    1074             :                 {
    1075           0 :                     toowide = true;
    1076           0 :                     break;
    1077             :                 }
    1078             : 
    1079     1480308 :                 value = PointerGetDatum(PG_DETOAST_DATUM(value));
    1080             :             }
    1081             : 
    1082     4852416 :             items[nrows].values[j] = value;
    1083     4852416 :             items[nrows].isnull[j] = isnull;
    1084             :         }
    1085             : 
    1086     1987920 :         if (toowide)
    1087           0 :             continue;
    1088             : 
    1089     1987920 :         nrows++;
    1090             :     }
    1091             : 
    1092             :     /* store the actual number of items (ignoring the too-wide ones) */
    1093         804 :     *nitems = nrows;
    1094             : 
    1095             :     /* all items were too wide */
    1096         804 :     if (nrows == 0)
    1097             :     {
    1098             :         /* everything is allocated as a single chunk */
    1099           0 :         pfree(items);
    1100           0 :         return NULL;
    1101             :     }
    1102             : 
    1103             :     /* do the sort, using the multi-sort */
    1104         804 :     qsort_interruptible(items, nrows, sizeof(SortItem),
    1105             :                         multi_sort_compare, mss);
    1106             : 
    1107         804 :     return items;
    1108             : }
    1109             : 
    1110             : /*
    1111             :  * has_stats_of_kind
    1112             :  *      Check whether the list contains statistic of a given kind
    1113             :  */
    1114             : bool
    1115        4842 : has_stats_of_kind(List *stats, char requiredkind)
    1116             : {
    1117             :     ListCell   *l;
    1118             : 
    1119        8028 :     foreach(l, stats)
    1120             :     {
    1121        5604 :         StatisticExtInfo *stat = (StatisticExtInfo *) lfirst(l);
    1122             : 
    1123        5604 :         if (stat->kind == requiredkind)
    1124        2418 :             return true;
    1125             :     }
    1126             : 
    1127        2424 :     return false;
    1128             : }
    1129             : 
    1130             : /*
    1131             :  * stat_find_expression
    1132             :  *      Search for an expression in statistics object's list of expressions.
    1133             :  *
    1134             :  * Returns the index of the expression in the statistics object's list of
    1135             :  * expressions, or -1 if not found.
    1136             :  */
    1137             : static int
    1138         516 : stat_find_expression(StatisticExtInfo *stat, Node *expr)
    1139             : {
    1140             :     ListCell   *lc;
    1141             :     int         idx;
    1142             : 
    1143         516 :     idx = 0;
    1144         996 :     foreach(lc, stat->exprs)
    1145             :     {
    1146         972 :         Node       *stat_expr = (Node *) lfirst(lc);
    1147             : 
    1148         972 :         if (equal(stat_expr, expr))
    1149         492 :             return idx;
    1150         480 :         idx++;
    1151             :     }
    1152             : 
    1153             :     /* Expression not found */
    1154          24 :     return -1;
    1155             : }
    1156             : 
    1157             : /*
    1158             :  * stat_covers_expressions
    1159             :  *      Test whether a statistics object covers all expressions in a list.
    1160             :  *
    1161             :  * Returns true if all expressions are covered.  If expr_idxs is non-NULL, it
    1162             :  * is populated with the indexes of the expressions found.
    1163             :  */
    1164             : static bool
    1165        3030 : stat_covers_expressions(StatisticExtInfo *stat, List *exprs,
    1166             :                         Bitmapset **expr_idxs)
    1167             : {
    1168             :     ListCell   *lc;
    1169             : 
    1170        3522 :     foreach(lc, exprs)
    1171             :     {
    1172         516 :         Node       *expr = (Node *) lfirst(lc);
    1173             :         int         expr_idx;
    1174             : 
    1175         516 :         expr_idx = stat_find_expression(stat, expr);
    1176         516 :         if (expr_idx == -1)
    1177          24 :             return false;
    1178             : 
    1179         492 :         if (expr_idxs != NULL)
    1180         246 :             *expr_idxs = bms_add_member(*expr_idxs, expr_idx);
    1181             :     }
    1182             : 
    1183             :     /* If we reach here, all expressions are covered */
    1184        3006 :     return true;
    1185             : }
    1186             : 
    1187             : /*
    1188             :  * choose_best_statistics
    1189             :  *      Look for and return statistics with the specified 'requiredkind' which
    1190             :  *      have keys that match at least two of the given attnums.  Return NULL if
    1191             :  *      there's no match.
    1192             :  *
    1193             :  * The current selection criteria is very simple - we choose the statistics
    1194             :  * object referencing the most attributes in covered (and still unestimated
    1195             :  * clauses), breaking ties in favor of objects with fewer keys overall.
    1196             :  *
    1197             :  * The clause_attnums is an array of bitmaps, storing attnums for individual
    1198             :  * clauses. A NULL element means the clause is either incompatible or already
    1199             :  * estimated.
    1200             :  *
    1201             :  * XXX If multiple statistics objects tie on both criteria, then which object
    1202             :  * is chosen depends on the order that they appear in the stats list. Perhaps
    1203             :  * further tiebreakers are needed.
    1204             :  */
    1205             : StatisticExtInfo *
    1206        1236 : choose_best_statistics(List *stats, char requiredkind, bool inh,
    1207             :                        Bitmapset **clause_attnums, List **clause_exprs,
    1208             :                        int nclauses)
    1209             : {
    1210             :     ListCell   *lc;
    1211        1236 :     StatisticExtInfo *best_match = NULL;
    1212        1236 :     int         best_num_matched = 2;   /* goal #1: maximize */
    1213        1236 :     int         best_match_keys = (STATS_MAX_DIMENSIONS + 1);   /* goal #2: minimize */
    1214             : 
    1215        3318 :     foreach(lc, stats)
    1216             :     {
    1217             :         int         i;
    1218        2082 :         StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
    1219        2082 :         Bitmapset  *matched_attnums = NULL;
    1220        2082 :         Bitmapset  *matched_exprs = NULL;
    1221             :         int         num_matched;
    1222             :         int         numkeys;
    1223             : 
    1224             :         /* skip statistics that are not of the correct type */
    1225        2082 :         if (info->kind != requiredkind)
    1226         468 :             continue;
    1227             : 
    1228             :         /* skip statistics with mismatching inheritance flag */
    1229        1614 :         if (info->inherit != inh)
    1230          24 :             continue;
    1231             : 
    1232             :         /*
    1233             :          * Collect attributes and expressions in remaining (unestimated)
    1234             :          * clauses fully covered by this statistic object.
    1235             :          *
    1236             :          * We know already estimated clauses have both clause_attnums and
    1237             :          * clause_exprs set to NULL. We leave the pointers NULL if already
    1238             :          * estimated, or we reset them to NULL after estimating the clause.
    1239             :          */
    1240        5418 :         for (i = 0; i < nclauses; i++)
    1241             :         {
    1242        3828 :             Bitmapset  *expr_idxs = NULL;
    1243             : 
    1244             :             /* ignore incompatible/estimated clauses */
    1245        3828 :             if (!clause_attnums[i] && !clause_exprs[i])
    1246        2226 :                 continue;
    1247             : 
    1248             :             /* ignore clauses that are not covered by this object */
    1249        1950 :             if (!bms_is_subset(clause_attnums[i], info->keys) ||
    1250        1620 :                 !stat_covers_expressions(info, clause_exprs[i], &expr_idxs))
    1251         348 :                 continue;
    1252             : 
    1253             :             /* record attnums and indexes of expressions covered */
    1254        1602 :             matched_attnums = bms_add_members(matched_attnums, clause_attnums[i]);
    1255        1602 :             matched_exprs = bms_add_members(matched_exprs, expr_idxs);
    1256             :         }
    1257             : 
    1258        1590 :         num_matched = bms_num_members(matched_attnums) + bms_num_members(matched_exprs);
    1259             : 
    1260        1590 :         bms_free(matched_attnums);
    1261        1590 :         bms_free(matched_exprs);
    1262             : 
    1263             :         /*
    1264             :          * save the actual number of keys in the stats so that we can choose
    1265             :          * the narrowest stats with the most matching keys.
    1266             :          */
    1267        1590 :         numkeys = bms_num_members(info->keys) + list_length(info->exprs);
    1268             : 
    1269             :         /*
    1270             :          * Use this object when it increases the number of matched attributes
    1271             :          * and expressions or when it matches the same number of attributes
    1272             :          * and expressions but these stats have fewer keys than any previous
    1273             :          * match.
    1274             :          */
    1275        1590 :         if (num_matched > best_num_matched ||
    1276         414 :             (num_matched == best_num_matched && numkeys < best_match_keys))
    1277             :         {
    1278         606 :             best_match = info;
    1279         606 :             best_num_matched = num_matched;
    1280         606 :             best_match_keys = numkeys;
    1281             :         }
    1282             :     }
    1283             : 
    1284        1236 :     return best_match;
    1285             : }
    1286             : 
    1287             : /*
    1288             :  * statext_is_compatible_clause_internal
    1289             :  *      Determines if the clause is compatible with MCV lists.
    1290             :  *
    1291             :  * To be compatible, the given clause must be a combination of supported
    1292             :  * clauses built from Vars or sub-expressions (where a sub-expression is
    1293             :  * something that exactly matches an expression found in statistics objects).
    1294             :  * This function recursively examines the clause and extracts any
    1295             :  * sub-expressions that will need to be matched against statistics.
    1296             :  *
    1297             :  * Currently, we only support the following types of clauses:
    1298             :  *
    1299             :  * (a) OpExprs of the form (Var/Expr op Const), or (Const op Var/Expr), where
    1300             :  * the op is one of ("=", "<", ">", ">=", "<=")
    1301             :  *
    1302             :  * (b) (Var/Expr IS [NOT] NULL)
    1303             :  *
    1304             :  * (c) combinations using AND/OR/NOT
    1305             :  *
    1306             :  * (d) ScalarArrayOpExprs of the form (Var/Expr op ANY (Const)) or
    1307             :  * (Var/Expr op ALL (Const))
    1308             :  *
    1309             :  * In the future, the range of supported clauses may be expanded to more
    1310             :  * complex cases, for example (Var op Var).
    1311             :  *
    1312             :  * Arguments:
    1313             :  * clause: (sub)clause to be inspected (bare clause, not a RestrictInfo)
    1314             :  * relid: rel that all Vars in clause must belong to
    1315             :  * *attnums: input/output parameter collecting attribute numbers of all
    1316             :  *      mentioned Vars.  Note that we do not offset the attribute numbers,
    1317             :  *      so we can't cope with system columns.
    1318             :  * *exprs: input/output parameter collecting primitive subclauses within
    1319             :  *      the clause tree
    1320             :  *
    1321             :  * Returns false if there is something we definitively can't handle.
    1322             :  * On true return, we can proceed to match the *exprs against statistics.
    1323             :  */
    1324             : static bool
    1325        2922 : statext_is_compatible_clause_internal(PlannerInfo *root, Node *clause,
    1326             :                                       Index relid, Bitmapset **attnums,
    1327             :                                       List **exprs)
    1328             : {
    1329             :     /* Look inside any binary-compatible relabeling (as in examine_variable) */
    1330        2922 :     if (IsA(clause, RelabelType))
    1331           0 :         clause = (Node *) ((RelabelType *) clause)->arg;
    1332             : 
    1333             :     /* plain Var references (boolean Vars or recursive checks) */
    1334        2922 :     if (IsA(clause, Var))
    1335             :     {
    1336        1344 :         Var        *var = (Var *) clause;
    1337             : 
    1338             :         /* Ensure var is from the correct relation */
    1339        1344 :         if (var->varno != relid)
    1340           0 :             return false;
    1341             : 
    1342             :         /* we also better ensure the Var is from the current level */
    1343        1344 :         if (var->varlevelsup > 0)
    1344           0 :             return false;
    1345             : 
    1346             :         /*
    1347             :          * Also reject system attributes and whole-row Vars (we don't allow
    1348             :          * stats on those).
    1349             :          */
    1350        1344 :         if (!AttrNumberIsForUserDefinedAttr(var->varattno))
    1351           0 :             return false;
    1352             : 
    1353             :         /* OK, record the attnum for later permissions checks. */
    1354        1344 :         *attnums = bms_add_member(*attnums, var->varattno);
    1355             : 
    1356        1344 :         return true;
    1357             :     }
    1358             : 
    1359             :     /* (Var/Expr op Const) or (Const op Var/Expr) */
    1360        1578 :     if (is_opclause(clause))
    1361             :     {
    1362        1140 :         RangeTblEntry *rte = root->simple_rte_array[relid];
    1363        1140 :         OpExpr     *expr = (OpExpr *) clause;
    1364             :         Node       *clause_expr;
    1365             : 
    1366             :         /* Only expressions with two arguments are considered compatible. */
    1367        1140 :         if (list_length(expr->args) != 2)
    1368           0 :             return false;
    1369             : 
    1370             :         /* Check if the expression has the right shape */
    1371        1140 :         if (!examine_opclause_args(expr->args, &clause_expr, NULL, NULL))
    1372           0 :             return false;
    1373             : 
    1374             :         /*
    1375             :          * If it's not one of the supported operators ("=", "<", ">", etc.),
    1376             :          * just ignore the clause, as it's not compatible with MCV lists.
    1377             :          *
    1378             :          * This uses the function for estimating selectivity, not the operator
    1379             :          * directly (a bit awkward, but well ...).
    1380             :          */
    1381        1140 :         switch (get_oprrest(expr->opno))
    1382             :         {
    1383        1140 :             case F_EQSEL:
    1384             :             case F_NEQSEL:
    1385             :             case F_SCALARLTSEL:
    1386             :             case F_SCALARLESEL:
    1387             :             case F_SCALARGTSEL:
    1388             :             case F_SCALARGESEL:
    1389             :                 /* supported, will continue with inspection of the Var/Expr */
    1390        1140 :                 break;
    1391             : 
    1392           0 :             default:
    1393             :                 /* other estimators are considered unknown/unsupported */
    1394           0 :                 return false;
    1395             :         }
    1396             : 
    1397             :         /*
    1398             :          * If there are any securityQuals on the RTE from security barrier
    1399             :          * views or RLS policies, then the user may not have access to all the
    1400             :          * table's data, and we must check that the operator is leakproof.
    1401             :          *
    1402             :          * If the operator is leaky, then we must ignore this clause for the
    1403             :          * purposes of estimating with MCV lists, otherwise the operator might
    1404             :          * reveal values from the MCV list that the user doesn't have
    1405             :          * permission to see.
    1406             :          */
    1407        1140 :         if (rte->securityQuals != NIL &&
    1408           0 :             !get_func_leakproof(get_opcode(expr->opno)))
    1409           0 :             return false;
    1410             : 
    1411             :         /* Check (Var op Const) or (Const op Var) clauses by recursing. */
    1412        1140 :         if (IsA(clause_expr, Var))
    1413         948 :             return statext_is_compatible_clause_internal(root, clause_expr,
    1414             :                                                          relid, attnums, exprs);
    1415             : 
    1416             :         /* Otherwise we have (Expr op Const) or (Const op Expr). */
    1417         192 :         *exprs = lappend(*exprs, clause_expr);
    1418         192 :         return true;
    1419             :     }
    1420             : 
    1421             :     /* Var/Expr IN Array */
    1422         438 :     if (IsA(clause, ScalarArrayOpExpr))
    1423             :     {
    1424         288 :         RangeTblEntry *rte = root->simple_rte_array[relid];
    1425         288 :         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) clause;
    1426             :         Node       *clause_expr;
    1427             :         bool        expronleft;
    1428             : 
    1429             :         /* Only expressions with two arguments are considered compatible. */
    1430         288 :         if (list_length(expr->args) != 2)
    1431           0 :             return false;
    1432             : 
    1433             :         /* Check if the expression has the right shape (one Var, one Const) */
    1434         288 :         if (!examine_opclause_args(expr->args, &clause_expr, NULL, &expronleft))
    1435           0 :             return false;
    1436             : 
    1437             :         /* We only support Var on left, Const on right */
    1438         288 :         if (!expronleft)
    1439           6 :             return false;
    1440             : 
    1441             :         /*
    1442             :          * If it's not one of the supported operators ("=", "<", ">", etc.),
    1443             :          * just ignore the clause, as it's not compatible with MCV lists.
    1444             :          *
    1445             :          * This uses the function for estimating selectivity, not the operator
    1446             :          * directly (a bit awkward, but well ...).
    1447             :          */
    1448         282 :         switch (get_oprrest(expr->opno))
    1449             :         {
    1450         282 :             case F_EQSEL:
    1451             :             case F_NEQSEL:
    1452             :             case F_SCALARLTSEL:
    1453             :             case F_SCALARLESEL:
    1454             :             case F_SCALARGTSEL:
    1455             :             case F_SCALARGESEL:
    1456             :                 /* supported, will continue with inspection of the Var/Expr */
    1457         282 :                 break;
    1458             : 
    1459           0 :             default:
    1460             :                 /* other estimators are considered unknown/unsupported */
    1461           0 :                 return false;
    1462             :         }
    1463             : 
    1464             :         /*
    1465             :          * If there are any securityQuals on the RTE from security barrier
    1466             :          * views or RLS policies, then the user may not have access to all the
    1467             :          * table's data, and we must check that the operator is leakproof.
    1468             :          *
    1469             :          * If the operator is leaky, then we must ignore this clause for the
    1470             :          * purposes of estimating with MCV lists, otherwise the operator might
    1471             :          * reveal values from the MCV list that the user doesn't have
    1472             :          * permission to see.
    1473             :          */
    1474         282 :         if (rte->securityQuals != NIL &&
    1475           0 :             !get_func_leakproof(get_opcode(expr->opno)))
    1476           0 :             return false;
    1477             : 
    1478             :         /* Check Var IN Array clauses by recursing. */
    1479         282 :         if (IsA(clause_expr, Var))
    1480         228 :             return statext_is_compatible_clause_internal(root, clause_expr,
    1481             :                                                          relid, attnums, exprs);
    1482             : 
    1483             :         /* Otherwise we have Expr IN Array. */
    1484          54 :         *exprs = lappend(*exprs, clause_expr);
    1485          54 :         return true;
    1486             :     }
    1487             : 
    1488             :     /* AND/OR/NOT clause */
    1489         300 :     if (is_andclause(clause) ||
    1490         282 :         is_orclause(clause) ||
    1491         132 :         is_notclause(clause))
    1492             :     {
    1493             :         /*
    1494             :          * AND/OR/NOT-clauses are supported if all sub-clauses are supported
    1495             :          *
    1496             :          * Perhaps we could improve this by handling mixed cases, when some of
    1497             :          * the clauses are supported and some are not. Selectivity for the
    1498             :          * supported subclauses would be computed using extended statistics,
    1499             :          * and the remaining clauses would be estimated using the traditional
    1500             :          * algorithm (product of selectivities).
    1501             :          *
    1502             :          * It however seems overly complex, and in a way we already do that
    1503             :          * because if we reject the whole clause as unsupported here, it will
    1504             :          * be eventually passed to clauselist_selectivity() which does exactly
    1505             :          * this (split into supported/unsupported clauses etc).
    1506             :          */
    1507          48 :         BoolExpr   *expr = (BoolExpr *) clause;
    1508             :         ListCell   *lc;
    1509             : 
    1510         114 :         foreach(lc, expr->args)
    1511             :         {
    1512             :             /*
    1513             :              * If we find an incompatible clause in the arguments, treat the
    1514             :              * whole clause as incompatible.
    1515             :              */
    1516          66 :             if (!statext_is_compatible_clause_internal(root,
    1517          66 :                                                        (Node *) lfirst(lc),
    1518             :                                                        relid, attnums, exprs))
    1519           0 :                 return false;
    1520             :         }
    1521             : 
    1522          48 :         return true;
    1523             :     }
    1524             : 
    1525             :     /* Var/Expr IS NULL */
    1526         102 :     if (IsA(clause, NullTest))
    1527             :     {
    1528          96 :         NullTest   *nt = (NullTest *) clause;
    1529             : 
    1530             :         /* Check Var IS NULL clauses by recursing. */
    1531          96 :         if (IsA(nt->arg, Var))
    1532          90 :             return statext_is_compatible_clause_internal(root, (Node *) (nt->arg),
    1533             :                                                          relid, attnums, exprs);
    1534             : 
    1535             :         /* Otherwise we have Expr IS NULL. */
    1536           6 :         *exprs = lappend(*exprs, nt->arg);
    1537           6 :         return true;
    1538             :     }
    1539             : 
    1540             :     /*
    1541             :      * Treat any other expressions as bare expressions to be matched against
    1542             :      * expressions in statistics objects.
    1543             :      */
    1544           6 :     *exprs = lappend(*exprs, clause);
    1545           6 :     return true;
    1546             : }
    1547             : 
    1548             : /*
    1549             :  * statext_is_compatible_clause
    1550             :  *      Determines if the clause is compatible with MCV lists.
    1551             :  *
    1552             :  * See statext_is_compatible_clause_internal, above, for the basic rules.
    1553             :  * This layer deals with RestrictInfo superstructure and applies permissions
    1554             :  * checks to verify that it's okay to examine all mentioned Vars.
    1555             :  *
    1556             :  * Arguments:
    1557             :  * clause: clause to be inspected (in RestrictInfo form)
    1558             :  * relid: rel that all Vars in clause must belong to
    1559             :  * *attnums: input/output parameter collecting attribute numbers of all
    1560             :  *      mentioned Vars.  Note that we do not offset the attribute numbers,
    1561             :  *      so we can't cope with system columns.
    1562             :  * *exprs: input/output parameter collecting primitive subclauses within
    1563             :  *      the clause tree
    1564             :  *
    1565             :  * Returns false if there is something we definitively can't handle.
    1566             :  * On true return, we can proceed to match the *exprs against statistics.
    1567             :  */
    1568             : static bool
    1569        1644 : statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid,
    1570             :                              Bitmapset **attnums, List **exprs)
    1571             : {
    1572        1644 :     RangeTblEntry *rte = root->simple_rte_array[relid];
    1573        1644 :     RelOptInfo *rel = root->simple_rel_array[relid];
    1574             :     RestrictInfo *rinfo;
    1575             :     int         clause_relid;
    1576             :     Oid         userid;
    1577             : 
    1578             :     /*
    1579             :      * Special-case handling for bare BoolExpr AND clauses, because the
    1580             :      * restrictinfo machinery doesn't build RestrictInfos on top of AND
    1581             :      * clauses.
    1582             :      */
    1583        1644 :     if (is_andclause(clause))
    1584             :     {
    1585          48 :         BoolExpr   *expr = (BoolExpr *) clause;
    1586             :         ListCell   *lc;
    1587             : 
    1588             :         /*
    1589             :          * Check that each sub-clause is compatible.  We expect these to be
    1590             :          * RestrictInfos.
    1591             :          */
    1592         162 :         foreach(lc, expr->args)
    1593             :         {
    1594         114 :             if (!statext_is_compatible_clause(root, (Node *) lfirst(lc),
    1595             :                                               relid, attnums, exprs))
    1596           0 :                 return false;
    1597             :         }
    1598             : 
    1599          48 :         return true;
    1600             :     }
    1601             : 
    1602             :     /* Otherwise it must be a RestrictInfo. */
    1603        1596 :     if (!IsA(clause, RestrictInfo))
    1604           0 :         return false;
    1605        1596 :     rinfo = (RestrictInfo *) clause;
    1606             : 
    1607             :     /* Pseudoconstants are not really interesting here. */
    1608        1596 :     if (rinfo->pseudoconstant)
    1609           6 :         return false;
    1610             : 
    1611             :     /* Clauses referencing other varnos are incompatible. */
    1612        1590 :     if (!bms_get_singleton_member(rinfo->clause_relids, &clause_relid) ||
    1613        1590 :         clause_relid != relid)
    1614           0 :         return false;
    1615             : 
    1616             :     /* Check the clause and determine what attributes it references. */
    1617        1590 :     if (!statext_is_compatible_clause_internal(root, (Node *) rinfo->clause,
    1618             :                                                relid, attnums, exprs))
    1619           6 :         return false;
    1620             : 
    1621             :     /*
    1622             :      * Check that the user has permission to read all required attributes.
    1623             :      */
    1624        1584 :     userid = OidIsValid(rel->userid) ? rel->userid : GetUserId();
    1625             : 
    1626             :     /* Table-level SELECT privilege is sufficient for all columns */
    1627        1584 :     if (pg_class_aclcheck(rte->relid, userid, ACL_SELECT) != ACLCHECK_OK)
    1628             :     {
    1629          48 :         Bitmapset  *clause_attnums = NULL;
    1630          48 :         int         attnum = -1;
    1631             : 
    1632             :         /*
    1633             :          * We have to check per-column privileges.  *attnums has the attnums
    1634             :          * for individual Vars we saw, but there may also be Vars within
    1635             :          * subexpressions in *exprs.  We can use pull_varattnos() to extract
    1636             :          * those, but there's an impedance mismatch: attnums returned by
    1637             :          * pull_varattnos() are offset by FirstLowInvalidHeapAttributeNumber,
    1638             :          * while attnums within *attnums aren't.  Convert *attnums to the
    1639             :          * offset style so we can combine the results.
    1640             :          */
    1641          90 :         while ((attnum = bms_next_member(*attnums, attnum)) >= 0)
    1642             :         {
    1643          42 :             clause_attnums =
    1644          42 :                 bms_add_member(clause_attnums,
    1645             :                                attnum - FirstLowInvalidHeapAttributeNumber);
    1646             :         }
    1647             : 
    1648             :         /* Now merge attnums from *exprs into clause_attnums */
    1649          48 :         if (*exprs != NIL)
    1650           6 :             pull_varattnos((Node *) *exprs, relid, &clause_attnums);
    1651             : 
    1652          48 :         attnum = -1;
    1653          48 :         while ((attnum = bms_next_member(clause_attnums, attnum)) >= 0)
    1654             :         {
    1655             :             /* Undo the offset */
    1656          48 :             AttrNumber  attno = attnum + FirstLowInvalidHeapAttributeNumber;
    1657             : 
    1658          48 :             if (attno == InvalidAttrNumber)
    1659             :             {
    1660             :                 /* Whole-row reference, so must have access to all columns */
    1661           6 :                 if (pg_attribute_aclcheck_all(rte->relid, userid, ACL_SELECT,
    1662             :                                               ACLMASK_ALL) != ACLCHECK_OK)
    1663          48 :                     return false;
    1664             :             }
    1665             :             else
    1666             :             {
    1667          42 :                 if (pg_attribute_aclcheck(rte->relid, attno, userid,
    1668             :                                           ACL_SELECT) != ACLCHECK_OK)
    1669          42 :                     return false;
    1670             :             }
    1671             :         }
    1672             :     }
    1673             : 
    1674             :     /* If we reach here, the clause is OK */
    1675        1536 :     return true;
    1676             : }
    1677             : 
    1678             : /*
    1679             :  * statext_mcv_clauselist_selectivity
    1680             :  *      Estimate clauses using the best multi-column statistics.
    1681             :  *
    1682             :  * Applies available extended (multi-column) statistics on a table. There may
    1683             :  * be multiple applicable statistics (with respect to the clauses), in which
    1684             :  * case we use greedy approach. In each round we select the best statistic on
    1685             :  * a table (measured by the number of attributes extracted from the clauses
    1686             :  * and covered by it), and compute the selectivity for the supplied clauses.
    1687             :  * We repeat this process with the remaining clauses (if any), until none of
    1688             :  * the available statistics can be used.
    1689             :  *
    1690             :  * One of the main challenges with using MCV lists is how to extrapolate the
    1691             :  * estimate to the data not covered by the MCV list. To do that, we compute
    1692             :  * not only the "MCV selectivity" (selectivities for MCV items matching the
    1693             :  * supplied clauses), but also the following related selectivities:
    1694             :  *
    1695             :  * - simple selectivity:  Computed without extended statistics, i.e. as if the
    1696             :  * columns/clauses were independent.
    1697             :  *
    1698             :  * - base selectivity:  Similar to simple selectivity, but is computed using
    1699             :  * the extended statistic by adding up the base frequencies (that we compute
    1700             :  * and store for each MCV item) of matching MCV items.
    1701             :  *
    1702             :  * - total selectivity: Selectivity covered by the whole MCV list.
    1703             :  *
    1704             :  * These are passed to mcv_combine_selectivities() which combines them to
    1705             :  * produce a selectivity estimate that makes use of both per-column statistics
    1706             :  * and the multi-column MCV statistics.
    1707             :  *
    1708             :  * 'estimatedclauses' is an input/output parameter.  We set bits for the
    1709             :  * 0-based 'clauses' indexes we estimate for and also skip clause items that
    1710             :  * already have a bit set.
    1711             :  */
    1712             : static Selectivity
    1713        2478 : statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid,
    1714             :                                    JoinType jointype, SpecialJoinInfo *sjinfo,
    1715             :                                    RelOptInfo *rel, Bitmapset **estimatedclauses,
    1716             :                                    bool is_or)
    1717             : {
    1718             :     ListCell   *l;
    1719             :     Bitmapset **list_attnums;   /* attnums extracted from the clause */
    1720             :     List      **list_exprs;     /* expressions matched to any statistic */
    1721             :     int         listidx;
    1722        2478 :     Selectivity sel = (is_or) ? 0.0 : 1.0;
    1723        2478 :     RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
    1724             : 
    1725             :     /* check if there's any stats that might be useful for us. */
    1726        2478 :     if (!has_stats_of_kind(rel->statlist, STATS_EXT_MCV))
    1727        1848 :         return sel;
    1728             : 
    1729         630 :     list_attnums = (Bitmapset **) palloc(sizeof(Bitmapset *) *
    1730         630 :                                          list_length(clauses));
    1731             : 
    1732             :     /* expressions extracted from complex expressions */
    1733         630 :     list_exprs = (List **) palloc(sizeof(Node *) * list_length(clauses));
    1734             : 
    1735             :     /*
    1736             :      * Pre-process the clauses list to extract the attnums and expressions
    1737             :      * seen in each item.  We need to determine if there are any clauses which
    1738             :      * will be useful for selectivity estimations with extended stats.  Along
    1739             :      * the way we'll record all of the attnums and expressions for each clause
    1740             :      * in lists which we'll reference later so we don't need to repeat the
    1741             :      * same work again.
    1742             :      *
    1743             :      * We also skip clauses that we already estimated using different types of
    1744             :      * statistics (we treat them as incompatible).
    1745             :      */
    1746         630 :     listidx = 0;
    1747        2160 :     foreach(l, clauses)
    1748             :     {
    1749        1530 :         Node       *clause = (Node *) lfirst(l);
    1750        1530 :         Bitmapset  *attnums = NULL;
    1751        1530 :         List       *exprs = NIL;
    1752             : 
    1753        3060 :         if (!bms_is_member(listidx, *estimatedclauses) &&
    1754        1530 :             statext_is_compatible_clause(root, clause, rel->relid, &attnums, &exprs))
    1755             :         {
    1756        1470 :             list_attnums[listidx] = attnums;
    1757        1470 :             list_exprs[listidx] = exprs;
    1758             :         }
    1759             :         else
    1760             :         {
    1761          60 :             list_attnums[listidx] = NULL;
    1762          60 :             list_exprs[listidx] = NIL;
    1763             :         }
    1764             : 
    1765        1530 :         listidx++;
    1766             :     }
    1767             : 
    1768             :     /* apply as many extended statistics as possible */
    1769             :     while (true)
    1770         606 :     {
    1771             :         StatisticExtInfo *stat;
    1772             :         List       *stat_clauses;
    1773             :         Bitmapset  *simple_clauses;
    1774             : 
    1775             :         /* find the best suited statistics object for these attnums */
    1776        1236 :         stat = choose_best_statistics(rel->statlist, STATS_EXT_MCV, rte->inh,
    1777             :                                       list_attnums, list_exprs,
    1778             :                                       list_length(clauses));
    1779             : 
    1780             :         /*
    1781             :          * if no (additional) matching stats could be found then we've nothing
    1782             :          * to do
    1783             :          */
    1784        1236 :         if (!stat)
    1785         630 :             break;
    1786             : 
    1787             :         /* Ensure choose_best_statistics produced an expected stats type. */
    1788             :         Assert(stat->kind == STATS_EXT_MCV);
    1789             : 
    1790             :         /* now filter the clauses to be estimated using the selected MCV */
    1791         606 :         stat_clauses = NIL;
    1792             : 
    1793             :         /* record which clauses are simple (single column or expression) */
    1794         606 :         simple_clauses = NULL;
    1795             : 
    1796         606 :         listidx = -1;
    1797        2112 :         foreach(l, clauses)
    1798             :         {
    1799             :             /* Increment the index before we decide if to skip the clause. */
    1800        1506 :             listidx++;
    1801             : 
    1802             :             /*
    1803             :              * Ignore clauses from which we did not extract any attnums or
    1804             :              * expressions (this needs to be consistent with what we do in
    1805             :              * choose_best_statistics).
    1806             :              *
    1807             :              * This also eliminates already estimated clauses - both those
    1808             :              * estimated before and during applying extended statistics.
    1809             :              *
    1810             :              * XXX This check is needed because both bms_is_subset and
    1811             :              * stat_covers_expressions return true for empty attnums and
    1812             :              * expressions.
    1813             :              */
    1814        1506 :             if (!list_attnums[listidx] && !list_exprs[listidx])
    1815          36 :                 continue;
    1816             : 
    1817             :             /*
    1818             :              * The clause was not estimated yet, and we've extracted either
    1819             :              * attnums or expressions from it. Ignore it if it's not fully
    1820             :              * covered by the chosen statistics object.
    1821             :              *
    1822             :              * We need to check both attributes and expressions, and reject if
    1823             :              * either is not covered.
    1824             :              */
    1825        1470 :             if (!bms_is_subset(list_attnums[listidx], stat->keys) ||
    1826        1410 :                 !stat_covers_expressions(stat, list_exprs[listidx], NULL))
    1827          66 :                 continue;
    1828             : 
    1829             :             /*
    1830             :              * Now we know the clause is compatible (we have either attnums or
    1831             :              * expressions extracted from it), and was not estimated yet.
    1832             :              */
    1833             : 
    1834             :             /* record simple clauses (single column or expression) */
    1835        1650 :             if ((list_attnums[listidx] == NULL &&
    1836         246 :                  list_length(list_exprs[listidx]) == 1) ||
    1837        2316 :                 (list_exprs[listidx] == NIL &&
    1838        1158 :                  bms_membership(list_attnums[listidx]) == BMS_SINGLETON))
    1839        1344 :                 simple_clauses = bms_add_member(simple_clauses,
    1840             :                                                 list_length(stat_clauses));
    1841             : 
    1842             :             /* add clause to list and mark it as estimated */
    1843        1404 :             stat_clauses = lappend(stat_clauses, (Node *) lfirst(l));
    1844        1404 :             *estimatedclauses = bms_add_member(*estimatedclauses, listidx);
    1845             : 
    1846             :             /*
    1847             :              * Reset the pointers, so that choose_best_statistics knows this
    1848             :              * clause was estimated and does not consider it again.
    1849             :              */
    1850        1404 :             bms_free(list_attnums[listidx]);
    1851        1404 :             list_attnums[listidx] = NULL;
    1852             : 
    1853        1404 :             list_free(list_exprs[listidx]);
    1854        1404 :             list_exprs[listidx] = NULL;
    1855             :         }
    1856             : 
    1857         606 :         if (is_or)
    1858             :         {
    1859          96 :             bool       *or_matches = NULL;
    1860          96 :             Selectivity simple_or_sel = 0.0,
    1861          96 :                         stat_sel = 0.0;
    1862             :             MCVList    *mcv_list;
    1863             : 
    1864             :             /* Load the MCV list stored in the statistics object */
    1865          96 :             mcv_list = statext_mcv_load(stat->statOid, rte->inh);
    1866             : 
    1867             :             /*
    1868             :              * Compute the selectivity of the ORed list of clauses covered by
    1869             :              * this statistics object by estimating each in turn and combining
    1870             :              * them using the formula P(A OR B) = P(A) + P(B) - P(A AND B).
    1871             :              * This allows us to use the multivariate MCV stats to better
    1872             :              * estimate the individual terms and their overlap.
    1873             :              *
    1874             :              * Each time we iterate this formula, the clause "A" above is
    1875             :              * equal to all the clauses processed so far, combined with "OR".
    1876             :              */
    1877          96 :             listidx = 0;
    1878         336 :             foreach(l, stat_clauses)
    1879             :             {
    1880         240 :                 Node       *clause = (Node *) lfirst(l);
    1881             :                 Selectivity simple_sel,
    1882             :                             overlap_simple_sel,
    1883             :                             mcv_sel,
    1884             :                             mcv_basesel,
    1885             :                             overlap_mcvsel,
    1886             :                             overlap_basesel,
    1887             :                             mcv_totalsel,
    1888             :                             clause_sel,
    1889             :                             overlap_sel;
    1890             : 
    1891             :                 /*
    1892             :                  * "Simple" selectivity of the next clause and its overlap
    1893             :                  * with any of the previous clauses.  These are our initial
    1894             :                  * estimates of P(B) and P(A AND B), assuming independence of
    1895             :                  * columns/clauses.
    1896             :                  */
    1897         240 :                 simple_sel = clause_selectivity_ext(root, clause, varRelid,
    1898             :                                                     jointype, sjinfo, false);
    1899             : 
    1900         240 :                 overlap_simple_sel = simple_or_sel * simple_sel;
    1901             : 
    1902             :                 /*
    1903             :                  * New "simple" selectivity of all clauses seen so far,
    1904             :                  * assuming independence.
    1905             :                  */
    1906         240 :                 simple_or_sel += simple_sel - overlap_simple_sel;
    1907         240 :                 CLAMP_PROBABILITY(simple_or_sel);
    1908             : 
    1909             :                 /*
    1910             :                  * Multi-column estimate of this clause using MCV statistics,
    1911             :                  * along with base and total selectivities, and corresponding
    1912             :                  * selectivities for the overlap term P(A AND B).
    1913             :                  */
    1914         240 :                 mcv_sel = mcv_clause_selectivity_or(root, stat, mcv_list,
    1915             :                                                     clause, &or_matches,
    1916             :                                                     &mcv_basesel,
    1917             :                                                     &overlap_mcvsel,
    1918             :                                                     &overlap_basesel,
    1919             :                                                     &mcv_totalsel);
    1920             : 
    1921             :                 /*
    1922             :                  * Combine the simple and multi-column estimates.
    1923             :                  *
    1924             :                  * If this clause is a simple single-column clause, then we
    1925             :                  * just use the simple selectivity estimate for it, since the
    1926             :                  * multi-column statistics are unlikely to improve on that
    1927             :                  * (and in fact could make it worse).  For the overlap, we
    1928             :                  * always make use of the multi-column statistics.
    1929             :                  */
    1930         240 :                 if (bms_is_member(listidx, simple_clauses))
    1931         192 :                     clause_sel = simple_sel;
    1932             :                 else
    1933          48 :                     clause_sel = mcv_combine_selectivities(simple_sel,
    1934             :                                                            mcv_sel,
    1935             :                                                            mcv_basesel,
    1936             :                                                            mcv_totalsel);
    1937             : 
    1938         240 :                 overlap_sel = mcv_combine_selectivities(overlap_simple_sel,
    1939             :                                                         overlap_mcvsel,
    1940             :                                                         overlap_basesel,
    1941             :                                                         mcv_totalsel);
    1942             : 
    1943             :                 /* Factor these into the result for this statistics object */
    1944         240 :                 stat_sel += clause_sel - overlap_sel;
    1945         240 :                 CLAMP_PROBABILITY(stat_sel);
    1946             : 
    1947         240 :                 listidx++;
    1948             :             }
    1949             : 
    1950             :             /*
    1951             :              * Factor the result for this statistics object into the overall
    1952             :              * result.  We treat the results from each separate statistics
    1953             :              * object as independent of one another.
    1954             :              */
    1955          96 :             sel = sel + stat_sel - sel * stat_sel;
    1956             :         }
    1957             :         else                    /* Implicitly-ANDed list of clauses */
    1958             :         {
    1959             :             Selectivity simple_sel,
    1960             :                         mcv_sel,
    1961             :                         mcv_basesel,
    1962             :                         mcv_totalsel,
    1963             :                         stat_sel;
    1964             : 
    1965             :             /*
    1966             :              * "Simple" selectivity, i.e. without any extended statistics,
    1967             :              * essentially assuming independence of the columns/clauses.
    1968             :              */
    1969         510 :             simple_sel = clauselist_selectivity_ext(root, stat_clauses,
    1970             :                                                     varRelid, jointype,
    1971             :                                                     sjinfo, false);
    1972             : 
    1973             :             /*
    1974             :              * Multi-column estimate using MCV statistics, along with base and
    1975             :              * total selectivities.
    1976             :              */
    1977         510 :             mcv_sel = mcv_clauselist_selectivity(root, stat, stat_clauses,
    1978             :                                                  varRelid, jointype, sjinfo,
    1979             :                                                  rel, &mcv_basesel,
    1980             :                                                  &mcv_totalsel);
    1981             : 
    1982             :             /* Combine the simple and multi-column estimates. */
    1983         510 :             stat_sel = mcv_combine_selectivities(simple_sel,
    1984             :                                                  mcv_sel,
    1985             :                                                  mcv_basesel,
    1986             :                                                  mcv_totalsel);
    1987             : 
    1988             :             /* Factor this into the overall result */
    1989         510 :             sel *= stat_sel;
    1990             :         }
    1991             :     }
    1992             : 
    1993         630 :     return sel;
    1994             : }
    1995             : 
    1996             : /*
    1997             :  * statext_clauselist_selectivity
    1998             :  *      Estimate clauses using the best multi-column statistics.
    1999             :  */
    2000             : Selectivity
    2001        2478 : statext_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid,
    2002             :                                JoinType jointype, SpecialJoinInfo *sjinfo,
    2003             :                                RelOptInfo *rel, Bitmapset **estimatedclauses,
    2004             :                                bool is_or)
    2005             : {
    2006             :     Selectivity sel;
    2007             : 
    2008             :     /* First, try estimating clauses using a multivariate MCV list. */
    2009        2478 :     sel = statext_mcv_clauselist_selectivity(root, clauses, varRelid, jointype,
    2010             :                                              sjinfo, rel, estimatedclauses, is_or);
    2011             : 
    2012             :     /*
    2013             :      * Functional dependencies only work for clauses connected by AND, so for
    2014             :      * OR clauses we're done.
    2015             :      */
    2016        2478 :     if (is_or)
    2017         114 :         return sel;
    2018             : 
    2019             :     /*
    2020             :      * Then, apply functional dependencies on the remaining clauses by calling
    2021             :      * dependencies_clauselist_selectivity.  Pass 'estimatedclauses' so the
    2022             :      * function can properly skip clauses already estimated above.
    2023             :      *
    2024             :      * The reasoning for applying dependencies last is that the more complex
    2025             :      * stats can track more complex correlations between the attributes, and
    2026             :      * so may be considered more reliable.
    2027             :      *
    2028             :      * For example, MCV list can give us an exact selectivity for values in
    2029             :      * two columns, while functional dependencies can only provide information
    2030             :      * about the overall strength of the dependency.
    2031             :      */
    2032        2364 :     sel *= dependencies_clauselist_selectivity(root, clauses, varRelid,
    2033             :                                                jointype, sjinfo, rel,
    2034             :                                                estimatedclauses);
    2035             : 
    2036        2364 :     return sel;
    2037             : }
    2038             : 
    2039             : /*
    2040             :  * examine_opclause_args
    2041             :  *      Split an operator expression's arguments into Expr and Const parts.
    2042             :  *
    2043             :  * Attempts to match the arguments to either (Expr op Const) or (Const op
    2044             :  * Expr), possibly with a RelabelType on top. When the expression matches this
    2045             :  * form, returns true, otherwise returns false.
    2046             :  *
    2047             :  * Optionally returns pointers to the extracted Expr/Const nodes, when passed
    2048             :  * non-null pointers (exprp, cstp and expronleftp). The expronleftp flag
    2049             :  * specifies on which side of the operator we found the expression node.
    2050             :  */
    2051             : bool
    2052        2766 : examine_opclause_args(List *args, Node **exprp, Const **cstp,
    2053             :                       bool *expronleftp)
    2054             : {
    2055             :     Node       *expr;
    2056             :     Const      *cst;
    2057             :     bool        expronleft;
    2058             :     Node       *leftop,
    2059             :                *rightop;
    2060             : 
    2061             :     /* enforced by statext_is_compatible_clause_internal */
    2062             :     Assert(list_length(args) == 2);
    2063             : 
    2064        2766 :     leftop = linitial(args);
    2065        2766 :     rightop = lsecond(args);
    2066             : 
    2067             :     /* strip RelabelType from either side of the expression */
    2068        2766 :     if (IsA(leftop, RelabelType))
    2069         324 :         leftop = (Node *) ((RelabelType *) leftop)->arg;
    2070             : 
    2071        2766 :     if (IsA(rightop, RelabelType))
    2072          60 :         rightop = (Node *) ((RelabelType *) rightop)->arg;
    2073             : 
    2074        2766 :     if (IsA(rightop, Const))
    2075             :     {
    2076        2604 :         expr = (Node *) leftop;
    2077        2604 :         cst = (Const *) rightop;
    2078        2604 :         expronleft = true;
    2079             :     }
    2080         162 :     else if (IsA(leftop, Const))
    2081             :     {
    2082         162 :         expr = (Node *) rightop;
    2083         162 :         cst = (Const *) leftop;
    2084         162 :         expronleft = false;
    2085             :     }
    2086             :     else
    2087           0 :         return false;
    2088             : 
    2089             :     /* return pointers to the extracted parts if requested */
    2090        2766 :     if (exprp)
    2091        2766 :         *exprp = expr;
    2092             : 
    2093        2766 :     if (cstp)
    2094        1338 :         *cstp = cst;
    2095             : 
    2096        2766 :     if (expronleftp)
    2097        1626 :         *expronleftp = expronleft;
    2098             : 
    2099        2766 :     return true;
    2100             : }
    2101             : 
    2102             : 
    2103             : /*
    2104             :  * Compute statistics about expressions of a relation.
    2105             :  */
    2106             : static void
    2107         156 : compute_expr_stats(Relation onerel, AnlExprData *exprdata, int nexprs,
    2108             :                    HeapTuple *rows, int numrows)
    2109             : {
    2110             :     MemoryContext expr_context,
    2111             :                 old_context;
    2112             :     int         ind,
    2113             :                 i;
    2114             : 
    2115         156 :     expr_context = AllocSetContextCreate(CurrentMemoryContext,
    2116             :                                          "Analyze Expression",
    2117             :                                          ALLOCSET_DEFAULT_SIZES);
    2118         156 :     old_context = MemoryContextSwitchTo(expr_context);
    2119             : 
    2120         456 :     for (ind = 0; ind < nexprs; ind++)
    2121             :     {
    2122         300 :         AnlExprData *thisdata = &exprdata[ind];
    2123         300 :         VacAttrStats *stats = thisdata->vacattrstat;
    2124         300 :         Node       *expr = thisdata->expr;
    2125             :         TupleTableSlot *slot;
    2126             :         EState     *estate;
    2127             :         ExprContext *econtext;
    2128             :         Datum      *exprvals;
    2129             :         bool       *exprnulls;
    2130             :         ExprState  *exprstate;
    2131             :         int         tcnt;
    2132             : 
    2133             :         /* Are we still in the main context? */
    2134             :         Assert(CurrentMemoryContext == expr_context);
    2135             : 
    2136             :         /*
    2137             :          * Need an EState for evaluation of expressions.  Create it in the
    2138             :          * per-expression context to be sure it gets cleaned up at the bottom
    2139             :          * of the loop.
    2140             :          */
    2141         300 :         estate = CreateExecutorState();
    2142         300 :         econtext = GetPerTupleExprContext(estate);
    2143             : 
    2144             :         /* Set up expression evaluation state */
    2145         300 :         exprstate = ExecPrepareExpr((Expr *) expr, estate);
    2146             : 
    2147             :         /* Need a slot to hold the current heap tuple, too */
    2148         300 :         slot = MakeSingleTupleTableSlot(RelationGetDescr(onerel),
    2149             :                                         &TTSOpsHeapTuple);
    2150             : 
    2151             :         /* Arrange for econtext's scan tuple to be the tuple under test */
    2152         300 :         econtext->ecxt_scantuple = slot;
    2153             : 
    2154             :         /* Compute and save expression values */
    2155         300 :         exprvals = (Datum *) palloc(numrows * sizeof(Datum));
    2156         300 :         exprnulls = (bool *) palloc(numrows * sizeof(bool));
    2157             : 
    2158         300 :         tcnt = 0;
    2159      399930 :         for (i = 0; i < numrows; i++)
    2160             :         {
    2161             :             Datum       datum;
    2162             :             bool        isnull;
    2163             : 
    2164             :             /*
    2165             :              * Reset the per-tuple context each time, to reclaim any cruft
    2166             :              * left behind by evaluating the statistics expressions.
    2167             :              */
    2168      399630 :             ResetExprContext(econtext);
    2169             : 
    2170             :             /* Set up for expression evaluation */
    2171      399630 :             ExecStoreHeapTuple(rows[i], slot, false);
    2172             : 
    2173             :             /*
    2174             :              * Evaluate the expression. We do this in the per-tuple context so
    2175             :              * as not to leak memory, and then copy the result into the
    2176             :              * context created at the beginning of this function.
    2177             :              */
    2178      399630 :             datum = ExecEvalExprSwitchContext(exprstate,
    2179      399630 :                                               GetPerTupleExprContext(estate),
    2180             :                                               &isnull);
    2181      399630 :             if (isnull)
    2182             :             {
    2183           0 :                 exprvals[tcnt] = (Datum) 0;
    2184           0 :                 exprnulls[tcnt] = true;
    2185             :             }
    2186             :             else
    2187             :             {
    2188             :                 /* Make sure we copy the data into the context. */
    2189             :                 Assert(CurrentMemoryContext == expr_context);
    2190             : 
    2191      799260 :                 exprvals[tcnt] = datumCopy(datum,
    2192      399630 :                                            stats->attrtype->typbyval,
    2193      399630 :                                            stats->attrtype->typlen);
    2194      399630 :                 exprnulls[tcnt] = false;
    2195             :             }
    2196             : 
    2197      399630 :             tcnt++;
    2198             :         }
    2199             : 
    2200             :         /*
    2201             :          * Now we can compute the statistics for the expression columns.
    2202             :          *
    2203             :          * XXX Unlike compute_index_stats we don't need to switch and reset
    2204             :          * memory contexts here, because we're only computing stats for a
    2205             :          * single expression (and not iterating over many indexes), so we just
    2206             :          * do it in expr_context. Note that compute_stats copies the result
    2207             :          * into stats->anl_context, so it does not disappear.
    2208             :          */
    2209         300 :         if (tcnt > 0)
    2210             :         {
    2211             :             AttributeOpts *aopt =
    2212         300 :                 get_attribute_options(onerel->rd_id, stats->tupattnum);
    2213             : 
    2214         300 :             stats->exprvals = exprvals;
    2215         300 :             stats->exprnulls = exprnulls;
    2216         300 :             stats->rowstride = 1;
    2217         300 :             stats->compute_stats(stats,
    2218             :                                  expr_fetch_func,
    2219             :                                  tcnt,
    2220             :                                  tcnt);
    2221             : 
    2222             :             /*
    2223             :              * If the n_distinct option is specified, it overrides the above
    2224             :              * computation.
    2225             :              */
    2226         300 :             if (aopt != NULL && aopt->n_distinct != 0.0)
    2227           0 :                 stats->stadistinct = aopt->n_distinct;
    2228             :         }
    2229             : 
    2230             :         /* And clean up */
    2231         300 :         MemoryContextSwitchTo(expr_context);
    2232             : 
    2233         300 :         ExecDropSingleTupleTableSlot(slot);
    2234         300 :         FreeExecutorState(estate);
    2235         300 :         MemoryContextReset(expr_context);
    2236             :     }
    2237             : 
    2238         156 :     MemoryContextSwitchTo(old_context);
    2239         156 :     MemoryContextDelete(expr_context);
    2240         156 : }
    2241             : 
    2242             : 
    2243             : /*
    2244             :  * Fetch function for analyzing statistics object expressions.
    2245             :  *
    2246             :  * We have not bothered to construct tuples from the data, instead the data
    2247             :  * is just in Datum arrays.
    2248             :  */
    2249             : static Datum
    2250      399630 : expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
    2251             : {
    2252             :     int         i;
    2253             : 
    2254             :     /* exprvals and exprnulls are already offset for proper column */
    2255      399630 :     i = rownum * stats->rowstride;
    2256      399630 :     *isNull = stats->exprnulls[i];
    2257      399630 :     return stats->exprvals[i];
    2258             : }
    2259             : 
    2260             : /*
    2261             :  * Build analyze data for a list of expressions. As this is not tied
    2262             :  * directly to a relation (table or index), we have to fake some of
    2263             :  * the fields in examine_expression().
    2264             :  */
    2265             : static AnlExprData *
    2266         156 : build_expr_data(List *exprs, int stattarget)
    2267             : {
    2268             :     int         idx;
    2269         156 :     int         nexprs = list_length(exprs);
    2270             :     AnlExprData *exprdata;
    2271             :     ListCell   *lc;
    2272             : 
    2273         156 :     exprdata = (AnlExprData *) palloc0(nexprs * sizeof(AnlExprData));
    2274             : 
    2275         156 :     idx = 0;
    2276         456 :     foreach(lc, exprs)
    2277             :     {
    2278         300 :         Node       *expr = (Node *) lfirst(lc);
    2279         300 :         AnlExprData *thisdata = &exprdata[idx];
    2280             : 
    2281         300 :         thisdata->expr = expr;
    2282         300 :         thisdata->vacattrstat = examine_expression(expr, stattarget);
    2283         300 :         idx++;
    2284             :     }
    2285             : 
    2286         156 :     return exprdata;
    2287             : }
    2288             : 
    2289             : /* form an array of pg_statistic rows (per update_attstats) */
    2290             : static Datum
    2291         156 : serialize_expr_stats(AnlExprData *exprdata, int nexprs)
    2292             : {
    2293             :     int         exprno;
    2294             :     Oid         typOid;
    2295             :     Relation    sd;
    2296             : 
    2297         156 :     ArrayBuildState *astate = NULL;
    2298             : 
    2299         156 :     sd = table_open(StatisticRelationId, RowExclusiveLock);
    2300             : 
    2301             :     /* lookup OID of composite type for pg_statistic */
    2302         156 :     typOid = get_rel_type_id(StatisticRelationId);
    2303         156 :     if (!OidIsValid(typOid))
    2304           0 :         ereport(ERROR,
    2305             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2306             :                  errmsg("relation \"%s\" does not have a composite type",
    2307             :                         "pg_statistic")));
    2308             : 
    2309         456 :     for (exprno = 0; exprno < nexprs; exprno++)
    2310             :     {
    2311             :         int         i,
    2312             :                     k;
    2313         300 :         VacAttrStats *stats = exprdata[exprno].vacattrstat;
    2314             : 
    2315             :         Datum       values[Natts_pg_statistic];
    2316             :         bool        nulls[Natts_pg_statistic];
    2317             :         HeapTuple   stup;
    2318             : 
    2319         300 :         if (!stats->stats_valid)
    2320             :         {
    2321           0 :             astate = accumArrayResult(astate,
    2322             :                                       (Datum) 0,
    2323             :                                       true,
    2324             :                                       typOid,
    2325             :                                       CurrentMemoryContext);
    2326           0 :             continue;
    2327             :         }
    2328             : 
    2329             :         /*
    2330             :          * Construct a new pg_statistic tuple
    2331             :          */
    2332        9600 :         for (i = 0; i < Natts_pg_statistic; ++i)
    2333             :         {
    2334        9300 :             nulls[i] = false;
    2335             :         }
    2336             : 
    2337         300 :         values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(InvalidOid);
    2338         300 :         values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(InvalidAttrNumber);
    2339         300 :         values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(false);
    2340         300 :         values[Anum_pg_statistic_stanullfrac - 1] = Float4GetDatum(stats->stanullfrac);
    2341         300 :         values[Anum_pg_statistic_stawidth - 1] = Int32GetDatum(stats->stawidth);
    2342         300 :         values[Anum_pg_statistic_stadistinct - 1] = Float4GetDatum(stats->stadistinct);
    2343         300 :         i = Anum_pg_statistic_stakind1 - 1;
    2344        1800 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    2345             :         {
    2346        1500 :             values[i++] = Int16GetDatum(stats->stakind[k]); /* stakindN */
    2347             :         }
    2348         300 :         i = Anum_pg_statistic_staop1 - 1;
    2349        1800 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    2350             :         {
    2351        1500 :             values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */
    2352             :         }
    2353         300 :         i = Anum_pg_statistic_stacoll1 - 1;
    2354        1800 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    2355             :         {
    2356        1500 :             values[i++] = ObjectIdGetDatum(stats->stacoll[k]);   /* stacollN */
    2357             :         }
    2358         300 :         i = Anum_pg_statistic_stanumbers1 - 1;
    2359        1800 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    2360             :         {
    2361        1500 :             int         nnum = stats->numnumbers[k];
    2362             : 
    2363        1500 :             if (nnum > 0)
    2364             :             {
    2365             :                 int         n;
    2366         588 :                 Datum      *numdatums = (Datum *) palloc(nnum * sizeof(Datum));
    2367             :                 ArrayType  *arry;
    2368             : 
    2369        4986 :                 for (n = 0; n < nnum; n++)
    2370        4398 :                     numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
    2371         588 :                 arry = construct_array_builtin(numdatums, nnum, FLOAT4OID);
    2372         588 :                 values[i++] = PointerGetDatum(arry);    /* stanumbersN */
    2373             :             }
    2374             :             else
    2375             :             {
    2376         912 :                 nulls[i] = true;
    2377         912 :                 values[i++] = (Datum) 0;
    2378             :             }
    2379             :         }
    2380         300 :         i = Anum_pg_statistic_stavalues1 - 1;
    2381        1800 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    2382             :         {
    2383        1500 :             if (stats->numvalues[k] > 0)
    2384             :             {
    2385             :                 ArrayType  *arry;
    2386             : 
    2387         318 :                 arry = construct_array(stats->stavalues[k],
    2388             :                                        stats->numvalues[k],
    2389             :                                        stats->statypid[k],
    2390         318 :                                        stats->statyplen[k],
    2391         318 :                                        stats->statypbyval[k],
    2392         318 :                                        stats->statypalign[k]);
    2393         318 :                 values[i++] = PointerGetDatum(arry);    /* stavaluesN */
    2394             :             }
    2395             :             else
    2396             :             {
    2397        1182 :                 nulls[i] = true;
    2398        1182 :                 values[i++] = (Datum) 0;
    2399             :             }
    2400             :         }
    2401             : 
    2402         300 :         stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
    2403             : 
    2404         300 :         astate = accumArrayResult(astate,
    2405             :                                   heap_copy_tuple_as_datum(stup, RelationGetDescr(sd)),
    2406             :                                   false,
    2407             :                                   typOid,
    2408             :                                   CurrentMemoryContext);
    2409             :     }
    2410             : 
    2411         156 :     table_close(sd, RowExclusiveLock);
    2412             : 
    2413         156 :     return makeArrayResult(astate, CurrentMemoryContext);
    2414             : }
    2415             : 
    2416             : /*
    2417             :  * Loads pg_statistic record from expression statistics for expression
    2418             :  * identified by the supplied index.
    2419             :  */
    2420             : HeapTuple
    2421        1644 : statext_expressions_load(Oid stxoid, bool inh, int idx)
    2422             : {
    2423             :     bool        isnull;
    2424             :     Datum       value;
    2425             :     HeapTuple   htup;
    2426             :     ExpandedArrayHeader *eah;
    2427             :     HeapTupleHeader td;
    2428             :     HeapTupleData tmptup;
    2429             :     HeapTuple   tup;
    2430             : 
    2431        1644 :     htup = SearchSysCache2(STATEXTDATASTXOID,
    2432             :                            ObjectIdGetDatum(stxoid), BoolGetDatum(inh));
    2433        1644 :     if (!HeapTupleIsValid(htup))
    2434           0 :         elog(ERROR, "cache lookup failed for statistics object %u", stxoid);
    2435             : 
    2436        1644 :     value = SysCacheGetAttr(STATEXTDATASTXOID, htup,
    2437             :                             Anum_pg_statistic_ext_data_stxdexpr, &isnull);
    2438        1644 :     if (isnull)
    2439           0 :         elog(ERROR,
    2440             :              "requested statistics kind \"%c\" is not yet built for statistics object %u",
    2441             :              STATS_EXT_EXPRESSIONS, stxoid);
    2442             : 
    2443        1644 :     eah = DatumGetExpandedArray(value);
    2444             : 
    2445        1644 :     deconstruct_expanded_array(eah);
    2446             : 
    2447        1644 :     td = DatumGetHeapTupleHeader(eah->dvalues[idx]);
    2448             : 
    2449             :     /* Build a temporary HeapTuple control structure */
    2450        1644 :     tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
    2451        1644 :     ItemPointerSetInvalid(&(tmptup.t_self));
    2452        1644 :     tmptup.t_tableOid = InvalidOid;
    2453        1644 :     tmptup.t_data = td;
    2454             : 
    2455        1644 :     tup = heap_copytuple(&tmptup);
    2456             : 
    2457        1644 :     ReleaseSysCache(htup);
    2458             : 
    2459        1644 :     return tup;
    2460             : }
    2461             : 
    2462             : /*
    2463             :  * Evaluate the expressions, so that we can use the results to build
    2464             :  * all the requested statistics types. This matters especially for
    2465             :  * expensive expressions, of course.
    2466             :  */
    2467             : static StatsBuildData *
    2468         378 : make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows,
    2469             :                 VacAttrStats **stats, int stattarget)
    2470             : {
    2471             :     /* evaluated expressions */
    2472             :     StatsBuildData *result;
    2473             :     char       *ptr;
    2474             :     Size        len;
    2475             : 
    2476             :     int         i;
    2477             :     int         k;
    2478             :     int         idx;
    2479             :     TupleTableSlot *slot;
    2480             :     EState     *estate;
    2481             :     ExprContext *econtext;
    2482         378 :     List       *exprstates = NIL;
    2483         378 :     int         nkeys = bms_num_members(stat->columns) + list_length(stat->exprs);
    2484             :     ListCell   *lc;
    2485             : 
    2486             :     /* allocate everything as a single chunk, so we can free it easily */
    2487         378 :     len = MAXALIGN(sizeof(StatsBuildData));
    2488         378 :     len += MAXALIGN(sizeof(AttrNumber) * nkeys);    /* attnums */
    2489         378 :     len += MAXALIGN(sizeof(VacAttrStats *) * nkeys);    /* stats */
    2490             : 
    2491             :     /* values */
    2492         378 :     len += MAXALIGN(sizeof(Datum *) * nkeys);
    2493         378 :     len += nkeys * MAXALIGN(sizeof(Datum) * numrows);
    2494             : 
    2495             :     /* nulls */
    2496         378 :     len += MAXALIGN(sizeof(bool *) * nkeys);
    2497         378 :     len += nkeys * MAXALIGN(sizeof(bool) * numrows);
    2498             : 
    2499         378 :     ptr = palloc(len);
    2500             : 
    2501             :     /* set the pointers */
    2502         378 :     result = (StatsBuildData *) ptr;
    2503         378 :     ptr += MAXALIGN(sizeof(StatsBuildData));
    2504             : 
    2505             :     /* attnums */
    2506         378 :     result->attnums = (AttrNumber *) ptr;
    2507         378 :     ptr += MAXALIGN(sizeof(AttrNumber) * nkeys);
    2508             : 
    2509             :     /* stats */
    2510         378 :     result->stats = (VacAttrStats **) ptr;
    2511         378 :     ptr += MAXALIGN(sizeof(VacAttrStats *) * nkeys);
    2512             : 
    2513             :     /* values */
    2514         378 :     result->values = (Datum **) ptr;
    2515         378 :     ptr += MAXALIGN(sizeof(Datum *) * nkeys);
    2516             : 
    2517             :     /* nulls */
    2518         378 :     result->nulls = (bool **) ptr;
    2519         378 :     ptr += MAXALIGN(sizeof(bool *) * nkeys);
    2520             : 
    2521        1338 :     for (i = 0; i < nkeys; i++)
    2522             :     {
    2523         960 :         result->values[i] = (Datum *) ptr;
    2524         960 :         ptr += MAXALIGN(sizeof(Datum) * numrows);
    2525             : 
    2526         960 :         result->nulls[i] = (bool *) ptr;
    2527         960 :         ptr += MAXALIGN(sizeof(bool) * numrows);
    2528             :     }
    2529             : 
    2530             :     Assert((ptr - (char *) result) == len);
    2531             : 
    2532             :     /* we have it allocated, so let's fill the values */
    2533         378 :     result->nattnums = nkeys;
    2534         378 :     result->numrows = numrows;
    2535             : 
    2536             :     /* fill the attribute info - first attributes, then expressions */
    2537         378 :     idx = 0;
    2538         378 :     k = -1;
    2539        1038 :     while ((k = bms_next_member(stat->columns, k)) >= 0)
    2540             :     {
    2541         660 :         result->attnums[idx] = k;
    2542         660 :         result->stats[idx] = stats[idx];
    2543             : 
    2544         660 :         idx++;
    2545             :     }
    2546             : 
    2547         378 :     k = -1;
    2548         678 :     foreach(lc, stat->exprs)
    2549             :     {
    2550         300 :         Node       *expr = (Node *) lfirst(lc);
    2551             : 
    2552         300 :         result->attnums[idx] = k;
    2553         300 :         result->stats[idx] = examine_expression(expr, stattarget);
    2554             : 
    2555         300 :         idx++;
    2556         300 :         k--;
    2557             :     }
    2558             : 
    2559             :     /* first extract values for all the regular attributes */
    2560      763038 :     for (i = 0; i < numrows; i++)
    2561             :     {
    2562      762660 :         idx = 0;
    2563      762660 :         k = -1;
    2564     2504544 :         while ((k = bms_next_member(stat->columns, k)) >= 0)
    2565             :         {
    2566     3483768 :             result->values[idx][i] = heap_getattr(rows[i], k,
    2567     1741884 :                                                   result->stats[idx]->tupDesc,
    2568     1741884 :                                                   &result->nulls[idx][i]);
    2569             : 
    2570     1741884 :             idx++;
    2571             :         }
    2572             :     }
    2573             : 
    2574             :     /* Need an EState for evaluation expressions. */
    2575         378 :     estate = CreateExecutorState();
    2576         378 :     econtext = GetPerTupleExprContext(estate);
    2577             : 
    2578             :     /* Need a slot to hold the current heap tuple, too */
    2579         378 :     slot = MakeSingleTupleTableSlot(RelationGetDescr(rel),
    2580             :                                     &TTSOpsHeapTuple);
    2581             : 
    2582             :     /* Arrange for econtext's scan tuple to be the tuple under test */
    2583         378 :     econtext->ecxt_scantuple = slot;
    2584             : 
    2585             :     /* Set up expression evaluation state */
    2586         378 :     exprstates = ExecPrepareExprList(stat->exprs, estate);
    2587             : 
    2588      763038 :     for (i = 0; i < numrows; i++)
    2589             :     {
    2590             :         /*
    2591             :          * Reset the per-tuple context each time, to reclaim any cruft left
    2592             :          * behind by evaluating the statistics object expressions.
    2593             :          */
    2594      762660 :         ResetExprContext(econtext);
    2595             : 
    2596             :         /* Set up for expression evaluation */
    2597      762660 :         ExecStoreHeapTuple(rows[i], slot, false);
    2598             : 
    2599      762660 :         idx = bms_num_members(stat->columns);
    2600     1162290 :         foreach(lc, exprstates)
    2601             :         {
    2602             :             Datum       datum;
    2603             :             bool        isnull;
    2604      399630 :             ExprState  *exprstate = (ExprState *) lfirst(lc);
    2605             : 
    2606             :             /*
    2607             :              * XXX This probably leaks memory. Maybe we should use
    2608             :              * ExecEvalExprSwitchContext but then we need to copy the result
    2609             :              * somewhere else.
    2610             :              */
    2611      399630 :             datum = ExecEvalExpr(exprstate,
    2612      399630 :                                  GetPerTupleExprContext(estate),
    2613             :                                  &isnull);
    2614      399630 :             if (isnull)
    2615             :             {
    2616           0 :                 result->values[idx][i] = (Datum) 0;
    2617           0 :                 result->nulls[idx][i] = true;
    2618             :             }
    2619             :             else
    2620             :             {
    2621      399630 :                 result->values[idx][i] = (Datum) datum;
    2622      399630 :                 result->nulls[idx][i] = false;
    2623             :             }
    2624             : 
    2625      399630 :             idx++;
    2626             :         }
    2627             :     }
    2628             : 
    2629         378 :     ExecDropSingleTupleTableSlot(slot);
    2630         378 :     FreeExecutorState(estate);
    2631             : 
    2632         378 :     return result;
    2633             : }

Generated by: LCOV version 1.14