Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitae366aa

Browse files
committed
Detach constraints when partitions are detached
I (Álvaro) forgot to do this ineb7ed3f, leading to undroppableconstraints after partitions are detached. Repair.Reported-by: Amit LangoteAuthor: Amit LangoteDiscussion:https://postgr.es/m/c1c9b688-b886-84f7-4048-1e4ebe9b1d06@lab.ntt.co.jp
1 parent289198c commitae366aa

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
Oididxid=lfirst_oid(cell);
1509715097
Relationidx;
15098+
OidconstrOid;
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 covidxpartvalues (4,1);
758758
insert into covidxpartvalues (4,1);
759759
createunique indexon covidxpart (b) include (a);-- should fail
760+
761+
-- check that detaching a partition also detaches the primary key constraint
762+
createtableparted_pk_detach_test (aintprimary key) partition by list (a);
763+
createtableparted_pk_detach_test1 partition of parted_pk_detach_test forvaluesin (1);
764+
altertable parted_pk_detach_test1 dropconstraint parted_pk_detach_test1_pkey;-- should fail
765+
altertable parted_pk_detach_test detach partition parted_pk_detach_test1;
766+
altertable parted_pk_detach_test1 dropconstraint parted_pk_detach_test1_pkey;
767+
droptable parted_pk_detach_test, parted_pk_detach_test1;
768+
createtableparted_uniq_detach_test (aint unique) partition by list (a);
769+
createtableparted_uniq_detach_test1 partition of parted_uniq_detach_test forvaluesin (1);
770+
altertable parted_uniq_detach_test1 dropconstraint parted_uniq_detach_test1_a_key;-- should fail
771+
altertable parted_uniq_detach_test detach partition parted_uniq_detach_test1;
772+
altertable parted_uniq_detach_test1 dropconstraint parted_uniq_detach_test1_a_key;
773+
droptable parted_uniq_detach_test, parted_uniq_detach_test1;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp