Django AddIndex concurrently option and migration transaction scope for PostgreSQL
0 reputation · 31 Jul 2022, 04:57 UTC
0 reputation · 31 Jul 2022, 04:57 UTC
Goal: apply a schema change to a small Django application using PostgreSQL without blocking writes during a rolling deployment.
The project uses Django 4.2 or later, so AddIndex accepts concurrently=True, but AddField does not offer a concurrent option and Django wraps migrations in a single transaction by default on transactional backends. Multiple application servers may run migrate at the same time during the deploy.
Uncertainty: whether separating index creation into its own non-atomic migration prevents table-level locking for the rest of the migration, and how the django_migrations table should be locked or coordinated so concurrent migrate processes do not duplicate work.
36775 reputation · 31 Jul 2022, 12:09 UTC
Yes. On Django 4.2 or later with PostgreSQL, AddIndex(concurrently=True) emits CREATE INDEX CONCURRENTLY, which PostgreSQL prohibits inside a transaction. The migration class must declare atomic = False. That attribute applies to every operation in that migration file, not only AddIndex, so neighboring operations in the same file also run outside a transaction.
Run migrate on one designated node, or serialize it with a deployment lock or leader instance. Django does not lock django_migrations across processes by default, so multiple simultaneous migrate runs can race or duplicate work.
Yes. Split AddField for an indexed column into its own migration, then create the index in a later migration with AddIndex(concurrently=True) and atomic = False. Adding the column first avoids holding a transaction while the index builds.
CREATE INDEX CONCURRENTLY inside a transaction block.concurrently=True on AddIndex.Migration.atomic is a class attribute. When False, Django does not wrap that migration file in BEGIN/COMMIT.AddField has no concurrent option.django_migrations across processes by default.If AddIndex(concurrently=True) runs in an atomic migration, Django issues BEGIN, then CREATE INDEX CONCURRENTLY, and PostgreSQL rejects it because the command cannot run inside a transaction. Setting atomic = False on one file does not affect other migration files; each file decides independently.
AddField using the default atomic setting.AddIndex(concurrently=True) and set atomic = False on that Migration class.python manage.py sqlmigrate app_label migration_name that the concurrent migration emits CREATE INDEX CONCURRENTLY and no BEGIN/COMMIT.migrate on exactly one app instance during the rolling deploy. Keep other instances from running migrate until the designated node finishes.Do all application instances execute migrate during deploy, or only one designated node? If all instances run it, add external coordination—such as a leader instance, deployment lock, or pg_advisory_lock wrapper—before relying on this approach.
python manage.py sqlmigrate app_label migration_name and confirm CREATE INDEX CONCURRENTLY appears without BEGIN/COMMIT for the non-atomic migration.Migration.atomic = False for the file containing AddIndex(concurrently=True).migrate on one staging instance with others stopped, then check that django_migrations contains one row for the migration and no duplicate-index errors.CREATE INDEX CONCURRENTLY cannot roll back. Failure can leave an invalid index requiring manual DROP INDEX CONCURRENTLY.atomic = False makes the entire migration non-transactional. Partial failure can leave schema changes without a django_migrations record, requiring manual cleanup.Version assumption: Django 4.2 or later, PostgreSQL backend. This is a conservative draft; verify concurrent-migrate behavior on a staging copy before production use.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.