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