Reduce memory on insert operations: Remove one of the copies of the batch that we keep in memory#109
Merged
BentsiLeviav merged 6 commits intoJul 19, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Reduces peak memory usage during WriteBatch insert operations by removing the materialized “boxed” intermediate [][]any/[][]interface{} copy and instead converting CSV rows to driver rows lazily while appending to the ClickHouse batch.
Changes:
- Refactors
InsertBatchto accept a re-iterable lazy row sequence (iter.Seq2[[]any, error]) and converts rows on-the-fly duringbatch.Append. - Simplifies
MergeUpdatedRowsto return a compacted[][]any(no “holes” +skipIdxmap), skipping and warning on missing PK matches. - Updates
ReplaceBatch/UpdateBatchto useMapErr(slices.Values(...), ...)for lazy row production.
Correctness & Data Integrity
- Retry semantics are preserved (the sequence must be re-iterable per attempt; doc comment correctly calls this out).
- Update path behavior remains equivalent: rows with missing table matches are still skipped; now the “no rows to insert” case is handled explicitly in
UpdateBatch.
ClickHouse-specific concerns
- No change to the underlying
PrepareBatch/Append/Sendbuffering behavior; this is correctly scoped to removing one in-process copy.
Error handling & observability
- Errors remain wrapped; conversion errors now abort the batch to return the connection to the pool.
Tests
- Repository includes e2e coverage exercising insert/update flows via the SDK tester (
destination/main_e2e_test.go). (Not executed as part of this review.)
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| destination/db/rows.go | Simplifies updated-row merge to return a compacted slice and skip missing matches. |
| destination/db/clickhouse.go | Implements lazy row conversion and updates insert/update batch code paths to avoid materializing boxed rows. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
BentsiLeviav
approved these changes
Jul 19, 2026
BentsiLeviav
deleted the
clickhouse/reduce-memory-used-in-insert-operations
branch
July 19, 2026 11:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Related to #107
Problem: Syncs on wide tables were hitting the container memory limit (OOM) during WriteBatch. For each insert batch (up to 100k rows), the connector held three full copies in memory at once:
Solution: Eliminate the boxed intermediate copy. InsertBatch now takes a lazy row sequence (iter.Seq2[[]any, error]) instead of a materialized slice, and ReplaceBatch converts each CSV row via ToInsertRow on the fly as it's appended to the driver batch — so each converted row is garbage-collectable immediately after Append. Peak memory per batch drops from raw + boxed + driver block to raw + driver block.
The raw CSV batch is intentionally kept: inserts are retried on network errors, so the sequence must be re-iterable (documented on InsertBatch). Conversion errors abort the insert and are not retried, as before.
As a side simplification, MergeUpdatedRows now returns a compacted slice instead of a slice with holes plus a skipIdx map.
No user-facing changes.
Follow-ups planned: