mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +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:
@ -90,6 +90,7 @@ static void RangeVarCallbackForReindexIndex(const RangeVar *relation,
|
||||
static bool ReindexRelationConcurrently(Oid relationOid, int options);
|
||||
static void ReindexPartitionedIndex(Relation parentIdx);
|
||||
static void update_relispartition(Oid relationId, bool newval);
|
||||
static bool CompareOpclassOptions(Datum *opts1, Datum *opts2, int natts);
|
||||
|
||||
/*
|
||||
* callback argument type for RangeVarCallbackForReindexIndex()
|
||||
@ -268,6 +269,18 @@ CheckIndexCompatible(Oid oldId,
|
||||
}
|
||||
}
|
||||
|
||||
/* Any change in opclass options break compatibility. */
|
||||
if (ret)
|
||||
{
|
||||
Datum *opclassOptions = RelationGetIndexRawAttOptions(irel);
|
||||
|
||||
ret = CompareOpclassOptions(opclassOptions,
|
||||
indexInfo->ii_OpclassOptions, old_natts);
|
||||
|
||||
if (opclassOptions)
|
||||
pfree(opclassOptions);
|
||||
}
|
||||
|
||||
/* Any change in exclusion operator selections breaks compatibility. */
|
||||
if (ret && indexInfo->ii_ExclusionOps != NULL)
|
||||
{
|
||||
@ -302,6 +315,42 @@ CheckIndexCompatible(Oid oldId,
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* CompareOpclassOptions
|
||||
*
|
||||
* Compare per-column opclass options which are represented by arrays of text[]
|
||||
* datums. Both elements of arrays and array themselves can be NULL.
|
||||
*/
|
||||
static bool
|
||||
CompareOpclassOptions(Datum *opts1, Datum *opts2, int natts)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!opts1 && !opts2)
|
||||
return true;
|
||||
|
||||
for (i = 0; i < natts; i++)
|
||||
{
|
||||
Datum opt1 = opts1 ? opts1[i] : (Datum) 0;
|
||||
Datum opt2 = opts2 ? opts2[i] : (Datum) 0;
|
||||
|
||||
if (opt1 == (Datum) 0)
|
||||
{
|
||||
if (opt2 == (Datum) 0)
|
||||
continue;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
else if (opt2 == (Datum) 0)
|
||||
return false;
|
||||
|
||||
/* Compare non-NULL text[] datums. */
|
||||
if (!DatumGetBool(DirectFunctionCall2(array_eq, opt1, opt2)))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* WaitForOlderSnapshots
|
||||
@ -1528,7 +1577,7 @@ CheckPredicate(Expr *predicate)
|
||||
|
||||
/*
|
||||
* Compute per-index-column information, including indexed column numbers
|
||||
* or index expressions, opclasses, and indoptions. Note, all output vectors
|
||||
* or index expressions, opclasses and their options. Note, all output vectors
|
||||
* should be allocated for all columns, including "including" ones.
|
||||
*/
|
||||
static void
|
||||
@ -1829,6 +1878,20 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
|
||||
accessMethodName)));
|
||||
}
|
||||
|
||||
/* Set up the per-column opclass options (attoptions field). */
|
||||
if (attribute->opclassopts)
|
||||
{
|
||||
Assert(attn < nkeycols);
|
||||
|
||||
if (!indexInfo->ii_OpclassOptions)
|
||||
indexInfo->ii_OpclassOptions =
|
||||
palloc0(sizeof(Datum) * indexInfo->ii_NumIndexAttrs);
|
||||
|
||||
indexInfo->ii_OpclassOptions[attn] =
|
||||
transformRelOptions((Datum) 0, attribute->opclassopts,
|
||||
NULL, NULL, false, false);
|
||||
}
|
||||
|
||||
attn++;
|
||||
}
|
||||
}
|
||||
|
@ -53,14 +53,15 @@
|
||||
static void AlterOpFamilyAdd(AlterOpFamilyStmt *stmt,
|
||||
Oid amoid, Oid opfamilyoid,
|
||||
int maxOpNumber, int maxProcNumber,
|
||||
List *items);
|
||||
int opclassOptsProcNumber, List *items);
|
||||
static void AlterOpFamilyDrop(AlterOpFamilyStmt *stmt,
|
||||
Oid amoid, Oid opfamilyoid,
|
||||
int maxOpNumber, int maxProcNumber,
|
||||
List *items);
|
||||
static void processTypesSpec(List *args, Oid *lefttype, Oid *righttype);
|
||||
static void assignOperTypes(OpFamilyMember *member, Oid amoid, Oid typeoid);
|
||||
static void assignProcTypes(OpFamilyMember *member, Oid amoid, Oid typeoid);
|
||||
static void assignProcTypes(OpFamilyMember *member, Oid amoid, Oid typeoid,
|
||||
int opclassOptsProcNum);
|
||||
static void addFamilyMember(List **list, OpFamilyMember *member, bool isProc);
|
||||
static void storeOperators(List *opfamilyname, Oid amoid,
|
||||
Oid opfamilyoid, Oid opclassoid,
|
||||
@ -337,6 +338,7 @@ DefineOpClass(CreateOpClassStmt *stmt)
|
||||
opfamilyoid, /* oid of containing opfamily */
|
||||
opclassoid; /* oid of opclass we create */
|
||||
int maxOpNumber, /* amstrategies value */
|
||||
optsProcNumber, /* amoptsprocnum value */
|
||||
maxProcNumber; /* amsupport value */
|
||||
bool amstorage; /* amstorage flag */
|
||||
List *operators; /* OpFamilyMember list for operators */
|
||||
@ -381,6 +383,7 @@ DefineOpClass(CreateOpClassStmt *stmt)
|
||||
if (maxOpNumber <= 0)
|
||||
maxOpNumber = SHRT_MAX;
|
||||
maxProcNumber = amroutine->amsupport;
|
||||
optsProcNumber = amroutine->amoptsprocnum;
|
||||
amstorage = amroutine->amstorage;
|
||||
|
||||
/* XXX Should we make any privilege check against the AM? */
|
||||
@ -536,7 +539,6 @@ DefineOpClass(CreateOpClassStmt *stmt)
|
||||
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FUNCTION,
|
||||
get_func_name(funcOid));
|
||||
#endif
|
||||
|
||||
/* Save the info */
|
||||
member = (OpFamilyMember *) palloc0(sizeof(OpFamilyMember));
|
||||
member->object = funcOid;
|
||||
@ -547,7 +549,7 @@ DefineOpClass(CreateOpClassStmt *stmt)
|
||||
processTypesSpec(item->class_args,
|
||||
&member->lefttype, &member->righttype);
|
||||
|
||||
assignProcTypes(member, amoid, typeoid);
|
||||
assignProcTypes(member, amoid, typeoid, optsProcNumber);
|
||||
addFamilyMember(&procedures, member, true);
|
||||
break;
|
||||
case OPCLASS_ITEM_STORAGETYPE:
|
||||
@ -777,6 +779,7 @@ AlterOpFamily(AlterOpFamilyStmt *stmt)
|
||||
Oid amoid, /* our AM's oid */
|
||||
opfamilyoid; /* oid of opfamily */
|
||||
int maxOpNumber, /* amstrategies value */
|
||||
optsProcNumber, /* amopclassopts value */
|
||||
maxProcNumber; /* amsupport value */
|
||||
HeapTuple tup;
|
||||
Form_pg_am amform;
|
||||
@ -800,6 +803,7 @@ AlterOpFamily(AlterOpFamilyStmt *stmt)
|
||||
if (maxOpNumber <= 0)
|
||||
maxOpNumber = SHRT_MAX;
|
||||
maxProcNumber = amroutine->amsupport;
|
||||
optsProcNumber = amroutine->amoptsprocnum;
|
||||
|
||||
/* XXX Should we make any privilege check against the AM? */
|
||||
|
||||
@ -824,7 +828,8 @@ AlterOpFamily(AlterOpFamilyStmt *stmt)
|
||||
maxOpNumber, maxProcNumber, stmt->items);
|
||||
else
|
||||
AlterOpFamilyAdd(stmt, amoid, opfamilyoid,
|
||||
maxOpNumber, maxProcNumber, stmt->items);
|
||||
maxOpNumber, maxProcNumber, optsProcNumber,
|
||||
stmt->items);
|
||||
|
||||
return opfamilyoid;
|
||||
}
|
||||
@ -834,7 +839,8 @@ AlterOpFamily(AlterOpFamilyStmt *stmt)
|
||||
*/
|
||||
static void
|
||||
AlterOpFamilyAdd(AlterOpFamilyStmt *stmt, Oid amoid, Oid opfamilyoid,
|
||||
int maxOpNumber, int maxProcNumber, List *items)
|
||||
int maxOpNumber, int maxProcNumber, int optsProcNumber,
|
||||
List *items)
|
||||
{
|
||||
List *operators; /* OpFamilyMember list for operators */
|
||||
List *procedures; /* OpFamilyMember list for support procs */
|
||||
@ -926,7 +932,7 @@ AlterOpFamilyAdd(AlterOpFamilyStmt *stmt, Oid amoid, Oid opfamilyoid,
|
||||
processTypesSpec(item->class_args,
|
||||
&member->lefttype, &member->righttype);
|
||||
|
||||
assignProcTypes(member, amoid, InvalidOid);
|
||||
assignProcTypes(member, amoid, InvalidOid, optsProcNumber);
|
||||
addFamilyMember(&procedures, member, true);
|
||||
break;
|
||||
case OPCLASS_ITEM_STORAGETYPE:
|
||||
@ -1129,7 +1135,8 @@ assignOperTypes(OpFamilyMember *member, Oid amoid, Oid typeoid)
|
||||
* and do any validity checking we can manage.
|
||||
*/
|
||||
static void
|
||||
assignProcTypes(OpFamilyMember *member, Oid amoid, Oid typeoid)
|
||||
assignProcTypes(OpFamilyMember *member, Oid amoid, Oid typeoid,
|
||||
int opclassOptsProcNum)
|
||||
{
|
||||
HeapTuple proctup;
|
||||
Form_pg_proc procform;
|
||||
@ -1140,6 +1147,36 @@ assignProcTypes(OpFamilyMember *member, Oid amoid, Oid typeoid)
|
||||
elog(ERROR, "cache lookup failed for function %u", member->object);
|
||||
procform = (Form_pg_proc) GETSTRUCT(proctup);
|
||||
|
||||
/* Check the signature of the opclass options parsing function */
|
||||
if (member->number == opclassOptsProcNum)
|
||||
{
|
||||
if (OidIsValid(typeoid))
|
||||
{
|
||||
if ((OidIsValid(member->lefttype) && member->lefttype != typeoid) ||
|
||||
(OidIsValid(member->righttype) && member->righttype != typeoid))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||
errmsg("associated data types for opclass options "
|
||||
"parsing functions must match opclass input type")));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (member->lefttype != member->righttype)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||
errmsg("left and right associated data types for "
|
||||
"opclass options parsing functions must match")));
|
||||
}
|
||||
|
||||
if (procform->prorettype != VOIDOID ||
|
||||
procform->pronargs != 1 ||
|
||||
procform->proargtypes.values[0] != INTERNALOID)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||
errmsg("invalid opclass options parsing function"),
|
||||
errhint("opclass options parsing function must have signature '%s'",
|
||||
"(internal) RETURNS void")));
|
||||
}
|
||||
/*
|
||||
* btree comparison procs must be 2-arg procs returning int4. btree
|
||||
* sortsupport procs must take internal and return void. btree in_range
|
||||
@ -1148,7 +1185,7 @@ assignProcTypes(OpFamilyMember *member, Oid amoid, Oid typeoid)
|
||||
* returning int4, while proc 2 must be a 2-arg proc returning int8.
|
||||
* Otherwise we don't know.
|
||||
*/
|
||||
if (amoid == BTREE_AM_OID)
|
||||
else if (amoid == BTREE_AM_OID)
|
||||
{
|
||||
if (member->number == BTORDER_PROC)
|
||||
{
|
||||
|
@ -6085,7 +6085,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
|
||||
|
||||
ReleaseSysCache(typeTuple);
|
||||
|
||||
InsertPgAttributeTuple(attrdesc, &attribute, NULL);
|
||||
InsertPgAttributeTuple(attrdesc, &attribute, (Datum) 0, NULL);
|
||||
|
||||
table_close(attrdesc, RowExclusiveLock);
|
||||
|
||||
|
Reference in New Issue
Block a user