1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-26 18:17:33 +03:00

Doc: clarify locking requirements for ALTER TABLE ADD FOREIGN KEY.

The docs explained that a SHARE ROW EXCLUSIVE lock is needed on the
referenced table, but failed to say the same about the table being
altered.  Since the page says that ACCESS EXCLUSIVE lock is taken
unless otherwise stated, this left readers with the wrong conclusion.

Discussion: https://postgr.es/m/834603375.3470346.1586482852542@mail.yahoo.com
This commit is contained in:
Tom Lane 2020-04-10 13:12:58 -04:00
parent 8f111150e6
commit 61a9b814c5

View File

@ -99,9 +99,9 @@ ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
<para> <para>
<command>ALTER TABLE</command> changes the definition of an existing table. <command>ALTER TABLE</command> changes the definition of an existing table.
There are several subforms described below. Note that the lock level required There are several subforms described below. Note that the lock level required
may differ for each subform. An <literal>ACCESS EXCLUSIVE</literal> lock is held may differ for each subform. An <literal>ACCESS EXCLUSIVE</literal> lock is
unless explicitly noted. When multiple subcommands are listed, the lock acquired unless explicitly noted. When multiple subcommands are given, the
held will be the strictest one required from any subcommand. lock acquired will be the strictest one required by any subcommand.
<variablelist> <variablelist>
<varlistentry> <varlistentry>
@ -313,8 +313,8 @@ ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
<term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable> [ NOT VALID ]</literal></term> <term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable> [ NOT VALID ]</literal></term>
<listitem> <listitem>
<para> <para>
This form adds a new constraint to a table using the same syntax as This form adds a new constraint to a table using the same constraint
<xref linkend="SQL-CREATETABLE">, plus the option <literal>NOT syntax as <xref linkend="sql-createtable">, plus the option <literal>NOT
VALID</literal>, which is currently only allowed for foreign key VALID</literal>, which is currently only allowed for foreign key
and CHECK constraints. and CHECK constraints.
</para> </para>
@ -335,6 +335,17 @@ ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
endterm="sql-altertable-notes-title"> below for more information endterm="sql-altertable-notes-title"> below for more information
about using the <literal>NOT VALID</literal> option. about using the <literal>NOT VALID</literal> option.
</para> </para>
<para>
Although most forms of <literal>ADD
<replaceable class="parameter">table_constraint</replaceable></literal>
require an <literal>ACCESS EXCLUSIVE</literal> lock, <literal>ADD
FOREIGN KEY</literal> requires only a <literal>SHARE ROW
EXCLUSIVE</literal> lock. Note that <literal>ADD FOREIGN KEY</literal>
also acquires a <literal>SHARE ROW EXCLUSIVE</literal> lock on the
referenced table, in addition to the lock on the table on which the
constraint is declared.
</para>
</listitem> </listitem>
</varlistentry> </varlistentry>