Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

docs(agents): add ClickHouse view and MV migration rules to skills and review checklist (#15035)

Steffen Schmitz46d78aa52c66
diff --git a/.agents/skills/clickhouse-best-practices/SKILL.md b/.agents/skills/clickhouse-best-practices/SKILL.md
index f15754e2a5..1ae9e83325 100644
--- a/.agents/skills/clickhouse-best-practices/SKILL.md
+++ b/.agents/skills/clickhouse-best-practices/SKILL.md
@@ -53,6 +53,30 @@ Comprehensive guidance for ClickHouse covering schema design, query optimization
with `SETTINGS mutations_sync = 2`. The matching `unclustered/` file runs
against plain `MergeTree` and does not need (and should not duplicate)
these settings.
+- Never use `CREATE OR REPLACE VIEW` (nor `CREATE OR REPLACE TABLE` /
+ `EXCHANGE TABLES`) in ClickHouse migrations. The atomic replace requires
+ `renameat2` filesystem support, which NFS-backed self-hosted deployments
+ (e.g. ClickHouse data on AWS EFS) lack — the migration fails and the
+ deployment aborts on startup (GitHub issue #14906). Redefine a plain view as
+ two statements in the same migration file: `DROP VIEW IF EXISTS <name>
+ [ON CLUSTER default];` then `CREATE VIEW <name> [ON CLUSTER default] AS …`.
+ The migration runner passes `x-multi-statement=true` and golang-migrate
+ splits files on `;` without parsing SQL, so keep semicolons out of comments
+ and string literals. Keep every statement idempotent
+ (`IF EXISTS`/`IF NOT EXISTS`) so a dirty, half-applied migration can be
+ re-run after `migrate force`. Readers hitting the view inside the
+ drop→create window fail transiently — acceptable for the `analytics_*`
+ export views, so keep plain views off product hot paths.
+- Never drop-and-recreate a materialized view whose source table receives live
+ inserts: every row inserted between `DROP` and `CREATE` is silently and
+ permanently missing from the target table. Change an MV's SELECT with
+ `ALTER TABLE <mv> [ON CLUSTER default] MODIFY QUERY <select>`, which swaps
+ the transformation without interrupting ingestion. When the change adds
+ columns, `ALTER` the target table(s) first (`ADD COLUMN IF NOT EXISTS …`),
+ then `MODIFY QUERY`; in `clustered/` files those target-table `ALTER`s must
+ carry `SETTINGS alter_sync = 2` so no host applies the new MV query before
+ its target replica has the new columns. `MODIFY QUERY` is only viable for
+ TO-table MVs (all Langfuse MVs use `TO`).
---
@@ -80,6 +104,8 @@ Comprehensive guidance for ClickHouse covering schema design, query optimization
- [ ] Partition key cardinality bounded (100-1,000 values)
- [ ] ReplacingMergeTree has version column if used
- [ ] Clustered migration files with multiple ALTERs on the same table use `SETTINGS alter_sync = 2` (metadata) and `SETTINGS mutations_sync = 2` (`MATERIALIZE …`, `UPDATE`, `DELETE`); unclustered mirror has none
+- [ ] No `CREATE OR REPLACE VIEW/TABLE` or `EXCHANGE TABLES` in migrations (breaks NFS/EFS self-hosting); plain views are redefined via `DROP VIEW IF EXISTS` + `CREATE VIEW` in the same file
+- [ ] Materialized views are never dropped and recreated while their source table takes inserts; SELECT changes go through `ALTER TABLE <mv> MODIFY QUERY` after the target-table `ALTER`s
### For Query Reviews (SELECT, JOIN, aggregations)
diff --git a/.agents/skills/code-review/references/review-checklist.md b/.agents/skills/code-review/references/review-checklist.md
index be56603406..61497949d2 100644
--- a/.agents/skills/code-review/references/review-checklist.md
+++ b/.agents/skills/code-review/references/review-checklist.md
@@ -10,6 +10,9 @@ This is the canonical shared review checklist for Langfuse.
- E.g. `ReplacingMergeTree` is likely an error while `ReplicatedReplacingMergeTree` would be correct in most cases.
- ClickHouse migrations in the `packages/shared/clickhouse/migrations/unclustered` directory must not include `ON CLUSTER` statements and must not use `Replicated` merge tree table types.
- Migrations in `packages/shared/clickhouse/migrations/clustered` should match their counterparts in `packages/shared/clickhouse/migrations/unclustered` aside from the restrictions listed above.
+- Migrations must not use `CREATE OR REPLACE VIEW` (nor `CREATE OR REPLACE TABLE` / `EXCHANGE TABLES`): the atomic replace requires `renameat2`, which fails on NFS-backed self-hosted deployments such as ClickHouse-on-EFS (GitHub issue #14906). Plain views are redefined with `DROP VIEW IF EXISTS <name>;` followed by `CREATE VIEW <name> AS ...` in the same migration file — the migration runner enables `x-multi-statement=true`, and every statement should stay idempotent (`IF EXISTS` / `IF NOT EXISTS`) so a dirty migration can be re-run.
+- Materialized views must never be dropped and recreated while their source table receives inserts — rows written between `DROP` and `CREATE` are silently and permanently missing from the target table. Redefine the SELECT with `ALTER TABLE <mv> MODIFY QUERY ...` instead, altering the target table(s) first when columns change.
+- For rationale and full patterns, see "Langfuse-Specific Rules" in the [`clickhouse-best-practices`](../../clickhouse-best-practices/SKILL.md) skill.
- When adding new indexes on ClickHouse, ensure that there is a corresponding `MATERIALIZE INDEX` statement in the same migration. The materialization can use `SETTINGS mutations_sync = 2` if they operate on smaller tables, but may timeout otherwise.
- All ClickHouse queries on project-scoped tables (traces, observations, scores, events, sessions, etc.) must include `WHERE project_id = {projectId: String}` filter to ensure proper tenant isolation and that queries only access data from the intended project.
- For operations on the `events` table, you must never use the `FINAL` keyword as it kills performance. `events` is built so that `FINAL` is never required.
diff --git a/packages/shared/AGENTS.md b/packages/shared/AGENTS.md
index b3d35cb945..d2b3575352 100644
--- a/packages/shared/AGENTS.md
+++ b/packages/shared/AGENTS.md
@@ -100,6 +100,10 @@ the same PR.
### ClickHouse schema change
1. Add migration under `clickhouse/migrations/*`.
+ - Redefining views or materialized views follows strict patterns (no
+ `CREATE OR REPLACE VIEW`; MV SELECT changes via
+ `ALTER TABLE … MODIFY QUERY`) — apply the "Langfuse-Specific Rules" in
+ `.agents/skills/clickhouse-best-practices/SKILL.md` for any new ClickHouse migration.
2. Update ClickHouse query/mapping logic in `src/server/clickhouse/*` and
related repositories.
3. Validate ingestion/read path impact in both `web` and `worker`.