LCOV - differential code coverage report
Current view: top level - src/backend/commands - analyze.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 94.6 % 978 925 15 22 16 10 505 15 395 27 509 6
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 17 17 16 1 16
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * analyze.c
       4                 :  *    the Postgres statistics generator
       5                 :  *
       6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7                 :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :  *
       9                 :  *
      10                 :  * IDENTIFICATION
      11                 :  *    src/backend/commands/analyze.c
      12                 :  *
      13                 :  *-------------------------------------------------------------------------
      14                 :  */
      15                 : #include "postgres.h"
      16                 : 
      17                 : #include <math.h>
      18                 : 
      19                 : #include "access/detoast.h"
      20                 : #include "access/genam.h"
      21                 : #include "access/multixact.h"
      22                 : #include "access/relation.h"
      23                 : #include "access/sysattr.h"
      24                 : #include "access/table.h"
      25                 : #include "access/tableam.h"
      26                 : #include "access/transam.h"
      27                 : #include "access/tupconvert.h"
      28                 : #include "access/visibilitymap.h"
      29                 : #include "access/xact.h"
      30                 : #include "catalog/catalog.h"
      31                 : #include "catalog/index.h"
      32                 : #include "catalog/indexing.h"
      33                 : #include "catalog/pg_collation.h"
      34                 : #include "catalog/pg_inherits.h"
      35                 : #include "catalog/pg_namespace.h"
      36                 : #include "catalog/pg_statistic_ext.h"
      37                 : #include "commands/dbcommands.h"
      38                 : #include "commands/progress.h"
      39                 : #include "commands/tablecmds.h"
      40                 : #include "commands/vacuum.h"
      41                 : #include "common/pg_prng.h"
      42                 : #include "executor/executor.h"
      43                 : #include "foreign/fdwapi.h"
      44                 : #include "miscadmin.h"
      45                 : #include "nodes/nodeFuncs.h"
      46                 : #include "parser/parse_oper.h"
      47                 : #include "parser/parse_relation.h"
      48                 : #include "pgstat.h"
      49                 : #include "postmaster/autovacuum.h"
      50                 : #include "statistics/extended_stats_internal.h"
      51                 : #include "statistics/statistics.h"
      52                 : #include "storage/bufmgr.h"
      53                 : #include "storage/lmgr.h"
      54                 : #include "storage/proc.h"
      55                 : #include "storage/procarray.h"
      56                 : #include "utils/acl.h"
      57                 : #include "utils/attoptcache.h"
      58                 : #include "utils/builtins.h"
      59                 : #include "utils/datum.h"
      60                 : #include "utils/fmgroids.h"
      61                 : #include "utils/guc.h"
      62                 : #include "utils/lsyscache.h"
      63                 : #include "utils/memutils.h"
      64                 : #include "utils/pg_rusage.h"
      65                 : #include "utils/sampling.h"
      66                 : #include "utils/sortsupport.h"
      67                 : #include "utils/spccache.h"
      68                 : #include "utils/syscache.h"
      69                 : #include "utils/timestamp.h"
      70                 : 
      71                 : 
      72                 : /* Per-index data for ANALYZE */
      73                 : typedef struct AnlIndexData
      74                 : {
      75                 :     IndexInfo  *indexInfo;      /* BuildIndexInfo result */
      76                 :     double      tupleFract;     /* fraction of rows for partial index */
      77                 :     VacAttrStats **vacattrstats;    /* index attrs to analyze */
      78                 :     int         attr_cnt;
      79                 : } AnlIndexData;
      80                 : 
      81                 : 
      82                 : /* Default statistics target (GUC parameter) */
      83                 : int         default_statistics_target = 100;
      84                 : 
      85                 : /* A few variables that don't seem worth passing around as parameters */
      86                 : static MemoryContext anl_context = NULL;
      87                 : static BufferAccessStrategy vac_strategy;
      88                 : 
      89                 : 
      90                 : static void do_analyze_rel(Relation onerel,
      91                 :                            VacuumParams *params, List *va_cols,
      92                 :                            AcquireSampleRowsFunc acquirefunc, BlockNumber relpages,
      93                 :                            bool inh, bool in_outer_xact, int elevel);
      94                 : static void compute_index_stats(Relation onerel, double totalrows,
      95                 :                                 AnlIndexData *indexdata, int nindexes,
      96                 :                                 HeapTuple *rows, int numrows,
      97                 :                                 MemoryContext col_context);
      98                 : static VacAttrStats *examine_attribute(Relation onerel, int attnum,
      99                 :                                        Node *index_expr);
     100                 : static int  acquire_sample_rows(Relation onerel, int elevel,
     101                 :                                 HeapTuple *rows, int targrows,
     102                 :                                 double *totalrows, double *totaldeadrows);
     103                 : static int  compare_rows(const void *a, const void *b, void *arg);
     104                 : static int  acquire_inherited_sample_rows(Relation onerel, int elevel,
     105                 :                                           HeapTuple *rows, int targrows,
     106                 :                                           double *totalrows, double *totaldeadrows);
     107                 : static void update_attstats(Oid relid, bool inh,
     108                 :                             int natts, VacAttrStats **vacattrstats);
     109                 : static Datum std_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull);
     110                 : static Datum ind_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull);
     111                 : 
     112                 : 
     113                 : /*
     114                 :  *  analyze_rel() -- analyze one relation
     115                 :  *
     116                 :  * relid identifies the relation to analyze.  If relation is supplied, use
     117                 :  * the name therein for reporting any failure to open/lock the rel; do not
     118                 :  * use it once we've successfully opened the rel, since it might be stale.
     119                 :  */
     120                 : void
     121 CBC       24097 : analyze_rel(Oid relid, RangeVar *relation,
     122                 :             VacuumParams *params, List *va_cols, bool in_outer_xact,
     123                 :             BufferAccessStrategy bstrategy)
     124                 : {
     125                 :     Relation    onerel;
     126                 :     int         elevel;
     127           24097 :     AcquireSampleRowsFunc acquirefunc = NULL;
     128           24097 :     BlockNumber relpages = 0;
     129                 : 
     130                 :     /* Select logging level */
     131           24097 :     if (params->options & VACOPT_VERBOSE)
     132 UBC           0 :         elevel = INFO;
     133                 :     else
     134 CBC       24097 :         elevel = DEBUG2;
     135                 : 
     136                 :     /* Set up static variables */
     137           24097 :     vac_strategy = bstrategy;
     138                 : 
     139                 :     /*
     140                 :      * Check for user-requested abort.
     141                 :      */
     142           24097 :     CHECK_FOR_INTERRUPTS();
     143                 : 
     144                 :     /*
     145                 :      * Open the relation, getting ShareUpdateExclusiveLock to ensure that two
     146                 :      * ANALYZEs don't run on it concurrently.  (This also locks out a
     147                 :      * concurrent VACUUM, which doesn't matter much at the moment but might
     148                 :      * matter if we ever try to accumulate stats on dead tuples.) If the rel
     149                 :      * has been dropped since we last saw it, we don't need to process it.
     150                 :      *
     151                 :      * Make sure to generate only logs for ANALYZE in this case.
     152                 :      */
     153           24097 :     onerel = vacuum_open_relation(relid, relation, params->options & ~(VACOPT_VACUUM),
     154           24097 :                                   params->log_min_duration >= 0,
     155                 :                                   ShareUpdateExclusiveLock);
     156                 : 
     157                 :     /* leave if relation could not be opened or locked */
     158           24097 :     if (!onerel)
     159             337 :         return;
     160                 : 
     161                 :     /*
     162                 :      * Check if relation needs to be skipped based on privileges.  This check
     163                 :      * happens also when building the relation list to analyze for a manual
     164                 :      * operation, and needs to be done additionally here as ANALYZE could
     165                 :      * happen across multiple transactions where privileges could have changed
     166                 :      * in-between.  Make sure to generate only logs for ANALYZE in this case.
     167 ECB             :      */
     168 GNC       24093 :     if (!vacuum_is_permitted_for_relation(RelationGetRelid(onerel),
     169                 :                                           onerel->rd_rel,
     170           24093 :                                           params->options & VACOPT_ANALYZE))
     171 ECB             :     {
     172 CBC           9 :         relation_close(onerel, ShareUpdateExclusiveLock);
     173 GIC           9 :         return;
     174                 :     }
     175                 : 
     176                 :     /*
     177                 :      * Silently ignore tables that are temp tables of other backends ---
     178                 :      * trying to analyze these is rather pointless, since their contents are
     179                 :      * probably not up-to-date on disk.  (We don't throw a warning here; it
     180                 :      * would just lead to chatter during a database-wide ANALYZE.)
     181 ECB             :      */
     182 GIC       24084 :     if (RELATION_IS_OTHER_TEMP(onerel))
     183 EUB             :     {
     184 UBC           0 :         relation_close(onerel, ShareUpdateExclusiveLock);
     185 UIC           0 :         return;
     186                 :     }
     187                 : 
     188                 :     /*
     189                 :      * We can ANALYZE any table except pg_statistic. See update_attstats
     190 ECB             :      */
     191 GIC       24084 :     if (RelationGetRelid(onerel) == StatisticRelationId)
     192 ECB             :     {
     193 CBC         324 :         relation_close(onerel, ShareUpdateExclusiveLock);
     194 GIC         324 :         return;
     195                 :     }
     196                 : 
     197                 :     /*
     198                 :      * Check that it's of an analyzable relkind, and set up appropriately.
     199 ECB             :      */
     200 CBC       23760 :     if (onerel->rd_rel->relkind == RELKIND_RELATION ||
     201 GIC         347 :         onerel->rd_rel->relkind == RELKIND_MATVIEW)
     202                 :     {
     203 ECB             :         /* Regular table, so we'll use the regular row acquisition function */
     204 GIC       23413 :         acquirefunc = acquire_sample_rows;
     205 ECB             :         /* Also get regular table's size */
     206 GIC       23413 :         relpages = RelationGetNumberOfBlocks(onerel);
     207 ECB             :     }
     208 GIC         347 :     else if (onerel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
     209                 :     {
     210                 :         /*
     211                 :          * For a foreign table, call the FDW's hook function to see whether it
     212                 :          * supports analysis.
     213                 :          */
     214 ECB             :         FdwRoutine *fdwroutine;
     215 GIC          25 :         bool        ok = false;
     216 ECB             : 
     217 GIC          25 :         fdwroutine = GetFdwRoutineForRelation(onerel, false);
     218 ECB             : 
     219 CBC          25 :         if (fdwroutine->AnalyzeForeignTable != NULL)
     220 GIC          25 :             ok = fdwroutine->AnalyzeForeignTable(onerel,
     221                 :                                                  &acquirefunc,
     222                 :                                                  &relpages);
     223 ECB             : 
     224 GIC          25 :         if (!ok)
     225 EUB             :         {
     226 UIC           0 :             ereport(WARNING,
     227                 :                     (errmsg("skipping \"%s\" --- cannot analyze this foreign table",
     228 EUB             :                             RelationGetRelationName(onerel))));
     229 UBC           0 :             relation_close(onerel, ShareUpdateExclusiveLock);
     230 UIC           0 :             return;
     231                 :         }
     232 ECB             :     }
     233 GIC         322 :     else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     234                 :     {
     235                 :         /*
     236                 :          * For partitioned tables, we want to do the recursive ANALYZE below.
     237                 :          */
     238                 :     }
     239                 :     else
     240                 :     {
     241 EUB             :         /* No need for a WARNING if we already complained during VACUUM */
     242 UBC           0 :         if (!(params->options & VACOPT_VACUUM))
     243 UIC           0 :             ereport(WARNING,
     244                 :                     (errmsg("skipping \"%s\" --- cannot analyze non-tables or special system tables",
     245 EUB             :                             RelationGetRelationName(onerel))));
     246 UBC           0 :         relation_close(onerel, ShareUpdateExclusiveLock);
     247 UIC           0 :         return;
     248                 :     }
     249                 : 
     250                 :     /*
     251                 :      * OK, let's do it.  First, initialize progress reporting.
     252 ECB             :      */
     253 GIC       23760 :     pgstat_progress_start_command(PROGRESS_COMMAND_ANALYZE,
     254                 :                                   RelationGetRelid(onerel));
     255                 : 
     256                 :     /*
     257                 :      * Do the normal non-recursive ANALYZE.  We can skip this for partitioned
     258                 :      * tables, which don't contain any rows.
     259 ECB             :      */
     260 CBC       23760 :     if (onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
     261 GIC       23438 :         do_analyze_rel(onerel, params, va_cols, acquirefunc,
     262                 :                        relpages, false, in_outer_xact, elevel);
     263                 : 
     264                 :     /*
     265                 :      * If there are child tables, do recursive ANALYZE.
     266 ECB             :      */
     267 CBC       23739 :     if (onerel->rd_rel->relhassubclass)
     268 GIC         348 :         do_analyze_rel(onerel, params, va_cols, acquirefunc, relpages,
     269                 :                        true, in_outer_xact, elevel);
     270                 : 
     271                 :     /*
     272                 :      * Close source relation now, but keep lock so that no one deletes it
     273                 :      * before we commit.  (If someone did, they'd fail to clean up the entries
     274                 :      * we made in pg_statistic.  Also, releasing the lock before commit would
     275                 :      * expose us to concurrent-update failures in update_attstats.)
     276 ECB             :      */
     277 GIC       23730 :     relation_close(onerel, NoLock);
     278 ECB             : 
     279 GIC       23730 :     pgstat_progress_end_command();
     280                 : }
     281                 : 
     282                 : /*
     283                 :  *  do_analyze_rel() -- analyze one relation, recursively or not
     284                 :  *
     285                 :  * Note that "acquirefunc" is only relevant for the non-inherited case.
     286                 :  * For the inherited case, acquire_inherited_sample_rows() determines the
     287                 :  * appropriate acquirefunc for each child table.
     288                 :  */
     289 ECB             : static void
     290 GIC       23786 : do_analyze_rel(Relation onerel, VacuumParams *params,
     291                 :                List *va_cols, AcquireSampleRowsFunc acquirefunc,
     292                 :                BlockNumber relpages, bool inh, bool in_outer_xact,
     293                 :                int elevel)
     294                 : {
     295                 :     int         attr_cnt,
     296                 :                 tcnt,
     297                 :                 i,
     298                 :                 ind;
     299                 :     Relation   *Irel;
     300                 :     int         nindexes;
     301                 :     bool        hasindex;
     302                 :     VacAttrStats **vacattrstats;
     303                 :     AnlIndexData *indexdata;
     304                 :     int         targrows,
     305                 :                 numrows,
     306                 :                 minrows;
     307                 :     double      totalrows,
     308                 :                 totaldeadrows;
     309                 :     HeapTuple  *rows;
     310 ECB             :     PGRUsage    ru0;
     311 GIC       23786 :     TimestampTz starttime = 0;
     312                 :     MemoryContext caller_context;
     313                 :     Oid         save_userid;
     314                 :     int         save_sec_context;
     315 ECB             :     int         save_nestlevel;
     316 CBC       23786 :     int64       AnalyzePageHit = VacuumPageHit;
     317           23786 :     int64       AnalyzePageMiss = VacuumPageMiss;
     318           23786 :     int64       AnalyzePageDirty = VacuumPageDirty;
     319           23786 :     PgStat_Counter startreadtime = 0;
     320 GIC       23786 :     PgStat_Counter startwritetime = 0;
     321 ECB             : 
     322 CBC       23786 :     if (inh)
     323 GIC         348 :         ereport(elevel,
     324                 :                 (errmsg("analyzing \"%s.%s\" inheritance tree",
     325                 :                         get_namespace_name(RelationGetNamespace(onerel)),
     326                 :                         RelationGetRelationName(onerel))));
     327 ECB             :     else
     328 GIC       23438 :         ereport(elevel,
     329                 :                 (errmsg("analyzing \"%s.%s\"",
     330                 :                         get_namespace_name(RelationGetNamespace(onerel)),
     331                 :                         RelationGetRelationName(onerel))));
     332                 : 
     333                 :     /*
     334                 :      * Set up a working context so that we can easily free whatever junk gets
     335                 :      * created.
     336 ECB             :      */
     337 GIC       23786 :     anl_context = AllocSetContextCreate(CurrentMemoryContext,
     338                 :                                         "Analyze",
     339 ECB             :                                         ALLOCSET_DEFAULT_SIZES);
     340 GIC       23786 :     caller_context = MemoryContextSwitchTo(anl_context);
     341                 : 
     342                 :     /*
     343                 :      * Switch to the table owner's userid, so that any index functions are run
     344                 :      * as that user.  Also lock down security-restricted operations and
     345                 :      * arrange to make GUC variable changes local to this command.
     346 ECB             :      */
     347 CBC       23786 :     GetUserIdAndSecContext(&save_userid, &save_sec_context);
     348 GIC       23786 :     SetUserIdAndSecContext(onerel->rd_rel->relowner,
     349 ECB             :                            save_sec_context | SECURITY_RESTRICTED_OPERATION);
     350 GIC       23786 :     save_nestlevel = NewGUCNestLevel();
     351                 : 
     352 ECB             :     /* measure elapsed time iff autovacuum logging requires it */
     353 GIC       23786 :     if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
     354 ECB             :     {
     355 GIC         159 :         if (track_io_timing)
     356 EUB             :         {
     357 UBC           0 :             startreadtime = pgStatBlockReadTime;
     358 UIC           0 :             startwritetime = pgStatBlockWriteTime;
     359                 :         }
     360 ECB             : 
     361 CBC         159 :         pg_rusage_init(&ru0);
     362 GNC         159 :         starttime = GetCurrentTimestamp();
     363                 :     }
     364                 : 
     365                 :     /*
     366                 :      * Determine which columns to analyze
     367                 :      *
     368                 :      * Note that system attributes are never analyzed, so we just reject them
     369                 :      * at the lookup stage.  We also reject duplicate column mentions.  (We
     370                 :      * could alternatively ignore duplicates, but analyzing a column twice
     371 ECB             :      * won't work; we'd end up making a conflicting update in pg_statistic.)
     372                 :      */
     373 CBC       23786 :     if (va_cols != NIL)
     374                 :     {
     375 GIC          47 :         Bitmapset  *unique_cols = NULL;
     376 ECB             :         ListCell   *le;
     377                 : 
     378 CBC          47 :         vacattrstats = (VacAttrStats **) palloc(list_length(va_cols) *
     379 ECB             :                                                 sizeof(VacAttrStats *));
     380 GIC          47 :         tcnt = 0;
     381 CBC          82 :         foreach(le, va_cols)
     382                 :         {
     383              60 :             char       *col = strVal(lfirst(le));
     384 ECB             : 
     385 CBC          60 :             i = attnameAttNum(onerel, col, false);
     386 GIC          60 :             if (i == InvalidAttrNumber)
     387              19 :                 ereport(ERROR,
     388                 :                         (errcode(ERRCODE_UNDEFINED_COLUMN),
     389 ECB             :                          errmsg("column \"%s\" of relation \"%s\" does not exist",
     390                 :                                 col, RelationGetRelationName(onerel))));
     391 GIC          41 :             if (bms_is_member(i, unique_cols))
     392               6 :                 ereport(ERROR,
     393                 :                         (errcode(ERRCODE_DUPLICATE_COLUMN),
     394 ECB             :                          errmsg("column \"%s\" of relation \"%s\" appears more than once",
     395                 :                                 col, RelationGetRelationName(onerel))));
     396 CBC          35 :             unique_cols = bms_add_member(unique_cols, i);
     397 ECB             : 
     398 CBC          35 :             vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
     399 GIC          35 :             if (vacattrstats[tcnt] != NULL)
     400 CBC          35 :                 tcnt++;
     401                 :         }
     402 GIC          22 :         attr_cnt = tcnt;
     403                 :     }
     404 ECB             :     else
     405                 :     {
     406 CBC       23739 :         attr_cnt = onerel->rd_att->natts;
     407 ECB             :         vacattrstats = (VacAttrStats **)
     408 CBC       23739 :             palloc(attr_cnt * sizeof(VacAttrStats *));
     409 GIC       23739 :         tcnt = 0;
     410 CBC      220750 :         for (i = 1; i <= attr_cnt; i++)
     411 ECB             :         {
     412 CBC      197011 :             vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
     413 GIC      197011 :             if (vacattrstats[tcnt] != NULL)
     414 CBC      197005 :                 tcnt++;
     415                 :         }
     416 GIC       23739 :         attr_cnt = tcnt;
     417                 :     }
     418                 : 
     419                 :     /*
     420                 :      * Open all indexes of the relation, and see if there are any analyzable
     421                 :      * columns in the indexes.  We do not analyze index columns if there was
     422                 :      * an explicit column list in the ANALYZE command, however.
     423                 :      *
     424                 :      * If we are doing a recursive scan, we don't want to touch the parent's
     425                 :      * indexes at all.  If we're processing a partitioned table, we need to
     426 ECB             :      * know if there are any indexes, but we don't want to process them.
     427                 :      */
     428 CBC       23761 :     if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     429                 :     {
     430             313 :         List       *idxs = RelationGetIndexList(onerel);
     431 ECB             : 
     432 CBC         313 :         Irel = NULL;
     433             313 :         nindexes = 0;
     434 GIC         313 :         hasindex = idxs != NIL;
     435 CBC         313 :         list_free(idxs);
     436                 :     }
     437           23448 :     else if (!inh)
     438 ECB             :     {
     439 GIC       23422 :         vac_open_indexes(onerel, AccessShareLock, &nindexes, &Irel);
     440           23422 :         hasindex = nindexes > 0;
     441                 :     }
     442 ECB             :     else
     443                 :     {
     444 CBC          26 :         Irel = NULL;
     445 GIC          26 :         nindexes = 0;
     446 CBC          26 :         hasindex = false;
     447 ECB             :     }
     448 GIC       23761 :     indexdata = NULL;
     449 CBC       23761 :     if (nindexes > 0)
     450 ECB             :     {
     451 GIC       20995 :         indexdata = (AnlIndexData *) palloc0(nindexes * sizeof(AnlIndexData));
     452 CBC       61625 :         for (ind = 0; ind < nindexes; ind++)
     453                 :         {
     454 GIC       40631 :             AnlIndexData *thisdata = &indexdata[ind];
     455 ECB             :             IndexInfo  *indexInfo;
     456                 : 
     457 CBC       40631 :             thisdata->indexInfo = indexInfo = BuildIndexInfo(Irel[ind]);
     458 GIC       40630 :             thisdata->tupleFract = 1.0; /* fix later if partial */
     459 CBC       40630 :             if (indexInfo->ii_Expressions != NIL && va_cols == NIL)
     460                 :             {
     461              31 :                 ListCell   *indexpr_item = list_head(indexInfo->ii_Expressions);
     462 ECB             : 
     463 CBC          31 :                 thisdata->vacattrstats = (VacAttrStats **)
     464              31 :                     palloc(indexInfo->ii_NumIndexAttrs * sizeof(VacAttrStats *));
     465 GIC          31 :                 tcnt = 0;
     466 CBC          64 :                 for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
     467                 :                 {
     468              33 :                     int         keycol = indexInfo->ii_IndexAttrNumbers[i];
     469                 : 
     470 GIC          33 :                     if (keycol == 0)
     471                 :                     {
     472                 :                         /* Found an index expression */
     473 ECB             :                         Node       *indexkey;
     474 EUB             : 
     475 CBC          31 :                         if (indexpr_item == NULL)   /* shouldn't happen */
     476 LBC           0 :                             elog(ERROR, "too few entries in indexprs list");
     477 GIC          31 :                         indexkey = (Node *) lfirst(indexpr_item);
     478 CBC          31 :                         indexpr_item = lnext(indexInfo->ii_Expressions,
     479 ECB             :                                              indexpr_item);
     480 CBC          62 :                         thisdata->vacattrstats[tcnt] =
     481              31 :                             examine_attribute(Irel[ind], i + 1, indexkey);
     482 GIC          31 :                         if (thisdata->vacattrstats[tcnt] != NULL)
     483              31 :                             tcnt++;
     484 ECB             :                     }
     485                 :                 }
     486 GIC          31 :                 thisdata->attr_cnt = tcnt;
     487                 :             }
     488                 :         }
     489                 :     }
     490                 : 
     491                 :     /*
     492                 :      * Determine how many rows we need to sample, using the worst case from
     493                 :      * all analyzable columns.  We use a lower bound of 100 rows to avoid
     494                 :      * possible overflow in Vitter's algorithm.  (Note: that will also be the
     495 ECB             :      * target in the corner case where there are no analyzable columns.)
     496                 :      */
     497 GIC       23760 :     targrows = 100;
     498 CBC      220787 :     for (i = 0; i < attr_cnt; i++)
     499 ECB             :     {
     500 GIC      197027 :         if (targrows < vacattrstats[i]->minrows)
     501 CBC       23757 :             targrows = vacattrstats[i]->minrows;
     502                 :     }
     503           64390 :     for (ind = 0; ind < nindexes; ind++)
     504                 :     {
     505           40630 :         AnlIndexData *thisdata = &indexdata[ind];
     506                 : 
     507           40661 :         for (i = 0; i < thisdata->attr_cnt; i++)
     508 EUB             :         {
     509 GIC          31 :             if (targrows < thisdata->vacattrstats[i]->minrows)
     510 UIC           0 :                 targrows = thisdata->vacattrstats[i]->minrows;
     511                 :         }
     512                 :     }
     513                 : 
     514                 :     /*
     515                 :      * Look at extended statistics objects too, as those may define custom
     516                 :      * statistics target. So we may need to sample more rows and then build
     517 ECB             :      * the statistics with enough detail.
     518                 :      */
     519 CBC       23760 :     minrows = ComputeExtStatisticsRows(onerel, attr_cnt, vacattrstats);
     520 EUB             : 
     521 GIC       23760 :     if (targrows < minrows)
     522 UIC           0 :         targrows = minrows;
     523                 : 
     524                 :     /*
     525 ECB             :      * Acquire the sample rows
     526                 :      */
     527 GIC       23760 :     rows = (HeapTuple *) palloc(targrows * sizeof(HeapTuple));
     528           23760 :     pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE,
     529 ECB             :                                  inh ? PROGRESS_ANALYZE_PHASE_ACQUIRE_SAMPLE_ROWS_INH :
     530                 :                                  PROGRESS_ANALYZE_PHASE_ACQUIRE_SAMPLE_ROWS);
     531 GIC       23760 :     if (inh)
     532             339 :         numrows = acquire_inherited_sample_rows(onerel, elevel,
     533                 :                                                 rows, targrows,
     534 ECB             :                                                 &totalrows, &totaldeadrows);
     535                 :     else
     536 GIC       23421 :         numrows = (*acquirefunc) (onerel, elevel,
     537                 :                                   rows, targrows,
     538                 :                                   &totalrows, &totaldeadrows);
     539                 : 
     540                 :     /*
     541                 :      * Compute the statistics.  Temporary results during the calculations for
     542                 :      * each column are stored in a child context.  The calc routines are
     543                 :      * responsible to make sure that whatever they store into the VacAttrStats
     544 ECB             :      * structure is allocated in anl_context.
     545                 :      */
     546 GIC       23759 :     if (numrows > 0)
     547                 :     {
     548                 :         MemoryContext col_context,
     549 ECB             :                     old_context;
     550                 : 
     551 GIC       14181 :         pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE,
     552 ECB             :                                      PROGRESS_ANALYZE_PHASE_COMPUTE_STATS);
     553                 : 
     554 GIC       14181 :         col_context = AllocSetContextCreate(anl_context,
     555 ECB             :                                             "Analyze Column",
     556                 :                                             ALLOCSET_DEFAULT_SIZES);
     557 CBC       14181 :         old_context = MemoryContextSwitchTo(col_context);
     558                 : 
     559          153145 :         for (i = 0; i < attr_cnt; i++)
     560                 :         {
     561 GIC      138964 :             VacAttrStats *stats = vacattrstats[i];
     562 ECB             :             AttributeOpts *aopt;
     563                 : 
     564 CBC      138964 :             stats->rows = rows;
     565 GIC      138964 :             stats->tupDesc = onerel->rd_att;
     566          138964 :             stats->compute_stats(stats,
     567                 :                                  std_fetch_func,
     568                 :                                  numrows,
     569                 :                                  totalrows);
     570                 : 
     571                 :             /*
     572                 :              * If the appropriate flavor of the n_distinct option is
     573 ECB             :              * specified, override with the corresponding value.
     574                 :              */
     575 GIC      138964 :             aopt = get_attribute_options(onerel->rd_id, stats->attr->attnum);
     576          138964 :             if (aopt != NULL)
     577                 :             {
     578 ECB             :                 float8      n_distinct;
     579                 : 
     580 CBC           3 :                 n_distinct = inh ? aopt->n_distinct_inherited : aopt->n_distinct;
     581 GIC           3 :                 if (n_distinct != 0.0)
     582               3 :                     stats->stadistinct = n_distinct;
     583 ECB             :             }
     584                 : 
     585 GIC      138964 :             MemoryContextResetAndDeleteChildren(col_context);
     586 ECB             :         }
     587                 : 
     588 GIC       14181 :         if (nindexes > 0)
     589           11564 :             compute_index_stats(onerel, totalrows,
     590                 :                                 indexdata, nindexes,
     591                 :                                 rows, numrows,
     592 ECB             :                                 col_context);
     593                 : 
     594 GIC       14178 :         MemoryContextSwitchTo(old_context);
     595           14178 :         MemoryContextDelete(col_context);
     596                 : 
     597                 :         /*
     598                 :          * Emit the completed stats rows into pg_statistic, replacing any
     599                 :          * previous statistics for the target columns.  (If there are stats in
     600 ECB             :          * pg_statistic for columns we didn't process, we leave them alone.)
     601                 :          */
     602 GIC       14178 :         update_attstats(RelationGetRelid(onerel), inh,
     603 ECB             :                         attr_cnt, vacattrstats);
     604                 : 
     605 CBC       37909 :         for (ind = 0; ind < nindexes; ind++)
     606                 :         {
     607           23731 :             AnlIndexData *thisdata = &indexdata[ind];
     608                 : 
     609 GIC       23731 :             update_attstats(RelationGetRelid(Irel[ind]), false,
     610                 :                             thisdata->attr_cnt, thisdata->vacattrstats);
     611                 :         }
     612 ECB             : 
     613                 :         /* Build extended statistics (if there are any). */
     614 GIC       14178 :         BuildRelationExtStatistics(onerel, inh, totalrows, numrows, rows,
     615                 :                                    attr_cnt, vacattrstats);
     616 ECB             :     }
     617                 : 
     618 GIC       23756 :     pgstat_progress_update_param(PROGRESS_ANALYZE_PHASE,
     619                 :                                  PROGRESS_ANALYZE_PHASE_FINALIZE_ANALYZE);
     620                 : 
     621                 :     /*
     622                 :      * Update pages/tuples stats in pg_class ... but not if we're doing
     623                 :      * inherited stats.
     624                 :      *
     625                 :      * We assume that VACUUM hasn't set pg_class.reltuples already, even
     626                 :      * during a VACUUM ANALYZE.  Although VACUUM often updates pg_class,
     627                 :      * exceptions exist.  A "VACUUM (ANALYZE, INDEX_CLEANUP OFF)" command will
     628                 :      * never update pg_class entries for index relations.  It's also possible
     629                 :      * that an individual index's pg_class entry won't be updated during
     630 ECB             :      * VACUUM if the index AM returns NULL from its amvacuumcleanup() routine.
     631                 :      */
     632 GIC       23756 :     if (!inh)
     633                 :     {
     634 ECB             :         BlockNumber relallvisible;
     635                 : 
     636 GIC       23417 :         visibilitymap_count(onerel, &relallvisible, NULL);
     637 ECB             : 
     638                 :         /* Update pg_class for table relation */
     639 GIC       23417 :         vac_update_relstats(onerel,
     640                 :                             relpages,
     641                 :                             totalrows,
     642                 :                             relallvisible,
     643                 :                             hasindex,
     644                 :                             InvalidTransactionId,
     645                 :                             InvalidMultiXactId,
     646                 :                             NULL, NULL,
     647                 :                             in_outer_xact);
     648 ECB             : 
     649                 :         /* Same for indexes */
     650 CBC       64041 :         for (ind = 0; ind < nindexes; ind++)
     651                 :         {
     652 GIC       40624 :             AnlIndexData *thisdata = &indexdata[ind];
     653 ECB             :             double      totalindexrows;
     654                 : 
     655 CBC       40624 :             totalindexrows = ceil(thisdata->tupleFract * totalrows);
     656 GIC       40624 :             vac_update_relstats(Irel[ind],
     657           40624 :                                 RelationGetNumberOfBlocks(Irel[ind]),
     658                 :                                 totalindexrows,
     659                 :                                 0,
     660                 :                                 false,
     661                 :                                 InvalidTransactionId,
     662                 :                                 InvalidMultiXactId,
     663                 :                                 NULL, NULL,
     664                 :                                 in_outer_xact);
     665 ECB             :         }
     666                 :     }
     667 GIC         339 :     else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     668                 :     {
     669                 :         /*
     670                 :          * Partitioned tables don't have storage, so we don't set any fields
     671 ECB             :          * in their pg_class entries except for reltuples and relhasindex.
     672                 :          */
     673 GIC         313 :         vac_update_relstats(onerel, -1, totalrows,
     674                 :                             0, hasindex, InvalidTransactionId,
     675                 :                             InvalidMultiXactId,
     676                 :                             NULL, NULL,
     677                 :                             in_outer_xact);
     678                 :     }
     679                 : 
     680                 :     /*
     681                 :      * Now report ANALYZE to the cumulative stats system.  For regular tables,
     682                 :      * we do it only if not doing inherited stats.  For partitioned tables, we
     683                 :      * only do it for inherited stats. (We're never called for not-inherited
     684                 :      * stats on partitioned tables anyway.)
     685                 :      *
     686                 :      * Reset the changes_since_analyze counter only if we analyzed all
     687 ECB             :      * columns; otherwise, there is still work for auto-analyze to do.
     688                 :      */
     689 GIC       23756 :     if (!inh)
     690 CBC       23417 :         pgstat_report_analyze(onerel, totalrows, totaldeadrows,
     691 ECB             :                               (va_cols == NIL));
     692 GIC         339 :     else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     693             313 :         pgstat_report_analyze(onerel, 0, 0, (va_cols == NIL));
     694                 : 
     695                 :     /*
     696                 :      * If this isn't part of VACUUM ANALYZE, let index AMs do cleanup.
     697                 :      *
     698                 :      * Note that most index AMs perform a no-op as a matter of policy for
     699                 :      * amvacuumcleanup() when called in ANALYZE-only mode.  The only exception
     700 ECB             :      * among core index AMs is GIN/ginvacuumcleanup().
     701                 :      */
     702 CBC       23756 :     if (!(params->options & VACOPT_VACUUM))
     703                 :     {
     704 GIC       63282 :         for (ind = 0; ind < nindexes; ind++)
     705                 :         {
     706                 :             IndexBulkDeleteResult *stats;
     707 ECB             :             IndexVacuumInfo ivinfo;
     708                 : 
     709 CBC       40024 :             ivinfo.index = Irel[ind];
     710 GNC       40024 :             ivinfo.heaprel = onerel;
     711 CBC       40024 :             ivinfo.analyze_only = true;
     712           40024 :             ivinfo.estimated_count = true;
     713           40024 :             ivinfo.message_level = elevel;
     714           40024 :             ivinfo.num_heap_tuples = onerel->rd_rel->reltuples;
     715 GIC       40024 :             ivinfo.strategy = vac_strategy;
     716 ECB             : 
     717 GIC       40024 :             stats = index_vacuum_cleanup(&ivinfo, NULL);
     718 ECB             : 
     719 GBC       40024 :             if (stats)
     720 UIC           0 :                 pfree(stats);
     721                 :         }
     722                 :     }
     723                 : 
     724 ECB             :     /* Done with indexes */
     725 GIC       23756 :     vac_close_indexes(nindexes, Irel, NoLock);
     726                 : 
     727 ECB             :     /* Log the action if appropriate */
     728 GIC       23756 :     if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
     729 ECB             :     {
     730 GIC         159 :         TimestampTz endtime = GetCurrentTimestamp();
     731 ECB             : 
     732 CBC         236 :         if (params->log_min_duration == 0 ||
     733 GIC          77 :             TimestampDifferenceExceeds(starttime, endtime,
     734                 :                                        params->log_min_duration))
     735                 :         {
     736 ECB             :             long        delay_in_ms;
     737 CBC          82 :             double      read_rate = 0;
     738 GIC          82 :             double      write_rate = 0;
     739                 :             StringInfoData buf;
     740                 : 
     741                 :             /*
     742                 :              * Calculate the difference in the Page Hit/Miss/Dirty that
     743                 :              * happened as part of the analyze by subtracting out the
     744                 :              * pre-analyze values which we saved above.
     745 ECB             :              */
     746 CBC          82 :             AnalyzePageHit = VacuumPageHit - AnalyzePageHit;
     747              82 :             AnalyzePageMiss = VacuumPageMiss - AnalyzePageMiss;
     748 GIC          82 :             AnalyzePageDirty = VacuumPageDirty - AnalyzePageDirty;
     749                 : 
     750                 :             /*
     751                 :              * We do not expect an analyze to take > 25 days and it simplifies
     752                 :              * things a bit to use TimestampDifferenceMilliseconds.
     753 ECB             :              */
     754 GIC          82 :             delay_in_ms = TimestampDifferenceMilliseconds(starttime, endtime);
     755                 : 
     756                 :             /*
     757                 :              * Note that we are reporting these read/write rates in the same
     758                 :              * manner as VACUUM does, which means that while the 'average read
     759                 :              * rate' here actually corresponds to page misses and resulting
     760                 :              * reads which are also picked up by track_io_timing, if enabled,
     761                 :              * the 'average write rate' is actually talking about the rate of
     762                 :              * pages being dirtied, not being written out, so it's typical to
     763                 :              * have a non-zero 'avg write rate' while I/O timings only reports
     764                 :              * reads.
     765                 :              *
     766                 :              * It's not clear that an ANALYZE will ever result in
     767                 :              * FlushBuffer() being called, but we track and support reporting
     768                 :              * on I/O write time in case that changes as it's practically free
     769                 :              * to do so anyway.
     770                 :              */
     771 ECB             : 
     772 GIC          82 :             if (delay_in_ms > 0)
     773 ECB             :             {
     774 CBC          82 :                 read_rate = (double) BLCKSZ * AnalyzePageMiss / (1024 * 1024) /
     775              82 :                     (delay_in_ms / 1000.0);
     776              82 :                 write_rate = (double) BLCKSZ * AnalyzePageDirty / (1024 * 1024) /
     777 GIC          82 :                     (delay_in_ms / 1000.0);
     778                 :             }
     779                 : 
     780                 :             /*
     781                 :              * We split this up so we don't emit empty I/O timing values when
     782                 :              * track_io_timing isn't enabled.
     783                 :              */
     784 ECB             : 
     785 CBC          82 :             initStringInfo(&buf);
     786 GIC          82 :             appendStringInfo(&buf, _("automatic analyze of table \"%s.%s.%s\"\n"),
     787 ECB             :                              get_database_name(MyDatabaseId),
     788 CBC          82 :                              get_namespace_name(RelationGetNamespace(onerel)),
     789              82 :                              RelationGetRelationName(onerel));
     790 GIC          82 :             if (track_io_timing)
     791 EUB             :             {
     792 UBC           0 :                 double      read_ms = (double) (pgStatBlockReadTime - startreadtime) / 1000;
     793 UIC           0 :                 double      write_ms = (double) (pgStatBlockWriteTime - startwritetime) / 1000;
     794 EUB             : 
     795 UIC           0 :                 appendStringInfo(&buf, _("I/O timings: read: %.3f ms, write: %.3f ms\n"),
     796                 :                                  read_ms, write_ms);
     797 ECB             :             }
     798 GIC          82 :             appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"),
     799 ECB             :                              read_rate, write_rate);
     800 GIC          82 :             appendStringInfo(&buf, _("buffer usage: %lld hits, %lld misses, %lld dirtied\n"),
     801                 :                              (long long) AnalyzePageHit,
     802                 :                              (long long) AnalyzePageMiss,
     803 ECB             :                              (long long) AnalyzePageDirty);
     804 GIC          82 :             appendStringInfo(&buf, _("system usage: %s"), pg_rusage_show(&ru0));
     805 ECB             : 
     806 GIC          82 :             ereport(LOG,
     807                 :                     (errmsg_internal("%s", buf.data)));
     808 ECB             : 
     809 GIC          82 :             pfree(buf.data);
     810                 :         }
     811                 :     }
     812                 : 
     813 ECB             :     /* Roll back any GUC changes executed by index functions */
     814 GIC       23756 :     AtEOXact_GUC(false, save_nestlevel);
     815                 : 
     816 ECB             :     /* Restore userid and security context */
     817 GIC       23756 :     SetUserIdAndSecContext(save_userid, save_sec_context);
     818                 : 
     819 ECB             :     /* Restore current context and release memory */
     820 CBC       23756 :     MemoryContextSwitchTo(caller_context);
     821           23756 :     MemoryContextDelete(anl_context);
     822           23756 :     anl_context = NULL;
     823 GIC       23756 : }
     824                 : 
     825                 : /*
     826                 :  * Compute statistics about indexes of a relation
     827                 :  */
     828 ECB             : static void
     829 GIC       11564 : compute_index_stats(Relation onerel, double totalrows,
     830                 :                     AnlIndexData *indexdata, int nindexes,
     831                 :                     HeapTuple *rows, int numrows,
     832                 :                     MemoryContext col_context)
     833                 : {
     834                 :     MemoryContext ind_context,
     835                 :                 old_context;
     836                 :     Datum       values[INDEX_MAX_KEYS];
     837                 :     bool        isnull[INDEX_MAX_KEYS];
     838                 :     int         ind,
     839                 :                 i;
     840 ECB             : 
     841 GIC       11564 :     ind_context = AllocSetContextCreate(anl_context,
     842                 :                                         "Analyze Index",
     843 ECB             :                                         ALLOCSET_DEFAULT_SIZES);
     844 GIC       11564 :     old_context = MemoryContextSwitchTo(ind_context);
     845 ECB             : 
     846 GIC       35298 :     for (ind = 0; ind < nindexes; ind++)
     847 ECB             :     {
     848 CBC       23737 :         AnlIndexData *thisdata = &indexdata[ind];
     849           23737 :         IndexInfo  *indexInfo = thisdata->indexInfo;
     850 GIC       23737 :         int         attr_cnt = thisdata->attr_cnt;
     851                 :         TupleTableSlot *slot;
     852                 :         EState     *estate;
     853                 :         ExprContext *econtext;
     854                 :         ExprState  *predicate;
     855                 :         Datum      *exprvals;
     856                 :         bool       *exprnulls;
     857                 :         int         numindexrows,
     858                 :                     tcnt,
     859                 :                     rowno;
     860                 :         double      totalindexrows;
     861                 : 
     862 ECB             :         /* Ignore index if no columns to analyze and not partial */
     863 CBC       23737 :         if (attr_cnt == 0 && indexInfo->ii_Predicate == NIL)
     864 GIC       23689 :             continue;
     865                 : 
     866                 :         /*
     867                 :          * Need an EState for evaluation of index expressions and
     868                 :          * partial-index predicates.  Create it in the per-index context to be
     869                 :          * sure it gets cleaned up at the bottom of the loop.
     870 ECB             :          */
     871 CBC          48 :         estate = CreateExecutorState();
     872 GIC          48 :         econtext = GetPerTupleExprContext(estate);
     873 ECB             :         /* Need a slot to hold the current heap tuple, too */
     874 GIC          48 :         slot = MakeSingleTupleTableSlot(RelationGetDescr(onerel),
     875                 :                                         &TTSOpsHeapTuple);
     876                 : 
     877 ECB             :         /* Arrange for econtext's scan tuple to be the tuple under test */
     878 GIC          48 :         econtext->ecxt_scantuple = slot;
     879                 : 
     880 ECB             :         /* Set up execution state for predicate. */
     881 GIC          48 :         predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
     882                 : 
     883 ECB             :         /* Compute and save index expression values */
     884 CBC          48 :         exprvals = (Datum *) palloc(numrows * attr_cnt * sizeof(Datum));
     885              48 :         exprnulls = (bool *) palloc(numrows * attr_cnt * sizeof(bool));
     886              48 :         numindexrows = 0;
     887              48 :         tcnt = 0;
     888 GIC      106287 :         for (rowno = 0; rowno < numrows; rowno++)
     889 ECB             :         {
     890 GIC      106242 :             HeapTuple   heapTuple = rows[rowno];
     891 ECB             : 
     892 GIC      106242 :             vacuum_delay_point();
     893                 : 
     894                 :             /*
     895                 :              * Reset the per-tuple context each time, to reclaim any cruft
     896                 :              * left behind by evaluating the predicate or index expressions.
     897 ECB             :              */
     898 GIC      106242 :             ResetExprContext(econtext);
     899                 : 
     900 ECB             :             /* Set up for predicate or expression evaluation */
     901 GIC      106242 :             ExecStoreHeapTuple(heapTuple, slot, false);
     902                 : 
     903 ECB             :             /* If index is partial, check predicate */
     904 GIC      106242 :             if (predicate != NULL)
     905 ECB             :             {
     906 CBC       29024 :                 if (!ExecQual(predicate, econtext))
     907 GIC       10666 :                     continue;
     908 ECB             :             }
     909 GIC       95576 :             numindexrows++;
     910 ECB             : 
     911 GIC       95576 :             if (attr_cnt > 0)
     912                 :             {
     913                 :                 /*
     914                 :                  * Evaluate the index row to compute expression values. We
     915                 :                  * could do this by hand, but FormIndexDatum is convenient.
     916 ECB             :                  */
     917 GIC       77218 :                 FormIndexDatum(indexInfo,
     918                 :                                slot,
     919                 :                                estate,
     920                 :                                values,
     921                 :                                isnull);
     922                 : 
     923                 :                 /*
     924                 :                  * Save just the columns we care about.  We copy the values
     925                 :                  * into ind_context from the estate's per-tuple context.
     926 ECB             :                  */
     927 GIC      154430 :                 for (i = 0; i < attr_cnt; i++)
     928 ECB             :                 {
     929 CBC       77215 :                     VacAttrStats *stats = thisdata->vacattrstats[i];
     930 GIC       77215 :                     int         attnum = stats->attr->attnum;
     931 ECB             : 
     932 GIC       77215 :                     if (isnull[attnum - 1])
     933 EUB             :                     {
     934 UBC           0 :                         exprvals[tcnt] = (Datum) 0;
     935 UIC           0 :                         exprnulls[tcnt] = true;
     936                 :                     }
     937                 :                     else
     938 ECB             :                     {
     939 CBC      154430 :                         exprvals[tcnt] = datumCopy(values[attnum - 1],
     940           77215 :                                                    stats->attrtype->typbyval,
     941           77215 :                                                    stats->attrtype->typlen);
     942 GIC       77215 :                         exprnulls[tcnt] = false;
     943 ECB             :                     }
     944 GIC       77215 :                     tcnt++;
     945                 :                 }
     946                 :             }
     947                 :         }
     948                 : 
     949                 :         /*
     950                 :          * Having counted the number of rows that pass the predicate in the
     951                 :          * sample, we can estimate the total number of rows in the index.
     952 ECB             :          */
     953 CBC          45 :         thisdata->tupleFract = (double) numindexrows / (double) numrows;
     954 GIC          45 :         totalindexrows = ceil(thisdata->tupleFract * totalrows);
     955                 : 
     956                 :         /*
     957                 :          * Now we can compute the statistics for the expression columns.
     958 ECB             :          */
     959 GIC          45 :         if (numindexrows > 0)
     960 ECB             :         {
     961 CBC          42 :             MemoryContextSwitchTo(col_context);
     962 GIC          67 :             for (i = 0; i < attr_cnt; i++)
     963 ECB             :             {
     964 GIC          25 :                 VacAttrStats *stats = thisdata->vacattrstats[i];
     965 ECB             : 
     966 CBC          25 :                 stats->exprvals = exprvals + i;
     967              25 :                 stats->exprnulls = exprnulls + i;
     968              25 :                 stats->rowstride = attr_cnt;
     969 GIC          25 :                 stats->compute_stats(stats,
     970                 :                                      ind_fetch_func,
     971                 :                                      numindexrows,
     972                 :                                      totalindexrows);
     973 ECB             : 
     974 GIC          25 :                 MemoryContextResetAndDeleteChildren(col_context);
     975                 :             }
     976                 :         }
     977                 : 
     978 ECB             :         /* And clean up */
     979 GIC          45 :         MemoryContextSwitchTo(ind_context);
     980 ECB             : 
     981 CBC          45 :         ExecDropSingleTupleTableSlot(slot);
     982              45 :         FreeExecutorState(estate);
     983 GIC          45 :         MemoryContextResetAndDeleteChildren(ind_context);
     984                 :     }
     985 ECB             : 
     986 CBC       11561 :     MemoryContextSwitchTo(old_context);
     987           11561 :     MemoryContextDelete(ind_context);
     988 GIC       11561 : }
     989                 : 
     990                 : /*
     991                 :  * examine_attribute -- pre-analysis of a single column
     992                 :  *
     993                 :  * Determine whether the column is analyzable; if so, create and initialize
     994                 :  * a VacAttrStats struct for it.  If not, return NULL.
     995                 :  *
     996                 :  * If index_expr isn't NULL, then we're trying to analyze an expression index,
     997                 :  * and index_expr is the expression tree representing the column's data.
     998                 :  */
     999 ECB             : static VacAttrStats *
    1000 GIC      197077 : examine_attribute(Relation onerel, int attnum, Node *index_expr)
    1001 ECB             : {
    1002 GIC      197077 :     Form_pg_attribute attr = TupleDescAttr(onerel->rd_att, attnum - 1);
    1003                 :     HeapTuple   typtuple;
    1004                 :     VacAttrStats *stats;
    1005                 :     int         i;
    1006                 :     bool        ok;
    1007                 : 
    1008 ECB             :     /* Never analyze dropped columns */
    1009 CBC      197077 :     if (attr->attisdropped)
    1010 GIC           3 :         return NULL;
    1011                 : 
    1012 ECB             :     /* Don't analyze column if user has specified not to */
    1013 CBC      197074 :     if (attr->attstattarget == 0)
    1014 GIC           3 :         return NULL;
    1015                 : 
    1016                 :     /*
    1017                 :      * Create the VacAttrStats struct.  Note that we only have a copy of the
    1018                 :      * fixed fields of the pg_attribute tuple.
    1019 ECB             :      */
    1020 CBC      197071 :     stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
    1021          197071 :     stats->attr = (Form_pg_attribute) palloc(ATTRIBUTE_FIXED_PART_SIZE);
    1022 GIC      197071 :     memcpy(stats->attr, attr, ATTRIBUTE_FIXED_PART_SIZE);
    1023                 : 
    1024                 :     /*
    1025                 :      * When analyzing an expression index, believe the expression tree's type
    1026                 :      * not the column datatype --- the latter might be the opckeytype storage
    1027                 :      * type of the opclass, which is not interesting for our purposes.  (Note:
    1028                 :      * if we did anything with non-expression index columns, we'd need to
    1029                 :      * figure out where to get the correct type info from, but for now that's
    1030                 :      * not a problem.)  It's not clear whether anyone will care about the
    1031                 :      * typmod, but we store that too just in case.
    1032 ECB             :      */
    1033 GIC      197071 :     if (index_expr)
    1034 ECB             :     {
    1035 CBC          31 :         stats->attrtypid = exprType(index_expr);
    1036 GIC          31 :         stats->attrtypmod = exprTypmod(index_expr);
    1037                 : 
    1038                 :         /*
    1039                 :          * If a collation has been specified for the index column, use that in
    1040                 :          * preference to anything else; but if not, fall back to whatever we
    1041                 :          * can get from the expression.
    1042 ECB             :          */
    1043 CBC          31 :         if (OidIsValid(onerel->rd_indcollation[attnum - 1]))
    1044 GIC           6 :             stats->attrcollid = onerel->rd_indcollation[attnum - 1];
    1045 ECB             :         else
    1046 GIC          25 :             stats->attrcollid = exprCollation(index_expr);
    1047                 :     }
    1048                 :     else
    1049 ECB             :     {
    1050 CBC      197040 :         stats->attrtypid = attr->atttypid;
    1051          197040 :         stats->attrtypmod = attr->atttypmod;
    1052 GIC      197040 :         stats->attrcollid = attr->attcollation;
    1053                 :     }
    1054 ECB             : 
    1055 GIC      197071 :     typtuple = SearchSysCacheCopy1(TYPEOID,
    1056 ECB             :                                    ObjectIdGetDatum(stats->attrtypid));
    1057 GBC      197071 :     if (!HeapTupleIsValid(typtuple))
    1058 LBC           0 :         elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
    1059 CBC      197071 :     stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
    1060          197071 :     stats->anl_context = anl_context;
    1061 GIC      197071 :     stats->tupattnum = attnum;
    1062                 : 
    1063                 :     /*
    1064                 :      * The fields describing the stats->stavalues[n] element types default to
    1065                 :      * the type of the data being analyzed, but the type-specific typanalyze
    1066                 :      * function can change them if it wants to store something else.
    1067 ECB             :      */
    1068 GIC     1182426 :     for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
    1069 ECB             :     {
    1070 CBC      985355 :         stats->statypid[i] = stats->attrtypid;
    1071          985355 :         stats->statyplen[i] = stats->attrtype->typlen;
    1072          985355 :         stats->statypbyval[i] = stats->attrtype->typbyval;
    1073 GIC      985355 :         stats->statypalign[i] = stats->attrtype->typalign;
    1074                 :     }
    1075                 : 
    1076                 :     /*
    1077                 :      * Call the type-specific typanalyze function.  If none is specified, use
    1078                 :      * std_typanalyze().
    1079 ECB             :      */
    1080 CBC      197071 :     if (OidIsValid(stats->attrtype->typanalyze))
    1081 GIC       14103 :         ok = DatumGetBool(OidFunctionCall1(stats->attrtype->typanalyze,
    1082                 :                                            PointerGetDatum(stats)));
    1083 ECB             :     else
    1084 GIC      182968 :         ok = std_typanalyze(stats);
    1085 ECB             : 
    1086 GIC      197071 :     if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
    1087 EUB             :     {
    1088 UBC           0 :         heap_freetuple(typtuple);
    1089               0 :         pfree(stats->attr);
    1090               0 :         pfree(stats);
    1091 UIC           0 :         return NULL;
    1092                 :     }
    1093 ECB             : 
    1094 GIC      197071 :     return stats;
    1095                 : }
    1096                 : 
    1097                 : /*
    1098                 :  * acquire_sample_rows -- acquire a random sample of rows from the table
    1099                 :  *
    1100                 :  * Selected rows are returned in the caller-allocated array rows[], which
    1101                 :  * must have at least targrows entries.
    1102                 :  * The actual number of rows selected is returned as the function result.
    1103                 :  * We also estimate the total numbers of live and dead rows in the table,
    1104                 :  * and return them into *totalrows and *totaldeadrows, respectively.
    1105                 :  *
    1106                 :  * The returned list of tuples is in order by physical position in the table.
    1107                 :  * (We will rely on this later to derive correlation estimates.)
    1108                 :  *
    1109                 :  * As of May 2004 we use a new two-stage method:  Stage one selects up
    1110                 :  * to targrows random blocks (or all blocks, if there aren't so many).
    1111                 :  * Stage two scans these blocks and uses the Vitter algorithm to create
    1112                 :  * a random sample of targrows rows (or less, if there are less in the
    1113                 :  * sample of blocks).  The two stages are executed simultaneously: each
    1114                 :  * block is processed as soon as stage one returns its number and while
    1115                 :  * the rows are read stage two controls which ones are to be inserted
    1116                 :  * into the sample.
    1117                 :  *
    1118                 :  * Although every row has an equal chance of ending up in the final
    1119                 :  * sample, this sampling method is not perfect: not every possible
    1120                 :  * sample has an equal chance of being selected.  For large relations
    1121                 :  * the number of different blocks represented by the sample tends to be
    1122                 :  * too small.  We can live with that for now.  Improvements are welcome.
    1123                 :  *
    1124                 :  * An important property of this sampling method is that because we do
    1125                 :  * look at a statistically unbiased set of blocks, we should get
    1126                 :  * unbiased estimates of the average numbers of live and dead rows per
    1127                 :  * block.  The previous sampling method put too much credence in the row
    1128                 :  * density near the start of the table.
    1129                 :  */
    1130 ECB             : static int
    1131 GIC       24256 : acquire_sample_rows(Relation onerel, int elevel,
    1132                 :                     HeapTuple *rows, int targrows,
    1133                 :                     double *totalrows, double *totaldeadrows)
    1134 ECB             : {
    1135 CBC       24256 :     int         numrows = 0;    /* # rows now in reservoir */
    1136           24256 :     double      samplerows = 0; /* total # rows collected */
    1137           24256 :     double      liverows = 0;   /* # live rows seen */
    1138           24256 :     double      deadrows = 0;   /* # dead rows seen */
    1139 GIC       24256 :     double      rowstoskip = -1;    /* -1 means not set yet */
    1140                 :     uint32      randseed;       /* Seed for block sampler(s) */
    1141                 :     BlockNumber totalblocks;
    1142                 :     TransactionId OldestXmin;
    1143                 :     BlockSamplerData bs;
    1144                 :     ReservoirStateData rstate;
    1145                 :     TupleTableSlot *slot;
    1146                 :     TableScanDesc scan;
    1147 ECB             :     BlockNumber nblocks;
    1148 GIC       24256 :     BlockNumber blksdone = 0;
    1149 ECB             : #ifdef USE_PREFETCH
    1150 GIC       24256 :     int         prefetch_maximum = 0;   /* blocks to prefetch if enabled */
    1151                 :     BlockSamplerData prefetch_bs;
    1152                 : #endif
    1153 ECB             : 
    1154 GIC       24256 :     Assert(targrows > 0);
    1155 ECB             : 
    1156 GIC       24256 :     totalblocks = RelationGetNumberOfBlocks(onerel);
    1157                 : 
    1158 ECB             :     /* Need a cutoff xmin for HeapTupleSatisfiesVacuum */
    1159 GIC       24256 :     OldestXmin = GetOldestNonRemovableTransactionId(onerel);
    1160                 : 
    1161 ECB             :     /* Prepare for sampling block numbers */
    1162 CBC       24256 :     randseed = pg_prng_uint32(&pg_global_prng_state);
    1163 GIC       24256 :     nblocks = BlockSampler_Init(&bs, totalblocks, targrows, randseed);
    1164                 : 
    1165 ECB             : #ifdef USE_PREFETCH
    1166 GIC       24256 :     prefetch_maximum = get_tablespace_maintenance_io_concurrency(onerel->rd_rel->reltablespace);
    1167 ECB             :     /* Create another BlockSampler, using the same seed, for prefetching */
    1168 CBC       24256 :     if (prefetch_maximum)
    1169 GIC       24256 :         (void) BlockSampler_Init(&prefetch_bs, totalblocks, targrows, randseed);
    1170                 : #endif
    1171                 : 
    1172 ECB             :     /* Report sampling block numbers */
    1173 GIC       24256 :     pgstat_progress_update_param(PROGRESS_ANALYZE_BLOCKS_TOTAL,
    1174                 :                                  nblocks);
    1175                 : 
    1176 ECB             :     /* Prepare for sampling rows */
    1177 GIC       24256 :     reservoir_init_selection_state(&rstate, targrows);
    1178 ECB             : 
    1179 CBC       24256 :     scan = table_beginscan_analyze(onerel);
    1180 GIC       24256 :     slot = table_slot_create(onerel, NULL);
    1181                 : 
    1182                 : #ifdef USE_PREFETCH
    1183                 : 
    1184                 :     /*
    1185                 :      * If we are doing prefetching, then go ahead and tell the kernel about
    1186                 :      * the first set of pages we are going to want.  This also moves our
    1187                 :      * iterator out ahead of the main one being used, where we will keep it so
    1188                 :      * that we're always pre-fetching out prefetch_maximum number of blocks
    1189                 :      * ahead.
    1190 ECB             :      */
    1191 GIC       24256 :     if (prefetch_maximum)
    1192 ECB             :     {
    1193 GIC       80832 :         for (int i = 0; i < prefetch_maximum; i++)
    1194                 :         {
    1195                 :             BlockNumber prefetch_block;
    1196 ECB             : 
    1197 CBC       77493 :             if (!BlockSampler_HasMore(&prefetch_bs))
    1198 GIC       20917 :                 break;
    1199 ECB             : 
    1200 CBC       56576 :             prefetch_block = BlockSampler_Next(&prefetch_bs);
    1201 GIC       56576 :             PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, prefetch_block);
    1202                 :         }
    1203                 :     }
    1204                 : #endif
    1205                 : 
    1206 ECB             :     /* Outer loop over blocks to sample */
    1207 GIC      171614 :     while (BlockSampler_HasMore(&bs))
    1208                 :     {
    1209 ECB             :         bool        block_accepted;
    1210 GIC      147358 :         BlockNumber targblock = BlockSampler_Next(&bs);
    1211 ECB             : #ifdef USE_PREFETCH
    1212 GIC      147358 :         BlockNumber prefetch_targblock = InvalidBlockNumber;
    1213                 : 
    1214                 :         /*
    1215                 :          * Make sure that every time the main BlockSampler is moved forward
    1216                 :          * that our prefetch BlockSampler also gets moved forward, so that we
    1217                 :          * always stay out ahead.
    1218 ECB             :          */
    1219 CBC      147358 :         if (prefetch_maximum && BlockSampler_HasMore(&prefetch_bs))
    1220 GIC       90782 :             prefetch_targblock = BlockSampler_Next(&prefetch_bs);
    1221                 : #endif
    1222 ECB             : 
    1223 GIC      147358 :         vacuum_delay_point();
    1224 ECB             : 
    1225 GIC      147358 :         block_accepted = table_scan_analyze_next_block(scan, targblock, vac_strategy);
    1226                 : 
    1227                 : #ifdef USE_PREFETCH
    1228                 : 
    1229                 :         /*
    1230                 :          * When pre-fetching, after we get a block, tell the kernel about the
    1231                 :          * next one we will want, if there's any left.
    1232                 :          *
    1233                 :          * We want to do this even if the table_scan_analyze_next_block() call
    1234                 :          * above decides against analyzing the block it picked.
    1235 ECB             :          */
    1236 CBC      147358 :         if (prefetch_maximum && prefetch_targblock != InvalidBlockNumber)
    1237 GIC       90782 :             PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, prefetch_targblock);
    1238                 : #endif
    1239                 : 
    1240                 :         /*
    1241                 :          * Don't analyze if table_scan_analyze_next_block() indicated this
    1242                 :          * block is unsuitable for analyzing.
    1243 ECB             :          */
    1244 GBC      147358 :         if (!block_accepted)
    1245 UIC           0 :             continue;
    1246 ECB             : 
    1247 GIC    10303246 :         while (table_scan_analyze_next_tuple(scan, OldestXmin, &liverows, &deadrows, slot))
    1248                 :         {
    1249                 :             /*
    1250                 :              * The first targrows sample rows are simply copied into the
    1251                 :              * reservoir. Then we start replacing tuples in the sample until
    1252                 :              * we reach the end of the relation.  This algorithm is from Jeff
    1253                 :              * Vitter's paper (see full citation in utils/misc/sampling.c). It
    1254                 :              * works by repeatedly computing the number of tuples to skip
    1255                 :              * before selecting a tuple, which replaces a randomly chosen
    1256                 :              * element of the reservoir (current set of tuples).  At all times
    1257                 :              * the reservoir is a true random sample of the tuples we've
    1258                 :              * passed over so far, so when we fall off the end of the relation
    1259                 :              * we're done.
    1260 ECB             :              */
    1261 CBC    10155888 :             if (numrows < targrows)
    1262 GIC    10029743 :                 rows[numrows++] = ExecCopySlotHeapTuple(slot);
    1263                 :             else
    1264                 :             {
    1265                 :                 /*
    1266                 :                  * t in Vitter's paper is the number of records already
    1267                 :                  * processed.  If we need to compute a new S value, we must
    1268                 :                  * use the not-yet-incremented value of samplerows as t.
    1269 ECB             :                  */
    1270 CBC      126145 :                 if (rowstoskip < 0)
    1271 GIC       57678 :                     rowstoskip = reservoir_get_next_S(&rstate, samplerows, targrows);
    1272 ECB             : 
    1273 GIC      126145 :                 if (rowstoskip <= 0)
    1274                 :                 {
    1275                 :                     /*
    1276                 :                      * Found a suitable tuple, so save it, replacing one old
    1277                 :                      * tuple at random
    1278 ECB             :                      */
    1279 GIC       57652 :                     int         k = (int) (targrows * sampler_random_fract(&rstate.randstate));
    1280 ECB             : 
    1281 CBC       57652 :                     Assert(k >= 0 && k < targrows);
    1282           57652 :                     heap_freetuple(rows[k]);
    1283 GIC       57652 :                     rows[k] = ExecCopySlotHeapTuple(slot);
    1284                 :                 }
    1285 ECB             : 
    1286 GIC      126145 :                 rowstoskip -= 1;
    1287                 :             }
    1288 ECB             : 
    1289 GIC    10155888 :             samplerows += 1;
    1290                 :         }
    1291 ECB             : 
    1292 GIC      147358 :         pgstat_progress_update_param(PROGRESS_ANALYZE_BLOCKS_DONE,
    1293                 :                                      ++blksdone);
    1294                 :     }
    1295 ECB             : 
    1296 CBC       24256 :     ExecDropSingleTupleTableSlot(slot);
    1297 GIC       24256 :     table_endscan(scan);
    1298                 : 
    1299                 :     /*
    1300                 :      * If we didn't find as many tuples as we wanted then we're done. No sort
    1301                 :      * is needed, since they're already in order.
    1302                 :      *
    1303                 :      * Otherwise we need to sort the collected tuples by position
    1304                 :      * (itempointer). It's not worth worrying about corner cases where the
    1305                 :      * tuples are already sorted.
    1306 ECB             :      */
    1307 CBC       24256 :     if (numrows == targrows)
    1308 GNC          79 :         qsort_interruptible(rows, numrows, sizeof(HeapTuple),
    1309                 :                             compare_rows, NULL);
    1310                 : 
    1311                 :     /*
    1312                 :      * Estimate total numbers of live and dead rows in relation, extrapolating
    1313                 :      * on the assumption that the average tuple density in pages we didn't
    1314                 :      * scan is the same as in the pages we did scan.  Since what we scanned is
    1315                 :      * a random sample of the pages in the relation, this should be a good
    1316                 :      * assumption.
    1317 ECB             :      */
    1318 GIC       24256 :     if (bs.m > 0)
    1319 ECB             :     {
    1320 CBC       14708 :         *totalrows = floor((liverows / bs.m) * totalblocks + 0.5);
    1321 GIC       14708 :         *totaldeadrows = floor((deadrows / bs.m) * totalblocks + 0.5);
    1322                 :     }
    1323                 :     else
    1324 ECB             :     {
    1325 CBC        9548 :         *totalrows = 0.0;
    1326 GIC        9548 :         *totaldeadrows = 0.0;
    1327                 :     }
    1328                 : 
    1329                 :     /*
    1330                 :      * Emit some interesting relation info
    1331 ECB             :      */
    1332 GIC       24256 :     ereport(elevel,
    1333                 :             (errmsg("\"%s\": scanned %d of %u pages, "
    1334                 :                     "containing %.0f live rows and %.0f dead rows; "
    1335                 :                     "%d rows in sample, %.0f estimated total rows",
    1336                 :                     RelationGetRelationName(onerel),
    1337                 :                     bs.m, totalblocks,
    1338                 :                     liverows, deadrows,
    1339                 :                     numrows, *totalrows)));
    1340 ECB             : 
    1341 GIC       24256 :     return numrows;
    1342                 : }
    1343                 : 
    1344                 : /*
    1345                 :  * Comparator for sorting rows[] array
    1346                 :  */
    1347 ECB             : static int
    1348 GIC     1958594 : compare_rows(const void *a, const void *b, void *arg)
    1349 ECB             : {
    1350 CBC     1958594 :     HeapTuple   ha = *(const HeapTuple *) a;
    1351         1958594 :     HeapTuple   hb = *(const HeapTuple *) b;
    1352         1958594 :     BlockNumber ba = ItemPointerGetBlockNumber(&ha->t_self);
    1353         1958594 :     OffsetNumber oa = ItemPointerGetOffsetNumber(&ha->t_self);
    1354         1958594 :     BlockNumber bb = ItemPointerGetBlockNumber(&hb->t_self);
    1355 GIC     1958594 :     OffsetNumber ob = ItemPointerGetOffsetNumber(&hb->t_self);
    1356 ECB             : 
    1357 CBC     1958594 :     if (ba < bb)
    1358          432127 :         return -1;
    1359         1526467 :     if (ba > bb)
    1360          441265 :         return 1;
    1361         1085202 :     if (oa < ob)
    1362          711024 :         return -1;
    1363          374178 :     if (oa > ob)
    1364 GBC      374178 :         return 1;
    1365 UIC           0 :     return 0;
    1366                 : }
    1367                 : 
    1368                 : 
    1369                 : /*
    1370                 :  * acquire_inherited_sample_rows -- acquire sample rows from inheritance tree
    1371                 :  *
    1372                 :  * This has the same API as acquire_sample_rows, except that rows are
    1373                 :  * collected from all inheritance children as well as the specified table.
    1374                 :  * We fail and return zero if there are no inheritance children, or if all
    1375                 :  * children are foreign tables that don't support ANALYZE.
    1376                 :  */
    1377 ECB             : static int
    1378 GIC         339 : acquire_inherited_sample_rows(Relation onerel, int elevel,
    1379                 :                               HeapTuple *rows, int targrows,
    1380                 :                               double *totalrows, double *totaldeadrows)
    1381                 : {
    1382                 :     List       *tableOIDs;
    1383                 :     Relation   *rels;
    1384                 :     AcquireSampleRowsFunc *acquirefuncs;
    1385                 :     double     *relblocks;
    1386                 :     double      totalblocks;
    1387                 :     int         numrows,
    1388                 :                 nrels,
    1389                 :                 i;
    1390                 :     ListCell   *lc;
    1391                 :     bool        has_child;
    1392                 : 
    1393 ECB             :     /* Initialize output parameters to zero now, in case we exit early */
    1394 CBC         339 :     *totalrows = 0;
    1395 GIC         339 :     *totaldeadrows = 0;
    1396                 : 
    1397                 :     /*
    1398                 :      * Find all members of inheritance set.  We only need AccessShareLock on
    1399                 :      * the children.
    1400                 :      */
    1401 ECB             :     tableOIDs =
    1402 GIC         339 :         find_all_inheritors(RelationGetRelid(onerel), AccessShareLock, NULL);
    1403                 : 
    1404                 :     /*
    1405                 :      * Check that there's at least one descendant, else fail.  This could
    1406                 :      * happen despite analyze_rel's relhassubclass check, if table once had a
    1407                 :      * child but no longer does.  In that case, we can clear the
    1408                 :      * relhassubclass field so as not to make the same mistake again later.
    1409                 :      * (This is safe because we hold ShareUpdateExclusiveLock.)
    1410 ECB             :      */
    1411 GIC         339 :     if (list_length(tableOIDs) < 2)
    1412                 :     {
    1413 EUB             :         /* CCI because we already updated the pg_class row in this command */
    1414 UBC           0 :         CommandCounterIncrement();
    1415               0 :         SetRelationHasSubclass(RelationGetRelid(onerel), false);
    1416 UIC           0 :         ereport(elevel,
    1417                 :                 (errmsg("skipping analyze of \"%s.%s\" inheritance tree --- this inheritance tree contains no child tables",
    1418                 :                         get_namespace_name(RelationGetNamespace(onerel)),
    1419 EUB             :                         RelationGetRelationName(onerel))));
    1420 UIC           0 :         return 0;
    1421                 :     }
    1422                 : 
    1423                 :     /*
    1424                 :      * Identify acquirefuncs to use, and count blocks in all the relations.
    1425                 :      * The result could overflow BlockNumber, so we use double arithmetic.
    1426 ECB             :      */
    1427 GIC         339 :     rels = (Relation *) palloc(list_length(tableOIDs) * sizeof(Relation));
    1428 ECB             :     acquirefuncs = (AcquireSampleRowsFunc *)
    1429 CBC         339 :         palloc(list_length(tableOIDs) * sizeof(AcquireSampleRowsFunc));
    1430             339 :     relblocks = (double *) palloc(list_length(tableOIDs) * sizeof(double));
    1431             339 :     totalblocks = 0;
    1432             339 :     nrels = 0;
    1433             339 :     has_child = false;
    1434 GIC        1625 :     foreach(lc, tableOIDs)
    1435 ECB             :     {
    1436 GIC        1286 :         Oid         childOID = lfirst_oid(lc);
    1437 ECB             :         Relation    childrel;
    1438 CBC        1286 :         AcquireSampleRowsFunc acquirefunc = NULL;
    1439 GIC        1286 :         BlockNumber relpages = 0;
    1440                 : 
    1441 ECB             :         /* We already got the needed lock */
    1442 GIC        1286 :         childrel = table_open(childOID, NoLock);
    1443                 : 
    1444 ECB             :         /* Ignore if temp table of another backend */
    1445 GIC        1286 :         if (RELATION_IS_OTHER_TEMP(childrel))
    1446                 :         {
    1447 EUB             :             /* ... but release the lock on it */
    1448 UBC           0 :             Assert(childrel != onerel);
    1449 LBC           0 :             table_close(childrel, AccessShareLock);
    1450 GIC         346 :             continue;
    1451                 :         }
    1452                 : 
    1453 ECB             :         /* Check table type (MATVIEW can't happen, but might as well allow) */
    1454 CBC        1286 :         if (childrel->rd_rel->relkind == RELKIND_RELATION ||
    1455 GIC         361 :             childrel->rd_rel->relkind == RELKIND_MATVIEW)
    1456                 :         {
    1457 ECB             :             /* Regular table, so use the regular row acquisition function */
    1458 CBC         925 :             acquirefunc = acquire_sample_rows;
    1459 GIC         925 :             relpages = RelationGetNumberOfBlocks(childrel);
    1460 ECB             :         }
    1461 GIC         361 :         else if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
    1462                 :         {
    1463                 :             /*
    1464                 :              * For a foreign table, call the FDW's hook function to see
    1465                 :              * whether it supports analysis.
    1466                 :              */
    1467 ECB             :             FdwRoutine *fdwroutine;
    1468 GIC          15 :             bool        ok = false;
    1469 ECB             : 
    1470 GIC          15 :             fdwroutine = GetFdwRoutineForRelation(childrel, false);
    1471 ECB             : 
    1472 CBC          15 :             if (fdwroutine->AnalyzeForeignTable != NULL)
    1473 GIC          15 :                 ok = fdwroutine->AnalyzeForeignTable(childrel,
    1474                 :                                                      &acquirefunc,
    1475                 :                                                      &relpages);
    1476 ECB             : 
    1477 GIC          15 :             if (!ok)
    1478                 :             {
    1479 EUB             :                 /* ignore, but release the lock on it */
    1480 UBC           0 :                 Assert(childrel != onerel);
    1481               0 :                 table_close(childrel, AccessShareLock);
    1482 UIC           0 :                 continue;
    1483                 :             }
    1484                 :         }
    1485                 :         else
    1486                 :         {
    1487                 :             /*
    1488                 :              * ignore, but release the lock on it.  don't try to unlock the
    1489                 :              * passed-in relation
    1490 ECB             :              */
    1491 CBC         346 :             Assert(childrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
    1492             346 :             if (childrel != onerel)
    1493 GIC          33 :                 table_close(childrel, AccessShareLock);
    1494 ECB             :             else
    1495 CBC         313 :                 table_close(childrel, NoLock);
    1496 GIC         346 :             continue;
    1497                 :         }
    1498                 : 
    1499 ECB             :         /* OK, we'll process this child */
    1500 CBC         940 :         has_child = true;
    1501             940 :         rels[nrels] = childrel;
    1502             940 :         acquirefuncs[nrels] = acquirefunc;
    1503             940 :         relblocks[nrels] = (double) relpages;
    1504             940 :         totalblocks += (double) relpages;
    1505 GIC         940 :         nrels++;
    1506                 :     }
    1507                 : 
    1508                 :     /*
    1509                 :      * If we don't have at least one child table to consider, fail.  If the
    1510                 :      * relation is a partitioned table, it's not counted as a child table.
    1511 ECB             :      */
    1512 GIC         339 :     if (!has_child)
    1513 EUB             :     {
    1514 UIC           0 :         ereport(elevel,
    1515                 :                 (errmsg("skipping analyze of \"%s.%s\" inheritance tree --- this inheritance tree contains no analyzable child tables",
    1516                 :                         get_namespace_name(RelationGetNamespace(onerel)),
    1517 EUB             :                         RelationGetRelationName(onerel))));
    1518 UIC           0 :         return 0;
    1519                 :     }
    1520                 : 
    1521                 :     /*
    1522                 :      * Now sample rows from each relation, proportionally to its fraction of
    1523                 :      * the total block count.  (This might be less than desirable if the child
    1524                 :      * rels have radically different free-space percentages, but it's not
    1525                 :      * clear that it's worth working harder.)
    1526 ECB             :      */
    1527 GIC         339 :     pgstat_progress_update_param(PROGRESS_ANALYZE_CHILD_TABLES_TOTAL,
    1528 ECB             :                                  nrels);
    1529 CBC         339 :     numrows = 0;
    1530 GIC        1279 :     for (i = 0; i < nrels; i++)
    1531 ECB             :     {
    1532 CBC         940 :         Relation    childrel = rels[i];
    1533             940 :         AcquireSampleRowsFunc acquirefunc = acquirefuncs[i];
    1534 GIC         940 :         double      childblocks = relblocks[i];
    1535 ECB             : 
    1536 CBC         940 :         pgstat_progress_update_param(PROGRESS_ANALYZE_CURRENT_CHILD_TABLE_RELID,
    1537 GIC         940 :                                      RelationGetRelid(childrel));
    1538 ECB             : 
    1539 GIC         940 :         if (childblocks > 0)
    1540                 :         {
    1541                 :             int         childtargrows;
    1542 ECB             : 
    1543 GIC         875 :             childtargrows = (int) rint(targrows * childblocks / totalblocks);
    1544 ECB             :             /* Make sure we don't overrun due to roundoff error */
    1545 CBC         875 :             childtargrows = Min(childtargrows, targrows - numrows);
    1546 GIC         875 :             if (childtargrows > 0)
    1547                 :             {
    1548                 :                 int         childrows;
    1549                 :                 double      trows,
    1550                 :                             tdrows;
    1551                 : 
    1552 ECB             :                 /* Fetch a random sample of the child's rows */
    1553 CBC         875 :                 childrows = (*acquirefunc) (childrel, elevel,
    1554 GIC         875 :                                             rows + numrows, childtargrows,
    1555                 :                                             &trows, &tdrows);
    1556                 : 
    1557 ECB             :                 /* We may need to convert from child's rowtype to parent's */
    1558 CBC         875 :                 if (childrows > 0 &&
    1559 GIC         875 :                     !equalTupleDescs(RelationGetDescr(childrel),
    1560                 :                                      RelationGetDescr(onerel)))
    1561                 :                 {
    1562                 :                     TupleConversionMap *map;
    1563 ECB             : 
    1564 GIC         849 :                     map = convert_tuples_by_name(RelationGetDescr(childrel),
    1565 ECB             :                                                  RelationGetDescr(onerel));
    1566 GIC         849 :                     if (map != NULL)
    1567                 :                     {
    1568                 :                         int         j;
    1569 ECB             : 
    1570 GIC       53023 :                         for (j = 0; j < childrows; j++)
    1571                 :                         {
    1572                 :                             HeapTuple   newtup;
    1573 ECB             : 
    1574 CBC       52975 :                             newtup = execute_attr_map_tuple(rows[numrows + j], map);
    1575           52975 :                             heap_freetuple(rows[numrows + j]);
    1576 GIC       52975 :                             rows[numrows + j] = newtup;
    1577 ECB             :                         }
    1578 GIC          48 :                         free_conversion_map(map);
    1579                 :                     }
    1580                 :                 }
    1581                 : 
    1582 ECB             :                 /* And add to counts */
    1583 CBC         875 :                 numrows += childrows;
    1584             875 :                 *totalrows += trows;
    1585 GIC         875 :                 *totaldeadrows += tdrows;
    1586                 :             }
    1587                 :         }
    1588                 : 
    1589                 :         /*
    1590                 :          * Note: we cannot release the child-table locks, since we may have
    1591                 :          * pointers to their TOAST tables in the sampled rows.
    1592 ECB             :          */
    1593 CBC         940 :         table_close(childrel, NoLock);
    1594             940 :         pgstat_progress_update_param(PROGRESS_ANALYZE_CHILD_TABLES_DONE,
    1595 GIC         940 :                                      i + 1);
    1596                 :     }
    1597 ECB             : 
    1598 GIC         339 :     return numrows;
    1599                 : }
    1600                 : 
    1601                 : 
    1602                 : /*
    1603                 :  *  update_attstats() -- update attribute statistics for one relation
    1604                 :  *
    1605                 :  *      Statistics are stored in several places: the pg_class row for the
    1606                 :  *      relation has stats about the whole relation, and there is a
    1607                 :  *      pg_statistic row for each (non-system) attribute that has ever
    1608                 :  *      been analyzed.  The pg_class values are updated by VACUUM, not here.
    1609                 :  *
    1610                 :  *      pg_statistic rows are just added or updated normally.  This means
    1611                 :  *      that pg_statistic will probably contain some deleted rows at the
    1612                 :  *      completion of a vacuum cycle, unless it happens to get vacuumed last.
    1613                 :  *
    1614                 :  *      To keep things simple, we punt for pg_statistic, and don't try
    1615                 :  *      to compute or store rows for pg_statistic itself in pg_statistic.
    1616                 :  *      This could possibly be made to work, but it's not worth the trouble.
    1617                 :  *      Note analyze_rel() has seen to it that we won't come here when
    1618                 :  *      vacuuming pg_statistic itself.
    1619                 :  *
    1620                 :  *      Note: there would be a race condition here if two backends could
    1621                 :  *      ANALYZE the same table concurrently.  Presently, we lock that out
    1622                 :  *      by taking a self-exclusive lock on the relation in analyze_rel().
    1623                 :  */
    1624 ECB             : static void
    1625 GIC       37909 : update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
    1626                 : {
    1627                 :     Relation    sd;
    1628 ECB             :     int         attno;
    1629 GNC       37909 :     CatalogIndexState indstate = NULL;
    1630                 : 
    1631 CBC       37909 :     if (natts <= 0)
    1632           23706 :         return;                 /* nothing to do */
    1633                 : 
    1634           14203 :     sd = table_open(StatisticRelationId, RowExclusiveLock);
    1635                 : 
    1636          153192 :     for (attno = 0; attno < natts; attno++)
    1637                 :     {
    1638          138989 :         VacAttrStats *stats = vacattrstats[attno];
    1639                 :         HeapTuple   stup,
    1640                 :                     oldtup;
    1641                 :         int         i,
    1642                 :                     k,
    1643                 :                     n;
    1644                 :         Datum       values[Natts_pg_statistic];
    1645                 :         bool        nulls[Natts_pg_statistic];
    1646                 :         bool        replaces[Natts_pg_statistic];
    1647                 : 
    1648                 :         /* Ignore attr if we weren't able to collect stats */
    1649          138989 :         if (!stats->stats_valid)
    1650               3 :             continue;
    1651                 : 
    1652                 :         /*
    1653                 :          * Construct a new pg_statistic tuple
    1654                 :          */
    1655         4447552 :         for (i = 0; i < Natts_pg_statistic; ++i)
    1656                 :         {
    1657         4308566 :             nulls[i] = false;
    1658         4308566 :             replaces[i] = true;
    1659                 :         }
    1660                 : 
    1661          138986 :         values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(relid);
    1662          138986 :         values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(stats->attr->attnum);
    1663          138986 :         values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(inh);
    1664          138986 :         values[Anum_pg_statistic_stanullfrac - 1] = Float4GetDatum(stats->stanullfrac);
    1665          138986 :         values[Anum_pg_statistic_stawidth - 1] = Int32GetDatum(stats->stawidth);
    1666          138986 :         values[Anum_pg_statistic_stadistinct - 1] = Float4GetDatum(stats->stadistinct);
    1667          138986 :         i = Anum_pg_statistic_stakind1 - 1;
    1668          833916 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    1669                 :         {
    1670          694930 :             values[i++] = Int16GetDatum(stats->stakind[k]); /* stakindN */
    1671                 :         }
    1672          138986 :         i = Anum_pg_statistic_staop1 - 1;
    1673          833916 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    1674                 :         {
    1675          694930 :             values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */
    1676                 :         }
    1677          138986 :         i = Anum_pg_statistic_stacoll1 - 1;
    1678          833916 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    1679                 :         {
    1680          694930 :             values[i++] = ObjectIdGetDatum(stats->stacoll[k]);   /* stacollN */
    1681                 :         }
    1682          138986 :         i = Anum_pg_statistic_stanumbers1 - 1;
    1683          833916 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    1684                 :         {
    1685          694930 :             int         nnum = stats->numnumbers[k];
    1686                 : 
    1687          694930 :             if (nnum > 0)
    1688                 :             {
    1689          217669 :                 Datum      *numdatums = (Datum *) palloc(nnum * sizeof(Datum));
    1690                 :                 ArrayType  *arry;
    1691                 : 
    1692         1871243 :                 for (n = 0; n < nnum; n++)
    1693         1653574 :                     numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
    1694 GNC      217669 :                 arry = construct_array_builtin(numdatums, nnum, FLOAT4OID);
    1695 GIC      217669 :                 values[i++] = PointerGetDatum(arry);    /* stanumbersN */
    1696 ECB             :             }
    1697                 :             else
    1698                 :             {
    1699 GIC      477261 :                 nulls[i] = true;
    1700 CBC      477261 :                 values[i++] = (Datum) 0;
    1701 ECB             :             }
    1702                 :         }
    1703 CBC      138986 :         i = Anum_pg_statistic_stavalues1 - 1;
    1704 GIC      833916 :         for (k = 0; k < STATISTIC_NUM_SLOTS; k++)
    1705                 :         {
    1706          694930 :             if (stats->numvalues[k] > 0)
    1707 ECB             :             {
    1708                 :                 ArrayType  *arry;
    1709                 : 
    1710 CBC      153974 :                 arry = construct_array(stats->stavalues[k],
    1711 ECB             :                                        stats->numvalues[k],
    1712                 :                                        stats->statypid[k],
    1713 CBC      153974 :                                        stats->statyplen[k],
    1714 GIC      153974 :                                        stats->statypbyval[k],
    1715          153974 :                                        stats->statypalign[k]);
    1716          153974 :                 values[i++] = PointerGetDatum(arry);    /* stavaluesN */
    1717 ECB             :             }
    1718                 :             else
    1719                 :             {
    1720 GIC      540956 :                 nulls[i] = true;
    1721          540956 :                 values[i++] = (Datum) 0;
    1722                 :             }
    1723 ECB             :         }
    1724                 : 
    1725                 :         /* Is there already a pg_statistic tuple for this attribute? */
    1726 GIC      277972 :         oldtup = SearchSysCache3(STATRELATTINH,
    1727                 :                                  ObjectIdGetDatum(relid),
    1728          138986 :                                  Int16GetDatum(stats->attr->attnum),
    1729 ECB             :                                  BoolGetDatum(inh));
    1730                 : 
    1731                 :         /* Open index information when we know we need it */
    1732 GNC      138986 :         if (indstate == NULL)
    1733           14200 :             indstate = CatalogOpenIndexes(sd);
    1734                 : 
    1735 GIC      138986 :         if (HeapTupleIsValid(oldtup))
    1736 ECB             :         {
    1737                 :             /* Yes, replace it */
    1738 GIC       11585 :             stup = heap_modify_tuple(oldtup,
    1739 ECB             :                                      RelationGetDescr(sd),
    1740                 :                                      values,
    1741                 :                                      nulls,
    1742                 :                                      replaces);
    1743 GIC       11585 :             ReleaseSysCache(oldtup);
    1744 GNC       11585 :             CatalogTupleUpdateWithInfo(sd, &stup->t_self, stup, indstate);
    1745 ECB             :         }
    1746                 :         else
    1747                 :         {
    1748                 :             /* No, insert new tuple */
    1749 GIC      127401 :             stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
    1750 GNC      127401 :             CatalogTupleInsertWithInfo(sd, stup, indstate);
    1751 ECB             :         }
    1752                 : 
    1753 GIC      138986 :         heap_freetuple(stup);
    1754 ECB             :     }
    1755                 : 
    1756 GNC       14203 :     if (indstate != NULL)
    1757           14200 :         CatalogCloseIndexes(indstate);
    1758 GIC       14203 :     table_close(sd, RowExclusiveLock);
    1759 ECB             : }
    1760                 : 
    1761                 : /*
    1762                 :  * Standard fetch function for use by compute_stats subroutines.
    1763                 :  *
    1764                 :  * This exists to provide some insulation between compute_stats routines
    1765                 :  * and the actual storage of the sample data.
    1766                 :  */
    1767                 : static Datum
    1768 GIC   119864779 : std_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
    1769                 : {
    1770       119864779 :     int         attnum = stats->tupattnum;
    1771 CBC   119864779 :     HeapTuple   tuple = stats->rows[rownum];
    1772 GIC   119864779 :     TupleDesc   tupDesc = stats->tupDesc;
    1773 ECB             : 
    1774 CBC   119864779 :     return heap_getattr(tuple, attnum, tupDesc, isNull);
    1775 ECB             : }
    1776                 : 
    1777                 : /*
    1778                 :  * Fetch function for analyzing index expressions.
    1779                 :  *
    1780                 :  * We have not bothered to construct index tuples, instead the data is
    1781                 :  * just in Datum arrays.
    1782                 :  */
    1783                 : static Datum
    1784 GIC       77215 : ind_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
    1785                 : {
    1786                 :     int         i;
    1787 ECB             : 
    1788                 :     /* exprvals and exprnulls are already offset for proper column */
    1789 GIC       77215 :     i = rownum * stats->rowstride;
    1790           77215 :     *isNull = stats->exprnulls[i];
    1791           77215 :     return stats->exprvals[i];
    1792 ECB             : }
    1793                 : 
    1794                 : 
    1795                 : /*==========================================================================
    1796                 :  *
    1797                 :  * Code below this point represents the "standard" type-specific statistics
    1798                 :  * analysis algorithms.  This code can be replaced on a per-data-type basis
    1799                 :  * by setting a nonzero value in pg_type.typanalyze.
    1800                 :  *
    1801                 :  *==========================================================================
    1802                 :  */
    1803                 : 
    1804                 : 
    1805                 : /*
    1806                 :  * To avoid consuming too much memory during analysis and/or too much space
    1807                 :  * in the resulting pg_statistic rows, we ignore varlena datums that are wider
    1808                 :  * than WIDTH_THRESHOLD (after detoasting!).  This is legitimate for MCV
    1809                 :  * and distinct-value calculations since a wide value is unlikely to be
    1810                 :  * duplicated at all, much less be a most-common value.  For the same reason,
    1811                 :  * ignoring wide values will not affect our estimates of histogram bin
    1812                 :  * boundaries very much.
    1813                 :  */
    1814                 : #define WIDTH_THRESHOLD  1024
    1815                 : 
    1816                 : #define swapInt(a,b)    do {int _tmp; _tmp=a; a=b; b=_tmp;} while(0)
    1817                 : #define swapDatum(a,b)  do {Datum _tmp; _tmp=a; a=b; b=_tmp;} while(0)
    1818                 : 
    1819                 : /*
    1820                 :  * Extra information used by the default analysis routines
    1821                 :  */
    1822                 : typedef struct
    1823                 : {
    1824                 :     int         count;          /* # of duplicates */
    1825                 :     int         first;          /* values[] index of first occurrence */
    1826                 : } ScalarMCVItem;
    1827                 : 
    1828                 : typedef struct
    1829                 : {
    1830                 :     SortSupport ssup;
    1831                 :     int        *tupnoLink;
    1832                 : } CompareScalarsContext;
    1833                 : 
    1834                 : 
    1835                 : static void compute_trivial_stats(VacAttrStatsP stats,
    1836                 :                                   AnalyzeAttrFetchFunc fetchfunc,
    1837                 :                                   int samplerows,
    1838                 :                                   double totalrows);
    1839                 : static void compute_distinct_stats(VacAttrStatsP stats,
    1840                 :                                    AnalyzeAttrFetchFunc fetchfunc,
    1841                 :                                    int samplerows,
    1842                 :                                    double totalrows);
    1843                 : static void compute_scalar_stats(VacAttrStatsP stats,
    1844                 :                                  AnalyzeAttrFetchFunc fetchfunc,
    1845                 :                                  int samplerows,
    1846                 :                                  double totalrows);
    1847                 : static int  compare_scalars(const void *a, const void *b, void *arg);
    1848                 : static int  compare_mcvs(const void *a, const void *b, void *arg);
    1849                 : static int  analyze_mcv_list(int *mcv_counts,
    1850                 :                              int num_mcv,
    1851                 :                              double stadistinct,
    1852                 :                              double stanullfrac,
    1853                 :                              int samplerows,
    1854                 :                              double totalrows);
    1855                 : 
    1856                 : 
    1857                 : /*
    1858                 :  * std_typanalyze -- the default type-specific typanalyze function
    1859                 :  */
    1860                 : bool
    1861 GIC      197628 : std_typanalyze(VacAttrStats *stats)
    1862                 : {
    1863          197628 :     Form_pg_attribute attr = stats->attr;
    1864 ECB             :     Oid         ltopr;
    1865                 :     Oid         eqopr;
    1866                 :     StdAnalyzeData *mystats;
    1867                 : 
    1868                 :     /* If the attstattarget column is negative, use the default value */
    1869                 :     /* NB: it is okay to scribble on stats->attr since it's a copy */
    1870 GIC      197628 :     if (attr->attstattarget < 0)
    1871          197337 :         attr->attstattarget = default_statistics_target;
    1872                 : 
    1873 ECB             :     /* Look for default "<" and "=" operators for column's type */
    1874 CBC      197628 :     get_sort_group_operators(stats->attrtypid,
    1875                 :                              false, false, false,
    1876                 :                              &ltopr, &eqopr, NULL,
    1877 ECB             :                              NULL);
    1878                 : 
    1879                 :     /* Save the operator info for compute_stats routines */
    1880 GIC      197628 :     mystats = (StdAnalyzeData *) palloc(sizeof(StdAnalyzeData));
    1881          197628 :     mystats->eqopr = eqopr;
    1882          197628 :     mystats->eqfunc = OidIsValid(eqopr) ? get_opcode(eqopr) : InvalidOid;
    1883 CBC      197628 :     mystats->ltopr = ltopr;
    1884          197628 :     stats->extra_data = mystats;
    1885 ECB             : 
    1886                 :     /*
    1887                 :      * Determine which standard statistics algorithm to use
    1888                 :      */
    1889 GIC      197628 :     if (OidIsValid(eqopr) && OidIsValid(ltopr))
    1890                 :     {
    1891                 :         /* Seems to be a scalar datatype */
    1892 CBC      191043 :         stats->compute_stats = compute_scalar_stats;
    1893                 :         /*--------------------
    1894                 :          * The following choice of minrows is based on the paper
    1895 ECB             :          * "Random sampling for histogram construction: how much is enough?"
    1896                 :          * by Surajit Chaudhuri, Rajeev Motwani and Vivek Narasayya, in
    1897                 :          * Proceedings of ACM SIGMOD International Conference on Management
    1898                 :          * of Data, 1998, Pages 436-447.  Their Corollary 1 to Theorem 5
    1899                 :          * says that for table size n, histogram size k, maximum relative
    1900                 :          * error in bin size f, and error probability gamma, the minimum
    1901                 :          * random sample size is
    1902                 :          *      r = 4 * k * ln(2*n/gamma) / f^2
    1903                 :          * Taking f = 0.5, gamma = 0.01, n = 10^6 rows, we obtain
    1904                 :          *      r = 305.82 * k
    1905                 :          * Note that because of the log function, the dependence on n is
    1906                 :          * quite weak; even at n = 10^12, a 300*k sample gives <= 0.66
    1907                 :          * bin size error with probability 0.99.  So there's no real need to
    1908                 :          * scale for n, which is a good thing because we don't necessarily
    1909                 :          * know it at this point.
    1910                 :          *--------------------
    1911                 :          */
    1912 GIC      191043 :         stats->minrows = 300 * attr->attstattarget;
    1913                 :     }
    1914            6585 :     else if (OidIsValid(eqopr))
    1915 ECB             :     {
    1916                 :         /* We can still recognize distinct values */
    1917 CBC        5858 :         stats->compute_stats = compute_distinct_stats;
    1918                 :         /* Might as well use the same minrows as above */
    1919 GIC        5858 :         stats->minrows = 300 * attr->attstattarget;
    1920 ECB             :     }
    1921                 :     else
    1922                 :     {
    1923                 :         /* Can't do much but the trivial stuff */
    1924 GIC         727 :         stats->compute_stats = compute_trivial_stats;
    1925                 :         /* Might as well use the same minrows as above */
    1926             727 :         stats->minrows = 300 * attr->attstattarget;
    1927 ECB             :     }
    1928                 : 
    1929 CBC      197628 :     return true;
    1930                 : }
    1931                 : 
    1932 ECB             : 
    1933                 : /*
    1934                 :  *  compute_trivial_stats() -- compute very basic column statistics
    1935                 :  *
    1936                 :  *  We use this when we cannot find a hash "=" operator for the datatype.
    1937                 :  *
    1938                 :  *  We determine the fraction of non-null rows and the average datum width.
    1939                 :  */
    1940                 : static void
    1941 GIC         403 : compute_trivial_stats(VacAttrStatsP stats,
    1942                 :                       AnalyzeAttrFetchFunc fetchfunc,
    1943                 :                       int samplerows,
    1944 ECB             :                       double totalrows)
    1945                 : {
    1946                 :     int         i;
    1947 GIC         403 :     int         null_cnt = 0;
    1948             403 :     int         nonnull_cnt = 0;
    1949             403 :     double      total_width = 0;
    1950 CBC         806 :     bool        is_varlena = (!stats->attrtype->typbyval &&
    1951             403 :                               stats->attrtype->typlen == -1);
    1952             806 :     bool        is_varwidth = (!stats->attrtype->typbyval &&
    1953             403 :                                stats->attrtype->typlen < 0);
    1954 ECB             : 
    1955 CBC     1285815 :     for (i = 0; i < samplerows; i++)
    1956 ECB             :     {
    1957                 :         Datum       value;
    1958                 :         bool        isnull;
    1959                 : 
    1960 GIC     1285412 :         vacuum_delay_point();
    1961                 : 
    1962         1285412 :         value = fetchfunc(stats, i, &isnull);
    1963 ECB             : 
    1964                 :         /* Check for null/nonnull */
    1965 CBC     1285412 :         if (isnull)
    1966                 :         {
    1967 GIC     1022060 :             null_cnt++;
    1968 CBC     1022060 :             continue;
    1969                 :         }
    1970          263352 :         nonnull_cnt++;
    1971 ECB             : 
    1972                 :         /*
    1973                 :          * If it's a variable-width field, add up widths for average width
    1974                 :          * calculation.  Note that if the value is toasted, we use the toasted
    1975                 :          * width.  We don't bother with this calculation if it's a fixed-width
    1976                 :          * type.
    1977                 :          */
    1978 GIC      263352 :         if (is_varlena)
    1979                 :         {
    1980           51852 :             total_width += VARSIZE_ANY(DatumGetPointer(value));
    1981 ECB             :         }
    1982 GIC      211500 :         else if (is_varwidth)
    1983 ECB             :         {
    1984                 :             /* must be cstring */
    1985 LBC           0 :             total_width += strlen(DatumGetCString(value)) + 1;
    1986                 :         }
    1987                 :     }
    1988 EUB             : 
    1989                 :     /* We can only compute average width if we found some non-null values. */
    1990 GIC         403 :     if (nonnull_cnt > 0)
    1991                 :     {
    1992              75 :         stats->stats_valid = true;
    1993 ECB             :         /* Do the simple null-frac and width stats */
    1994 GIC          75 :         stats->stanullfrac = (double) null_cnt / (double) samplerows;
    1995 CBC          75 :         if (is_varwidth)
    1996 GIC          28 :             stats->stawidth = total_width / (double) nonnull_cnt;
    1997 ECB             :         else
    1998 CBC          47 :             stats->stawidth = stats->attrtype->typlen;
    1999              75 :         stats->stadistinct = 0.0;    /* "unknown" */
    2000                 :     }
    2001             328 :     else if (null_cnt > 0)
    2002 ECB             :     {
    2003                 :         /* We found only nulls; assume the column is entirely null */
    2004 CBC         328 :         stats->stats_valid = true;
    2005 GIC         328 :         stats->stanullfrac = 1.0;
    2006             328 :         if (is_varwidth)
    2007 CBC         328 :             stats->stawidth = 0; /* "unknown" */
    2008 ECB             :         else
    2009 LBC           0 :             stats->stawidth = stats->attrtype->typlen;
    2010 CBC         328 :         stats->stadistinct = 0.0;    /* "unknown" */
    2011                 :     }
    2012 GBC         403 : }
    2013 ECB             : 
    2014                 : 
    2015                 : /*
    2016                 :  *  compute_distinct_stats() -- compute column statistics including ndistinct
    2017                 :  *
    2018                 :  *  We use this when we can find only an "=" operator for the datatype.
    2019                 :  *
    2020                 :  *  We determine the fraction of non-null rows, the average width, the
    2021                 :  *  most common values, and the (estimated) number of distinct values.
    2022                 :  *
    2023                 :  *  The most common values are determined by brute force: we keep a list
    2024                 :  *  of previously seen values, ordered by number of times seen, as we scan
    2025                 :  *  the samples.  A newly seen value is inserted just after the last
    2026                 :  *  multiply-seen value, causing the bottommost (oldest) singly-seen value
    2027                 :  *  to drop off the list.  The accuracy of this method, and also its cost,
    2028                 :  *  depend mainly on the length of the list we are willing to keep.
    2029                 :  */
    2030                 : static void
    2031 GIC        4238 : compute_distinct_stats(VacAttrStatsP stats,
    2032                 :                        AnalyzeAttrFetchFunc fetchfunc,
    2033                 :                        int samplerows,
    2034 ECB             :                        double totalrows)
    2035                 : {
    2036                 :     int         i;
    2037 GIC        4238 :     int         null_cnt = 0;
    2038            4238 :     int         nonnull_cnt = 0;
    2039            4238 :     int         toowide_cnt = 0;
    2040 CBC        4238 :     double      total_width = 0;
    2041            7170 :     bool        is_varlena = (!stats->attrtype->typbyval &&
    2042            2932 :                               stats->attrtype->typlen == -1);
    2043            7170 :     bool        is_varwidth = (!stats->attrtype->typbyval &&
    2044            2932 :                                stats->attrtype->typlen < 0);
    2045 ECB             :     FmgrInfo    f_cmpeq;
    2046                 :     typedef struct
    2047                 :     {
    2048                 :         Datum       value;
    2049                 :         int         count;
    2050                 :     } TrackItem;
    2051                 :     TrackItem  *track;
    2052                 :     int         track_cnt,
    2053                 :                 track_max;
    2054 GIC        4238 :     int         num_mcv = stats->attr->attstattarget;
    2055            4238 :     StdAnalyzeData *mystats = (StdAnalyzeData *) stats->extra_data;
    2056                 : 
    2057 ECB             :     /*
    2058                 :      * We track up to 2*n values for an n-element MCV list; but at least 10
    2059                 :      */
    2060 GIC        4238 :     track_max = 2 * num_mcv;
    2061            4238 :     if (track_max < 10)
    2062              39 :         track_max = 10;
    2063 CBC        4238 :     track = (TrackItem *) palloc(track_max * sizeof(TrackItem));
    2064            4238 :     track_cnt = 0;
    2065 ECB             : 
    2066 CBC        4238 :     fmgr_info(mystats->eqfunc, &f_cmpeq);
    2067 ECB             : 
    2068 GIC     2778529 :     for (i = 0; i < samplerows; i++)
    2069 ECB             :     {
    2070                 :         Datum       value;
    2071                 :         bool        isnull;
    2072                 :         bool        match;
    2073                 :         int         firstcount1,
    2074                 :                     j;
    2075                 : 
    2076 GIC     2774291 :         vacuum_delay_point();
    2077                 : 
    2078         2774291 :         value = fetchfunc(stats, i, &isnull);
    2079 ECB             : 
    2080                 :         /* Check for null/nonnull */
    2081 CBC     2774291 :         if (isnull)
    2082                 :         {
    2083 GIC     2336018 :             null_cnt++;
    2084 CBC     2336018 :             continue;
    2085                 :         }
    2086          438273 :         nonnull_cnt++;
    2087 ECB             : 
    2088                 :         /*
    2089                 :          * If it's a variable-width field, add up widths for average width
    2090                 :          * calculation.  Note that if the value is toasted, we use the toasted
    2091                 :          * width.  We don't bother with this calculation if it's a fixed-width
    2092                 :          * type.
    2093                 :          */
    2094 GIC      438273 :         if (is_varlena)
    2095                 :         {
    2096          163933 :             total_width += VARSIZE_ANY(DatumGetPointer(value));
    2097 ECB             : 
    2098                 :             /*
    2099                 :              * If the value is toasted, we want to detoast it just once to
    2100                 :              * avoid repeated detoastings and resultant excess memory usage
    2101                 :              * during the comparisons.  Also, check to see if the value is
    2102                 :              * excessively wide, and if so don't detoast at all --- just
    2103                 :              * ignore the value.
    2104                 :              */
    2105 GIC      163933 :             if (toast_raw_datum_size(value) > WIDTH_THRESHOLD)
    2106                 :             {
    2107 UIC           0 :                 toowide_cnt++;
    2108 LBC           0 :                 continue;
    2109                 :             }
    2110 GBC      163933 :             value = PointerGetDatum(PG_DETOAST_DATUM(value));
    2111 EUB             :         }
    2112 GIC      274340 :         else if (is_varwidth)
    2113 ECB             :         {
    2114                 :             /* must be cstring */
    2115 LBC           0 :             total_width += strlen(DatumGetCString(value)) + 1;
    2116                 :         }
    2117                 : 
    2118 EUB             :         /*
    2119                 :          * See if the value matches anything we're already tracking.
    2120                 :          */
    2121 GIC      438273 :         match = false;
    2122          438273 :         firstcount1 = track_cnt;
    2123          617202 :         for (j = 0; j < track_cnt; j++)
    2124 ECB             :         {
    2125 CBC      607051 :             if (DatumGetBool(FunctionCall2Coll(&f_cmpeq,
    2126 ECB             :                                                stats->attrcollid,
    2127 GIC      607051 :                                                value, track[j].value)))
    2128 ECB             :             {
    2129 GIC      428122 :                 match = true;
    2130 CBC      428122 :                 break;
    2131                 :             }
    2132          178929 :             if (j < firstcount1 && track[j].count == 1)
    2133            4979 :                 firstcount1 = j;
    2134                 :         }
    2135 ECB             : 
    2136 CBC      438273 :         if (match)
    2137                 :         {
    2138                 :             /* Found a match */
    2139          428122 :             track[j].count++;
    2140                 :             /* This value may now need to "bubble up" in the track list */
    2141 GIC      432965 :             while (j > 0 && track[j].count > track[j - 1].count)
    2142 ECB             :             {
    2143 GIC        4843 :                 swapDatum(track[j].value, track[j - 1].value);
    2144 CBC        4843 :                 swapInt(track[j].count, track[j - 1].count);
    2145 GIC        4843 :                 j--;
    2146 ECB             :             }
    2147                 :         }
    2148                 :         else
    2149                 :         {
    2150                 :             /* No match.  Insert at head of count-1 list */
    2151 GIC       10151 :             if (track_cnt < track_max)
    2152           10151 :                 track_cnt++;
    2153           31864 :             for (j = track_cnt - 1; j > firstcount1; j--)
    2154 ECB             :             {
    2155 CBC       21713 :                 track[j].value = track[j - 1].value;
    2156           21713 :                 track[j].count = track[j - 1].count;
    2157                 :             }
    2158           10151 :             if (firstcount1 < track_cnt)
    2159 ECB             :             {
    2160 GIC       10151 :                 track[firstcount1].value = value;
    2161 CBC       10151 :                 track[firstcount1].count = 1;
    2162                 :             }
    2163 ECB             :         }
    2164                 :     }
    2165                 : 
    2166                 :     /* We can only compute real stats if we found some non-null values. */
    2167 GIC        4238 :     if (nonnull_cnt > 0)
    2168                 :     {
    2169                 :         int         nmultiple,
    2170 ECB             :                     summultiple;
    2171                 : 
    2172 GIC        2959 :         stats->stats_valid = true;
    2173                 :         /* Do the simple null-frac and width stats */
    2174            2959 :         stats->stanullfrac = (double) null_cnt / (double) samplerows;
    2175 CBC        2959 :         if (is_varwidth)
    2176 GIC        1653 :             stats->stawidth = total_width / (double) nonnull_cnt;
    2177 ECB             :         else
    2178 CBC        1306 :             stats->stawidth = stats->attrtype->typlen;
    2179 ECB             : 
    2180                 :         /* Count the number of values we found multiple times */
    2181 CBC        2959 :         summultiple = 0;
    2182 GIC       10519 :         for (nmultiple = 0; nmultiple < track_cnt; nmultiple++)
    2183                 :         {
    2184 CBC        9181 :             if (track[nmultiple].count == 1)
    2185            1621 :                 break;
    2186 GIC        7560 :             summultiple += track[nmultiple].count;
    2187 ECB             :         }
    2188                 : 
    2189 CBC        2959 :         if (nmultiple == 0)
    2190                 :         {
    2191                 :             /*
    2192 ECB             :              * If we found no repeated non-null values, assume it's a unique
    2193                 :              * column; but be sure to discount for any nulls we found.
    2194                 :              */
    2195 GIC         607 :             stats->stadistinct = -1.0 * (1.0 - stats->stanullfrac);
    2196                 :         }
    2197            2352 :         else if (track_cnt < track_max && toowide_cnt == 0 &&
    2198 ECB             :                  nmultiple == track_cnt)
    2199                 :         {
    2200                 :             /*
    2201                 :              * Our track list includes every value in the sample, and every
    2202                 :              * value appeared more than once.  Assume the column has just
    2203                 :              * these values.  (This case is meant to address columns with
    2204                 :              * small, fixed sets of possible values, such as boolean or enum
    2205                 :              * columns.  If there are any values that appear just once in the
    2206                 :              * sample, including too-wide values, we should assume that that's
    2207                 :              * not what we're dealing with.)
    2208                 :              */
    2209 GIC        1338 :             stats->stadistinct = track_cnt;
    2210                 :         }
    2211                 :         else
    2212 ECB             :         {
    2213                 :             /*----------
    2214                 :              * Estimate the number of distinct values using the estimator
    2215                 :              * proposed by Haas and Stokes in IBM Research Report RJ 10025:
    2216                 :              *      n*d / (n - f1 + f1*n/N)
    2217                 :              * where f1 is the number of distinct values that occurred
    2218                 :              * exactly once in our sample of n rows (from a total of N),
    2219                 :              * and d is the total number of distinct values in the sample.
    2220                 :              * This is their Duj1 estimator; the other estimators they
    2221                 :              * recommend are considerably more complex, and are numerically
    2222                 :              * very unstable when n is much smaller than N.
    2223                 :              *
    2224                 :              * In this calculation, we consider only non-nulls.  We used to
    2225                 :              * include rows with null values in the n and N counts, but that
    2226                 :              * leads to inaccurate answers in columns with many nulls, and
    2227                 :              * it's intuitively bogus anyway considering the desired result is
    2228                 :              * the number of distinct non-null values.
    2229                 :              *
    2230                 :              * We assume (not very reliably!) that all the multiply-occurring
    2231                 :              * values are reflected in the final track[] list, and the other
    2232                 :              * nonnull values all appeared but once.  (XXX this usually
    2233                 :              * results in a drastic overestimate of ndistinct.  Can we do
    2234                 :              * any better?)
    2235                 :              *----------
    2236                 :              */
    2237 GIC        1014 :             int         f1 = nonnull_cnt - summultiple;
    2238            1014 :             int         d = f1 + nmultiple;
    2239            1014 :             double      n = samplerows - null_cnt;
    2240 CBC        1014 :             double      N = totalrows * (1.0 - stats->stanullfrac);
    2241 ECB             :             double      stadistinct;
    2242                 : 
    2243                 :             /* N == 0 shouldn't happen, but just in case ... */
    2244 GIC        1014 :             if (N > 0)
    2245            1014 :                 stadistinct = (n * d) / ((n - f1) + f1 * n / N);
    2246                 :             else
    2247 LBC           0 :                 stadistinct = 0;
    2248 ECB             : 
    2249                 :             /* Clamp to sane range in case of roundoff error */
    2250 GBC        1014 :             if (stadistinct < d)
    2251 GIC         329 :                 stadistinct = d;
    2252            1014 :             if (stadistinct > N)
    2253 LBC           0 :                 stadistinct = N;
    2254 ECB             :             /* And round to integer */
    2255 CBC        1014 :             stats->stadistinct = floor(stadistinct + 0.5);
    2256 EUB             :         }
    2257                 : 
    2258 ECB             :         /*
    2259                 :          * If we estimated the number of distinct values at more than 10% of
    2260                 :          * the total row count (a very arbitrary limit), then assume that
    2261                 :          * stadistinct should scale with the row count rather than be a fixed
    2262                 :          * value.
    2263                 :          */
    2264 GIC        2959 :         if (stats->stadistinct > 0.1 * totalrows)
    2265             389 :             stats->stadistinct = -(stats->stadistinct / totalrows);
    2266                 : 
    2267 ECB             :         /*
    2268                 :          * Decide how many values are worth storing as most-common values. If
    2269                 :          * we are able to generate a complete MCV list (all the values in the
    2270                 :          * sample will fit, and we think these are all the ones in the table),
    2271                 :          * then do so.  Otherwise, store only those values that are
    2272                 :          * significantly more common than the values not in the list.
    2273                 :          *
    2274                 :          * Note: the first of these cases is meant to address columns with
    2275                 :          * small, fixed sets of possible values, such as boolean or enum
    2276                 :          * columns.  If we can *completely* represent the column population by
    2277                 :          * an MCV list that will fit into the stats target, then we should do
    2278                 :          * so and thus provide the planner with complete information.  But if
    2279                 :          * the MCV list is not complete, it's generally worth being more
    2280                 :          * selective, and not just filling it all the way up to the stats
    2281                 :          * target.
    2282                 :          */
    2283 GIC        2959 :         if (track_cnt < track_max && toowide_cnt == 0 &&
    2284            2956 :             stats->stadistinct > 0 &&
    2285                 :             track_cnt <= num_mcv)
    2286 ECB             :         {
    2287                 :             /* Track list includes all values seen, and all will fit */
    2288 GIC        1948 :             num_mcv = track_cnt;
    2289                 :         }
    2290                 :         else
    2291 ECB             :         {
    2292                 :             int        *mcv_counts;
    2293                 : 
    2294                 :             /* Incomplete list; decide how many values are worth keeping */
    2295 GIC        1011 :             if (num_mcv > track_cnt)
    2296             981 :                 num_mcv = track_cnt;
    2297                 : 
    2298 CBC        1011 :             if (num_mcv > 0)
    2299 ECB             :             {
    2300 GIC        1011 :                 mcv_counts = (int *) palloc(num_mcv * sizeof(int));
    2301 CBC        2545 :                 for (i = 0; i < num_mcv; i++)
    2302 GIC        1534 :                     mcv_counts[i] = track[i].count;
    2303 ECB             : 
    2304 CBC        1011 :                 num_mcv = analyze_mcv_list(mcv_counts, num_mcv,
    2305            1011 :                                            stats->stadistinct,
    2306 GIC        1011 :                                            stats->stanullfrac,
    2307 ECB             :                                            samplerows, totalrows);
    2308                 :             }
    2309                 :         }
    2310                 : 
    2311                 :         /* Generate MCV slot entry */
    2312 GIC        2959 :         if (num_mcv > 0)
    2313                 :         {
    2314                 :             MemoryContext old_context;
    2315 ECB             :             Datum      *mcv_values;
    2316                 :             float4     *mcv_freqs;
    2317                 : 
    2318                 :             /* Must copy the target values into anl_context */
    2319 GIC        2955 :             old_context = MemoryContextSwitchTo(stats->anl_context);
    2320            2955 :             mcv_values = (Datum *) palloc(num_mcv * sizeof(Datum));
    2321            2955 :             mcv_freqs = (float4 *) palloc(num_mcv * sizeof(float4));
    2322 CBC       12976 :             for (i = 0; i < num_mcv; i++)
    2323 ECB             :             {
    2324 CBC       20042 :                 mcv_values[i] = datumCopy(track[i].value,
    2325           10021 :                                           stats->attrtype->typbyval,
    2326 GIC       10021 :                                           stats->attrtype->typlen);
    2327 CBC       10021 :                 mcv_freqs[i] = (double) track[i].count / (double) samplerows;
    2328 ECB             :             }
    2329 CBC        2955 :             MemoryContextSwitchTo(old_context);
    2330 ECB             : 
    2331 GIC        2955 :             stats->stakind[0] = STATISTIC_KIND_MCV;
    2332 CBC        2955 :             stats->staop[0] = mystats->eqopr;
    2333 GIC        2955 :             stats->stacoll[0] = stats->attrcollid;
    2334 CBC        2955 :             stats->stanumbers[0] = mcv_freqs;
    2335            2955 :             stats->numnumbers[0] = num_mcv;
    2336            2955 :             stats->stavalues[0] = mcv_values;
    2337            2955 :             stats->numvalues[0] = num_mcv;
    2338 ECB             : 
    2339                 :             /*
    2340                 :              * Accept the defaults for stats->statypid and others. They have
    2341                 :              * been set before we were called (see vacuum.h)
    2342                 :              */
    2343                 :         }
    2344                 :     }
    2345 GIC        1279 :     else if (null_cnt > 0)
    2346                 :     {
    2347                 :         /* We found only nulls; assume the column is entirely null */
    2348 CBC        1279 :         stats->stats_valid = true;
    2349 GIC        1279 :         stats->stanullfrac = 1.0;
    2350            1279 :         if (is_varwidth)
    2351 CBC        1279 :             stats->stawidth = 0; /* "unknown" */
    2352 ECB             :         else
    2353 LBC           0 :             stats->stawidth = stats->attrtype->typlen;
    2354 CBC        1279 :         stats->stadistinct = 0.0;    /* "unknown" */
    2355                 :     }
    2356 EUB             : 
    2357 ECB             :     /* We don't need to bother cleaning up any of our temporary palloc's */
    2358 GIC        4238 : }
    2359                 : 
    2360                 : 
    2361 ECB             : /*
    2362                 :  *  compute_scalar_stats() -- compute column statistics
    2363                 :  *
    2364                 :  *  We use this when we can find "=" and "<" operators for the datatype.
    2365                 :  *
    2366                 :  *  We determine the fraction of non-null rows, the average width, the
    2367                 :  *  most common values, the (estimated) number of distinct values, the
    2368                 :  *  distribution histogram, and the correlation of physical to logical order.
    2369                 :  *
    2370                 :  *  The desired stats can be determined fairly easily after sorting the
    2371                 :  *  data values into order.
    2372                 :  */
    2373                 : static void
    2374 GIC      134473 : compute_scalar_stats(VacAttrStatsP stats,
    2375                 :                      AnalyzeAttrFetchFunc fetchfunc,
    2376                 :                      int samplerows,
    2377 ECB             :                      double totalrows)
    2378                 : {
    2379                 :     int         i;
    2380 GIC      134473 :     int         null_cnt = 0;
    2381          134473 :     int         nonnull_cnt = 0;
    2382          134473 :     int         toowide_cnt = 0;
    2383 CBC      134473 :     double      total_width = 0;
    2384          166233 :     bool        is_varlena = (!stats->attrtype->typbyval &&
    2385           31760 :                               stats->attrtype->typlen == -1);
    2386          166233 :     bool        is_varwidth = (!stats->attrtype->typbyval &&
    2387           31760 :                                stats->attrtype->typlen < 0);
    2388 ECB             :     double      corr_xysum;
    2389                 :     SortSupportData ssup;
    2390                 :     ScalarItem *values;
    2391 GIC      134473 :     int         values_cnt = 0;
    2392                 :     int        *tupnoLink;
    2393                 :     ScalarMCVItem *track;
    2394 CBC      134473 :     int         track_cnt = 0;
    2395 GIC      134473 :     int         num_mcv = stats->attr->attstattarget;
    2396          134473 :     int         num_bins = stats->attr->attstattarget;
    2397 CBC      134473 :     StdAnalyzeData *mystats = (StdAnalyzeData *) stats->extra_data;
    2398 ECB             : 
    2399 CBC      134473 :     values = (ScalarItem *) palloc(samplerows * sizeof(ScalarItem));
    2400          134473 :     tupnoLink = (int *) palloc(samplerows * sizeof(int));
    2401 GIC      134473 :     track = (ScalarMCVItem *) palloc(num_mcv * sizeof(ScalarMCVItem));
    2402 ECB             : 
    2403 CBC      134473 :     memset(&ssup, 0, sizeof(ssup));
    2404          134473 :     ssup.ssup_cxt = CurrentMemoryContext;
    2405 GIC      134473 :     ssup.ssup_collation = stats->attrcollid;
    2406 CBC      134473 :     ssup.ssup_nulls_first = false;
    2407 ECB             : 
    2408                 :     /*
    2409                 :      * For now, don't perform abbreviated key conversion, because full values
    2410                 :      * are required for MCV slot generation.  Supporting that optimization
    2411                 :      * would necessitate teaching compare_scalars() to call a tie-breaker.
    2412                 :      */
    2413 GIC      134473 :     ssup.abbreviate = false;
    2414                 : 
    2415          134473 :     PrepareSortSupportFromOrderingOp(mystats->ltopr, &ssup);
    2416 ECB             : 
    2417                 :     /* Initial scan to find sortable values */
    2418 CBC   108059039 :     for (i = 0; i < samplerows; i++)
    2419                 :     {
    2420                 :         Datum       value;
    2421 ECB             :         bool        isnull;
    2422                 : 
    2423 GIC   107924566 :         vacuum_delay_point();
    2424                 : 
    2425       107924566 :         value = fetchfunc(stats, i, &isnull);
    2426 ECB             : 
    2427                 :         /* Check for null/nonnull */
    2428 CBC   107924566 :         if (isnull)
    2429                 :         {
    2430 GIC    14078242 :             null_cnt++;
    2431 CBC    14143383 :             continue;
    2432                 :         }
    2433        93846324 :         nonnull_cnt++;
    2434 ECB             : 
    2435                 :         /*
    2436                 :          * If it's a variable-width field, add up widths for average width
    2437                 :          * calculation.  Note that if the value is toasted, we use the toasted
    2438                 :          * width.  We don't bother with this calculation if it's a fixed-width
    2439                 :          * type.
    2440                 :          */
    2441 GIC    93846324 :         if (is_varlena)
    2442                 :         {
    2443         8617183 :             total_width += VARSIZE_ANY(DatumGetPointer(value));
    2444 ECB             : 
    2445                 :             /*
    2446                 :              * If the value is toasted, we want to detoast it just once to
    2447                 :              * avoid repeated detoastings and resultant excess memory usage
    2448                 :              * during the comparisons.  Also, check to see if the value is
    2449                 :              * excessively wide, and if so don't detoast at all --- just
    2450                 :              * ignore the value.
    2451                 :              */
    2452 GIC     8617183 :             if (toast_raw_datum_size(value) > WIDTH_THRESHOLD)
    2453                 :             {
    2454           65141 :                 toowide_cnt++;
    2455 CBC       65141 :                 continue;
    2456                 :             }
    2457         8552042 :             value = PointerGetDatum(PG_DETOAST_DATUM(value));
    2458 ECB             :         }
    2459 GIC    85229141 :         else if (is_varwidth)
    2460 ECB             :         {
    2461                 :             /* must be cstring */
    2462 LBC           0 :             total_width += strlen(DatumGetCString(value)) + 1;
    2463                 :         }
    2464                 : 
    2465 EUB             :         /* Add it to the list to be sorted */
    2466 GIC    93781183 :         values[values_cnt].value = value;
    2467        93781183 :         values[values_cnt].tupno = values_cnt;
    2468        93781183 :         tupnoLink[values_cnt] = values_cnt;
    2469 CBC    93781183 :         values_cnt++;
    2470 ECB             :     }
    2471                 : 
    2472                 :     /* We can only compute real stats if we found some sortable values. */
    2473 GIC      134473 :     if (values_cnt > 0)
    2474                 :     {
    2475                 :         int         ndistinct,  /* # distinct values in sample */
    2476 ECB             :                     nmultiple,  /* # that appear multiple times */
    2477                 :                     num_hist,
    2478                 :                     dups_cnt;
    2479 GIC      125939 :         int         slot_idx = 0;
    2480                 :         CompareScalarsContext cxt;
    2481                 : 
    2482 ECB             :         /* Sort the collected values */
    2483 GIC      125939 :         cxt.ssup = &ssup;
    2484          125939 :         cxt.tupnoLink = tupnoLink;
    2485 GNC      125939 :         qsort_interruptible(values, values_cnt, sizeof(ScalarItem),
    2486                 :                             compare_scalars, &cxt);
    2487 ECB             : 
    2488                 :         /*
    2489                 :          * Now scan the values in order, find the most common ones, and also
    2490                 :          * accumulate ordering-correlation statistics.
    2491                 :          *
    2492                 :          * To determine which are most common, we first have to count the
    2493                 :          * number of duplicates of each value.  The duplicates are adjacent in
    2494                 :          * the sorted list, so a brute-force approach is to compare successive
    2495                 :          * datum values until we find two that are not equal. However, that
    2496                 :          * requires N-1 invocations of the datum comparison routine, which are
    2497                 :          * completely redundant with work that was done during the sort.  (The
    2498                 :          * sort algorithm must at some point have compared each pair of items
    2499                 :          * that are adjacent in the sorted order; otherwise it could not know
    2500                 :          * that it's ordered the pair correctly.) We exploit this by having
    2501                 :          * compare_scalars remember the highest tupno index that each
    2502                 :          * ScalarItem has been found equal to.  At the end of the sort, a
    2503                 :          * ScalarItem's tupnoLink will still point to itself if and only if it
    2504                 :          * is the last item of its group of duplicates (since the group will
    2505                 :          * be ordered by tupno).
    2506                 :          */
    2507 GIC      125939 :         corr_xysum = 0;
    2508          125939 :         ndistinct = 0;
    2509          125939 :         nmultiple = 0;
    2510 CBC      125939 :         dups_cnt = 0;
    2511        93907122 :         for (i = 0; i < values_cnt; i++)
    2512 ECB             :         {
    2513 CBC    93781183 :             int         tupno = values[i].tupno;
    2514 ECB             : 
    2515 GIC    93781183 :             corr_xysum += ((double) i) * ((double) tupno);
    2516 CBC    93781183 :             dups_cnt++;
    2517 GIC    93781183 :             if (tupnoLink[tupno] == tupno)
    2518 ECB             :             {
    2519                 :                 /* Reached end of duplicates of this value */
    2520 CBC    16222141 :                 ndistinct++;
    2521 GIC    16222141 :                 if (dups_cnt > 1)
    2522                 :                 {
    2523 CBC     1810101 :                     nmultiple++;
    2524         1810101 :                     if (track_cnt < num_mcv ||
    2525 GIC      626580 :                         dups_cnt > track[track_cnt - 1].count)
    2526 ECB             :                     {
    2527                 :                         /*
    2528                 :                          * Found a new item for the mcv list; find its
    2529                 :                          * position, bubbling down old items if needed. Loop
    2530                 :                          * invariant is that j points at an empty/ replaceable
    2531                 :                          * slot.
    2532                 :                          */
    2533                 :                         int         j;
    2534                 : 
    2535 GIC     1365545 :                         if (track_cnt < num_mcv)
    2536         1183521 :                             track_cnt++;
    2537        19498446 :                         for (j = track_cnt - 1; j > 0; j--)
    2538 ECB             :                         {
    2539 CBC    19350259 :                             if (dups_cnt <= track[j - 1].count)
    2540         1217358 :                                 break;
    2541 GIC    18132901 :                             track[j].count = track[j - 1].count;
    2542 CBC    18132901 :                             track[j].first = track[j - 1].first;
    2543 ECB             :                         }
    2544 CBC     1365545 :                         track[j].count = dups_cnt;
    2545         1365545 :                         track[j].first = i + 1 - dups_cnt;
    2546                 :                     }
    2547 ECB             :                 }
    2548 CBC    16222141 :                 dups_cnt = 0;
    2549                 :             }
    2550                 :         }
    2551 ECB             : 
    2552 GIC      125939 :         stats->stats_valid = true;
    2553                 :         /* Do the simple null-frac and width stats */
    2554          125939 :         stats->stanullfrac = (double) null_cnt / (double) samplerows;
    2555 CBC      125939 :         if (is_varwidth)
    2556 GIC       16382 :             stats->stawidth = total_width / (double) nonnull_cnt;
    2557 ECB             :         else
    2558 CBC      109557 :             stats->stawidth = stats->attrtype->typlen;
    2559 ECB             : 
    2560 GIC      125939 :         if (nmultiple == 0)
    2561 ECB             :         {
    2562                 :             /*
    2563                 :              * If we found no repeated non-null values, assume it's a unique
    2564                 :              * column; but be sure to discount for any nulls we found.
    2565                 :              */
    2566 GIC       30358 :             stats->stadistinct = -1.0 * (1.0 - stats->stanullfrac);
    2567                 :         }
    2568           95581 :         else if (toowide_cnt == 0 && nmultiple == ndistinct)
    2569 ECB             :         {
    2570                 :             /*
    2571                 :              * Every value in the sample appeared more than once.  Assume the
    2572                 :              * column has just these values.  (This case is meant to address
    2573                 :              * columns with small, fixed sets of possible values, such as
    2574                 :              * boolean or enum columns.  If there are any values that appear
    2575                 :              * just once in the sample, including too-wide values, we should
    2576                 :              * assume that that's not what we're dealing with.)
    2577                 :              */
    2578 GIC       54983 :             stats->stadistinct = ndistinct;
    2579                 :         }
    2580                 :         else
    2581 ECB             :         {
    2582                 :             /*----------
    2583                 :              * Estimate the number of distinct values using the estimator
    2584                 :              * proposed by Haas and Stokes in IBM Research Report RJ 10025:
    2585                 :              *      n*d / (n - f1 + f1*n/N)
    2586                 :              * where f1 is the number of distinct values that occurred
    2587                 :              * exactly once in our sample of n rows (from a total of N),
    2588                 :              * and d is the total number of distinct values in the sample.
    2589                 :              * This is their Duj1 estimator; the other estimators they
    2590                 :              * recommend are considerably more complex, and are numerically
    2591                 :              * very unstable when n is much smaller than N.
    2592                 :              *
    2593                 :              * In this calculation, we consider only non-nulls.  We used to
    2594                 :              * include rows with null values in the n and N counts, but that
    2595                 :              * leads to inaccurate answers in columns with many nulls, and
    2596                 :              * it's intuitively bogus anyway considering the desired result is
    2597                 :              * the number of distinct non-null values.
    2598                 :              *
    2599                 :              * Overwidth values are assumed to have been distinct.
    2600                 :              *----------
    2601                 :              */
    2602 GIC       40598 :             int         f1 = ndistinct - nmultiple + toowide_cnt;
    2603           40598 :             int         d = f1 + nmultiple;
    2604           40598 :             double      n = samplerows - null_cnt;
    2605 CBC       40598 :             double      N = totalrows * (1.0 - stats->stanullfrac);
    2606 ECB             :             double      stadistinct;
    2607                 : 
    2608                 :             /* N == 0 shouldn't happen, but just in case ... */
    2609 GIC       40598 :             if (N > 0)
    2610           40598 :                 stadistinct = (n * d) / ((n - f1) + f1 * n / N);
    2611                 :             else
    2612 LBC           0 :                 stadistinct = 0;
    2613 ECB             : 
    2614                 :             /* Clamp to sane range in case of roundoff error */
    2615 GBC       40598 :             if (stadistinct < d)
    2616 GIC         982 :                 stadistinct = d;
    2617           40598 :             if (stadistinct > N)
    2618 LBC           0 :                 stadistinct = N;
    2619 ECB             :             /* And round to integer */
    2620 CBC       40598 :             stats->stadistinct = floor(stadistinct + 0.5);
    2621 EUB             :         }
    2622                 : 
    2623 ECB             :         /*
    2624                 :          * If we estimated the number of distinct values at more than 10% of
    2625                 :          * the total row count (a very arbitrary limit), then assume that
    2626                 :          * stadistinct should scale with the row count rather than be a fixed
    2627                 :          * value.
    2628                 :          */
    2629 GIC      125939 :         if (stats->stadistinct > 0.1 * totalrows)
    2630           27662 :             stats->stadistinct = -(stats->stadistinct / totalrows);
    2631                 : 
    2632 ECB             :         /*
    2633                 :          * Decide how many values are worth storing as most-common values. If
    2634                 :          * we are able to generate a complete MCV list (all the values in the
    2635                 :          * sample will fit, and we think these are all the ones in the table),
    2636                 :          * then do so.  Otherwise, store only those values that are
    2637                 :          * significantly more common than the values not in the list.
    2638                 :          *
    2639                 :          * Note: the first of these cases is meant to address columns with
    2640                 :          * small, fixed sets of possible values, such as boolean or enum
    2641                 :          * columns.  If we can *completely* represent the column population by
    2642                 :          * an MCV list that will fit into the stats target, then we should do
    2643                 :          * so and thus provide the planner with complete information.  But if
    2644                 :          * the MCV list is not complete, it's generally worth being more
    2645                 :          * selective, and not just filling it all the way up to the stats
    2646                 :          * target.
    2647                 :          */
    2648 GIC      125939 :         if (track_cnt == ndistinct && toowide_cnt == 0 &&
    2649           54664 :             stats->stadistinct > 0 &&
    2650                 :             track_cnt <= num_mcv)
    2651 ECB             :         {
    2652                 :             /* Track list includes all values seen, and all will fit */
    2653 GIC       50810 :             num_mcv = track_cnt;
    2654                 :         }
    2655                 :         else
    2656 ECB             :         {
    2657                 :             int        *mcv_counts;
    2658                 : 
    2659                 :             /* Incomplete list; decide how many values are worth keeping */
    2660 GIC       75129 :             if (num_mcv > track_cnt)
    2661           69604 :                 num_mcv = track_cnt;
    2662                 : 
    2663 CBC       75129 :             if (num_mcv > 0)
    2664 ECB             :             {
    2665 GIC       44771 :                 mcv_counts = (int *) palloc(num_mcv * sizeof(int));
    2666 CBC     1027020 :                 for (i = 0; i < num_mcv; i++)
    2667 GIC      982249 :                     mcv_counts[i] = track[i].count;
    2668 ECB             : 
    2669 CBC       44771 :                 num_mcv = analyze_mcv_list(mcv_counts, num_mcv,
    2670           44771 :                                            stats->stadistinct,
    2671 GIC       44771 :                                            stats->stanullfrac,
    2672 ECB             :                                            samplerows, totalrows);
    2673                 :             }
    2674                 :         }
    2675                 : 
    2676                 :         /* Generate MCV slot entry */
    2677 GIC      125939 :         if (num_mcv > 0)
    2678                 :         {
    2679                 :             MemoryContext old_context;
    2680 ECB             :             Datum      *mcv_values;
    2681                 :             float4     *mcv_freqs;
    2682                 : 
    2683                 :             /* Must copy the target values into anl_context */
    2684 GIC       95544 :             old_context = MemoryContextSwitchTo(stats->anl_context);
    2685           95544 :             mcv_values = (Datum *) palloc(num_mcv * sizeof(Datum));
    2686           95544 :             mcv_freqs = (float4 *) palloc(num_mcv * sizeof(float4));
    2687 CBC     1278962 :             for (i = 0; i < num_mcv; i++)
    2688 ECB             :             {
    2689 CBC     2366836 :                 mcv_values[i] = datumCopy(values[track[i].first].value,
    2690         1183418 :                                           stats->attrtype->typbyval,
    2691 GIC     1183418 :                                           stats->attrtype->typlen);
    2692 CBC     1183418 :                 mcv_freqs[i] = (double) track[i].count / (double) samplerows;
    2693 ECB             :             }
    2694 CBC       95544 :             MemoryContextSwitchTo(old_context);
    2695 ECB             : 
    2696 GIC       95544 :             stats->stakind[slot_idx] = STATISTIC_KIND_MCV;
    2697 CBC       95544 :             stats->staop[slot_idx] = mystats->eqopr;
    2698 GIC       95544 :             stats->stacoll[slot_idx] = stats->attrcollid;
    2699 CBC       95544 :             stats->stanumbers[slot_idx] = mcv_freqs;
    2700           95544 :             stats->numnumbers[slot_idx] = num_mcv;
    2701           95544 :             stats->stavalues[slot_idx] = mcv_values;
    2702           95544 :             stats->numvalues[slot_idx] = num_mcv;
    2703 ECB             : 
    2704                 :             /*
    2705                 :              * Accept the defaults for stats->statypid and others. They have
    2706                 :              * been set before we were called (see vacuum.h)
    2707                 :              */
    2708 GIC       95544 :             slot_idx++;
    2709                 :         }
    2710                 : 
    2711 ECB             :         /*
    2712                 :          * Generate a histogram slot entry if there are at least two distinct
    2713                 :          * values not accounted for in the MCV list.  (This ensures the
    2714                 :          * histogram won't collapse to empty or a singleton.)
    2715                 :          */
    2716 GIC      125939 :         num_hist = ndistinct - num_mcv;
    2717          125939 :         if (num_hist > num_bins)
    2718           18193 :             num_hist = num_bins + 1;
    2719 CBC      125939 :         if (num_hist >= 2)
    2720 ECB             :         {
    2721                 :             MemoryContext old_context;
    2722                 :             Datum      *hist_values;
    2723                 :             int         nvals;
    2724                 :             int         pos,
    2725                 :                         posfrac,
    2726                 :                         delta,
    2727                 :                         deltafrac;
    2728                 : 
    2729                 :             /* Sort the MCV items into position order to speed next loop */
    2730 GNC       53893 :             qsort_interruptible(track, num_mcv, sizeof(ScalarMCVItem),
    2731                 :                                 compare_mcvs, NULL);
    2732                 : 
    2733 ECB             :             /*
    2734                 :              * Collapse out the MCV items from the values[] array.
    2735                 :              *
    2736                 :              * Note we destroy the values[] array here... but we don't need it
    2737                 :              * for anything more.  We do, however, still need values_cnt.
    2738                 :              * nvals will be the number of remaining entries in values[].
    2739                 :              */
    2740 GIC       53893 :             if (num_mcv > 0)
    2741                 :             {
    2742                 :                 int         src,
    2743 ECB             :                             dest;
    2744                 :                 int         j;
    2745                 : 
    2746 GIC       33407 :                 src = dest = 0;
    2747           33407 :                 j = 0;          /* index of next interesting MCV item */
    2748         1417706 :                 while (src < values_cnt)
    2749 ECB             :                 {
    2750                 :                     int         ncopy;
    2751                 : 
    2752 GIC     1384299 :                     if (j < num_mcv)
    2753                 :                     {
    2754         1359624 :                         int         first = track[j].first;
    2755 ECB             : 
    2756 GIC     1359624 :                         if (src >= first)
    2757 ECB             :                         {
    2758                 :                             /* advance past this MCV item */
    2759 CBC      942787 :                             src = first + track[j].count;
    2760 GIC      942787 :                             j++;
    2761          942787 :                             continue;
    2762 ECB             :                         }
    2763 CBC      416837 :                         ncopy = first - src;
    2764 ECB             :                     }
    2765                 :                     else
    2766 CBC       24675 :                         ncopy = values_cnt - src;
    2767 GIC      441512 :                     memmove(&values[dest], &values[src],
    2768                 :                             ncopy * sizeof(ScalarItem));
    2769 CBC      441512 :                     src += ncopy;
    2770          441512 :                     dest += ncopy;
    2771                 :                 }
    2772           33407 :                 nvals = dest;
    2773 ECB             :             }
    2774                 :             else
    2775 CBC       20486 :                 nvals = values_cnt;
    2776 GIC       53893 :             Assert(nvals >= num_hist);
    2777                 : 
    2778 ECB             :             /* Must copy the target values into anl_context */
    2779 CBC       53893 :             old_context = MemoryContextSwitchTo(stats->anl_context);
    2780 GIC       53893 :             hist_values = (Datum *) palloc(num_hist * sizeof(Datum));
    2781                 : 
    2782 ECB             :             /*
    2783                 :              * The object of this loop is to copy the first and last values[]
    2784                 :              * entries along with evenly-spaced values in between.  So the
    2785                 :              * i'th value is values[(i * (nvals - 1)) / (num_hist - 1)].  But
    2786                 :              * computing that subscript directly risks integer overflow when
    2787                 :              * the stats target is more than a couple thousand.  Instead we
    2788                 :              * add (nvals - 1) / (num_hist - 1) to pos at each step, tracking
    2789                 :              * the integral and fractional parts of the sum separately.
    2790                 :              */
    2791 GIC       53893 :             delta = (nvals - 1) / (num_hist - 1);
    2792           53893 :             deltafrac = (nvals - 1) % (num_hist - 1);
    2793           53893 :             pos = posfrac = 0;
    2794 ECB             : 
    2795 CBC     2603068 :             for (i = 0; i < num_hist; i++)
    2796 ECB             :             {
    2797 GIC     5098350 :                 hist_values[i] = datumCopy(values[pos].value,
    2798 CBC     2549175 :                                            stats->attrtype->typbyval,
    2799 GIC     2549175 :                                            stats->attrtype->typlen);
    2800 CBC     2549175 :                 pos += delta;
    2801         2549175 :                 posfrac += deltafrac;
    2802         2549175 :                 if (posfrac >= (num_hist - 1))
    2803 ECB             :                 {
    2804                 :                     /* fractional part exceeds 1, carry to integer part */
    2805 CBC      848862 :                     pos++;
    2806 GIC      848862 :                     posfrac -= (num_hist - 1);
    2807                 :                 }
    2808 ECB             :             }
    2809                 : 
    2810 GIC       53893 :             MemoryContextSwitchTo(old_context);
    2811                 : 
    2812           53893 :             stats->stakind[slot_idx] = STATISTIC_KIND_HISTOGRAM;
    2813 CBC       53893 :             stats->staop[slot_idx] = mystats->ltopr;
    2814 GIC       53893 :             stats->stacoll[slot_idx] = stats->attrcollid;
    2815 CBC       53893 :             stats->stavalues[slot_idx] = hist_values;
    2816           53893 :             stats->numvalues[slot_idx] = num_hist;
    2817 ECB             : 
    2818                 :             /*
    2819                 :              * Accept the defaults for stats->statypid and others. They have
    2820                 :              * been set before we were called (see vacuum.h)
    2821                 :              */
    2822 GIC       53893 :             slot_idx++;
    2823                 :         }
    2824                 : 
    2825 ECB             :         /* Generate a correlation entry if there are multiple values */
    2826 GIC      125939 :         if (values_cnt > 1)
    2827                 :         {
    2828                 :             MemoryContext old_context;
    2829 ECB             :             float4     *corrs;
    2830                 :             double      corr_xsum,
    2831                 :                         corr_x2sum;
    2832                 : 
    2833                 :             /* Must copy the target values into anl_context */
    2834 GIC      116030 :             old_context = MemoryContextSwitchTo(stats->anl_context);
    2835          116030 :             corrs = (float4 *) palloc(sizeof(float4));
    2836          116030 :             MemoryContextSwitchTo(old_context);
    2837 ECB             : 
    2838                 :             /*----------
    2839                 :              * Since we know the x and y value sets are both
    2840                 :              *      0, 1, ..., values_cnt-1
    2841                 :              * we have sum(x) = sum(y) =
    2842                 :              *      (values_cnt-1)*values_cnt / 2
    2843                 :              * and sum(x^2) = sum(y^2) =
    2844                 :              *      (values_cnt-1)*values_cnt*(2*values_cnt-1) / 6.
    2845                 :              *----------
    2846                 :              */
    2847 GIC      116030 :             corr_xsum = ((double) (values_cnt - 1)) *
    2848          116030 :                 ((double) values_cnt) / 2.0;
    2849          116030 :             corr_x2sum = ((double) (values_cnt - 1)) *
    2850 CBC      116030 :                 ((double) values_cnt) * (double) (2 * values_cnt - 1) / 6.0;
    2851 ECB             : 
    2852                 :             /* And the correlation coefficient reduces to */
    2853 CBC      116030 :             corrs[0] = (values_cnt * corr_xysum - corr_xsum * corr_xsum) /
    2854 GIC      116030 :                 (values_cnt * corr_x2sum - corr_xsum * corr_xsum);
    2855                 : 
    2856 CBC      116030 :             stats->stakind[slot_idx] = STATISTIC_KIND_CORRELATION;
    2857          116030 :             stats->staop[slot_idx] = mystats->ltopr;
    2858 GIC      116030 :             stats->stacoll[slot_idx] = stats->attrcollid;
    2859 CBC      116030 :             stats->stanumbers[slot_idx] = corrs;
    2860          116030 :             stats->numnumbers[slot_idx] = 1;
    2861          116030 :             slot_idx++;
    2862 ECB             :         }
    2863                 :     }
    2864 CBC        8534 :     else if (nonnull_cnt > 0)
    2865                 :     {
    2866                 :         /* We found some non-null values, but they were all too wide */
    2867             650 :         Assert(nonnull_cnt == toowide_cnt);
    2868 GIC         650 :         stats->stats_valid = true;
    2869                 :         /* Do the simple null-frac and width stats */
    2870 CBC         650 :         stats->stanullfrac = (double) null_cnt / (double) samplerows;
    2871             650 :         if (is_varwidth)
    2872 GIC         650 :             stats->stawidth = total_width / (double) nonnull_cnt;
    2873 ECB             :         else
    2874 LBC           0 :             stats->stawidth = stats->attrtype->typlen;
    2875 ECB             :         /* Assume all too-wide values are distinct, so it's a unique column */
    2876 GIC         650 :         stats->stadistinct = -1.0 * (1.0 - stats->stanullfrac);
    2877 EUB             :     }
    2878 GIC        7884 :     else if (null_cnt > 0)
    2879 ECB             :     {
    2880                 :         /* We found only nulls; assume the column is entirely null */
    2881 CBC        7884 :         stats->stats_valid = true;
    2882 GIC        7884 :         stats->stanullfrac = 1.0;
    2883            7884 :         if (is_varwidth)
    2884 CBC        7404 :             stats->stawidth = 0; /* "unknown" */
    2885 ECB             :         else
    2886 CBC         480 :             stats->stawidth = stats->attrtype->typlen;
    2887            7884 :         stats->stadistinct = 0.0;    /* "unknown" */
    2888                 :     }
    2889 ECB             : 
    2890                 :     /* We don't need to bother cleaning up any of our temporary palloc's */
    2891 GIC      134473 : }
    2892                 : 
    2893                 : /*
    2894 ECB             :  * Comparator for sorting ScalarItems
    2895                 :  *
    2896                 :  * Aside from sorting the items, we update the tupnoLink[] array
    2897                 :  * whenever two ScalarItems are found to contain equal datums.  The array
    2898                 :  * is indexed by tupno; for each ScalarItem, it contains the highest
    2899                 :  * tupno that that item's datum has been found to be equal to.  This allows
    2900                 :  * us to avoid additional comparisons in compute_scalar_stats().
    2901                 :  */
    2902                 : static int
    2903 GIC   814220939 : compare_scalars(const void *a, const void *b, void *arg)
    2904                 : {
    2905       814220939 :     Datum       da = ((const ScalarItem *) a)->value;
    2906 CBC   814220939 :     int         ta = ((const ScalarItem *) a)->tupno;
    2907 GIC   814220939 :     Datum       db = ((const ScalarItem *) b)->value;
    2908 CBC   814220939 :     int         tb = ((const ScalarItem *) b)->tupno;
    2909       814220939 :     CompareScalarsContext *cxt = (CompareScalarsContext *) arg;
    2910 ECB             :     int         compare;
    2911                 : 
    2912 CBC   814220939 :     compare = ApplySortComparator(da, false, db, false, cxt->ssup);
    2913 GIC   814220939 :     if (compare != 0)
    2914       279521290 :         return compare;
    2915 ECB             : 
    2916                 :     /*
    2917                 :      * The two datums are equal, so update cxt->tupnoLink[].
    2918                 :      */
    2919 GIC   534699649 :     if (cxt->tupnoLink[ta] < tb)
    2920        82840942 :         cxt->tupnoLink[ta] = tb;
    2921       534699649 :     if (cxt->tupnoLink[tb] < ta)
    2922 CBC     5398053 :         cxt->tupnoLink[tb] = ta;
    2923 ECB             : 
    2924                 :     /*
    2925                 :      * For equal datums, sort by tupno
    2926                 :      */
    2927 GIC   534699649 :     return ta - tb;
    2928                 : }
    2929                 : 
    2930 ECB             : /*
    2931                 :  * Comparator for sorting ScalarMCVItems by position
    2932                 :  */
    2933                 : static int
    2934 GIC     4942544 : compare_mcvs(const void *a, const void *b, void *arg)
    2935                 : {
    2936         4942544 :     int         da = ((const ScalarMCVItem *) a)->first;
    2937 CBC     4942544 :     int         db = ((const ScalarMCVItem *) b)->first;
    2938                 : 
    2939         4942544 :     return da - db;
    2940 ECB             : }
    2941                 : 
    2942                 : /*
    2943                 :  * Analyze the list of common values in the sample and decide how many are
    2944                 :  * worth storing in the table's MCV list.
    2945                 :  *
    2946                 :  * mcv_counts is assumed to be a list of the counts of the most common values
    2947                 :  * seen in the sample, starting with the most common.  The return value is the
    2948                 :  * number that are significantly more common than the values not in the list,
    2949                 :  * and which are therefore deemed worth storing in the table's MCV list.
    2950                 :  */
    2951                 : static int
    2952 GIC       45782 : analyze_mcv_list(int *mcv_counts,
    2953                 :                  int num_mcv,
    2954                 :                  double stadistinct,
    2955 ECB             :                  double stanullfrac,
    2956                 :                  int samplerows,
    2957                 :                  double totalrows)
    2958                 : {
    2959                 :     double      ndistinct_table;
    2960                 :     double      sumcount;
    2961                 :     int         i;
    2962                 : 
    2963                 :     /*
    2964                 :      * If the entire table was sampled, keep the whole list.  This also
    2965                 :      * protects us against division by zero in the code below.
    2966                 :      */
    2967 GIC       45782 :     if (samplerows == totalrows || totalrows <= 1.0)
    2968           45361 :         return num_mcv;
    2969                 : 
    2970 ECB             :     /* Re-extract the estimated number of distinct nonnull values in table */
    2971 CBC         421 :     ndistinct_table = stadistinct;
    2972 GIC         421 :     if (ndistinct_table < 0)
    2973              84 :         ndistinct_table = -ndistinct_table * totalrows;
    2974 ECB             : 
    2975                 :     /*
    2976                 :      * Exclude the least common values from the MCV list, if they are not
    2977                 :      * significantly more common than the estimated selectivity they would
    2978                 :      * have if they weren't in the list.  All non-MCV values are assumed to be
    2979                 :      * equally common, after taking into account the frequencies of all the
    2980                 :      * values in the MCV list and the number of nulls (c.f. eqsel()).
    2981                 :      *
    2982                 :      * Here sumcount tracks the total count of all but the last (least common)
    2983                 :      * value in the MCV list, allowing us to determine the effect of excluding
    2984                 :      * that value from the list.
    2985                 :      *
    2986                 :      * Note that we deliberately do this by removing values from the full
    2987                 :      * list, rather than starting with an empty list and adding values,
    2988                 :      * because the latter approach can fail to add any values if all the most
    2989                 :      * common values have around the same frequency and make up the majority
    2990                 :      * of the table, so that the overall average frequency of all values is
    2991                 :      * roughly the same as that of the common values.  This would lead to any
    2992                 :      * uncommon values being significantly overestimated.
    2993                 :      */
    2994 GIC         421 :     sumcount = 0.0;
    2995             865 :     for (i = 0; i < num_mcv - 1; i++)
    2996             444 :         sumcount += mcv_counts[i];
    2997 ECB             : 
    2998 CBC         487 :     while (num_mcv > 0)
    2999 ECB             :     {
    3000                 :         double      selec,
    3001                 :                     otherdistinct,
    3002                 :                     N,
    3003                 :                     n,
    3004                 :                     K,
    3005                 :                     variance,
    3006                 :                     stddev;
    3007                 : 
    3008                 :         /*
    3009                 :          * Estimated selectivity the least common value would have if it
    3010                 :          * wasn't in the MCV list (c.f. eqsel()).
    3011                 :          */
    3012 GIC         487 :         selec = 1.0 - sumcount / samplerows - stanullfrac;
    3013             487 :         if (selec < 0.0)
    3014 UIC           0 :             selec = 0.0;
    3015 CBC         487 :         if (selec > 1.0)
    3016 LBC           0 :             selec = 1.0;
    3017 GBC         487 :         otherdistinct = ndistinct_table - (num_mcv - 1);
    3018 CBC         487 :         if (otherdistinct > 1)
    3019 GBC         487 :             selec /= otherdistinct;
    3020 ECB             : 
    3021                 :         /*
    3022                 :          * If the value is kept in the MCV list, its population frequency is
    3023                 :          * assumed to equal its sample frequency.  We use the lower end of a
    3024                 :          * textbook continuity-corrected Wald-type confidence interval to
    3025                 :          * determine if that is significantly more common than the non-MCV
    3026                 :          * frequency --- specifically we assume the population frequency is
    3027                 :          * highly likely to be within around 2 standard errors of the sample
    3028                 :          * frequency, which equates to an interval of 2 standard deviations
    3029                 :          * either side of the sample count, plus an additional 0.5 for the
    3030                 :          * continuity correction.  Since we are sampling without replacement,
    3031                 :          * this is a hypergeometric distribution.
    3032                 :          *
    3033                 :          * XXX: Empirically, this approach seems to work quite well, but it
    3034                 :          * may be worth considering more advanced techniques for estimating
    3035                 :          * the confidence interval of the hypergeometric distribution.
    3036                 :          */
    3037 GIC         487 :         N = totalrows;
    3038             487 :         n = samplerows;
    3039             487 :         K = N * mcv_counts[num_mcv - 1] / n;
    3040 CBC         487 :         variance = n * K * (N - K) * (N - n) / (N * N * (N - 1));
    3041             487 :         stddev = sqrt(variance);
    3042 ECB             : 
    3043 CBC         487 :         if (mcv_counts[num_mcv - 1] > selec * samplerows + 2 * stddev + 0.5)
    3044 ECB             :         {
    3045                 :             /*
    3046                 :              * The value is significantly more common than the non-MCV
    3047                 :              * selectivity would suggest.  Keep it, and all the other more
    3048                 :              * common values in the list.
    3049                 :              */
    3050 GIC         380 :             break;
    3051                 :         }
    3052                 :         else
    3053 ECB             :         {
    3054                 :             /* Discard this value and consider the next least common value */
    3055 GIC         107 :             num_mcv--;
    3056             107 :             if (num_mcv == 0)
    3057              41 :                 break;
    3058 CBC          66 :             sumcount -= mcv_counts[num_mcv - 1];
    3059 ECB             :         }
    3060                 :     }
    3061 CBC         421 :     return num_mcv;
    3062                 : }
        

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