LCOV - code coverage report
Current view: top level - src/backend/access/brin - brin_minmax_multi.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 650 756 86.0 %
Date: 2024-04-19 13:11:37 Functions: 46 51 90.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14