Registry / database / django-pgactivity

django-pgactivity

JSON →
library1.8.0pypypi✓ verified 24d ago

django-pgactivity is a Python library that provides tools to monitor, kill, and analyze active PostgreSQL queries directly from a Django application. It offers a proxy model for the `pg_stat_activity` view, a management command, and utilities like query context annotation and dynamic statement timeouts. The current version is 1.8.0, and it maintains an active release cadence, frequently updating to support new versions of Python, Django, and PostgreSQL.

pip install django-pgactivity
INSTALL
IMPORT
SIG · DJANGO-PGACTIVITY
D
django-pgactivity
databasepythonv1.8.0
Install
3.5s avg
Import
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 66.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.5s · import 0.000s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PGActivity
from pgactivity import PGActivity
from pgactivity import PGActivity

To get started, add 'pgactivity' to your Django project's `INSTALLED_APPS`. You can then use the `pgactivity` management command to view and filter active PostgreSQL queries, or integrate its features programmatically. The example demonstrates using the `@pgactivity.timeout` decorator to enforce a statement timeout on a view function. Make sure your Django project is correctly configured to use PostgreSQL as its database backend.

# settings.py INSTALLED_APPS = [ # ... 'pgactivity', # ... ] # myapp/views.py (example using timeout decorator) import pgactivity from django.db import connection, utils from django.http import HttpResponse @pgactivity.timeout(0.5) # Set a 500ms timeout for queries in this view def my_slow_view(request): try: with connection.cursor() as cursor: cursor.execute("SELECT pg_sleep(1)") # This will likely time out return HttpResponse("Operation completed within timeout.") except utils.OperationalError as e: return HttpResponse(f"Operation timed out or failed: {e}", status=500) # To view active queries from the command line: # python manage.py pgactivity
manage.py --version
Debug
Known issues
breakingVersion 1.8.0 dropped support for Python 3.9. Users on Python 3.9 must upgrade their Python version to 3.10 or later before upgrading to django-pgactivity 1.8.0.
fix
Upgrade Python to 3.10 or higher. For example, `pyenv install 3.10.13 && pyenv local 3.10.13`.
affects: >=1.8.0
breakingEarlier versions dropped support for older environments: 1.7.0 dropped Python 3.8 support, and 1.5.0 dropped Django 3.2 and Postgres 12 support. Ensure your environment meets the compatibility requirements (Python 3.10+, Django 4.2+, Postgres 14+).
fix
Review the compatibility matrix (Python 3.10-3.14, Django 4.2-6.0, Psycopg 2-3, Postgres 14-18) and upgrade your dependencies accordingly.
affects: >=1.5.0, >=1.7.0
gotchaForgetting to add 'pgactivity' to `INSTALLED_APPS` in your Django `settings.py` will result in `AppRegistryNotReady` or `ModuleNotFoundError` errors when trying to use its features like the `PGActivity` model or management commands.
fix
Add `'pgactivity'` to the `INSTALLED_APPS` list in your Django `settings.py` file.
affects: *
gotchaEffective use of `django-pgactivity` requires a correctly configured PostgreSQL database in Django's `settings.DATABASES`. Incorrect `ENGINE`, `NAME`, `USER`, `PASSWORD`, `HOST`, or `PORT` will prevent the library from connecting to and monitoring your database.
fix
Double-check your `DATABASES` configuration in `settings.py` to ensure all PostgreSQL connection details are accurate and the Django application can successfully connect to your PostgreSQL instance.
affects: *
gotchaThe `pg_stat_activity` view (which `django-pgactivity` queries) provides extensive information about active queries, but it does not directly expose memory usage statistics. For memory monitoring, you would need to use operating-system level facilities or more specialized PostgreSQL monitoring tools.
fix
Be aware of the data available through `pg_stat_activity` and integrate with other monitoring solutions if detailed memory usage per query is required.
affects: *
Upgrade
Version history
1.8.0latest on PyPI · released Nov 30, 2025
Audit
Dependencies
DjangorequiredCore framework dependency; compatible with Django 4.2 - 6.0.
psycopgrequiredPostgreSQL adapter for Python; compatible with Psycopg 2 - 3. `psycopg2-binary` is commonly used.
PythonrequiredRuntime environment; compatible with Python 3.10 - 3.14.
PostgreSQLrequiredDatabase server; compatible with PostgreSQL 14 - 18.
Agent activity
31 hits · last 30 days
node
28
OpenAI (training)
2
Resources