Skip to content

Commit 72d0642

Browse files
committed
Invalidate partitions of table being attached/detached
Failing to do that, any direct inserts/updates of those partitions would fail to enforce the correct constraint, that is, one that considers the new partition constraint of their parent table. Backpatch to 10. Reported by: Hou Zhijie <houzj.fnst@fujitsu.com> Author: Amit Langote <amitlangote09@gmail.com> Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Nitin Jadhav <nitinjadhavpostgres@gmail.com> Reviewed-by: Pavel Borisov <pashkin.elfe@gmail.com> Discussion: https://postgr.es/m/OS3PR01MB5718DA1C4609A25186D1FBF194089%40OS3PR01MB5718.jpnprd01.prod.outlook.com
1 parent 5b353aa commit 72d0642

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17307,6 +17307,22 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd,
1730717307

1730817308
ObjectAddressSet(address, RelationRelationId, RelationGetRelid(attachrel));
1730917309

17310+
/*
17311+
* If the partition we just attached is partitioned itself, invalidate
17312+
* relcache for all descendent partitions too to ensure that their
17313+
* rd_partcheck expression trees are rebuilt; partitions already locked
17314+
* at the beginning of this function.
17315+
*/
17316+
if (attachrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
17317+
{
17318+
ListCell *l;
17319+
17320+
foreach(l, attachrel_children)
17321+
{
17322+
CacheInvalidateRelcacheByRelid(lfirst_oid(l));
17323+
}
17324+
}
17325+
1731017326
/* keep our lock until commit */
1731117327
table_close(attachrel, NoLock);
1731217328

@@ -17997,6 +18013,25 @@ DetachPartitionFinalize(Relation rel, Relation partRel, bool concurrent,
1799718013
* included in its partition descriptor.
1799818014
*/
1799918015
CacheInvalidateRelcache(rel);
18016+
18017+
/*
18018+
* If the partition we just detached is partitioned itself, invalidate
18019+
* relcache for all descendent partitions too to ensure that their
18020+
* rd_partcheck expression trees are rebuilt; must lock partitions
18021+
* before doing so, using the same lockmode as what partRel has been
18022+
* locked with by the caller.
18023+
*/
18024+
if (partRel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
18025+
{
18026+
List *children;
18027+
18028+
children = find_all_inheritors(RelationGetRelid(partRel),
18029+
AccessExclusiveLock, NULL);
18030+
foreach(cell, children)
18031+
{
18032+
CacheInvalidateRelcacheByRelid(lfirst_oid(cell));
18033+
}
18034+
}
1800018035
}
1800118036

1800218037
/*

src/test/regress/expected/alter_table.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,3 +4496,23 @@ select indexrelid::regclass, indisclustered from pg_index
44964496
(2 rows)
44974497

44984498
drop table alttype_cluster;
4499+
--
4500+
-- Check that attaching or detaching a partitioned partition correctly leads
4501+
-- to its partitions' constraint being updated to reflect the parent's
4502+
-- newly added/removed constraint
4503+
create table target_parted (a int, b int) partition by list (a);
4504+
create table attach_parted (a int, b int) partition by list (b);
4505+
create table attach_parted_part1 partition of attach_parted for values in (1);
4506+
-- insert a row directly into the leaf partition so that its partition
4507+
-- constraint is built and stored in the relcache
4508+
insert into attach_parted_part1 values (1, 1);
4509+
-- the following better invalidate the partition constraint of the leaf
4510+
-- partition too...
4511+
alter table target_parted attach partition attach_parted for values in (1);
4512+
-- ...such that the following insert fails
4513+
insert into attach_parted_part1 values (2, 1);
4514+
ERROR: new row for relation "attach_parted_part1" violates partition constraint
4515+
DETAIL: Failing row contains (2, 1).
4516+
-- ...and doesn't when the partition is detached along with its own partition
4517+
alter table target_parted detach partition attach_parted;
4518+
insert into attach_parted_part1 values (2, 1);

src/test/regress/sql/alter_table.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,3 +2949,22 @@ select indexrelid::regclass, indisclustered from pg_index
29492949
where indrelid = 'alttype_cluster'::regclass
29502950
order by indexrelid::regclass::text;
29512951
drop table alttype_cluster;
2952+
2953+
--
2954+
-- Check that attaching or detaching a partitioned partition correctly leads
2955+
-- to its partitions' constraint being updated to reflect the parent's
2956+
-- newly added/removed constraint
2957+
create table target_parted (a int, b int) partition by list (a);
2958+
create table attach_parted (a int, b int) partition by list (b);
2959+
create table attach_parted_part1 partition of attach_parted for values in (1);
2960+
-- insert a row directly into the leaf partition so that its partition
2961+
-- constraint is built and stored in the relcache
2962+
insert into attach_parted_part1 values (1, 1);
2963+
-- the following better invalidate the partition constraint of the leaf
2964+
-- partition too...
2965+
alter table target_parted attach partition attach_parted for values in (1);
2966+
-- ...such that the following insert fails
2967+
insert into attach_parted_part1 values (2, 1);
2968+
-- ...and doesn't when the partition is detached along with its own partition
2969+
alter table target_parted detach partition attach_parted;
2970+
insert into attach_parted_part1 values (2, 1);

0 commit comments

Comments
 (0)