Skip to content

Commit ae366aa

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 289198c commit ae366aa

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
@@ -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

src/test/regress/expected/indexing.out

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

src/test/regress/sql/indexing.sql

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy