PostgreSQL Source Code  git master
array_typanalyze.c File Reference
#include "postgres.h"
#include "access/detoast.h"
#include "commands/vacuum.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
Include dependency graph for array_typanalyze.c:

Go to the source code of this file.

Data Structures

struct  ArrayAnalyzeExtraData
 
struct  TrackItem
 
struct  DECountItem
 

Macros

#define ARRAY_WIDTH_THRESHOLD   0x10000
 

Functions

static void compute_array_stats (VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
 
static void prune_element_hashtable (HTAB *elements_tab, int b_current)
 
static uint32 element_hash (const void *key, Size keysize)
 
static int element_match (const void *key1, const void *key2, Size keysize)
 
static int element_compare (const void *key1, const void *key2)
 
static int trackitem_compare_frequencies_desc (const void *e1, const void *e2)
 
static int trackitem_compare_element (const void *e1, const void *e2)
 
static int countitem_compare_count (const void *e1, const void *e2)
 
Datum array_typanalyze (PG_FUNCTION_ARGS)
 

Variables

static ArrayAnalyzeExtraDataarray_extra_data
 

Macro Definition Documentation

◆ ARRAY_WIDTH_THRESHOLD

#define ARRAY_WIDTH_THRESHOLD   0x10000

Definition at line 33 of file array_typanalyze.c.

Referenced by compute_array_stats().

Function Documentation

◆ array_typanalyze()

Datum array_typanalyze ( PG_FUNCTION_ARGS  )

Definition at line 98 of file array_typanalyze.c.

References VacAttrStats::attrcollid, VacAttrStats::attrtypid, ArrayAnalyzeExtraData::cmp, TypeCacheEntry::cmp_proc_finfo, ArrayAnalyzeExtraData::coll_id, compute_array_stats(), VacAttrStats::compute_stats, elog, ArrayAnalyzeExtraData::eq_opr, TypeCacheEntry::eq_opr, ERROR, VacAttrStats::extra_data, FmgrInfo::fn_oid, get_base_element_type(), ArrayAnalyzeExtraData::hash, TypeCacheEntry::hash_proc_finfo, lookup_type_cache(), OidIsValid, palloc(), PG_GETARG_POINTER, PG_RETURN_BOOL, ArrayAnalyzeExtraData::std_compute_stats, ArrayAnalyzeExtraData::std_extra_data, std_typanalyze(), TypeCacheEntry::typalign, ArrayAnalyzeExtraData::typalign, TypeCacheEntry::typbyval, ArrayAnalyzeExtraData::typbyval, TypeCacheEntry::type_id, ArrayAnalyzeExtraData::type_id, TYPECACHE_CMP_PROC_FINFO, TYPECACHE_EQ_OPR, TYPECACHE_HASH_PROC_FINFO, TypeCacheEntry::typlen, and ArrayAnalyzeExtraData::typlen.

99 {
101  Oid element_typeid;
102  TypeCacheEntry *typentry;
103  ArrayAnalyzeExtraData *extra_data;
104 
105  /*
106  * Call the standard typanalyze function. It may fail to find needed
107  * operators, in which case we also can't do anything, so just fail.
108  */
109  if (!std_typanalyze(stats))
110  PG_RETURN_BOOL(false);
111 
112  /*
113  * Check attribute data type is a varlena array (or a domain over one).
114  */
115  element_typeid = get_base_element_type(stats->attrtypid);
116  if (!OidIsValid(element_typeid))
117  elog(ERROR, "array_typanalyze was invoked for non-array type %u",
118  stats->attrtypid);
119 
120  /*
121  * Gather information about the element type. If we fail to find
122  * something, return leaving the state from std_typanalyze() in place.
123  */
124  typentry = lookup_type_cache(element_typeid,
128 
129  if (!OidIsValid(typentry->eq_opr) ||
130  !OidIsValid(typentry->cmp_proc_finfo.fn_oid) ||
131  !OidIsValid(typentry->hash_proc_finfo.fn_oid))
132  PG_RETURN_BOOL(true);
133 
134  /* Store our findings for use by compute_array_stats() */
135  extra_data = (ArrayAnalyzeExtraData *) palloc(sizeof(ArrayAnalyzeExtraData));
136  extra_data->type_id = typentry->type_id;
137  extra_data->eq_opr = typentry->eq_opr;
138  extra_data->coll_id = stats->attrcollid; /* collation we should use */
139  extra_data->typbyval = typentry->typbyval;
140  extra_data->typlen = typentry->typlen;
141  extra_data->typalign = typentry->typalign;
142  extra_data->cmp = &typentry->cmp_proc_finfo;
143  extra_data->hash = &typentry->hash_proc_finfo;
144 
145  /* Save old compute_stats and extra_data for scalar statistics ... */
146  extra_data->std_compute_stats = stats->compute_stats;
147  extra_data->std_extra_data = stats->extra_data;
148 
149  /* ... and replace with our info */
151  stats->extra_data = extra_data;
152 
153  /*
154  * Note we leave stats->minrows set as std_typanalyze set it. Should it
155  * be increased for array analysis purposes?
156  */
157 
158  PG_RETURN_BOOL(true);
159 }
#define TYPECACHE_HASH_PROC_FINFO
Definition: typcache.h:135
static void compute_array_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
#define TYPECACHE_EQ_OPR
Definition: typcache.h:128
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
bool std_typanalyze(VacAttrStats *stats)
Definition: analyze.c:1660
unsigned int Oid
Definition: postgres_ext.h:31
int16 typlen
Definition: typcache.h:37
bool typbyval
Definition: typcache.h:38
#define OidIsValid(objectId)
Definition: c.h:638
FmgrInfo cmp_proc_finfo
Definition: typcache.h:73
Oid attrtypid
Definition: vacuum.h:86
#define ERROR
Definition: elog.h:43
AnalyzeAttrComputeStatsFunc std_compute_stats
FmgrInfo hash_proc_finfo
Definition: typcache.h:74
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:322
Oid fn_oid
Definition: fmgr.h:59
Oid attrcollid
Definition: vacuum.h:89
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2599
char typalign
Definition: typcache.h:39
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:226
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:96
#define TYPECACHE_CMP_PROC_FINFO
Definition: typcache.h:134
void * extra_data
Definition: vacuum.h:98

◆ compute_array_stats()

static void compute_array_stats ( VacAttrStats stats,
AnalyzeAttrFetchFunc  fetchfunc,
int  samplerows,
double  totalrows 
)
static

Definition at line 216 of file array_typanalyze.c.

References VacAttrStats::anl_context, ARR_ELEMTYPE, ARRAY_WIDTH_THRESHOLD, Assert, VacAttrStats::attr, ArrayAnalyzeExtraData::coll_id, DECountItem::count, countitem_compare_count(), CurrentMemoryContext, datumCopy(), DatumGetArrayTypeP, DEBUG3, deconstruct_array(), TrackItem::delta, element_hash(), element_match(), elog, HASHCTL::entrysize, ArrayAnalyzeExtraData::eq_opr, ERROR, VacAttrStats::extra_data, TrackItem::frequency, DECountItem::frequency, HASHCTL::hash, HASH_BLOBS, HASH_COMPARE, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FUNCTION, hash_get_num_entries(), hash_search(), hash_seq_init(), hash_seq_search(), HASHCTL::hcxt, i, INT64_FORMAT, TrackItem::key, HASHCTL::keysize, TrackItem::last_container, HASHCTL::match, Max, MemoryContextAlloc(), MemoryContextSwitchTo(), MemSet, Min, VacAttrStats::numnumbers, VacAttrStats::numvalues, palloc(), pfree(), PointerGetDatum, prune_element_hashtable(), qsort, VacAttrStats::stacoll, VacAttrStats::stakind, VacAttrStats::stanumbers, VacAttrStats::staop, STATISTIC_NUM_SLOTS, VacAttrStats::statypalign, VacAttrStats::statypbyval, VacAttrStats::statypid, VacAttrStats::statyplen, VacAttrStats::stavalues, ArrayAnalyzeExtraData::std_compute_stats, ArrayAnalyzeExtraData::std_extra_data, toast_raw_datum_size(), trackitem_compare_element(), trackitem_compare_frequencies_desc(), ArrayAnalyzeExtraData::typalign, ArrayAnalyzeExtraData::typbyval, ArrayAnalyzeExtraData::type_id, ArrayAnalyzeExtraData::typlen, vacuum_delay_point(), and value.

Referenced by array_typanalyze().

218 {
219  ArrayAnalyzeExtraData *extra_data;
220  int num_mcelem;
221  int null_cnt = 0;
222  int null_elem_cnt = 0;
223  int analyzed_rows = 0;
224 
225  /* This is D from the LC algorithm. */
226  HTAB *elements_tab;
227  HASHCTL elem_hash_ctl;
228  HASH_SEQ_STATUS scan_status;
229 
230  /* This is the current bucket number from the LC algorithm */
231  int b_current;
232 
233  /* This is 'w' from the LC algorithm */
234  int bucket_width;
235  int array_no;
236  int64 element_no;
237  TrackItem *item;
238  int slot_idx;
239  HTAB *count_tab;
240  HASHCTL count_hash_ctl;
241  DECountItem *count_item;
242 
243  extra_data = (ArrayAnalyzeExtraData *) stats->extra_data;
244 
245  /*
246  * Invoke analyze.c's standard analysis function to create scalar-style
247  * stats for the column. It will expect its own extra_data pointer, so
248  * temporarily install that.
249  */
250  stats->extra_data = extra_data->std_extra_data;
251  extra_data->std_compute_stats(stats, fetchfunc, samplerows, totalrows);
252  stats->extra_data = extra_data;
253 
254  /*
255  * Set up static pointer for use by subroutines. We wait till here in
256  * case std_compute_stats somehow recursively invokes us (probably not
257  * possible, but ...)
258  */
259  array_extra_data = extra_data;
260 
261  /*
262  * We want statistics_target * 10 elements in the MCELEM array. This
263  * multiplier is pretty arbitrary, but is meant to reflect the fact that
264  * the number of individual elements tracked in pg_statistic ought to be
265  * more than the number of values for a simple scalar column.
266  */
267  num_mcelem = stats->attr->attstattarget * 10;
268 
269  /*
270  * We set bucket width equal to num_mcelem / 0.007 as per the comment
271  * above.
272  */
273  bucket_width = num_mcelem * 1000 / 7;
274 
275  /*
276  * Create the hashtable. It will be in local memory, so we don't need to
277  * worry about overflowing the initial size. Also we don't need to pay any
278  * attention to locking and memory management.
279  */
280  MemSet(&elem_hash_ctl, 0, sizeof(elem_hash_ctl));
281  elem_hash_ctl.keysize = sizeof(Datum);
282  elem_hash_ctl.entrysize = sizeof(TrackItem);
283  elem_hash_ctl.hash = element_hash;
284  elem_hash_ctl.match = element_match;
285  elem_hash_ctl.hcxt = CurrentMemoryContext;
286  elements_tab = hash_create("Analyzed elements table",
287  num_mcelem,
288  &elem_hash_ctl,
290 
291  /* hashtable for array distinct elements counts */
292  MemSet(&count_hash_ctl, 0, sizeof(count_hash_ctl));
293  count_hash_ctl.keysize = sizeof(int);
294  count_hash_ctl.entrysize = sizeof(DECountItem);
295  count_hash_ctl.hcxt = CurrentMemoryContext;
296  count_tab = hash_create("Array distinct element count table",
297  64,
298  &count_hash_ctl,
300 
301  /* Initialize counters. */
302  b_current = 1;
303  element_no = 0;
304 
305  /* Loop over the arrays. */
306  for (array_no = 0; array_no < samplerows; array_no++)
307  {
308  Datum value;
309  bool isnull;
310  ArrayType *array;
311  int num_elems;
312  Datum *elem_values;
313  bool *elem_nulls;
314  bool null_present;
315  int j;
316  int64 prev_element_no = element_no;
317  int distinct_count;
318  bool count_item_found;
319 
321 
322  value = fetchfunc(stats, array_no, &isnull);
323  if (isnull)
324  {
325  /* array is null, just count that */
326  null_cnt++;
327  continue;
328  }
329 
330  /* Skip too-large values. */
332  continue;
333  else
334  analyzed_rows++;
335 
336  /*
337  * Now detoast the array if needed, and deconstruct into datums.
338  */
339  array = DatumGetArrayTypeP(value);
340 
341  Assert(ARR_ELEMTYPE(array) == extra_data->type_id);
342  deconstruct_array(array,
343  extra_data->type_id,
344  extra_data->typlen,
345  extra_data->typbyval,
346  extra_data->typalign,
347  &elem_values, &elem_nulls, &num_elems);
348 
349  /*
350  * We loop through the elements in the array and add them to our
351  * tracking hashtable.
352  */
353  null_present = false;
354  for (j = 0; j < num_elems; j++)
355  {
356  Datum elem_value;
357  bool found;
358 
359  /* No null element processing other than flag setting here */
360  if (elem_nulls[j])
361  {
362  null_present = true;
363  continue;
364  }
365 
366  /* Lookup current element in hashtable, adding it if new */
367  elem_value = elem_values[j];
368  item = (TrackItem *) hash_search(elements_tab,
369  (const void *) &elem_value,
370  HASH_ENTER, &found);
371 
372  if (found)
373  {
374  /* The element value is already on the tracking list */
375 
376  /*
377  * The operators we assist ignore duplicate array elements, so
378  * count a given distinct element only once per array.
379  */
380  if (item->last_container == array_no)
381  continue;
382 
383  item->frequency++;
384  item->last_container = array_no;
385  }
386  else
387  {
388  /* Initialize new tracking list element */
389 
390  /*
391  * If element type is pass-by-reference, we must copy it into
392  * palloc'd space, so that we can release the array below. (We
393  * do this so that the space needed for element values is
394  * limited by the size of the hashtable; if we kept all the
395  * array values around, it could be much more.)
396  */
397  item->key = datumCopy(elem_value,
398  extra_data->typbyval,
399  extra_data->typlen);
400 
401  item->frequency = 1;
402  item->delta = b_current - 1;
403  item->last_container = array_no;
404  }
405 
406  /* element_no is the number of elements processed (ie N) */
407  element_no++;
408 
409  /* We prune the D structure after processing each bucket */
410  if (element_no % bucket_width == 0)
411  {
412  prune_element_hashtable(elements_tab, b_current);
413  b_current++;
414  }
415  }
416 
417  /* Count null element presence once per array. */
418  if (null_present)
419  null_elem_cnt++;
420 
421  /* Update frequency of the particular array distinct element count. */
422  distinct_count = (int) (element_no - prev_element_no);
423  count_item = (DECountItem *) hash_search(count_tab, &distinct_count,
424  HASH_ENTER,
425  &count_item_found);
426 
427  if (count_item_found)
428  count_item->frequency++;
429  else
430  count_item->frequency = 1;
431 
432  /* Free memory allocated while detoasting. */
433  if (PointerGetDatum(array) != value)
434  pfree(array);
435  pfree(elem_values);
436  pfree(elem_nulls);
437  }
438 
439  /* Skip pg_statistic slots occupied by standard statistics */
440  slot_idx = 0;
441  while (slot_idx < STATISTIC_NUM_SLOTS && stats->stakind[slot_idx] != 0)
442  slot_idx++;
443  if (slot_idx > STATISTIC_NUM_SLOTS - 2)
444  elog(ERROR, "insufficient pg_statistic slots for array stats");
445 
446  /* We can only compute real stats if we found some non-null values. */
447  if (analyzed_rows > 0)
448  {
449  int nonnull_cnt = analyzed_rows;
450  int count_items_count;
451  int i;
452  TrackItem **sort_table;
453  int track_len;
454  int64 cutoff_freq;
455  int64 minfreq,
456  maxfreq;
457 
458  /*
459  * We assume the standard stats code already took care of setting
460  * stats_valid, stanullfrac, stawidth, stadistinct. We'd have to
461  * re-compute those values if we wanted to not store the standard
462  * stats.
463  */
464 
465  /*
466  * Construct an array of the interesting hashtable items, that is,
467  * those meeting the cutoff frequency (s - epsilon)*N. Also identify
468  * the minimum and maximum frequencies among these items.
469  *
470  * Since epsilon = s/10 and bucket_width = 1/epsilon, the cutoff
471  * frequency is 9*N / bucket_width.
472  */
473  cutoff_freq = 9 * element_no / bucket_width;
474 
475  i = hash_get_num_entries(elements_tab); /* surely enough space */
476  sort_table = (TrackItem **) palloc(sizeof(TrackItem *) * i);
477 
478  hash_seq_init(&scan_status, elements_tab);
479  track_len = 0;
480  minfreq = element_no;
481  maxfreq = 0;
482  while ((item = (TrackItem *) hash_seq_search(&scan_status)) != NULL)
483  {
484  if (item->frequency > cutoff_freq)
485  {
486  sort_table[track_len++] = item;
487  minfreq = Min(minfreq, item->frequency);
488  maxfreq = Max(maxfreq, item->frequency);
489  }
490  }
491  Assert(track_len <= i);
492 
493  /* emit some statistics for debug purposes */
494  elog(DEBUG3, "compute_array_stats: target # mces = %d, "
495  "bucket width = %d, "
496  "# elements = " INT64_FORMAT ", hashtable size = %d, "
497  "usable entries = %d",
498  num_mcelem, bucket_width, element_no, i, track_len);
499 
500  /*
501  * If we obtained more elements than we really want, get rid of those
502  * with least frequencies. The easiest way is to qsort the array into
503  * descending frequency order and truncate the array.
504  */
505  if (num_mcelem < track_len)
506  {
507  qsort(sort_table, track_len, sizeof(TrackItem *),
509  /* reset minfreq to the smallest frequency we're keeping */
510  minfreq = sort_table[num_mcelem - 1]->frequency;
511  }
512  else
513  num_mcelem = track_len;
514 
515  /* Generate MCELEM slot entry */
516  if (num_mcelem > 0)
517  {
518  MemoryContext old_context;
519  Datum *mcelem_values;
520  float4 *mcelem_freqs;
521 
522  /*
523  * We want to store statistics sorted on the element value using
524  * the element type's default comparison function. This permits
525  * fast binary searches in selectivity estimation functions.
526  */
527  qsort(sort_table, num_mcelem, sizeof(TrackItem *),
529 
530  /* Must copy the target values into anl_context */
531  old_context = MemoryContextSwitchTo(stats->anl_context);
532 
533  /*
534  * We sorted statistics on the element value, but we want to be
535  * able to find the minimal and maximal frequencies without going
536  * through all the values. We also want the frequency of null
537  * elements. Store these three values at the end of mcelem_freqs.
538  */
539  mcelem_values = (Datum *) palloc(num_mcelem * sizeof(Datum));
540  mcelem_freqs = (float4 *) palloc((num_mcelem + 3) * sizeof(float4));
541 
542  /*
543  * See comments above about use of nonnull_cnt as the divisor for
544  * the final frequency estimates.
545  */
546  for (i = 0; i < num_mcelem; i++)
547  {
548  TrackItem *item = sort_table[i];
549 
550  mcelem_values[i] = datumCopy(item->key,
551  extra_data->typbyval,
552  extra_data->typlen);
553  mcelem_freqs[i] = (double) item->frequency /
554  (double) nonnull_cnt;
555  }
556  mcelem_freqs[i++] = (double) minfreq / (double) nonnull_cnt;
557  mcelem_freqs[i++] = (double) maxfreq / (double) nonnull_cnt;
558  mcelem_freqs[i++] = (double) null_elem_cnt / (double) nonnull_cnt;
559 
560  MemoryContextSwitchTo(old_context);
561 
562  stats->stakind[slot_idx] = STATISTIC_KIND_MCELEM;
563  stats->staop[slot_idx] = extra_data->eq_opr;
564  stats->stacoll[slot_idx] = extra_data->coll_id;
565  stats->stanumbers[slot_idx] = mcelem_freqs;
566  /* See above comment about extra stanumber entries */
567  stats->numnumbers[slot_idx] = num_mcelem + 3;
568  stats->stavalues[slot_idx] = mcelem_values;
569  stats->numvalues[slot_idx] = num_mcelem;
570  /* We are storing values of element type */
571  stats->statypid[slot_idx] = extra_data->type_id;
572  stats->statyplen[slot_idx] = extra_data->typlen;
573  stats->statypbyval[slot_idx] = extra_data->typbyval;
574  stats->statypalign[slot_idx] = extra_data->typalign;
575  slot_idx++;
576  }
577 
578  /* Generate DECHIST slot entry */
579  count_items_count = hash_get_num_entries(count_tab);
580  if (count_items_count > 0)
581  {
582  int num_hist = stats->attr->attstattarget;
583  DECountItem **sorted_count_items;
584  int j;
585  int delta;
586  int64 frac;
587  float4 *hist;
588 
589  /* num_hist must be at least 2 for the loop below to work */
590  num_hist = Max(num_hist, 2);
591 
592  /*
593  * Create an array of DECountItem pointers, and sort them into
594  * increasing count order.
595  */
596  sorted_count_items = (DECountItem **)
597  palloc(sizeof(DECountItem *) * count_items_count);
598  hash_seq_init(&scan_status, count_tab);
599  j = 0;
600  while ((count_item = (DECountItem *) hash_seq_search(&scan_status)) != NULL)
601  {
602  sorted_count_items[j++] = count_item;
603  }
604  qsort(sorted_count_items, count_items_count,
606 
607  /*
608  * Prepare to fill stanumbers with the histogram, followed by the
609  * average count. This array must be stored in anl_context.
610  */
611  hist = (float4 *)
613  sizeof(float4) * (num_hist + 1));
614  hist[num_hist] = (double) element_no / (double) nonnull_cnt;
615 
616  /*----------
617  * Construct the histogram of distinct-element counts (DECs).
618  *
619  * The object of this loop is to copy the min and max DECs to
620  * hist[0] and hist[num_hist - 1], along with evenly-spaced DECs
621  * in between (where "evenly-spaced" is with reference to the
622  * whole input population of arrays). If we had a complete sorted
623  * array of DECs, one per analyzed row, the i'th hist value would
624  * come from DECs[i * (analyzed_rows - 1) / (num_hist - 1)]
625  * (compare the histogram-making loop in compute_scalar_stats()).
626  * But instead of that we have the sorted_count_items[] array,
627  * which holds unique DEC values with their frequencies (that is,
628  * a run-length-compressed version of the full array). So we
629  * control advancing through sorted_count_items[] with the
630  * variable "frac", which is defined as (x - y) * (num_hist - 1),
631  * where x is the index in the notional DECs array corresponding
632  * to the start of the next sorted_count_items[] element's run,
633  * and y is the index in DECs from which we should take the next
634  * histogram value. We have to advance whenever x <= y, that is
635  * frac <= 0. The x component is the sum of the frequencies seen
636  * so far (up through the current sorted_count_items[] element),
637  * and of course y * (num_hist - 1) = i * (analyzed_rows - 1),
638  * per the subscript calculation above. (The subscript calculation
639  * implies dropping any fractional part of y; in this formulation
640  * that's handled by not advancing until frac reaches 1.)
641  *
642  * Even though frac has a bounded range, it could overflow int32
643  * when working with very large statistics targets, so we do that
644  * math in int64.
645  *----------
646  */
647  delta = analyzed_rows - 1;
648  j = 0; /* current index in sorted_count_items */
649  /* Initialize frac for sorted_count_items[0]; y is initially 0 */
650  frac = (int64) sorted_count_items[0]->frequency * (num_hist - 1);
651  for (i = 0; i < num_hist; i++)
652  {
653  while (frac <= 0)
654  {
655  /* Advance, and update x component of frac */
656  j++;
657  frac += (int64) sorted_count_items[j]->frequency * (num_hist - 1);
658  }
659  hist[i] = sorted_count_items[j]->count;
660  frac -= delta; /* update y for upcoming i increment */
661  }
662  Assert(j == count_items_count - 1);
663 
664  stats->stakind[slot_idx] = STATISTIC_KIND_DECHIST;
665  stats->staop[slot_idx] = extra_data->eq_opr;
666  stats->stacoll[slot_idx] = extra_data->coll_id;
667  stats->stanumbers[slot_idx] = hist;
668  stats->numnumbers[slot_idx] = num_hist + 1;
669  slot_idx++;
670  }
671  }
672 
673  /*
674  * We don't need to bother cleaning up any of our temporary palloc's. The
675  * hashtable should also go away, as it used a child memory context.
676  */
677 }
static void prune_element_hashtable(HTAB *elements_tab, int b_current)
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
#define DEBUG3
Definition: elog.h:23
#define PointerGetDatum(X)
Definition: postgres.h:556
#define Min(x, y)
Definition: c.h:904
Datum * stavalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:114
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
static struct @145 value
#define MemSet(start, val, len)
Definition: c.h:955
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1335
bool statypbyval[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:124
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
static int countitem_compare_count(const void *e1, const void *e2)
char statypalign[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:125
Form_pg_attribute attr
Definition: vacuum.h:85
static int element_match(const void *key1, const void *key2, Size keysize)
Definition: dynahash.c:208
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
Oid stacoll[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:110
AnalyzeAttrComputeStatsFunc std_compute_stats
int numnumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:111
LexemeHashKey key
Definition: ts_typanalyze.c:34
static ArrayAnalyzeExtraData * array_extra_data
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:806
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:130
static int trackitem_compare_element(const void *e1, const void *e2)
Oid staop[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:109
float float4
Definition: c.h:490
#define HASH_BLOBS
Definition: hsearch.h:88
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
uintptr_t Datum
Definition: postgres.h:367
int16 stakind[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:108
Size keysize
Definition: hsearch.h:72
HashCompareFunc match
Definition: hsearch.h:75
Oid statypid[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:122
#define STATISTIC_NUM_SLOTS
Definition: pg_statistic.h:126
#define Max(x, y)
Definition: c.h:898
#define ARRAY_WIDTH_THRESHOLD
#define Assert(condition)
Definition: c.h:732
float4 * stanumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:112
#define HASH_COMPARE
Definition: hsearch.h:90
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1389
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1379
#define INT64_FORMAT
Definition: c.h:400
MemoryContext anl_context
Definition: vacuum.h:90
static uint32 element_hash(const void *key, Size keysize)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
int numvalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:113
void * palloc(Size size)
Definition: mcxt.c:949
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
int16 statyplen[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:123
#define elog(elevel,...)
Definition: elog.h:226
int i
void * extra_data
Definition: vacuum.h:98
#define qsort(a, b, c, d)
Definition: port.h:492
void vacuum_delay_point(void)
Definition: vacuum.c:1946
#define ARR_ELEMTYPE(a)
Definition: array.h:280
HashValueFunc hash
Definition: hsearch.h:74
#define HASH_FUNCTION
Definition: hsearch.h:89
#define DatumGetArrayTypeP(X)
Definition: array.h:249
static int trackitem_compare_frequencies_desc(const void *e1, const void *e2)

◆ countitem_compare_count()

static int countitem_compare_count ( const void *  e1,
const void *  e2 
)
static

Definition at line 783 of file array_typanalyze.c.

Referenced by compute_array_stats().

784 {
785  const DECountItem *const *t1 = (const DECountItem *const *) e1;
786  const DECountItem *const *t2 = (const DECountItem *const *) e2;
787 
788  if ((*t1)->count < (*t2)->count)
789  return -1;
790  else if ((*t1)->count == (*t2)->count)
791  return 0;
792  else
793  return 1;
794 }

◆ element_compare()

static int element_compare ( const void *  key1,
const void *  key2 
)
static

Definition at line 743 of file array_typanalyze.c.

References ArrayAnalyzeExtraData::cmp, ArrayAnalyzeExtraData::coll_id, DatumGetInt32, and FunctionCall2Coll().

Referenced by element_match(), and trackitem_compare_element().

744 {
745  Datum d1 = *((const Datum *) key1);
746  Datum d2 = *((const Datum *) key2);
747  Datum c;
748 
751  d1, d2);
752  return DatumGetInt32(c);
753 }
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1150
char * c
static ArrayAnalyzeExtraData * array_extra_data
uintptr_t Datum
Definition: postgres.h:367

◆ element_hash()

static uint32 element_hash ( const void *  key,
Size  keysize 
)
static

Definition at line 713 of file array_typanalyze.c.

References ArrayAnalyzeExtraData::coll_id, DatumGetUInt32, FunctionCall1Coll(), and ArrayAnalyzeExtraData::hash.

Referenced by compute_array_stats().

714 {
715  Datum d = *((const Datum *) key);
716  Datum h;
717 
720  d);
721  return DatumGetUInt32(h);
722 }
#define DatumGetUInt32(X)
Definition: postgres.h:486
static ArrayAnalyzeExtraData * array_extra_data
uintptr_t Datum
Definition: postgres.h:367
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1130

◆ element_match()

static int element_match ( const void *  key1,
const void *  key2,
Size  keysize 
)
static

Definition at line 728 of file array_typanalyze.c.

References element_compare().

Referenced by compute_array_stats().

729 {
730  /* The keysize parameter is superfluous here */
731  return element_compare(key1, key2);
732 }
static int element_compare(const void *key1, const void *key2)

◆ prune_element_hashtable()

static void prune_element_hashtable ( HTAB elements_tab,
int  b_current 
)
static

Definition at line 684 of file array_typanalyze.c.

References DatumGetPointer, TrackItem::delta, elog, ERROR, TrackItem::frequency, HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), TrackItem::key, pfree(), ArrayAnalyzeExtraData::typbyval, and value.

Referenced by compute_array_stats().

685 {
686  HASH_SEQ_STATUS scan_status;
687  TrackItem *item;
688 
689  hash_seq_init(&scan_status, elements_tab);
690  while ((item = (TrackItem *) hash_seq_search(&scan_status)) != NULL)
691  {
692  if (item->frequency + item->delta <= b_current)
693  {
694  Datum value = item->key;
695 
696  if (hash_search(elements_tab, (const void *) &item->key,
697  HASH_REMOVE, NULL) == NULL)
698  elog(ERROR, "hash table corrupted");
699  /* We should free memory if element is not passed by value */
701  pfree(DatumGetPointer(value));
702  }
703  }
704 }
static struct @145 value
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
LexemeHashKey key
Definition: ts_typanalyze.c:34
static ArrayAnalyzeExtraData * array_extra_data
uintptr_t Datum
Definition: postgres.h:367
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1389
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1379
#define DatumGetPointer(X)
Definition: postgres.h:549
#define elog(elevel,...)
Definition: elog.h:226

◆ trackitem_compare_element()

static int trackitem_compare_element ( const void *  e1,
const void *  e2 
)
static

Definition at line 771 of file array_typanalyze.c.

References element_compare().

Referenced by compute_array_stats().

772 {
773  const TrackItem *const *t1 = (const TrackItem *const *) e1;
774  const TrackItem *const *t2 = (const TrackItem *const *) e2;
775 
776  return element_compare(&(*t1)->key, &(*t2)->key);
777 }
static int element_compare(const void *key1, const void *key2)

◆ trackitem_compare_frequencies_desc()

static int trackitem_compare_frequencies_desc ( const void *  e1,
const void *  e2 
)
static

Definition at line 759 of file array_typanalyze.c.

References TrackItem::frequency.

Referenced by compute_array_stats().

760 {
761  const TrackItem *const *t1 = (const TrackItem *const *) e1;
762  const TrackItem *const *t2 = (const TrackItem *const *) e2;
763 
764  return (*t2)->frequency - (*t1)->frequency;
765 }

Variable Documentation

◆ array_extra_data

ArrayAnalyzeExtraData* array_extra_data
static

Definition at line 65 of file array_typanalyze.c.