1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-31 17:02:12 +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

@@ -480,6 +480,7 @@ static void add_cast_to(StringInfo buf, Oid typid);
static char *generate_qualified_type_name(Oid typid);
static text *string_to_text(char *str);
static char *flatten_reloptions(Oid relid);
static void get_reloptions(StringInfo buf, Datum reloptions);
#define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
@@ -1384,6 +1385,8 @@ pg_get_indexdef_worker(Oid indexrelid, int colno,
{
int16 opt = indoption->values[keyno];
Oid indcoll = indcollation->values[keyno];
Datum attoptions = get_attoptions(indexrelid, keyno + 1);
bool has_options = attoptions != (Datum) 0;
/* Add collation, if not default for column */
if (OidIsValid(indcoll) && indcoll != keycolcollation)
@@ -1391,7 +1394,15 @@ pg_get_indexdef_worker(Oid indexrelid, int colno,
generate_collation_name((indcoll)));
/* Add the operator class name, if not default */
get_opclass_name(indclass->values[keyno], keycoltype, &buf);
get_opclass_name(indclass->values[keyno],
has_options ? InvalidOid : keycoltype, &buf);
if (has_options)
{
appendStringInfoString(&buf, " (");
get_reloptions(&buf, attoptions);
appendStringInfoChar(&buf, ')');
}
/* Add options if relevant */
if (amroutine->amcanorder)
@@ -10573,6 +10584,23 @@ get_opclass_name(Oid opclass, Oid actual_datatype,
ReleaseSysCache(ht_opc);
}
/*
* generate_opclass_name
* Compute the name to display for a opclass specified by OID
*
* The result includes all necessary quoting and schema-prefixing.
*/
char *
generate_opclass_name(Oid opclass)
{
StringInfoData buf;
initStringInfo(&buf);
get_opclass_name(opclass, InvalidOid, &buf);
return &buf.data[1]; /* get_opclass_name() prepends space */
}
/*
* processIndirection - take care of array and subfield assignment
*
@@ -11250,6 +11278,62 @@ string_to_text(char *str)
return result;
}
/*
* Generate a C string representing a relation options from text[] datum.
*/
static void
get_reloptions(StringInfo buf, Datum reloptions)
{
Datum *options;
int noptions;
int i;
deconstruct_array(DatumGetArrayTypeP(reloptions),
TEXTOID, -1, false, TYPALIGN_INT,
&options, NULL, &noptions);
for (i = 0; i < noptions; i++)
{
char *option = TextDatumGetCString(options[i]);
char *name;
char *separator;
char *value;
/*
* Each array element should have the form name=value. If the "="
* is missing for some reason, treat it like an empty value.
*/
name = option;
separator = strchr(option, '=');
if (separator)
{
*separator = '\0';
value = separator + 1;
}
else
value = "";
if (i > 0)
appendStringInfoString(buf, ", ");
appendStringInfo(buf, "%s=", quote_identifier(name));
/*
* In general we need to quote the value; but to avoid unnecessary
* clutter, do not quote if it is an identifier that would not
* need quoting. (We could also allow numbers, but that is a bit
* trickier than it looks --- for example, are leading zeroes
* significant? We don't want to assume very much here about what
* custom reloptions might mean.)
*/
if (quote_identifier(value) == value)
appendStringInfoString(buf, value);
else
simple_quote_literal(buf, value);
pfree(option);
}
}
/*
* Generate a C string representing a relation's reloptions, or NULL if none.
*/
@@ -11270,56 +11354,9 @@ flatten_reloptions(Oid relid)
if (!isnull)
{
StringInfoData buf;
Datum *options;
int noptions;
int i;
initStringInfo(&buf);
deconstruct_array(DatumGetArrayTypeP(reloptions),
TEXTOID, -1, false, TYPALIGN_INT,
&options, NULL, &noptions);
for (i = 0; i < noptions; i++)
{
char *option = TextDatumGetCString(options[i]);
char *name;
char *separator;
char *value;
/*
* Each array element should have the form name=value. If the "="
* is missing for some reason, treat it like an empty value.
*/
name = option;
separator = strchr(option, '=');
if (separator)
{
*separator = '\0';
value = separator + 1;
}
else
value = "";
if (i > 0)
appendStringInfoString(&buf, ", ");
appendStringInfo(&buf, "%s=", quote_identifier(name));
/*
* In general we need to quote the value; but to avoid unnecessary
* clutter, do not quote if it is an identifier that would not
* need quoting. (We could also allow numbers, but that is a bit
* trickier than it looks --- for example, are leading zeroes
* significant? We don't want to assume very much here about what
* custom reloptions might mean.)
*/
if (quote_identifier(value) == value)
appendStringInfoString(&buf, value);
else
simple_quote_literal(&buf, value);
pfree(option);
}
get_reloptions(&buf, reloptions);
result = buf.data;
}

View File

@@ -6367,6 +6367,7 @@ gincost_pattern(IndexOptInfo *index, int indexcol,
Oid clause_op, Datum query,
GinQualCounts *counts)
{
FmgrInfo flinfo;
Oid extractProcOid;
Oid collation;
int strategy_op;
@@ -6416,15 +6417,19 @@ gincost_pattern(IndexOptInfo *index, int indexcol,
else
collation = DEFAULT_COLLATION_OID;
OidFunctionCall7Coll(extractProcOid,
collation,
query,
PointerGetDatum(&nentries),
UInt16GetDatum(strategy_op),
PointerGetDatum(&partial_matches),
PointerGetDatum(&extra_data),
PointerGetDatum(&nullFlags),
PointerGetDatum(&searchMode));
fmgr_info(extractProcOid, &flinfo);
set_fn_opclass_options(&flinfo, index->opclassoptions[indexcol]);
FunctionCall7Coll(&flinfo,
collation,
query,
PointerGetDatum(&nentries),
UInt16GetDatum(strategy_op),
PointerGetDatum(&partial_matches),
PointerGetDatum(&extra_data),
PointerGetDatum(&nullFlags),
PointerGetDatum(&searchMode));
if (nentries <= 0 && searchMode == GIN_SEARCH_MODE_DEFAULT)
{

View File

@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -23,17 +24,25 @@
#include "utils/pg_crc.h"
#define SIGLENINT 31 /* >121 => key will toast, so it will not work
* !!! */
/* tsvector_ops opclass options */
typedef struct
{
int32 vl_len_; /* varlena header (do not touch directly!) */
int siglen; /* signature length */
} GistTsVectorOptions;
#define SIGLEN ( sizeof(int32) * SIGLENINT )
#define SIGLENBIT (SIGLEN * BITS_PER_BYTE)
#define SIGLEN_DEFAULT (31 * 4)
#define SIGLEN_MAX GISTMaxIndexKeySize
#define GET_SIGLEN() (PG_HAS_OPCLASS_OPTIONS() ? \
((GistTsVectorOptions *) PG_GET_OPCLASS_OPTIONS())->siglen : \
SIGLEN_DEFAULT)
#define SIGLENBIT(siglen) ((siglen) * BITS_PER_BYTE)
typedef char BITVEC[SIGLEN];
typedef char *BITVECP;
#define LOOPBYTE \
for(i=0;i<SIGLEN;i++)
#define LOOPBYTE(siglen) \
for (i = 0; i < siglen; i++)
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
#define GETBITBYTE(x,i) ( ((char)(x)) >> (i) & 0x01 )
@@ -41,8 +50,8 @@ typedef char *BITVECP;
#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))
#define GETENTRY(vec,pos) ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -66,13 +75,14 @@ typedef struct
#define ISALLTRUE(x) ( ((SignTSVector*)(x))->flag & ALLISTRUE )
#define GTHDRSIZE ( VARHDRSZ + sizeof(int32) )
#define CALCGTSIZE(flag, len) ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
#define CALCGTSIZE(flag, len) ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )
#define GETSIGN(x) ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
#define GETSIGLEN(x)( VARSIZE(x) - GTHDRSIZE )
#define GETARR(x) ( (int32*)( (char*)(x)+GTHDRSIZE ) )
#define ARRNELEM(x) ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )
static int32 sizebitvec(BITVECP sign);
static int32 sizebitvec(BITVECP sign, int siglen);
Datum
gtsvectorin(PG_FUNCTION_ARGS)
@@ -103,9 +113,10 @@ gtsvectorout(PG_FUNCTION_ARGS)
sprintf(outbuf, ARROUTSTR, (int) ARRNELEM(key));
else
{
int cnttrue = (ISALLTRUE(key)) ? SIGLENBIT : sizebitvec(GETSIGN(key));
int siglen = GETSIGLEN(key);
int cnttrue = (ISALLTRUE(key)) ? SIGLENBIT(siglen) : sizebitvec(GETSIGN(key), siglen);
sprintf(outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT - cnttrue);
sprintf(outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
}
PG_FREE_IF_COPY(key, 0);
@@ -124,36 +135,49 @@ compareint(const void *va, const void *vb)
}
static void
makesign(BITVECP sign, SignTSVector *a)
makesign(BITVECP sign, SignTSVector *a, int siglen)
{
int32 k,
len = ARRNELEM(a);
int32 *ptr = GETARR(a);
MemSet((void *) sign, 0, sizeof(BITVEC));
MemSet((void *) sign, 0, siglen);
for (k = 0; k < len; k++)
HASH(sign, ptr[k]);
HASH(sign, ptr[k], siglen);
}
static SignTSVector *
gtsvector_alloc(int flag, int len, BITVECP sign)
{
int size = CALCGTSIZE(flag, len);
SignTSVector *res = palloc(size);
SET_VARSIZE(res, size);
res->flag = flag;
if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
memcpy(GETSIGN(res), sign, len);
return res;
}
Datum
gtsvector_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
int siglen = GET_SIGLEN();
GISTENTRY *retval = entry;
if (entry->leafkey)
{ /* tsvector */
SignTSVector *res;
TSVector val = DatumGetTSVector(entry->key);
SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
int32 len;
int32 *arr;
WordEntry *ptr = ARRPTR(val);
char *words = STRPTR(val);
len = CALCGTSIZE(ARRKEY, val->size);
res = (SignTSVector *) palloc(len);
SET_VARSIZE(res, len);
res->flag = ARRKEY;
arr = GETARR(res);
len = val->size;
while (len--)
@@ -185,13 +209,9 @@ gtsvector_compress(PG_FUNCTION_ARGS)
/* make signature, if array is too long */
if (VARSIZE(res) > TOAST_INDEX_TARGET)
{
SignTSVector *ressign;
SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
len = CALCGTSIZE(SIGNKEY, 0);
ressign = (SignTSVector *) palloc(len);
SET_VARSIZE(ressign, len);
ressign->flag = SIGNKEY;
makesign(GETSIGN(ressign), res);
makesign(GETSIGN(ressign), res, siglen);
res = ressign;
}
@@ -203,22 +223,17 @@ gtsvector_compress(PG_FUNCTION_ARGS)
else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
!ISALLTRUE(DatumGetPointer(entry->key)))
{
int32 i,
len;
int32 i;
SignTSVector *res;
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
LOOPBYTE
LOOPBYTE(siglen)
{
if ((sign[i] & 0xff) != 0xff)
PG_RETURN_POINTER(retval);
}
len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
res = (SignTSVector *) palloc(len);
SET_VARSIZE(res, len);
res->flag = SIGNKEY | ALLISTRUE;
res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(res),
entry->rel, entry->page,
@@ -292,12 +307,14 @@ checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
static bool
checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
{
void *key = (SignTSVector *) checkval;
/*
* we are not able to find a prefix in signature tree
*/
if (val->prefix)
return true;
return GETBIT(checkval, HASHVAL(val->valcrc));
return GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key)));
}
Datum
@@ -324,7 +341,7 @@ gtsvector_consistent(PG_FUNCTION_ARGS)
/* since signature is lossy, cannot specify CALC_NOT here */
PG_RETURN_BOOL(TS_execute(GETQUERY(query),
(void *) GETSIGN(key),
key,
TS_EXEC_PHRASE_NO_POS,
checkcondition_bit));
}
@@ -342,7 +359,7 @@ gtsvector_consistent(PG_FUNCTION_ARGS)
}
static int32
unionkey(BITVECP sbase, SignTSVector *add)
unionkey(BITVECP sbase, SignTSVector *add, int siglen)
{
int32 i;
@@ -353,7 +370,9 @@ unionkey(BITVECP sbase, SignTSVector *add)
if (ISALLTRUE(add))
return 1;
LOOPBYTE
Assert(GETSIGLEN(add) == siglen);
LOOPBYTE(siglen)
sbase[i] |= sadd[i];
}
else
@@ -361,7 +380,7 @@ unionkey(BITVECP sbase, SignTSVector *add)
int32 *ptr = GETARR(add);
for (i = 0; i < ARRNELEM(add); i++)
HASH(sbase, ptr[i]);
HASH(sbase, ptr[i], siglen);
}
return 0;
}
@@ -372,30 +391,24 @@ gtsvector_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;
SignTSVector *result;
int siglen = GET_SIGLEN();
SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
BITVECP base = GETSIGN(result);
int32 i;
memset(base, 0, siglen);
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(result->flag, siglen));
break;
}
}
flag |= SIGNKEY;
len = CALCGTSIZE(flag, 0);
result = (SignTSVector *) palloc(len);
*size = len;
SET_VARSIZE(result, len);
result->flag = flag;
if (!ISALLTRUE(result))
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
*size = VARSIZE(result);
PG_RETURN_POINTER(result);
}
@@ -406,6 +419,7 @@ gtsvector_same(PG_FUNCTION_ARGS)
SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
int siglen = GET_SIGLEN();
if (ISSIGNKEY(a))
{ /* then b also ISSIGNKEY */
@@ -421,8 +435,10 @@ gtsvector_same(PG_FUNCTION_ARGS)
BITVECP sa = GETSIGN(a),
sb = GETSIGN(b);
Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
*result = true;
LOOPBYTE
LOOPBYTE(siglen)
{
if (sa[i] != sb[i])
{
@@ -459,19 +475,19 @@ gtsvector_same(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 */
@@ -483,17 +499,22 @@ hemdistsign(BITVECP a, BITVECP b)
static int
hemdist(SignTSVector *a, SignTSVector *b)
{
int siglena = GETSIGLEN(a);
int siglenb = GETSIGLEN(b);
if (ISALLTRUE(a))
{
if (ISALLTRUE(b))
return 0;
else
return SIGLENBIT - sizebitvec(GETSIGN(b));
return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
}
else if (ISALLTRUE(b))
return SIGLENBIT - sizebitvec(GETSIGN(a));
return SIGLENBIT(siglena) - sizebitvec(GETSIGN(a), siglena);
return hemdistsign(GETSIGN(a), GETSIGN(b));
Assert(siglena == siglenb);
return hemdistsign(GETSIGN(a), GETSIGN(b), siglena);
}
Datum
@@ -502,6 +523,7 @@ gtsvector_penalty(PG_FUNCTION_ARGS)
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
float *penalty = (float *) PG_GETARG_POINTER(2);
int siglen = GET_SIGLEN();
SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
BITVECP orig = GETSIGN(origval);
@@ -510,14 +532,22 @@ gtsvector_penalty(PG_FUNCTION_ARGS)
if (ISARRKEY(newval))
{
BITVEC sign;
BITVECP sign = palloc(siglen);
makesign(sign, newval);
makesign(sign, newval, siglen);
if (ISALLTRUE(origval))
*penalty = ((float) (SIGLENBIT - sizebitvec(sign))) / (float) (SIGLENBIT + 1);
{
int siglenbit = SIGLENBIT(siglen);
*penalty =
(float) (siglenbit - sizebitvec(sign, siglen)) /
(float) (siglenbit + 1);
}
else
*penalty = hemdistsign(sign, orig);
*penalty = hemdistsign(sign, orig, siglen);
pfree(sign);
}
else
*penalty = hemdist(origval, newval);
@@ -527,19 +557,19 @@ gtsvector_penalty(PG_FUNCTION_ARGS)
typedef struct
{
bool allistrue;
BITVEC sign;
BITVECP sign;
} CACHESIGN;
static void
fillcache(CACHESIGN *item, SignTSVector *key)
fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
{
item->allistrue = false;
if (ISARRKEY(key))
makesign(item->sign, key);
makesign(item->sign, key, siglen);
else if (ISALLTRUE(key))
item->allistrue = true;
else
memcpy((void *) item->sign, (void *) GETSIGN(key), sizeof(BITVEC));
memcpy((void *) item->sign, (void *) GETSIGN(key), siglen);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
@@ -563,19 +593,19 @@ comparecost(const void *va, const void *vb)
static int
hemdistcache(CACHESIGN *a, CACHESIGN *b)
hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
{
if (a->allistrue)
{
if (b->allistrue)
return 0;
else
return SIGLENBIT - sizebitvec(b->sign);
return SIGLENBIT(siglen) - sizebitvec(b->sign, siglen);
}
else if (b->allistrue)
return SIGLENBIT - sizebitvec(a->sign);
return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
return hemdistsign(a->sign, b->sign);
return hemdistsign(a->sign, b->sign, siglen);
}
Datum
@@ -583,6 +613,7 @@ gtsvector_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;
SignTSVector *datum_l,
@@ -602,6 +633,7 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
BITVECP ptr;
int i;
CACHESIGN *cache;
char *cache_sign;
SPLITCOST *costvector;
maxoff = entryvec->n - 2;
@@ -610,16 +642,22 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
v->spl_right = (OffsetNumber *) palloc(nbytes);
cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber));
cache_sign = palloc(siglen * (maxoff + 2));
for (j = 0; j < maxoff + 2; j++)
cache[j].sign = &cache_sign[siglen * j];
fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber),
siglen);
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
{
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
{
if (k == FirstOffsetNumber)
fillcache(&cache[j], GETENTRY(entryvec, j));
fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
size_waste = hemdistcache(&(cache[j]), &(cache[k]));
size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
if (size_waste > waste)
{
waste = size_waste;
@@ -641,44 +679,21 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
}
/* form initial .. */
if (cache[seed_1].allistrue)
{
datum_l = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_l->flag = SIGNKEY | ALLISTRUE;
}
else
{
datum_l = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY, 0));
SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY, 0));
datum_l->flag = SIGNKEY;
memcpy((void *) GETSIGN(datum_l), (void *) cache[seed_1].sign, sizeof(BITVEC));
}
if (cache[seed_2].allistrue)
{
datum_r = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_r->flag = SIGNKEY | ALLISTRUE;
}
else
{
datum_r = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY, 0));
SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY, 0));
datum_r->flag = SIGNKEY;
memcpy((void *) GETSIGN(datum_r), (void *) cache[seed_2].sign, sizeof(BITVEC));
}
datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
siglen, cache[seed_1].sign);
datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
siglen, cache[seed_2].sign);
union_l = GETSIGN(datum_l);
union_r = GETSIGN(datum_r);
maxoff = OffsetNumberNext(maxoff);
fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff));
fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
/* sort before ... */
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
{
costvector[j - 1].pos = j;
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
costvector[j - 1].cost = Abs(size_alpha - size_beta);
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
@@ -704,36 +719,40 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
if (ISALLTRUE(datum_l) && cache[j].allistrue)
size_alpha = 0;
else
size_alpha = SIGLENBIT - sizebitvec((cache[j].allistrue) ?
GETSIGN(datum_l) :
GETSIGN(cache[j].sign));
size_alpha = SIGLENBIT(siglen) -
sizebitvec((cache[j].allistrue) ?
GETSIGN(datum_l) :
GETSIGN(cache[j].sign),
siglen);
}
else
size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l));
size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
if (ISALLTRUE(datum_r) || cache[j].allistrue)
{
if (ISALLTRUE(datum_r) && cache[j].allistrue)
size_beta = 0;
else
size_beta = SIGLENBIT - sizebitvec((cache[j].allistrue) ?
GETSIGN(datum_r) :
GETSIGN(cache[j].sign));
size_beta = SIGLENBIT(siglen) -
sizebitvec((cache[j].allistrue) ?
GETSIGN(datum_r) :
GETSIGN(cache[j].sign),
siglen);
}
else
size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r));
size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
{
if (ISALLTRUE(datum_l) || cache[j].allistrue)
{
if (!ISALLTRUE(datum_l))
MemSet((void *) GETSIGN(datum_l), 0xff, sizeof(BITVEC));
MemSet((void *) GETSIGN(datum_l), 0xff, siglen);
}
else
{
ptr = cache[j].sign;
LOOPBYTE
LOOPBYTE(siglen)
union_l[i] |= ptr[i];
}
*left++ = j;
@@ -744,12 +763,12 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
if (ISALLTRUE(datum_r) || cache[j].allistrue)
{
if (!ISALLTRUE(datum_r))
MemSet((void *) GETSIGN(datum_r), 0xff, sizeof(BITVEC));
MemSet((void *) GETSIGN(datum_r), 0xff, siglen);
}
else
{
ptr = cache[j].sign;
LOOPBYTE
LOOPBYTE(siglen)
union_r[i] |= ptr[i];
}
*right++ = j;
@@ -776,3 +795,16 @@ gtsvector_consistent_oldsig(PG_FUNCTION_ARGS)
{
return gtsvector_consistent(fcinfo);
}
Datum
gtsvector_options(PG_FUNCTION_ARGS)
{
local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
add_local_int_reloption(relopts, "siglen", "signature length",
SIGLEN_DEFAULT, 1, SIGLEN_MAX,
offsetof(GistTsVectorOptions, siglen));
PG_RETURN_VOID();
}

View File

@@ -909,6 +909,41 @@ get_atttypetypmodcoll(Oid relid, AttrNumber attnum,
ReleaseSysCache(tp);
}
/*
* get_attoptions
*
* Given the relation id and the attribute number,
* return the attribute options text[] datum, if any.
*/
Datum
get_attoptions(Oid relid, int16 attnum)
{
HeapTuple tuple;
Datum attopts;
Datum result;
bool isnull;
tuple = SearchSysCache2(ATTNUM,
ObjectIdGetDatum(relid),
Int16GetDatum(attnum));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
attnum, relid);
attopts = SysCacheGetAttr(ATTNAME, tuple, Anum_pg_attribute_attoptions,
&isnull);
if (isnull)
result = (Datum) 0;
else
result = datumCopy(attopts, false, -1); /* text[] */
ReleaseSysCache(tuple);
return result;
}
/* ---------- PG_CAST CACHE ---------- */
/*

View File

@@ -1426,7 +1426,7 @@ RelationInitIndexAccessInfo(Relation relation)
amsupport = relation->rd_indam->amsupport;
if (amsupport > 0)
{
int nsupport = indnatts * amsupport;
int nsupport = indnatts * (amsupport + 1);
relation->rd_support = (RegProcedure *)
MemoryContextAllocZero(indexcxt, nsupport * sizeof(RegProcedure));
@@ -1490,6 +1490,8 @@ RelationInitIndexAccessInfo(Relation relation)
indoption = (int2vector *) DatumGetPointer(indoptionDatum);
memcpy(relation->rd_indoption, indoption->values, indnkeyatts * sizeof(int16));
(void) RelationGetIndexAttOptions(relation, false);
/*
* expressions, predicate, exclusion caches will be filled later
*/
@@ -1539,9 +1541,9 @@ IndexSupportInitialize(oidvector *indclass,
opFamily[attIndex] = opcentry->opcfamily;
opcInType[attIndex] = opcentry->opcintype;
if (maxSupportNumber > 0)
memcpy(&indexSupport[attIndex * maxSupportNumber],
memcpy(&indexSupport[attIndex * (maxSupportNumber + 1)],
opcentry->supportProcs,
maxSupportNumber * sizeof(RegProcedure));
(maxSupportNumber + 1) * sizeof(RegProcedure));
}
}
@@ -1606,7 +1608,7 @@ LookupOpclassInfo(Oid operatorClassOid,
if (numSupport > 0)
opcentry->supportProcs = (RegProcedure *)
MemoryContextAllocZero(CacheMemoryContext,
numSupport * sizeof(RegProcedure));
(numSupport + 1) * sizeof(RegProcedure));
else
opcentry->supportProcs = NULL;
}
@@ -1693,13 +1695,12 @@ LookupOpclassInfo(Oid operatorClassOid,
{
Form_pg_amproc amprocform = (Form_pg_amproc) GETSTRUCT(htup);
if (amprocform->amprocnum <= 0 ||
if (amprocform->amprocnum < 0 ||
(StrategyNumber) amprocform->amprocnum > numSupport)
elog(ERROR, "invalid amproc number %d for opclass %u",
amprocform->amprocnum, operatorClassOid);
opcentry->supportProcs[amprocform->amprocnum - 1] =
amprocform->amproc;
opcentry->supportProcs[amprocform->amprocnum] = amprocform->amproc;
}
systable_endscan(scan);
@@ -3980,6 +3981,8 @@ load_critical_index(Oid indexoid, Oid heapoid)
ird->rd_refcnt = 1;
UnlockRelationOid(indexoid, AccessShareLock);
UnlockRelationOid(heapoid, AccessShareLock);
(void) RelationGetIndexAttOptions(ird, false);
}
/*
@@ -5185,6 +5188,100 @@ GetRelationPublicationActions(Relation relation)
return pubactions;
}
/*
* RelationGetIndexRawAttOptions -- get AM/opclass-specific options for the index
*/
Datum *
RelationGetIndexRawAttOptions(Relation indexrel)
{
Oid indexrelid = RelationGetRelid(indexrel);
int16 natts = RelationGetNumberOfAttributes(indexrel);
Datum *options = NULL;
int16 attnum;
for (attnum = 1; attnum <= natts; attnum++)
{
if (!OidIsValid(index_getprocid(indexrel, attnum,
indexrel->rd_indam->amoptsprocnum)))
continue;
if (!options)
options = palloc0(sizeof(Datum) * natts);
options[attnum - 1] = get_attoptions(indexrelid, attnum);
}
return options;
}
static bytea **
CopyIndexAttOptions(bytea **srcopts, int natts)
{
bytea **opts = palloc(sizeof(*opts) * natts);
for (int i = 0; i < natts; i++)
{
bytea *opt = srcopts[i];
opts[i] = !opt ? NULL : (bytea *)
DatumGetPointer(datumCopy(PointerGetDatum(opt), false, -1));
}
return opts;
}
/*
* RelationGetIndexAttOptions
* get AM/opclass-specific options for an index parsed into a binary form
*/
bytea **
RelationGetIndexAttOptions(Relation relation, bool copy)
{
MemoryContext oldcxt;
bytea **opts = relation->rd_opcoptions;
Oid relid = RelationGetRelid(relation);
int natts = RelationGetNumberOfAttributes(relation); /* XXX IndexRelationGetNumberOfKeyAttributes */
int i;
/* Try to copy cached options. */
if (opts)
return copy ? CopyIndexAttOptions(opts, natts) : opts;
/* Get and parse opclass options. */
opts = palloc0(sizeof(*opts) * natts);
for (i = 0; i < natts; i++)
{
if (criticalRelcachesBuilt && relid != AttributeRelidNumIndexId)
{
Datum attoptions = get_attoptions(relid, i + 1);
opts[i] = index_opclass_options(relation, i + 1, attoptions, false);
if (attoptions != (Datum) 0)
pfree(DatumGetPointer(attoptions));
}
}
/* Copy parsed options to the cache. */
oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
relation->rd_opcoptions = CopyIndexAttOptions(opts, natts);
MemoryContextSwitchTo(oldcxt);
if (copy)
return opts;
for (i = 0; i < natts; i++)
{
if (opts[i])
pfree(opts[i]);
}
pfree(opts);
return relation->rd_opcoptions;
}
/*
* Routines to support ereport() reports of relation-related errors
*
@@ -5546,8 +5643,25 @@ load_relcache_init_file(bool shared)
rel->rd_indoption = indoption;
/* finally, read the vector of opcoptions values */
rel->rd_opcoptions = (bytea **)
MemoryContextAllocZero(indexcxt, sizeof(*rel->rd_opcoptions) * relform->relnatts);
for (i = 0; i < relform->relnatts; i++)
{
if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
goto read_failed;
if (len > 0)
{
rel->rd_opcoptions[i] = (bytea *) MemoryContextAlloc(indexcxt, len);
if (fread(rel->rd_opcoptions[i], 1, len, fp) != len)
goto read_failed;
}
}
/* set up zeroed fmgr-info vector */
nsupport = relform->relnatts * rel->rd_indam->amsupport;
nsupport = relform->relnatts * (rel->rd_indam->amsupport + 1);
rel->rd_supportinfo = (FmgrInfo *)
MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
}
@@ -5574,6 +5688,7 @@ load_relcache_init_file(bool shared)
Assert(rel->rd_supportinfo == NULL);
Assert(rel->rd_indoption == NULL);
Assert(rel->rd_indcollation == NULL);
Assert(rel->rd_opcoptions == NULL);
}
/*
@@ -5847,7 +5962,7 @@ write_relcache_init_file(bool shared)
/* next, write the vector of support procedure OIDs */
write_item(rel->rd_support,
relform->relnatts * (rel->rd_indam->amsupport * sizeof(RegProcedure)),
relform->relnatts * ((rel->rd_indam->amsupport + 1) * sizeof(RegProcedure)),
fp);
/* next, write the vector of collation OIDs */
@@ -5859,6 +5974,16 @@ write_relcache_init_file(bool shared)
write_item(rel->rd_indoption,
relform->relnatts * sizeof(int16),
fp);
Assert(rel->rd_opcoptions);
/* finally, write the vector of opcoptions values */
for (i = 0; i < relform->relnatts; i++)
{
bytea *opt = rel->rd_opcoptions[i];
write_item(opt, opt ? VARSIZE(opt) : 0, fp);
}
}
}

