Monitor, kill, and analyze Postgres queries.
Low maintenance activity. Last release 12 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install django-pgactivitydjango-pgactivity
django-pgactivity makes it easy to view, filter, and kill active Postgres queries.
Some of the features at a glance:
- The
PGActivityproxy model andpgactivitymanagement command for querying and filtering the Postgres 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 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:
- The process ID of the connection.
- The duration of the query.
- The state of the query (see the Postgres docs for values).
- Attached context using
pgactivity.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 --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 .