@@ -37,6 +37,12 @@ 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
+ MERGE PARTITIONS (<replaceable class="parameter">partition_name1</replaceable>, <replaceable class="parameter">partition_name2</replaceable> [, ...]) INTO <replaceable class="parameter">partition_name</replaceable>
42
+ ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
43
+ SPLIT PARTITION <replaceable class="parameter">partition_name</replaceable> INTO
44
+ (PARTITION <replaceable class="parameter">partition_name1</replaceable> { FOR VALUES <replaceable class="parameter">partition_bound_spec</replaceable> | DEFAULT },
45
+ PARTITION <replaceable class="parameter">partition_name2</replaceable> { FOR VALUES <replaceable class="parameter">partition_bound_spec</replaceable> | DEFAULT } [, ...])
40
46
41
47
<phrase>where <replaceable class="parameter">action</replaceable> is one of:</phrase>
42
48
@@ -1147,14 +1153,167 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
1147
1153
</listitem>
1148
1154
</varlistentry>
1149
1155
1156
+ <varlistentry id="sql-altertable-split-partition">
1157
+ <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>
1158
+
1159
+ <listitem>
1160
+ <para>
1161
+ This form splits a single partition of the target table into a new
1162
+ partitions. Hash-partitioned target table is not supported. Bounds of new
1163
+ partitions should not overlap with new and existing partitions
1164
+ (except <replaceable class="parameter">partition_name</replaceable>).
1165
+ If the split partition is a <literal>DEFAULT</literal> partition, one of
1166
+ the new partitions must be <literal>DEFAULT</literal>.
1167
+ In case one of the new partitions or one of existing partitions is
1168
+ <literal>DEFAULT</literal>, new partitions <replaceable class="parameter">partition_name1</replaceable>,
1169
+ <replaceable class="parameter">partition_name2</replaceable>, ... can
1170
+ have spaces between partitions bounds. If the partitioned table does not
1171
+ have a <literal>DEFAULT</literal> partition, the <literal>DEFAULT</literal>
1172
+ partition can be defined as one of the new partitions.
1173
+ </para>
1174
+ <para>
1175
+ In case new partitions do not contain a <literal>DEFAULT</literal>
1176
+ partition and the partitioned table does not have a <literal>DEFAULT</literal>
1177
+ partition, the following must be true: sum bounds of new partitions
1178
+ <replaceable class="parameter">partition_name1</replaceable>,
1179
+ <replaceable class="parameter">partition_name2</replaceable>, ... should
1180
+ be equal to bound of split partition <replaceable class="parameter">partition_name</replaceable>.
1181
+ One of the new partitions <replaceable class="parameter">partition_name1</replaceable>,
1182
+ <replaceable class="parameter">partition_name2</replaceable>, ... can have
1183
+ the same name as split partition <replaceable class="parameter">partition_name</replaceable>
1184
+ (this is suitable in case of splitting a <literal>DEFAULT</literal>
1185
+ partition: we split it, but after splitting we have a partition with the
1186
+ same name). Only simple, non-partitioned partition can be split.
1187
+ </para>
1188
+ <para>
1189
+ New partitions will have the same owner as the parent partition.
1190
+ It is the user's responsibility to setup <acronym>ACL</acronym> on new
1191
+ partitions.
1192
+ </para>
1193
+ <para>
1194
+ The indexes and identity are created later, after moving the data
1195
+ into the new partitions.
1196
+ Extended statistics aren't copied from the parent table, for consistency with
1197
+ <command>CREATE TABLE PARTITION OF</command>.
1198
+ New partitions will inherit the same table access method, persistence
1199
+ type, and tablespace as the parent table.
1200
+ </para>
1201
+ <para>
1202
+ When partition is split, any individual objects belonging to this
1203
+ partition, such as constraints or statistics will be dropped. This ccurs
1204
+ because <command>ALTER TABLE SPLIT PARTITION</command> uses the partitioned table itself
1205
+ as the template to define these objects.
1206
+ </para>
1207
+ <para>
1208
+ If split partition has some objects dependent on it, the command can
1209
+ not be done (<literal>CASCADE</literal> is not used, an error will be returned).
1210
+ </para>
1211
+ <note>
1212
+ <para>
1213
+ Split partition acquires a <literal>ACCESS EXCLUSIVE</literal> lock on
1214
+ the parent table, in addition to the <literal>ACCESS EXCLUSIVE</literal>
1215
+ lock on the table being split.
1216
+ </para>
1217
+ </note>
1218
+ </listitem>
1219
+ </varlistentry>
1220
+
1221
+ <varlistentry id="sql-altertable-merge-partitions">
1222
+ <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>
1223
+
1224
+ <listitem>
1225
+ <para>
1226
+ This form merges several partitions of the target table into a new partition.
1227
+ Hash-partitioned target table is not supported.
1228
+ If <literal>DEFAULT</literal> partition is not in the
1229
+ list of partitions <replaceable class="parameter">partition_name1</replaceable>,
1230
+ <replaceable class="parameter">partition_name2</replaceable> [, ...]:
1231
+ <itemizedlist>
1232
+ <listitem>
1233
+ <para>
1234
+ For range-partitioned tables, the ranges of the partitions
1235
+ <replaceable class="parameter">partition_name1</replaceable>,
1236
+ <replaceable class="parameter">partition_name2</replaceable>, [...]
1237
+ must be adjacent in order to be merged. Otherwise, an error will be
1238
+ raised. The resulting combined range will be the new partition bound
1239
+ for the partition <replaceable class="parameter">partition_name</replaceable>.
1240
+ </para>
1241
+ </listitem>
1242
+ <listitem>
1243
+ <para>
1244
+ For list-partitioned tables, the partition bounds of
1245
+ <replaceable class="parameter">partition_name1</replaceable>,
1246
+ <replaceable class="parameter">partition_name2</replaceable>, [...]
1247
+ are combined to form the new partition bound for
1248
+ <replaceable class="parameter">partition_name</replaceable>.
1249
+ </para>
1250
+ </listitem>
1251
+ </itemizedlist>
1252
+ If <literal>DEFAULT</literal> partition is in the list of partitions <replaceable class="parameter">partition_name1</replaceable>,
1253
+ <replaceable class="parameter">partition_name2</replaceable> [, ...]:
1254
+ <itemizedlist>
1255
+ <listitem>
1256
+ <para>
1257
+ The partition <replaceable class="parameter">partition_name</replaceable>
1258
+ will be the new <literal>DEFAULT</literal> partition of the target table.
1259
+ </para>
1260
+ </listitem>
1261
+ <listitem>
1262
+ <para>
1263
+ The partition bound specifications for all partitions-
1264
+ <replaceable class="parameter">partition_name1</replaceable>,
1265
+ <replaceable class="parameter">partition_name2</replaceable>, [...]
1266
+ can be arbitrary.
1267
+ </para>
1268
+ </listitem>
1269
+ </itemizedlist>
1270
+ The new partition <replaceable class="parameter">partition_name</replaceable>
1271
+ can have the same name as one of the merged partitions. Only simple,
1272
+ non-partitioned partitions can be merged.
1273
+ </para>
1274
+ <para>
1275
+ If merged partitions have different owners, an error will be generated.
1276
+ The owner of the merged partitions will be the owner of the new partition.
1277
+ It is the user's responsibility to setup <acronym>ACL</acronym> on the
1278
+ new partition.
1279
+ </para>
1280
+ <para>
1281
+ The indexes and identity are created later, after moving the data
1282
+ into the new partition.
1283
+ Extended statistics aren't copied from the parent table, for consistency with
1284
+ <command>CREATE TABLE PARTITION OF</command>.
1285
+ The new partition will inherit the same table access method, persistence
1286
+ type, and tablespace as the parent table.
1287
+ </para>
1288
+ <para>
1289
+ When partitions are merged, any individual objects belonging to those
1290
+ partitions, such as constraints or statistics will be dropped. This occurs
1291
+ because <command>ALTER TABLE MERGE PARTITIONS</command> uses the partitioned table itself as the
1292
+ template to define these objects.
1293
+ </para>
1294
+ <para>
1295
+ If merged partitions have some objects dependent on them, the command can
1296
+ not be done (<literal>CASCADE</literal> is not used, an error will be returned).
1297
+ </para>
1298
+ <note>
1299
+ <para>
1300
+ Merging partitions acquires a <literal>ACCESS EXCLUSIVE</literal> lock on
1301
+ the parent table, in addition to the <literal>ACCESS EXCLUSIVE</literal>
1302
+ locks on the tables being merged and on the default partition (if any).
1303
+ </para>
1304
+ </note>
1305
+ </listitem>
1306
+ </varlistentry>
1307
+
1150
1308
</variablelist>
1151
1309
</para>
1152
1310
1153
1311
<para>
1154
1312
All the forms of <command>ALTER TABLE</command> that act on a single table,
1155
1313
except <literal>RENAME</literal>, <literal>SET SCHEMA</literal>,
1156
- <literal>ATTACH PARTITION</literal>, and
1157
- <literal>DETACH PARTITION</literal> can be combined into
1314
+ <literal>ATTACH PARTITION</literal>, <literal>DETACH PARTITION</literal>,
1315
+ <literal>SPLIT PARTITION</literal>, and <literal>MERGE PARTITIONS</literal>
1316
+ can be combined into
1158
1317
a list of multiple alterations to be applied together. For example, it
1159
1318
is possible to add several columns and/or alter the type of several
1160
1319
columns in a single command. This is particularly useful with large
@@ -1397,7 +1556,19 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
1397
1556
<term><replaceable class="parameter">partition_name</replaceable></term>
1398
1557
<listitem>
1399
1558
<para>
1400
- The name of the table to attach as a new partition or to detach from this table.
1559
+ The name of the table to attach as a new partition or to detach from this table,
1560
+ or the name of split partition, or the name of the new merged partition.
1561
+ </para>
1562
+ </listitem>
1563
+ </varlistentry>
1564
+
1565
+ <varlistentry id="sql-altertable-parms-partition-name1">
1566
+ <term><replaceable class="parameter">partition_name1</replaceable></term>
1567
+ <term><replaceable class="parameter">partition_name2</replaceable></term>
1568
+ <listitem>
1569
+ <para>
1570
+ The names of the tables being merged into the new partition or split into
1571
+ new partitions.
1401
1572
</para>
1402
1573
</listitem>
1403
1574
</varlistentry>
@@ -1830,6 +2001,31 @@ ALTER TABLE measurement
1830
2001
DETACH PARTITION measurement_y2015m12;
1831
2002
</programlisting></para>
1832
2003
2004
+ <para>
2005
+ To split a single partition of the range-partitioned table:
2006
+ <programlisting>
2007
+ ALTER TABLE sales_range SPLIT PARTITION sales_feb_mar_apr2023 INTO
2008
+ (PARTITION sales_feb2023 FOR VALUES FROM ('2023-02-01') TO ('2023-03-01'),
2009
+ PARTITION sales_mar2023 FOR VALUES FROM ('2023-03-01') TO ('2023-04-01'),
2010
+ PARTITION sales_apr2023 FOR VALUES FROM ('2023-04-01') TO ('2023-05-01'));
2011
+ </programlisting></para>
2012
+
2013
+ <para>
2014
+ To split a single partition of the list-partitioned table:
2015
+ <programlisting>
2016
+ ALTER TABLE sales_list SPLIT PARTITION sales_all INTO
2017
+ (PARTITION sales_west FOR VALUES IN ('Lisbon', 'New York', 'Madrid'),
2018
+ PARTITION sales_east FOR VALUES IN ('Bejing', 'Delhi', 'Vladivostok'),
2019
+ PARTITION sales_central FOR VALUES IN ('Warsaw', 'Berlin', 'Kyiv'));
2020
+ </programlisting></para>
2021
+
2022
+ <para>
2023
+ To merge several partitions into one partition of the target table:
2024
+ <programlisting>
2025
+ ALTER TABLE sales_list MERGE PARTITIONS (sales_west, sales_east, sales_central)
2026
+ INTO sales_all;
2027
+ </programlisting></para>
2028
+
1833
2029
</refsect1>
1834
2030
1835
2031
<refsect1>
0 commit comments