2008-05-17 05:28:26 +04:00
|
|
|
/*
|
2010-09-21 00:08:53 +04:00
|
|
|
* contrib/intarray/_int_gist.c
|
2008-05-17 05:28:26 +04:00
|
|
|
*/
|
2008-05-12 04:00:54 +04:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2015-03-26 00:39:42 +03:00
|
|
|
#include <limits.h>
|
|
|
|
|
2019-10-23 06:56:22 +03:00
|
|
|
#include "_int.h"
|
2008-05-12 04:00:54 +04:00
|
|
|
#include "access/gist.h"
|
2015-05-15 23:03:16 +03:00
|
|
|
#include "access/stratnum.h"
|
2008-05-12 04:00:54 +04:00
|
|
|
|
2004-03-30 19:45:33 +04:00
|
|
|
#define GETENTRY(vec,pos) ((ArrayType *) DatumGetPointer((vec)->vector[(pos)].key))
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2018-11-24 02:56:39 +03:00
|
|
|
/*
|
|
|
|
* Control the maximum sparseness of compressed keys.
|
|
|
|
*
|
|
|
|
* The upper safe bound for this limit is half the maximum allocatable array
|
|
|
|
* size. A lower bound would give more guarantees that pathological data
|
|
|
|
* wouldn't eat excessive CPU and memory, but at the expense of breaking
|
|
|
|
* possibly working (after a fashion) indexes.
|
|
|
|
*/
|
|
|
|
#define MAXNUMELTS (Min((MaxAllocSize / sizeof(Datum)),((MaxAllocSize - ARR_OVERHEAD_NONULLS(1)) / sizeof(int)))/2)
|
|
|
|
/* or: #define MAXNUMELTS 1000000 */
|
|
|
|
|
2003-06-11 23:31:05 +04:00
|
|
|
/*
|
|
|
|
** GiST support methods
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_consistent);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_compress);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_decompress);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_penalty);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_picksplit);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_union);
|
|
|
|
PG_FUNCTION_INFO_V1(g_int_same);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Consistent method for _intments
|
|
|
|
** Should return false if for all data items x below entry,
|
2017-08-16 07:22:32 +03:00
|
|
|
** the predicate x op query == false, where op is the oper
|
2003-06-11 23:31:05 +04:00
|
|
|
** corresponding to strategy in the pg_amop table.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
g_int_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
2011-01-09 08:39:21 +03:00
|
|
|
ArrayType *query = PG_GETARG_ARRAYTYPE_P_COPY(1);
|
2003-06-11 23:31:05 +04:00
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2009-06-11 18:49:15 +04:00
|
|
|
|
2008-04-14 21:05:34 +04:00
|
|
|
/* Oid subtype = PG_GETARG_OID(3); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(4);
|
2003-06-11 23:31:05 +04:00
|
|
|
bool retval;
|
|
|
|
|
2008-04-14 21:05:34 +04:00
|
|
|
/* this is exact except for RTSameStrategyNumber */
|
|
|
|
*recheck = (strategy == RTSameStrategyNumber);
|
|
|
|
|
2006-10-04 04:30:14 +04:00
|
|
|
if (strategy == BooleanSearchStrategy)
|
|
|
|
{
|
2006-04-03 12:21:05 +04:00
|
|
|
retval = execconsistent((QUERYTYPE *) query,
|
2006-10-04 04:30:14 +04:00
|
|
|
(ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
GIST_LEAF(entry));
|
2006-04-03 12:21:05 +04:00
|
|
|
|
2006-10-04 04:30:14 +04:00
|
|
|
pfree(query);
|
2006-04-03 12:21:05 +04:00
|
|
|
PG_RETURN_BOOL(retval);
|
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
/* sort query for fast search, key is already sorted */
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(query);
|
2003-06-11 23:31:05 +04:00
|
|
|
PREPAREARR(query);
|
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
|
|
|
retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
query);
|
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
if (GIST_LEAF(entry))
|
2011-01-09 08:39:21 +03:00
|
|
|
DirectFunctionCall3(g_int_same,
|
2003-06-11 23:31:05 +04:00
|
|
|
entry->key,
|
|
|
|
PointerGetDatum(query),
|
2011-01-09 08:39:21 +03:00
|
|
|
PointerGetDatum(&retval));
|
2003-06-11 23:31:05 +04:00
|
|
|
else
|
|
|
|
retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
query);
|
|
|
|
break;
|
|
|
|
case RTContainsStrategyNumber:
|
2006-09-10 21:36:52 +04:00
|
|
|
case RTOldContainsStrategyNumber:
|
2003-06-11 23:31:05 +04:00
|
|
|
retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
query);
|
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2006-09-10 21:36:52 +04:00
|
|
|
case RTOldContainedByStrategyNumber:
|
2003-06-11 23:31:05 +04:00
|
|
|
if (GIST_LEAF(entry))
|
|
|
|
retval = inner_int_contains(query,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 22:35:54 +03:00
|
|
|
(ArrayType *) DatumGetPointer(entry->key));
|
2003-06-11 23:31:05 +04:00
|
|
|
else
|
Fix intarray's GiST opclasses to not fail for empty arrays with <@.
contrib/intarray considers "arraycol <@ constant-array" to be indexable,
but its GiST opclass code fails to reliably find index entries for empty
array values (which of course should trivially match such queries).
This is because the test condition to see whether we should descend
through a non-leaf node is wrong.
Unfortunately, empty array entries could be anywhere in the index,
as these index opclasses are currently designed. So there's no way
to fix this except by lobotomizing <@ indexscans to scan the whole
index ... which is what this patch does. That's pretty unfortunate:
the performance is now actually worse than a seqscan, in most cases.
We'd be better off to remove <@ from the GiST opclasses entirely,
and perhaps a future non-back-patchable patch will do so.
In the meantime, applications whose performance is adversely impacted
have a couple of options. They could switch to a GIN index, which
doesn't have this bug, or they could replace "arraycol <@ constant-array"
with "arraycol <@ constant-array AND arraycol && constant-array".
That will provide about the same performance as before, and it will find
all non-empty subsets of the given constant-array, which is all that
could reliably be expected of the query before.
While at it, add some more regression test cases to improve code
coverage of contrib/intarray.
In passing, adjust resize_intArrayType so that when it's returning an
empty array, it uses construct_empty_array for that rather than
cowboy hacking on the input array. While the hack produces an array
that looks valid for most purposes, it isn't bitwise equal to empty
arrays produced by other code paths, which could have subtle odd
effects. I don't think this code path is performance-critical
enough to justify such shortcuts. (Back-patch this part only as far
as v11; before commit 01783ac36 we were not careful about this in
other intarray code paths either.)
Back-patch the <@ fixes to all supported versions, since this was
broken from day one.
Patch by me; thanks to Alexander Korotkov for review.
Discussion: https://postgr.es/m/458.1565114141@sss.pgh.pa.us
2019-08-07 01:04:51 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Unfortunately, because empty arrays could be anywhere in
|
|
|
|
* the index, we must search the whole tree.
|
|
|
|
*/
|
|
|
|
retval = true;
|
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-16 07:22:32 +03:00
|
|
|
retval = false;
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
2006-10-04 04:30:14 +04:00
|
|
|
pfree(query);
|
2003-06-11 23:31:05 +04:00
|
|
|
PG_RETURN_BOOL(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2003-08-04 04:43:34 +04:00
|
|
|
g_int_union(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
2003-08-04 04:43:34 +04:00
|
|
|
int *size = (int *) PG_GETARG_POINTER(1);
|
2012-06-25 02:51:46 +04:00
|
|
|
int32 i,
|
2003-08-04 04:43:34 +04:00
|
|
|
*ptr;
|
2005-11-19 06:00:09 +03:00
|
|
|
ArrayType *res;
|
|
|
|
int totlen = 0;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2004-03-30 19:45:33 +04:00
|
|
|
for (i = 0; i < entryvec->n; i++)
|
2005-11-19 06:00:09 +03:00
|
|
|
{
|
2005-11-22 21:17:34 +03:00
|
|
|
ArrayType *ent = GETENTRY(entryvec, i);
|
2005-11-19 06:00:09 +03:00
|
|
|
|
|
|
|
CHECKARRVALID(ent);
|
|
|
|
totlen += ARRNELEMS(ent);
|
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2003-08-04 04:43:34 +04:00
|
|
|
res = new_intArrayType(totlen);
|
|
|
|
ptr = ARRPTR(res);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2004-03-30 19:45:33 +04:00
|
|
|
for (i = 0; i < entryvec->n; i++)
|
2003-08-04 04:43:34 +04:00
|
|
|
{
|
2005-11-22 21:17:34 +03:00
|
|
|
ArrayType *ent = GETENTRY(entryvec, i);
|
|
|
|
int nel;
|
2005-11-19 06:00:09 +03:00
|
|
|
|
|
|
|
nel = ARRNELEMS(ent);
|
2012-06-25 02:51:46 +04:00
|
|
|
memcpy(ptr, ARRPTR(ent), nel * sizeof(int32));
|
2005-11-19 06:00:09 +03:00
|
|
|
ptr += nel;
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
|
|
|
|
2003-08-04 04:43:34 +04:00
|
|
|
QSORT(res, 1);
|
|
|
|
res = _int_unique(res);
|
|
|
|
*size = VARSIZE(res);
|
2003-06-11 23:31:05 +04:00
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GiST Compress and Decompress methods
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
g_int_compress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *r;
|
2018-11-24 02:56:39 +03:00
|
|
|
int len,
|
|
|
|
lenr;
|
2003-06-11 23:31:05 +04:00
|
|
|
int *dr;
|
|
|
|
int i,
|
2018-11-24 02:56:39 +03:00
|
|
|
j,
|
2003-06-11 23:31:05 +04:00
|
|
|
cand;
|
2018-11-24 02:56:39 +03:00
|
|
|
int64 min;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
if (entry->leafkey)
|
|
|
|
{
|
2011-01-09 08:39:21 +03:00
|
|
|
r = DatumGetArrayTypePCopy(entry->key);
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(r);
|
2003-06-11 23:31:05 +04:00
|
|
|
PREPAREARR(r);
|
2005-11-14 19:11:37 +03:00
|
|
|
|
2005-11-22 21:17:34 +03:00
|
|
|
if (ARRNELEMS(r) >= 2 * MAXNUMRANGE)
|
2006-03-01 09:30:32 +03:00
|
|
|
elog(NOTICE, "input array is too big (%d maximum allowed, %d current), use gist__intbig_ops opclass instead",
|
2005-11-22 21:17:34 +03:00
|
|
|
2 * MAXNUMRANGE - 1, ARRNELEMS(r));
|
|
|
|
|
2003-06-11 23:31:05 +04:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
2017-08-16 07:22:32 +03:00
|
|
|
entry->rel, entry->page, entry->offset, false);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
|
2005-11-22 21:17:34 +03:00
|
|
|
/*
|
|
|
|
* leaf entries never compress one more time, only when entry->leafkey
|
|
|
|
* ==true, so now we work only with internal keys
|
|
|
|
*/
|
2005-11-14 19:11:37 +03:00
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
r = DatumGetArrayTypeP(entry->key);
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(r);
|
2011-01-09 08:39:21 +03:00
|
|
|
if (ARRISEMPTY(r))
|
2003-06-11 23:31:05 +04:00
|
|
|
{
|
|
|
|
if (r != (ArrayType *) DatumGetPointer(entry->key))
|
|
|
|
pfree(r);
|
|
|
|
PG_RETURN_POINTER(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((len = ARRNELEMS(r)) >= 2 * MAXNUMRANGE)
|
|
|
|
{ /* compress */
|
|
|
|
if (r == (ArrayType *) DatumGetPointer(entry->key))
|
2011-01-09 08:39:21 +03:00
|
|
|
r = DatumGetArrayTypePCopy(entry->key);
|
2003-06-11 23:31:05 +04:00
|
|
|
r = resize_intArrayType(r, 2 * (len));
|
|
|
|
|
|
|
|
dr = ARRPTR(r);
|
|
|
|
|
2018-11-24 02:56:39 +03:00
|
|
|
/*
|
|
|
|
* "len" at this point is the number of ranges we will construct.
|
|
|
|
* "lenr" is the number of ranges we must eventually remove by
|
|
|
|
* merging, we must be careful to remove no more than this number.
|
|
|
|
*/
|
|
|
|
lenr = len - MAXNUMRANGE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initially assume we can merge consecutive ints into a range. but we
|
|
|
|
* must count every value removed and stop when lenr runs out
|
|
|
|
*/
|
|
|
|
for (j = i = len - 1; i > 0 && lenr > 0; i--, j--)
|
|
|
|
{
|
2019-05-22 19:55:34 +03:00
|
|
|
int r_end = dr[i];
|
|
|
|
int r_start = r_end;
|
|
|
|
|
|
|
|
while (i > 0 && lenr > 0 && dr[i - 1] == r_start - 1)
|
2018-11-24 02:56:39 +03:00
|
|
|
--r_start, --i, --lenr;
|
2019-05-22 19:55:34 +03:00
|
|
|
dr[2 * j] = r_start;
|
|
|
|
dr[2 * j + 1] = r_end;
|
2018-11-24 02:56:39 +03:00
|
|
|
}
|
|
|
|
/* just copy the rest, if any, as trivial ranges */
|
|
|
|
for (; i >= 0; i--, j--)
|
2019-05-22 19:55:34 +03:00
|
|
|
dr[2 * j] = dr[2 * j + 1] = dr[i];
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2018-11-24 02:56:39 +03:00
|
|
|
if (++j)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* shunt everything down to start at the right place
|
|
|
|
*/
|
2019-05-22 19:55:34 +03:00
|
|
|
memmove((void *) &dr[0], (void *) &dr[2 * j], 2 * (len - j) * sizeof(int32));
|
2018-11-24 02:56:39 +03:00
|
|
|
}
|
2019-05-22 19:55:34 +03:00
|
|
|
|
2018-11-24 02:56:39 +03:00
|
|
|
/*
|
|
|
|
* make "len" be number of array elements, not ranges
|
|
|
|
*/
|
2019-05-22 19:55:34 +03:00
|
|
|
len = 2 * (len - j);
|
2003-06-11 23:31:05 +04:00
|
|
|
cand = 1;
|
|
|
|
while (len > MAXNUMRANGE * 2)
|
|
|
|
{
|
2018-11-24 02:56:39 +03:00
|
|
|
min = PG_INT64_MAX;
|
2003-06-11 23:31:05 +04:00
|
|
|
for (i = 2; i < len; i += 2)
|
2019-05-22 19:55:34 +03:00
|
|
|
if (min > ((int64) dr[i] - (int64) dr[i - 1]))
|
2003-06-11 23:31:05 +04:00
|
|
|
{
|
2019-05-22 19:55:34 +03:00
|
|
|
min = ((int64) dr[i] - (int64) dr[i - 1]);
|
2003-06-11 23:31:05 +04:00
|
|
|
cand = i;
|
|
|
|
}
|
2006-04-13 01:46:31 +04:00
|
|
|
memmove((void *) &dr[cand - 1], (void *) &dr[cand + 1], (len - cand - 1) * sizeof(int32));
|
2003-06-11 23:31:05 +04:00
|
|
|
len -= 2;
|
|
|
|
}
|
2019-05-22 19:55:34 +03:00
|
|
|
|
2018-11-24 02:56:39 +03:00
|
|
|
/*
|
|
|
|
* check sparseness of result
|
|
|
|
*/
|
|
|
|
lenr = internal_size(dr, len);
|
|
|
|
if (lenr < 0 || lenr > MAXNUMELTS)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errmsg("data is too sparse, recreate index using gist__intbig_ops opclass instead")));
|
|
|
|
|
2003-06-11 23:31:05 +04:00
|
|
|
r = resize_intArrayType(r, len);
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
2017-08-16 07:22:32 +03:00
|
|
|
entry->rel, entry->page, entry->offset, false);
|
2003-06-11 23:31:05 +04:00
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PG_RETURN_POINTER(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
g_int_decompress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *r;
|
|
|
|
int *dr,
|
|
|
|
lenr;
|
|
|
|
ArrayType *in;
|
|
|
|
int lenin;
|
|
|
|
int *din;
|
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
|
2011-01-09 08:39:21 +03:00
|
|
|
in = DatumGetArrayTypeP(entry->key);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(in);
|
2011-01-09 08:39:21 +03:00
|
|
|
if (ARRISEMPTY(in))
|
2007-04-06 08:21:44 +04:00
|
|
|
{
|
2007-11-16 00:14:46 +03:00
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
|
|
|
{
|
2007-04-06 08:21:44 +04:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(in),
|
2017-08-16 07:22:32 +03:00
|
|
|
entry->rel, entry->page, entry->offset, false);
|
2007-04-06 08:21:44 +04:00
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
|
2003-06-11 23:31:05 +04:00
|
|
|
PG_RETURN_POINTER(entry);
|
2007-04-06 08:21:44 +04:00
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
lenin = ARRNELEMS(in);
|
|
|
|
|
2005-11-14 19:11:37 +03:00
|
|
|
if (lenin < 2 * MAXNUMRANGE)
|
2003-06-11 23:31:05 +04:00
|
|
|
{ /* not compressed value */
|
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
|
|
|
{
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(in),
|
2017-08-16 07:22:32 +03:00
|
|
|
entry->rel, entry->page, entry->offset, false);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
PG_RETURN_POINTER(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
din = ARRPTR(in);
|
|
|
|
lenr = internal_size(din, lenin);
|
2018-11-24 02:56:39 +03:00
|
|
|
if (lenr < 0 || lenr > MAXNUMELTS)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errmsg("compressed array is too big, recreate index using gist__intbig_ops opclass instead")));
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
r = new_intArrayType(lenr);
|
|
|
|
dr = ARRPTR(r);
|
|
|
|
|
|
|
|
for (i = 0; i < lenin; i += 2)
|
|
|
|
for (j = din[i]; j <= din[i + 1]; j++)
|
|
|
|
if ((!i) || *(dr - 1) != j)
|
|
|
|
*dr++ = j;
|
|
|
|
|
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
|
|
|
pfree(in);
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
2017-08-16 07:22:32 +03:00
|
|
|
entry->rel, entry->page, entry->offset, false);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Penalty method for _intments
|
|
|
|
*/
|
|
|
|
Datum
|
2003-08-04 04:43:34 +04:00
|
|
|
g_int_penalty(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
|
|
|
|
float *result = (float *) PG_GETARG_POINTER(2);
|
2003-06-11 23:31:05 +04:00
|
|
|
ArrayType *ud;
|
|
|
|
float tmp1,
|
|
|
|
tmp2;
|
|
|
|
|
|
|
|
ud = inner_int_union((ArrayType *) DatumGetPointer(origentry->key),
|
2003-08-04 04:43:34 +04:00
|
|
|
(ArrayType *) DatumGetPointer(newentry->key));
|
2003-06-11 23:31:05 +04:00
|
|
|
rt__int_size(ud, &tmp1);
|
|
|
|
rt__int_size((ArrayType *) DatumGetPointer(origentry->key), &tmp2);
|
|
|
|
*result = tmp1 - tmp2;
|
|
|
|
pfree(ud);
|
|
|
|
|
2003-08-04 04:43:34 +04:00
|
|
|
PG_RETURN_POINTER(result);
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
g_int_same(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2011-01-09 08:39:21 +03:00
|
|
|
ArrayType *a = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
ArrayType *b = PG_GETARG_ARRAYTYPE_P(1);
|
2003-06-11 23:31:05 +04:00
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
2012-06-25 02:51:46 +04:00
|
|
|
int32 n = ARRNELEMS(a);
|
|
|
|
int32 *da,
|
2003-06-11 23:31:05 +04:00
|
|
|
*db;
|
|
|
|
|
2005-11-19 06:00:09 +03:00
|
|
|
CHECKARRVALID(a);
|
|
|
|
CHECKARRVALID(b);
|
|
|
|
|
2003-06-11 23:31:05 +04:00
|
|
|
if (n != ARRNELEMS(b))
|
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
2017-08-16 07:22:32 +03:00
|
|
|
*result = true;
|
2003-06-11 23:31:05 +04:00
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
|
|
|
while (n--)
|
2011-01-09 08:39:21 +03:00
|
|
|
{
|
2003-06-11 23:31:05 +04:00
|
|
|
if (*da++ != *db++)
|
|
|
|
{
|
2017-08-16 07:22:32 +03:00
|
|
|
*result = false;
|
2003-06-11 23:31:05 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-01-09 08:39:21 +03:00
|
|
|
}
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
** Common GiST Method
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
OffsetNumber pos;
|
|
|
|
float cost;
|
2007-11-16 01:25:18 +03:00
|
|
|
} SPLITCOST;
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
comparecost(const void *a, const void *b)
|
|
|
|
{
|
2011-09-11 22:54:32 +04:00
|
|
|
if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
|
2003-06-11 23:31:05 +04:00
|
|
|
return 0;
|
|
|
|
else
|
2011-09-11 22:54:32 +04:00
|
|
|
return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST PickSplit method for _intments
|
|
|
|
** We use Guttman's poly time split algorithm
|
|
|
|
*/
|
|
|
|
Datum
|
2003-08-04 04:43:34 +04:00
|
|
|
g_int_picksplit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
2003-06-11 23:31:05 +04:00
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
|
|
|
OffsetNumber i,
|
|
|
|
j;
|
|
|
|
ArrayType *datum_alpha,
|
|
|
|
*datum_beta;
|
|
|
|
ArrayType *datum_l,
|
|
|
|
*datum_r;
|
|
|
|
ArrayType *union_d,
|
|
|
|
*union_dl,
|
|
|
|
*union_dr;
|
|
|
|
ArrayType *inter_d;
|
|
|
|
bool firsttime;
|
|
|
|
float size_alpha,
|
|
|
|
size_beta,
|
|
|
|
size_union,
|
|
|
|
size_inter;
|
|
|
|
float size_waste,
|
|
|
|
waste;
|
|
|
|
float size_l,
|
|
|
|
size_r;
|
|
|
|
int nbytes;
|
|
|
|
OffsetNumber seed_1 = 0,
|
|
|
|
seed_2 = 0;
|
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
OffsetNumber maxoff;
|
|
|
|
SPLITCOST *costvector;
|
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2004-03-30 19:45:33 +04:00
|
|
|
elog(DEBUG3, "--------picksplit %d", entryvec->n);
|
2003-06-11 23:31:05 +04:00
|
|
|
#endif
|
|
|
|
|
2004-03-30 19:45:33 +04:00
|
|
|
maxoff = entryvec->n - 2;
|
2003-06-11 23:31:05 +04:00
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
v->spl_left = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_right = (OffsetNumber *) palloc(nbytes);
|
|
|
|
|
|
|
|
firsttime = true;
|
|
|
|
waste = 0.0;
|
|
|
|
for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
datum_alpha = GETENTRY(entryvec, i);
|
2003-06-11 23:31:05 +04:00
|
|
|
for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
datum_beta = GETENTRY(entryvec, j);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
/* compute the wasted space by unioning these guys */
|
|
|
|
/* size_waste = size_union - size_inter; */
|
|
|
|
union_d = inner_int_union(datum_alpha, datum_beta);
|
|
|
|
rt__int_size(union_d, &size_union);
|
|
|
|
inter_d = inner_int_inter(datum_alpha, datum_beta);
|
|
|
|
rt__int_size(inter_d, &size_inter);
|
|
|
|
size_waste = size_union - size_inter;
|
|
|
|
|
|
|
|
pfree(union_d);
|
2015-02-17 00:26:23 +03:00
|
|
|
pfree(inter_d);
|
2003-06-11 23:31:05 +04:00
|
|
|
|
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* are these a more promising split that what we've already seen?
|
2003-06-11 23:31:05 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (size_waste > waste || firsttime)
|
|
|
|
{
|
|
|
|
waste = size_waste;
|
|
|
|
seed_1 = i;
|
|
|
|
seed_2 = j;
|
|
|
|
firsttime = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
left = v->spl_left;
|
|
|
|
v->spl_nleft = 0;
|
|
|
|
right = v->spl_right;
|
|
|
|
v->spl_nright = 0;
|
|
|
|
if (seed_1 == 0 || seed_2 == 0)
|
|
|
|
{
|
|
|
|
seed_1 = 1;
|
|
|
|
seed_2 = 2;
|
|
|
|
}
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
datum_alpha = GETENTRY(entryvec, seed_1);
|
2003-06-11 23:31:05 +04:00
|
|
|
datum_l = copy_intArrayType(datum_alpha);
|
|
|
|
rt__int_size(datum_l, &size_l);
|
2004-08-29 09:07:03 +04:00
|
|
|
datum_beta = GETENTRY(entryvec, seed_2);
|
2003-06-11 23:31:05 +04:00
|
|
|
datum_r = copy_intArrayType(datum_beta);
|
|
|
|
rt__int_size(datum_r, &size_r);
|
|
|
|
|
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sort entries
|
|
|
|
*/
|
|
|
|
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
|
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
costvector[i - 1].pos = i;
|
2004-08-29 09:07:03 +04:00
|
|
|
datum_alpha = GETENTRY(entryvec, i);
|
2003-06-11 23:31:05 +04:00
|
|
|
union_d = inner_int_union(datum_l, datum_alpha);
|
|
|
|
rt__int_size(union_d, &size_alpha);
|
|
|
|
pfree(union_d);
|
|
|
|
union_d = inner_int_union(datum_r, datum_alpha);
|
|
|
|
rt__int_size(union_d, &size_beta);
|
|
|
|
pfree(union_d);
|
2004-10-21 23:28:36 +04:00
|
|
|
costvector[i - 1].cost = Abs((size_alpha - size_l) - (size_beta - size_r));
|
2003-06-11 23:31:05 +04:00
|
|
|
}
|
|
|
|
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
|
|
|
|
|
|
|
|
/*
|
2014-05-06 20:12:18 +04:00
|
|
|
* Now split up the regions between the two seeds. An important property
|
2005-10-15 06:49:52 +04:00
|
|
|
* of this split algorithm is that the split vector v has the indices of
|
|
|
|
* items to be split in order in its left and right vectors. We exploit
|
|
|
|
* this property by doing a merge in the code that actually splits the
|
|
|
|
* page.
|
2003-06-11 23:31:05 +04:00
|
|
|
*
|
2005-10-15 06:49:52 +04:00
|
|
|
* For efficiency, we also place the new index tuple in this loop. This is
|
|
|
|
* handled at the very end, when we have placed all the existing tuples
|
|
|
|
* and i == maxoff + 1.
|
2003-06-11 23:31:05 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < maxoff; j++)
|
|
|
|
{
|
|
|
|
i = costvector[j].pos;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* If we've already decided where to place this item, just put it on
|
2014-05-06 20:12:18 +04:00
|
|
|
* the right list. Otherwise, we need to figure out which page needs
|
2005-10-15 06:49:52 +04:00
|
|
|
* the least enlargement in order to store the item.
|
2003-06-11 23:31:05 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (i == seed_1)
|
|
|
|
{
|
|
|
|
*left++ = i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (i == seed_2)
|
|
|
|
{
|
|
|
|
*right++ = i;
|
|
|
|
v->spl_nright++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* okay, which page needs least enlargement? */
|
2004-08-29 09:07:03 +04:00
|
|
|
datum_alpha = GETENTRY(entryvec, i);
|
2003-06-11 23:31:05 +04:00
|
|
|
union_dl = inner_int_union(datum_l, datum_alpha);
|
|
|
|
union_dr = inner_int_union(datum_r, datum_alpha);
|
|
|
|
rt__int_size(union_dl, &size_alpha);
|
|
|
|
rt__int_size(union_dr, &size_beta);
|
|
|
|
|
|
|
|
/* pick which page to add it to */
|
|
|
|
if (size_alpha - size_l < size_beta - size_r + WISH_F(v->spl_nleft, v->spl_nright, 0.01))
|
|
|
|
{
|
2015-02-17 00:26:23 +03:00
|
|
|
pfree(datum_l);
|
|
|
|
pfree(union_dr);
|
2003-06-11 23:31:05 +04:00
|
|
|
datum_l = union_dl;
|
|
|
|
size_l = size_alpha;
|
|
|
|
*left++ = i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-17 00:26:23 +03:00
|
|
|
pfree(datum_r);
|
|
|
|
pfree(union_dl);
|
2003-06-11 23:31:05 +04:00
|
|
|
datum_r = union_dr;
|
|
|
|
size_r = size_beta;
|
|
|
|
*right++ = i;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pfree(costvector);
|
|
|
|
*right = *left = FirstOffsetNumber;
|
|
|
|
|
|
|
|
v->spl_ldatum = PointerGetDatum(datum_l);
|
|
|
|
v->spl_rdatum = PointerGetDatum(datum_r);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(v);
|
|
|
|
}
|