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): give pattern buckets the volume rollup's retention (#115484)

Jon McCallum75c32691edd8
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 86a7d2fef41f..bdf4a894c5cf 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -599,10 +599,10 @@ SQL
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)"
+ ttl = "time_bucket + toIntervalDay(greatest(42, retention_days))"
settings = {
index_granularity = "8192"
- ttl_only_drop_parts = "1"
+ ttl_only_drop_parts = "0"
}
column "team_id" {
type = "Int32"
@@ -629,6 +629,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
@@ -664,6 +667,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 04c7dff2ebf5..96e5002c5f5d 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -1096,10 +1096,10 @@ SQL
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)"
+ ttl = "time_bucket + toIntervalDay(greatest(42, retention_days))"
settings = {
index_granularity = "8192"
- ttl_only_drop_parts = "1"
+ ttl_only_drop_parts = "0"
}
column "team_id" {
type = "Int32"
@@ -1126,6 +1126,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
@@ -1161,6 +1164,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index eb0228813769..205e6d3ea379 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -5826,10 +5826,10 @@ SQL
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)"
+ ttl = "time_bucket + toIntervalDay(greatest(42, retention_days))"
settings = {
index_granularity = "8192"
- ttl_only_drop_parts = "1"
+ ttl_only_drop_parts = "0"
}
column "team_id" {
type = "Int32"
@@ -5856,6 +5856,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
@@ -5891,6 +5894,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index f40d7c54d700..e577dd701a27 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -682,10 +682,10 @@ SQL
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)"
+ ttl = "time_bucket + toIntervalDay(greatest(42, retention_days))"
settings = {
index_granularity = "8192"
- ttl_only_drop_parts = "1"
+ ttl_only_drop_parts = "0"
}
column "team_id" {
type = "Int32"
@@ -712,6 +712,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
@@ -747,6 +750,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index 8d99bfb4e621..4630123145f7 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -682,10 +682,10 @@ SQL
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)"
+ ttl = "time_bucket + toIntervalDay(greatest(42, retention_days))"
settings = {
index_granularity = "8192"
- ttl_only_drop_parts = "1"
+ ttl_only_drop_parts = "0"
}
column "team_id" {
type = "Int32"
@@ -712,6 +712,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
@@ -747,6 +750,9 @@ SQL
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
diff --git a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
index 5ba62b99d817..9956633c07e4 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
@@ -739,10 +739,10 @@ database "posthog" {
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)"
+ ttl = "time_bucket + toIntervalDay(greatest(42, retention_days))"
settings = {
index_granularity = "8192"
- ttl_only_drop_parts = "1"
+ ttl_only_drop_parts = "0"
}
column "team_id" {
type = "Int32"
@@ -769,6 +769,9 @@ database "posthog" {
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
@@ -804,6 +807,9 @@ database "posthog" {
column "pattern" {
type = "String"
}
+ column "retention_days" {
+ type = "SimpleAggregateFunction(max, UInt16)"
+ }
column "log_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index 369a0a21df42..075a462f1025 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -175,8 +175,9 @@ CREATE TABLE posthog.logs_pattern_buckets (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
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;
+) 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(greatest(42, retention_days)) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 0;
CREATE TABLE posthog.logs_pattern_buckets_distributed (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
@@ -186,6 +187,7 @@ CREATE TABLE posthog.logs_pattern_buckets_distributed (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
log_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index 743d3bbd4f94..32b105251309 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -267,8 +267,9 @@ CREATE TABLE posthog.logs_pattern_buckets (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
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;
+) 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(greatest(42, retention_days)) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 0;
CREATE TABLE posthog.logs_pattern_buckets_distributed (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
@@ -278,6 +279,7 @@ CREATE TABLE posthog.logs_pattern_buckets_distributed (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
log_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 56186244153c..79c0d63b1abd 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -1176,8 +1176,9 @@ CREATE TABLE posthog.logs_pattern_buckets (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
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;
+) 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(greatest(42, retention_days)) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 0;
CREATE TABLE posthog.logs_pattern_buckets_distributed (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
@@ -1187,6 +1188,7 @@ CREATE TABLE posthog.logs_pattern_buckets_distributed (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
log_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index ed73696c6151..09821a18d3a1 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -199,8 +199,9 @@ CREATE TABLE posthog.logs_pattern_buckets (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
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;
+) 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(greatest(42, retention_days)) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 0;
CREATE TABLE posthog.logs_pattern_buckets_distributed (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
@@ -210,6 +211,7 @@ CREATE TABLE posthog.logs_pattern_buckets_distributed (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
log_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index a1cbf684c4b3..59566ebaba62 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -199,8 +199,9 @@ CREATE TABLE posthog.logs_pattern_buckets (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
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;
+) 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(greatest(42, retention_days)) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 0;
CREATE TABLE posthog.logs_pattern_buckets_distributed (
team_id Int32,
time_bucket DateTime('UTC') CODEC(DoubleDelta, ZSTD(1)),
@@ -210,6 +211,7 @@ CREATE TABLE posthog.logs_pattern_buckets_distributed (
severity_text LowCardinality(String),
pattern_version UInt8,
pattern String,
+ retention_days SimpleAggregateFunction(max, UInt16),
log_count SimpleAggregateFunction(sum, UInt64)
) ENGINE = Distributed('logs', 'posthog', 'logs_pattern_buckets');
CREATE TABLE posthog.logs_volume_buckets (
diff --git a/posthog/clickhouse/logs/logs_pattern_buckets.py b/posthog/clickhouse/logs/logs_pattern_buckets.py
index 01d67a6fe392..45aec484d826 100644
--- a/posthog/clickhouse/logs/logs_pattern_buckets.py
+++ b/posthog/clickhouse/logs/logs_pattern_buckets.py
@@ -34,14 +34,16 @@ from posthog.clickhouse.table_engines import AggregatingMergeTree, Distributed,
# 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,
+# TTL, `retention_days`, `ttl_only_drop_parts` and the test-only TTL skip all
+# match logs_volume_buckets, which gives the reasons. The 42-day floor keeps 6
+# weekly samples per time-of-week slot. Nothing caps patterns per series yet, so
# 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():
+ ttl_clause = "" if settings.TEST else "TTL time_bucket + toIntervalDay(greatest(42, retention_days))"
return f"""
CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}
(
@@ -53,14 +55,15 @@ CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NA
`severity_text` LowCardinality(String),
`pattern_version` UInt8,
`pattern` String,
+ `retention_days` SimpleAggregateFunction(max, UInt16),
`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
+{ttl_clause}
+SETTINGS index_granularity = 8192, ttl_only_drop_parts = 0
"""
diff --git a/posthog/clickhouse/migrations/0354_logs_pattern_buckets_retention.py b/posthog/clickhouse/migrations/0354_logs_pattern_buckets_retention.py
new file mode 100644
index 000000000000..fc3c8336abf6
--- /dev/null
+++ b/posthog/clickhouse/migrations/0354_logs_pattern_buckets_retention.py
@@ -0,0 +1,35 @@
+from django.conf import settings
+
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+
+DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+operations = [
+ # Nothing writes to logs_pattern_buckets yet, so the TTL change has no existing
+ # parts to rewrite.
+ run_sql_with_exceptions(
+ f"""
+ALTER TABLE {DB}.logs_pattern_buckets
+ ADD COLUMN IF NOT EXISTS retention_days SimpleAggregateFunction(max, UInt16) AFTER pattern,
+ MODIFY TTL time_bucket + toIntervalDay(greatest(42, retention_days))
+ SETTINGS materialize_ttl_after_modify = 0
+""",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.logs_pattern_buckets MODIFY SETTING ttl_only_drop_parts = 0",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.logs_pattern_buckets_distributed "
+ "ADD COLUMN IF NOT EXISTS retention_days SimpleAggregateFunction(max, UInt16) AFTER pattern",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 26c6e8ed1132..05b480078d01 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0353_flag_evaluations_person_columns
+0354_logs_pattern_buckets_retention
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 100fb997a426..a39236f902ed 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -4201,14 +4201,15 @@
`severity_text` LowCardinality(String),
`pattern_version` UInt8,
`pattern` String,
+ `retention_days` SimpleAggregateFunction(max, UInt16),
`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
+
+ SETTINGS index_granularity = 8192, ttl_only_drop_parts = 0
'''
# ---
@@ -12805,14 +12806,15 @@
`severity_text` LowCardinality(String),
`pattern_version` UInt8,
`pattern` String,
+ `retention_days` SimpleAggregateFunction(max, UInt16),
`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
+
+ SETTINGS index_granularity = 8192, ttl_only_drop_parts = 0
'''
# ---