mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-06 21:49:19 +02:00
Check equality semantics for unique indexes on partitioned tables.
We require the partition key to be a subset of the set of columns being made unique, so that physically-separate indexes on the different partitions are sufficient to enforce the uniqueness constraint. The existing code checked that the listed columns appear, but did not inquire into the index semantics, which is a serious oversight given that different index opclasses might enforce completely different notions of uniqueness. Ideally, perhaps, we'd just match the partition key opfamily to the index opfamily. But hash partitioning uses hash opfamilies which we can't directly match to btree opfamilies. Hence, look up the equality operator in each family, and accept if it's the same operator. This should be okay in a fairly general sense, since the equality operator ought to precisely represent the opfamily's notion of uniqueness. A remaining weak spot is that we don't have a cross-index-AM notion of which opfamily member is "equality". But we know which one to use for hash and btree AMs, and those are the only two that are relevant here at present. (Any non-core AMs that know how to enforce equality are out of luck, for now.) Back-patch to v11 where this feature was introduced. Guancheng Luo, revised a bit by me Discussion: https://postgr.es/m/D9C3CEF7-04E8-47A1-8300-CA1DCD5ED40D@gmail.com
This commit is contained in:
parent
f79bea4b6b
commit
76ea606968
@ -822,44 +822,75 @@ DefineIndex(Oid relationId,
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* If this table is partitioned and we're creating a unique index or a
|
* If this table is partitioned and we're creating a unique index or a
|
||||||
* primary key, make sure that the indexed columns are part of the
|
* primary key, make sure that the partition key is a subset of the
|
||||||
* partition key. Otherwise it would be possible to violate uniqueness by
|
* index's columns. Otherwise it would be possible to violate uniqueness
|
||||||
* putting values that ought to be unique in different partitions.
|
* by putting values that ought to be unique in different partitions.
|
||||||
*
|
*
|
||||||
* We could lift this limitation if we had global indexes, but those have
|
* We could lift this limitation if we had global indexes, but those have
|
||||||
* their own problems, so this is a useful feature combination.
|
* their own problems, so this is a useful feature combination.
|
||||||
*/
|
*/
|
||||||
if (partitioned && (stmt->unique || stmt->primary))
|
if (partitioned && (stmt->unique || stmt->primary))
|
||||||
{
|
{
|
||||||
PartitionKey key = rel->rd_partkey;
|
PartitionKey key = RelationGetPartitionKey(rel);
|
||||||
|
const char *constraint_type;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (stmt->primary)
|
||||||
|
constraint_type = "PRIMARY KEY";
|
||||||
|
else if (stmt->unique)
|
||||||
|
constraint_type = "UNIQUE";
|
||||||
|
else if (stmt->excludeOpNames != NIL)
|
||||||
|
constraint_type = "EXCLUDE";
|
||||||
|
else
|
||||||
|
{
|
||||||
|
elog(ERROR, "unknown constraint type");
|
||||||
|
constraint_type = NULL; /* keep compiler quiet */
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A partitioned table can have unique indexes, as long as all the
|
* Verify that all the columns in the partition key appear in the
|
||||||
* columns in the partition key appear in the unique key. A
|
* unique key definition, with the same notion of equality.
|
||||||
* partition-local index can enforce global uniqueness iff the PK
|
|
||||||
* value completely determines the partition that a row is in.
|
|
||||||
*
|
|
||||||
* Thus, verify that all the columns in the partition key appear in
|
|
||||||
* the unique key definition.
|
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < key->partnatts; i++)
|
for (i = 0; i < key->partnatts; i++)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
int eq_strategy;
|
||||||
|
Oid ptkey_eqop;
|
||||||
int j;
|
int j;
|
||||||
const char *constraint_type;
|
|
||||||
|
|
||||||
if (stmt->primary)
|
/*
|
||||||
constraint_type = "PRIMARY KEY";
|
* Identify the equality operator associated with this partkey
|
||||||
else if (stmt->unique)
|
* column. For list and range partitioning, partkeys use btree
|
||||||
constraint_type = "UNIQUE";
|
* operator classes; hash partitioning uses hash operator classes.
|
||||||
else if (stmt->excludeOpNames != NIL)
|
* (Keep this in sync with ComputePartitionAttrs!)
|
||||||
constraint_type = "EXCLUDE";
|
*/
|
||||||
|
if (key->strategy == PARTITION_STRATEGY_HASH)
|
||||||
|
eq_strategy = HTEqualStrategyNumber;
|
||||||
else
|
else
|
||||||
{
|
eq_strategy = BTEqualStrategyNumber;
|
||||||
elog(ERROR, "unknown constraint type");
|
|
||||||
constraint_type = NULL; /* keep compiler quiet */
|
ptkey_eqop = get_opfamily_member(key->partopfamily[i],
|
||||||
}
|
key->partopcintype[i],
|
||||||
|
key->partopcintype[i],
|
||||||
|
eq_strategy);
|
||||||
|
if (!OidIsValid(ptkey_eqop))
|
||||||
|
elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
|
||||||
|
eq_strategy, key->partopcintype[i], key->partopcintype[i],
|
||||||
|
key->partopfamily[i]);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We'll need to be able to identify the equality operators
|
||||||
|
* associated with index columns, too. We know what to do with
|
||||||
|
* btree opclasses; if there are ever any other index types that
|
||||||
|
* support unique indexes, this logic will need extension.
|
||||||
|
*/
|
||||||
|
if (accessMethodId == BTREE_AM_OID)
|
||||||
|
eq_strategy = BTEqualStrategyNumber;
|
||||||
|
else
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||||
|
errmsg("cannot match partition key to an index using access method \"%s\"",
|
||||||
|
accessMethodName)));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* It may be possible to support UNIQUE constraints when partition
|
* It may be possible to support UNIQUE constraints when partition
|
||||||
@ -873,19 +904,40 @@ DefineIndex(Oid relationId,
|
|||||||
errdetail("%s constraints cannot be used when partition keys include expressions.",
|
errdetail("%s constraints cannot be used when partition keys include expressions.",
|
||||||
constraint_type)));
|
constraint_type)));
|
||||||
|
|
||||||
|
/* Search the index column(s) for a match */
|
||||||
for (j = 0; j < indexInfo->ii_NumIndexKeyAttrs; j++)
|
for (j = 0; j < indexInfo->ii_NumIndexKeyAttrs; j++)
|
||||||
{
|
{
|
||||||
if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
|
if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
|
||||||
{
|
{
|
||||||
found = true;
|
/* Matched the column, now what about the equality op? */
|
||||||
break;
|
Oid idx_opfamily;
|
||||||
|
Oid idx_opcintype;
|
||||||
|
|
||||||
|
if (get_opclass_opfamily_and_input_type(classObjectId[j],
|
||||||
|
&idx_opfamily,
|
||||||
|
&idx_opcintype))
|
||||||
|
{
|
||||||
|
Oid idx_eqop;
|
||||||
|
|
||||||
|
idx_eqop = get_opfamily_member(idx_opfamily,
|
||||||
|
idx_opcintype,
|
||||||
|
idx_opcintype,
|
||||||
|
eq_strategy);
|
||||||
|
if (ptkey_eqop == idx_eqop)
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
Form_pg_attribute att;
|
Form_pg_attribute att;
|
||||||
|
|
||||||
att = TupleDescAttr(RelationGetDescr(rel), key->partattrs[i] - 1);
|
att = TupleDescAttr(RelationGetDescr(rel),
|
||||||
|
key->partattrs[i] - 1);
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||||
errmsg("insufficient columns in %s constraint definition",
|
errmsg("insufficient columns in %s constraint definition",
|
||||||
|
Loading…
Reference in New Issue
Block a user