django迁移期间的OperationalError在大型表上发布

时间:2017-12-04 13:34:04

标签: python django postgresql

有时在大桌子上的django迁移(> 5000万行)时,我收到以下错误:

回溯:

Traceback (most recent call last):
  File "src/audit/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/operations/fields.py", line 86, in database_forwards
    field,
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 439, in add_field
    self.execute(sql)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.OperationalError: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.

你能不能给我建议 - 我应该改变哪些postgres设置才能解决这个问题? 我尝试设置statement_timeout = 0, idle_in_transaction_session_timeout = 0没有结果。

迁移:

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0028_auto_'),
    ]

    operations = [
        migrations.AddField(
            model_name='risk',
            name='test3',
            field=models.CharField(default='testme', max_length=50),
        ),
    ]

SQL:

BEGIN;
--
-- Add field test3 to risk
--
ALTER TABLE "core_risk" ADD COLUMN "test3" varchar(50) DEFAULT 'testme' NOT NULL;
ALTER TABLE "core_risk" ALTER COLUMN "test3" DROP DEFAULT;
COMMIT;

0 个答案:

没有答案