Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit501b018

Browse files
committed
Check equality semantics for unique indexes on partitioned tables.
We require the partition key to be a subset of the set of columnsbeing made unique, so that physically-separate indexes on the differentpartitions are sufficient to enforce the uniqueness constraint.The existing code checked that the listed columns appear, but did notinquire into the index semantics, which is a serious oversight giventhat different index opclasses might enforce completely differentnotions of uniqueness.Ideally, perhaps, we'd just match the partition key opfamily to theindex opfamily. But hash partitioning uses hash opfamilies which wecan't directly match to btree opfamilies. Hence, look up the equalityoperator in each family, and accept if it's the same operator. Thisshould be okay in a fairly general sense, since the equality operatorought to precisely represent the opfamily's notion of uniqueness.A remaining weak spot is that we don't have a cross-index-AM notion ofwhich opfamily member is "equality". But we know which one to use forhash and btree AMs, and those are the only two that are relevant hereat present. (Any non-core AMs that know how to enforce equality areout of luck, for now.)Back-patch to v11 where this feature was introduced.Guancheng Luo, revised a bit by meDiscussion:https://postgr.es/m/D9C3CEF7-04E8-47A1-8300-CA1DCD5ED40D@gmail.com
1 parenta808186 commit501b018

File tree

1 file changed

+77
-25
lines changed

1 file changed

+77
-25
lines changed

‎src/backend/commands/indexcmds.c

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -871,44 +871,75 @@ DefineIndex(Oid relationId,
871871

872872
/*
873873
* If this table is partitioned and we're creating a unique index or a
874-
* primary key, make sure that theindexed columns are part of the
875-
*partition key. Otherwise it would be possible to violate uniqueness by
876-
* putting values that ought to be unique in different partitions.
874+
* primary key, make sure that thepartition key is a subset of the
875+
*index's columns. Otherwise it would be possible to violate uniqueness
876+
*byputting values that ought to be unique in different partitions.
877877
*
878878
* We could lift this limitation if we had global indexes, but those have
879879
* their own problems, so this is a useful feature combination.
880880
*/
881881
if (partitioned&& (stmt->unique||stmt->primary))
882882
{
883883
PartitionKeykey=RelationGetPartitionKey(rel);
884+
constchar*constraint_type;
884885
inti;
885886

887+
if (stmt->primary)
888+
constraint_type="PRIMARY KEY";
889+
elseif (stmt->unique)
890+
constraint_type="UNIQUE";
891+
elseif (stmt->excludeOpNames!=NIL)
892+
constraint_type="EXCLUDE";
893+
else
894+
{
895+
elog(ERROR,"unknown constraint type");
896+
constraint_type=NULL;/* keep compiler quiet */
897+
}
898+
886899
/*
887-
* A partitioned table can have unique indexes, as long as all the
888-
* columns in the partition key appear in the unique key. A
889-
* partition-local index can enforce global uniqueness iff the PK
890-
* value completely determines the partition that a row is in.
891-
*
892-
* Thus, verify that all the columns in the partition key appear in
893-
* the unique key definition.
900+
* Verify that all the columns in the partition key appear in the
901+
* unique key definition, with the same notion of equality.
894902
*/
895903
for (i=0;i<key->partnatts;i++)
896904
{
897905
boolfound= false;
906+
inteq_strategy;
907+
Oidptkey_eqop;
898908
intj;
899-
constchar*constraint_type;
900-
901-
if (stmt->primary)
902-
constraint_type="PRIMARY KEY";
903-
elseif (stmt->unique)
904-
constraint_type="UNIQUE";
905-
elseif (stmt->excludeOpNames!=NIL)
906-
constraint_type="EXCLUDE";
909+
910+
/*
911+
* Identify the equality operator associated with this partkey
912+
* column. For list and range partitioning, partkeys use btree
913+
* operator classes; hash partitioning uses hash operator classes.
914+
* (Keep this in sync with ComputePartitionAttrs!)
915+
*/
916+
if (key->strategy==PARTITION_STRATEGY_HASH)
917+
eq_strategy=HTEqualStrategyNumber;
907918
else
908-
{
909-
elog(ERROR,"unknown constraint type");
910-
constraint_type=NULL;/* keep compiler quiet */
911-
}
919+
eq_strategy=BTEqualStrategyNumber;
920+
921+
ptkey_eqop=get_opfamily_member(key->partopfamily[i],
922+
key->partopcintype[i],
923+
key->partopcintype[i],
924+
eq_strategy);
925+
if (!OidIsValid(ptkey_eqop))
926+
elog(ERROR,"missing operator %d(%u,%u) in partition opfamily %u",
927+
eq_strategy,key->partopcintype[i],key->partopcintype[i],
928+
key->partopfamily[i]);
929+
930+
/*
931+
* We'll need to be able to identify the equality operators
932+
* associated with index columns, too. We know what to do with
933+
* btree opclasses; if there are ever any other index types that
934+
* support unique indexes, this logic will need extension.
935+
*/
936+
if (accessMethodId==BTREE_AM_OID)
937+
eq_strategy=BTEqualStrategyNumber;
938+
else
939+
ereport(ERROR,
940+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
941+
errmsg("cannot match partition key to an index using access method \"%s\"",
942+
accessMethodName)));
912943

913944
/*
914945
* It may be possible to support UNIQUE constraints when partition
@@ -922,19 +953,40 @@ DefineIndex(Oid relationId,
922953
errdetail("%s constraints cannot be used when partition keys include expressions.",
923954
constraint_type)));
924955

956+
/* Search the index column(s) for a match */
925957
for (j=0;j<indexInfo->ii_NumIndexKeyAttrs;j++)
926958
{
927959
if (key->partattrs[i]==indexInfo->ii_IndexAttrNumbers[j])
928960
{
929-
found= true;
930-
break;
961+
/* Matched the column, now what about the equality op? */
962+
Oididx_opfamily;
963+
Oididx_opcintype;
964+
965+
if (get_opclass_opfamily_and_input_type(classObjectId[j],
966+
&idx_opfamily,
967+
&idx_opcintype))
968+
{
969+
Oididx_eqop;
970+
971+
idx_eqop=get_opfamily_member(idx_opfamily,
972+
idx_opcintype,
973+
idx_opcintype,
974+
eq_strategy);
975+
if (ptkey_eqop==idx_eqop)
976+
{
977+
found= true;
978+
break;
979+
}
980+
}
931981
}
932982
}
983+
933984
if (!found)
934985
{
935986
Form_pg_attributeatt;
936987

937-
att=TupleDescAttr(RelationGetDescr(rel),key->partattrs[i]-1);
988+
att=TupleDescAttr(RelationGetDescr(rel),
989+
key->partattrs[i]-1);
938990
ereport(ERROR,
939991
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
940992
errmsg("insufficient columns in %s constraint definition",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp