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 querysleepEachRow(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:
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:
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:
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.
break mode has this signature:
typeisQueryFinishandexception_codeis0: the query is recorded as successful.query_duration_msis equal tomax_execution_timeor 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']isbreak. TheSettingsmap contains only the settings that were changed for the query, so an empty value means the default,throw.ProfileEvents['OverflowBreak']is1or more. This counter is incremented every time data processing is stopped by a limit whose overflow mode isbreak. Other limits use it too, for examplemax_rows_to_readwithread_overflow_mode = 'break', which is why the query also filters ontimeout_overflow_mode.
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:throw:
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 withTIMEOUT_EXCEEDED(code 159), and the client can report the error or retry. For a user: -
Give the query more time. Raise
max_execution_timefor 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
breakonly where an incomplete result is acceptable, for example for queries that stream rows to a tool that shows whatever arrived in time.
Related settings
max_execution_time_leafandtimeout_overflow_mode_leafare the same pair of settings for the leaf nodes of distributed queries.- Restrictions on query complexity describes the other limits and their overflow modes.
- Setting a limit on query execution time shows the default behaviour, when the query fails with
TIMEOUT_EXCEEDED.