#31653: PostgreSQL add constraints via NOT VALID / VALIDATE CONSTRAINT
-------------------------------------------------+------------------------
               Reporter:  Adam (Chainz) Johnson  |          Owner:  nobody
                   Type:  Cleanup/optimization   |         Status:  new
              Component:  Migrations             |        Version:  master
               Severity:  Normal                 |       Keywords:
           Triage Stage:  Unreviewed             |      Has patch:  0
    Needs documentation:  0                      |    Needs tests:  0
Patch needs improvement:  0                      |  Easy pickings:  0
                  UI/UX:  0                      |
-------------------------------------------------+------------------------
 If you read the ALTER TABLE PostgreSQL docs (
 https://www.postgresql.org/docs/12/sql-altertable.html ) for `ADD
 CONSTRAINT`, you'll see that it supports the `NOT VALID` option. This
 prevents the constraint from being checked against all existing data,
 although it does affect all inserted/updated rows. Adding a constraint
 this way does not exclusively lock the whole table to check it. To promote
 it to valid, the `VALIDATE CONSTRAINT` syntax can be used. This allows a
 non-exclusive lock on the table to validate all existing rows. This two
 step techinque allows constraints to be added to very active tables
 without locking out concurrent access and thus inflicting downtime.

 I suggest Django always use this form when adding foreign keys and check
 constraints.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31653>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.cbfb713aa7d5bb0072c24d352ff808e1%40djangoproject.com.

Reply via email to