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/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 (VacAttrStats **stats, int numattrs)
 
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 (int numrows, HeapTuple *rows, Bitmapset *attrs, VacAttrStats **stats, double totalrows, int stattarget)
 
static int compare_sort_item_count (const void *a, const void *b)
 
static int sort_item_compare (const void *a, const void *b, void *arg)
 
MCVListstatext_mcv_load (Oid mvoid)
 
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 boolmcv_get_match_bitmap (PlannerInfo *root, List *clauses, Bitmapset *keys, MCVList *mcvlist, bool is_or)
 
Selectivity mcv_clauselist_selectivity (PlannerInfo *root, StatisticExtInfo *stat, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Selectivity *basesel, Selectivity *totalsel)
 

Macro Definition Documentation

◆ ITEM_SIZE

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

Definition at line 56 of file mcv.c.

Referenced by statext_mcv_serialize().

◆ MinSizeOfMCVList

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

Definition at line 62 of file mcv.c.

Referenced by statext_mcv_deserialize().

◆ RESULT_IS_FINAL

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

Definition at line 103 of file mcv.c.

Referenced by mcv_get_match_bitmap().

◆ RESULT_MERGE

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

Definition at line 91 of file mcv.c.

Referenced by mcv_get_match_bitmap().

◆ SizeOfMCVList

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

Definition at line 71 of file mcv.c.

Referenced by statext_mcv_deserialize().

Function Documentation

◆ build_column_frequencies()

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

Definition at line 489 of file mcv.c.

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

Referenced by statext_mcv_build().

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

◆ build_distinct_groups()

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

Definition at line 423 of file mcv.c.

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

Referenced by statext_mcv_build().

