ClickHouse Knowledge Patch
Use this skill when writing, reviewing, upgrading, or operating ClickHouse SQL and server configurations. Start with the compatibility-sensitive items below, then load the topic reference that matches the task.
How to apply this skill
- Inspect the target server version, table engines, client settings, and relevant feature flags before generating SQL.
- For upgrades, review changed defaults and removed compatibility paths before adopting new syntax.
- Treat experimental features as explicit opt-ins and keep their configuration easy to remove.
- For distributed or object-storage work, distinguish query settings, engine settings, server settings, named collections, and access-control grants.
- Validate optimizer choices with
EXPLAIN; do not assume a projection, index, cache, or parallel-replica path is selected merely because it exists. - Preserve operational safety around background work, credential inheritance, deduplication, and remote-query execution.
Reference index
| Reference | Topics |
|---|---|
| Upgrades, security, and settings | Credential inheritance, removed settings, deduplication migration, changed defaults, access changes, terminal output |
| Query language and functions | CTEs, EXPLAIN, time functions, aggregates, query parameters, dictionary lookup, hashing, JSON extraction |
| Tables, indexes, and views | MergeTree, projections, text and hypothetical indexes, semi-structured types, constraints, refresh chains |
| External data and formats | Object storage, lakehouse catalogs, URL dispatch, remote databases, Arrow Flight, GeoJSON, PNG |
| Operations, integrations, and tooling | Background controls, workloads, Kafka and NATS, Keeper, dictionaries, PromQL, local listeners, streaming |
| Vectors, geometry, and classifiers | QBit, quantization, Hadamard transforms, embedding generation, Naive Bayes, geometry and vector tiles |
Breaking and security-sensitive changes
Do not assume user queries inherit server cloud credentials
User SQL that accesses S3 must supply credentials or use NOSIGN. Named collections default use_environment_credentials to 0. Restoring environment lookup requires both:
use_environment_credentials = 1
s3_allow_server_credentials_in_user_queries = 1
The second setting is off by default. Persistent S3-backed objects denied server credentials load anonymously during startup or RESTORE while s3_load_table_anonymously_if_credentials_restricted remains enabled.
Complete the insert-deduplication migration before upgrading
Only the unified insert hash is accepted. A server configured with old_separate_hashes or compatible_double_hashes fails at startup. Before the final upgrade from a legacy setting:
- Run an intermediate release with
compatible_double_hashes. - Keep it active for at least
replicated_deduplication_window_secondson replicated tables, or fornon_replicated_deduplication_windowinserts on non-replicated tables. - Remove
insert_deduplication_versionor set it tonew_unified_hash.
Replace removed and changed SQL behavior
- Replace
snowflakeToDateTime,snowflakeToDateTime64,dateTimeToSnowflake, anddateTime64ToSnowflakewith the corresponding names containingSnowflakeID; the deprecated-function switch no longer restores the old names. toTimereturnsTimewith the defaultuse_legacy_to_time = 0. UsetoTimeWithFixedDateor explicitly enable the legacy behavior when fixed-date output is required.- Use only
hasColumnInTable(database, table, column); the remote overload with host and credentials is gone. DateTime64has a much wider calendar range, but precisions 8 and 9 remain bounded byInt64ticks and nanosecond precision still tops out at2262-04-11.
Update server and table configuration
- Server-config
resourcesandworkload_classifierssections are ignored with a warning. Define scheduling throughCREATE RESOURCEandCREATE WORKLOAD. AggregatingMergeTreerejects ordinary columns that are neither sorting-key dimensions nor aggregate-state measures. Useallow_dimensions_outside_sorting_key = 1only as a deliberate compatibility escape hatch.- Asynchronous inserts are enabled by default; verify latency, acknowledgment, buffering, and deduplication assumptions in small-insert clients.
- Direct object-storage backups do not accept
ziporzipx; choose a tar-based archive such astar.gz.
Query planning and indexing essentials
Measure plans, not just estimates
EXPLAIN ANALYZE SELECT ... executes the query and adds runtime metrics to the standard plan. For static inspection, pretty=1 renders a tree and compact=1 collapses expression steps:
EXPLAIN indexes=1, pretty=1, compact=1
SELECT number FROM numbers(10);
Reuse expensive CTEs
Enable enable_materialized_cte, then force one evaluation into a temporary table:
WITH expensive AS MATERIALIZED (
SELECT key, sum(value) AS total FROM source GROUP BY key
)
SELECT * FROM expensive AS a JOIN expensive AS b USING (key);
Test and target data skipping
- Filtered projections may include
WHERE; the optimizer can use one when the query predicate implies the projection predicate. CREATE HYPOTHETICAL INDEXplusEXPLAIN WHATIFevaluates a session-local skip index without persisting it. Candidate construction reads data and counts against limits and quotas.- A granularity-1 MinMax index can be generated for every numeric column with
add_minmax_index_for_numeric_columns = 1. - Text indexes require an explicit tokenizer and
enable_full_text_index = 1; token postprocessors can normalize tokens after tokenization.
External data and lakehouse essentials
Route URLs by scheme deliberately
The URL engine and url function dispatch file://, standard S3-family, Azure, and hdfs:// schemes to specialized implementations after applying url_base; HTTP(S) stays on URL. Non-default S3-compatible schemes still require the s3 engine or function.
Use remote queries as read-only sources
The mysql, postgresql, and sqlite engines and table functions accept (SELECT ...) or query('SELECT ...') instead of a table name. ClickHouse infers the result structure, and the resulting source is read-only.
Account for object-storage path and access behavior
data/**/file.txtnow matchesdata/file.txtas well as deeper paths.extra_credentials(role_arn = '...')selects an IAM role fors3; grants can be scoped to anS3('url-pattern')resource.partition_strategy = 'hive'plusPARTITION BYwrites Hive-style directories.- Parallel replicas can automatically distribute cluster file processing; set
parallel_replicas_for_cluster_engines = 0to opt out.
Operations and streaming essentials
Control background activity consistently
Use engine-agnostic SYSTEM STOP, START, PAUSE, CANCEL, and REFRESH, including ALL BACKGROUND forms, for queue engines and refreshable views. Treat these as service-impacting commands.
For NATS JetStream, acknowledgment follows successful insertion. nats_wait_for_flush_interval keeps consumption open through a flush interval, while nats_commit_on_select makes direct reads acknowledge consumed messages.
Define workload memory through SQL
CREATE RESOURCE memory (MEMORY RESERVATION);
Workload hierarchies accept max_memory limits and weight shares; route queries with SETTINGS workload = 'name'.
Use local and continuous execution intentionally
- In
clickhouse-local,SYSTEM START LISTEN TCPandSYSTEM START LISTEN HTTPexpose the current session; stop each listener explicitly when finished. - With
enable_streaming_queries = 1,SELECT ... STREAMremains open for new rows. ACURSORcan resume fromblock_numberandblock_offset. QueryRunnerexecutes queries represented by inserted rows, which suits generated batches, benchmarks, fuzzing, routing, and shadow traffic.
Common SQL additions
Typed settings parameters
SELECT count()
FROM events
SETTINGS max_threads = {threads:UInt64};
SET max_threads = {threads:UInt64};
Tuple aggregation and time-zone postfixes
SELECT sumTuple((a, b)) FROM t;
SELECT ts AT TIME ZONE 'UTC', ts AT LOCAL FROM t;
The -Tuple combinator aggregates corresponding tuple elements independently and preserves element names. Multi-argument aggregates pair positions across input tuples.
Schema changes without restating definitions
ALTER TABLE events
MODIFY CONSTRAINT IF EXISTS positive_value CHECK value > 0;
ALTER TABLE events
MODIFY COLUMN kind ADD ENUM VALUES('archived' = 5);
Preserve unrelated access settings
ALTER USER, ALTER ROLE, and ALTER SETTINGS PROFILE accept SET name = value as an alias for MODIFY SETTING name = value. Unlike a bare SETTINGS clause, this form leaves all other settings intact.
Vector and geometry guardrails
QBit(Int8)stores quantized vectors; stridedQBit(T, dimension, stride)separates dimension groups into streams so transposed searches can read only an initialused_dimssubset.- Transposed and quantized distance functions are approximate. Keep the original precision available when recall or exact reranking matters.
randomHadamardTransformis deterministic for a fixed seed and preserves norm; optional output truncation provides random projection before quantization.- Experimental embedding functions require an explicit server-side credential collection and
allow_experimental_ai_functions = 1. geometryIntersectCartesianandgeometryIntersectSphericalaccept differing supported geometry types, including the genericGeometrytype.
Open the indexed references for complete syntax, settings, defaults, feature gates, and edge behavior before implementing a production change.