Skip to main content
An INSERT INTO ... SELECT keeps in memory the rows that are about to become new data parts: for every insert thread, a block of up to min_insert_block_size_rows rows or min_insert_block_size_bytes bytes. If the query fails with MEMORY_LIMIT_EXCEEDED (code 241), reduce the number of insert threads first, and the size of the blocks second.

The error

The first words name the limit that was reached: Query is max_memory_usage, User is max_memory_usage_for_user, and (total) is the memory limit of the server. When the message ends with OvercommitTracker decision: Query was selected to stop by OvercommitTracker, the limit of the user or of the server was reached and this query was chosen as the one to stop; see Memory overcommit.

Where the memory goes

  1. The SELECT part reads the source with up to max_threads threads.
  2. The rows flow into up to max_insert_threads parallel insert streams, but not more than the number of streams that the SELECT produces.
  3. Each insert stream collects rows until it has min_insert_block_size_rows rows (1048449 by default) or min_insert_block_size_bytes bytes (268402944 by default, about 256 MiB), whichever comes first. It then sorts the block and writes it as a new part, or as one part for each partition that the block touches.
A stream holds more than one such block at a time, because it collects the next block while the previous one is being written. The peak is therefore a few block sizes multiplied by the number of insert streams. By default max_insert_threads is 0, which means the number of CPU cores, so an INSERT ... SELECT writes in parallel. Before version 26.8 the default was 1. The settings reference lists the lower defaults that ClickHouse Cloud uses.

Example

Create a source table with 6 million rows of about 120 bytes each, and an empty copy:
With four threads and a memory limit of 500 MB, copying the table fails with the error shown above:
With one insert thread it succeeds, and so does a parallel insert with smaller blocks:
The SETTINGS clause at the end of the query applies to both the SELECT and the INSERT part.

What each setting costs

The following table shows the peak memory (memory_usage in system.query_log) and the number of new parts for the same copy without a memory limit. The numbers are from one test machine and differ on yours, but the direction of each change is the same.
  • Fewer insert threads lower the memory without changing the number of parts; the insert takes longer. Setting max_threads = 1 has the same effect, because the insert cannot have more streams than the SELECT, but it also makes the reading single-threaded.
  • Smaller blocks lower the memory further, but every block becomes a part: ten times smaller blocks mean ten times more parts for the background merges to process. For wide rows, lower min_insert_block_size_bytes instead of the number of rows.
  • Do not go too low. Blocks are not split, only combined, so a value like min_insert_block_size_rows = 1000 turns every block that the SELECT produces into a part of its own: 733 parts instead of 6 in this test. On a large table this can end with delayed inserts and the TOO_MANY_PARTS error.
You can measure your own query the same way:
For the three inserts of the example it returns the following (the memory figures vary by some tens of megabytes between runs):

Other things to check