Skip to content

feat: add row-based automatic flushing for native batches - #2193

Open
xuzijie990329-boop wants to merge 3 commits into
ClickHouse:mainfrom
xuzijie990329-boop:batch-flush-interval-rows
Open

xuzijie990329-boop wants to merge 3 commits into
ClickHouse:mainfrom
xuzijie990329-boop:batch-flush-interval-rows

Conversation

@xuzijie990329-boop

@xuzijie990329-boop xuzijie990329-boop commented Nov 30, 2026 •

Copy link
Copy Markdown

Summary

Add an opt-in driver.WithFlushIntervalRows(n) option for native batches.

Large INSERT batches currently buffer all rows before Send. Wide String and
Map columns can require several times the input size during column encoding
and compression. MaxCompressionBuffer cannot reliably bound this because its
size check runs after a complete column has been encoded.

This change reuses Batch.Flush to emit a block every N appended rows while
keeping the INSERT open until Send. The default is zero, preserving existing
behavior. HTTP batch behavior is unchanged.

This is a row-based, backward-compatible step toward #809. It does not attempt
byte-accurate buffering or change the column encoding pipeline. A single large
value or map can still exceed the intended memory budget.

Related to #809 and #1384.

Compatibility

  • Existing batches are unchanged unless the option is supplied.
  • Automatic flushing is implemented only for native batches.
  • WithCloseOnFlush is rejected when combined with this option to avoid
    accidentally finalizing the INSERT at an interval boundary.

Tests

  • Exact row count and unique IDs across multiple flushed blocks
  • Tail block smaller than the configured interval
  • Map, LowCardinality, and Array(Map) round-trip comparisons
  • Append after automatic Flush and final Send
  • WithReleaseConnection compatibility
  • Default behavior with automatic flushing disabled
  • Invalid interval and incompatible option handling

Validation

  • go test ./... -run '^$' -count=1 passed
  • go vet ./lib/driver passed
  • go test -race -count=1 -v ./tests/issues -run TestBatchFlushIntervalRows
    not yet run locally; requires a running ClickHouse instance

Checklist

  • Unit and integration tests covering the common scenarios were added

Note

Medium Risk
Changes native batch INSERT buffering and connection lifecycle on interval flush; mistakes could affect data integrity or resource handling, though behavior is opt-in and guarded by validation and tests.

Overview
Adds opt-in driver.WithFlushIntervalRows(n) so native PrepareBatch inserts can Flush every N appended rows while the INSERT stays open until Send, reducing peak memory from buffering wide columns without changing default behavior (0 = disabled; HTTP still ignores flush).

PrepareBatch now validates options up front: negative intervals error, and WithFlushIntervalRows cannot be used with WithCloseOnFlush. The batch stores the interval and Append calls a new autoFlush path that reacquires the connection when needed, arms the same context watchdog used elsewhere, then flushes; failed auto-flush leaves the batch unusable per the new docs.

Integration tests in tests/issues/809_test.go cover multi-block inserts, tail blocks, complex types, WithReleaseConnection, disabled interval, canceled context, and invalid option combinations.

Reviewed by Cursor Bugbot for commit 1c181e3. Bugbot is set up for automated code reviews on this repo. Configure here.

@cla-assistant

cla-assistant Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread conn_batch.go Resolved
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

AI review gate — AI review passed

Clean review from: Cursor Bugbot.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread conn_batch.go Resolved
Comment thread conn_batch.go Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 9a926cf. Configure here.

Comment thread conn_batch.go Resolved
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.

1 participant