LCOV - differential code coverage report
Current view: top level - src/backend/access/brin - brin_minmax_multi.c (source / functions) Coverage Total Hit UNC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 76.6 % 894 685 8 43 158 1 57 5 622 47 58 3 5
Current Date: 2023-04-08 17:13:01 Functions: 86.8 % 53 46 5 2 6 2 38 5 6
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 [..60] days: 100.0 % 4 4 2 2
Legend: Lines: hit not hit (60,120] days: 66.7 % 3 2 1 2
(180,240] days: 77.5 % 40 31 8 1 18 3 10 1 13
(240..) days: 76.5 % 847 648 42 157 1 39 608 46 45
Function coverage date bins:
(240..) days: 71.9 % 64 46 5 2 6 2 38 5 6

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

Generated by: LCOV version v1.16-55-g56c0a2a