Skip to content

Commit 5862174

Browse files
committed
Clarify use of temporary tables within partition trees
Since their introduction, partition trees have been a bit lossy regarding temporary relations. Inheritance trees respect the following patterns: 1) a child relation can be temporary if the parent is permanent. 2) a child relation can be temporary if the parent is temporary. 3) a child relation cannot be permanent if the parent is temporary. 4) The use of temporary relations also imply that when both parent and child need to be from the same sessions. Partitions share many similar patterns with inheritance, however the handling of the partition bounds make the situation a bit tricky for case 1) as the partition code bases a lot of its lookup code upon PartitionDesc which does not really look after relpersistence. This causes for example a temporary partition created by session A to be visible by another session B, preventing this session B to create an extra partition which overlaps with the temporary one created by A with a non-intuitive error message. There could be use-cases where mixing permanent partitioned tables with temporary partitions make sense, but that would be a new feature. Partitions respect 2), 3) and 4) already. It is a bit depressing to see those error checks happening in MergeAttributes() whose purpose is different, but that's left as future refactoring work. Back-patch down to 10, which is where partitioning has been introduced, except that default partitions do not apply there. Documentation also includes limitations related to the use of temporary tables with partition trees. Reported-by: David Rowley Author: Amit Langote, Michael Paquier Reviewed-by: Ashutosh Bapat, Amit Langote, Michael Paquier Discussion: https://postgr.es/m/CAKJS1f94Ojk0og9GMkRHGt8wHTW=ijq5KzJKuoBoqWLwSVwGmw@mail.gmail.com
1 parent a6fb937 commit 5862174

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,16 @@ ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
33373337
not the partitioned table.
33383338
</para>
33393339
</listitem>
3340+
3341+
<listitem>
3342+
<para>
3343+
Mixing temporary and permanent relations in the same partition tree is
3344+
not allowed. Hence, if the partitioned table is permanent, so must be
3345+
its partitions and likewise if the partitioned table is temporary. When
3346+
using temporary relations, all members of the partition tree have to be
3347+
from the same session.
3348+
</para>
3349+
</listitem>
33403350
</itemizedlist>
33413351
</para>
33423352
</sect3>

src/backend/commands/tablecmds.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,19 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
17731773
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
17741774
errmsg("inherited relation \"%s\" is not a table or foreign table",
17751775
parent->relname)));
1776+
1777+
/*
1778+
* If the parent is permanent, so must be all of its partitions. Note
1779+
* that inheritance allows that case.
1780+
*/
1781+
if (is_partition &&
1782+
relation->rd_rel->relpersistence != RELPERSISTENCE_TEMP &&
1783+
relpersistence == RELPERSISTENCE_TEMP)
1784+
ereport(ERROR,
1785+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1786+
errmsg("cannot create a temporary relation as partition of permanent relation \"%s\"",
1787+
RelationGetRelationName(relation))));
1788+
17761789
/* Permanent rels cannot inherit from temporary ones */
17771790
if (relpersistence != RELPERSISTENCE_TEMP &&
17781791
relation->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
@@ -13539,6 +13552,14 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd)
1353913552
RelationGetRelationName(rel),
1354013553
RelationGetRelationName(attachrel))));
1354113554

13555+
/* If the parent is permanent, so must be all of its partitions. */
13556+
if (rel->rd_rel->relpersistence != RELPERSISTENCE_TEMP &&
13557+
attachrel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
13558+
ereport(ERROR,
13559+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
13560+
errmsg("cannot attach a temporary relation as partition of permanent relation \"%s\"",
13561+
RelationGetRelationName(rel))));
13562+
1354213563
/* Temp parent cannot have a partition that is itself not a temp */
1354313564
if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP &&
1354413565
attachrel->rd_rel->relpersistence != RELPERSISTENCE_TEMP)

src/test/regress/expected/alter_table.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,3 +3653,19 @@ create table parted_validate_test_1 partition of parted_validate_test for values
36533653
alter table parted_validate_test add constraint parted_validate_test_chka check (a > 0) not valid;
36543654
alter table parted_validate_test validate constraint parted_validate_test_chka;
36553655
drop table parted_validate_test;
3656+
-- check combinations of temporary and permanent relations when attaching
3657+
-- partitions.
3658+
create table perm_part_parent (a int) partition by list (a);
3659+
create temp table temp_part_parent (a int) partition by list (a);
3660+
create table perm_part_child (a int);
3661+
create temp table temp_part_child (a int);
3662+
alter table temp_part_parent attach partition perm_part_child
3663+
for values in (1, 2); -- error
3664+
ERROR: cannot attach a permanent relation as partition of temporary relation "temp_part_parent"
3665+
alter table perm_part_parent attach partition temp_part_child
3666+
for values in (1, 2); -- error
3667+
ERROR: cannot attach a temporary relation as partition of permanent relation "perm_part_parent"
3668+
alter table temp_part_parent attach partition temp_part_child
3669+
for values in (1, 2); -- ok
3670+
drop table perm_part_parent cascade;
3671+
drop table temp_part_parent cascade;

