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.

feat(metrics): new metrics3 schema tables (#100839)

Frank Hamand945cb3f3da5d
diff --git a/docs/internal/metrics-catalog.md b/docs/internal/metrics-catalog.md
index e666abe8d7ff..286ee71057ee 100644
--- a/docs/internal/metrics-catalog.md
+++ b/docs/internal/metrics-catalog.md
@@ -24,3 +24,65 @@ The card gives the shared `Sparkline` component `w-full h-full` inside a fixed-h
The component tests check the POST batch after the queue delay.
The Storybook tests use an explicit container width and wait for a visible chart.
They do not wait for every loading placeholder to disappear: offscreen cards stay unloaded until they enter view.
+
+## Metadata migration
+
+Migration `0323_metrics_metadata3_dual_write` adds three metadata tables on the logs cluster.
+`metric_series3` partitions by `toDate(original_expiry_timestamp)` and keeps `ReplacingMergeTree(last_seen)`.
+Merges retain the latest labelled row for each series within each expiry day.
+The expiry timestamp still controls row retention.
+Expiry partitions keep rows with different expiry dates in separate parts, so later expiry dates do not delay removal of earlier parts.
+A query for one activity day can read several expiry partitions.
+Series rows do not provide complete daily activity history.
+
+`metric_attributes3` includes `metric_name` in its columns, sort key, and materialized view grouping.
+Both metric attributes and resource attributes include the metric name.
+The sort key also includes `service_name` and `original_expiry_time_bucket` to keep their counts and retention separate during merges.
+The hourly buckets, expiry partitions, label filter, and attribute length limits match `metric_attributes2`.
+
+`metric_names3` stores metric names in hourly activity buckets, partitioned by expiry day.
+The separate `original_expiry_time_bucket` column has type `DateTime64(0)` and contains the source expiry timestamp rounded down to the hour.
+The partition key is `toDate(original_expiry_time_bucket)`.
+Its sort key is `(team_id, time_bucket, metric_name, original_expiry_time_bucket)` to support recent name lists for one project.
+The view grouping and sort key include the expiry hour, so rows with different expiry hours cannot merge.
+Its materialized view reads only samples where `has_labels` is true, including samples with empty attribute maps.
+`AggregatingMergeTree` combines rows across series and services for each project, activity hour, name, and expiry hour.
+It retains the maximum `original_expiry_timestamp` within each group. This aggregate controls TTL and remains separate from both bucket columns.
+The table records activity from labelled samples, not every sample.
+Time filters use the hourly buckets, so discovery can include activity outside the requested range within its boundary hours.
+Later readers must use `DISTINCT metric_name` because merges are asynchronous and names repeat across activity and expiry buckets.
+Queries with other label filters still need series data.
+
+Four new materialized views read `metrics2_input` and write to the new tables.
+The existing views continue to write to `metric_series2` and `metric_attributes2`.
+The distributed tables and HogQL schemas still read the existing tables.
+This phase adds storage and insert work; it does not improve query speed until reads move to the new tables.
+
+### Backfill and read cutover
+
+The migration does not copy historical rows.
+Backfill the new destination tables directly after all four materialized views exist on each logs replica.
+Do not replay a backfill through `metrics2_input`, because its existing views would also write the raw samples and old metadata again.
+
+Use a source boundary that excludes rows already written by the new views.
+Attribute counts use addition, so overlapping writes or repeated backfill batches can count rows twice.
+A timestamp boundary alone does not exclude late samples.
+Preserve the original expiry timestamps in the backfill.
+
+`metric_series2` retains only the latest row for each series after merges.
+Copying it cannot restore rows for earlier expiry partitions.
+Use retained samples and the matching series labels to populate those partitions with the original expiry timestamps.
+`metric_attributes2` does not retain metric names, so it cannot supply the new attribute rows alone.
+Preserve the `has_labels` filter when rebuilding attribute counts from samples.
+
+For `metric_names3`, use only source samples with `has_labels` set.
+Group by project, metric name, and the sample timestamp rounded to the start of its hour.
+Also group by `original_expiry_time_bucket`, computed from the source expiry timestamp rounded down to the hour.
+Keep the maximum original expiry timestamp for each group.
+Repeated catalog rows merge with `max`, so overlapping catalog batches do not add counts or shorten retention.
+The latest series rows alone cannot restore earlier hourly activity.
+
+Before a read cutover, compare metric names, label pairs, series presence per expiry day, and retention for the same source range.
+Check insert latency, materialized view errors, part counts, and storage growth during this phase.
+A later migration can move the distributed readers after the backfill is complete.
+Keep the old tables until that read cutover is stable.
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 84ab631e0045..a825e2af2fc7 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -940,6 +940,67 @@ SQL
}
}
+ table "metric_attributes3" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_attributes_distributed" {
column "team_id" {
type = "Int32"
@@ -972,6 +1033,34 @@ SQL
}
}
+ table "metric_names3" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_names3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_samples" {
column "team_id" {
type = "Int32"
@@ -1264,6 +1353,90 @@ SQL
}
}
+ table "metric_series3" {
+ order_by = ["team_id", "metric_name", "series_fingerprint"]
+ partition_by = "toDate(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ default = "false"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ index "idx_service_set" {
+ expr = "service_name"
+ type = "set(1000)"
+ granularity = 1
+ }
+ index "idx_resource_fingerprint" {
+ expr = "resource_fingerprint"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_keys" {
+ expr = "mapKeys(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_values" {
+ expr = "mapValues(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series3"
+ replica_name = "{replica}-{shard}"
+ version_column = "last_seen"
+ }
+ }
+
table "metric_series_distributed" {
column "team_id" {
type = "Int32"
@@ -3350,6 +3523,101 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "metrics2_input_to_metric_names3" {
+ to_table = "posthog.metric_names3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
materialized_view "metrics2_input_to_metric_series" {
to_table = "posthog.metric_series2"
query = <<SQL
@@ -3412,6 +3680,68 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_series3" {
+ to_table = "posthog.metric_series3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "metrics2_input_to_metrics" {
to_table = "posthog.metrics2"
query = <<SQL
@@ -3575,6 +3905,69 @@ SQL
}
}
+ materialized_view "metrics2_input_to_resource_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "ops_query_log_archive_mv" {
to_table = "posthog.writable_query_log_archive"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 87d146c28936..0a5f53cae972 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -1504,6 +1504,67 @@ SQL
}
}
+ table "metric_attributes3" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_attributes_distributed" {
column "team_id" {
type = "Int32"
@@ -1536,6 +1597,34 @@ SQL
}
}
+ table "metric_names3" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_names3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_samples" {
column "team_id" {
type = "Int32"
@@ -1828,6 +1917,90 @@ SQL
}
}
+ table "metric_series3" {
+ order_by = ["team_id", "metric_name", "series_fingerprint"]
+ partition_by = "toDate(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ default = "false"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ index "idx_service_set" {
+ expr = "service_name"
+ type = "set(1000)"
+ granularity = 1
+ }
+ index "idx_resource_fingerprint" {
+ expr = "resource_fingerprint"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_keys" {
+ expr = "mapKeys(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_values" {
+ expr = "mapValues(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series3"
+ replica_name = "{replica}-{shard}"
+ version_column = "last_seen"
+ }
+ }
+
table "metric_series_distributed" {
column "team_id" {
type = "Int32"
@@ -4094,6 +4267,101 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "metrics2_input_to_metric_names3" {
+ to_table = "posthog.metric_names3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
materialized_view "metrics2_input_to_metric_series" {
to_table = "posthog.metric_series2"
query = <<SQL
@@ -4156,6 +4424,68 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_series3" {
+ to_table = "posthog.metric_series3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "metrics2_input_to_metrics" {
to_table = "posthog.metrics2"
query = <<SQL
@@ -4319,6 +4649,69 @@ SQL
}
}
+ materialized_view "metrics2_input_to_resource_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_span_to_attributes" {
to_table = "posthog.trace_attributes"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index b444d9e12dde..36e8fe3e2b19 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -6373,6 +6373,67 @@ SQL
}
}
+ table "metric_attributes3" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_attributes_distributed" {
column "team_id" {
type = "Int32"
@@ -6405,6 +6466,34 @@ SQL
}
}
+ table "metric_names3" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_names3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_samples" {
column "team_id" {
type = "Int32"
@@ -6697,6 +6786,90 @@ SQL
}
}
+ table "metric_series3" {
+ order_by = ["team_id", "metric_name", "series_fingerprint"]
+ partition_by = "toDate(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ default = "false"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ index "idx_service_set" {
+ expr = "service_name"
+ type = "set(1000)"
+ granularity = 1
+ }
+ index "idx_resource_fingerprint" {
+ expr = "resource_fingerprint"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_keys" {
+ expr = "mapKeys(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_values" {
+ expr = "mapValues(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series3"
+ replica_name = "{replica}-{shard}"
+ version_column = "last_seen"
+ }
+ }
+
table "metric_series_distributed" {
column "team_id" {
type = "Int32"
@@ -21277,6 +21450,101 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "metrics2_input_to_metric_names3" {
+ to_table = "posthog.metric_names3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
materialized_view "metrics2_input_to_metric_series" {
to_table = "posthog.metric_series2"
query = <<SQL
@@ -21339,6 +21607,68 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_series3" {
+ to_table = "posthog.metric_series3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "metrics2_input_to_metrics" {
to_table = "posthog.metrics2"
query = <<SQL
@@ -21502,6 +21832,69 @@ SQL
}
}
+ materialized_view "metrics2_input_to_resource_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "ops_query_log_archive_mv" {
to_table = "posthog.writable_query_log_archive"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index 99aa4e2baa9e..ce7ac64c0a31 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -1090,6 +1090,67 @@ SQL
}
}
+ table "metric_attributes3" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_attributes_distributed" {
column "team_id" {
type = "Int32"
@@ -1122,6 +1183,34 @@ SQL
}
}
+ table "metric_names3" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_names3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_samples" {
column "team_id" {
type = "Int32"
@@ -1414,6 +1503,90 @@ SQL
}
}
+ table "metric_series3" {
+ order_by = ["team_id", "metric_name", "series_fingerprint"]
+ partition_by = "toDate(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ default = "false"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ index "idx_service_set" {
+ expr = "service_name"
+ type = "set(1000)"
+ granularity = 1
+ }
+ index "idx_resource_fingerprint" {
+ expr = "resource_fingerprint"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_keys" {
+ expr = "mapKeys(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_values" {
+ expr = "mapValues(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series3"
+ replica_name = "{replica}-{shard}"
+ version_column = "last_seen"
+ }
+ }
+
table "metric_series_distributed" {
column "team_id" {
type = "Int32"
@@ -3676,6 +3849,101 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "metrics2_input_to_metric_names3" {
+ to_table = "posthog.metric_names3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
materialized_view "metrics2_input_to_metric_series" {
to_table = "posthog.metric_series2"
query = <<SQL
@@ -3738,6 +4006,68 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_series3" {
+ to_table = "posthog.metric_series3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "metrics2_input_to_metrics" {
to_table = "posthog.metrics2"
query = <<SQL
@@ -3901,6 +4231,69 @@ SQL
}
}
+ materialized_view "metrics2_input_to_resource_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "ops_query_log_archive_mv" {
to_table = "posthog.writable_query_log_archive"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index e566f4b7574e..3003e1d95fdb 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -1090,6 +1090,67 @@ SQL
}
}
+ table "metric_attributes3" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_attributes_distributed" {
column "team_id" {
type = "Int32"
@@ -1122,6 +1183,34 @@ SQL
}
}
+ table "metric_names3" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_names3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "metric_samples" {
column "team_id" {
type = "Int32"
@@ -1414,6 +1503,90 @@ SQL
}
}
+ table "metric_series3" {
+ order_by = ["team_id", "metric_name", "series_fingerprint"]
+ partition_by = "toDate(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ default = "false"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ index "idx_service_set" {
+ expr = "service_name"
+ type = "set(1000)"
+ granularity = 1
+ }
+ index "idx_resource_fingerprint" {
+ expr = "resource_fingerprint"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_keys" {
+ expr = "mapKeys(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_values" {
+ expr = "mapValues(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series3"
+ replica_name = "{replica}-{shard}"
+ version_column = "last_seen"
+ }
+ }
+
table "metric_series_distributed" {
column "team_id" {
type = "Int32"
@@ -3658,6 +3831,101 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "metrics2_input_to_metric_names3" {
+ to_table = "posthog.metric_names3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
materialized_view "metrics2_input_to_metric_series" {
to_table = "posthog.metric_series2"
query = <<SQL
@@ -3720,6 +3988,68 @@ SQL
}
}
+ materialized_view "metrics2_input_to_metric_series3" {
+ to_table = "posthog.metric_series3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "metrics2_input_to_metrics" {
to_table = "posthog.metrics2"
query = <<SQL
@@ -3883,6 +4213,69 @@ SQL
}
}
+ materialized_view "metrics2_input_to_resource_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "ops_query_log_archive_mv" {
to_table = "posthog.writable_query_log_archive"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/metadata3.hcl b/posthog/clickhouse/hcl/roles/logs/metrics/metadata3.hcl
new file mode 100644
index 000000000000..4015af11797e
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/metadata3.hcl
@@ -0,0 +1,296 @@
+database "posthog" {
+ table "metric_names3" {
+ partition_by = "toDate(original_expiry_time_bucket)"
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_names3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+ materialized_view "metrics2_input_to_metric_names3" {
+ to_table = "posthog.metric_names3"
+ query = file("sql/metrics2_input_to_metric_names3.sql")
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+ table "metric_series3" {
+ partition_by = "toDate(original_expiry_timestamp)"
+ order_by = ["team_id", "metric_name", "series_fingerprint"]
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ default = "false"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ index "idx_service_set" {
+ expr = "service_name"
+ type = "set(1000)"
+ granularity = 1
+ }
+ index "idx_resource_fingerprint" {
+ expr = "resource_fingerprint"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_keys" {
+ expr = "mapKeys(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attr_values" {
+ expr = "mapValues(attributes)"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series3"
+ replica_name = "{replica}-{shard}"
+ version_column = "last_seen"
+ }
+ }
+ table "metric_attributes3" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes3"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+ materialized_view "metrics2_input_to_metric_series3" {
+ to_table = "posthog.metric_series3"
+ query = file("sql/metrics2_input_to_metric_series3.sql")
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "metric_type" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "aggregation_temporality" {
+ type = "LowCardinality(String)"
+ }
+ column "is_monotonic" {
+ type = "Bool"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+ materialized_view "metrics2_input_to_metric_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = file("sql/metrics2_input_to_metric_attributes3.sql")
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+ materialized_view "metrics2_input_to_resource_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = file("sql/metrics2_input_to_resource_attributes3.sql")
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+}
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_attributes3.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_attributes3.sql
new file mode 100644
index 000000000000..880a76e211d6
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_attributes3.sql
@@ -0,0 +1,29 @@
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_names3.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_names3.sql
new file mode 100644
index 000000000000..78a4b1a53c24
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_names3.sql
@@ -0,0 +1,9 @@
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_series3.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_series3.sql
new file mode 100644
index 000000000000..43e893aebd5c
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_series3.sql
@@ -0,0 +1,16 @@
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_resource_attributes3.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_resource_attributes3.sql
new file mode 100644
index 000000000000..91acde351cdb
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_resource_attributes3.sql
@@ -0,0 +1,29 @@
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index c8fa667fa4d4..ff0c7b218990 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -258,6 +258,21 @@ CREATE TABLE posthog.metric_attributes2 (
INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metric_attributes3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes3', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metric_attributes_distributed (
team_id Int32,
time_bucket DateTime64(0),
@@ -268,6 +283,13 @@ CREATE TABLE posthog.metric_attributes_distributed (
attribute_type LowCardinality(String),
attribute_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'metric_attributes2');
+CREATE TABLE posthog.metric_names3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_names3', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_samples1 (
team_id Int32,
metric_name LowCardinality(String),
@@ -319,6 +341,27 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metric_series3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ metric_type LowCardinality(String),
+ unit LowCardinality(String),
+ aggregation_temporality LowCardinality(String),
+ is_monotonic Bool DEFAULT false,
+ service_name LowCardinality(String),
+ instrumentation_scope String,
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ attributes Map(LowCardinality(String), String),
+ last_seen DateTime64(6),
+ original_expiry_timestamp DateTime64(6),
+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series3', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -967,6 +1010,45 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_names3 TO posthog.metric_names3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
team_id,
metric_name,
@@ -983,6 +1065,22 @@ CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metr
original_expiry_timestamp
FROM posthog.metrics2_input
WHERE has_labels;
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series3 TO posthog.metric_series3 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT
team_id,
metric_name,
@@ -1036,6 +1134,35 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
CREATE MATERIALIZED VIEW posthog.ops_query_log_archive_mv TO posthog.writable_query_log_archive (hostname LowCardinality(String), user LowCardinality(String), query_id String, initial_query_id String, is_initial_query UInt8, type Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4), event_date Date, event_time DateTime, event_time_microseconds DateTime64(6), query_start_time DateTime, query_start_time_microseconds DateTime64(6), query_duration_ms UInt64, read_rows UInt64, read_bytes UInt64, written_rows UInt64, written_bytes UInt64, result_rows UInt64, result_bytes UInt64, memory_usage UInt64, peak_threads_usage UInt64, current_database LowCardinality(String), query String, formatted_query String, normalized_query_hash UInt64, query_kind LowCardinality(String), exception_code Int32, exception String, stack_trace String, team_id Int64, log_comment String, ProfileEvents Map(LowCardinality(String), UInt64)) AS SELECT
hostname,
user,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index 71a31b4f0828..e0cae83022bf 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -371,6 +371,21 @@ CREATE TABLE posthog.metric_attributes2 (
INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metric_attributes3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes3', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metric_attributes_distributed (
team_id Int32,
time_bucket DateTime64(0),
@@ -381,6 +396,13 @@ CREATE TABLE posthog.metric_attributes_distributed (
attribute_type LowCardinality(String),
attribute_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'metric_attributes2');
+CREATE TABLE posthog.metric_names3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_names3', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_samples1 (
team_id Int32,
metric_name LowCardinality(String),
@@ -432,6 +454,27 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metric_series3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ metric_type LowCardinality(String),
+ unit LowCardinality(String),
+ aggregation_temporality LowCardinality(String),
+ is_monotonic Bool DEFAULT false,
+ service_name LowCardinality(String),
+ instrumentation_scope String,
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ attributes Map(LowCardinality(String), String),
+ last_seen DateTime64(6),
+ original_expiry_timestamp DateTime64(6),
+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series3', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -1140,6 +1183,45 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_names3 TO posthog.metric_names3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
team_id,
metric_name,
@@ -1156,6 +1238,22 @@ CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metr
original_expiry_timestamp
FROM posthog.metrics2_input
WHERE has_labels;
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series3 TO posthog.metric_series3 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT
team_id,
metric_name,
@@ -1209,6 +1307,35 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
CREATE MATERIALIZED VIEW posthog.trace_span_to_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
original_expiry_time_bucket,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index ce73acb11c07..c4a38acadc2f 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -1262,6 +1262,21 @@ CREATE TABLE posthog.metric_attributes2 (
INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metric_attributes3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes3', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metric_attributes_distributed (
team_id Int32,
time_bucket DateTime64(0),
@@ -1272,6 +1287,13 @@ CREATE TABLE posthog.metric_attributes_distributed (
attribute_type LowCardinality(String),
attribute_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'metric_attributes2');
+CREATE TABLE posthog.metric_names3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_names3', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_samples1 (
team_id Int32,
metric_name LowCardinality(String),
@@ -1323,6 +1345,27 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metric_series3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ metric_type LowCardinality(String),
+ unit LowCardinality(String),
+ aggregation_temporality LowCardinality(String),
+ is_monotonic Bool DEFAULT false,
+ service_name LowCardinality(String),
+ instrumentation_scope String,
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ attributes Map(LowCardinality(String), String),
+ last_seen DateTime64(6),
+ original_expiry_timestamp DateTime64(6),
+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series3', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -5004,6 +5047,45 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_names3 TO posthog.metric_names3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
team_id,
metric_name,
@@ -5020,6 +5102,22 @@ CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metr
original_expiry_timestamp
FROM posthog.metrics2_input
WHERE has_labels;
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series3 TO posthog.metric_series3 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT
team_id,
metric_name,
@@ -5073,6 +5171,35 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
CREATE MATERIALIZED VIEW posthog.performance_events_mv TO posthog.writeable_performance_events (uuid UUID, session_id String, window_id String, pageview_id String, distinct_id String, timestamp DateTime64(3), time_origin DateTime64(3, 'UTC'), entry_type LowCardinality(String), name String, team_id Int64, current_url String, start_time Float64, duration Float64, redirect_start Float64, redirect_end Float64, worker_start Float64, fetch_start Float64, domain_lookup_start Float64, domain_lookup_end Float64, connect_start Float64, secure_connection_start Float64, connect_end Float64, request_start Float64, response_start Float64, response_end Float64, decoded_body_size Int64, encoded_body_size Int64, initiator_type LowCardinality(String), next_hop_protocol LowCardinality(String), render_blocking_status LowCardinality(String), response_status Int64, transfer_size Int64, largest_contentful_paint_element String, largest_contentful_paint_render_time Float64, largest_contentful_paint_load_time Float64, largest_contentful_paint_size Float64, largest_contentful_paint_id String, largest_contentful_paint_url String, dom_complete Float64, dom_content_loaded_event Float64, dom_interactive Float64, load_event_end Float64, load_event_start Float64, redirect_count Int64, navigation_type LowCardinality(String), unload_event_end Float64, unload_event_start Float64, _timestamp Nullable(DateTime), _offset UInt64, _partition UInt64) AS SELECT
uuid,
session_id,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index b2e0c4bd2f44..663c9b82f195 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -303,6 +303,21 @@ CREATE TABLE posthog.metric_attributes2 (
INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metric_attributes3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes3', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metric_attributes_distributed (
team_id Int32,
time_bucket DateTime64(0),
@@ -313,6 +328,13 @@ CREATE TABLE posthog.metric_attributes_distributed (
attribute_type LowCardinality(String),
attribute_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'metric_attributes2');
+CREATE TABLE posthog.metric_names3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_names3', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_samples1 (
team_id Int32,
metric_name LowCardinality(String),
@@ -364,6 +386,27 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metric_series3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ metric_type LowCardinality(String),
+ unit LowCardinality(String),
+ aggregation_temporality LowCardinality(String),
+ is_monotonic Bool DEFAULT false,
+ service_name LowCardinality(String),
+ instrumentation_scope String,
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ attributes Map(LowCardinality(String), String),
+ last_seen DateTime64(6),
+ original_expiry_timestamp DateTime64(6),
+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series3', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -1060,6 +1103,45 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_names3 TO posthog.metric_names3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
team_id,
metric_name,
@@ -1076,6 +1158,22 @@ CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metr
original_expiry_timestamp
FROM posthog.metrics2_input
WHERE has_labels;
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series3 TO posthog.metric_series3 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT
team_id,
metric_name,
@@ -1129,6 +1227,35 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
CREATE MATERIALIZED VIEW posthog.ops_query_log_archive_mv TO posthog.writable_query_log_archive (hostname LowCardinality(String), user LowCardinality(String), query_id String, initial_query_id String, is_initial_query UInt8, type Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4), event_date Date, event_time DateTime, event_time_microseconds DateTime64(6), query_start_time DateTime, query_start_time_microseconds DateTime64(6), query_duration_ms UInt64, read_rows UInt64, read_bytes UInt64, written_rows UInt64, written_bytes UInt64, result_rows UInt64, result_bytes UInt64, memory_usage UInt64, peak_threads_usage UInt64, current_database LowCardinality(String), query String, formatted_query String, normalized_query_hash UInt64, query_kind LowCardinality(String), exception_code Int32, exception String, stack_trace String, team_id Int64, log_comment String, ProfileEvents Map(LowCardinality(String), UInt64)) AS SELECT
hostname,
user,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index 1ff859fcf4d0..05ccfe3c80ed 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -303,6 +303,21 @@ CREATE TABLE posthog.metric_attributes2 (
INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metric_attributes3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes3', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metric_attributes_distributed (
team_id Int32,
time_bucket DateTime64(0),
@@ -313,6 +328,13 @@ CREATE TABLE posthog.metric_attributes_distributed (
attribute_type LowCardinality(String),
attribute_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'metric_attributes2');
+CREATE TABLE posthog.metric_names3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_names3', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_samples1 (
team_id Int32,
metric_name LowCardinality(String),
@@ -364,6 +386,27 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metric_series3 (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ metric_type LowCardinality(String),
+ unit LowCardinality(String),
+ aggregation_temporality LowCardinality(String),
+ is_monotonic Bool DEFAULT false,
+ service_name LowCardinality(String),
+ instrumentation_scope String,
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ attributes Map(LowCardinality(String), String),
+ last_seen DateTime64(6),
+ original_expiry_timestamp DateTime64(6),
+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series3', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -1054,6 +1097,45 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_names3 TO posthog.metric_names3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics2_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
team_id,
metric_name,
@@ -1070,6 +1152,22 @@ CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metr
original_expiry_timestamp
FROM posthog.metrics2_input
WHERE has_labels;
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series3 TO posthog.metric_series3 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM posthog.metrics2_input
+WHERE has_labels;
CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT
team_id,
metric_name,
@@ -1123,6 +1221,35 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes3 TO posthog.metric_attributes3 (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
CREATE MATERIALIZED VIEW posthog.ops_query_log_archive_mv TO posthog.writable_query_log_archive (hostname LowCardinality(String), user LowCardinality(String), query_id String, initial_query_id String, is_initial_query UInt8, type Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4), event_date Date, event_time DateTime, event_time_microseconds DateTime64(6), query_start_time DateTime, query_start_time_microseconds DateTime64(6), query_duration_ms UInt64, read_rows UInt64, read_bytes UInt64, written_rows UInt64, written_bytes UInt64, result_rows UInt64, result_bytes UInt64, memory_usage UInt64, peak_threads_usage UInt64, current_database LowCardinality(String), query String, formatted_query String, normalized_query_hash UInt64, query_kind LowCardinality(String), exception_code Int32, exception String, stack_trace String, team_id Int64, log_comment String, ProfileEvents Map(LowCardinality(String), UInt64)) AS SELECT
hostname,
user,
diff --git a/posthog/clickhouse/metrics/__init__.py b/posthog/clickhouse/metrics/__init__.py
index 42753f78b11a..a8ea6f8e38a3 100644
--- a/posthog/clickhouse/metrics/__init__.py
+++ b/posthog/clickhouse/metrics/__init__.py
@@ -34,8 +34,24 @@ from .metrics2 import (
METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,
METRICS2_TABLE_SQL,
)
+from .metrics3 import (
+ METRIC_ATTRIBUTES3_TABLE_SQL,
+ METRIC_NAMES3_TABLE_SQL,
+ METRIC_SERIES3_TABLE_SQL,
+ METRICS2_INPUT_TO_METRIC_ATTRIBUTES3_MV,
+ METRICS2_INPUT_TO_METRIC_NAMES3_MV,
+ METRICS2_INPUT_TO_METRIC_SERIES3_MV,
+ METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV,
+)
__all__ = [
+ "METRIC_ATTRIBUTES3_TABLE_SQL",
+ "METRIC_NAMES3_TABLE_SQL",
+ "METRIC_SERIES3_TABLE_SQL",
+ "METRICS2_INPUT_TO_METRIC_ATTRIBUTES3_MV",
+ "METRICS2_INPUT_TO_METRIC_NAMES3_MV",
+ "METRICS2_INPUT_TO_METRIC_SERIES3_MV",
+ "METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV",
"KAFKA_METRICS_AVRO2_MV",
"KAFKA_METRICS_AVRO2_TABLE_SQL",
"METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL",
diff --git a/posthog/clickhouse/metrics/metrics3.py b/posthog/clickhouse/metrics/metrics3.py
new file mode 100644
index 000000000000..d835e4a3e854
--- /dev/null
+++ b/posthog/clickhouse/metrics/metrics3.py
@@ -0,0 +1,202 @@
+from django.conf import settings
+
+from posthog.clickhouse.table_engines import AggregatingMergeTree, ReplacingMergeTree, ReplicationScheme
+
+from .metrics2 import METRICS2_INPUT_TABLE_NAME
+
+METRIC_SERIES3_TABLE_NAME = "metric_series3"
+METRIC_ATTRIBUTES3_TABLE_NAME = "metric_attributes3"
+METRIC_NAMES3_TABLE_NAME = "metric_names3"
+
+
+def _db() -> str:
+ return settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+
+def METRIC_SERIES3_TABLE_SQL() -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {_db()}.{METRIC_SERIES3_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `series_fingerprint` UInt64 CODEC(Delta(8), Default),
+ `metric_type` LowCardinality(String),
+ `unit` LowCardinality(String),
+ `aggregation_temporality` LowCardinality(String),
+ `is_monotonic` Bool DEFAULT false,
+ `service_name` LowCardinality(String),
+ `instrumentation_scope` String,
+ `resource_attributes` Map(LowCardinality(String), String),
+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),
+ `attributes` Map(LowCardinality(String), String),
+ `last_seen` DateTime64(6),
+ `original_expiry_timestamp` DateTime64(6),
+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
+)
+ENGINE = {ReplacingMergeTree(METRIC_SERIES3_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED, ver="last_seen")}
+PARTITION BY toDate(original_expiry_timestamp)
+ORDER BY (team_id, metric_name, series_fingerprint)
+TTL original_expiry_timestamp
+SETTINGS index_granularity = 8192
+"""
+
+
+def METRIC_ATTRIBUTES3_TABLE_SQL() -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {_db()}.{METRIC_ATTRIBUTES3_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+)
+ENGINE = {AggregatingMergeTree(METRIC_ATTRIBUTES3_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY toDate(original_expiry_time_bucket)
+ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket)
+TTL original_expiry_time_bucket
+SETTINGS
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1
+"""
+
+
+def METRIC_NAMES3_TABLE_SQL() -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {_db()}.{METRIC_NAMES3_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+)
+ENGINE = {AggregatingMergeTree(METRIC_NAMES3_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY toDate(original_expiry_time_bucket)
+ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+TTL original_expiry_timestamp
+SETTINGS index_granularity = 8192
+"""
+
+
+def METRICS2_INPUT_TO_METRIC_NAMES3_MV() -> str:
+ db = _db()
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS2_INPUT_TABLE_NAME}_to_metric_names3 TO {db}.{METRIC_NAMES3_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+)
+AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM {db}.{METRICS2_INPUT_TABLE_NAME} AS input
+WHERE has_labels
+GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket
+"""
+
+
+def METRICS2_INPUT_TO_METRIC_SERIES3_MV() -> str:
+ db = _db()
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS2_INPUT_TABLE_NAME}_to_metric_series3 TO {db}.{METRIC_SERIES3_TABLE_NAME}
+AS SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+FROM {db}.{METRICS2_INPUT_TABLE_NAME}
+WHERE has_labels
+"""
+
+
+def _attributes_mv(view_suffix: str, source_map: str, attribute_type: str, filter_long_pairs: bool) -> str:
+ db = _db()
+ attributes_expr = (
+ f"mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), {source_map})"
+ if filter_long_pairs
+ else source_map
+ )
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS2_INPUT_TABLE_NAME}_to_{view_suffix} TO {db}.{METRIC_ATTRIBUTES3_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64)
+)
+AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+(
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ {attributes_expr} AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ '{attribute_type}' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM {db}.{METRICS2_INPUT_TABLE_NAME}
+ WHERE has_labels
+ GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ filtered_attributes
+)
+"""
+
+
+def METRICS2_INPUT_TO_METRIC_ATTRIBUTES3_MV() -> str:
+ return _attributes_mv("metric_attributes3", "attributes", "metric", filter_long_pairs=True)
+
+
+def METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV() -> str:
+ return _attributes_mv("resource_attributes3", "resource_attributes", "resource", filter_long_pairs=False)
diff --git a/posthog/clickhouse/migrations/0323_metrics_metadata3_dual_write.py b/posthog/clickhouse/migrations/0323_metrics_metadata3_dual_write.py
new file mode 100644
index 000000000000..88a0e2c4d46b
--- /dev/null
+++ b/posthog/clickhouse/migrations/0323_metrics_metadata3_dual_write.py
@@ -0,0 +1,21 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.metrics import (
+ METRIC_ATTRIBUTES3_TABLE_SQL,
+ METRIC_NAMES3_TABLE_SQL,
+ METRIC_SERIES3_TABLE_SQL,
+ METRICS2_INPUT_TO_METRIC_ATTRIBUTES3_MV,
+ METRICS2_INPUT_TO_METRIC_NAMES3_MV,
+ METRICS2_INPUT_TO_METRIC_SERIES3_MV,
+ METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV,
+)
+
+operations = [
+ run_sql_with_exceptions(METRIC_SERIES3_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRIC_ATTRIBUTES3_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRIC_NAMES3_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRICS2_INPUT_TO_METRIC_SERIES3_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRICS2_INPUT_TO_METRIC_ATTRIBUTES3_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRICS2_INPUT_TO_METRIC_NAMES3_MV(), node_roles=[NodeRole.LOGS]),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 481e560876b9..5a95c70ee64f 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0322_add_snapshot_mode_v2_to_session_replay_events
+0323_metrics_metadata3_dual_write
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index 9e984733cc18..3c4e6ea22867 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -36,19 +36,26 @@ from posthog.clickhouse.logs import (
from posthog.clickhouse.metrics import (
METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL,
METRIC_ATTRIBUTES2_TABLE_SQL,
+ METRIC_ATTRIBUTES3_TABLE_SQL,
METRIC_ATTRIBUTES_TABLE_SQL,
+ METRIC_NAMES3_TABLE_SQL,
METRIC_SAMPLES_DISTRIBUTED_TABLE_SQL,
METRIC_SAMPLES_TABLE_SQL,
METRIC_SERIES2_DISTRIBUTED_TABLE_SQL,
METRIC_SERIES2_TABLE_SQL,
+ METRIC_SERIES3_TABLE_SQL,
METRIC_SERIES_DISTRIBUTED_TABLE_SQL,
METRIC_SERIES_TABLE_SQL,
METRICS1_TABLE_SQL,
METRICS2_DISTRIBUTED_TABLE_SQL,
METRICS2_INPUT_TABLE_SQL,
+ METRICS2_INPUT_TO_METRIC_ATTRIBUTES3_MV,
METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV,
+ METRICS2_INPUT_TO_METRIC_NAMES3_MV,
+ METRICS2_INPUT_TO_METRIC_SERIES3_MV,
METRICS2_INPUT_TO_METRIC_SERIES_MV,
METRICS2_INPUT_TO_METRICS_MV,
+ METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV,
METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,
METRICS2_TABLE_SQL,
METRICS_DISTRIBUTED_TABLE_SQL,
@@ -459,7 +466,10 @@ CREATE_MERGETREE_TABLE_QUERIES = (
METRICS2_INPUT_TABLE_SQL,
METRICS2_TABLE_SQL,
METRIC_SERIES2_TABLE_SQL,
+ METRIC_SERIES3_TABLE_SQL,
METRIC_ATTRIBUTES2_TABLE_SQL,
+ METRIC_ATTRIBUTES3_TABLE_SQL,
+ METRIC_NAMES3_TABLE_SQL,
TOPHOG_DATA_TABLE_SQL,
BILLING_USAGE_RECORDS_DATA_TABLE_SQL,
AI_EVENTS_DATA_TABLE_SQL,
@@ -640,8 +650,12 @@ CREATE_MV_TABLE_QUERIES = (
LOGS34_TO_VOLUME_BUCKETS_MV,
METRICS2_INPUT_TO_METRICS_MV,
METRICS2_INPUT_TO_METRIC_SERIES_MV,
+ METRICS2_INPUT_TO_METRIC_SERIES3_MV,
METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV,
+ METRICS2_INPUT_TO_METRIC_ATTRIBUTES3_MV,
+ METRICS2_INPUT_TO_METRIC_NAMES3_MV,
METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,
+ METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV,
TOPHOG_MV_SQL,
BILLING_USAGE_RECORDS_MV_SQL,
AI_EVENTS_MV_SQL,
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 25d88c87de9e..78f622318208 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -4101,6 +4101,35 @@
'''
# ---
+# name: test_create_table_query[metric_attributes3]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metric_attributes3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_attributes3', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket)
+ TTL original_expiry_time_bucket
+ SETTINGS
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query[metric_attributes]
'''
@@ -4131,6 +4160,25 @@
# name: test_create_table_query[metric_attributes_distributed]
"CREATE TABLE IF NOT EXISTS posthog_test.metric_attributes_distributed AS posthog_test.metric_attributes2 ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metric_attributes2')"
# ---
+# name: test_create_table_query[metric_names3]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metric_names3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_names3', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+ TTL original_expiry_timestamp
+ SETTINGS index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query[metric_samples1]
'''
@@ -4219,6 +4267,39 @@
'''
# ---
+# name: test_create_table_query[metric_series3]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metric_series3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `series_fingerprint` UInt64 CODEC(Delta(8), Default),
+ `metric_type` LowCardinality(String),
+ `unit` LowCardinality(String),
+ `aggregation_temporality` LowCardinality(String),
+ `is_monotonic` Bool DEFAULT false,
+ `service_name` LowCardinality(String),
+ `instrumentation_scope` String,
+ `resource_attributes` Map(LowCardinality(String), String),
+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),
+ `attributes` Map(LowCardinality(String), String),
+ `last_seen` DateTime64(6),
+ `original_expiry_timestamp` DateTime64(6),
+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
+ )
+ ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_series3', '{replica}-{shard}', last_seen)
+ PARTITION BY toDate(original_expiry_timestamp)
+ ORDER BY (team_id, metric_name, series_fingerprint)
+ TTL original_expiry_timestamp
+ SETTINGS index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query[metric_series]
"CREATE TABLE IF NOT EXISTS posthog_test.metric_series AS posthog_test.metric_series1 ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metric_series1')"
# ---
@@ -4402,6 +4483,58 @@
'''
# ---
+# name: test_create_table_query[metrics2_input_to_metric_attributes3]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics2_input_to_metric_attributes3 TO posthog_test.metric_attributes3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+ FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog_test.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ filtered_attributes
+ )
+
+ '''
+# ---
# name: test_create_table_query[metrics2_input_to_metric_attributes]
'''
@@ -4450,6 +4583,52 @@
'''
# ---
+# name: test_create_table_query[metrics2_input_to_metric_names3]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics2_input_to_metric_names3 TO posthog_test.metric_names3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ )
+ AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ FROM posthog_test.metrics2_input AS input
+ WHERE has_labels
+ GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket
+
+ '''
+# ---
+# name: test_create_table_query[metrics2_input_to_metric_series3]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics2_input_to_metric_series3 TO posthog_test.metric_series3
+ AS SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp AS last_seen,
+ original_expiry_timestamp
+ FROM posthog_test.metrics2_input
+ WHERE has_labels
+
+ '''
+# ---
# name: test_create_table_query[metrics2_input_to_metric_series]
'''
@@ -4506,6 +4685,58 @@
'''
# ---
+# name: test_create_table_query[metrics2_input_to_resource_attributes3]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics2_input_to_resource_attributes3 TO posthog_test.metric_attributes3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ AS SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+ FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_attributes AS filtered_attributes,
+ arrayJoin(filtered_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog_test.metrics2_input
+ WHERE has_labels
+ GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ filtered_attributes
+ )
+
+ '''
+# ---
# name: test_create_table_query[metrics2_input_to_resource_attributes]
'''
@@ -11170,6 +11401,35 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[metric_attributes3]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metric_attributes3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_attributes3', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket)
+ TTL original_expiry_time_bucket
+ SETTINGS
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[metric_attributes]
'''
@@ -11197,6 +11457,25 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[metric_names3]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metric_names3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_names3', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+ TTL original_expiry_timestamp
+ SETTINGS index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[metric_samples1]
'''
@@ -11282,6 +11561,39 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[metric_series3]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metric_series3
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `series_fingerprint` UInt64 CODEC(Delta(8), Default),
+ `metric_type` LowCardinality(String),
+ `unit` LowCardinality(String),
+ `aggregation_temporality` LowCardinality(String),
+ `is_monotonic` Bool DEFAULT false,
+ `service_name` LowCardinality(String),
+ `instrumentation_scope` String,
+ `resource_attributes` Map(LowCardinality(String), String),
+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),
+ `attributes` Map(LowCardinality(String), String),
+ `last_seen` DateTime64(6),
+ `original_expiry_timestamp` DateTime64(6),
+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
+ )
+ ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_series3', '{replica}-{shard}', last_seen)
+ PARTITION BY toDate(original_expiry_timestamp)
+ ORDER BY (team_id, metric_name, series_fingerprint)
+ TTL original_expiry_timestamp
+ SETTINGS index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[metrics1]
'''