# 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``:
```python
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.9 - 3.13, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 17.
## 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](CONTRIBUTING.md).
## Creators
- [Wes Kendall](https://github.com/wesleykendall)
- [Paul Gilmartin](https://github.com/PaulGilmartin)
Raw data
{
"_id": null,
"home_page": "https://github.com/AmbitionEng/django-pgactivity",
"name": "django-pgactivity",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.9.0",
"maintainer_email": null,
"keywords": null,
"author": "Wes Kendall",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/42/24/87ef93b1f4df5daa025739aba69b83a53814405dcb1a45e853177447b965/django_pgactivity-1.7.1.tar.gz",
"platform": null,
"description": "# django-pgactivity\n\n`django-pgactivity` makes it easy to view, filter, and kill active Postgres queries.\n\nSome of the features at a glance:\n\n* 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).\n* `pgactivity.context` and `pgactivity.middleware.ActivityMiddleware` for annotating queries with application metadata, such as the request URL.\n* `pgactivity.cancel` and `pgactivity.terminate` for canceling and terminating queries. The `PGActivity` model manager also has these methods.\n* `pgactivity.timeout` for dynamically setting the statement timeout.\n\n## Quick Start\n\n### Basic Command Usage\n\nUse `python manage.py pgactivity` to view and filter active queries. Output looks like the following:\n\n 39225 | 0:01:32 | IDLE_IN_TRANSACTION | None | lock auth_user in access exclusiv\n 39299 | 0:00:15 | ACTIVE | None | SELECT \"auth_user\".\"id\", \"auth_user\".\"password\n 39315 | 0:00:00 | ACTIVE | None | WITH _pgactivity_activity_cte AS ( SELECT pid\n\nThe default output attributes are:\n\n1. The process ID of the connection.\n2. The duration of the query.\n3. 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).\n4. Attached context using `pgactivity.context`.\n5. The query SQL.\n\nApply filters with `-f` (or `--filter`). Here we query for all active queries that have a duration longer than a minute:\n\n python manage.py pgactivity -f state=ACTIVE -f 'duration__gt=1 minute'\n\nCancel or terminate activity with `--cancel` or `--terminate`. Here we terminate a query based on the process ID:\n\n python manage.py pgactivity 39225 --terminate\n\n### Attaching Context\n\nYou 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`.\n\nWhen 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:\n\n 39299 | 0:00:15 | ACTIVE | {\"url\": \"/admin/\", \"method\": \"GET\"} | SELECT \"auth_use\n\n### Proxy Model\n\nUse 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.\n\n## Setting the Statement Timeout\n\nDynamically set the SQL statement timeout of code using `pgactivity.timeout``:\n\n```python\nimport pgactivity\n\n@pgactivity.timeout(0.5)\ndef my_operation():\n # Any queries in this operation that take over 500 milliseconds will throw\n # django.db.utils.OperationalError.\n```\n\n## Compatibility\n\n`django-pgactivity` is compatible with Python 3.9 - 3.13, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 17.\n\n## Documentation\n\n[View the django-pgactivity docs here](https://django-pgactivity.readthedocs.io) to learn more about:\n\n* The proxy models and custom queryset methods.\n* Attaching application context to queries.\n* Using and configuring the management command.\n* Setting dynamic statement timeouts.\n\n## Installation\n\nInstall `django-pgactivity` with:\n\n pip3 install django-pgactivity\nAfter this, add `pgactivity` to the `INSTALLED_APPS` setting of your Django project.\n\n## Contributing Guide\n\nFor information on setting up django-pgactivity for development and contributing changes, view [CONTRIBUTING.md](CONTRIBUTING.md).\n\n## Creators\n\n- [Wes Kendall](https://github.com/wesleykendall)\n- [Paul Gilmartin](https://github.com/PaulGilmartin)\n",
"bugtrack_url": null,
"license": "BSD-3-Clause",
"summary": "Monitor, kill, and analyze Postgres queries.",
"version": "1.7.1",
"project_urls": {
"Documentation": "https://django-pgactivity.readthedocs.io",
"Homepage": "https://github.com/AmbitionEng/django-pgactivity",
"Repository": "https://github.com/AmbitionEng/django-pgactivity"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c3c29de50e5ed5e09cd7eb64e99f743087d4f6839917d7e69198860fc7442a58",
"md5": "db580290718446b87d85b96f93478924",
"sha256": "0cd9e8dbaab7997410bd0fe490417d63f568fa27fd02e548bb3ea8dc73e0e319"
},
"downloads": -1,
"filename": "django_pgactivity-1.7.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "db580290718446b87d85b96f93478924",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.9.0",
"size": 14422,
"upload_time": "2024-12-16T02:01:42",
"upload_time_iso_8601": "2024-12-16T02:01:42.637103Z",
"url": "https://files.pythonhosted.org/packages/c3/c2/9de50e5ed5e09cd7eb64e99f743087d4f6839917d7e69198860fc7442a58/django_pgactivity-1.7.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "422487ef93b1f4df5daa025739aba69b83a53814405dcb1a45e853177447b965",
"md5": "4cc1405fcd428eca054e0e984e43e85b",
"sha256": "4d8aad75c2d48e1e79f39d9163b5f134f5867f65c75f08c37fa745d5e8f3d6fa"
},
"downloads": -1,
"filename": "django_pgactivity-1.7.1.tar.gz",
"has_sig": false,
"md5_digest": "4cc1405fcd428eca054e0e984e43e85b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.9.0",
"size": 13080,
"upload_time": "2024-12-16T02:01:44",
"upload_time_iso_8601": "2024-12-16T02:01:44.621245Z",
"url": "https://files.pythonhosted.org/packages/42/24/87ef93b1f4df5daa025739aba69b83a53814405dcb1a45e853177447b965/django_pgactivity-1.7.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-16 02:01:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "AmbitionEng",
"github_project": "django-pgactivity",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"circle": true,
"tox": true,
"lcname": "django-pgactivity"
}