LCOV - differential code coverage report
Current view: top level - src/backend/statistics - extended_stats.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 92.2 % 811 748 5 38 20 18 379 5 346 25 394 3
Current Date: 2023-04-08 15:15:32 Functions: 97.0 % 33 32 1 27 1 4 1 27
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a