PostgreSQL Source Code  git master
brin_minmax_multi.c
Go to the documentation of this file.
1 /*
2  * brin_minmax_multi.c
3  * Implementation of Multi Min/Max opclass for BRIN
4  *
5  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * Implements a variant of minmax opclass, where the summary is composed of
10  * multiple smaller intervals. This allows us to handle outliers, which
11  * usually make the simple minmax opclass inefficient.
12  *
13  * Consider for example page range with simple minmax interval [1000,2000],
14  * and assume a new row gets inserted into the range with value 1000000.
15  * Due to that the interval gets [1000,1000000]. I.e. the minmax interval
16  * got 1000x wider and won't be useful to eliminate scan keys between 2001
17  * and 1000000.
18  *
19  * With minmax-multi opclass, we may have [1000,2000] interval initially,
20  * but after adding the new row we start tracking it as two interval:
21  *
22  * [1000,2000] and [1000000,1000000]
23  *
24  * This allows us to still eliminate the page range when the scan keys hit
25  * the gap between 2000 and 1000000, making it useful in cases when the
26  * simple minmax opclass gets inefficient.
27  *
28  * The number of intervals tracked per page range is somewhat flexible.
29  * What is restricted is the number of values per page range, and the limit
30  * is currently 32 (see values_per_range reloption). Collapsed intervals
31  * (with equal minimum and maximum value) are stored as a single value,
32  * while regular intervals require two values.
33  *
34  * When the number of values gets too high (by adding new values to the
35  * summary), we merge some of the intervals to free space for more values.
36  * This is done in a greedy way - we simply pick the two closest intervals,
37  * merge them, and repeat this until the number of values to store gets
38  * sufficiently low (below 50% of maximum values), but that is mostly
39  * arbitrary threshold and may be changed easily).
40  *
41  * To pick the closest intervals we use the "distance" support procedure,
42  * which measures space between two ranges (i.e. the length of an interval).
43  * The computed value may be an approximation - in the worst case we will
44  * merge two ranges that are slightly less optimal at that step, but the
45  * index should still produce correct results.
46  *
47  * The compactions (reducing the number of values) is fairly expensive, as
48  * it requires calling the distance functions, sorting etc. So when building
49  * the summary, we use a significantly larger buffer, and only enforce the
50  * exact limit at the very end. This improves performance, and it also helps
51  * with building better ranges (due to the greedy approach).
52  *
53  *
54  * IDENTIFICATION
55  * src/backend/access/brin/brin_minmax_multi.c
56  */
57 #include "postgres.h"
58 
59 /* needed for PGSQL_AF_INET */
60 #include <sys/socket.h>
61 
62 #include "access/genam.h"
63 #include "access/brin.h"
64 #include "access/brin_internal.h"
65 #include "access/brin_tuple.h"
66 #include "access/reloptions.h"
67 #include "access/stratnum.h"
68 #include "access/htup_details.h"
69 #include "catalog/pg_type.h"
70 #include "catalog/pg_am.h"
71 #include "catalog/pg_amop.h"
72 #include "utils/array.h"
73 #include "utils/builtins.h"
74 #include "utils/date.h"
75 #include "utils/datum.h"
76 #include "utils/float.h"
77 #include "utils/inet.h"
78 #include "utils/lsyscache.h"
79 #include "utils/memutils.h"
80 #include "utils/numeric.h"
81 #include "utils/pg_lsn.h"
82 #include "utils/rel.h"
83 #include "utils/syscache.h"
84 #include "utils/timestamp.h"
85 #include "utils/uuid.h"
86 
87 /*
88  * Additional SQL level support functions
89  *
90  * Procedure numbers must not use values reserved for BRIN itself; see
91  * brin_internal.h.
92  */
93 #define MINMAX_MAX_PROCNUMS 1 /* maximum support procs we need */
94 #define PROCNUM_DISTANCE 11 /* required, distance between values */
95 
96 /*
97  * Subtract this from procnum to obtain index in MinmaxMultiOpaque arrays
98  * (Must be equal to minimum of private procnums).
99  */
100 #define PROCNUM_BASE 11
101 
102 /*
103  * Sizing the insert buffer - we use 10x the number of values specified
104  * in the reloption, but we cap it to 8192 not to get too large. When
105  * the buffer gets full, we reduce the number of values by half.
106  */
107 #define MINMAX_BUFFER_FACTOR 10
108 #define MINMAX_BUFFER_MIN 256
109 #define MINMAX_BUFFER_MAX 8192
110 #define MINMAX_BUFFER_LOAD_FACTOR 0.5
111 
112 typedef struct MinmaxMultiOpaque
113 {
119 
120 /*
121  * Storage type for BRIN's minmax reloptions
122  */
123 typedef struct MinMaxMultiOptions
124 {
125  int32 vl_len_; /* varlena header (do not touch directly!) */
126  int valuesPerRange; /* number of values per range */
128 
129 #define MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE 32
130 
131 #define MinMaxMultiGetValuesPerRange(opts) \
132  ((opts) && (((MinMaxMultiOptions *) (opts))->valuesPerRange != 0) ? \
133  ((MinMaxMultiOptions *) (opts))->valuesPerRange : \
134  MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE)
135 
136 #define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
137 
138 /*
139  * The summary of minmax-multi indexes has two representations - Ranges for
140  * convenient processing, and SerializedRanges for storage in bytea value.
141  *
142  * The Ranges struct stores the boundary values in a single array, but we
143  * treat regular and single-point ranges differently to save space. For
144  * regular ranges (with different boundary values) we have to store both
145  * values, while for "single-point ranges" we only need to save one value.
146  *
147  * The 'values' array stores boundary values for regular ranges first (there
148  * are 2*nranges values to store), and then the nvalues boundary values for
149  * single-point ranges. That is, we have (2*nranges + nvalues) boundary
150  * values in the array.
151  *
152  * +---------------------------------+-------------------------------+
153  * | ranges (sorted pairs of values) | sorted values (single points) |
154  * +---------------------------------+-------------------------------+
155  *
156  * This allows us to quickly add new values, and store outliers without
157  * making the other ranges very wide.
158  *
159  * We never store more than maxvalues values (as set by values_per_range
160  * reloption). If needed we merge some of the ranges.
161  *
162  * To minimize palloc overhead, we always allocate the full array with
163  * space for maxvalues elements. This should be fine as long as the
164  * maxvalues is reasonably small (64 seems fine), which is the case
165  * thanks to values_per_range reloption being limited to 256.
166  */
167 typedef struct Ranges
168 {
169  /* Cache information that we need quite often. */
174 
175  /* (2*nranges + nvalues) <= maxvalues */
176  int nranges; /* number of ranges in the array (stored) */
177  int nsorted; /* number of sorted values (ranges + points) */
178  int nvalues; /* number of values in the data array (all) */
179  int maxvalues; /* maximum number of values (reloption) */
180 
181  /*
182  * We simply add the values into a large buffer, without any expensive
183  * steps (sorting, deduplication, ...). The buffer is a multiple of the
184  * target number of values, so the compaction happens less often,
185  * amortizing the costs. We keep the actual target and compact to the
186  * requested number of values at the very end, before serializing to
187  * on-disk representation.
188  */
189  /* requested number of values */
191 
192  /* values stored for this range - either raw values, or ranges */
195 
196 /*
197  * On-disk the summary is stored as a bytea value, with a simple header
198  * with basic metadata, followed by the boundary values. It has a varlena
199  * header, so can be treated as varlena directly.
200  *
201  * See brin_range_serialize/brin_range_deserialize for serialization details.
202  */
203 typedef struct SerializedRanges
204 {
205  /* varlena header (do not touch directly!) */
207 
208  /* type of values stored in the data array */
210 
211  /* (2*nranges + nvalues) <= maxvalues */
212  int nranges; /* number of ranges in the array (stored) */
213  int nvalues; /* number of values in the data array (all) */
214  int maxvalues; /* maximum number of values (reloption) */
215 
216  /* contains the actual data */
219 
221 
222 static Ranges *brin_range_deserialize(int maxvalues, SerializedRanges *range);
223 
224 
225 /*
226  * Used to represent ranges expanded to make merging and combining easier.
227  *
228  * Each expanded range is essentially an interval, represented by min/max
229  * values, along with a flag whether it's a collapsed range (in which case
230  * the min and max values are equal). We have the flag to handle by-ref
231  * data types - we can't simply compare the datums, and this saves some
232  * calls to the type-specific comparator function.
233  */
234 typedef struct ExpandedRange
235 {
236  Datum minval; /* lower boundary */
237  Datum maxval; /* upper boundary */
238  bool collapsed; /* true if minval==maxval */
240 
241 /*
242  * Represents a distance between two ranges (identified by index into
243  * an array of extended ranges).
244  */
245 typedef struct DistanceValue
246 {
247  int index;
248  double value;
250 
251 
252 /* Cache for support and strategy procedures. */
253 
254 static FmgrInfo *minmax_multi_get_procinfo(BrinDesc *bdesc, uint16 attno,
255  uint16 procnum);
256 
258  uint16 attno, Oid subtype,
259  uint16 strategynum);
260 
261 typedef struct compare_context
262 {
266 
267 static int compare_values(const void *a, const void *b, void *arg);
268 
269 
270 #ifdef USE_ASSERT_CHECKING
271 /*
272  * Check that the order of the array values is correct, using the cmp
273  * function (which should be BTLessStrategyNumber).
274  */
275 static void
276 AssertArrayOrder(FmgrInfo *cmp, Oid colloid, Datum *values, int nvalues)
277 {
278  int i;
279  Datum lt;
280 
281  for (i = 0; i < (nvalues - 1); i++)
282  {
283  lt = FunctionCall2Coll(cmp, colloid, values[i], values[i + 1]);
284  Assert(DatumGetBool(lt));
285  }
286 }
287 #endif
288 
289 /*
290  * Comprehensive check of the Ranges structure.
291  */
292 static void
293 AssertCheckRanges(Ranges *ranges, FmgrInfo *cmpFn, Oid colloid)
294 {
295 #ifdef USE_ASSERT_CHECKING
296  int i;
297 
298  /* some basic sanity checks */
299  Assert(ranges->nranges >= 0);
300  Assert(ranges->nsorted >= 0);
301  Assert(ranges->nvalues >= ranges->nsorted);
302  Assert(ranges->maxvalues >= 2 * ranges->nranges + ranges->nvalues);
303  Assert(ranges->typid != InvalidOid);
304 
305  /*
306  * First the ranges - there are 2*nranges boundary values, and the values
307  * have to be strictly ordered (equal values would mean the range is
308  * collapsed, and should be stored as a point). This also guarantees that
309  * the ranges do not overlap.
310  */
311  AssertArrayOrder(cmpFn, colloid, ranges->values, 2 * ranges->nranges);
312 
313  /* then the single-point ranges (with nvalues boundary values ) */
314  AssertArrayOrder(cmpFn, colloid, &ranges->values[2 * ranges->nranges],
315  ranges->nsorted);
316 
317  /*
318  * Check that none of the values are not covered by ranges (both sorted
319  * and unsorted)
320  */
321  for (i = 0; i < ranges->nvalues; i++)
322  {
323  Datum compar;
324  int start,
325  end;
326  Datum minvalue,
327  maxvalue;
328 
329  Datum value = ranges->values[2 * ranges->nranges + i];
330 
331  if (ranges->nranges == 0)
332  break;
333 
334  minvalue = ranges->values[0];
335  maxvalue = ranges->values[2 * ranges->nranges - 1];
336 
337  /*
338  * Is the value smaller than the minval? If yes, we'll recurse to the
339  * left side of range array.
340  */
341  compar = FunctionCall2Coll(cmpFn, colloid, value, minvalue);
342 
343  /* smaller than the smallest value in the first range */
344  if (DatumGetBool(compar))
345  continue;
346 
347  /*
348  * Is the value greater than the maxval? If yes, we'll recurse to the
349  * right side of range array.
350  */
351  compar = FunctionCall2Coll(cmpFn, colloid, maxvalue, value);
352 
353  /* larger than the largest value in the last range */
354  if (DatumGetBool(compar))
355  continue;
356 
357  start = 0; /* first range */
358  end = ranges->nranges - 1; /* last range */
359  while (true)
360  {
361  int midpoint = (start + end) / 2;
362 
363  /* this means we ran out of ranges in the last step */
364  if (start > end)
365  break;
366 
367  /* copy the min/max values from the ranges */
368  minvalue = ranges->values[2 * midpoint];
369  maxvalue = ranges->values[2 * midpoint + 1];
370 
371  /*
372  * Is the value smaller than the minval? If yes, we'll recurse to
373  * the left side of range array.
374  */
375  compar = FunctionCall2Coll(cmpFn, colloid, value, minvalue);
376 
377  /* smaller than the smallest value in this range */
378  if (DatumGetBool(compar))
379  {
380  end = (midpoint - 1);
381  continue;
382  }
383 
384  /*
385  * Is the value greater than the minval? If yes, we'll recurse to
386  * the right side of range array.
387  */
388  compar = FunctionCall2Coll(cmpFn, colloid, maxvalue, value);
389 
390  /* larger than the largest value in this range */
391  if (DatumGetBool(compar))
392  {
393  start = (midpoint + 1);
394  continue;
395  }
396 
397  /* hey, we found a matching range */
398  Assert(false);
399  }
400  }
401 
402  /* and values in the unsorted part must not be in sorted part */
403  for (i = ranges->nsorted; i < ranges->nvalues; i++)
404  {
405  compare_context cxt;
406  Datum value = ranges->values[2 * ranges->nranges + i];
407 
408  if (ranges->nsorted == 0)
409  break;
410 
411  cxt.colloid = ranges->colloid;
412  cxt.cmpFn = ranges->cmp;
413 
414  Assert(bsearch_arg(&value, &ranges->values[2 * ranges->nranges],
415  ranges->nsorted, sizeof(Datum),
416  compare_values, (void *) &cxt) == NULL);
417  }
418 #endif
419 }
420 
421 /*
422  * Check that the expanded ranges (built when reducing the number of ranges
423  * by combining some of them) are correctly sorted and do not overlap.
424  */
425 static void
427  Form_pg_attribute attr, ExpandedRange *ranges,
428  int nranges)
429 {
430 #ifdef USE_ASSERT_CHECKING
431  int i;
432  FmgrInfo *eq;
433  FmgrInfo *lt;
434 
435  eq = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
437 
438  lt = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
440 
441  /*
442  * Each range independently should be valid, i.e. that for the boundary
443  * values (lower <= upper).
444  */
445  for (i = 0; i < nranges; i++)
446  {
447  Datum r;
448  Datum minval = ranges[i].minval;
449  Datum maxval = ranges[i].maxval;
450 
451  if (ranges[i].collapsed) /* collapsed: minval == maxval */
452  r = FunctionCall2Coll(eq, colloid, minval, maxval);
453  else /* non-collapsed: minval < maxval */
454  r = FunctionCall2Coll(lt, colloid, minval, maxval);
455 
456  Assert(DatumGetBool(r));
457  }
458 
459  /*
460  * And the ranges should be ordered and must not overlap, i.e. upper <
461  * lower for boundaries of consecutive ranges.
462  */
463  for (i = 0; i < nranges - 1; i++)
464  {
465  Datum r;
466  Datum maxval = ranges[i].maxval;
467  Datum minval = ranges[i + 1].minval;
468 
469  r = FunctionCall2Coll(lt, colloid, maxval, minval);
470 
471  Assert(DatumGetBool(r));
472  }
473 #endif
474 }
475 
476 
477 /*
478  * minmax_multi_init
479  * Initialize the deserialized range list, allocate all the memory.
480  *
481  * This is only in-memory representation of the ranges, so we allocate
482  * enough space for the maximum number of values (so as not to have to do
483  * repallocs as the ranges grow).
484  */
485 static Ranges *
486 minmax_multi_init(int maxvalues)
487 {
488  Size len;
489  Ranges *ranges;
490 
491  Assert(maxvalues > 0);
492 
493  len = offsetof(Ranges, values); /* fixed header */
494  len += maxvalues * sizeof(Datum); /* Datum values */
495 
496  ranges = (Ranges *) palloc0(len);
497 
498  ranges->maxvalues = maxvalues;
499 
500  return ranges;
501 }
502 
503 
504 /*
505  * range_deduplicate_values
506  * Deduplicate the part with values in the simple points.
507  *
508  * This is meant to be a cheaper way of reducing the size of the ranges. It
509  * does not touch the ranges, and only sorts the other values - it does not
510  * call the distance functions, which may be quite expensive, etc.
511  *
512  * We do know the values are not duplicate with the ranges, because we check
513  * that before adding a new value. Same for the sorted part of values.
514  */
515 static void
517 {
518  int i,
519  n;
520  int start;
521  compare_context cxt;
522 
523  /*
524  * If there are no unsorted values, we're done (this probably can't
525  * happen, as we're adding values to unsorted part).
526  */
527  if (range->nsorted == range->nvalues)
528  return;
529 
530  /* sort the values */
531  cxt.colloid = range->colloid;
532  cxt.cmpFn = range->cmp;
533 
534  /* the values start right after the ranges (which are always sorted) */
535  start = 2 * range->nranges;
536 
537  /*
538  * XXX This might do a merge sort, to leverage that the first part of the
539  * array is already sorted. If the sorted part is large, it might be quite
540  * a bit faster.
541  */
542  qsort_arg(&range->values[start],
543  range->nvalues, sizeof(Datum),
544  compare_values, (void *) &cxt);
545 
546  n = 1;
547  for (i = 1; i < range->nvalues; i++)
548  {
549  /* same as preceding value, so store it */
550  if (compare_values(&range->values[start + i - 1],
551  &range->values[start + i],
552  (void *) &cxt) == 0)
553  continue;
554 
555  range->values[start + n] = range->values[start + i];
556 
557  n++;
558  }
559 
560  /* now all the values are sorted */
561  range->nvalues = n;
562  range->nsorted = n;
563 
564  AssertCheckRanges(range, range->cmp, range->colloid);
565 }
566 
567 
568 /*
569  * brin_range_serialize
570  * Serialize the in-memory representation into a compact varlena value.
571  *
572  * Simply copy the header and then also the individual values, as stored
573  * in the in-memory value array.
574  */
575 static SerializedRanges *
577 {
578  Size len;
579  int nvalues;
580  SerializedRanges *serialized;
581  Oid typid;
582  int typlen;
583  bool typbyval;
584 
585  int i;
586  char *ptr;
587 
588  /* simple sanity checks */
589  Assert(range->nranges >= 0);
590  Assert(range->nsorted >= 0);
591  Assert(range->nvalues >= 0);
592  Assert(range->maxvalues > 0);
593  Assert(range->target_maxvalues > 0);
594 
595  /* at this point the range should be compacted to the target size */
596  Assert(2 * range->nranges + range->nvalues <= range->target_maxvalues);
597 
598  Assert(range->target_maxvalues <= range->maxvalues);
599 
600  /* range boundaries are always sorted */
601  Assert(range->nvalues >= range->nsorted);
602 
603  /* deduplicate values, if there's unsorted part */
605 
606  /* see how many Datum values we actually have */
607  nvalues = 2 * range->nranges + range->nvalues;
608 
609  typid = range->typid;
610  typbyval = get_typbyval(typid);
611  typlen = get_typlen(typid);
612 
613  /* header is always needed */
615 
616  /*
617  * The space needed depends on data type - for fixed-length data types
618  * (by-value and some by-reference) it's pretty simple, just multiply
619  * (attlen * nvalues) and we're done. For variable-length by-reference
620  * types we need to actually walk all the values and sum the lengths.
621  */
622  if (typlen == -1) /* varlena */
623  {
624  int i;
625 
626  for (i = 0; i < nvalues; i++)
627  {
628  len += VARSIZE_ANY(range->values[i]);
629  }
630  }
631  else if (typlen == -2) /* cstring */
632  {
633  int i;
634 
635  for (i = 0; i < nvalues; i++)
636  {
637  /* don't forget to include the null terminator ;-) */
638  len += strlen(DatumGetCString(range->values[i])) + 1;
639  }
640  }
641  else /* fixed-length types (even by-reference) */
642  {
643  Assert(typlen > 0);
644  len += nvalues * typlen;
645  }
646 
647  /*
648  * Allocate the serialized object, copy the basic information. The
649  * serialized object is a varlena, so update the header.
650  */
651  serialized = (SerializedRanges *) palloc0(len);
652  SET_VARSIZE(serialized, len);
653 
654  serialized->typid = typid;
655  serialized->nranges = range->nranges;
656  serialized->nvalues = range->nvalues;
657  serialized->maxvalues = range->target_maxvalues;
658 
659  /*
660  * And now copy also the boundary values (like the length calculation this
661  * depends on the particular data type).
662  */
663  ptr = serialized->data; /* start of the serialized data */
664 
665  for (i = 0; i < nvalues; i++)
666  {
667  if (typbyval) /* simple by-value data types */
668  {
669  Datum tmp;
670 
671  /*
672  * For byval types, we need to copy just the significant bytes -
673  * we can't use memcpy directly, as that assumes little-endian
674  * behavior. store_att_byval does almost what we need, but it
675  * requires a properly aligned buffer - the output buffer does not
676  * guarantee that. So we simply use a local Datum variable (which
677  * guarantees proper alignment), and then copy the value from it.
678  */
679  store_att_byval(&tmp, range->values[i], typlen);
680 
681  memcpy(ptr, &tmp, typlen);
682  ptr += typlen;
683  }
684  else if (typlen > 0) /* fixed-length by-ref types */
685  {
686  memcpy(ptr, DatumGetPointer(range->values[i]), typlen);
687  ptr += typlen;
688  }
689  else if (typlen == -1) /* varlena */
690  {
691  int tmp = VARSIZE_ANY(DatumGetPointer(range->values[i]));
692 
693  memcpy(ptr, DatumGetPointer(range->values[i]), tmp);
694  ptr += tmp;
695  }
696  else if (typlen == -2) /* cstring */
697  {
698  int tmp = strlen(DatumGetCString(range->values[i])) + 1;
699 
700  memcpy(ptr, DatumGetCString(range->values[i]), tmp);
701  ptr += tmp;
702  }
703 
704  /* make sure we haven't overflown the buffer end */
705  Assert(ptr <= ((char *) serialized + len));
706  }
707 
708  /* exact size */
709  Assert(ptr == ((char *) serialized + len));
710 
711  return serialized;
712 }
713 
714 /*
715  * brin_range_deserialize
716  * Serialize the in-memory representation into a compact varlena value.
717  *
718  * Simply copy the header and then also the individual values, as stored
719  * in the in-memory value array.
720  */
721 static Ranges *
722 brin_range_deserialize(int maxvalues, SerializedRanges *serialized)
723 {
724  int i,
725  nvalues;
726  char *ptr,
727  *dataptr;
728  bool typbyval;
729  int typlen;
730  Size datalen;
731 
732  Ranges *range;
733 
734  Assert(serialized->nranges >= 0);
735  Assert(serialized->nvalues >= 0);
736  Assert(serialized->maxvalues > 0);
737 
738  nvalues = 2 * serialized->nranges + serialized->nvalues;
739 
740  Assert(nvalues <= serialized->maxvalues);
741  Assert(serialized->maxvalues <= maxvalues);
742 
743  range = minmax_multi_init(maxvalues);
744 
745  /* copy the header info */
746  range->nranges = serialized->nranges;
747  range->nvalues = serialized->nvalues;
748  range->nsorted = serialized->nvalues;
749  range->maxvalues = maxvalues;
750  range->target_maxvalues = serialized->maxvalues;
751 
752  range->typid = serialized->typid;
753 
754  typbyval = get_typbyval(serialized->typid);
755  typlen = get_typlen(serialized->typid);
756 
757  /*
758  * And now deconstruct the values into Datum array. We have to copy the
759  * data because the serialized representation ignores alignment, and we
760  * don't want to rely on it being kept around anyway.
761  */
762  ptr = serialized->data;
763 
764  /*
765  * We don't want to allocate many pieces, so we just allocate everything
766  * in one chunk. How much space will we need?
767  *
768  * XXX We don't need to copy simple by-value data types.
769  */
770  datalen = 0;
771  dataptr = NULL;
772  for (i = 0; (i < nvalues) && (!typbyval); i++)
773  {
774  if (typlen > 0) /* fixed-length by-ref types */
775  datalen += MAXALIGN(typlen);
776  else if (typlen == -1) /* varlena */
777  {
778  datalen += MAXALIGN(VARSIZE_ANY(DatumGetPointer(ptr)));
779  ptr += VARSIZE_ANY(DatumGetPointer(ptr));
780  }
781  else if (typlen == -2) /* cstring */
782  {
783  Size slen = strlen(DatumGetCString(ptr)) + 1;
784 
785  datalen += MAXALIGN(slen);
786  ptr += slen;
787  }
788  }
789 
790  if (datalen > 0)
791  dataptr = palloc(datalen);
792 
793  /*
794  * Restore the source pointer (might have been modified when calculating
795  * the space we need to allocate).
796  */
797  ptr = serialized->data;
798 
799  for (i = 0; i < nvalues; i++)
800  {
801  if (typbyval) /* simple by-value data types */
802  {
803  Datum v = 0;
804 
805  memcpy(&v, ptr, typlen);
806 
807  range->values[i] = fetch_att(&v, true, typlen);
808  ptr += typlen;
809  }
810  else if (typlen > 0) /* fixed-length by-ref types */
811  {
812  range->values[i] = PointerGetDatum(dataptr);
813 
814  memcpy(dataptr, ptr, typlen);
815  dataptr += MAXALIGN(typlen);
816 
817  ptr += typlen;
818  }
819  else if (typlen == -1) /* varlena */
820  {
821  range->values[i] = PointerGetDatum(dataptr);
822 
823  memcpy(dataptr, ptr, VARSIZE_ANY(ptr));
824  dataptr += MAXALIGN(VARSIZE_ANY(ptr));
825  ptr += VARSIZE_ANY(ptr);
826  }
827  else if (typlen == -2) /* cstring */
828  {
829  Size slen = strlen(ptr) + 1;
830 
831  range->values[i] = PointerGetDatum(dataptr);
832 
833  memcpy(dataptr, ptr, slen);
834  dataptr += MAXALIGN(slen);
835  ptr += slen;
836  }
837 
838  /* make sure we haven't overflown the buffer end */
839  Assert(ptr <= ((char *) serialized + VARSIZE_ANY(serialized)));
840  }
841 
842  /* should have consumed the whole input value exactly */
843  Assert(ptr == ((char *) serialized + VARSIZE_ANY(serialized)));
844 
845  /* return the deserialized value */
846  return range;
847 }
848 
849 /*
850  * compare_expanded_ranges
851  * Compare the expanded ranges - first by minimum, then by maximum.
852  *
853  * We do guarantee that ranges in a single Ranges object do not overlap, so it
854  * may seem strange that we don't order just by minimum. But when merging two
855  * Ranges (which happens in the union function), the ranges may in fact
856  * overlap. So we do compare both.
857  */
858 static int
859 compare_expanded_ranges(const void *a, const void *b, void *arg)
860 {
861  ExpandedRange *ra = (ExpandedRange *) a;
862  ExpandedRange *rb = (ExpandedRange *) b;
863  Datum r;
864 
866 
867  /* first compare minvals */
868  r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, ra->minval, rb->minval);
869 
870  if (DatumGetBool(r))
871  return -1;
872 
873  r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, rb->minval, ra->minval);
874 
875  if (DatumGetBool(r))
876  return 1;
877 
878  /* then compare maxvals */
879  r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, ra->maxval, rb->maxval);
880 
881  if (DatumGetBool(r))
882  return -1;
883 
884  r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, rb->maxval, ra->maxval);
885 
886  if (DatumGetBool(r))
887  return 1;
888 
889  return 0;
890 }
891 
892 /*
893  * compare_values
894  * Compare the values.
895  */
896 static int
897 compare_values(const void *a, const void *b, void *arg)
898 {
899  Datum *da = (Datum *) a;
900  Datum *db = (Datum *) b;
901  Datum r;
902 
904 
905  r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, *da, *db);
906 
907  if (DatumGetBool(r))
908  return -1;
909 
910  r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, *db, *da);
911 
912  if (DatumGetBool(r))
913  return 1;
914 
915  return 0;
916 }
917 
918 /*
919  * Check if the new value matches one of the existing ranges.
920  */
921 static bool
922 has_matching_range(BrinDesc *bdesc, Oid colloid, Ranges *ranges,
923  Datum newval, AttrNumber attno, Oid typid)
924 {
925  Datum compar;
926 
927  Datum minvalue = ranges->values[0];
928  Datum maxvalue = ranges->values[2 * ranges->nranges - 1];
929 
930  FmgrInfo *cmpLessFn;
931  FmgrInfo *cmpGreaterFn;
932 
933  /* binary search on ranges */
934  int start,
935  end;
936 
937  if (ranges->nranges == 0)
938  return false;
939 
940  /*
941  * Otherwise, need to compare the new value with boundaries of all the
942  * ranges. First check if it's less than the absolute minimum, which is
943  * the first value in the array.
944  */
945  cmpLessFn = minmax_multi_get_strategy_procinfo(bdesc, attno, typid,
947  compar = FunctionCall2Coll(cmpLessFn, colloid, newval, minvalue);
948 
949  /* smaller than the smallest value in the range list */
950  if (DatumGetBool(compar))
951  return false;
952 
953  /*
954  * And now compare it to the existing maximum (last value in the data
955  * array). But only if we haven't already ruled out a possible match in
956  * the minvalue check.
957  */
958  cmpGreaterFn = minmax_multi_get_strategy_procinfo(bdesc, attno, typid,
960  compar = FunctionCall2Coll(cmpGreaterFn, colloid, newval, maxvalue);
961 
962  if (DatumGetBool(compar))
963  return false;
964 
965  /*
966  * So we know it's in the general min/max, the question is whether it
967  * falls in one of the ranges or gaps. We'll do a binary search on
968  * individual ranges - for each range we check equality (value falls into
969  * the range), and then check ranges either above or below the current
970  * range.
971  */
972  start = 0; /* first range */
973  end = (ranges->nranges - 1); /* last range */
974  while (true)
975  {
976  int midpoint = (start + end) / 2;
977 
978  /* this means we ran out of ranges in the last step */
979  if (start > end)
980  return false;
981 
982  /* copy the min/max values from the ranges */
983  minvalue = ranges->values[2 * midpoint];
984  maxvalue = ranges->values[2 * midpoint + 1];
985 
986  /*
987  * Is the value smaller than the minval? If yes, we'll recurse to the
988  * left side of range array.
989  */
990  compar = FunctionCall2Coll(cmpLessFn, colloid, newval, minvalue);
991 
992  /* smaller than the smallest value in this range */
993  if (DatumGetBool(compar))
994  {
995  end = (midpoint - 1);
996  continue;
997  }
998 
999  /*
1000  * Is the value greater than the minval? If yes, we'll recurse to the
1001  * right side of range array.
1002  */
1003  compar = FunctionCall2Coll(cmpGreaterFn, colloid, newval, maxvalue);
1004 
1005  /* larger than the largest value in this range */
1006  if (DatumGetBool(compar))
1007  {
1008  start = (midpoint + 1);
1009  continue;
1010  }
1011 
1012  /* hey, we found a matching range */
1013  return true;
1014  }
1015 
1016  return false;
1017 }
1018 
1019 
1020 /*
1021  * range_contains_value
1022  * See if the new value is already contained in the range list.
1023  *
1024  * We first inspect the list of intervals. We use a small trick - we check
1025  * the value against min/max of the whole range (min of the first interval,
1026  * max of the last one) first, and only inspect the individual intervals if
1027  * this passes.
1028  *
1029  * If the value matches none of the intervals, we check the exact values.
1030  * We simply loop through them and invoke equality operator on them.
1031  *
1032  * The last parameter (full) determines whether we need to search all the
1033  * values, including the unsorted part. With full=false, the unsorted part
1034  * is not searched, which may produce false negatives and duplicate values
1035  * (in the unsorted part only), but when we're building the range that's
1036  * fine - we'll deduplicate before serialization, and it can only happen
1037  * if there already are unsorted values (so it was already modified).
1038  *
1039  * Serialized ranges don't have any unsorted values, so this can't cause
1040  * false negatives during querying.
1041  */
1042 static bool
1044  AttrNumber attno, Form_pg_attribute attr,
1045  Ranges *ranges, Datum newval, bool full)
1046 {
1047  int i;
1048  FmgrInfo *cmpEqualFn;
1049  Oid typid = attr->atttypid;
1050 
1051  /*
1052  * First inspect the ranges, if there are any. We first check the whole
1053  * range, and only when there's still a chance of getting a match we
1054  * inspect the individual ranges.
1055  */
1056  if (has_matching_range(bdesc, colloid, ranges, newval, attno, typid))
1057  return true;
1058 
1059  cmpEqualFn = minmax_multi_get_strategy_procinfo(bdesc, attno, typid,
1061 
1062  /*
1063  * There is no matching range, so let's inspect the sorted values.
1064  *
1065  * We do a sequential search for small numbers of values, and binary
1066  * search once we have more than 16 values. This threshold is somewhat
1067  * arbitrary, as it depends on how expensive the comparison function is.
1068  *
1069  * XXX If we use the threshold here, maybe we should do the same thing in
1070  * has_matching_range? Or maybe we should do the bin search all the time?
1071  *
1072  * XXX We could use the same optimization as for ranges, to check if the
1073  * value is between min/max, to maybe rule out all sorted values without
1074  * having to inspect all of them.
1075  */
1076  if (ranges->nsorted >= 16)
1077  {
1078  compare_context cxt;
1079 
1080  cxt.colloid = ranges->colloid;
1081  cxt.cmpFn = ranges->cmp;
1082 
1083  if (bsearch_arg(&newval, &ranges->values[2 * ranges->nranges],
1084  ranges->nsorted, sizeof(Datum),
1085  compare_values, (void *) &cxt) != NULL)
1086  return true;
1087  }
1088  else
1089  {
1090  for (i = 2 * ranges->nranges; i < 2 * ranges->nranges + ranges->nsorted; i++)
1091  {
1092  Datum compar;
1093 
1094  compar = FunctionCall2Coll(cmpEqualFn, colloid, newval, ranges->values[i]);
1095 
1096  /* found an exact match */
1097  if (DatumGetBool(compar))
1098  return true;
1099  }
1100  }
1101 
1102  /* If not asked to inspect the unsorted part, we're done. */
1103  if (!full)
1104  return false;
1105 
1106  /* Inspect the unsorted part. */
1107  for (i = 2 * ranges->nranges + ranges->nsorted; i < 2 * ranges->nranges + ranges->nvalues; i++)
1108  {
1109  Datum compar;
1110 
1111  compar = FunctionCall2Coll(cmpEqualFn, colloid, newval, ranges->values[i]);
1112 
1113  /* found an exact match */
1114  if (DatumGetBool(compar))
1115  return true;
1116  }
1117 
1118  /* the value is not covered by this BRIN tuple */
1119  return false;
1120 }
1121 
1122 /*
1123  * Expand ranges from Ranges into ExpandedRange array. This expects the
1124  * eranges to be pre-allocated and with the correct size - there needs to be
1125  * (nranges + nvalues) elements.
1126  *
1127  * The order of expanded ranges is arbitrary. We do expand the ranges first,
1128  * and this part is sorted. But then we expand the values, and this part may
1129  * be unsorted.
1130  */
1131 static void
1132 fill_expanded_ranges(ExpandedRange *eranges, int neranges, Ranges *ranges)
1133 {
1134  int idx;
1135  int i;
1136 
1137  /* Check that the output array has the right size. */
1138  Assert(neranges == (ranges->nranges + ranges->nvalues));
1139 
1140  idx = 0;
1141  for (i = 0; i < ranges->nranges; i++)
1142  {
1143  eranges[idx].minval = ranges->values[2 * i];
1144  eranges[idx].maxval = ranges->values[2 * i + 1];
1145  eranges[idx].collapsed = false;
1146  idx++;
1147 
1148  Assert(idx <= neranges);
1149  }
1150 
1151  for (i = 0; i < ranges->nvalues; i++)
1152  {
1153  eranges[idx].minval = ranges->values[2 * ranges->nranges + i];
1154  eranges[idx].maxval = ranges->values[2 * ranges->nranges + i];
1155  eranges[idx].collapsed = true;
1156  idx++;
1157 
1158  Assert(idx <= neranges);
1159  }
1160 
1161  /* Did we produce the expected number of elements? */
1162  Assert(idx == neranges);
1163 
1164  return;
1165 }
1166 
1167 /*
1168  * Sort and deduplicate expanded ranges.
1169  *
1170  * The ranges may be deduplicated - we're simply appending values, without
1171  * checking for duplicates etc. So maybe the deduplication will reduce the
1172  * number of ranges enough, and we won't have to compute the distances etc.
1173  *
1174  * Returns the number of expanded ranges.
1175  */
1176 static int
1178  ExpandedRange *eranges, int neranges)
1179 {
1180  int n;
1181  int i;
1182  compare_context cxt;
1183 
1184  Assert(neranges > 0);
1185 
1186  /* sort the values */
1187  cxt.colloid = colloid;
1188  cxt.cmpFn = cmp;
1189 
1190  /*
1191  * XXX We do qsort on all the values, but we could also leverage the fact
1192  * that some of the input data is already sorted (all the ranges and maybe
1193  * some of the points) and do merge sort.
1194  */
1195  qsort_arg(eranges, neranges, sizeof(ExpandedRange),
1196  compare_expanded_ranges, (void *) &cxt);
1197 
1198  /*
1199  * Deduplicate the ranges - simply compare each range to the preceding
1200  * one, and skip the duplicate ones.
1201  */
1202  n = 1;
1203  for (i = 1; i < neranges; i++)
1204  {
1205  /* if the current range is equal to the preceding one, do nothing */
1206  if (!compare_expanded_ranges(&eranges[i - 1], &eranges[i], (void *) &cxt))
1207  continue;
1208 
1209  /* otherwise, copy it to n-th place (if not already there) */
1210  if (i != n)
1211  memcpy(&eranges[n], &eranges[i], sizeof(ExpandedRange));
1212 
1213  n++;
1214  }
1215 
1216  Assert((n > 0) && (n <= neranges));
1217 
1218  return n;
1219 }
1220 
1221 /*
1222  * When combining multiple Range values (in union function), some of the
1223  * ranges may overlap. We simply merge the overlapping ranges to fix that.
1224  *
1225  * XXX This assumes the expanded ranges were previously sorted (by minval
1226  * and then maxval). We leverage this when detecting overlap.
1227  */
1228 static int
1230  ExpandedRange *eranges, int neranges)
1231 {
1232  int idx;
1233 
1234  /* Merge ranges (idx) and (idx+1) if they overlap. */
1235  idx = 0;
1236  while (idx < (neranges - 1))
1237  {
1238  Datum r;
1239 
1240  /*
1241  * comparing [?,maxval] vs. [minval,?] - the ranges overlap if (minval
1242  * < maxval)
1243  */
1244  r = FunctionCall2Coll(cmp, colloid,
1245  eranges[idx].maxval,
1246  eranges[idx + 1].minval);
1247 
1248  /*
1249  * Nope, maxval < minval, so no overlap. And we know the ranges are
1250  * ordered, so there are no more overlaps, because all the remaining
1251  * ranges have greater or equal minval.
1252  */
1253  if (DatumGetBool(r))
1254  {
1255  /* proceed to the next range */
1256  idx += 1;
1257  continue;
1258  }
1259 
1260  /*
1261  * So ranges 'idx' and 'idx+1' do overlap, but we don't know if
1262  * 'idx+1' is contained in 'idx', or if they overlap only partially.
1263  * So compare the upper bounds and keep the larger one.
1264  */
1265  r = FunctionCall2Coll(cmp, colloid,
1266  eranges[idx].maxval,
1267  eranges[idx + 1].maxval);
1268 
1269  if (DatumGetBool(r))
1270  eranges[idx].maxval = eranges[idx + 1].maxval;
1271 
1272  /*
1273  * The range certainly is no longer collapsed (irrespectively of the
1274  * previous state).
1275  */
1276  eranges[idx].collapsed = false;
1277 
1278  /*
1279  * Now get rid of the (idx+1) range entirely by shifting the remaining
1280  * ranges by 1. There are neranges elements, and we need to move
1281  * elements from (idx+2). That means the number of elements to move is
1282  * [ncranges - (idx+2)].
1283  */
1284  memmove(&eranges[idx + 1], &eranges[idx + 2],
1285  (neranges - (idx + 2)) * sizeof(ExpandedRange));
1286 
1287  /*
1288  * Decrease the number of ranges, and repeat (with the same range, as
1289  * it might overlap with additional ranges thanks to the merge).
1290  */
1291  neranges--;
1292  }
1293 
1294  return neranges;
1295 }
1296 
1297 /*
1298  * Simple comparator for distance values, comparing the double value.
1299  * This is intentionally sorting the distances in descending order, i.e.
1300  * the longer gaps will be at the front.
1301  */
1302 static int
1303 compare_distances(const void *a, const void *b)
1304 {
1305  DistanceValue *da = (DistanceValue *) a;
1306  DistanceValue *db = (DistanceValue *) b;
1307 
1308  if (da->value < db->value)
1309  return 1;
1310  else if (da->value > db->value)
1311  return -1;
1312 
1313  return 0;
1314 }
1315 
1316 /*
1317  * Given an array of expanded ranges, compute size of the gaps between each
1318  * range. For neranges there are (neranges-1) gaps.
1319  *
1320  * We simply call the "distance" function to compute the (max-min) for pairs
1321  * of consecutive ranges. The function may be fairly expensive, so we do that
1322  * just once (and then use it to pick as many ranges to merge as possible).
1323  *
1324  * See reduce_expanded_ranges for details.
1325  */
1326 static DistanceValue *
1327 build_distances(FmgrInfo *distanceFn, Oid colloid,
1328  ExpandedRange *eranges, int neranges)
1329 {
1330  int i;
1331  int ndistances;
1332  DistanceValue *distances;
1333 
1334  Assert(neranges >= 2);
1335 
1336  ndistances = (neranges - 1);
1337  distances = (DistanceValue *) palloc0(sizeof(DistanceValue) * ndistances);
1338 
1339  /*
1340  * Walk through the ranges once and compute the distance between the
1341  * ranges so that we can sort them once.
1342  */
1343  for (i = 0; i < ndistances; i++)
1344  {
1345  Datum a1,
1346  a2,
1347  r;
1348 
1349  a1 = eranges[i].maxval;
1350  a2 = eranges[i + 1].minval;
1351 
1352  /* compute length of the gap (between max/min) */
1353  r = FunctionCall2Coll(distanceFn, colloid, a1, a2);
1354 
1355  /* remember the index of the gap the distance is for */
1356  distances[i].index = i;
1357  distances[i].value = DatumGetFloat8(r);
1358  }
1359 
1360  /*
1361  * Sort the distances in descending order, so that the longest gaps are at
1362  * the front.
1363  */
1364  pg_qsort(distances, ndistances, sizeof(DistanceValue), compare_distances);
1365 
1366  return distances;
1367 }
1368 
1369 /*
1370  * Builds expanded ranges for the existing ranges (and single-point ranges),
1371  * and also the new value (which did not fit into the array). This expanded
1372  * representation makes the processing a bit easier, as it allows handling
1373  * ranges and points the same way.
1374  *
1375  * We sort and deduplicate the expanded ranges - this is necessary, because
1376  * the points may be unsorted. And moreover the two parts (ranges and
1377  * points) are sorted on their own.
1378  */
1379 static ExpandedRange *
1381  int *nranges)
1382 {
1383  int neranges;
1384  ExpandedRange *eranges;
1385 
1386  /* both ranges and points are expanded into a separate element */
1387  neranges = ranges->nranges + ranges->nvalues;
1388 
1389  eranges = (ExpandedRange *) palloc0(neranges * sizeof(ExpandedRange));
1390 
1391  /* fill the expanded ranges */
1392  fill_expanded_ranges(eranges, neranges, ranges);
1393 
1394  /* sort and deduplicate the expanded ranges */
1395  neranges = sort_expanded_ranges(cmp, colloid, eranges, neranges);
1396 
1397  /* remember how many ranges we built */
1398  *nranges = neranges;
1399 
1400  return eranges;
1401 }
1402 
1403 #ifdef USE_ASSERT_CHECKING
1404 /*
1405  * Counts boundary values needed to store the ranges. Each single-point
1406  * range is stored using a single value, each regular range needs two.
1407  */
1408 static int
1409 count_values(ExpandedRange *cranges, int ncranges)
1410 {
1411  int i;
1412  int count;
1413 
1414  count = 0;
1415  for (i = 0; i < ncranges; i++)
1416  {
1417  if (cranges[i].collapsed)
1418  count += 1;
1419  else
1420  count += 2;
1421  }
1422 
1423  return count;
1424 }
1425 #endif
1426 
1427 /*
1428  * reduce_expanded_ranges
1429  * reduce the ranges until the number of values is low enough
1430  *
1431  * Combines ranges until the number of boundary values drops below the
1432  * threshold specified by max_values. This happens by merging enough
1433  * ranges by the distance between them.
1434  *
1435  * Returns the number of result ranges.
1436  *
1437  * We simply use the global min/max and then add boundaries for enough
1438  * largest gaps. Each gap adds 2 values, so we simply use (target/2-1)
1439  * distances. Then we simply sort all the values - each two values are
1440  * a boundary of a range (possibly collapsed).
1441  *
1442  * XXX Some of the ranges may be collapsed (i.e. the min/max values are
1443  * equal), but we ignore that for now. We could repeat the process,
1444  * adding a couple more gaps recursively.
1445  *
1446  * XXX The ranges to merge are selected solely using the distance. But
1447  * that may not be the best strategy, for example when multiple gaps
1448  * are of equal (or very similar) length.
1449  *
1450  * Consider for example points 1, 2, 3, .., 64, which have gaps of the
1451  * same length 1 of course. In that case, we tend to pick the first
1452  * gap of that length, which leads to this:
1453  *
1454  * step 1: [1, 2], 3, 4, 5, .., 64
1455  * step 2: [1, 3], 4, 5, .., 64
1456  * step 3: [1, 4], 5, .., 64
1457  * ...
1458  *
1459  * So in the end we'll have one "large" range and multiple small points.
1460  * That may be fine, but it seems a bit strange and non-optimal. Maybe
1461  * we should consider other things when picking ranges to merge - e.g.
1462  * length of the ranges? Or perhaps randomize the choice of ranges, with
1463  * probability inversely proportional to the distance (the gap lengths
1464  * may be very close, but not exactly the same).
1465  *
1466  * XXX Or maybe we could just handle this by using random value as a
1467  * tie-break, or by adding random noise to the actual distance.
1468  */
1469 static int
1470 reduce_expanded_ranges(ExpandedRange *eranges, int neranges,
1471  DistanceValue *distances, int max_values,
1472  FmgrInfo *cmp, Oid colloid)
1473 {
1474  int i;
1475  int nvalues;
1476  Datum *values;
1477 
1478  compare_context cxt;
1479 
1480  /* total number of gaps between ranges */
1481  int ndistances = (neranges - 1);
1482 
1483  /* number of gaps to keep */
1484  int keep = (max_values / 2 - 1);
1485 
1486  /*
1487  * Maybe we have a sufficiently low number of ranges already?
1488  *
1489  * XXX This should happen before we actually do the expensive stuff like
1490  * sorting, so maybe this should be just an assert.
1491  */
1492  if (keep >= ndistances)
1493  return neranges;
1494 
1495  /* sort the values */
1496  cxt.colloid = colloid;
1497  cxt.cmpFn = cmp;
1498 
1499  /* allocate space for the boundary values */
1500  nvalues = 0;
1501  values = (Datum *) palloc(sizeof(Datum) * max_values);
1502 
1503  /* add the global min/max values, from the first/last range */
1504  values[nvalues++] = eranges[0].minval;
1505  values[nvalues++] = eranges[neranges - 1].maxval;
1506 
1507  /* add boundary values for enough gaps */
1508  for (i = 0; i < keep; i++)
1509  {
1510  /* index of the gap between (index) and (index+1) ranges */
1511  int index = distances[i].index;
1512 
1513  Assert((index >= 0) && ((index + 1) < neranges));
1514 
1515  /* add max from the preceding range, minval from the next one */
1516  values[nvalues++] = eranges[index].maxval;
1517  values[nvalues++] = eranges[index + 1].minval;
1518 
1519  Assert(nvalues <= max_values);
1520  }
1521 
1522  /* We should have an even number of range values. */
1523  Assert(nvalues % 2 == 0);
1524 
1525  /*
1526  * Sort the values using the comparator function, and form ranges from the
1527  * sorted result.
1528  */
1529  qsort_arg(values, nvalues, sizeof(Datum),
1530  compare_values, (void *) &cxt);
1531 
1532  /* We have nvalues boundary values, which means nvalues/2 ranges. */
1533  for (i = 0; i < (nvalues / 2); i++)
1534  {
1535  eranges[i].minval = values[2 * i];
1536  eranges[i].maxval = values[2 * i + 1];
1537 
1538  /* if the boundary values are the same, it's a collapsed range */
1539  eranges[i].collapsed = (compare_values(&values[2 * i],
1540  &values[2 * i + 1],
1541  &cxt) == 0);
1542  }
1543 
1544  return (nvalues / 2);
1545 }
1546 
1547 /*
1548  * Store the boundary values from ExpandedRanges back into 'ranges' (using
1549  * only the minimal number of values needed).
1550  */
1551 static void
1552 store_expanded_ranges(Ranges *ranges, ExpandedRange *eranges, int neranges)
1553 {
1554  int i;
1555  int idx = 0;
1556 
1557  /* first copy in the regular ranges */
1558  ranges->nranges = 0;
1559  for (i = 0; i < neranges; i++)
1560  {
1561  if (!eranges[i].collapsed)
1562  {
1563  ranges->values[idx++] = eranges[i].minval;
1564  ranges->values[idx++] = eranges[i].maxval;
1565  ranges->nranges++;
1566  }
1567  }
1568 
1569  /* now copy in the collapsed ones */
1570  ranges->nvalues = 0;
1571  for (i = 0; i < neranges; i++)
1572  {
1573  if (eranges[i].collapsed)
1574  {
1575  ranges->values[idx++] = eranges[i].minval;
1576  ranges->nvalues++;
1577  }
1578  }
1579 
1580  /* all the values are sorted */
1581  ranges->nsorted = ranges->nvalues;
1582 
1583  Assert(count_values(eranges, neranges) == 2 * ranges->nranges + ranges->nvalues);
1584  Assert(2 * ranges->nranges + ranges->nvalues <= ranges->maxvalues);
1585 }
1586 
1587 
1588 /*
1589  * Consider freeing space in the ranges. Checks if there's space for at least
1590  * one new value, and performs compaction if needed.
1591  *
1592  * Returns true if the value was actually modified.
1593  */
1594 static bool
1596  AttrNumber attno, Form_pg_attribute attr,
1597  Ranges *range)
1598 {
1599  MemoryContext ctx;
1600  MemoryContext oldctx;
1601 
1602  FmgrInfo *cmpFn,
1603  *distanceFn;
1604 
1605  /* expanded ranges */
1606  ExpandedRange *eranges;
1607  int neranges;
1608  DistanceValue *distances;
1609 
1610  /*
1611  * If there is free space in the buffer, we're done without having to
1612  * modify anything.
1613  */
1614  if (2 * range->nranges + range->nvalues < range->maxvalues)
1615  return false;
1616 
1617  /* we'll certainly need the comparator, so just look it up now */
1618  cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
1620 
1621  /* deduplicate values, if there's an unsorted part */
1623 
1624  /*
1625  * Did we reduce enough free space by just the deduplication?
1626  *
1627  * We don't simply check against range->maxvalues again. The deduplication
1628  * might have freed very little space (e.g. just one value), forcing us to
1629  * do deduplication very often. In that case, it's better to do the
1630  * compaction and reduce more space.
1631  */
1632  if (2 * range->nranges + range->nvalues <= range->maxvalues * MINMAX_BUFFER_LOAD_FACTOR)
1633  return true;
1634 
1635  /*
1636  * We need to combine some of the existing ranges, to reduce the number of
1637  * values we have to store.
1638  *
1639  * The distanceFn calls (which may internally call e.g. numeric_le) may
1640  * allocate quite a bit of memory, and we must not leak it (we might have
1641  * to do this repeatedly, even for a single BRIN page range). Otherwise
1642  * we'd have problems e.g. when building new indexes. So we use a memory
1643  * context and make sure we free the memory at the end (so if we call the
1644  * distance function many times, it might be an issue, but meh).
1645  */
1647  "minmax-multi context",
1649 
1650  oldctx = MemoryContextSwitchTo(ctx);
1651 
1652  /* build the expanded ranges */
1653  eranges = build_expanded_ranges(cmpFn, colloid, range, &neranges);
1654 
1655  /* and we'll also need the 'distance' procedure */
1656  distanceFn = minmax_multi_get_procinfo(bdesc, attno, PROCNUM_DISTANCE);
1657 
1658  /* build array of gap distances and sort them in ascending order */
1659  distances = build_distances(distanceFn, colloid, eranges, neranges);
1660 
1661  /*
1662  * Combine ranges until we release at least 50% of the space. This
1663  * threshold is somewhat arbitrary, perhaps needs tuning. We must not use
1664  * too low or high value.
1665  */
1666  neranges = reduce_expanded_ranges(eranges, neranges, distances,
1667  range->maxvalues * MINMAX_BUFFER_LOAD_FACTOR,
1668  cmpFn, colloid);
1669 
1670  /* Make sure we've sufficiently reduced the number of ranges. */
1671  Assert(count_values(eranges, neranges) <= range->maxvalues * MINMAX_BUFFER_LOAD_FACTOR);
1672 
1673  /* decompose the expanded ranges into regular ranges and single values */
1674  store_expanded_ranges(range, eranges, neranges);
1675 
1676  MemoryContextSwitchTo(oldctx);
1677  MemoryContextDelete(ctx);
1678 
1679  /* Did we break the ranges somehow? */
1680  AssertCheckRanges(range, cmpFn, colloid);
1681 
1682  return true;
1683 }
1684 
1685 /*
1686  * range_add_value
1687  * Add the new value to the minmax-multi range.
1688  */
1689 static bool
1690 range_add_value(BrinDesc *bdesc, Oid colloid,
1691  AttrNumber attno, Form_pg_attribute attr,
1692  Ranges *ranges, Datum newval)
1693 {
1694  FmgrInfo *cmpFn;
1695  bool modified = false;
1696 
1697  /* we'll certainly need the comparator, so just look it up now */
1698  cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
1700 
1701  /* comprehensive checks of the input ranges */
1702  AssertCheckRanges(ranges, cmpFn, colloid);
1703 
1704  /*
1705  * Make sure there's enough free space in the buffer. We only trigger this
1706  * when the buffer is full, which means it had to be modified as we size
1707  * it to be larger than what is stored on disk.
1708  *
1709  * This needs to happen before we check if the value is contained in the
1710  * range, because the value might be in the unsorted part, and we don't
1711  * check that in range_contains_value. The deduplication would then move
1712  * it to the sorted part, and we'd add the value too, which violates the
1713  * rule that we never have duplicates with the ranges or sorted values.
1714  *
1715  * We might also deduplicate and recheck if the value is contained, but
1716  * that seems like overkill. We'd need to deduplicate anyway, so why not
1717  * do it now.
1718  */
1719  modified = ensure_free_space_in_buffer(bdesc, colloid,
1720  attno, attr, ranges);
1721 
1722  /*
1723  * Bail out if the value already is covered by the range.
1724  *
1725  * We could also add values until we hit values_per_range, and then do the
1726  * deduplication in a batch, hoping for better efficiency. But that would
1727  * mean we actually modify the range every time, which means having to
1728  * serialize the value, which does palloc, walks the values, copies them,
1729  * etc. Not exactly cheap.
1730  *
1731  * So instead we do the check, which should be fairly cheap - assuming the
1732  * comparator function is not very expensive.
1733  *
1734  * This also implies the values array can't contain duplicate values.
1735  */
1736  if (range_contains_value(bdesc, colloid, attno, attr, ranges, newval, false))
1737  return modified;
1738 
1739  /* Make a copy of the value, if needed. */
1740  newval = datumCopy(newval, attr->attbyval, attr->attlen);
1741 
1742  /*
1743  * If there's space in the values array, copy it in and we're done.
1744  *
1745  * We do want to keep the values sorted (to speed up searches), so we do a
1746  * simple insertion sort. We could do something more elaborate, e.g. by
1747  * sorting the values only now and then, but for small counts (e.g. when
1748  * maxvalues is 64) this should be fine.
1749  */
1750  ranges->values[2 * ranges->nranges + ranges->nvalues] = newval;
1751  ranges->nvalues++;
1752 
1753  /* If we added the first value, we can consider it as sorted. */
1754  if (ranges->nvalues == 1)
1755  ranges->nsorted = 1;
1756 
1757  /*
1758  * Check we haven't broken the ordering of boundary values (checks both
1759  * parts, but that doesn't hurt).
1760  */
1761  AssertCheckRanges(ranges, cmpFn, colloid);
1762 
1763  /* Check the range contains the value we just added. */
1764  Assert(range_contains_value(bdesc, colloid, attno, attr, ranges, newval, true));
1765 
1766  /* yep, we've modified the range */
1767  return true;
1768 }
1769 
1770 /*
1771  * Generate range representation of data collected during "batch mode".
1772  * This is similar to reduce_expanded_ranges, except that we can't assume
1773  * the values are sorted and there may be duplicate values.
1774  */
1775 static void
1776 compactify_ranges(BrinDesc *bdesc, Ranges *ranges, int max_values)
1777 {
1778  FmgrInfo *cmpFn,
1779  *distanceFn;
1780 
1781  /* expanded ranges */
1782  ExpandedRange *eranges;
1783  int neranges;
1784  DistanceValue *distances;
1785 
1786  MemoryContext ctx;
1787  MemoryContext oldctx;
1788 
1789  /*
1790  * Do we need to actually compactify anything?
1791  *
1792  * There are two reasons why compaction may be needed - firstly, there may
1793  * be too many values, or some of the values may be unsorted.
1794  */
1795  if ((ranges->nranges * 2 + ranges->nvalues <= max_values) &&
1796  (ranges->nsorted == ranges->nvalues))
1797  return;
1798 
1799  /* we'll certainly need the comparator, so just look it up now */
1800  cmpFn = minmax_multi_get_strategy_procinfo(bdesc, ranges->attno, ranges->typid,
1802 
1803  /* and we'll also need the 'distance' procedure */
1804  distanceFn = minmax_multi_get_procinfo(bdesc, ranges->attno, PROCNUM_DISTANCE);
1805 
1806  /*
1807  * The distanceFn calls (which may internally call e.g. numeric_le) may
1808  * allocate quite a bit of memory, and we must not leak it. Otherwise,
1809  * we'd have problems e.g. when building indexes. So we create a local
1810  * memory context and make sure we free the memory before leaving this
1811  * function (not after every call).
1812  */
1814  "minmax-multi context",
1816 
1817  oldctx = MemoryContextSwitchTo(ctx);
1818 
1819  /* build the expanded ranges */
1820  eranges = build_expanded_ranges(cmpFn, ranges->colloid, ranges, &neranges);
1821 
1822  /* build array of gap distances and sort them in ascending order */
1823  distances = build_distances(distanceFn, ranges->colloid,
1824  eranges, neranges);
1825 
1826  /*
1827  * Combine ranges until we get below max_values. We don't use any scale
1828  * factor, because this is used during serialization, and we don't expect
1829  * more tuples to be inserted anytime soon.
1830  */
1831  neranges = reduce_expanded_ranges(eranges, neranges, distances,
1832  max_values, cmpFn, ranges->colloid);
1833 
1834  Assert(count_values(eranges, neranges) <= max_values);
1835 
1836  /* transform back into regular ranges and single values */
1837  store_expanded_ranges(ranges, eranges, neranges);
1838 
1839  /* check all the range invariants */
1840  AssertCheckRanges(ranges, cmpFn, ranges->colloid);
1841 
1842  MemoryContextSwitchTo(oldctx);
1843  MemoryContextDelete(ctx);
1844 }
1845 
1846 Datum
1848 {
1849  BrinOpcInfo *result;
1850 
1851  /*
1852  * opaque->strategy_procinfos is initialized lazily; here it is set to
1853  * all-uninitialized by palloc0 which sets fn_oid to InvalidOid.
1854  */
1855 
1856  result = palloc0(MAXALIGN(SizeofBrinOpcInfo(1)) +
1857  sizeof(MinmaxMultiOpaque));
1858  result->oi_nstored = 1;
1859  result->oi_regular_nulls = true;
1860  result->oi_opaque = (MinmaxMultiOpaque *)
1861  MAXALIGN((char *) result + SizeofBrinOpcInfo(1));
1862  result->oi_typcache[0] = lookup_type_cache(PG_BRIN_MINMAX_MULTI_SUMMARYOID, 0);
1863 
1864  PG_RETURN_POINTER(result);
1865 }
1866 
1867 /*
1868  * Compute the distance between two float4 values (plain subtraction).
1869  */
1870 Datum
1872 {
1873  float a1 = PG_GETARG_FLOAT4(0);
1874  float a2 = PG_GETARG_FLOAT4(1);
1875 
1876  /* if both values are NaN, then we consider them the same */
1877  if (isnan(a1) && isnan(a2))
1878  PG_RETURN_FLOAT8(0.0);
1879 
1880  /* if one value is NaN, use infinite distance */
1881  if (isnan(a1) || isnan(a2))
1883 
1884  /*
1885  * We know the values are range boundaries, but the range may be collapsed
1886  * (i.e. single points), with equal values.
1887  */
1888  Assert(a1 <= a2);
1889 
1890  PG_RETURN_FLOAT8((double) a2 - (double) a1);
1891 }
1892 
1893 /*
1894  * Compute the distance between two float8 values (plain subtraction).
1895  */
1896 Datum
1898 {
1899  double a1 = PG_GETARG_FLOAT8(0);
1900  double a2 = PG_GETARG_FLOAT8(1);
1901 
1902  /* if both values are NaN, then we consider them the same */
1903  if (isnan(a1) && isnan(a2))
1904  PG_RETURN_FLOAT8(0.0);
1905 
1906  /* if one value is NaN, use infinite distance */
1907  if (isnan(a1) || isnan(a2))
1909 
1910  /*
1911  * We know the values are range boundaries, but the range may be collapsed
1912  * (i.e. single points), with equal values.
1913  */
1914  Assert(a1 <= a2);
1915 
1916  PG_RETURN_FLOAT8(a2 - a1);
1917 }
1918 
1919 /*
1920  * Compute the distance between two int2 values (plain subtraction).
1921  */
1922 Datum
1924 {
1925  int16 a1 = PG_GETARG_INT16(0);
1926  int16 a2 = PG_GETARG_INT16(1);
1927 
1928  /*
1929  * We know the values are range boundaries, but the range may be collapsed
1930  * (i.e. single points), with equal values.
1931  */
1932  Assert(a1 <= a2);
1933 
1934  PG_RETURN_FLOAT8((double) a2 - (double) a1);
1935 }
1936 
1937 /*
1938  * Compute the distance between two int4 values (plain subtraction).
1939  */
1940 Datum
1942 {
1943  int32 a1 = PG_GETARG_INT32(0);
1944  int32 a2 = PG_GETARG_INT32(1);
1945 
1946  /*
1947  * We know the values are range boundaries, but the range may be collapsed
1948  * (i.e. single points), with equal values.
1949  */
1950  Assert(a1 <= a2);
1951 
1952  PG_RETURN_FLOAT8((double) a2 - (double) a1);
1953 }
1954 
1955 /*
1956  * Compute the distance between two int8 values (plain subtraction).
1957  */
1958 Datum
1960 {
1961  int64 a1 = PG_GETARG_INT64(0);
1962  int64 a2 = PG_GETARG_INT64(1);
1963 
1964  /*
1965  * We know the values are range boundaries, but the range may be collapsed
1966  * (i.e. single points), with equal values.
1967  */
1968  Assert(a1 <= a2);
1969 
1970  PG_RETURN_FLOAT8((double) a2 - (double) a1);
1971 }
1972 
1973 /*
1974  * Compute the distance between two tid values (by mapping them to float8 and
1975  * then subtracting them).
1976  */
1977 Datum
1979 {
1980  double da1,
1981  da2;
1982 
1985 
1986  /*
1987  * We know the values are range boundaries, but the range may be collapsed
1988  * (i.e. single points), with equal values.
1989  */
1990  Assert(ItemPointerCompare(pa1, pa2) <= 0);
1991 
1992  /*
1993  * We use the no-check variants here, because user-supplied values may
1994  * have (ip_posid == 0). See ItemPointerCompare.
1995  */
1998 
2001 
2002  PG_RETURN_FLOAT8(da2 - da1);
2003 }
2004 
2005 /*
2006  * Compute the distance between two numeric values (plain subtraction).
2007  */
2008 Datum
2010 {
2011  Datum d;
2012  Datum a1 = PG_GETARG_DATUM(0);
2013  Datum a2 = PG_GETARG_DATUM(1);
2014 
2015  /*
2016  * We know the values are range boundaries, but the range may be collapsed
2017  * (i.e. single points), with equal values.
2018  */
2020 
2021  d = DirectFunctionCall2(numeric_sub, a2, a1); /* a2 - a1 */
2022 
2024 }
2025 
2026 /*
2027  * Compute the approximate distance between two UUID values.
2028  *
2029  * XXX We do not need a perfectly accurate value, so we approximate the
2030  * deltas (which would have to be 128-bit integers) with a 64-bit float.
2031  * The small inaccuracies do not matter in practice, in the worst case
2032  * we'll decide to merge ranges that are not the closest ones.
2033  */
2034 Datum
2036 {
2037  int i;
2038  float8 delta = 0;
2039 
2040  Datum a1 = PG_GETARG_DATUM(0);
2041  Datum a2 = PG_GETARG_DATUM(1);
2042 
2043  pg_uuid_t *u1 = DatumGetUUIDP(a1);
2044  pg_uuid_t *u2 = DatumGetUUIDP(a2);
2045 
2046  /*
2047  * We know the values are range boundaries, but the range may be collapsed
2048  * (i.e. single points), with equal values.
2049  */
2051 
2052  /* compute approximate delta as a double precision value */
2053  for (i = UUID_LEN - 1; i >= 0; i--)
2054  {
2055  delta += (int) u2->data[i] - (int) u1->data[i];
2056  delta /= 256;
2057  }
2058 
2059  Assert(delta >= 0);
2060 
2061  PG_RETURN_FLOAT8(delta);
2062 }
2063 
2064 /*
2065  * Compute the approximate distance between two dates.
2066  */
2067 Datum
2069 {
2070  DateADT dateVal1 = PG_GETARG_DATEADT(0);
2071  DateADT dateVal2 = PG_GETARG_DATEADT(1);
2072 
2073  if (DATE_NOT_FINITE(dateVal1) || DATE_NOT_FINITE(dateVal2))
2074  PG_RETURN_FLOAT8(0);
2075 
2076  PG_RETURN_FLOAT8(dateVal1 - dateVal2);
2077 }
2078 
2079 /*
2080  * Compute the approximate distance between two time (without tz) values.
2081  *
2082  * TimeADT is just an int64, so we simply subtract the values directly.
2083  */
2084 Datum
2086 {
2087  float8 delta = 0;
2088 
2089  TimeADT ta = PG_GETARG_TIMEADT(0);
2090  TimeADT tb = PG_GETARG_TIMEADT(1);
2091 
2092  delta = (tb - ta);
2093 
2094  Assert(delta >= 0);
2095 
2096  PG_RETURN_FLOAT8(delta);
2097 }
2098 
2099 /*
2100  * Compute the approximate distance between two timetz values.
2101  *
2102  * Simply subtracts the TimeADT (int64) values embedded in TimeTzADT.
2103  */
2104 Datum
2106 {
2107  float8 delta = 0;
2108 
2111 
2112  delta = (tb->time - ta->time) + (tb->zone - ta->zone) * USECS_PER_SEC;
2113 
2114  Assert(delta >= 0);
2115 
2116  PG_RETURN_FLOAT8(delta);
2117 }
2118 
2119 /*
2120  * Compute the distance between two timestamp values.
2121  */
2122 Datum
2124 {
2125  float8 delta = 0;
2126 
2127  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2128  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2129 
2130  if (TIMESTAMP_NOT_FINITE(dt1) || TIMESTAMP_NOT_FINITE(dt2))
2131  PG_RETURN_FLOAT8(0);
2132 
2133  delta = dt2 - dt1;
2134 
2135  Assert(delta >= 0);
2136 
2137  PG_RETURN_FLOAT8(delta);
2138 }
2139 
2140 /*
2141  * Compute the distance between two interval values.
2142  */
2143 Datum
2145 {
2146  float8 delta = 0;
2147 
2148  Interval *ia = PG_GETARG_INTERVAL_P(0);
2149  Interval *ib = PG_GETARG_INTERVAL_P(1);
2150  Interval *result;
2151 
2152  int64 dayfraction;
2153  int64 days;
2154 
2155  result = (Interval *) palloc(sizeof(Interval));
2156 
2157  result->month = ib->month - ia->month;
2158  /* overflow check copied from int4mi */
2159  if (!SAMESIGN(ib->month, ia->month) &&
2160  !SAMESIGN(result->month, ib->month))
2161  ereport(ERROR,
2162  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2163  errmsg("interval out of range")));
2164 
2165  result->day = ib->day - ia->day;
2166  if (!SAMESIGN(ib->day, ia->day) &&
2167  !SAMESIGN(result->day, ib->day))
2168  ereport(ERROR,
2169  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2170  errmsg("interval out of range")));
2171 
2172  result->time = ib->time - ia->time;
2173  if (!SAMESIGN(ib->time, ia->time) &&
2174  !SAMESIGN(result->time, ib->time))
2175  ereport(ERROR,
2176  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2177  errmsg("interval out of range")));
2178 
2179  /*
2180  * Delta is (fractional) number of days between the intervals. Assume
2181  * months have 30 days for consistency with interval_cmp_internal. We
2182  * don't need to be exact, in the worst case we'll build a bit less
2183  * efficient ranges. But we should not contradict interval_cmp.
2184  */
2185  dayfraction = result->time % USECS_PER_DAY;
2186  days = result->time / USECS_PER_DAY;
2187  days += result->month * INT64CONST(30);
2188  days += result->day;
2189 
2190  /* convert to double precision */
2191  delta = (double) days + dayfraction / (double) USECS_PER_DAY;
2192 
2193  Assert(delta >= 0);
2194 
2195  PG_RETURN_FLOAT8(delta);
2196 }
2197 
2198 /*
2199  * Compute the distance between two pg_lsn values.
2200  *
2201  * LSN is just an int64 encoding position in the stream, so just subtract
2202  * those int64 values directly.
2203  */
2204 Datum
2206 {
2207  float8 delta = 0;
2208 
2209  XLogRecPtr lsna = PG_GETARG_LSN(0);
2210  XLogRecPtr lsnb = PG_GETARG_LSN(1);
2211 
2212  delta = (lsnb - lsna);
2213 
2214  Assert(delta >= 0);
2215 
2216  PG_RETURN_FLOAT8(delta);
2217 }
2218 
2219 /*
2220  * Compute the distance between two macaddr values.
2221  *
2222  * mac addresses are treated as 6 unsigned chars, so do the same thing we
2223  * already do for UUID values.
2224  */
2225 Datum
2227 {
2228  float8 delta;
2229 
2232 
2233  delta = ((float8) b->f - (float8) a->f);
2234  delta /= 256;
2235 
2236  delta += ((float8) b->e - (float8) a->e);
2237  delta /= 256;
2238 
2239  delta += ((float8) b->d - (float8) a->d);
2240  delta /= 256;
2241 
2242  delta += ((float8) b->c - (float8) a->c);
2243  delta /= 256;
2244 
2245  delta += ((float8) b->b - (float8) a->b);
2246  delta /= 256;
2247 
2248  delta += ((float8) b->a - (float8) a->a);
2249  delta /= 256;
2250 
2251  Assert(delta >= 0);
2252 
2253  PG_RETURN_FLOAT8(delta);
2254 }
2255 
2256 /*
2257  * Compute the distance between two macaddr8 values.
2258  *
2259  * macaddr8 addresses are 8 unsigned chars, so do the same thing we
2260  * already do for UUID values.
2261  */
2262 Datum
2264 {
2265  float8 delta;
2266 
2269 
2270  delta = ((float8) b->h - (float8) a->h);
2271  delta /= 256;
2272 
2273  delta += ((float8) b->g - (float8) a->g);
2274  delta /= 256;
2275 
2276  delta += ((float8) b->f - (float8) a->f);
2277  delta /= 256;
2278 
2279  delta += ((float8) b->e - (float8) a->e);
2280  delta /= 256;
2281 
2282  delta += ((float8) b->d - (float8) a->d);
2283  delta /= 256;
2284 
2285  delta += ((float8) b->c - (float8) a->c);
2286  delta /= 256;
2287 
2288  delta += ((float8) b->b - (float8) a->b);
2289  delta /= 256;
2290 
2291  delta += ((float8) b->a - (float8) a->a);
2292  delta /= 256;
2293 
2294  Assert(delta >= 0);
2295 
2296  PG_RETURN_FLOAT8(delta);
2297 }
2298 
2299 /*
2300  * Compute the distance between two inet values.
2301  *
2302  * The distance is defined as the difference between 32-bit/128-bit values,
2303  * depending on the IP version. The distance is computed by subtracting
2304  * the bytes and normalizing it to [0,1] range for each IP family.
2305  * Addresses from different families are considered to be in maximum
2306  * distance, which is 1.0.
2307  *
2308  * XXX Does this need to consider the mask (bits)? For now, it's ignored.
2309  */
2310 Datum
2312 {
2313  float8 delta;
2314  int i;
2315  int len;
2316  unsigned char *addra,
2317  *addrb;
2318 
2319  inet *ipa = PG_GETARG_INET_PP(0);
2320  inet *ipb = PG_GETARG_INET_PP(1);
2321 
2322  int lena,
2323  lenb;
2324 
2325  /*
2326  * If the addresses are from different families, consider them to be in
2327  * maximal possible distance (which is 1.0).
2328  */
2329  if (ip_family(ipa) != ip_family(ipb))
2330  PG_RETURN_FLOAT8(1.0);
2331 
2332  addra = (unsigned char *) palloc(ip_addrsize(ipa));
2333  memcpy(addra, ip_addr(ipa), ip_addrsize(ipa));
2334 
2335  addrb = (unsigned char *) palloc(ip_addrsize(ipb));
2336  memcpy(addrb, ip_addr(ipb), ip_addrsize(ipb));
2337 
2338  /*
2339  * The length is calculated from the mask length, because we sort the
2340  * addresses by first address in the range, so A.B.C.D/24 < A.B.C.1 (the
2341  * first range starts at A.B.C.0, which is before A.B.C.1). We don't want
2342  * to produce a negative delta in this case, so we just cut the extra
2343  * bytes.
2344  *
2345  * XXX Maybe this should be a bit more careful and cut the bits, not just
2346  * whole bytes.
2347  */
2348  lena = ip_bits(ipa);
2349  lenb = ip_bits(ipb);
2350 
2351  len = ip_addrsize(ipa);
2352 
2353  /* apply the network mask to both addresses */
2354  for (i = 0; i < len; i++)
2355  {
2356  unsigned char mask;
2357  int nbits;
2358 
2359  nbits = lena - (i * 8);
2360  if (nbits < 8)
2361  {
2362  mask = (0xFF << (8 - nbits));
2363  addra[i] = (addra[i] & mask);
2364  }
2365 
2366  nbits = lenb - (i * 8);
2367  if (nbits < 8)
2368  {
2369  mask = (0xFF << (8 - nbits));
2370  addrb[i] = (addrb[i] & mask);
2371  }
2372  }
2373 
2374  /* Calculate the difference between the addresses. */
2375  delta = 0;
2376  for (i = len - 1; i >= 0; i--)
2377  {
2378  unsigned char a = addra[i];
2379  unsigned char b = addrb[i];
2380 
2381  delta += (float8) b - (float8) a;
2382  delta /= 256;
2383  }
2384 
2385  Assert((delta >= 0) && (delta <= 1));
2386 
2387  pfree(addra);
2388  pfree(addrb);
2389 
2390  PG_RETURN_FLOAT8(delta);
2391 }
2392 
2393 static void
2395 {
2396  Ranges *ranges = (Ranges *) DatumGetPointer(src);
2397  SerializedRanges *s;
2398 
2399  /*
2400  * In batch mode, we need to compress the accumulated values to the
2401  * actually requested number of values/ranges.
2402  */
2403  compactify_ranges(bdesc, ranges, ranges->target_maxvalues);
2404 
2405  /* At this point everything has to be fully sorted. */
2406  Assert(ranges->nsorted == ranges->nvalues);
2407 
2408  s = brin_range_serialize(ranges);
2409  dst[0] = PointerGetDatum(s);
2410 }
2411 
2412 static int
2414 {
2416 }
2417 
2418 /*
2419  * Examine the given index tuple (which contains the partial status of a
2420  * certain page range) by comparing it to the given value that comes from
2421  * another heap tuple. If the new value is outside the min/max range
2422  * specified by the existing tuple values, update the index tuple and return
2423  * true. Otherwise, return false and do not modify in this case.
2424  */
2425 Datum
2427 {
2428  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
2429  BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
2431  bool isnull PG_USED_FOR_ASSERTS_ONLY = PG_GETARG_DATUM(3);
2433  Oid colloid = PG_GET_COLLATION();
2434  bool modified = false;
2435  Form_pg_attribute attr;
2436  AttrNumber attno;
2437  Ranges *ranges;
2438  SerializedRanges *serialized = NULL;
2439 
2440  Assert(!isnull);
2441 
2442  attno = column->bv_attno;
2443  attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
2444 
2445  /* use the already deserialized value, if possible */
2446  ranges = (Ranges *) DatumGetPointer(column->bv_mem_value);
2447 
2448  /*
2449  * If this is the first non-null value, we need to initialize the range
2450  * list. Otherwise, just extract the existing range list from BrinValues.
2451  *
2452  * When starting with an empty range, we assume this is a batch mode and
2453  * we use a larger buffer. The buffer size is derived from the BRIN range
2454  * size, number of rows per page, with some sensible min/max values. A
2455  * small buffer would be bad for performance, but a large buffer might
2456  * require a lot of memory (because of keeping all the values).
2457  */
2458  if (column->bv_allnulls)
2459  {
2460  MemoryContext oldctx;
2461 
2462  int target_maxvalues;
2463  int maxvalues;
2464  BlockNumber pagesPerRange = BrinGetPagesPerRange(bdesc->bd_index);
2465 
2466  /* what was specified as a reloption? */
2467  target_maxvalues = brin_minmax_multi_get_values(bdesc, opts);
2468 
2469  /*
2470  * Determine the insert buffer size - we use 10x the target, capped to
2471  * the maximum number of values in the heap range. This is more than
2472  * enough, considering the actual number of rows per page is likely
2473  * much lower, but meh.
2474  */
2475  maxvalues = Min(target_maxvalues * MINMAX_BUFFER_FACTOR,
2476  MaxHeapTuplesPerPage * pagesPerRange);
2477 
2478  /* but always at least the original value */
2479  maxvalues = Max(maxvalues, target_maxvalues);
2480 
2481  /* always cap by MIN/MAX */
2482  maxvalues = Max(maxvalues, MINMAX_BUFFER_MIN);
2483  maxvalues = Min(maxvalues, MINMAX_BUFFER_MAX);
2484 
2485  oldctx = MemoryContextSwitchTo(column->bv_context);
2486  ranges = minmax_multi_init(maxvalues);
2487  ranges->attno = attno;
2488  ranges->colloid = colloid;
2489  ranges->typid = attr->atttypid;
2490  ranges->target_maxvalues = target_maxvalues;
2491 
2492  /* we'll certainly need the comparator, so just look it up now */
2493  ranges->cmp = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
2495 
2496  MemoryContextSwitchTo(oldctx);
2497 
2498  column->bv_allnulls = false;
2499  modified = true;
2500 
2501  column->bv_mem_value = PointerGetDatum(ranges);
2503  }
2504  else if (!ranges)
2505  {
2506  MemoryContext oldctx;
2507 
2508  int maxvalues;
2509  BlockNumber pagesPerRange = BrinGetPagesPerRange(bdesc->bd_index);
2510 
2511  oldctx = MemoryContextSwitchTo(column->bv_context);
2512 
2513  serialized = (SerializedRanges *) PG_DETOAST_DATUM(column->bv_values[0]);
2514 
2515  /*
2516  * Determine the insert buffer size - we use 10x the target, capped to
2517  * the maximum number of values in the heap range. This is more than
2518  * enough, considering the actual number of rows per page is likely
2519  * much lower, but meh.
2520  */
2521  maxvalues = Min(serialized->maxvalues * MINMAX_BUFFER_FACTOR,
2522  MaxHeapTuplesPerPage * pagesPerRange);
2523 
2524  /* but always at least the original value */
2525  maxvalues = Max(maxvalues, serialized->maxvalues);
2526 
2527  /* always cap by MIN/MAX */
2528  maxvalues = Max(maxvalues, MINMAX_BUFFER_MIN);
2529  maxvalues = Min(maxvalues, MINMAX_BUFFER_MAX);
2530 
2531  ranges = brin_range_deserialize(maxvalues, serialized);
2532 
2533  ranges->attno = attno;
2534  ranges->colloid = colloid;
2535  ranges->typid = attr->atttypid;
2536 
2537  /* we'll certainly need the comparator, so just look it up now */
2538  ranges->cmp = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
2540 
2541  column->bv_mem_value = PointerGetDatum(ranges);
2543 
2544  MemoryContextSwitchTo(oldctx);
2545  }
2546 
2547  /*
2548  * Try to add the new value to the range. We need to update the modified
2549  * flag, so that we serialize the updated summary later.
2550  */
2551  modified |= range_add_value(bdesc, colloid, attno, attr, ranges, newval);
2552 
2553 
2554  PG_RETURN_BOOL(modified);
2555 }
2556 
2557 /*
2558  * Given an index tuple corresponding to a certain page range and a scan key,
2559  * return whether the scan key is consistent with the index tuple's min/max
2560  * values. Return true if so, false otherwise.
2561  */
2562 Datum
2564 {
2565  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
2566  BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
2567  ScanKey *keys = (ScanKey *) PG_GETARG_POINTER(2);
2568  int nkeys = PG_GETARG_INT32(3);
2569 
2570  Oid colloid = PG_GET_COLLATION(),
2571  subtype;
2572  AttrNumber attno;
2573  Datum value;
2574  FmgrInfo *finfo;
2575  SerializedRanges *serialized;
2576  Ranges *ranges;
2577  int keyno;
2578  int rangeno;
2579  int i;
2580 
2581  attno = column->bv_attno;
2582 
2583  serialized = (SerializedRanges *) PG_DETOAST_DATUM(column->bv_values[0]);
2584  ranges = brin_range_deserialize(serialized->maxvalues, serialized);
2585 
2586  /* inspect the ranges, and for each one evaluate the scan keys */
2587  for (rangeno = 0; rangeno < ranges->nranges; rangeno++)
2588  {
2589  Datum minval = ranges->values[2 * rangeno];
2590  Datum maxval = ranges->values[2 * rangeno + 1];
2591 
2592  /* assume the range is matching, and we'll try to prove otherwise */
2593  bool matching = true;
2594 
2595  for (keyno = 0; keyno < nkeys; keyno++)
2596  {
2597  Datum matches;
2598  ScanKey key = keys[keyno];
2599 
2600  /* NULL keys are handled and filtered-out in bringetbitmap */
2601  Assert(!(key->sk_flags & SK_ISNULL));
2602 
2603  attno = key->sk_attno;
2604  subtype = key->sk_subtype;
2605  value = key->sk_argument;
2606  switch (key->sk_strategy)
2607  {
2608  case BTLessStrategyNumber:
2610  finfo = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2611  key->sk_strategy);
2612  /* first value from the array */
2613  matches = FunctionCall2Coll(finfo, colloid, minval, value);
2614  break;
2615 
2616  case BTEqualStrategyNumber:
2617  {
2618  Datum compar;
2619  FmgrInfo *cmpFn;
2620 
2621  /* by default this range does not match */
2622  matches = false;
2623 
2624  /*
2625  * Otherwise, need to compare the new value with
2626  * boundaries of all the ranges. First check if it's
2627  * less than the absolute minimum, which is the first
2628  * value in the array.
2629  */
2630  cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2632  compar = FunctionCall2Coll(cmpFn, colloid, minval, value);
2633 
2634  /* smaller than the smallest value in this range */
2635  if (DatumGetBool(compar))
2636  break;
2637 
2638  cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2640  compar = FunctionCall2Coll(cmpFn, colloid, maxval, value);
2641 
2642  /* larger than the largest value in this range */
2643  if (DatumGetBool(compar))
2644  break;
2645 
2646  /*
2647  * We haven't managed to eliminate this range, so
2648  * consider it matching.
2649  */
2650  matches = true;
2651 
2652  break;
2653  }
2656  finfo = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2657  key->sk_strategy);
2658  /* last value from the array */
2659  matches = FunctionCall2Coll(finfo, colloid, maxval, value);
2660  break;
2661 
2662  default:
2663  /* shouldn't happen */
2664  elog(ERROR, "invalid strategy number %d", key->sk_strategy);
2665  matches = 0;
2666  break;
2667  }
2668 
2669  /* the range has to match all the scan keys */
2670  matching &= DatumGetBool(matches);
2671 
2672  /* once we find a non-matching key, we're done */
2673  if (!matching)
2674  break;
2675  }
2676 
2677  /*
2678  * have we found a range matching all scan keys? if yes, we're done
2679  */
2680  if (matching)
2682  }
2683 
2684  /*
2685  * And now inspect the values. We don't bother with doing a binary search
2686  * here, because we're dealing with serialized / fully compacted ranges,
2687  * so there should be only very few values.
2688  */
2689  for (i = 0; i < ranges->nvalues; i++)
2690  {
2691  Datum val = ranges->values[2 * ranges->nranges + i];
2692 
2693  /* assume the range is matching, and we'll try to prove otherwise */
2694  bool matching = true;
2695 
2696  for (keyno = 0; keyno < nkeys; keyno++)
2697  {
2698  Datum matches;
2699  ScanKey key = keys[keyno];
2700 
2701  /* we've already dealt with NULL keys at the beginning */
2702  if (key->sk_flags & SK_ISNULL)
2703  continue;
2704 
2705  attno = key->sk_attno;
2706  subtype = key->sk_subtype;
2707  value = key->sk_argument;
2708  switch (key->sk_strategy)
2709  {
2710  case BTLessStrategyNumber:
2712  case BTEqualStrategyNumber:
2715 
2716  finfo = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2717  key->sk_strategy);
2718  matches = FunctionCall2Coll(finfo, colloid, val, value);
2719  break;
2720 
2721  default:
2722  /* shouldn't happen */
2723  elog(ERROR, "invalid strategy number %d", key->sk_strategy);
2724  matches = 0;
2725  break;
2726  }
2727 
2728  /* the range has to match all the scan keys */
2729  matching &= DatumGetBool(matches);
2730 
2731  /* once we find a non-matching key, we're done */
2732  if (!matching)
2733  break;
2734  }
2735 
2736  /* have we found a range matching all scan keys? if yes, we're done */
2737  if (matching)
2739  }
2740 
2741  PG_RETURN_DATUM(BoolGetDatum(false));
2742 }
2743 
2744 /*
2745  * Given two BrinValues, update the first of them as a union of the summary
2746  * values contained in both. The second one is untouched.
2747  */
2748 Datum
2750 {
2751  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
2752  BrinValues *col_a = (BrinValues *) PG_GETARG_POINTER(1);
2753  BrinValues *col_b = (BrinValues *) PG_GETARG_POINTER(2);
2754 
2755  Oid colloid = PG_GET_COLLATION();
2756  SerializedRanges *serialized_a;
2757  SerializedRanges *serialized_b;
2758  Ranges *ranges_a;
2759  Ranges *ranges_b;
2760  AttrNumber attno;
2761  Form_pg_attribute attr;
2762  ExpandedRange *eranges;
2763  int neranges;
2764  FmgrInfo *cmpFn,
2765  *distanceFn;
2766  DistanceValue *distances;
2767  MemoryContext ctx;
2768  MemoryContext oldctx;
2769 
2770  Assert(col_a->bv_attno == col_b->bv_attno);
2771  Assert(!col_a->bv_allnulls && !col_b->bv_allnulls);
2772 
2773  attno = col_a->bv_attno;
2774  attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
2775 
2776  serialized_a = (SerializedRanges *) PG_DETOAST_DATUM(col_a->bv_values[0]);
2777  serialized_b = (SerializedRanges *) PG_DETOAST_DATUM(col_b->bv_values[0]);
2778 
2779  ranges_a = brin_range_deserialize(serialized_a->maxvalues, serialized_a);
2780  ranges_b = brin_range_deserialize(serialized_b->maxvalues, serialized_b);
2781 
2782  /* make sure neither of the ranges is NULL */
2783  Assert(ranges_a && ranges_b);
2784 
2785  neranges = (ranges_a->nranges + ranges_a->nvalues) +
2786  (ranges_b->nranges + ranges_b->nvalues);
2787 
2788  /*
2789  * The distanceFn calls (which may internally call e.g. numeric_le) may
2790  * allocate quite a bit of memory, and we must not leak it. Otherwise,
2791  * we'd have problems e.g. when building indexes. So we create a local
2792  * memory context and make sure we free the memory before leaving this
2793  * function (not after every call).
2794  */
2796  "minmax-multi context",
2798 
2799  oldctx = MemoryContextSwitchTo(ctx);
2800 
2801  /* allocate and fill */
2802  eranges = (ExpandedRange *) palloc0(neranges * sizeof(ExpandedRange));
2803 
2804  /* fill the expanded ranges with entries for the first range */
2805  fill_expanded_ranges(eranges, ranges_a->nranges + ranges_a->nvalues,
2806  ranges_a);
2807 
2808  /* and now add combine ranges for the second range */
2809  fill_expanded_ranges(&eranges[ranges_a->nranges + ranges_a->nvalues],
2810  ranges_b->nranges + ranges_b->nvalues,
2811  ranges_b);
2812 
2813  cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
2815 
2816  /* sort the expanded ranges */
2817  neranges = sort_expanded_ranges(cmpFn, colloid, eranges, neranges);
2818 
2819  /*
2820  * We've loaded two different lists of expanded ranges, so some of them
2821  * may be overlapping. So walk through them and merge them.
2822  */
2823  neranges = merge_overlapping_ranges(cmpFn, colloid, eranges, neranges);
2824 
2825  /* check that the combine ranges are correct (no overlaps, ordering) */
2826  AssertCheckExpandedRanges(bdesc, colloid, attno, attr, eranges, neranges);
2827 
2828  /*
2829  * If needed, reduce some of the ranges.
2830  *
2831  * XXX This may be fairly expensive, so maybe we should do it only when
2832  * it's actually needed (when we have too many ranges).
2833  */
2834 
2835  /* build array of gap distances and sort them in ascending order */
2836  distanceFn = minmax_multi_get_procinfo(bdesc, attno, PROCNUM_DISTANCE);
2837  distances = build_distances(distanceFn, colloid, eranges, neranges);
2838 
2839  /*
2840  * See how many values would be needed to store the current ranges, and if
2841  * needed combine as many of them to get below the threshold. The
2842  * collapsed ranges will be stored as a single value.
2843  *
2844  * XXX This does not apply the load factor, as we don't expect to add more
2845  * values to the range, so we prefer to keep as many ranges as possible.
2846  *
2847  * XXX Can the maxvalues be different in the two ranges? Perhaps we should
2848  * use maximum of those?
2849  */
2850  neranges = reduce_expanded_ranges(eranges, neranges, distances,
2851  ranges_a->maxvalues,
2852  cmpFn, colloid);
2853 
2854  /* update the first range summary */
2855  store_expanded_ranges(ranges_a, eranges, neranges);
2856 
2857  MemoryContextSwitchTo(oldctx);
2858  MemoryContextDelete(ctx);
2859 
2860  /* cleanup and update the serialized value */
2861  pfree(serialized_a);
2862  col_a->bv_values[0] = PointerGetDatum(brin_range_serialize(ranges_a));
2863 
2864  PG_RETURN_VOID();
2865 }
2866 
2867 /*
2868  * Cache and return minmax multi opclass support procedure
2869  *
2870  * Return the procedure corresponding to the given function support number
2871  * or null if it does not exist.
2872  */
2873 static FmgrInfo *
2875 {
2876  MinmaxMultiOpaque *opaque;
2877  uint16 basenum = procnum - PROCNUM_BASE;
2878 
2879  /*
2880  * We cache these in the opaque struct, to avoid repetitive syscache
2881  * lookups.
2882  */
2883  opaque = (MinmaxMultiOpaque *) bdesc->bd_info[attno - 1]->oi_opaque;
2884 
2885  /*
2886  * If we already searched for this proc and didn't find it, don't bother
2887  * searching again.
2888  */
2889  if (opaque->extra_proc_missing[basenum])
2890  return NULL;
2891 
2892  if (opaque->extra_procinfos[basenum].fn_oid == InvalidOid)
2893  {
2894  if (RegProcedureIsValid(index_getprocid(bdesc->bd_index, attno,
2895  procnum)))
2896  {
2897  fmgr_info_copy(&opaque->extra_procinfos[basenum],
2898  index_getprocinfo(bdesc->bd_index, attno, procnum),
2899  bdesc->bd_context);
2900  }
2901  else
2902  {
2903  opaque->extra_proc_missing[basenum] = true;
2904  return NULL;
2905  }
2906  }
2907 
2908  return &opaque->extra_procinfos[basenum];
2909 }
2910 
2911 /*
2912  * Cache and return the procedure for the given strategy.
2913  *
2914  * Note: this function mirrors minmax_multi_get_strategy_procinfo; see notes
2915  * there. If changes are made here, see that function too.
2916  */
2917 static FmgrInfo *
2919  uint16 strategynum)
2920 {
2921  MinmaxMultiOpaque *opaque;
2922 
2923  Assert(strategynum >= 1 &&
2924  strategynum <= BTMaxStrategyNumber);
2925 
2926  opaque = (MinmaxMultiOpaque *) bdesc->bd_info[attno - 1]->oi_opaque;
2927 
2928  /*
2929  * We cache the procedures for the previous subtype in the opaque struct,
2930  * to avoid repetitive syscache lookups. If the subtype changed,
2931  * invalidate all the cached entries.
2932  */
2933  if (opaque->cached_subtype != subtype)
2934  {
2935  uint16 i;
2936 
2937  for (i = 1; i <= BTMaxStrategyNumber; i++)
2938  opaque->strategy_procinfos[i - 1].fn_oid = InvalidOid;
2939  opaque->cached_subtype = subtype;
2940  }
2941 
2942  if (opaque->strategy_procinfos[strategynum - 1].fn_oid == InvalidOid)
2943  {
2944  Form_pg_attribute attr;
2945  HeapTuple tuple;
2946  Oid opfamily,
2947  oprid;
2948  bool isNull;
2949 
2950  opfamily = bdesc->bd_index->rd_opfamily[attno - 1];
2951  attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
2952  tuple = SearchSysCache4(AMOPSTRATEGY, ObjectIdGetDatum(opfamily),
2953  ObjectIdGetDatum(attr->atttypid),
2954  ObjectIdGetDatum(subtype),
2955  Int16GetDatum(strategynum));
2956  if (!HeapTupleIsValid(tuple))
2957  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2958  strategynum, attr->atttypid, subtype, opfamily);
2959 
2961  Anum_pg_amop_amopopr, &isNull));
2962  ReleaseSysCache(tuple);
2963  Assert(!isNull && RegProcedureIsValid(oprid));
2964 
2966  &opaque->strategy_procinfos[strategynum - 1],
2967  bdesc->bd_context);
2968  }
2969 
2970  return &opaque->strategy_procinfos[strategynum - 1];
2971 }
2972 
2973 Datum
2975 {
2976  local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
2977 
2978  init_local_reloptions(relopts, sizeof(MinMaxMultiOptions));
2979 
2980  add_local_int_reloption(relopts, "values_per_range", "desc",
2982  offsetof(MinMaxMultiOptions, valuesPerRange));
2983 
2984  PG_RETURN_VOID();
2985 }
2986 
2987 /*
2988  * brin_minmax_multi_summary_in
2989  * - input routine for type brin_minmax_multi_summary.
2990  *
2991  * brin_minmax_multi_summary is only used internally to represent summaries
2992  * in BRIN minmax-multi indexes, so it has no operations of its own, and we
2993  * disallow input too.
2994  */
2995 Datum
2997 {
2998  /*
2999  * brin_minmax_multi_summary stores the data in binary form and parsing
3000  * text input is not needed, so disallow this.
3001  */
3002  ereport(ERROR,
3003  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3004  errmsg("cannot accept a value of type %s", "brin_minmax_multi_summary")));
3005 
3006  PG_RETURN_VOID(); /* keep compiler quiet */
3007 }
3008 
3009 
3010 /*
3011  * brin_minmax_multi_summary_out
3012  * - output routine for type brin_minmax_multi_summary.
3013  *
3014  * BRIN minmax-multi summaries are serialized into a bytea value, but we
3015  * want to output something nicer humans can understand.
3016  */
3017 Datum
3019 {
3020  int i;
3021  int idx;
3022  SerializedRanges *ranges;
3023  Ranges *ranges_deserialized;
3025  bool isvarlena;
3026  Oid outfunc;
3027  FmgrInfo fmgrinfo;
3028  ArrayBuildState *astate_values = NULL;
3029 
3030  initStringInfo(&str);
3031  appendStringInfoChar(&str, '{');
3032 
3033  /*
3034  * Detoast to get value with full 4B header (can't be stored in a toast
3035  * table, but can use 1B header).
3036  */
3038 
3039  /* lookup output func for the type */
3040  getTypeOutputInfo(ranges->typid, &outfunc, &isvarlena);
3041  fmgr_info(outfunc, &fmgrinfo);
3042 
3043  /* deserialize the range info easy-to-process pieces */
3044  ranges_deserialized = brin_range_deserialize(ranges->maxvalues, ranges);
3045 
3046  appendStringInfo(&str, "nranges: %d nvalues: %d maxvalues: %d",
3047  ranges_deserialized->nranges,
3048  ranges_deserialized->nvalues,
3049  ranges_deserialized->maxvalues);
3050 
3051  /* serialize ranges */
3052  idx = 0;
3053  for (i = 0; i < ranges_deserialized->nranges; i++)
3054  {
3055  char *a,
3056  *b;
3057  text *c;
3059 
3060  initStringInfo(&str);
3061 
3062  a = OutputFunctionCall(&fmgrinfo, ranges_deserialized->values[idx++]);
3063  b = OutputFunctionCall(&fmgrinfo, ranges_deserialized->values[idx++]);
3064 
3065  appendStringInfo(&str, "%s ... %s", a, b);
3066 
3067  c = cstring_to_text(str.data);
3068 
3069  astate_values = accumArrayResult(astate_values,
3070  PointerGetDatum(c),
3071  false,
3072  TEXTOID,
3074  }
3075 
3076  if (ranges_deserialized->nranges > 0)
3077  {
3078  Oid typoutput;
3079  bool typIsVarlena;
3080  Datum val;
3081  char *extval;
3082 
3083  getTypeOutputInfo(ANYARRAYOID, &typoutput, &typIsVarlena);
3084 
3086 
3087  extval = OidOutputFunctionCall(typoutput, val);
3088 
3089  appendStringInfo(&str, " ranges: %s", extval);
3090  }
3091 
3092  /* serialize individual values */
3093  astate_values = NULL;
3094 
3095  for (i = 0; i < ranges_deserialized->nvalues; i++)
3096  {
3097  Datum a;
3098  text *b;
3100 
3101  initStringInfo(&str);
3102 
3103  a = FunctionCall1(&fmgrinfo, ranges_deserialized->values[idx++]);
3104 
3106 
3107  b = cstring_to_text(str.data);
3108 
3109  astate_values = accumArrayResult(astate_values,
3110  PointerGetDatum(b),
3111  false,
3112  TEXTOID,
3114  }
3115 
3116  if (ranges_deserialized->nvalues > 0)
3117  {
3118  Oid typoutput;
3119  bool typIsVarlena;
3120  Datum val;
3121  char *extval;
3122 
3123  getTypeOutputInfo(ANYARRAYOID, &typoutput, &typIsVarlena);
3124 
3126 
3127  extval = OidOutputFunctionCall(typoutput, val);
3128 
3129  appendStringInfo(&str, " values: %s", extval);
3130  }
3131 
3132 
3133  appendStringInfoChar(&str, '}');
3134 
3135  PG_RETURN_CSTRING(str.data);
3136 }
3137 
3138 /*
3139  * brin_minmax_multi_summary_recv
3140  * - binary input routine for type brin_minmax_multi_summary.
3141  */
3142 Datum
3144 {
3145  ereport(ERROR,
3146  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3147  errmsg("cannot accept a value of type %s", "brin_minmax_multi_summary")));
3148 
3149  PG_RETURN_VOID(); /* keep compiler quiet */
3150 }
3151 
3152 /*
3153  * brin_minmax_multi_summary_send
3154  * - binary output routine for type brin_minmax_multi_summary.
3155  *
3156  * BRIN minmax-multi summaries are serialized in a bytea value (although
3157  * the type is named differently), so let's just send that.
3158  */
3159 Datum
3161 {
3162  return byteasend(fcinfo);
3163 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5123
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5187
int16 AttrNumber
Definition: attnum.h:21
const char *const days[]
Definition: datetime.c:83
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:2842
Datum numeric_le(PG_FUNCTION_ARGS)
Definition: numeric.c:2407
Datum numeric_float8(PG_FUNCTION_ARGS)
Definition: numeric.c:4426
uint32 BlockNumber
Definition: block.h:31
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define BrinGetPagesPerRange(relation)
Definition: brin.h:39
#define SizeofBrinOpcInfo(ncols)
Definition: brin_internal.h:41
Datum brin_minmax_multi_distance_float8(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_options(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_union(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_distance_float4(PG_FUNCTION_ARGS)
#define MinMaxMultiGetValuesPerRange(opts)
static void AssertCheckExpandedRanges(BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, ExpandedRange *ranges, int nranges)
struct DistanceValue DistanceValue
Datum brin_minmax_multi_distance_int8(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_summary_recv(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_summary_out(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_add_value(PG_FUNCTION_ARGS)
struct SerializedRanges SerializedRanges
Datum brin_minmax_multi_distance_uuid(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_distance_inet(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_consistent(PG_FUNCTION_ARGS)
static void AssertCheckRanges(Ranges *ranges, FmgrInfo *cmpFn, Oid colloid)
static int compare_expanded_ranges(const void *a, const void *b, void *arg)
Datum brin_minmax_multi_distance_time(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_distance_timestamp(PG_FUNCTION_ARGS)
static bool range_add_value(BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *ranges, Datum newval)
static Ranges * minmax_multi_init(int maxvalues)
static bool ensure_free_space_in_buffer(BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *range)
static int reduce_expanded_ranges(ExpandedRange *eranges, int neranges, DistanceValue *distances, int max_values, FmgrInfo *cmp, Oid colloid)
struct MinMaxMultiOptions MinMaxMultiOptions
struct MinmaxMultiOpaque MinmaxMultiOpaque
static int compare_values(const void *a, const void *b, void *arg)
static void compactify_ranges(BrinDesc *bdesc, Ranges *ranges, int max_values)
#define MINMAX_BUFFER_MAX
Datum brin_minmax_multi_distance_numeric(PG_FUNCTION_ARGS)
#define MINMAX_BUFFER_LOAD_FACTOR
Datum brin_minmax_multi_summary_send(PG_FUNCTION_ARGS)
static Ranges * brin_range_deserialize(int maxvalues, SerializedRanges *range)
static ExpandedRange * build_expanded_ranges(FmgrInfo *cmp, Oid colloid, Ranges *ranges, int *nranges)
Datum brin_minmax_multi_distance_pg_lsn(PG_FUNCTION_ARGS)
static int brin_minmax_multi_get_values(BrinDesc *bdesc, MinMaxMultiOptions *opts)
struct compare_context compare_context
static FmgrInfo * minmax_multi_get_procinfo(BrinDesc *bdesc, uint16 attno, uint16 procnum)
struct ExpandedRange ExpandedRange
#define SAMESIGN(a, b)
Datum brin_minmax_multi_distance_macaddr8(PG_FUNCTION_ARGS)
#define MINMAX_MAX_PROCNUMS
static int sort_expanded_ranges(FmgrInfo *cmp, Oid colloid, ExpandedRange *eranges, int neranges)
#define MINMAX_BUFFER_FACTOR
Datum brin_minmax_multi_distance_date(PG_FUNCTION_ARGS)
struct Ranges Ranges
static void range_deduplicate_values(Ranges *range)
static void fill_expanded_ranges(ExpandedRange *eranges, int neranges, Ranges *ranges)
static int merge_overlapping_ranges(FmgrInfo *cmp, Oid colloid, ExpandedRange *eranges, int neranges)
Datum brin_minmax_multi_summary_in(PG_FUNCTION_ARGS)
static void store_expanded_ranges(Ranges *ranges, ExpandedRange *eranges, int neranges)
#define PROCNUM_BASE
static bool has_matching_range(BrinDesc *bdesc, Oid colloid, Ranges *ranges, Datum newval, AttrNumber attno, Oid typid)
Datum brin_minmax_multi_distance_int2(PG_FUNCTION_ARGS)
static bool range_contains_value(BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *ranges, Datum newval, bool full)
Datum brin_minmax_multi_opcinfo(PG_FUNCTION_ARGS)
static int compare_distances(const void *a, const void *b)
Datum brin_minmax_multi_distance_interval(PG_FUNCTION_ARGS)
Datum brin_minmax_multi_distance_timetz(PG_FUNCTION_ARGS)
static void brin_minmax_multi_serialize(BrinDesc *bdesc, Datum src, Datum *dst)
static SerializedRanges * brin_range_serialize(Ranges *range)
Datum brin_minmax_multi_distance_tid(PG_FUNCTION_ARGS)
#define PROCNUM_DISTANCE
static DistanceValue * build_distances(FmgrInfo *distanceFn, Oid colloid, ExpandedRange *eranges, int neranges)
Datum brin_minmax_multi_distance_macaddr(PG_FUNCTION_ARGS)
static FmgrInfo * minmax_multi_get_strategy_procinfo(BrinDesc *bdesc, uint16 attno, Oid subtype, uint16 strategynum)
#define MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE
Datum brin_minmax_multi_distance_int4(PG_FUNCTION_ARGS)
#define MINMAX_BUFFER_MIN
unsigned short uint16
Definition: c.h:440
#define RegProcedureIsValid(p)
Definition: c.h:712
#define Min(x, y)
Definition: c.h:986
signed short int16
Definition: c.h:428
#define MAXALIGN(LEN)
Definition: c.h:757
#define offsetof(type, field)
Definition: c.h:727
signed int int32
Definition: c.h:429
#define Max(x, y)
Definition: c.h:980
double float8
Definition: c.h:565
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:350
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:155
size_t Size
Definition: c.h:540
int64 Timestamp
Definition: timestamp.h:38
#define USECS_PER_DAY
Definition: timestamp.h:130
#define USECS_PER_SEC
Definition: timestamp.h:133
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:161
#define DATE_NOT_FINITE(j)
Definition: date.h:43
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
int32 DateADT
Definition: date.h:23
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
static float8 get_float8_infinity(void)
Definition: float.h:93
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1134
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1639
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1559
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:594
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:633
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_GET_OPCLASS_OPTIONS()
Definition: fmgr.h:342
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:649
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define newval
static const FormData_pg_attribute a1
Definition: heap.c:141
static const FormData_pg_attribute a2
Definition: heap.c:155
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define MaxHeapTuplesPerPage
Definition: htup_details.h:568
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:803
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:769
static struct @151 value
long val
Definition: informix.c:664
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int i
Definition: isn.c:73
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
#define ItemPointerGetBlockNumberNoCheck(pointer)
Definition: itemptr.h:89
ItemPointerData * ItemPointer
Definition: itemptr.h:49
#define ItemPointerGetOffsetNumberNoCheck(pointer)
Definition: itemptr.h:108
Assert(fmt[strlen(fmt) - 1] !='\n')
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2864
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1266
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2154
bool get_typbyval(Oid typid)
Definition: lsyscache.c:2179
void pfree(void *pointer)
Definition: mcxt.c:1175
void * palloc0(Size size)
Definition: mcxt.c:1099
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
void * palloc(Size size)
Definition: mcxt.c:1068
#define AllocSetContextCreate
Definition: memutils.h:173
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Oid oprid(Operator op)
Definition: parse_oper.c:250
static AmcheckOptions opts
Definition: pg_amcheck.c:110
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
void * arg
const void size_t len
const void * data
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
void * bsearch_arg(const void *key, const void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *, void *), void *arg)
Definition: bsearch_arg.c:55
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
void pg_qsort(void *base, size_t nel, size_t elsize, int(*cmp)(const void *, const void *))
#define DatumGetObjectId(X)
Definition: postgres.h:544
uintptr_t Datum
Definition: postgres.h:411
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348
#define DatumGetBool(X)
Definition: postgres.h:437
#define DatumGetPointer(X)
Definition: postgres.h:593
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define DatumGetFloat8(X)
Definition: postgres.h:758
#define BoolGetDatum(X)
Definition: postgres.h:446
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define Int16GetDatum(X)
Definition: postgres.h:495
#define DatumGetCString(X)
Definition: postgres.h:610
#define PointerGetDatum(X)
Definition: postgres.h:600
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * c
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747
void init_local_reloptions(local_relopts *opts, Size relopt_struct_size)
Definition: reloptions.c:736
void add_local_int_reloption(local_relopts *relopts, const char *name, const char *desc, int default_val, int min_val, int max_val, int offset)
Definition: reloptions.c:920
#define SK_ISNULL
Definition: skey.h:115
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTMaxStrategyNumber
Definition: stratnum.h:35
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
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
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
Relation bd_index
Definition: brin_internal.h:50
MemoryContext bd_context
Definition: brin_internal.h:47
TypeCacheEntry * oi_typcache[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:37
uint16 oi_nstored
Definition: brin_internal.h:28
bool oi_regular_nulls
Definition: brin_internal.h:31
void * oi_opaque
Definition: brin_internal.h:34
MemoryContext bv_context
Definition: brin_tuple.h:36
Datum bv_mem_value
Definition: brin_tuple.h:35
brin_serialize_callback_type bv_serialize
Definition: brin_tuple.h:37
Datum * bv_values
Definition: brin_tuple.h:34
AttrNumber bv_attno
Definition: brin_tuple.h:31
bool bv_allnulls
Definition: brin_tuple.h:33
Definition: fmgr.h:57
Oid fn_oid
Definition: fmgr.h:59
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
TimeOffset time
Definition: timestamp.h:49
bool extra_proc_missing[MINMAX_MAX_PROCNUMS]
FmgrInfo extra_procinfos[MINMAX_MAX_PROCNUMS]
FmgrInfo strategy_procinfos[BTMaxStrategyNumber]
int target_maxvalues
AttrNumber attno
FmgrInfo * cmp
Datum values[FLEXIBLE_ARRAY_MEMBER]
Oid * rd_opfamily
Definition: rel.h:203
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: date.h:28
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30
int nranges
Definition: regguts.h:278
Definition: type.h:90
Definition: inet.h:53
Definition: inet.h:108
Definition: inet.h:95
Definition: uuid.h:21
unsigned char data[UUID_LEN]
Definition: uuid.h:22
Definition: c.h:622
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
HeapTuple SearchSysCache4(int cacheId, Datum key1, Datum key2, Datum key3, Datum key4)
Definition: syscache.c:1206
@ AMOPSTRATEGY
Definition: syscache.h:38
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define store_att_byval(T, newdatum, attlen)
Definition: tupmacs.h:226
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:75
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:339
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
#define PG_GETARG_MACADDR8_P(n)
Definition: inet.h:139
#define ip_addr(inetptr)
Definition: inet.h:77
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define ip_bits(inetptr)
Definition: inet.h:74
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
Datum uuid_le(PG_FUNCTION_ARGS)
Definition: uuid.c:178
#define UUID_LEN
Definition: uuid.h:18
#define DatumGetUUIDP(X)
Definition: uuid.h:28
text * cstring_to_text(const char *s)
Definition: varlena.c:188
Datum byteasend(PG_FUNCTION_ARGS)
Definition: varlena.c:492
uint64 XLogRecPtr
Definition: xlogdefs.h:21