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): live convergence gate for OPS/LOGS HCL schema (#67134)

Paweł Szczur09cdb0ac8f91
diff --git a/.github/workflows/ci-clickhouse-hcl-schema.yml b/.github/workflows/ci-clickhouse-hcl-schema.yml
index cefa39b1c379..bda88041df44 100644
--- a/.github/workflows/ci-clickhouse-hcl-schema.yml
+++ b/.github/workflows/ci-clickhouse-hcl-schema.yml
@@ -28,11 +28,11 @@ permissions:
packages: read
env:
- # Pin to a concrete chschema build (not :latest) for reproducibility. sha-1871283
- # adds file()/heredoc query refs with beautify-canonical normalization on the
- # layer loader (#68/#69) so a query authored as file("sql/x.sql") compares equal
- # to its golden; it retains the modifier-aware diff (#59). Override via repo variable.
- HCLEXP_IMAGE: ${{ vars.HCLEXP_IMAGE || 'ghcr.io/posthog/chschema:sha-1871283' }}
+ # Pin to a concrete chschema build (not :latest) for reproducibility. sha-c0affa0
+ # adds `introspect -exclude` (#79) for the live gate; it retains file()/heredoc
+ # query refs (#68/#69) so a query authored as file("sql/x.sql") compares equal to
+ # its golden, and the modifier-aware diff (#59). Override via repo variable.
+ HCLEXP_IMAGE: ${{ vars.HCLEXP_IMAGE || 'ghcr.io/posthog/chschema:sha-c0affa0' }}
jobs:
ops-hcl-guard:
diff --git a/.github/workflows/ci-clickhouse-multinode-migrations.yml b/.github/workflows/ci-clickhouse-multinode-migrations.yml
index 4cb93ffe8e72..51dfa0770332 100644
--- a/.github/workflows/ci-clickhouse-multinode-migrations.yml
+++ b/.github/workflows/ci-clickhouse-multinode-migrations.yml
@@ -13,6 +13,7 @@ on:
- 'docker/clickhouse/config.d/multinode/**'
- 'docker-compose.multinode-clickhouse.yml'
- 'tools/infra-scripts/clickhouse-multinode/**'
+ - 'posthog/clickhouse/hcl/**'
- '.github/workflows/ci-clickhouse-multinode-migrations.yml'
merge_group:
workflow_dispatch:
@@ -23,6 +24,8 @@ concurrency:
permissions:
contents: read
+ # Pull the pinned hclexp image from ghcr for the live-schema convergence gate.
+ packages: read
jobs:
multinode-migration-smoke:
@@ -52,6 +55,9 @@ jobs:
# of running a full `manage.py migrate`. Cache miss → file absent →
# script falls back to migrate automatically.
PRIME_POSTGRES_FROM: schema.sql.gz
+ # Pinned chschema build used by the live-schema convergence gate
+ # (verify-live.sh). Keep in sync with posthog/clickhouse/hcl/bin/hclexp.
+ HCLEXP_IMAGE: ${{ vars.HCLEXP_IMAGE || 'ghcr.io/posthog/chschema:sha-c0affa0' }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
@@ -126,6 +132,16 @@ jobs:
path: schema.sql.gz
key: ${{ steps.schema-key.outputs.key }}
+ - name: Log in to ghcr.io
+ uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
+ with:
+ registry: ghcr.io
+ username: ${{ github.actor }}
+ password: ${{ secrets.GITHUB_TOKEN }}
+
+ - name: Pull hclexp image
+ run: docker pull "$HCLEXP_IMAGE"
+
- name: Run smoke test
run: tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke
diff --git a/posthog/clickhouse/hcl/README.md b/posthog/clickhouse/hcl/README.md
index 1a4864fb0153..d4147f72ca63 100644
--- a/posthog/clickhouse/hcl/README.md
+++ b/posthog/clickhouse/hcl/README.md
@@ -27,13 +27,49 @@ hcl/
<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)
- check.sh # CI guard: validate + diff every node vs golden + verify golden/ & sql/ are fresh
+ check.sh # CI guard (offline): validate + diff every node vs golden + verify golden/ & sql/ are fresh
+ dump-live.sh # CI gate step 1 (live): introspect the migrated OPS/LOGS nodes into HCL dumps
+ check-live.sh # CI gate step 2 (offline): diff those dumps vs golden — catches migrations that desync from the HCL
+ exclude.hcl # objects the gate drops (transient + cross-cluster proxies + out-of-band-managed, not in the managed set)
diff.sh # preview the DDL your uncommitted edits produce, per node
gen-golden.sh # (re)generate golden/ — hclexp load per node
gen-sql.sh # (re)generate sql/
codegen/gen_migration.py # turn an edit into run_sql_with_exceptions(...) operations
```
+## Convergence gate: migrations must reproduce the golden (`dump-live.sh` + `check-live.sh`)
+
+`check.sh` is **offline** — it proves the HCL is internally consistent and that `golden/`/`sql/` are
+fresh, but it never contacts a cluster, so it cannot tell whether the imperative migrations in
+`posthog/clickhouse/migrations/` still produce the schema the HCL declares. That gap is how old
+migrations silently desynced the live OPS/LOGS schema from the HCL.
+
+The convergence gate closes it, in **two steps** that run inside the multinode migration smoke
+(`tools/infra-scripts/clickhouse-multinode/`, workflow `ci-clickhouse-multinode-migrations.yml`)
+**after** `manage.py migrate_clickhouse`:
+
+1. **`dump-live.sh [outdir]`** — `hclexp introspect` each managed role's live node into
+ `<outdir>/<env>-<role>.hcl`, dropping unmanaged / transient objects via `exclude.hcl`. Needs the
+ cluster (a `--network host` container, or `HCLEXP_BIN` locally).
+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 +
+ `exclude.hcl` globs), and require nothing left. Offline — only needs `hclexp`.
+
+```bash
+DUMP=$(bash posthog/clickhouse/hcl/dump-live.sh) # step 1 -> prints the dump dir
+bash posthog/clickhouse/hcl/check-live.sh "$DUMP" # step 2
+```
+
+Remaining drift means a migration changed the live schema without the HCL being updated (or vice
+versa). Fix the migration to match the HCL, or — if the change is intended — edit the layer, rerun
+`gen-golden.sh`/`gen-sql.sh`, and add the migration (the normal change flow below). Step 2 is
+**enforced** (drift fails the smoke); export `VERIFY_LIVE_WARN=1` to make it informational while
+reconciling a new role.
+
+LOGS is compared against `golden/local-<role>.hcl`; until a `local-logs` golden is seeded (introspect
+the live local logs node, then curate), the script skips LOGS with a notice. OPS is enforced via the
+existing `golden/local-ops.hcl`.
+
`node_roles` is **derived**: an object in `roles/shared/` appears in every node's composition →
`node_roles` = every managed role (currently `[LOGS, OPS]`); an object under `roles/ops/` appears
only in the ops nodes → `[OPS]`; one under `roles/logs/` → `[LOGS]`.
@@ -54,7 +90,7 @@ HCL=posthog/clickhouse/hcl
$HCL/bin/hclexp -help
# it is equivalent to:
docker run --rm -v "$PWD:/work" -v "${TMPDIR:-/tmp}:${TMPDIR:-/tmp}" -w /work \
- ghcr.io/posthog/chschema:sha-1871283 -help
+ ghcr.io/posthog/chschema:sha-c0affa0 -help
```
(For faster local iteration you can build the binary — `go build -o hclexp ./cmd/hclexp` in
diff --git a/posthog/clickhouse/hcl/bin/hclexp b/posthog/clickhouse/hcl/bin/hclexp
index 9a7b1457854f..d0ec0fbe2954 100755
--- a/posthog/clickhouse/hcl/bin/hclexp
+++ b/posthog/clickhouse/hcl/bin/hclexp
@@ -10,12 +10,12 @@
# (`go build -o hclexp ./cmd/hclexp` in the sibling repo) for faster iteration.
set -euo pipefail
-# Pinned to a concrete build (not :latest) for reproducibility. sha-1871283 adds
-# file()/heredoc query refs with beautify-canonical normalization on the layer
-# loader (chschema #68/#69) and beautified dump-sql (#70); it retains structured
-# JSON diff (#66), the plan command (#67), and the modifier-aware diff (#59).
+# Pinned to a concrete build (not :latest) for reproducibility. sha-c0affa0 adds
+# `introspect -exclude` (chschema #79), which the live convergence gate
+# (dump-live.sh) requires; it retains file()/heredoc query refs (#68/#69),
+# structured JSON diff (#66), and the modifier-aware diff (#59).
# Override via $HCLEXP_IMAGE; bump when intentionally upgrading hclexp.
-HCLEXP_IMAGE="${HCLEXP_IMAGE:-ghcr.io/posthog/chschema:sha-1871283}"
+HCLEXP_IMAGE="${HCLEXP_IMAGE:-ghcr.io/posthog/chschema:sha-c0affa0}"
if [[ -n "${HCLEXP_BIN:-}" ]]; then
exec "$HCLEXP_BIN" "$@"
diff --git a/posthog/clickhouse/hcl/check-live.sh b/posthog/clickhouse/hcl/check-live.sh
new file mode 100755
index 000000000000..c1ec296174d2
--- /dev/null
+++ b/posthog/clickhouse/hcl/check-live.sh
@@ -0,0 +1,90 @@
+#!/usr/bin/env bash
+# Convergence gate, step 2 of 2: diff the live HCL dumps produced by dump-live.sh
+# against the committed golden per <env>-<role>, and require nothing but ignored
+# operations remain. Offline — needs only hclexp (via bin/hclexp), no cluster.
+#
+# For each role it runs `hclexp diff -left golden -right <dump> -format json` and
+# drops the operations the gate intentionally ignores — named_collections (secret
+# Kafka broker config the schema golden never models) and objects whose name
+# matches an exclude.hcl glob (out-of-band-managed: real on prod but not created
+# by the local migrate path). Anything left is real drift: fix the migration to
+# match posthog/clickhouse/hcl/, or edit the HCL layer + rerun gen-golden.sh /
+# gen-sql.sh + add the migration. See README.md.
+#
+# Usage: check-live.sh [dumpdir] (dumpdir defaults to $LIVE_DUMP_DIR)
+#
+# Env knobs:
+# VERIFY_LIVE_WARN=1 report drift but exit 0 (informational rollout).
+# VERIFY_LIVE_ENV=<env> golden + dump env to compare (default: local).
+set -euo pipefail
+
+HCL=posthog/clickhouse/hcl
+HCLEXP="$HCL/bin/hclexp" # offline wrapper (no cluster network needed)
+GOLDEN="$HCL/golden"
+EXCLUDE="$HCL/exclude.hcl"
+ENV="${VERIFY_LIVE_ENV:-local}"
+WARN="${VERIFY_LIVE_WARN:-0}"
+DUMPDIR="${1:-${LIVE_DUMP_DIR:?dump dir required (pass as arg1 or set LIVE_DUMP_DIR); run dump-live.sh first}}"
+
+ROLES=(ops logs)
+
+# Object-name globs the gate ignores, parsed from exclude.hcl (the quoted glob
+# strings) — the same list dump-live.sh feeds hclexp -exclude, applied here to
+# the diff so golden-only out-of-band objects don't count as drift.
+GATE_IGNORE="$(grep -oE '"[^"]+"' "$EXCLUDE" 2>/dev/null | tr -d '"' | tr '\n' ' ')"
+
+# Read `hclexp diff -format json` on stdin, print the operations that count as
+# real drift (one per line), exit non-zero iff any remain. Drops named_collections
+# and objects matching a gate-ignore glob — both are workarounds for hclexp diff
+# lacking subset/exclude scoping (PostHog/chschema#75); remove once it lands.
+filter_drift() {
+ # -c (not `- <<heredoc`) so stdin stays the piped JSON; globs arrive as argv.
+ python3 -c '
+import sys, json, fnmatch
+globs = sys.argv[1:]
+ops = json.load(sys.stdin).get("operations", [])
+def ignored(o):
+ if o.get("object_type") == "named_collection":
+ return True
+ name = o.get("object", "")
+ return any(fnmatch.fnmatch(name, g) for g in globs)
+drift = [o for o in ops if not ignored(o)]
+for o in drift:
+ db = o.get("database") or ""
+ obj = (db + "." + o["object"]) if db else o["object"]
+ print(" " + o["kind"] + " " + o["object_type"] + " " + obj)
+sys.exit(1 if drift else 0)
+' "$@"
+}
+
+rc=0
+for role in "${ROLES[@]}"; do
+ golden="$GOLDEN/$ENV-$role.hcl"
+ live="$DUMPDIR/$ENV-$role.hcl"
+
+ if [ ! -f "$golden" ]; then
+ echo "== $ENV/$role: no golden ($golden) — skipping (add it to enforce this role) =="
+ continue
+ fi
+ if [ ! -f "$live" ]; then
+ echo "FAIL: no dump for $ENV/$role ($live) — run dump-live.sh first"
+ rc=1; continue
+ fi
+
+ echo "== $ENV/$role: diff golden vs live dump =="
+ # shellcheck disable=SC2086 # GATE_IGNORE is a deliberately word-split glob list
+ if drift="$("$HCLEXP" diff -left "$golden" -right "$live" -format json | filter_drift $GATE_IGNORE)"; then
+ echo "no differences"
+ else
+ echo "DRIFT: $ENV/$role — migrations produced a schema that differs from the HCL golden"
+ echo "$drift"
+ rc=1
+ fi
+done
+
+if [ "$rc" -ne 0 ] && [ "$WARN" = "1" ]; then
+ echo "check-live: drift detected (warn mode — not failing). Reconcile before enforcing."
+ exit 0
+fi
+[ "$rc" -eq 0 ] && echo "check-live: OPS/LOGS live schema matches the HCL golden"
+exit $rc
diff --git a/posthog/clickhouse/hcl/dump-live.sh b/posthog/clickhouse/hcl/dump-live.sh
new file mode 100755
index 000000000000..c5f2f5efe791
--- /dev/null
+++ b/posthog/clickhouse/hcl/dump-live.sh
@@ -0,0 +1,83 @@
+#!/usr/bin/env bash
+# Convergence gate, step 1 of 2: introspect the live OPS/LOGS nodes on a booted
+# multinode stack (see tools/infra-scripts/clickhouse-multinode/) into one HCL
+# dump per <env>-<role>. Step 2 (check-live.sh) diffs those dumps against the
+# committed golden — offline. Splitting keeps the cluster/network-dependent
+# capture separate from the deterministic comparison.
+#
+# Transient / unmanaged objects are dropped at introspect time via exclude.hcl.
+#
+# Usage: dump-live.sh [outdir]
+# outdir defaults to $LIVE_DUMP_DIR, else a fresh temp dir. The dir is printed
+# as the last stdout line so a caller can capture it; progress goes to stderr.
+# Keep it under $TMPDIR or the repo so the containerized hclexp can see it.
+#
+# Env knobs:
+# VERIFY_LIVE_ENV=<env> names the dump files (default: local).
+# HCLEXP_BIN=<path> local hclexp binary (host network); otherwise a
+# `--network host` container reaches the published ports.
+# <ROLE>_HOST/_PORT/_DB override a role's connection (e.g. OPS_PORT=9300).
+# CLICKHOUSE_USER / CLICKHOUSE_PASSWORD credentials (default: default / empty).
+set -euo pipefail
+
+HCL=posthog/clickhouse/hcl
+EXCLUDE="$HCL/exclude.hcl"
+ENV="${VERIFY_LIVE_ENV:-local}"
+CH_USER="${CLICKHOUSE_USER:-default}"
+CH_PASSWORD="${CLICKHOUSE_PASSWORD:-}"
+OUTDIR="${1:-${LIVE_DUMP_DIR:-$(mktemp -d "${TMPDIR:-/tmp}/ch-live-dump.XXXXXX")}}"
+mkdir -p "$OUTDIR"
+# mktemp -d makes the dir mode 700; the nonroot hclexp container (a different uid
+# than the host) then can't traverse it to read the dumps back in check-live.
+# Make it world-traversable so the container can read what the host wrote.
+chmod 0755 "$OUTDIR" 2>/dev/null || true
+
+# Match the published ports in docker-compose.multinode-clickhouse.yml.
+# role default-host default-port default-db
+ROLES=(
+ "ops localhost 9300 posthog"
+ "logs localhost 9500 posthog"
+)
+
+# Pin to the same chschema build as bin/hclexp; override via repo variable.
+HCLEXP_IMAGE="${HCLEXP_IMAGE:-ghcr.io/posthog/chschema:sha-c0affa0}"
+
+# hclexp that can reach ClickHouse on the host's published ports. Prefer a local
+# binary; otherwise a container sharing the host network namespace so localhost
+# resolves to the published compose ports (works on Linux CI; on macOS set
+# HCLEXP_BIN to a locally built binary).
+run_hclexp() {
+ if [[ -n "${HCLEXP_BIN:-}" ]]; then
+ "$HCLEXP_BIN" "$@"
+ return
+ fi
+ local tmp="${TMPDIR:-/tmp}"; tmp="${tmp%/}"
+ docker run --rm --network host -v "$PWD:/work" -v "$tmp:$tmp" -w /work "$HCLEXP_IMAGE" "$@"
+}
+
+rc=0
+for spec in "${ROLES[@]}"; do
+ read -r role dhost dport ddb <<<"$spec"
+ uc="$(printf '%s' "$role" | tr '[:lower:]' '[:upper:]')"
+ eval "host=\"\${${uc}_HOST:-$dhost}\""
+ eval "port=\"\${${uc}_PORT:-$dport}\""
+ eval "db=\"\${${uc}_DB:-$ddb}\""
+
+ out="$OUTDIR/$ENV-$role.hcl"
+ echo "== dump $ENV/$role from $host:$port/$db -> $out ==" >&2
+ # Write via stdout (`-out -`) and redirect on the host, NOT `-out <file>`: the
+ # hclexp container runs as a nonroot uid that differs from the runner's, so it
+ # can't create files in the runner-owned dump dir (permission denied). Letting
+ # the host shell write the file sidesteps the uid mismatch. pipefail makes a
+ # failed introspect fail the redirect too.
+ if ! run_hclexp introspect -host "$host" -port "$port" -database "$db" \
+ -user "$CH_USER" -password "$CH_PASSWORD" \
+ -node "$role" -exclude "$EXCLUDE" -out - > "$out"; then
+ echo "FAIL: introspect $ENV/$role ($host:$port/$db)" >&2
+ rc=1
+ fi
+ chmod 0644 "$out" 2>/dev/null || true # readable by the nonroot container in check-live
+done
+
+echo "$OUTDIR"
+exit $rc
diff --git a/posthog/clickhouse/hcl/exclude.hcl b/posthog/clickhouse/hcl/exclude.hcl
new file mode 100644
index 000000000000..05d25aedefd4
--- /dev/null
+++ b/posthog/clickhouse/hcl/exclude.hcl
@@ -0,0 +1,49 @@
+# Objects skipped when introspecting a live OPS/LOGS node for the convergence
+# gate (verify-live.sh). A name matching any glob (filepath.Match syntax) is
+# dropped before its DDL is parsed, so it neither lands in the introspected HCL
+# nor is compared against the golden. Patterns match both the bare name
+# ("events_main") and the db-qualified form ("posthog.events_main").
+#
+# Two classes live here:
+# 1. Transient objects (atomic-replace temporaries, migration/backfill scratch,
+# backups) — never part of the managed schema.
+# 2. Cross-cluster proxies present on the node but intentionally NOT authored in
+# the OPS/LOGS golden — they belong to another role's managed set (e.g. the
+# events_* distributed proxies the OPS node carries so it can query the main
+# cluster). These are the same names check.sh lists in its validate SKIP.
+#
+# Grow this list from what the reconciliation pass surfaces — anything the live
+# node has that the golden intentionally omits goes here, with a one-line reason.
+
+exclude {
+ patterns = [
+ # --- transient (ClickHouse atomic CREATE-OR-REPLACE / EXCHANGE) ---
+ "_tmp_replace_*",
+
+ # --- migration / ORM / backfill scratch ---
+ "tmp_*",
+ "*_tmp",
+ "infi_clickhouse_orm_migrations*",
+
+ # --- backups / staging / backfills ---
+ "*_backup",
+ "*_backup_*",
+ "*_staging",
+ "*_backfill",
+
+ # --- cross-cluster proxies carried by the node but owned elsewhere ---
+ # Distributed proxies into the main event cluster; not part of OPS/LOGS.
+ "events_main",
+ "events_recent",
+
+ # --- out-of-band managed: real on prod, not created by the local
+ # migrate_clickhouse path, so the gate ignores them on BOTH sides until
+ # a proper OPS migration reproduces them locally. Remove each entry once
+ # its migration lands. ---
+ # custom_metrics* views are created on NodeRole.DATA (migration 0117), never
+ # on the OPS node, yet prod OPS carries them (created out-of-band).
+ "custom_metrics*",
+ # Orphan: present on prod OPS but no migration or code creates it anywhere.
+ "events_team_daily_stats",
+ ]
+}
diff --git a/posthog/clickhouse/hcl/golden/dev-ops.hcl b/posthog/clickhouse/hcl/golden/dev-ops.hcl
index 2c579b165ce4..b0ee41dd8c30 100644
--- a/posthog/clickhouse/hcl/golden/dev-ops.hcl
+++ b/posthog/clickhouse/hcl/golden/dev-ops.hcl
@@ -1575,38 +1575,4 @@ SELECT
SQL
}
-
- view "daily_aggregated_query_log_archive" {
- query = <<SQL
-SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label
-SQL
-
- }
}
diff --git a/posthog/clickhouse/hcl/golden/local-ops.hcl b/posthog/clickhouse/hcl/golden/local-ops.hcl
index fa45b275627d..c87095c2add2 100644
--- a/posthog/clickhouse/hcl/golden/local-ops.hcl
+++ b/posthog/clickhouse/hcl/golden/local-ops.hcl
@@ -1317,38 +1317,4 @@ SELECT
SQL
}
-
- view "daily_aggregated_query_log_archive" {
- query = <<SQL
-SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label
-SQL
-
- }
}
diff --git a/posthog/clickhouse/hcl/golden/prod-eu-ops.hcl b/posthog/clickhouse/hcl/golden/prod-eu-ops.hcl
index 1e577ffc538f..182b8035f29c 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu-ops.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu-ops.hcl
@@ -1645,38 +1645,4 @@ SELECT
SQL
}
-
- view "daily_aggregated_query_log_archive" {
- query = <<SQL
-SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label
-SQL
-
- }
}
diff --git a/posthog/clickhouse/hcl/golden/prod-us-ops.hcl b/posthog/clickhouse/hcl/golden/prod-us-ops.hcl
index b8f5e47ccb42..9815a3b905e8 100644
--- a/posthog/clickhouse/hcl/golden/prod-us-ops.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us-ops.hcl
@@ -1732,38 +1732,4 @@ SELECT
SQL
}
-
- view "daily_aggregated_query_log_archive" {
- query = <<SQL
-SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label
-SQL
-
- }
}
diff --git a/posthog/clickhouse/hcl/roles/ops/shared/core.hcl b/posthog/clickhouse/hcl/roles/ops/shared/core.hcl
index 59b4cc58c538..9b62dafaaaa8 100644
--- a/posthog/clickhouse/hcl/roles/ops/shared/core.hcl
+++ b/posthog/clickhouse/hcl/roles/ops/shared/core.hcl
@@ -513,9 +513,6 @@ database "posthog" {
replica_name = "{replica}-{shard}"
}
}
- view "daily_aggregated_query_log_archive" {
- query = file("sql/daily_aggregated_query_log_archive.sql")
- }
view "custom_metrics" {
query = file("sql/custom_metrics.sql")
}
diff --git a/posthog/clickhouse/hcl/roles/ops/shared/sql/daily_aggregated_query_log_archive.sql b/posthog/clickhouse/hcl/roles/ops/shared/sql/daily_aggregated_query_log_archive.sql
deleted file mode 100644
index 4007d8448faa..000000000000
--- a/posthog/clickhouse/hcl/roles/ops/shared/sql/daily_aggregated_query_log_archive.sql
+++ /dev/null
@@ -1,28 +0,0 @@
-SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label
diff --git a/posthog/clickhouse/hcl/sql/dev-ops.sql b/posthog/clickhouse/hcl/sql/dev-ops.sql
index 889ca2a83d70..13b2f8be1fa9 100644
--- a/posthog/clickhouse/hcl/sql/dev-ops.sql
+++ b/posthog/clickhouse/hcl/sql/dev-ops.sql
@@ -252,34 +252,6 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
-CREATE VIEW posthog.daily_aggregated_query_log_archive AS SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label;
CREATE TABLE posthog.prom_metrics (
id UUID DEFAULT reinterpretAsUUID(sipHash128(metric_name, all_tags)),
timestamp DateTime64(3),
diff --git a/posthog/clickhouse/hcl/sql/local-ops.sql b/posthog/clickhouse/hcl/sql/local-ops.sql
index bda04eea146e..7b3c48cc7c2a 100644
--- a/posthog/clickhouse/hcl/sql/local-ops.sql
+++ b/posthog/clickhouse/hcl/sql/local-ops.sql
@@ -214,34 +214,6 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
-CREATE VIEW posthog.daily_aggregated_query_log_archive AS SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label;
CREATE TABLE posthog.query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
diff --git a/posthog/clickhouse/hcl/sql/prod-eu-ops.sql b/posthog/clickhouse/hcl/sql/prod-eu-ops.sql
index d7b89dc5ad71..d18899fdfe18 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu-ops.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu-ops.sql
@@ -305,34 +305,6 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
-CREATE VIEW posthog.daily_aggregated_query_log_archive AS SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label;
CREATE TABLE posthog.query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
diff --git a/posthog/clickhouse/hcl/sql/prod-us-ops.sql b/posthog/clickhouse/hcl/sql/prod-us-ops.sql
index daaef93c0e98..9adb8860bf2a 100644
--- a/posthog/clickhouse/hcl/sql/prod-us-ops.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us-ops.sql
@@ -333,34 +333,6 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
-CREATE VIEW posthog.daily_aggregated_query_log_archive AS SELECT
- event_date AS day,
- team_id,
- user,
- current_database,
- query_kind,
- lc_kind,
- lc_access_method,
- lc_query_type,
- lc_product,
- lc_name,
- lc_feature,
- lc_query__kind,
- lc_api_key_label,
- count() AS query_count,
- sum(read_bytes) AS read_bytes,
- sum(read_rows) AS read_rows,
- sum(query_duration_ms) AS query_duration_ms,
- sum(ProfileEvents_OSCPUVirtualTimeMicroseconds) AS cpu_microseconds,
- countIf(exception_code != 0) AS error_count,
- countIf(exception_code IN (159, 160, 241)) AS timeout_oom_count
-FROM posthog.sharded_query_log_archive
-WHERE
- is_initial_query
-AND
- (event_date < today())
-GROUP BY
- day, team_id, user, current_database, query_kind, lc_kind, lc_access_method, lc_query_type, lc_product, lc_name, lc_feature, lc_query__kind, lc_api_key_label;
CREATE TABLE posthog.query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
diff --git a/posthog/clickhouse/query_log_archive.py b/posthog/clickhouse/query_log_archive.py
index 2d0d8084bd93..65dadc10b7a4 100644
--- a/posthog/clickhouse/query_log_archive.py
+++ b/posthog/clickhouse/query_log_archive.py
@@ -817,7 +817,7 @@ SELECT
stack_trace,
JSONExtractInt(log_comment, 'team_id') as team_id,
- log_comment,
+ if(isValidJSON(log_comment), log_comment, '{}') AS log_comment,
ProfileEvents
FROM system.query_log
WHERE
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 42cd7e680b0c..2a4284ed6776 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -3793,7 +3793,7 @@
stack_trace,
JSONExtractInt(log_comment, 'team_id') as team_id,
- log_comment,
+ if(isValidJSON(log_comment), log_comment, '{}') AS log_comment,
ProfileEvents
FROM system.query_log
WHERE
diff --git a/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke b/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke
index 1ef15c3739ae..801c44823b4b 100755
--- a/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke
+++ b/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke
@@ -193,6 +193,20 @@ SMOKE_STAGE="verify"
echo "==> Verifying per-node table layout..."
python tools/infra-scripts/clickhouse-multinode/verify-multinode-clickhouse.py
+# Convergence gate: the migrations just applied must reproduce the declarative
+# HCL golden for the managed OPS/LOGS roles. Two steps — (1) introspect those
+# nodes into HCL dumps, (2) diff the dumps against posthog/clickhouse/hcl/golden/.
+# Enforced by default (drift fails the smoke); export VERIFY_LIVE_WARN=1 to make
+# it informational (report drift but pass) while reconciling.
+SMOKE_STAGE="dump_live_hcl"
+echo "==> Dumping live OPS/LOGS schema..."
+LIVE_DUMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/ch-live-dump.XXXXXX")"
+bash posthog/clickhouse/hcl/dump-live.sh "$LIVE_DUMP_DIR" >/dev/null
+
+SMOKE_STAGE="check_live_hcl"
+echo "==> Comparing dumped schema against the HCL golden..."
+VERIFY_LIVE_WARN="${VERIFY_LIVE_WARN:-0}" bash posthog/clickhouse/hcl/check-live.sh "$LIVE_DUMP_DIR"
+
SMOKE_PASSED=1
SMOKE_STAGE="teardown"