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 "utils/fmgrprotos.h"
#include "utils/lsyscache.h"
#include "lib/stringinfo.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "statistics/extended_stats_internal.h"
#include "statistics/statistics.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, int numrows, HeapTuple *rows, VacAttrStats **stats, 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, int numrows, HeapTuple *rows, Bitmapset *attrs, VacAttrStats **stats)
 
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 55 of file mvdistinct.c.

◆ MinSizeOfItems

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

Definition at line 58 of file mvdistinct.c.

Referenced by statext_ndistinct_deserialize().

◆ SizeOfHeader

#define SizeOfHeader   (3 * sizeof(uint32))

Definition at line 48 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 51 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 524 of file mvdistinct.c.

Referenced by ndistinct_for_combination().

525 {
526  double numer,
527  denom,
528  ndistinct;
529 
530  numer = (double) numrows * (double) d;
531 
532  denom = (double) (numrows - f1) +
533  (double) f1 * (double) numrows / totalrows;
534 
535  ndistinct = numer / denom;
536 
537  /* Clamp to sane range in case of roundoff error */
538  if (ndistinct < (double) d)
539  ndistinct = (double) d;
540 
541  if (ndistinct > totalrows)
542  ndistinct = totalrows;
543 
544  return floor(ndistinct + 0.5);
545 }

◆ generate_combinations()

static void generate_combinations ( CombinationGenerator state)
static

Definition at line 703 of file mvdistinct.c.

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

Referenced by generator_init().

704 {
705  int *current = (int *) palloc0(sizeof(int) * state->k);
706 
707  generate_combinations_recurse(state, 0, 0, current);
708 
709  pfree(current);
710 }
void pfree(void *pointer)
Definition: mcxt.c:1056
void * palloc0(Size size)
Definition: mcxt.c:980
static void generate_combinations_recurse(CombinationGenerator *state, int index, int start, int *current)
Definition: mvdistinct.c:668

◆ generate_combinations_recurse()

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

Definition at line 668 of file mvdistinct.c.

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

Referenced by generate_combinations().

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

◆ generator_free()

static void generator_free ( CombinationGenerator state)
static

Definition at line 653 of file mvdistinct.c.

References CombinationGenerator::combinations, and pfree().

Referenced by statext_ndistinct_build().

654 {
655  pfree(state->combinations);
656  pfree(state);
657 }
void pfree(void *pointer)
Definition: mcxt.c:1056

◆ generator_init()

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

Definition at line 600 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().

601 {
603 
604  Assert((n >= k) && (k > 0));
605 
606  /* allocate the generator state as a single chunk of memory */
607  state = (CombinationGenerator *) palloc(sizeof(CombinationGenerator));
608 
609  state->ncombinations = n_choose_k(n, k);
610 
611  /* pre-allocate space for all combinations */
612  state->combinations = (int *) palloc(sizeof(int) * k * state->ncombinations);
613 
614  state->current = 0;
615  state->k = k;
616  state->n = n;
617 
618  /* now actually pre-generate all the combinations of K elements */
619  generate_combinations(state);
620 
621  /* make sure we got the expected number of combinations */
622  Assert(state->current == state->ncombinations);
623 
624  /* reset the number, so we start with the first one */
625  state->current = 0;
626 
627  return state;
628 }
static void generate_combinations(CombinationGenerator *state)
Definition: mvdistinct.c:703
static int n_choose_k(int n, int k)
Definition: mvdistinct.c:553
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
void * palloc(Size size)
Definition: mcxt.c:949

◆ generator_next()

static int * generator_next ( CombinationGenerator state)
static

Definition at line 638 of file mvdistinct.c.

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

Referenced by statext_ndistinct_build().

639 {
640  if (state->current == state->ncombinations)
641  return NULL;
642 
643  return &state->combinations[state->k * state->current++];
644 }

◆ n_choose_k()

static int n_choose_k ( int  n,
int  k 
)
static

Definition at line 553 of file mvdistinct.c.

References Assert, CombinationGenerator::k, and Min.

Referenced by generator_init().

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

◆ ndistinct_for_combination()

