Repository navigation
Open
Labels
incorrect usagequeriesArea: how the client builds and runs statements (parameters, quoting, batches)
Description
Observed
- A single
PrepareBatch+Send()call from the Go native client — with an explicit, knownquery_idset viaclickhouse.WithQueryID— produces twoQueryStart/QueryFinishpairs insystem.query_log, each with a distinctquery_idand both withis_initial_query = 1. - The server-generated second entry rewrites the query: the client sent
INSERT INTO dup_test_events (id, value) FORMAT Native; the server loggedINSERT INTO vts_dup_test3.dup_test_events (id, value) FORMAT Native— fully qualifying the table name and altering formatting — under a new server-assigned UUID. - Physical storage (
system.parts) on a plainMergeTreetable (no deduplication mechanism) shows exactly the expected row count (10 rows, not 20), proving only one write reached storage despite two log entries each claimingwritten_rows = 10.
Expected behaviour
Exactly one QueryStart/QueryFinish pair per client Send() call, carrying the client-supplied query_id. The server should not generate an additional top-level query_log entry with a rewritten query string and a new UUID for the same physical insert.
Code example
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
)
const insertDupTestEvents = `INSERT INTO dup_test_events
(
id,
value
)`
const explicitQueryID = "vts-explicit-query-id-test-0001"
func main() {
options := &clickhouse.Options{
Addr: []string{"127.0.0.1:9000"},
Auth: clickhouse.Auth{
Database: "vts_dup_test3",
Username: "vts",
Password: "vts123",
},
Compression: &clickhouse.Compression{Method: clickhouse.CompressionLZ4},
DialTimeout: 5 * time.Second,
MaxOpenConns: 5,
MaxIdleConns: 5,
ConnMaxLifetime: 10 * time.Minute,
}
conn, err := clickhouse.Open(options)
if err != nil {
log.Fatalf("open: %v", err)
}
defer conn.Close()
ctx := clickhouse.Context(context.Background(), clickhouse.WithQueryID(explicitQueryID))
if err := conn.Ping(ctx); err != nil {
log.Fatalf("ping: %v", err)
}
batch, err := conn.PrepareBatch(ctx, insertDupTestEvents)
if err != nil {
log.Fatalf("prepare batch: %v", err)
}
defer batch.Close()
for i := 0; i < 10; i++ {
if err := batch.Append(uint64(i), fmt.Sprintf("row-%d", i)); err != nil {
log.Fatalf("append row %d: %v", i, err)
}
}
if err := batch.Send(); err != nil {
log.Fatalf("send: %v", err)
}
fmt.Printf("sent with explicit query_id=%s\n", explicitQueryID)
}After running, query:
SELECT
query_id,
initial_query_id,
is_initial_query,
type,
written_rows,
query
FROM system.query_log
WHERE (query ILIKE '%dup_test_events%') AND (query ILIKE '%INSERT%')
ORDER BY event_time_microseconds ASC
FORMAT Vertical;And verify physical row count:
SELECT sum(rows)
FROM system.parts
WHERE database = 'vts_dup_test3'
AND table = 'dup_test_events'
AND active = 1;Error log
No error returned to the client. The anomaly is silent —
batch.Send() returns nil. The duplicate entry is only
visible in system.query_log.
Details
Environment
clickhouse-goversion: v2.40.3- Interface: ClickHouse API (
clickhouse.Open, native protocol) - Go version: (please fill in)
- Operating system: (please fill in)
- ClickHouse version: 26.7.1.1315
- Is it a ClickHouse Cloud? No (self-hosted single node)
- ClickHouse Server non-default settings, if any: None relevant; isolated test database, no replicas, no distributed tables, no materialized views
CREATE TABLEstatement:
CREATE DATABASE IF NOT EXISTS vts_dup_test3;
CREATE TABLE vts_dup_test3.dup_test_events
(
id UInt64,
value String
)
ENGINE = MergeTree()
ORDER BY id;- Sample data: 10 synthetic rows (
id0–9,value"row-0"…"row-9"), no obfuscation needed.
Metadata
Metadata
Assignees
Labels
incorrect usagequeriesArea: how the client builds and runs statements (parameters, quoting, batches)
Activity
margasystems commentedon Aug 31, 2026
Update with additional findings since filing this:
1. Confirmed independent of the client library version. Reproduces identically on both
v2.40.3(originally reported) and the latestv2.48.0— same two-query_idpattern, same row counts, same query-text difference. Ruling out a client-side fix.2. Confirmed the client only ever issues one request. Setting an explicit
query_idviaclickhouse.Context(ctx, clickhouse.WithQueryID("my-id"))before the singlePrepareBatch/Send()call shows that chosen ID lands on exactly one of the two logged entries (the unqualified, literal-text one). The other entry gets its own independently-generated UUID that the client never created or passed anywhere — proving the second entry isn't a second client-issued request, but something generated server-side.3. Bisected across ClickHouse server versions with the identical minimal repro (
PrepareBatch/Append(10 rows)/Send(), plainMergeTree, no replicas/distributed tables):query_id?24.8.14.39-ltsquery_idper insert25.3.14.14-ltsquery_idper insert26.3.27.3-lts26.7.1.1315-stableSo this looks like a regression introduced somewhere between the
25.3and26.3server releases, not something specific to a single non-LTS build — it affects the current LTS line (26.3) too. Given (2) above, I'd guess this points to a server-side query-logging change in that range rather than anything in the Go client, though I don't have visibility into the ClickHouse server source to confirm exactly what changed.Given
system.partsconclusively shows only the true row count ever lands on disk (no actual duplicate write, confirmed on a non-deduplicatingMergeTreetable across every version tested), this looks like a query-log bookkeeping/instrumentation issue rather than a data-integrity risk — but still worth fixing since it silently doubles what shows up insystem.query_logmetrics/monitoring built on top of it.fix: keep a single query_id for native batch INSERT (ClickHouse#1997)
fix: keep a single query_id for native batch INSERT (ClickHouse#1997)
fix: honor WithAsync on PrepareBatch and cover HTTP ClickHouse#1997
test: set async_insert=0 in the ClickHouse#1997 tests and make them C…
polyglotAI-bot commentedon Nov 13, 2026
Recommendation: Close — no-op: works as intended
The second row is the flush of a server-side async insert: ClickHouse turned on
async_insertby default in 26.2, and the client sends only one query. PR #2000 adds a note about this to the batch-insert docs; it can be reviewed separately, and it usesRefs, so it does not close this issue.main: Not a bug —7188b645, ClickHouse 26.9.1; the server default shows two rows,async_insert=0shows one row, and an HTTPINSERTwithcurl(no client) also shows two rows.WithAsyncwhen the server default is async), HTTP PrepareBatch sends the user's query_id with its internal DESCRIBE TABLE, risking QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING #2033 (HTTPPrepareBatchand thequery_idof itsDESCRIBE TABLE).incorrect usageAnalysis:
system.settings_changeson 26.9.1 has the row26.2: async_insert 0 → 1, "Enable async inserts by default.", which agrees with the bisect in the issue (25.3 clean, 26.3 shows it). Withasync_insert=1, the server puts the rows of the clientINSERTin a buffer and writes them later in a flush query. The server logs the flush withquery_kind = AsyncInsertFlush, a newquery_id, and the table name with the database name.system.asynchronous_insert_loglinks the two IDs (query_id→flush_query_id), and the table gets each row once. The client sends oneQuerypacket with the caller'squery_id(conn_send_query.go:20) and cannot set thequery_idof the flush. To get onequery_logrow for a batch, setasync_insert=0inOptions.Settingsor withclickhouse.WithSettingson the context ofPrepareBatch; to count inserts insystem.query_log, filter onquery_kind.Reproduction: complete program and output
Output on
main(7188b645), ClickHouse 26.9.1:The same
INSERTover HTTP withcurl, without the client:Automated triage by Polyglot AI. Classification added on 2026-11-19. A maintainer makes the final decision.