File tree Expand file tree Collapse file tree 3 files changed +47
-0
lines changed Expand file tree Collapse file tree 3 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -15233,6 +15233,7 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
1523315233 {
1523415234 Oid idxid = lfirst_oid (cell );
1523515235 Relation idx ;
15236+ Oid constrOid ;
1523615237
1523715238 if (!has_superclass (idxid ))
1523815239 continue ;
@@ -15244,6 +15245,23 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
1524415245 IndexSetParentIndex (idx , InvalidOid );
1524515246 update_relispartition (classRel , idxid , false);
1524615247 index_close (idx , NoLock );
15248+
15249+ /*
15250+ * Detach any constraints associated with the index too. Only UNIQUE
15251+ * and PRIMARY KEY index constraints can be inherited, so no need
15252+ * to check for others.
15253+ */
15254+ if (!idx -> rd_index -> indisprimary && !idx -> rd_index -> indisunique )
15255+ continue ;
15256+
15257+ constrOid = get_relation_idx_constraint_oid (RelationGetRelid (partRel ),
15258+ idxid );
15259+ if (!OidIsValid (constrOid ))
15260+ elog (ERROR , "missing pg_constraint entry of index \"%s\" of partition \"%s\"" ,
15261+ RelationGetRelationName (idx ),
15262+ RelationGetRelationName (partRel ));
15263+
15264+ ConstraintSetParentConstraint (constrOid , InvalidOid );
1524715265 }
1524815266 heap_close (classRel , RowExclusiveLock );
1524915267
Original file line number Diff line number Diff line change @@ -1387,3 +1387,18 @@ DETAIL: Key (a)=(4) already exists.
13871387create unique index on covidxpart (b) include (a); -- should fail
13881388ERROR: insufficient columns in UNIQUE constraint definition
13891389DETAIL: UNIQUE constraint on table "covidxpart" lacks column "a" which is part of the partition key.
1390+ -- check that detaching a partition also detaches the primary key constraint
1391+ create table parted_pk_detach_test (a int primary key) partition by list (a);
1392+ create table parted_pk_detach_test1 partition of parted_pk_detach_test for values in (1);
1393+ alter table parted_pk_detach_test1 drop constraint parted_pk_detach_test1_pkey; -- should fail
1394+ ERROR: cannot drop inherited constraint "parted_pk_detach_test1_pkey" of relation "parted_pk_detach_test1"
1395+ alter table parted_pk_detach_test detach partition parted_pk_detach_test1;
1396+ alter table parted_pk_detach_test1 drop constraint parted_pk_detach_test1_pkey;
1397+ drop table parted_pk_detach_test, parted_pk_detach_test1;
1398+ create table parted_uniq_detach_test (a int unique) partition by list (a);
1399+ create table parted_uniq_detach_test1 partition of parted_uniq_detach_test for values in (1);
1400+ alter table parted_uniq_detach_test1 drop constraint parted_uniq_detach_test1_a_key; -- should fail
1401+ ERROR: cannot drop inherited constraint "parted_uniq_detach_test1_a_key" of relation "parted_uniq_detach_test1"
1402+ alter table parted_uniq_detach_test detach partition parted_uniq_detach_test1;
1403+ alter table parted_uniq_detach_test1 drop constraint parted_uniq_detach_test1_a_key;
1404+ drop table parted_uniq_detach_test, parted_uniq_detach_test1;
Original file line number Diff line number Diff line change @@ -740,3 +740,17 @@ alter table covidxpart attach partition covidxpart4 for values in (4);
740740insert into covidxpart values (4 , 1 );
741741insert into covidxpart values (4 , 1 );
742742create unique index on covidxpart (b) include (a); -- should fail
743+
744+ -- check that detaching a partition also detaches the primary key constraint
745+ create table parted_pk_detach_test (a int primary key ) partition by list (a);
746+ create table parted_pk_detach_test1 partition of parted_pk_detach_test for values in (1 );
747+ alter table parted_pk_detach_test1 drop constraint parted_pk_detach_test1_pkey; -- should fail
748+ alter table parted_pk_detach_test detach partition parted_pk_detach_test1;
749+ alter table parted_pk_detach_test1 drop constraint parted_pk_detach_test1_pkey;
750+ drop table parted_pk_detach_test, parted_pk_detach_test1;
751+ create table parted_uniq_detach_test (a int unique) partition by list (a);
752+ create table parted_uniq_detach_test1 partition of parted_uniq_detach_test for values in (1 );
753+ alter table parted_uniq_detach_test1 drop constraint parted_uniq_detach_test1_a_key; -- should fail
754+ alter table parted_uniq_detach_test detach partition parted_uniq_detach_test1;
755+ alter table parted_uniq_detach_test1 drop constraint parted_uniq_detach_test1_a_key;
756+ drop table parted_uniq_detach_test, parted_uniq_detach_test1;
You can’t perform that action at this time.
0 commit comments