Skip to content

fix: bound clickHouse v2 s3 copy memory - #7407

Merged
achettyiitr merged 4 commits into
release/1.87.xfrom
fix/clickhouse-v2-s3-copy-memory-settings
Sep 18, 2026
Merged

achettyiitr merged 4 commits into
release/1.87.xfrom
fix/clickhouse-v2-s3-copy-memory-settings

Conversation

@achettyiitr

@achettyiitr achettyiitr commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Description

The ClickHouse v2 copy engine loads a table with a single statement that globs the whole load-file folder:

loadFolder := loadFolderDir + "*.csv.gz"
INSERT INTO "ns"."tracks" (cols)
SELECT * FROM s3('<folder>/*.csv.gz', ..., 'CSV', '<coltypes>', 'gz')
SETTINGS date_time_input_format = 'best_effort',
         input_format_csv_arrays_as_nested_csv = 1;

ClickHouse downloads, decompresses and parses every file server-side, so peak memory follows its parse and insert parallelism — not anything batched client-side (commitEvery belongs to loadByDownloadingLoadFiles, the other branch, and is never reached when the S3 engine is on).

A large enough folder exhausts the instance:

code: 241 (total) memory limit exceeded: would use 47.01 GiB
(attempt to allocate chunk of 257.28 MiB), current RSS: 46.84 GiB, maximum: 46.80 GiB
... While executing CSVRowInputFormat: While executing ReadFromObjectStorage

Both settings on the statement are parsing semantics, so there was no lever for this short of resizing the ClickHouse instance.

Change

Adds seven settings to the copy statement, each resolved per workspace with a global fallback, matching the existing disableLoadTableStats / maxLoadDelay pattern:

Config key ClickHouse setting Default Reference
s3Copy.maxThreads max_threads auto(18) docs
s3Copy.maxParsingThreads max_parsing_threads auto(18) docs
s3Copy.maxInsertThreads max_insert_threads 0 (auto; Cloud sets 4 here) docs
s3Copy.maxMemoryUsage max_memory_usage 0 — no per-query limit docs
s3Copy.minInsertBlockSizeBytes min_insert_block_size_bytes 268402944 (~256 MiB) docs
s3Copy.minInsertBlockSizeRows min_insert_block_size_rows 1048449 docs
s3Copy.disableParallelParsing input_format_parallel_parsing 1 (enabled) docs

Resolved as Warehouse.clickhouse.v2.<workspaceID>.s3Copy.<name> then Warehouse.clickhouse.v2.s3Copy.<name>. The same links are in the code beside the settings list.

Defaults above are read from system.settings on the affected instance (ClickHouse Cloud 26.4.1.2359, 18 cores), not just the docs — Cloud has already set max_insert_threads = 4 there, and the docs quote min_insert_block_size_rows as 1,048,545 against the server's 1,048,449.

Why these, and what to reach for first

ClickHouse's own S3 insert tuning guidance sizes block size against thread count: min_insert_block_size_bytes ≈ peak_memory / (3 * max_insert_threads). On the affected instance that is 46.8 GiB / (3 * 4) ≈ 3.9 GiB, well above the 256 MiB already in effect — so block size is not what is driving this.

The likelier driver is parse-side parallelism: max_parsing_threads defaults to one per core, auto(18) here, and each thread holds a buffer across a folder of many gzipped files, which is the frame the failing allocation is inside. input_format_parallel_parsing = 0 is the documented workaround, but it gives the parallelism up altogether; max_parsing_threads bounds it instead, which matters because the copy path is currently ~7-10x faster than the insert path it replaced.

Worth noting max_memory_usage defaults to 0, i.e. no per-query ceiling: the copy was not stopped by its own limit, it grew until it hit the server-wide budget. Where several workspaces share an instance, one oversized copy can starve the others rather than just failing itself, so a per-query cap is useful containment even though it does not reduce usage.

max_download_threads / max_download_buffer_size are deliberately left out: downloads are already capped at 4 threads by default on this instance, and the stack points at the parse stage, not the fetch.

Every setting is unset by default, so a deployment that configures none of them sends a byte-identical statement. This matters: the S3 copy path is currently ~7-10x faster than the previous insert path in production, and this change must not regress that by default.

Unset is -1 rather than 0 because min_insert_block_size_rows is meant to take 0 when paired with min_insert_block_size_bytes.

Test plan

  • go build ./warehouse/..., go vet, gofmt clean
  • TestS3CopySettingsV2: unset default, global keys, per-workspace override beating global, 0 reaching the statement rather than reading as unset, parallel parsing disabled by name, the full name mapping spelled out so a typo in a ClickHouse setting name fails in the test rather than on the server, and the clause staying a single settings list
  • Existing TestS3CredentialsRegexV2 (credential masking) still passes against the rebuilt statement
  • Integration suite: new using s3 engine with memory settings case in TestIntegration loads through the s3 engine with every supported setting applied, including the two that must be sendable as 0. Only the server rejects a bad setting name or a malformed clause, so this is the layer that proves what the copy engine builds is something ClickHouse will actually run. (Compiles and vets locally; needs SLOW=1 and a running docker daemon, so it runs in CI.)
  • Set maxParsingThreads (or disableParallelParsing) for the affected workspace and confirm the load completes without code: 241

Linear Ticket

https://linear.app/rudderstack/issue/INT-7100

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

Every setting value is read through GetInt64Var / GetBoolVar and formatted with %d, so only integers and a fixed boolean reach the SQL — config cannot inject arbitrary text into the statement. Credential masking is unchanged and still covered by TestS3CredentialsRegexV2.

🤖 Generated with Claude Code

