Fix unique INCLUDE indexes on partitioned tables
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 14 Jan 2019 22:25:19 +0000 (19:25 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 14 Jan 2019 22:28:10 +0000 (19:28 -0300)
We were considering the INCLUDE columns as part of the key, allowing
unicity-violating rows to be inserted in different partitions.

Concurrent development conflict in eb7ed3f30634 and 8224de4f42cc.

Reported-by: Justin Pryzby
Discussion: https://postgr.es/m/20190109065109.GA4285@telsasoft.com

src/backend/commands/indexcmds.c
src/test/regress/expected/indexing.out
src/test/regress/sql/indexing.sql

index d2639036223f317841ec45f851f8b6cc20e2693d..c8c64d9a425e9439d597939f6bbb80655b9f86e1 100644 (file)
@@ -721,7 +721,7 @@ DefineIndex(Oid relationId,
                         errdetail("%s constraints cannot be used when partition keys include expressions.",
                                   constraint_type)));
 
-           for (j = 0; j < indexInfo->ii_NumIndexAttrs; j++)
+           for (j = 0; j < indexInfo->ii_NumIndexKeyAttrs; j++)
            {
                if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
                {
index caacf3f7991748df283765c4f782920dae984864..118f2c78df44236a85dfba7ac2e0a28553bc61ee 100644 (file)
@@ -1411,3 +1411,6 @@ insert into covidxpart values (4, 1);
 insert into covidxpart values (4, 1);
 ERROR:  duplicate key value violates unique constraint "covidxpart4_a_b_idx"
 DETAIL:  Key (a)=(4) already exists.
+create unique index on covidxpart (b) include (a); -- should fail
+ERROR:  insufficient columns in UNIQUE constraint definition
+DETAIL:  UNIQUE constraint on table "covidxpart" lacks column "a" which is part of the partition key.
index 6878cde50986f38736dcaa9b8286360ee0349563..d4a64c18c7e1df6f94e5f98056479ed281437b1d 100644 (file)
@@ -756,3 +756,4 @@ create unique index on covidxpart4 (a);
 alter table covidxpart attach partition covidxpart4 for values in (4);
 insert into covidxpart values (4, 1);
 insert into covidxpart values (4, 1);
+create unique index on covidxpart (b) include (a); -- should fail