---
title: "django-pgactivity | pypx"
description: "Monitor, kill, and analyze Postgres queries."
canonical_url: "https://pypx.app/packages/django-pgactivity"
meta:
  "og:description": "Monitor, kill, and analyze Postgres queries."
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-pgactivity%2Cversion_1.8.0%2Csummary_~TW9uaXRvciwga2lsbCwgYW5kIGFuYWx5emUgUG9zdGdyZXMgcXVlcmllcy4%2Clicense_BSD%2BLicense%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tcGdhY3Rpdml0eSI%2Cs_YQ3twpaMDaypv8gT.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "django-pgactivity 1.8.0"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-pgactivity%2Cversion_1.8.0%2Csummary_~TW9uaXRvciwga2lsbCwgYW5kIGFuYWx5emUgUG9zdGdyZXMgcXVlcmllcy4%2Clicense_BSD%2BLicense%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tcGdhY3Rpdml0eSI%2Cs_YQ3twpaMDaypv8gT.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **django-pgactivity**

v1.8.0

Monitor, kill, and analyze Postgres queries.

14 KB Python 3.10.0+ py3-none-anyBSD License1 deps

[** Diff **](https://pypx.app/packages/django-pgactivity/diff)

`$ uv add django-pgactivity`

# django-pgactivity

`django-pgactivity` makes it easy to view, filter, and kill active Postgres queries.

Some of the features at a glance:

- The `PGActivity` proxy model and `pgactivity` management command for querying and filtering the [Postgres pg\_stat\_activity view](https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-ACTIVITY-VIEW).
- `pgactivity.context` and `pgactivity.middleware.ActivityMiddleware` for annotating queries with application metadata, such as the request URL.
- `pgactivity.cancel` and `pgactivity.terminate` for canceling and terminating queries. The `PGActivity` model manager also has these methods.
- `pgactivity.timeout` for 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:

1. The process ID of the connection.
2. The duration of the query.
3. The state of the query (see the [Postgres docs](https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-ACTIVITY-VIEW) for values).
4. Attached context using `pgactivity.context`.
5. 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 `django-pgactivity`.

When using the middleware, the `url` of the request and the `method` of the request are automatically added. Here's what the output looks like when using the `pgactivity` command:

```
39299 | 0:00:15 | ACTIVE | {"url": "/admin/", "method": "GET"} | SELECT "auth_use
```

### Proxy Model

Use the `pgactivity.models.PGActivity` proxy model to query the [Postgres pg\_stat\_activity view](https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-ACTIVITY-VIEW). The model contains most of the fields from the view, and the `cancel` and `terminate` methods can be applied to the queryset.

## Setting the Statement Timeout

Dynamically set the SQL statement timeout of code using \`pgactivity.timeout\`\`:

```
import pgactivity

@pgactivity.timeout(0.5)
def my_operation():
    # Any queries in this operation that take over 500 milliseconds will throw
    # django.db.utils.OperationalError.
```

## Compatibility

`django-pgactivity` is compatible with Python 3.10 - 3.14, Django 4.2 - 6.0, Psycopg 2 - 3, and Postgres 14 - 18.

## Documentation

[View the django-pgactivity docs here](https://django-pgactivity.readthedocs.io) to 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.

## Installation

Install `django-pgactivity` with:

```
pip3 install django-pgactivity
```

After this, add `pgactivity` to the `INSTALLED_APPS` setting of your Django project.

## Contributing Guide

For information on setting up django-pgactivity for development and contributing changes, view [CONTRIBUTING.md](https://pypx.app/CONTRIBUTING.md).

## Creators

- [Wes Kendall](https://github.com/wesleykendall)
- [Paul Gilmartin](https://github.com/PaulGilmartin)

## **Details **

<dl>

<dt>Version</dt>
<dd>1.8.0</dd>

<dt>License</dt>
<dd>BSD License</dd>

<dt>Python</dt>
<dd>&lt;4,&gt;=3.10.0</dd>

<dt>Maintainer</dt>
<dd>Wes Kendall</dd></dl>

## **Links**

- [Documentation](https://django-pgactivity.readthedocs.io)
- [Homepage](https://github.com/AmbitionEng/django-pgactivity)
- [Repository](https://github.com/AmbitionEng/django-pgactivity)
- [View on PyPI](https://pypi.org/project/django-pgactivity/)

## **Release Cadence **

**1**

releases in the past year

avg 95 days between releases

## **Maintainers **

- Wes Kendall

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/django-pgactivity#webpage","@type":"WebPage","description":"Monitor, kill, and analyze Postgres queries.","name":"django-pgactivity","url":"https://pypx.app/packages/django-pgactivity","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/django-pgactivity"]}]},{"@id":"https://pypx.app/packages/django-pgactivity#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Monitor, kill, and analyze Postgres queries.","license":"BSD License","name":"django-pgactivity","softwareVersion":"1.8.0"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
