Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Created October 3, 2025 09:50
Show Gist options
  • Save kavirajk/9d2a74dc70c325d260d4b836ea2e2fcc to your computer and use it in GitHub Desktop.
Save kavirajk/9d2a74dc70c325d260d4b836ea2e2fcc to your computer and use it in GitHub Desktop.
withstdasync_sqldb.go
func TestWithStdAsyncDB(t *testing.T) {
conn := clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{"localhost:9000"},
Protocol: clickhouse.Native,
})
ctx := context.Background()
queryID := fmt.Sprintf("%d", time.Now().Unix())
// queryID := "async-with-opendb"
ctx = clickhouse.Context(ctx, clickhouse.WithStdAsync(true), clickhouse.WithQueryID(queryID))
_, err := conn.ExecContext(ctx, `DROP TABLE IF EXISTS example`)
require.NoError(t, err)
const ddl = `
CREATE TABLE example (
Col1 UInt64
, Col2 String
, Col3 Array(UInt8)
, Col4 DateTime
) ENGINE = Memory
`
_, err = conn.ExecContext(ctx, ddl)
require.NoError(t, err)
for i := 0; i < 100; i++ {
_, err := conn.ExecContext(ctx, `INSERT INTO example VALUES (?, ?, ?, now())`, i, "Golang", []uint8{1, 2, 4})
require.NoError(t, err)
}
fmt.Println("QueryID", queryID)
}
@kavirajk
Copy link
Author

kavirajk commented Oct 3, 2025

If you explore this query via systems.query_log, we can verify it worked with async settings.

d280839474d7 :) select query, Settings from system.query_log where query_id='1759484788' ORDER BY event_time desc LIMIT 10;

SELECT
    query,
    Settings
FROM system.query_log
WHERE query_id = '1759484788'
ORDER BY event_time DESC
LIMIT 10

Query id: 1d828408-7529-4c4e-93ad-1a160393dbcd

    ┌─query───────────────────────┬─Settings─────────────┐
 1. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
 2. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
 3. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
 4. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
 5. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
 6. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
 7. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
 8. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
 9. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
10. │ INSERT INTO example VALUES  │ {'async_insert':'1'} │
    └─────────────────────────────┴──────────────────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment