Package: django-pgactivity
django-pgactivitydjango-pgactivitymakes it easy to view, filter, and kill active Postgres queries.Some of the features at a glance:ThePGActivityproxy model andpgactivitymanagement command for querying and filtering thePostgres pg_stat_activity view.pgactivity.contextandpgactivity.middleware.ActivityMiddlewarefor annotating queries with application metadata, such as the request URL.pgactivity.cancelandpgactivity.terminatefor canceling and terminating queries. ThePGActivitymodel manager also has these methods.pgactivity.timeoutfor dynamically setting the statement timeout.Quick StartBasic Command UsageUsepython manage.py pgactivityto 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 pidThe default output attributes are:The process ID of the connection.The duration of the query.The state of the query (see thePostgres docsfor values).Attached context usingpgactivity.context.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--cancelor--terminate. Here we terminate a query based on the process ID:python manage.py pgactivity 39225 --terminateAttaching ContextYou can attach context to queries to better understand where they originate usingpgactivity.contextor by addingpgactivity.middleware.ActivityMiddlewaretosettings.MIDDLEWARE. Underneath the hood, a comment is added to the SQL statement and surfaced indjango-pgactivity.When using the middleware, theurlof the request and themethodof the request are automatically added. Here's what the output looks like when using thepgactivitycommand:39299 | 0:00:15 | ACTIVE | {"url": "/admin/", "method": "GET"} | SELECT "auth_useProxy ModelUse thepgactivity.models.PGActivityproxy model to query thePostgres pg_stat_activity view. The model contains most of the fields from the view, and thecancelandterminatemethods can be applied to the queryset.Setting the Statement TimeoutDynamically set the SQL statement timeout of code using `pgactivity.timeout``:importpgactivity@pgactivity.timeout(0.5)defmy_operation():# Any queries in this operation that take over 500 milliseconds will throw# django.db.utils.OperationalError.Compatibilitydjango-pgactivityis compatible with Python 3.8 - 3.12, Django 3.2 - 5.0, Psycopg 2 - 3, and Postgres 12 - 16.DocumentationView the django-pgactivity docs hereto 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.InstallationInstalldjango-pgactivitywith:pip3 install django-pgactivityAfter this, addpgactivityto theINSTALLED_APPSsetting of your Django project.Contributing GuideFor information on setting up django-pgactivity for development and contributing changes, viewCONTRIBUTING.md.Primary AuthorsWes KendallPaul Gilmartin