1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +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

@ -701,6 +701,47 @@ add_reloption(relopt_gen *newoption)
need_initialization = true;
}
/*
* init_local_reloptions
* Initialize local reloptions that will parsed into bytea structure of
* 'relopt_struct_size'.
*/
void
init_local_reloptions(local_relopts *opts, Size relopt_struct_size)
{
opts->options = NIL;
opts->validators = NIL;
opts->relopt_struct_size = relopt_struct_size;
}
/*
* register_reloptions_validator
* Register custom validation callback that will be called at the end of
* build_local_reloptions().
*/
void
register_reloptions_validator(local_relopts *opts, relopts_validator validator)
{
opts->validators = lappend(opts->validators, validator);
}
/*
* add_local_reloption
* Add an already-created custom reloption to the local list.
*/
static void
add_local_reloption(local_relopts *relopts, relopt_gen *newoption, int offset)
{
local_relopt *opt = palloc(sizeof(*opt));
Assert(offset < relopts->relopt_struct_size);
opt->option = newoption;
opt->offset = offset;
relopts->options = lappend(relopts->options, opt);
}
/*
* allocate_reloption
* Allocate a new reloption and initialize the type-agnostic fields
@ -714,7 +755,10 @@ allocate_reloption(bits32 kinds, int type, const char *name, const char *desc,
size_t size;
relopt_gen *newoption;
oldcxt = MemoryContextSwitchTo(TopMemoryContext);
if (kinds != RELOPT_KIND_LOCAL)
oldcxt = MemoryContextSwitchTo(TopMemoryContext);
else
oldcxt = NULL;
switch (type)
{
@ -750,7 +794,25 @@ allocate_reloption(bits32 kinds, int type, const char *name, const char *desc,
newoption->type = type;
newoption->lockmode = lockmode;
MemoryContextSwitchTo(oldcxt);
if (oldcxt != NULL)
MemoryContextSwitchTo(oldcxt);
return newoption;
}
/*
* init_bool_reloption
* Allocate and initialize a new boolean reloption
*/
static relopt_bool *
init_bool_reloption(bits32 kinds, const char *name, const char *desc,
bool default_val, LOCKMODE lockmode)
{
relopt_bool *newoption;
newoption = (relopt_bool *) allocate_reloption(kinds, RELOPT_TYPE_BOOL,
name, desc, lockmode);
newoption->default_val = default_val;
return newoption;
}
@ -763,15 +825,50 @@ void
add_bool_reloption(bits32 kinds, const char *name, const char *desc,
bool default_val, LOCKMODE lockmode)
{
relopt_bool *newoption;
newoption = (relopt_bool *) allocate_reloption(kinds, RELOPT_TYPE_BOOL,
name, desc, lockmode);
newoption->default_val = default_val;
relopt_bool *newoption = init_bool_reloption(kinds, name, desc,
default_val, lockmode);
add_reloption((relopt_gen *) newoption);
}
/*
* add_local_bool_reloption
* Add a new boolean local reloption
*
* 'offset' is offset of bool-typed field.
*/
void
add_local_bool_reloption(local_relopts *relopts, const char *name,
const char *desc, bool default_val, int offset)
{
relopt_bool *newoption = init_bool_reloption(RELOPT_KIND_LOCAL,
name, desc,
default_val, 0);
add_local_reloption(relopts, (relopt_gen *) newoption, offset);
}
/*
* init_real_reloption
* Allocate and initialize a new integer reloption
*/
static relopt_int *
init_int_reloption(bits32 kinds, const char *name, const char *desc,
int default_val, int min_val, int max_val,
LOCKMODE lockmode)
{
relopt_int *newoption;
newoption = (relopt_int *) allocate_reloption(kinds, RELOPT_TYPE_INT,
name, desc, lockmode);
newoption->default_val = default_val;
newoption->min = min_val;
newoption->max = max_val;
return newoption;
}
/*
* add_int_reloption
* Add a new integer reloption
@ -780,24 +877,39 @@ void
add_int_reloption(bits32 kinds, const char *name, const char *desc, int default_val,
int min_val, int max_val, LOCKMODE lockmode)
{
relopt_int *newoption;
newoption = (relopt_int *) allocate_reloption(kinds, RELOPT_TYPE_INT,
name, desc, lockmode);
newoption->default_val = default_val;
newoption->min = min_val;
newoption->max = max_val;
relopt_int *newoption = init_int_reloption(kinds, name, desc,
default_val, min_val,
max_val, lockmode);
add_reloption((relopt_gen *) newoption);
}
/*
* add_real_reloption
* Add a new float reloption
* add_local_int_reloption
* Add a new local integer reloption
*
* 'offset' is offset of int-typed field.
*/
void
add_real_reloption(bits32 kinds, const char *name, const char *desc, double default_val,
double min_val, double max_val, LOCKMODE lockmode)
add_local_int_reloption(local_relopts *relopts, const char *name,
const char *desc, int default_val, int min_val,
int max_val, int offset)
{
relopt_int *newoption = init_int_reloption(RELOPT_KIND_LOCAL,
name, desc, default_val,
min_val, max_val, 0);
add_local_reloption(relopts, (relopt_gen *) newoption, offset);
}
/*
* init_real_reloption
* Allocate and initialize a new real reloption
*/
static relopt_real *
init_real_reloption(bits32 kinds, const char *name, const char *desc,
double default_val, double min_val, double max_val,
LOCKMODE lockmode)
{
relopt_real *newoption;
@ -807,9 +919,65 @@ add_real_reloption(bits32 kinds, const char *name, const char *desc, double defa
newoption->min = min_val;
newoption->max = max_val;
return newoption;
}
/*
* add_real_reloption
* Add a new float reloption
*/
void
add_real_reloption(bits32 kinds, const char *name, const char *desc,
double default_val, double min_val, double max_val,
LOCKMODE lockmode)
{
relopt_real *newoption = init_real_reloption(kinds, name, desc,
default_val, min_val,
max_val, lockmode);
add_reloption((relopt_gen *) newoption);
}
/*
* add_local_real_reloption
* Add a new local float reloption
*
* 'offset' is offset of double-typed field.
*/
void
add_local_real_reloption(local_relopts *relopts, const char *name,
const char *desc, double default_val,
double min_val, double max_val, int offset)
{
relopt_real *newoption = init_real_reloption(RELOPT_KIND_LOCAL,
name, desc,
default_val, min_val,
max_val, 0);
add_local_reloption(relopts, (relopt_gen *) newoption, offset);
}
/*
* init_enum_reloption
* Allocate and initialize a new enum reloption
*/
static relopt_enum *
init_enum_reloption(bits32 kinds, const char *name, const char *desc,
relopt_enum_elt_def *members, int default_val,
const char *detailmsg, LOCKMODE lockmode)
{
relopt_enum *newoption;
newoption = (relopt_enum *) allocate_reloption(kinds, RELOPT_TYPE_ENUM,
name, desc, lockmode);
newoption->members = members;
newoption->default_val = default_val;
newoption->detailmsg = detailmsg;
return newoption;
}
/*
* add_enum_reloption
* Add a new enum reloption
@ -827,17 +995,72 @@ add_enum_reloption(bits32 kinds, const char *name, const char *desc,
relopt_enum_elt_def *members, int default_val,
const char *detailmsg, LOCKMODE lockmode)
{
relopt_enum *newoption;
newoption = (relopt_enum *) allocate_reloption(kinds, RELOPT_TYPE_ENUM,
name, desc, lockmode);
newoption->members = members;
newoption->default_val = default_val;
newoption->detailmsg = detailmsg;
relopt_enum *newoption = init_enum_reloption(kinds, name, desc,
members, default_val,
detailmsg, lockmode);
add_reloption((relopt_gen *) newoption);
}
/*
* add_local_enum_reloption
* Add a new local enum reloption
*
* 'offset' is offset of int-typed field.
*/
void
add_local_enum_reloption(local_relopts *relopts, const char *name,
const char *desc, relopt_enum_elt_def *members,
int default_val, const char *detailmsg, int offset)
{
relopt_enum *newoption = init_enum_reloption(RELOPT_KIND_LOCAL,
name, desc,
members, default_val,
detailmsg, 0);
add_local_reloption(relopts, (relopt_gen *) newoption, offset);
}
/*
* init_string_reloption
* Allocate and initialize a new string reloption
*/
static relopt_string *
init_string_reloption(bits32 kinds, const char *name, const char *desc,
const char *default_val,
validate_string_relopt validator,
fill_string_relopt filler,
LOCKMODE lockmode)
{
relopt_string *newoption;
/* make sure the validator/default combination is sane */
if (validator)
(validator) (default_val);
newoption = (relopt_string *) allocate_reloption(kinds, RELOPT_TYPE_STRING,
name, desc, lockmode);
newoption->validate_cb = validator;
newoption->fill_cb = filler;
if (default_val)
{
if (kinds == RELOPT_KIND_LOCAL)
newoption->default_val = strdup(default_val);
else
newoption->default_val = MemoryContextStrdup(TopMemoryContext, default_val);
newoption->default_len = strlen(default_val);
newoption->default_isnull = false;
}
else
{
newoption->default_val = "";
newoption->default_len = 0;
newoption->default_isnull = true;
}
return newoption;
}
/*
* add_string_reloption
* Add a new string reloption
@ -848,35 +1071,40 @@ add_enum_reloption(bits32 kinds, const char *name, const char *desc,
* the validation.
*/
void
add_string_reloption(bits32 kinds, const char *name, const char *desc, const char *default_val,
validate_string_relopt validator, LOCKMODE lockmode)
add_string_reloption(bits32 kinds, const char *name, const char *desc,
const char *default_val, validate_string_relopt validator,
LOCKMODE lockmode)
{
relopt_string *newoption;
/* make sure the validator/default combination is sane */
if (validator)
(validator) (default_val);
newoption = (relopt_string *) allocate_reloption(kinds, RELOPT_TYPE_STRING,
name, desc, lockmode);
newoption->validate_cb = validator;
if (default_val)
{
newoption->default_val = MemoryContextStrdup(TopMemoryContext,
default_val);
newoption->default_len = strlen(default_val);
newoption->default_isnull = false;
}
else
{
newoption->default_val = "";
newoption->default_len = 0;
newoption->default_isnull = true;
}
relopt_string *newoption = init_string_reloption(kinds, name, desc,
default_val,
validator, NULL,
lockmode);
add_reloption((relopt_gen *) newoption);
}
/*
* add_local_string_reloption
* Add a new local string reloption
*
* 'offset' is offset of int-typed field that will store offset of string value
* in the resulting bytea structure.
*/
void
add_local_string_reloption(local_relopts *relopts, const char *name,
const char *desc, const char *default_val,
validate_string_relopt validator,
fill_string_relopt filler, int offset)
{
relopt_string *newoption = init_string_reloption(RELOPT_KIND_LOCAL,
name, desc,
default_val,
validator, filler,
0);
add_local_reloption(relopts, (relopt_gen *) newoption, offset);
}
/*
* Transform a relation options list (list of DefElem) into the text array
* format that is kept in pg_class.reloptions, including only those options
@ -1173,6 +1401,60 @@ extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
return options;
}
static void
parseRelOptionsInternal(Datum options, bool validate,
relopt_value *reloptions, int numoptions)
{
ArrayType *array = DatumGetArrayTypeP(options);
Datum *optiondatums;
int noptions;
int i;
deconstruct_array(array, TEXTOID, -1, false, TYPALIGN_INT,
&optiondatums, NULL, &noptions);
for (i = 0; i < noptions; i++)
{
char *text_str = VARDATA(optiondatums[i]);
int text_len = VARSIZE(optiondatums[i]) - VARHDRSZ;
int j;
/* Search for a match in reloptions */
for (j = 0; j < numoptions; j++)
{
int kw_len = reloptions[j].gen->namelen;
if (text_len > kw_len && text_str[kw_len] == '=' &&
strncmp(text_str, reloptions[j].gen->name, kw_len) == 0)
{
parse_one_reloption(&reloptions[j], text_str, text_len,
validate);
break;
}
}
if (j >= numoptions && validate)
{
char *s;
char *p;
s = TextDatumGetCString(optiondatums[i]);
p = strchr(s, '=');
if (p)
*p = '\0';
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unrecognized parameter \"%s\"", s)));
}
}
/* It's worth avoiding memory leaks in this function */
pfree(optiondatums);
if (((void *) array) != DatumGetPointer(options))
pfree(array);
}
/*
* Interpret reloptions that are given in text-array format.
*
@ -1227,59 +1509,37 @@ parseRelOptions(Datum options, bool validate, relopt_kind kind,
/* Done if no options */
if (PointerIsValid(DatumGetPointer(options)))
{
ArrayType *array = DatumGetArrayTypeP(options);
Datum *optiondatums;
int noptions;
deconstruct_array(array, TEXTOID, -1, false, TYPALIGN_INT,
&optiondatums, NULL, &noptions);
for (i = 0; i < noptions; i++)
{
char *text_str = VARDATA(optiondatums[i]);
int text_len = VARSIZE(optiondatums[i]) - VARHDRSZ;
int j;
/* Search for a match in reloptions */
for (j = 0; j < numoptions; j++)
{
int kw_len = reloptions[j].gen->namelen;
if (text_len > kw_len && text_str[kw_len] == '=' &&
strncmp(text_str, reloptions[j].gen->name, kw_len) == 0)
{
parse_one_reloption(&reloptions[j], text_str, text_len,
validate);
break;
}
}
if (j >= numoptions && validate)
{
char *s;
char *p;
s = TextDatumGetCString(optiondatums[i]);
p = strchr(s, '=');
if (p)
*p = '\0';
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unrecognized parameter \"%s\"", s)));
}
}
/* It's worth avoiding memory leaks in this function */
pfree(optiondatums);
if (((void *) array) != DatumGetPointer(options))
pfree(array);
}
parseRelOptionsInternal(options, validate, reloptions, numoptions);
*numrelopts = numoptions;
return reloptions;
}
/* Parse local unregistered options. */
static relopt_value *
parseLocalRelOptions(local_relopts *relopts, Datum options, bool validate)
{
int nopts = list_length(relopts->options);
relopt_value *values = palloc(sizeof(*values) * nopts);
ListCell *lc;
int i = 0;
foreach(lc, relopts->options)
{
local_relopt *opt = lfirst(lc);
values[i].gen = opt->option;
values[i].isset = false;
i++;
}
if (options != (Datum) 0)
parseRelOptionsInternal(options, validate, values, nopts);
return values;
}
/*
* Subroutine for parseRelOptions, to parse and validate a single option's
* value
@ -1424,8 +1684,24 @@ allocateReloptStruct(Size base, relopt_value *options, int numoptions)
int i;
for (i = 0; i < numoptions; i++)
if (options[i].gen->type == RELOPT_TYPE_STRING)
size += GET_STRING_RELOPTION_LEN(options[i]) + 1;
{
relopt_value *optval = &options[i];
if (optval->gen->type == RELOPT_TYPE_STRING)
{
relopt_string *optstr = (relopt_string *) optval->gen;
if (optstr->fill_cb)
{
const char *val = optval->isset ? optval->values.string_val :
optstr->default_isnull ? NULL : optstr->default_val;
size += optstr->fill_cb(val, NULL);
}
else
size += GET_STRING_RELOPTION_LEN(*optval) + 1;
}
}
return palloc0(size);
}
@ -1494,7 +1770,21 @@ fillRelOptions(void *rdopts, Size basesize,
else
string_val = NULL;
if (string_val == NULL)
if (optstring->fill_cb)
{
Size size =
optstring->fill_cb(string_val,
(char *) rdopts + offset);
if (size)
{
*(int *) itempos = offset;
offset += size;
}
else
*(int *) itempos = 0;
}
else if (string_val == NULL)
*(int *) itempos = 0;
else
{
@ -1625,6 +1915,46 @@ build_reloptions(Datum reloptions, bool validate,
return rdopts;
}
/*
* Parse local options, allocate a bytea struct that's of the specified
* 'base_size' plus any extra space that's needed for string variables,
* fill its option's fields located at the given offsets and return it.
*/
void *
build_local_reloptions(local_relopts *relopts, Datum options, bool validate)
{
int noptions = list_length(relopts->options);
relopt_parse_elt *elems = palloc(sizeof(*elems) * noptions);
relopt_value *vals;
void *opts;
int i = 0;
ListCell *lc;
foreach(lc, relopts->options)
{
local_relopt *opt = lfirst(lc);
elems[i].optname = opt->option->name;
elems[i].opttype = opt->option->type;
elems[i].offset = opt->offset;
i++;
}
vals = parseLocalRelOptions(relopts, options, validate);
opts = allocateReloptStruct(relopts->relopt_struct_size, vals, noptions);
fillRelOptions(opts, relopts->relopt_struct_size, vals, noptions, validate,
elems, noptions);
foreach(lc, relopts->validators)
((relopts_validator) lfirst(lc)) (opts, vals, noptions);
if (elems)
pfree(elems);
return opts;
}
/*
* Option parser for partitioned tables
*/