Skip to content

Navigation Menu

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

Appearance settings

Commit c086896

Browse filesBrowse files
committed
Fix tablespace handling in MERGE/SPLIT partition commands.
As commit ca41030 stated, new partitions without a specified tablespace should inherit the parent relation's tablespace. However, previously, ALTER TABLE MERGE PARTITIONS and ALTER TABLE SPLIT PARTITION commands always created new partitions in the default tablespace, ignoring the parent's tablespace. This commit ensures new partitions inherit the parent's tablespace. Backpatch to v17 where these commands were introduced. Author: Fujii Masao Reviewed-by: Masahiko Sawada Discussion: https://postgr.es/m/abaf390b-3320-40a5-8815-ef476db5cfe7@oss.nttdata.com
1 parent 069d0ff commit c086896
Copy full SHA for c086896

File tree

6 files changed

+79
-3
lines changed
Filter options

6 files changed

+79
-3
lines changed

‎doc/src/sgml/ref/alter_table.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/alter_table.sgml
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
11631163
New partitions will have the same table access method as the parent.
11641164
If the parent table is persistent then new partitions are created
11651165
persistent. If the parent table is temporary then new partitions
1166-
are also created temporary.
1166+
are also created temporary. New partitions will also be created in
1167+
the same tablespace as the parent.
11671168
</para>
11681169
<note>
11691170
<para>
@@ -1235,7 +1236,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
12351236
The new partition will have the same table access method as the parent.
12361237
If the parent table is persistent then the new partition is created
12371238
persistent. If the parent table is temporary then the new partition
1238-
is also created temporary.
1239+
is also created temporary. The new partition will also be created in
1240+
the same tablespace as the parent.
12391241
</para>
12401242
<note>
12411243
<para>

‎src/backend/commands/tablecmds.c

Copy file name to clipboardExpand all lines: src/backend/commands/tablecmds.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20340,7 +20340,7 @@ createPartitionTable(RangeVar *newPartName, Relation modelRel,
2034020340
createStmt->constraints = NIL;
2034120341
createStmt->options = NIL;
2034220342
createStmt->oncommit = ONCOMMIT_NOOP;
20343-
createStmt->tablespacename = NULL;
20343+
createStmt->tablespacename = get_tablespace_name(modelRel->rd_rel->reltablespace);
2034420344
createStmt->if_not_exists = false;
2034520345
createStmt->accessMethod = get_am_name(modelRel->rd_rel->relam);
2034620346

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

Copy file name to clipboardExpand all lines: src/test/regress/expected/partition_merge.out
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,29 @@ SET search_path = partitions_merge_schema, pg_temp, public;
861861
-- Can't merge temporary partitions into a persistent partition
862862
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
863863
ROLLBACK;
864+
-- Check the new partition inherits parent's tablespace
865+
CREATE TABLE t (i int PRIMARY KEY USING INDEX TABLESPACE regress_tblspace)
866+
PARTITION BY RANGE (i) TABLESPACE regress_tblspace;
867+
CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
868+
CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
869+
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
870+
SELECT tablename, tablespace FROM pg_tables
871+
WHERE tablename IN ('t', 'tp_0_2') ORDER BY tablename, tablespace;
872+
tablename | tablespace
873+
-----------+------------------
874+
t | regress_tblspace
875+
tp_0_2 | regress_tblspace
876+
(2 rows)
877+
878+
SELECT tablename, indexname, tablespace FROM pg_indexes
879+
WHERE tablename IN ('t', 'tp_0_2') ORDER BY tablename, indexname, tablespace;
880+
tablename | indexname | tablespace
881+
-----------+-------------+------------------
882+
t | t_pkey | regress_tblspace
883+
tp_0_2 | tp_0_2_pkey | regress_tblspace
884+
(2 rows)
885+
886+
DROP TABLE t;
864887
-- Check the new partition inherits parent's table access method
865888
SET search_path = partitions_merge_schema, public;
866889
CREATE ACCESS METHOD partitions_merge_heap TYPE TABLE HANDLER heap_tableam_handler;

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

Copy file name to clipboardExpand all lines: src/test/regress/expected/partition_split.out
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,32 @@ SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid)
14931493
tp_1_2 | FOR VALUES FROM (1) TO (2) | t
14941494
(2 rows)
14951495

1496+
DROP TABLE t;
1497+
-- Check the new partitions inherit parent's tablespace
1498+
CREATE TABLE t (i int PRIMARY KEY USING INDEX TABLESPACE regress_tblspace)
1499+
PARTITION BY RANGE (i) TABLESPACE regress_tblspace;
1500+
CREATE TABLE tp_0_2 PARTITION OF t FOR VALUES FROM (0) TO (2);
1501+
ALTER TABLE t SPLIT PARTITION tp_0_2 INTO
1502+
(PARTITION tp_0_1 FOR VALUES FROM (0) TO (1),
1503+
PARTITION tp_1_2 FOR VALUES FROM (1) TO (2));
1504+
SELECT tablename, tablespace FROM pg_tables
1505+
WHERE tablename IN ('t', 'tp_0_1', 'tp_1_2') ORDER BY tablename, tablespace;
1506+
tablename | tablespace
1507+
-----------+------------------
1508+
t | regress_tblspace
1509+
tp_0_1 | regress_tblspace
1510+
tp_1_2 | regress_tblspace
1511+
(3 rows)
1512+
1513+
SELECT tablename, indexname, tablespace FROM pg_indexes
1514+
WHERE tablename IN ('t', 'tp_0_1', 'tp_1_2') ORDER BY tablename, indexname, tablespace;
1515+
tablename | indexname | tablespace
1516+
-----------+-------------+------------------
1517+
t | t_pkey | regress_tblspace
1518+
tp_0_1 | tp_0_1_pkey | regress_tblspace
1519+
tp_1_2 | tp_1_2_pkey | regress_tblspace
1520+
(3 rows)
1521+
14961522
DROP TABLE t;
14971523
-- Check new partitions inherits parent's table access method
14981524
CREATE ACCESS METHOD partition_split_heap TYPE TABLE HANDLER heap_tableam_handler;

‎src/test/regress/sql/partition_merge.sql

Copy file name to clipboardExpand all lines: src/test/regress/sql/partition_merge.sql
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,18 @@ SET search_path = partitions_merge_schema, pg_temp, public;
536536
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
537537
ROLLBACK;
538538

539+
-- Check the new partition inherits parent's tablespace
540+
CREATE TABLE t (i int PRIMARY KEY USING INDEX TABLESPACE regress_tblspace)
541+
PARTITION BY RANGE (i) TABLESPACE regress_tblspace;
542+
CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
543+
CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
544+
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
545+
SELECT tablename, tablespace FROM pg_tables
546+
WHERE tablename IN ('t', 'tp_0_2') ORDER BY tablename, tablespace;
547+
SELECT tablename, indexname, tablespace FROM pg_indexes
548+
WHERE tablename IN ('t', 'tp_0_2') ORDER BY tablename, indexname, tablespace;
549+
DROP TABLE t;
550+
539551
-- Check the new partition inherits parent's table access method
540552
SET search_path = partitions_merge_schema, public;
541553
CREATE ACCESS METHOD partitions_merge_heap TYPE TABLE HANDLER heap_tableam_handler;

‎src/test/regress/sql/partition_split.sql

Copy file name to clipboardExpand all lines: src/test/regress/sql/partition_split.sql
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,19 @@ SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid)
880880

881881
DROP TABLE t;
882882

883+
-- Check the new partitions inherit parent's tablespace
884+
CREATE TABLE t (i int PRIMARY KEY USING INDEX TABLESPACE regress_tblspace)
885+
PARTITION BY RANGE (i) TABLESPACE regress_tblspace;
886+
CREATE TABLE tp_0_2 PARTITION OF t FOR VALUES FROM (0) TO (2);
887+
ALTER TABLE t SPLIT PARTITION tp_0_2 INTO
888+
(PARTITION tp_0_1 FOR VALUES FROM (0) TO (1),
889+
PARTITION tp_1_2 FOR VALUES FROM (1) TO (2));
890+
SELECT tablename, tablespace FROM pg_tables
891+
WHERE tablename IN ('t', 'tp_0_1', 'tp_1_2') ORDER BY tablename, tablespace;
892+
SELECT tablename, indexname, tablespace FROM pg_indexes
893+
WHERE tablename IN ('t', 'tp_0_1', 'tp_1_2') ORDER BY tablename, indexname, tablespace;
894+
DROP TABLE t;
895+
883896
-- Check new partitions inherits parent's table access method
884897
CREATE ACCESS METHOD partition_split_heap TYPE TABLE HANDLER heap_tableam_handler;
885898
CREATE TABLE t (i int) PARTITION BY RANGE (i) USING partition_split_heap;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.