clickhouse-migrations
Skill721 starsChanged 2 months agoMITView on GitHub
ClickHouse migration patterns and rules. Use when creating or modifying ClickHouse migrations.
- Deletes or force-pushes
1 other project uses a version of this.
Adopted by
Most recently taken up by
- PostHog/posthogword for word, first seen Oct 4, 2026
Other versions1 project uses a version of this.
Word for word · 1
- PostHog/posthog★ 40k100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository119 other files it gives its agents.
- AGENTS.mdAGENTS.md
- common/AGENTS.mdAGENTS.md
- proto/AGENTS.mdAGENTS.md
- rust/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- common/CLAUDE.mdCLAUDE.md
- proto/CLAUDE.mdCLAUDE.md
- rust/CLAUDE.mdCLAUDE.md
- .cursor/rules/django-python.mdcCursor rule
- .cursor/rules/react-typescript.mdcCursor rule
- .cursor/rules/rust.mdcCursor rule
- adding-activity-loggingSkill
- adding-api-scopesSkill
- adding-inbound-webhooksSkill
- adding-inbox-sourcesSkill
- adding-ingestion-warningsSkill
- adding-mcp-store-serversSkill
- adding-personhog-rpcSkill
- adding-product-alertingSkill
- adding-project-secret-api-key-authSkill
- adopting-generated-api-typesSkill
- analyzing-experiment-precompute-canarySkill
- analyzing-experiment-query-performanceSkill
- analyzing-insights-across-teamsSkill
- announcing-behavior-changesSkill
- auditing-llm-gateway-paritySkill
- auditing-warehouse-source-coverageSkill
- authenticating-to-clickhouseSkill
- authoring-ci-workflowsSkill
- autoresolving-pr-conflictsSkill
- building-product-empty-statesSkill
- debugging-ci-failuresSkill
- debugging-local-replaySkill
- debugging-local-task-agent-runsSkill
- debugging-mcp-analyticsSkill
- debugging-signals-pipelineSkill
- debugging-table-access-deniedSkill
- depot-ciSkill
- depot-container-buildsSkill
- depot-github-runnersSkill
- developing-subscriptionsSkill
- django-migrationsSkill
- django-startup-timeSkill
- documenting-warehouse-sourcesSkill
- editing-agents-mdSkill
- establishing-code-ownershipSkill
- extending-hobby-smoke-testsSkill
- extending-personhog-test-harnessSkill
- finding-llm-gateway-migration-candidatesSkill
- fixing-flaky-testsSkill
- gating-production-deploysSkill
- gating-sensitive-actionsSkill
- generating-clickhouse-query-performance-reportsSkill
- hogliSkill
- implementing-mcp-toolsSkill
- implementing-mcp-ui-appsSkill
- implementing-warehouse-sourcesSkill
- improving-drf-endpointsSkill
- improving-mcp-toolsSkill
- ingestion-pipeline-doctor-nodejsSkill
- The first 60 shown.
The file
Show its rules and text
We split this skill into the rules it gives an agent
15 rules. About 0 are common, 3 are distinctive, and 12 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
12
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 8
Read posthog/clickhouse/migrations/AGENTS.md for comprehensive patterns, cluster setup, examples, and ingestion layer details.
- Very specific: line 27
Only the members of NodeRole in posthog/clickhouse/client/connection.py are valid: ALL, DATA, INGESTION_EVENTS, INGESTION_SMALL, …
- Very specific: line 82
NEVER use ON CLUSTER clause in SQL statements
- Very specific: line 83
Always use IF EXISTS / IF NOT EXISTS clauses
- Very specific: line 84
When dropping and recreating replicated table in same migration, use DROP TABLE IF EXISTS ...
- Very specific: line 85
If a function generating SQL has on_cluster param, always set on_cluster=False
- Very specific: line 86
Use sharded=True when altering sharded tables
- Very specific: line 87
Use is_alter_on_replicated_table=True when altering non-sharded replicated tables
- Distinctive: line 100
If you need both a schema change and application code that uses the new schema, ship the migration first in its own PR and merge it before …
- Distinctive: line 104
No table should exist only in the cloud: Every table created via migration must also exist in a local dev environment.
- Very specific: line 126
Call run_mode() where you need it rather than assigning a module-level constant.
- Very specific: line 128
If you create a new table inside such a guard, you must also add its SQL function to posthog/clickhouse/schema.py in the appropriate tuple …
- Distinctive: line 151
After writing the migration:
- Very specific: line 160
Read posthog/clickhouse/hcl/README.md before editing a layer — it covers which layer to pick, abstract column lists, and the codegen path …
- Very specific: line 165
Delete entry from infi_clickhouse_orm_migrations table to re-run a migration.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
ClickHouse Migrations
Read posthog/clickhouse/migrations/AGENTS.md for comprehensive patterns, cluster setup, examples, and ingestion layer details.
Quick reference
Migration structure
operations = [
run_sql_with_exceptions(
SQL_FUNCTION(),
node_roles=[...],
sharded=False, # True for sharded tables
is_alter_on_replicated_table=False # True for ALTER on replicated tables
),
]Node roles (choose by the cluster that owns the object)
Only the members of NodeRole in posthog/clickhouse/client/connection.py are valid: ALL, DATA,
INGESTION_EVENTS, INGESTION_SMALL, INGESTION_MEDIUM, ENDPOINTS, LOGS, AI_EVENTS, AUX,
BATCH_EXPORTS, OPS, SESSIONS. A name that is not in that enum fails at import, which aborts
migration discovery and takes every job that runs migrations down with it.
Pick the role by which cluster owns the object, not by its type alone:
[NodeRole.DATA]: anything on the main cluster — sharded tables, non-sharded replicated tables, distributed read tables, views, dictionaries. The default, and right for most migrations.[NodeRole.INGESTION_SMALL]: writable tables, Kafka tables, materialized views on the ingestion layer[NodeRole.OPS],[NodeRole.LOGS],[NodeRole.AUX],[NodeRole.AI_EVENTS],[NodeRole.SESSIONS],[NodeRole.BATCH_EXPORTS]: objects that live on a satellite cluster. A table for one of those onDATAlands on the wrong nodes and leaves the intended cluster without it, so check where the object is read and written before defaulting toDATA. Dev runs the same satellite clusters as US/EU prod — never branch onCLOUD_DEPLOYMENTto give dev a different layout.[NodeRole.ALL]: rarely used
Table engines quick reference
MergeTree engines:
AggregatingMergeTree(table, replication_scheme=ReplicationScheme.SHARDED)for sharded tablesReplacingMergeTree(table, replication_scheme=ReplicationScheme.REPLICATED)for non-sharded- Other variants:
CollapsingMergeTree,ReplacingMergeTreeDeleted
Distributed engine:
- Sharded:
Distributed(data_table="sharded_events", sharding_key="sipHash64(person_id)") - Non-sharded:
Distributed(data_table="my_table", cluster=settings.CLICKHOUSE_SINGLE_SHARD_CLUSTER)
Kafka engine — kafka_engine() returns the ENGINE clause only, so every table appends its own
SETTINGS. Start from this baseline:
SETTINGS kafka_skip_broken_messages = 100,
kafka_num_consumers = {kafka_num_consumers(1)},
kafka_thread_per_consumer = 1,
kafka_poll_timeout_ms = 10000,
kafka_max_block_size = 100000kafka_skip_broken_messagesdefaults to 0, where one malformed message stops the consumer for good.kafka_poll_timeout_msis 10000 for WarpStream, which does not supportfetch.min.bytes.kafka_max_block_sizedefaults to about a million rows, so 100000 is a reduction; lower for wide rows, not below ~1000.kafka_num_consumersis per node, so the role's node count multiplies it; the total must not exceed the topic's partitions.- Columns and settings are fixed at creation: drop and recreate the table with its MV, no
SYNC.ALTER TABLE <mv> MODIFY QUERYcovers an MV-SELECT-only change. Never forkafka_events_json_ws/events_json_ws_mv— see Critical rules.
posthog/clickhouse/migrations/AGENTS.md has the sizing rules and the rest.
Critical rules
- NEVER use
ON CLUSTERclause in SQL statements - Always use
IF EXISTS/IF NOT EXISTSclauses - When dropping and recreating replicated table in same migration, use
DROP TABLE IF EXISTS ... SYNC - If a function generating SQL has on_cluster param, always set
on_cluster=False - Use
sharded=Truewhen altering sharded tables - Use
is_alter_on_replicated_table=Truewhen altering non-sharded replicated tables - Never write
CODEC(ZSTD(1))on a column — the server already compresses every column with ZSTD, so it buys nothing. Declare a CODEC only where it beats that default, and check theORDER BYfirst:Delta/DoubleDeltaneed the column near-sorted in storage order (a leading sort-key prefix), and lose on a column the key only buckets or omits.T64/Gorillaare ordering-independent. Put it on the storage table only — a CODEC on a Distributed or Kafka table is inert metadata that drifts from the sharded table it fronts. - Never write a
DROP COLUMNmigration yourself —DROP COLUMNcan get stuck in ClickHouse and block releases. Column removal is a two-step process: (1) the ClickHouse team drops the column directly on the cluster, then (2) you write a migration with the matchingDROP COLUMNso the codebase schema stays in sync. Never initiate the drop from a migration without the ClickHouse team having done step 1 first. - Never drop or recreate
kafka_events_json_wsorevents_json_ws_mv— these tables are a no-go zone. The MV definition differs significantly between US prod, EU prod, and dev (dozens of environment-specificmat_*columns) and those differences are not reflected in the repo. Dropping and recreating from repo SQL would destroy the environment-specific schema and break event ingestion. Any change must go through the ClickHouse team.
PR scope
A PR that contains a ClickHouse migration must be migration-only. Do not mix migration files with feature code, API changes, model changes, or frontend changes in the same PR. Migration-related files are:
- The migration file itself (
posthog/clickhouse/migrations/0NNN_*.py) - SQL definition files the migration depends on (e.g.
posthog/clickhouse/sql/*.py, table engine helpers) - Tests that directly exercise the migration or the SQL definitions it touches
If you need both a schema change and application code that uses the new schema, ship the migration first in its own PR and merge it before the application-code PR.
Local setup parity
No table should exist only in the cloud. Every table created via migration must also exist in a local dev environment.
Some migrations are cloud-guarded and skipped in local/hobby dev. Gate on posthog.run_mode, never on settings.CLOUD_DEPLOYMENT directly, because the clickhouse-migrations-use-run-mode semgrep rule blocks raw comparisons:
from posthog.run_mode import RunMode, run_mode
operations = (
[]
if not run_mode().is_deployed_cloud # US/EU/DEV
else [...]
)| Predicate | True for |
run_mode().is_deployed_cloud | US, EU, DEV (the usual migration gate) |
run_mode().is_prod_cloud | US, EU (excludes staging) |
run_mode() is RunMode.CLOUD_US | one region (CLOUD_EU, CLOUD_DEV likewise) |
run_mode().is_cloud also counts E2E, so it is wrong for a migration. That one matches posthog.cloud_utils.is_cloud.
Call run_mode() where you need it rather than assigning a module-level constant. posthog/clickhouse/test/test_migrations.py re-imports every migration under a patched posthog.settings.CLOUD_DEPLOYMENT to check each deployment's branch for stray ON CLUSTER, and a cached value would silently skip that coverage.
If you create a new table inside such a guard, you must also add its SQL function to posthog/clickhouse/schema.py in the appropriate tuple so the table gets created locally:
| Table type | Tuple in schema.py |
| MergeTree / base table | CREATE_MERGETREE_TABLE_QUERIES |
| Distributed / writable | CREATE_DISTRIBUTED_TABLE_QUERIES |
| Kafka consumer | CREATE_KAFKA_TABLE_QUERIES |
| Materialized view | CREATE_MV_TABLE_QUERIES |
| Non-materialized view | CREATE_VIEW_QUERIES |
| Dictionary | CREATE_DICTIONARY_QUERIES |
The only exception is tables whose definition intentionally differs per environment and is not tracked in the repo (e.g. the no-go zone events_json_ws_mv table).
Dictionary credentials: when a dictionary uses a SOURCE(CLICKHOUSE(...)), resolve the source user/password via get_clickhouse_creds(ClickHouseUser.DICT_READER) and interpolate them into the USER/PASSWORD clause — do not hardcode default/CLICKHOUSE_USER or omit credentials. This keeps dictionary auth on the dedicated low-privilege dict_reader user, decoupled from default; it falls back to default creds when the env vars are unset. See posthog/models/exchange_rate/sql.py for the pattern.
Declarative schema (HCL) must agree
Some roles are also declared in posthog/clickhouse/hcl/, and the multinode migration smoke re-introspects
the migrated cluster and fails on any drift between the live schema and the committed golden. A table added
by a migration on a managed role therefore needs the HCL updated in the same PR, or CI fails at
check_live_hcl with a DRIFT: block naming your new objects.
The data role is managed for the local-multi composition, so a new table on NodeRole.DATA counts.
After writing the migration:
HCL=posthog/clickhouse/hcl
# add the table to the layer that composes it, e.g. $HCL/roles/data/local/tables.hcl
bash $HCL/gen-golden.sh && bash $HCL/gen-sql.sh # refresh generated artifacts
bash $HCL/check.sh # must exit 0Read posthog/clickhouse/hcl/README.md before editing a layer — it covers which layer to pick, abstract
column lists, and the codegen path that writes the migration for you.
Testing
Delete entry from infi_clickhouse_orm_migrations table to re-run a migration.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

