Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1adf16b

Browse files
committed
Implement ALTER TABLE ... MERGE PARTITIONS ... command
This new DDL command merges several partitions into the one partition of the target table. The target partition is created using new createPartitionTable() function with parent partition as the template. This commit comprises quite naive implementation which works in single process and holds the ACCESS EXCLUSIVE LOCK on the parent table during all the operations including the tuple routing. This is why this new DDL command can't be recommended for large partitioned tables under a high load. However, this implementation come in handy in certain cases even as is. Also, it could be used as a foundation for future implementations with lesser locking and possibly parallel. Discussion: https://postgr.es/m/c73a1746-0cd0-6bdd-6b23-3ae0b7c0c582%40postgrespro.ru Author: Dmitry Koval Reviewed-by: Matthias van de Meent, Laurenz Albe, Zhihong Yu, Justin Pryzby Reviewed-by: Alvaro Herrera, Robert Haas, Stephane Tachoires
1 parent fe1431e commit 1adf16b

File tree

17 files changed

+2189
-22
lines changed

17 files changed

+2189
-22
lines changed

doc/src/sgml/ddl.sgml

+19
Original file line numberDiff line numberDiff line change
@@ -4379,6 +4379,25 @@ ALTER TABLE measurement_y2006m02 ADD UNIQUE (city_id, logdate);
43794379
ALTER INDEX measurement_city_id_logdate_key
43804380
ATTACH PARTITION measurement_y2006m02_city_id_logdate_key;
43814381
...
4382+
</programlisting>
4383+
</para>
4384+
4385+
<para>
4386+
There is also an option for merging multiple table partitions into
4387+
a single partition using the
4388+
<link linkend="sql-altertable-merge-partitions"><command>ALTER TABLE ... MERGE PARTITIONS</command></link>.
4389+
This feature simplifies the management of partitioned tables by allowing
4390+
administrators to combine partitions that are no longer needed as
4391+
separate entities. It's important to note that this operation is not
4392+
supported for hash-partitioned tables and acquires an
4393+
<literal>ACCESS EXCLUSIVE</literal> lock, which could impact high-load
4394+
systems due to the lock's restrictive nature. For example, we can
4395+
merge three monthly partitions into one quarter partition:
4396+
<programlisting>
4397+
ALTER TABLE measurement
4398+
MERGE PARTITIONS (measurement_y2006m01,
4399+
measurement_y2006m02,
4400+
measurement_y2006m03) INTO measurement_y2006q1;
43824401
</programlisting>
43834402
</para>
43844403
</sect3>

doc/src/sgml/ref/alter_table.sgml

+74-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
3737
ATTACH PARTITION <replaceable class="parameter">partition_name</replaceable> { FOR VALUES <replaceable class="parameter">partition_bound_spec</replaceable> | DEFAULT }
3838
ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
3939
DETACH PARTITION <replaceable class="parameter">partition_name</replaceable> [ CONCURRENTLY | FINALIZE ]
40+
ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
41+
MERGE PARTITIONS (<replaceable class="parameter">partition_name1</replaceable>, <replaceable class="parameter">partition_name2</replaceable> [, ...])
42+
INTO <replaceable class="parameter">partition_name</replaceable>
4043

4144
<phrase>where <replaceable class="parameter">action</replaceable> is one of:</phrase>
4245

@@ -1118,14 +1121,74 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
11181121
</listitem>
11191122
</varlistentry>
11201123

