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(propvals): part 1: add property_count col to kafka_property_values (#59141)

Andy Zhao51510ae630ee
diff --git a/posthog/clickhouse/migrations/0257_property_values_count_column.py b/posthog/clickhouse/migrations/0257_property_values_count_column.py
new file mode 100644
index 000000000000..e729f537316c
--- /dev/null
+++ b/posthog/clickhouse/migrations/0257_property_values_count_column.py
@@ -0,0 +1,42 @@
+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,
+)
+
+# Kafka engine tables don't support ALTER ADD COLUMN, so we drop and recreate
+# the MV + Kafka engine table together.
+
+if settings.CLOUD_DEPLOYMENT in ("US", "EU"):
+ _ROLES = [NodeRole.AUX]
+elif settings.CLOUD_DEPLOYMENT == "DEV":
+ _ROLES = [NodeRole.DATA]
+else:
+ _ROLES = []
+
+operations = (
+ [
+ run_sql_with_exceptions(
+ DROP_PROPERTY_VALUES_MV_SQL(),
+ node_roles=_ROLES,
+ ),
+ run_sql_with_exceptions(
+ DROP_KAFKA_PROPERTY_VALUES_TABLE_SQL(),
+ node_roles=_ROLES,
+ ),
+ run_sql_with_exceptions(
+ KAFKA_PROPERTY_VALUES_TABLE_SQL_FN(),
+ node_roles=_ROLES,
+ ),
+ run_sql_with_exceptions(
+ PROPERTY_VALUES_MV_SQL(),
+ node_roles=_ROLES,
+ ),
+ ]
+ if _ROLES
+ else []
+)
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index fa7fdec0ef91..1474d68af325 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0256_web_overview_preaggregated
+0257_property_values_count_column
diff --git a/posthog/clickhouse/property_values.py b/posthog/clickhouse/property_values.py
index 7dda6c9156b2..8b7051863c97 100644
--- a/posthog/clickhouse/property_values.py
+++ b/posthog/clickhouse/property_values.py
@@ -9,15 +9,17 @@ KAFKA_TABLE_NAME = f"kafka_{TABLE_NAME}"
MV_NAME = f"{TABLE_NAME}_mv"
DISTRIBUTED_TABLE_NAME = f"{TABLE_NAME}_distributed"
-# The Kafka message schema: pre-processed rows from the WarpStream pipeline.
-# Each message is one (team_id, property_type, property_key, property_value) tuple.
+# The Kafka message schema: pre-processed rows from the property-values aggregator.
+# Each message is one (team_id, property_type, property_key, property_value)
+# tuple plus an accumulated `property_count` from the aggregator's flush window.
KAFKA_PROPERTY_VALUES_TABLE_SQL = """
CREATE TABLE IF NOT EXISTS {table_name}
(
`team_id` Int64,
`property_type` LowCardinality(String),
`property_key` String,
- `property_value` String
+ `property_value` String,
+ `property_count` UInt64
) ENGINE = {engine}
"""
@@ -81,7 +83,7 @@ AS SELECT
property_type,
property_key,
property_value,
- toUInt64(1) as property_count,
+ property_count,
coalesce(_timestamp, now()) as last_seen
FROM {database}.{kafka_table}
WHERE lengthUTF8(property_key) > 0
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index a43700192b3a..62c0a1e4286b 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -574,7 +574,8 @@
`team_id` Int64,
`property_type` LowCardinality(String),
`property_key` String,
- `property_value` String
+ `property_value` String,
+ `property_count` UInt64
) ENGINE = Kafka(warpstream_ingestion, kafka_topic_list = 'clickhouse_property_values_test', kafka_group_name = 'clickhouse_property_values', kafka_format = 'JSONEachRow')
'''
@@ -2400,7 +2401,8 @@
`team_id` Int64,
`property_type` LowCardinality(String),
`property_key` String,
- `property_value` String
+ `property_value` String,
+ `property_count` UInt64
) ENGINE = Kafka(warpstream_ingestion, kafka_topic_list = 'clickhouse_property_values_test', kafka_group_name = 'clickhouse_property_values', kafka_format = 'JSONEachRow')
'''
@@ -3712,7 +3714,7 @@
property_type,
property_key,
property_value,
- toUInt64(1) as property_count,
+ property_count,
coalesce(_timestamp, now()) as last_seen
FROM posthog_test.kafka_property_values
WHERE lengthUTF8(property_key) > 0