Skip to main content
When a query runs longer than max_execution_time and timeout_overflow_mode is set to break, ClickHouse stops the query without an error and returns what it has produced so far. A query that aggregates or sorts has produced nothing at that moment, so the result is empty. The same query returns rows when it finishes within the limit, or when it runs in a session with a higher limit or with the default mode throw, which is why the behaviour looks inconsistent.

Reproduce the behaviour

In the following query sleepEachRow(0.3) makes every row take 0.3 seconds and max_block_size = 1 puts every row into its own block, so the query needs about three seconds:
With a limit of one second and the mode break, the query stops after about one second. It returns no rows and reports no error, although count without GROUP BY normally always returns one row:
The aggregation needs all of its input before it can return the first row, so there is nothing to return when the query is stopped. Queries with ORDER BY behave the same way. A query that streams rows to the client as it reads them returns the rows that were ready before the limit:
In both cases the result is incomplete, and nothing in the response tells the client so.

Confirm it in the query log

system.query_log records the settings that were changed for a query and a counter of stopped executions. Entries are written with a delay of a few seconds; SYSTEM FLUSH LOGS writes them immediately.
For the two queries above it returns the following (the durations and the exact value of the counter vary a little between runs):
A query that was stopped by the time limit in the break mode has this signature:
  • type is QueryFinish and exception_code is 0: the query is recorded as successful.
  • query_duration_ms is equal to max_execution_time or a little above it. The limit is checked only at certain points of data processing, so the query overruns it slightly.
  • Settings['timeout_overflow_mode'] is break. The Settings map contains only the settings that were changed for the query, so an empty value means the default, throw.
  • ProfileEvents['OverflowBreak'] is 1 or more. This counter is incremented every time data processing is stopped by a limit whose overflow mode is break. Other limits use it too, for example max_rows_to_read with read_overflow_mode = 'break', which is why the query also filters on timeout_overflow_mode.
To find the runs of one particular query, add event_time, query_id, user and query to the select list and filter on normalized_query_hash or on a fragment of the query text. Every replica has its own query log. In ClickHouse Cloud, or on a self-managed cluster, read the logs of all replicas with clusterAllReplicas, as described in Querying across nodes and versions.

How to fix it

First find out where the two settings come from. Run this in the same kind of connection that the affected application or tool uses:
The defaults are no time limit and the mode throw:
A changed value can come from the settings of the user or of a settings profile, from a SET statement in the session, from the SETTINGS clause of the query, or from the connection options of a client, a driver or a BI tool. See Query-level session settings for the order of priority. Then choose one of the following:
  • Fail instead of returning an incomplete result. Set timeout_overflow_mode = 'throw' for the queries, users or tools for which an empty or partial result is misleading. The query then fails with TIMEOUT_EXCEEDED (code 159), and the client can report the error or retry. For a user:
  • Give the query more time. Raise max_execution_time for this workload, in the same place where the current value is set.
  • Make the query finish within the limit. Narrow the time range, filter on the columns of the primary key, or read from a pre-aggregated table.
  • Keep break only where an incomplete result is acceptable, for example for queries that stream rows to a tool that shows whatever arrived in time.