File tree 3 files changed +47
-0
lines changed
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)
15233
15233
{
15234
15234
Oid idxid = lfirst_oid (cell );
15235
15235
Relation idx ;
15236
+ Oid constrOid ;
15236
15237
15237
15238
if (!has_superclass (idxid ))
15238
15239
continue ;
@@ -15244,6 +15245,23 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
15244
15245
IndexSetParentIndex (idx , InvalidOid );
15245
15246
update_relispartition (classRel , idxid , false);
15246
15247
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 );
15247
15265
}
15248
15266
heap_close (classRel , RowExclusiveLock );
15249
15267
Original file line number Diff line number Diff line change @@ -1387,3 +1387,18 @@ DETAIL: Key (a)=(4) already exists.
1387
1387
create unique index on covidxpart (b) include (a); -- should fail
1388
1388
ERROR: insufficient columns in UNIQUE constraint definition
1389
1389
DETAIL: 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);
740
740
insert into covidxpart values (4 , 1 );
741
741
insert into covidxpart values (4 , 1 );
742
742
create 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