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(logs): add the logs_pattern_buckets rollup table (#89478)

Jon McCallum788e536caca5
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index d6719677bdc5..5bf0a5b32502 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -748,6 +748,85 @@ SQL
}
}
+ table "logs_pattern_buckets" {
+ primary_key = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version"]
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version", "pattern"]
+ partition_by = "toDate(time_bucket)"
+ ttl = "time_bucket + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_pattern_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_pattern_buckets_distributed" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "logs_pattern_buckets"
+ }
+ }
+
table "logs_volume_buckets" {
order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
partition_by = "toDate(time_bucket)"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index a8da37cb56e1..a26d508580fd 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -1160,6 +1160,85 @@ SQL
}
}
+ table "logs_pattern_buckets" {
+ primary_key = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version"]
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version", "pattern"]
+ partition_by = "toDate(time_bucket)"
+ ttl = "time_bucket + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_pattern_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_pattern_buckets_distributed" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "logs_pattern_buckets"
+ }
+ }
+
table "logs_volume_buckets" {
order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
partition_by = "toDate(time_bucket)"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index f56e99f908e7..4795d1fb89ee 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -5692,6 +5692,85 @@ SQL
}
}
+ table "logs_pattern_buckets" {
+ primary_key = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version"]
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version", "pattern"]
+ partition_by = "toDate(time_bucket)"
+ ttl = "time_bucket + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_pattern_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_pattern_buckets_distributed" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "logs_pattern_buckets"
+ }
+ }
+
table "logs_volume_buckets" {
order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
partition_by = "toDate(time_bucket)"
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index b9d795bc03de..b65e0de04cac 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -746,6 +746,85 @@ SQL
}
}
+ table "logs_pattern_buckets" {
+ primary_key = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version"]
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version", "pattern"]
+ partition_by = "toDate(time_bucket)"
+ ttl = "time_bucket + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_pattern_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_pattern_buckets_distributed" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "logs_pattern_buckets"
+ }
+ }
+
table "logs_volume_buckets" {
order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
partition_by = "toDate(time_bucket)"
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index 36dc99dfec8e..063678d6baab 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -746,6 +746,85 @@ SQL
}
}
+ table "logs_pattern_buckets" {
+ primary_key = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version"]
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version", "pattern"]
+ partition_by = "toDate(time_bucket)"
+ ttl = "time_bucket + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_pattern_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_pattern_buckets_distributed" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "logs_pattern_buckets"
+ }
+ }
+
table "logs_volume_buckets" {
order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
partition_by = "toDate(time_bucket)"
diff --git a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
index 3b65b22c5369..3c88eb052467 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
@@ -726,6 +726,85 @@ database "posthog" {
}
}
+ table "logs_pattern_buckets" {
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version", "pattern"]
+ primary_key = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text", "pattern_version"]
+ partition_by = "toDate(time_bucket)"
+ ttl = "time_bucket + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_pattern_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_pattern_buckets_distributed" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "logs_pattern_buckets"
+ }
+ }
+
table "metric_samples" {
column "team_id" {
type = "Int32"
diff --git a/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl b/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl
index 6d12d7daaea6..e69cc7865304 100644
--- a/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl
@@ -69,6 +69,14 @@ database "posthog" {
}
}
+ patch_table "logs_pattern_buckets_distributed" {
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "logs_pattern_buckets"
+ }
+ }
+
patch_table "metric_samples" {
engine "distributed" {
cluster_name = "logs"
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index 4eb15c4983ee..ba5cd58d555a 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -211,6 +211,28 @@ CREATE TABLE posthog.logs_kafka_metrics_distributed (
max_created_at SimpleAggregateFunction(max, DateTime64(9)),
max_lag SimpleAggregateFunction(max, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'logs_kafka_metrics');
+CREATE TABLE posthog.logs_pattern_buckets (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_pattern_buckets', '{replica}-{shard}') PRIMARY KEY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version) ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_pattern_buckets_distributed (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('logs', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index cbaadd04fe33..db3c0df6770e 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -279,6 +279,28 @@ CREATE TABLE posthog.logs_kafka_metrics_distributed (
max_created_at SimpleAggregateFunction(max, DateTime64(9)),
max_lag SimpleAggregateFunction(max, UInt64)
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs_kafka_metrics');
+CREATE TABLE posthog.logs_pattern_buckets (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_pattern_buckets', '{replica}-{shard}') PRIMARY KEY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version) ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_pattern_buckets_distributed (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 1af8538bf9cb..65c66268443a 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -1144,6 +1144,28 @@ CREATE TABLE posthog.logs_kafka_metrics_distributed (
max_created_at SimpleAggregateFunction(max, DateTime64(9)),
max_lag SimpleAggregateFunction(max, UInt64)
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs_kafka_metrics');
+CREATE TABLE posthog.logs_pattern_buckets (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_pattern_buckets', '{replica}-{shard}') PRIMARY KEY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version) ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_pattern_buckets_distributed (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index 5300630f964c..5561cff3604b 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -211,6 +211,28 @@ CREATE TABLE posthog.logs_kafka_metrics_distributed (
max_created_at SimpleAggregateFunction(max, DateTime64(9)),
max_lag SimpleAggregateFunction(max, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'logs_kafka_metrics');
+CREATE TABLE posthog.logs_pattern_buckets (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_pattern_buckets', '{replica}-{shard}') PRIMARY KEY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version) ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_pattern_buckets_distributed (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('logs', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index da60c22d42bf..e09897b91862 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -211,6 +211,28 @@ CREATE TABLE posthog.logs_kafka_metrics_distributed (
max_created_at SimpleAggregateFunction(max, DateTime64(9)),
max_lag SimpleAggregateFunction(max, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'logs_kafka_metrics');
+CREATE TABLE posthog.logs_pattern_buckets (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_pattern_buckets', '{replica}-{shard}') PRIMARY KEY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version) ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_pattern_buckets_distributed (
+ team_id Int32,
+ time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ pattern_version UInt8,
+ pattern String,
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('logs', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.py
index 19e88cedf9d1..7a99a3ace1e7 100644
--- a/posthog/clickhouse/logs/__init__.py
+++ b/posthog/clickhouse/logs/__init__.py
@@ -25,6 +25,7 @@ from .logs34 import (
)
from .logs_billing_metrics import LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL, LOGS_BILLING_METRICS_TABLE_SQL
from .logs_kafka_metrics import LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL, LOGS_KAFKA_METRICS_TABLE_SQL
+from .logs_pattern_buckets import LOGS_PATTERN_BUCKETS_DISTRIBUTED_TABLE_SQL, LOGS_PATTERN_BUCKETS_TABLE_SQL
from .logs_volume_buckets import LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL, LOGS_VOLUME_BUCKETS_TABLE_SQL
__all__ = [
@@ -57,5 +58,7 @@ __all__ = [
"LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL",
"LOGS_VOLUME_BUCKETS_TABLE_SQL",
"LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL",
+ "LOGS_PATTERN_BUCKETS_TABLE_SQL",
+ "LOGS_PATTERN_BUCKETS_DISTRIBUTED_TABLE_SQL",
"LOGS34_TO_VOLUME_BUCKETS_MV",
]
diff --git a/posthog/clickhouse/logs/logs_pattern_buckets.py b/posthog/clickhouse/logs/logs_pattern_buckets.py
new file mode 100644
index 000000000000..01d67a6fe392
--- /dev/null
+++ b/posthog/clickhouse/logs/logs_pattern_buckets.py
@@ -0,0 +1,77 @@
+from django.conf import settings
+
+from posthog.clickhouse.table_engines import AggregatingMergeTree, Distributed, ReplicationScheme
+
+# Log pattern rollup for anomaly detection, at logs_volume_buckets' grain plus
+# `pattern`, so a reader can ask how often one masked shape occurred in one
+# series. The writer picks the bucket width, not this table. logs_volume_buckets
+# uses a 5-minute UTC grid and the sizing here assumes the same.
+#
+# Like logs_volume_buckets, this table holds partial counts, so readers must
+# always GROUP BY the key columns and sum(log_count), never read rows raw. See
+# that table for the rest of the summing and dedup semantics, which are the
+# same here.
+#
+# ORDER BY keeps logs_volume_buckets' time-early prefix, for the reason its own
+# comment gives, and appends `pattern` last. Reading one shape's history is then
+# a range scan inside one series.
+#
+# PRIMARY KEY stops before `pattern`. The sparse index keeps one mark per
+# granule, so an unbounded String there stores a full copy per mark for a column
+# too distinct to prune granules on. The grain does not change: ORDER BY still
+# carries `pattern`, so merges still collapse on it.
+#
+# `pattern` is a plain String. Its cardinality is unbounded by construction, so
+# a LowCardinality dictionary costs more than it saves. It carries no explicit
+# codec either: the server compresses every column with ZSTD, and a column last
+# in the sort key stores equal values adjacent, which ZSTD handles well.
+#
+# `severity_text` stays in the grain, so the same shape at two severities keeps
+# two rows. A reader that ignores severity can still collapse them, but a reader
+# that keeps it can tell an escalation from a new shape.
+#
+# `pattern_version` sits in the grain, just before `pattern`. Two patterns only
+# mean the same shape when the masker version that produced them matches, so a
+# masking change must start a new series rather than merge into the old one.
+#
+# TTL is 42 days (6 weekly samples per time-of-week slot), independent of raw
+# log retention. Nothing caps patterns per series yet, so until something does,
+# row count here is bounded only by how many distinct shapes a service emits.
+
+TABLE_NAME = "logs_pattern_buckets"
+
+
+def LOGS_PATTERN_BUCKETS_TABLE_SQL():
+ return f"""
+CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}
+(
+ `team_id` Int32,
+ `time_bucket` DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ `service_name` LowCardinality(String),
+ `namespace` LowCardinality(String),
+ `environment` LowCardinality(String),
+ `severity_text` LowCardinality(String),
+ `pattern_version` UInt8,
+ `pattern` String,
+ `log_count` SimpleAggregateFunction(sum, UInt64)
+)
+ENGINE = {AggregatingMergeTree(TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY toDate(time_bucket)
+ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern)
+PRIMARY KEY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version)
+TTL time_bucket + INTERVAL 42 DAY
+SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1
+"""
+
+
+def LOGS_PATTERN_BUCKETS_DISTRIBUTED_TABLE_SQL():
+ return """
+CREATE TABLE IF NOT EXISTS {database}.logs_pattern_buckets_distributed AS {database}.{table_name} ENGINE = {engine}
+""".format(
+ engine=Distributed(
+ data_table=TABLE_NAME,
+ cluster=settings.CLICKHOUSE_LOGS_CLUSTER,
+ ),
+ database=settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE,
+ table_name=TABLE_NAME,
+ )
diff --git a/posthog/clickhouse/migrations/0313_logs_pattern_buckets.py b/posthog/clickhouse/migrations/0313_logs_pattern_buckets.py
new file mode 100644
index 000000000000..28eb784f0dd7
--- /dev/null
+++ b/posthog/clickhouse/migrations/0313_logs_pattern_buckets.py
@@ -0,0 +1,8 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.logs import LOGS_PATTERN_BUCKETS_DISTRIBUTED_TABLE_SQL, LOGS_PATTERN_BUCKETS_TABLE_SQL
+
+operations = [
+ run_sql_with_exceptions(LOGS_PATTERN_BUCKETS_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS_PATTERN_BUCKETS_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 92d0bc382e83..e86622ea942d 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0312_reapply_error_tracking_issue_severity
+0313_logs_pattern_buckets
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index 72ab8a2b62f5..f15b49a51d94 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -28,6 +28,8 @@ from posthog.clickhouse.logs import (
LOGS_DISTRIBUTED_TABLE_SQL,
LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,
LOGS_KAFKA_METRICS_TABLE_SQL,
+ LOGS_PATTERN_BUCKETS_DISTRIBUTED_TABLE_SQL,
+ LOGS_PATTERN_BUCKETS_TABLE_SQL,
LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL,
LOGS_VOLUME_BUCKETS_TABLE_SQL,
)
@@ -428,6 +430,7 @@ CREATE_MERGETREE_TABLE_QUERIES = (
LOG_ATTRIBUTES2_TABLE_SQL,
LOG_ATTRIBUTES3_TABLE_SQL,
LOGS_VOLUME_BUCKETS_TABLE_SQL,
+ LOGS_PATTERN_BUCKETS_TABLE_SQL,
TRACE_SPANS_TABLE_SQL,
TRACE_ATTRIBUTES_TABLE_SQL,
TRACE_ATTRIBUTES2_TABLE_SQL,
@@ -518,6 +521,7 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (
LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,
LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,
LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL,
+ LOGS_PATTERN_BUCKETS_DISTRIBUTED_TABLE_SQL,
TRACE_SPANS_DISTRIBUTED_TABLE_SQL,
TRACE_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,
METRICS_DISTRIBUTED_TABLE_SQL,
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 33b1765e09fb..b3659c878d1d 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -3821,6 +3821,37 @@
'''
# ---
+# name: test_create_table_query[logs_pattern_buckets]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_pattern_buckets
+ (
+ `team_id` Int32,
+ `time_bucket` DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ `service_name` LowCardinality(String),
+ `namespace` LowCardinality(String),
+ `environment` LowCardinality(String),
+ `severity_text` LowCardinality(String),
+ `pattern_version` UInt8,
+ `pattern` String,
+ `log_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs_pattern_buckets', '{replica}-{shard}')
+ PARTITION BY toDate(time_bucket)
+ ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern)
+ PRIMARY KEY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version)
+ TTL time_bucket + INTERVAL 42 DAY
+ SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1
+
+ '''
+# ---
+# name: test_create_table_query[logs_pattern_buckets_distributed]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_pattern_buckets_distributed AS posthog_test.logs_pattern_buckets ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'logs_pattern_buckets')
+
+ '''
+# ---
# name: test_create_table_query[logs_volume_buckets]
'''
@@ -10743,6 +10774,30 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[logs_pattern_buckets]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_pattern_buckets
+ (
+ `team_id` Int32,
+ `time_bucket` DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ `service_name` LowCardinality(String),
+ `namespace` LowCardinality(String),
+ `environment` LowCardinality(String),
+ `severity_text` LowCardinality(String),
+ `pattern_version` UInt8,
+ `pattern` String,
+ `log_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs_pattern_buckets', '{replica}-{shard}')
+ PARTITION BY toDate(time_bucket)
+ ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version, pattern)
+ PRIMARY KEY (team_id, time_bucket, service_name, namespace, environment, severity_text, pattern_version)
+ TTL time_bucket + INTERVAL 42 DAY
+ SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[logs_volume_buckets]
'''