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): dedup env MV variants via patch_materialized_view (#78726)

Paweł Szczurd45a38406726
diff --git a/posthog/clickhouse/hcl/bin/image.txt b/posthog/clickhouse/hcl/bin/image.txt
index 343987c481ab..a0bf6526dea2 100644
--- a/posthog/clickhouse/hcl/bin/image.txt
+++ b/posthog/clickhouse/hcl/bin/image.txt
@@ -1 +1 @@
-ghcr.io/posthog/chschema:sha-5a3a7ce
+ghcr.io/posthog/chschema:sha-f72d69a
diff --git a/posthog/clickhouse/hcl/duplicates-baseline.txt b/posthog/clickhouse/hcl/duplicates-baseline.txt
index e28c27f7400b..f3b3c3f3684c 100644
--- a/posthog/clickhouse/hcl/duplicates-baseline.txt
+++ b/posthog/clickhouse/hcl/duplicates-baseline.txt
@@ -1,15 +1,9 @@
# Objects still declared in >=2 composed layers (database.name), sorted.
# The locate -duplicates guard in check.sh fails on any duplicate NOT listed here.
# This list only shrinks. Every remaining entry has a named blocker:
-# - kafka_logs34_avro_mv, kafka_logs_avro_billing_metrics_mv,
-# kafka_trace_spans_avro_mv: materialized views whose query differs per env;
-# hclexp has no override = true and no patch form for MVs yet (chschema #169).
# - events, raw_sessions_v3, channel_definition: events-family replicas whose
# env copies carry mat_ columns; they resolve when the cloud env overlays
# move to posthog-cloud-infra (Phase 2), not by a restructure here.
posthog.channel_definition
posthog.events
-posthog.kafka_logs34_avro_mv
-posthog.kafka_logs_avro_billing_metrics_mv
-posthog.kafka_trace_spans_avro_mv
posthog.raw_sessions_v3
diff --git a/posthog/clickhouse/hcl/manifest.hcl b/posthog/clickhouse/hcl/manifest.hcl
index 9c59a8ba51b1..cea412922818 100644
--- a/posthog/clickhouse/hcl/manifest.hcl
+++ b/posthog/clickhouse/hcl/manifest.hcl
@@ -37,7 +37,7 @@ role "ops" {
# than the shared cloud layers.
role "logs" {
env "local-multi" { layers = ["roles/shared/qla.hcl", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/local"] }
- env "dev" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/shared", "roles/logs/dev"] }
+ env "dev" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/shared"] }
env "prod-us" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/shared", "roles/logs/prod", "roles/logs/prod-us"] }
env "prod-eu" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/traces", "roles/logs/shared", "roles/logs/prod", "roles/logs/prod-eu"] }
}
diff --git a/posthog/clickhouse/hcl/migration.md b/posthog/clickhouse/hcl/migration.md
index 914eab55518b..c9f95244feda 100644
--- a/posthog/clickhouse/hcl/migration.md
+++ b/posthog/clickhouse/hcl/migration.md
@@ -99,7 +99,7 @@ Each table shall be **defined** once, if there's a difference between envs, it s
`hclexp locate -duplicates` draws exactly this line (verified against the pin: table+`patch_table` → 0, table+`override=true` → 0, table+plain redeclaration → 1, plain+`extend` redeclaration → 1, `extend`+`extend` → 1), so it is the enforcement mechanism, not a heuristic. Both repos gate on it against a `duplicates-baseline.txt` that may only shrink.
-The patch vocabulary covers tables fully, so "express it as a patch" is always available for a table content difference. `patch_table` carries `column` and `index` (both with positional `after`), `modify_column` (full replacement column spec — `type` is required), `engine`, `order_by`, `partition_by`, `ttl` and `settings`; `patch_view` and `patch_dictionary` do the same for views and dictionaries. `settings` merge into the target with the patch winning on collision; everything else replaces. Built on demand in PostHog/chschema — #153 (settings merge), #156 (full vocab + `patch_view`/`patch_dictionary`), #159 (positioned column adds), #161 (positioned index adds). Two gaps remain: projections are not patchable (a table with an env-extra projection needs `override = true`), and materialized views have neither `override = true` nor a patch form — an MV whose query differs per env must stay declared per env, parked in the baseline.
+The patch vocabulary covers tables and materialized views fully, so "express it as a patch" is always available for a content difference. `patch_table` carries `column` and `index` (both with positional `after`), `modify_column` (full replacement column spec — `type` is required), additive `projection`, `engine`, `order_by`, `partition_by`, `ttl` and `settings`; `patch_view` and `patch_dictionary` do the same for views and dictionaries, and `patch_materialized_view` carries `query` (replace) plus the column operations — an MV whose query differs per env stays declared once with per-env query patches. `settings` merge into the target with the patch winning on collision; everything else replaces. Built on demand in PostHog/chschema — #153 (settings merge), #156 (full vocab + `patch_view`/`patch_dictionary`), #159 (positioned column adds), #161 (positioned index adds), #170 (`patch_materialized_view`, MV `override = true`, projections in `patch_table`).
`patch_column` (#165) is the `extend` side of the same idea: a child using `extend` can specialize a single _inherited_ column — type, nullability, default kind, CODEC, TTL, comment — while keeping every unspecified field and the inherited column order. A plain `column` block on a child still means _add_, and still collides with an inherited name. This is what lets one codec-free abstract back both a storage table and its Distributed proxy, with only the storage child declaring CODECs:
@@ -115,7 +115,7 @@ table "sharded_events" {
Before that, the choice was to hang the CODECs on the abstract — forcing them onto the proxy too — or to stop sharing and repeat the column list.
-In this repo the cross-_role_ duplicates have been factored into `roles/coshared/<member-set>/` layers (one layer per set of stacks that co-host the objects), with env deltas as `patch_*` blocks in the env layers. What keeps the baseline non-empty is exactly the two vocabulary gaps above (per-env materialized views) plus the `mat_`-column-bearing events-family replicas, which resolve when the cloud env overlays move to posthog-cloud-infra rather than by any restructure here.
+In this repo the cross-_role_ duplicates have been factored into `roles/coshared/<member-set>/` layers (one layer per set of stacks that co-host the objects), with env deltas as `patch_*` blocks in the env layers. What keeps the baseline non-empty is only the `mat_`-column-bearing events-family replicas, which resolve when the cloud env overlays move to posthog-cloud-infra rather than by any restructure here.
The purpose of the extension is to making the schema changes uniform across all envs: think adding a column or table shall be possible in one place and affect all envs.
diff --git a/posthog/clickhouse/hcl/roles/logs/dev/sql/kafka_logs34_avro_mv.sql b/posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs34_avro_mv.sql
similarity index 100%
rename from posthog/clickhouse/hcl/roles/logs/dev/sql/kafka_logs34_avro_mv.sql
rename to posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs34_avro_mv.sql
diff --git a/posthog/clickhouse/hcl/roles/logs/dev/sql/kafka_logs_avro_billing_metrics_mv.sql b/posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs_avro_billing_metrics_mv.sql
similarity index 100%
rename from posthog/clickhouse/hcl/roles/logs/dev/sql/kafka_logs_avro_billing_metrics_mv.sql
rename to posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs_avro_billing_metrics_mv.sql
diff --git a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
index 92c706d82822..dccddde576cd 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
@@ -997,4 +997,98 @@ database "posthog" {
}
}
+ materialized_view "kafka_logs34_avro_mv" {
+ to_table = "posthog.logs34"
+ query = file("sql/kafka_logs34_avro_mv.sql")
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "trace_flags" {
+ type = "Int32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "body" {
+ type = "String"
+ }
+ column "severity_text" {
+ type = "String"
+ }
+ column "severity_number" {
+ type = "Int32"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "event_name" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(String, String)"
+ }
+ column "resource_attributes" {
+ type = "Map(String, String)"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "_partition" {
+ type = "UInt64"
+ }
+ column "_topic" {
+ type = "LowCardinality(String)"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_record_count" {
+ type = "Int64"
+ }
+ column "_bytes_uncompressed" {
+ type = "Nullable(Int64)"
+ }
+ column "_bytes_compressed" {
+ type = "Nullable(Int64)"
+ }
+ }
+
+ materialized_view "kafka_logs_avro_billing_metrics_mv" {
+ to_table = "posthog.logs_billing_metrics"
+ query = file("sql/kafka_logs_avro_billing_metrics_mv.sql")
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "bytes_uncompressed" {
+ type = "SimpleAggregateFunction(sum, Float64)"
+ }
+ column "bytes_compressed" {
+ type = "SimpleAggregateFunction(sum, Float64)"
+ }
+ column "record_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
}
diff --git a/posthog/clickhouse/hcl/roles/logs/dev/tables.hcl b/posthog/clickhouse/hcl/roles/logs/dev/tables.hcl
deleted file mode 100644
index d1390b9d4087..000000000000
--- a/posthog/clickhouse/hcl/roles/logs/dev/tables.hcl
+++ /dev/null
@@ -1,97 +0,0 @@
-# LOGS dev overlay — env-specific / differing logs objects.
-database "posthog" {
-
- materialized_view "kafka_logs34_avro_mv" {
- to_table = "posthog.logs34"
- query = file("sql/kafka_logs34_avro_mv.sql")
- column "uuid" {
- type = "String"
- }
- column "trace_id" {
- type = "String"
- }
- column "span_id" {
- type = "String"
- }
- column "trace_flags" {
- type = "Int32"
- }
- column "timestamp" {
- type = "DateTime64(6)"
- }
- column "observed_timestamp" {
- type = "DateTime64(6)"
- }
- column "body" {
- type = "String"
- }
- column "severity_text" {
- type = "String"
- }
- column "severity_number" {
- type = "Int32"
- }
- column "service_name" {
- type = "String"
- }
- column "instrumentation_scope" {
- type = "String"
- }
- column "event_name" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(String, String)"
- }
- column "resource_attributes" {
- type = "Map(String, String)"
- }
- column "team_id" {
- type = "Int32"
- }
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
- }
- column "_partition" {
- type = "UInt64"
- }
- column "_topic" {
- type = "LowCardinality(String)"
- }
- column "_offset" {
- type = "UInt64"
- }
- column "_record_count" {
- type = "Int64"
- }
- column "_bytes_uncompressed" {
- type = "Nullable(Int64)"
- }
- column "_bytes_compressed" {
- type = "Nullable(Int64)"
- }
- }
-
- materialized_view "kafka_logs_avro_billing_metrics_mv" {
- to_table = "posthog.logs_billing_metrics"
- query = file("sql/kafka_logs_avro_billing_metrics_mv.sql")
- column "team_id" {
- type = "Int32"
- }
- column "time_bucket" {
- type = "DateTime"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "bytes_uncompressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
- }
- column "bytes_compressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
- }
- column "record_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
- }
- }
-}
diff --git a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
index 03329e7bc311..f071071d834a 100644
--- a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
@@ -402,162 +402,7 @@ SQL
replica_name = "{replica}-{shard}"
}
}
- table "logs34" {
- override = true
- order_by = ["team_id", "time_bucket", "service_name", "resource_fingerprint", "severity_text", "timestamp"]
- partition_by = "toDate(original_expiry_timestamp)"
- ttl = "original_expiry_timestamp"
- settings = {
- add_minmax_index_for_numeric_columns = "1"
- allow_experimental_reverse_key = "1"
- index_granularity = "8192"
- index_granularity_bytes = "104857600"
- map_serialization_version = "with_buckets"
- ttl_only_drop_parts = "1"
- }
- column "time_bucket" {
- type = "DateTime"
- materialized = "toStartOfDay(timestamp)"
- }
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
- }
- column "uuid" {
- type = "String"
- }
- column "team_id" {
- type = "Int32"
- }
- column "trace_id" {
- type = "String"
- }
- column "span_id" {
- type = "String"
- }
- column "trace_flags" {
- type = "Int32"
- }
- column "timestamp" {
- type = "DateTime64(6)"
- codec = "DoubleDelta"
- }
- column "observed_timestamp" {
- type = "DateTime64(6)"
- }
- column "created_at" {
- type = "DateTime64(6)"
- materialized = "now()"
- }
- column "body" {
- type = "String"
- }
- column "severity_text" {
- type = "LowCardinality(String)"
- }
- column "severity_number" {
- type = "Int32"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "resource_attributes" {
- type = "Map(LowCardinality(String), String)"
- }
- column "resource_fingerprint" {
- type = "UInt64"
- materialized = "cityHash64(resource_attributes)"
- }
- column "instrumentation_scope" {
- type = "String"
- }
- column "event_name" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(LowCardinality(String), String)"
- }
- column "level" {
- type = "String"
- alias = "severity_text"
- }
- column "mat_body_ipv4_matches" {
- type = "Array(String)"
- alias = "extractAll(body, '(\\\\d\\\\.((25[0-5]|(2[0-4]|1(0, 1)[0-9])(0, 1)[0-9])\\\\.)(2, 2)([0-9]))')"
- }
- column "time_minute" {
- type = "DateTime"
- alias = "toStartOfMinute(timestamp)"
- }
- column "attributes" {
- type = "Map(LowCardinality(String), String)"
- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
- }
- column "attributes_map_float" {
- type = "Map(LowCardinality(String), Float64)"
- materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"
- }
- column "attributes_map_datetime" {
- type = "Map(LowCardinality(String), DateTime64(6))"
- materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
- }
- column "_partition" {
- type = "UInt32"
- }
- column "_topic" {
- type = "String"
- }
- column "_offset" {
- type = "UInt64"
- }
- column "_bytes_uncompressed" {
- type = "UInt64"
- }
- column "_bytes_compressed" {
- type = "UInt64"
- }
- column "_record_count" {
- type = "UInt64"
- }
- index "idx_severity_text_set" {
- expr = "severity_text"
- type = "set(10)"
- granularity = 1
- }
- index "idx_attributes_str_keys" {
- expr = "mapKeys(attributes_map_str)"
- type = "bloom_filter(0.01)"
- granularity = 1
- }
- index "idx_attributes_str_values" {
- expr = "mapValues(attributes_map_str)"
- type = "bloom_filter(0.001)"
- granularity = 1
- }
- index "idx_mat_body_ipv4_matches" {
- expr = "mat_body_ipv4_matches"
- type = "bloom_filter(0.01)"
- granularity = 1
- }
- index "idx_body_ngram3" {
- expr = "lower(body)"
- type = "ngrambf_v1(3, 25000, 2, 0)"
- granularity = 1
- }
- index "idx_uuid_bloom" {
- expr = "uuid"
- type = "bloom_filter(0.01)"
- granularity = 1
- }
- index "idx_observed_minmax" {
- expr = "observed_timestamp"
- type = "minmax"
- granularity = 1
- }
- index "idx_timestamp_minmax" {
- expr = "timestamp"
- type = "minmax"
- granularity = 1
- }
+ patch_table "logs34" {
projection "projection_aggregate_counts" {
query = <<SQL
SELECT
@@ -578,9 +423,8 @@ SQL
replica_name = "{replica}-{shard}"
}
}
- materialized_view "kafka_logs34_avro_mv" {
- to_table = "posthog.logs34"
- query = <<SQL
+ patch_materialized_view "kafka_logs34_avro_mv" {
+ query = <<SQL
SELECT
kafka_logs_avro.* EXCEPT(created_at, attribute_values, attribute_keys, attributes, attributes_map_str, attributes_map_float, attributes_map_datetime, resource_attributes, bytes_uncompressed),
mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
@@ -599,76 +443,9 @@ SELECT
FROM posthog.kafka_logs_avro
SQL
- column "uuid" {
- type = "String"
- }
- column "trace_id" {
- type = "String"
- }
- column "span_id" {
- type = "String"
- }
- column "trace_flags" {
- type = "Int32"
- }
- column "timestamp" {
- type = "DateTime64(6)"
- }
- column "observed_timestamp" {
- type = "DateTime64(6)"
- }
- column "body" {
- type = "String"
- }
- column "severity_text" {
- type = "String"
- }
- column "severity_number" {
- type = "Int32"
- }
- column "service_name" {
- type = "String"
- }
- column "instrumentation_scope" {
- type = "String"
- }
- column "event_name" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(String, String)"
- }
- column "resource_attributes" {
- type = "Map(String, String)"
- }
- column "team_id" {
- type = "Int32"
- }
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
- }
- column "_partition" {
- type = "UInt64"
- }
- column "_topic" {
- type = "LowCardinality(String)"
- }
- column "_offset" {
- type = "UInt64"
- }
- column "_record_count" {
- type = "Int64"
- }
- column "_bytes_uncompressed" {
- type = "Nullable(Int64)"
- }
- column "_bytes_compressed" {
- type = "Nullable(Int64)"
- }
}
- materialized_view "kafka_logs_avro_billing_metrics_mv" {
- to_table = "posthog.logs_billing_metrics"
- query = <<SQL
+ patch_materialized_view "kafka_logs_avro_billing_metrics_mv" {
+ query = <<SQL
SELECT
team_id,
time_bucket,
@@ -690,24 +467,6 @@ GROUP BY
team_id, time_bucket, service_name
SQL
- column "team_id" {
- type = "Int32"
- }
- column "time_bucket" {
- type = "DateTime"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "bytes_uncompressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
- }
- column "bytes_compressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
- }
- column "record_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
- }
}
materialized_view "logs32_to_log_attributes" {
to_table = "posthog.log_attributes"
@@ -831,187 +590,7 @@ SQL
}
}
- table "trace_spans" {
- override = true
- order_by = ["team_id", "time_bucket", "service_name", "resource_fingerprint", "status_code", "name", "timestamp"]
- partition_by = "toDate(original_expiry_timestamp)"
- ttl = "original_expiry_timestamp"
- settings = {
- allow_part_offset_column_in_projections = "1"
- index_granularity = "8192"
- index_granularity_bytes = "104857600"
- map_serialization_version = "with_buckets"
- ttl_only_drop_parts = "1"
- }
- column "time_bucket" {
- type = "DateTime"
- materialized = "toStartOfInterval(timestamp, toIntervalHour(4))"
- }
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
- }
- column "uuid" {
- type = "String"
- }
- column "team_id" {
- type = "Int32"
- }
- column "trace_id" {
- type = "String"
- }
- column "span_id" {
- type = "String"
- }
- column "parent_span_id" {
- type = "String"
- }
- column "is_root_span" {
- type = "Bool"
- materialized = "replaceAll(trimRight(parent_span_id, '='), 'A', '') = ''"
- }
- column "trace_state" {
- type = "String"
- }
- column "name" {
- type = "LowCardinality(String)"
- }
- column "kind" {
- type = "Int8"
- }
- column "flags" {
- type = "UInt32"
- }
- column "timestamp" {
- type = "DateTime64(6)"
- }
- column "end_time" {
- type = "DateTime64(6)"
- }
- column "observed_timestamp" {
- type = "DateTime64(6)"
- }
- column "created_at" {
- type = "DateTime64(6)"
- materialized = "now()"
- }
- column "duration_nano" {
- type = "UInt64"
- materialized = "toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000"
- }
- column "status_code" {
- type = "Int16"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "resource_attributes" {
- type = "Map(LowCardinality(String), String)"
- }
- column "resource_fingerprint" {
- type = "UInt64"
- materialized = "cityHash64(resource_attributes)"
- }
- column "instrumentation_scope" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(LowCardinality(String), String)"
- }
- column "attributes" {
- type = "Map(LowCardinality(String), String)"
- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
- }
- column "attributes_map_float" {
- type = "Map(LowCardinality(String), Float64)"
- materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"
- }
- column "attributes_map_datetime" {
- type = "Map(LowCardinality(String), DateTime64(6))"
- materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
- }
- column "dropped_attributes_count" {
- type = "UInt32"
- }
- column "dropped_events_count" {
- type = "UInt32"
- }
- column "dropped_links_count" {
- type = "UInt32"
- }
- column "events" {
- type = "Array(String)"
- }
- column "links" {
- type = "Array(String)"
- }
- column "_partition" {
- type = "UInt32"
- }
- column "_topic" {
- type = "String"
- }
- column "_offset" {
- type = "UInt64"
- }
- column "_bytes_uncompressed" {
- type = "UInt64"
- }
- column "_bytes_compressed" {
- type = "UInt64"
- }
- column "_record_count" {
- type = "UInt64"
- }
- index "idx_name" {
- expr = "name"
- type = "ngrambf_v1(4, 5000, 2, 0)"
- granularity = 16
- }
- index "idx_kind" {
- expr = "kind"
- type = "minmax"
- granularity = 4
- }
- index "idx_duration" {
- expr = "duration_nano"
- type = "minmax"
- granularity = 1
- }
- index "idx_status_code" {
- expr = "status_code"
- type = "minmax"
- granularity = 1
- }
- index "idx_timestamp_minmax" {
- expr = "timestamp"
- type = "minmax"
- granularity = 1
- }
- index "idx_observed_minmax" {
- expr = "observed_timestamp"
- type = "minmax"
- granularity = 1
- }
- index "idx_attributes_str_keys" {
- expr = "mapKeys(attributes_map_str)"
- type = "bloom_filter(0.01)"
- granularity = 16
- }
- index "idx_attributes_str_values" {
- expr = "mapValues(attributes_map_str)"
- type = "bloom_filter(0.001)"
- granularity = 16
- }
- index "idx_trace_bloom_part" {
- expr = "trace_id"
- type = "bloom_filter(0.00001)"
- granularity = 99999
- }
- index "idx_span_id_bloom_part" {
- expr = "span_id"
- type = "bloom_filter(0.00001)"
- granularity = 99999
- }
+ patch_table "trace_spans" {
projection "projection_aggregate_counts" {
query = <<SQL
SELECT
@@ -1282,9 +861,8 @@ SQL
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "kafka_trace_spans_avro_mv" {
- to_table = "posthog.trace_spans"
- query = <<SQL
+ patch_materialized_view "kafka_trace_spans_avro_mv" {
+ query = <<SQL
SELECT
* EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
toInt8(kind) AS kind,
@@ -1309,74 +887,9 @@ SELECT
FROM posthog.kafka_trace_spans_avro
SQL
- column "uuid" {
- type = "String"
- }
- column "trace_id" {
- type = "String"
- }
- column "span_id" {
- type = "String"
- }
- column "parent_span_id" {
- type = "String"
- }
- column "trace_state" {
- type = "String"
- }
- column "name" {
- type = "String"
- }
- column "kind" {
- type = "Int8"
- }
- column "flags" {
- type = "UInt32"
- }
- column "timestamp" {
- type = "DateTime64(6)"
- }
- column "end_time" {
- type = "DateTime64(6)"
- }
- column "observed_timestamp" {
- type = "DateTime64(6)"
- }
- column "service_name" {
- type = "String"
- }
- column "resource_attributes" {
- type = "Map(LowCardinality(String), String)"
- }
- column "instrumentation_scope" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(LowCardinality(String), String)"
- }
- column "dropped_attributes_count" {
- type = "UInt32"
- }
- column "events" {
- type = "Array(String)"
- }
- column "dropped_events_count" {
- type = "UInt32"
- }
- column "links" {
- type = "Array(String)"
- }
- column "dropped_links_count" {
- type = "UInt32"
- }
- column "status_code" {
- type = "Int16"
- }
- column "team_id" {
- type = "Int32"
- }
column "original_expiry_timestamp" {
- type = "DateTime64(6)"
+ type = "DateTime64(6)"
+ after = "team_id"
}
}
}
diff --git a/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl b/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl
index aa6b7a7ede3d..d18aef49598b 100644
--- a/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl
@@ -30,6 +30,12 @@ database "posthog" {
thread_per_consumer = true
}
}
+ patch_materialized_view "kafka_logs34_avro_mv" {
+ query = file("sql/kafka_logs34_avro_mv.sql")
+ }
+ patch_materialized_view "kafka_logs_avro_billing_metrics_mv" {
+ query = file("sql/kafka_logs_avro_billing_metrics_mv.sql")
+ }
patch_table "trace_spans_distributed" {
column "_partition" {
type = "UInt32"
diff --git a/posthog/clickhouse/hcl/roles/logs/prod-eu/tables.hcl b/posthog/clickhouse/hcl/roles/logs/prod-eu/tables.hcl
deleted file mode 100644
index c54f3d4cfc3c..000000000000
--- a/posthog/clickhouse/hcl/roles/logs/prod-eu/tables.hcl
+++ /dev/null
@@ -1,95 +0,0 @@
-# LOGS prod-eu overlay — env-specific / differing logs objects.
-database "posthog" {
- materialized_view "kafka_logs34_avro_mv" {
- to_table = "posthog.logs34"
- query = file("sql/kafka_logs34_avro_mv.sql")
- column "uuid" {
- type = "String"
- }
- column "trace_id" {
- type = "String"
- }
- column "span_id" {
- type = "String"
- }
- column "trace_flags" {
- type = "Int32"
- }
- column "timestamp" {
- type = "DateTime64(6)"
- }
- column "observed_timestamp" {
- type = "DateTime64(6)"
- }
- column "body" {
- type = "String"
- }
- column "severity_text" {
- type = "String"
- }
- column "severity_number" {
- type = "Int32"
- }
- column "service_name" {
- type = "String"
- }
- column "instrumentation_scope" {
- type = "String"
- }
- column "event_name" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(String, String)"
- }
- column "resource_attributes" {
- type = "Map(String, String)"
- }
- column "team_id" {
- type = "Int32"
- }
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
- }
- column "_partition" {
- type = "UInt64"
- }
- column "_topic" {
- type = "LowCardinality(String)"
- }
- column "_offset" {
- type = "UInt64"
- }
- column "_record_count" {
- type = "Int64"
- }
- column "_bytes_uncompressed" {
- type = "Nullable(Int64)"
- }
- column "_bytes_compressed" {
- type = "Nullable(Int64)"
- }
- }
- materialized_view "kafka_logs_avro_billing_metrics_mv" {
- to_table = "posthog.logs_billing_metrics"
- query = file("sql/kafka_logs_avro_billing_metrics_mv.sql")
- column "team_id" {
- type = "Int32"
- }
- column "time_bucket" {
- type = "DateTime"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "bytes_uncompressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
- }
- column "bytes_compressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
- }
- column "record_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
- }
- }
-}
diff --git a/posthog/clickhouse/hcl/roles/logs/prod-us/sql/kafka_logs34_avro_mv.sql b/posthog/clickhouse/hcl/roles/logs/prod-us/sql/kafka_logs34_avro_mv.sql
deleted file mode 100644
index a1132b907b05..000000000000
--- a/posthog/clickhouse/hcl/roles/logs/prod-us/sql/kafka_logs34_avro_mv.sql
+++ /dev/null
@@ -1,16 +0,0 @@
-SELECT
- kafka_logs_avro.* EXCEPT(created_at, attribute_values, attribute_keys, attributes, attributes_map_str, attributes_map_float, attributes_map_datetime, resource_attributes),
- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
- observed_timestamp
- + toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
- ) AS original_expiry_timestamp,
- _partition,
- _topic,
- _offset,
- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,
- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,
- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed
-FROM posthog.kafka_logs_avro
diff --git a/posthog/clickhouse/hcl/roles/logs/prod-us/sql/kafka_logs_avro_billing_metrics_mv.sql b/posthog/clickhouse/hcl/roles/logs/prod-us/sql/kafka_logs_avro_billing_metrics_mv.sql
deleted file mode 100644
index 2dce06860d5e..000000000000
--- a/posthog/clickhouse/hcl/roles/logs/prod-us/sql/kafka_logs_avro_billing_metrics_mv.sql
+++ /dev/null
@@ -1,20 +0,0 @@
-SELECT
- team_id,
- time_bucket,
- service_name,
- sumSimpleState(floor(_bytes_uncompressed / _record_count)) AS bytes_uncompressed,
- sumSimpleState(floor(_bytes_compressed / _record_count)) AS bytes_compressed,
- sumSimpleState(1) AS record_count
-FROM
- (
- SELECT
- team_id,
- toStartOfInterval(timestamp, toIntervalMinute(1)) AS time_bucket,
- service_name AS service_name,
- _record_count,
- _bytes_uncompressed,
- _bytes_compressed
- FROM posthog.logs34
- )
-GROUP BY
- team_id, time_bucket, service_name
diff --git a/posthog/clickhouse/hcl/roles/logs/prod-us/tables.hcl b/posthog/clickhouse/hcl/roles/logs/prod-us/tables.hcl
deleted file mode 100644
index 5c37f6ecba19..000000000000
--- a/posthog/clickhouse/hcl/roles/logs/prod-us/tables.hcl
+++ /dev/null
@@ -1,96 +0,0 @@
-# LOGS prod-us overlay — env-specific / differing logs objects.
-database "posthog" {
- materialized_view "kafka_logs34_avro_mv" {
- to_table = "posthog.logs34"
- query = file("sql/kafka_logs34_avro_mv.sql")
- column "uuid" {
- type = "String"
- }
- column "trace_id" {
- type = "String"
- }
- column "span_id" {
- type = "String"
- }
- column "trace_flags" {
- type = "Int32"
- }
- column "timestamp" {
- type = "DateTime64(6)"
- }
- column "observed_timestamp" {
- type = "DateTime64(6)"
- }
- column "body" {
- type = "String"
- }
- column "severity_text" {
- type = "String"
- }
- column "severity_number" {
- type = "Int32"
- }
- column "service_name" {
- type = "String"
- }
- column "instrumentation_scope" {
- type = "String"
- }
- column "event_name" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(String, String)"
- }
- column "resource_attributes" {
- type = "Map(String, String)"
- }
- column "team_id" {
- type = "Int32"
- }
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
- }
- column "_partition" {
- type = "UInt64"
- }
- column "_topic" {
- type = "LowCardinality(String)"
- }
- column "_offset" {
- type = "UInt64"
- }
- column "_record_count" {
- type = "Int64"
- }
- column "_bytes_uncompressed" {
- type = "Nullable(Int64)"
- }
- column "_bytes_compressed" {
- type = "Nullable(Int64)"
- }
- }
-
- materialized_view "kafka_logs_avro_billing_metrics_mv" {
- to_table = "posthog.logs_billing_metrics"
- query = file("sql/kafka_logs_avro_billing_metrics_mv.sql")
- column "team_id" {
- type = "Int32"
- }
- column "time_bucket" {
- type = "DateTime"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "bytes_uncompressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
- }
- column "bytes_compressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
- }
- column "record_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
- }
- }
-}
diff --git a/posthog/clickhouse/hcl/roles/logs/prod/tables.hcl b/posthog/clickhouse/hcl/roles/logs/prod/tables.hcl
index 4907662b6cfd..c441e3db4f3e 100644
--- a/posthog/clickhouse/hcl/roles/logs/prod/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/prod/tables.hcl
@@ -80,75 +80,4 @@ database "posthog" {
type = "DateTime64(6)"
}
}
-
- materialized_view "kafka_trace_spans_avro_mv" {
- to_table = "posthog.trace_spans"
- query = file("sql/kafka_trace_spans_avro_mv.sql")
- column "uuid" {
- type = "String"
- }
- column "trace_id" {
- type = "String"
- }
- column "span_id" {
- type = "String"
- }
- column "parent_span_id" {
- type = "String"
- }
- column "trace_state" {
- type = "String"
- }
- column "name" {
- type = "String"
- }
- column "kind" {
- type = "Int8"
- }
- column "flags" {
- type = "UInt32"
- }
- column "timestamp" {
- type = "DateTime64(6)"
- }
- column "end_time" {
- type = "DateTime64(6)"
- }
- column "observed_timestamp" {
- type = "DateTime64(6)"
- }
- column "service_name" {
- type = "String"
- }
- column "resource_attributes" {
- type = "Map(LowCardinality(String), String)"
- }
- column "instrumentation_scope" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(LowCardinality(String), String)"
- }
- column "dropped_attributes_count" {
- type = "UInt32"
- }
- column "events" {
- type = "Array(String)"
- }
- column "dropped_events_count" {
- type = "UInt32"
- }
- column "links" {
- type = "Array(String)"
- }
- column "dropped_links_count" {
- type = "UInt32"
- }
- column "status_code" {
- type = "Int16"
- }
- column "team_id" {
- type = "Int32"
- }
- }
}
diff --git a/posthog/clickhouse/hcl/roles/logs/prod/sql/kafka_trace_spans_avro_mv.sql b/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql
similarity index 100%
rename from posthog/clickhouse/hcl/roles/logs/prod/sql/kafka_trace_spans_avro_mv.sql
rename to posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql
diff --git a/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl b/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
index bf21a1535473..36d71a3df868 100644
--- a/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
@@ -1,7 +1,6 @@
# The trace-spans suite, hosted by the local logs node and both prod logs envs
-# (dev has no traces). Env deltas are patch_table blocks in the env layers; the
-# ingest MV (kafka_trace_spans_avro_mv) stays per-env until hclexp supports
-# overriding materialized views.
+# (dev has no traces). Env deltas are patch_table / patch_materialized_view
+# blocks in the env layers, never second definitions.
database "posthog" {
table "trace_attributes" {
order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
@@ -567,4 +566,75 @@ database "posthog" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
+
+ materialized_view "kafka_trace_spans_avro_mv" {
+ to_table = "posthog.trace_spans"
+ query = file("sql/kafka_trace_spans_avro_mv.sql")
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "String"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "status_code" {
+ type = "Int16"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ }
}