diff options
author | Alexander Korotkov | 2020-03-30 16:17:11 +0000 |
---|---|---|
committer | Alexander Korotkov | 2020-03-30 16:17:23 +0000 |
commit | 911e70207703799605f5a0e8aad9f06cff067c63 (patch) | |
tree | 5cdbffea1945c2c0b766db195a55bcb372d60537 /src/backend/access/index | |
parent | 1d53432ff940b789c2431ba476a2a6e2db3edf84 (diff) |
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
Diffstat (limited to 'src/backend/access/index')
-rw-r--r-- | src/backend/access/index/amvalidate.c | 11 | ||||
-rw-r--r-- | src/backend/access/index/indexam.c | 77 |
2 files changed, 84 insertions, 4 deletions
diff --git a/src/backend/access/index/amvalidate.c b/src/backend/access/index/amvalidate.c index 3eae6aa012f..24d49750ada 100644 --- a/src/backend/access/index/amvalidate.c +++ b/src/backend/access/index/amvalidate.c @@ -21,6 +21,7 @@ #include "catalog/pg_opclass.h" #include "catalog/pg_operator.h" #include "catalog/pg_proc.h" +#include "catalog/pg_type.h" #include "parser/parse_coerce.h" #include "utils/syscache.h" @@ -183,6 +184,16 @@ check_amproc_signature(Oid funcid, Oid restype, bool exact, } /* + * Validate the signature of an opclass options support function, that should + * be 'void(internal)'. + */ +bool +check_amoptsproc_signature(Oid funcid) +{ + return check_amproc_signature(funcid, VOIDOID, true, 1, 1, INTERNALOID); +} + +/* * Validate the signature (argument and result types) of an opclass operator. * Return true if OK, false if not. * diff --git a/src/backend/access/index/indexam.c b/src/backend/access/index/indexam.c index a5210d0b342..f7e4c65d99f 100644 --- a/src/backend/access/index/indexam.c +++ b/src/backend/access/index/indexam.c @@ -45,17 +45,23 @@ #include "access/amapi.h" #include "access/heapam.h" +#include "access/reloptions.h" #include "access/relscan.h" #include "access/tableam.h" #include "access/transam.h" #include "access/xlog.h" #include "catalog/index.h" +#include "catalog/pg_amproc.h" #include "catalog/pg_type.h" +#include "commands/defrem.h" +#include "nodes/makefuncs.h" #include "pgstat.h" #include "storage/bufmgr.h" #include "storage/lmgr.h" #include "storage/predicate.h" +#include "utils/ruleutils.h" #include "utils/snapmgr.h" +#include "utils/syscache.h" /* ---------------------------------------------------------------- @@ -767,9 +773,9 @@ index_getprocid(Relation irel, nproc = irel->rd_indam->amsupport; - Assert(procnum > 0 && procnum <= (uint16) nproc); + Assert(procnum >= 0 && procnum <= (uint16) nproc); - procindex = (nproc * (attnum - 1)) + (procnum - 1); + procindex = ((nproc + 1) * (attnum - 1)) + procnum; loc = irel->rd_support; @@ -797,13 +803,15 @@ index_getprocinfo(Relation irel, { FmgrInfo *locinfo; int nproc; + int optsproc; int procindex; nproc = irel->rd_indam->amsupport; + optsproc = irel->rd_indam->amoptsprocnum; - Assert(procnum > 0 && procnum <= (uint16) nproc); + Assert(procnum >= 0 && procnum <= (uint16) nproc); - procindex = (nproc * (attnum - 1)) + (procnum - 1); + procindex = ((nproc + 1) * (attnum - 1)) + procnum; locinfo = irel->rd_supportinfo; @@ -832,6 +840,17 @@ index_getprocinfo(Relation irel, procnum, attnum, RelationGetRelationName(irel)); fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt); + + if (procnum != optsproc) + { + /* Initialize locinfo->fn_expr with opclass options Const */ + bytea **attoptions = RelationGetIndexAttOptions(irel, false); + MemoryContext oldcxt = MemoryContextSwitchTo(irel->rd_indexcxt); + + set_fn_opclass_options(locinfo, attoptions[attnum - 1]); + + MemoryContextSwitchTo(oldcxt); + } } return locinfo; @@ -906,3 +925,53 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, } } } + +/* ---------------- + * index_opclass_options + * + * Parse opclass-specific options for index column. + * ---------------- + */ +bytea * +index_opclass_options(Relation indrel, AttrNumber attnum, Datum attoptions, + bool validate) +{ + int amoptsprocnum = indrel->rd_indam->amoptsprocnum; + Oid procid = index_getprocid(indrel, attnum, amoptsprocnum); + FmgrInfo *procinfo; + local_relopts relopts; + + if (!OidIsValid(procid)) + { + Oid opclass; + Datum indclassDatum; + oidvector *indclass; + bool isnull; + + if (!DatumGetPointer(attoptions)) + return NULL; /* ok, no options, no procedure */ + + /* + * Report an error if the opclass's options-parsing procedure does not + * exist but the opclass options are specified. + */ + indclassDatum = SysCacheGetAttr(INDEXRELID, indrel->rd_indextuple, + Anum_pg_index_indclass, &isnull); + Assert(!isnull); + indclass = (oidvector *) DatumGetPointer(indclassDatum); + opclass = indclass->values[attnum - 1]; + + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("operator class %s has no options", + generate_opclass_name(opclass)))); + } + + init_local_reloptions(&relopts, 0); + + procinfo = index_getprocinfo(indrel, attnum, amoptsprocnum); + + (void) FunctionCall1(procinfo, PointerGetDatum(&relopts)); + + return build_local_reloptions(&relopts, attoptions, validate); +} |