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

Generated by: LCOV version 2.0-1