Skip to content

PGActivity Model

The foundation of django-pgactivity is the pgactivity.models.PGActivity model. It's a wrapper around the pg_stat_activity Postgres view with additional goodies.

Query all current activity like so:

from pgactivity.models import PGActivity

PGActivity.objects.all()

Here are some of the fields that can be queried:

See pgactivity.models.PGActivity for a list of every field, which also includes transaction and client information.

There are some special queryset methods worth noting:

  • PGActivity.objects.pid(pid1, pid2): Filter based on the process ID. Although it's possible to filter on id directly, using the pid method results in a more efficient query.
  • PGActivity.objects.filter(...).cancel(): Cancels all matching queries using pg_cancel_backend.
  • PGActivity.objects.filter(...).terminate(): Terminates all matching queries using pg_terminate_backend.

When querying the SQL, remember that it's truncated to 1024 characters by default and can only be changed by adjusting the global track_activities_query_size Postgres setting. In order to better understand where queries originate, see the context section.