Skip to content

chore: Async inserts docs and example updates#1690

Merged
SpencerTorres merged 3 commits intomainfrom
kavirajk/async-fixes-and-docs-update
Oct 22, 2025
Merged

chore: Async inserts docs and example updates#1690
SpencerTorres merged 3 commits intomainfrom
kavirajk/async-fixes-and-docs-update

Conversation

@kavirajk
Copy link
Copy Markdown
Contributor

Summary

Previously it was confusing (1) how to use async inserts (2) what kind of interfaces supports async inserts.

This PR address both. Changes

  1. Deprecate WithStdAsync and prefer new WithAsync
  2. Deprecate AsyncInsert() api and prefer new WithAsync
  3. Add more examples for all possible cases
  4. Update README

Checklist

Delete items not relevant to your PR:

Previously it was confusing (1) how to use async inserts (2) what kind
of interfaces supports async inserts.

This PR address both. Changes
1. Deprecate `WithStdAsync` and prefer new `WithAsync`
2. Deprecate `AsyncInsert()` api and prefer new `WithAsync`
3. Add more examples for all possible cases
4. Update README

Signed-off-by: Kaviraj <[email protected]>
@kavirajk kavirajk marked this pull request as ready for review October 17, 2025 11:50
@mshustov mshustov requested review from Copilot and jkaflik and removed request for Copilot October 17, 2025 13:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Deprecates legacy async insert APIs in favor of a new context-based WithAsync option and updates examples and README accordingly.

  • Introduces WithAsync() and deprecates WithStdAsync() and AsyncInsert()
  • Refactors examples/benchmarks to use Exec + WithAsync() instead of AsyncInsert()
  • Adds separate native/HTTP async insert examples and updates documentation

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/utils.go Adjusts HTTP connection helpers to accept a string session identifier instead of *testing.T
lib/driver/driver.go Marks AsyncInsert as deprecated in the driver interface
examples/std/main_test.go Splits async insert test into native and HTTP variants
examples/std/async_native.go Renames function and switches to WithAsync() usage
examples/std/async_http.go Adds new HTTP async insert example using WithAsync()
examples/clickhouse_api/utils.go Adds HTTP connection helper; import reordering
examples/clickhouse_api/main_test.go Splits async insert test into native and HTTP variants
examples/clickhouse_api/async_native.go Renames and migrates to Exec + WithAsync()
examples/clickhouse_api/async_http.go Adds HTTP async insert example with session name
context.go Adds WithAsync() and deprecates WithStdAsync()
conn_async_insert.go Minor formatting (blank line)
clickhouse.go Adds context-based async handling inside Exec; deprecates AsyncInsert method
benchmark/v2/write-async/main.go Migrates benchmark to WithAsync() but retains a stale argument
README.md Updates async insert documentation and deprecation notice

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

err := conn.AsyncInsert(ctx, fmt.Sprintf(`INSERT INTO benchmark_async VALUES (
err := conn.Exec(ctx, fmt.Sprintf(`INSERT INTO benchmark_async VALUES (
%d, '%s', [1, 2, 3, 4, 5, 6, 7, 8, 9], now()
)`, i, "Golang SQL database driver"), false)
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

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

The trailing false argument is now treated as a bind parameter to Exec, but the query has no placeholders; this will cause a parameter count mismatch or unintended binding. Remove the false argument and rely on WithAsync(true) for async wait semantics (or adjust WithAsync(false) if non-waiting behavior is desired).

Suggested change
)`, i, "Golang SQL database driver"), false)
)`, i, "Golang SQL database driver"))

Copilot uses AI. Check for mistakes.
HTTP protocol supports batching. It can be enabled by setting `async_insert` when using standard `Prepare` method.

For more details please see [asynchronous inserts](https://clickhouse.com/docs/en/optimize/asynchronous-inserts#enabling-asynchronous-inserts) documentation.
**NOTE**: The old `AsyncInsert()` api is deprecated and will be removed in future versions. We highly recommend to use `WithAsync()` api for all the Async Insert use cases.
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

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

Correct acronym capitalization: change 'api' to 'API' in both occurrences.

Suggested change
**NOTE**: The old `AsyncInsert()` api is deprecated and will be removed in future versions. We highly recommend to use `WithAsync()` api for all the Async Insert use cases.
**NOTE**: The old `AsyncInsert()` API is deprecated and will be removed in future versions. We highly recommend to use `WithAsync()` API for all the Async Insert use cases.

Copilot uses AI. Check for mistakes.
Comment on lines +16 to +37
const ddl = `
CREATE TABLE example (
Col1 UInt64
, Col2 String
, Col3 Array(UInt8)
, Col4 DateTime
) ENGINE = Memory
`
if _, err := conn.Exec(ddl); err != nil {
return err
}
ctx := clickhouse.Context(context.Background(), clickhouse.WithAsync(false))
{
for i := 0; i < 100; i++ {
_, err := conn.ExecContext(ctx, `INSERT INTO example VALUES (
?, ?, ?, now()
)`, i, "Golang SQL database driver", []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9})
if err != nil {
return err
}
}
}
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

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

[nitpick] This block duplicates the logic in async_native.go (table DDL + insert loop); consider extracting a shared helper (e.g., createAndFillExample(ctx, conn)) to reduce repetition and ease future changes.

Copilot uses AI. Check for mistakes.
@SpencerTorres SpencerTorres merged commit 1d64d6c into main Oct 22, 2025
14 checks passed
@vincentbernat
Copy link
Copy Markdown
Contributor

vincentbernat commented Nov 24, 2025

Shouldn't we use clickhouse.WithAsync(true)?

My bad, the boolean is for whatever to wait for asynchronous insert to return. So, false means return immediatly and true means wait for the async insertion to be done.

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.

4 participants