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

> System table containing the query ids of the queries executed in the current session.

# system.session_query_ids

<h2 id="description">
  Description
</h2>

Contains the query ids of the queries executed in the current session, in execution order. Use it to find "the queries I just ran" in [`system.query_log`](/docs/reference/system-tables/query_log) without assigning `query_id` client-side or tagging queries with `log_comment`.

The contents are session-scoped: each session sees only its own history, and other sessions' queries never appear.

A query id is recorded when the query *starts*, so:

* The currently running query is already visible when it selects from the table.
* Failed queries are recorded too — retrieving the id of a query that just failed is a primary use case.

Internal queries (system log flushes and similar) are not recorded. Sub-queries that distributed queries run on remote shards are not recorded either — only the initiating query appears, in the initiator's session.

<h2 id="columns">
  Columns
</h2>

* `sequence_number` ([UInt64](/docs/reference/data-types/int-uint)) — Position of the query within the session, monotonically increasing.
* `query_id` ([String](/docs/reference/data-types/string)) — The query id, can be joined with `system.query_log`.

<h2 id="session-scoping-per-interface">
  Session scoping per interface
</h2>

* **Native/TCP connections** (`clickhouse-client`, drivers) and **`clickhouse-local`**: the session is the connection, so the history accumulates across all queries of the connection, including multi-query client invocations.
* **HTTP with the `session_id` parameter**: the history persists across requests that pass the same `session_id`, until the session expires.
* **HTTP without `session_id`**: every request is its own session, so the table only ever shows the current query.

<h2 id="history-size">
  History size
</h2>

The history is a ring buffer bounded by the session setting `session_query_ids_history_size` (default `1000`); when the history exceeds this size, the oldest entries are evicted first. Setting it to `0` disables recording; entries recorded earlier stay in the table until truncated or evicted.

The setting is read at query start, before the query is parsed, so a `SETTINGS` clause of the query itself does not affect whether that query is recorded; use `SET`, an HTTP URL parameter, or a settings profile instead.

<h2 id="truncate">
  TRUNCATE
</h2>

`TRUNCATE TABLE system.session_query_ids` clears the history of the current session. The sequence counter is not reset, so `sequence_number` values are never reused within a session.

<h2 id="example">
  Example
</h2>

Run a few queries, then fetch their details from [`system.query_log`](/docs/reference/system-tables/query_log):

```sql theme={null}
SELECT 1 FORMAT Null;
SELECT 2 FORMAT Null;

SELECT * FROM system.session_query_ids;
```

```text theme={null}
┌─sequence_number─┬─query_id─────────────────────────────┐
│               1 │ 4c9e97a3-b806-4a5c-9a94-5a614c3d0b3f │
│               2 │ 8f9a2f59-30d3-4f8e-b3a1-6a0c1a4d3e2b │
│               3 │ f2b1c4d8-7e6a-4b5c-8d9e-0a1b2c3d4e5f │
└─────────────────┴──────────────────────────────────────┘
```

The current query is recorded at its start, so it appears as the last entry. Timestamps, status, and all other details are available by joining `system.query_log`:

```sql theme={null}
SYSTEM FLUSH LOGS query_log;

SELECT query_id, type, query, query_duration_ms
FROM system.query_log
WHERE query_id IN (SELECT query_id FROM system.session_query_ids)
ORDER BY event_time_microseconds;
```

<h2 id="see-also">
  See also
</h2>

* [`system.query_log`](/docs/reference/system-tables/query_log) — details of executed queries
* `session_query_ids_history_size` setting
