Module
pgactivity
pgactivity.context
Bases: ContextDecorator
A context manager that adds additional metadata to SQL statements.
Once any code has entered pgactivity.context
, all subsequent
entrances of pgactivity.context
will be overwrite keys.
To add context only if a parent has already entered pgactivity.context
,
one can call pgactivity.context
as a function without entering it.
The metadata set in the function call will be part of the context if
pgactivity.context
has previously been entered. Otherwise it will
be ignored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metadata |
dict
|
Metadata that should be attached to the activity context |
{}
|
Example
Here we track a "key" with a value of "value"::
with pgactivity.context(key='value'):
# Do things..
# All SQL will have a {'key': 'value'} metadata comment.
# Nesting will add additional metadata to the current
# context
# Add metadata if a parent piece of code has already entered
# pgactivity.context
pgactivity.context(key='value')
Source code in pgactivity/runtime.py
pgactivity.cancel
pgactivity.pid
pgactivity.terminate
Terminate activity using the Postgres pg_teminate_backend
function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*pids |
int
|
The process ID(s) to terminate. |
()
|
using |
str
|
The database to use. |
DEFAULT_DB_ALIAS
|
Returns:
Type | Description |
---|---|
List[int]
|
Terminated process IDs |
Source code in pgactivity/core.py
pgactivity.timeout
timeout(
timeout: Union[dt.timedelta, int, float, None] = _unset,
*,
using: str = DEFAULT_DB_ALIAS,
**timedelta_kwargs: int
)
Set the statement timeout as a decorator or context manager.
A value of None
will set an infinite statement timeout.
A value of less than a millisecond is not permitted.
Nested invocations will successfully apply and rollback the timeout to the previous value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout |
Union[timedelta, int, float, None]
|
The number of seconds as an integer or float. Use a timedelta
object to precisely specify the timeout interval. Use |
_unset
|
using |
str
|
The database to use. |
DEFAULT_DB_ALIAS
|
**timedelta_kwargs |
int
|
Keyword arguments to directly supply to
datetime.timedelta to create an interval. E.g.
|
{}
|
Raises:
Type | Description |
---|---|
OperationalError
|
When a timeout occurs |
TypeError
|
When the timeout interval is an incorrect type |
Source code in pgactivity/core.py
pgactivity.contrib
pgactivity.contrib.execute_from_command_line
execute_from_command_line(
*args: Any,
ignore_commands: Union[List[str], None] = None,
exec_func: Union[Callable, None] = None,
**kwargs: Any
)
A drop-in replacement for Django's execute_from_command_line
that attaches
the command name as context. Can be used in a manage.py file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Any
|
Args for |
()
|
ignore_commands |
Union[List[str], None]
|
Command names that should be ignored. Both "runserver" and "runserver_plus" are always added to this value. |
None
|
exec_func |
Union[Callable, None]
|
the function to executed. Defaults to Django's
|
None
|
**kwargs |
Any
|
Kwargs for |
{}
|
Source code in pgactivity/contrib.py
pgactivity.middleware
pgactivity.middleware.ActivityMiddleware
Annotates the url/method in the pgactivity context.
Source code in pgactivity/middleware.py
pgactivity.models
pgactivity.models.JSONField
Bases: JSONField
A JSONField that has a stable import path.
Useful for migrations since the JSONField path changed in a Django upgrade.
pgactivity.models.NoObjectsManager
Bases: Manager
Django's dumpdata and other commands will try to dump PG models. This manager is set as the default manager on PG models to prevent that.
pgactivity.models.PGActivity
Bases: PGTable
Wraps Postgres's pg_stat_activity
view.
Attributes:
Name | Type | Description |
---|---|---|
start |
DateTimeField
|
The start of the query. |
duration |
DurationField
|
The duration of the query. |
query |
TextField
|
The SQL. |
context |
JSONField
|
Context tracked by |
state |
CharField
|
The state of the query. One of ACTIVE, IDLE, IDLE_IN_TRANSACTION, IDLE_IN_TRANSACTION_(ABORTED) FASTPATH_FUNCTION_CALL, or DISABLED. |
xact_start |
DateTimeField
|
Time when the current transaction was started, or null if no transaction is active. |
backend_start |
DateTimeField
|
Time when this process was started. |
state_change |
DateTimeField
|
Time when the state was last changed. |
wait_event_type |
CharField
|
Type of event for which backend is waiting or null.
|
wait_event |
CharField
|
Wait event name if backend is currently waiting.
|
backend_xid |
CharField
|
Top-level transaction identifier of this backend, if any. |
backend_xmin |
CharField
|
The current backend's xmin horizon, if any. |
backend_type |
CharField
|
One of LAUNCHER, AUTOVACUUM_WORKER, LOGICAL_REPLICATION_LAUNCHER, LOGICAL_REPLICATION_WORKER, PARALLEL_WORKER, BACKGROUND_WRITER, CLIENT_BACKEND, CHECKPOINTER, ARCHIVER, STARTUP, WALRECEIVER, WALSENDER, or WALWRITER. |
client_addr |
CharField
|
IP address of the client connected to this backend, if any. |
client_hostname |
CharField
|
Host name of the connected client, as reported by a reverse DNS lookup of client_addr. |
client_port |
IntegerField
|
TCP port number that the client is using for communication with this backend, or -1 if a Unix socket is used. |
pgactivity.models.PGActivityQuerySet
Bases: PGTableQuerySet
The Queryset for the PGActivity
model.
Source code in pgactivity/models.py
cancel
config
Use a config name from settings.PGACTIVITY_CONFIGS
to apply filters. Config overrides can be provided
in the keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the config. Must be a key from |
required |
**overrides |
Any
|
Any overrides to apply to the final config dictionary. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
dict |
QuerySet
|
The configuration |
Source code in pgactivity/models.py
pgactivity.models.PGTableQueryCompiler
Bases: SQLCompiler
as_sql
Return a CTE for the pg_stat_activity to facilitate queries
pgactivity.models.PGTableQuerySet
Bases: QuerySet
The base queryset for PG* models.
Allows for the process IDs to be set on the query compiler, making the query much more efficient.