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, bool inh)
 
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.

◆ SizeOfHeader

#define SizeOfHeader   (3 * sizeof(uint32))

Definition at line 46 of file mvdistinct.c.

◆ SizeOfItem

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

Definition at line 49 of file mvdistinct.c.

Typedef Documentation

◆ CombinationGenerator

Function Documentation

◆ estimate_ndistinct()

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

Definition at line 522 of file mvdistinct.c.

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

References f1.

Referenced by ndistinct_for_combination().

◆ generate_combinations()

static void generate_combinations ( CombinationGenerator state)
static

Definition at line 693 of file mvdistinct.c.

694 {
695  int *current = (int *) palloc0(sizeof(int) * state->k);
696 
697  generate_combinations_recurse(state, 0, 0, current);
698 
699  pfree(current);
700 }
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc0(Size size)
Definition: mcxt.c:1257
static void generate_combinations_recurse(CombinationGenerator *state, int index, int start, int *current)
Definition: mvdistinct.c:658
Definition: regguts.h:323

References generate_combinations_recurse(), palloc0(), and pfree().

Referenced by generator_init().

◆ generate_combinations_recurse()

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

Definition at line 658 of file mvdistinct.c.

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

References i.

Referenced by generate_combinations().

◆ generator_free()

static void generator_free ( CombinationGenerator state)
static

Definition at line 643 of file mvdistinct.c.

644 {
645  pfree(state->combinations);
646  pfree(state);
647 }

References pfree().

Referenced by statext_ndistinct_build().

◆ generator_init()

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

Definition at line 590 of file mvdistinct.c.

591 {
593 
594  Assert((n >= k) && (k > 0));
595 
596  /* allocate the generator state as a single chunk of memory */
598 
599  state->ncombinations = n_choose_k(n, k);
600 
601  /* pre-allocate space for all combinations */
602  state->combinations = (int *) palloc(sizeof(int) * k * state->ncombinations);
603 
604  state->current = 0;
605  state->k = k;
606  state->n = n;
607 
608  /* now actually pre-generate all the combinations of K elements */
610 
611  /* make sure we got the expected number of combinations */
612  Assert(state->current == state->ncombinations);
613 
614  /* reset the number, so we start with the first one */
615  state->current = 0;
616 
617  return state;
618 }
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc(Size size)
Definition: mcxt.c:1226
static int n_choose_k(int n, int k)
Definition: mvdistinct.c:551
static void generate_combinations(CombinationGenerator *state)
Definition: mvdistinct.c:693

References Assert(), generate_combinations(), n_choose_k(), and palloc().

Referenced by statext_ndistinct_build().

◆ generator_next()

static int * generator_next ( CombinationGenerator state)
static

Definition at line 628 of file mvdistinct.c.

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

Referenced by statext_ndistinct_build().

◆ n_choose_k()

static int n_choose_k ( int  n,
int  k 
)
static

Definition at line 551 of file mvdistinct.c.

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

References Assert(), and Min.

Referenced by generator_init().

◆ ndistinct_for_combination()

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

Definition at line 426 of file mvdistinct.c.

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

References VacAttrStats::attrcollid, VacAttrStats::attrtypid, collid, data, elog(), ERROR, estimate_ndistinct(), f1, i, InvalidOid, SortItem::isnull, j, lookup_type_cache(), multi_sort_add_dimension(), multi_sort_compare(), multi_sort_init(), palloc(), palloc0(), qsort_interruptible(), type, TYPECACHE_LT_OPR, values, and SortItem::values.

Referenced by statext_ndistinct_build().

◆ num_combinations()

static int num_combinations ( int  n)
static

Definition at line 576 of file mvdistinct.c.

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

Referenced by statext_ndistinct_build().

◆ pg_ndistinct_in()

Datum pg_ndistinct_in ( PG_FUNCTION_ARGS  )

Definition at line 340 of file mvdistinct.c.

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

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

◆ pg_ndistinct_out()

Datum pg_ndistinct_out ( PG_FUNCTION_ARGS  )

Definition at line 356 of file mvdistinct.c.

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

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

◆ pg_ndistinct_recv()

Datum pg_ndistinct_recv ( PG_FUNCTION_ARGS  )

Definition at line 393 of file mvdistinct.c.

394 {
395  ereport(ERROR,
396  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
397  errmsg("cannot accept a value of type %s", "pg_ndistinct")));
398 
399  PG_RETURN_VOID(); /* keep compiler quiet */
400 }

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

◆ pg_ndistinct_send()

Datum pg_ndistinct_send ( PG_FUNCTION_ARGS  )

Definition at line 409 of file mvdistinct.c.

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

References byteasend().

◆ statext_ndistinct_build()

MVNDistinct* statext_ndistinct_build ( double  totalrows,
StatsBuildData data 
)

Definition at line 89 of file mvdistinct.c.

