Skip to main content

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

Metadata

Release files for django-pgmigrate 1.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-pgmigrate 1.6.0
File Size Uploaded
django_pgmigrate-1.6.0.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-pgmigrate 1.6.0
File Interpreter ABI Platform
django_pgmigrate-1.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / django_pgmigrate-1.6.0.tar.gz

Download URL django_pgmigrate-1.6.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
13b6bb02a48d9cd2825012d2bb09d81c3bcb0c6c9e33a112933351ddb6fe800e
BLAKE2b-256 checksum
How to use checksums
5d2795c89304b84b95e75d388eb44770cae380b762725aa16e486c1eac46717a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.0 Linux/6.8.0-1040-aws

Release files / django_pgmigrate-1.6.0-py3-none-any.whl

Download URL django_pgmigrate-1.6.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7eacdfd1282f3e03cd58211da100372d0e584a9ed87429171b85e32be8c1f596
BLAKE2b-256 checksum
How to use checksums
91ea0620df0605084ae9289d231b91e4f3399bf496113d5a95ef7f70597f4dea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.0 Linux/6.8.0-1040-aws

Release history Release notifications | RSS feed

This release

1.6.0 This release

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page