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” insystem.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.
Session scoping per interface
- Native/TCP connections (
clickhouse-client, drivers) andclickhouse-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_idparameter: the history persists across requests that pass the samesession_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 settingsession_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.
Columns
sequence_number(UInt64) — Position of the query within the session, monotonically increasing.query_id(String) — The query id, can be joined withsystem.query_log.
Example
Run a few queries, then fetch their details fromsystem.query_log:
system.query_log:
GLOBAL IN evaluates the subquery once, in the current session. With plain IN, a query executed with parallel replicas also evaluates it on the other replicas, which do not see the history of the current session, so the rows they read are filtered out.
See also
- system.query_log - Details of executed queries.
session_query_ids_history_sizesetting.