90 {
91  MVNDistinct *result;
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 
136  }
137 
138  /* must consume exactly the whole output array */
139  Assert(itemcnt == result->nitems);
140 
141  return result;
142 }
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define nitems(x)
Definition: indent.h:31
static double ndistinct_for_combination(double totalrows, StatsBuildData *data, int k, int *combination)
Definition: mvdistinct.c:426
static int num_combinations(int n)
Definition: mvdistinct.c:576
static void generator_free(CombinationGenerator *state)
Definition: mvdistinct.c:643
static CombinationGenerator * generator_init(int n, int k)
Definition: mvdistinct.c:590
static int * generator_next(CombinationGenerator *state)
Definition: mvdistinct.c:628
#define STATS_NDISTINCT_MAGIC
Definition: statistics.h:22
#define STATS_NDISTINCT_TYPE_BASIC
Definition: statistics.h:23
uint32 type
Definition: statistics.h:37
uint32 magic
Definition: statistics.h:36

References Assert(), AttributeNumberIsValid, MVNDistinctItem::attributes, data, generator_free(), generator_init(), generator_next(), MVNDistinct::items, j, MVNDistinct::magic, MVNDistinctItem::nattributes, MVNDistinctItem::ndistinct, ndistinct_for_combination(), MVNDistinct::nitems, nitems, num_combinations(), palloc(), STATS_NDISTINCT_MAGIC, STATS_NDISTINCT_TYPE_BASIC, and MVNDistinct::type.

Referenced by BuildRelationExtStatistics().

◆ statext_ndistinct_deserialize()

MVNDistinct* statext_ndistinct_deserialize ( bytea data)

Definition at line 251 of file mvdistinct.c.

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

References Assert(), MVNDistinctItem::attributes, data, elog(), ERROR, i, MVNDistinct::items, MVNDistinct::magic, MAXALIGN, MinSizeOfItems, MVNDistinctItem::nattributes, MVNDistinctItem::ndistinct, MVNDistinct::nitems, 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().

◆ statext_ndistinct_load()

MVNDistinct* statext_ndistinct_load ( Oid  mvoid,
bool  inh 
)

Definition at line 149 of file mvdistinct.c.

150 {
151  MVNDistinct *result;
152  bool isnull;
153  Datum ndist;
154  HeapTuple htup;
155 
157  ObjectIdGetDatum(mvoid), BoolGetDatum(inh));
158  if (!HeapTupleIsValid(htup))
159  elog(ERROR, "cache lookup failed for statistics object %u", mvoid);
160 
161  ndist = SysCacheGetAttr(STATEXTDATASTXOID, htup,
162  Anum_pg_statistic_ext_data_stxdndistinct, &isnull);
163  if (isnull)
164  elog(ERROR,
165  "requested statistics kind \"%c\" is not yet built for statistics object %u",
166  STATS_EXT_NDISTINCT, mvoid);
167 
169 
170  ReleaseSysCache(htup);
171 
172  return result;
173 }
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1081
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:831
@ STATEXTDATASTXOID
Definition: syscache.h:94

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

Referenced by estimate_multivariate_ndistinct().

◆ statext_ndistinct_serialize()

bytea* statext_ndistinct_serialize ( MVNDistinct ndistinct)

Definition at line 180 of file mvdistinct.c.

181 {
182  int i;
183  bytea *output;
184  char *tmp;
185  Size len;
186 
187  Assert(ndistinct->magic == STATS_NDISTINCT_MAGIC);
188  Assert(ndistinct->type == STATS_NDISTINCT_TYPE_BASIC);
189 
190  /*
191  * Base size is size of scalar fields in the struct, plus one base struct
192  * for each item, including number of items for each.
193  */
195 
196  /* and also include space for the actual attribute numbers */
197  for (i = 0; i < ndistinct->nitems; i++)
198  {
199  int nmembers;
200 
201  nmembers = ndistinct->items[i].nattributes;
202  Assert(nmembers >= 2);
203 
204  len += SizeOfItem(nmembers);
205  }
206 
207  output = (bytea *) palloc(len);
209 
210  tmp = VARDATA(output);
211 
212  /* Store the base struct values (magic, type, nitems) */
213  memcpy(tmp, &ndistinct->magic, sizeof(uint32));
214  tmp += sizeof(uint32);
215  memcpy(tmp, &ndistinct->type, sizeof(uint32));
216  tmp += sizeof(uint32);
217  memcpy(tmp, &ndistinct->nitems, sizeof(uint32));
218  tmp += sizeof(uint32);
219 
220  /*
221  * store number of attributes and attribute numbers for each entry
222  */
223  for (i = 0; i < ndistinct->nitems; i++)
224  {
225  MVNDistinctItem item = ndistinct->items[i];
226  int nmembers = item.nattributes;
227 
228  memcpy(tmp, &item.ndistinct, sizeof(double));
229  tmp += sizeof(double);
230  memcpy(tmp, &nmembers, sizeof(int));
231  tmp += sizeof(int);
232 
233  memcpy(tmp, item.attributes, sizeof(AttrNumber) * nmembers);
234  tmp += nmembers * sizeof(AttrNumber);
235 
236  /* protect against overflows */
237  Assert(tmp <= ((char *) output + len));
238  }
239 
240  /* check we used exactly the expected space */
241  Assert(tmp == ((char *) output + len));
242 
243  return output;
244 }
#define VARHDRSZ
Definition: c.h:681
FILE * output
#define SizeOfItem(natts)
Definition: mvdistinct.c:49
const void size_t len
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References Assert(), MVNDistinctItem::attributes, i, MVNDistinct::items, len, 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().