> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SHOW STATEMENTS

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `SHOW STATEMENTS` <InternalLink path="sql-statements">statement</InternalLink> lists details about currently active SQL queries, including:

* The internal ID of the query
* The node executing the query
* The SQL query itself
* How long the query has been running
* The client address, application name, and user that issued the query
* The ID for the current session

These details let you monitor the progress of active queries and, if necessary, identify those that may need to be <InternalLink path="cancel-query">cancelled</InternalLink> to prevent unwanted resource consumption.

<Note>
  Schema changes and <InternalLink path="backup">`BACKUP`</InternalLink>/<InternalLink path="restore">`RESTORE`</InternalLink> statements are not executed as queries internally and so are not listed by `SHOW STATEMENTS`. To monitor such statements, use <InternalLink path="show-jobs">`SHOW JOBS`</InternalLink> instead.
</Note>

## Aliases

In CockroachDB, the following are aliases for `SHOW STATEMENTS`:

* `SHOW QUERIES`

## Required privileges

All users can see their own currently active queries. Users with the <InternalLink path="security-reference/authorization#supported-privileges">`VIEWACTIVITY` or `VIEWACTIVITYREDACTED` privilege</InternalLink> can view see all users' currently active queries. `VIEWACTIVITYREDACTED` causes constants in queries being executed by other users to be redacted.

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/kYb5CU8_1XGPxWhU/images/sql-diagrams/v24.1/show_statements.svg?fit=max&auto=format&n=kYb5CU8_1XGPxWhU&q=85&s=89483f10c8a5600112dd96be66197ca7" alt="show_statements syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="539" height="81" data-path="images/sql-diagrams/v24.1/show_statements.svg" />

<img src="https://mintcdn.com/cockroachlabs/qJGq7h8L1SHY5gmZ/images/generated/docs/v24.1/show-statements/syntax-diagram-6bdd04b800b5.svg?fit=max&auto=format&n=qJGq7h8L1SHY5gmZ&q=85&s=cd28d6da06596b7e3ea08710d5017af9" alt="SQL syntax diagram" width="539" height="81" data-path="images/generated/docs/v24.1/show-statements/syntax-diagram-6bdd04b800b5.svg" />

* To list the active queries across all nodes of the cluster, use `SHOW STATEMENTS` or `SHOW CLUSTER STATEMENTS`.
* To list the active queries just on the local node, use `SHOW LOCAL STATEMENTS`.

## Response

The following fields are returned for each query:

| Field               | Description                                                                                                                                                                                                                                                    |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `query\_id`         | The ID of the query.                                                                                                                                                                                                                                           |
| `node\_id`          | The ID of the node.                                                                                                                                                                                                                                            |
| `session\_id`       | The ID of the session.                                                                                                                                                                                                                                         |
| `user\_name`        | The username of the connected user.                                                                                                                                                                                                                            |
| `start`             | The <InternalLink path="timestamp">`timestamptz`</InternalLink> at which the query started.                                                                                                                                                                    |
| `query`             | The SQL query.                                                                                                                                                                                                                                                 |
| `client\_address`   | The address and port of the client that issued the SQL query.                                                                                                                                                                                                  |
| `application\_name` | The <InternalLink path="set-vars#supported-variables">application name</InternalLink> specified by the client, if any. For queries from the <InternalLink path="cockroach-sql">built-in SQL client</InternalLink>, this will be `$ cockroach sql`.             |
| `distributed`       | If `true`, the query is being executed by the Distributed SQL (DistSQL) engine. If `false`, the query is being executed by the standard "local" SQL engine. If `NULL`, the query is being prepared and it's not yet known which execution engine will be used. |
| `phase`             | The phase of the query's execution. If `preparing`, the statement is being parsed and planned. If `executing`, the statement is being executed.                                                                                                                |

## Examples

### List queries across the cluster

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SHOW CLUSTER STATEMENTS;
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
              query_id             | node_id |            session_id            | user_name |              start               |                                 query                                 | client_address  | application_name | distributed |   phase
+----------------------------------+---------+----------------------------------+-----------+----------------------------------+-----------------------------------------------------------------------+-----------------+------------------+-------------+-----------+
  15f92b12b2fb95b00000000000000002 |       2 | 15f92b10b92ed4080000000000000002 | root      | 2020-03-04 17:48:23.309592+00:00 | SELECT city, id FROM vehicles WHERE city = $1                         | 127.0.0.1:65092 |                  |    false    | executing
  15f92b12b2ea5f700000000000000001 |       1 | 15f92adefd48d8a00000000000000001 | root      | 2020-03-04 17:48:23.3085+00:00   | SHOW CLUSTER STATEMENTS                                               | 127.0.0.1:64970 | $ cockroach sql  |    false    | executing
  15f92b12b2ffeb100000000000000003 |       3 | 15f92b0e4ea399680000000000000003 | root      | 2020-03-04 17:48:23.30989+00:00  | UPSERT INTO vehicle_location_histories VALUES ($1, $2, now(), $3, $4) | 127.0.0.1:65088 |                  |    NULL     | preparing
(3 rows)
```

Alternatively, you can use `SHOW STATEMENTS` to receive the same response.

### List queries on the local node

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SHOW LOCAL STATEMENTS;
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
              query_id             | node_id |            session_id            | user_name |              start               |                           query                            | client_address  | application_name | distributed |   phase
+----------------------------------+---------+----------------------------------+-----------+----------------------------------+------------------------------------------------------------+-----------------+------------------+-------------+-----------+
  15f92b15bece88900000000000000001 |       1 | 15f92aefb240d2980000000000000001 | root      | 2020-03-04 17:48:36.392919+00:00 | INSERT INTO user_promo_codes VALUES ($1, $2, $3, now(), 1) | 127.0.0.1:65044 |                  |    false    | executing
  15f92b15bed80a280000000000000001 |       1 | 15f92adefd48d8a00000000000000001 | root      | 2020-03-04 17:48:36.393495+00:00 | SHOW LOCAL STATEMENTS                                      | 127.0.0.1:64970 | $ cockroach sql  |    false    | executing
(2 rows)
```

