mirror of
https://github.com/postgres/postgres.git
synced 2025-07-26 01:22:12 +03:00
Restructure operator classes to allow improved handling of cross-data-type
cases. Operator classes now exist within "operator families". While most families are equivalent to a single class, related classes can be grouped into one family to represent the fact that they are semantically compatible. Cross-type operators are now naturally adjunct parts of a family, without having to wedge them into a particular opclass as we had done originally. This commit restructures the catalogs and cleans up enough of the fallout so that everything still works at least as well as before, but most of the work needed to actually improve the planner's behavior will come later. Also, there are not yet CREATE/DROP/ALTER OPERATOR FAMILY commands; the only way to create a new family right now is to allow CREATE OPERATOR CLASS to make one by default. I owe some more documentation work, too. But that can all be done in smaller pieces once this infrastructure is in place.
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.149 2006/10/04 00:29:51 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.150 2006/12/23 00:43:09 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -802,31 +802,37 @@ GetIndexOpClass(List *opclass, Oid attrType,
|
||||
Oid
|
||||
GetDefaultOpClass(Oid type_id, Oid am_id)
|
||||
{
|
||||
Oid result = InvalidOid;
|
||||
int nexact = 0;
|
||||
int ncompatible = 0;
|
||||
Oid exactOid = InvalidOid;
|
||||
Oid compatibleOid = InvalidOid;
|
||||
int ncompatiblepreferred = 0;
|
||||
Relation rel;
|
||||
ScanKeyData skey[1];
|
||||
SysScanDesc scan;
|
||||
HeapTuple tup;
|
||||
CATEGORY tcategory;
|
||||
|
||||
/* If it's a domain, look at the base type instead */
|
||||
type_id = getBaseType(type_id);
|
||||
|
||||
tcategory = TypeCategory(type_id);
|
||||
|
||||
/*
|
||||
* We scan through all the opclasses available for the access method,
|
||||
* looking for one that is marked default and matches the target type
|
||||
* (either exactly or binary-compatibly, but prefer an exact match).
|
||||
*
|
||||
* We could find more than one binary-compatible match, in which case we
|
||||
* require the user to specify which one he wants. If we find more than
|
||||
* one exact match, then someone put bogus entries in pg_opclass.
|
||||
* We could find more than one binary-compatible match. If just one is
|
||||
* for a preferred type, use that one; otherwise we fail, forcing the user
|
||||
* to specify which one he wants. (The preferred-type special case is a
|
||||
* kluge for varchar: it's binary-compatible to both text and bpchar, so
|
||||
* we need a tiebreaker.) If we find more than one exact match, then
|
||||
* someone put bogus entries in pg_opclass.
|
||||
*/
|
||||
rel = heap_open(OperatorClassRelationId, AccessShareLock);
|
||||
|
||||
ScanKeyInit(&skey[0],
|
||||
Anum_pg_opclass_opcamid,
|
||||
Anum_pg_opclass_opcmethod,
|
||||
BTEqualStrategyNumber, F_OIDEQ,
|
||||
ObjectIdGetDatum(am_id));
|
||||
|
||||
@ -837,17 +843,26 @@ GetDefaultOpClass(Oid type_id, Oid am_id)
|
||||
{
|
||||
Form_pg_opclass opclass = (Form_pg_opclass) GETSTRUCT(tup);
|
||||
|
||||
if (opclass->opcdefault)
|
||||
/* ignore altogether if not a default opclass */
|
||||
if (!opclass->opcdefault)
|
||||
continue;
|
||||
if (opclass->opcintype == type_id)
|
||||
{
|
||||
if (opclass->opcintype == type_id)
|
||||
nexact++;
|
||||
result = HeapTupleGetOid(tup);
|
||||
}
|
||||
else if (nexact == 0 &&
|
||||
IsBinaryCoercible(type_id, opclass->opcintype))
|
||||
{
|
||||
if (IsPreferredType(tcategory, opclass->opcintype))
|
||||
{
|
||||
nexact++;
|
||||
exactOid = HeapTupleGetOid(tup);
|
||||
ncompatiblepreferred++;
|
||||
result = HeapTupleGetOid(tup);
|
||||
}
|
||||
else if (IsBinaryCoercible(type_id, opclass->opcintype))
|
||||
else if (ncompatiblepreferred == 0)
|
||||
{
|
||||
ncompatible++;
|
||||
compatibleOid = HeapTupleGetOid(tup);
|
||||
result = HeapTupleGetOid(tup);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -856,15 +871,17 @@ GetDefaultOpClass(Oid type_id, Oid am_id)
|
||||
|
||||
heap_close(rel, AccessShareLock);
|
||||
|
||||
if (nexact == 1)
|
||||
return exactOid;
|
||||
if (nexact != 0)
|
||||
/* raise error if pg_opclass contains inconsistent data */
|
||||
if (nexact > 1)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
||||
errmsg("there are multiple default operator classes for data type %s",
|
||||
format_type_be(type_id))));
|
||||
if (ncompatible == 1)
|
||||
return compatibleOid;
|
||||
|
||||
if (nexact == 1 ||
|
||||
ncompatiblepreferred == 1 ||
|
||||
(ncompatiblepreferred == 0 && ncompatible == 1))
|
||||
return result;
|
||||
|
||||
return InvalidOid;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -9,7 +9,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/operatorcmds.c,v 1.33 2006/10/04 00:29:51 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/operatorcmds.c,v 1.34 2006/12/23 00:43:09 tgl Exp $
|
||||
*
|
||||
* DESCRIPTION
|
||||
* The "DefineFoo" routines take the parse tree and pick out the
|
||||
@ -64,8 +64,8 @@ DefineOperator(List *names, List *parameters)
|
||||
char *oprName;
|
||||
Oid oprNamespace;
|
||||
AclResult aclresult;
|
||||
bool canHash = false; /* operator hashes */
|
||||
bool canMerge = false; /* operator merges */
|
||||
bool canHash = false; /* operator hashes */
|
||||
List *functionName = NIL; /* function for operator */
|
||||
TypeName *typeName1 = NULL; /* first type name */
|
||||
TypeName *typeName2 = NULL; /* second type name */
|
||||
@ -75,10 +75,6 @@ DefineOperator(List *names, List *parameters)
|
||||
List *negatorName = NIL; /* optional negator operator name */
|
||||
List *restrictionName = NIL; /* optional restrict. sel. procedure */
|
||||
List *joinName = NIL; /* optional join sel. procedure */
|
||||
List *leftSortName = NIL; /* optional left sort operator */
|
||||
List *rightSortName = NIL; /* optional right sort operator */
|
||||
List *ltCompareName = NIL; /* optional < compare operator */
|
||||
List *gtCompareName = NIL; /* optional > compare operator */
|
||||
ListCell *pl;
|
||||
|
||||
/* Convert list of names to a name and namespace */
|
||||
@ -127,14 +123,15 @@ DefineOperator(List *names, List *parameters)
|
||||
canHash = defGetBoolean(defel);
|
||||
else if (pg_strcasecmp(defel->defname, "merges") == 0)
|
||||
canMerge = defGetBoolean(defel);
|
||||
/* These obsolete options are taken as meaning canMerge */
|
||||
else if (pg_strcasecmp(defel->defname, "sort1") == 0)
|
||||
leftSortName = defGetQualifiedName(defel);
|
||||
canMerge = true;
|
||||
else if (pg_strcasecmp(defel->defname, "sort2") == 0)
|
||||
rightSortName = defGetQualifiedName(defel);
|
||||
canMerge = true;
|
||||
else if (pg_strcasecmp(defel->defname, "ltcmp") == 0)
|
||||
ltCompareName = defGetQualifiedName(defel);
|
||||
canMerge = true;
|
||||
else if (pg_strcasecmp(defel->defname, "gtcmp") == 0)
|
||||
gtCompareName = defGetQualifiedName(defel);
|
||||
canMerge = true;
|
||||
else
|
||||
ereport(WARNING,
|
||||
(errcode(ERRCODE_SYNTAX_ERROR),
|
||||
@ -156,26 +153,6 @@ DefineOperator(List *names, List *parameters)
|
||||
if (typeName2)
|
||||
typeId2 = typenameTypeId(NULL, typeName2);
|
||||
|
||||
/*
|
||||
* If any of the mergejoin support operators were given, then canMerge is
|
||||
* implicit. If canMerge is specified or implicit, fill in default
|
||||
* operator names for any missing mergejoin support operators.
|
||||
*/
|
||||
if (leftSortName || rightSortName || ltCompareName || gtCompareName)
|
||||
canMerge = true;
|
||||
|
||||
if (canMerge)
|
||||
{
|
||||
if (!leftSortName)
|
||||
leftSortName = list_make1(makeString("<"));
|
||||
if (!rightSortName)
|
||||
rightSortName = list_make1(makeString("<"));
|
||||
if (!ltCompareName)
|
||||
ltCompareName = list_make1(makeString("<"));
|
||||
if (!gtCompareName)
|
||||
gtCompareName = list_make1(makeString(">"));
|
||||
}
|
||||
|
||||
/*
|
||||
* now have OperatorCreate do all the work..
|
||||
*/
|
||||
@ -188,11 +165,8 @@ DefineOperator(List *names, List *parameters)
|
||||
negatorName, /* optional negator operator name */
|
||||
restrictionName, /* optional restrict. sel. procedure */
|
||||
joinName, /* optional join sel. procedure name */
|
||||
canHash, /* operator hashes */
|
||||
leftSortName, /* optional left sort operator */
|
||||
rightSortName, /* optional right sort operator */
|
||||
ltCompareName, /* optional < comparison op */
|
||||
gtCompareName); /* optional < comparison op */
|
||||
canMerge, /* operator merges */
|
||||
canHash); /* operator hashes */
|
||||
}
|
||||
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.206 2006/10/13 21:43:18 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.207 2006/12/23 00:43:09 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -4145,7 +4145,7 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
|
||||
* generate a warning if not, since otherwise costly seqscans will be
|
||||
* incurred to check FK validity.
|
||||
*/
|
||||
if (!op_in_opclass(oprid(o), opclasses[i]))
|
||||
if (!op_in_opfamily(oprid(o), get_opclass_family(opclasses[i])))
|
||||
ereport(WARNING,
|
||||
(errmsg("foreign key constraint \"%s\" "
|
||||
"will require costly sequential scans",
|
||||
|
Reference in New Issue
Block a user