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 @@ -15095,6 +15095,7 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
1509515095 {
1509615096 Oid idxid = lfirst_oid (cell );
1509715097 Relation idx ;
15098+ Oid constrOid ;
1509815099
1509915100 if (!has_superclass (idxid ))
1510015101 continue ;
@@ -15106,6 +15107,23 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
1510615107 IndexSetParentIndex (idx , InvalidOid );
1510715108 update_relispartition (classRel , idxid , false);
1510815109 index_close (idx , NoLock );
15110+
15111+ /*
15112+ * Detach any constraints associated with the index too. Only UNIQUE
15113+ * and PRIMARY KEY index constraints can be inherited, so no need
15114+ * to check for others.
15115+ */
15116+ if (!idx -> rd_index -> indisprimary && !idx -> rd_index -> indisunique )
15117+ continue ;
15118+
15119+ constrOid = get_relation_idx_constraint_oid (RelationGetRelid (partRel ),
15120+ idxid );
15121+ if (!OidIsValid (constrOid ))
15122+ elog (ERROR , "missing pg_constraint entry of index \"%s\" of partition \"%s\"" ,
15123+ RelationGetRelationName (idx ),
15124+ RelationGetRelationName (partRel ));
15125+
15126+ ConstraintSetParentConstraint (constrOid , InvalidOid );
1510915127 }
1511015128 table_close (classRel , RowExclusiveLock );
1511115129
Original file line number Diff line number Diff line change @@ -1414,3 +1414,18 @@ DETAIL: Key (a)=(4) already exists.
14141414create unique index on covidxpart (b) include (a); -- should fail
14151415ERROR: insufficient columns in UNIQUE constraint definition
14161416DETAIL: UNIQUE constraint on table "covidxpart" lacks column "a" which is part of the partition key.
1417+ -- check that detaching a partition also detaches the primary key constraint
1418+ create table parted_pk_detach_test (a int primary key) partition by list (a);
1419+ create table parted_pk_detach_test1 partition of parted_pk_detach_test for values in (1);
1420+ alter table parted_pk_detach_test1 drop constraint parted_pk_detach_test1_pkey; -- should fail
1421+ ERROR: cannot drop inherited constraint "parted_pk_detach_test1_pkey" of relation "parted_pk_detach_test1"
1422+ alter table parted_pk_detach_test detach partition parted_pk_detach_test1;
1423+ alter table parted_pk_detach_test1 drop constraint parted_pk_detach_test1_pkey;
1424+ drop table parted_pk_detach_test, parted_pk_detach_test1;
1425+ create table parted_uniq_detach_test (a int unique) partition by list (a);
1426+ create table parted_uniq_detach_test1 partition of parted_uniq_detach_test for values in (1);
1427+ alter table parted_uniq_detach_test1 drop constraint parted_uniq_detach_test1_a_key; -- should fail
1428+ ERROR: cannot drop inherited constraint "parted_uniq_detach_test1_a_key" of relation "parted_uniq_detach_test1"
1429+ alter table parted_uniq_detach_test detach partition parted_uniq_detach_test1;
1430+ alter table parted_uniq_detach_test1 drop constraint parted_uniq_detach_test1_a_key;
1431+ drop table parted_uniq_detach_test, parted_uniq_detach_test1;
Original file line number Diff line number Diff line change @@ -757,3 +757,17 @@ alter table covidxpart attach partition covidxpart4 for values in (4);
757757insert into covidxpart values (4 , 1 );
758758insert into covidxpart values (4 , 1 );
759759create unique index on covidxpart (b) include (a); -- should fail
760+
761+ -- check that detaching a partition also detaches the primary key constraint
762+ create table parted_pk_detach_test (a int primary key ) partition by list (a);
763+ create table parted_pk_detach_test1 partition of parted_pk_detach_test for values in (1 );
764+ alter table parted_pk_detach_test1 drop constraint parted_pk_detach_test1_pkey; -- should fail
765+ alter table parted_pk_detach_test detach partition parted_pk_detach_test1;
766+ alter table parted_pk_detach_test1 drop constraint parted_pk_detach_test1_pkey;
767+ drop table parted_pk_detach_test, parted_pk_detach_test1;
768+ create table parted_uniq_detach_test (a int unique) partition by list (a);
769+ create table parted_uniq_detach_test1 partition of parted_uniq_detach_test for values in (1 );
770+ alter table parted_uniq_detach_test1 drop constraint parted_uniq_detach_test1_a_key; -- should fail
771+ alter table parted_uniq_detach_test detach partition parted_uniq_detach_test1;
772+ alter table parted_uniq_detach_test1 drop constraint parted_uniq_detach_test1_a_key;
773+ drop table parted_uniq_detach_test, parted_uniq_detach_test1;
You can’t perform that action at this time.
0 commit comments