LCOV - code coverage report
Current view: top level - contrib/intarray - _int_gist.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 251 274 91.6 %
Date: 2024-03-29 08:11:33 Functions: 17 17 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/intarray/_int_gist.c
       3             :  */
       4             : #include "postgres.h"
       5             : 
       6             : #include <limits.h>
       7             : #include <math.h>
       8             : 
       9             : #include "_int.h"
      10             : #include "access/gist.h"
      11             : #include "access/reloptions.h"
      12             : #include "access/stratnum.h"
      13             : 
      14             : #define GETENTRY(vec,pos) ((ArrayType *) DatumGetPointer((vec)->vector[(pos)].key))
      15             : 
      16             : /*
      17             :  * Control the maximum sparseness of compressed keys.
      18             :  *
      19             :  * The upper safe bound for this limit is half the maximum allocatable array
      20             :  * size. A lower bound would give more guarantees that pathological data
      21             :  * wouldn't eat excessive CPU and memory, but at the expense of breaking
      22             :  * possibly working (after a fashion) indexes.
      23             :  */
      24             : #define MAXNUMELTS (Min((MaxAllocSize / sizeof(Datum)),((MaxAllocSize - ARR_OVERHEAD_NONULLS(1)) / sizeof(int)))/2)
      25             : /* or: #define MAXNUMELTS 1000000 */
      26             : 
      27             : /*
      28             : ** GiST support methods
      29             : */
      30           4 : PG_FUNCTION_INFO_V1(g_int_consistent);
      31           4 : PG_FUNCTION_INFO_V1(g_int_compress);
      32           4 : PG_FUNCTION_INFO_V1(g_int_decompress);
      33           4 : PG_FUNCTION_INFO_V1(g_int_penalty);
      34           4 : PG_FUNCTION_INFO_V1(g_int_picksplit);
      35           4 : PG_FUNCTION_INFO_V1(g_int_union);
      36           4 : PG_FUNCTION_INFO_V1(g_int_same);
      37           4 : PG_FUNCTION_INFO_V1(g_int_options);
      38             : 
      39             : 
      40             : /*
      41             : ** The GiST Consistent method for _intments
      42             : ** Should return false if for all data items x below entry,
      43             : ** the predicate x op query == false, where op is the oper
      44             : ** corresponding to strategy in the pg_amop table.
      45             : */
      46             : Datum
      47      254604 : g_int_consistent(PG_FUNCTION_ARGS)
      48             : {
      49      254604 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      50      254604 :     ArrayType  *query = PG_GETARG_ARRAYTYPE_P_COPY(1);
      51      254604 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
      52             : 
      53             :     /* Oid      subtype = PG_GETARG_OID(3); */
      54      254604 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
      55      254604 :     bool        retval = false; /* silence compiler warning */
      56             : 
      57             :     /* this is exact except for RTSameStrategyNumber */
      58      254604 :     *recheck = (strategy == RTSameStrategyNumber);
      59             : 
      60      254604 :     if (strategy == BooleanSearchStrategy)
      61             :     {
      62      161142 :         retval = execconsistent((QUERYTYPE *) query,
      63      161142 :                                 (ArrayType *) DatumGetPointer(entry->key),
      64      161142 :                                 GIST_LEAF(entry));
      65             : 
      66      161142 :         pfree(query);
      67      161142 :         PG_RETURN_BOOL(retval);
      68             :     }
      69             : 
      70             :     /* sort query for fast search, key is already sorted */
      71       93462 :     CHECKARRVALID(query);
      72       93462 :     PREPAREARR(query);
      73             : 
      74       93462 :     switch (strategy)
      75             :     {
      76       33580 :         case RTOverlapStrategyNumber:
      77       33580 :             retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
      78             :                                        query);
      79       33580 :             break;
      80        9732 :         case RTSameStrategyNumber:
      81        9732 :             if (GIST_LEAF(entry))
      82        9076 :                 DirectFunctionCall3(g_int_same,
      83             :                                     entry->key,
      84             :                                     PointerGetDatum(query),
      85             :                                     PointerGetDatum(&retval));
      86             :             else
      87         656 :                 retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
      88             :                                             query);
      89        9732 :             break;
      90       50150 :         case RTContainsStrategyNumber:
      91             :         case RTOldContainsStrategyNumber:
      92       50150 :             retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
      93             :                                         query);
      94       50150 :             break;
      95           0 :         case RTContainedByStrategyNumber:
      96             :         case RTOldContainedByStrategyNumber:
      97             : 
      98             :             /*
      99             :              * This code is unreachable as of intarray 1.4, because the <@
     100             :              * operator has been removed from the opclass.  We keep it for now
     101             :              * to support older versions of the SQL definitions.
     102             :              */
     103           0 :             if (GIST_LEAF(entry))
     104           0 :                 retval = inner_int_contains(query,
     105           0 :                                             (ArrayType *) DatumGetPointer(entry->key));
     106             :             else
     107             :             {
     108             :                 /*
     109             :                  * Unfortunately, because empty arrays could be anywhere in
     110             :                  * the index, we must search the whole tree.
     111             :                  */
     112           0 :                 retval = true;
     113             :             }
     114           0 :             break;
     115           0 :         default:
     116           0 :             retval = false;
     117             :     }
     118       93462 :     pfree(query);
     119       93462 :     PG_RETURN_BOOL(retval);
     120             : }
     121             : 
     122             : Datum
     123      114978 : g_int_union(PG_FUNCTION_ARGS)
     124             : {
     125      114978 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     126      114978 :     int        *size = (int *) PG_GETARG_POINTER(1);
     127             :     int32       i,
     128             :                *ptr;
     129             :     ArrayType  *res;
     130      114978 :     int         totlen = 0;
     131             : 
     132      346924 :     for (i = 0; i < entryvec->n; i++)
     133             :     {
     134      231946 :         ArrayType  *ent = GETENTRY(entryvec, i);
     135             : 
     136      231946 :         CHECKARRVALID(ent);
     137      231946 :         totlen += ARRNELEMS(ent);
     138             :     }
     139             : 
     140      114978 :     res = new_intArrayType(totlen);
     141      114978 :     ptr = ARRPTR(res);
     142             : 
     143      346924 :     for (i = 0; i < entryvec->n; i++)
     144             :     {
     145      231946 :         ArrayType  *ent = GETENTRY(entryvec, i);
     146             :         int         nel;
     147             : 
     148      231946 :         nel = ARRNELEMS(ent);
     149      231946 :         memcpy(ptr, ARRPTR(ent), nel * sizeof(int32));
     150      231946 :         ptr += nel;
     151             :     }
     152             : 
     153      114978 :     QSORT(res, 1);
     154      114978 :     res = _int_unique(res);
     155      114978 :     *size = VARSIZE(res);
     156      114978 :     PG_RETURN_POINTER(res);
     157             : }
     158             : 
     159             : /*
     160             : ** GiST Compress and Decompress methods
     161             : */
     162             : Datum
     163       59210 : g_int_compress(PG_FUNCTION_ARGS)
     164             : {
     165       59210 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     166             :     GISTENTRY  *retval;
     167             :     ArrayType  *r;
     168       59210 :     int         num_ranges = G_INT_GET_NUMRANGES();
     169             :     int         len,
     170             :                 lenr;
     171             :     int        *dr;
     172             :     int         i,
     173             :                 j,
     174             :                 cand;
     175             :     int64       min;
     176             : 
     177       59210 :     if (entry->leafkey)
     178             :     {
     179       40544 :         r = DatumGetArrayTypePCopy(entry->key);
     180       40544 :         CHECKARRVALID(r);
     181       40544 :         PREPAREARR(r);
     182             : 
     183       40544 :         if (ARRNELEMS(r) >= 2 * num_ranges)
     184           2 :             ereport(ERROR,
     185             :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     186             :                      errmsg("input array is too big (%d maximum allowed, %d current), use gist__intbig_ops opclass instead",
     187             :                             2 * num_ranges - 1, ARRNELEMS(r))));
     188             : 
     189       40542 :         retval = palloc(sizeof(GISTENTRY));
     190       40542 :         gistentryinit(*retval, PointerGetDatum(r),
     191             :                       entry->rel, entry->page, entry->offset, false);
     192             : 
     193       40542 :         PG_RETURN_POINTER(retval);
     194             :     }
     195             : 
     196             :     /*
     197             :      * leaf entries never compress one more time, only when entry->leafkey
     198             :      * ==true, so now we work only with internal keys
     199             :      */
     200             : 
     201       18666 :     r = DatumGetArrayTypeP(entry->key);
     202       18666 :     CHECKARRVALID(r);
     203       18666 :     if (ARRISEMPTY(r))
     204             :     {
     205           0 :         if (r != (ArrayType *) DatumGetPointer(entry->key))
     206           0 :             pfree(r);
     207           0 :         PG_RETURN_POINTER(entry);
     208             :     }
     209             : 
     210       18666 :     if ((len = ARRNELEMS(r)) >= 2 * num_ranges)
     211             :     {                           /* compress */
     212        2742 :         if (r == (ArrayType *) DatumGetPointer(entry->key))
     213        2742 :             r = DatumGetArrayTypePCopy(entry->key);
     214        2742 :         r = resize_intArrayType(r, 2 * (len));
     215             : 
     216        2742 :         dr = ARRPTR(r);
     217             : 
     218             :         /*
     219             :          * "len" at this point is the number of ranges we will construct.
     220             :          * "lenr" is the number of ranges we must eventually remove by
     221             :          * merging, we must be careful to remove no more than this number.
     222             :          */
     223        2742 :         lenr = len - num_ranges;
     224             : 
     225             :         /*
     226             :          * Initially assume we can merge consecutive ints into a range. but we
     227             :          * must count every value removed and stop when lenr runs out
     228             :          */
     229      128374 :         for (j = i = len - 1; i > 0 && lenr > 0; i--, j--)
     230             :         {
     231      125632 :             int         r_end = dr[i];
     232      125632 :             int         r_start = r_end;
     233             : 
     234     1350496 :             while (i > 0 && lenr > 0 && dr[i - 1] == r_start - 1)
     235     1224864 :                 --r_start, --i, --lenr;
     236      125632 :             dr[2 * j] = r_start;
     237      125632 :             dr[2 * j + 1] = r_end;
     238             :         }
     239             :         /* just copy the rest, if any, as trivial ranges */
     240      532526 :         for (; i >= 0; i--, j--)
     241      529784 :             dr[2 * j] = dr[2 * j + 1] = dr[i];
     242             : 
     243        2742 :         if (++j)
     244             :         {
     245             :             /*
     246             :              * shunt everything down to start at the right place
     247             :              */
     248        2742 :             memmove(&dr[0], &dr[2 * j], 2 * (len - j) * sizeof(int32));
     249             :         }
     250             : 
     251             :         /*
     252             :          * make "len" be number of array elements, not ranges
     253             :          */
     254        2742 :         len = 2 * (len - j);
     255        2742 :         cand = 1;
     256        2742 :         while (len > num_ranges * 2)
     257             :         {
     258           0 :             min = PG_INT64_MAX;
     259           0 :             for (i = 2; i < len; i += 2)
     260           0 :                 if (min > ((int64) dr[i] - (int64) dr[i - 1]))
     261             :                 {
     262           0 :                     min = ((int64) dr[i] - (int64) dr[i - 1]);
     263           0 :                     cand = i;
     264             :                 }
     265           0 :             memmove(&dr[cand - 1], &dr[cand + 1], (len - cand - 1) * sizeof(int32));
     266           0 :             len -= 2;
     267             :         }
     268             : 
     269             :         /*
     270             :          * check sparseness of result
     271             :          */
     272        2742 :         lenr = internal_size(dr, len);
     273        2742 :         if (lenr < 0 || lenr > MAXNUMELTS)
     274           0 :             ereport(ERROR,
     275             :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     276             :                      errmsg("data is too sparse, recreate index using gist__intbig_ops opclass instead")));
     277             : 
     278        2742 :         r = resize_intArrayType(r, len);
     279        2742 :         retval = palloc(sizeof(GISTENTRY));
     280        2742 :         gistentryinit(*retval, PointerGetDatum(r),
     281             :                       entry->rel, entry->page, entry->offset, false);
     282        2742 :         PG_RETURN_POINTER(retval);
     283             :     }
     284             :     else
     285       15924 :         PG_RETURN_POINTER(entry);
     286             : }
     287             : 
     288             : Datum
     289     1301242 : g_int_decompress(PG_FUNCTION_ARGS)
     290             : {
     291     1301242 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     292             :     GISTENTRY  *retval;
     293             :     ArrayType  *r;
     294     1301242 :     int         num_ranges = G_INT_GET_NUMRANGES();
     295             :     int        *dr,
     296             :                 lenr;
     297             :     ArrayType  *in;
     298             :     int         lenin;
     299             :     int        *din;
     300             :     int         i;
     301             : 
     302     1301242 :     in = DatumGetArrayTypeP(entry->key);
     303             : 
     304     1301242 :     CHECKARRVALID(in);
     305     1301242 :     if (ARRISEMPTY(in))
     306             :     {
     307         736 :         if (in != (ArrayType *) DatumGetPointer(entry->key))
     308             :         {
     309         736 :             retval = palloc(sizeof(GISTENTRY));
     310         736 :             gistentryinit(*retval, PointerGetDatum(in),
     311             :                           entry->rel, entry->page, entry->offset, false);
     312         736 :             PG_RETURN_POINTER(retval);
     313             :         }
     314             : 
     315           0 :         PG_RETURN_POINTER(entry);
     316             :     }
     317             : 
     318     1300506 :     lenin = ARRNELEMS(in);
     319             : 
     320     1300506 :     if (lenin < 2 * num_ranges)
     321             :     {                           /* not compressed value */
     322     1244900 :         if (in != (ArrayType *) DatumGetPointer(entry->key))
     323             :         {
     324      537058 :             retval = palloc(sizeof(GISTENTRY));
     325      537058 :             gistentryinit(*retval, PointerGetDatum(in),
     326             :                           entry->rel, entry->page, entry->offset, false);
     327             : 
     328      537058 :             PG_RETURN_POINTER(retval);
     329             :         }
     330      707842 :         PG_RETURN_POINTER(entry);
     331             :     }
     332             : 
     333       55606 :     din = ARRPTR(in);
     334       55606 :     lenr = internal_size(din, lenin);
     335       55606 :     if (lenr < 0 || lenr > MAXNUMELTS)
     336           0 :         ereport(ERROR,
     337             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     338             :                  errmsg("compressed array is too big, recreate index using gist__intbig_ops opclass instead")));
     339             : 
     340       55606 :     r = new_intArrayType(lenr);
     341       55606 :     dr = ARRPTR(r);
     342             : 
     343    13114062 :     for (i = 0; i < lenin; i += 2)
     344             :     {
     345             :         /* use int64 for j in case din[i + 1] is INT_MAX */
     346    52187832 :         for (int64 j = din[i]; j <= din[i + 1]; j++)
     347    39129376 :             if ((!i) || *(dr - 1) != j)
     348    39129376 :                 *dr++ = (int) j;
     349             :     }
     350             : 
     351       55606 :     if (in != (ArrayType *) DatumGetPointer(entry->key))
     352       55606 :         pfree(in);
     353       55606 :     retval = palloc(sizeof(GISTENTRY));
     354       55606 :     gistentryinit(*retval, PointerGetDatum(r),
     355             :                   entry->rel, entry->page, entry->offset, false);
     356             : 
     357       55606 :     PG_RETURN_POINTER(retval);
     358             : }
     359             : 
     360             : /*
     361             : ** The GiST Penalty method for _intments
     362             : */
     363             : Datum
     364      626922 : g_int_penalty(PG_FUNCTION_ARGS)
     365             : {
     366      626922 :     GISTENTRY  *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
     367      626922 :     GISTENTRY  *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
     368      626922 :     float      *result = (float *) PG_GETARG_POINTER(2);
     369             :     ArrayType  *ud;
     370             :     float       tmp1,
     371             :                 tmp2;
     372             : 
     373      626922 :     ud = inner_int_union((ArrayType *) DatumGetPointer(origentry->key),
     374      626922 :                          (ArrayType *) DatumGetPointer(newentry->key));
     375      626922 :     rt__int_size(ud, &tmp1);
     376      626922 :     rt__int_size((ArrayType *) DatumGetPointer(origentry->key), &tmp2);
     377      626922 :     *result = tmp1 - tmp2;
     378      626922 :     pfree(ud);
     379             : 
     380      626922 :     PG_RETURN_POINTER(result);
     381             : }
     382             : 
     383             : 
     384             : 
     385             : Datum
     386      123992 : g_int_same(PG_FUNCTION_ARGS)
     387             : {
     388      123992 :     ArrayType  *a = PG_GETARG_ARRAYTYPE_P(0);
     389      123992 :     ArrayType  *b = PG_GETARG_ARRAYTYPE_P(1);
     390      123992 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     391      123992 :     int32       n = ARRNELEMS(a);
     392             :     int32      *da,
     393             :                *db;
     394             : 
     395      123992 :     CHECKARRVALID(a);
     396      123992 :     CHECKARRVALID(b);
     397             : 
     398      123992 :     if (n != ARRNELEMS(b))
     399             :     {
     400       23890 :         *result = false;
     401       23890 :         PG_RETURN_POINTER(result);
     402             :     }
     403      100102 :     *result = true;
     404      100102 :     da = ARRPTR(a);
     405      100102 :     db = ARRPTR(b);
     406    29981884 :     while (n--)
     407             :     {
     408    29883128 :         if (*da++ != *db++)
     409             :         {
     410        1346 :             *result = false;
     411        1346 :             break;
     412             :         }
     413             :     }
     414             : 
     415      100102 :     PG_RETURN_POINTER(result);
     416             : }
     417             : 
     418             : /*****************************************************************
     419             : ** Common GiST Method
     420             : *****************************************************************/
     421             : 
     422             : typedef struct
     423             : {
     424             :     OffsetNumber pos;
     425             :     float       cost;
     426             : } SPLITCOST;
     427             : 
     428             : static int
     429      126672 : comparecost(const void *a, const void *b)
     430             : {
     431      126672 :     if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
     432       67090 :         return 0;
     433             :     else
     434       59582 :         return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
     435             : }
     436             : 
     437             : /*
     438             : ** The GiST PickSplit method for _intments
     439             : ** We use Guttman's poly time split algorithm
     440             : */
     441             : Datum
     442        1232 : g_int_picksplit(PG_FUNCTION_ARGS)
     443             : {
     444        1232 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     445        1232 :     GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
     446             :     OffsetNumber i,
     447             :                 j;
     448             :     ArrayType  *datum_alpha,
     449             :                *datum_beta;
     450             :     ArrayType  *datum_l,
     451             :                *datum_r;
     452             :     ArrayType  *union_d,
     453             :                *union_dl,
     454             :                *union_dr;
     455             :     ArrayType  *inter_d;
     456             :     bool        firsttime;
     457             :     float       size_alpha,
     458             :                 size_beta,
     459             :                 size_union,
     460             :                 size_inter;
     461             :     float       size_waste,
     462             :                 waste;
     463             :     float       size_l,
     464             :                 size_r;
     465             :     int         nbytes;
     466        1232 :     OffsetNumber seed_1 = 0,
     467        1232 :                 seed_2 = 0;
     468             :     OffsetNumber *left,
     469             :                *right;
     470             :     OffsetNumber maxoff;
     471             :     SPLITCOST  *costvector;
     472             : 
     473             : #ifdef GIST_DEBUG
     474             :     elog(DEBUG3, "--------picksplit %d", entryvec->n);
     475             : #endif
     476             : 
     477        1232 :     maxoff = entryvec->n - 2;
     478        1232 :     nbytes = (maxoff + 2) * sizeof(OffsetNumber);
     479        1232 :     v->spl_left = (OffsetNumber *) palloc(nbytes);
     480        1232 :     v->spl_right = (OffsetNumber *) palloc(nbytes);
     481             : 
     482        1232 :     firsttime = true;
     483        1232 :     waste = 0.0;
     484       66188 :     for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
     485             :     {
     486       64956 :         datum_alpha = GETENTRY(entryvec, i);
     487     3668066 :         for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
     488             :         {
     489     3603110 :             datum_beta = GETENTRY(entryvec, j);
     490             : 
     491             :             /* compute the wasted space by unioning these guys */
     492             :             /* size_waste = size_union - size_inter; */
     493     3603110 :             union_d = inner_int_union(datum_alpha, datum_beta);
     494     3603110 :             rt__int_size(union_d, &size_union);
     495     3603110 :             inter_d = inner_int_inter(datum_alpha, datum_beta);
     496     3603110 :             rt__int_size(inter_d, &size_inter);
     497     3603110 :             size_waste = size_union - size_inter;
     498             : 
     499     3603110 :             pfree(union_d);
     500     3603110 :             pfree(inter_d);
     501             : 
     502             :             /*
     503             :              * are these a more promising split that what we've already seen?
     504             :              */
     505             : 
     506     3603110 :             if (size_waste > waste || firsttime)
     507             :             {
     508        5810 :                 waste = size_waste;
     509        5810 :                 seed_1 = i;
     510        5810 :                 seed_2 = j;
     511        5810 :                 firsttime = false;
     512             :             }
     513             :         }
     514             :     }
     515             : 
     516        1232 :     left = v->spl_left;
     517        1232 :     v->spl_nleft = 0;
     518        1232 :     right = v->spl_right;
     519        1232 :     v->spl_nright = 0;
     520        1232 :     if (seed_1 == 0 || seed_2 == 0)
     521             :     {
     522           0 :         seed_1 = 1;
     523           0 :         seed_2 = 2;
     524             :     }
     525             : 
     526        1232 :     datum_alpha = GETENTRY(entryvec, seed_1);
     527        1232 :     datum_l = copy_intArrayType(datum_alpha);
     528        1232 :     rt__int_size(datum_l, &size_l);
     529        1232 :     datum_beta = GETENTRY(entryvec, seed_2);
     530        1232 :     datum_r = copy_intArrayType(datum_beta);
     531        1232 :     rt__int_size(datum_r, &size_r);
     532             : 
     533        1232 :     maxoff = OffsetNumberNext(maxoff);
     534             : 
     535             :     /*
     536             :      * sort entries
     537             :      */
     538        1232 :     costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
     539       68652 :     for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
     540             :     {
     541       67420 :         costvector[i - 1].pos = i;
     542       67420 :         datum_alpha = GETENTRY(entryvec, i);
     543       67420 :         union_d = inner_int_union(datum_l, datum_alpha);
     544       67420 :         rt__int_size(union_d, &size_alpha);
     545       67420 :         pfree(union_d);
     546       67420 :         union_d = inner_int_union(datum_r, datum_alpha);
     547       67420 :         rt__int_size(union_d, &size_beta);
     548       67420 :         pfree(union_d);
     549       67420 :         costvector[i - 1].cost = fabsf((size_alpha - size_l) - (size_beta - size_r));
     550             :     }
     551        1232 :     qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
     552             : 
     553             :     /*
     554             :      * Now split up the regions between the two seeds.  An important property
     555             :      * of this split algorithm is that the split vector v has the indices of
     556             :      * items to be split in order in its left and right vectors.  We exploit
     557             :      * this property by doing a merge in the code that actually splits the
     558             :      * page.
     559             :      *
     560             :      * For efficiency, we also place the new index tuple in this loop. This is
     561             :      * handled at the very end, when we have placed all the existing tuples
     562             :      * and i == maxoff + 1.
     563             :      */
     564             : 
     565             : 
     566       68652 :     for (j = 0; j < maxoff; j++)
     567             :     {
     568       67420 :         i = costvector[j].pos;
     569             : 
     570             :         /*
     571             :          * If we've already decided where to place this item, just put it on
     572             :          * the right list.  Otherwise, we need to figure out which page needs
     573             :          * the least enlargement in order to store the item.
     574             :          */
     575             : 
     576       67420 :         if (i == seed_1)
     577             :         {
     578        1232 :             *left++ = i;
     579        1232 :             v->spl_nleft++;
     580        1232 :             continue;
     581             :         }
     582       66188 :         else if (i == seed_2)
     583             :         {
     584        1232 :             *right++ = i;
     585        1232 :             v->spl_nright++;
     586        1232 :             continue;
     587             :         }
     588             : 
     589             :         /* okay, which page needs least enlargement? */
     590       64956 :         datum_alpha = GETENTRY(entryvec, i);
     591       64956 :         union_dl = inner_int_union(datum_l, datum_alpha);
     592       64956 :         union_dr = inner_int_union(datum_r, datum_alpha);
     593       64956 :         rt__int_size(union_dl, &size_alpha);
     594       64956 :         rt__int_size(union_dr, &size_beta);
     595             : 
     596             :         /* pick which page to add it to */
     597       64956 :         if (size_alpha - size_l < size_beta - size_r + WISH_F(v->spl_nleft, v->spl_nright, 0.01))
     598             :         {
     599       31182 :             pfree(datum_l);
     600       31182 :             pfree(union_dr);
     601       31182 :             datum_l = union_dl;
     602       31182 :             size_l = size_alpha;
     603       31182 :             *left++ = i;
     604       31182 :             v->spl_nleft++;
     605             :         }
     606             :         else
     607             :         {
     608       33774 :             pfree(datum_r);
     609       33774 :             pfree(union_dl);
     610       33774 :             datum_r = union_dr;
     611       33774 :             size_r = size_beta;
     612       33774 :             *right++ = i;
     613       33774 :             v->spl_nright++;
     614             :         }
     615             :     }
     616        1232 :     pfree(costvector);
     617        1232 :     *right = *left = FirstOffsetNumber;
     618             : 
     619        1232 :     v->spl_ldatum = PointerGetDatum(datum_l);
     620        1232 :     v->spl_rdatum = PointerGetDatum(datum_r);
     621             : 
     622        1232 :     PG_RETURN_POINTER(v);
     623             : }
     624             : 
     625             : Datum
     626          26 : g_int_options(PG_FUNCTION_ARGS)
     627             : {
     628          26 :     local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
     629             : 
     630          26 :     init_local_reloptions(relopts, sizeof(GISTIntArrayOptions));
     631          26 :     add_local_int_reloption(relopts, "numranges",
     632             :                             "number of ranges for compression",
     633             :                             G_INT_NUMRANGES_DEFAULT, 1, G_INT_NUMRANGES_MAX,
     634             :                             offsetof(GISTIntArrayOptions, num_ranges));
     635             : 
     636          26 :     PG_RETURN_VOID();
     637             : }

Generated by: LCOV version 1.14