Skip to content

Commit 8709228

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 7b7fbe1 commit 8709228

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

doc/src/sgml/ddl.sgml

+25-3
Original file line numberDiff line numberDiff line change
@@ -3970,6 +3970,11 @@ ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
39703970
</programlisting>
39713971
</para>
39723972

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

39874009
<para>

doc/src/sgml/ref/alter_table.sgml

+11-2
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,17 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
976976
<para>
977977
Attaching a partition acquires a
978978
<literal>SHARE UPDATE EXCLUSIVE</literal> lock on the parent table,
979-
in addition to <literal>ACCESS EXCLUSIVE</literal> locks on the table
980-
to be attached and on the default partition (if any).
979+
in addition to the <literal>ACCESS EXCLUSIVE</literal> locks on the table
980+
being attached and on the default partition (if any).
981+
</para>
982+
983+
<para>
984+
Further locks must also be held on all sub-partitions if the table being
985+
attached is itself a partitioned table. Likewise if the default
986+
partition is itself a partitioned table. The locking of the
987+
sub-partitions can be avoided by adding a <literal>CHECK</literal>
988+
constraint as described in
989+
<xref linkend="ddl-partitioning-declarative-maintenance"/>.
981990
</para>
982991
</listitem>
983992
</varlistentry>

0 commit comments

Comments
 (0)