PostgreSQL Source Code  git master
mvdistinct.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/htup_details.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_statistic_ext_data.h"
#include "lib/stringinfo.h"
#include "statistics/extended_stats_internal.h"
#include "statistics/statistics.h"
#include "utils/fmgrprotos.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for mvdistinct.c:

Go to the source code of this file.

Data Structures

struct  CombinationGenerator
 

Macros

#define SizeOfHeader   (3 * sizeof(uint32))
 
#define SizeOfItem(natts)   (sizeof(double) + sizeof(int) + (natts) * sizeof(AttrNumber))
 
#define MinSizeOfItem   SizeOfItem(2)
 
#define MinSizeOfItems(nitems)   (SizeOfHeader + (nitems) * MinSizeOfItem)
 

Typedefs

typedef struct CombinationGenerator CombinationGenerator
 

Functions

static double ndistinct_for_combination (double totalrows, StatsBuildData *data, int k, int *combination)
 
static double estimate_ndistinct (double totalrows, int numrows, int d, int f1)
 
static int n_choose_k (int n, int k)
 
static int num_combinations (int n)
 
static CombinationGeneratorgenerator_init (int n, int k)
 
static void generator_free (CombinationGenerator *state)
 
static int * generator_next (CombinationGenerator *state)
 
static void generate_combinations (CombinationGenerator *state)
 
MVNDistinctstatext_ndistinct_build (double totalrows, StatsBuildData *data)
 
MVNDistinctstatext_ndistinct_load (Oid mvoid)
 
byteastatext_ndistinct_serialize (MVNDistinct *ndistinct)
 
MVNDistinctstatext_ndistinct_deserialize (bytea *data)
 
Datum pg_ndistinct_in (PG_FUNCTION_ARGS)
 
Datum pg_ndistinct_out (PG_FUNCTION_ARGS)
 
Datum pg_ndistinct_recv (PG_FUNCTION_ARGS)
 
Datum pg_ndistinct_send (PG_FUNCTION_ARGS)
 
static void generate_combinations_recurse (CombinationGenerator *state, int index, int start, int *current)
 

Macro Definition Documentation

◆ MinSizeOfItem

#define MinSizeOfItem   SizeOfItem(2)

Definition at line 53 of file mvdistinct.c.

◆ MinSizeOfItems

#define MinSizeOfItems (   nitems)    (SizeOfHeader + (nitems) * MinSizeOfItem)

Definition at line 56 of file mvdistinct.c.

Referenced by statext_ndistinct_deserialize().

◆ SizeOfHeader

#define SizeOfHeader   (3 * sizeof(uint32))

Definition at line 46 of file mvdistinct.c.

Referenced by statext_ndistinct_deserialize(), and statext_ndistinct_serialize().

◆ SizeOfItem

#define SizeOfItem (   natts)    (sizeof(double) + sizeof(int) + (natts) * sizeof(AttrNumber))

Definition at line 49 of file mvdistinct.c.

Referenced by statext_ndistinct_serialize().

Typedef Documentation

◆ CombinationGenerator

Function Documentation

◆ estimate_ndistinct()

static double estimate_ndistinct ( double  totalrows,
int  numrows,
int  d,
int  f1 
)
static

Definition at line 521 of file mvdistinct.c.

Referenced by ndistinct_for_combination().

522 {
523  double numer,
524  denom,
525  ndistinct;
526 
527  numer = (double) numrows * (double) d;
528 
529  denom = (double) (numrows - f1) +
530  (double) f1 * (double) numrows / totalrows;
531 
532  ndistinct = numer / denom;
533 
534  /* Clamp to sane range in case of roundoff error */
535  if (ndistinct < (double) d)
536  ndistinct = (double) d;
537 
538  if (ndistinct > totalrows)
539  ndistinct = totalrows;
540 
541  return floor(ndistinct + 0.5);
542 }
int f1[ARRAY_SIZE]
Definition: sql-declare.c:113

◆ generate_combinations()

static void generate_combinations ( CombinationGenerator state)
static

Definition at line 692 of file mvdistinct.c.

References CombinationGenerator::current, generate_combinations_recurse(), CombinationGenerator::k, palloc0(), and pfree().

Referenced by generator_init().

