1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

Implement operator class parameters

PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing.  These index AMs are GiST, GIN,
SP-GiST and BRIN.  There opclasses define representation of keys, operations on
them and supported search strategies.  So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision.  This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.

This commit doesn't introduce new storage in system catalog.  Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.

In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions.  Options
are set to fn_expr as the constant bytea expression.  It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.

This commit comes with some examples of opclass options usage.  We parametrize
signature length in GiST.  That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops.  Also we parametrize maximum number of integer ranges for
gist__int_ops.  However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.

Catversion is bumped.

Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
This commit is contained in:
Alexander Korotkov
2020-03-30 19:17:11 +03:00
parent 1d53432ff9
commit 911e702077
108 changed files with 4086 additions and 924 deletions

View File

@ -12,7 +12,8 @@ OBJS = \
_intbig_gist.o
EXTENSION = intarray
DATA = intarray--1.2.sql intarray--1.1--1.2.sql intarray--1.0--1.1.sql
DATA = intarray--1.2--1.3.sql intarray--1.2.sql intarray--1.1--1.2.sql \
intarray--1.0--1.1.sql
PGFILEDESC = "intarray - functions and operators for arrays of integers"
REGRESS = _int

View File

@ -8,7 +8,19 @@
#include "utils/memutils.h"
/* number ranges for compression */
#define MAXNUMRANGE 100
#define G_INT_NUMRANGES_DEFAULT 100
#define G_INT_NUMRANGES_MAX ((GISTMaxIndexKeySize - VARHDRSZ) / \
(2 * sizeof(int32)))
#define G_INT_GET_NUMRANGES() (PG_HAS_OPCLASS_OPTIONS() ? \
((GISTIntArrayOptions *) PG_GET_OPCLASS_OPTIONS())->num_ranges : \
G_INT_NUMRANGES_DEFAULT)
/* gist_int_ops opclass options */
typedef struct
{
int32 vl_len_; /* varlena header (do not touch directly!) */
int num_ranges; /* number of ranges */
} GISTIntArrayOptions;
/* useful macros for accessing int4 arrays */
#define ARRPTR(x) ( (int32 *) ARR_DATA_PTR(x) )
@ -47,15 +59,17 @@
/* bigint defines */
#define SIGLENINT 63 /* >122 => key will toast, so very slow!!! */
#define SIGLEN ( sizeof(int)*SIGLENINT )
#define SIGLENBIT (SIGLEN*BITS_PER_BYTE)
#define SIGLEN_DEFAULT (63 * 4)
#define SIGLEN_MAX GISTMaxIndexKeySize
#define SIGLENBIT(siglen) ((siglen) * BITS_PER_BYTE)
#define GET_SIGLEN() (PG_HAS_OPCLASS_OPTIONS() ? \
((GISTIntArrayBigOptions *) PG_GET_OPCLASS_OPTIONS())->siglen : \
SIGLEN_DEFAULT)
typedef char BITVEC[SIGLEN];
typedef char *BITVECP;
#define LOOPBYTE \
for(i=0;i<SIGLEN;i++)
#define LOOPBYTE(siglen) \
for (i = 0; i < siglen; i++)
/* beware of multiple evaluation of arguments to these macros! */
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
@ -63,8 +77,15 @@ typedef char *BITVECP;
#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
#define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen))
#define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen))
/* gist_intbig_ops opclass options */
typedef struct
{
int32 vl_len_; /* varlena header (do not touch directly!) */
int siglen; /* signature length in bytes */
} GISTIntArrayBigOptions;
/*
* type of index key
@ -81,7 +102,7 @@ typedef struct
#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
#define GTHDRSIZE (VARHDRSZ + sizeof(int32))
#define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
#define CALCGTSIZE(flag, siglen) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : (siglen)) )
#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
@ -103,7 +124,7 @@ bool inner_int_contains(ArrayType *a, ArrayType *b);
ArrayType *inner_int_union(ArrayType *a, ArrayType *b);
ArrayType *inner_int_inter(ArrayType *a, ArrayType *b);
void rt__int_size(ArrayType *a, float *size);
void gensign(BITVEC sign, int *a, int len);
void gensign(BITVECP sign, int *a, int len, int siglen);
/*****************************************************************************
@ -149,7 +170,7 @@ typedef struct QUERYTYPE
#define PG_GETARG_QUERYTYPE_P(n) DatumGetQueryTypeP(PG_GETARG_DATUM(n))
#define PG_GETARG_QUERYTYPE_P_COPY(n) DatumGetQueryTypePCopy(PG_GETARG_DATUM(n))
bool signconsistent(QUERYTYPE *query, BITVEC sign, bool calcnot);
bool signconsistent(QUERYTYPE *query, BITVECP sign, int siglen, bool calcnot);
bool execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot);
bool gin_bool_consistent(QUERYTYPE *query, bool *check);

View File

@ -232,7 +232,7 @@ typedef struct
* is there value 'val' in (sorted) array or not ?
*/
static bool
checkcondition_arr(void *checkval, ITEM *item)
checkcondition_arr(void *checkval, ITEM *item, void *options)
{
int32 *StopLow = ((CHKVAL *) checkval)->arrb;
int32 *StopHigh = ((CHKVAL *) checkval)->arre;
@ -254,42 +254,42 @@ checkcondition_arr(void *checkval, ITEM *item)
}
static bool
checkcondition_bit(void *checkval, ITEM *item)
checkcondition_bit(void *checkval, ITEM *item, void *siglen)
{
return GETBIT(checkval, HASHVAL(item->val));
return GETBIT(checkval, HASHVAL(item->val, (int)(intptr_t) siglen));
}
/*
* evaluate boolean expression, using chkcond() to test the primitive cases
*/
static bool
execute(ITEM *curitem, void *checkval, bool calcnot,
bool (*chkcond) (void *checkval, ITEM *item))
execute(ITEM *curitem, void *checkval, void *options, bool calcnot,
bool (*chkcond) (void *checkval, ITEM *item, void *options))
{
/* since this function recurses, it could be driven to stack overflow */
check_stack_depth();
if (curitem->type == VAL)
return (*chkcond) (checkval, curitem);
return (*chkcond) (checkval, curitem, options);
else if (curitem->val == (int32) '!')
{
return calcnot ?
((execute(curitem - 1, checkval, calcnot, chkcond)) ? false : true)
((execute(curitem - 1, checkval, options, calcnot, chkcond)) ? false : true)
: true;
}
else if (curitem->val == (int32) '&')
{
if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
return execute(curitem - 1, checkval, calcnot, chkcond);
if (execute(curitem + curitem->left, checkval, options, calcnot, chkcond))
return execute(curitem - 1, checkval, options, calcnot, chkcond);
else
return false;
}
else
{ /* |-operator */
if (execute(curitem + curitem->left, checkval, calcnot, chkcond))
if (execute(curitem + curitem->left, checkval, options, calcnot, chkcond))
return true;
else
return execute(curitem - 1, checkval, calcnot, chkcond);
return execute(curitem - 1, checkval, options, calcnot, chkcond);
}
}
@ -297,10 +297,10 @@ execute(ITEM *curitem, void *checkval, bool calcnot,
* signconsistent & execconsistent called by *_consistent
*/
bool
signconsistent(QUERYTYPE *query, BITVEC sign, bool calcnot)
signconsistent(QUERYTYPE *query, BITVECP sign, int siglen, bool calcnot)
{
return execute(GETQUERY(query) + query->size - 1,
(void *) sign, calcnot,
(void *) sign, (void *)(intptr_t) siglen, calcnot,
checkcondition_bit);
}
@ -314,7 +314,7 @@ execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot)
chkval.arrb = ARRPTR(array);
chkval.arre = chkval.arrb + ARRNELEMS(array);
return execute(GETQUERY(query) + query->size - 1,
(void *) &chkval, calcnot,
(void *) &chkval, NULL, calcnot,
checkcondition_arr);
}
@ -325,7 +325,7 @@ typedef struct
} GinChkVal;
static bool
checkcondition_gin(void *checkval, ITEM *item)
checkcondition_gin(void *checkval, ITEM *item, void *options)
{
GinChkVal *gcv = (GinChkVal *) checkval;
@ -356,7 +356,7 @@ gin_bool_consistent(QUERYTYPE *query, bool *check)
}
return execute(GETQUERY(query) + query->size - 1,
(void *) &gcv, true,
(void *) &gcv, NULL, true,
checkcondition_gin);
}
@ -428,7 +428,7 @@ boolop(PG_FUNCTION_ARGS)
chkval.arrb = ARRPTR(val);
chkval.arre = chkval.arrb + ARRNELEMS(val);
result = execute(GETQUERY(query) + query->size - 1,
&chkval, true,
&chkval, NULL, true,
checkcondition_arr);
pfree(val);

