Skip to main content

Monitor, kill, and analyze Postgres queries.

Project description

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.8 - 3.12, Django 3.2 - 5.0, Psycopg 2 - 3, and Postgres 12 - 16.

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.

Primary Authors

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_pgactivity-1.4.1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

django_pgactivity-1.4.1-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file django_pgactivity-1.4.1.tar.gz.

File metadata

  • Download URL: django_pgactivity-1.4.1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.12.0 Linux/5.15.0-1056-aws

File hashes

Hashes for django_pgactivity-1.4.1.tar.gz
Algorithm Hash digest
SHA256 00da0f0156daa37f5f113c7a6d9378a6f6d111e44f20d3b30b367d5428e18b07
MD5 46dff6161df4b779b0d33e1705e2cee0
BLAKE2b-256 9773a1e0f2c185b86febd93c5e4f0e83ca1757314e01af77035157a361462abb

See more details on using hashes here.

File details

Details for the file django_pgactivity-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: django_pgactivity-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.12.0 Linux/5.15.0-1056-aws

File hashes

Hashes for django_pgactivity-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7affa4dc08e7650092a582375729081362a3103f1148e34e8406ddf114eeb95
MD5 af0ba7375941529dfa9c102ca8ff6957
BLAKE2b-256 ab2a0f58319bd961e699d0bf1a4c9a1aa5b73e9df776955f0f633016f98af10b

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