Files
genai-toolbox/docs/en/resources/tools/postgres/postgres-list-locks.md
Siddharth Ravi 5abad5d56c feat(tools/postgres): add long_running_transactions, list_locks and replication_stats tools (#1751)
Adds the following tools for Postgre
(1) long_running_transactions - reports transactions that exceed a
configured duration threshold.
(2) list_locks - lists active locks in the database, including the
associated process, lock type, relation, mode, and the query holding or
waiting on the lock.
(3) replication_stats - reports replication-related metrics for WAL
streaming replicas, including lag sizes presented in human-readable
form.

<img width="3020" height="1420" alt="image"
src="https://github.com/user-attachments/assets/e7d70063-b90c-4464-90ec-1bd810c02cac"
/>

<img width="3036" height="1374" alt="image"
src="https://github.com/user-attachments/assets/f7cf584b-ae01-455c-9c9c-acca3166a549"
/>

<img width="2682" height="868" alt="image"
src="https://github.com/user-attachments/assets/dd10646c-4521-4d8f-a111-760d6eb01249"
/>


## PR Checklist
- [Y] Make sure you reviewed

[CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md)
- [Y] Make sure to open an issue as a

[bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose)
  before writing your code! That way we can discuss the change, evaluate
  designs, and agree on the general idea
- [Y] Ensure the tests and linter pass
- [Y] Code coverage does not decrease (if any source code was changed)
- [Y] Appropriate docs were updated (if necessary)
- [Y] Make sure to add `!` if this involve a breaking change

🛠️ Fixes #1691 #1715

---------

Co-authored-by: Wenxin Du <117315983+duwenxin99@users.noreply.github.com>
2025-11-18 19:23:10 +00:00

2.7 KiB

title, type, weight, description, aliases
title type weight description aliases
postgres-list-locks docs 1 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.
/resources/tools/postgres-list-locks

About

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.

Compatible sources:

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.

Query

The tool aggregates locks per backend (process) and returns the concatenated transaction ids and lock entries. The SQL used by the tool looks like:

SELECT
    locked.pid,
    locked.usename,
    locked.query,
    string_agg(locked.transactionid::text,':') as trxid,
    string_agg(locked.lockinfo,'||') as locks
FROM
    (SELECT
      a.pid,
      a.usename,
      a.query,
      l.transactionid,
      (l.granted::text||','||coalesce(l.relation::regclass,0)::text||','||l.mode::text)::text as lockinfo
    FROM
      pg_stat_activity a
      JOIN pg_locks l ON l.pid = a.pid  AND a.pid != pg_backend_pid()) as locked
GROUP BY 
    locked.pid, locked.usename, locked.query;

Example

tools:
  list_locks:
    kind: postgres-list-locks
    source: postgres-source
    description: "Lists active locks with associated process and query information."

Example response element (aggregated per process):

{
  "pid": 23456,
  "usename": "dbuser",
  "query": "INSERT INTO orders (...) VALUES (...);",
  "trxid": "12345:0",
  "locks": "true,public.orders,RowExclusiveLock||false,0,ShareUpdateExclusiveLock"
}

Reference

field type required description
pid integer true Process id (backend pid).
usename string true Database user.
query string true SQL text associated with the session.
trxid string true Aggregated transaction ids for the process, joined by ':' (string). Each element is the transactionid as text.
locks string true Aggregated lock info entries for the process, joined by '