: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(metrics): add metrics4_view clickhouse view (#107582)
Frank Hamand58efa59bff95
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hclindex 7b457ca70412..24de49af943b 100644--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl@@ -5063,6 +5063,71 @@ SELECTSQL}++ view "metrics4_view" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00')+SQL++ }}named_collection "msk_cluster" {diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hclindex b76d5ff96d5c..05298f60977c 100644--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl@@ -5474,6 +5474,71 @@ SQLtype = "SimpleAggregateFunction(max, Decimal(18, 6))"}}++ view "metrics4_view" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00')+SQL++ }}named_collection "msk_cluster" {diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hclindex edb2633a9a43..7dd1fada0a4d 100644--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl@@ -25680,6 +25680,71 @@ SQL}+ view "metrics4_view" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00')+SQL++ }+view "persons_batch_export" {query = <<SQLWITHdiff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hclindex 621481f2eb3b..e6d08870b323 100644--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl@@ -5411,6 +5411,71 @@ SELECTSQL}++ view "metrics4_view" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00')+SQL++ }}named_collection "msk_cluster" {diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hclindex 849f3ae97d6d..8b3c67b478b9 100644--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl@@ -5393,6 +5393,71 @@ SELECTSQL}++ view "metrics4_view" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00')+SQL++ }}named_collection "msk_cluster" {diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl b/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hclindex 90576d50a012..38f3bc7c270e 100644--- a/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl+++ b/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl@@ -308,4 +308,7 @@ database "posthog" {version_column = "timestamp"}}+ view "metrics4_view" {+ query = file("sql/metrics4_view.sql")+ }}diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics4_view.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics4_view.sqlnew file mode 100644index 000000000000..139c3b91caf7--- /dev/null+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics4_view.sql@@ -0,0 +1,59 @@+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00')diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sqlindex db22036df539..6e083351f045 100644--- a/posthog/clickhouse/hcl/sql/dev/logs.sql+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql@@ -1559,6 +1559,65 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT1 AS value,'Test to check that the metric endpoint is working' AS help,'gauge' AS type;+CREATE VIEW posthog.metrics4_view AS SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00');CREATE TABLE posthog.metric_samples (team_id Int32,metric_name LowCardinality(String),diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sqlindex af892f05f9bf..3ab6b1bc37ca 100644--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql@@ -1613,6 +1613,65 @@ CREATE MATERIALIZED VIEW posthog.trace_spans_to_kafka_metrics_mv TO posthog.tracFROM posthog.trace_spansGROUP BY_partition, _topic;+CREATE VIEW posthog.metrics4_view AS SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00');CREATE TABLE posthog.logs (time_bucket DateTime MATERIALIZED toStartOfDay(timestamp) CODEC(DoubleDelta, ZSTD(1)),original_expiry_timestamp DateTime64(6) CODEC(DoubleDelta, ZSTD(1)),diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sqlindex 13fa8e682669..1e24e86faa9e 100644--- a/posthog/clickhouse/hcl/sql/local-single/all.sql+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql@@ -6328,6 +6328,65 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT1 AS value,'Test to check that the metric endpoint is working' AS help,'gauge' AS type;+CREATE VIEW posthog.metrics4_view AS SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00');CREATE VIEW posthog.persons_batch_export AS WITHnew_persons AS (SELECT id, max(version) AS version, argMax(_timestamp, person.version) AS _timestamp2 FROM posthog.person WHERE (team_id = {team_id: Int64}) AND (id IN (SELECT id FROM posthog.person WHERE (team_id = {team_id: Int64}) AND (_timestamp >= {interval_start: DateTime64}) AND (_timestamp < {interval_end: DateTime64}))) GROUP BY id HAVING (_timestamp2 >= {interval_start: DateTime64}) AND (_timestamp2 < {interval_end: DateTime64})),new_distinct_ids AS (SELECT argMax(person_id, person_distinct_id2.version) AS person_id FROM posthog.person_distinct_id2 WHERE (team_id = {team_id: Int64}) AND (distinct_id IN (SELECT distinct_id FROM posthog.person_distinct_id2 WHERE (team_id = {team_id: Int64}) AND (_timestamp >= {interval_start: DateTime64}) AND (_timestamp < {interval_end: DateTime64}))) GROUP BY distinct_id HAVING (argMax(_timestamp, person_distinct_id2.version) >= {interval_start: DateTime64}) AND (argMax(_timestamp, person_distinct_id2.version) < {interval_end: DateTime64})),diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sqlindex db26bc6f3695..184b1ce5cf21 100644--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql@@ -1669,6 +1669,65 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT1 AS value,'Test to check that the metric endpoint is working' AS help,'gauge' AS type;+CREATE VIEW posthog.metrics4_view AS SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00');CREATE TABLE posthog.metric_samples (team_id Int32,metric_name LowCardinality(String),diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sqlindex 1556201107a2..fe76b0051360 100644--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql@@ -1663,6 +1663,65 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT1 AS value,'Test to check that the metric endpoint is working' AS help,'gauge' AS type;+CREATE VIEW posthog.metrics4_view AS SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM posthog.metrics2+WHERE+ (time_bucket > toDateTime('2026-08-25 00:00:00'))+AND+ (time_bucket < toDateTime('2026-09-14 00:00:00'))+AND+ (timestamp > toDateTime('2026-08-25 00:00:00'))+AND+ (timestamp < toDateTime('2026-09-14 00:00:00'))+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM+ posthog.metrics4_samples ARRAY JOIN timestamp_arr AS point_timestamp, observed_timestamp_arr AS point_observed_timestamp, value_arr AS point_value, count_arr AS point_count, histogram_counts_arr AS point_histogram_counts, trace_id_arr AS point_trace_id, span_id_arr AS point_span_id, trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('2026-09-14 00:00:00');CREATE TABLE posthog.metric_samples (team_id Int32,metric_name LowCardinality(String),diff --git a/posthog/clickhouse/metrics/__init__.py b/posthog/clickhouse/metrics/__init__.pyindex 9043d3cb53de..9915279e3440 100644--- a/posthog/clickhouse/metrics/__init__.py+++ b/posthog/clickhouse/metrics/__init__.py@@ -56,6 +56,7 @@ from .metrics4 import (METRICS4_NAMES_TABLE_SQL,METRICS4_SAMPLES_TABLE_SQL,METRICS4_SERIES_TABLE_SQL,+ METRICS4_VIEW_SQL,WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL,WRITABLE_METRICS4_NAMES_TABLE_SQL,WRITABLE_METRICS4_SAMPLES_TABLE_SQL,@@ -82,6 +83,7 @@ __all__ = ["METRICS4_NAMES_TABLE_SQL","METRICS4_SAMPLES_TABLE_SQL","METRICS4_SERIES_TABLE_SQL",+ "METRICS4_VIEW_SQL","WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL","WRITABLE_METRICS4_NAMES_TABLE_SQL","WRITABLE_METRICS4_SAMPLES_TABLE_SQL",diff --git a/posthog/clickhouse/metrics/metrics4.py b/posthog/clickhouse/metrics/metrics4.pyindex a4ac8b1ea14f..b1b0bf8d5635 100644--- a/posthog/clickhouse/metrics/metrics4.py+++ b/posthog/clickhouse/metrics/metrics4.py@@ -48,6 +48,11 @@ WRITABLE_METRICS4_SERIES_TABLE_NAME = "writable_metrics4_series"WRITABLE_METRICS4_NAMES_TABLE_NAME = "writable_metrics4_names"WRITABLE_METRICS4_ATTRIBUTES_TABLE_NAME = "writable_metrics4_attributes"METRICS4_MAX_SAMPLES_PER_SERIES_HOUR = 10_000+METRICS4_VIEW_NAME = "metrics4_view"+# metrics2 serves the hours before the cut-over. metrics4_samples serves the cut-over hour and later.+# The view is temporary. After 2026-10-14, the 30-day retention removes all metrics2 data.+METRICS4_VIEW_METRICS2_START = "2026-08-25 00:00:00"+METRICS4_VIEW_CUTOVER = "2026-09-14 00:00:00"# Each tuple maps an input column to its metrics4 array element type.METRICS4_POINT_ARRAY_COLUMNS: tuple[tuple[str, str], ...] = (@@ -461,3 +466,75 @@ def METRICS4_INPUT_TO_METRICS4_RESOURCE_ATTRIBUTES_MV() -> str:return _metrics4_attributes_mv("metrics4_resource_attributes", "resource_attributes", "resource", filter_long_pairs=False)+++def METRICS4_VIEW_SQL() -> str:+ # The view gives the metrics2 row shape. ARRAY JOIN makes one row for each point.+ # Partial rows of one series-hour hold different points, so no FINAL is necessary.+ db = _db()+ return f"""+CREATE OR REPLACE VIEW {db}.{METRICS4_VIEW_NAME} AS+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope+FROM {db}.metrics2+WHERE time_bucket > toDateTime('{METRICS4_VIEW_METRICS2_START}')+ AND time_bucket < toDateTime('{METRICS4_VIEW_CUTOVER}')+ AND timestamp > toDateTime('{METRICS4_VIEW_METRICS2_START}')+ AND timestamp < toDateTime('{METRICS4_VIEW_CUTOVER}')+UNION ALL+SELECT+ team_id,+ metric_name,+ time_bucket,+ series_fingerprint,+ resource_fingerprint,+ point_timestamp AS timestamp,+ point_observed_timestamp AS observed_timestamp,+ toDateTime64(original_expiry_date, 6) AS original_expiry_timestamp,+ service_name,+ metric_type,+ point_value AS value,+ point_count AS count,+ histogram_bounds,+ point_histogram_counts AS histogram_counts,+ point_trace_id AS trace_id,+ point_span_id AS span_id,+ point_trace_flags AS trace_flags,+ toBool(has_labels) AS has_labels,+ unit,+ aggregation_temporality,+ toBool(is_monotonic) AS is_monotonic,+ instrumentation_scope+FROM {db}.{METRICS4_SAMPLES_TABLE_NAME}+ARRAY JOIN+ timestamp_arr AS point_timestamp,+ observed_timestamp_arr AS point_observed_timestamp,+ value_arr AS point_value,+ count_arr AS point_count,+ histogram_counts_arr AS point_histogram_counts,+ trace_id_arr AS point_trace_id,+ span_id_arr AS point_span_id,+ trace_flags_arr AS point_trace_flags+WHERE time_bucket >= toDateTime('{METRICS4_VIEW_CUTOVER}')+"""diff --git a/posthog/clickhouse/migrations/0336_metrics4_view.py b/posthog/clickhouse/migrations/0336_metrics4_view.pynew file mode 100644index 000000000000..26815ba06c39--- /dev/null+++ b/posthog/clickhouse/migrations/0336_metrics4_view.py@@ -0,0 +1,7 @@+from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.clickhouse.metrics import METRICS4_VIEW_SQL++operations = [+ run_sql_with_exceptions(METRICS4_VIEW_SQL(), node_roles=[NodeRole.LOGS]),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex 0605a4b8bf88..eb94c277d267 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0335_trace_spans_drop_old_bloom_filters+0336_metrics4_viewdiff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.pyindex 1ef69a14c50e..613a8072161a 100644--- a/posthog/clickhouse/schema.py+++ b/posthog/clickhouse/schema.py@@ -79,6 +79,7 @@ from posthog.clickhouse.metrics import (METRICS4_NAMES_TABLE_SQL,METRICS4_SAMPLES_TABLE_SQL,METRICS4_SERIES_TABLE_SQL,+ METRICS4_VIEW_SQL,METRICS_DISTRIBUTED_TABLE_SQL,WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL,WRITABLE_METRICS4_NAMES_TABLE_SQL,@@ -748,6 +749,7 @@ CREATE_VIEW_QUERIES = (SESSIONS_VIEW_SQL,RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL,RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL_V3,+ METRICS4_VIEW_SQL,)