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