static double ndistinct_for_combination ( double  totalrows,
int  numrows,
HeapTuple rows,
VacAttrStats **  stats,
int  k,
int *  combination 
)
static

Definition at line 431 of file mvdistinct.c.

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

Referenced by statext_ndistinct_build().

433 {
434  int i,
435  j;
436  int f1,
437  cnt,
438  d;
439  bool *isnull;
440  Datum *values;
441  SortItem *items;
442  MultiSortSupport mss;
443 
444  mss = multi_sort_init(k);
445 
446  /*
447  * In order to determine the number of distinct elements, create separate
448  * values[]/isnull[] arrays with all the data we have, then sort them
449  * using the specified column combination as dimensions. We could try to
450  * sort in place, but it'd probably be more complex and bug-prone.
451  */
452  items = (SortItem *) palloc(numrows * sizeof(SortItem));
453  values = (Datum *) palloc0(sizeof(Datum) * numrows * k);
454  isnull = (bool *) palloc0(sizeof(bool) * numrows * k);
455 
456  for (i = 0; i < numrows; i++)
457  {
458  items[i].values = &values[i * k];
459  items[i].isnull = &isnull[i * k];
460  }
461 
462  /*
463  * For each dimension, set up sort-support and fill in the values from the
464  * sample data.
465  *
466  * We use the column data types' default sort operators and collations;
467  * perhaps at some point it'd be worth using column-specific collations?
468  */
469  for (i = 0; i < k; i++)
470  {
471  VacAttrStats *colstat = stats[combination[i]];
473 
474  type = lookup_type_cache(colstat->attrtypid, 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  colstat->attrtypid);
478 
479  /* prepare the sort function for this dimension */
480  multi_sort_add_dimension(mss, i, type->lt_opr, colstat->attrcollid);
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] =
486  heap_getattr(rows[j],
487  colstat->attr->attnum,
488  colstat->tupDesc,
489  &items[j].isnull[i]);
490  }
491  }
492 
493  /* We can sort the array now ... */
494  qsort_arg((void *) items, numrows, sizeof(SortItem),
495  multi_sort_compare, mss);
496 
497  /* ... and count the number of distinct combinations */
498 
499  f1 = 0;
500  cnt = 1;
501  d = 1;
502  for (i = 1; i < numrows; i++)
503  {
504  if (multi_sort_compare(&items[i], &items[i - 1], mss) != 0)
505  {
506  if (cnt == 1)
507  f1 += 1;
508 
509  d++;
510  cnt = 0;
511  }
512 
513  cnt += 1;
514  }
515 
516  if (cnt == 1)
517  f1 += 1;
518 
519  return estimate_ndistinct(totalrows, numrows, d, f1);
520 }
TupleDesc tupDesc
Definition: vacuum.h:133
Form_pg_attribute attr
Definition: vacuum.h:85
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)
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
static double estimate_ndistinct(double totalrows, int numrows, int d, int f1)
Definition: mvdistinct.c:524
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 InvalidOid
Definition: postgres_ext.h:36
int multi_sort_compare(const void *a, const void *b, void *arg)
Oid attrcollid
Definition: vacuum.h:89
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:226
int i
#define TYPECACHE_LT_OPR
Definition: typcache.h:129

◆ num_combinations()

static int num_combinations ( int  n)
static

Definition at line 578 of file mvdistinct.c.

References CombinationGenerator::k, and CombinationGenerator::n.

Referenced by statext_ndistinct_build().

579 {
580  int k;
581  int ncombs = 1;
582 
583  for (k = 1; k <= n; k++)
584  ncombs *= 2;
585 
586  ncombs -= (n + 1);
587 
588  return ncombs;
589 }

◆ pg_ndistinct_in()

Datum pg_ndistinct_in ( PG_FUNCTION_ARGS  )

Definition at line 345 of file mvdistinct.c.

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

346 {
347  ereport(ERROR,
348  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
349  errmsg("cannot accept a value of type %s", "pg_ndistinct")));
350 
351  PG_RETURN_VOID(); /* keep compiler quiet */
352 }
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_ndistinct_out()