1124+
<varlistentry id="sql-altertable-merge-partitions">
1125+
<term><literal>MERGE PARTITIONS (<replaceable class="parameter">partition_name1</replaceable>, <replaceable class="parameter">partition_name2</replaceable> [, ...]) INTO <replaceable class="parameter">partition_name</replaceable></literal></term>
1126+
1127+
<listitem>
1128+
<para>
1129+
This form merges several partitions into the one partition of the target table.
1130+
Hash-partitioning is not supported. If DEFAULT partition is not in the
1131+
list of partitions <replaceable class="parameter">partition_name1</replaceable>,
1132+
<replaceable class="parameter">partition_name2</replaceable> [, ...]:
1133+
<itemizedlist>
1134+
<listitem>
1135+
<para>
1136+
For range-partitioned tables is necessary that the ranges
1137+
of the partitions <replaceable class="parameter">partition_name1</replaceable>,
1138+
<replaceable class="parameter">partition_name2</replaceable> [, ...] can
1139+
be merged into one range without spaces and overlaps (otherwise an error
1140+
will be generated). The combined range will be the range for the partition
1141+
<replaceable class="parameter">partition_name</replaceable>.
1142+
</para>
1143+
</listitem>
1144+
<listitem>
1145+
<para>
1146+
For list-partitioned tables the values lists of all partitions
1147+
<replaceable class="parameter">partition_name1</replaceable>,
1148+
<replaceable class="parameter">partition_name2</replaceable> [, ...] are
1149+
combined and form a list of values of partition
1150+
<replaceable class="parameter">partition_name</replaceable>.
1151+
</para>
1152+
</listitem>
1153+
</itemizedlist>
1154+
If DEFAULT partition is in the list of partitions <replaceable class="parameter">partition_name1</replaceable>,
1155+
<replaceable class="parameter">partition_name2</replaceable> [, ...]:
1156+
<itemizedlist>
1157+
<listitem>
1158+
<para>
1159+
The partition <replaceable class="parameter">partition_name</replaceable>
1160+
will be the DEFAULT partition.
1161+
</para>
1162+
</listitem>
1163+
<listitem>
1164+
<para>
1165+
For range- and list-partitioned tables the ranges and lists of values
1166+
of the merged partitions can be any.
1167+
</para>
1168+
</listitem>
1169+
</itemizedlist>
1170+
The new partition <replaceable class="parameter">partition_name</replaceable>
1171+
can have the same name as one of the merged partitions. Only simple,
1172+
non-partitioned partitions can be merged.
1173+
</para>
1174+
<note>
1175+
<para>
1176+
This command acquires an <literal>ACCESS EXCLUSIVE</literal> lock.
1177+
This is a significant limitation, which limits the usage of this
1178+
command with large partitioned tables under a high load.
1179+
</para>
1180+
</note>
1181+
</listitem>
1182+
</varlistentry>
1183+
11211184
</variablelist>
11221185
</para>
11231186

11241187
<para>
11251188
All the forms of ALTER TABLE that act on a single table, except
11261189
<literal>RENAME</literal>, <literal>SET SCHEMA</literal>,
1127-
<literal>ATTACH PARTITION</literal>, and
1128-
<literal>DETACH PARTITION</literal> can be combined into
1190+
<literal>ATTACH PARTITION</literal>, <literal>DETACH PARTITION</literal>,
1191+
and <literal>MERGE PARTITIONS</literal> can be combined into
11291192
a list of multiple alterations to be applied together. For example, it
11301193
is possible to add several columns and/or alter the type of several
11311194
columns in a single command. This is particularly useful with large
@@ -1368,7 +1431,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
13681431
<term><replaceable class="parameter">partition_name</replaceable></term>
13691432
<listitem>
13701433
<para>
1371-
The name of the table to attach as a new partition or to detach from this table.
1434+
The name of the table to attach as a new partition or to detach from this table,
1435+
or the name of the new merged partition.
13721436
</para>
13731437
</listitem>
13741438
</varlistentry>
@@ -1784,6 +1848,13 @@ ALTER TABLE measurement
17841848
DETACH PARTITION measurement_y2015m12;
17851849
</programlisting></para>
17861850

1851+
<para>
1852+
To merge several partitions into one partition of the target table:
1853+
<programlisting>
1854+
ALTER TABLE sales_list MERGE PARTITIONS (sales_west, sales_east, sales_central)
1855+
INTO sales_all;
1856+
</programlisting></para>
1857+
17871858
</refsect1>
17881859

17891860
<refsect1>

0 commit comments

Comments
 (0)