clickhouse-go v2.x. For a guide with code examples, see Configuration.
Version annotationsOptions added in
clickhouse-go v2.35.0 or later are marked with (Since vX.Y.Z) next to their description. Options without a “Since” tag have been available since v2.0 and are present in every supported release.How options are set
Options exist at three scopes:
Where scopes overlap, the more specific scope wins: Batch > Query > Connection. For
Settings, query-level keys are merged with connection-level keys and query-level wins on conflict.
Via Options struct:
Connection options
Protocol and connection
Authentication
Timeouts
A ClickHouse Cloud service that has been idle is paused, and wakes on the first incoming connection. Wake-up typically takes a few tens of seconds, during which the dial blocks. If
DialTimeout is lower than the wake time, the first query after an idle period fails with a dial timeout instead of running.Set DialTimeout to 1m–2m for clients that may be the first to reach an idled service. The tradeoff is slower failure detection during a real outage — dials block for the full timeout before erroring — so prefer scoping the higher timeout to the first connect, or use context deadlines on individual queries to cap end-to-end latency.Connection pool
database/sql only
ConnMaxIdleTime is a standard Go database/sql pool setting. It isn’t available in the clickhouse.Options struct or via clickhouse.Open(). Set it after OpenDB():Standard database/sql pool settings
When usingclickhouse.OpenDB() or sql.Open("clickhouse", dsn), the returned *sql.DB supports Go’s standard pool methods. OpenDB() auto-applies the first three from Options:
ClickHouse API (clickhouse.Open)These methods are not available on the connection returned by
clickhouse.Open(). The ClickHouse API manages its own pool internally using the Options struct fields directly.Compression
Compression method support by protocol:
TLS
See TLS for code examples.
Logging
Buffers and memory
HTTP-specific
Two-layer HTTP poolingWhen using HTTP, there are two connection pools:
- Layer 1 (application):
MaxIdleConns/MaxOpenConns— controlshttpConnectobjects - Layer 2 (transport):
HttpMaxConnsPerHost— controls underlying TCP connections
HttpMaxConnsPerHost.Advanced connection
Client information
ClickHouse server settings
Common settings:
Context-level query options
Set per-query usingclickhouse.Context():
Context deadline behaviorIf the context has a deadline > 1s,
max_execution_time is automatically set to seconds_remaining + 5. This overrides any manually set value.Batch options
Passed toPrepareBatch(). Import: github.com/ClickHouse/clickhouse-go/v2/lib/driver.
Quick reference tables
Connection pool sizing recommendations
Timeout recommendations
DSN parameter quick reference
Troubleshooting
Connection pool exhausted: “acquire conn timeout”
Cause: Connection pool exhausted - allMaxOpenConns connections are in use and none became available within DialTimeout.
Fix
Try the following steps in order, and diagnose the root cause before tuning knobs:
- Check for long-running queries holding connections:
SELECT query_id, elapsed FROM system.processes ORDER BY elapsed DESC. If found, address the slow queries first. - If you run long-lived batches (minutes/hours between
PrepareBatch()andSend()), useWithReleaseConnection()to return the connection to the pool while the batch is open. - Increase
MaxOpenConnsto match observed concurrency. - Increase
DialTimeoutonly if bursts are expected and acquisition wait is the actual bottleneck.
Read timeout and connection reset errors
Cause:ReadTimeout exceeded while waiting for a server response, or the connection was closed by the server/network.
Fix:
- Increase
ReadTimeoutfor long-running queries - Use context deadlines for per-query timeout control
- Check ClickHouse server-side
max_execution_timelimits
“Code: 516. Authentication failed”
Cause: Wrong username, password, or the user doesn’t exist. Fix:- Verify credentials against
system.userstable - Check for URL-encoding issues with special characters in DSN passwords
- Confirm the user has access to the specified database
TLS certificate errors
Gradual memory growth
Cause: Large idle connection buffers accumulating. Fix:- Set
FreeBufOnConnRelease: truein memory-constrained environments - Reduce
MaxIdleConnsto limit idle connections - Reduce
MaxCompressionBufferif using compression - Lower
ConnMaxLifetimeto cycle connections more frequently