Skip to main content

Monitor, kill, and analyze Postgres queries.

Project description

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 pg_stats_activity table.

  • pgactivity.context and pgactivity.middleware.ActivityMiddleware for annotating queries with application metadata, such as the request URL.

  • pgactivity.cancel and pgactivity.terminate for cancelling and terminating queries. The PGActivity model manager also has these methods.

  • pgactivity.timeout for dynamically setting the statement timeout.

Quick Start

Use the pgactivity ls subcommand to see activity queries:

python manage.py pgactivity ls

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 columns are as follows:

  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.

Cancel activity with:

python manage.py pgactivity cancel <process id> <process id> ...

Idle operations such as the first cannot always be cancelled. Terminate the connection with:

python manage.py pgactivity terminate <process id> <process id> ...

Decorate your code with pgactivity.context to attach context to SQL statements. Install pgactivity.middleware.ActivityMiddleware to automatically add the URL and request method to every query. Then you will see values in the context column:

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

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

import pgactivity

@pgactivity.timeout(pgactivity.timedelta(milliseconds=500))
def my_operation():
    # Any queries in this operation that take over 500 milliseconds will throw
    # an exception

Compatibility

django-pgactivity is compatible with Python 3.7 - 3.10, Django 2.2 - 4.1, and Postgres 10 - 15.

Documentation

View the django-pgactivity docs here for more examples of the management command, configuration options, context tracking, and the proxy model.

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.rst.

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.0.0.tar.gz (11.4 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.0.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file django-pgactivity-1.0.0.tar.gz.

File metadata

  • Download URL: django-pgactivity-1.0.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.10.6 Linux/5.15.0-1021-aws

File hashes

Hashes for django-pgactivity-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f2c4c9b701eb341b068f106df27538bec3a4ef12f9f4d7de13389c739a66b1d1
MD5 db50e0d3fced3149d7b4956b354ee66e
BLAKE2b-256 4d78378522493eb008bdb0b3069281af17728fd34a220d0bfc385ccb5aaa4d90

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_pgactivity-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.10.6 Linux/5.15.0-1021-aws

File hashes

Hashes for django_pgactivity-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bf6a720c4a2949f3fdd0689768a26b47690346f1fdab2803108d999760c76fe
MD5 864fa0d3576255f313a27a433ca88cd0
BLAKE2b-256 12f82f2ed73cd21f7986dd5bf63697af37644b65954174be4c16ca35f3ea32a0

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