: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
fix(clickhouse): keep $sent_at and session duration permanent in udf (#111699)
Rory Shanks03714f693b3d
diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.goindex 793c2d9598c3..2c07222775c9 100644--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go@@ -95,8 +95,11 @@ func isTemporaryProperty(key string) bool {"$debug_first_full_snapshot_timestamp", "$snapshot_max_depth_exceeded","$sess_rec_flush_size", "$session_recording_remote_config","$session_recording_network_payload_capture", "$session_recording_canvas_recording",- "$replay_script_config", "$sent_at", "$lib_rate_limit_remaining_tokens", "$lib_custom_api_host":+ "$replay_script_config", "$lib_rate_limit_remaining_tokens", "$lib_custom_api_host":return true+ case "$sdk_debug_current_session_duration":+ // Customers read it over windows longer than the temporary retention, so it stays permanent.+ return false}return strings.HasPrefix(key, "$sdk_debug_")}diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.goindex c688b33c829e..c5b3e6856008 100644--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go@@ -637,8 +637,8 @@ func TestProcessLineSplitsTemporaryProperties(t *testing.T) {{name: "allowlist",input: `{"$set":{"score":7,"enabled":false},"$set_once":{"source":"demo"},"$unset":["old"],"$group_set":{"tier":"basic"},"$feature_flag_request_id":"request-example","$debug_first_full_snapshot_timestamp":1,"$snapshot_max_depth_exceeded":true,"$sess_rec_flush_size":2,"$session_recording_remote_config":{"enabled":true},"$session_recording_network_payload_capture":false,"$session_recording_canvas_recording":true,"$replay_script_config":{"version":3},"$sent_at":"2026-01-01","$lib_rate_limit_remaining_tokens":0,"$lib_custom_api_host":"https://example.com","$sdk_debug_new_metric":[[1,"x"],null],"$sdk_debug_current_session_duration":42,"$debug_images":[{"type":"elf"}],"$feature/demo":"control","$active_feature_flags":["demo"],"$feature_flag_payload":{},"$feature_flag_payloads":{},"$feature_flag_bootstrapped_payload":{},"$feature_flag_original_payload":{},"$transformations_succeeded":[],"custom":{"$set":"keep"}}`,- permanent: `{"$debug_images":[{"type":"elf"}],"custom":{"$set":"keep"},"$feature_flags":{"demo":"control"}}`,- temporary: `{"$set":{"score":7,"enabled":false},"$set_once":{"source":"demo"},"$unset":["old"],"$group_set":{"tier":"basic"},"$feature_flag_request_id":"request-example","$debug_first_full_snapshot_timestamp":1,"$snapshot_max_depth_exceeded":true,"$sess_rec_flush_size":2,"$session_recording_remote_config":{"enabled":true},"$session_recording_network_payload_capture":false,"$session_recording_canvas_recording":true,"$replay_script_config":{"version":3},"$sent_at":"2026-01-01","$lib_rate_limit_remaining_tokens":0,"$lib_custom_api_host":"https://example.com","$sdk_debug_new_metric":[[1,"x"],null],"$sdk_debug_current_session_duration":42}`,+ permanent: `{"$sent_at":"2026-01-01","$sdk_debug_current_session_duration":42,"$debug_images":[{"type":"elf"}],"custom":{"$set":"keep"},"$feature_flags":{"demo":"control"}}`,+ temporary: `{"$set":{"score":7,"enabled":false},"$set_once":{"source":"demo"},"$unset":["old"],"$group_set":{"tier":"basic"},"$feature_flag_request_id":"request-example","$debug_first_full_snapshot_timestamp":1,"$snapshot_max_depth_exceeded":true,"$sess_rec_flush_size":2,"$session_recording_remote_config":{"enabled":true},"$session_recording_network_payload_capture":false,"$session_recording_canvas_recording":true,"$replay_script_config":{"version":3},"$lib_rate_limit_remaining_tokens":0,"$lib_custom_api_host":"https://example.com","$sdk_debug_new_metric":[[1,"x"],null]}`,},{name: "dotted keys and normalization",diff --git a/clickhouse-udfs/util/testdata/json_clean_posthog_event_properties/stateless/clean_event_properties.reference b/clickhouse-udfs/util/testdata/json_clean_posthog_event_properties/stateless/clean_event_properties.referenceindex 6d1b33acfbb1..1e65ef54c2c3 100644--- a/clickhouse-udfs/util/testdata/json_clean_posthog_event_properties/stateless/clean_event_properties.reference+++ b/clickhouse-udfs/util/testdata/json_clean_posthog_event_properties/stateless/clean_event_properties.reference@@ -1,2 +1,2 @@{"$agent_turn":"3.0","$ai_total_cost_usd":"1.5","$is_identified":"true","$mcp_listed_tool_names":["search"],"$exception_list":[{"type":"TypeError"}],"Account.client_id":"abc","Account":{},"huge":"18446744073709551616","max_uint":18446744073709551615,"dupe":"kept","$feature_flags":{"beta":"control"}}-{"$set.profile.score":7,"$debug_images":[{"type":"elf"}],"custom":true}+{"$set.profile.score":7,"$sdk_debug_current_session_duration":42,"$debug_images":[{"type":"elf"}],"custom":true}diff --git a/clickhouse-udfs/util/testdata/json_clean_posthog_temporary_properties/stateless/clean_temporary_properties.reference b/clickhouse-udfs/util/testdata/json_clean_posthog_temporary_properties/stateless/clean_temporary_properties.referenceindex 55aead379578..31880e8bf1ec 100644--- a/clickhouse-udfs/util/testdata/json_clean_posthog_temporary_properties/stateless/clean_temporary_properties.reference+++ b/clickhouse-udfs/util/testdata/json_clean_posthog_temporary_properties/stateless/clean_temporary_properties.reference@@ -1,3 +1,3 @@-{"$set":{"score":7,"enabled":false},"$unset":["old"],"$sdk_debug_probe.count":2,"$sdk_debug_current_session_duration":42,"$feature_flag_request_id":"request-example"}+{"$set":{"score":7,"enabled":false},"$unset":["old"],"$sdk_debug_probe.count":2,"$feature_flag_request_id":"request-example"}{}{"$set":{"enabled":false},"$unset":[]}diff --git a/docs/internal/clickhouse-utility-udfs.md b/docs/internal/clickhouse-utility-udfs.mdindex 43b1ca946143..26bb8dbb43ee 100644--- a/docs/internal/clickhouse-utility-udfs.md+++ b/docs/internal/clickhouse-utility-udfs.md@@ -72,16 +72,16 @@ SELECT JSONCleanPostHogEvent('{"$set":{"score":7},"$feature/demo":"control","pla### `JSONCleanPostHogTemporaryProperties(json)`-Accepts a JSON object and retains only the following top-level properties. A dotted key is one flat key, so `$set.foo` is not retained; a key that starts with `$sdk_debug_` is. It uses the event cleaner's null-object-field removal, duplicate handling, and integer protection, without coercing values to declared schema types. Non-object input fails.+Accepts a JSON object and retains only the following top-level properties. A dotted key is one flat key, so `$set.foo` is not retained; a key that starts with `$sdk_debug_` is, except `$sdk_debug_current_session_duration`. It uses the event cleaner's null-object-field removal, duplicate handling, and integer protection, without coercing values to declared schema types. Non-object input fails.| Category | Allowlist || ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ || Person and group instructions | `$set`, `$set_once`, `$unset`, `$group_set` |-| SDK diagnostics | Every `$sdk_debug_*` property, including session duration |+| SDK diagnostics | Every `$sdk_debug_*` property, except `$sdk_debug_current_session_duration` || Flag diagnostics | `$feature_flag_request_id` || Replay diagnostics | `$debug_first_full_snapshot_timestamp`, `$snapshot_max_depth_exceeded`, `$sess_rec_flush_size` || Replay configuration | `$session_recording_remote_config`, `$session_recording_network_payload_capture`, `$session_recording_canvas_recording`, `$replay_script_config` |-| Transport diagnostics | `$sent_at`, `$lib_rate_limit_remaining_tokens`, `$lib_custom_api_host` |+| Transport diagnostics | `$lib_rate_limit_remaining_tokens`, `$lib_custom_api_host` |`$feature_flag_request_id` moves to temporary properties on every event type. `$debug_images` remains in permanent properties. Feature-flag payloads and `$active_feature_flags` are excluded from both outputs. Matching applies only at the root: a custom object's nested `$set` is not a temporary property.@@ -92,8 +92,8 @@ WITH '{"$set":{"score":7},"$sdk_debug_probe":true,"$sdk_debug_current_session_duSELECTJSONCleanPostHogEventProperties(raw_properties) AS properties,JSONCleanPostHogTemporaryProperties(raw_properties) AS temporary_properties;--- properties: {"custom":"kept"}--- temporary_properties: {"$set":{"score":7},"$sdk_debug_probe":true,"$sdk_debug_current_session_duration":42,"$feature_flag_request_id":"request-example"}+-- properties: {"$sdk_debug_current_session_duration":42,"custom":"kept"}+-- temporary_properties: {"$set":{"score":7},"$sdk_debug_probe":true,"$feature_flag_request_id":"request-example"}```Both functions use the same executable. The temporary entry point uses `--temporary-properties` with the existing chunk protocol.diff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64index c7ae68068ad0..50e90cef51f2 100755Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 differdiff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64index bc7755669056..bf2af796301c 100755Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 differ