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): store metric type in the metric names table (#113923)

Frank Hamandf957aa8fee50
diff --git a/posthog/clickhouse/hcl/golden/dev/apm.hcl b/posthog/clickhouse/hcl/golden/dev/apm.hcl
index 8d06337fc73c..fe63fabd1f80 100644
--- a/posthog/clickhouse/hcl/golden/dev/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/apm.hcl
@@ -677,6 +677,9 @@ database "posthog" {
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -1528,7 +1531,8 @@ SELECT
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
@@ -1553,6 +1557,9 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
}
materialized_view "metrics4_input_to_metrics4_resource_attributes" {
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 5d3db566e85b..1f1fa5bd83f2 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -1889,6 +1889,13 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
+ column "metric_type" {
+ type = "String"
+ alias = "metric_types[1]"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 7ed2d6d461af..cd71a801b114 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -2390,6 +2390,13 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
+ column "metric_type" {
+ type = "String"
+ alias = "metric_types[1]"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index a36689cdaa11..2746bbbe73ec 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -7535,6 +7535,13 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
+ column "metric_type" {
+ type = "String"
+ alias = "metric_types[1]"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
@@ -17945,6 +17952,9 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -22355,7 +22365,8 @@ SELECT
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
@@ -22380,6 +22391,9 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
}
materialized_view "metrics4_input_to_metrics4_resource_attributes" {
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl b/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
index 3fe614cdfca0..c581a4d80e37 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
@@ -224,6 +224,9 @@ database "posthog" {
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -675,7 +678,8 @@ SELECT
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
@@ -700,6 +704,9 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
}
materialized_view "metrics4_input_to_metrics4_resource_attributes" {
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index 7a090a0bf0ea..5a25ba850506 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -1976,6 +1976,13 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
+ column "metric_type" {
+ type = "String"
+ alias = "metric_types[1]"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/prod-us/apm.hcl b/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
index 3fe614cdfca0..c581a4d80e37 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
@@ -224,6 +224,9 @@ database "posthog" {
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -675,7 +678,8 @@ SELECT
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
@@ -700,6 +704,9 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
}
materialized_view "metrics4_input_to_metrics4_resource_attributes" {
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index 274372eac99d..4b548efc64cd 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -1976,6 +1976,13 @@ SQL
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
+ column "metric_type" {
+ type = "String"
+ alias = "metric_types[1]"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl b/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl
index 53d058d348ee..c1abd6dbda52 100644
--- a/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl
+++ b/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl
@@ -143,6 +143,7 @@ database "posthog" {
column "original_expiry_time_bucket" { type = "DateTime64(0)" }
column "original_expiry_timestamp" { type = "SimpleAggregateFunction(max, DateTime64(6))" }
column "service_name" { type = "LowCardinality(String)" }
+ column "metric_types" { type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))" }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -254,6 +255,7 @@ database "posthog" {
column "original_expiry_time_bucket" { type = "DateTime64(0)" }
column "original_expiry_timestamp" { type = "SimpleAggregateFunction(max, DateTime64(6))" }
column "service_name" { type = "LowCardinality(String)" }
+ column "metric_types" { type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))" }
}
materialized_view "metrics4_input_to_metrics4_attributes" {
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql
index 09b5a81958d9..488b17e6ed36 100644
--- a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql
+++ b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql
@@ -4,7 +4,8 @@ SELECT
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl b/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl
index d495e5815b63..e5c33f0e4ea3 100644
--- a/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl
@@ -95,6 +95,13 @@ database "posthog" {
column "service_name" {
type = "LowCardinality(String)"
}
+ column "metric_types" {
+ type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
+ }
+ column "metric_type" {
+ type = "String"
+ alias = "metric_types[1]"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/sql/dev/apm.sql b/posthog/clickhouse/hcl/sql/dev/apm.sql
index 0d7f92704013..efc9323a277c 100644
--- a/posthog/clickhouse/hcl/sql/dev/apm.sql
+++ b/posthog/clickhouse/hcl/sql/dev/apm.sql
@@ -203,7 +203,8 @@ CREATE TABLE posthog.writable_metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))
) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
CREATE TABLE posthog.writable_metrics4_samples (
team_id Int32,
@@ -478,13 +479,14 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
-CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String)) AS SELECT
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String), metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))) 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,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index 2c8b365b36ad..305d330e589e 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -471,7 +471,9 @@ CREATE TABLE posthog.metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ metric_type String ALIAS metric_types[1]
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index 68da898f13c5..6a4e7f282cf6 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -563,7 +563,9 @@ CREATE TABLE posthog.metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ metric_type String ALIAS metric_types[1]
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 11b6521dfba4..3c3127c09d64 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -1534,7 +1534,9 @@ CREATE TABLE posthog.metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ metric_type String ALIAS metric_types[1]
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
@@ -4074,7 +4076,8 @@ CREATE TABLE posthog.writable_metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))
) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
CREATE TABLE posthog.writable_metrics4_samples (
team_id Int32,
@@ -5344,13 +5347,14 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
-CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String)) AS SELECT
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String), metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))) 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,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/apm.sql b/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
index 177846223350..81d0c301ea46 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
@@ -71,7 +71,8 @@ CREATE TABLE posthog.writable_metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))
) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
CREATE TABLE posthog.writable_metrics4_samples (
team_id Int32,
@@ -227,13 +228,14 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
-CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String)) AS SELECT
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String), metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))) 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,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index 2dc3dcba8f00..76bf24c5a5e3 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -495,7 +495,9 @@ CREATE TABLE posthog.metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ metric_type String ALIAS metric_types[1]
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/apm.sql b/posthog/clickhouse/hcl/sql/prod-us/apm.sql
index 44c5282de894..07cb28d2901b 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/apm.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/apm.sql
@@ -71,7 +71,8 @@ CREATE TABLE posthog.writable_metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))
) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
CREATE TABLE posthog.writable_metrics4_samples (
team_id Int32,
@@ -227,13 +228,14 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
-CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String)) AS SELECT
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String), metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))) 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,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index d7339c54e4a1..c724cfa3dfdf 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -495,7 +495,9 @@ CREATE TABLE posthog.metrics4_names (
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
- service_name LowCardinality(String)
+ service_name LowCardinality(String),
+ metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ metric_type String ALIAS metric_types[1]
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
diff --git a/posthog/clickhouse/metrics/metrics4.py b/posthog/clickhouse/metrics/metrics4.py
index 0b2df4dfd46d..139c7c4ce7ed 100644
--- a/posthog/clickhouse/metrics/metrics4.py
+++ b/posthog/clickhouse/metrics/metrics4.py
@@ -171,7 +171,8 @@ def WRITABLE_METRICS4_NAMES_TABLE_SQL() -> str:
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
`original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
- `service_name` LowCardinality(String)""",
+ `service_name` LowCardinality(String),
+ `metric_types` SimpleAggregateFunction(groupUniqArrayArray, Array(String))""",
)
@@ -296,7 +297,9 @@ CREATE TABLE IF NOT EXISTS {_db()}.{METRICS4_NAMES_TABLE_NAME}
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
`original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
- `service_name` LowCardinality(String)
+ `service_name` LowCardinality(String),
+ `metric_types` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `metric_type` String ALIAS metric_types[1]
)
ENGINE = {AggregatingMergeTree(METRICS4_NAMES_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
PARTITION BY toDate(original_expiry_time_bucket)
@@ -403,7 +406,8 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS4_INPUT_TABLE_NAME}_to_{METR
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
`original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
- `service_name` LowCardinality(String)
+ `service_name` LowCardinality(String),
+ `metric_types` SimpleAggregateFunction(groupUniqArrayArray, Array(String))
)
AS SELECT
team_id,
@@ -411,7 +415,8 @@ AS SELECT
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM {db}.{METRICS4_INPUT_TABLE_NAME} AS input
WHERE has_labels
GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
diff --git a/posthog/clickhouse/migrations/0351_metrics4_names_metric_type.py b/posthog/clickhouse/migrations/0351_metrics4_names_metric_type.py
new file mode 100644
index 000000000000..c2acfa6fc3fd
--- /dev/null
+++ b/posthog/clickhouse/migrations/0351_metrics4_names_metric_type.py
@@ -0,0 +1,44 @@
+from django.conf import settings
+
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+
+DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+# Not in the sort key: 0327 builds the table from the current SQL, so a longer key would make 0342's
+# MODIFY ORDER BY fail on a fresh cluster. The set union on merge keeps every type a name was sent as.
+# Existing parts read an empty array and leave by TTL.
+operations = [
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.metrics4_names "
+ "ADD COLUMN IF NOT EXISTS metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String)), "
+ "ADD COLUMN IF NOT EXISTS metric_type String ALIAS metric_types[1]",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.writable_metrics4_names "
+ "ADD COLUMN IF NOT EXISTS metric_types SimpleAggregateFunction(groupUniqArrayArray, Array(String))",
+ node_roles=[NodeRole.APM],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+ run_sql_with_exceptions(
+ f"""ALTER TABLE {DB}.metrics4_input_to_metrics4_names MODIFY QUERY
+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,
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
+FROM {DB}.metrics4_input AS input
+WHERE has_labels
+GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name""",
+ node_roles=[NodeRole.APM],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 541eb85fd1a2..2ca10607f352 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0350_flag_evaluations_mv_insert_time
+0351_metrics4_names_metric_type
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 67c6d4cf6d69..b0502bee9a94 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -4893,7 +4893,8 @@
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
`original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
- `service_name` LowCardinality(String)
+ `service_name` LowCardinality(String),
+ `metric_types` SimpleAggregateFunction(groupUniqArrayArray, Array(String))
)
AS SELECT
team_id,
@@ -4901,7 +4902,8 @@
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
- service_name
+ service_name,
+ groupUniqArrayArraySimpleState([toString(metric_type)]) AS metric_types
FROM posthog_test.metrics4_input AS input
WHERE has_labels
GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
@@ -5031,7 +5033,9 @@
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
`original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
- `service_name` LowCardinality(String)
+ `service_name` LowCardinality(String),
+ `metric_types` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `metric_type` String ALIAS metric_types[1]
)
ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_names', '{replica}-{shard}')
PARTITION BY toDate(original_expiry_time_bucket)
@@ -10562,7 +10566,8 @@
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
`original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
- `service_name` LowCardinality(String)
+ `service_name` LowCardinality(String),
+ `metric_types` SimpleAggregateFunction(groupUniqArrayArray, Array(String))
)
ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metrics4_names')
@@ -12539,7 +12544,9 @@
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
`original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
- `service_name` LowCardinality(String)
+ `service_name` LowCardinality(String),
+ `metric_types` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `metric_type` String ALIAS metric_types[1]
)
ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_names', '{replica}-{shard}')
PARTITION BY toDate(original_expiry_time_bucket)