Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 00376ea

Browse files
committed
Detach constraints when partitions are detached
I (Álvaro) forgot to do this in eb7ed3f, leading to undroppable constraints after partitions are detached. Repair. Reported-by: Amit Langote Author: Amit Langote Discussion: https://postgr.es/m/c1c9b688-b886-84f7-4048-1e4ebe9b1d06@lab.ntt.co.jp
1 parent 0359d83 commit 00376ea

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/test/regress/expected/indexing.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,3 +1387,18 @@ DETAIL: Key (a)=(4) already exists.
13871387
create unique index on covidxpart (b) include (a); -- should fail
13881388
ERROR: insufficient columns in UNIQUE constraint definition
13891389
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;

src/test/regress/sql/indexing.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,3 +740,17 @@ alter table covidxpart attach partition covidxpart4 for values in (4);
740740
insert into covidxpart values (4, 1);
741741
insert into covidxpart values (4, 1);
742742
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;

0 commit comments

Comments
 (0)