Skip to main content

django-pgactivity

django-pgactivity makes it easy to view, filter, and kill active Postgres queries.

Some of the features at a glance:

  • The PGActivity proxy model and pgactivity management command for querying and filtering the Postgres pg_stat_activity view.
  • pgactivity.context and pgactivity.middleware.ActivityMiddleware for annotating queries with application metadata, such as the request URL.
  • pgactivity.cancel and pgactivity.terminate for canceling and terminating queries. The PGActivity model manager also has these methods.
  • pgactivity.timeout for dynamically setting the statement timeout.

Quick Start

Basic Command Usage

Use python manage.py pgactivity to view and filter active queries. Output looks like the following:

39225 | 0:01:32 | IDLE_IN_TRANSACTION | None | lock auth_user in access exclusiv
39299 | 0:00:15 | ACTIVE | None | SELECT "auth_user"."id", "auth_user"."password
39315 | 0:00:00 | ACTIVE | None | WITH _pgactivity_activity_cte AS ( SELECT pid

The default output attributes are:

  1. The process ID of the connection.
  2. The duration of the query.
  3. The state of the query (see the Postgres docs for values).
  4. Attached context using pgactivity.context.
  5. The query SQL.

Apply filters with -f (or --filter). Here we query for all active queries that have a duration longer than a minute:

python manage.py pgactivity -f state=ACTIVE -f 'duration__gt=1 minute'

Cancel or terminate activity with --cancel or --terminate. Here we terminate a query based on the process ID:

python manage.py pgactivity 39225 --terminate

Attaching Context

You can attach context to queries to better understand where they originate using pgactivity.context or by adding pgactivity.middleware.ActivityMiddleware to settings.MIDDLEWARE. Underneath the hood, a comment is added to the SQL statement and surfaced in django-pgactivity.

When using the middleware, the url of the request and the method of the request are automatically added. Here's what the output looks like when using the pgactivity command:

39299 | 0:00:15 | ACTIVE | {"url": "/admin/", "method": "GET"} | SELECT "auth_use

Proxy Model

Use the pgactivity.models.PGActivity proxy model to query the Postgres pg_stat_activity view. The model contains most of the fields from the view, and the cancel and terminate methods can be applied to the queryset.

Setting the Statement Timeout

Dynamically set the SQL statement timeout of code using `pgactivity.timeout``:

import pgactivity

@pgactivity.timeout(0.5)
def my_operation():
    # Any queries in this operation that take over 500 milliseconds will throw
    # django.db.utils.OperationalError.

Compatibility

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

Documentation

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

  • The proxy models and custom queryset methods.
  • Attaching application context to queries.
  • Using and configuring the management command.
  • Setting dynamic statement timeouts.

Installation

Install django-pgactivity with:

pip3 install django-pgactivity

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

Contributing Guide

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

Creators

Metadata

Release files for django-pgactivity 1.8.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-pgactivity 1.8.0
File Size Uploaded
django_pgactivity-1.8.0.tar.gz 12.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-pgactivity 1.8.0
File Interpreter ABI Platform
django_pgactivity-1.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.2 kB

Release files / django_pgactivity-1.8.0.tar.gz

Download URL django_pgactivity-1.8.0.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0988995caca9d5b1e7b60c0f335135cd85cb0625cddb1b95ce71bd73d6752d22
BLAKE2b-256 checksum
How to use checksums
842bf8f91d90e29e8969a61b4267068b1566e6ec33a9cc83621721a45b9e6e6a
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_pgactivity-1.8.0-py3-none-any.whl

Download URL django_pgactivity-1.8.0-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b17f5d570266d26b3f8797379aeb721c95f60f999629a0648558374f0579eda
BLAKE2b-256 checksum
How to use checksums
89b1966b4717bbcd002d0c6b90ba3af0361aee4b3d9b75246cb99d372ea42343
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.8.0 This release

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

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