View File

@ -7,6 +7,7 @@
#include "_int.h"
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
#define GETENTRY(vec,pos) ((ArrayType *) DatumGetPointer((vec)->vector[(pos)].key))
@ -32,6 +33,7 @@ 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);
PG_FUNCTION_INFO_V1(g_int_options);
/*
@ -156,6 +158,7 @@ g_int_compress(PG_FUNCTION_ARGS)
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
ArrayType *r;
int num_ranges = G_INT_GET_NUMRANGES();
int len,
lenr;
int *dr;
@ -170,9 +173,9 @@ g_int_compress(PG_FUNCTION_ARGS)
CHECKARRVALID(r);
PREPAREARR(r);
if (ARRNELEMS(r) >= 2 * MAXNUMRANGE)
if (ARRNELEMS(r) >= 2 * num_ranges)
elog(NOTICE, "input array is too big (%d maximum allowed, %d current), use gist__intbig_ops opclass instead",
2 * MAXNUMRANGE - 1, ARRNELEMS(r));
2 * num_ranges - 1, ARRNELEMS(r));
retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(r),
@ -195,7 +198,7 @@ g_int_compress(PG_FUNCTION_ARGS)
PG_RETURN_POINTER(entry);
}
if ((len = ARRNELEMS(r)) >= 2 * MAXNUMRANGE)
if ((len = ARRNELEMS(r)) >= 2 * num_ranges)
{ /* compress */
if (r == (ArrayType *) DatumGetPointer(entry->key))
r = DatumGetArrayTypePCopy(entry->key);
@ -208,7 +211,7 @@ g_int_compress(PG_FUNCTION_ARGS)
* "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;
lenr = len - num_ranges;
/*
* Initially assume we can merge consecutive ints into a range. but we
@ -241,7 +244,7 @@ g_int_compress(PG_FUNCTION_ARGS)
*/
len = 2 * (len - j);
cand = 1;
while (len > MAXNUMRANGE * 2)
while (len > num_ranges * 2)
{
min = PG_INT64_MAX;
for (i = 2; i < len; i += 2)
@ -278,6 +281,7 @@ g_int_decompress(PG_FUNCTION_ARGS)
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
ArrayType *r;
int num_ranges = G_INT_GET_NUMRANGES();
int *dr,
lenr;
ArrayType *in;
@ -304,7 +308,7 @@ g_int_decompress(PG_FUNCTION_ARGS)
lenin = ARRNELEMS(in);
if (lenin < 2 * MAXNUMRANGE)
if (lenin < 2 * num_ranges)
{ /* not compressed value */
if (in != (ArrayType *) DatumGetPointer(entry->key))
{
@ -604,3 +608,17 @@ g_int_picksplit(PG_FUNCTION_ARGS)
PG_RETURN_POINTER(v);
}
Datum
g_int_options(PG_FUNCTION_ARGS)
{
local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
init_local_reloptions(relopts, sizeof(GISTIntArrayOptions));
add_local_int_reloption(relopts, "numranges",
"number of ranges for compression",
G_INT_NUMRANGES_DEFAULT, 1, G_INT_NUMRANGES_MAX,
offsetof(GISTIntArrayOptions, num_ranges));
PG_RETURN_VOID();
}

View File

@ -319,14 +319,14 @@ _int_unique(ArrayType *r)
}
void
gensign(BITVEC sign, int *a, int len)
gensign(BITVECP sign, int *a, int len, int siglen)
{
int i;
/* we assume that the sign vector is previously zeroed */
for (i = 0; i < len; i++)
{
HASH(sign, *a);
HASH(sign, *a, siglen);
a++;
}
}

View File

@ -5,6 +5,7 @@
#include "_int.h"
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "port/pg_bitutils.h"
@ -19,6 +20,8 @@ PG_FUNCTION_INFO_V1(g_intbig_penalty);
PG_FUNCTION_INFO_V1(g_intbig_picksplit);
PG_FUNCTION_INFO_V1(g_intbig_union);
PG_FUNCTION_INFO_V1(g_intbig_same);
PG_FUNCTION_INFO_V1(g_intbig_options);
PG_FUNCTION_INFO_V1(_intbig_in);
PG_FUNCTION_INFO_V1(_intbig_out);
@ -40,12 +43,33 @@ _intbig_out(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(0);
}
static GISTTYPE *
_intbig_alloc(bool allistrue, int siglen, BITVECP sign)
{
int flag = allistrue ? ALLISTRUE : 0;
int size = CALCGTSIZE(flag, siglen);
GISTTYPE *res = (GISTTYPE *) palloc(size);
SET_VARSIZE(res, size);
res->flag = flag;
if (!allistrue)
{
if (sign)
memcpy(GETSIGN(res), sign, siglen);
else
memset(GETSIGN(res), 0, siglen);
}
return res;
}
/*********************************************************************
** intbig functions
*********************************************************************/
static bool
_intbig_overlap(GISTTYPE *a, ArrayType *b)
_intbig_overlap(GISTTYPE *a, ArrayType *b, int siglen)
{
int num = ARRNELEMS(b);
int32 *ptr = ARRPTR(b);
@ -54,7 +78,7 @@ _intbig_overlap(GISTTYPE *a, ArrayType *b)
while (num--)
{
if (GETBIT(GETSIGN(a), HASHVAL(*ptr)))
if (GETBIT(GETSIGN(a), HASHVAL(*ptr, siglen)))
return true;
ptr++;
}
@ -63,7 +87,7 @@ _intbig_overlap(GISTTYPE *a, ArrayType *b)
}
static bool
_intbig_contains(GISTTYPE *a, ArrayType *b)
_intbig_contains(GISTTYPE *a, ArrayType *b, int siglen)
{
int num = ARRNELEMS(b);
int32 *ptr = ARRPTR(b);
@ -72,7 +96,7 @@ _intbig_contains(GISTTYPE *a, ArrayType *b)
while (num--)
{
if (!GETBIT(GETSIGN(a), HASHVAL(*ptr)))
if (!GETBIT(GETSIGN(a), HASHVAL(*ptr, siglen)))
return false;
ptr++;
}
@ -86,6 +110,7 @@ g_intbig_same(PG_FUNCTION_ARGS)
GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
int siglen = GET_SIGLEN();
if (ISALLTRUE(a) && ISALLTRUE(b))
*result = true;
@ -100,7 +125,7 @@ g_intbig_same(PG_FUNCTION_ARGS)
sb = GETSIGN(b);
*result = true;
LOOPBYTE
LOOPBYTE(siglen)
{
if (sa[i] != sb[i])
{
@ -116,6 +141,7 @@ Datum
g_intbig_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
int siglen = GET_SIGLEN();
if (entry->leafkey)
{
@ -123,7 +149,7 @@ g_intbig_compress(PG_FUNCTION_ARGS)
ArrayType *in = DatumGetArrayTypeP(entry->key);
int32 *ptr;
int num;
GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
GISTTYPE *res = _intbig_alloc(false, siglen, NULL);
CHECKARRVALID(in);
if (ARRISEMPTY(in))
@ -136,11 +162,10 @@ g_intbig_compress(PG_FUNCTION_ARGS)
ptr = ARRPTR(in);
num = ARRNELEMS(in);
}
SET_VARSIZE(res, CALCGTSIZE(0));
while (num--)
{
HASH(GETSIGN(res), *ptr);
HASH(GETSIGN(res), *ptr, siglen);
ptr++;
}
@ -161,16 +186,13 @@ g_intbig_compress(PG_FUNCTION_ARGS)
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
GISTTYPE *res;
LOOPBYTE
LOOPBYTE(siglen)
{
if ((sign[i] & 0xff) != 0xff)
PG_RETURN_POINTER(entry);
}
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
SET_VARSIZE(res, CALCGTSIZE(ALLISTRUE));
res->flag = ALLISTRUE;
res = _intbig_alloc(true, siglen, sign);
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(res),
entry->rel, entry->page,
@ -184,19 +206,19 @@ g_intbig_compress(PG_FUNCTION_ARGS)
static int32
sizebitvec(BITVECP sign)
sizebitvec(BITVECP sign, int siglen)
{
return pg_popcount(sign, SIGLEN);
return pg_popcount(sign, siglen);
}
static int
hemdistsign(BITVECP a, BITVECP b)
hemdistsign(BITVECP a, BITVECP b, int siglen)
{
int i,
diff,
dist = 0;
LOOPBYTE
LOOPBYTE(siglen)
{
diff = (unsigned char) (a[i] ^ b[i]);
/* Using the popcount functions here isn't likely to win */
@ -206,19 +228,19 @@ hemdistsign(BITVECP a, BITVECP b)
}
static int
hemdist(GISTTYPE *a, GISTTYPE *b)
hemdist(GISTTYPE *a, GISTTYPE *b, int siglen)
{
if (ISALLTRUE(a))
{
if (ISALLTRUE(b))
return 0;
else
return SIGLENBIT - sizebitvec(GETSIGN(b));
return SIGLENBIT(siglen) - sizebitvec(GETSIGN(b), siglen);
}
else if (ISALLTRUE(b))
return SIGLENBIT - sizebitvec(GETSIGN(a));
return SIGLENBIT(siglen) - sizebitvec(GETSIGN(a), siglen);
return hemdistsign(GETSIGN(a), GETSIGN(b));
return hemdistsign(GETSIGN(a), GETSIGN(b), siglen);
}
Datum
@ -228,14 +250,14 @@ g_intbig_decompress(PG_FUNCTION_ARGS)
}
static int32
unionkey(BITVECP sbase, GISTTYPE *add)
unionkey(BITVECP sbase, GISTTYPE *add, int siglen)
{
int32 i;
BITVECP sadd = GETSIGN(add);
if (ISALLTRUE(add))
return 1;
LOOPBYTE
LOOPBYTE(siglen)
sbase[i] |= sadd[i];
return 0;
}
@ -245,29 +267,22 @@ g_intbig_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *size = (int *) PG_GETARG_POINTER(1);
BITVEC base;
int32 i,
len;
int32 flag = 0;
GISTTYPE *result;
int siglen = GET_SIGLEN();
int32 i;
GISTTYPE *result = _intbig_alloc(false, siglen, NULL);
BITVECP base = GETSIGN(result);
MemSet((void *) base, 0, sizeof(BITVEC));
for (i = 0; i < entryvec->n; i++)
{
if (unionkey(base, GETENTRY(entryvec, i)))
if (unionkey(base, GETENTRY(entryvec, i), siglen))
{
flag = ALLISTRUE;
result->flag |= ALLISTRUE;
SET_VARSIZE(result, CALCGTSIZE(ALLISTRUE, siglen));
break;
}
}
len = CALCGTSIZE(flag);
result = (GISTTYPE *) palloc(len);
SET_VARSIZE(result, len);
result->flag = flag;
if (!ISALLTRUE(result))
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
*size = len;
*size = VARSIZE(result);
PG_RETURN_POINTER(result);
}
@ -280,8 +295,9 @@ g_intbig_penalty(PG_FUNCTION_ARGS)
float *penalty = (float *) PG_GETARG_POINTER(2);
GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
int siglen = GET_SIGLEN();
*penalty = hemdist(origval, newval);
*penalty = hemdist(origval, newval, siglen);
PG_RETURN_POINTER(penalty);
}
@ -304,6 +320,7 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
int siglen = GET_SIGLEN();
OffsetNumber k,
j;
GISTTYPE *datum_l,
@ -336,7 +353,7 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
_k = GETENTRY(entryvec, k);
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
{
size_waste = hemdist(_k, GETENTRY(entryvec, j));
size_waste = hemdist(_k, GETENTRY(entryvec, j), siglen);
if (size_waste > waste)
{
waste = size_waste;
@ -358,32 +375,10 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
}
/* form initial .. */
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
SET_VARSIZE(datum_l, GTHDRSIZE);
datum_l->flag = ALLISTRUE;
}
else
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
SET_VARSIZE(datum_l, GTHDRSIZE + SIGLEN);
datum_l->flag = 0;
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC));
}
if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
SET_VARSIZE(datum_r, GTHDRSIZE);
datum_r->flag = ALLISTRUE;
}
else
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
SET_VARSIZE(datum_r, GTHDRSIZE + SIGLEN);
datum_r->flag = 0;
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
}
datum_l = _intbig_alloc(ISALLTRUE(GETENTRY(entryvec, seed_1)), siglen,
GETSIGN(GETENTRY(entryvec, seed_1)));
datum_r = _intbig_alloc(ISALLTRUE(GETENTRY(entryvec, seed_2)), siglen,
GETSIGN(GETENTRY(entryvec, seed_2)));
maxoff = OffsetNumberNext(maxoff);
/* sort before ... */
@ -392,8 +387,8 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
{
costvector[j - 1].pos = j;
_j = GETENTRY(entryvec, j);
size_alpha = hemdist(datum_l, _j);
size_beta = hemdist(datum_r, _j);
size_alpha = hemdist(datum_l, _j, siglen);
size_beta = hemdist(datum_r, _j, siglen);
costvector[j - 1].cost = Abs(size_alpha - size_beta);
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
@ -417,20 +412,20 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
continue;
}
_j = GETENTRY(entryvec, j);
size_alpha = hemdist(datum_l, _j);
size_beta = hemdist(datum_r, _j);
size_alpha = hemdist(datum_l, _j, siglen);
size_beta = hemdist(datum_r, _j, siglen);
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
{
if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
{
if (!ISALLTRUE(datum_l))
MemSet((void *) union_l, 0xff, sizeof(BITVEC));
MemSet((void *) union_l, 0xff, siglen);
}
else
{
ptr = GETSIGN(_j);
LOOPBYTE
LOOPBYTE(siglen)
union_l[i] |= ptr[i];
}
*left++ = j;
@ -441,12 +436,12 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
{
if (!ISALLTRUE(datum_r))
MemSet((void *) union_r, 0xff, sizeof(BITVEC));
MemSet((void *) union_r, 0xff, siglen);
}
else
{
ptr = GETSIGN(_j);
LOOPBYTE
LOOPBYTE(siglen)
union_r[i] |= ptr[i];
}
*right++ = j;
@ -472,6 +467,7 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
/* Oid subtype = PG_GETARG_OID(3); */
bool *recheck = (bool *) PG_GETARG_POINTER(4);
int siglen = GET_SIGLEN();
bool retval;
/* All cases served by this function are inexact */
@ -484,6 +480,7 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
{
retval = signconsistent((QUERYTYPE *) query,
GETSIGN(DatumGetPointer(entry->key)),
siglen,
false);
PG_FREE_IF_COPY(query, 1);
PG_RETURN_BOOL(retval);
@ -494,7 +491,8 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
switch (strategy)
{
case RTOverlapStrategyNumber:
retval = _intbig_overlap((GISTTYPE *) DatumGetPointer(entry->key), query);
retval = _intbig_overlap((GISTTYPE *) DatumGetPointer(entry->key),
query, siglen);
break;
case RTSameStrategyNumber:
if (GIST_LEAF(entry))
@ -502,22 +500,18 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
int i,
num = ARRNELEMS(query);
int32 *ptr = ARRPTR(query);
BITVEC qp;
BITVECP dq,
BITVECP dq = palloc0(siglen),
de;
memset(qp, 0, sizeof(BITVEC));
while (num--)
{
HASH(qp, *ptr);
HASH(dq, *ptr, siglen);
ptr++;
}
de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
dq = qp;
retval = true;
LOOPBYTE
LOOPBYTE(siglen)
{
if (de[i] != dq[i])
{
@ -526,13 +520,16 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
}
}
pfree(dq);
}
else
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key), query);
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key),
query, siglen);
break;
case RTContainsStrategyNumber:
case RTOldContainsStrategyNumber:
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key), query);
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key),
query, siglen);
break;
case RTContainedByStrategyNumber:
case RTOldContainedByStrategyNumber:
@ -541,22 +538,18 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
int i,
num = ARRNELEMS(query);
int32 *ptr = ARRPTR(query);
BITVEC qp;
BITVECP dq,
BITVECP dq = palloc0(siglen),
de;
memset(qp, 0, sizeof(BITVEC));
while (num--)
{
HASH(qp, *ptr);
HASH(dq, *ptr, siglen);
ptr++;
}
de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
dq = qp;
retval = true;
LOOPBYTE
LOOPBYTE(siglen)
{
if (de[i] & ~dq[i])
{
@ -580,3 +573,17 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
PG_FREE_IF_COPY(query, 1);
PG_RETURN_BOOL(retval);
}
Datum
g_intbig_options(PG_FUNCTION_ARGS)
{
local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
init_local_reloptions(relopts, sizeof(GISTIntArrayBigOptions));
add_local_int_reloption(relopts, "siglen",
"signature length in bytes",
SIGLEN_DEFAULT, 1, SIGLEN_MAX,
offsetof(GISTIntArrayBigOptions, siglen));
PG_RETURN_VOID();
}

View File

@ -547,6 +547,166 @@ SELECT count(*) from test__int WHERE a @@ '!20 & !21';
6343
(1 row)
DROP INDEX text_idx;
CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 0));
ERROR: value 0 out of bounds for option "numranges"
DETAIL: Valid values are between "1" and "252".
CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 253));
ERROR: value 253 out of bounds for option "numranges"
DETAIL: Valid values are between "1" and "252".
CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 252));
SELECT count(*) from test__int WHERE a && '{23,50}';
count
-------
403
(1 row)
SELECT count(*) from test__int WHERE a @@ '23|50';
count
-------
403
(1 row)
SELECT count(*) from test__int WHERE a @> '{23,50}';
count
-------
12
(1 row)
SELECT count(*) from test__int WHERE a @@ '23&50';
count
-------
12
(1 row)
SELECT count(*) from test__int WHERE a @> '{20,23}';
count
-------
12
(1 row)
SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
count
-------
10
(1 row)
SELECT count(*) from test__int WHERE a = '{73,23,20}';
count
-------
1
(1 row)
SELECT count(*) from test__int WHERE a @@ '50&68';
count
-------
9
(1 row)
SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
count
-------
21
(1 row)
SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
count
-------
21
(1 row)
SELECT count(*) from test__int WHERE a @@ '20 | !21';
count
-------
6566
(1 row)
SELECT count(*) from test__int WHERE a @@ '!20 & !21';
count
-------
6343
(1 row)
DROP INDEX text_idx;
CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 0));
ERROR: value 0 out of bounds for option "siglen"
DETAIL: Valid values are between "1" and "2024".
CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 2025));
ERROR: value 2025 out of bounds for option "siglen"
DETAIL: Valid values are between "1" and "2024".
CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 2024));
SELECT count(*) from test__int WHERE a && '{23,50}';
count
-------
403
(1 row)
SELECT count(*) from test__int WHERE a @@ '23|50';
count
-------
403
(1 row)
SELECT count(*) from test__int WHERE a @> '{23,50}';
count
-------
12
(1 row)
SELECT count(*) from test__int WHERE a @@ '23&50';
count
-------
12
(1 row)
SELECT count(*) from test__int WHERE a @> '{20,23}';
count
-------
12
(1 row)
SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
count
-------
10
(1 row)
SELECT count(*) from test__int WHERE a = '{73,23,20}';
count
-------
1
(1 row)
SELECT count(*) from test__int WHERE a @@ '50&68';
count
-------
9
(1 row)
SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
count
-------
21
(1 row)
SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
count
-------
21
(1 row)
SELECT count(*) from test__int WHERE a @@ '20 | !21';
count
-------
6566
(1 row)
SELECT count(*) from test__int WHERE a @@ '!20 & !21';
count
-------
6343
(1 row)
DROP INDEX text_idx;
CREATE INDEX text_idx on test__int using gist ( a gist__intbig_ops );
SELECT count(*) from test__int WHERE a && '{23,50}';

