doc: mention unusability of dropped CHECK to verify NOT NULL

It's possible to use a CHECK (col IS NOT NULL) constraint to skip
scanning a table for nulls when adding a NOT NULL constraint on the same
column.  However, if the CHECK constraint is dropped on the same command
that the NOT NULL is added, this fails, i.e., makes the NOT NULL addition
slow.  The best we can do about it at this stage is to document this so
that users aren't taken by surprise.

(In Postgres 18 you can directly add the NOT NULL constraint as NOT
VALID instead, so there's no longer much use for the CHECK constraint,
therefore no point in building mechanism to support the case better.)

Reported-by: Andrew <psy2000usa@yahoo.com>
Reviewed-by: David G. Johnston <david.g.johnston@gmail.com>
Discussion: https://postgr.es/m/175385113607.786.16774570234342968908@wrigleys.postgresql.org
This commit is contained in:
Álvaro Herrera 2025-08-04 13:26:45 +02:00
parent bca9a1900c
commit 126665289f
No known key found for this signature in database
GPG Key ID: 1C20ACB9D5C564AE

View File

@ -240,9 +240,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
provided none of the records in the table contain a provided none of the records in the table contain a
<literal>NULL</literal> value for the column. Ordinarily this is <literal>NULL</literal> value for the column. Ordinarily this is
checked during the <literal>ALTER TABLE</literal> by scanning the checked during the <literal>ALTER TABLE</literal> by scanning the
entire table; however, if a valid <literal>CHECK</literal> constraint is entire table, unless <literal>NOT VALID</literal> is specified;
found which proves no <literal>NULL</literal> can exist, then the however, if a valid <literal>CHECK</literal> constraint exists
table scan is skipped. (and is not dropped in the same command) which proves no
<literal>NULL</literal> can exist, then the table scan is skipped.
If a column has an invalid not-null constraint, If a column has an invalid not-null constraint,
<literal>SET NOT NULL</literal> validates it. <literal>SET NOT NULL</literal> validates it.
</para> </para>