Avoid costly downtime during Postgres migrations.
Project description
django-pgmigrate
django-pgmigrate
helps you avoid costly downtime with Postgres migrations.
Imagine the following happens:
- A long-running task queries a model in a transaction and keeps the transaction open.
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:
There are two additional outputs in the migrate
command versus the original:
- The first output line shows the Postgres process ID. This is useful for querying activity that's blocking the process.
- 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.9 - 3.13, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 17.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_pgmigrate-1.5.0.tar.gz
.
File metadata
- Download URL: django_pgmigrate-1.5.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/5.15.0-1057-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6949188ccaae26e7626c4edb7f8af93b83bdda08cf4c169631dddf2622fdd1df |
|
MD5 | 2003e4c614df5cf8ed957e1f193fe24c |
|
BLAKE2b-256 | b4e3a4a6d0db1cbed906f6b41440a3986150090fde79f9d2725cfbc2ef03c7dc |
File details
Details for the file django_pgmigrate-1.5.0-py3-none-any.whl
.
File metadata
- Download URL: django_pgmigrate-1.5.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/5.15.0-1057-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83836c4cfd631f9d506bc72ed92c12e0329ba19208747d60aa19de96582fe54e |
|
MD5 | c6c227301761ab5fe6f7847ec3325fef |
|
BLAKE2b-256 | b11d5d9eb5bb35660dc096062b9f023305e0778ee4b04f332843b7e2b7f2d02e |