The copy engine reads a whole folder of gzipped CSV in one
INSERT ... SELECT FROM s3(), so its peak memory follows ClickHouse's
parse and insert parallelism, not anything batched client-side. A large
enough folder exhausts the instance:

  code: 241 (total) memory limit exceeded: would use 47.01 GiB,
  maximum: 46.80 GiB ... While executing CSVRowInputFormat:
  While executing ReadFromObjectStorage

The statement carried only two settings, both about parsing semantics,
so there was no lever for this short of resizing the instance. Add
max_threads, max_insert_threads, max_memory_usage,
min_insert_block_size_bytes, min_insert_block_size_rows and
input_format_parallel_parsing, each resolved per workspace with a global
fallback.

Every one is unset by default, so a deployment that configures none of
them sends the statement it sent before. Unset is -1 rather than 0
because min_insert_block_size_rows is meant to take 0 alongside
min_insert_block_size_bytes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

🔒 Scanned for secrets using gitleaks 8.29.1
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

🔒 Scanned for secrets using gitleaks 8.29.1
Only the server rejects a bad setting name or a malformed settings
clause, so the unit tests cannot tell whether what the copy engine
builds is something ClickHouse will run. Add a case that loads through
the s3 engine with every supported setting applied, including the two
that have to be sendable as zero.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

🔒 Scanned for secrets using gitleaks 8.29.1
Parallel CSV parsing defaults to one thread per core — auto(18) on the
instance that ran out of memory — and each holds a buffer, which is what
the failing allocation was inside. disableParallelParsing could only
give that up altogether; bounding the threads keeps most of the
parallelism the copy path is fast because of.

Also spell the full name mapping out in a test, so a typo in a
ClickHouse setting name fails there rather than on the server mid-load.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

🔒 Scanned for secrets using gitleaks 8.29.1
@achettyiitr achettyiitr changed the title fix(warehouse): bound ClickHouse v2 S3 copy memory fix: bound ClickHouse v2 S3 copy memory Sep 18, 2026
@achettyiitr achettyiitr changed the title fix: bound ClickHouse v2 S3 copy memory fix: bound ClickHouse v2 s3 copy memory Sep 18, 2026
@achettyiitr achettyiitr changed the title fix: bound ClickHouse v2 s3 copy memory fix: bound clickHouse v2 s3 copy memory Sep 18, 2026
@codecov

codecov Bot commented Sep 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.87%. Comparing base (59558bd) to head (9076426).

Additional details and impacted files
@@                Coverage Diff                 @@
##           release/1.87.x    #7407      +/-   ##
==================================================
+ Coverage           84.38%   84.87%   +0.49%     
==================================================
  Files                 620      620              
  Lines               62706    62729      +23     
==================================================
+ Hits                52912    53239     +327     
+ Misses               9791     9487     -304     
  Partials                3        3              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@achettyiitr
achettyiitr merged commit ee78070 into release/1.87.x Sep 18, 2026
94 of 96 checks passed
@achettyiitr
achettyiitr deleted the fix/clickhouse-v2-s3-copy-memory-settings branch September 18, 2026 09:24
achettyiitr pushed a commit that referenced this pull request Sep 18, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.87.1](v1.87.0...v1.87.1)
(2026-09-18)


### Bug Fixes

* bound clickHouse v2 s3 copy memory
([#7407](#7407))
([ee78070](ee78070))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: rudderstack-github-actions[bot] <236995729+rudderstack-github-actions[bot]@users.noreply.github.com>
achettyiitr added a commit that referenced this pull request Sep 18, 2026
# Description

Syncing patch release v1.87.1 to main branch

**↓↓ Please review and edit commit overrides before merging ↓↓**

BEGIN_COMMIT_OVERRIDE
fix: bound clickHouse v2 s3 copy memory (#7407)
END_COMMIT_OVERRIDE

---------

Co-authored-by: Akash Chetty <[email protected]>
Co-authored-by: rudderstack-github-actions[bot] <236995729+rudderstack-github-actions[bot]@users.noreply.github.com>
This was referenced Sep 21, 2026
achettyiitr pushed a commit that referenced this pull request Sep 21, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.88.0-rc.1](v1.87.0...v1.88.0-rc.1)
(2026-09-21)


### Features

* **warehouse:** bigquery workload identity federation authentication
([#7361](#7361))
([d16ab5f](d16ab5f))


### Bug Fixes

* bound clickHouse v2 s3 copy memory
([#7407](#7407))
([a17a78f](a17a78f))


### Miscellaneous

* cleanup python mirroring
([#7395](#7395))
([9f9bcde](9f9bcde))
* default failed-keys error capture on
([#7404](#7404))
([db84987](db84987))
* sync release v1.87.0 to main branch
([#7394](#7394))
([80353d1](80353d1))
* tag namespace config http client stats with version and mode
([#7401](#7401))
([d152ae5](d152ae5))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: rudderstack-github-actions[bot] <236995729+rudderstack-github-actions[bot]@users.noreply.github.com>
mihir20 pushed a commit that referenced this pull request Sep 22, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.88.0](v1.87.0...v1.88.0)
(2026-09-21)


### Features

* **warehouse:** bigquery workload identity federation authentication
([#7361](#7361))
([d16ab5f](d16ab5f))


### Bug Fixes

* bound clickHouse v2 s3 copy memory
([#7407](#7407))
([a17a78f](a17a78f))


### Miscellaneous

* cleanup python mirroring
([#7395](#7395))
([9f9bcde](9f9bcde))
* default failed-keys error capture on
([#7404](#7404))
([db84987](db84987))
* sync release v1.87.0 to main branch
([#7394](#7394))
([80353d1](80353d1))
* tag namespace config http client stats with version and mode
([#7401](#7401))
([d152ae5](d152ae5))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: rudderstack-github-actions[bot] <236995729+rudderstack-github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants