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 259c96f

Browse filesBrowse files
committed
Inherit parent's AM for partition MERGE/SPLIT operations
This commit makes new partitions created by ALTER TABLE ... SPLIT PARTITION and ALTER TABLE ... MERGE PARTITIONS commands inherit the paret table access method. Reported-by: Alexander Lakhin Discussion: https://postgr.es/m/84ada05b-be5c-473e-6d1c-ebe5dd21b190%40gmail.com Reviewed-by: Pavel Borisov
1 parent 60ae37a commit 259c96f
Copy full SHA for 259c96f

File tree

6 files changed

+74
-2
lines changed
Filter options

6 files changed

+74
-2
lines changed

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

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/alter_table.sgml
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
11581158
SQL command <literal>CREATE TABLE <replaceable class="parameter">partition_nameN</replaceable> (LIKE <replaceable class="parameter">name</replaceable> INCLUDING ALL EXCLUDING INDEXES EXCLUDING IDENTITY)</literal>.
11591159
The indexes and identity are created later, after moving the data
11601160
into the new partitions.
1161+
New partitions will have the same table access method as the parent.
11611162
If the parent table is persistent then new partitions are created
11621163
persistent. If the parent table is temporary then new partitions
11631164
are also created temporary.
@@ -1227,6 +1228,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
12271228
SQL command <literal>CREATE TABLE <replaceable class="parameter">partition_name</replaceable> (LIKE <replaceable class="parameter">name</replaceable> INCLUDING ALL EXCLUDING INDEXES EXCLUDING IDENTITY)</literal>.
12281229
The indexes and identity are created later, after moving the data
12291230
into the new partition.
1231+
The new partition will have the same table access method as the parent.
12301232
If the parent table is persistent then the new partition is created
12311233
persistent. If the parent table is temporary then the new partition
12321234
is also created temporary.

‎src/backend/commands/tablecmds.c

Copy file name to clipboardExpand all lines: src/backend/commands/tablecmds.c
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21213,6 +21213,11 @@ moveSplitTableRows(Relation rel, Relation splitRel, List *partlist, List *newPar
2121321213
*
2121421214
* Emulates command: CREATE [TEMP] TABLE <newPartName> (LIKE <modelRel's name>
2121521215
* INCLUDING ALL EXCLUDING INDEXES EXCLUDING IDENTITY)
21216+
*
21217+
* Also, this function sets the new partition access method same as parent
21218+
* table access methods (similarly to CREATE TABLE ... PARTITION OF). It
21219+
* checks that parent and child tables have compatible persistence.
21220+
*
2121621221
* Function returns the created relation (locked in AccessExclusiveLock mode).
2121721222
*/
2121821223
static Relation
@@ -21243,6 +21248,7 @@ createPartitionTable(RangeVar *newPartName, Relation modelRel,
2124321248
createStmt->oncommit = ONCOMMIT_NOOP;
2124421249
createStmt->tablespacename = NULL;
2124521250
createStmt->if_not_exists = false;
21251+
createStmt->accessMethod = get_am_name(modelRel->rd_rel->relam);
2124621252

2124721253
tlc = makeNode(TableLikeClause);
2124821254
tlc->relation = makeRangeVar(get_namespace_name(RelationGetNamespace(modelRel)),

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

Copy file name to clipboardExpand all lines: src/test/regress/expected/partition_merge.out
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,24 @@ SET search_path = partitions_merge_schema, pg_temp, public;
862862
-- Can't merge temporary partitions into a persistent partition
863863
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
864864
ROLLBACK;
865+
-- Check the new partition inherits parent's table access method
866+
SET search_path = partitions_merge_schema, public;
867+
CREATE ACCESS METHOD partitions_merge_heap TYPE TABLE HANDLER heap_tableam_handler;
868+
CREATE TABLE t (i int) PARTITION BY RANGE (i) USING partitions_merge_heap;
869+
CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
870+
CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
871+
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
872+
SELECT c.relname, a.amname
873+
FROM pg_class c JOIN pg_am a ON c.relam = a.oid
874+
WHERE c.oid IN ('t'::regclass, 'tp_0_2'::regclass);
875+
relname | amname
876+
---------+-----------------------
877+
tp_0_2 | partitions_merge_heap
878+
t | partitions_merge_heap
879+
(2 rows)
880+
881+
DROP TABLE t;
882+
DROP ACCESS METHOD partitions_merge_heap;
865883
RESET search_path;
866884
--
867885
DROP SCHEMA partitions_merge_schema;

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

Copy file name to clipboardExpand all lines: src/test/regress/expected/partition_split.out
+21-2Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ ALTER TABLE sales_range SPLIT PARTITION sales_feb_mar_apr2022 INTO
8585
PARTITION sales_mar2022 FOR VALUES FROM ('2022-03-01') TO ('2022-04-01'),
8686
PARTITION sales_apr2022 FOR VALUES FROM ('2022-04-01') TO ('2022-06-01'));
8787
ERROR: upper bound of partition "sales_apr2022" is greater than upper bound of split partition
88-
LINE 4: PARTITION sales_apr2022 FOR VALUES FROM ('2022-04-01') TO...
89-
^
88+
LINE 4: ... sales_apr2022 FOR VALUES FROM ('2022-04-01') TO ('2022-06-0...
89+
^
9090
-- ERROR: lower bound of partition "sales_mar2022" conflicts with upper bound of previous partition "sales_feb2022"
9191
ALTER TABLE sales_range SPLIT PARTITION sales_feb_mar_apr2022 INTO
9292
(PARTITION sales_feb2022 FOR VALUES FROM ('2022-02-01') TO ('2022-03-01'),
@@ -1494,6 +1494,25 @@ SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid)
14941494
(2 rows)
14951495

14961496
DROP TABLE t;
1497+
-- Check new partitions inherits parent's table access method
1498+
CREATE ACCESS METHOD partition_split_heap TYPE TABLE HANDLER heap_tableam_handler;
1499+
CREATE TABLE t (i int) PARTITION BY RANGE (i) USING partition_split_heap;
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 c.relname, a.amname
1505+
FROM pg_class c JOIN pg_am a ON c.relam = a.oid
1506+
WHERE c.oid IN ('t'::regclass, 'tp_0_1'::regclass, 'tp_1_2'::regclass);
1507+
relname | amname
1508+
---------+----------------------
1509+
t | partition_split_heap
1510+
tp_0_1 | partition_split_heap
1511+
tp_1_2 | partition_split_heap
1512+
(3 rows)
1513+
1514+
DROP TABLE t;
1515+
DROP ACCESS METHOD partition_split_heap;
14971516
--
14981517
DROP SCHEMA partition_split_schema;
14991518
DROP SCHEMA partition_split_schema2;

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

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

538+
-- Check the new partition inherits parent's table access method
539+
SET search_path = partitions_merge_schema, public;
540+
CREATE ACCESS METHOD partitions_merge_heap TYPE TABLE HANDLER heap_tableam_handler;
541+
CREATE TABLE t (i int) PARTITION BY RANGE (i) USING partitions_merge_heap;
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 c.relname, a.amname
546+
FROM pg_class c JOIN pg_am a ON c.relam = a.oid
547+
WHERE c.oid IN ('t'::regclass, 'tp_0_2'::regclass);
548+
DROP TABLE t;
549+
DROP ACCESS METHOD partitions_merge_heap;
550+
538551
RESET search_path;
539552

540553
--

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

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

881881
DROP TABLE t;
882882

883+
-- Check new partitions inherits parent's table access method
884+
CREATE ACCESS METHOD partition_split_heap TYPE TABLE HANDLER heap_tableam_handler;
885+
CREATE TABLE t (i int) PARTITION BY RANGE (i) USING partition_split_heap;
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 c.relname, a.amname
891+
FROM pg_class c JOIN pg_am a ON c.relam = a.oid
892+
WHERE c.oid IN ('t'::regclass, 'tp_0_1'::regclass, 'tp_1_2'::regclass);
893+
DROP TABLE t;
894+
DROP ACCESS METHOD partition_split_heap;
895+
896+
883897
--
884898
DROP SCHEMA partition_split_schema;
885899
DROP SCHEMA partition_split_schema2;

0 commit comments

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