Datum pg_ndistinct_out ( PG_FUNCTION_ARGS  )

Definition at line 361 of file mvdistinct.c.

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

362 {
363  bytea *data = PG_GETARG_BYTEA_PP(0);
365  int i;
367 
368  initStringInfo(&str);
369  appendStringInfoChar(&str, '{');
370 
371  for (i = 0; i < ndist->nitems; i++)
372  {
373  MVNDistinctItem item = ndist->items[i];
374  int x = -1;
375  bool first = true;
376 
377  if (i > 0)
378  appendStringInfoString(&str, ", ");
379 
380  while ((x = bms_next_member(item.attrs, x)) >= 0)
381  {
382  appendStringInfo(&str, "%s%d", first ? "\"" : ", ", x);
383  first = false;
384  }
385  appendStringInfo(&str, "\": %d", (int) item.ndistinct);
386  }
387 
388  appendStringInfoChar(&str, '}');
389 
390  PG_RETURN_CSTRING(str.data);
391 }
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:38
MVNDistinct * statext_ndistinct_deserialize(bytea *data)
Definition: mvdistinct.c:250
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
double ndistinct
Definition: statistics.h:28
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:163
uint32 nitems
Definition: statistics.h:37
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:175
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
Bitmapset * attrs
Definition: statistics.h:29
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
int i
Definition: c.h:549

◆ pg_ndistinct_recv()

Datum pg_ndistinct_recv ( PG_FUNCTION_ARGS  )

Definition at line 398 of file mvdistinct.c.

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

399 {
400  ereport(ERROR,
401  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
402  errmsg("cannot accept a value of type %s", "pg_ndistinct")));
403 
404  PG_RETURN_VOID(); /* keep compiler quiet */
405 }
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_ndistinct_send()

Datum pg_ndistinct_send ( PG_FUNCTION_ARGS  )

Definition at line 414 of file mvdistinct.c.

References byteasend().

415 {
416  return byteasend(fcinfo);
417 }
Datum byteasend(PG_FUNCTION_ARGS)
Definition: varlena.c:464

◆ statext_ndistinct_build()

MVNDistinct* statext_ndistinct_build ( double  totalrows,
int  numrows,
HeapTuple rows,
Bitmapset attrs,
VacAttrStats **  stats 
)

Definition at line 87 of file mvdistinct.c.

References Assert, VacAttrStats::attr, MVNDistinctItem::attrs, bms_add_member(), bms_num_members(), generator_free(), generator_init(), generator_next(), MVNDistinct::items, CombinationGenerator::k, MVNDistinct::magic, MVNDistinctItem::ndistinct, ndistinct_for_combination(), MVNDistinct::nitems, num_combinations(), offsetof, palloc(), STATS_NDISTINCT_MAGIC, STATS_NDISTINCT_TYPE_BASIC, and MVNDistinct::type.

Referenced by BuildRelationExtStatistics().

