Skip to main content
Over the HTTP interface, a query that returns no rows, such as INSERT ... SELECT, sends nothing until it has finished: no status line, no headers, no body. While the query runs, the connection is idle. If the query takes longer than the read timeout of your client, or longer than the idle timeout of a load balancer, proxy or firewall on the way, the connection is dropped and the response never arrives, although the query normally completes on the server. Either do not wait on the connection (run_query_in_background) or keep the connection busy (progress headers).

What happens

  1. The client sends INSERT INTO target_table SELECT ... FROM source_table.
  2. The server runs the query. It writes the response (200 OK, the X-ClickHouse-Summary header and an empty body) only when the query ends.
  3. Before that, one of two things happens:
    • The client stops waiting when its own timeout expires.
    • A network device between the client and the server drops the connection because no data passed through it for too long. The client then gets a connection error or keeps waiting until its own timeout, even if this timeout is longer than the query.
  4. The server does not notice that the client is gone until it tries to write the response, so the query runs to the end.
Raising the timeout of the client helps only in the first case. In the second case the response is lost whatever the client does, which looks as if the server never acknowledged a query that in fact succeeded.

Check what happened to the query

Do not repeat the request blindly: the first INSERT ... SELECT may still be running or may have completed, and a second one can insert the same rows again. Send a query_id of your own with every request, so that you can look the query up:
While the query runs, it is in system.processes:
After it has finished, system.query_log has the outcome:
Both tables are local to a server. On a cluster or behind a load balancer, query all servers as shown in Monitor execution.

Run the query in the background

Since ClickHouse 26.8, the setting run_query_in_background detaches a query from the connection. The server checks the query, starts it, returns an empty successful response at once, and runs the query to completion whatever happens to the connection. Over HTTP, pass the setting as a URL parameter:
Then poll system.query_log with the query from the previous section. An error that occurs after the query has started is recorded only there. See Background queries for the details and the limitations.

Keep the connection busy with progress headers

If the client has to wait for the result on the same connection, let the server send data while the query runs. With send_progress_in_http_headers the server sends the status line together with the first X-ClickHouse-Progress header and repeats this header while the query reads and writes rows, at most once per http_headers_progress_interval_ms:
Things to know:
  • Set the interval below the shortest idle timeout on the path, but not much lower. The default is 100 milliseconds, which produces thousands of headers for a long query, and HTTP clients limit the number or the total size of response headers. Python’s http.client, for example, accepts at most 100 headers, and Node.js limits their total size (see the JavaScript client troubleshooting notes).
  • The response starts with 200 OK before the query has finished. If the query fails later, the status stays 200, the error code is in the X-ClickHouse-Exception-Code response header and the message is in the body. Check both.
  • The timeout of the client for the whole request must still be longer than the query.

What wait_end_of_query does and does not do

The URL parameter wait_end_of_query=1 (the http_wait_end_of_query setting) makes the server buffer the response. When progress headers are disabled, it also delays the status code and headers until the query has finished. This matters for queries that return rows: an error in the middle of the query is then reported with a proper HTTP status instead of inside a 200 response. Progress headers take precedence over this delay. With send_progress_in_http_headers=1, the first progress header starts the 200 response before the query finishes, even with wait_end_of_query=1. If the query fails after that, check the X-ClickHouse-Exception-Code response header and the response body instead of trusting the status code. With progress headers disabled, wait_end_of_query does not help with an idle connection. Nothing is sent while the query runs, and an INSERT ... SELECT has no result to buffer, so its response is written when the query ends with or without this parameter. With progress headers enabled, those headers keep the connection active but commit the status code early.

About server-side timeouts

You do not need to change http_receive_timeout and http_send_timeout for a long query. They limit a single read from or write to the socket, not the duration of the query.