Skip to content

Commit c590904

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 6feb229 commit c590904

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
@@ -3962,6 +3962,11 @@ ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
39623962
</programlisting>
39633963
</para>
39643964

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

39794001
<para>

doc/src/sgml/ref/alter_table.sgml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,17 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
913913
<para>
914914
Attaching a partition acquires a
915915
<literal>SHARE UPDATE EXCLUSIVE</literal> lock on the parent table,
916-
in addition to <literal>ACCESS EXCLUSIVE</literal> locks on the table
917-
to be attached and on the default partition (if any).
916+
in addition to the <literal>ACCESS EXCLUSIVE</literal> locks on the table
917+
being attached and on the default partition (if any).
918+
</para>
919+
920+
<para>
921+
Further locks must also be held on all sub-partitions if the table being
922+
attached is itself a partitioned table. Likewise if the default
923+
partition is itself a partitioned table. The locking of the
924+
sub-partitions can be avoided by adding a <literal>CHECK</literal>
925+
constraint as described in
926+
<xref linkend="ddl-partitioning-declarative-maintenance"/>.
918927
</para>
919928
</listitem>
920929
</varlistentry>

0 commit comments

Comments
 (0)