About Huge Data Migrations

By default, Django wraps each migration in a transaction on databases that support transactions (like PostgreSQL). For huge data migrations, set atomic = False on the Migration class so rows are committed in batches rather than held in one enormous transaction that balloons memory.

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

The combination of atomic = False, chunked processing, idempotent logic, and raw SQL where possible will handle most cases without needing more RAM.

Tips and Tricks Programming Development Databases Django 6.x Django 5.2 Django 4.2 PostgreSQL MySQL Migrations