@@ -359,27 +359,36 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
359359 <xref linkend="sql-createtable"/>, plus the option <literal>NOT
360360 VALID</literal>, which is currently only allowed for foreign key
361361 and CHECK constraints.
362- If the constraint is marked <literal>NOT VALID</literal>, the
363- potentially-lengthy initial check to verify that all rows in the table
364- satisfy the constraint is skipped. The constraint will still be
362+ </para>
363+
364+ <para>
365+ Normally, this form will cause a scan of the table to verify that all
366+ existing rows in the table satisfy the new constraint. But if
367+ the <literal>NOT VALID</literal> option is used, this
368+ potentially-lengthy scan is skipped. The constraint will still be
365369 enforced against subsequent inserts or updates (that is, they'll fail
366370 unless there is a matching row in the referenced table, in the case
367- of foreign keys; and they'll fail unless the new row matches the
368- specified checkconstraints ). But the
371+ of foreign keys, or they'll fail unless the new row matches the
372+ specified checkcondition ). But the
369373 database will not assume that the constraint holds for all rows in
370374 the table, until it is validated by using the <literal>VALIDATE
371- CONSTRAINT</literal> option. Foreign key constraints on partitioned
372- tables may not be declared <literal>NOT VALID</literal> at present.
375+ CONSTRAINT</literal> option.
376+ See <xref linkend="sql-altertable-notes"
377+ endterm="sql-altertable-notes-title"/> below for more information
378+ about using the <literal>NOT VALID</literal> option.
373379 </para>
374380
375381 <para>
376- The addition of a foreign key constraint requires a
377- <literal>SHARE ROW EXCLUSIVE</literal> lock on the referenced table.
382+ Addition of a foreign key constraint requires a
383+ <literal>SHARE ROW EXCLUSIVE</literal> lock on the referenced table,
384+ in addition to the lock on the table receiving the constraint.
378385 </para>
379386
380387 <para>
381388 Additional restrictions apply when unique or primary key constraints
382- are added to partitioned tables; see <xref linkend="sql-createtable" />.
389+ are added to partitioned tables; see <xref linkend="sql-createtable"/>.
390+ Also, foreign key constraints on partitioned
391+ tables may not be declared <literal>NOT VALID</literal> at present.
383392 </para>
384393
385394 </listitem>
@@ -454,23 +463,13 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
454463 <term><literal>VALIDATE CONSTRAINT</literal></term>
455464 <listitem>
456465 <para>
457- This form validates a foreign key or check constraint that was previously created
458- as <literal>NOT VALID</literal>, by scanning the table to ensure there
459- are no rows for which the constraint is not satisfied.
460- Nothing happens if the constraint is already marked valid.
461- </para>
462- <para>
463- Validation can be a long process on larger tables. The value of separating
464- validation from initial creation is that you can defer validation to less
465- busy times, or can be used to give additional time to correct pre-existing
466- errors while preventing new errors. Note also that validation on its own
467- does not prevent normal write commands against the table while it runs.
468- </para>
469- <para>
470- Validation acquires only a <literal>SHARE UPDATE EXCLUSIVE</literal> lock
471- on the table being altered. If the constraint is a foreign key then
472- a <literal>ROW SHARE</literal> lock is also required on
473- the table referenced by the constraint.
466+ This form validates a foreign key or check constraint that was
467+ previously created as <literal>NOT VALID</literal>, by scanning the
468+ table to ensure there are no rows for which the constraint is not
469+ satisfied. Nothing happens if the constraint is already marked valid.
470+ (See <xref linkend="sql-altertable-notes"
471+ endterm="sql-altertable-notes-title"/> below for an explanation of the
472+ usefulness of this command.)
474473 </para>
475474 </listitem>
476475 </varlistentry>
@@ -1190,8 +1189,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
11901189 </variablelist>
11911190 </refsect1>
11921191
1193- <refsect1>
1194- <title>Notes</title>
1192+ <refsect1 id="sql-altertable-notes" >
1193+ <title id="sql-altertable-notes-title" >Notes</title>
11951194
11961195 <para>
11971196 The key word <literal>COLUMN</literal> is noise and can be omitted.
@@ -1237,6 +1236,32 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
12371236 rewrites can thereby be combined into a single pass over the table.
12381237 </para>
12391238
1239+ <para>
1240+ Scanning a large table to verify a new foreign key or check constraint
1241+ can take a long time, and other updates to the table are locked out
1242+ until the <command>ALTER TABLE ADD CONSTRAINT</command> command is
1243+ committed. The main purpose of the <literal>NOT VALID</literal>
1244+ constraint option is to reduce the impact of adding a constraint on
1245+ concurrent updates. With <literal>NOT VALID</literal>,
1246+ the <command>ADD CONSTRAINT</command> command does not scan the table
1247+ and can be committed immediately. After that, a <literal>VALIDATE
1248+ CONSTRAINT</literal> command can be issued to verify that existing rows
1249+ satisfy the constraint. The validation step does not need to lock out
1250+ concurrent updates, since it knows that other transactions will be
1251+ enforcing the constraint for rows that they insert or update; only
1252+ pre-existing rows need to be checked. Hence, validation acquires only
1253+ a <literal>SHARE UPDATE EXCLUSIVE</literal> lock on the table being
1254+ altered. (If the constraint is a foreign key then a <literal>ROW
1255+ SHARE</literal> lock is also required on the table referenced by the
1256+ constraint.) In addition to improving concurrency, it can be useful to
1257+ use <literal>NOT VALID</literal> and <literal>VALIDATE
1258+ CONSTRAINT</literal> in cases where the table is known to contain
1259+ pre-existing violations. Once the constraint is in place, no new
1260+ violations can be inserted, and the existing problems can be corrected
1261+ at leisure until <literal>VALIDATE CONSTRAINT</literal> finally
1262+ succeeds.
1263+ </para>
1264+
12401265 <para>
12411266 The <literal>DROP COLUMN</literal> form does not physically remove
12421267 the column, but simply makes it invisible to SQL operations. Subsequent