src/test/regress/expected/create_table.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,3 +825,13 @@ Partitions: boolspart_f FOR VALUES IN (false),
825825
boolspart_t FOR VALUES IN (true)
826826

827827
drop table boolspart;
828+
-- partitions mixing temporary and permanent relations
829+
create table perm_parted (a int) partition by list (a);
830+
create temporary table temp_parted (a int) partition by list (a);
831+
create table perm_part partition of temp_parted for values in (1, 2); -- error
832+
ERROR: cannot create a permanent relation as partition of temporary relation "temp_parted"
833+
create temp table temp_part partition of perm_parted for values in (1, 2); -- error
834+
ERROR: cannot create a temporary relation as partition of permanent relation "perm_parted"
835+
create temp table temp_part partition of temp_parted for values in (1, 2); -- ok
836+
drop table perm_parted cascade;
837+
drop table temp_parted cascade;

src/test/regress/expected/foreign_data.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,18 @@ TRUNCATE pt2; -- ERROR
20192019
ERROR: "pt2_1" is not a table
20202020
DROP FOREIGN TABLE pt2_1;
20212021
DROP TABLE pt2;
2022+
-- foreign table cannot be part of partition tree made of temporary
2023+
-- relations.
2024+
CREATE TEMP TABLE temp_parted (a int) PARTITION BY LIST (a);
2025+
CREATE FOREIGN TABLE foreign_part PARTITION OF temp_parted
2026+
FOR VALUES IN (1, 2) SERVER s0; -- ERROR
2027+
ERROR: cannot create a permanent relation as partition of temporary relation "temp_parted"
2028+
CREATE FOREIGN TABLE foreign_part (a int) SERVER s0;
2029+
ALTER TABLE temp_parted ATTACH PARTITION foreign_part
2030+
FOR VALUES IN (1, 2); -- ERROR
2031+
ERROR: cannot attach a permanent relation as partition of temporary relation "temp_parted"
2032+
DROP FOREIGN TABLE foreign_part;
2033+
DROP TABLE temp_parted;
20222034
-- Cleanup
20232035
DROP SCHEMA foreign_schema CASCADE;
20242036
DROP ROLE regress_test_role; -- ERROR

src/test/regress/sql/alter_table.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,3 +2373,18 @@ create table parted_validate_test_1 partition of parted_validate_test for values
23732373
alter table parted_validate_test add constraint parted_validate_test_chka check (a > 0) not valid;
23742374
alter table parted_validate_test validate constraint parted_validate_test_chka;
23752375
drop table parted_validate_test;
2376+
2377+
-- check combinations of temporary and permanent relations when attaching
2378+
-- partitions.
2379+
create table perm_part_parent (a int) partition by list (a);
2380+
create temp table temp_part_parent (a int) partition by list (a);
2381+
create table perm_part_child (a int);
2382+
create temp table temp_part_child (a int);
2383+
alter table temp_part_parent attach partition perm_part_child
2384+
for values in (1, 2); -- error
2385+
alter table perm_part_parent attach partition temp_part_child
2386+
for values in (1, 2); -- error
2387+
alter table temp_part_parent attach partition temp_part_child
2388+
for values in (1, 2); -- ok
2389+
drop table perm_part_parent cascade;
2390+
drop table temp_part_parent cascade;

src/test/regress/sql/create_table.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,12 @@ create table boolspart_t partition of boolspart for values in (true);
675675
create table boolspart_f partition of boolspart for values in (false);
676676
\d+ boolspart
677677
drop table boolspart;
678+
679+
-- partitions mixing temporary and permanent relations
680+
create table perm_parted (a int) partition by list (a);
681+
create temporary table temp_parted (a int) partition by list (a);
682+
create table perm_part partition of temp_parted for values in (1, 2); -- error
683+
create temp table temp_part partition of perm_parted for values in (1, 2); -- error
684+
create temp table temp_part partition of temp_parted for values in (1, 2); -- ok
685+
drop table perm_parted cascade;
686+
drop table temp_parted cascade;

src/test/regress/sql/foreign_data.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,17 @@ TRUNCATE pt2; -- ERROR
786786
DROP FOREIGN TABLE pt2_1;
787787
DROP TABLE pt2;
788788

789+
-- foreign table cannot be part of partition tree made of temporary
790+
-- relations.
791+
CREATE TEMP TABLE temp_parted (a int) PARTITION BY LIST (a);
792+
CREATE FOREIGN TABLE foreign_part PARTITION OF temp_parted
793+
FOR VALUES IN (1, 2) SERVER s0; -- ERROR
794+
CREATE FOREIGN TABLE foreign_part (a int) SERVER s0;
795+
ALTER TABLE temp_parted ATTACH PARTITION foreign_part
796+
FOR VALUES IN (1, 2); -- ERROR
797+
DROP FOREIGN TABLE foreign_part;
798+
DROP TABLE temp_parted;
799+
789800
-- Cleanup
790801
DROP SCHEMA foreign_schema CASCADE;
791802
DROP ROLE regress_test_role; -- ERROR

0 commit comments

Comments
 (0)