89 {
91  int k;
92  int itemcnt;
93  int numattrs = bms_num_members(attrs);
94  int numcombs = num_combinations(numattrs);
95 
96  result = palloc(offsetof(MVNDistinct, items) +
97  numcombs * sizeof(MVNDistinctItem));
98  result->magic = STATS_NDISTINCT_MAGIC;
100  result->nitems = numcombs;
101 
102  itemcnt = 0;
103  for (k = 2; k <= numattrs; k++)
104  {
105  int *combination;
107 
108  /* generate combinations of K out of N elements */
109  generator = generator_init(numattrs, k);
110 
111  while ((combination = generator_next(generator)))
112  {
113  MVNDistinctItem *item = &result->items[itemcnt];
114  int j;
115 
116  item->attrs = NULL;
117  for (j = 0; j < k; j++)
118  item->attrs = bms_add_member(item->attrs,
119  stats[combination[j]]->attr->attnum);
120  item->ndistinct =
121  ndistinct_for_combination(totalrows, numrows, rows,
122  stats, k, combination);
123 
124  itemcnt++;
125  Assert(itemcnt <= result->nitems);
126  }
127 
128  generator_free(generator);
129  }
130 
131  /* must consume exactly the whole output array */
132  Assert(itemcnt == result->nitems);
133 
134  return result;
135 }
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:38
static double ndistinct_for_combination(double totalrows, int numrows, HeapTuple *rows, VacAttrStats **stats, int k, int *combination)
Definition: mvdistinct.c:431
double ndistinct
Definition: statistics.h:28
static int * generator_next(CombinationGenerator *state)
Definition: mvdistinct.c:638
Form_pg_attribute attr
Definition: vacuum.h:85
#define STATS_NDISTINCT_TYPE_BASIC
Definition: statistics.h:23
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
uint32 nitems
Definition: statistics.h:37
uint32 magic
Definition: statistics.h:35
static void generator_free(CombinationGenerator *state)
Definition: mvdistinct.c:653
int result
Definition: header.h:19
uint32 type
Definition: statistics.h:36
#define Assert(condition)
Definition: c.h:732
Bitmapset * attrs
Definition: statistics.h:29
#define STATS_NDISTINCT_MAGIC
Definition: statistics.h:22
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
void * palloc(Size size)
Definition: mcxt.c:949
static int num_combinations(int n)
Definition: mvdistinct.c:578
static CombinationGenerator * generator_init(int n, int k)
Definition: mvdistinct.c:600
#define offsetof(type, field)
Definition: c.h:655

◆ statext_ndistinct_deserialize()

MVNDistinct* statext_ndistinct_deserialize ( bytea data)

Definition at line 250 of file mvdistinct.c.

References Assert, MVNDistinctItem::attrs, bms_add_member(), elog, ERROR, i, MVNDistinct::items, MVNDistinct::magic, MAXALIGN, MinSizeOfItems, MVNDistinctItem::ndistinct, MVNDistinct::nitems, offsetof, 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  int nelems;
306 
307  item->attrs = NULL;
308 
309  /* ndistinct value */
310  memcpy(&item->ndistinct, tmp, sizeof(double));
311  tmp += sizeof(double);
312 
313  /* number of attributes */
314  memcpy(&nelems, tmp, sizeof(int));
315  tmp += sizeof(int);
316  Assert((nelems >= 2) && (nelems <= STATS_MAX_DIMENSIONS));
317 
318  while (nelems-- > 0)
319  {
320  AttrNumber attno;
321 
322  memcpy(&attno, tmp, sizeof(AttrNumber));
323  tmp += sizeof(AttrNumber);
324  item->attrs = bms_add_member(item->attrs, attno);
325  }
326 
327  /* still within the bytea */
328  Assert(tmp <= ((char *) data + VARSIZE_ANY(data)));
329  }
330 
331  /* we should have consumed the whole bytea exactly */
332  Assert(tmp == ((char *) data + VARSIZE_ANY(data)));
333 
334  return ndistinct;
335 }
#define MinSizeOfItems(nitems)
Definition: mvdistinct.c:58
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:38
double ndistinct
Definition: statistics.h:28
#define STATS_NDISTINCT_TYPE_BASIC
Definition: statistics.h:23
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:358
uint32 nitems
Definition: statistics.h:37
uint32 magic
Definition: statistics.h:35
void * palloc0(Size size)
Definition: mcxt.c:980
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
uint32 type
Definition: statistics.h:36
#define SizeOfHeader
Definition: mvdistinct.c:48
#define Assert(condition)
Definition: c.h:732
size_t Size
Definition: c.h:466
Bitmapset * attrs
Definition: statistics.h:29
#define STATS_NDISTINCT_MAGIC
Definition: statistics.h:22
#define MAXALIGN(LEN)
Definition: c.h:685
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define elog(elevel,...)
Definition: elog.h:226
int i
int16 AttrNumber
Definition: attnum.h:21
#define offsetof(type, field)
Definition: c.h:655

◆ statext_ndistinct_load()

MVNDistinct* statext_ndistinct_load ( Oid  mvoid)

Definition at line 142 of file mvdistinct.c.

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

Referenced by estimate_multivariate_ndistinct().

143 {
145  bool isnull;
146  Datum ndist;
147  HeapTuple htup;
148 
150  if (!HeapTupleIsValid(htup))
151  elog(ERROR, "cache lookup failed for statistics object %u", mvoid);
152 
153  ndist = SysCacheGetAttr(STATEXTDATASTXOID, htup,
154  Anum_pg_statistic_ext_data_stxdndistinct, &isnull);
155  if (isnull)
156  elog(ERROR,
157  "requested statistic kind \"%c\" is not yet built for statistics object %u",
158  STATS_EXT_NDISTINCT, mvoid);
159 
161 
162  ReleaseSysCache(htup);
163 
164  return result;
165 }
MVNDistinct * statext_ndistinct_deserialize(bytea *data)
Definition: mvdistinct.c:250
#define DatumGetByteaPP(X)
Definition: fmgr.h:285
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
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_ndistinct_serialize()

bytea* statext_ndistinct_serialize ( MVNDistinct ndistinct)

Definition at line 172 of file mvdistinct.c.

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

Referenced by statext_store().

173 {
174  int i;
175  bytea *output;
176  char *tmp;
177  Size len;
178 
179  Assert(ndistinct->magic == STATS_NDISTINCT_MAGIC);
180  Assert(ndistinct->type == STATS_NDISTINCT_TYPE_BASIC);
181 
182  /*
183  * Base size is size of scalar fields in the struct, plus one base struct
184  * for each item, including number of items for each.
185  */
186  len = VARHDRSZ + SizeOfHeader;
187 
188  /* and also include space for the actual attribute numbers */
189  for (i = 0; i < ndistinct->nitems; i++)
190  {
191  int nmembers;
192 
193  nmembers = bms_num_members(ndistinct->items[i].attrs);
194  Assert(nmembers >= 2);
195 
196  len += SizeOfItem(nmembers);
197  }
198 
199  output = (bytea *) palloc(len);
200  SET_VARSIZE(output, len);
201 
202  tmp = VARDATA(output);
203 
204  /* Store the base struct values (magic, type, nitems) */
205  memcpy(tmp, &ndistinct->magic, sizeof(uint32));
206  tmp += sizeof(uint32);
207  memcpy(tmp, &ndistinct->type, sizeof(uint32));
208  tmp += sizeof(uint32);
209  memcpy(tmp, &ndistinct->nitems, sizeof(uint32));
210  tmp += sizeof(uint32);
211 
212  /*
213  * store number of attributes and attribute numbers for each entry
214  */
215  for (i = 0; i < ndistinct->nitems; i++)
216  {
217  MVNDistinctItem item = ndistinct->items[i];
218  int nmembers = bms_num_members(item.attrs);
219  int x;
220 
221  memcpy(tmp, &item.ndistinct, sizeof(double));
222  tmp += sizeof(double);
223  memcpy(tmp, &nmembers, sizeof(int));
224  tmp += sizeof(int);
225 
226  x = -1;
227  while ((x = bms_next_member(item.attrs, x)) >= 0)
228  {
230 
231  memcpy(tmp, &value, sizeof(AttrNumber));
232  tmp += sizeof(AttrNumber);
233  }
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:302
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:38
static void output(uint64 loop_count)
#define VARHDRSZ
Definition: c.h:555
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
double ndistinct
Definition: statistics.h:28
static struct @145 value
#define STATS_NDISTINCT_TYPE_BASIC
Definition: statistics.h:23
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
#define SizeOfItem(natts)
Definition: mvdistinct.c:51
unsigned int uint32
Definition: c.h:358
uint32 nitems
Definition: statistics.h:37
uint32 magic
Definition: statistics.h:35
uint32 type
Definition: statistics.h:36
#define SizeOfHeader
Definition: mvdistinct.c:48
#define Assert(condition)
Definition: c.h:732
size_t Size
Definition: c.h:466
Bitmapset * attrs
Definition: statistics.h:29
#define STATS_NDISTINCT_MAGIC
Definition: statistics.h:22
void * palloc(Size size)
Definition: mcxt.c:949
int i
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
int16 AttrNumber
Definition: attnum.h:21