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 logs_volume_buckets rollup table for anomaly detection (#80503)

Jon McCallum059ab2ca14f7
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 8b7940b7a25d..5fee8a2b5a8b 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -695,6 +695,78 @@ database "posthog" {
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "bucket_start", "generation", "service_name", "namespace", "environment", "severity_text"]
+ partition_by = "toDate(bucket_start)"
+ ttl = "bucket_start + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "UInt64"
+ }
+ engine "replicated_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 "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "UInt64"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ 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 07ba051e46c1..248b067bc2f5 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -1118,6 +1118,78 @@ SQL
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "bucket_start", "generation", "service_name", "namespace", "environment", "severity_text"]
+ partition_by = "toDate(bucket_start)"
+ ttl = "bucket_start + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "UInt64"
+ }
+ engine "replicated_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 "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "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 11d0409bc692..813b22ac27ba 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -776,6 +776,78 @@ database "posthog" {
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "bucket_start", "generation", "service_name", "namespace", "environment", "severity_text"]
+ partition_by = "toDate(bucket_start)"
+ ttl = "bucket_start + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "UInt64"
+ }
+ engine "replicated_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 "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "UInt64"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ 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 f74cee513ada..2070b947fe0f 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -775,6 +775,78 @@ database "posthog" {
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "bucket_start", "generation", "service_name", "namespace", "environment", "severity_text"]
+ partition_by = "toDate(bucket_start)"
+ ttl = "bucket_start + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "UInt64"
+ }
+ engine "replicated_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 "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "UInt64"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ 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..b347ea6249eb 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
@@ -691,6 +691,76 @@ database "posthog" {
}
}
+ table "logs_volume_buckets" {
+ order_by = ["team_id", "bucket_start", "generation", "service_name", "namespace", "environment", "severity_text"]
+ partition_by = "toDate(bucket_start)"
+ ttl = "bucket_start + toIntervalDay(42)"
+ settings = {
+ index_granularity = "8192"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "UInt64"
+ }
+ engine "replicated_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 "bucket_start" {
+ type = "DateTime('UTC')"
+ codec = "DoubleDelta, ZSTD(1)"
+ }
+ column "generation" {
+ type = "UInt64"
+ }
+ 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 = "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/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index 9dd4a9a7be85..a5acbeddc8f5 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -190,6 +190,26 @@ 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,
+ bucket_start DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ generation UInt64,
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ log_count UInt64
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.logs_volume_buckets', '{replica}-{shard}') ORDER BY (team_id, bucket_start, generation, service_name, namespace, environment, severity_text) PARTITION BY toDate(bucket_start) TTL bucket_start + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_volume_buckets_distributed (
+ team_id Int32,
+ bucket_start DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ generation UInt64,
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ log_count UInt64
+) ENGINE = Distributed('posthog_single_shard', '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 80ba081cd7e4..abc973897bb1 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -267,6 +267,26 @@ 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,
+ bucket_start DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ generation UInt64,
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ log_count UInt64
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.logs_volume_buckets', '{replica}-{shard}') ORDER BY (team_id, bucket_start, generation, service_name, namespace, environment, severity_text) PARTITION BY toDate(bucket_start) TTL bucket_start + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_volume_buckets_distributed (
+ team_id Int32,
+ bucket_start DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ generation UInt64,
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ log_count 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 1e96f743a0bb..c8f6b94c0fc4 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -214,6 +214,26 @@ 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,
+ bucket_start DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ generation UInt64,
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ log_count UInt64
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.logs_volume_buckets', '{replica}-{shard}') ORDER BY (team_id, bucket_start, generation, service_name, namespace, environment, severity_text) PARTITION BY toDate(bucket_start) TTL bucket_start + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_volume_buckets_distributed (
+ team_id Int32,
+ bucket_start DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ generation UInt64,
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ log_count UInt64
+) ENGINE = Distributed('posthog_single_shard', '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 118717db2189..d2fe83d2455c 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -213,6 +213,26 @@ 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,
+ bucket_start DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ generation UInt64,
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ log_count UInt64
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.logs_volume_buckets', '{replica}-{shard}') ORDER BY (team_id, bucket_start, generation, service_name, namespace, environment, severity_text) PARTITION BY toDate(bucket_start) TTL bucket_start + toIntervalDay(42) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.logs_volume_buckets_distributed (
+ team_id Int32,
+ bucket_start DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ generation UInt64,
+ service_name LowCardinality(String),
+ namespace LowCardinality(String),
+ environment LowCardinality(String),
+ severity_text LowCardinality(String),
+ log_count UInt64
+) ENGINE = Distributed('posthog_single_shard', '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 ddda0a1f8ac6..fa40b3d068c2 100644
--- a/posthog/clickhouse/logs/__init__.py
+++ b/posthog/clickhouse/logs/__init__.py
@@ -24,6 +24,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",
@@ -53,4 +54,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..43a3e50cba06
--- /dev/null
+++ b/posthog/clickhouse/logs/logs_volume_buckets.py
@@ -0,0 +1,65 @@
+from django.conf import settings
+
+from posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplicationScheme
+
+# Per-5-minute log volume rollup for anomaly detection. One row per
+# (team, bucket, generation, series) where the series identity is
+# (service_name, namespace, environment, severity_text) — evidence dimensions
+# (service.version, instrumentation_scope, host/pod) are deliberately excluded:
+# they multiply rows 5-30x and belong on the filed issue record, not the rollup.
+#
+# Buckets are fixed wall-clock windows on a 5-minute UTC grid, never sliding.
+# `generation` is the unix-millis start of one insert attempt, allocated and
+# committed in Postgres (LogsVolumeBucketCompletion); storage here is immutable
+# append-only and readers must filter to committed (bucket_start, generation)
+# pairs — visibility is protocol-level, which is why this is a plain MergeTree
+# rather than a Replacing/Collapsing variant (those can't express keys that
+# vanish in a later generation, or double-count across generations).
+#
+# ORDER BY is time-early (unlike logs34's dims-early tail): the dominant read is
+# team + scattered bucket windows with no dim predicate, and with only ~288
+# buckets/series/day a dims-early key makes every granule span the whole day.
+#
+# TTL is 42 days (6 weekly samples per time-of-week slot), decoupled from raw
+# log retention and deliberately capped: longer dilutes the recent picture.
+# Superseded and never-committed generations are not deleted — invisible to
+# readers, they age out with the TTL (~3x row overhead, sub-GB). The writer
+# issues no mutations; explicit cleanup DELETEs stay a deferred option.
+
+TABLE_NAME = "logs_volume_buckets"
+DISTRIBUTED_TABLE_NAME = "logs_volume_buckets_distributed"
+
+
+def LOGS_VOLUME_BUCKETS_TABLE_SQL():
+ return f"""
+CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}
+(
+ `team_id` Int32,
+ `bucket_start` DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ `generation` UInt64,
+ `service_name` LowCardinality(String),
+ `namespace` LowCardinality(String),
+ `environment` LowCardinality(String),
+ `severity_text` LowCardinality(String),
+ `log_count` UInt64
+)
+ENGINE = {MergeTreeEngine(TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY toDate(bucket_start)
+ORDER BY (team_id, bucket_start, generation, service_name, namespace, environment, severity_text)
+TTL bucket_start + 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}.{distributed_table_name} AS {database}.{table_name} ENGINE = {engine}
+""".format(
+ engine=Distributed(
+ data_table=TABLE_NAME,
+ cluster=settings.CLICKHOUSE_LOGS_CLUSTER,
+ ),
+ database=settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE,
+ distributed_table_name=DISTRIBUTED_TABLE_NAME,
+ table_name=TABLE_NAME,
+ )
diff --git a/posthog/clickhouse/migrations/0294_logs_volume_buckets.py b/posthog/clickhouse/migrations/0294_logs_volume_buckets.py
new file mode 100644
index 000000000000..52b95bab894d
--- /dev/null
+++ b/posthog/clickhouse/migrations/0294_logs_volume_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_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 13e3feef566a..d2cb6d8d42c9 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0293_add_session_id_bloom_filter_index
+0294_logs_volume_buckets
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 5c92f99935dc..1c9086876882 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -3827,6 +3827,35 @@
'''
# ---
+# name: test_create_table_query[logs_volume_buckets]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_volume_buckets
+ (
+ `team_id` Int32,
+ `bucket_start` DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ `generation` UInt64,
+ `service_name` LowCardinality(String),
+ `namespace` LowCardinality(String),
+ `environment` LowCardinality(String),
+ `severity_text` LowCardinality(String),
+ `log_count` UInt64
+ )
+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs_volume_buckets', '{replica}-{shard}')
+ PARTITION BY toDate(bucket_start)
+ ORDER BY (team_id, bucket_start, generation, service_name, namespace, environment, severity_text)
+ TTL bucket_start + 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]
'''
@@ -10677,6 +10706,28 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[logs_volume_buckets]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_volume_buckets
+ (
+ `team_id` Int32,
+ `bucket_start` DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
+ `generation` UInt64,
+ `service_name` LowCardinality(String),
+ `namespace` LowCardinality(String),
+ `environment` LowCardinality(String),
+ `severity_text` LowCardinality(String),
+ `log_count` UInt64
+ )
+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs_volume_buckets', '{replica}-{shard}')
+ PARTITION BY toDate(bucket_start)
+ ORDER BY (team_id, bucket_start, generation, service_name, namespace, environment, severity_text)
+ TTL bucket_start + INTERVAL 42 DAY
+ SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[message_assets_data]
'''