Skip to content

Commit 056c565

Browse files
committed
doc: Improve "Partition Maintenance" section
This adds some reference links and clarifies the wording a bit. Author: Robert Treat <rob@xzilla.net> Reviewed-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> Discussion: https://postgr.es/m/CABV9wwNGn-pweak6_pvL5PJ1mivDNPKfg0Tck_1oTUETv5Y=dg@mail.gmail.com
1 parent 0294df2 commit 056c565

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4283,18 +4283,20 @@ CREATE TABLE measurement_y2008m02 PARTITION OF measurement
42834283
TABLESPACE fasttablespace;
42844284
</programlisting>
42854285

4286-
As an alternative, it is sometimes more convenient to create the
4287-
new table outside the partition structure, and attach it as a
4288-
partition later. This allows new data to be loaded, checked, and
4289-
transformed prior to it appearing in the partitioned table.
4286+
As an alternative to creating a new partition, it is sometimes more
4287+
convenient to create a new table separate from the partition structure
4288+
and attach it as a partition later. This allows new data to be loaded,
4289+
checked, and transformed prior to it appearing in the partitioned table.
42904290
Moreover, the <literal>ATTACH PARTITION</literal> operation requires
4291-
only <literal>SHARE UPDATE EXCLUSIVE</literal> lock on the
4292-
partitioned table, as opposed to the <literal>ACCESS
4293-
EXCLUSIVE</literal> lock that is required by <command>CREATE TABLE
4294-
... PARTITION OF</command>, so it is more friendly to concurrent
4295-
operations on the partitioned table.
4296-
The <literal>CREATE TABLE ... LIKE</literal> option is helpful
4297-
to avoid tediously repeating the parent table's definition:
4291+
only a <literal>SHARE UPDATE EXCLUSIVE</literal> lock on the
4292+
partitioned table rather than the <literal>ACCESS EXCLUSIVE</literal>
4293+
lock required by <command>CREATE TABLE ... PARTITION OF</command>,
4294+
so it is more friendly to concurrent operations on the partitioned table;
4295+
see <link linkend="sql-altertable-attach-partition"><literal>ALTER TABLE ... ATTACH PARTITION</literal></link>
4296+
for additional details. The
4297+
<link linkend="sql-createtable-parms-like"><literal>CREATE TABLE ... LIKE</literal></link>
4298+
option can be helpful to avoid tediously repeating the parent table's
4299+
definition; for example:
42984300

42994301
<programlisting>
43004302
CREATE TABLE measurement_y2008m02
@@ -4313,17 +4315,15 @@ ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
43134315
</para>
43144316

43154317
<para>
4316-
Before running the <command>ATTACH PARTITION</command> command, it is
4317-
recommended to create a <literal>CHECK</literal> constraint on the table to
4318-
be attached that matches the expected partition constraint, as
4319-
illustrated above. That way, the system will be able to skip the scan
4320-
which is otherwise needed to validate the implicit
4321-
partition constraint. Without the <literal>CHECK</literal> constraint,
4318+
Note that when running the <command>ATTACH PARTITION</command> command,
43224319
the table will be scanned to validate the partition constraint while
43234320
holding an <literal>ACCESS EXCLUSIVE</literal> lock on that partition.
4324-
It is recommended to drop the now-redundant <literal>CHECK</literal>
4325-
constraint after the <command>ATTACH PARTITION</command> is complete. If
4326-
the table being attached is itself a partitioned table, then each of its
4321+
As shown above, it is recommended to avoid this scan by creating a
4322+
<literal>CHECK</literal> constraint matching the expected partition
4323+
constraint on the table prior to attaching it. Once the
4324+
<command>ATTACH PARTITION</command> is complete, it is recommended to drop
4325+
the now-redundant <literal>CHECK</literal> constraint.
4326+
If the table being attached is itself a partitioned table, then each of its
43274327
sub-partitions will be recursively locked and scanned until either a
43284328
suitable <literal>CHECK</literal> constraint is encountered or the leaf
43294329
partitions are reached.
@@ -4333,7 +4333,7 @@ ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
43334333
Similarly, if the partitioned table has a <literal>DEFAULT</literal>
43344334
partition, it is recommended to create a <literal>CHECK</literal>
43354335
constraint which excludes the to-be-attached partition's constraint. If
4336-
this is not done then the <literal>DEFAULT</literal> partition will be
4336+
this is not done, the <literal>DEFAULT</literal> partition will be
43374337
scanned to verify that it contains no records which should be located in
43384338
the partition being attached. This operation will be performed whilst
43394339
holding an <literal>ACCESS EXCLUSIVE</literal> lock on the <literal>
@@ -4344,21 +4344,21 @@ ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
43444344
</para>
43454345

43464346
<para>
4347-
As explained above, it is possible to create indexes on partitioned tables
4348-
so that they are applied automatically to the entire hierarchy.
4349-
This is very
4350-
convenient, as not only will the existing partitions become indexed, but
4351-
also any partitions that are created in the future will. One limitation is
4352-
that it's not possible to use the <literal>CONCURRENTLY</literal>
4353-
qualifier when creating such a partitioned index. To avoid long lock
4354-
times, it is possible to use <command>CREATE INDEX ON ONLY</command>
4355-
the partitioned table; such an index is marked invalid, and the partitions
4356-
do not get the index applied automatically. The indexes on partitions can
4357-
be created individually using <literal>CONCURRENTLY</literal>, and then
4358-
<firstterm>attached</firstterm> to the index on the parent using
4359-
<command>ALTER INDEX .. ATTACH PARTITION</command>. Once indexes for all
4360-
partitions are attached to the parent index, the parent index is marked
4361-
valid automatically. Example:
4347+
As mentioned earlier, it is possible to create indexes on partitioned
4348+
tables so that they are applied automatically to the entire hierarchy.
4349+
This can be very convenient as not only will all existing partitions be
4350+
indexed, but any future partitions will be as well. However, one
4351+
limitation when creating new indexes on partitioned tables is that it
4352+
is not possible to use the <literal>CONCURRENTLY</literal>
4353+
qualifier, which could lead to long lock times. To avoid this, you can
4354+
use <command>CREATE INDEX ON ONLY</command> the partitioned table, which
4355+
creates the new index marked as invalid, preventing automatic application
4356+
to existing partitions. Instead, indexes can then be created individually
4357+
on each partition using <literal>CONCURRENTLY</literal> and
4358+
<firstterm>attached</firstterm> to the partitioned index on the parent
4359+
using <command>ALTER INDEX ... ATTACH PARTITION</command>. Once indexes for
4360+
all the partitions are attached to the parent index, the parent index will
4361+
be marked valid automatically. Example:
43624362
<programlisting>
43634363
CREATE INDEX measurement_usls_idx ON ONLY measurement (unitsales);
43644364

0 commit comments

Comments
 (0)