View File

@ -0,0 +1,20 @@
/* contrib/intarray/intarray--1.2--1.3.sql */
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
\echo Use "ALTER EXTENSION intarray UPDATE TO '1.3'" to load this file. \quit
CREATE FUNCTION g_int_options(internal)
RETURNS void
AS 'MODULE_PATHNAME', 'g_int_options'
LANGUAGE C IMMUTABLE PARALLEL SAFE;
CREATE FUNCTION g_intbig_options(internal)
RETURNS void
AS 'MODULE_PATHNAME', 'g_intbig_options'
LANGUAGE C IMMUTABLE PARALLEL SAFE;
ALTER OPERATOR FAMILY gist__int_ops USING gist
ADD FUNCTION 10 (_int4) g_int_options (internal);
ALTER OPERATOR FAMILY gist__intbig_ops USING gist
ADD FUNCTION 10 (_int4) g_intbig_options (internal);

View File

@ -1,6 +1,6 @@
# intarray extension
comment = 'functions, operators, and index support for 1-D arrays of integers'
default_version = '1.2'
default_version = '1.3'
module_pathname = '$libdir/_int'
relocatable = true
trusted = true

View File

@ -110,6 +110,42 @@ SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
SELECT count(*) from test__int WHERE a @@ '20 | !21';
SELECT count(*) from test__int WHERE a @@ '!20 & !21';
DROP INDEX text_idx;
CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 0));
CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 253));
CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 252));
SELECT count(*) from test__int WHERE a && '{23,50}';
SELECT count(*) from test__int WHERE a @@ '23|50';
SELECT count(*) from test__int WHERE a @> '{23,50}';
SELECT count(*) from test__int WHERE a @@ '23&50';
SELECT count(*) from test__int WHERE a @> '{20,23}';
SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
SELECT count(*) from test__int WHERE a = '{73,23,20}';
SELECT count(*) from test__int WHERE a @@ '50&68';
SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
SELECT count(*) from test__int WHERE a @@ '20 | !21';
SELECT count(*) from test__int WHERE a @@ '!20 & !21';
DROP INDEX text_idx;
CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 0));
CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 2025));
CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 2024));
SELECT count(*) from test__int WHERE a && '{23,50}';
SELECT count(*) from test__int WHERE a @@ '23|50';
SELECT count(*) from test__int WHERE a @> '{23,50}';
SELECT count(*) from test__int WHERE a @@ '23&50';
SELECT count(*) from test__int WHERE a @> '{20,23}';
SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
SELECT count(*) from test__int WHERE a = '{73,23,20}';
SELECT count(*) from test__int WHERE a @@ '50&68';
SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
SELECT count(*) from test__int WHERE a @@ '20 | !21';
SELECT count(*) from test__int WHERE a @@ '!20 & !21';
DROP INDEX text_idx;
CREATE INDEX text_idx on test__int using gist ( a gist__intbig_ops );