Repository navigation
docs: explain the second query_log row of a batch insert - #2000
sankalpsthakur wants to merge 10 commits into
Conversation
15159aa to
597b95c
Compare
ClickHouse 26.3+ defaults async_insert=1, so PrepareBatch/Send was logged twice in system.query_log: the client INSERT under the intended query_id, then a server flush that rewrites the table as database.table and assigns a new UUID. PrepareBatch already buffers client-side; send async_insert=0 unless the caller set it on the context or connection.
597b95c to
5d35867
Compare
🤖 Claude reviewThe PR adds Key concerns:
Blind spots:
Verdict: 💬 Needs discussion General findings
Inline comments are attached to the relevant lines. This summary updates in place on re-review. |
must108
left a comment
There was a problem hiding this comment.
Hi @sankalpsthakur, thanks for your contribution! Could you please address the issues flagged by the agent? We will most likely have to discuss this change further, but until then, this would be great.
Keep the batch default of async_insert=0 when the caller did not opt in (the 26.3+ duplicate query_id bug). Treat WithAsync/WithStdAsync as a query-level opt-in before the connection setting, matching AsyncInsert. Add an HTTP PrepareBatch/Send regression next to the native test.
|
Addressed the review notes that are code:
The default of sending |
|
@must108 thanks, I added coverage for the HTTP batch path and database/sql in tests/issues/issue_1997_test.go (native, HTTP, database/sql, with race detector on 26.3). Could you take another look when you have a moment? |
|
agreed, batches no longer force async_insert=0, server setting applies unless the caller opts in with SyncBatchInsert or sync_batch_insert=true, ptal |
…ive-insert-query-id-efec # Conflicts: # conn_batch.go # conn_http_batch.go
|
merged main, resolved conflict with #2068 by reusing applyBatchAsyncInsertSettings, explicit async_insert still wins, ptal |
| ConnMaxLifetime time.Duration // default 1 hour | ||
| ConnOpenStrategy ConnOpenStrategy | ||
| FreeBufOnConnRelease bool // drop preserved memory buffer after each query | ||
| SyncBatchInsert bool // send async_insert=0 on batch inserts unless async_insert is set explicitly |
There was a problem hiding this comment.
SyncBatchInsert is necessary
SyncBatchInsert sends async_insert=0 for batches, which a caller can already get with Options.Settings{"async_insert": 0} or WithSettings on the batch context.
- The only difference is that
Execinserts keep the server default. The PR does not give a use case for that difference. - The connection setting silently cancels the option (
Settings{"async_insert": 1}+SyncBatchInsert: truegives async batches). A caller can set the two and get no error. - If the option stays, add it to
docs/config-reference.mdx,docs/configuration.mdx, and the DSN parameter list inREADME.md, and add a DSN parse case toclickhouse_options_test.go. - Alternative: remove the field and keep only a docs note that recommends
async_insert=0for callers who need onequery_id.
There was a problem hiding this comment.
not necessary, so i removed it in be7583d. Options.Settings{"async_insert": 0} sends the same setting for every query on the connection, and WithSettings on the context of PrepareBatch sends it for one batch, so a caller can keep async Exec inserts and still get a sync batch. #1848 is open for a general way to disable async inserts. the docs now show both ways (7d5fb88).
| // An explicit query-level or connection-level `async_insert` always wins. | ||
| func applyBatchAsyncInsert(opt *Options, o *QueryOptions) { | ||
| if o.settings != nil { | ||
| if _, ok := o.settings["async_insert"]; ok { |
There was a problem hiding this comment.
async_insert now cancels WithAsync for batches
This early return changes the behavior of main, where applyBatchAsyncInsertSettings always writes the WithAsync settings, as asyncInsert does for Exec.
WithSettings{"async_insert": 1}+WithAsync(false):mainsendswait_for_async_insert=0. This PR sends nowait_for_async_insert, so the server waits for the flush.WithSettings{"async_insert": 0}+WithAsync(true):mainsends an async batch. This PR sends a sync batch, whileExecwith the same context stays async.- This check is not necessary for the opt-in. Move it below the
o.async.okblock, then update thequery setting wins over WithAsyncunit test and the two docs notes.
- if o.settings != nil {
- if _, ok := o.settings["async_insert"]; ok {
- return
- }
- }
if o.async.ok {
...
return
}
+ if _, ok := o.settings["async_insert"]; ok {
+ return
+ }There was a problem hiding this comment.
fixed by removing the helper in be7583d. the diff now touches no go file outside tests/, so applyBatchAsyncInsertSettings writes the WithAsync settings as it does on main.
| require.NoError(t, conn.QueryRow(ctx, fmt.Sprintf("SELECT count() FROM %s", table)).Scan(&stored)) | ||
| require.Equal(t, uint64(10), stored) | ||
|
|
||
| if err := conn.Exec(ctx, "SYSTEM FLUSH LOGS"); err != nil { |
There was a problem hiding this comment.
system.query_log from all replicas on ClickHouse Cloud
On ClickHouse Cloud the INSERT and the log query can go to different replicas, so uniqExact(query_id) can return 0 and the test is flaky.
- When
clickhouse_tests.CloudClickHouseis true, useSYSTEM FLUSH LOGS ON CLUSTER 'default'andclusterAllReplicas('default', system.query_log), astests/issues/issue_2067_test.godoes. - The same change is necessary in the HTTP test and the
database/sqltest in this file. A shared helper for the flush and the two assertions removes the three copies. t.Skipfon aSYSTEM FLUSH LOGSerror hides a failure of the main assertion. Userequire.NoError.
There was a problem hiding this comment.
fixed in b2cd7b5. one helper replaces the three copies; on cloud it runs SYSTEM FLUSH LOGS ON CLUSTER 'default' and reads clusterAllReplicas('default', system.query_log), and polls until the insert row is in the log, as issue_2067_test.go does.
| // regression: httpBatch.Send also honors Options.SyncBatchInsert. | ||
| func TestIssue1997_HTTPInsertSingleQueryID(t *testing.T) { | ||
| env, err := clickhouse_tests.GetTestEnvironment("issues") | ||
| require.NoError(t, err) |
There was a problem hiding this comment.
These hand-made options always use env.HttpPort without TLS, so the test cannot connect when CLICKHOUSE_USE_SSL=true (ClickHouse Cloud).
- Build the options with
clickhouse_tests.ClientOptionsFromEnv(env, clickhouse.Settings{}, true), then setSyncBatchInsert = true. This function selects the HTTPS port and the TLS configuration, and it does not addasync_insert. - The
database/sqltest covers only the native protocol. Add an HTTP DSN case if the option stays.
There was a problem hiding this comment.
fixed in b2cd7b5. the options come from ClientOptionsFromEnv, so HTTP gets the HTTPS port and TLS on cloud. the database/sql test runs on native and HTTP now.
| case "sync_batch_insert": | ||
| syncBatch, err := strconv.ParseBool(params.Get(v)) | ||
| if err != nil { | ||
| return fmt.Errorf("clickhouse [dsn parse]: sync_batch_insert: %s", err) |
There was a problem hiding this comment.
💡 Nit — nit: wrap the parse error with %w
%s drops the error chain. The adjacent http_proxy case already uses %w.
| return fmt.Errorf("clickhouse [dsn parse]: sync_batch_insert: %s", err) | |
| return fmt.Errorf("clickhouse [dsn parse]: sync_batch_insert: %w", err) |
There was a problem hiding this comment.
no longer applies, the sync_batch_insert dsn key is gone with the option in be7583d.
…loud-safe The tests set async_insert=0 themselves, with Options.Settings or with WithSettings on the context of the batch. They no longer use Options.SyncBatchInsert. They cover the Native and HTTP protocols, for the ClickHouse API and for database/sql. The test suite sets async_insert=0 on its connections, so the tests remove it from the client settings first. On ClickHouse Cloud, the tests flush and read system.query_log on all replicas. One helper replaces the three copies of the log check. It fails the test when the flush fails, and it polls until the row of the INSERT is in the log. The client options come from ClientOptionsFromEnv, so the HTTP cases use the TLS port and the settings of the test suite.
async_insert=0 in Options.Settings, or in WithSettings on the context of the batch, gives the same result. The new option is not necessary. Remove the option, the sync_batch_insert DSN key, the unit test, and the notes in the docs. The option was never in a release. This restores applyBatchAsyncInsertSettings in the Native and HTTP batch code. WithAsync overrides a query-level async_insert setting again, as it did before this PR.
ClickHouse 26.2 and later runs inserts as async inserts by default. An async insert adds a second INSERT row to system.query_log, with the query_id of the server flush. Explain this in the batch insert sections of the ClickHouse API page and the database/sql page. Show how to get one query_id: set async_insert=0 in Options.Settings, or in WithSettings on the context of the batch.
Refs #1997
What this PR does
A batch insert can show two
INSERTrows insystem.query_log. From ClickHouse 26.2,async_insertis on by default. The first row is theINSERTof the client, with thequery_idof the client. The second row is the flush of the server, with a newquery_id. Only one write reaches the table. Thequery_idandflush_query_idcolumns ofsystem.asynchronous_insert_loglink the two IDs.The client cannot set the
query_idof the flush. To get onequery_idfor a batch, setasync_insert=0inOptions.Settings, or inclickhouse.WithSettingson the context ofPrepareBatch. The driver already sends the settings and the query ID, so this PR does not change the behavior of the driver.docs/clickhouse-api.mdxanddocs/database-sql-api.mdx.tests/issues/issue_1997_test.gosetsasync_insert=0in both ways, on the Native and HTTP protocols, for the ClickHouse API and fordatabase/sql. On ClickHouse Cloud the tests flush and readsystem.query_logon all replicas.An earlier version of this PR added
Options.SyncBatchInsertand async_batch_insertDSN key. This version removes both, becauseasync_insert=0in the settings gives the same result. #1848 is open for an API that disables async inserts in general.Validation
go build ./...,gofmtandgo test -c ./tests/issuespass locally.tests/issuestests need a ClickHouse server and have not run on this version yet. CI is their first run.AI coding tools assisted with the change and this description.