Skip to content

Commit aa1e921

Browse files
committed
Doc: Clarify lock levels taken during ATTACH PARTITION
It wasn't all that clear which lock levels, if any, would be held on the DEFAULT partition during an ATTACH PARTITION operation. Also, clarify which locks will be taken if the DEFAULT partition or the table being attached are themselves partitioned tables. Here I'm only backpatching to v12 as before then we obtained an ACCESS EXCLUSIVE lock on the partitioned table. It seems much less relevant to mention which locks are taken on other tables when the partitioned table itself is locked with an ACCESS EXCLUSIVE lock. Author: Matthias van de Meent, David Rowley Discussion: https://postgr.es/m/CAEze2WiTB6iwrV8W_J=fnrnZ7fowW3qu-8iQ8zCHP3FiQ6+o-A@mail.gmail.com Backpatch-through: 12
1 parent b8f91d7 commit aa1e921

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3968,6 +3968,11 @@ ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
39683968
</programlisting>
39693969
</para>
39703970

3971+
<para>
3972+
The <command>ATTACH PARTITION</command> command requires taking a
3973+
<literal>SHARE UPDATE EXCLUSIVE</literal> lock on the partitioned table.
3974+
</para>
3975+
39713976
<para>
39723977
Before running the <command>ATTACH PARTITION</command> command, it is
39733978
recommended to create a <literal>CHECK</literal> constraint on the table to
@@ -3976,10 +3981,27 @@ ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
39763981
which is otherwise needed to validate the implicit
39773982
partition constraint. Without the <literal>CHECK</literal> constraint,
39783983
the table will be scanned to validate the partition constraint while
3979-
holding both an <literal>ACCESS EXCLUSIVE</literal> lock on that partition
3980-
and a <literal>SHARE UPDATE EXCLUSIVE</literal> lock on the parent table.
3984+
holding an <literal>ACCESS EXCLUSIVE</literal> lock on that partition.
39813985
It is recommended to drop the now-redundant <literal>CHECK</literal>
3982-
constraint after <command>ATTACH PARTITION</command> is finished.
3986+
constraint after the <command>ATTACH PARTITION</command> is complete. If
3987+
the table being attached is itself a partitioned table then each of its
3988+
sub-partitions will be recursively locked and scanned until either a
3989+
suitable <literal>CHECK</literal> constraint is encountered or the leaf
3990+
partitions are reached.
3991+
</para>
3992+
3993+
<para>
3994+
Similarly, if the partitioned table has a <literal>DEFAULT</literal>
3995+
partition, it is recommended to create a <literal>CHECK</literal>
3996+
constraint which excludes the to-be-attached partition's constraint. If
3997+
this is not done then the <literal>DEFAULT</literal> partition will be
3998+
scanned to verify that it contains no records which should be located in
3999+
the partition being attached. This operation will be performed whilst
4000+
holding an <literal>ACCESS EXCLUSIVE</literal> lock on the <literal>
4001+
DEFAULT</literal> partition. If the <literal>DEFAULT</literal> partition
4002+
is itself a partitioned table then each of its partitions will be
4003+
recursively checked in the same way as the table being attached, as
4004+
mentioned above.
39834005
</para>
39844006

39854007
<para>

doc/src/sgml/ref/alter_table.sgml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,17 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
934934
<para>
935935
Attaching a partition acquires a
936936
<literal>SHARE UPDATE EXCLUSIVE</literal> lock on the parent table,
937-
in addition to <literal>ACCESS EXCLUSIVE</literal> locks on the table
938-
to be attached and on the default partition (if any).
937+
in addition to the <literal>ACCESS EXCLUSIVE</literal> locks on the table
938+
being attached and on the default partition (if any).
939+
</para>
940+
941+
<para>
942+
Further locks must also be held on all sub-partitions if the table being
943+
attached is itself a partitioned table. Likewise if the default
944+
partition is itself a partitioned table. The locking of the
945+
sub-partitions can be avoided by adding a <literal>CHECK</literal>
946+
constraint as described in
947+
<xref linkend="ddl-partitioning-declarative-maintenance"/>.
939948
</para>
940949
</listitem>
941950
</varlistentry>

0 commit comments

Comments
 (0)