Django迁移postgres在大表上时发生操作错误

2024-04-25 23:34:52 发布

您现在位置:Python中文网/ 问答频道 /正文

有时在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;

Tags: djangoinpycoreselfexecutedblib