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

Commit00376ea

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 parent0359d83 commit00376ea

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
Oididxid=lfirst_oid(cell);
1523515235
Relationidx;
15236+
OidconstrOid;
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 covidxpartvalues (4,1);
741741
insert into covidxpartvalues (4,1);
742742
createunique indexon covidxpart (b) include (a);-- should fail
743+
744+
-- check that detaching a partition also detaches the primary key constraint
745+
createtableparted_pk_detach_test (aintprimary key) partition by list (a);
746+
createtableparted_pk_detach_test1 partition of parted_pk_detach_test forvaluesin (1);
747+
altertable parted_pk_detach_test1 dropconstraint parted_pk_detach_test1_pkey;-- should fail
748+
altertable parted_pk_detach_test detach partition parted_pk_detach_test1;
749+
altertable parted_pk_detach_test1 dropconstraint parted_pk_detach_test1_pkey;
750+
droptable parted_pk_detach_test, parted_pk_detach_test1;
751+
createtableparted_uniq_detach_test (aint unique) partition by list (a);
752+
createtableparted_uniq_detach_test1 partition of parted_uniq_detach_test forvaluesin (1);
753+
altertable parted_uniq_detach_test1 dropconstraint parted_uniq_detach_test1_a_key;-- should fail
754+
altertable parted_uniq_detach_test detach partition parted_uniq_detach_test1;
755+
altertable parted_uniq_detach_test1 dropconstraint parted_uniq_detach_test1_a_key;
756+
droptable parted_uniq_detach_test, parted_uniq_detach_test1;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp