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): recreate logs_volume_buckets as a summing rollup table (#84185)

Jon McCallumb7617bd48a52
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index fd6ee7d2e0b1..dda0bfb48173 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -695,6 +695,72 @@ database "posthog" {
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_volume_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_volume_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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "logs_volume_buckets"
+ }
+ }
+
table "metric_attributes" {
order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
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 a9dc49099205..5e9ea5933654 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -1118,6 +1118,72 @@ SQL
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_volume_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_volume_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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "logs_volume_buckets"
+ }
+ }
+
table "metric_samples" {
column "team_id" {
type = "Int32"
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index f88323b41650..44995cf5715b 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -776,6 +776,72 @@ database "posthog" {
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_volume_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_volume_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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "logs_volume_buckets"
+ }
+ }
+
table "metric_attributes" {
order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
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 62f9597e60cb..98aa1e79f56e 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -775,6 +775,72 @@ database "posthog" {
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_volume_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_volume_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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "logs_volume_buckets"
+ }
+ }
+
table "metric_attributes" {
order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
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 dccddde576cd..248e6c92baaa 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
@@ -691,6 +691,72 @@ database "posthog" {
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "time_bucket", "service_name", "namespace", "environment", "severity_text"]
+ 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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.logs_volume_buckets"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "logs_volume_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 "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "logs_volume_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 b944a8756e3d..f4518c51a276 100644
--- a/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl
@@ -61,6 +61,14 @@ database "posthog" {
}
}
+ patch_table "logs_volume_buckets_distributed" {
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "logs_volume_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 36ee8359d67a..44f25b5dec7a 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -190,6 +190,24 @@ 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_volume_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),
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_volume_buckets', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_volume_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),
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('logs', 'posthog', 'logs_volume_buckets');
CREATE TABLE posthog.metric_attributes (
team_id Int32,
time_bucket DateTime64(0),
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index 0f56c0fb9b9c..b18f1450c619 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -267,6 +267,24 @@ 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_volume_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),
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_volume_buckets', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_volume_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),
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs_volume_buckets');
CREATE TABLE posthog.metric_samples1 (
team_id Int32,
metric_name LowCardinality(String),
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index bb669b689192..90c6968048ed 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -214,6 +214,24 @@ 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_volume_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),
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_volume_buckets', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_volume_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),
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('logs', 'posthog', 'logs_volume_buckets');
CREATE TABLE posthog.metric_attributes (
team_id Int32,
time_bucket DateTime64(0),
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index 8e3e05a34afa..d59f8e822da5 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -213,6 +213,24 @@ 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_volume_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),
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.logs_volume_buckets', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_volume_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),
+ log_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('logs', 'posthog', 'logs_volume_buckets');
CREATE TABLE posthog.metric_attributes (
team_id Int32,
time_bucket DateTime64(0),
diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.py
index 4c9b483f8237..94726b0142b2 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_volume_buckets import LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL, LOGS_VOLUME_BUCKETS_TABLE_SQL
__all__ = [
"LOGS32_TABLE_SQL",
@@ -55,4 +56,6 @@ __all__ = [
"LOG_ATTRIBUTES3_DISTRIBUTED_TABLE_SQL",
"LOGS_BILLING_METRICS_TABLE_SQL",
"LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL",
+ "LOGS_VOLUME_BUCKETS_TABLE_SQL",
+ "LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL",
]
diff --git a/posthog/clickhouse/logs/logs_volume_buckets.py b/posthog/clickhouse/logs/logs_volume_buckets.py
new file mode 100644
index 000000000000..14a38bff024a
--- /dev/null
+++ b/posthog/clickhouse/logs/logs_volume_buckets.py
@@ -0,0 +1,58 @@
+from django.conf import settings
+
+from posthog.clickhouse.table_engines import AggregatingMergeTree, Distributed, ReplicationScheme
+
+# Log volume rollup on a 5-minute UTC bucket grid, for anomaly detection.
+# Higher-cardinality evidence dimensions (service.version, instrumentation
+# scope, host/pod) are deliberately excluded to keep the series count low;
+# they belong on the detection output, not the rollup.
+#
+# The table holds partial counts: every insert lands as its own rows and only
+# merges collapse equal keys by summing, so readers must always GROUP BY the
+# key columns and sum(log_count), never read rows raw. There is no dedup: a
+# writer that inserts the same block twice (for example a replayed Kafka
+# batch) inflates the count, which is accepted because small inflation beats
+# a missed bucket.
+#
+# ORDER BY is time-early (unlike logs34's dims-early tail) because the dominant
+# read is one team over a bucket window with no dim predicate, and rows per
+# series per day are few enough that a dims-early key would make every granule
+# span the whole day.
+#
+# TTL is 42 days (6 weekly samples per time-of-week slot), independent of raw
+# log retention.
+
+TABLE_NAME = "logs_volume_buckets"
+
+
+def LOGS_VOLUME_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),
+ `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)
+TTL time_bucket + INTERVAL 42 DAY
+SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1
+"""
+
+
+def LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL():
+ return """
+CREATE TABLE IF NOT EXISTS {database}.logs_volume_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/0300_logs_volume_buckets_summing.py b/posthog/clickhouse/migrations/0300_logs_volume_buckets_summing.py
new file mode 100644
index 000000000000..52b95bab894d
--- /dev/null
+++ b/posthog/clickhouse/migrations/0300_logs_volume_buckets_summing.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_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL, LOGS_VOLUME_BUCKETS_TABLE_SQL
+
+operations = [
+ run_sql_with_exceptions(LOGS_VOLUME_BUCKETS_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS_VOLUME_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 4dc135326081..002f6031acc3 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0299_drop_logs_volume_buckets
+0300_logs_volume_buckets_summing
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index 31218ea0eee9..422682b0a1f4 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -29,6 +29,8 @@ from posthog.clickhouse.logs import (
LOGS_DISTRIBUTED_TABLE_SQL,
LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,
LOGS_KAFKA_METRICS_TABLE_SQL,
+ LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL,
+ LOGS_VOLUME_BUCKETS_TABLE_SQL,
)
from posthog.clickhouse.metrics import (
METRIC_ATTRIBUTES_MV,
@@ -419,6 +421,7 @@ CREATE_MERGETREE_TABLE_QUERIES = (
LOGS34_TABLE_SQL,
LOG_ATTRIBUTES2_TABLE_SQL,
LOG_ATTRIBUTES3_TABLE_SQL,
+ LOGS_VOLUME_BUCKETS_TABLE_SQL,
TRACE_SPANS_TABLE_SQL,
TRACE_ATTRIBUTES_TABLE_SQL,
TRACE_ATTRIBUTES2_TABLE_SQL,
@@ -507,6 +510,7 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (
LOG_ATTRIBUTES3_DISTRIBUTED_TABLE_SQL,
LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,
LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,
+ LOGS_VOLUME_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 a8709dadca33..ab56813dc85a 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -3777,6 +3777,34 @@
'''
# ---
+# name: test_create_table_query[logs_volume_buckets]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_volume_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),
+ `log_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs_volume_buckets', '{replica}-{shard}')
+ PARTITION BY toDate(time_bucket)
+ ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text)
+ TTL time_bucket + INTERVAL 42 DAY
+ SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1
+
+ '''
+# ---
+# name: test_create_table_query[logs_volume_buckets_distributed]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_volume_buckets_distributed AS posthog_test.logs_volume_buckets ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'logs_volume_buckets')
+
+ '''
+# ---
# name: test_create_table_query[marketing_conversions_preaggregated]
'''
@@ -10612,6 +10640,27 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[logs_volume_buckets]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_volume_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),
+ `log_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs_volume_buckets', '{replica}-{shard}')
+ PARTITION BY toDate(time_bucket)
+ ORDER BY (team_id, time_bucket, service_name, namespace, environment, severity_text)
+ TTL time_bucket + INTERVAL 42 DAY
+ SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[message_assets_data]
'''