Error 159: TIMEOUT_EXCEEDED
This error occurs when a query exceeds the configured timeout limits for execution, connection, or network operations. It indicates that the operation took longer than the maximum allowed time and was automatically cancelled by ClickHouse.
Most common causes
-
Query execution timeout exceeded
- Query takes longer than
max_execution_timesetting - Long-running aggregations or joins
- Full table scans on large tables
- Inefficient query patterns
- Query takes longer than
-
Network socket timeout
- Client connection timeout during long queries
- Timeout while writing results to client socket
- Client disconnected before query completed
- Load balancer or proxy timeout between client and server
-
Distributed query timeout
- Timeout communicating with remote servers in cluster
- Network latency between cluster nodes
- Slow responses from remote shards
-
Resource contention causing slowness
- High CPU usage delaying query completion
- Memory pressure causing disk spilling
- I/O bottlenecks with slow storage
- Too many concurrent queries
-
HTTP connection timeout
- HTTP client timeout shorter than query execution time
- Keep-alive timeout mismatched between client and server
- Idle connection timeout on load balancers
Common solutions
1. Increase timeout settings
2. Optimize the query
3. Configure client-side timeout
For HTTP clients:
4. Handle timeout before checking execution speed
5. Enable query cancellation on client disconnect
6. Use async inserts with appropriate timeout
Common scenarios
Scenario 1: Query timeout with max_execution_time
Cause: Query ran longer than max_execution_time setting.
Solution:
Scenario 2: Network socket timeout
Cause: Client connection timed out while server was sending results.
Solution:
- Increase client socket timeout
- Use compression to reduce data transfer time
- Add
LIMITclause to reduce result size - Ensure stable network connection
Scenario 3: JDBC/HTTP client timeout
Cause: Client-side timeout shorter than query execution time.
Solution:
Scenario 4: Distributed query timeout
Cause: Remote shard not responding within timeout.
Solution:
Scenario 5: Load balancer timeout
Cause: Load balancer or proxy has shorter timeout than query duration.
Solution:
- Configure load balancer timeout settings
- Use direct connection for long-running queries
- Enable TCP keep-alive to maintain connection
Prevention tips
- Set appropriate timeouts: Match client and server timeout settings
- Monitor query performance: Identify and optimize slow queries
- Use LIMIT clauses: Reduce result set size for exploratory queries
- Optimize table design: Use proper primary keys and partitioning
- Configure keep-alive: Prevent idle connection timeouts
- Test long queries: Verify timeout settings before production use
- Use query result cache: Cache expensive query results
Debugging steps
-
Check current timeout settings:
-
Find queries that timed out:
-
Check if query completed despite timeout:
-
Analyze query performance:
-
Check for resource bottlenecks:
Special considerations
For HTTP/JDBC clients:
- Client timeout and server
max_execution_timeare independent - Query may continue running on server after client timeout
- Use
cancel_http_readonly_queries_on_client_close = 1to auto-cancel
For distributed queries:
- Each shard has its own timeout
- Network latency adds to total execution time
- Use
distributed_connections_timeoutfor shard communication
For long-running analytical queries:
- Consider using materialized views for pre-aggregation
- Break complex queries into smaller steps
- Use query result cache for repeated queries
- Schedule heavy queries during off-peak hours
For aggregations with external sorting:
- Large aggregations may spill to disk
- Merging temporary files can take significant time
- Monitor memory usage and
max_bytes_before_external_group_by
Timeout-related settings
Synchronizing client and server timeouts
To ensure queries stop when client times out:
cancel_http_readonly_queries_on_client_close only works when readonly > 0, which is automatic for HTTP GET requests.
If you're experiencing this error:
- Check if timeout is due to query complexity or timeout configuration
- Review
max_execution_timesetting and increase if needed - For HTTP/JDBC clients, ensure client timeout >= server timeout
- Use
EXPLAINto analyze query plan and optimize - Monitor query performance in
system.query_log - Consider breaking long queries into smaller batches
- For production workloads, set appropriate timeout values based on query patterns
Related documentation: