: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): derive original_expiry_timestamp from timestamp in logs and traces Kafka MVs (#103036)
Frank Hamandef57a779edd3
diff --git a/posthog/clickhouse/hcl/golden/dev/apm.hcl b/posthog/clickhouse/hcl/golden/dev/apm.hclindex 88eb157eb9c3..8d06337fc73c 100644--- a/posthog/clickhouse/hcl/golden/dev/apm.hcl+++ b/posthog/clickhouse/hcl/golden/dev/apm.hcl@@ -1093,7 +1093,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),@@ -1350,7 +1350,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl b/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hclindex fead3407d47a..5ab599d7e4be 100644--- a/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl@@ -969,7 +969,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hclindex 44c550ef9a87..7ed2d6d461af 100644--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl@@ -3757,7 +3757,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hclindex 77ef10786a94..02d8c25ad8ad 100644--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl@@ -21067,7 +21067,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),@@ -21407,7 +21407,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hclindex 4d626732c237..7a090a0bf0ea 100644--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl@@ -3460,7 +3460,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hclindex fdb556e27ef5..274372eac99d 100644--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl@@ -3442,7 +3442,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/roles/coshared/apm_ingest/sql/kafka_trace_spans_avro_mv.sql b/posthog/clickhouse/hcl/roles/coshared/apm_ingest/sql/kafka_trace_spans_avro_mv.sqlindex 5ba185ab489a..f67978a828b9 100644--- a/posthog/clickhouse/hcl/roles/coshared/apm_ingest/sql/kafka_trace_spans_avro_mv.sql+++ b/posthog/clickhouse/hcl/roles/coshared/apm_ingest/sql/kafka_trace_spans_avro_mv.sql@@ -21,7 +21,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/sql/kafka_logs34_avro_mv.sql b/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/sql/kafka_logs34_avro_mv.sqlindex 267ac14d472c..5ce53c50ad30 100644--- a/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/sql/kafka_logs34_avro_mv.sql+++ b/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/sql/kafka_logs34_avro_mv.sql@@ -14,7 +14,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/roles/ingestion_events/local/sql/kafka_logs34_avro_mv.sql b/posthog/clickhouse/hcl/roles/ingestion_events/local/sql/kafka_logs34_avro_mv.sqlindex 267ac14d472c..5ce53c50ad30 100644--- a/posthog/clickhouse/hcl/roles/ingestion_events/local/sql/kafka_logs34_avro_mv.sql+++ b/posthog/clickhouse/hcl/roles/ingestion_events/local/sql/kafka_logs34_avro_mv.sql@@ -14,7 +14,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl b/posthog/clickhouse/hcl/roles/logs/local/tables.hclindex 1e4a2fed2769..f0ffaf981917 100644--- a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl+++ b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl@@ -612,7 +612,7 @@ SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/sql/dev/apm.sql b/posthog/clickhouse/hcl/sql/dev/apm.sqlindex da2ff4d573fe..0d7f92704013 100644--- a/posthog/clickhouse/hcl/sql/dev/apm.sql+++ b/posthog/clickhouse/hcl/sql/dev/apm.sql@@ -344,7 +344,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),@@ -434,7 +434,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.writable_tmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql b/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sqlindex 2d908b2dfa24..70811905adde 100644--- a/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql@@ -301,7 +301,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sqlindex 88a9d6d9a058..68da898f13c5 100644--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql@@ -969,7 +969,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spanmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sqlindex 60d556164e22..dfe18477f035 100644--- a/posthog/clickhouse/hcl/sql/local-single/all.sql+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql@@ -4893,7 +4893,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),@@ -5013,7 +5013,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spanmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sqlindex bbe28e68ada3..2dc3dcba8f00 100644--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql@@ -944,7 +944,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spanmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sqlindex 272c7730b40a..d7339c54e4a1 100644--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql@@ -938,7 +938,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spanmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp+ timestamp+ toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0),diff --git a/posthog/clickhouse/logs/logs34.py b/posthog/clickhouse/logs/logs34.pyindex 6c02f1d783f3..df4bdce96c16 100644--- a/posthog/clickhouse/logs/logs34.py+++ b/posthog/clickhouse/logs/logs34.py@@ -312,7 +312,7 @@ def KAFKA_LOGS34_AVRO_MV_SELECT():mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp + toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15)))) AS original_expiry_timestamp,+ timestamp + toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15)))) AS original_expiry_timestamp,_partition,_topic,_offset,diff --git a/posthog/clickhouse/migrations/0348_expiry_from_timestamp.py b/posthog/clickhouse/migrations/0348_expiry_from_timestamp.pynew file mode 100644index 000000000000..951678a0c1f2--- /dev/null+++ b/posthog/clickhouse/migrations/0348_expiry_from_timestamp.py@@ -0,0 +1,34 @@+from django.conf import settings++from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.clickhouse.logs import KAFKA_LOGS34_AVRO_MV+from posthog.clickhouse.traces import KAFKA_TRACE_SPANS_AVRO_MV+from posthog.run_mode import RunMode, run_mode++DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE++# `original_expiry_timestamp` drives PARTITION BY and TTL on logs34 and trace_spans. The Kafka MVs+# derived it from `observed_timestamp` (ingest time), so a record with an old `timestamp` — late or+# backfilled data — stayed for the full retention window after ingest instead of after the moment it+# describes. Both MVs now derive it from `timestamp`.+#+# Only the MV SELECT changes; the Kafka engine tables keep their column set and stay in place. An MV+# is not replicated, so no SYNC is needed. Rows already stored keep their existing expiry.+#+# Placement follows the HCL layers that hold each MV. Dev's apm nodes compose both+# `coshared/logs_avro_ingest` and `coshared/apm_ingest`, and the MVs there write through the+# `writable_*` Distributed proxies into the logs cluster, as 0330 describes. Elsewhere the logs MV+# lives on the ingestion-events nodes (0305/0307) and writes through `writable_logs34`, and the+# traces MV lives on the logs nodes (0290/0330) and writes into `trace_spans` directly.+_is_dev = run_mode() is RunMode.CLOUD_DEV+_logs_role = NodeRole.APM if _is_dev else NodeRole.INGESTION_EVENTS+_traces_role = NodeRole.APM if _is_dev else NodeRole.LOGS+_traces_mv = KAFKA_TRACE_SPANS_AVRO_MV(to_table="writable_trace_spans") if _is_dev else KAFKA_TRACE_SPANS_AVRO_MV()++operations = [+ run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_logs34_avro_mv", node_roles=[_logs_role]),+ run_sql_with_exceptions(KAFKA_LOGS34_AVRO_MV(to_table="writable_logs34"), node_roles=[_logs_role]),+ run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_trace_spans_avro_mv", node_roles=[_traces_role]),+ run_sql_with_exceptions(_traces_mv, node_roles=[_traces_role]),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex ce57d41a6de9..2ad7e345e905 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0347_drop_metrics2_ingest+0348_expiry_from_timestampdiff --git a/posthog/clickhouse/traces/spans.py b/posthog/clickhouse/traces/spans.pyindex 27a2912d1611..18a001298c86 100644--- a/posthog/clickhouse/traces/spans.py+++ b/posthog/clickhouse/traces/spans.py@@ -388,7 +388,7 @@ AS SELECTmapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp + toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15)))) AS original_expiry_timestamp,+ timestamp + toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15)))) AS original_expiry_timestamp,_partition,_topic,_offset,