Description
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 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.
Columns
sequence_number (UInt64) — Position of the query within the session, monotonically increasing.
query_id (String) — The query id, can be joined with system.query_log.
Session scoping per interface
- 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.
History size
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.
TRUNCATE
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.
Example
Run a few queries, then fetch their details from system.query_log:
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:
See also
system.query_log — details of executed queries
session_query_ids_history_size setting
Last modified on August 25, 2026