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(flags): switch flag_evaluations typed columns to DEFAULT (#83382)

Phil Haack1bbce15f0219
diff --git a/docs/internal/clickhouse-deletion-coverage.md b/docs/internal/clickhouse-deletion-coverage.md
index 5d255d8f25ab..29b6511047f7 100644
--- a/docs/internal/clickhouse-deletion-coverage.md
+++ b/docs/internal/clickhouse-deletion-coverage.md
@@ -47,27 +47,31 @@ That decision predates this document; the older `posthog/models/async_deletion/d
The events property-removal path rewrites rows in a staging table and resets each affected materialized column with `ALTER TABLE … UPDATE <col> = ''`.
That works because `materialize()` creates columns as `DEFAULT <expr>`, which is assignable.
-`flag_evaluations` declares its nine typed columns as true ClickHouse `MATERIALIZED`. Measured against ClickHouse 26.6:
+All of that machinery (column discovery, staging rewrite, shard walk) is scoped to `events`; none of it reaches `flag_evaluations`.
+Until it does, `get_property_removal_shards` refuses to start when the table holds rows matching the request, so a request cannot complete while data it named survives.
+The check costs nothing while the table is empty.
-- Assigning to one is rejected: `Cannot UPDATE materialized column 'session_id'`.
-- Updating `properties` is rejected too, because `flag_key` is materialized from it and sits in the sort key: `Updated column 'properties' affects MATERIALIZED column 'flag_key', which is a key column`.
-- `CREATE TABLE tmp AS sharded_flag_evaluations ENGINE = MergeTree()` inherits the sort key and the column kinds, so the staging table hits the same rejection.
+The schema stopped being a second obstacle with migration `0301_flag_evaluations_default_columns`, which recreated the nine typed columns as `DEFAULT <expr>`, the kind `materialize()` mints on events; they were true ClickHouse `MATERIALIZED` before, which is not assignable at all.
+Measured against ClickHouse 26.6.2 on the `DEFAULT` shape:
-The planned fix is two follow-ups:
+- `CREATE TABLE` accepts a `DEFAULT`-from-`properties` column (`flag_key`) in the sort key, and an insert that omits the typed columns computes them from `properties`.
+- Assigning to a non-key typed column is accepted: `ALTER TABLE … UPDATE session_id = ''` completes. Under `MATERIALIZED` it was rejected with `Cannot UPDATE materialized column 'session_id'`.
+- Updating `properties` is accepted, alone and in the events-path form that resets affected typed columns in the same mutation.
+ The `MATERIALIZED`-era rejection (`Updated column 'properties' affects MATERIALIZED column 'flag_key', which is a key column`) does not fire for `DEFAULT` dependents.
+- An `UPDATE` of `properties` does not recompute the typed columns; rows keep their stored values.
+ The rewrite must reset each affected column explicitly, exactly as the events path already does.
+- `flag_key` itself can never be reset: `ALTER TABLE … UPDATE flag_key = ''` is rejected with `Cannot UPDATE key column 'flag_key'` (`CANNOT_UPDATE_COLUMN`), whatever the column kind.
+ A request naming `$feature_flag` therefore still cannot be honored by mutation; that one property needs a refusal, or the heavier rewrite: `INSERT … SELECT` the cleaned rows omitting the typed columns so the shard recomputes them, then lightweight-delete the originals.
-1. Recreate the table with the typed columns as `DEFAULT <expr>`, the kind `materialize()` mints on events.
- Recreating is only free while the table is empty, so this must land before the producer ships.
-2. Point the events rewrite machinery (column discovery, staging rewrite, shard walk) at the table; today all of it is scoped to `events`.
+The switch also changed two behaviors, measured on the same shape:
-One open question for the schema follow-up: `flag_key` sits in the sort key, and ClickHouse never accepts `UPDATE` on a key column, whatever its kind.
-Whether `UPDATE properties` is accepted once a `DEFAULT` key column depends on it needs measuring.
-If it is still rejected, the fallback is the heavier rewrite: `INSERT … SELECT` the cleaned rows, let the shard recompute the typed columns on write, then lightweight-delete the originals.
+- `SELECT *` on the shard now includes the nine typed columns, where `MATERIALIZED` hid them. Nothing in the repo depended on the hidden shape.
+- An insert that names a typed column stores the given value even when it contradicts `properties`, where `MATERIALIZED` rejected such inserts.
+ Producers must omit the columns; the Kafka path enforces that because `writable_flag_evaluations` does not declare them.
-Until the fix exists, `get_property_removal_shards` refuses to start when the table holds rows matching the request, so a request cannot complete while data it named survives.
-The check costs nothing while the table is empty.
+The remaining fix is pointing the events rewrite machinery at this table, with the `$feature_flag` limitation above built into whatever it does here.
-This is also why `flag_evaluations` is deliberately absent from `MATERIALIZATION_VALID_TABLES`.
-Adding it would let `materialize()` mint `DEFAULT`-kind columns on a table whose property-removal path cannot reset them.
+`flag_evaluations` stays deliberately absent from `MATERIALIZATION_VALID_TABLES` until that lands: new `materialize()`-minted columns would only widen what the unfixed path silently leaves behind.
#### If a request arrives before the fix lands
@@ -101,7 +105,7 @@ Keeping the fork downstream of person resolution is the contract, tracked on #81
## Adding a table
-Register it in `PERSONAL_DATA_TARGETS`, with capability flags reflecting what its schema can actually take.
+Register it in `PERSONAL_DATA_TARGETS`, with capability flags reflecting what its schema can actually take and what the sweep code actually implements: `accepts_property_rewrite` needs the rewrite machinery to reach the table, not just assignable columns.
If it is not going to be swept, add it to `TTL_ONLY_TABLES` with the window you are accepting.
`posthog/clickhouse/test/test_deletion_coverage.py` fails on any storage table that declares `person_properties` and appears in neither list, so the decision has to be made rather than skipped.
diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
index afb8ea713e0c..9ae7977ec6ba 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
@@ -6159,49 +6159,49 @@ database "posthog" {
default = "timestamp"
}
column "$group_0" {
- type = "String"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^\"|\"$', '')"
- comment = "column_materializer::$group_0"
+ type = "String"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^\"|\"$', '')"
+ comment = "column_materializer::$group_0"
}
column "$group_1" {
- type = "String"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^\"|\"$', '')"
- comment = "column_materializer::$group_1"
+ type = "String"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^\"|\"$', '')"
+ comment = "column_materializer::$group_1"
}
column "$group_2" {
- type = "String"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^\"|\"$', '')"
- comment = "column_materializer::$group_2"
+ type = "String"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^\"|\"$', '')"
+ comment = "column_materializer::$group_2"
}
column "$group_3" {
- type = "String"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^\"|\"$', '')"
- comment = "column_materializer::$group_3"
+ type = "String"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^\"|\"$', '')"
+ comment = "column_materializer::$group_3"
}
column "$group_4" {
- type = "String"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^\"|\"$', '')"
- comment = "column_materializer::$group_4"
+ type = "String"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^\"|\"$', '')"
+ comment = "column_materializer::$group_4"
}
column "flag_key" {
- type = "String"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^\"|\"$', '')"
- comment = "column_materializer::properties::$feature_flag"
+ type = "String"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^\"|\"$', '')"
+ comment = "column_materializer::properties::$feature_flag"
}
column "response" {
- type = "LowCardinality(String)"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^\"|\"$', '')"
- comment = "column_materializer::properties::$feature_flag_response"
+ type = "LowCardinality(String)"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^\"|\"$', '')"
+ comment = "column_materializer::properties::$feature_flag_response"
}
column "session_id" {
- type = "String"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^\"|\"$', '')"
- comment = "column_materializer::properties::$session_id"
+ type = "String"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^\"|\"$', '')"
+ comment = "column_materializer::properties::$session_id"
}
column "request_id" {
- type = "String"
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
- comment = "column_materializer::properties::$feature_flag_request_id"
+ type = "String"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
+ comment = "column_materializer::properties::$feature_flag_request_id"
}
column "_timestamp" {
type = "DateTime"
diff --git a/posthog/clickhouse/hcl/roles/data/local/tables.hcl b/posthog/clickhouse/hcl/roles/data/local/tables.hcl
index 72c4523bc04b..b942186ec3e4 100644
--- a/posthog/clickhouse/hcl/roles/data/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/data/local/tables.hcl
@@ -1,8 +1,9 @@
database "posthog" {
# Mirrors the events column set minus events' materialized property columns, with
- # the full properties JSON kept as the source of truth. The materialized columns
- # below carry their expression only on sharded_flag_evaluations; the Distributed
- # proxy repeats them plain, because a Distributed engine computes nothing.
+ # the full properties JSON kept as the source of truth. The typed property columns
+ # below carry their DEFAULT expression only on sharded_flag_evaluations; the
+ # Distributed proxy repeats them plain, because a Distributed engine computes
+ # nothing.
table "_flag_evaluations_columns" {
abstract = true
column "uuid" {
@@ -3474,31 +3475,31 @@ database "posthog" {
}
extend = "_flag_evaluations_columns"
patch_column "$group_0" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^\"|\"$', '')"
}
patch_column "$group_1" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^\"|\"$', '')"
}
patch_column "$group_2" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^\"|\"$', '')"
}
patch_column "$group_3" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^\"|\"$', '')"
}
patch_column "$group_4" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^\"|\"$', '')"
}
patch_column "flag_key" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^\"|\"$', '')"
}
patch_column "response" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^\"|\"$', '')"
}
patch_column "session_id" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^\"|\"$', '')"
}
patch_column "request_id" {
- materialized = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
+ default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
}
index "distinct_id_idx" {
expr = "distinct_id"
diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sql
index c8ddce4966aa..5d121f46f4fa 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql
@@ -1187,15 +1187,15 @@ CREATE TABLE posthog.sharded_flag_evaluations (
group3_properties String,
group4_properties String,
inserted_at DateTime64(6, 'UTC') DEFAULT timestamp,
- $group_0 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^"|"$', '') COMMENT 'column_materializer::$group_0',
- $group_1 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^"|"$', '') COMMENT 'column_materializer::$group_1',
- $group_2 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^"|"$', '') COMMENT 'column_materializer::$group_2',
- $group_3 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^"|"$', '') COMMENT 'column_materializer::$group_3',
- $group_4 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^"|"$', '') COMMENT 'column_materializer::$group_4',
- flag_key String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag',
- response LowCardinality(String) MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response',
- session_id String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id',
- request_id String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ $group_0 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^"|"$', '') COMMENT 'column_materializer::$group_0',
+ $group_1 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^"|"$', '') COMMENT 'column_materializer::$group_1',
+ $group_2 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^"|"$', '') COMMENT 'column_materializer::$group_2',
+ $group_3 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^"|"$', '') COMMENT 'column_materializer::$group_3',
+ $group_4 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^"|"$', '') COMMENT 'column_materializer::$group_4',
+ flag_key String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag',
+ response LowCardinality(String) DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response',
+ session_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id',
+ request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id',
_timestamp DateTime,
_offset UInt64,
_partition UInt64,
diff --git a/posthog/clickhouse/migrations/0301_flag_evaluations_default_columns.py b/posthog/clickhouse/migrations/0301_flag_evaluations_default_columns.py
new file mode 100644
index 000000000000..54df9c6b19ab
--- /dev/null
+++ b/posthog/clickhouse/migrations/0301_flag_evaluations_default_columns.py
@@ -0,0 +1,52 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.models.flag_evaluations.sql import (
+ DISTRIBUTED_FLAG_EVALUATIONS_TABLE_SQL,
+ DROP_FLAG_EVALUATIONS_TABLE_SQL,
+ FLAG_EVALUATIONS_TABLE,
+ FLAG_EVALUATIONS_TABLE_SQL,
+ FLAG_EVALUATIONS_WRITABLE_TABLE,
+ WRITABLE_FLAG_EVALUATIONS_TABLE_SQL,
+)
+
+# Recreates flag_evaluations with its nine typed property columns as DEFAULT
+# instead of MATERIALIZED, the assignable kind the property-removal rewrite
+# resets by ALTER UPDATE. This clears the schema obstacle only: the rewrite
+# machinery still sweeps just the events tables, so property removal does not
+# reach this table yet (docs/internal/clickhouse-deletion-coverage.md).
+# posthog/models/flag_evaluations/sql.py carries the full rationale.
+#
+# Drop-and-recreate rather than ALTER, as in 0297: nothing produces to the topic
+# yet, so the family is empty everywhere and this must land before the producer
+# does. Unlike 0297 the Kafka table and the MV stay untouched, because their DDL
+# does not change; only the sharded table's column kind does. The two Distributed
+# fronts render identical DDL too, but recreating them is free while the family
+# is empty and reconciles any environment whose fronts drifted from the repo
+# rendering. The sharded table is replicated, so its drop carries SYNC to clear
+# ZooKeeper metadata before the recreate; the Distributed fronts drop plain.
+operations = [
+ run_sql_with_exceptions(
+ f"DROP TABLE IF EXISTS {FLAG_EVALUATIONS_TABLE}",
+ node_roles=[NodeRole.DATA],
+ ),
+ run_sql_with_exceptions(
+ f"DROP TABLE IF EXISTS {FLAG_EVALUATIONS_WRITABLE_TABLE}",
+ node_roles=[NodeRole.INGESTION_MEDIUM],
+ ),
+ run_sql_with_exceptions(
+ DROP_FLAG_EVALUATIONS_TABLE_SQL(),
+ node_roles=[NodeRole.DATA],
+ ),
+ run_sql_with_exceptions(
+ FLAG_EVALUATIONS_TABLE_SQL(),
+ node_roles=[NodeRole.DATA],
+ ),
+ run_sql_with_exceptions(
+ WRITABLE_FLAG_EVALUATIONS_TABLE_SQL(),
+ node_roles=[NodeRole.INGESTION_MEDIUM],
+ ),
+ run_sql_with_exceptions(
+ DISTRIBUTED_FLAG_EVALUATIONS_TABLE_SQL(),
+ node_roles=[NodeRole.DATA],
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 002f6031acc3..3549876e5af0 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0300_logs_volume_buckets_summing
+0301_flag_evaluations_default_columns
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index ab56813dc85a..9526421abde3 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -6927,15 +6927,15 @@
group4_properties String,
inserted_at DateTime64(6, 'UTC') DEFAULT timestamp
- , $group_0 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^"|"$', '') COMMENT 'column_materializer::$group_0'
- , $group_1 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^"|"$', '') COMMENT 'column_materializer::$group_1'
- , $group_2 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^"|"$', '') COMMENT 'column_materializer::$group_2'
- , $group_3 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^"|"$', '') COMMENT 'column_materializer::$group_3'
- , $group_4 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^"|"$', '') COMMENT 'column_materializer::$group_4'
- , flag_key String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag'
- , response LowCardinality(String) MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response'
- , session_id String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id'
- , request_id String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id'
+ , $group_0 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^"|"$', '') COMMENT 'column_materializer::$group_0'
+ , $group_1 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^"|"$', '') COMMENT 'column_materializer::$group_1'
+ , $group_2 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^"|"$', '') COMMENT 'column_materializer::$group_2'
+ , $group_3 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^"|"$', '') COMMENT 'column_materializer::$group_3'
+ , $group_4 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^"|"$', '') COMMENT 'column_materializer::$group_4'
+ , flag_key String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag'
+ , response LowCardinality(String) DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response'
+ , session_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id'
+ , request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id'
, INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1
@@ -11902,15 +11902,15 @@
group4_properties String,
inserted_at DateTime64(6, 'UTC') DEFAULT timestamp
- , $group_0 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^"|"$', '') COMMENT 'column_materializer::$group_0'
- , $group_1 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^"|"$', '') COMMENT 'column_materializer::$group_1'
- , $group_2 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^"|"$', '') COMMENT 'column_materializer::$group_2'
- , $group_3 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^"|"$', '') COMMENT 'column_materializer::$group_3'
- , $group_4 String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^"|"$', '') COMMENT 'column_materializer::$group_4'
- , flag_key String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag'
- , response LowCardinality(String) MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response'
- , session_id String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id'
- , request_id String MATERIALIZED replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id'
+ , $group_0 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_0'), '^"|"$', '') COMMENT 'column_materializer::$group_0'
+ , $group_1 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_1'), '^"|"$', '') COMMENT 'column_materializer::$group_1'
+ , $group_2 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_2'), '^"|"$', '') COMMENT 'column_materializer::$group_2'
+ , $group_3 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_3'), '^"|"$', '') COMMENT 'column_materializer::$group_3'
+ , $group_4 String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$group_4'), '^"|"$', '') COMMENT 'column_materializer::$group_4'
+ , flag_key String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag'
+ , response LowCardinality(String) DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response'
+ , session_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id'
+ , request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id'
, INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1
diff --git a/posthog/clickhouse/test/test_schema.py b/posthog/clickhouse/test/test_schema.py
index 17be8e15860c..67ad72906160 100644
--- a/posthog/clickhouse/test/test_schema.py
+++ b/posthog/clickhouse/test/test_schema.py
@@ -111,11 +111,12 @@ def test_flag_evaluations_mv_projection_matches_column_template():
def test_flag_evaluations_read_table_declares_every_stored_column():
- # The materialized columns are declared on sharded_flag_evaluations, which
- # computes them, and repeated as plain columns on the Distributed read table,
- # which computes nothing. Those two lists are maintained by hand, so a column
- # or a type changed in one and not the other stays invisible until a query
- # asks flag_evaluations for something only the shards have.
+ # The typed property columns carry their DEFAULT expression on
+ # sharded_flag_evaluations, which computes them, and are repeated as plain
+ # columns on the Distributed read table, which computes nothing. Those two
+ # lists are maintained by hand, so a column or a type changed in one and not
+ # the other stays invisible until a query asks flag_evaluations for
+ # something only the shards have.
stored_columns = _flag_evaluations_table_columns(FLAG_EVALUATIONS_TABLE_SQL())
assert stored_columns
diff --git a/posthog/dags/data_deletion_requests.py b/posthog/dags/data_deletion_requests.py
index 1298545c779d..90abbb327419 100644
--- a/posthog/dags/data_deletion_requests.py
+++ b/posthog/dags/data_deletion_requests.py
@@ -596,9 +596,10 @@ def execute_event_deletion(
_PROPERTY_REWRITE_UNSWEEPABLE_REASON = (
- "its typed columns are MATERIALIZED from properties, so ClickHouse rejects both an assignment "
- "to them and an update of properties itself; rewriting these rows needs a re-insert instead of "
- "the staging-table mutation this job runs. There is no way to complete this request today: "
+ "the property-rewrite machinery is scoped to the events tables and does not reach it; a "
+ "request naming $feature_flag additionally cannot "
+ "be honored by mutation at all, because flag_key sits in the table's sort key where no UPDATE "
+ "can reset it. There is no way to complete this request today: "
"either narrow its events to ones this table never stores, or wait out the table's TTL. "
f"See {_COVERAGE_DOC}."
)
diff --git a/posthog/dags/tests/test_data_deletion_requests.py b/posthog/dags/tests/test_data_deletion_requests.py
index 870ee91f63e0..5317ed7ecb66 100644
--- a/posthog/dags/tests/test_data_deletion_requests.py
+++ b/posthog/dags/tests/test_data_deletion_requests.py
@@ -2054,9 +2054,10 @@ def test_deferred_event_removal_queues_flag_evaluations_and_blocks_promotion(clu
@pytest.mark.django_db
def test_get_property_removal_shards_refuses_when_flag_evaluations_holds_matching_rows(cluster: ClickhouseCluster):
- # Property removal cannot rewrite flag_evaluations: its typed columns are MATERIALIZED, so
- # ClickHouse rejects both an assignment to them and an update of properties itself. Completing
- # the request anyway would report the property erased while a copy of it survived.
+ # Property removal cannot rewrite flag_evaluations: the rewrite machinery is scoped to the
+ # events tables, and flag_key sits in the sort key where no mutation can reset it, so even
+ # that machinery could not fully honor a request naming $feature_flag. Completing the
+ # request anyway would report the property erased while a copy of it survived.
request = DataDeletionRequest.objects.create(
team_id=PROP_TEAM_ID,
request_type=RequestType.PROPERTY_REMOVAL,
diff --git a/posthog/models/deletion_targets.py b/posthog/models/deletion_targets.py
index 3f3713d1f2a5..b078ee4dd06a 100644
--- a/posthog/models/deletion_targets.py
+++ b/posthog/models/deletion_targets.py
@@ -66,8 +66,11 @@ class DeletionTarget:
# exists. A compiled fragment names physical columns (mat_*, the property-group maps, or JSON
# subcolumns), so it only runs against the schema it was compiled for.
hogql_schema: HogQLSchema | None = None
- # properties/person_properties can be rewritten in place. True only where the property columns
- # are DEFAULT-kind, as materialize() mints them, and so can be reset by ALTER UPDATE.
+ # properties/person_properties can be rewritten in place. True needs both halves: the property
+ # columns are DEFAULT-kind (assignable by ALTER UPDATE, as materialize() mints them), and the
+ # property-rewrite machinery in posthog/dags/data_deletion_requests.py actually sweeps the
+ # table, which today is hardcoded to the events tables. flag_evaluations satisfies only the
+ # schema half, so flipping this without extending the sweep silently under-deletes.
accepts_property_rewrite: bool = False
# Read uuids from this table when queueing a deferred deletion. False where the rows duplicate
# another target's uuids, which would queue each one twice.
diff --git a/posthog/models/flag_evaluations/sql.py b/posthog/models/flag_evaluations/sql.py
index 2ec2ccb3582e..339f75497740 100644
--- a/posthog/models/flag_evaluations/sql.py
+++ b/posthog/models/flag_evaluations/sql.py
@@ -48,8 +48,9 @@ FLAG_EVALUATIONS_SHARDING_KEY = "sipHash64(distinct_id)"
# The sort key matches the queries we run: per-flag usage over a date range, and
# uniques within one flag. toDate(timestamp) sits inside it because PARTITION BY
# is monthly — without it, a one-day query for one flag would read that flag's
-# whole month. flag_key is a materialized column; ClickHouse computes those before
-# it sorts a part, so one can carry a sort key. The trailing hash intentionally
+# whole month. flag_key is a DEFAULT column; ClickHouse fills column defaults at
+# insert, before it sorts a part, so one can carry a sort key, though a key
+# column can never be ALTER UPDATEd, whatever its kind. The trailing hash intentionally
# differs from the sharding key — cityHash64 is the events table's convention for
# within-shard ordering — and a MergeTree ORDER BY is immutable once data exists,
# so the two must not silently move together.
@@ -111,24 +112,36 @@ _FLAG_EVALUATIONS_COLUMNS = _FLAG_EVALUATIONS_COLUMNS_TEMPLATE.format(ts_default
#
# $group_0..$group_4 carry the events table's names, types and comment form so
# group filtering resolves the same columns on both.
-_FLAG_EVALUATIONS_MATERIALIZED_COLUMNS = f"""
- , $group_0 String MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$group_0')")} COMMENT 'column_materializer::$group_0'
- , $group_1 String MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$group_1')")} COMMENT 'column_materializer::$group_1'
- , $group_2 String MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$group_2')")} COMMENT 'column_materializer::$group_2'
- , $group_3 String MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$group_3')")} COMMENT 'column_materializer::$group_3'
- , $group_4 String MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$group_4')")} COMMENT 'column_materializer::$group_4'
- , flag_key String MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$feature_flag')")} COMMENT 'column_materializer::properties::$feature_flag'
- , response LowCardinality(String) MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$feature_flag_response')")} COMMENT 'column_materializer::properties::$feature_flag_response'
- , session_id String MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$session_id')")} COMMENT 'column_materializer::properties::$session_id'
- , request_id String MATERIALIZED {trim_quotes_expr("JSONExtractRaw(properties, '$feature_flag_request_id')")} COMMENT 'column_materializer::properties::$feature_flag_request_id'
+#
+# DEFAULT rather than MATERIALIZED, the kind materialize() mints on sharded_events:
+# both compute the expression when an insert omits the column, but only a DEFAULT
+# column accepts ALTER UPDATE, which the events property-removal path relies on to
+# reset extracted values whose source property was erased (see
+# docs/internal/clickhouse-deletion-coverage.md). An UPDATE of properties does not
+# recompute these columns, so a rewrite must reset each affected column in the
+# same mutation. The cost is a footgun MATERIALIZED did not have: an insert that
+# names one of these columns stores the given value even when it contradicts
+# properties. Producers must omit them, which the Kafka path enforces by
+# writable_flag_evaluations not declaring them.
+_FLAG_EVALUATIONS_TYPED_COLUMNS = f"""
+ , $group_0 String DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$group_0')")} COMMENT 'column_materializer::$group_0'
+ , $group_1 String DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$group_1')")} COMMENT 'column_materializer::$group_1'
+ , $group_2 String DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$group_2')")} COMMENT 'column_materializer::$group_2'
+ , $group_3 String DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$group_3')")} COMMENT 'column_materializer::$group_3'
+ , $group_4 String DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$group_4')")} COMMENT 'column_materializer::$group_4'
+ , flag_key String DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$feature_flag')")} COMMENT 'column_materializer::properties::$feature_flag'
+ , response LowCardinality(String) DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$feature_flag_response')")} COMMENT 'column_materializer::properties::$feature_flag_response'
+ , session_id String DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$session_id')")} COMMENT 'column_materializer::properties::$session_id'
+ , request_id String DEFAULT {trim_quotes_expr("JSONExtractRaw(properties, '$feature_flag_request_id')")} COMMENT 'column_materializer::properties::$feature_flag_request_id'
"""
# A Distributed engine computes nothing, so the read table repeats the same names
# and types without the expression, which is what lets a query against
# flag_evaluations select the columns the shards store. The writable table omits
-# them entirely, because rows arrive there without these columns and the shard
-# fills them in.
-_FLAG_EVALUATIONS_PROXY_MATERIALIZED_COLUMNS = """
+# them entirely: carrying the DEFAULT expressions there would compute the values
+# on the ingestion nodes and ship the widened rows over the network, so rows
+# arrive narrow and the shard computes them, matching writable_events.
+_FLAG_EVALUATIONS_PROXY_TYPED_COLUMNS = """
, $group_0 String COMMENT 'column_materializer::$group_0'
, $group_1 String COMMENT 'column_materializer::$group_1'
, $group_2 String COMMENT 'column_materializer::$group_2'
@@ -176,7 +189,7 @@ FLAG_EVALUATIONS_TABLE_SQL = lambda: (
CREATE TABLE IF NOT EXISTS {FLAG_EVALUATIONS_DATA_TABLE}
(
{_FLAG_EVALUATIONS_COLUMNS}
- {_FLAG_EVALUATIONS_MATERIALIZED_COLUMNS}
+ {_FLAG_EVALUATIONS_TYPED_COLUMNS}
{_FLAG_EVALUATIONS_INDEXES}
{KAFKA_COLUMNS_WITH_PARTITION}
)
@@ -213,12 +226,12 @@ def DROP_FLAG_EVALUATIONS_TABLE_SQL() -> str:
return f"DROP TABLE IF EXISTS {FLAG_EVALUATIONS_DATA_TABLE} SYNC"
-def _distributed_table_sql(table_name: str, *, materialized_columns: str = "") -> str:
+def _distributed_table_sql(table_name: str, *, typed_columns: str = "") -> str:
return f"""
CREATE TABLE IF NOT EXISTS {table_name}
(
{_FLAG_EVALUATIONS_COLUMNS}
- {materialized_columns}
+ {typed_columns}
{KAFKA_COLUMNS_WITH_PARTITION}
)
ENGINE = {Distributed(data_table=FLAG_EVALUATIONS_DATA_TABLE, sharding_key=FLAG_EVALUATIONS_SHARDING_KEY)}
@@ -230,7 +243,7 @@ WRITABLE_FLAG_EVALUATIONS_TABLE_SQL = lambda: _distributed_table_sql(FLAG_EVALUA
# Read path on DATA nodes, and the name queries use.
DISTRIBUTED_FLAG_EVALUATIONS_TABLE_SQL = lambda: _distributed_table_sql(
- FLAG_EVALUATIONS_TABLE, materialized_columns=_FLAG_EVALUATIONS_PROXY_MATERIALIZED_COLUMNS
+ FLAG_EVALUATIONS_TABLE, typed_columns=_FLAG_EVALUATIONS_PROXY_TYPED_COLUMNS
)