currentActivity

Return current server connections and running queries (pg_stat_activity) across all databases: pid, database, user, application_name, backend_type, state, wait event, query text, transaction and query duration, leader_pid for parallel workers, and the backend's xid and xmin.

read-onlyany connectionsweeps replication groupsweeps groupcan return data values

Synopsis

currentActivity([min_duration_s], [pid], [query_id], [state])

Description

Return current server connections and running queries (pg_stat_activity) across all databases: pid, database, user, application_name, backend_type, state, wait event, query text, transaction and query duration, leader_pid for parallel workers, and the backend's xid and xmin. query_id is returned as a decimal string and is the join key to statementStats and explainQuery, so a statement seen running here can be looked up and planned.

All filters are optional and combine; with none the whole view is returned, which on a busy server is mostly idle connections and internal processes.

This operation can return values that came from your data. It never changes anything and never returns rows, but read what reaches the caller before connecting it to a database whose contents are sensitive.

Parameters

min_duration_s optionalnumber
only backends whose current query has been running at least this many seconds. Plain idle backends are excluded, since their query_start dates a statement that already finished
pid optionalinteger
a single backend, together with its parallel workers (any backend whose leader_pid is this pid)
query_id optionalstring
only backends running this query_id, as a decimal string; use it to find who is running a statement identified by statementStats. Requires compute_query_id to be enabled (the default 'auto' enables it when pg_stat_statements is loaded)
state optionalstring
only backends in this pg_stat_activity state, e.g. "active" or "idle in transaction"

Also accepts connection, replication_group, group, role, described once under arguments every tool takes.

Output

Keyed by backend pid; each value is that backend's state, query and wait event. An absent extension or a missing grant is reported as {error, hint} in place of the map.

Scope

This reading is instance-wide -- every database on the same postmaster returns it identically, so asking each of them in turn repeats one answer. The counters here are each server's own, so members of a replication group legitimately disagree and the answer is their sum, not the primary's copy.

Example mocked data

Request

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "currentActivity",
    "arguments": {
      "min_duration_s": 5
    }
  }
}

Result

{
  "51877": {
    "database": "shop",
    "user": "app_rw",
    "application_name": "checkout-api",
    "client_addr": "10.0.4.17",
    "backend_type": "client backend",
    "state": "active",
    "wait_event_type": "Lock",
    "wait_event": "transactionid",
    "query_duration_s": 12.402,
    "xact_duration_s": 12.44,
    "query_id": "-6203381990422871044",
    "query": "UPDATE shop.orders SET status = $1 WHERE id = $2"
  }
}

Invented values on a fictional shop database, shaped by and checked against this tool's output schema. Real output is returned as structuredContent to clients that negotiate MCP 2025-06-18 or later.

See also

diskUsage, databaseSize, serverSettings, currentLocks, databaseStats, statementStats, wraparoundStatus, progressStats, ioStats, checkpointStats, tableIOStats, hostCapacity, bufferCacheSummary, bufferCacheContents