: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(logs): write pattern buckets from logs34 (#115369)
Jon McCallum99c777b6bdeb
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hclindex bdf4a894c5cf..84efeeeae0ee 100644--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl@@ -4168,6 +4168,92 @@ SQL}}+ materialized_view "logs34_to_pattern_buckets" {+ to_table = "posthog.logs_pattern_buckets"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern+SQL++ column "team_id" {+ type = "Int32"+ }+ column "time_bucket" {+ type = "DateTime('UTC')"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "namespace" {+ type = "LowCardinality(String)"+ }+ column "environment" {+ type = "LowCardinality(String)"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "pattern_version" {+ type = "UInt8"+ }+ column "pattern" {+ type = "String"+ }+ column "retention_days" {+ type = "SimpleAggregateFunction(max, UInt16)"+ }+ column "log_count" {+ type = "SimpleAggregateFunction(sum, UInt64)"+ }+ }+materialized_view "logs34_to_resource_attributes3" {to_table = "posthog.log_attributes3"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hclindex 96e5002c5f5d..94aa8c616e86 100644--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl@@ -4465,6 +4465,92 @@ SQL}}+ materialized_view "logs34_to_pattern_buckets" {+ to_table = "posthog.logs_pattern_buckets"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern+SQL++ column "team_id" {+ type = "Int32"+ }+ column "time_bucket" {+ type = "DateTime('UTC')"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "namespace" {+ type = "LowCardinality(String)"+ }+ column "environment" {+ type = "LowCardinality(String)"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "pattern_version" {+ type = "UInt8"+ }+ column "pattern" {+ type = "String"+ }+ column "retention_days" {+ type = "SimpleAggregateFunction(max, UInt16)"+ }+ column "log_count" {+ type = "SimpleAggregateFunction(sum, UInt64)"+ }+ }+materialized_view "logs34_to_resource_attributes3" {to_table = "posthog.log_attributes3"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hclindex 205e6d3ea379..b1e6a9f277e8 100644--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl@@ -23720,6 +23720,92 @@ SQL}}+ materialized_view "logs34_to_pattern_buckets" {+ to_table = "posthog.logs_pattern_buckets"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern+SQL++ column "team_id" {+ type = "Int32"+ }+ column "time_bucket" {+ type = "DateTime('UTC')"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "namespace" {+ type = "LowCardinality(String)"+ }+ column "environment" {+ type = "LowCardinality(String)"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "pattern_version" {+ type = "UInt8"+ }+ column "pattern" {+ type = "String"+ }+ column "retention_days" {+ type = "SimpleAggregateFunction(max, UInt16)"+ }+ column "log_count" {+ type = "SimpleAggregateFunction(sum, UInt64)"+ }+ }+materialized_view "logs34_to_resource_attributes3" {to_table = "posthog.log_attributes3"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hclindex e577dd701a27..cef67a272fcc 100644--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl@@ -4401,6 +4401,92 @@ SQL}}+ materialized_view "logs34_to_pattern_buckets" {+ to_table = "posthog.logs_pattern_buckets"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern+SQL++ column "team_id" {+ type = "Int32"+ }+ column "time_bucket" {+ type = "DateTime('UTC')"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "namespace" {+ type = "LowCardinality(String)"+ }+ column "environment" {+ type = "LowCardinality(String)"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "pattern_version" {+ type = "UInt8"+ }+ column "pattern" {+ type = "String"+ }+ column "retention_days" {+ type = "SimpleAggregateFunction(max, UInt16)"+ }+ column "log_count" {+ type = "SimpleAggregateFunction(sum, UInt64)"+ }+ }+materialized_view "logs34_to_resource_attributes3" {to_table = "posthog.log_attributes3"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hclindex 4630123145f7..239208242abe 100644--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl@@ -4383,6 +4383,92 @@ SQL}}+ materialized_view "logs34_to_pattern_buckets" {+ to_table = "posthog.logs_pattern_buckets"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern+SQL++ column "team_id" {+ type = "Int32"+ }+ column "time_bucket" {+ type = "DateTime('UTC')"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "namespace" {+ type = "LowCardinality(String)"+ }+ column "environment" {+ type = "LowCardinality(String)"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "pattern_version" {+ type = "UInt8"+ }+ column "pattern" {+ type = "String"+ }+ column "retention_days" {+ type = "SimpleAggregateFunction(max, UInt16)"+ }+ column "log_count" {+ type = "SimpleAggregateFunction(sum, UInt64)"+ }+ }+materialized_view "logs34_to_resource_attributes3" {to_table = "posthog.log_attributes3"query = <<SQLdiff --git a/posthog/clickhouse/hcl/roles/logs/base/sql/logs34_to_pattern_buckets.sql b/posthog/clickhouse/hcl/roles/logs/base/sql/logs34_to_pattern_buckets.sqlnew file mode 100644index 000000000000..2a7104bb2715--- /dev/null+++ b/posthog/clickhouse/hcl/roles/logs/base/sql/logs34_to_pattern_buckets.sql@@ -0,0 +1,48 @@+SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0) + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, patterndiff --git a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl b/posthog/clickhouse/hcl/roles/logs/base/tables.hclindex 9956633c07e4..a92b40d263e8 100644--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl@@ -201,6 +201,42 @@ database "posthog" {}}+ materialized_view "logs34_to_pattern_buckets" {+ to_table = "posthog.logs_pattern_buckets"+ query = file("sql/logs34_to_pattern_buckets.sql")++ column "team_id" {+ type = "Int32"+ }+ column "time_bucket" {+ type = "DateTime('UTC')"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "namespace" {+ type = "LowCardinality(String)"+ }+ column "environment" {+ type = "LowCardinality(String)"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "pattern_version" {+ type = "UInt8"+ }+ column "pattern" {+ type = "String"+ }+ column "retention_days" {+ type = "SimpleAggregateFunction(max, UInt16)"+ }+ column "log_count" {+ type = "SimpleAggregateFunction(sum, UInt64)"+ }+ }+table "metric_samples1" {order_by = ["team_id", "metric_name", "series_fingerprint", "timestamp"]partition_by = "toDate(timestamp)"diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sqlindex 075a462f1025..1a037b5a0460 100644--- a/posthog/clickhouse/hcl/sql/dev/logs.sql+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql@@ -1194,6 +1194,55 @@ FROMGROUP BYteam_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes);+CREATE MATERIALIZED VIEW posthog.logs34_to_pattern_buckets TO posthog.logs_pattern_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), pattern_version UInt8, pattern String, retention_days SimpleAggregateFunction(max, UInt16), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern;CREATE MATERIALIZED VIEW posthog.logs34_to_resource_attributes3 TO posthog.log_attributes3 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), severity_text LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sqlindex 32b105251309..b16b90c0da1c 100644--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql@@ -1233,6 +1233,55 @@ FROMGROUP BYteam_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes);+CREATE MATERIALIZED VIEW posthog.logs34_to_pattern_buckets TO posthog.logs_pattern_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), pattern_version UInt8, pattern String, retention_days SimpleAggregateFunction(max, UInt16), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern;CREATE MATERIALIZED VIEW posthog.logs34_to_resource_attributes3 TO posthog.log_attributes3 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), severity_text LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sqlindex 79c0d63b1abd..0fb1abe05009 100644--- a/posthog/clickhouse/hcl/sql/local-single/all.sql+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql@@ -5359,6 +5359,55 @@ FROMGROUP BYteam_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes);+CREATE MATERIALIZED VIEW posthog.logs34_to_pattern_buckets TO posthog.logs_pattern_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), pattern_version UInt8, pattern String, retention_days SimpleAggregateFunction(max, UInt16), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern;CREATE MATERIALIZED VIEW posthog.logs34_to_resource_attributes3 TO posthog.log_attributes3 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), severity_text LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sqlindex 09821a18d3a1..9ba20e83d25d 100644--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql@@ -1271,6 +1271,55 @@ FROMGROUP BYteam_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes);+CREATE MATERIALIZED VIEW posthog.logs34_to_pattern_buckets TO posthog.logs_pattern_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), pattern_version UInt8, pattern String, retention_days SimpleAggregateFunction(max, UInt16), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern;CREATE MATERIALIZED VIEW posthog.logs34_to_resource_attributes3 TO posthog.log_attributes3 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), severity_text LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sqlindex 59566ebaba62..a35e2cf985de 100644--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql@@ -1265,6 +1265,55 @@ FROMGROUP BYteam_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes);+CREATE MATERIALIZED VIEW posthog.logs34_to_pattern_buckets TO posthog.logs_pattern_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), pattern_version UInt8, pattern String, retention_days SimpleAggregateFunction(max, UInt16), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ (resource_attributes['k8s.namespace.name']) != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ (resource_attributes['deployment.environment.name']) != '',+ resource_attributes['deployment.environment.name'],+ if(+ (resource_attributes['deployment.environment']) != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(+ least(+ intDiv(+ greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0)+ + 86399999999,+ 86400000000+ ),+ 3650+ )+ ) AS retention_days+ FROM posthog.logs34+ WHERE pattern_version != 0+ )+GROUP BY+ team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern;CREATE MATERIALIZED VIEW posthog.logs34_to_resource_attributes3 TO posthog.log_attributes3 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), severity_text LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.pyindex fdcdc9c07d09..89563cc2f26e 100644--- a/posthog/clickhouse/logs/__init__.py+++ b/posthog/clickhouse/logs/__init__.py@@ -18,6 +18,8 @@ from .logs34 import (KAFKA_LOGS_AVRO_TABLE_SQL,LOGS34_TABLE_SQL,LOGS34_TO_LOG_ATTRIBUTES3_MV,+ LOGS34_TO_PATTERN_BUCKETS_MV,+ LOGS34_TO_PATTERN_BUCKETS_MV_SELECT,LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,LOGS34_TO_VOLUME_BUCKETS_MV,LOGS34_TO_VOLUME_BUCKETS_MV_SELECT,@@ -63,4 +65,6 @@ __all__ = ["LOGS_PATTERN_BUCKETS_DISTRIBUTED_TABLE_SQL","LOGS34_TO_VOLUME_BUCKETS_MV","LOGS34_TO_VOLUME_BUCKETS_MV_SELECT",+ "LOGS34_TO_PATTERN_BUCKETS_MV",+ "LOGS34_TO_PATTERN_BUCKETS_MV_SELECT",]diff --git a/posthog/clickhouse/logs/logs34.py b/posthog/clickhouse/logs/logs34.pyindex df4bdce96c16..738e46500586 100644--- a/posthog/clickhouse/logs/logs34.py+++ b/posthog/clickhouse/logs/logs34.py@@ -362,34 +362,13 @@ AS {KAFKA_LOGS34_AVRO_MV_SELECT()}"""-def LOGS34_TO_VOLUME_BUCKETS_MV_SELECT():- db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE- # Groups rows exactly like _rollup_sql in- # products/logs/backend/temporal/volume_tick/aggregation.py, which carries- # the reasoning for the environment fallback and severity lowercasing;- # `retention_days` is a measure folded into each group, not a dimension.- # The 300s grid literal is frozen into the DDL at migration time;- # BUCKET_SECONDS there must stay equal to it or the detector reads buckets- # this MV never writes.- #- # `retention_days` rounds the lifetime from the bucket to the raw expiry up- # to whole days, so event-time skew and bucket rounding cannot expire the- # rollup before its logs. Microseconds preserve fractional-second expiries.- # The 3650-day guard sits above the product's retention ceiling and bounds- # corrupt expiries. Max keeps mixed retentions as long as the latest expiry.- return f"""SELECT- team_id,- time_bucket,- service_name,- namespace,- environment,- severity_text,- maxSimpleState(retention_days) AS retention_days,- sumSimpleState(1) AS log_count-FROM-(- SELECT- team_id,+# Bucket and series columns shared by the volume and pattern rollups, so their+# series stay comparable one to one. They group rows exactly like _rollup_sql in+# products/logs/backend/temporal/volume_tick/aggregation.py, which carries the+# reasoning for the environment fallback and severity lowercasing. The 300s grid+# literal is frozen into each MV's DDL at migration time; BUCKET_SECONDS there+# must stay equal to it or the detector reads buckets these MVs never write.+_LOGS34_SERIES_KEY_COLUMNS = """ team_id,toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,service_name,if(@@ -406,8 +385,34 @@ FROMresource_attributes['env'])) AS environment,- lower(severity_text) AS severity_text,- toUInt16(least(intDiv(greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0) + 86399999999, 86400000000), 3650)) AS retention_days+ lower(severity_text) AS severity_text"""+++# `retention_days` is a measure folded into each rollup group, not a dimension.+# It rounds the lifetime from the bucket to the raw expiry up to whole days, so+# event-time skew and bucket rounding cannot expire the rollup before its logs.+# Microseconds preserve fractional-second expiries. The 3650-day guard sits above+# the product's retention ceiling and bounds corrupt expiries. Max keeps mixed+# retentions as long as the latest expiry.+_LOGS34_RETENTION_DAYS_COLUMN = """ toUInt16(least(intDiv(greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0) + 86399999999, 86400000000), 3650)) AS retention_days"""+++def LOGS34_TO_VOLUME_BUCKETS_MV_SELECT():+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE+ return f"""SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+(+ SELECT+{_LOGS34_SERIES_KEY_COLUMNS},+{_LOGS34_RETENTION_DAYS_COLUMN}FROM {db}.{TABLE_NAME})GROUP BY team_id, time_bucket, service_name, namespace, environment, severity_text"""@@ -431,6 +436,56 @@ AS {LOGS34_TO_VOLUME_BUCKETS_MV_SELECT()}"""+def LOGS34_TO_PATTERN_BUCKETS_MV_SELECT() -> str:+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE+ # `pattern_version = 0` marks a row the ingestion consumer never stamped. Those+ # rows would all collapse into one empty-pattern bucket per series, so they are+ # dropped. The filter is on the version, not on `pattern != ''`, because a+ # stamped empty body also yields an empty pattern, and that is a real shape.+ return f"""SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+FROM+(+ SELECT+{_LOGS34_SERIES_KEY_COLUMNS},+ pattern_version,+ pattern,+{_LOGS34_RETENTION_DAYS_COLUMN}+ FROM {db}.{TABLE_NAME}+ WHERE pattern_version != 0+)+GROUP BY team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern"""+++def LOGS34_TO_PATTERN_BUCKETS_MV() -> str:+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE+ return f"""+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.logs34_to_pattern_buckets TO {db}.logs_pattern_buckets+(+ `team_id` Int32,+ `time_bucket` DateTime('UTC'),+ `service_name` LowCardinality(String),+ `namespace` LowCardinality(String),+ `environment` LowCardinality(String),+ `severity_text` LowCardinality(String),+ `pattern_version` UInt8,+ `pattern` String,+ `retention_days` SimpleAggregateFunction(max, UInt16),+ `log_count` SimpleAggregateFunction(sum, UInt64)+)+AS {LOGS34_TO_PATTERN_BUCKETS_MV_SELECT()}+"""++def KAFKA_LOGS_AVRO_KAFKA_METRICS_MV():db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASEreturn f"""diff --git a/posthog/clickhouse/migrations/0355_logs34_to_pattern_buckets_mv.py b/posthog/clickhouse/migrations/0355_logs34_to_pattern_buckets_mv.pynew file mode 100644index 000000000000..2b681390c40e--- /dev/null+++ b/posthog/clickhouse/migrations/0355_logs34_to_pattern_buckets_mv.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.logs import LOGS34_TO_PATTERN_BUCKETS_MV++operations = [+ run_sql_with_exceptions(LOGS34_TO_PATTERN_BUCKETS_MV(), node_roles=[NodeRole.LOGS]),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex 05b480078d01..44cd66913e30 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0354_logs_pattern_buckets_retention+0355_logs34_to_pattern_buckets_mvdiff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.pyindex 9aab899dcec2..df9a0c7ec452 100644--- a/posthog/clickhouse/schema.py+++ b/posthog/clickhouse/schema.py@@ -30,6 +30,7 @@ from posthog.clickhouse.logs import (LOGS32_TABLE_SQL,LOGS34_TABLE_SQL,LOGS34_TO_LOG_ATTRIBUTES3_MV,+ LOGS34_TO_PATTERN_BUCKETS_MV,LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,LOGS34_TO_VOLUME_BUCKETS_MV,LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,@@ -716,6 +717,7 @@ CREATE_MV_TABLE_QUERIES = (LOGS34_TO_LOG_ATTRIBUTES3_MV,LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,LOGS34_TO_VOLUME_BUCKETS_MV,+ LOGS34_TO_PATTERN_BUCKETS_MV,METRICS4_INPUT_TO_METRICS4_SAMPLES_MV,METRICS4_INPUT_TO_METRICS4_SERIES_MV,METRICS4_INPUT_TO_METRICS4_NAMES_MV,diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex a39236f902ed..83980d694126 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -4016,6 +4016,64 @@'''# ---+# name: test_create_table_query[logs34_to_pattern_buckets]+ '''++ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs34_to_pattern_buckets TO posthog_test.logs_pattern_buckets+ (+ `team_id` Int32,+ `time_bucket` DateTime('UTC'),+ `service_name` LowCardinality(String),+ `namespace` LowCardinality(String),+ `environment` LowCardinality(String),+ `severity_text` LowCardinality(String),+ `pattern_version` UInt8,+ `pattern` String,+ `retention_days` SimpleAggregateFunction(max, UInt16),+ `log_count` SimpleAggregateFunction(sum, UInt64)+ )+ AS SELECT+ team_id,+ time_bucket,+ service_name,+ namespace,+ environment,+ severity_text,+ pattern_version,+ pattern,+ maxSimpleState(retention_days) AS retention_days,+ sumSimpleState(1) AS log_count+ FROM+ (+ SELECT+ team_id,+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,+ service_name,+ if(+ resource_attributes['k8s.namespace.name'] != '',+ resource_attributes['k8s.namespace.name'],+ resource_attributes['service.namespace']+ ) AS namespace,+ if(+ resource_attributes['deployment.environment.name'] != '',+ resource_attributes['deployment.environment.name'],+ if(+ resource_attributes['deployment.environment'] != '',+ resource_attributes['deployment.environment'],+ resource_attributes['env']+ )+ ) AS environment,+ lower(severity_text) AS severity_text,+ pattern_version,+ pattern,+ toUInt16(least(intDiv(greatest(dateDiff('microsecond', time_bucket, original_expiry_timestamp), 0) + 86399999999, 86400000000), 3650)) AS retention_days+ FROM posthog_test.logs34+ WHERE pattern_version != 0+ )+ GROUP BY team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern++ '''+# ---# name: test_create_table_query[logs34_to_resource_attributes3]'''diff --git a/posthog/clickhouse/test/test_schema.py b/posthog/clickhouse/test/test_schema.pyindex 4446aaf88ac2..2a0d7acc39f6 100644--- a/posthog/clickhouse/test/test_schema.py+++ b/posthog/clickhouse/test/test_schema.py@@ -14,7 +14,7 @@ from posthog.hogql.database.schema.flag_evaluations import FLAG_EVALUATIONS_CLICfrom posthog.clickhouse.client import sync_executefrom posthog.clickhouse.kafka_engine import CONSUMER_GROUP_EVENTS_JSON_NATIVE_JSON, KAFKA_COLUMNS_WITH_PARTITION-from posthog.clickhouse.logs import LOGS34_TO_VOLUME_BUCKETS_MV_SELECT+from posthog.clickhouse.logs import LOGS34_TO_PATTERN_BUCKETS_MV_SELECT, LOGS34_TO_VOLUME_BUCKETS_MV_SELECTfrom posthog.clickhouse.schema import (CREATE_KAFKA_TABLE_QUERIES,CREATE_MERGETREE_TABLE_QUERIES,@@ -152,6 +152,32 @@ def test_logs_volume_bucket_retention_covers_raw_expiry()[email protected]("clickhouse_database")+def test_logs_pattern_buckets_keep_stamped_empty_patterns_and_drop_unstamped_rows() -> None:+ select = LOGS34_TO_PATTERN_BUCKETS_MV_SELECT().replace(+ f"FROM {django_settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.logs34", "FROM pattern_input"+ )+ rows = sync_execute(+ """+ WITH pattern_input AS (+ SELECT+ 42 AS team_id,+ toDateTime64('2026-01-02 12:01:00', 6, 'UTC') AS timestamp,+ timestamp + toIntervalDay(row.3) AS original_expiry_timestamp,+ 'checkout' AS service_name,+ 'INFO' AS severity_text,+ CAST(map(), 'Map(String, String)') AS resource_attributes,+ toUInt8(row.1) AS pattern_version,+ row.2 AS pattern+ FROM (SELECT arrayJoin([(0, '', 14), (5, '', 14), (5, 'GET <N>', 14), (5, 'GET <N>', 30)]) AS row)+ )+ SELECT pattern_version, pattern, retention_days, log_count FROM ("""+ + select+ + ") ORDER BY pattern"+ )+ assert rows == [(5, "", 15, 1), (5, "GET <N>", 31, 2)]++def _column_definition_lines(block: str) -> Iterator[str]:for raw_line in block.splitlines():line = raw_line.strip().lstrip(",").strip()