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

Commitdc3e436

Browse files
committed
Block creation of partitions with open references to its parent
When a partition is created as part of a trigger processing, it ispossible that the partition which just gets created changes theproperties of the table the executor of the ongoing command relies on,causing a subsequent crash. This has been found possible when forexample using a BEFORE INSERT which creates a new partition for apartitioned table being inserted to.Any attempt to do so is blocked when working on a partition, withregression tests added for both CREATE TABLE PARTITION OF and ALTERTABLE ATTACH PARTITION.Reported-by: Dmitry ShalashovAuthor: Amit LangoteReviewed-by: Michael Paquier, Tom LaneDiscussion:https://postgr.es/m/15437-3fe01ee66bd1bae1@postgresql.orgBackpatch-through: 10
1 parent4bc772e commitdc3e436

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,14 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
19931993
else
19941994
relation=heap_openrv(parent,AccessExclusiveLock);
19951995

1996+
/*
1997+
* Check for active uses of the parent partitioned table in the
1998+
* current transaction, such as being used in some manner by an
1999+
* enclosing command.
2000+
*/
2001+
if (is_partition)
2002+
CheckTableNotInUse(relation,"CREATE TABLE .. PARTITION OF");
2003+
19962004
/*
19972005
* We do not allow partitioned tables and partitions to participate in
19982006
* regular inheritance.

‎src/test/regress/expected/alter_table.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3954,6 +3954,24 @@ ERROR: cannot attach a temporary relation as partition of permanent relation "p
39543954
alter table temp_part_parent attach partition temp_part_child default; -- ok
39553955
drop table perm_part_parent cascade;
39563956
drop table temp_part_parent cascade;
3957+
-- check that attaching partitions to a table while it is being used is
3958+
-- prevented
3959+
create table tab_part_attach (a int) partition by list (a);
3960+
create or replace function func_part_attach() returns trigger
3961+
language plpgsql as $$
3962+
begin
3963+
execute 'create table tab_part_attach_1 (a int)';
3964+
execute 'alter table tab_part_attach attach partition tab_part_attach_1 for values in (1)';
3965+
return null;
3966+
end $$;
3967+
create trigger trig_part_attach before insert on tab_part_attach
3968+
for each statement execute procedure func_part_attach();
3969+
insert into tab_part_attach values (1);
3970+
ERROR: cannot ALTER TABLE "tab_part_attach" because it is being used by active queries in this session
3971+
CONTEXT: SQL statement "alter table tab_part_attach attach partition tab_part_attach_1 for values in (1)"
3972+
PL/pgSQL function func_part_attach() line 4 at EXECUTE
3973+
drop table tab_part_attach;
3974+
drop function func_part_attach();
39573975
-- test case where the partitioning operator is a SQL function whose
39583976
-- evaluation results in the table's relcache being rebuilt partway through
39593977
-- the execution of an ATTACH PARTITION command

‎src/test/regress/expected/create_table.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,19 @@ ERROR: cannot create a temporary relation as partition of permanent relation "p
910910
create temp table temp_part partition of temp_parted default; -- ok
911911
drop table perm_parted cascade;
912912
drop table temp_parted cascade;
913+
-- check that adding partitions to a table while it is being used is prevented
914+
create table tab_part_create (a int) partition by list (a);
915+
create or replace function func_part_create() returns trigger
916+
language plpgsql as $$
917+
begin
918+
execute 'create table tab_part_create_1 partition of tab_part_create for values in (1)';
919+
return null;
920+
end $$;
921+
create trigger trig_part_create before insert on tab_part_create
922+
for each statement execute procedure func_part_create();
923+
insert into tab_part_create values (1);
924+
ERROR: cannot CREATE TABLE .. PARTITION OF "tab_part_create" because it is being used by active queries in this session
925+
CONTEXT: SQL statement "create table tab_part_create_1 partition of tab_part_create for values in (1)"
926+
PL/pgSQL function func_part_create() line 3 at EXECUTE
927+
drop table tab_part_create;
928+
drop function func_part_create();

‎src/test/regress/sql/alter_table.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,22 @@ alter table temp_part_parent attach partition temp_part_child default; -- ok
26072607
droptable perm_part_parent cascade;
26082608
droptable temp_part_parent cascade;
26092609

2610+
-- check that attaching partitions to a table while it is being used is
2611+
-- prevented
2612+
createtabletab_part_attach (aint) partition by list (a);
2613+
create or replacefunctionfunc_part_attach() returns trigger
2614+
language plpgsqlas $$
2615+
begin
2616+
execute'create table tab_part_attach_1 (a int)';
2617+
execute'alter table tab_part_attach attach partition tab_part_attach_1 for values in (1)';
2618+
returnnull;
2619+
end $$;
2620+
createtriggertrig_part_attach before inserton tab_part_attach
2621+
for each statement execute procedure func_part_attach();
2622+
insert into tab_part_attachvalues (1);
2623+
droptable tab_part_attach;
2624+
dropfunction func_part_attach();
2625+
26102626
-- test case where the partitioning operator is a SQL function whose
26112627
-- evaluation results in the table's relcache being rebuilt partway through
26122628
-- the execution of an ATTACH PARTITION command

‎src/test/regress/sql/create_table.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,17 @@ create temp table temp_part partition of perm_parted default; -- error
730730
create temp table temp_part partition of temp_parted default;-- ok
731731
droptable perm_parted cascade;
732732
droptable temp_parted cascade;
733+
734+
-- check that adding partitions to a table while it is being used is prevented
735+
createtabletab_part_create (aint) partition by list (a);
736+
create or replacefunctionfunc_part_create() returns trigger
737+
language plpgsqlas $$
738+
begin
739+
execute'create table tab_part_create_1 partition of tab_part_create for values in (1)';
740+
returnnull;
741+
end $$;
742+
createtriggertrig_part_create before inserton tab_part_create
743+
for each statement execute procedure func_part_create();
744+
insert into tab_part_createvalues (1);
745+
droptable tab_part_create;
746+
dropfunction func_part_create();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp