PostgreSQL Source Code  git master
mcv.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/htup_details.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_statistic_ext_data.h"
#include "fmgr.h"
#include "funcapi.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "statistics/extended_stats_internal.h"
#include "statistics/statistics.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/bytea.h"
#include "utils/fmgroids.h"
#include "utils/fmgrprotos.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for mcv.c:

Go to the source code of this file.

Macros

#define ITEM_SIZE(ndims)    ((ndims) * (sizeof(uint16) + sizeof(bool)) + 2 * sizeof(double))
 
#define MinSizeOfMCVList    (VARHDRSZ + sizeof(uint32) * 3 + sizeof(AttrNumber))
 
#define SizeOfMCVList(ndims, nitems)
 
#define RESULT_MERGE(value, is_or, match)    ((is_or) ? ((value) || (match)) : ((value) && (match)))
 
#define RESULT_IS_FINAL(value, is_or)   ((is_or) ? (value) : (!(value)))
 

Functions

static MultiSortSupport build_mss (StatsBuildData *data)
 
static SortItembuild_distinct_groups (int numrows, SortItem *items, MultiSortSupport mss, int *ndistinct)
 
static SortItem ** build_column_frequencies (SortItem *groups, int ngroups, MultiSortSupport mss, int *ncounts)
 
static int count_distinct_groups (int numrows, SortItem *items, MultiSortSupport mss)
 
static double get_mincount_for_mcv_list (int samplerows, double totalrows)
 
MCVListstatext_mcv_build (StatsBuildData *data, double totalrows, int stattarget)
 
static int compare_sort_item_count (const void *a, const void *b, void *arg)
 
static int sort_item_compare (const void *a, const void *b, void *arg)
 
MCVListstatext_mcv_load (Oid mvoid, bool inh)
 
byteastatext_mcv_serialize (MCVList *mcvlist, VacAttrStats **stats)
 
MCVListstatext_mcv_deserialize (bytea *data)
 
Datum pg_stats_ext_mcvlist_items (PG_FUNCTION_ARGS)
 
Datum pg_mcv_list_in (PG_FUNCTION_ARGS)
 
Datum pg_mcv_list_out (PG_FUNCTION_ARGS)
 
Datum pg_mcv_list_recv (PG_FUNCTION_ARGS)
 
Datum pg_mcv_list_send (PG_FUNCTION_ARGS)
 
static int mcv_match_expression (Node *expr, Bitmapset *keys, List *exprs, Oid *collid)
 
static boolmcv_get_match_bitmap (PlannerInfo *root, List *clauses, Bitmapset *keys, List *exprs, MCVList *mcvlist, bool is_or)
 
Selectivity mcv_combine_selectivities (Selectivity simple_sel, Selectivity mcv_sel, Selectivity mcv_basesel, Selectivity mcv_totalsel)
 
Selectivity mcv_clauselist_selectivity (PlannerInfo *root, StatisticExtInfo *stat, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Selectivity *basesel, Selectivity *totalsel)
 
Selectivity mcv_clause_selectivity_or (PlannerInfo *root, StatisticExtInfo *stat, MCVList *mcv, Node *clause, bool **or_matches, Selectivity *basesel, Selectivity *overlap_mcvsel, Selectivity *overlap_basesel, Selectivity *totalsel)
 

Macro Definition Documentation

◆ ITEM_SIZE

#define ITEM_SIZE (   ndims)     ((ndims) * (sizeof(uint16) + sizeof(bool)) + 2 * sizeof(double))

Definition at line 57 of file mcv.c.

◆ MinSizeOfMCVList

#define MinSizeOfMCVList    (VARHDRSZ + sizeof(uint32) * 3 + sizeof(AttrNumber))

Definition at line 63 of file mcv.c.

◆ RESULT_IS_FINAL

#define RESULT_IS_FINAL (   value,
  is_or 
)    ((is_or) ? (value) : (!(value)))

Definition at line 104 of file mcv.c.

◆ RESULT_MERGE

#define RESULT_MERGE (   value,
  is_or,
  match 
)     ((is_or) ? ((value) || (match)) : ((value) && (match)))

Definition at line 92 of file mcv.c.

◆ SizeOfMCVList

#define SizeOfMCVList (   ndims,
  nitems 
)
Value:
((MinSizeOfMCVList + sizeof(Oid) * (ndims)) + \
((ndims) * sizeof(DimensionInfo)) + \
((nitems) * ITEM_SIZE(ndims)))
struct DimensionInfo DimensionInfo
#define nitems(x)
Definition: indent.h:31
#define ITEM_SIZE(ndims)
Definition: mcv.c:57
#define MinSizeOfMCVList
Definition: mcv.c:63
unsigned int Oid
Definition: postgres_ext.h:31

Definition at line 72 of file mcv.c.

Function Documentation

◆ build_column_frequencies()

static SortItem ** build_column_frequencies ( SortItem groups,
int  ngroups,
MultiSortSupport  mss,
int *  ncounts 
)
static

Definition at line 494 of file mcv.c.

496 {
497  int i,
498  dim;
499  SortItem **result;
500  char *ptr;
501 
502  Assert(groups);
503  Assert(ncounts);
504 
505  /* allocate arrays for all columns as a single chunk */
506  ptr = palloc(MAXALIGN(sizeof(SortItem *) * mss->ndims) +
507  mss->ndims * MAXALIGN(sizeof(SortItem) * ngroups));
508 
509  /* initial array of pointers */
510  result = (SortItem **) ptr;
511  ptr += MAXALIGN(sizeof(SortItem *) * mss->ndims);
512 
513  for (dim = 0; dim < mss->ndims; dim++)
514  {
515  SortSupport ssup = &mss->ssup[dim];
516 
517  /* array of values for a single column */
518  result[dim] = (SortItem *) ptr;
519  ptr += MAXALIGN(sizeof(SortItem) * ngroups);
520 
521  /* extract data for the dimension */
522  for (i = 0; i < ngroups; i++)
523  {
524  /* point into the input groups */
525  result[dim][i].values = &groups[i].values[dim];
526  result[dim][i].isnull = &groups[i].isnull[dim];
527  result[dim][i].count = groups[i].count;
528  }
529 
530  /* sort the values, deduplicate */
531  qsort_interruptible(result[dim], ngroups, sizeof(SortItem),
532  sort_item_compare, ssup);
533 
534  /*
535  * Identify distinct values, compute frequency (there might be
536  * multiple MCV items containing this value, so we need to sum counts
537  * from all of them.
538  */
539  ncounts[dim] = 1;
540  for (i = 1; i < ngroups; i++)
541  {
542  if (sort_item_compare(&result[dim][i - 1], &result[dim][i], ssup) == 0)
543  {
544  result[dim][ncounts[dim] - 1].count += result[dim][i].count;
545  continue;
546  }
547 
548  result[dim][ncounts[dim]] = result[dim][i];
549 
550  ncounts[dim]++;
551  }
552  }
553 
554  return result;
555 }
#define MAXALIGN(LEN)
Definition: c.h:800
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
static int sort_item_compare(const void *a, const void *b, void *arg)
Definition: mcv.c:469
void * palloc(Size size)
Definition: mcxt.c:1226
void qsort_interruptible(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER]

References Assert(), SortItem::count, i, SortItem::isnull, MAXALIGN, MultiSortSupportData::ndims, palloc(), qsort_interruptible(), sort_item_compare(), MultiSortSupportData::ssup, and SortItem::values.

Referenced by statext_mcv_build().

◆ build_distinct_groups()

static SortItem * build_distinct_groups ( int  numrows,
SortItem items,
MultiSortSupport  mss,
int *  ndistinct 
)
static

Definition at line 428 of file mcv.c.

430 {
431  int i,
432  j;
433  int ngroups = count_distinct_groups(numrows, items, mss);
434 
435  SortItem *groups = (SortItem *) palloc(ngroups * sizeof(SortItem));
436 
437  j = 0;
438  groups[0] = items[0];
439  groups[0].count = 1;
440 
441  for (i = 1; i < numrows; i++)
442  {
443  /* Assume sorted in ascending order. */
444  Assert(multi_sort_compare(&items[i], &items[i - 1], mss) >= 0);
445 
446  /* New distinct group detected. */
447  if (multi_sort_compare(&items[i], &items[i - 1], mss) != 0)
448  {
449  groups[++j] = items[i];
450  groups[j].count = 0;
451  }
452 
453  groups[j].count++;
454  }
455 
456  /* ensure we filled the expected number of distinct groups */
457  Assert(j + 1 == ngroups);
458 
459  /* Sort the distinct groups by frequency (in descending order). */
460  qsort_interruptible(groups, ngroups, sizeof(SortItem),
462 
463  *ndistinct = ngroups;
464  return groups;
465 }
int multi_sort_compare(const void *a, const void *b, void *arg)
int j
Definition: isn.c:74
static int compare_sort_item_count(const void *a, const void *b, void *arg)
Definition: mcv.c:407
static int count_distinct_groups(int numrows, SortItem *items, MultiSortSupport mss)
Definition: mcv.c:383

References Assert(), compare_sort_item_count(), SortItem::count, count_distinct_groups(), i, j, multi_sort_compare(), palloc(), and qsort_interruptible().

Referenced by statext_mcv_build().

◆ build_mss()

static MultiSortSupport build_mss ( StatsBuildData data)
static

Definition at line 351 of file mcv.c.

352 {
353  int i;
354  int numattrs = data->nattnums;
355 
356  /* Sort by multiple columns (using array of SortSupport) */
357  MultiSortSupport mss = multi_sort_init(numattrs);
358 
359  /* prepare the sort functions for all the attributes */
360  for (i = 0; i < numattrs; i++)
361  {
362  VacAttrStats *colstat = data->stats[i];
364 
366  if (type->lt_opr == InvalidOid) /* shouldn't happen */
367  elog(ERROR, "cache lookup failed for ordering operator for type %u",
368  colstat->attrtypid);
369 
370  multi_sort_add_dimension(mss, i, type->lt_opr, colstat->attrcollid);
371  }
372 
373  return mss;
374 }
#define ERROR
Definition: elog.h:39
MultiSortSupport multi_sort_init(int ndims)
void multi_sort_add_dimension(MultiSortSupport mss, int sortdim, Oid oper, Oid collation)
const void * data
#define InvalidOid
Definition: postgres_ext.h:36
Oid attrtypid
Definition: vacuum.h:125
Oid attrcollid
Definition: vacuum.h:128
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:344
#define TYPECACHE_LT_OPR
Definition: typcache.h:137
const char * type

References VacAttrStats::attrcollid, VacAttrStats::attrtypid, data, elog(), ERROR, i, InvalidOid, lookup_type_cache(), multi_sort_add_dimension(), multi_sort_init(), type, and TYPECACHE_LT_OPR.

Referenced by statext_mcv_build().

◆ compare_sort_item_count()

static int compare_sort_item_count ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 407 of file mcv.c.

408 {
409  SortItem *ia = (SortItem *) a;
410  SortItem *ib = (SortItem *) b;
411 
412  if (ia->count == ib->count)
413  return 0;
414  else if (ia->count > ib->count)
415  return -1;
416 
417  return 1;
418 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, b, and SortItem::count.

Referenced by build_distinct_groups().

◆ count_distinct_groups()

static int count_distinct_groups ( int  numrows,
SortItem items,
MultiSortSupport  mss 
)
static

Definition at line 383 of file mcv.c.

384 {
385  int i;
386  int ndistinct;
387 
388  ndistinct = 1;
389  for (i = 1; i < numrows; i++)
390  {
391  /* make sure the array really is sorted */
392  Assert(multi_sort_compare(&items[i], &items[i - 1], mss) >= 0);
393 
394  if (multi_sort_compare(&items[i], &items[i - 1], mss) != 0)
395  ndistinct += 1;
396  }
397 
398  return ndistinct;
399 }

References Assert(), i, and multi_sort_compare().

Referenced by build_distinct_groups().

◆ get_mincount_for_mcv_list()

static double get_mincount_for_mcv_list ( int  samplerows,
double  totalrows 
)
static

Definition at line 152 of file mcv.c.

153 {
154  double n = samplerows;
155  double N = totalrows;
156  double numer,
157  denom;
158 
159  numer = n * (N - n);
160  denom = N - n + 0.04 * n * (N - 1);
161 
162  /* Guard against division by zero (possible if n = N = 1) */
163  if (denom == 0.0)
164  return 0.0;
165 
166  return numer / denom;
167 }

Referenced by statext_mcv_build().

◆ mcv_clause_selectivity_or()

Selectivity mcv_clause_selectivity_or ( PlannerInfo root,
StatisticExtInfo stat,
MCVList mcv,
Node clause,
bool **  or_matches,
Selectivity basesel,
Selectivity overlap_mcvsel,
Selectivity overlap_basesel,
Selectivity totalsel 
)

Definition at line 2130 of file mcv.c.

2134 {
2135  Selectivity s = 0.0;
2136  bool *new_matches;
2137  int i;
2138 
2139  /* build the OR-matches bitmap, if not built already */
2140  if (*or_matches == NULL)
2141  *or_matches = palloc0(sizeof(bool) * mcv->nitems);
2142 
2143  /* build the match bitmap for the new clause */
2144  new_matches = mcv_get_match_bitmap(root, list_make1(clause), stat->keys,
2145  stat->exprs, mcv, false);
2146 
2147  /*
2148  * Sum the frequencies for all the MCV items matching this clause and also
2149  * those matching the overlap between this clause and any of the preceding
2150  * clauses as described above.
2151  */
2152  *basesel = 0.0;
2153  *overlap_mcvsel = 0.0;
2154  *overlap_basesel = 0.0;
2155  *totalsel = 0.0;
2156  for (i = 0; i < mcv->nitems; i++)
2157  {
2158  *totalsel += mcv->items[i].frequency;
2159 
2160  if (new_matches[i])
2161  {
2162  s += mcv->items[i].frequency;
2163  *basesel += mcv->items[i].base_frequency;
2164 
2165  if ((*or_matches)[i])
2166  {
2167  *overlap_mcvsel += mcv->items[i].frequency;
2168  *overlap_basesel += mcv->items[i].base_frequency;
2169  }
2170  }
2171 
2172  /* update the OR-matches bitmap for the next clause */
2173  (*or_matches)[i] = (*or_matches)[i] || new_matches[i];
2174  }
2175 
2176  pfree(new_matches);
2177 
2178  return s;
2179 }
static bool * mcv_get_match_bitmap(PlannerInfo *root, List *clauses, Bitmapset *keys, List *exprs, MCVList *mcvlist, bool is_or)
Definition: mcv.c:1603
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc0(Size size)
Definition: mcxt.c:1257
double Selectivity
Definition: nodes.h:261
#define list_make1(x1)
Definition: pg_list.h:212
double frequency
Definition: statistics.h:80
double base_frequency
Definition: statistics.h:81
uint32 nitems
Definition: statistics.h:91
MCVItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:94

References MCVItem::base_frequency, MCVItem::frequency, i, MCVList::items, list_make1, mcv_get_match_bitmap(), MCVList::nitems, palloc0(), and pfree().

Referenced by statext_mcv_clauselist_selectivity().

◆ mcv_clauselist_selectivity()

Selectivity mcv_clauselist_selectivity ( PlannerInfo root,
StatisticExtInfo stat,
List clauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo,
RelOptInfo rel,
Selectivity basesel,
Selectivity totalsel 
)

Definition at line 2052 of file mcv.c.

2057 {
2058  int i;
2059  MCVList *mcv;
2060  Selectivity s = 0.0;
2061  RangeTblEntry *rte = root->simple_rte_array[rel->relid];
2062 
2063  /* match/mismatch bitmap for each MCV item */
2064  bool *matches = NULL;
2065 
2066  /* load the MCV list stored in the statistics object */
2067  mcv = statext_mcv_load(stat->statOid, rte->inh);
2068 
2069  /* build a match bitmap for the clauses */
2070  matches = mcv_get_match_bitmap(root, clauses, stat->keys, stat->exprs,
2071  mcv, false);
2072 
2073  /* sum frequencies for all the matching MCV items */
2074  *basesel = 0.0;
2075  *totalsel = 0.0;
2076  for (i = 0; i < mcv->nitems; i++)
2077  {
2078  *totalsel += mcv->items[i].frequency;
2079 
2080  if (matches[i] != false)
2081  {
2082  *basesel += mcv->items[i].base_frequency;
2083  s += mcv->items[i].frequency;
2084  }
2085  }
2086 
2087  return s;
2088 }
MCVList * statext_mcv_load(Oid mvoid, bool inh)
Definition: mcv.c:562
Index relid
Definition: pathnodes.h:903

References MCVItem::base_frequency, MCVItem::frequency, i, RangeTblEntry::inh, MCVList::items, mcv_get_match_bitmap(), MCVList::nitems, RelOptInfo::relid, and statext_mcv_load().

Referenced by statext_mcv_clauselist_selectivity().

◆ mcv_combine_selectivities()

Selectivity mcv_combine_selectivities ( Selectivity  simple_sel,
Selectivity  mcv_sel,
Selectivity  mcv_basesel,
Selectivity  mcv_totalsel 
)

Definition at line 2010 of file mcv.c.

2014 {
2015  Selectivity other_sel;
2016  Selectivity sel;
2017 
2018  /* estimated selectivity of values not covered by MCV matches */
2019  other_sel = simple_sel - mcv_basesel;
2020  CLAMP_PROBABILITY(other_sel);
2021 
2022  /* this non-MCV selectivity cannot exceed 1 - mcv_totalsel */
2023  if (other_sel > 1.0 - mcv_totalsel)
2024  other_sel = 1.0 - mcv_totalsel;
2025 
2026  /* overall selectivity is the sum of the MCV and non-MCV parts */
2027  sel = mcv_sel + other_sel;
2028  CLAMP_PROBABILITY(sel);
2029 
2030  return sel;
2031 }
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:63

References CLAMP_PROBABILITY.

Referenced by statext_mcv_clauselist_selectivity().

◆ mcv_get_match_bitmap()

static bool* mcv_get_match_bitmap ( PlannerInfo root,
List clauses,
Bitmapset keys,
List exprs,
MCVList mcvlist,
bool  is_or 
)
static

Definition at line 1603 of file mcv.c.

1606 {
1607  ListCell *l;
1608  bool *matches;
1609 
1610  /* The bitmap may be partially built. */
1611  Assert(clauses != NIL);
1612  Assert(mcvlist != NULL);
1613  Assert(mcvlist->nitems > 0);
1614  Assert(mcvlist->nitems <= STATS_MCVLIST_MAX_ITEMS);
1615 
1616  matches = palloc(sizeof(bool) * mcvlist->nitems);
1617  memset(matches, !is_or, sizeof(bool) * mcvlist->nitems);
1618 
1619  /*
1620  * Loop through the list of clauses, and for each of them evaluate all the
1621  * MCV items not yet eliminated by the preceding clauses.
1622  */
1623  foreach(l, clauses)
1624  {
1625  Node *clause = (Node *) lfirst(l);
1626 
1627  /* if it's a RestrictInfo, then extract the clause */
1628  if (IsA(clause, RestrictInfo))
1629  clause = (Node *) ((RestrictInfo *) clause)->clause;
1630 
1631  /*
1632  * Handle the various types of clauses - OpClause, NullTest and
1633  * AND/OR/NOT
1634  */
1635  if (is_opclause(clause))
1636  {
1637  OpExpr *expr = (OpExpr *) clause;
1638  FmgrInfo opproc;
1639 
1640  /* valid only after examine_opclause_args returns true */
1641  Node *clause_expr;
1642  Const *cst;
1643  bool expronleft;
1644  int idx;
1645  Oid collid;
1646 
1647  fmgr_info(get_opcode(expr->opno), &opproc);
1648 
1649  /* extract the var/expr and const from the expression */
1650  if (!examine_opclause_args(expr->args, &clause_expr, &cst, &expronleft))
1651  elog(ERROR, "incompatible clause");
1652 
1653  /* match the attribute/expression to a dimension of the statistic */
1654  idx = mcv_match_expression(clause_expr, keys, exprs, &collid);
1655 
1656  /*
1657  * Walk through the MCV items and evaluate the current clause. We
1658  * can skip items that were already ruled out, and terminate if
1659  * there are no remaining MCV items that might possibly match.
1660  */
1661  for (int i = 0; i < mcvlist->nitems; i++)
1662  {
1663  bool match = true;
1664  MCVItem *item = &mcvlist->items[i];
1665 
1666  Assert(idx >= 0);
1667 
1668  /*
1669  * When the MCV item or the Const value is NULL we can treat
1670  * this as a mismatch. We must not call the operator because
1671  * of strictness.
1672  */
1673  if (item->isnull[idx] || cst->constisnull)
1674  {
1675  matches[i] = RESULT_MERGE(matches[i], is_or, false);
1676  continue;
1677  }
1678 
1679  /*
1680  * Skip MCV items that can't change result in the bitmap. Once
1681  * the value gets false for AND-lists, or true for OR-lists,
1682  * we don't need to look at more clauses.
1683  */
1684  if (RESULT_IS_FINAL(matches[i], is_or))
1685  continue;
1686 
1687  /*
1688  * First check whether the constant is below the lower
1689  * boundary (in that case we can skip the bucket, because
1690  * there's no overlap).
1691  *
1692  * We don't store collations used to build the statistics, but
1693  * we can use the collation for the attribute itself, as
1694  * stored in varcollid. We do reset the statistics after a
1695  * type change (including collation change), so this is OK.
1696  * For expressions, we use the collation extracted from the
1697  * expression itself.
1698  */
1699  if (expronleft)
1700  match = DatumGetBool(FunctionCall2Coll(&opproc,
1701  collid,
1702  item->values[idx],
1703  cst->constvalue));
1704  else
1705  match = DatumGetBool(FunctionCall2Coll(&opproc,
1706  collid,
1707  cst->constvalue,
1708  item->values[idx]));
1709 
1710  /* update the match bitmap with the result */
1711  matches[i] = RESULT_MERGE(matches[i], is_or, match);
1712  }
1713  }
1714  else if (IsA(clause, ScalarArrayOpExpr))
1715  {
1716  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) clause;
1717  FmgrInfo opproc;
1718 
1719  /* valid only after examine_opclause_args returns true */
1720  Node *clause_expr;
1721  Const *cst;
1722  bool expronleft;
1723  Oid collid;
1724  int idx;
1725 
1726  /* array evaluation */
1727  ArrayType *arrayval;
1728  int16 elmlen;
1729  bool elmbyval;
1730  char elmalign;
1731  int num_elems;
1732  Datum *elem_values;
1733  bool *elem_nulls;
1734 
1735  fmgr_info(get_opcode(expr->opno), &opproc);
1736 
1737  /* extract the var/expr and const from the expression */
1738  if (!examine_opclause_args(expr->args, &clause_expr, &cst, &expronleft))
1739  elog(ERROR, "incompatible clause");
1740 
1741  /* We expect Var on left */
1742  if (!expronleft)
1743  elog(ERROR, "incompatible clause");
1744 
1745  /*
1746  * Deconstruct the array constant, unless it's NULL (we'll cover
1747  * that case below)
1748  */
1749  if (!cst->constisnull)
1750  {
1751  arrayval = DatumGetArrayTypeP(cst->constvalue);
1753  &elmlen, &elmbyval, &elmalign);
1754  deconstruct_array(arrayval,
1755  ARR_ELEMTYPE(arrayval),
1756  elmlen, elmbyval, elmalign,
1757  &elem_values, &elem_nulls, &num_elems);
1758  }
1759 
1760  /* match the attribute/expression to a dimension of the statistic */
1761  idx = mcv_match_expression(clause_expr, keys, exprs, &collid);
1762 
1763  /*
1764  * Walk through the MCV items and evaluate the current clause. We
1765  * can skip items that were already ruled out, and terminate if
1766  * there are no remaining MCV items that might possibly match.
1767  */
1768  for (int i = 0; i < mcvlist->nitems; i++)
1769  {
1770  int j;
1771  bool match = !expr->useOr;
1772  MCVItem *item = &mcvlist->items[i];
1773 
1774  /*
1775  * When the MCV item or the Const value is NULL we can treat
1776  * this as a mismatch. We must not call the operator because
1777  * of strictness.
1778  */
1779  if (item->isnull[idx] || cst->constisnull)
1780  {
1781  matches[i] = RESULT_MERGE(matches[i], is_or, false);
1782  continue;
1783  }
1784 
1785  /*
1786  * Skip MCV items that can't change result in the bitmap. Once
1787  * the value gets false for AND-lists, or true for OR-lists,
1788  * we don't need to look at more clauses.
1789  */
1790  if (RESULT_IS_FINAL(matches[i], is_or))
1791  continue;
1792 
1793  for (j = 0; j < num_elems; j++)
1794  {
1795  Datum elem_value = elem_values[j];
1796  bool elem_isnull = elem_nulls[j];
1797  bool elem_match;
1798 
1799  /* NULL values always evaluate as not matching. */
1800  if (elem_isnull)
1801  {
1802  match = RESULT_MERGE(match, expr->useOr, false);
1803  continue;
1804  }
1805 
1806  /*
1807  * Stop evaluating the array elements once we reach a
1808  * matching value that can't change - ALL() is the same as
1809  * AND-list, ANY() is the same as OR-list.
1810  */
1811  if (RESULT_IS_FINAL(match, expr->useOr))
1812  break;
1813 
1814  elem_match = DatumGetBool(FunctionCall2Coll(&opproc,
1815  collid,
1816  item->values[idx],
1817  elem_value));
1818 
1819  match = RESULT_MERGE(match, expr->useOr, elem_match);
1820  }
1821 
1822  /* update the match bitmap with the result */
1823  matches[i] = RESULT_MERGE(matches[i], is_or, match);
1824  }
1825  }
1826  else if (IsA(clause, NullTest))
1827  {
1828  NullTest *expr = (NullTest *) clause;
1829  Node *clause_expr = (Node *) (expr->arg);
1830 
1831  /* match the attribute/expression to a dimension of the statistic */
1832  int idx = mcv_match_expression(clause_expr, keys, exprs, NULL);
1833 
1834  /*
1835  * Walk through the MCV items and evaluate the current clause. We
1836  * can skip items that were already ruled out, and terminate if
1837  * there are no remaining MCV items that might possibly match.
1838  */
1839  for (int i = 0; i < mcvlist->nitems; i++)
1840  {
1841  bool match = false; /* assume mismatch */
1842  MCVItem *item = &mcvlist->items[i];
1843 
1844  /* if the clause mismatches the MCV item, update the bitmap */
1845  switch (expr->nulltesttype)
1846  {
1847  case IS_NULL:
1848  match = (item->isnull[idx]) ? true : match;
1849  break;
1850 
1851  case IS_NOT_NULL:
1852  match = (!item->isnull[idx]) ? true : match;
1853  break;
1854  }
1855 
1856  /* now, update the match bitmap, depending on OR/AND type */
1857  matches[i] = RESULT_MERGE(matches[i], is_or, match);
1858  }
1859  }
1860  else if (is_orclause(clause) || is_andclause(clause))
1861  {
1862  /* AND/OR clause, with all subclauses being compatible */
1863 
1864  int i;
1865  BoolExpr *bool_clause = ((BoolExpr *) clause);
1866  List *bool_clauses = bool_clause->args;
1867 
1868  /* match/mismatch bitmap for each MCV item */
1869  bool *bool_matches = NULL;
1870 
1871  Assert(bool_clauses != NIL);
1872  Assert(list_length(bool_clauses) >= 2);
1873 
1874  /* build the match bitmap for the OR-clauses */
1875  bool_matches = mcv_get_match_bitmap(root, bool_clauses, keys, exprs,
1876  mcvlist, is_orclause(clause));
1877 
1878  /*
1879  * Merge the bitmap produced by mcv_get_match_bitmap into the
1880  * current one. We need to consider if we're evaluating AND or OR
1881  * condition when merging the results.
1882  */
1883  for (i = 0; i < mcvlist->nitems; i++)
1884  matches[i] = RESULT_MERGE(matches[i], is_or, bool_matches[i]);
1885 
1886  pfree(bool_matches);
1887  }
1888  else if (is_notclause(clause))
1889  {
1890  /* NOT clause, with all subclauses compatible */
1891 
1892  int i;
1893  BoolExpr *not_clause = ((BoolExpr *) clause);
1894  List *not_args = not_clause->args;
1895 
1896  /* match/mismatch bitmap for each MCV item */
1897  bool *not_matches = NULL;
1898 
1899  Assert(not_args != NIL);
1900  Assert(list_length(not_args) == 1);
1901 
1902  /* build the match bitmap for the NOT-clause */
1903  not_matches = mcv_get_match_bitmap(root, not_args, keys, exprs,
1904  mcvlist, false);
1905 
1906  /*
1907  * Merge the bitmap produced by mcv_get_match_bitmap into the
1908  * current one. We're handling a NOT clause, so invert the result
1909  * before merging it into the global bitmap.
1910  */
1911  for (i = 0; i < mcvlist->nitems; i++)
1912  matches[i] = RESULT_MERGE(matches[i], is_or, !not_matches[i]);
1913 
1914  pfree(not_matches);
1915  }
1916  else if (IsA(clause, Var))
1917  {
1918  /* Var (has to be a boolean Var, possibly from below NOT) */
1919 
1920  Var *var = (Var *) (clause);
1921 
1922  /* match the attribute to a dimension of the statistic */
1923  int idx = bms_member_index(keys, var->varattno);
1924 
1925  Assert(var->vartype == BOOLOID);
1926 
1927  /*
1928  * Walk through the MCV items and evaluate the current clause. We
1929  * can skip items that were already ruled out, and terminate if
1930  * there are no remaining MCV items that might possibly match.
1931  */
1932  for (int i = 0; i < mcvlist->nitems; i++)
1933  {
1934  MCVItem *item = &mcvlist->items[i];
1935  bool match = false;
1936 
1937  /* if the item is NULL, it's a mismatch */
1938  if (!item->isnull[idx] && DatumGetBool(item->values[idx]))
1939  match = true;
1940 
1941  /* update the result bitmap */
1942  matches[i] = RESULT_MERGE(matches[i], is_or, match);
1943  }
1944  }
1945  else
1946  {
1947  /* Otherwise, it must be a bare boolean-returning expression */
1948  int idx;
1949 
1950  /* match the expression to a dimension of the statistic */
1951  idx = mcv_match_expression(clause, keys, exprs, NULL);
1952 
1953  /*
1954  * Walk through the MCV items and evaluate the current clause. We
1955  * can skip items that were already ruled out, and terminate if
1956  * there are no remaining MCV items that might possibly match.
1957  */
1958  for (int i = 0; i < mcvlist->nitems; i++)
1959  {
1960  bool match;
1961  MCVItem *item = &mcvlist->items[i];
1962 
1963  /* "match" just means it's bool TRUE */
1964  match = !item->isnull[idx] && DatumGetBool(item->values[idx]);
1965 
1966  /* now, update the match bitmap, depending on OR/AND type */
1967  matches[i] = RESULT_MERGE(matches[i], is_or, match);
1968  }
1969  }
1970  }
1971 
1972  return matches;
1973 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define DatumGetArrayTypeP(X)
Definition: array.h:254
#define ARR_ELEMTYPE(a)
Definition: array.h:285
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3578
int bms_member_index(Bitmapset *a, int x)
Definition: bitmapset.c:486
signed short int16
Definition: c.h:482
Oid collid
bool examine_opclause_args(List *args, Node **exprp, Const **cstp, bool *expronleftp)
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1132
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2253
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1289
#define RESULT_MERGE(value, is_or, match)
Definition: mcv.c:92
#define RESULT_IS_FINAL(value, is_or)
Definition: mcv.c:104
static int mcv_match_expression(Node *expr, Bitmapset *keys, List *exprs, Oid *collid)
Definition: mcv.c:1539
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:114
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:74
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:123
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
uintptr_t Datum
Definition: postgres.h:64
@ IS_NULL
Definition: primnodes.h:1686
@ IS_NOT_NULL
Definition: primnodes.h:1686
#define STATS_MCVLIST_MAX_ITEMS
Definition: statistics.h:70
List * args
Definition: primnodes.h:867
Definition: fmgr.h:57
Definition: pg_list.h:54
bool * isnull
Definition: statistics.h:82
Datum * values
Definition: statistics.h:83
Definition: nodes.h:129
NullTestType nulltesttype
Definition: primnodes.h:1693
Expr * arg
Definition: primnodes.h:1692
Oid opno
Definition: primnodes.h:745
List * args
Definition: primnodes.h:763
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238

References NullTest::arg, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, ARR_ELEMTYPE, Assert(), bms_member_index(), collid, DatumGetArrayTypeP, DatumGetBool(), deconstruct_array(), elog(), ERROR, examine_opclause_args(), fmgr_info(), FunctionCall2Coll(), get_opcode(), get_typlenbyvalalign(), i, idx(), is_andclause(), IS_NOT_NULL, is_notclause(), IS_NULL, is_opclause(), is_orclause(), IsA, MCVItem::isnull, MCVList::items, j, lfirst, list_length(), mcv_match_expression(), NIL, MCVList::nitems, NullTest::nulltesttype, OpExpr::opno, ScalarArrayOpExpr::opno, palloc(), pfree(), RESULT_IS_FINAL, RESULT_MERGE, STATS_MCVLIST_MAX_ITEMS, ScalarArrayOpExpr::useOr, MCVItem::values, and Var::varattno.

Referenced by mcv_clause_selectivity_or(), and mcv_clauselist_selectivity().

◆ mcv_match_expression()

static int mcv_match_expression ( Node expr,
Bitmapset keys,
List exprs,
Oid collid 
)
static

Definition at line 1539 of file mcv.c.

1540 {
1541  int idx;
1542 
1543  if (IsA(expr, Var))
1544  {
1545  /* simple Var, so just lookup using varattno */
1546  Var *var = (Var *) expr;
1547 
1548  if (collid)
1549  *collid = var->varcollid;
1550 
1551  idx = bms_member_index(keys, var->varattno);
1552 
1553  if (idx < 0)
1554  elog(ERROR, "variable not found in statistics object");
1555  }
1556  else
1557  {
1558  /* expression - lookup in stats expressions */
1559  ListCell *lc;
1560 
1561  if (collid)
1562  *collid = exprCollation(expr);
1563 
1564  /* expressions are stored after the simple columns */
1565  idx = bms_num_members(keys);
1566  foreach(lc, exprs)
1567  {
1568  Node *stat_expr = (Node *) lfirst(lc);
1569 
1570  if (equal(expr, stat_expr))
1571  break;
1572 
1573  idx++;
1574  }
1575 
1576  if (lc == NULL)
1577  elog(ERROR, "expression not found in statistics object");
1578  }
1579 
1580  return idx;
1581 }
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:685
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:786

References bms_member_index(), bms_num_members(), collid, elog(), equal(), ERROR, exprCollation(), idx(), IsA, lfirst, and Var::varattno.

Referenced by mcv_get_match_bitmap().

◆ pg_mcv_list_in()

Datum pg_mcv_list_in ( PG_FUNCTION_ARGS  )

Definition at line 1476 of file mcv.c.

1477 {
1478  /*
1479  * pg_mcv_list stores the data in binary form and parsing text input is
1480  * not needed, so disallow this.
1481  */
1482  ereport(ERROR,
1483  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1484  errmsg("cannot accept a value of type %s", "pg_mcv_list")));
1485 
1486  PG_RETURN_VOID(); /* keep compiler quiet */
1487 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_VOID()
Definition: fmgr.h:349

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_VOID.

◆ pg_mcv_list_out()

Datum pg_mcv_list_out ( PG_FUNCTION_ARGS  )

Definition at line 1502 of file mcv.c.

1503 {
1504  return byteaout(fcinfo);
1505 }
Datum byteaout(PG_FUNCTION_ARGS)
Definition: varlena.c:386

References byteaout().

◆ pg_mcv_list_recv()

Datum pg_mcv_list_recv ( PG_FUNCTION_ARGS  )

Definition at line 1511 of file mcv.c.

1512 {
1513  ereport(ERROR,
1514  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1515  errmsg("cannot accept a value of type %s", "pg_mcv_list")));
1516 
1517  PG_RETURN_VOID(); /* keep compiler quiet */
1518 }

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_VOID.

◆ pg_mcv_list_send()

Datum pg_mcv_list_send ( PG_FUNCTION_ARGS  )

Definition at line 1527 of file mcv.c.

1528 {
1529  return byteasend(fcinfo);
1530 }
Datum byteasend(PG_FUNCTION_ARGS)
Definition: varlena.c:488

References byteasend().

◆ pg_stats_ext_mcvlist_items()

Datum pg_stats_ext_mcvlist_items ( PG_FUNCTION_ARGS  )

Definition at line 1342 of file mcv.c.

1343 {
1344  FuncCallContext *funcctx;
1345 
1346  /* stuff done only on the first call of the function */
1347  if (SRF_IS_FIRSTCALL())
1348  {
1349  MemoryContext oldcontext;
1350  MCVList *mcvlist;
1351  TupleDesc tupdesc;
1352 
1353  /* create a function context for cross-call persistence */
1354  funcctx = SRF_FIRSTCALL_INIT();
1355 
1356  /* switch to memory context appropriate for multiple function calls */
1357  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1358 
1360 
1361  funcctx->user_fctx = mcvlist;
1362 
1363  /* total number of tuples to be returned */
1364  funcctx->max_calls = 0;
1365  if (funcctx->user_fctx != NULL)
1366  funcctx->max_calls = mcvlist->nitems;
1367 
1368  /* Build a tuple descriptor for our result type */
1369  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1370  ereport(ERROR,
1371  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1372  errmsg("function returning record called in context "
1373  "that cannot accept type record")));
1374  tupdesc = BlessTupleDesc(tupdesc);
1375 
1376  /*
1377  * generate attribute metadata needed later to produce tuples from raw
1378  * C strings
1379  */
1380  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
1381 
1382  MemoryContextSwitchTo(oldcontext);
1383  }
1384 
1385  /* stuff done on every call of the function */
1386  funcctx = SRF_PERCALL_SETUP();
1387 
1388  if (funcctx->call_cntr < funcctx->max_calls) /* do when there is more
1389  * left to send */
1390  {
1391  Datum values[5];
1392  bool nulls[5];
1393  HeapTuple tuple;
1394  Datum result;
1395  ArrayBuildState *astate_values = NULL;
1396  ArrayBuildState *astate_nulls = NULL;
1397 
1398  int i;
1399  MCVList *mcvlist;
1400  MCVItem *item;
1401 
1402  mcvlist = (MCVList *) funcctx->user_fctx;
1403 
1404  Assert(funcctx->call_cntr < mcvlist->nitems);
1405 
1406  item = &mcvlist->items[funcctx->call_cntr];
1407 
1408  for (i = 0; i < mcvlist->ndimensions; i++)
1409  {
1410 
1411  astate_nulls = accumArrayResult(astate_nulls,
1412  BoolGetDatum(item->isnull[i]),
1413  false,
1414  BOOLOID,
1416 
1417  if (!item->isnull[i])
1418  {
1419  bool isvarlena;
1420  Oid outfunc;
1421  FmgrInfo fmgrinfo;
1422  Datum val;
1423  text *txt;
1424 
1425  /* lookup output func for the type */
1426  getTypeOutputInfo(mcvlist->types[i], &outfunc, &isvarlena);
1427  fmgr_info(outfunc, &fmgrinfo);
1428 
1429  val = FunctionCall1(&fmgrinfo, item->values[i]);
1431 
1432  astate_values = accumArrayResult(astate_values,
1433  PointerGetDatum(txt),
1434  false,
1435  TEXTOID,
1437  }
1438  else
1439  astate_values = accumArrayResult(astate_values,
1440  (Datum) 0,
1441  true,
1442  TEXTOID,
1444  }
1445 
1446  values[0] = Int32GetDatum(funcctx->call_cntr);
1447  values[1] = makeArrayResult(astate_values, CurrentMemoryContext);
1448  values[2] = makeArrayResult(astate_nulls, CurrentMemoryContext);
1449  values[3] = Float8GetDatum(item->frequency);
1450  values[4] = Float8GetDatum(item->base_frequency);
1451 
1452  /* no NULLs in the tuple */
1453  memset(nulls, 0, sizeof(nulls));
1454 
1455  /* build a tuple */
1456  tuple = heap_form_tuple(funcctx->attinmeta->tupdesc, values, nulls);
1457 
1458  /* make the tuple into a datum */
1459  result = HeapTupleGetDatum(tuple);
1460 
1461  SRF_RETURN_NEXT(funcctx, result);
1462  }
1463  else /* do when there is no more left */
1464  {
1465  SRF_RETURN_DONE(funcctx);
1466  }
1467 }
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5297
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5367
static Datum values[MAXATTR]
Definition: bootstrap.c:156
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2072
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2087
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1799
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:660
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:335
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1108
long val
Definition: informix.c:664
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2889
MCVList * statext_mcv_deserialize(bytea *data)
Definition: mcv.c:1000
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
TupleDesc tupdesc
Definition: funcapi.h:38
void * user_fctx
Definition: funcapi.h:82
uint64 max_calls
Definition: funcapi.h:74
uint64 call_cntr
Definition: funcapi.h:65
AttInMetadata * attinmeta
Definition: funcapi.h:91
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
AttrNumber ndimensions
Definition: statistics.h:92
Oid types[STATS_MAX_DIMENSIONS]
Definition: statistics.h:93
Definition: c.h:676
text * cstring_to_text(const char *s)
Definition: varlena.c:182

References accumArrayResult(), Assert(), FuncCallContext::attinmeta, MCVItem::base_frequency, BlessTupleDesc(), BoolGetDatum(), FuncCallContext::call_cntr, cstring_to_text(), CurrentMemoryContext, DatumGetPointer(), ereport, errcode(), errmsg(), ERROR, Float8GetDatum(), fmgr_info(), MCVItem::frequency, FunctionCall1, get_call_result_type(), getTypeOutputInfo(), heap_form_tuple(), HeapTupleGetDatum(), i, Int32GetDatum(), MCVItem::isnull, MCVList::items, makeArrayResult(), FuncCallContext::max_calls, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, MCVList::ndimensions, MCVList::nitems, PG_GETARG_BYTEA_P, PointerGetDatum(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, statext_mcv_deserialize(), AttInMetadata::tupdesc, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, MCVList::types, FuncCallContext::user_fctx, val, values, and MCVItem::values.

◆ sort_item_compare()

static int sort_item_compare ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 469 of file mcv.c.

470 {
471  SortSupport ssup = (SortSupport) arg;
472  SortItem *ia = (SortItem *) a;
473  SortItem *ib = (SortItem *) b;
474 
475  return ApplySortComparator(ia->values[0], ia->isnull[0],
476  ib->values[0], ib->isnull[0],
477  ssup);
478 }
void * arg
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200

References a, ApplySortComparator(), arg, b, SortItem::isnull, and SortItem::values.

Referenced by build_column_frequencies().

◆ statext_mcv_build()

MCVList* statext_mcv_build ( StatsBuildData data,
double  totalrows,
int  stattarget 
)

Definition at line 184 of file mcv.c.

185 {
186  int i,
187  numattrs,
188  numrows,
189  ngroups,
190  nitems;
191  double mincount;
192  SortItem *items;
193  SortItem *groups;
194  MCVList *mcvlist = NULL;
195  MultiSortSupport mss;
196 
197  /* comparator for all the columns */
198  mss = build_mss(data);
199 
200  /* sort the rows */
201  items = build_sorted_items(data, &nitems, mss,
202  data->nattnums, data->attnums);
203 
204  if (!items)
205  return NULL;
206 
207  /* for convenience */
208  numattrs = data->nattnums;
209  numrows = data->numrows;
210 
211  /* transform the sorted rows into groups (sorted by frequency) */
212  groups = build_distinct_groups(nitems, items, mss, &ngroups);
213 
214  /*
215  * The maximum number of MCV items to store, based on the statistics
216  * target we computed for the statistics object (from the target set for
217  * the object itself, attributes and the system default). In any case, we
218  * can't keep more groups than we have available.
219  */
220  nitems = stattarget;
221  if (nitems > ngroups)
222  nitems = ngroups;
223 
224  /*
225  * Decide how many items to keep in the MCV list. We can't use the same
226  * algorithm as per-column MCV lists, because that only considers the
227  * actual group frequency - but we're primarily interested in how the
228  * actual frequency differs from the base frequency (product of simple
229  * per-column frequencies, as if the columns were independent).
230  *
231  * Using the same algorithm might exclude items that are close to the
232  * "average" frequency of the sample. But that does not say whether the
233  * observed frequency is close to the base frequency or not. We also need
234  * to consider unexpectedly uncommon items (again, compared to the base
235  * frequency), and the single-column algorithm does not have to.
236  *
237  * We simply decide how many items to keep by computing the minimum count
238  * using get_mincount_for_mcv_list() and then keep all items that seem to
239  * be more common than that.
240  */
241  mincount = get_mincount_for_mcv_list(numrows, totalrows);
242 
243  /*
244  * Walk the groups until we find the first group with a count below the
245  * mincount threshold (the index of that group is the number of groups we
246  * want to keep).
247  */
248  for (i = 0; i < nitems; i++)
249  {
250  if (groups[i].count < mincount)
251  {
252  nitems = i;
253  break;
254  }
255  }
256 
257  /*
258  * At this point, we know the number of items for the MCV list. There
259  * might be none (for uniform distribution with many groups), and in that
260  * case, there will be no MCV list. Otherwise, construct the MCV list.
261  */
262  if (nitems > 0)
263  {
264  int j;
265  SortItem key;
266  MultiSortSupport tmp;
267 
268  /* frequencies for values in each attribute */
269  SortItem **freqs;
270  int *nfreqs;
271 
272  /* used to search values */
273  tmp = (MultiSortSupport) palloc(offsetof(MultiSortSupportData, ssup)
274  + sizeof(SortSupportData));
275 
276  /* compute frequencies for values in each column */
277  nfreqs = (int *) palloc0(sizeof(int) * numattrs);
278  freqs = build_column_frequencies(groups, ngroups, mss, nfreqs);
279 
280  /*
281  * Allocate the MCV list structure, set the global parameters.
282  */
283  mcvlist = (MCVList *) palloc0(offsetof(MCVList, items) +
284  sizeof(MCVItem) * nitems);
285 
286  mcvlist->magic = STATS_MCV_MAGIC;
287  mcvlist->type = STATS_MCV_TYPE_BASIC;
288  mcvlist->ndimensions = numattrs;
289  mcvlist->nitems = nitems;
290 
291  /* store info about data type OIDs */
292  for (i = 0; i < numattrs; i++)
293  mcvlist->types[i] = data->stats[i]->attrtypid;
294 
295  /* Copy the first chunk of groups into the result. */
296  for (i = 0; i < nitems; i++)
297  {
298  /* just point to the proper place in the list */
299  MCVItem *item = &mcvlist->items[i];
300 
301  item->values = (Datum *) palloc(sizeof(Datum) * numattrs);
302  item->isnull = (bool *) palloc(sizeof(bool) * numattrs);
303 
304  /* copy values for the group */
305  memcpy(item->values, groups[i].values, sizeof(Datum) * numattrs);
306  memcpy(item->isnull, groups[i].isnull, sizeof(bool) * numattrs);
307 
308  /* groups should be sorted by frequency in descending order */
309  Assert((i == 0) || (groups[i - 1].count >= groups[i].count));
310 
311  /* group frequency */
312  item->frequency = (double) groups[i].count / numrows;
313 
314  /* base frequency, if the attributes were independent */
315  item->base_frequency = 1.0;
316  for (j = 0; j < numattrs; j++)
317  {
318  SortItem *freq;
319 
320  /* single dimension */
321  tmp->ndims = 1;
322  tmp->ssup[0] = mss->ssup[j];
323 
324  /* fill search key */
325  key.values = &groups[i].values[j];
326  key.isnull = &groups[i].isnull[j];
327 
328  freq = (SortItem *) bsearch_arg(&key, freqs[j], nfreqs[j],
329  sizeof(SortItem),
330  multi_sort_compare, tmp);
331 
332  item->base_frequency *= ((double) freq->count) / numrows;
333  }
334  }
335 
336  pfree(nfreqs);
337  pfree(freqs);
338  }
339 
340  pfree(items);
341  pfree(groups);
342 
343  return mcvlist;
344 }
SortItem * build_sorted_items(StatsBuildData *data, int *nitems, MultiSortSupport mss, int numattrs, AttrNumber *attnums)
MultiSortSupportData * MultiSortSupport
static MultiSortSupport build_mss(StatsBuildData *data)
Definition: mcv.c:351
static double get_mincount_for_mcv_list(int samplerows, double totalrows)
Definition: mcv.c:152
static SortItem ** build_column_frequencies(SortItem *groups, int ngroups, MultiSortSupport mss, int *ncounts)
Definition: mcv.c:494
static SortItem * build_distinct_groups(int numrows, SortItem *items, MultiSortSupport mss, int *ndistinct)
Definition: mcv.c:428
void * bsearch_arg(const void *key, const void *base0, size_t nmemb, size_t size, int(*compar)(const void *, const void *, void *), void *arg)
Definition: bsearch_arg.c:55
struct SortSupportData SortSupportData
#define STATS_MCV_TYPE_BASIC
Definition: statistics.h:67
#define STATS_MCV_MAGIC
Definition: statistics.h:66
struct MCVItem MCVItem
uint32 type
Definition: statistics.h:90
uint32 magic
Definition: statistics.h:89

References Assert(), MCVItem::base_frequency, bsearch_arg(), build_column_frequencies(), build_distinct_groups(), build_mss(), build_sorted_items(), SortItem::count, data, MCVItem::frequency, get_mincount_for_mcv_list(), i, SortItem::isnull, MCVItem::isnull, MCVList::items, j, sort-test::key, MCVList::magic, multi_sort_compare(), MCVList::ndimensions, MultiSortSupportData::ndims, MCVList::nitems, nitems, palloc(), palloc0(), pfree(), MultiSortSupportData::ssup, STATS_MCV_MAGIC, STATS_MCV_TYPE_BASIC, MCVList::type, MCVList::types, SortItem::values, and MCVItem::values.

Referenced by BuildRelationExtStatistics().

◆ statext_mcv_deserialize()

MCVList* statext_mcv_deserialize ( bytea data)

Definition at line 1000 of file mcv.c.

1001 {
1002  int dim,
1003  i;
1004  Size expected_size;
1005  MCVList *mcvlist;
1006  char *raw;
1007  char *ptr;
1008  char *endptr PG_USED_FOR_ASSERTS_ONLY;
1009 
1010  int ndims,
1011  nitems;
1012  DimensionInfo *info = NULL;
1013 
1014  /* local allocation buffer (used only for deserialization) */
1015  Datum **map = NULL;
1016 
1017  /* MCV list */
1018  Size mcvlen;
1019 
1020  /* buffer used for the result */
1021  Size datalen;
1022  char *dataptr;
1023  char *valuesptr;
1024  char *isnullptr;
1025 
1026  if (data == NULL)
1027  return NULL;
1028 
1029  /*
1030  * We can't possibly deserialize a MCV list if there's not even a complete
1031  * header. We need an explicit formula here, because we serialize the
1032  * header fields one by one, so we need to ignore struct alignment.
1033  */
1035  elog(ERROR, "invalid MCV size %zu (expected at least %zu)",
1037 
1038  /* read the MCV list header */
1039  mcvlist = (MCVList *) palloc0(offsetof(MCVList, items));
1040 
1041  /* pointer to the data part (skip the varlena header) */
1042  raw = (char *) data;
1043  ptr = VARDATA_ANY(raw);
1044  endptr = (char *) raw + VARSIZE_ANY(data);
1045 
1046  /* get the header and perform further sanity checks */
1047  memcpy(&mcvlist->magic, ptr, sizeof(uint32));
1048  ptr += sizeof(uint32);
1049 
1050  memcpy(&mcvlist->type, ptr, sizeof(uint32));
1051  ptr += sizeof(uint32);
1052 
1053  memcpy(&mcvlist->nitems, ptr, sizeof(uint32));
1054  ptr += sizeof(uint32);
1055 
1056  memcpy(&mcvlist->ndimensions, ptr, sizeof(AttrNumber));
1057  ptr += sizeof(AttrNumber);
1058 
1059  if (mcvlist->magic != STATS_MCV_MAGIC)
1060  elog(ERROR, "invalid MCV magic %u (expected %u)",
1061  mcvlist->magic, STATS_MCV_MAGIC);
1062 
1063  if (mcvlist->type != STATS_MCV_TYPE_BASIC)
1064  elog(ERROR, "invalid MCV type %u (expected %u)",
1065  mcvlist->type, STATS_MCV_TYPE_BASIC);
1066 
1067  if (mcvlist->ndimensions == 0)
1068  elog(ERROR, "invalid zero-length dimension array in MCVList");
1069  else if ((mcvlist->ndimensions > STATS_MAX_DIMENSIONS) ||
1070  (mcvlist->ndimensions < 0))
1071  elog(ERROR, "invalid length (%d) dimension array in MCVList",
1072  mcvlist->ndimensions);
1073 
1074  if (mcvlist->nitems == 0)
1075  elog(ERROR, "invalid zero-length item array in MCVList");
1076  else if (mcvlist->nitems > STATS_MCVLIST_MAX_ITEMS)
1077  elog(ERROR, "invalid length (%u) item array in MCVList",
1078  mcvlist->nitems);
1079 
1080  nitems = mcvlist->nitems;
1081  ndims = mcvlist->ndimensions;
1082 
1083  /*
1084  * Check amount of data including DimensionInfo for all dimensions and
1085  * also the serialized items (including uint16 indexes). Also, walk
1086  * through the dimension information and add it to the sum.
1087  */
1088  expected_size = SizeOfMCVList(ndims, nitems);
1089 
1090  /*
1091  * Check that we have at least the dimension and info records, along with
1092  * the items. We don't know the size of the serialized values yet. We need
1093  * to do this check first, before accessing the dimension info.
1094  */
1095  if (VARSIZE_ANY(data) < expected_size)
1096  elog(ERROR, "invalid MCV size %zu (expected %zu)",
1097  VARSIZE_ANY(data), expected_size);
1098 
1099  /* Now copy the array of type Oids. */
1100  memcpy(mcvlist->types, ptr, sizeof(Oid) * ndims);
1101  ptr += (sizeof(Oid) * ndims);
1102 
1103  /* Now it's safe to access the dimension info. */
1104  info = palloc(ndims * sizeof(DimensionInfo));
1105 
1106  memcpy(info, ptr, ndims * sizeof(DimensionInfo));
1107  ptr += (ndims * sizeof(DimensionInfo));
1108 
1109  /* account for the value arrays */
1110  for (dim = 0; dim < ndims; dim++)
1111  {
1112  /*
1113  * XXX I wonder if we can/should rely on asserts here. Maybe those
1114  * checks should be done every time?
1115  */
1116  Assert(info[dim].nvalues >= 0);
1117  Assert(info[dim].nbytes >= 0);
1118 
1119  expected_size += info[dim].nbytes;
1120  }
1121 
1122  /*
1123  * Now we know the total expected MCV size, including all the pieces
1124  * (header, dimension info. items and deduplicated data). So do the final
1125  * check on size.
1126  */
1127  if (VARSIZE_ANY(data) != expected_size)
1128  elog(ERROR, "invalid MCV size %zu (expected %zu)",
1129  VARSIZE_ANY(data), expected_size);
1130 
1131  /*
1132  * We need an array of Datum values for each dimension, so that we can
1133  * easily translate the uint16 indexes later. We also need a top-level
1134  * array of pointers to those per-dimension arrays.
1135  *
1136  * While allocating the arrays for dimensions, compute how much space we
1137  * need for a copy of the by-ref data, as we can't simply point to the
1138  * original values (it might go away).
1139  */
1140  datalen = 0; /* space for by-ref data */
1141  map = (Datum **) palloc(ndims * sizeof(Datum *));
1142 
1143  for (dim = 0; dim < ndims; dim++)
1144  {
1145  map[dim] = (Datum *) palloc(sizeof(Datum) * info[dim].nvalues);
1146 
1147  /* space needed for a copy of data for by-ref types */
1148  datalen += info[dim].nbytes_aligned;
1149  }
1150 
1151  /*
1152  * Now resize the MCV list so that the allocation includes all the data.
1153  *
1154  * Allocate space for a copy of the data, as we can't simply reference the
1155  * serialized data - it's not aligned properly, and it may disappear while
1156  * we're still using the MCV list, e.g. due to catcache release.
1157  *
1158  * We do care about alignment here, because we will allocate all the
1159  * pieces at once, but then use pointers to different parts.
1160  */
1161  mcvlen = MAXALIGN(offsetof(MCVList, items) + (sizeof(MCVItem) * nitems));
1162 
1163  /* arrays of values and isnull flags for all MCV items */
1164  mcvlen += nitems * MAXALIGN(sizeof(Datum) * ndims);
1165  mcvlen += nitems * MAXALIGN(sizeof(bool) * ndims);
1166 
1167  /* we don't quite need to align this, but it makes some asserts easier */
1168  mcvlen += MAXALIGN(datalen);
1169 
1170  /* now resize the deserialized MCV list, and compute pointers to parts */
1171  mcvlist = repalloc(mcvlist, mcvlen);
1172 
1173  /* pointer to the beginning of values/isnull arrays */
1174  valuesptr = (char *) mcvlist
1175  + MAXALIGN(offsetof(MCVList, items) + (sizeof(MCVItem) * nitems));
1176 
1177  isnullptr = valuesptr + (nitems * MAXALIGN(sizeof(Datum) * ndims));
1178 
1179  dataptr = isnullptr + (nitems * MAXALIGN(sizeof(bool) * ndims));
1180 
1181  /*
1182  * Build mapping (index => value) for translating the serialized data into
1183  * the in-memory representation.
1184  */
1185  for (dim = 0; dim < ndims; dim++)
1186  {
1187  /* remember start position in the input array */
1188  char *start PG_USED_FOR_ASSERTS_ONLY = ptr;
1189 
1190  if (info[dim].typbyval)
1191  {
1192  /* for by-val types we simply copy data into the mapping */
1193  for (i = 0; i < info[dim].nvalues; i++)
1194  {
1195  Datum v = 0;
1196 
1197  memcpy(&v, ptr, info[dim].typlen);
1198  ptr += info[dim].typlen;
1199 
1200  map[dim][i] = fetch_att(&v, true, info[dim].typlen);
1201 
1202  /* no under/overflow of input array */
1203  Assert(ptr <= (start + info[dim].nbytes));
1204  }
1205  }
1206  else
1207  {
1208  /* for by-ref types we need to also make a copy of the data */
1209 
1210  /* passed by reference, but fixed length (name, tid, ...) */
1211  if (info[dim].typlen > 0)
1212  {
1213  for (i = 0; i < info[dim].nvalues; i++)
1214  {
1215  memcpy(dataptr, ptr, info[dim].typlen);
1216  ptr += info[dim].typlen;
1217 
1218  /* just point into the array */
1219  map[dim][i] = PointerGetDatum(dataptr);
1220  dataptr += MAXALIGN(info[dim].typlen);
1221  }
1222  }
1223  else if (info[dim].typlen == -1)
1224  {
1225  /* varlena */
1226  for (i = 0; i < info[dim].nvalues; i++)
1227  {
1228  uint32 len;
1229 
1230  /* read the uint32 length */
1231  memcpy(&len, ptr, sizeof(uint32));
1232  ptr += sizeof(uint32);
1233 
1234  /* the length is data-only */
1235  SET_VARSIZE(dataptr, len + VARHDRSZ);
1236  memcpy(VARDATA(dataptr), ptr, len);
1237  ptr += len;
1238 
1239  /* just point into the array */
1240  map[dim][i] = PointerGetDatum(dataptr);
1241 
1242  /* skip to place of the next deserialized value */
1243  dataptr += MAXALIGN(len + VARHDRSZ);
1244  }
1245  }
1246  else if (info[dim].typlen == -2)
1247  {
1248  /* cstring */
1249  for (i = 0; i < info[dim].nvalues; i++)
1250  {
1251  uint32 len;
1252 
1253  memcpy(&len, ptr, sizeof(uint32));
1254  ptr += sizeof(uint32);
1255 
1256  memcpy(dataptr, ptr, len);
1257  ptr += len;
1258 
1259  /* just point into the array */
1260  map[dim][i] = PointerGetDatum(dataptr);
1261  dataptr += MAXALIGN(len);
1262  }
1263  }
1264 
1265  /* no under/overflow of input array */
1266  Assert(ptr <= (start + info[dim].nbytes));
1267 
1268  /* no overflow of the output mcv value */
1269  Assert(dataptr <= ((char *) mcvlist + mcvlen));
1270  }
1271 
1272  /* check we consumed input data for this dimension exactly */
1273  Assert(ptr == (start + info[dim].nbytes));
1274  }
1275 
1276  /* we should have also filled the MCV list exactly */
1277  Assert(dataptr == ((char *) mcvlist + mcvlen));
1278 
1279  /* deserialize the MCV items and translate the indexes to Datums */
1280  for (i = 0; i < nitems; i++)
1281  {
1282  MCVItem *item = &mcvlist->items[i];
1283 
1284  item->values = (Datum *) valuesptr;
1285  valuesptr += MAXALIGN(sizeof(Datum) * ndims);
1286 
1287  item->isnull = (bool *) isnullptr;
1288  isnullptr += MAXALIGN(sizeof(bool) * ndims);
1289 
1290  memcpy(item->isnull, ptr, sizeof(bool) * ndims);
1291  ptr += sizeof(bool) * ndims;
1292 
1293  memcpy(&item->frequency, ptr, sizeof(double));
1294  ptr += sizeof(double);
1295 
1296  memcpy(&item->base_frequency, ptr, sizeof(double));
1297  ptr += sizeof(double);
1298 
1299  /* finally translate the indexes (for non-NULL only) */
1300  for (dim = 0; dim < ndims; dim++)
1301  {
1302  uint16 index;
1303 
1304  memcpy(&index, ptr, sizeof(uint16));
1305  ptr += sizeof(uint16);
1306 
1307  if (item->isnull[dim])
1308  continue;
1309 
1310  item->values[dim] = map[dim][index];
1311  }
1312 
1313  /* check we're not overflowing the input */
1314  Assert(ptr <= endptr);
1315  }
1316 
1317  /* check that we processed all the data */
1318  Assert(ptr == endptr);
1319 
1320  /* release the buffers used for mapping */
1321  for (dim = 0; dim < ndims; dim++)
1322  pfree(map[dim]);
1323 
1324  pfree(map);
1325 
1326  return mcvlist;
1327 }
int16 AttrNumber
Definition: attnum.h:21
unsigned short uint16
Definition: c.h:494
unsigned int uint32
Definition: c.h:495
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:171
#define VARHDRSZ
Definition: c.h:681
unsigned char bool
Definition: c.h:445
size_t Size
Definition: c.h:594
#define SizeOfMCVList(ndims, nitems)
Definition: mcv.c:72
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1476
const void size_t len
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19
Definition: type.h:95
static Datum fetch_att(const void *T, bool attbyval, int attlen)
Definition: tupmacs.h:52
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References Assert(), MCVItem::base_frequency, data, elog(), ERROR, fetch_att(), MCVItem::frequency, i, MCVItem::isnull, MCVList::items, len, MCVList::magic, MAXALIGN, MinSizeOfMCVList, DimensionInfo::nbytes, DimensionInfo::nbytes_aligned, MCVList::ndimensions, MCVList::nitems, nitems, DimensionInfo::nvalues, palloc(), palloc0(), pfree(), PG_USED_FOR_ASSERTS_ONLY, PointerGetDatum(), repalloc(), SET_VARSIZE, SizeOfMCVList, STATS_MAX_DIMENSIONS, STATS_MCV_MAGIC, STATS_MCV_TYPE_BASIC, STATS_MCVLIST_MAX_ITEMS, MCVList::type, MCVList::types, DimensionInfo::typlen, MCVItem::values, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY.

Referenced by pg_stats_ext_mcvlist_items(), and statext_mcv_load().

◆ statext_mcv_load()

MCVList* statext_mcv_load ( Oid  mvoid,
bool  inh 
)

Definition at line 562 of file mcv.c.

563 {
564  MCVList *result;
565  bool isnull;
566  Datum mcvlist;
568  ObjectIdGetDatum(mvoid), BoolGetDatum(inh));
569 
570  if (!HeapTupleIsValid(htup))
571  elog(ERROR, "cache lookup failed for statistics object %u", mvoid);
572 
573  mcvlist = SysCacheGetAttr(STATEXTDATASTXOID, htup,
574  Anum_pg_statistic_ext_data_stxdmcv, &isnull);
575 
576  if (isnull)
577  elog(ERROR,
578  "requested statistics kind \"%c\" is not yet built for statistics object %u",
579  STATS_EXT_DEPENDENCIES, mvoid);
580 
581  result = statext_mcv_deserialize(DatumGetByteaP(mcvlist));
582 
583  ReleaseSysCache(htup);
584 
585  return result;
586 }
#define DatumGetByteaP(X)
Definition: fmgr.h:331
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1081
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:831
@ STATEXTDATASTXOID
Definition: syscache.h:94

References BoolGetDatum(), DatumGetByteaP, elog(), ERROR, HeapTupleIsValid, ObjectIdGetDatum(), ReleaseSysCache(), SearchSysCache2(), statext_mcv_deserialize(), STATEXTDATASTXOID, and SysCacheGetAttr().

Referenced by mcv_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

◆ statext_mcv_serialize()

bytea* statext_mcv_serialize ( MCVList mcvlist,
VacAttrStats **  stats 
)

Definition at line 625 of file mcv.c.

626 {
627  int i;
628  int dim;
629  int ndims = mcvlist->ndimensions;
630 
631  SortSupport ssup;
632  DimensionInfo *info;
633 
634  Size total_length;
635 
636  /* serialized items (indexes into arrays, etc.) */
637  bytea *raw;
638  char *ptr;
639  char *endptr PG_USED_FOR_ASSERTS_ONLY;
640 
641  /* values per dimension (and number of non-NULL values) */
642  Datum **values = (Datum **) palloc0(sizeof(Datum *) * ndims);
643  int *counts = (int *) palloc0(sizeof(int) * ndims);
644 
645  /*
646  * We'll include some rudimentary information about the attribute types
647  * (length, by-val flag), so that we don't have to look them up while
648  * deserializing the MCV list (we already have the type OID in the
649  * header). This is safe because when changing the type of the attribute
650  * the statistics gets dropped automatically. We need to store the info
651  * about the arrays of deduplicated values anyway.
652  */
653  info = (DimensionInfo *) palloc0(sizeof(DimensionInfo) * ndims);
654 
655  /* sort support data for all attributes included in the MCV list */
656  ssup = (SortSupport) palloc0(sizeof(SortSupportData) * ndims);
657 
658  /* collect and deduplicate values for each dimension (attribute) */
659  for (dim = 0; dim < ndims; dim++)
660  {
661  int ndistinct;
662  TypeCacheEntry *typentry;
663 
664  /*
665  * Lookup the LT operator (can't get it from stats extra_data, as we
666  * don't know how to interpret that - scalar vs. array etc.).
667  */
668  typentry = lookup_type_cache(stats[dim]->attrtypid, TYPECACHE_LT_OPR);
669 
670  /* copy important info about the data type (length, by-value) */
671  info[dim].typlen = stats[dim]->attrtype->typlen;
672  info[dim].typbyval = stats[dim]->attrtype->typbyval;
673 
674  /* allocate space for values in the attribute and collect them */
675  values[dim] = (Datum *) palloc0(sizeof(Datum) * mcvlist->nitems);
676 
677  for (i = 0; i < mcvlist->nitems; i++)
678  {
679  /* skip NULL values - we don't need to deduplicate those */
680  if (mcvlist->items[i].isnull[dim])
681  continue;
682 
683  /* append the value at the end */
684  values[dim][counts[dim]] = mcvlist->items[i].values[dim];
685  counts[dim] += 1;
686  }
687 
688  /* if there are just NULL values in this dimension, we're done */
689  if (counts[dim] == 0)
690  continue;
691 
692  /* sort and deduplicate the data */
693  ssup[dim].ssup_cxt = CurrentMemoryContext;
694  ssup[dim].ssup_collation = stats[dim]->attrcollid;
695  ssup[dim].ssup_nulls_first = false;
696 
697  PrepareSortSupportFromOrderingOp(typentry->lt_opr, &ssup[dim]);
698 
699  qsort_interruptible(values[dim], counts[dim], sizeof(Datum),
700  compare_scalars_simple, &ssup[dim]);
701 
702  /*
703  * Walk through the array and eliminate duplicate values, but keep the
704  * ordering (so that we can do a binary search later). We know there's
705  * at least one item as (counts[dim] != 0), so we can skip the first
706  * element.
707  */
708  ndistinct = 1; /* number of distinct values */
709  for (i = 1; i < counts[dim]; i++)
710  {
711  /* expect sorted array */
712  Assert(compare_datums_simple(values[dim][i - 1], values[dim][i], &ssup[dim]) <= 0);
713 
714  /* if the value is the same as the previous one, we can skip it */
715  if (!compare_datums_simple(values[dim][i - 1], values[dim][i], &ssup[dim]))
716  continue;
717 
718  values[dim][ndistinct] = values[dim][i];
719  ndistinct += 1;
720  }
721 
722  /* we must not exceed PG_UINT16_MAX, as we use uint16 indexes */
723  Assert(ndistinct <= PG_UINT16_MAX);
724 
725  /*
726  * Store additional info about the attribute - number of deduplicated
727  * values, and also size of the serialized data. For fixed-length data
728  * types this is trivial to compute, for varwidth types we need to
729  * actually walk the array and sum the sizes.
730  */
731  info[dim].nvalues = ndistinct;
732 
733  if (info[dim].typbyval) /* by-value data types */
734  {
735  info[dim].nbytes = info[dim].nvalues * info[dim].typlen;
736 
737  /*
738  * We copy the data into the MCV item during deserialization, so
739  * we don't need to allocate any extra space.
740  */
741  info[dim].nbytes_aligned = 0;
742  }
743  else if (info[dim].typlen > 0) /* fixed-length by-ref */
744  {
745  /*
746  * We don't care about alignment in the serialized data, so we
747  * pack the data as much as possible. But we also track how much
748  * data will be needed after deserialization, and in that case we
749  * need to account for alignment of each item.
750  *
751  * Note: As the items are fixed-length, we could easily compute
752  * this during deserialization, but we do it here anyway.
753  */
754  info[dim].nbytes = info[dim].nvalues * info[dim].typlen;
755  info[dim].nbytes_aligned = info[dim].nvalues * MAXALIGN(info[dim].typlen);
756  }
757  else if (info[dim].typlen == -1) /* varlena */
758  {
759  info[dim].nbytes = 0;
760  info[dim].nbytes_aligned = 0;
761  for (i = 0; i < info[dim].nvalues; i++)
762  {
763  Size len;
764 
765  /*
766  * For varlena values, we detoast the values and store the
767  * length and data separately. We don't bother with alignment
768  * here, which means that during deserialization we need to
769  * copy the fields and only access the copies.
770  */
772 
773  /* serialized length (uint32 length + data) */
774  len = VARSIZE_ANY_EXHDR(values[dim][i]);
775  info[dim].nbytes += sizeof(uint32); /* length */
776  info[dim].nbytes += len; /* value (no header) */
777 
778  /*
779  * During deserialization we'll build regular varlena values
780  * with full headers, and we need to align them properly.
781  */
782  info[dim].nbytes_aligned += MAXALIGN(VARHDRSZ + len);
783  }
784  }
785  else if (info[dim].typlen == -2) /* cstring */
786  {
787  info[dim].nbytes = 0;
788  info[dim].nbytes_aligned = 0;
789  for (i = 0; i < info[dim].nvalues; i++)
790  {
791  Size len;
792 
793  /*
794  * cstring is handled similar to varlena - first we store the
795  * length as uint32 and then the data. We don't care about
796  * alignment, which means that during deserialization we need
797  * to copy the fields and only access the copies.
798  */
799 
800  /* c-strings include terminator, so +1 byte */
801  len = strlen(DatumGetCString(values[dim][i])) + 1;
802  info[dim].nbytes += sizeof(uint32); /* length */
803  info[dim].nbytes += len; /* value */
804 
805  /* space needed for properly aligned deserialized copies */
806  info[dim].nbytes_aligned += MAXALIGN(len);
807  }
808  }
809 
810  /* we know (count>0) so there must be some data */
811  Assert(info[dim].nbytes > 0);
812  }
813 
814  /*
815  * Now we can finally compute how much space we'll actually need for the
816  * whole serialized MCV list (varlena header, MCV header, dimension info
817  * for each attribute, deduplicated values and items).
818  */
819  total_length = (3 * sizeof(uint32)) /* magic + type + nitems */
820  + sizeof(AttrNumber) /* ndimensions */
821  + (ndims * sizeof(Oid)); /* attribute types */
822 
823  /* dimension info */
824  total_length += ndims * sizeof(DimensionInfo);
825 
826  /* add space for the arrays of deduplicated values */
827  for (i = 0; i < ndims; i++)
828  total_length += info[i].nbytes;
829 
830  /*
831  * And finally account for the items (those are fixed-length, thanks to
832  * replacing values with uint16 indexes into the deduplicated arrays).
833  */
834  total_length += mcvlist->nitems * ITEM_SIZE(dim);
835 
836  /*
837  * Allocate space for the whole serialized MCV list (we'll skip bytes, so
838  * we set them to zero to make the result more compressible).
839  */
840  raw = (bytea *) palloc0(VARHDRSZ + total_length);
841  SET_VARSIZE(raw, VARHDRSZ + total_length);
842 
843  ptr = VARDATA(raw);
844  endptr = ptr + total_length;
845 
846  /* copy the MCV list header fields, one by one */
847  memcpy(ptr, &mcvlist->magic, sizeof(uint32));
848  ptr += sizeof(uint32);
849 
850  memcpy(ptr, &mcvlist->type, sizeof(uint32));
851  ptr += sizeof(uint32);
852 
853  memcpy(ptr, &mcvlist->nitems, sizeof(uint32));
854  ptr += sizeof(uint32);
855 
856  memcpy(ptr, &mcvlist->ndimensions, sizeof(AttrNumber));
857  ptr += sizeof(AttrNumber);
858 
859  memcpy(ptr, mcvlist->types, sizeof(Oid) * ndims);
860  ptr += (sizeof(Oid) * ndims);
861 
862  /* store information about the attributes (data amounts, ...) */
863  memcpy(ptr, info, sizeof(DimensionInfo) * ndims);
864  ptr += sizeof(DimensionInfo) * ndims;
865 
866  /* Copy the deduplicated values for all attributes to the output. */
867  for (dim = 0; dim < ndims; dim++)
868  {
869  /* remember the starting point for Asserts later */
870  char *start PG_USED_FOR_ASSERTS_ONLY = ptr;
871 
872  for (i = 0; i < info[dim].nvalues; i++)
873  {
874  Datum value = values[dim][i];
875 
876  if (info[dim].typbyval) /* passed by value */
877  {
878  Datum tmp;
879 
880  /*
881  * For byval types, we need to copy just the significant bytes
882  * - we can't use memcpy directly, as that assumes
883  * little-endian behavior. store_att_byval does almost what
884  * we need, but it requires a properly aligned buffer - the
885  * output buffer does not guarantee that. So we simply use a
886  * local Datum variable (which guarantees proper alignment),
887  * and then copy the value from it.
888  */
889  store_att_byval(&tmp, value, info[dim].typlen);
890 
891  memcpy(ptr, &tmp, info[dim].typlen);
892  ptr += info[dim].typlen;
893  }
894  else if (info[dim].typlen > 0) /* passed by reference */
895  {
896  /* no special alignment needed, treated as char array */
897  memcpy(ptr, DatumGetPointer(value), info[dim].typlen);
898  ptr += info[dim].typlen;
899  }
900  else if (info[dim].typlen == -1) /* varlena */
901  {
903 
904  /* copy the length */
905  memcpy(ptr, &len, sizeof(uint32));
906  ptr += sizeof(uint32);
907 
908  /* data from the varlena value (without the header) */
909  memcpy(ptr, VARDATA_ANY(DatumGetPointer(value)), len);
910  ptr += len;
911  }
912  else if (info[dim].typlen == -2) /* cstring */
913  {
914  uint32 len = (uint32) strlen(DatumGetCString(value)) + 1;
915 
916  /* copy the length */
917  memcpy(ptr, &len, sizeof(uint32));
918  ptr += sizeof(uint32);
919 
920  /* value */
921  memcpy(ptr, DatumGetCString(value), len);
922  ptr += len;
923  }
924 
925  /* no underflows or overflows */
926  Assert((ptr > start) && ((ptr - start) <= info[dim].nbytes));
927  }
928 
929  /* we should get exactly nbytes of data for this dimension */
930  Assert((ptr - start) == info[dim].nbytes);
931  }
932 
933  /* Serialize the items, with uint16 indexes instead of the values. */
934  for (i = 0; i < mcvlist->nitems; i++)
935  {
936  MCVItem *mcvitem = &mcvlist->items[i];
937 
938  /* don't write beyond the allocated space */
939  Assert(ptr <= (endptr - ITEM_SIZE(dim)));
940 
941  /* copy NULL and frequency flags into the serialized MCV */
942  memcpy(ptr, mcvitem->isnull, sizeof(bool) * ndims);
943  ptr += sizeof(bool) * ndims;
944 
945  memcpy(ptr, &mcvitem->frequency, sizeof(double));
946  ptr += sizeof(double);
947 
948  memcpy(ptr, &mcvitem->base_frequency, sizeof(double));
949  ptr += sizeof(double);
950 
951  /* store the indexes last */
952  for (dim = 0; dim < ndims; dim++)
953  {
954  uint16 index = 0;
955  Datum *value;
956 
957  /* do the lookup only for non-NULL values */
958  if (!mcvitem->isnull[dim])
959  {
960  value = (Datum *) bsearch_arg(&mcvitem->values[dim], values[dim],
961  info[dim].nvalues, sizeof(Datum),
962  compare_scalars_simple, &ssup[dim]);
963 
964  Assert(value != NULL); /* serialization or deduplication
965  * error */
966 
967  /* compute index within the deduplicated array */
968  index = (uint16) (value - values[dim]);
969 
970  /* check the index is within expected bounds */
971  Assert(index < info[dim].nvalues);
972  }
973 
974  /* copy the index into the serialized MCV */
975  memcpy(ptr, &index, sizeof(uint16));
976  ptr += sizeof(uint16);
977  }
978 
979  /* make sure we don't overflow the allocated value */
980  Assert(ptr <= endptr);
981  }
982 
983  /* at this point we expect to match the total_length exactly */
984  Assert(ptr == endptr);
985 
986  pfree(values);
987  pfree(counts);
988 
989  return raw;
990 }
#define PG_UINT16_MAX
Definition: c.h:576
int compare_scalars_simple(const void *a, const void *b, void *arg)
int compare_datums_simple(Datum a, Datum b, SortSupport ssup)
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
static struct @148 value
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:135
bool ssup_nulls_first
Definition: sortsupport.h:75
MemoryContext ssup_cxt
Definition: sortsupport.h:66
Form_pg_type attrtype
Definition: vacuum.h:127
static void store_att_byval(void *T, Datum newdatum, int attlen)
Definition: tupmacs.h:183
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References Assert(), VacAttrStats::attrcollid, VacAttrStats::attrtype, MCVItem::base_frequency, bsearch_arg(), compare_datums_simple(), compare_scalars_simple(), CurrentMemoryContext, DatumGetCString(), DatumGetPointer(), MCVItem::frequency, i, MCVItem::isnull, ITEM_SIZE, MCVList::items, len, lookup_type_cache(), TypeCacheEntry::lt_opr, MCVList::magic, MAXALIGN, DimensionInfo::nbytes, DimensionInfo::nbytes_aligned, MCVList::ndimensions, MCVList::nitems, DimensionInfo::nvalues, palloc0(), pfree(), PG_DETOAST_DATUM, PG_UINT16_MAX, PG_USED_FOR_ASSERTS_ONLY, PointerGetDatum(), PrepareSortSupportFromOrderingOp(), qsort_interruptible(), SET_VARSIZE, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, store_att_byval(), DimensionInfo::typbyval, MCVList::type, TYPECACHE_LT_OPR, MCVList::types, DimensionInfo::typlen, value, values, MCVItem::values, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by statext_store().