Skip to main content

Avoid costly downtime during Postgres migrations.

Project description

django-pgmigrate

django-pgmigrate helps you avoid costly downtime with Postgres migrations.

Imagine the following happens:

  1. A long-running task queries a model in a transaction and keeps the transaction open.
  2. python manage.py migrate tries to change a field on the model.

Because of how Postgres queues locks, this common scenario causes every subsequent query on the model to block until the query from 1) has finished.

django-pgmigrate provides the following features to alleviate problematic locking scenarios when running migrations:

  • Detect blocking queries and terminate them automatically (the default behavior).
  • Print blocking queries so that you can inspect and terminate them manually.
  • Set the lock timeout so that migrations are terminated if they block too long.

Installation

Install django-pgmigrate with:

pip3 install django-pgmigrate

After this, add pgactivity, pglock, and pgmigrate to the INSTALLED_APPS setting of your Django project.

Quick Start

After following the installation instructions, running python manage.py migrate will automatically terminate any blocking queries. Here's an example of what it looks like:

Terminate Blocking

There are two additional outputs in the migrate command versus the original:

  1. The first output line shows the Postgres process ID. This is useful for querying activity that's blocking the process.
  2. The yellow text shows when a blocking query was detected and terminated. In our case, it was blocking auth migration 12.

You can configure django-pgmigrate to show blocked queries instead of automatically killing them, and you can also set the lock timeout to automatically cancel migrations if they block for too long. See the documentation section below for more details.

Compatibility

django-pgmigrate is compatible with Python 3.10 - 3.14, Django 4.2 - 6.0, Psycopg 2 - 3, and Postgres 14 - 18.

Documentation

View the django-pgmigrate docs here to learn more about:

  • How blocking queries are automatically terminated.
  • Configuring the command to show blocking activity instead of terminating it, along with instructions on how to manually view and terminate activity.
  • Configuring lock timeouts to automatically stop migrations if they block for too long.
  • Advanced usage such as creating custom actions to run when queries are blocked.

Contributing Guide

For information on setting up django-pgmigrate for development and contributing changes, view CONTRIBUTING.md.

Creators

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_pgmigrate-1.6.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pgmigrate-1.6.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file django_pgmigrate-1.6.0.tar.gz.

File metadata

  • Download URL: django_pgmigrate-1.6.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Linux/6.8.0-1040-aws

File hashes

Hashes for django_pgmigrate-1.6.0.tar.gz
Algorithm Hash digest
SHA256 13b6bb02a48d9cd2825012d2bb09d81c3bcb0c6c9e33a112933351ddb6fe800e
MD5 49de1fb58dfe1ceec146cdb2d268a636
BLAKE2b-256 5d2795c89304b84b95e75d388eb44770cae380b762725aa16e486c1eac46717a

See more details on using hashes here.

File details

Details for the file django_pgmigrate-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: django_pgmigrate-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Linux/6.8.0-1040-aws

File hashes

Hashes for django_pgmigrate-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7eacdfd1282f3e03cd58211da100372d0e584a9ed87429171b85e32be8c1f596
MD5 18ed9b58827af3a499a80a7151b76527
BLAKE2b-256 91ea0620df0605084ae9289d231b91e4f3399bf496113d5a95ef7f70597f4dea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page