@@ -37,6 +37,13 @@ ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
37
37
ATTACH PARTITION <replaceable class="parameter">partition_name</replaceable> { FOR VALUES <replaceable class="parameter">partition_bound_spec</replaceable> | DEFAULT }
38
38
ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
39
39
DETACH PARTITION <replaceable class="parameter">partition_name</replaceable> [ CONCURRENTLY | FINALIZE ]
40
+ ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
41
+ SPLIT PARTITION <replaceable class="parameter">partition_name</replaceable> INTO
42
+ (PARTITION <replaceable class="parameter">partition_name1</replaceable> { FOR VALUES <replaceable class="parameter">partition_bound_spec</replaceable> | DEFAULT },
43
+ PARTITION <replaceable class="parameter">partition_name2</replaceable> { FOR VALUES <replaceable class="parameter">partition_bound_spec</replaceable> | DEFAULT } [, ...])
44
+ ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
45
+ MERGE PARTITIONS (<replaceable class="parameter">partition_name1</replaceable>, <replaceable class="parameter">partition_name2</replaceable> [, ...])
46
+ INTO <replaceable class="parameter">partition_name</replaceable>
40
47
41
48
<phrase>where <replaceable class="parameter">action</replaceable> is one of:</phrase>
42
49
@@ -1147,14 +1154,142 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
1147
1154
</listitem>
1148
1155
</varlistentry>
1149
1156
1157
+ <varlistentry id="sql-altertable-split-partition">
1158
+ <term><literal>SPLIT PARTITION <replaceable class="parameter">partition_name</replaceable> INTO (PARTITION <replaceable class="parameter">partition_name1</replaceable> { FOR VALUES <replaceable class="parameter">partition_bound_spec</replaceable> | DEFAULT }, PARTITION <replaceable class="parameter">partition_name2</replaceable> { FOR VALUES <replaceable class="parameter">partition_bound_spec</replaceable> | DEFAULT } [, ...])</literal></term>
1159
+
1160
+ <listitem>
1161
+ <para>
1162
+ This form splits a single partition of the target table. Hash-partitioning
1163
+ is not supported. Bounds of new partitions should not overlap with new and
1164
+ existing partitions (except <replaceable class="parameter">partition_name</replaceable>).
1165
+ If the split partition is a DEFAULT partition, one of the new partitions must be DEFAULT.
1166
+ In case one of the new partitions or one of existing partitions is DEFAULT,
1167
+ new partitions <replaceable class="parameter">partition_name1</replaceable>,
1168
+ <replaceable class="parameter">partition_name2</replaceable>, ... can have spaces
1169
+ between partitions bounds. If the partitioned table does not have a DEFAULT
1170
+ partition, the DEFAULT partition can be defined as one of the new partitions.
1171
+ </para>
1172
+ <para>
1173
+ In case new partitions do not contain a DEFAULT partition and the partitioned table
1174
+ does not have a DEFAULT partition, the following must be true: sum bounds of
1175
+ new partitions <replaceable class="parameter">partition_name1</replaceable>,
1176
+ <replaceable class="parameter">partition_name2</replaceable>, ... should be
1177
+ equal to bound of split partition <replaceable class="parameter">partition_name</replaceable>.
1178
+ One of the new partitions <replaceable class="parameter">partition_name1</replaceable>,
1179
+ <replaceable class="parameter">partition_name2</replaceable>, ... can have
1180
+ the same name as split partition <replaceable class="parameter">partition_name</replaceable>
1181
+ (this is suitable in case of splitting a DEFAULT partition: we split it, but after
1182
+ splitting we have a partition with the same name).
1183
+ Only simple, non-partitioned partition can be split.
1184
+ </para>
1185
+ <para>
1186
+ New partitions will have the same owner as the parent.
1187
+ </para>
1188
+ <para>
1189
+ The indexes and identity are created later, after moving the data
1190
+ into the new partitions.
1191
+ Extended statistics aren't copied from the parent table, for consistency with
1192
+ <command>CREATE TABLE PARTITION OF</command>.
1193
+
1194
+ New partitions will have the same table access method as the parent.
1195
+ If the parent table is persistent then new partitions are created
1196
+ persistent. If the parent table is temporary then new partitions
1197
+ are also created temporary. New partitions will also be created in
1198
+ the same tablespace as the parent.
1199
+ </para>
1200
+ <note>
1201
+ <para>
1202
+ This command acquires an <literal>ACCESS EXCLUSIVE</literal> lock.
1203
+ This is a significant limitation, which limits the usage of this
1204
+ command with large partitioned tables under a high load.
1205
+ </para>
1206
+ </note>
1207
+ </listitem>
1208
+ </varlistentry>
1209
+
1210
+ <varlistentry id="sql-altertable-merge-partitions">
1211
+ <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>
1212
+
1213
+ <listitem>
1214
+ <para>
1215
+ This form merges several partitions into the one partition of the target table.
1216
+ Hash-partitioning is not supported. If DEFAULT partition is not in the
1217
+ list of partitions <replaceable class="parameter">partition_name1</replaceable>,
1218
+ <replaceable class="parameter">partition_name2</replaceable> [, ...]:
1219
+ <itemizedlist>
1220
+ <listitem>
1221
+ <para>
1222
+ For range-partitioned tables it is necessary that the ranges
1223
+ of the partitions <replaceable class="parameter">partition_name1</replaceable>,
1224
+ <replaceable class="parameter">partition_name2</replaceable> [, ...] can
1225
+ be merged into one range without spaces and overlaps (otherwise an error
1226
+ will be generated). The combined range will be the range for the partition
1227
+ <replaceable class="parameter">partition_name</replaceable>.
1228
+ </para>
1229
+ </listitem>
1230
+ <listitem>
1231
+ <para>
1232
+ For list-partitioned tables the value lists of all partitions
1233
+ <replaceable class="parameter">partition_name1</replaceable>,
1234
+ <replaceable class="parameter">partition_name2</replaceable> [, ...] are
1235
+ combined and form the list of values of partition
1236
+ <replaceable class="parameter">partition_name</replaceable>.
1237
+ </para>
1238
+ </listitem>
1239
+ </itemizedlist>
1240
+ If DEFAULT partition is in the list of partitions <replaceable class="parameter">partition_name1</replaceable>,
1241
+ <replaceable class="parameter">partition_name2</replaceable> [, ...]:
1242
+ <itemizedlist>
1243
+ <listitem>
1244
+ <para>
1245
+ The partition <replaceable class="parameter">partition_name</replaceable>
1246
+ will be the DEFAULT partition.
1247
+ </para>
1248
+ </listitem>
1249
+ <listitem>
1250
+ <para>
1251
+ For range- and list-partitioned tables the ranges and lists of values
1252
+ of the merged partitions can be any.
1253
+ </para>
1254
+ </listitem>
1255
+ </itemizedlist>
1256
+ The new partition <replaceable class="parameter">partition_name</replaceable>
1257
+ can have the same name as one of the merged partitions. Only simple,
1258
+ non-partitioned partitions can be merged.
1259
+ </para>
1260
+ <para>
1261
+ If merged partitions have different owners, an error will be generated.
1262
+ </para>
1263
+ <para>
1264
+ The indexes and identity are created later, after moving the data
1265
+ into the new partition.
1266
+ Extended statistics aren't copied from the parent table, for consistency with
1267
+ <command>CREATE TABLE PARTITION OF</command>.
1268
+ The new partition will have the same table access method as the parent.
1269
+ If the parent table is persistent then the new partition is created
1270
+ persistent. If the parent table is temporary then the new partition
1271
+ is also created temporary. The new partition will also be created in
1272
+ the same tablespace as the parent.
1273
+ </para>
1274
+ <note>
1275
+ <para>
1276
+ This command acquires an <literal>ACCESS EXCLUSIVE</literal> lock.
1277
+ This is a significant limitation, which limits the usage of this
1278
+ command with large partitioned tables under a high load.
1279
+ </para>
1280
+ </note>
1281
+ </listitem>
1282
+ </varlistentry>
1283
+
1150
1284
</variablelist>
1151
1285
</para>
1152
1286
1153
1287
<para>
1154
1288
All the forms of <command>ALTER TABLE</command> that act on a single table,
1155
1289
except <literal>RENAME</literal>, <literal>SET SCHEMA</literal>,
1156
- <literal>ATTACH PARTITION</literal>, and
1157
- <literal>DETACH PARTITION</literal> can be combined into
1290
+ <literal>ATTACH PARTITION</literal>, <literal>DETACH PARTITION</literal>,
1291
+ <literal>SPLIT PARTITION</literal>, and <literal>MERGE PARTITIONS</literal>
1292
+ can be combined into
1158
1293
a list of multiple alterations to be applied together. For example, it
1159
1294
is possible to add several columns and/or alter the type of several
1160
1295
columns in a single command. This is particularly useful with large
@@ -1397,7 +1532,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
1397
1532
<term><replaceable class="parameter">partition_name</replaceable></term>
1398
1533
<listitem>
1399
1534
<para>
1400
- The name of the table to attach as a new partition or to detach from this table.
1535
+ The name of the table to attach as a new partition or to detach from this table,
1536
+ or the name of split partition, or the name of the new merged partition.
1401
1537
</para>
1402
1538
</listitem>
1403
1539
</varlistentry>
@@ -1830,6 +1966,31 @@ ALTER TABLE measurement
1830
1966
DETACH PARTITION measurement_y2015m12;
1831
1967
</programlisting></para>
1832
1968
1969
+ <para>
1970
+ To split a single partition of the range-partitioned table:
1971
+ <programlisting>
1972
+ ALTER TABLE sales_range SPLIT PARTITION sales_feb_mar_apr2023 INTO
1973
+ (PARTITION sales_feb2023 FOR VALUES FROM ('2023-02-01') TO ('2023-03-01'),
1974
+ PARTITION sales_mar2023 FOR VALUES FROM ('2023-03-01') TO ('2023-04-01'),
1975
+ PARTITION sales_apr2023 FOR VALUES FROM ('2023-04-01') TO ('2023-05-01'));
1976
+ </programlisting></para>
1977
+
1978
+ <para>
1979
+ To split a single partition of the list-partitioned table:
1980
+ <programlisting>
1981
+ ALTER TABLE sales_list SPLIT PARTITION sales_all INTO
1982
+ (PARTITION sales_west FOR VALUES IN ('Lisbon', 'New York', 'Madrid'),
1983
+ PARTITION sales_east FOR VALUES IN ('Bejing', 'Delhi', 'Vladivostok'),
1984
+ PARTITION sales_central FOR VALUES IN ('Warsaw', 'Berlin', 'Kyiv'));
1985
+ </programlisting></para>
1986
+
1987
+ <para>
1988
+ To merge several partitions into one partition of the target table:
1989
+ <programlisting>
1990
+ ALTER TABLE sales_list MERGE PARTITIONS (sales_west, sales_east, sales_central)
1991
+ INTO sales_all;
1992
+ </programlisting></para>
1993
+
1833
1994
</refsect1>
1834
1995
1835
1996
<refsect1>
0 commit comments