425 {
426  int i,
427  j;
428  int ngroups = count_distinct_groups(numrows, items, mss);
429 
430  SortItem *groups = (SortItem *) palloc(ngroups * sizeof(SortItem));
431 
432  j = 0;
433  groups[0] = items[0];
434  groups[0].count = 1;
435 
436  for (i = 1; i < numrows; i++)
437  {
438  /* Assume sorted in ascending order. */
439  Assert(multi_sort_compare(&items[i], &items[i - 1], mss) >= 0);
440 
441  /* New distinct group detected. */
442  if (multi_sort_compare(&items[i], &items[i - 1], mss) != 0)
443  {
444  groups[++j] = items[i];
445  groups[j].count = 0;
446  }
447 
448  groups[j].count++;
449  }
450 
451  /* ensure we filled the expected number of distinct groups */
452  Assert(j + 1 == ngroups);
453 
454  /* Sort the distinct groups by frequency (in descending order). */
455  pg_qsort((void *) groups, ngroups, sizeof(SortItem),
457 
458  *ndistinct = ngroups;
459  return groups;
460 }
static int compare_sort_item_count(const void *a, const void *b)
Definition: mcv.c:403
#define Assert(condition)
Definition: c.h:732
int multi_sort_compare(const void *a, const void *b, void *arg)
void pg_qsort(void *base, size_t nel, size_t elsize, int(*cmp)(const void *, const void *))
Definition: qsort.c:113
void * palloc(Size size)
Definition: mcxt.c:949
int i
static int count_distinct_groups(int numrows, SortItem *items, MultiSortSupport mss)
Definition: mcv.c:380

◆ build_mss()

static MultiSortSupport build_mss ( VacAttrStats **  stats,
int  numattrs 
)
static

Definition at line 349 of file mcv.c.

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

Referenced by statext_mcv_build().

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

◆ compare_sort_item_count()

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

Definition at line 403 of file mcv.c.

References SortItem::count.

Referenced by build_distinct_groups().

404 {
405  SortItem *ia = (SortItem *) a;
406  SortItem *ib = (SortItem *) b;
407 
408  if (ia->count == ib->count)
409  return 0;
410  else if (ia->count > ib->count)
411  return -1;
412 
413  return 1;
414 }

◆ count_distinct_groups()

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

Definition at line 380 of file mcv.c.

References Assert, i, and multi_sort_compare().

Referenced by build_distinct_groups().

381 {
382  int i;
383  int ndistinct;
384 
385  ndistinct = 1;
386  for (i = 1; i < numrows; i++)
387  {
388  /* make sure the array really is sorted */
389  Assert(multi_sort_compare(&items[i], &items[i - 1], mss) >= 0);
390 
391  if (multi_sort_compare(&items[i], &items[i - 1], mss) != 0)
392  ndistinct += 1;
393  }
394 
395  return ndistinct;
396 }
#define Assert(condition)
Definition: c.h:732
int multi_sort_compare(const void *a, const void *b, void *arg)
int i

◆ get_mincount_for_mcv_list()

static double get_mincount_for_mcv_list ( int  samplerows,
double  totalrows 
)
static

Definition at line 151 of file mcv.c.

Referenced by statext_mcv_build().

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

◆ 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 1794 of file mcv.c.

References MCVItem::base_frequency, MCVItem::frequency, i, MCVList::items, StatisticExtInfo::keys, mcv_get_match_bitmap(), MCVList::nitems, statext_mcv_load(), and StatisticExtInfo::statOid.

Referenced by statext_mcv_clauselist_selectivity().

1799 {
1800  int i;
1801  MCVList *mcv;
1802  Selectivity s = 0.0;
1803 
1804  /* match/mismatch bitmap for each MCV item */
1805  bool *matches = NULL;
1806 
1807  /* load the MCV list stored in the statistics object */
1808  mcv = statext_mcv_load(stat->statOid);
1809 
1810  /* build a match bitmap for the clauses */
1811  matches = mcv_get_match_bitmap(root, clauses, stat->keys, mcv, false);
1812 
1813  /* sum frequencies for all the matching MCV items */
1814  *basesel = 0.0;
1815  *totalsel = 0.0;
1816  for (i = 0; i < mcv->nitems; i++)
1817  {
1818  *totalsel += mcv->items[i].frequency;
1819 
1820  if (matches[i] != false)
1821  {
1822  /* XXX Shouldn't the basesel be outside the if condition? */
1823  *basesel += mcv->items[i].base_frequency;
1824  s += mcv->items[i].frequency;
1825  }
1826  }
1827 
1828  return s;
1829 }
uint32 nitems
Definition: statistics.h:90
double Selectivity
Definition: nodes.h:658
MCVItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:93
static bool * mcv_get_match_bitmap(PlannerInfo *root, List *clauses, Bitmapset *keys, MCVList *mcvlist, bool is_or)
Definition: mcv.c:1543
MCVList * statext_mcv_load(Oid mvoid)
Definition: mcv.c:557
const symbol * s
Definition: header.h:17
double base_frequency
Definition: statistics.h:80
Bitmapset * keys
Definition: pathnodes.h:885
int i
double frequency
Definition: statistics.h:79

◆ mcv_get_match_bitmap()

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

Definition at line 1543 of file mcv.c.

References NullTest::arg, BoolExpr::args, Assert, bms_member_index(), Const::constisnull, Const::constvalue, DatumGetBool, elog, ERROR, examine_opclause_expression(), fmgr_info(), FunctionCall2Coll(), get_opcode(), i, idx(), is_andclause(), IS_NOT_NULL, is_notclause(), IS_NULL, is_opclause(), is_orclause(), IsA, MCVItem::isnull, MCVList::items, lfirst, list_length(), NIL, MCVList::nitems, NullTest::nulltesttype, OpExpr::opno, palloc(), pfree(), RESULT_IS_FINAL, RESULT_MERGE, STATS_MCVLIST_MAX_ITEMS, Node::type, MCVItem::values, Var::varattno, Var::varcollid, and Var::vartype.

Referenced by mcv_clauselist_selectivity().

1545 {
1546  int i;
1547  ListCell *l;
1548  bool *matches;
1549 
1550  /* The bitmap may be partially built. */
1551  Assert(clauses != NIL);
1552  Assert(list_length(clauses) >= 1);
1553  Assert(mcvlist != NULL);
1554  Assert(mcvlist->nitems > 0);
1555  Assert(mcvlist->nitems <= STATS_MCVLIST_MAX_ITEMS);
1556 
1557  matches = palloc(sizeof(bool) * mcvlist->nitems);
1558  memset(matches, (is_or) ? false : true,
1559  sizeof(bool) * mcvlist->nitems);
1560 
1561  /*
1562  * Loop through the list of clauses, and for each of them evaluate all the
1563  * MCV items not yet eliminated by the preceding clauses.
1564  */
1565  foreach(l, clauses)
1566  {
1567  Node *clause = (Node *) lfirst(l);
1568 
1569  /* if it's a RestrictInfo, then extract the clause */
1570  if (IsA(clause, RestrictInfo))
1571  clause = (Node *) ((RestrictInfo *) clause)->clause;
1572 
1573  /*
1574  * Handle the various types of clauses - OpClause, NullTest and
1575  * AND/OR/NOT
1576  */
1577  if (is_opclause(clause))
1578  {
1579  OpExpr *expr = (OpExpr *) clause;
1580  FmgrInfo opproc;
1581 
1582  /* valid only after examine_opclause_expression returns true */
1583  Var *var;
1584  Const *cst;
1585  bool varonleft;
1586 
1587  fmgr_info(get_opcode(expr->opno), &opproc);
1588 
1589  /* extract the var and const from the expression */
1590  if (examine_opclause_expression(expr, &var, &cst, &varonleft))
1591  {
1592  int idx;
1593 
1594  /* match the attribute to a dimension of the statistic */
1595  idx = bms_member_index(keys, var->varattno);
1596 
1597  /*
1598  * Walk through the MCV items and evaluate the current clause.
1599  * We can skip items that were already ruled out, and
1600  * terminate if there are no remaining MCV items that might
1601  * possibly match.
1602  */
1603  for (i = 0; i < mcvlist->nitems; i++)
1604  {
1605  bool match = true;
1606  MCVItem *item = &mcvlist->items[i];
1607 
1608  /*
1609  * When the MCV item or the Const value is NULL we can treat
1610  * this as a mismatch. We must not call the operator because
1611  * of strictness.
1612  */
1613  if (item->isnull[idx] || cst->constisnull)
1614  {
1615  matches[i] = RESULT_MERGE(matches[i], is_or, false);
1616  continue;
1617  }
1618 
1619  /*
1620  * Skip MCV items that can't change result in the bitmap.
1621  * Once the value gets false for AND-lists, or true for
1622  * OR-lists, we don't need to look at more clauses.
1623  */
1624  if (RESULT_IS_FINAL(matches[i], is_or))
1625  continue;
1626 
1627  /*
1628  * First check whether the constant is below the lower
1629  * boundary (in that case we can skip the bucket, because
1630  * there's no overlap).
1631  *
1632  * We don't store collations used to build the statistics,
1633  * but we can use the collation for the attribute itself,
1634  * as stored in varcollid. We do reset the statistics after
1635  * a type change (including collation change), so this is
1636  * OK. We may need to relax this after allowing extended
1637  * statistics on expressions.
1638  */
1639  if (varonleft)
1640  match = DatumGetBool(FunctionCall2Coll(&opproc,
1641  var->varcollid,
1642  item->values[idx],
1643  cst->constvalue));
1644  else
1645  match = DatumGetBool(FunctionCall2Coll(&opproc,
1646  var->varcollid,
1647  cst->constvalue,
1648  item->values[idx]));
1649 
1650  /* update the match bitmap with the result */
1651  matches[i] = RESULT_MERGE(matches[i], is_or, match);
1652  }
1653  }
1654  }
1655  else if (IsA(clause, NullTest))
1656  {
1657  NullTest *expr = (NullTest *) clause;
1658  Var *var = (Var *) (expr->arg);
1659 
1660  /* match the attribute to a dimension of the statistic */
1661  int idx = bms_member_index(keys, var->varattno);
1662 
1663  /*
1664  * Walk through the MCV items and evaluate the current clause. We
1665  * can skip items that were already ruled out, and terminate if
1666  * there are no remaining MCV items that might possibly match.
1667  */
1668  for (i = 0; i < mcvlist->nitems; i++)
1669  {
1670  bool match = false; /* assume mismatch */
1671  MCVItem *item = &mcvlist->items[i];
1672 
1673  /* if the clause mismatches the MCV item, update the bitmap */
1674  switch (expr->nulltesttype)
1675  {
1676  case IS_NULL:
1677  match = (item->isnull[idx]) ? true : match;
1678  break;
1679 
1680  case IS_NOT_NULL:
1681  match = (!item->isnull[idx]) ? true : match;
1682  break;
1683  }
1684 
1685  /* now, update the match bitmap, depending on OR/AND type */
1686  matches[i] = RESULT_MERGE(matches[i], is_or, match);
1687  }
1688  }
1689  else if (is_orclause(clause) || is_andclause(clause))
1690  {
1691  /* AND/OR clause, with all subclauses being compatible */
1692 
1693  int i;
1694  BoolExpr *bool_clause = ((BoolExpr *) clause);
1695  List *bool_clauses = bool_clause->args;
1696 
1697  /* match/mismatch bitmap for each MCV item */
1698  bool *bool_matches = NULL;
1699 
1700  Assert(bool_clauses != NIL);
1701  Assert(list_length(bool_clauses) >= 2);
1702 
1703  /* build the match bitmap for the OR-clauses */
1704  bool_matches = mcv_get_match_bitmap(root, bool_clauses, keys,
1705  mcvlist, is_orclause(clause));
1706 
1707  /*
1708  * Merge the bitmap produced by mcv_get_match_bitmap into the
1709  * current one. We need to consider if we're evaluating AND or OR
1710  * condition when merging the results.
1711  */
1712  for (i = 0; i < mcvlist->nitems; i++)
1713  matches[i] = RESULT_MERGE(matches[i], is_or, bool_matches[i]);
1714 
1715  pfree(bool_matches);
1716  }
1717  else if (is_notclause(clause))
1718  {
1719  /* NOT clause, with all subclauses compatible */
1720 
1721  int i;
1722  BoolExpr *not_clause = ((BoolExpr *) clause);
1723  List *not_args = not_clause->args;
1724 
1725  /* match/mismatch bitmap for each MCV item */
1726  bool *not_matches = NULL;
1727 
1728  Assert(not_args != NIL);
1729  Assert(list_length(not_args) == 1);
1730 
1731  /* build the match bitmap for the NOT-clause */
1732  not_matches = mcv_get_match_bitmap(root, not_args, keys,
1733  mcvlist, false);
1734 
1735  /*
1736  * Merge the bitmap produced by mcv_get_match_bitmap into the
1737  * current one. We're handling a NOT clause, so invert the result
1738  * before merging it into the global bitmap.
1739  */
1740  for (i = 0; i < mcvlist->nitems; i++)
1741  matches[i] = RESULT_MERGE(matches[i], is_or, !not_matches[i]);
1742 
1743  pfree(not_matches);
1744  }
1745  else if (IsA(clause, Var))
1746  {
1747  /* Var (has to be a boolean Var, possibly from below NOT) */
1748 
1749  Var *var = (Var *) (clause);
1750 
1751  /* match the attribute to a dimension of the statistic */
1752  int idx = bms_member_index(keys, var->varattno);
1753 
1754  Assert(var->vartype == BOOLOID);
1755 
1756  /*
1757  * Walk through the MCV items and evaluate the current clause. We
1758  * can skip items that were already ruled out, and terminate if
1759  * there are no remaining MCV items that might possibly match.
1760  */
1761  for (i = 0; i < mcvlist->nitems; i++)
1762  {
1763  MCVItem *item = &mcvlist->items[i];
1764  bool match = false;
1765 
1766  /* if the item is NULL, it's a mismatch */
1767  if (!item->isnull[idx] && DatumGetBool(item->values[idx]))
1768  match = true;
1769 
1770  /* update the result bitmap */
1771  matches[i] = RESULT_MERGE(matches[i], is_or, match);
1772  }
1773  }
1774  else
1775  elog(ERROR, "unknown clause type: %d", clause->type);
1776  }
1777 
1778  return matches;
1779 }
Datum constvalue
Definition: primnodes.h:200
uint32 nitems
Definition: statistics.h:90
#define NIL
Definition: pg_list.h:65
Definition: fmgr.h:56
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:103
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:94
Definition: nodes.h:525
AttrNumber varattno
Definition: primnodes.h:172
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
Datum * values
Definition: statistics.h:82
MCVItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:93
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1150
Definition: primnodes.h:167
static bool * mcv_get_match_bitmap(PlannerInfo *root, List *clauses, Bitmapset *keys, MCVList *mcvlist, bool is_or)
Definition: mcv.c:1543
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
Oid vartype
Definition: primnodes.h:174
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
Expr * arg
Definition: primnodes.h:1205
#define RESULT_MERGE(value, is_or, match)
Definition: mcv.c:91
NodeTag type
Definition: nodes.h:527
#define DatumGetBool(X)
Definition: postgres.h:393
int bms_member_index(Bitmapset *a, int x)
Definition: bitmapset.c:453
#define RESULT_IS_FINAL(value, is_or)
Definition: mcv.c:103
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:112
#define STATS_MCVLIST_MAX_ITEMS
Definition: statistics.h:69
NullTestType nulltesttype
Definition: primnodes.h:1206
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
List * args
Definition: primnodes.h:569
bool * isnull
Definition: statistics.h:81
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:226
int i
Oid varcollid
Definition: primnodes.h:176
Oid opno
Definition: primnodes.h:502
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
bool examine_opclause_expression(OpExpr *expr, Var **varp, Const **cstp, bool *varonleftp)
Definition: pg_list.h:50
bool constisnull
Definition: primnodes.h:201

◆ pg_mcv_list_in()

Datum pg_mcv_list_in ( PG_FUNCTION_ARGS  )

Definition at line 1467 of file mcv.c.

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

1468 {
1469  /*
1470  * pg_mcv_list stores the data in binary form and parsing text input is
1471  * not needed, so disallow this.
1472  */
1473  ereport(ERROR,
1474  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1475  errmsg("cannot accept a value of type %s", "pg_mcv_list")));
1476 
1477  PG_RETURN_VOID(); /* keep compiler quiet */
1478 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_VOID()
Definition: fmgr.h:339
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pg_mcv_list_out()

Datum pg_mcv_list_out ( PG_FUNCTION_ARGS  )

Definition at line 1493 of file mcv.c.

References byteaout().

1494 {
1495  return byteaout(fcinfo);
1496 }
Datum byteaout(PG_FUNCTION_ARGS)
Definition: varlena.c:373

◆ pg_mcv_list_recv()

Datum pg_mcv_list_recv ( PG_FUNCTION_ARGS  )

Definition at line 1502 of file mcv.c.

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

1503 {
1504  ereport(ERROR,
1505  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1506  errmsg("cannot accept a value of type %s", "pg_mcv_list")));
1507 
1508  PG_RETURN_VOID(); /* keep compiler quiet */
1509 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_VOID()
Definition: fmgr.h:339
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pg_mcv_list_send()

Datum pg_mcv_list_send ( PG_FUNCTION_ARGS  )

Definition at line 1518 of file mcv.c.

References byteasend().

1519 {
1520  return byteasend(fcinfo);
1521 }
Datum byteasend(PG_FUNCTION_ARGS)
Definition: varlena.c:464

◆ pg_stats_ext_mcvlist_items()

Datum pg_stats_ext_mcvlist_items ( PG_FUNCTION_ARGS  )

Definition at line 1334 of file mcv.c.

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, MCVItem::values, and values.

1335 {
1336  FuncCallContext *funcctx;
1337 
1338  /* stuff done only on the first call of the function */
1339  if (SRF_IS_FIRSTCALL())
1340  {
1341  MemoryContext oldcontext;
1342  MCVList *mcvlist;
1343  TupleDesc tupdesc;
1344 
1345  /* create a function context for cross-call persistence */
1346  funcctx = SRF_FIRSTCALL_INIT();
1347 
1348  /* switch to memory context appropriate for multiple function calls */
1349  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1350 
1352 
1353  funcctx->user_fctx = mcvlist;
1354 
1355  /* total number of tuples to be returned */
1356  funcctx->max_calls = 0;
1357  if (funcctx->user_fctx != NULL)
1358  funcctx->max_calls = mcvlist->nitems;
1359 
1360  /* Build a tuple descriptor for our result type */
1361  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1362  ereport(ERROR,
1363  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1364  errmsg("function returning record called in context "
1365  "that cannot accept type record")));
1366  tupdesc = BlessTupleDesc(tupdesc);
1367 
1368  /*
1369  * generate attribute metadata needed later to produce tuples from raw
1370  * C strings
1371  */
1372  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
1373 
1374  MemoryContextSwitchTo(oldcontext);
1375  }
1376 
1377  /* stuff done on every call of the function */
1378  funcctx = SRF_PERCALL_SETUP();
1379 
1380  if (funcctx->call_cntr < funcctx->max_calls) /* do when there is more left to send */
1381  {
1382  Datum values[5];
1383  bool nulls[5];
1384  HeapTuple tuple;
1385  Datum result;
1386  ArrayBuildState *astate_values = NULL;
1387  ArrayBuildState *astate_nulls = NULL;
1388 
1389  int i;
1390  MCVList *mcvlist;
1391  MCVItem *item;
1392 
1393  mcvlist = (MCVList *) funcctx->user_fctx;
1394 
1395  Assert(funcctx->call_cntr < mcvlist->nitems);
1396 
1397  item = &mcvlist->items[funcctx->call_cntr];
1398 
1399  for (i = 0; i < mcvlist->ndimensions; i++)
1400  {
1401 
1402  astate_nulls = accumArrayResult(astate_nulls,
1403  BoolGetDatum(item->isnull[i]),
1404  false,
1405  BOOLOID,
1407 
1408  if (!item->isnull[i])
1409  {
1410  bool isvarlena;
1411  Oid outfunc;
1412  FmgrInfo fmgrinfo;
1413  Datum val;
1414  text *txt;
1415 
1416  /* lookup output func for the type */
1417  getTypeOutputInfo(mcvlist->types[i], &outfunc, &isvarlena);
1418  fmgr_info(outfunc, &fmgrinfo);
1419 
1420  val = FunctionCall1(&fmgrinfo, item->values[i]);
1421  txt = cstring_to_text(DatumGetPointer(val));
1422 
1423  astate_values = accumArrayResult(astate_values,
1424  PointerGetDatum(txt),
1425  false,
1426  TEXTOID,
1428  }
1429  else
1430  astate_values = accumArrayResult(astate_values,
1431  (Datum) 0,
1432  true,
1433  TEXTOID,
1435  }
1436 
1437  values[0] = Int32GetDatum(funcctx->call_cntr);
1438  values[1] = PointerGetDatum(makeArrayResult(astate_values, CurrentMemoryContext));
1439  values[2] = PointerGetDatum(makeArrayResult(astate_nulls, CurrentMemoryContext));
1440  values[3] = Float8GetDatum(item->frequency);
1441  values[4] = Float8GetDatum(item->base_frequency);
1442 
1443  /* no NULLs in the tuple */
1444  memset(nulls, 0, sizeof(nulls));
1445 
1446  /* build a tuple */
1447  tuple = heap_form_tuple(funcctx->attinmeta->tupdesc, values, nulls);
1448 
1449  /* make the tuple into a datum */
1450  result = HeapTupleGetDatum(tuple);
1451 
1452  SRF_RETURN_NEXT(funcctx, result);
1453  }
1454  else /* do when there is no more left */
1455  {
1456  SRF_RETURN_DONE(funcctx);
1457  }
1458 }
uint32 nitems
Definition: statistics.h:90
uint64 call_cntr
Definition: funcapi.h:66
Definition: fmgr.h:56
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
Oid types[STATS_MAX_DIMENSIONS]
Definition: statistics.h:92
MCVList * statext_mcv_deserialize(bytea *data)
Definition: mcv.c:992
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
#define PointerGetDatum(X)
Definition: postgres.h:556
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
AttrNumber ndimensions
Definition: statistics.h:91
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
Datum * values
Definition: statistics.h:82
MCVItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:93
unsigned int Oid
Definition: postgres_ext.h:31
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1723
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:329
#define ERROR
Definition: elog.h:43
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2048
AttInMetadata * attinmeta
Definition: funcapi.h:92
TupleDesc tupdesc
Definition: funcapi.h:39
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define ereport(elevel, rest)
Definition: elog.h:141
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5117
uintptr_t Datum
Definition: postgres.h:367
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2063
#define BoolGetDatum(X)
Definition: postgres.h:402
int result
Definition: header.h:19
text * cstring_to_text(const char *s)
Definition: varlena.c:171
#define Assert(condition)
Definition: c.h:732
double base_frequency
Definition: statistics.h:80
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
#define DatumGetPointer(X)
Definition: postgres.h:549
bool * isnull
Definition: statistics.h:81
static Datum values[MAXATTR]
Definition: bootstrap.c:167
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5053
#define Int32GetDatum(X)
Definition: postgres.h:479
void * user_fctx
Definition: funcapi.h:83
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:635
Definition: c.h:549
long val
Definition: informix.c:684
uint64 max_calls
Definition: funcapi.h:75
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
double frequency
Definition: statistics.h:79
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ sort_item_compare()

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

Definition at line 464 of file mcv.c.

References ApplySortComparator(), SortItem::isnull, and SortItem::values.

Referenced by build_column_frequencies().

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

◆ statext_mcv_build()

MCVList* statext_mcv_build ( int  numrows,
HeapTuple rows,
Bitmapset attrs,
VacAttrStats **  stats,
double  totalrows,
int  stattarget 
)

Definition at line 183 of file mcv.c.

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

Referenced by BuildRelationExtStatistics().

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

◆ statext_mcv_deserialize()

MCVList* statext_mcv_deserialize ( bytea data)

Definition at line 992 of file mcv.c.

References Assert, MCVItem::base_frequency, elog, ERROR, fetch_att, MCVItem::frequency, i, MCVItem::isnull, MCVList::items, MCVList::magic, MAXALIGN, MinSizeOfMCVList, DimensionInfo::nbytes, DimensionInfo::nbytes_aligned, MCVList::ndimensions, MCVList::nitems, DimensionInfo::nvalues, offsetof, 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().

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

◆ statext_mcv_load()

MCVList* statext_mcv_load ( Oid  mvoid)

Definition at line 557 of file mcv.c.

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

Referenced by mcv_clauselist_selectivity().

558 {
559  MCVList *result;
560  bool isnull;
561  Datum mcvlist;
563 
564  if (!HeapTupleIsValid(htup))
565  elog(ERROR, "cache lookup failed for statistics object %u", mvoid);
566 
567  mcvlist = SysCacheGetAttr(STATEXTDATASTXOID, htup,
568  Anum_pg_statistic_ext_data_stxdmcv, &isnull);
569 
570  if (isnull)
571  elog(ERROR,
572  "requested statistic kind \"%c\" is not yet built for statistics object %u",
573  STATS_EXT_DEPENDENCIES, mvoid);
574 
575  result = statext_mcv_deserialize(DatumGetByteaP(mcvlist));
576 
577  ReleaseSysCache(htup);
578 
579  return result;
580 }
MCVList * statext_mcv_deserialize(bytea *data)
Definition: mcv.c:992
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define DatumGetByteaP(X)
Definition: fmgr.h:325
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
int result
Definition: header.h:19
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:226

◆ statext_mcv_serialize()

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

Definition at line 618 of file mcv.c.

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, 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_arg(), 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, MCVItem::values, values, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by statext_store().

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