adding-not-nullable-field
Use a check constraint instead of setting a column as NOT NULL
.
Postgres Version
In Postgres versions 11 of later, adding a non-null column with a default will complete without a table scan.
#
problemAdding a column as NOT NULL
requires a table scan and the ALTER TABLE
requires
an ACCESS EXCLUSIVE
lock. Reads and writes will be disabled while this statement is running.
#
solutions#
adding a non-nullable columnAdd a column as nullable and use a check constraint to verify integrity. The check constraint should be added as NOT NULL
and then validated.
Instead of:
Use:
Add the column as nullable, add a check constraint as NOT VALID
to verify new rows and updates are, backfill the column so it no longer contains null values, validate the constraint to verify existing rows are valid.
See "How not valid constraints work" for more information on adding constraints as NOT VALID
.
#
setting an existing column as non-nullableInstead of:
Use:
Add a check constraint as NOT VALID
to verify new rows and updates are, backfill the column so it no longer contains null values, validate the constraint to verify existing rows are valid.
See "How not valid constraints work" for more information on adding constraints as NOT VALID
.
#
solution for alembic and sqlalchemyInstead of:
Use:
Create Alembic migration manually, because Alembic not creates migration for constraints automatically. See the related GitHub Issue here.