
clickhouse-migrations
ClickHouse migration patterns and rules. Use when creating or modifying ClickHouse migrations.
- Installs
- 266
- Last 24h
- —
- Trending rank
- —
- Security
- Not audited
Install
Works with every agent the skills CLI supports.
npx skills add https://github.com/posthog/posthog --skill clickhouse-migrations- 01
Install
Run the command in your project. The CLI asks which agents to install for.
- 02
Describe the task
Ask your agent for something this skill covers. It loads clickhouse-migrations automatically when relevant.
- 03
Review the result
The agent follows the SKILL.md steps, scripts and references bundled with the skill.
SKILL.md
The instructions your agent reads when the skill is triggered.
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, APM, 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]: objects that belong on the main cluster — sharded tables, theDistributedread tables in front of sharded or satellite tables, views, dictionaries. A non-sharded replicated table onDATAkeeps a full copy on every main-cluster node, so it is the wrong home for a product rollup or pre-aggregate (see below).[NodeRole.INGESTION_SMALL]: writable tables, Kafka tables, materialized views on the ingestion layer[NodeRole.APM]: theingestion-apmnodes, the metrics ingestion layer. Kafka tables,Nullinput tables, materialized views and writableDistributedtables that write into storage onLOGS. Stateless like the other ingestion nodes, so storage tables never go here.[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
Pre-aggregates and rollups are stored on a satellite and read through a Distributed table on DATA
(and on the storage satellite, for ad-hoc reads there), unless the app already connects to that satellite
directly. A rollup of a satellite's own data stays on that
satellite; general product pre-aggregates go to AUX. Confirm the satellite with the live check below,
and find a current example's HCL layers with hclexp locate.
Check the live clusters before choosing a role
Older migrations, the HCL local layers and shared helpers can trail what runs in dev and production. Confirm placement against the live clusters before writing the migration:
- Use the ClickHouse MCP to check each of dev, prod-us and prod-eu: where comparable tables live,
which satellite tables the main cluster reads through
Distributedtables, and that the new table names are unused. See the Housekeeper MCP runbook for setup and usage. - When precedents disagree, follow the newest migration for that table family and confirm it matches what runs in all three environments. A layout that only some environments still run is mid-migration, not a pattern to copy.
- State in the PR description which cluster each new object lands on, and name the reference migration or live check behind the choice.
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 = 100000
kafka_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.
Every role with a local-multi block in manifest.hcl is managed, which covers the main cluster's data
role and most satellite and ingestion roles. Check that file for the role you are targeting; a new table on
a managed role counts. After writing the migration:
HCL=posthog/clickhouse/hcl
# add the table to the layer that composes it (hclexp locate a sibling table to find that layer)
bash $HCL/gen-golden.sh && bash $HCL/gen-sql.sh # refresh generated artifacts
bash $HCL/check.sh # must exit 0
Read 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.
FAQ
What does the clickhouse-migrations skill do?
ClickHouse migration patterns and rules. Use when creating or modifying ClickHouse migrations.
How do I install clickhouse-migrations?
Run npx skills add https://github.com/posthog/posthog --skill clickhouse-migrations in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/clickhouse-migrations/ (personal) or .claude/skills/clickhouse-migrations/ (project).
Which AI agents can use clickhouse-migrations?
Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.
Is clickhouse-migrations safe to install?
No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.
Who maintains clickhouse-migrations?
It is published by posthog in posthog/posthog, and it is an official skill from the maker of the technology it covers. It has 266 recorded installs.
More from posthog/posthog
implementing-agent-modes
posthog/posthog
Guidelines to create/update a new mode for PostHog AI agent. Modes are a way to limit what tools, prompts, and prompt injections are applied and under what conditions. Achieve better results using your plan mode.
react-doctor
posthog/posthog
Diagnose and fix React codebase health issues. Use when reviewing React code, fixing performance problems, auditing security, or improving code quality.
hogli
posthog/posthog
PostHog developer CLI and repo tooling reference. Use when the user mentions hogli, asks about repo CLI tools, bin scripts, Makefiles, how to run/build/test/lint, or any dev environment commands.
survey-sdk-audit
posthog/posthog
Audit PostHog survey SDK features and version requirements, and implement survey changes across the backend, UI, and SDK repositories.
setup-web-tests
posthog/posthog
Set up Python test environment in Claude Code for web where flox is unavailable. Use when you need to run backend tests and uv sync fails due to Python version mismatch.
django-migrations
posthog/posthog
Django migration patterns and safety workflow for PostHog. Use when creating, adjusting, or reviewing Django/Postgres migrations, including non-blocking index/constraint changes, multi-phase schema changes, data backfills, migration conflict rebasing, and product model moves that require SeparateDatabaseAndState. Also use for any deletion or removal of a model, table, column, product, or app — including deleting migration files or retiring a feature — even when no migration is written.
Popular data & databases skills
supabase-postgres-best-practices
supabase/agent-skills
Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and scheduled jobs (pgcron, pgmq), vector/semantic search (pgvector), and restoring dumps (pgrestore) or importing data. Also load it when diagnosing slow queries, high CPU, timeouts, EXPLAIN plans, connection exhaustion, locking, bloat, or rows visible to the wrong user or tenant. This is not just a performance guide — schema, migration, security, and SQL authoring tasks need these rules too, even for a one-column change or a single query.
prisma-client-api
prisma/skills
Prisma Client API reference covering model queries, filters, operators, and client methods. Use when writing database queries, using CRUD operations, filtering data, or configuring Prisma Client. Triggers on "prisma query", "findMany", "create", "update", "delete", "$transaction".
prisma-database-setup
prisma/skills
Deprecated compatibility name for prisma-orm-setup. Use only when a user or existing integration explicitly invokes prisma-database-setup; otherwise select prisma-orm-setup.
prisma-cli
prisma/skills
Prisma ORM 7 CLI reference for existing Prisma 7 applications or explicitly selected Prisma 7 workflows. Covers prisma init, generate, migrate, db, dev, studio, and mcp. For setup, connection repair, or Prisma 8, use prisma-orm-setup; not the Prisma Platform CLI.
prisma-postgres
prisma/skills
Deprecated compatibility name for prisma-postgres-setup. Use only when a user or existing integration explicitly invokes prisma-postgres; otherwise select prisma-postgres-setup.
prisma-postgres-setup
prisma/skills
Obtain or reuse a Prisma Postgres database and connect an application. Use when the user needs a database and has none configured ("I need a database", "add a Postgres database", "set up a database for this app"), runs or mentions create-db or npx create-db, or asks to set up or connect Prisma Postgres, including in v0 and the Vercel Marketplace. Hands Prisma ORM configuration to prisma-orm-setup; reuses an existing connection first and honors an explicit provider, driver, alternative ORM, or database-only choice.