693 {
694  int *current = (int *) palloc0(sizeof(int) * state->k);
695 
696  generate_combinations_recurse(state, 0, 0, current);
697 
698  pfree(current);
699 }
void pfree(void *pointer)
Definition: mcxt.c:1169
void * palloc0(Size size)
Definition: mcxt.c:1093
static void generate_combinations_recurse(CombinationGenerator *state, int index, int start, int *current)
Definition: mvdistinct.c:657

◆ generate_combinations_recurse()

static void generate_combinations_recurse ( CombinationGenerator state,
int  index,
int  start,
int *  current 
)
static

Definition at line 657 of file mvdistinct.c.

References CombinationGenerator::combinations, CombinationGenerator::current, i, CombinationGenerator::k, and CombinationGenerator::n.

Referenced by generate_combinations().

659 {
660  /* If we haven't filled all the elements, simply recurse. */
661  if (index < state->k)
662  {
663  int i;
664 
665  /*
666  * The values have to be in ascending order, so make sure we start
667  * with the value passed by parameter.
668  */
669 
670  for (i = start; i < state->n; i++)
671  {
672  current[index] = i;
673  generate_combinations_recurse(state, (index + 1), (i + 1), current);
674  }
675 
676  return;
677  }
678  else
679  {
680  /* we got a valid combination, add it to the array */
681  memcpy(&state->combinations[(state->k * state->current)],
682  current, state->k * sizeof(int));
683  state->current++;
684  }
685 }
Definition: type.h:89
static void generate_combinations_recurse(CombinationGenerator *state, int index, int start, int *current)
Definition: mvdistinct.c:657
int i

◆ generator_free()

static void generator_free ( CombinationGenerator state)
static

Definition at line 642 of file mvdistinct.c.

References CombinationGenerator::combinations, and pfree().

Referenced by statext_ndistinct_build().

643 {
644  pfree(state->combinations);
645  pfree(state);
646 }
void pfree(void *pointer)
Definition: mcxt.c:1169

◆ generator_init()

static CombinationGenerator * generator_init ( int  n,
int  k 
)
static

Definition at line 589 of file mvdistinct.c.

References Assert, CombinationGenerator::combinations, CombinationGenerator::current, generate_combinations(), CombinationGenerator::k, CombinationGenerator::n, n_choose_k(), CombinationGenerator::ncombinations, and palloc().

Referenced by statext_ndistinct_build().

590 {
592 
593  Assert((n >= k) && (k > 0));
594 
595  /* allocate the generator state as a single chunk of memory */
596  state = (CombinationGenerator *) palloc(sizeof(CombinationGenerator));
597 
598  state->ncombinations = n_choose_k(n, k);
599 
600  /* pre-allocate space for all combinations */
601  state->combinations = (int *) palloc(sizeof(int) * k * state->ncombinations);
602 
603  state->current = 0;
604  state->k = k;
605  state->n = n;
606 
607  /* now actually pre-generate all the combinations of K elements */
608  generate_combinations(state);
609 
610  /* make sure we got the expected number of combinations */
611  Assert(state->current == state->ncombinations);
612 
613  /* reset the number, so we start with the first one */
614  state->current = 0;
615 
616  return state;
617 }
static void generate_combinations(CombinationGenerator *state)
Definition: mvdistinct.c:692
static int n_choose_k(int n, int k)
Definition: mvdistinct.c:550
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
void * palloc(Size size)
Definition: mcxt.c:1062

◆ generator_next()

static int * generator_next ( CombinationGenerator state)
static

Definition at line 627 of file mvdistinct.c.

References CombinationGenerator::combinations, CombinationGenerator::current, CombinationGenerator::k, and CombinationGenerator::ncombinations.

Referenced by statext_ndistinct_build().

628 {
629  if (state->current == state->ncombinations)
630  return NULL;
631 
632  return &state->combinations[state->k * state->current++];
633 }

◆ n_choose_k()

static int n_choose_k ( int  n,
int  k 
)
static

Definition at line 550 of file mvdistinct.c.

References Assert, CombinationGenerator::k, and Min.

Referenced by generator_init().

551 {
552  int d,
553  r;
554 
555  Assert((k > 0) && (n >= k));
556 
557  /* use symmetry of the binomial coefficients */
558  k = Min(k, n - k);
559 
560  r = 1;
561  for (d = 1; d <= k; ++d)
562  {
563  r *= n--;
564  r /= d;
565  }
566 
567  return r;
568 }
#define Min(x, y)
Definition: c.h:986
#define Assert(condition)
Definition: c.h:804

◆ ndistinct_for_combination()

static double ndistinct_for_combination ( double  totalrows,
StatsBuildData data,
int  k,
int *  combination 
)
static

Definition at line 425 of file mvdistinct.c.

References VacAttrStats::attrcollid, VacAttrStats::attrtypid, elog, ERROR, estimate_ndistinct(), f1, i, InvalidOid, SortItem::isnull, CombinationGenerator::k, lookup_type_cache(), TypeCacheEntry::lt_opr, multi_sort_add_dimension(), multi_sort_compare(), multi_sort_init(), StatsBuildData::nulls, StatsBuildData::numrows, palloc(), palloc0(), qsort_arg(), StatsBuildData::stats, generate_unaccent_rules::type, TYPECACHE_LT_OPR, SortItem::values, StatsBuildData::values, and values.

Referenced by statext_ndistinct_build().

427 {
428  int i,
429  j;
430  int f1,
431  cnt,
432  d;
433  bool *isnull;
434  Datum *values;
435  SortItem *items;
436  MultiSortSupport mss;
437  int numrows = data->numrows;
438 
439  mss = multi_sort_init(k);
440 
441  /*
442  * In order to determine the number of distinct elements, create separate
443  * values[]/isnull[] arrays with all the data we have, then sort them
444  * using the specified column combination as dimensions. We could try to
445  * sort in place, but it'd probably be more complex and bug-prone.
446  */
447  items = (SortItem *) palloc(numrows * sizeof(SortItem));
448  values = (Datum *) palloc0(sizeof(Datum) * numrows * k);
449  isnull = (bool *) palloc0(sizeof(bool) * numrows * k);
450 
451  for (i = 0; i < numrows; i++)
452  {
453  items[i].values = &values[i * k];
454  items[i].isnull = &isnull[i * k];
455  }
456 
457  /*
458  * For each dimension, set up sort-support and fill in the values from the
459  * sample data.
460  *
461  * We use the column data types' default sort operators and collations;
462  * perhaps at some point it'd be worth using column-specific collations?
463  */
464  for (i = 0; i < k; i++)
465  {
466  Oid typid;
468  Oid collid = InvalidOid;
469  VacAttrStats *colstat = data->stats[combination[i]];
470 
471  typid = colstat->attrtypid;
472  collid = colstat->attrcollid;
473 
474  type = lookup_type_cache(typid, TYPECACHE_LT_OPR);
475  if (type->lt_opr == InvalidOid) /* shouldn't happen */
476  elog(ERROR, "cache lookup failed for ordering operator for type %u",
477  typid);
478 
479  /* prepare the sort function for this dimension */
480  multi_sort_add_dimension(mss, i, type->lt_opr, collid);
481 
482  /* accumulate all the data for this dimension into the arrays */
483  for (j = 0; j < numrows; j++)
484  {
485  items[j].values[i] = data->values[combination[i]][j];
486  items[j].isnull[i] = data->nulls[combination[i]][j];
487  }
488  }
489 
490  /* We can sort the array now ... */
491  qsort_arg((void *) items, numrows, sizeof(SortItem),
492  multi_sort_compare, mss);
493 
494  /* ... and count the number of distinct combinations */
495 
496  f1 = 0;
497  cnt = 1;
498  d = 1;
499  for (i = 1; i < numrows; i++)
500  {
501  if (multi_sort_compare(&items[i], &items[i - 1], mss) != 0)
502  {
503  if (cnt == 1)
504  f1 += 1;
505 
506  d++;
507  cnt = 0;
508  }
509 
510  cnt += 1;
511  }
512 
513  if (cnt == 1)
514  f1 += 1;
515 
516  return estimate_ndistinct(totalrows, numrows, d, f1);
517 }
unsigned int Oid
Definition: postgres_ext.h:31
Oid attrtypid
Definition: vacuum.h:124
#define ERROR
Definition: elog.h:46
void multi_sort_add_dimension(MultiSortSupport mss, int sortdim, Oid oper, Oid collation)
MultiSortSupport multi_sort_init(int ndims)
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
static double estimate_ndistinct(double totalrows, int numrows, int d, int f1)
Definition: mvdistinct.c:521
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
VacAttrStats ** stats
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:339
#define InvalidOid
Definition: postgres_ext.h:36
int multi_sort_compare(const void *a, const void *b, void *arg)
Oid attrcollid
Definition: vacuum.h:127
static Datum values[MAXATTR]
Definition: bootstrap.c:156
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
#define TYPECACHE_LT_OPR
Definition: typcache.h:137
int f1[ARRAY_SIZE]
Definition: sql-declare.c:113

◆ num_combinations()

static int num_combinations ( int  n)
static

Definition at line 575 of file mvdistinct.c.

Referenced by statext_ndistinct_build().

576 {
577  return (1 << n) - (n + 1);
578 }

◆ pg_ndistinct_in()

Datum pg_ndistinct_in ( PG_FUNCTION_ARGS  )

Definition at line 339 of file mvdistinct.c.

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

340 {
341  ereport(ERROR,
342  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
343  errmsg("cannot accept a value of type %s", "pg_ndistinct")));
344 
345  PG_RETURN_VOID(); /* keep compiler quiet */
346 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_VOID()
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_ndistinct_out()

Datum pg_ndistinct_out ( PG_FUNCTION_ARGS  )

Definition at line 355 of file mvdistinct.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), attnum, MVNDistinctItem::attributes, StringInfoData::data, i, initStringInfo(), MVNDistinct::items, MVNDistinctItem::nattributes, MVNDistinctItem::ndistinct, MVNDistinct::nitems, PG_GETARG_BYTEA_PP, PG_RETURN_CSTRING, statext_ndistinct_deserialize(), and generate_unaccent_rules::str.

356 {
357  bytea *data = PG_GETARG_BYTEA_PP(0);
359  int i;
361 
362  initStringInfo(&str);
363  appendStringInfoChar(&str, '{');
364 
365  for (i = 0; i < ndist->nitems; i++)
366  {
367  int j;
368  MVNDistinctItem item = ndist->items[i];
369 
370  if (i > 0)
371  appendStringInfoString(&str, ", ");
372 
373  for (j = 0; j < item.nattributes; j++)
374  {
375  AttrNumber attnum = item.attributes[j];
376 
377  appendStringInfo(&str, "%s%d", (j == 0) ? "\"" : ", ", attnum);
378  }
379  appendStringInfo(&str, "\": %d", (int) item.ndistinct);
380  }
381 
382  appendStringInfoChar(&str, '}');
383 
384  PG_RETURN_CSTRING(str.data);
385 }
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:39
MVNDistinct * statext_ndistinct_deserialize(bytea *data)
Definition: mvdistinct.c:250
double ndistinct
Definition: statistics.h:28
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
uint32 nitems
Definition: statistics.h:38
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
AttrNumber * attributes
Definition: statistics.h:30
int16 attnum
Definition: pg_attribute.h:83
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
int i
Definition: c.h:621
int16 AttrNumber
Definition: attnum.h:21

◆ pg_ndistinct_recv()

Datum pg_ndistinct_recv ( PG_FUNCTION_ARGS  )

Definition at line 392 of file mvdistinct.c.

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

393 {
394  ereport(ERROR,
395  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
396  errmsg("cannot accept a value of type %s", "pg_ndistinct")));
397 
398  PG_RETURN_VOID(); /* keep compiler quiet */
399 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_VOID()
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_ndistinct_send()

Datum pg_ndistinct_send ( PG_FUNCTION_ARGS  )

Definition at line 408 of file mvdistinct.c.

References byteasend().

409 {
410  return byteasend(fcinfo);
411 }
Datum byteasend(PG_FUNCTION_ARGS)
Definition: varlena.c:493

◆ statext_ndistinct_build()

MVNDistinct* statext_ndistinct_build ( double  totalrows,
StatsBuildData data 
)

Definition at line 89 of file mvdistinct.c.

References Assert, StatsBuildData::attnums, AttributeNumberIsValid, MVNDistinctItem::attributes, generator_free(), generator_init(), generator_next(), MVNDistinct::items, CombinationGenerator::k, MVNDistinct::magic, StatsBuildData::nattnums, MVNDistinctItem::nattributes, MVNDistinctItem::ndistinct, ndistinct_for_combination(), MVNDistinct::nitems, num_combinations(), offsetof, palloc(), STATS_NDISTINCT_MAGIC, STATS_NDISTINCT_TYPE_BASIC, and MVNDistinct::type.

Referenced by BuildRelationExtStatistics().

90 {
92  int k;
93  int itemcnt;
94  int numattrs = data->nattnums;
95  int numcombs = num_combinations(numattrs);
96 
97  result = palloc(offsetof(MVNDistinct, items) +
98  numcombs * sizeof(MVNDistinctItem));
99  result->magic = STATS_NDISTINCT_MAGIC;
101  result->nitems = numcombs;
102 
103  itemcnt = 0;
104  for (k = 2; k <= numattrs; k++)
105  {
106  int *combination;
108 
109  /* generate combinations of K out of N elements */
110  generator = generator_init(numattrs, k);
111 
112  while ((combination = generator_next(generator)))
113  {
114  MVNDistinctItem *item = &result->items[itemcnt];
115  int j;
116 
117  item->attributes = palloc(sizeof(AttrNumber) * k);
118  item->nattributes = k;
119 
120  /* translate the indexes to attnums */
121  for (j = 0; j < k; j++)
122  {
123  item->attributes[j] = data->attnums[combination[j]];
124 
126  }
127 
128  item->ndistinct =
129  ndistinct_for_combination(totalrows, data, k, combination);
130 
131  itemcnt++;
132  Assert(itemcnt <= result->nitems);
133  }
134 
135  generator_free(generator);
136  }
137 
138  /* must consume exactly the whole output array */
139  Assert(itemcnt == result->nitems);
140 
141  return result;
142 }
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:39
double ndistinct
Definition: statistics.h:28
static int * generator_next(CombinationGenerator *state)
Definition: mvdistinct.c:627
static double ndistinct_for_combination(double totalrows, StatsBuildData *data, int k, int *combination)
Definition: mvdistinct.c:425
#define STATS_NDISTINCT_TYPE_BASIC
Definition: statistics.h:23
uint32 nitems
Definition: statistics.h:38
uint32 magic
Definition: statistics.h:36
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
static void generator_free(CombinationGenerator *state)
Definition: mvdistinct.c:642
AttrNumber * attributes
Definition: statistics.h:30
int result
Definition: header.h:19
uint32 type
Definition: statistics.h:37
#define Assert(condition)
Definition: c.h:804
#define STATS_NDISTINCT_MAGIC
Definition: statistics.h:22
void * palloc(Size size)
Definition: mcxt.c:1062
static int num_combinations(int n)
Definition: mvdistinct.c:575
int16 AttrNumber
Definition: attnum.h:21
static CombinationGenerator * generator_init(int n, int k)
Definition: mvdistinct.c:589
#define offsetof(type, field)
Definition: c.h:727

◆ statext_ndistinct_deserialize()

MVNDistinct* statext_ndistinct_deserialize ( bytea data)

Definition at line 250 of file mvdistinct.c.

References Assert, MVNDistinctItem::attributes, elog, ERROR, i, MVNDistinct::items, MVNDistinct::magic, MAXALIGN, MinSizeOfItems, MVNDistinctItem::nattributes, MVNDistinctItem::ndistinct, MVNDistinct::nitems, offsetof, palloc(), palloc0(), SizeOfHeader, STATS_MAX_DIMENSIONS, STATS_NDISTINCT_MAGIC, STATS_NDISTINCT_TYPE_BASIC, MVNDistinct::type, VARDATA_ANY, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by pg_ndistinct_out(), and statext_ndistinct_load().

251 {
252  int i;
253  Size minimum_size;
254  MVNDistinct ndist;
255  MVNDistinct *ndistinct;
256  char *tmp;
257 
258  if (data == NULL)
259  return NULL;
260 
261  /* we expect at least the basic fields of MVNDistinct struct */
262  if (VARSIZE_ANY_EXHDR(data) < SizeOfHeader)
263  elog(ERROR, "invalid MVNDistinct size %zd (expected at least %zd)",
265 
266  /* initialize pointer to the data part (skip the varlena header) */
267  tmp = VARDATA_ANY(data);
268 
269  /* read the header fields and perform basic sanity checks */
270  memcpy(&ndist.magic, tmp, sizeof(uint32));
271  tmp += sizeof(uint32);
272  memcpy(&ndist.type, tmp, sizeof(uint32));
273  tmp += sizeof(uint32);
274  memcpy(&ndist.nitems, tmp, sizeof(uint32));
275  tmp += sizeof(uint32);
276 
277  if (ndist.magic != STATS_NDISTINCT_MAGIC)
278  elog(ERROR, "invalid ndistinct magic %08x (expected %08x)",
280  if (ndist.type != STATS_NDISTINCT_TYPE_BASIC)
281  elog(ERROR, "invalid ndistinct type %d (expected %d)",
283  if (ndist.nitems == 0)
284  elog(ERROR, "invalid zero-length item array in MVNDistinct");
285 
286  /* what minimum bytea size do we expect for those parameters */
287  minimum_size = MinSizeOfItems(ndist.nitems);
288  if (VARSIZE_ANY_EXHDR(data) < minimum_size)
289  elog(ERROR, "invalid MVNDistinct size %zd (expected at least %zd)",
290  VARSIZE_ANY_EXHDR(data), minimum_size);
291 
292  /*
293  * Allocate space for the ndistinct items (no space for each item's
294  * attnos: those live in bitmapsets allocated separately)
295  */
296  ndistinct = palloc0(MAXALIGN(offsetof(MVNDistinct, items)) +
297  (ndist.nitems * sizeof(MVNDistinctItem)));
298  ndistinct->magic = ndist.magic;
299  ndistinct->type = ndist.type;
300  ndistinct->nitems = ndist.nitems;
301 
302  for (i = 0; i < ndistinct->nitems; i++)
303  {
304  MVNDistinctItem *item = &ndistinct->items[i];
305 
306  /* ndistinct value */
307  memcpy(&item->ndistinct, tmp, sizeof(double));
308  tmp += sizeof(double);
309 
310  /* number of attributes */
311  memcpy(&item->nattributes, tmp, sizeof(int));
312  tmp += sizeof(int);
313  Assert((item->nattributes >= 2) && (item->nattributes <= STATS_MAX_DIMENSIONS));
314 
315  item->attributes
316  = (AttrNumber *) palloc(item->nattributes * sizeof(AttrNumber));
317 
318  memcpy(item->attributes, tmp, sizeof(AttrNumber) * item->nattributes);
319  tmp += sizeof(AttrNumber) * item->nattributes;
320 
321  /* still within the bytea */
322  Assert(tmp <= ((char *) data + VARSIZE_ANY(data)));
323  }
324 
325  /* we should have consumed the whole bytea exactly */
326  Assert(tmp == ((char *) data + VARSIZE_ANY(data)));
327 
328  return ndistinct;
329 }
#define MinSizeOfItems(nitems)
Definition: mvdistinct.c:56
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:39
double ndistinct
Definition: statistics.h:28
#define STATS_NDISTINCT_TYPE_BASIC
Definition: statistics.h:23
#define ERROR
Definition: elog.h:46
unsigned int uint32
Definition: c.h:441
uint32 nitems
Definition: statistics.h:38
uint32 magic
Definition: statistics.h:36
void * palloc0(Size size)
Definition: mcxt.c:1093
AttrNumber * attributes
Definition: statistics.h:30
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348
uint32 type
Definition: statistics.h:37
#define SizeOfHeader
Definition: mvdistinct.c:46
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
#define STATS_NDISTINCT_MAGIC
Definition: statistics.h:22
#define MAXALIGN(LEN)
Definition: c.h:757
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
int16 AttrNumber
Definition: attnum.h:21
#define offsetof(type, field)
Definition: c.h:727

◆ statext_ndistinct_load()

MVNDistinct* statext_ndistinct_load ( Oid  mvoid)

Definition at line 149 of file mvdistinct.c.

References DatumGetByteaPP, elog, ERROR, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1(), statext_ndistinct_deserialize(), STATEXTDATASTXOID, and SysCacheGetAttr().

Referenced by estimate_multivariate_ndistinct().

150 {
152  bool isnull;
153  Datum ndist;
154  HeapTuple htup;
155 
157  if (!HeapTupleIsValid(htup))
158  elog(ERROR, "cache lookup failed for statistics object %u", mvoid);
159 
160  ndist = SysCacheGetAttr(STATEXTDATASTXOID, htup,
161  Anum_pg_statistic_ext_data_stxdndistinct, &isnull);
162  if (isnull)
163  elog(ERROR,
164  "requested statistics kind \"%c\" is not yet built for statistics object %u",
165  STATS_EXT_NDISTINCT, mvoid);
166 
168 
169  ReleaseSysCache(htup);
170 
171  return result;
172 }
MVNDistinct * statext_ndistinct_deserialize(bytea *data)
Definition: mvdistinct.c:250
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
int result
Definition: header.h:19
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:232

◆ statext_ndistinct_serialize()

bytea* statext_ndistinct_serialize ( MVNDistinct ndistinct)

Definition at line 179 of file mvdistinct.c.

References Assert, MVNDistinctItem::attributes, i, MVNDistinct::items, MVNDistinct::magic, MVNDistinctItem::nattributes, MVNDistinctItem::ndistinct, MVNDistinct::nitems, output(), palloc(), SET_VARSIZE, SizeOfHeader, SizeOfItem, STATS_NDISTINCT_MAGIC, STATS_NDISTINCT_TYPE_BASIC, MVNDistinct::type, VARDATA, and VARHDRSZ.

Referenced by statext_store().

180 {
181  int i;
182  bytea *output;
183  char *tmp;
184  Size len;
185 
186  Assert(ndistinct->magic == STATS_NDISTINCT_MAGIC);
187  Assert(ndistinct->type == STATS_NDISTINCT_TYPE_BASIC);
188 
189  /*
190  * Base size is size of scalar fields in the struct, plus one base struct
191  * for each item, including number of items for each.
192  */
193  len = VARHDRSZ + SizeOfHeader;
194 
195  /* and also include space for the actual attribute numbers */
196  for (i = 0; i < ndistinct->nitems; i++)
197  {
198  int nmembers;
199 
200  nmembers = ndistinct->items[i].nattributes;
201  Assert(nmembers >= 2);
202 
203  len += SizeOfItem(nmembers);
204  }
205 
206  output = (bytea *) palloc(len);
207  SET_VARSIZE(output, len);
208 
209  tmp = VARDATA(output);
210 
211  /* Store the base struct values (magic, type, nitems) */
212  memcpy(tmp, &ndistinct->magic, sizeof(uint32));
213  tmp += sizeof(uint32);
214  memcpy(tmp, &ndistinct->type, sizeof(uint32));
215  tmp += sizeof(uint32);
216  memcpy(tmp, &ndistinct->nitems, sizeof(uint32));
217  tmp += sizeof(uint32);
218 
219  /*
220  * store number of attributes and attribute numbers for each entry
221  */
222  for (i = 0; i < ndistinct->nitems; i++)
223  {
224  MVNDistinctItem item = ndistinct->items[i];
225  int nmembers = item.nattributes;
226 
227  memcpy(tmp, &item.ndistinct, sizeof(double));
228  tmp += sizeof(double);
229  memcpy(tmp, &nmembers, sizeof(int));
230  tmp += sizeof(int);
231 
232  memcpy(tmp, item.attributes, sizeof(AttrNumber) * nmembers);
233  tmp += nmembers * sizeof(AttrNumber);
234 
235  /* protect against overflows */
236  Assert(tmp <= ((char *) output + len));
237  }
238 
239  /* check we used exactly the expected space */
240  Assert(tmp == ((char *) output + len));
241 
242  return output;
243 }
#define VARDATA(PTR)
Definition: postgres.h:315
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:39
static void output(uint64 loop_count)
#define VARHDRSZ
Definition: c.h:627
double ndistinct
Definition: statistics.h:28
#define STATS_NDISTINCT_TYPE_BASIC
Definition: statistics.h:23
#define SizeOfItem(natts)
Definition: mvdistinct.c:49
unsigned int uint32
Definition: c.h:441
uint32 nitems
Definition: statistics.h:38
uint32 magic
Definition: statistics.h:36
AttrNumber * attributes
Definition: statistics.h:30
uint32 type
Definition: statistics.h:37
#define SizeOfHeader
Definition: mvdistinct.c:46
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
#define STATS_NDISTINCT_MAGIC
Definition: statistics.h:22
void * palloc(Size size)
Definition: mcxt.c:1062
int i
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
int16 AttrNumber
Definition: attnum.h:21