### Filter for specific queries

You can use a <InternalLink path="select-clause">`SELECT`</InternalLink> statement to filter the list of active queries by one or more of the [response fields](#response).

#### Show all queries on node 2

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> WITH x as (SHOW CLUSTER STATEMENTS) SELECT * FROM x
      WHERE node_id = 2;
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
              query_id             | node_id |            session_id            | user_name |              start               |                                 query                                 | client_address  | application_name | distributed |   phase
+----------------------------------+---------+----------------------------------+-----------+----------------------------------+-----------------------------------------------------------------------+-----------------+------------------+-------------+-----------+
  15f92b1cb931f6900000000000000002 |       2 | 15f92b10b92ed4080000000000000002 | root      | 2020-03-04 17:49:06.363515+00:00 | UPSERT INTO vehicle_location_histories VALUES ($1, $2, now(), $3, $4) | 127.0.0.1:65092 |                  |    NULL     | preparing
(1 row)
```

#### Show all queries from a specific address and user

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> WITH x as (SHOW CLUSTER STATEMENTS) SELECT * FROM x
      WHERE client_address = '127.0.0.1:65196' AND user_name = 'maxroach';
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
              query_id             | node_id |            session_id            | user_name |              start               |                     query                     | client_address  | application_name | distributed |   phase
+----------------------------------+---------+----------------------------------+-----------+----------------------------------+-----------------------------------------------+-----------------+------------------+-------------+-----------+
  15f92bf4b27f0b480000000000000002 |       2 | 15f92b7dc85b7ba80000000000000002 | maxroach  | 2020-03-04 18:04:33.964083+00:00 | SELECT city, id FROM vehicles WHERE city = $1 | 127.0.0.1:65196 |                  |    false    | executing
(1 row)
```

#### Exclude queries from the built-in SQL client

To exclude queries from the <InternalLink path="cockroach-sql">built-in SQL client</InternalLink>, filter for queries that do not show `$ cockroach sql` as the `application_name`:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> WITH x as (SHOW CLUSTER STATEMENTS) SELECT * FROM x
      WHERE application_name != '$ cockroach sql';
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
              query_id             | node_id |            session_id            | user_name |              start               |                                 query                                 | client_address  | application_name | distributed |   phase
+----------------------------------+---------+----------------------------------+-----------+----------------------------------+-----------------------------------------------------------------------+-----------------+------------------+-------------+-----------+
  15f92c0dd24bec200000000000000003 |       3 | 15f92b0e4ea399680000000000000003 | root      | 2020-03-04 18:06:21.871708+00:00 | SELECT city, id FROM vehicles WHERE city = $1                         | 127.0.0.1:65088 |                  |    false    | executing
  15f92c0dd26655d80000000000000001 |       1 | 15f92be36964ac800000000000000001 | root      | 2020-03-04 18:06:21.873515+00:00 | UPSERT INTO vehicle_location_histories VALUES ($1, $2, now(), $3, $4) | 127.0.0.1:65240 |                  |    false    | executing
  15f92c0dd25882c80000000000000001 |       1 | 15f92aefb240d2980000000000000001 | root      | 2020-03-04 18:06:21.872608+00:00 | UPSERT INTO vehicle_location_histories VALUES ($1, $2, now(), $3, $4) | 127.0.0.1:65044 |                  |    false    | executing
  15f92c0dd262cb980000000000000002 |       2 | 15f92b7dc85b7ba80000000000000002 | maxroach  | 2020-03-04 18:06:21.873286+00:00 | SELECT city, id FROM vehicles WHERE city = $1                         | 127.0.0.1:65196 |                  |    false    | executing
```

### Cancel a query

When you see a query that is taking too long to complete, you can use the <InternalLink path="cancel-query">`CANCEL QUERY`</InternalLink> statement to end it.

For example, let's say you use `SHOW CLUSTER STATEMENTS` to find queries that have been running for more than 3 hours:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> WITH x as (SHOW CLUSTER STATEMENTS) SELECT * FROM x
      WHERE start < (now() - INTERVAL '3 hours');
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
              query_id             | node_id |            session_id            | user_name |              start              |        query        | client_address  | application_name | distributed |   phase
+----------------------------------+---------+----------------------------------+-----------+---------------------------------+---------------------+-----------------+------------------+-------------+-----------+
  15f92c745fe203600000000000000001 |       1 | 15f92c63d4b393b80000000000000001 | root      | 2020-03-04 18:13:42.33385+00:00 | SELECT * FROM rides | 127.0.0.1:65287 | $ cockroach sql  |    true     | executing
(1 row)
```

To cancel this long-running query, and stop it from consuming resources, you note the `query_id` and use it with the `CANCEL QUERY` statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CANCEL QUERY '15f92c745fe203600000000000000001';
```

## See also

* <InternalLink path="manage-long-running-queries">Manage Long-Running Queries</InternalLink>
* <InternalLink path="cancel-query">`CANCEL QUERY`</InternalLink>
* <InternalLink path="show-sessions">`SHOW SESSIONS`</InternalLink>
* <InternalLink path="show-jobs">`SHOW JOBS`</InternalLink>
* <InternalLink path="sql-statements">SQL Statements</InternalLink>
