Skip to content

: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.

chore(clickhouse): check live cluster placement before choosing migra… (#112154)

Bryan Ciaraldi388ca2121306
diff --git a/.agents/skills/clickhouse-migrations/SKILL.md b/.agents/skills/clickhouse-migrations/SKILL.md
index 0769619440ee..349630b63747 100644
--- a/.agents/skills/clickhouse-migrations/SKILL.md
+++ b/.agents/skills/clickhouse-migrations/SKILL.md
@@ -25,15 +25,20 @@ operations = [
### 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
+`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]`: anything on the main cluster — sharded tables, non-sharded replicated tables,
- distributed read tables, views, dictionaries. The default, and right for most migrations.
+- `[NodeRole.DATA]`: objects that belong on the main cluster — sharded tables, the `Distributed` read
+ tables in front of sharded or satellite tables, views, dictionaries. A non-sharded replicated table on
+ `DATA` keeps 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]`: the `ingestion-apm` nodes, the metrics ingestion layer. Kafka tables, `Null` input
+ tables, materialized views and writable `Distributed` tables that write into storage on `LOGS`.
+ 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 on
`DATA` lands on the wrong nodes and leaves the intended cluster without it, so check where the
@@ -41,6 +46,25 @@ Pick the role by **which cluster owns the object**, not by its type alone:
US/EU prod — never branch on `CLOUD_DEPLOYMENT` to 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:
+
+1. 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 `Distributed` tables, and that the new table
+ names are unused. See the [Housekeeper MCP runbook](https://runbooks.posthog.com/services/clickhouse/concepts/housekeeper-mcp) for setup and usage.
+2. 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.
+3. 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:
@@ -147,12 +171,13 @@ the migrated cluster and fails on any drift between the live schema and the comm
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:
+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:
```sh
HCL=posthog/clickhouse/hcl
-# add the table to the layer that composes it, e.g. $HCL/roles/data/local/tables.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
```
diff --git a/posthog/clickhouse/migrations/AGENTS.md b/posthog/clickhouse/migrations/AGENTS.md
index 2133f1376581..d6651d810d28 100644
--- a/posthog/clickhouse/migrations/AGENTS.md
+++ b/posthog/clickhouse/migrations/AGENTS.md
@@ -13,15 +13,30 @@ never a count.
Every environment runs one main cluster plus the same satellites and ingestion nodes.
- Main cluster (`posthog`) — sharded, several shards, multiple replicas per shard. Holds the
- sharded tables and is the default target for a migration.
+ sharded tables and the `Distributed` read tables the app queries, including those in front of
+ satellite tables.
- Satellites — `ai_events`, `aux`, `batch_exports`, `endpoints`, `logs`, `sessions`, `ops`. Each is
- a single shard with multiple replicas, and owns the tables for one product area.
-- Ingestion nodes — `ingestion-events`, `ingestion-medium`, `ingestion-small`. Stateless, one shard
- per node, no replicas. See Ingestion nodes below.
+ a single shard with multiple replicas, and owns the tables for one product area, including rollups
+ of that data. `aux` holds general product pre-aggregates (see "Satellite storage with a main-cluster
+ read table" below).
+- Ingestion nodes — `ingestion-events`, `ingestion-medium`, `ingestion-small`, `ingestion-apm`.
+ Stateless, one shard per node, no replicas. See Ingestion nodes below.
Shard and replica counts differ per environment and change over time, so nothing in a migration may
depend on them. Read `system.clusters` when you need the current shape.
+## Verify against the live clusters
+
+The repo can trail the clusters: older migrations, the HCL `local` layers and shared helpers may
+describe a layout that some environments have already moved away from. Before choosing `node_roles`
+for a new table, 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 `Distributed` tables, and that the
+new table names are unused. See the [Housekeeper MCP runbook](https://runbooks.posthog.com/services/clickhouse/concepts/housekeeper-mcp) for setup and usage.
+
+When precedents disagree, follow the newest migration for that table family and confirm it matches
+all three environments. A layout that only some environments still run is mid-migration, not a
+pattern to copy. State the chosen cluster, and the migration or check behind it, in the PR description.
+
## Ingestion nodes
It ingests data from Kafka to a proper ClickHouse shard.
@@ -35,6 +50,10 @@ The way this is done:
If a destination table is non-sharded, we pick only one node as data for the Distributed table.
+The `ingestion-apm` nodes (`NodeRole.APM`) run the same pattern for metrics: a Kafka table feeds a
+`Null` input table, materialized views fan it out to writable `Distributed` tables, and those write
+into the storage tables on the `logs` cluster (`NodeRole.LOGS`).
+
# Local setup parity
No table should exist only in the cloud. Every table created via migration must also exist
@@ -219,7 +238,32 @@ run_sql_with_exceptions(
)
```
-The table is created on data nodes because it's not sharded.
+A non-sharded table on `DATA` keeps a full copy on every main-cluster node, and every write and
+partition swap replicates to all of them. Reserve it for small tables the main cluster needs locally
+(joins, dictionary sources). A product rollup or pre-aggregate belongs on a satellite instead; see
+the next section.
+
+## Satellite storage with a main-cluster read table
+
+Pre-aggregates and rollups are stored on a satellite and read through a `Distributed` table on the
+main cluster, 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 above. Create the `Distributed`
+table on the storage satellite too, for ad-hoc reads there. The storage table needs a different name
+from the read table, since both exist on that satellite. With `AUX` as the storage satellite:
+
+```python
+operations = [
+ run_sql_with_exceptions(STORAGE_TABLE_SQL(), node_roles=[NodeRole.AUX]),
+ run_sql_with_exceptions(DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.DATA]),
+ run_sql_with_exceptions(DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.AUX]),
+]
+```
+
+The read table uses `Distributed(data_table=..., cluster=<the storage satellite's cluster setting>)`.
+Find a current example of this layout with the live check, and its HCL layers with `hclexp locate`.
+Inserts can go through the `Distributed` table on `DATA`, which forwards them to the satellite.
+Operations that must run where the storage table lives (partition swaps, `ALTER`s, recreating a
+staging table) go to a host of that satellite through `ClickhouseCluster` with its `NodeRole`.
## Replicated, sharded tables