View File

@@ -18,9 +18,11 @@
#include "access/detoast.h"
#include "catalog/pg_language.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/functions.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "pgstat.h"
#include "utils/acl.h"
@@ -1952,6 +1954,57 @@ get_fn_expr_variadic(FmgrInfo *flinfo)
return false;
}
/*
* Set options to FmgrInfo of opclass support function.
*
* Opclass support functions are called outside of expressions. Thanks to that
* we can use fn_expr to store opclass options as bytea constant.
*/
void
set_fn_opclass_options(FmgrInfo *flinfo, bytea *options)
{
flinfo->fn_expr = (Node *) makeConst(BYTEAOID, -1, InvalidOid, -1,
PointerGetDatum(options),
options == NULL, false);
}
/*
* Check if options are defined for opclass support function.
*/
bool
has_fn_opclass_options(FmgrInfo *flinfo)
{
if (flinfo && flinfo->fn_expr && IsA(flinfo->fn_expr, Const))
{
Const *expr = (Const *) flinfo->fn_expr;
if (expr->consttype == BYTEAOID)
return !expr->constisnull;
}
return false;
}
/*
* Get options for opclass support function.
*/
bytea *
get_fn_opclass_options(FmgrInfo *flinfo)
{
if (flinfo && flinfo->fn_expr && IsA(flinfo->fn_expr, Const))
{
Const *expr = (Const *) flinfo->fn_expr;
if (expr->consttype == BYTEAOID)
return expr->constisnull ? NULL : DatumGetByteaP(expr->constvalue);
}
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("opclass options info is absent in function call context")));
return NULL;
}
/*-------------------------------------------------------------------------
* Support routines for procedural language implementations
*-------------------------------------------------------------------------