File tree Expand file tree Collapse file tree 3 files changed +30
-7
lines changed Expand file tree Collapse file tree 3 files changed +30
-7
lines changed Original file line number Diff line number Diff line change @@ -218,12 +218,12 @@ index_check_primary_key(Relation heapRel,
218218 int i ;
219219
220220 /*
221- * If ALTER TABLE, check that there isn't already a PRIMARY KEY. In CREATE
222- * TABLE, we have faith that the parser rejected multiple pkey clauses;
223- * and CREATE INDEX doesn't have a way to say PRIMARY KEY, so it's no
224- * problem either.
221+ * If ALTER TABLE and CREATE TABLE .. PARTITION OF , check that there isn't
222+ * already a PRIMARY KEY. In CREATE TABLE for an ordinary relations, we
223+ * have faith that the parser rejected multiple pkey clauses; and CREATE
224+ * INDEX doesn't have a way to say PRIMARY KEY, so it's no problem either.
225225 */
226- if (is_alter_table &&
226+ if (( is_alter_table || heapRel -> rd_rel -> relispartition ) &&
227227 relationHasPrimaryKey (heapRel ))
228228 {
229229 ereport (ERROR ,
Original file line number Diff line number Diff line change @@ -800,8 +800,24 @@ Indexes:
800800 "idxpart_pkey" PRIMARY KEY, btree (a)
801801Number of partitions: 0
802802
803+ -- multiple primary key on child should fail
804+ create table failpart partition of idxpart (b primary key) for values from (0) to (100);
805+ ERROR: multiple primary keys for table "failpart" are not allowed
803806drop table idxpart;
804- -- but not if you fail to use the full partition key
807+ -- primary key on child is okay if there's no PK in the parent, though
808+ create table idxpart (a int) partition by range (a);
809+ create table idxpart1pk partition of idxpart (a primary key) for values from (0) to (100);
810+ \d idxpart1pk
811+ Table "public.idxpart1pk"
812+ Column | Type | Collation | Nullable | Default
813+ --------+---------+-----------+----------+---------
814+ a | integer | | not null |
815+ Partition of: idxpart FOR VALUES FROM (0) TO (100)
816+ Indexes:
817+ "idxpart1pk_pkey" PRIMARY KEY, btree (a)
818+
819+ drop table idxpart;
820+ -- Failing to use the full partition key is not allowed
805821create table idxpart (a int unique, b int) partition by range (a, b);
806822ERROR: insufficient columns in UNIQUE constraint definition
807823DETAIL: UNIQUE constraint on table "idxpart" lacks column "b" which is part of the partition key.
Original file line number Diff line number Diff line change @@ -401,9 +401,16 @@ drop table idxpart;
401401-- Verify that it works to add primary key / unique to partitioned tables
402402create table idxpart (a int primary key , b int ) partition by range (a);
403403\d idxpart
404+ -- multiple primary key on child should fail
405+ create table failpart partition of idxpart (b primary key ) for values from (0 ) to (100 );
406+ drop table idxpart;
407+ -- primary key on child is okay if there's no PK in the parent, though
408+ create table idxpart (a int ) partition by range (a);
409+ create table idxpart1pk partition of idxpart (a primary key ) for values from (0 ) to (100 );
410+ \d idxpart1pk
404411drop table idxpart;
405412
406- -- but not if you fail to use the full partition key
413+ -- Failing to use the full partition key is not allowed
407414create table idxpart (a int unique, b int ) partition by range (a, b);
408415create table idxpart (a int , b int unique) partition by range (a, b);
409416create table idxpart (a int primary key , b int ) partition by range (b, a);
You can’t perform that action at this time.
0 commit comments