@@ -359,27 +359,36 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
359
359
<xref linkend="sql-createtable"/>, plus the option <literal>NOT
360
360
VALID</literal>, which is currently only allowed for foreign key
361
361
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
365
369
enforced against subsequent inserts or updates (that is, they'll fail
366
370
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
369
373
database will not assume that the constraint holds for all rows in
370
374
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.
373
379
</para>
374
380
375
381
<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.
378
385
</para>
379
386
380
387
<para>
381
388
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.
383
392
</para>
384
393
385
394
</listitem>
@@ -454,23 +463,13 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
454
463
<term><literal>VALIDATE CONSTRAINT</literal></term>
455
464
<listitem>
456
465
<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.)
474
473
</para>
475
474
</listitem>
476
475
</varlistentry>
@@ -1190,8 +1189,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
1190
1189
</variablelist>
1191
1190
</refsect1>
1192
1191
1193
- <refsect1>
1194
- <title>Notes</title>
1192
+ <refsect1 id="sql-altertable-notes" >
1193
+ <title id="sql-altertable-notes-title" >Notes</title>
1195
1194
1196
1195
<para>
1197
1196
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
1237
1236
rewrites can thereby be combined into a single pass over the table.
1238
1237
</para>
1239
1238
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
+
1240
1265
<para>
1241
1266
The <literal>DROP COLUMN</literal> form does not physically remove
1242
1267
the column, but simply makes it invisible to SQL operations. Subsequent