+ endterm="sql-altertable-notes-title"/> below for more information
+ about using the NOT VALID option.
- The addition of a foreign key constraint requires a
- SHARE ROW EXCLUSIVE lock on the referenced table.
+ Addition of a foreign key constraint requires a
+ SHARE ROW EXCLUSIVE lock on the referenced table,
+ in addition to the lock on the table receiving the constraint.
Additional restrictions apply when unique or primary key constraints
- are added to partitioned tables; see .
+ are added to partitioned tables; see .
+ Also, foreign key constraints on partitioned
+ tables may not be declared NOT VALID at present.
VALIDATE CONSTRAINT
- This form validates a foreign key or check constraint that was previously created
- as NOT VALID, by scanning the table to ensure there
- are no rows for which the constraint is not satisfied.
- Nothing happens if the constraint is already marked valid.
-
- Validation can be a long process on larger tables. The value of separating
- validation from initial creation is that you can defer validation to less
- busy times, or can be used to give additional time to correct pre-existing
- errors while preventing new errors. Note also that validation on its own
- does not prevent normal write commands against the table while it runs.
-
- Validation acquires only a SHARE UPDATE EXCLUSIVE lock
- on the table being altered. If the constraint is a foreign key then
- a ROW SHARE lock is also required on
- the table referenced by the constraint.
+ This form validates a foreign key or check constraint that was
+ previously created as NOT VALID, by scanning the
+ table to ensure there are no rows for which the constraint is not
+ satisfied. Nothing happens if the constraint is already marked valid.
+ (See
+ endterm="sql-altertable-notes-title"/> below for an explanation of the
+ usefulness of this command.)
-
-
Notes
+ id="sql-altertable-notes">
+
id="sql-altertable-notes-title">Notes
The key word COLUMN is noise and can be omitted.
rewrites can thereby be combined into a single pass over the table.
+ Scanning a large table to verify a new foreign key or check constraint
+ can take a long time, and other updates to the table are locked out
+ until the ALTER TABLE ADD CONSTRAINT command is
+ committed. The main purpose of the NOT VALID
+ constraint option is to reduce the impact of adding a constraint on
+ concurrent updates. With NOT VALID,
+ the ADD CONSTRAINT command does not scan the table
+ and can be committed immediately. After that, a VALIDATE
+ CONSTRAINT command can be issued to verify that existing rows
+ satisfy the constraint. The validation step does not need to lock out
+ concurrent updates, since it knows that other transactions will be
+ enforcing the constraint for rows that they insert or update; only
+ pre-existing rows need to be checked. Hence, validation acquires only
+ a SHARE UPDATE EXCLUSIVE lock on the table being
+ altered. (If the constraint is a foreign key then a ROW
+ SHARE lock is also required on the table referenced by the
+ constraint.) In addition to improving concurrency, it can be useful to
+ use NOT VALID and VALIDATE
+ CONSTRAINT in cases where the table is known to contain
+ pre-existing violations. Once the constraint is in place, no new
+ violations can be inserted, and the existing problems can be corrected
+ at leisure until VALIDATE CONSTRAINT finally
+ succeeds.
+
+
The DROP COLUMN form does not physically remove
the column, but simply makes it invisible to SQL operations. Subsequent
and then to apply column NOT NULL constraints to columns of
the domain type as needed, rather than directly to the domain type.
+
+
PostgreSQL assumes that
+ CHECK constraints' conditions are immutable, that is,
+ they will always give the same result for the same input value. This
+ assumption is what justifies examining CHECK
+ constraints only when a value is first converted to be of a domain type,
+ and not at other times. (This is essentially the same as the treatment
+ of table CHECK constraints, as described in
+ .)
+
+
+ An example of a common way to break this assumption is to reference a
+ user-defined function in a CHECK expression, and then
+ change the behavior of that
+ function.
PostgreSQL does not disallow that,
+ but it will not notice if there are stored values of the domain type that
+ now violate the CHECK constraint. That would cause a
+ subsequent database dump and reload to fail. The recommended way to
+ handle such a change is to drop the constraint (using ALTER
+ DOMAIN), adjust the function definition, and re-add the
+ constraint, thereby rechecking it against stored data.
+