: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(clickhouse): bump hclexp pin, trim flag_evaluations codecs (#77401)
Paweł Szczur7eb965133a63
diff --git a/.agents/skills/clickhouse-migrations/SKILL.md b/.agents/skills/clickhouse-migrations/SKILL.mdindex 778fa9b709c8..2baa21d7b1e0 100644--- a/.agents/skills/clickhouse-migrations/SKILL.md+++ b/.agents/skills/clickhouse-migrations/SKILL.md@@ -49,6 +49,7 @@ Distributed engine:- If a function generating SQL has on_cluster param, always set `on_cluster=False`- Use `sharded=True` when altering sharded tables- Use `is_alter_on_replicated_table=True` when altering non-sharded replicated tables+- **Never write `CODEC(ZSTD(1))` on a column** — the server already compresses every column with ZSTD, so it buys nothing. Declare a CODEC only where it beats that default, and check the `ORDER BY` first: `Delta`/`DoubleDelta` need the column near-sorted in storage order (a leading sort-key prefix), and lose on a column the key only buckets or omits. `T64`/`Gorilla` are ordering-independent. Put it on the storage table only — a CODEC on a Distributed or Kafka table is inert metadata that drifts from the sharded table it fronts.- **Never write a `DROP COLUMN` migration yourself** — `DROP COLUMN` can get stuck in ClickHouse and block releases. Column removal is a two-step process: (1) the ClickHouse team drops the column directly on the cluster, then (2) you write a migration with the matching `DROP COLUMN` so the codebase schema stays in sync. Never initiate the drop from a migration without the ClickHouse team having done step 1 first.- **Never drop or recreate `kafka_events_json_ws` or `events_json_ws_mv`** — these tables are a no-go zone. The MV definition differs significantly between US prod, EU prod, and dev (dozens of environment-specific `mat_*` columns) and those differences are **not reflected in the repo**. Dropping and recreating from repo SQL would destroy the environment-specific schema and break event ingestion. Any change must go through the ClickHouse team.diff --git a/posthog/clickhouse/hcl/README.md b/posthog/clickhouse/hcl/README.mdindex 223ae1a33bee..2c563c161fb3 100644--- a/posthog/clickhouse/hcl/README.md+++ b/posthog/clickhouse/hcl/README.md@@ -119,6 +119,12 @@ build it yourself with `go build -o hclexp ./cmd/hclexp` in `../../../../python-`query = file("sql/<object>.sql")` (resolved relative to the layer file). The loader normalizes`file()`, heredoc, and inline forms to one canonical query, so the form is purely cosmetic — editthe `.sql`. `gen-sql.sh`/`gen-golden.sh` emit the beautified form.+ - a column list shared by a sharded table and its Distributed siblings → an `abstract` table the+ instances `extend`, kept codec-free. Most tables need no codec at all — the server already+ compresses with ZSTD, and `posthog/clickhouse/migrations/AGENTS.md` has the rule for the rare+ column that earns one. Where a column does, add it to the storage instance alone via+ `patch_column "<col>" { codec = ... }`: a Distributed table stores nothing, so a codec there is+ inert metadata that only invites the two column lists to drift.2. **Preview the DDL** the change produces, per node:diff --git a/posthog/clickhouse/hcl/bin/image.txt b/posthog/clickhouse/hcl/bin/image.txtindex c69db2dc760a..343987c481ab 100644--- a/posthog/clickhouse/hcl/bin/image.txt+++ b/posthog/clickhouse/hcl/bin/image.txt@@ -1 +1 @@-ghcr.io/posthog/chschema:sha-6db99e9+ghcr.io/posthog/chschema:sha-5a3a7cediff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hclindex 16b665c71043..af10328dd843 100644--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl@@ -1,7 +1,7 @@database "posthog" {table "adhoc_events_deletion" {order_by = ["team_id", "uuid"]- ttl = "deleted_at + toIntervalMonth(3)"+ ttl = "deleted_at + toIntervalMonth(3) WHERE is_deleted = 1"settings = {index_granularity = "8192"}@@ -1693,29 +1693,23 @@ database "posthog" {type = "UUID"}column "timestamp" {- type = "DateTime64(6, 'UTC')"- codec = "DoubleDelta, ZSTD(1)"+ type = "DateTime64(6, 'UTC')"}column "inserted_at" {type = "DateTime64(6, 'UTC')"default = "timestamp"- codec = "DoubleDelta, ZSTD(1)"}column "distinct_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "session_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "device_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "flag_key" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "response" {type = "LowCardinality(String)"@@ -1730,17 +1724,14 @@ database "posthog" {type = "LowCardinality(String)"}column "request_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "evaluated_at" {type = "DateTime64(6, 'UTC')"default = "timestamp"- codec = "DoubleDelta, ZSTD(1)"}column "error" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "locally_evaluated" {type = "Bool"@@ -1764,12 +1755,10 @@ database "posthog" {type = "LowCardinality(String)"}column "current_url" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "pathname" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "country_code" {type = "LowCardinality(String)"@@ -1778,24 +1767,19 @@ database "posthog" {type = "LowCardinality(String)"}column "group_0" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_1" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_2" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_3" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_4" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "_timestamp" {type = "DateTime"@@ -6140,29 +6124,23 @@ database "posthog" {type = "UUID"}column "timestamp" {- type = "DateTime64(6, 'UTC')"- codec = "DoubleDelta, ZSTD(1)"+ type = "DateTime64(6, 'UTC')"}column "inserted_at" {type = "DateTime64(6, 'UTC')"default = "timestamp"- codec = "DoubleDelta, ZSTD(1)"}column "distinct_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "session_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "device_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "flag_key" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "response" {type = "LowCardinality(String)"@@ -6177,17 +6155,14 @@ database "posthog" {type = "LowCardinality(String)"}column "request_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "evaluated_at" {type = "DateTime64(6, 'UTC')"default = "timestamp"- codec = "DoubleDelta, ZSTD(1)"}column "error" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "locally_evaluated" {type = "Bool"@@ -6211,12 +6186,10 @@ database "posthog" {type = "LowCardinality(String)"}column "current_url" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "pathname" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "country_code" {type = "LowCardinality(String)"@@ -6225,24 +6198,19 @@ database "posthog" {type = "LowCardinality(String)"}column "group_0" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_1" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_2" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_3" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_4" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "_timestamp" {type = "DateTime"diff --git a/posthog/clickhouse/hcl/roles/data/local/tables.hcl b/posthog/clickhouse/hcl/roles/data/local/tables.hclindex 2c8768f51616..c9506b2a909d 100644--- a/posthog/clickhouse/hcl/roles/data/local/tables.hcl+++ b/posthog/clickhouse/hcl/roles/data/local/tables.hcl@@ -8,29 +8,23 @@ database "posthog" {type = "UUID"}column "timestamp" {- type = "DateTime64(6, 'UTC')"- codec = "DoubleDelta, ZSTD(1)"+ type = "DateTime64(6, 'UTC')"}column "inserted_at" {type = "DateTime64(6, 'UTC')"default = "timestamp"- codec = "DoubleDelta, ZSTD(1)"}column "distinct_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "session_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "device_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "flag_key" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "response" {type = "LowCardinality(String)"@@ -45,17 +39,14 @@ database "posthog" {type = "LowCardinality(String)"}column "request_id" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "evaluated_at" {type = "DateTime64(6, 'UTC')"default = "timestamp"- codec = "DoubleDelta, ZSTD(1)"}column "error" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "locally_evaluated" {type = "Bool"@@ -79,12 +70,10 @@ database "posthog" {type = "LowCardinality(String)"}column "current_url" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "pathname" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "country_code" {type = "LowCardinality(String)"@@ -93,24 +82,19 @@ database "posthog" {type = "LowCardinality(String)"}column "group_0" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_1" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_2" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_3" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "group_4" {- type = "String"- codec = "ZSTD(1)"+ type = "String"}column "_timestamp" {type = "DateTime"@@ -124,7 +108,7 @@ database "posthog" {}table "adhoc_events_deletion" {order_by = ["team_id", "uuid"]- ttl = "deleted_at + toIntervalMonth(3)"+ ttl = "deleted_at + toIntervalMonth(3) WHERE is_deleted = 1"settings = {index_granularity = "8192"}diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sqlindex 2754663d9855..6a5ae2b8c176 100644--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql@@ -7,7 +7,7 @@ CREATE TABLE posthog.adhoc_events_deletion (created_at DateTime64(6, 'UTC') DEFAULT now64(),deleted_at DateTime,is_deleted UInt8 DEFAULT 0-) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.adhoc_events_deletion', '{replica}-{shard}', deleted_at, is_deleted) ORDER BY (team_id, uuid) TTL deleted_at + toIntervalMonth(3) SETTINGS index_granularity = 8192;+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.adhoc_events_deletion', '{replica}-{shard}', deleted_at, is_deleted) ORDER BY (team_id, uuid) TTL deleted_at + toIntervalMonth(3) WHERE is_deleted = 1 SETTINGS index_granularity = 8192;CREATE TABLE posthog.ai_events (uuid UUID,event LowCardinality(String),@@ -1172,19 +1172,19 @@ CREATE TABLE posthog.sharded_experiment_exposures_preaggregated (CREATE TABLE posthog.sharded_flag_evaluations (team_id Int64,uuid UUID,- timestamp DateTime64(6, 'UTC') CODEC(DoubleDelta, ZSTD(1)),- inserted_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- distinct_id String CODEC(ZSTD(1)),- session_id String CODEC(ZSTD(1)),- device_id String CODEC(ZSTD(1)),- flag_key String CODEC(ZSTD(1)),+ timestamp DateTime64(6, 'UTC'),+ inserted_at DateTime64(6, 'UTC') DEFAULT timestamp,+ distinct_id String,+ session_id String,+ device_id String,+ flag_key String,response LowCardinality(String),flag_id UInt64,flag_version UInt32,reason LowCardinality(String),- request_id String CODEC(ZSTD(1)),- evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- error String CODEC(ZSTD(1)),+ request_id String,+ evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp,+ error String,locally_evaluated Bool,lib LowCardinality(String),lib_version LowCardinality(String),@@ -1192,15 +1192,15 @@ CREATE TABLE posthog.sharded_flag_evaluations (os LowCardinality(String),os_version LowCardinality(String),app_version LowCardinality(String),- current_url String CODEC(ZSTD(1)),- pathname String CODEC(ZSTD(1)),+ current_url String,+ pathname String,country_code LowCardinality(String),subdivision_1_code LowCardinality(String),- group_0 String CODEC(ZSTD(1)),- group_1 String CODEC(ZSTD(1)),- group_2 String CODEC(ZSTD(1)),- group_3 String CODEC(ZSTD(1)),- group_4 String CODEC(ZSTD(1)),+ group_0 String,+ group_1 String,+ group_2 String,+ group_3 String,+ group_4 String,_timestamp DateTime,_offset UInt64,_partition UInt64,@@ -3122,19 +3122,19 @@ CREATE TABLE posthog.experiment_exposures_preaggregated (CREATE TABLE posthog.flag_evaluations (team_id Int64,uuid UUID,- timestamp DateTime64(6, 'UTC') CODEC(DoubleDelta, ZSTD(1)),- inserted_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- distinct_id String CODEC(ZSTD(1)),- session_id String CODEC(ZSTD(1)),- device_id String CODEC(ZSTD(1)),- flag_key String CODEC(ZSTD(1)),+ timestamp DateTime64(6, 'UTC'),+ inserted_at DateTime64(6, 'UTC') DEFAULT timestamp,+ distinct_id String,+ session_id String,+ device_id String,+ flag_key String,response LowCardinality(String),flag_id UInt64,flag_version UInt32,reason LowCardinality(String),- request_id String CODEC(ZSTD(1)),- evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- error String CODEC(ZSTD(1)),+ request_id String,+ evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp,+ error String,locally_evaluated Bool,lib LowCardinality(String),lib_version LowCardinality(String),@@ -3142,15 +3142,15 @@ CREATE TABLE posthog.flag_evaluations (os LowCardinality(String),os_version LowCardinality(String),app_version LowCardinality(String),- current_url String CODEC(ZSTD(1)),- pathname String CODEC(ZSTD(1)),+ current_url String,+ pathname String,country_code LowCardinality(String),subdivision_1_code LowCardinality(String),- group_0 String CODEC(ZSTD(1)),- group_1 String CODEC(ZSTD(1)),- group_2 String CODEC(ZSTD(1)),- group_3 String CODEC(ZSTD(1)),- group_4 String CODEC(ZSTD(1)),+ group_0 String,+ group_1 String,+ group_2 String,+ group_3 String,+ group_4 String,_timestamp DateTime,_offset UInt64,_partition UInt64diff --git a/posthog/clickhouse/migrations/AGENTS.md b/posthog/clickhouse/migrations/AGENTS.mdindex 41953ea1f954..990c976eb358 100644--- a/posthog/clickhouse/migrations/AGENTS.md+++ b/posthog/clickhouse/migrations/AGENTS.md@@ -172,6 +172,27 @@ engine=Distributed(>> `ALTER TABLE my_table DROP COLUMN IF EXISTS my_col`+> [!INFO]+> Do not write `CODEC(ZSTD(1))` on a column. The server config already compresses every+> column with ZSTD, so a per-column ZSTD codec buys nothing and just makes the column list+> longer.+>+> Declare a CODEC only where it beats that default, and check the `ORDER BY` before you do.+> The delta family (`Delta`, `DoubleDelta`) pays off on a column that is near-sorted **in+> storage order**, so it wants a leading `ORDER BY` prefix. A timestamp that the sort key+> only buckets (`toDate(timestamp)`), or that is absent from the key entirely, lands on disk+> in effectively random order — `DoubleDelta` then widens its encoding to fit the largest+> jump and strips structure ZSTD would have used, so it loses on both counts. `T64` and+> `Gorilla` do not care about ordering and are the better reach when the sort key is against+> you. Pair whatever you pick with ZSTD as the second stage, `CODEC(DoubleDelta, ZSTD(1))`,+> since the specialization replaces the default compression rather than layering on top of it.+>+> A CODEC belongs on the storage table only. Distributed and Kafka engine tables store+> nothing, but ClickHouse still keeps a declared CODEC in `SHOW CREATE TABLE`, so one there+> is inert metadata that drifts from the sharded table it fronts. In the declarative HCL+> schema this means a CODEC-free `abstract` column list, with the codecs added back on the+> sharded instance via `patch_column` — see `posthog/clickhouse/hcl/README.md`.+> [!CAUTION]> Never drop or recreate `kafka_events_json_ws` or `events_json_ws_mv`. These tables are a> no-go zone. The MV definition differs between US prod, EU prod, and dev (dozens ofdiff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex e541a6311a45..5c92f99935dc 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -1907,19 +1907,19 @@(team_id Int64,uuid UUID,- timestamp DateTime64(6, 'UTC') CODEC(DoubleDelta, ZSTD(1)),- inserted_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- distinct_id String CODEC(ZSTD(1)),- session_id String CODEC(ZSTD(1)),- device_id String CODEC(ZSTD(1)),- flag_key String CODEC(ZSTD(1)),+ timestamp DateTime64(6, 'UTC'),+ inserted_at DateTime64(6, 'UTC') DEFAULT timestamp,+ distinct_id String,+ session_id String,+ device_id String,+ flag_key String,response LowCardinality(String),flag_id UInt64,flag_version UInt32,reason LowCardinality(String),- request_id String CODEC(ZSTD(1)),- evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- error String CODEC(ZSTD(1)),+ request_id String,+ evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp,+ error String,locally_evaluated Bool,lib LowCardinality(String),lib_version LowCardinality(String),@@ -1927,15 +1927,15 @@os LowCardinality(String),os_version LowCardinality(String),app_version LowCardinality(String),- current_url String CODEC(ZSTD(1)),- pathname String CODEC(ZSTD(1)),+ current_url String,+ pathname String,country_code LowCardinality(String),subdivision_1_code LowCardinality(String),- group_0 String CODEC(ZSTD(1)),- group_1 String CODEC(ZSTD(1)),- group_2 String CODEC(ZSTD(1)),- group_3 String CODEC(ZSTD(1)),- group_4 String CODEC(ZSTD(1))+ group_0 String,+ group_1 String,+ group_2 String,+ group_3 String,+ group_4 String, _timestamp DateTime, _offset UInt64@@ -6934,19 +6934,19 @@(team_id Int64,uuid UUID,- timestamp DateTime64(6, 'UTC') CODEC(DoubleDelta, ZSTD(1)),- inserted_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- distinct_id String CODEC(ZSTD(1)),- session_id String CODEC(ZSTD(1)),- device_id String CODEC(ZSTD(1)),- flag_key String CODEC(ZSTD(1)),+ timestamp DateTime64(6, 'UTC'),+ inserted_at DateTime64(6, 'UTC') DEFAULT timestamp,+ distinct_id String,+ session_id String,+ device_id String,+ flag_key String,response LowCardinality(String),flag_id UInt64,flag_version UInt32,reason LowCardinality(String),- request_id String CODEC(ZSTD(1)),- evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- error String CODEC(ZSTD(1)),+ request_id String,+ evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp,+ error String,locally_evaluated Bool,lib LowCardinality(String),lib_version LowCardinality(String),@@ -6954,15 +6954,15 @@os LowCardinality(String),os_version LowCardinality(String),app_version LowCardinality(String),- current_url String CODEC(ZSTD(1)),- pathname String CODEC(ZSTD(1)),+ current_url String,+ pathname String,country_code LowCardinality(String),subdivision_1_code LowCardinality(String),- group_0 String CODEC(ZSTD(1)),- group_1 String CODEC(ZSTD(1)),- group_2 String CODEC(ZSTD(1)),- group_3 String CODEC(ZSTD(1)),- group_4 String CODEC(ZSTD(1)),+ group_0 String,+ group_1 String,+ group_2 String,+ group_3 String,+ group_4 String,INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1,INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1,INDEX request_id_idx request_id TYPE bloom_filter(0.01) GRANULARITY 1,@@ -9244,19 +9244,19 @@(team_id Int64,uuid UUID,- timestamp DateTime64(6, 'UTC') CODEC(DoubleDelta, ZSTD(1)),- inserted_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- distinct_id String CODEC(ZSTD(1)),- session_id String CODEC(ZSTD(1)),- device_id String CODEC(ZSTD(1)),- flag_key String CODEC(ZSTD(1)),+ timestamp DateTime64(6, 'UTC'),+ inserted_at DateTime64(6, 'UTC') DEFAULT timestamp,+ distinct_id String,+ session_id String,+ device_id String,+ flag_key String,response LowCardinality(String),flag_id UInt64,flag_version UInt32,reason LowCardinality(String),- request_id String CODEC(ZSTD(1)),- evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- error String CODEC(ZSTD(1)),+ request_id String,+ evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp,+ error String,locally_evaluated Bool,lib LowCardinality(String),lib_version LowCardinality(String),@@ -9264,15 +9264,15 @@os LowCardinality(String),os_version LowCardinality(String),app_version LowCardinality(String),- current_url String CODEC(ZSTD(1)),- pathname String CODEC(ZSTD(1)),+ current_url String,+ pathname String,country_code LowCardinality(String),subdivision_1_code LowCardinality(String),- group_0 String CODEC(ZSTD(1)),- group_1 String CODEC(ZSTD(1)),- group_2 String CODEC(ZSTD(1)),- group_3 String CODEC(ZSTD(1)),- group_4 String CODEC(ZSTD(1))+ group_0 String,+ group_1 String,+ group_2 String,+ group_3 String,+ group_4 String, _timestamp DateTime, _offset UInt64@@ -11903,19 +11903,19 @@(team_id Int64,uuid UUID,- timestamp DateTime64(6, 'UTC') CODEC(DoubleDelta, ZSTD(1)),- inserted_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- distinct_id String CODEC(ZSTD(1)),- session_id String CODEC(ZSTD(1)),- device_id String CODEC(ZSTD(1)),- flag_key String CODEC(ZSTD(1)),+ timestamp DateTime64(6, 'UTC'),+ inserted_at DateTime64(6, 'UTC') DEFAULT timestamp,+ distinct_id String,+ session_id String,+ device_id String,+ flag_key String,response LowCardinality(String),flag_id UInt64,flag_version UInt32,reason LowCardinality(String),- request_id String CODEC(ZSTD(1)),- evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp CODEC(DoubleDelta, ZSTD(1)),- error String CODEC(ZSTD(1)),+ request_id String,+ evaluated_at DateTime64(6, 'UTC') DEFAULT timestamp,+ error String,locally_evaluated Bool,lib LowCardinality(String),lib_version LowCardinality(String),@@ -11923,15 +11923,15 @@os LowCardinality(String),os_version LowCardinality(String),app_version LowCardinality(String),- current_url String CODEC(ZSTD(1)),- pathname String CODEC(ZSTD(1)),+ current_url String,+ pathname String,country_code LowCardinality(String),subdivision_1_code LowCardinality(String),- group_0 String CODEC(ZSTD(1)),- group_1 String CODEC(ZSTD(1)),- group_2 String CODEC(ZSTD(1)),- group_3 String CODEC(ZSTD(1)),- group_4 String CODEC(ZSTD(1)),+ group_0 String,+ group_1 String,+ group_2 String,+ group_3 String,+ group_4 String,INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1,INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1,INDEX request_id_idx request_id TYPE bloom_filter(0.01) GRANULARITY 1,diff --git a/posthog/models/flag_evaluations/sql.py b/posthog/models/flag_evaluations/sql.pyindex 8ec61939e50d..7345e0841ed0 100644--- a/posthog/models/flag_evaluations/sql.py+++ b/posthog/models/flag_evaluations/sql.py@@ -44,7 +44,7 @@ FLAG_EVALUATIONS_SHARDING_KEY = "sipHash64(distinct_id)"# move together.FLAG_EVALUATIONS_ORDER_BY = "(team_id, flag_key, toDate(timestamp), cityHash64(distinct_id))"-# One canonical column list, rendered in a Kafka variant and a storage variant.+# One canonical column list, rendered in a Kafka, a Distributed and a storage variant.## The Kafka engine table must NOT carry the timestamp DEFAULTs: JSONEachRow# fills omitted fields with the column default, and the MV's legacy-SDK@@ -53,24 +53,32 @@ FLAG_EVALUATIONS_ORDER_BY = "(team_id, flag_key, toDate(timestamp), cityHash64(d# table fills omitted columns from the Distributed table's own schema before# forwarding to the shard, so without them a direct insert via# writable_flag_evaluations would store epoch instead of the sharded table's-# fallback. That makes the Distributed and sharded column lists identical, which-# is also how the events family declares its CODECs.+# fallback.+#+# No column carries a CODEC. The server already compresses everything with ZSTD,+# and the delta family needs a near-sorted sequence to beat it: ORDER BY only+# buckets timestamp to a day and then sorts on a distinct_id hash, so on disk all+# three DateTime64 columns jump around inside a 24h window. DoubleDelta on that+# widens its encoding to fit the largest jump and strips the byte structure ZSTD+# would otherwise find. Revisit only with measurements, and note that a codec+# would then belong on the sharded table alone — a Distributed table stores+# nothing, so one there is inert metadata that invites the lists to drift._FLAG_EVALUATIONS_COLUMNS_TEMPLATE = """team_id Int64,uuid UUID,- timestamp DateTime64(6, 'UTC'){dt_codec},- inserted_at DateTime64(6, 'UTC'){ts_default}{dt_codec},- distinct_id String{codec},- session_id String{codec},- device_id String{codec},- flag_key String{codec},+ timestamp DateTime64(6, 'UTC'),+ inserted_at DateTime64(6, 'UTC'){ts_default},+ distinct_id String,+ session_id String,+ device_id String,+ flag_key String,response LowCardinality(String),flag_id UInt64,flag_version UInt32,reason LowCardinality(String),- request_id String{codec},- evaluated_at DateTime64(6, 'UTC'){ts_default}{dt_codec},- error String{codec},+ request_id String,+ evaluated_at DateTime64(6, 'UTC'){ts_default},+ error String,locally_evaluated Bool,lib LowCardinality(String),lib_version LowCardinality(String),@@ -78,26 +86,20 @@ _FLAG_EVALUATIONS_COLUMNS_TEMPLATE = """os LowCardinality(String),os_version LowCardinality(String),app_version LowCardinality(String),- current_url String{codec},- pathname String{codec},+ current_url String,+ pathname String,country_code LowCardinality(String),subdivision_1_code LowCardinality(String),- group_0 String{codec},- group_1 String{codec},- group_2 String{codec},- group_3 String{codec},- group_4 String{codec}+ group_0 String,+ group_1 String,+ group_2 String,+ group_3 String,+ group_4 String""".strip()-FLAG_EVALUATIONS_KAFKA_COLUMNS = _FLAG_EVALUATIONS_COLUMNS_TEMPLATE.format(ts_default="", codec="", dt_codec="")+FLAG_EVALUATIONS_KAFKA_COLUMNS = _FLAG_EVALUATIONS_COLUMNS_TEMPLATE.format(ts_default="")-# ZSTD on the plain String columns and DoubleDelta on the timestamps;-# LowCardinality columns compress well on their own.-_FLAG_EVALUATIONS_STORAGE_COLUMNS = _FLAG_EVALUATIONS_COLUMNS_TEMPLATE.format(- ts_default=" DEFAULT timestamp",- codec=" CODEC(ZSTD(1))",- dt_codec=" CODEC(DoubleDelta, ZSTD(1))",-)+_FLAG_EVALUATIONS_COLUMNS = _FLAG_EVALUATIONS_COLUMNS_TEMPLATE.format(ts_default=" DEFAULT timestamp")def FLAG_EVALUATIONS_DATA_TABLE_ENGINE() -> MergeTreeEngine:@@ -128,7 +130,7 @@ FLAG_EVALUATIONS_TABLE_SQL = lambda: (f"""CREATE TABLE IF NOT EXISTS {FLAG_EVALUATIONS_DATA_TABLE}(- {_FLAG_EVALUATIONS_STORAGE_COLUMNS},+ {_FLAG_EVALUATIONS_COLUMNS},INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1,INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1,INDEX request_id_idx request_id TYPE bloom_filter(0.01) GRANULARITY 1,@@ -152,7 +154,7 @@ def _distributed_table_sql(table_name: str) -> str:return f"""CREATE TABLE IF NOT EXISTS {table_name}(- {_FLAG_EVALUATIONS_STORAGE_COLUMNS}+ {_FLAG_EVALUATIONS_COLUMNS}{KAFKA_COLUMNS_WITH_PARTITION})ENGINE = {Distributed(data_table=FLAG_EVALUATIONS_DATA_TABLE, sharding_key=FLAG_EVALUATIONS_SHARDING_KEY)}