:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(clickhouse): document the HCL change recipes and CI gates (#81052)
Paweł Szczur73690f344b53
diff --git a/posthog/clickhouse/hcl/README.md b/posthog/clickhouse/hcl/README.mdindex 80565a8eef05..660588b0d84e 100644--- a/posthog/clickhouse/hcl/README.md+++ b/posthog/clickhouse/hcl/README.md@@ -5,8 +5,8 @@ Source of truth for satellite ClickHouse clusters, managed declaratively withSchemas are written in HCL, **composed per node** `(env, role)`, verified againstcaptured cluster dumps, and used to generate the migration that applies a change.-Currently covers the **OPS** and **LOGS** roles across the three cloud envs-(dev, prod-us, prod-eu); OPS also has `local`.+Covers the satellite roles (ops, logs, aux, `ai_events`, sessions, sessionsv3, `batch_exports`) in the cloud envs (dev, prod-us, prod-eu) where each exists, plus the local `data` node.+The prod data clusters carry per-env `mat_` columns, so their goldens and per-env override layers live in posthog-cloud-infra (see [The cloud side](#the-cloud-side-posthog-cloud-infra)).## Model: per-node composition@@ -23,15 +23,17 @@ hcl/# role "<role>" { env "<env>" { layers = [...] } } -> a node's layer stack# cluster "<name>" { roles = [...], aliases = [...] } -> cross-cluster proxy resolutionlib.sh # shared manifest helpers sourced by the scripts below- roles/shared/ # objects on every role (query_log_archive path + custom_metrics_* sub-views + ops_query_log_archive_mv)- roles/ops/shared/ # OPS objects on every OPS env- roles/ops/prod/ # OPS objects on both prod envs only (the metrics suite)- roles/ops/<env>/ # per-env OPS overlay (sharded_tophog zoo_path, prod-us ProfileEvents2, dev prom_metrics); env ∈ local/dev/prod-us/prod-eu- roles/logs/shared/ # LOGS objects on every LOGS env- roles/logs/<env>/ # per-env LOGS overlay (kafka/zoo_path/distributed variants; traces on prod only); env ∈ dev/prod-us/prod-eu+ roles/shared/ # objects on every role (the query_log_archive path + custom_metrics_* sub-views)+ roles/coshared/<set>/ # objects co-hosted by a specific SET of roles (aux_data, sessions_data, tophog, …) — one layer per member set+ roles/<role>/shared/ # objects on every env of one role+ roles/<role>/prod/ # objects on both prod envs only+ roles/<role>/<env>/ # per-env overlay: env-only objects + patch_* deltas; env ∈ local/dev/prod-us/prod-eu+ roles/data/shared/ # the events family declared once: the _event_base abstract + the events/sharded_events extenders+ roles/data/local/ # the rest of the local data node (migration-produced schema) + its patch_* deltas+ duplicates-baseline.txt # objects still declared in >1 composed layer; check.sh gates on it, and it only shrinks<layer>/sql/<object>.sql # view/MV query bodies extracted from a layer, referenced as query = file("sql/<object>.sql")- golden/<env>-<role>.hcl # resolved composition per node (the desired schema); check.sh diffs against it- sql/<env>-<role>.sql # generated build-from-scratch CREATE schema per node (apply to a fresh ClickHouse)+ golden/<env>/<role>.hcl # resolved composition per node (the desired schema); check.sh diffs against it+ sql/<env>/<role>.sql # generated build-from-scratch CREATE schema per node (apply to a fresh ClickHouse)check.sh # CI guard (offline): validate + diff every node vs golden + verify golden/ & sql/ are freshdump-live.sh # CI gate step 1 (live): introspect the migrated OPS/LOGS nodes into HCL dumpscheck-live.sh # CI gate step 2 (offline): diff those dumps vs golden — catches migrations that desync from the HCL@@ -59,7 +61,7 @@ The convergence gate closes it, in **two steps** that run inside the multinode m`<outdir>/hclexp-version.txt` (`hclexp -version`) recording the tool build that produced the dump —informational provenance, not gated by `check-live.sh`.2. **`check-live.sh <dumpdir>`** — for each role, `hclexp diff -format json` the committed- `golden/<env>-<role>.hcl` against the dump, drop the ignored operations (named_collections ++ `golden/<env>/<role>.hcl` against the dump, drop the ignored operations (named_collections +`exclude.hcl` globs), and require nothing left. Offline — only needs `hclexp`.```bash@@ -73,7 +75,7 @@ versa). Fix the migration to match the HCL, or — if the change is intended —**enforced** (drift fails the smoke); export `VERIFY_LIVE_WARN=1` to make it informational whilereconciling a new role.-Each managed role is compared against its `golden/local-<role>.hcl`. The local LOGS node runs a+Each managed role is compared against its `golden/local-multi/<role>.hcl`. The local LOGS node runs apartial/newer schema than the cloud logs nodes, so `local logs` composes a self-contained`roles/logs/local` (extracted from the live node) rather than the shared cloud layers.@@ -121,12 +123,19 @@ The wrapper resolves `$HCLEXP_BIN` → `hclexp` on `$PATH` → that image, so awins: run `bash $HCL/bin/install-hclexp` to extract one from the pinned image (what CI does), orbuild it yourself with `go build -o hclexp ./cmd/hclexp` in `../../../../python-clickhouse-schema`.-1. **Edit the right layer** for what you're changing:- - all-role object (the `query_log_archive` path, `custom_metrics_*` sub-views, cross-cluster MVs) → `roles/shared/`- - OPS-only → `roles/ops/shared/` (all OPS envs), `roles/ops/prod/` (both prod envs), or `roles/ops/<env>/` (one env)- - LOGS → `roles/logs/shared/` (common) or `roles/logs/<env>/` (per-env / differing)+1. **Edit the right layer** for what you're changing.+ Placement = which node stacks compose the layer, declared in `manifest.hcl`; find an existing object's single declaration with `hclexp locate` (or grep) rather than assuming:+ - an object on every role (the `query_log_archive` path, `custom_metrics_*` sub-views) → `roles/shared/`+ - an object a specific set of roles co-hosts (a data table plus the nodes holding its Distributed proxies) → `roles/coshared/<member-set>/`, and wire a new set into each member's stack in `manifest.hcl`+ - one role → `roles/<role>/shared/` (all its envs), `roles/<role>/prod/` (both prods), or `roles/<role>/<env>/` (one env)+ - the events family → the `_event_base` abstract + extenders in `roles/data/shared/`- a brand-new object → add it to the layer above **and**, if it's on a new role, add that role'sblock to `manifest.hcl` (+ a golden for it).+ - **declare an object once, everywhere it differs use a patch.**+ An env/role variant is expressed on top of the single declaration: `patch_table` / `patch_view` / `patch_dictionary` / `patch_materialized_view` in the differing stack's layer, or `extend` + `patch_column` for shared column lists.+ Never paste a second declaration — `check.sh` gates `hclexp locate -duplicates` against `duplicates-baseline.txt`, which may only shrink.+ A cloud-only difference (e.g. `mat_` columns) belongs in posthog-cloud-infra's `overrides/`, never here.+ Counting rules and the full patch vocabulary: [migration.md](migration.md).- a long view/MV `query` → keep it in `<layer>/sql/<object>.sql` and reference it as`query = file("sql/<object>.sql")` (resolved relative to the layer file). The loader normalizes`file()`, heredoc, and inline forms to one canonical query, so the form is purely cosmetic — edit@@ -175,8 +184,43 @@ The committed migration is the apply + history record; the HCL/golden/sql are ththe offline guard. `diff -sql` UNSAFE statements (engine/zoo_path/order_by recreations) are review-gatedand must never be auto-applied to production.+### Recipes++**Add a column to an existing table.**+Find the table's single declaration (`hclexp locate`, or grep the layers) and add the `column` block there — every env composing that layer gets it, which is the point of declaring once.+On an `extend` family, adding to the abstract (e.g. `_event_base`) reaches every extender; adding to one child reaches only that table.+If only some envs should have the column, leave the declaration alone and add it via `patch_table { column "…" { … } }` in those envs' layers instead.+Then steps 2–5 above: `diff.sh`, `gen_migration.py`, `gen-golden.sh`/`gen-sql.sh`, `check.sh`.++**Add an index (or projection).**+Same placement logic, with an `index` block inside the single table declaration (`expression`/`type`/`granularity`), or `patch_table { index "…" { … } }` for an env-only index.+`after` positions it; redefining an inherited index in a patch means drop + re-add in the same patch.+Note the generated `ADD INDEX` only affects newly written parts; backfilling it (`MATERIALIZE INDEX`) is a separate, deliberate operation to add by hand if history needs it.++**Add a new table.**+Declare it in the placement layer per step 1 (a Kafka table + MV + storage/Distributed group usually lands together in one layer), keep long view/MV queries in `<layer>/sql/<object>.sql`, and run the same steps 2–5.+If the table introduces a new role, add the `role` block to `manifest.hcl` and a golden for it.++**Change an env-specific variant.**+Edit the existing `patch_*` block in that env's layer (cloud envs: in posthog-cloud-infra's `overrides/`); the base declaration stays untouched.++## What CI runs on a PR touching this directory++- **`hcl-guard`** (`ci-clickhouse-hcl-schema.yml`, offline): `check.sh` — validate + compose every node, byte-compare against `golden/`, verify `sql/` freshness, and hold `locate -duplicates` to `duplicates-baseline.txt`.+- **`cloud-compose-gate`** (same workflow): dispatches posthog-cloud-infra to compose **your PR's ref** as the vendored base under its `overrides/`, per cloud env.+ It fails when a base change breaks the cloud composition — a renamed/dropped column a cloud patch targets, a name colliding with a cloud-added column, a reference a cloud-only object needs.+ It deliberately does **not** assert cloud goldens: a legitimate shared-layer change shifts them, and regenerating is the base-ref bump PR's job (below).+ The job skips (with a summary) on fork PRs and while the gate's App credentials are unprovisioned.+- **Convergence gate** (multinode smoke, see above): proves the imperative migrations still reproduce the local goldens on a live cluster.++## The cloud side (posthog-cloud-infra)++The cloud envs compose the same base layers, vendored by commit sha, plus private per-env `overrides/` (mat\_ columns and other cloud-only deltas), in posthog-cloud-infra's `clickhouse/hcl/`.+After a base change merges here, a **base-ref bump PR** there advances the pinned sha, regenerates the cloud goldens (your change now appears in every env's composed output), and produces the ordered migration SQL for the cloud clusters.+That PR runs the strict check (goldens asserted, baseline exact), so the golden movement the compose gate waved through gets reviewed and applied there.+## Build a node from scratch-`sql/<env>-<role>.sql` is the full, dependency-ordered CREATE schema for that node — e.g. apply-`sql/local-ops.sql` to a local ClickHouse to create the OPS schema. (It is faithful to the HCL, so it+`sql/<env>/<role>.sql` is the full, dependency-ordered CREATE schema for that node — e.g. apply+`sql/local-multi/ops.sql` to a local ClickHouse to create the OPS schema. (It is faithful to the HCL, so itreferences the real clusters / `{shard}` macros / Kafka; apply it to an env that has those configured.)diff --git a/posthog/clickhouse/hcl/migration.md b/posthog/clickhouse/hcl/migration.mdindex aae8b41d1e7a..f6195c667e15 100644--- a/posthog/clickhouse/hcl/migration.md+++ b/posthog/clickhouse/hcl/migration.md@@ -144,11 +144,13 @@ Full implementation plan: `docs/plans/2026-07-14-hcl-recreate.md`.## Deployment1. local and schema changes to base are done in PostHog/posthog, this is also what all tests run against-2. PR in PostHog/posthog shall trigger a run of a check in posthog-cloud-infra that validates that changes could be merged with the prod layer+2. a PR in PostHog/posthog triggers a compose check in posthog-cloud-infra validating the changes still compose under the prod overrides — the `cloud-compose-gate` job in `ci-clickhouse-hcl-schema.yml` dispatches cloud-infra's `ci-clickhouse-hcl-compose-gate.yaml` against the PR head; it asserts composability, not cloud goldens3. the cloud schema is composed as base + customizations in posthog-cloud-infra-4. when a PR is merged in PostHog/posthog it shall trigger a job and create PR in posthog-cloud-infra to bump pinned version of base; it shall generate a full ordered list of SQL queries that will be executed as part of migration+4. when a PR is merged in PostHog/posthog, a base-ref bump PR in posthog-cloud-infra advances the pinned base sha, regenerates the cloud goldens, and generates the full ordered list of SQL queries that will be executed as part of migration (creation of that PR is to be automated)5. after approval, the PR is merged and a migrator executes a migration+The change recipes (add a table, column, index; env variants) live in [README.md](README.md), "Making a change".+## Repos- this repo, PostHog/posthog - base repository@@ -156,5 +158,5 @@ Full implementation plan: `docs/plans/2026-07-14-hcl-recreate.md`.- PostHog/chschema - hclexp, github repo is PostHog/chschema- all needed tooling is on main (per-object comparison #139, locate + -duplicates #145); pin >= sha-5756e98- PostHog/posthog-cloud-infra - a repository with ansible and machine configurations- - notable branch: pawel/chore/clickhouse-hcl-data-goldens — the compose harness (vendored base + overrides + data goldens with mat\_ columns), to be merged first+ - `clickhouse/hcl/` holds the compose harness (vendored base pinned by sha + `overrides/` + data goldens with mat\_ columns) and the compose-gate workflow- PostHog/charts - kubernetes config and apps deployment scripts, an old / current clickhouse migration mechanism is run here as job in django web app deployemnt