{"id":3969,"library":"django-pgactivity","title":"Django Pgactivity","description":"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.","status":"active","version":"1.8.0","language":"en","source_language":"en","source_url":"https://github.com/AmbitionEng/django-pgactivity","tags":["django","postgresql","monitoring","database","query","performance","activity","admin"],"install":[{"cmd":"pip install django-pgactivity","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core framework dependency; compatible with Django 4.2 - 6.0.","package":"Django","optional":false},{"reason":"PostgreSQL adapter for Python; compatible with Psycopg 2 - 3. `psycopg2-binary` is commonly used.","package":"psycopg","optional":false},{"reason":"Runtime environment; compatible with Python 3.10 - 3.14.","package":"Python","optional":false},{"reason":"Database server; compatible with PostgreSQL 14 - 18.","package":"PostgreSQL","optional":false}],"imports":[{"note":"Used to query the Postgres pg_stat_activity view as a Django model.","symbol":"PGActivity","correct":"from pgactivity.models import PGActivity"},{"note":"Decorator/context manager for annotating SQL queries with application metadata.","symbol":"pgactivity.context","correct":"import pgactivity\n# then use @pgactivity.context(...) or with pgactivity.context(...)"},{"note":"Decorator for dynamically setting the SQL statement timeout for a block of code.","symbol":"pgactivity.timeout","correct":"import pgactivity\n# then use @pgactivity.timeout(...)"},{"note":"Middleware to automatically attach request URL and method as context to queries.","symbol":"ActivityMiddleware","correct":"from pgactivity.middleware import ActivityMiddleware"}],"quickstart":{"code":"# settings.py\nINSTALLED_APPS = [\n    # ...\n    'pgactivity',\n    # ...\n]\n\n# myapp/views.py (example using timeout decorator)\nimport pgactivity\nfrom django.db import connection, utils\nfrom django.http import HttpResponse\n\n@pgactivity.timeout(0.5) # Set a 500ms timeout for queries in this view\ndef my_slow_view(request):\n    try:\n        with connection.cursor() as cursor:\n            cursor.execute(\"SELECT pg_sleep(1)\") # This will likely time out\n        return HttpResponse(\"Operation completed within timeout.\")\n    except utils.OperationalError as e:\n        return HttpResponse(f\"Operation timed out or failed: {e}\", status=500)\n\n# To view active queries from the command line:\n# python manage.py pgactivity","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python to 3.10 or higher. For example, `pyenv install 3.10.13 && pyenv local 3.10.13`.","message":"Version 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.","severity":"breaking","affected_versions":">=1.8.0"},{"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.","message":"Earlier 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+).","severity":"breaking","affected_versions":">=1.5.0, >=1.7.0"},{"fix":"Add `'pgactivity'` to the `INSTALLED_APPS` list in your Django `settings.py` file.","message":"Forgetting 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.","severity":"gotcha","affected_versions":"*"},{"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.","message":"Effective 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.","severity":"gotcha","affected_versions":"*"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}