chore: Async inserts docs and example updates#1690
Conversation
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]>
Signed-off-by: Kaviraj <[email protected]>
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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).
| )`, i, "Golang SQL database driver"), false) | |
| )`, i, "Golang SQL database driver")) |
| 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. |
There was a problem hiding this comment.
Correct acronym capitalization: change 'api' to 'API' in both occurrences.
| **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. |
| 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 | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
[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.
Signed-off-by: Kaviraj <[email protected]>
|
Shouldn't we use 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. |
Summary
Previously it was confusing (1) how to use async inserts (2) what kind of interfaces supports async inserts.
This PR address both. Changes
WithStdAsyncand prefer newWithAsyncAsyncInsert()api and prefer newWithAsyncChecklist
Delete items not relevant to your PR: