mirror of
https://github.com/postgres/postgres.git
synced 2025-09-09 13:09:39 +03:00
Change gist stratnum function to use CompareType
This changes commit 7406ab623f
in that the gist strategy number
mapping support function is changed to use the CompareType enum as
input, instead of the "well-known" RT*StrategyNumber strategy numbers.
This is a bit cleaner, since you are not dealing with two sets of
strategy numbers. Also, this will enable us to subsume this system
into a more general system of using CompareType to define operator
semantics across index methods.
Discussion: https://www.postgresql.org/message-id/flat/E72EAA49-354D-4C2E-8EB9-255197F55330@enterprisedb.com
This commit is contained in:
@@ -1058,27 +1058,44 @@ gistGetFakeLSN(Relation rel)
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the same number that was received.
|
||||
*
|
||||
* This is for GiST opclasses that use the RT*StrategyNumber constants.
|
||||
* This is a stratnum support function for GiST opclasses that use the
|
||||
* RT*StrategyNumber constants.
|
||||
*/
|
||||
Datum
|
||||
gist_stratnum_identity(PG_FUNCTION_ARGS)
|
||||
gist_stratnum_common(PG_FUNCTION_ARGS)
|
||||
{
|
||||
StrategyNumber strat = PG_GETARG_UINT16(0);
|
||||
CompareType cmptype = PG_GETARG_INT32(0);
|
||||
|
||||
PG_RETURN_UINT16(strat);
|
||||
switch (cmptype)
|
||||
{
|
||||
case COMPARE_EQ:
|
||||
PG_RETURN_UINT16(RTEqualStrategyNumber);
|
||||
case COMPARE_LT:
|
||||
PG_RETURN_UINT16(RTLessStrategyNumber);
|
||||
case COMPARE_LE:
|
||||
PG_RETURN_UINT16(RTLessEqualStrategyNumber);
|
||||
case COMPARE_GT:
|
||||
PG_RETURN_UINT16(RTGreaterStrategyNumber);
|
||||
case COMPARE_GE:
|
||||
PG_RETURN_UINT16(RTGreaterEqualStrategyNumber);
|
||||
case COMPARE_OVERLAP:
|
||||
PG_RETURN_UINT16(RTOverlapStrategyNumber);
|
||||
case COMPARE_CONTAINED_BY:
|
||||
PG_RETURN_UINT16(RTContainedByStrategyNumber);
|
||||
default:
|
||||
PG_RETURN_UINT16(InvalidStrategy);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the opclass's private stratnum used for the given strategy.
|
||||
* Returns the opclass's private stratnum used for the given compare type.
|
||||
*
|
||||
* Calls the opclass's GIST_STRATNUM_PROC support function, if any,
|
||||
* and returns the result.
|
||||
* Returns InvalidStrategy if the function is not defined.
|
||||
*/
|
||||
StrategyNumber
|
||||
GistTranslateStratnum(Oid opclass, StrategyNumber strat)
|
||||
GistTranslateStratnum(Oid opclass, CompareType cmptype)
|
||||
{
|
||||
Oid opfamily;
|
||||
Oid opcintype;
|
||||
@@ -1095,6 +1112,6 @@ GistTranslateStratnum(Oid opclass, StrategyNumber strat)
|
||||
return InvalidStrategy;
|
||||
|
||||
/* Ask the translation function */
|
||||
result = OidFunctionCall1Coll(funcid, InvalidOid, UInt16GetDatum(strat));
|
||||
result = OidFunctionCall1Coll(funcid, InvalidOid, Int32GetDatum(cmptype));
|
||||
return DatumGetUInt16(result);
|
||||
}
|
||||
|
@@ -148,7 +148,7 @@ gistvalidate(Oid opclassoid)
|
||||
break;
|
||||
case GIST_STRATNUM_PROC:
|
||||
ok = check_amproc_signature(procform->amproc, INT2OID, true,
|
||||
1, 1, INT2OID);
|
||||
1, 1, INT4OID);
|
||||
break;
|
||||
default:
|
||||
ereport(INFO,
|
||||
|
@@ -1647,22 +1647,22 @@ FindFKPeriodOpers(Oid opclass,
|
||||
* of the old value, then we can treat the attribute as if it didn't
|
||||
* change, and skip the RI check.
|
||||
*/
|
||||
strat = RTContainedByStrategyNumber;
|
||||
GetOperatorFromWellKnownStrategy(opclass,
|
||||
InvalidOid,
|
||||
containedbyoperoid,
|
||||
&strat);
|
||||
GetOperatorFromCompareType(opclass,
|
||||
InvalidOid,
|
||||
COMPARE_CONTAINED_BY,
|
||||
containedbyoperoid,
|
||||
&strat);
|
||||
|
||||
/*
|
||||
* Now look up the ContainedBy operator. Its left arg must be the type of
|
||||
* the column (or rather of the opclass). Its right arg must match the
|
||||
* return type of the support proc.
|
||||
*/
|
||||
strat = RTContainedByStrategyNumber;
|
||||
GetOperatorFromWellKnownStrategy(opclass,
|
||||
ANYMULTIRANGEOID,
|
||||
aggedcontainedbyoperoid,
|
||||
&strat);
|
||||
GetOperatorFromCompareType(opclass,
|
||||
ANYMULTIRANGEOID,
|
||||
COMPARE_CONTAINED_BY,
|
||||
aggedcontainedbyoperoid,
|
||||
&strat);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -2178,15 +2178,15 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
|
||||
}
|
||||
else if (iswithoutoverlaps)
|
||||
{
|
||||
CompareType cmptype;
|
||||
StrategyNumber strat;
|
||||
Oid opid;
|
||||
|
||||
if (attn == nkeycols - 1)
|
||||
strat = RTOverlapStrategyNumber;
|
||||
cmptype = COMPARE_OVERLAP;
|
||||
else
|
||||
strat = RTEqualStrategyNumber;
|
||||
GetOperatorFromWellKnownStrategy(opclassOids[attn], InvalidOid,
|
||||
&opid, &strat);
|
||||
cmptype = COMPARE_EQ;
|
||||
GetOperatorFromCompareType(opclassOids[attn], InvalidOid, cmptype, &opid, &strat);
|
||||
indexInfo->ii_ExclusionOps[attn] = opid;
|
||||
indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
|
||||
indexInfo->ii_ExclusionStrats[attn] = strat;
|
||||
@@ -2422,30 +2422,28 @@ GetDefaultOpClass(Oid type_id, Oid am_id)
|
||||
}
|
||||
|
||||
/*
|
||||
* GetOperatorFromWellKnownStrategy
|
||||
* GetOperatorFromCompareType
|
||||
*
|
||||
* opclass - the opclass to use
|
||||
* rhstype - the type for the right-hand side, or InvalidOid to use the type of the given opclass.
|
||||
* cmptype - kind of operator to find
|
||||
* opid - holds the operator we found
|
||||
* strat - holds the input and output strategy number
|
||||
* strat - holds the output strategy number
|
||||
*
|
||||
* Finds an operator from a "well-known" strategy number. This is used for
|
||||
* temporal index constraints (and other temporal features) to look up
|
||||
* equality and overlaps operators, since the strategy numbers for non-btree
|
||||
* indexams need not follow any fixed scheme. We ask an opclass support
|
||||
* function to translate from the well-known number to the internal value. If
|
||||
* the function isn't defined or it gives no result, we return
|
||||
* InvalidStrategy.
|
||||
* Finds an operator from a CompareType. This is used for temporal index
|
||||
* constraints (and other temporal features) to look up equality and overlaps
|
||||
* operators. We ask an opclass support function to translate from the
|
||||
* compare type to the internal strategy numbers. If the function isn't
|
||||
* defined or it gives no result, we set *strat to InvalidStrategy.
|
||||
*/
|
||||
void
|
||||
GetOperatorFromWellKnownStrategy(Oid opclass, Oid rhstype,
|
||||
Oid *opid, StrategyNumber *strat)
|
||||
GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype,
|
||||
Oid *opid, StrategyNumber *strat)
|
||||
{
|
||||
Oid opfamily;
|
||||
Oid opcintype;
|
||||
StrategyNumber instrat = *strat;
|
||||
|
||||
Assert(instrat == RTEqualStrategyNumber || instrat == RTOverlapStrategyNumber || instrat == RTContainedByStrategyNumber);
|
||||
Assert(cmptype == COMPARE_EQ || cmptype == COMPARE_OVERLAP || cmptype == COMPARE_CONTAINED_BY);
|
||||
|
||||
*opid = InvalidOid;
|
||||
|
||||
@@ -2457,7 +2455,7 @@ GetOperatorFromWellKnownStrategy(Oid opclass, Oid rhstype,
|
||||
* For now we only need GiST support, but this could support other
|
||||
* indexams if we wanted.
|
||||
*/
|
||||
*strat = GistTranslateStratnum(opclass, instrat);
|
||||
*strat = GistTranslateStratnum(opclass, cmptype);
|
||||
if (*strat == InvalidStrategy)
|
||||
{
|
||||
HeapTuple tuple;
|
||||
@@ -2468,11 +2466,11 @@ GetOperatorFromWellKnownStrategy(Oid opclass, Oid rhstype,
|
||||
|
||||
ereport(ERROR,
|
||||
errcode(ERRCODE_UNDEFINED_OBJECT),
|
||||
instrat == RTEqualStrategyNumber ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
|
||||
instrat == RTOverlapStrategyNumber ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
|
||||
instrat == RTContainedByStrategyNumber ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
|
||||
errdetail("Could not translate strategy number %d for operator class \"%s\" for access method \"%s\".",
|
||||
instrat, NameStr(((Form_pg_opclass) GETSTRUCT(tuple))->opcname), "gist"));
|
||||
cmptype = COMPARE_EQ ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
|
||||
cmptype == COMPARE_OVERLAP ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
|
||||
cmptype == COMPARE_CONTAINED_BY ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
|
||||
errdetail("Could not translate compare type %d for operator class \"%s\" for access method \"%s\".",
|
||||
cmptype, NameStr(((Form_pg_opclass) GETSTRUCT(tuple))->opcname), "gist"));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -2495,9 +2493,9 @@ GetOperatorFromWellKnownStrategy(Oid opclass, Oid rhstype,
|
||||
|
||||
ereport(ERROR,
|
||||
errcode(ERRCODE_UNDEFINED_OBJECT),
|
||||
instrat == RTEqualStrategyNumber ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
|
||||
instrat == RTOverlapStrategyNumber ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
|
||||
instrat == RTContainedByStrategyNumber ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
|
||||
cmptype == COMPARE_EQ ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
|
||||
cmptype == COMPARE_OVERLAP ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
|
||||
cmptype == COMPARE_CONTAINED_BY ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
|
||||
errdetail("There is no suitable operator in operator family \"%s\" for access method \"%s\".",
|
||||
NameStr(((Form_pg_opfamily) GETSTRUCT(tuple))->opfname), "gist"));
|
||||
}
|
||||
|
@@ -9997,7 +9997,7 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
|
||||
|
||||
if (with_period)
|
||||
{
|
||||
StrategyNumber rtstrategy;
|
||||
CompareType cmptype;
|
||||
bool for_overlaps = with_period && i == numpks - 1;
|
||||
|
||||
/*
|
||||
@@ -10007,14 +10007,14 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
|
||||
if (amid != GIST_AM_OID)
|
||||
elog(ERROR, "only GiST indexes are supported for temporal foreign keys");
|
||||
|
||||
rtstrategy = for_overlaps ? RTOverlapStrategyNumber : RTEqualStrategyNumber;
|
||||
cmptype = for_overlaps ? COMPARE_OVERLAP : COMPARE_EQ;
|
||||
|
||||
/*
|
||||
* An opclass can use whatever strategy numbers it wants, so we
|
||||
* ask the opclass what number it actually uses instead of our RT*
|
||||
* constants.
|
||||
*/
|
||||
eqstrategy = GistTranslateStratnum(opclasses[i], rtstrategy);
|
||||
eqstrategy = GistTranslateStratnum(opclasses[i], cmptype);
|
||||
if (eqstrategy == InvalidStrategy)
|
||||
{
|
||||
HeapTuple tuple;
|
||||
@@ -10028,8 +10028,8 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
|
||||
for_overlaps
|
||||
? errmsg("could not identify an overlaps operator for foreign key")
|
||||
: errmsg("could not identify an equality operator for foreign key"),
|
||||
errdetail("Could not translate strategy number %d for operator class \"%s\" for access method \"%s\".",
|
||||
rtstrategy, NameStr(((Form_pg_opclass) GETSTRUCT(tuple))->opcname), "gist"));
|
||||
errdetail("Could not translate compare type %d for operator class \"%s\" for access method \"%s\".",
|
||||
cmptype, NameStr(((Form_pg_opclass) GETSTRUCT(tuple))->opcname), "gist"));
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@@ -57,7 +57,7 @@ get_equal_strategy_number(Oid opclass)
|
||||
ret = HTEqualStrategyNumber;
|
||||
break;
|
||||
case GIST_AM_OID:
|
||||
ret = GistTranslateStratnum(opclass, RTEqualStrategyNumber);
|
||||
ret = GistTranslateStratnum(opclass, COMPARE_EQ);
|
||||
break;
|
||||
default:
|
||||
ret = InvalidStrategy;
|
||||
|
Reference in New Issue
Block a user