You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The "postgres-list-locks" tool lists active locks in the database, including the associated process, lock type, relation, mode, and the query holding or waiting on the lock.
7
+
aliases:
8
+
- /resources/tools/postgres-list-locks
9
+
---
10
+
11
+
## About
12
+
13
+
The `postgres-list-locks` tool displays information about active locks by joining pg_stat_activity with pg_locks. This is useful to find transactions holding or waiting for locks and to troubleshoot contention.
This tool identifies all locks held by active processes showing the process ID, user, query text, and an aggregated list of all transactions and specific locks (relation, mode, grant status) associated with each process.
23
+
24
+
## Query
25
+
26
+
The tool aggregates locks per backend (process) and returns the concatenated transaction ids and lock entries. The SQL used by the tool looks like:
27
+
28
+
```sql
29
+
SELECT
30
+
locked.pid,
31
+
locked.usename,
32
+
locked.query,
33
+
string_agg(locked.transactionid::text,':') as trxid,
| pid | integer | true | Process id (backend pid). |
76
+
| usename | string | true | Database user. |
77
+
| query | string | true | SQL text associated with the session. |
78
+
| trxid | string | true | Aggregated transaction ids for the process, joined by ':' (string). Each element is the transactionid as text. |
79
+
| locks | string | true | Aggregated lock info entries for the process, joined by '||'. Each entry is a comma-separated triple: `granted,relation,mode` where `relation` may be `0` when not resolvable via regclass. |
The postgres-long-running-transactions tool Identifies and lists database transactions that exceed a specified time limit. For each of the long running transactions, the output contains the process id, database name, user name, application name, client address, state, connection age, transaction age, query age, last activity age, wait event type, wait event, and query string.
The `postgres-long-running-transactions` tool reports transactions that exceed a configured duration threshold by scanning `pg_stat_activity` for sessions where `xact_start` is set and older than the configured interval.
The tool returns a JSON array with one object per matching session (non-idle). Each object contains the process id, database and user, application name, client address, session state, several age intervals (connection, transaction, query, and last activity), wait event info, and the SQL text currently associated with the session.
22
+
23
+
Parameters:
24
+
25
+
-`min_duration` (optional): Only show transactions running at least this long (Postgres interval format, e.g., '5 minutes'). Default: `5 minutes`.
26
+
-`limit` (optional): Maximum number of results to return. Default: `20`.
27
+
28
+
## Query
29
+
30
+
The SQL used by the tool looks like:
31
+
32
+
```sql
33
+
SELECT
34
+
pid,
35
+
datname,
36
+
usename,
37
+
application_name as appname,
38
+
client_addr,
39
+
state,
40
+
now() - backend_start as conn_age,
41
+
now() - xact_start as xact_age,
42
+
now() - query_start as query_age,
43
+
now() - state_change as last_activity_age,
44
+
wait_event_type,
45
+
wait_event,
46
+
query
47
+
FROM
48
+
pg_stat_activity
49
+
WHERE
50
+
state <>'idle'
51
+
AND (now() - xact_start) > COALESCE($1::INTERVAL, interval '5 minutes')
52
+
AND xact_start IS NOT NULL
53
+
AND pid <> pg_backend_pid()
54
+
ORDER BY
55
+
xact_age DESC
56
+
LIMIT
57
+
COALESCE($2::int, 20);
58
+
```
59
+
60
+
## Example
61
+
62
+
```yaml
63
+
tools:
64
+
long_running_transactions:
65
+
kind: postgres-long-running-transactions
66
+
source: postgres-source
67
+
description: "Identifies transactions open longer than a threshold and returns details including query text and durations."
68
+
```
69
+
70
+
Example response element:
71
+
72
+
```json
73
+
{
74
+
"pid": 12345,
75
+
"datname": "my_database",
76
+
"usename": "dbuser",
77
+
"appname": "my_app",
78
+
"client_addr": "10.0.0.5",
79
+
"state": "idle in transaction",
80
+
"conn_age": "00:12:34",
81
+
"xact_age": "00:06:00",
82
+
"query_age": "00:02:00",
83
+
"last_activity_age": "00:01:30",
84
+
"wait_event_type": null,
85
+
"wait_event": null,
86
+
"query": "UPDATE users SET last_seen = now() WHERE id = 42;"
The "postgres-replication-stats" tool reports replication-related metrics for WAL streaming replicas, including lag sizes presented in human-readable form.
7
+
aliases:
8
+
- /resources/tools/postgres-replication-stats
9
+
---
10
+
11
+
## About
12
+
13
+
The `postgres-replication-stats` tool queries pg_stat_replication to surface the status of connected replicas. It reports application_name, client address, connection and sync state, and human-readable lag sizes (sent, write, flush, replay, and total) computed using WAL LSN differences.
This tool takes no parameters. It returns a JSON array; each element represents a replication connection on the primary and includes lag metrics formatted by pg_size_pretty.
22
+
23
+
## Example
24
+
25
+
```yaml
26
+
tools:
27
+
replication_stats:
28
+
kind: postgres-replication-stats
29
+
source: postgres-source
30
+
description: "Lists replication connections and readable WAL lag metrics."
Copy file name to clipboardExpand all lines: internal/prebuiltconfigs/tools/cloud-sql-postgres.yaml
+18Lines changed: 18 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,21 @@ tools:
49
49
source: cloudsql-pg-source
50
50
description: "List all installed PostgreSQL extensions with their name, version, schema, owner, and description."
51
51
52
+
long_running_transactions:
53
+
kind: postgres-long-running-transactions
54
+
source: cloudsql-pg-source
55
+
description: "Identifies and lists database transactions that exceed a specified time limit. For each of the long running transactions, the output contains the process id, database name, user name, application name, client address, state, connection age, transaction age, query age, last activity age, wait event type, wait event, and query string."
56
+
57
+
list_locks:
58
+
kind: postgres-list-locks
59
+
source: cloudsql-pg-source
60
+
description: "Identifies all locks held by active processes showing the process ID, user, query text, and an aggregated list of all transactions and specific locks (relation, mode, grant status) associated with each process."
61
+
62
+
replication_stats:
63
+
kind: postgres-replication-stats
64
+
source: cloudsql-pg-source
65
+
description: "Lists each replica's process ID, user name, application name, backend_xmin (standby's xmin horizon reported by hot_standby_feedback), client IP address, connection state, and sync_state, along with lag sizes in bytes for sent_lag (primary to sent), write_lag (sent to written), flush_lag (written to flushed), replay_lag (flushed to replayed), and the overall total_lag (primary to replayed)."
0 commit comments