[Django]-Do django db_index migrations run concurrently?

16πŸ‘

βœ…

There are AddIndexConcurrently and RemoveIndexConcurrently in Django 3.0:

https://docs.djangoproject.com/en/dev/ref/contrib/postgres/operations/#django.contrib.postgres.operations.AddIndexConcurrently

Create a migration and then change migrations.AddIndex to AddIndexConcurrently. Import it from django.contrib.postgres.operations.

πŸ‘€Max Malysh

13πŸ‘

With Django 1.10 migrations you can create a concurrent index by using RunSQL and disabling the wrapping transaction by making the migration non-atomic by setting atomic = False as a data attribute on the migration:

class Migration(migrations.Migration):
    atomic = False # disable transaction

    dependencies = []

    operations = [
        migrations.RunSQL('CREATE INDEX CONCURRENTLY ...')
    ]
πŸ‘€tgroshon

10πŸ‘

You could use the SeparateDatabaseAndState migration operation to provide a custom SQL command for creating the index. The operation accepts two lists of operations:

  • state_operations are operations to apply on the Django model state.
    They do not affect the database.

  • database_operations are operations to apply to the database.

An example migration may look like this:

from django.db import migrations, models

class Migration(migrations.Migration):
    atomic = False

    dependencies = [
        ('myapp', '0001_initial'),
    ]

    operations = [    
        migrations.SeparateDatabaseAndState(    
            state_operations=[
                # operation generated by `makemigrations` to create an ordinary index
                migrations.AlterField(
                    # ...  
                ),
            ],

            database_operations=[
                # operation to run custom SQL command (check the output of `sqlmigrate`
                # to see the auto-generated SQL, edit as needed)
                migrations.RunSQL(sql='CREATE INDEX CONCURRENTLY ...',
                                  reverse_sql='DROP INDEX ...'),
            ],
        ),
    ]

1πŸ‘

Do what tgroshon says for new django 1.10 +

for lesser versions of django i have had success with a more verbose subclassing method:

from django.db import migrations, models


class RunNonAtomicSQL(migrations.RunSQL):
    def _run_sql(self, schema_editor, sqls):
        if schema_editor.connection.in_atomic_block:
            schema_editor.atomic.__exit__(None, None, None)
        super(RunNonAtomicSQL, self)._run_sql(schema_editor, sqls)


class Migration(migrations.Migration):
    dependencies = [
    ]

    operations = [

        RunNonAtomicSQL(
            "CREATE INDEX CONCURRENTLY",
        )
    ]
πŸ‘€lee penkman

0πŸ‘

There is no support for PostgreSQL concurent index creation in django.

Here is the ticket requesting this feature – https://code.djangoproject.com/ticket/21039

But instead, you can manually specify any custom RunSQL operation in the migration –
https://docs.djangoproject.com/en/stable/ref/migration-operations/#runsql

πŸ‘€kmmbvnr

0πŸ‘

You can do something like


import django.contrib.postgres.indexes
from django.db import migrations, models
from django.contrib.postgres.operations import AddIndexConcurrently


class Migration(migrations.Migration):

    atomic = False

    dependencies = [
        ("app_name", "parent_migration"),
    ]

    operations = [
        AddIndexConcurrently(
            model_name="mymodel",
            index=django.contrib.postgres.indexes.GinIndex(
                fields=["field1"],
                name="field1_idx",
            ),
        ),
        AddIndexConcurrently(
            model_name="mymodel",
            index=models.Index(
                fields=["field2"], name="field2_idx"
            ),
        ),
    ]

Ref: https://docs.djangoproject.com/en/dev/ref/contrib/postgres/operations/#django.contrib.postgres.operations.AddIndexConcurrently

Leave a comment