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

# SHOW STATEMENTS

The `SHOW STATEMENTS` [statement](/docs/stable/sql-statements.md) 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 [cancelled](/docs/stable/cancel-query.md) to prevent unwanted resource consumption.

> [!NOTE]
>
> Schema changes and [`BACKUP`](/docs/stable/backup)/[`RESTORE`](/docs/stable/restore) statements are not executed as queries internally and so are not listed by `SHOW STATEMENTS`. To monitor such statements, use [`SHOW JOBS`](/docs/stable/show-jobs) instead.

## 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 [`VIEWACTIVITY` or `VIEWACTIVITYREDACTED` privilege](/docs/stable/security-reference/authorization#supported-privileges) can view see all users' currently active queries. `VIEWACTIVITYREDACTED` causes constants in queries being executed by other users to be redacted.

## Synopsis

![show\_statements syntax diagram](https://mintcdn.com/cockroachlabs/UZNSFbmU2EwEm6G5/images/sql-diagrams/common/show_statements.svg?fit=max\&auto=format\&n=UZNSFbmU2EwEm6G5\&q=85\&s=ae144cb36d8adee8ae6974afda30bd42)

* 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 [`timestamptz`](/docs/stable/timestamp) at which the query started. |
| `query` | The SQL query. Query text longer than the [`sql.status.active_query_text.max_bytes`](/docs/stable/cluster-settings#setting-sql-status-active-query-text-max-bytes) cluster setting is truncated with `…`. |
| `client_address` | The address and port of the client that issued the SQL query. |
| `application_name` | The [application name](/docs/stable/set-vars.md#supported-variables) specified by the client, if any. For queries from the [built-in SQL client](/docs/stable/cockroach-sql.md), 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;
```

```
              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;
```

```
              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 [`SELECT`](/docs/stable/select-clause) 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;
```

```
              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';
```

```
              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 [built-in SQL client](/docs/stable/cockroach-sql.md), 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';
```

```
              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 [`CANCEL QUERY`](/docs/stable/cancel-query) 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');
```

```
              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

* [Manage Long-Running Queries](/docs/stable/manage-long-running-queries.md)
* [`CANCEL QUERY`](/docs/stable/cancel-query)
* [`SHOW SESSIONS`](/docs/stable/show-sessions)
* [`SHOW JOBS`](/docs/stable/show-jobs)
* [SQL Statements](/docs/stable/sql-statements.md)
