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.

fix(clickhouse): repair multinode schema drift (#91569)

Hugues Pouillota3d599b7ed07
diff --git a/posthog/clickhouse/hcl/golden/dev/batch_exports.hcl b/posthog/clickhouse/hcl/golden/dev/batch_exports.hcl
index 20fdddfc0c3f..f83e323db06b 100644
--- a/posthog/clickhouse/hcl/golden/dev/batch_exports.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/batch_exports.hcl
@@ -351,7 +351,7 @@ database "posthog" {
table "sharded_events_recent" {
order_by = ["team_id", "toStartOfHour(inserted_at)", "event", "cityHash64(distinct_id)", "cityHash64(uuid)"]
partition_by = "toStartOfDay(inserted_at)"
- ttl = "toDateTime(inserted_at) + toIntervalDay(7)"
+ ttl = "toDate(inserted_at) + toIntervalDay(9)"
settings = {
index_granularity = "8192"
parts_to_delay_insert = "800"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
index a0c2a02e34a4..126a68686a1e 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
@@ -6056,7 +6056,7 @@ database "posthog" {
table "sharded_events_recent" {
order_by = ["team_id", "toStartOfHour(inserted_at)", "event", "cityHash64(distinct_id)", "cityHash64(uuid)"]
partition_by = "toStartOfDay(inserted_at)"
- ttl = "toDateTime(inserted_at) + toIntervalDay(7)"
+ ttl = "toDate(inserted_at) + toIntervalDay(9)"
settings = {
index_granularity = "8192"
ttl_only_drop_parts = "1"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 588003d4a873..a6189e60c20b 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -523,6 +523,12 @@ database "posthog" {
type = "UInt64"
codec = "DoubleDelta, ZSTD(1)"
}
+ column "pattern" {
+ type = "String"
+ }
+ column "pattern_version" {
+ type = "UInt8"
+ }
engine "distributed" {
cluster_name = "posthog_single_shard"
remote_database = "posthog"
@@ -670,6 +676,12 @@ database "posthog" {
type = "UInt64"
codec = "DoubleDelta, ZSTD(1)"
}
+ column "pattern" {
+ type = "String"
+ }
+ column "pattern_version" {
+ type = "UInt8"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index e34354ddc47f..668cf082ae3d 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -4931,6 +4931,12 @@ database "posthog" {
type = "UInt64"
codec = "DoubleDelta, ZSTD(1)"
}
+ column "pattern" {
+ type = "String"
+ }
+ column "pattern_version" {
+ type = "UInt8"
+ }
engine "distributed" {
cluster_name = "posthog_single_shard"
remote_database = "posthog"
@@ -5078,6 +5084,12 @@ database "posthog" {
type = "UInt64"
codec = "DoubleDelta, ZSTD(1)"
}
+ column "pattern" {
+ type = "String"
+ }
+ column "pattern_version" {
+ type = "UInt8"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
@@ -10352,7 +10364,7 @@ SQL
table "sharded_events_recent" {
order_by = ["team_id", "toStartOfHour(inserted_at)", "event", "cityHash64(distinct_id)", "cityHash64(uuid)"]
partition_by = "toStartOfDay(inserted_at)"
- ttl = "toDateTime(inserted_at) + toIntervalDay(7)"
+ ttl = "toDate(inserted_at) + toIntervalDay(9)"
settings = {
index_granularity = "8192"
ttl_only_drop_parts = "1"
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/batch_exports.hcl b/posthog/clickhouse/hcl/golden/prod-eu/batch_exports.hcl
index ea5001e24229..6c82589587ae 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/batch_exports.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/batch_exports.hcl
@@ -351,7 +351,7 @@ database "posthog" {
table "sharded_events_recent" {
order_by = ["team_id", "toStartOfHour(inserted_at)", "event", "cityHash64(distinct_id)", "cityHash64(uuid)"]
partition_by = "toStartOfDay(inserted_at)"
- ttl = "toDateTime(inserted_at) + toIntervalDay(7)"
+ ttl = "toDate(inserted_at) + toIntervalDay(9)"
settings = {
index_granularity = "8192"
ttl_only_drop_parts = "1"
diff --git a/posthog/clickhouse/hcl/golden/prod-us/batch_exports.hcl b/posthog/clickhouse/hcl/golden/prod-us/batch_exports.hcl
index 20fdddfc0c3f..f83e323db06b 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/batch_exports.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/batch_exports.hcl
@@ -351,7 +351,7 @@ database "posthog" {
table "sharded_events_recent" {
order_by = ["team_id", "toStartOfHour(inserted_at)", "event", "cityHash64(distinct_id)", "cityHash64(uuid)"]
partition_by = "toStartOfDay(inserted_at)"
- ttl = "toDateTime(inserted_at) + toIntervalDay(7)"
+ ttl = "toDate(inserted_at) + toIntervalDay(9)"
settings = {
index_granularity = "8192"
parts_to_delay_insert = "800"
diff --git a/posthog/clickhouse/hcl/roles/batch_exports/prod-us/tables.hcl b/posthog/clickhouse/hcl/roles/batch_exports/prod-us/tables.hcl
index e868c71b67d5..64444e3c8b90 100644
--- a/posthog/clickhouse/hcl/roles/batch_exports/prod-us/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/batch_exports/prod-us/tables.hcl
@@ -3,7 +3,7 @@ database "posthog" {
override = true
order_by = ["team_id", "toStartOfHour(inserted_at)", "event", "cityHash64(distinct_id)", "cityHash64(uuid)"]
partition_by = "toStartOfDay(inserted_at)"
- ttl = "toDateTime(inserted_at) + toIntervalDay(7)"
+ ttl = "toDate(inserted_at) + toIntervalDay(9)"
settings = {
index_granularity = "8192"
parts_to_delay_insert = "800"
diff --git a/posthog/clickhouse/hcl/roles/coshared/batch_exports_data/tables.hcl b/posthog/clickhouse/hcl/roles/coshared/batch_exports_data/tables.hcl
index dfb594c9f5a8..741c93164d9d 100644
--- a/posthog/clickhouse/hcl/roles/coshared/batch_exports_data/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/coshared/batch_exports_data/tables.hcl
@@ -6,7 +6,7 @@ database "posthog" {
table "sharded_events_recent" {
order_by = ["team_id", "toStartOfHour(inserted_at)", "event", "cityHash64(distinct_id)", "cityHash64(uuid)"]
partition_by = "toStartOfDay(inserted_at)"
- ttl = "toDateTime(inserted_at) + toIntervalDay(7)"
+ ttl = "toDate(inserted_at) + toIntervalDay(9)"
settings = {
index_granularity = "8192"
ttl_only_drop_parts = "1"
diff --git a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
index 9801170e107d..2734786dffd8 100644
--- a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
@@ -196,6 +196,12 @@ database "posthog" {
type = "UInt64"
codec = "DoubleDelta, ZSTD(1)"
}
+ column "pattern" {
+ type = "String"
+ }
+ column "pattern_version" {
+ type = "UInt8"
+ }
engine "distributed" {
cluster_name = "posthog_single_shard"
remote_database = "posthog"
@@ -342,6 +348,12 @@ database "posthog" {
type = "UInt64"
codec = "DoubleDelta, ZSTD(1)"
}
+ column "pattern" {
+ type = "String"
+ }
+ column "pattern_version" {
+ type = "UInt8"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
diff --git a/posthog/clickhouse/hcl/sql/dev/batch_exports.sql b/posthog/clickhouse/hcl/sql/dev/batch_exports.sql
index 9c44756e091d..15bda2e755da 100644
--- a/posthog/clickhouse/hcl/sql/dev/batch_exports.sql
+++ b/posthog/clickhouse/hcl/sql/dev/batch_exports.sql
@@ -122,7 +122,7 @@ CREATE TABLE posthog.sharded_events_recent (
_timestamp DateTime,
_offset UInt64,
inserted_at DateTime64(6, 'UTC') DEFAULT now64()
-) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/batch_exports/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDateTime(inserted_at) + toIntervalDay(7) SETTINGS index_granularity = 8192, parts_to_delay_insert = 800, parts_to_throw_insert = 1500, ttl_only_drop_parts = 1;
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/batch_exports/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDate(inserted_at) + toIntervalDay(9) SETTINGS index_granularity = 8192, parts_to_delay_insert = 800, parts_to_throw_insert = 1500, ttl_only_drop_parts = 1;
CREATE TABLE posthog.writable_query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sql
index 78031efaa7be..8ede6add2d6c 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql
@@ -1172,7 +1172,7 @@ CREATE TABLE posthog.sharded_events_recent (
_timestamp DateTime,
_offset UInt64,
inserted_at DateTime64(6, 'UTC') DEFAULT now64()
-) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDateTime(inserted_at) + toIntervalDay(7) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDate(inserted_at) + toIntervalDay(9) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.sharded_experiment_exposures_preaggregated (
team_id Int64,
job_id UUID,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index aee4cb4a977f..91580af78b82 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -136,6 +136,8 @@ CREATE TABLE posthog.logs32 (
_bytes_uncompressed UInt64 CODEC(DoubleDelta, ZSTD(1)),
_bytes_compressed UInt64 CODEC(DoubleDelta, ZSTD(1)),
_record_count UInt64 CODEC(DoubleDelta, ZSTD(1)),
+ pattern String,
+ pattern_version UInt8,
INDEX idx_severity_text_set severity_text TYPE set(10) GRANULARITY 1,
INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 1,
@@ -1222,7 +1224,9 @@ CREATE TABLE posthog.logs (
_offset UInt64 CODEC(DoubleDelta, ZSTD(1)),
_bytes_uncompressed UInt64 CODEC(DoubleDelta, ZSTD(1)),
_bytes_compressed UInt64 CODEC(DoubleDelta, ZSTD(1)),
- _record_count UInt64 CODEC(DoubleDelta, ZSTD(1))
+ _record_count UInt64 CODEC(DoubleDelta, ZSTD(1)),
+ pattern String,
+ pattern_version UInt8
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs32');
CREATE TABLE posthog.metric_samples (
team_id Int32,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 41602467d1ef..84ee8c2492b4 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -973,6 +973,8 @@ CREATE TABLE posthog.logs32 (
_bytes_uncompressed UInt64 CODEC(DoubleDelta, ZSTD(1)),
_bytes_compressed UInt64 CODEC(DoubleDelta, ZSTD(1)),
_record_count UInt64 CODEC(DoubleDelta, ZSTD(1)),
+ pattern String,
+ pattern_version UInt8,
INDEX idx_severity_text_set severity_text TYPE set(10) GRANULARITY 1,
INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 1,
@@ -1895,7 +1897,7 @@ CREATE TABLE posthog.sharded_events_recent (
_timestamp DateTime,
_offset UInt64,
inserted_at DateTime64(6, 'UTC') DEFAULT now64()
-) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDateTime(inserted_at) + toIntervalDay(7) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDate(inserted_at) + toIntervalDay(9) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.sharded_experiment_exposures_preaggregated (
team_id Int64,
job_id UUID,
@@ -6145,7 +6147,9 @@ CREATE TABLE posthog.logs (
_offset UInt64 CODEC(DoubleDelta, ZSTD(1)),
_bytes_uncompressed UInt64 CODEC(DoubleDelta, ZSTD(1)),
_bytes_compressed UInt64 CODEC(DoubleDelta, ZSTD(1)),
- _record_count UInt64 CODEC(DoubleDelta, ZSTD(1))
+ _record_count UInt64 CODEC(DoubleDelta, ZSTD(1)),
+ pattern String,
+ pattern_version UInt8
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs32');
CREATE TABLE posthog.marketing_conversions_preaggregated (
team_id Int64,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/batch_exports.sql b/posthog/clickhouse/hcl/sql/prod-eu/batch_exports.sql
index 620bee688e63..36b2c10b91f0 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/batch_exports.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/batch_exports.sql
@@ -123,7 +123,7 @@ CREATE TABLE posthog.sharded_events_recent (
_timestamp DateTime,
_offset UInt64,
inserted_at DateTime64(6, 'UTC') DEFAULT now64()
-) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/batch_exports/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDateTime(inserted_at) + toIntervalDay(7) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/batch_exports/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDate(inserted_at) + toIntervalDay(9) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.writable_query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
diff --git a/posthog/clickhouse/hcl/sql/prod-us/batch_exports.sql b/posthog/clickhouse/hcl/sql/prod-us/batch_exports.sql
index 122e81842535..77725791f004 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/batch_exports.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/batch_exports.sql
@@ -122,7 +122,7 @@ CREATE TABLE posthog.sharded_events_recent (
_timestamp DateTime,
_offset UInt64,
inserted_at DateTime64(6, 'UTC') DEFAULT now64()
-) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/batch_exports/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDateTime(inserted_at) + toIntervalDay(7) SETTINGS index_granularity = 8192, parts_to_delay_insert = 800, parts_to_throw_insert = 1500, ttl_only_drop_parts = 1;
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/batch_exports/{shard}/posthog.sharded_events_recent', '{replica}', _timestamp) ORDER BY (team_id, toStartOfHour(inserted_at), event, cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toStartOfDay(inserted_at) TTL toDate(inserted_at) + toIntervalDay(9) SETTINGS index_granularity = 8192, parts_to_delay_insert = 800, parts_to_throw_insert = 1500, ttl_only_drop_parts = 1;
CREATE TABLE posthog.writable_query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),