: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(workflows): add message_assets clickhouse tables (#66665)
Meikel Ratze35903e78031
diff --git a/posthog/clickhouse/kafka_engine.py b/posthog/clickhouse/kafka_engine.pyindex a813f8038051..1df66b6fd207 100644--- a/posthog/clickhouse/kafka_engine.py+++ b/posthog/clickhouse/kafka_engine.py@@ -17,6 +17,7 @@ CONSUMER_GROUP_SESSION_REPLAY_EVENTS = "clickhouse_session_replay_events" if _USCONSUMER_GROUP_SESSION_REPLAY_FEATURES = "clickhouse_session_replay_features" if _US else "group1"CONSUMER_GROUP_LOG_ENTRIES = "clickhouse_log_entries_v3" if _US else "clickhouse_log_entries"CONSUMER_GROUP_HOG_INVOCATION_RESULTS = "clickhouse_hog_invocation_results"+CONSUMER_GROUP_MESSAGE_ASSETS = "clickhouse_message_assets"CONSUMER_GROUP_DOCUMENT_EMBEDDINGS = "clickhouse_document_embeddings2" if _US else "clickhouse_document_embeddings"CONSUMER_GROUP_HEATMAPS = "clickhouse_heatmaps" if _US else "group1"CONSUMER_GROUP_PRECALCULATED_EVENTS = "clickhouse_precalculated_events2" if _US else "clickhouse_prefiltered_events"diff --git a/posthog/clickhouse/migrations/0284_message_assets.py b/posthog/clickhouse/migrations/0284_message_assets.pynew file mode 100644index 000000000000..72eea9883113--- /dev/null+++ b/posthog/clickhouse/migrations/0284_message_assets.py@@ -0,0 +1,27 @@+from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.models.message_assets.sql import (+ DISTRIBUTED_MESSAGE_ASSETS_TABLE_SQL,+ KAFKA_MESSAGE_ASSETS_TABLE_SQL,+ MESSAGE_ASSETS_DATA_TABLE_SQL,+ MESSAGE_ASSETS_MV_SQL,+)++operations = [+ run_sql_with_exceptions(+ MESSAGE_ASSETS_DATA_TABLE_SQL(),+ node_roles=[NodeRole.AUX],+ ),+ run_sql_with_exceptions(+ KAFKA_MESSAGE_ASSETS_TABLE_SQL(),+ node_roles=[NodeRole.AUX],+ ),+ run_sql_with_exceptions(+ MESSAGE_ASSETS_MV_SQL(),+ node_roles=[NodeRole.AUX],+ ),+ run_sql_with_exceptions(+ DISTRIBUTED_MESSAGE_ASSETS_TABLE_SQL(),+ node_roles=[NodeRole.AUX, NodeRole.DATA],+ ),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex 02a067a552cb..a756096508b9 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0283_metric_events+0284_message_assetsdiff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.pyindex 948e4d08e551..bfadc00d7bb8 100644--- a/posthog/clickhouse/schema.py+++ b/posthog/clickhouse/schema.py@@ -199,6 +199,12 @@ from posthog.models.ingestion_warnings.sql import (KAFKA_INGESTION_WARNINGS_TABLE_SQL,WRITABLE_INGESTION_WARNINGS_TABLE_SQL,)+from posthog.models.message_assets.sql import (+ DISTRIBUTED_MESSAGE_ASSETS_TABLE_SQL,+ KAFKA_MESSAGE_ASSETS_TABLE_SQL,+ MESSAGE_ASSETS_DATA_TABLE_SQL,+ MESSAGE_ASSETS_MV_SQL,+)from posthog.models.performance.sql import (DISTRIBUTED_PERFORMANCE_EVENTS_TABLE_SQL,KAFKA_PERFORMANCE_EVENTS_TABLE_SQL,@@ -331,6 +337,7 @@ CREATE_MERGETREE_TABLE_QUERIES = (APP_METRICS_DATA_TABLE_SQL,APP_METRICS2_DATA_TABLE_SQL,HOG_INVOCATION_RESULTS_DATA_TABLE_SQL,+ MESSAGE_ASSETS_DATA_TABLE_SQL,PERFORMANCE_EVENTS_TABLE_SQL,SESSION_REPLAY_EVENTS_TABLE_SQL,WRITABLE_SESSION_REPLAY_EVENTS_TABLE_SQL,@@ -390,6 +397,7 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (DISTRIBUTED_APP_METRICS_TABLE_SQL,DISTRIBUTED_APP_METRICS2_TABLE_SQL,DISTRIBUTED_HOG_INVOCATION_RESULTS_TABLE_SQL,+ DISTRIBUTED_MESSAGE_ASSETS_TABLE_SQL,WRITABLE_PERFORMANCE_EVENTS_TABLE_SQL,DISTRIBUTED_PERFORMANCE_EVENTS_TABLE_SQL,DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL,@@ -472,6 +480,7 @@ CREATE_KAFKA_TABLE_QUERIES = (KAFKA_APP_METRICS_TABLE_SQL,KAFKA_APP_METRICS2_TABLE_SQL,KAFKA_HOG_INVOCATION_RESULTS_TABLE_SQL,+ KAFKA_MESSAGE_ASSETS_TABLE_SQL,KAFKA_PERFORMANCE_EVENTS_TABLE_SQL,KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL,KAFKA_SESSION_REPLAY_FEATURES_TABLE_SQL,@@ -502,6 +511,7 @@ CREATE_MV_TABLE_QUERIES = (APP_METRICS_MV_TABLE_SQL,APP_METRICS2_MV_TABLE_SQL,HOG_INVOCATION_RESULTS_MV_SQL,+ MESSAGE_ASSETS_MV_SQL,PERFORMANCE_EVENTS_TABLE_MV_SQL,SESSION_REPLAY_EVENTS_TABLE_MV_SQL,SESSION_REPLAY_FEATURES_TABLE_MV_SQL,diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex f5d1c56a0351..42cd7e680b0c 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -358,6 +358,33 @@'''# ---+# name: test_create_kafka_table_with_different_kafka_host[kafka_message_assets]+ '''++ CREATE TABLE IF NOT EXISTS kafka_message_assets+ (+ team_id Int64,+ function_kind LowCardinality(String),+ function_id String,+ parent_run_id String,+ invocation_id String,+ action_id String,+ kind LowCardinality(String),+ distinct_id String,+ person_id String,+ recipient String,+ subject String,+ status LowCardinality(String),+ sent_at DateTime64(6, 'UTC'),+ version UInt64,+ is_deleted UInt8,+ html String+ )+ ENGINE = Kafka(warpstream_cyclotron, kafka_topic_list = 'clickhouse_message_assets_test', kafka_group_name = 'clickhouse_message_assets', kafka_format = 'JSONEachRow')+ SETTINGS kafka_skip_broken_messages = 100++ '''+# ---# name: test_create_kafka_table_with_different_kafka_host[kafka_performance_events]'''@@ -2218,6 +2245,33 @@'''# ---+# name: test_create_table_query[kafka_message_assets]+ '''++ CREATE TABLE IF NOT EXISTS kafka_message_assets+ (+ team_id Int64,+ function_kind LowCardinality(String),+ function_id String,+ parent_run_id String,+ invocation_id String,+ action_id String,+ kind LowCardinality(String),+ distinct_id String,+ person_id String,+ recipient String,+ subject String,+ status LowCardinality(String),+ sent_at DateTime64(6, 'UTC'),+ version UInt64,+ is_deleted UInt8,+ html String+ )+ ENGINE = Kafka(warpstream_cyclotron, kafka_topic_list = 'clickhouse_message_assets_test', kafka_group_name = 'clickhouse_message_assets', kafka_format = 'JSONEachRow')+ SETTINGS kafka_skip_broken_messages = 100++ '''+# ---# name: test_create_table_query[kafka_performance_events]'''@@ -3349,6 +3403,105 @@'''# ---+# name: test_create_table_query[message_assets]+ '''++ CREATE TABLE IF NOT EXISTS message_assets+ (+ team_id Int64,+ function_kind LowCardinality(String),+ function_id String,+ parent_run_id String,+ invocation_id String,+ action_id String,+ kind LowCardinality(String),+ distinct_id String,+ person_id String,+ recipient String,+ subject String,+ status LowCardinality(String),+ sent_at DateTime64(6, 'UTC'),+ version UInt64,+ is_deleted UInt8,+ html String++ , _timestamp DateTime+ , _offset UInt64+ , _partition UInt64++ )+ ENGINE = Distributed('aux', 'posthog_test', 'message_assets_data')++ '''+# ---+# name: test_create_table_query[message_assets_data]+ '''++ CREATE TABLE IF NOT EXISTS message_assets_data+ (+ team_id Int64,+ function_kind LowCardinality(String),+ function_id String,+ parent_run_id String,+ invocation_id String,+ action_id String,+ kind LowCardinality(String),+ distinct_id String,+ person_id String,+ recipient String,+ subject String,+ status LowCardinality(String),+ sent_at DateTime64(6, 'UTC'),+ version UInt64,+ is_deleted UInt8 DEFAULT 0,+ html String CODEC(ZSTD(3)),+ INDEX parent_run_idx parent_run_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX recipient_idx recipient TYPE bloom_filter(0.01) GRANULARITY 1++ , _timestamp DateTime+ , _offset UInt64+ , _partition UInt64++ )+ ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.message_assets_data', '{replica}-{shard}', version)+ PARTITION BY toYYYYMMDD(sent_at)+ ORDER BY (team_id, function_kind, function_id, invocation_id, action_id)++ SETTINGS index_granularity = 1024, ttl_only_drop_parts = 1++ '''+# ---+# name: test_create_table_query[message_assets_mv]+ '''++ CREATE MATERIALIZED VIEW IF NOT EXISTS message_assets_mv+ TO message_assets_data+ AS SELECT+ team_id,+ function_kind,+ function_id,+ parent_run_id,+ invocation_id,+ action_id,+ kind,+ distinct_id,+ person_id,+ recipient,+ subject,+ status,+ sent_at,+ version,+ is_deleted,+ html,+ _timestamp,+ _offset,+ _partition+ FROM kafka_message_assets++ '''+# ---# name: test_create_table_query[metric_attributes]'''@@ -9402,6 +9555,45 @@'''# ---+# name: test_create_table_query_replicated_and_storage[message_assets_data]+ '''++ CREATE TABLE IF NOT EXISTS message_assets_data+ (+ team_id Int64,+ function_kind LowCardinality(String),+ function_id String,+ parent_run_id String,+ invocation_id String,+ action_id String,+ kind LowCardinality(String),+ distinct_id String,+ person_id String,+ recipient String,+ subject String,+ status LowCardinality(String),+ sent_at DateTime64(6, 'UTC'),+ version UInt64,+ is_deleted UInt8 DEFAULT 0,+ html String CODEC(ZSTD(3)),+ INDEX parent_run_idx parent_run_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX recipient_idx recipient TYPE bloom_filter(0.01) GRANULARITY 1++ , _timestamp DateTime+ , _offset UInt64+ , _partition UInt64++ )+ ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.message_assets_data', '{replica}-{shard}', version)+ PARTITION BY toYYYYMMDD(sent_at)+ ORDER BY (team_id, function_kind, function_id, invocation_id, action_id)++ SETTINGS index_granularity = 1024, ttl_only_drop_parts = 1++ '''+# ---# name: test_create_table_query_replicated_and_storage[metric_attributes]'''diff --git a/posthog/kafka_client/topics.py b/posthog/kafka_client/topics.pyindex 01522088b1ce..60cfb69c7ee5 100644--- a/posthog/kafka_client/topics.py+++ b/posthog/kafka_client/topics.py@@ -34,6 +34,7 @@ KAFKA_PERSON_OVERRIDE = f"{KAFKA_PREFIX}clickhouse_person_override{SUFFIX}"KAFKA_LOG_ENTRIES = f"{KAFKA_PREFIX}log_entries{SUFFIX}"KAFKA_LOG_ENTRIES_V2_TEST = f"{KAFKA_PREFIX}log_entries_v2_test{SUFFIX}"KAFKA_HOG_INVOCATION_RESULTS = f"{KAFKA_PREFIX}clickhouse_hog_invocation_results{SUFFIX}"+KAFKA_MESSAGE_ASSETS = f"{KAFKA_PREFIX}clickhouse_message_assets{SUFFIX}"KAFKA_CLICKHOUSE_HEATMAP_EVENTS = f"{KAFKA_PREFIX}clickhouse_heatmap_events{SUFFIX}"KAFKA_CLICKHOUSE_AI_EVENTS_JSON = f"{KAFKA_PREFIX}clickhouse_ai_events_json{SUFFIX}"diff --git a/posthog/models/message_assets/sql.py b/posthog/models/message_assets/sql.pynew file mode 100644index 000000000000..b5468c49f00c--- /dev/null+++ b/posthog/models/message_assets/sql.py@@ -0,0 +1,210 @@+from django.conf import settings++from posthog.clickhouse.kafka_engine import (+ CONSUMER_GROUP_MESSAGE_ASSETS,+ KAFKA_COLUMNS_WITH_PARTITION,+ kafka_engine,+ ttl_period,+)+from posthog.clickhouse.table_engines import Distributed, ReplacingMergeTree, ReplicationScheme+from posthog.kafka_client.topics import KAFKA_MESSAGE_ASSETS++MESSAGE_ASSETS_TTL_DAYS = 30++# AUX-resident table family modelled on `hog_invocation_results`. One row per+# successfully sent email, keyed by (invocation_id, action_id) — a single+# workflow invocation can fan out to multiple email steps. Rendered HTML lives+# inline in the `html` column; columnar storage means listing queries don't read it.+MESSAGE_ASSETS_TABLE = "message_assets"+MESSAGE_ASSETS_DATA_TABLE = f"{MESSAGE_ASSETS_TABLE}_data"+KAFKA_MESSAGE_ASSETS_TABLE = f"kafka_{MESSAGE_ASSETS_TABLE}"+MESSAGE_ASSETS_MV_TABLE = f"{MESSAGE_ASSETS_TABLE}_mv"+++def DROP_MESSAGE_ASSETS_MV_SQL() -> str:+ return f"DROP TABLE IF EXISTS {MESSAGE_ASSETS_MV_TABLE}"+++def DROP_KAFKA_MESSAGE_ASSETS_TABLE_SQL() -> str:+ return f"DROP TABLE IF EXISTS {KAFKA_MESSAGE_ASSETS_TABLE}"+++def MESSAGE_ASSETS_ENGINE() -> ReplacingMergeTree:+ # `version` tie-breaks so a retried Kafka produce for the same asset+ # collapses to the latest row at merge time.+ return ReplacingMergeTree(+ MESSAGE_ASSETS_DATA_TABLE,+ ver="version",+ replication_scheme=ReplicationScheme.REPLICATED,+ )+++# `html` is last because it dominates row size; column-oriented reads mean+# listing queries never touch it.+MESSAGE_ASSETS_KAFKA_COLUMNS = """+ team_id Int64,+ function_kind LowCardinality(String),+ function_id String,+ parent_run_id String,+ invocation_id String,+ action_id String,+ kind LowCardinality(String),+ distinct_id String,+ person_id String,+ recipient String,+ subject String,+ status LowCardinality(String),+ sent_at DateTime64(6, 'UTC'),+ version UInt64,+ is_deleted UInt8,+ html String+""".strip()+++# Skip indexes match the listing query shape: filter by function_id ++# parent_run_id, search by recipient / distinct_id / person.+MESSAGE_ASSETS_DATA_TABLE_SQL = lambda: (+ f"""+CREATE TABLE IF NOT EXISTS {MESSAGE_ASSETS_DATA_TABLE}+(+ team_id Int64,+ function_kind LowCardinality(String),+ function_id String,+ parent_run_id String,+ invocation_id String,+ action_id String,+ kind LowCardinality(String),+ distinct_id String,+ person_id String,+ recipient String,+ subject String,+ status LowCardinality(String),+ sent_at DateTime64(6, 'UTC'),+ version UInt64,+ is_deleted UInt8 DEFAULT 0,+ html String CODEC(ZSTD(3)),+ INDEX parent_run_idx parent_run_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX recipient_idx recipient TYPE bloom_filter(0.01) GRANULARITY 1+ {KAFKA_COLUMNS_WITH_PARTITION}+)+ENGINE = {MESSAGE_ASSETS_ENGINE()}+PARTITION BY toYYYYMMDD(sent_at)+ORDER BY (team_id, function_kind, function_id, invocation_id, action_id)+{ttl_period("sent_at", MESSAGE_ASSETS_TTL_DAYS, unit="DAY")}+SETTINGS index_granularity = 1024, ttl_only_drop_parts = 1+"""+)+++# Created on both AUX (so AUX-local queries work) and DATA (so HogQL queries+# from the main cluster reach the data).+DISTRIBUTED_MESSAGE_ASSETS_TABLE_SQL = lambda: (+ f"""+CREATE TABLE IF NOT EXISTS {MESSAGE_ASSETS_TABLE}+(+ {MESSAGE_ASSETS_KAFKA_COLUMNS}+ {KAFKA_COLUMNS_WITH_PARTITION}+)+ENGINE = {Distributed(data_table=MESSAGE_ASSETS_DATA_TABLE, cluster=settings.CLICKHOUSE_AUX_CLUSTER)}+"""+)+++# Backed by the warpstream-cyclotron named collection — same cluster the CDP+# producer writes to and the same one hog_invocation_results consumes from.+KAFKA_MESSAGE_ASSETS_TABLE_SQL = lambda: (+ f"""+CREATE TABLE IF NOT EXISTS {KAFKA_MESSAGE_ASSETS_TABLE}+(+ {MESSAGE_ASSETS_KAFKA_COLUMNS}+)+ENGINE = {+ kafka_engine(+ topic=KAFKA_MESSAGE_ASSETS,+ group=CONSUMER_GROUP_MESSAGE_ASSETS,+ named_collection=settings.CLICKHOUSE_KAFKA_WARPSTREAM_CYCLOTRON_NAMED_COLLECTION,+ )+ }+SETTINGS kafka_skip_broken_messages = 100+"""+)+++MESSAGE_ASSETS_MV_SQL = lambda target_table=MESSAGE_ASSETS_DATA_TABLE: (+ f"""+CREATE MATERIALIZED VIEW IF NOT EXISTS {MESSAGE_ASSETS_MV_TABLE}+TO {target_table}+AS SELECT+ team_id,+ function_kind,+ function_id,+ parent_run_id,+ invocation_id,+ action_id,+ kind,+ distinct_id,+ person_id,+ recipient,+ subject,+ status,+ sent_at,+ version,+ is_deleted,+ html,+ _timestamp,+ _offset,+ _partition+FROM {KAFKA_MESSAGE_ASSETS_TABLE}+"""+)+++TRUNCATE_MESSAGE_ASSETS_TABLE_SQL = f"TRUNCATE TABLE IF EXISTS {MESSAGE_ASSETS_DATA_TABLE}"+++# Writes go to the local data table — the distributed read alias isn't writable.+INSERT_MESSAGE_ASSET_SQL = f"""+INSERT INTO {MESSAGE_ASSETS_DATA_TABLE} (+ team_id,+ function_kind,+ function_id,+ parent_run_id,+ invocation_id,+ action_id,+ kind,+ distinct_id,+ person_id,+ recipient,+ subject,+ status,+ sent_at,+ version,+ is_deleted,+ html,+ _timestamp,+ _offset,+ _partition+)+SELECT+ %(team_id)s,+ %(function_kind)s,+ %(function_id)s,+ %(parent_run_id)s,+ %(invocation_id)s,+ %(action_id)s,+ %(kind)s,+ %(distinct_id)s,+ %(person_id)s,+ %(recipient)s,+ %(subject)s,+ %(status)s,+ %(sent_at)s,+ %(version)s,+ %(is_deleted)s,+ %(html)s,+ now(),+ 0,+ 0+"""