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: correct kafka named collection and add dev tables (#57704)

Andy Zhao5c17c330b3c2
diff --git a/posthog/clickhouse/migrations/0249_property_values_kafka_named_collection.py b/posthog/clickhouse/migrations/0249_property_values_kafka_named_collection.py
new file mode 100644
index 000000000000..2a0a6b3d53b9
--- /dev/null
+++ b/posthog/clickhouse/migrations/0249_property_values_kafka_named_collection.py
@@ -0,0 +1,35 @@
+from posthog import settings
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.property_values import (
+ DROP_KAFKA_PROPERTY_VALUES_TABLE_SQL,
+ DROP_PROPERTY_VALUES_MV_SQL,
+ KAFKA_PROPERTY_VALUES_TABLE_SQL_FN,
+ PROPERTY_VALUES_MV_SQL,
+ PROPERTY_VALUES_TABLE_SQL,
+)
+
+# Two fixes to 0244:
+# - US/EU: kafka_property_values used the default msk_cluster named collection,
+# but the topic is produced to warpstream_ingestion. Drop the MV first, then
+# the Kafka table, then recreate both with the right named collection.
+# Storage table is untouched.
+# - DEV: 0244's AUX ops no-op'd because dev has no hostClusterRole=aux nodes.
+# Create storage, Kafka, and MV on DATA. Dev's `aux` cluster aliases to those
+# same hosts so the existing Distributed proxy resolves correctly.
+
+if settings.CLOUD_DEPLOYMENT in ("US", "EU"):
+ operations = [
+ run_sql_with_exceptions(DROP_PROPERTY_VALUES_MV_SQL(), node_roles=[NodeRole.AUX]),
+ run_sql_with_exceptions(DROP_KAFKA_PROPERTY_VALUES_TABLE_SQL(), node_roles=[NodeRole.AUX]),
+ run_sql_with_exceptions(KAFKA_PROPERTY_VALUES_TABLE_SQL_FN(), node_roles=[NodeRole.AUX]),
+ run_sql_with_exceptions(PROPERTY_VALUES_MV_SQL(), node_roles=[NodeRole.AUX]),
+ ]
+elif settings.CLOUD_DEPLOYMENT == "DEV":
+ operations = [
+ run_sql_with_exceptions(PROPERTY_VALUES_TABLE_SQL(), node_roles=[NodeRole.DATA]),
+ run_sql_with_exceptions(KAFKA_PROPERTY_VALUES_TABLE_SQL_FN(), node_roles=[NodeRole.DATA]),
+ run_sql_with_exceptions(PROPERTY_VALUES_MV_SQL(), node_roles=[NodeRole.DATA]),
+ ]
+else:
+ operations = []
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index deb07ec74345..dc3a5646d3ce 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0248_drop_msk_kafka_engines_with_ws_equivalents
+0249_property_values_kafka_named_collection
diff --git a/posthog/clickhouse/property_values.py b/posthog/clickhouse/property_values.py
index a4cf2563a64a..ae7ad50d6305 100644
--- a/posthog/clickhouse/property_values.py
+++ b/posthog/clickhouse/property_values.py
@@ -17,8 +17,7 @@ CREATE TABLE IF NOT EXISTS {table_name}
`team_id` Int64,
`property_type` LowCardinality(String),
`property_key` String,
- `property_value` String,
- `_timestamp` DateTime
+ `property_value` String
) ENGINE = {engine}
"""
@@ -57,10 +56,22 @@ SETTINGS
def KAFKA_PROPERTY_VALUES_TABLE_SQL_FN() -> str:
return KAFKA_PROPERTY_VALUES_TABLE_SQL.format(
table_name=KAFKA_TABLE_NAME,
- engine=kafka_engine(topic=KAFKA_CLICKHOUSE_PROPERTY_VALUES, group=CONSUMER_GROUP_PROPERTY_VALUES),
+ engine=kafka_engine(
+ topic=KAFKA_CLICKHOUSE_PROPERTY_VALUES,
+ group=CONSUMER_GROUP_PROPERTY_VALUES,
+ named_collection=settings.CLICKHOUSE_KAFKA_WARPSTREAM_INGESTION_NAMED_COLLECTION,
+ ),
)
+def DROP_KAFKA_PROPERTY_VALUES_TABLE_SQL() -> str:
+ return f"DROP TABLE IF EXISTS {KAFKA_TABLE_NAME}"
+
+
+def DROP_PROPERTY_VALUES_MV_SQL() -> str:
+ return f"DROP TABLE IF EXISTS {MV_NAME}"
+
+
def PROPERTY_VALUES_MV_SQL() -> str:
return """
CREATE MATERIALIZED VIEW IF NOT EXISTS {mv_name}
@@ -71,7 +82,7 @@ AS SELECT
property_key,
property_value,
toUInt64(1) as property_count,
- _timestamp as last_seen
+ coalesce(_timestamp, now()) as last_seen
FROM {database}.{kafka_table}
WHERE lengthUTF8(property_key) > 0
AND lengthUTF8(property_key) <= 400 -- matches Django PropertyDefinition.name max_length
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 2e7105304be8..09f0f74de83c 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -602,9 +602,8 @@
`team_id` Int64,
`property_type` LowCardinality(String),
`property_key` String,
- `property_value` String,
- `_timestamp` DateTime
- ) ENGINE = Kafka(msk_cluster, kafka_topic_list = 'clickhouse_property_values_test', kafka_group_name = 'clickhouse_property_values', kafka_format = 'JSONEachRow')
+ `property_value` String
+ ) ENGINE = Kafka(warpstream_ingestion, kafka_topic_list = 'clickhouse_property_values_test', kafka_group_name = 'clickhouse_property_values', kafka_format = 'JSONEachRow')
'''
# ---
@@ -2330,9 +2329,8 @@
`team_id` Int64,
`property_type` LowCardinality(String),
`property_key` String,
- `property_value` String,
- `_timestamp` DateTime
- ) ENGINE = Kafka(msk_cluster, kafka_topic_list = 'clickhouse_property_values_test', kafka_group_name = 'clickhouse_property_values', kafka_format = 'JSONEachRow')
+ `property_value` String
+ ) ENGINE = Kafka(warpstream_ingestion, kafka_topic_list = 'clickhouse_property_values_test', kafka_group_name = 'clickhouse_property_values', kafka_format = 'JSONEachRow')
'''
# ---
@@ -3421,7 +3419,7 @@
property_key,
property_value,
toUInt64(1) as property_count,
- _timestamp as last_seen
+ coalesce(_timestamp, now()) as last_seen
FROM posthog_test.kafka_property_values
WHERE lengthUTF8(property_key) > 0
AND lengthUTF8(property_key) <= 400 -- matches Django PropertyDefinition.name max_length