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(replay): add a readable capture mode aggregate column (#99183)

Manoel Aranda Netoc66860471d8e
diff --git a/docs/internal/mobile-replay-capture-mode.md b/docs/internal/mobile-replay-capture-mode.md
index f59b13c45f1c..2d2514da9d20 100644
--- a/docs/internal/mobile-replay-capture-mode.md
+++ b/docs/internal/mobile-replay-capture-mode.md
@@ -1,7 +1,9 @@
# Mobile replay capture mode
-Replay ingestion stores `snapshot_mode` in ClickHouse recording metadata so recordings can be counted by rendering mode without downloading replay blobs.
+Replay ingestion sends `snapshot_mode` in its Kafka metadata payload.
+The materialized view stores that value as a nullable string aggregate state in `snapshot_mode_v2`, so recordings can be counted by rendering mode without downloading replay blobs.
This field only applies to events with `$snapshot_source = 'mobile'`.
+The aggregate table stores capture mode only in `snapshot_mode_v2`.
## Classification
@@ -19,12 +21,13 @@ Base64 image data does not identify screenshot mode because wireframe-mode image
A recording uses one mode throughout its lifetime.
The recorder stops inspecting wireframes once it identifies the mode for its in-memory storage block.
Each new block can identify the mode independently, without a shared cache or a ClickHouse lookup.
-ClickHouse stores `AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))` and retains the first non-null mode across blocks.
+ClickHouse stores `AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))` and retains the first non-null mode across blocks.
+The materialized views and destination tables use matching plain nullable aggregate states for `snapshot_source`, `snapshot_library`, and `snapshot_mode_v2`.
Blocks without visual evidence cannot overwrite a known mode.
## Querying daily recording counts
-Use `argMinMerge(snapshot_mode)` on the physical ClickHouse `session_replay_events` table.
+Use `argMinMerge(snapshot_mode_v2)` on the physical ClickHouse `session_replay_events` table.
The column is not exposed through the HogQL schema.
Group by both `team_id` and `session_id` before counting; one recording can have multiple rows or span midnight.
@@ -44,7 +47,7 @@ FROM
session_id,
min(min_first_timestamp) AS started_at,
argMinMerge(snapshot_source) AS source,
- argMinMerge(snapshot_mode) AS mode,
+ argMinMerge(snapshot_mode_v2) AS mode,
max(is_deleted) AS deleted
FROM session_replay_events
WHERE team_id = 1
@@ -65,10 +68,21 @@ Keep the `unknown` count visible to distinguish missing classifications from wir
## Deployment and historical data
-Ship the ClickHouse migration separately and apply it before deploying the ingestion change.
-The migration updates the sharded, read, and writable tables, plus the MSK or WarpStream ingestion path used by the environment.
+Existing installations need schema cleanup before deploying this change.
+Coordinate with the ClickHouse team to remove the old stored `snapshot_mode` column and align `snapshot_source` to `AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))` on the sharded, writable, and read tables.
+Migration `0322` checks these prerequisites on every target node before adding columns or replacing the materialized views.
+If a check fails, the migration stops without changing tables or views.
+Complete the coordinated cleanup, then retry the migration.
+Changes to historical migrations apply to fresh installations and do not repair tables on installations that already ran them.
+The replacement-column migration does not perform this cleanup or convert existing source aggregate states.
+
+The replacement-column migration adds `snapshot_mode_v2` to the sharded, read, and writable tables before recreating the active MSK or WarpStream materialized view.
+The producer's `snapshot_mode` payload and the Kafka table remain unchanged, so the ingestion classifier does not need a coordinated deployment.
Old producers can omit the nullable field during rollout.
-Only newly ingested visual snapshots populate the mode.
+The migration does not backfill `snapshot_mode_v2`.
+Do not remove the Kafka payload field; it supplies `snapshot_mode_v2`.
+
+Only blocks ingested through the updated materialized view populate `snapshot_mode_v2`.
Historical recordings remain unknown unless new blocks identify their mode or retained replay blobs are classified and backfilled separately.
The anonymized ML mirror uses separate metadata storage and is outside this ClickHouse reporting path.
diff --git a/posthog/clickhouse/hcl/golden/dev/data.hcl b/posthog/clickhouse/hcl/golden/dev/data.hcl
index ad3555053ea4..0b32f78ba54c 100644
--- a/posthog/clickhouse/hcl/golden/dev/data.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/data.hcl
@@ -4025,7 +4025,7 @@ database "posthog" {
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -4062,8 +4062,8 @@ database "posthog" {
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "distributed" {
cluster_name = "posthog"
@@ -6532,7 +6532,7 @@ database "posthog" {
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -6569,8 +6569,8 @@ database "posthog" {
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.session_replay_events"
@@ -8484,13 +8484,13 @@ database "posthog" {
type = "SimpleAggregateFunction(sum, Int64)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "SimpleAggregateFunction(max, DateTime)"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
index 8466420faec6..e0da94dd2c98 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
@@ -4623,7 +4623,7 @@ database "posthog" {
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -4660,8 +4660,8 @@ database "posthog" {
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "distributed" {
cluster_name = "posthog"
@@ -7763,7 +7763,7 @@ database "posthog" {
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -7800,8 +7800,8 @@ database "posthog" {
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.session_replay_events"
@@ -9953,13 +9953,13 @@ database "posthog" {
type = "SimpleAggregateFunction(sum, Int64)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "SimpleAggregateFunction(max, DateTime)"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl b/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl
index 938f2ea07930..68f3997a3d57 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl
@@ -1759,13 +1759,13 @@ database "posthog" {
type = "SimpleAggregateFunction(sum, Int64)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "SimpleAggregateFunction(max, DateTime)"
@@ -2654,7 +2654,7 @@ SELECT
sum(size) AS size,
sum(message_count) AS message_count,
sum(event_count) AS event_count,
- argMinState(snapshot_source, first_timestamp) AS snapshot_source,
+ argMinState(replay.snapshot_source, first_timestamp) AS snapshot_source,
argMinState(snapshot_library, first_timestamp) AS snapshot_library,
max(_timestamp) AS _timestamp,
max(retention_period_days) AS retention_period_days,
@@ -2663,8 +2663,8 @@ SELECT
groupUniqArrayArray(ai_tags_freeform) AS ai_tags_freeform,
max(ai_highlighted) AS ai_highlighted,
max(surfacing_score) AS surfacing_score,
- argMinState(snapshot_mode, first_timestamp) AS snapshot_mode
-FROM posthog.kafka_session_replay_events
+ argMinState(replay.snapshot_mode, first_timestamp) AS snapshot_mode_v2
+FROM posthog.kafka_session_replay_events AS replay
GROUP BY
session_id, team_id
SQL
@@ -2730,13 +2730,13 @@ SQL
type = "Int64"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "Nullable(DateTime)"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index e201c0fa23b9..b444d9e12dde 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -9360,7 +9360,7 @@ SQL
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -9397,8 +9397,8 @@ SQL
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "distributed" {
cluster_name = "posthog"
@@ -13480,7 +13480,7 @@ SQL
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -13517,8 +13517,8 @@ SQL
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.session_replay_events"
@@ -18063,13 +18063,13 @@ SQL
type = "SimpleAggregateFunction(sum, Int64)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "SimpleAggregateFunction(max, DateTime)"
@@ -22715,7 +22715,7 @@ SELECT
sum(size) AS size,
sum(message_count) AS message_count,
sum(event_count) AS event_count,
- argMinState(snapshot_source, first_timestamp) AS snapshot_source,
+ argMinState(replay.snapshot_source, first_timestamp) AS snapshot_source,
argMinState(snapshot_library, first_timestamp) AS snapshot_library,
max(_timestamp) AS _timestamp,
max(retention_period_days) AS retention_period_days,
@@ -22724,8 +22724,8 @@ SELECT
groupUniqArrayArray(ai_tags_freeform) AS ai_tags_freeform,
max(ai_highlighted) AS ai_highlighted,
max(surfacing_score) AS surfacing_score,
- argMinState(snapshot_mode, first_timestamp) AS snapshot_mode
-FROM posthog.kafka_session_replay_events
+ argMinState(replay.snapshot_mode, first_timestamp) AS snapshot_mode_v2
+FROM posthog.kafka_session_replay_events AS replay
GROUP BY
session_id, team_id
SQL
@@ -22791,13 +22791,13 @@ SQL
type = "Int64"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "Nullable(DateTime)"
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/data.hcl b/posthog/clickhouse/hcl/golden/prod-eu/data.hcl
index afed040dcbe1..e07cfe65e43f 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/data.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/data.hcl
@@ -4670,7 +4670,7 @@ database "posthog" {
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -4707,8 +4707,8 @@ database "posthog" {
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "distributed" {
cluster_name = "posthog"
@@ -6567,8 +6567,8 @@ database "posthog" {
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.session_replay_events"
@@ -8737,13 +8737,13 @@ database "posthog" {
type = "SimpleAggregateFunction(sum, Int64)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "SimpleAggregateFunction(max, DateTime)"
diff --git a/posthog/clickhouse/hcl/golden/prod-us/data.hcl b/posthog/clickhouse/hcl/golden/prod-us/data.hcl
index 6ae879438942..1954d4436992 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/data.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/data.hcl
@@ -4465,7 +4465,7 @@ database "posthog" {
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -4502,8 +4502,8 @@ database "posthog" {
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "distributed" {
cluster_name = "posthog"
@@ -6329,8 +6329,8 @@ database "posthog" {
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/reshard/{shard}/posthog.session_replay_events"
@@ -8559,13 +8559,13 @@ database "posthog" {
type = "SimpleAggregateFunction(sum, Int64)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "SimpleAggregateFunction(max, DateTime)"
diff --git a/posthog/clickhouse/hcl/roles/coshared/session_replay_write/tables.hcl b/posthog/clickhouse/hcl/roles/coshared/session_replay_write/tables.hcl
index 6beab50571ab..51f77094da2d 100644
--- a/posthog/clickhouse/hcl/roles/coshared/session_replay_write/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/coshared/session_replay_write/tables.hcl
@@ -61,13 +61,13 @@ database "posthog" {
type = "SimpleAggregateFunction(sum, Int64)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "SimpleAggregateFunction(max, DateTime)"
diff --git a/posthog/clickhouse/hcl/roles/data/common/shared_declarations.hcl b/posthog/clickhouse/hcl/roles/data/common/shared_declarations.hcl
index f8f8b36390c7..e84a48d7df05 100644
--- a/posthog/clickhouse/hcl/roles/data/common/shared_declarations.hcl
+++ b/posthog/clickhouse/hcl/roles/data/common/shared_declarations.hcl
@@ -1132,7 +1132,7 @@ SQL
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -1169,8 +1169,8 @@ SQL
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
engine "distributed" {
cluster_name = "posthog"
@@ -1459,7 +1459,7 @@ SQL
type = "SimpleAggregateFunction(max, DateTime)"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "all_urls" {
type = "SimpleAggregateFunction(groupUniqArrayArray, Array(String))"
@@ -1496,8 +1496,8 @@ SQL
column "surfacing_score" {
type = "SimpleAggregateFunction(max, Nullable(Float32))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
order_by = ["toDate(min_first_timestamp)", "team_id", "session_id"]
partition_by = "toYYYYMM(min_first_timestamp)"
diff --git a/posthog/clickhouse/hcl/roles/data/prod-eu/tables.hcl b/posthog/clickhouse/hcl/roles/data/prod-eu/tables.hcl
index f2e5dc554cdc..702fe37c8a6d 100644
--- a/posthog/clickhouse/hcl/roles/data/prod-eu/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/data/prod-eu/tables.hcl
@@ -1898,9 +1898,6 @@ SQL
settings = {
ttl_only_drop_parts = "1"
}
- modify_column "snapshot_source" {
- type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
- }
}
patch_table "writable_events_recent" {
diff --git a/posthog/clickhouse/hcl/roles/data/prod-us/tables.hcl b/posthog/clickhouse/hcl/roles/data/prod-us/tables.hcl
index fffcfba37b44..cb6161a09f54 100644
--- a/posthog/clickhouse/hcl/roles/data/prod-us/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/data/prod-us/tables.hcl
@@ -1611,9 +1611,6 @@ SQL
}
patch_table "sharded_session_replay_events" {
- modify_column "snapshot_source" {
- type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
- }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/reshard/{shard}/posthog.session_replay_events"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl b/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl
index 08841fb98955..310d9543bec2 100644
--- a/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl
@@ -1966,7 +1966,7 @@ SELECT
sum(size) AS size,
sum(message_count) AS message_count,
sum(event_count) AS event_count,
- argMinState(snapshot_source, first_timestamp) AS snapshot_source,
+ argMinState(replay.snapshot_source, first_timestamp) AS snapshot_source,
argMinState(snapshot_library, first_timestamp) AS snapshot_library,
max(_timestamp) AS _timestamp,
max(retention_period_days) AS retention_period_days,
@@ -1975,8 +1975,8 @@ SELECT
groupUniqArrayArray(ai_tags_freeform) AS ai_tags_freeform,
max(ai_highlighted) AS ai_highlighted,
max(surfacing_score) AS surfacing_score,
- argMinState(snapshot_mode, first_timestamp) AS snapshot_mode
-FROM posthog.kafka_session_replay_events
+ argMinState(replay.snapshot_mode, first_timestamp) AS snapshot_mode_v2
+FROM posthog.kafka_session_replay_events AS replay
GROUP BY
session_id, team_id
SQL
@@ -2042,13 +2042,13 @@ SQL
type = "Int64"
}
column "snapshot_source" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "snapshot_library" {
type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
- column "snapshot_mode" {
- type = "AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))"
+ column "snapshot_mode_v2" {
+ type = "AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))"
}
column "_timestamp" {
type = "Nullable(DateTime)"
diff --git a/posthog/clickhouse/hcl/sql/dev/data.sql b/posthog/clickhouse/hcl/sql/dev/data.sql
index 4c8aa8c5de57..b8991ccd3e60 100644
--- a/posthog/clickhouse/hcl/sql/dev/data.sql
+++ b/posthog/clickhouse/hcl/sql/dev/data.sql
@@ -1401,7 +1401,7 @@ CREATE TABLE posthog.sharded_session_replay_events (
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
_timestamp SimpleAggregateFunction(max, DateTime),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
@@ -1413,7 +1413,7 @@ CREATE TABLE posthog.sharded_session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/{shard}/posthog.session_replay_events', '{replica}') ORDER BY (toDate(min_first_timestamp), team_id, session_id) PARTITION BY toYYYYMM(min_first_timestamp) SETTINGS index_granularity = 512, ttl_only_drop_parts = 1;
CREATE TABLE posthog.sharded_sessions (
session_id String,
@@ -2009,9 +2009,9 @@ CREATE TABLE posthog.writable_session_replay_events (
size SimpleAggregateFunction(sum, Int64),
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
is_deleted SimpleAggregateFunction(max, UInt8) DEFAULT 0
@@ -2762,7 +2762,7 @@ CREATE TABLE posthog.session_replay_events (
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
_timestamp SimpleAggregateFunction(max, DateTime),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
@@ -2774,7 +2774,7 @@ CREATE TABLE posthog.session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = Distributed('posthog', 'posthog', 'sharded_session_replay_events', sipHash64(distinct_id));
CREATE TABLE posthog.sessions (
session_id String,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sql
index 5b5a69c361a3..870b5490994c 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql
@@ -1655,7 +1655,7 @@ CREATE TABLE posthog.sharded_session_replay_events (
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
_timestamp SimpleAggregateFunction(max, DateTime),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
@@ -1667,7 +1667,7 @@ CREATE TABLE posthog.sharded_session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/{shard}/posthog.session_replay_events', '{replica}') ORDER BY (toDate(min_first_timestamp), team_id, session_id) PARTITION BY toYYYYMM(min_first_timestamp) SETTINGS index_granularity = 512;
CREATE TABLE posthog.sharded_sessions (
session_id String,
@@ -2330,9 +2330,9 @@ CREATE TABLE posthog.writable_session_replay_events (
size SimpleAggregateFunction(sum, Int64),
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
is_deleted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
@@ -3438,7 +3438,7 @@ CREATE TABLE posthog.session_replay_events (
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
_timestamp SimpleAggregateFunction(max, DateTime),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
@@ -3450,7 +3450,7 @@ CREATE TABLE posthog.session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = Distributed('posthog', 'posthog', 'sharded_session_replay_events', sipHash64(distinct_id));
CREATE TABLE posthog.sessions (
session_id String,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql b/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql
index 6d0867249b63..fd713ed3f3a0 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql
@@ -528,9 +528,9 @@ CREATE TABLE posthog.writable_session_replay_events (
size SimpleAggregateFunction(sum, Int64),
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
is_deleted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
@@ -750,7 +750,7 @@ CREATE MATERIALIZED VIEW posthog.posthog_document_embeddings_kafka_to_buffer_mv
_offset,
_partition
FROM posthog.kafka_posthog_document_embeddings;
-CREATE MATERIALIZED VIEW posthog.session_replay_events_mv TO posthog.writable_session_replay_events (session_id String, team_id Int64, distinct_id String, min_first_timestamp DateTime64(6, 'UTC'), max_last_timestamp DateTime64(6, 'UTC'), block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))), block_last_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))), block_urls SimpleAggregateFunction(groupArrayArray, Array(String)), first_url AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)), click_count Int64, keypress_count Int64, mouse_activity_count Int64, active_milliseconds Int64, console_log_count Int64, console_warn_count Int64, console_error_count Int64, size Int64, message_count Int64, event_count Int64, snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')), snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')), _timestamp Nullable(DateTime), retention_period_days SimpleAggregateFunction(max, Nullable(Int64)), is_deleted SimpleAggregateFunction(max, UInt8), ai_tags_fixed SimpleAggregateFunction(groupUniqArrayArray, Array(String)), ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)), ai_highlighted SimpleAggregateFunction(max, UInt8), surfacing_score SimpleAggregateFunction(max, Nullable(Float32))) AS SELECT
+CREATE MATERIALIZED VIEW posthog.session_replay_events_mv TO posthog.writable_session_replay_events (session_id String, team_id Int64, distinct_id String, min_first_timestamp DateTime64(6, 'UTC'), max_last_timestamp DateTime64(6, 'UTC'), block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))), block_last_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))), block_urls SimpleAggregateFunction(groupArrayArray, Array(String)), first_url AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)), click_count Int64, keypress_count Int64, mouse_activity_count Int64, active_milliseconds Int64, console_log_count Int64, console_warn_count Int64, console_error_count Int64, size Int64, message_count Int64, event_count Int64, snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), _timestamp Nullable(DateTime), retention_period_days SimpleAggregateFunction(max, Nullable(Int64)), is_deleted SimpleAggregateFunction(max, UInt8), ai_tags_fixed SimpleAggregateFunction(groupUniqArrayArray, Array(String)), ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)), ai_highlighted SimpleAggregateFunction(max, UInt8), surfacing_score SimpleAggregateFunction(max, Nullable(Float32))) AS SELECT
session_id,
team_id,
any(distinct_id) AS distinct_id,
@@ -771,7 +771,7 @@ CREATE MATERIALIZED VIEW posthog.session_replay_events_mv TO posthog.writable_se
sum(size) AS size,
sum(message_count) AS message_count,
sum(event_count) AS event_count,
- argMinState(snapshot_source, first_timestamp) AS snapshot_source,
+ argMinState(replay.snapshot_source, first_timestamp) AS snapshot_source,
argMinState(snapshot_library, first_timestamp) AS snapshot_library,
max(_timestamp) AS _timestamp,
max(retention_period_days) AS retention_period_days,
@@ -780,8 +780,8 @@ CREATE MATERIALIZED VIEW posthog.session_replay_events_mv TO posthog.writable_se
groupUniqArrayArray(ai_tags_freeform) AS ai_tags_freeform,
max(ai_highlighted) AS ai_highlighted,
max(surfacing_score) AS surfacing_score,
- argMinState(snapshot_mode, first_timestamp) AS snapshot_mode
-FROM posthog.kafka_session_replay_events
+ argMinState(replay.snapshot_mode, first_timestamp) AS snapshot_mode_v2
+FROM posthog.kafka_session_replay_events AS replay
GROUP BY
session_id, team_id;
CREATE MATERIALIZED VIEW posthog.usage_report_events_preagg_mv TO posthog.writable_usage_report_events_preagg (date Date, team_id Int64, person_mode Enum8('full'=0, 'propertyless'=1, 'force_upgrade'=2), lib String, event String, distinct_events_unique AggregateFunction(uniqExact, Tuple(UInt64, UInt64, UInt64)), event_count AggregateFunction(sum, UInt64)) AS SELECT
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 276a69b487d6..ce73acb11c07 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -2754,7 +2754,7 @@ CREATE TABLE posthog.sharded_session_replay_events (
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
_timestamp SimpleAggregateFunction(max, DateTime),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
@@ -2766,7 +2766,7 @@ CREATE TABLE posthog.sharded_session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/{shard}/posthog.session_replay_events', '{replica}') ORDER BY (toDate(min_first_timestamp), team_id, session_id) PARTITION BY toYYYYMM(min_first_timestamp) SETTINGS index_granularity = 512;
CREATE TABLE posthog.sharded_session_replay_features (
session_id String,
@@ -4102,9 +4102,9 @@ CREATE TABLE posthog.writable_session_replay_events (
size SimpleAggregateFunction(sum, Int64),
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
is_deleted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
@@ -5332,7 +5332,7 @@ WHERE
bitAnd(bitShiftRight(toUInt128(accurateCastOrNull(`$session_id`, 'UUID')), 76), 15) = 7
GROUP BY
team_id, toStartOfHour(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(session_id_v7, 80)), 1000))), cityHash64(session_id_v7), session_id_v7;
-CREATE MATERIALIZED VIEW posthog.session_replay_events_mv TO posthog.writable_session_replay_events (session_id String, team_id Int64, distinct_id String, min_first_timestamp DateTime64(6, 'UTC'), max_last_timestamp DateTime64(6, 'UTC'), block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))), block_last_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))), block_urls SimpleAggregateFunction(groupArrayArray, Array(String)), first_url AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)), click_count Int64, keypress_count Int64, mouse_activity_count Int64, active_milliseconds Int64, console_log_count Int64, console_warn_count Int64, console_error_count Int64, size Int64, message_count Int64, event_count Int64, snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')), snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')), _timestamp Nullable(DateTime), retention_period_days SimpleAggregateFunction(max, Nullable(Int64)), is_deleted SimpleAggregateFunction(max, UInt8), ai_tags_fixed SimpleAggregateFunction(groupUniqArrayArray, Array(String)), ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)), ai_highlighted SimpleAggregateFunction(max, UInt8), surfacing_score SimpleAggregateFunction(max, Nullable(Float32))) AS SELECT
+CREATE MATERIALIZED VIEW posthog.session_replay_events_mv TO posthog.writable_session_replay_events (session_id String, team_id Int64, distinct_id String, min_first_timestamp DateTime64(6, 'UTC'), max_last_timestamp DateTime64(6, 'UTC'), block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))), block_last_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))), block_urls SimpleAggregateFunction(groupArrayArray, Array(String)), first_url AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)), click_count Int64, keypress_count Int64, mouse_activity_count Int64, active_milliseconds Int64, console_log_count Int64, console_warn_count Int64, console_error_count Int64, size Int64, message_count Int64, event_count Int64, snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')), _timestamp Nullable(DateTime), retention_period_days SimpleAggregateFunction(max, Nullable(Int64)), is_deleted SimpleAggregateFunction(max, UInt8), ai_tags_fixed SimpleAggregateFunction(groupUniqArrayArray, Array(String)), ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)), ai_highlighted SimpleAggregateFunction(max, UInt8), surfacing_score SimpleAggregateFunction(max, Nullable(Float32))) AS SELECT
session_id,
team_id,
any(distinct_id) AS distinct_id,
@@ -5353,7 +5353,7 @@ CREATE MATERIALIZED VIEW posthog.session_replay_events_mv TO posthog.writable_se
sum(size) AS size,
sum(message_count) AS message_count,
sum(event_count) AS event_count,
- argMinState(snapshot_source, first_timestamp) AS snapshot_source,
+ argMinState(replay.snapshot_source, first_timestamp) AS snapshot_source,
argMinState(snapshot_library, first_timestamp) AS snapshot_library,
max(_timestamp) AS _timestamp,
max(retention_period_days) AS retention_period_days,
@@ -5362,8 +5362,8 @@ CREATE MATERIALIZED VIEW posthog.session_replay_events_mv TO posthog.writable_se
groupUniqArrayArray(ai_tags_freeform) AS ai_tags_freeform,
max(ai_highlighted) AS ai_highlighted,
max(surfacing_score) AS surfacing_score,
- argMinState(snapshot_mode, first_timestamp) AS snapshot_mode
-FROM posthog.kafka_session_replay_events
+ argMinState(replay.snapshot_mode, first_timestamp) AS snapshot_mode_v2
+FROM posthog.kafka_session_replay_events AS replay
GROUP BY
session_id, team_id;
CREATE MATERIALIZED VIEW posthog.session_replay_features_mv TO posthog.writable_session_replay_features (session_id String, team_id Int64, distinct_id String, min_first_timestamp DateTime64(6, 'UTC'), max_last_timestamp DateTime64(6, 'UTC'), event_count Int64, mouse_position_count Int64, mouse_sum_x Float64, mouse_sum_x_squared Float64, mouse_sum_y Float64, mouse_sum_y_squared Float64, mouse_distance_traveled Float64, mouse_direction_change_count Int64, mouse_velocity_sum Float64, mouse_velocity_sum_of_squares Float64, mouse_velocity_count Int64, scroll_event_count Int64, total_scroll_magnitude Float64, scroll_direction_reversal_count Int64, rapid_scroll_reversal_count Int64, scroll_to_top_count Int64, click_count Int64, keypress_count Int64, mouse_activity_count Int64, rage_click_count Int64, dead_click_count Int64, backspace_count Int64, inter_action_gap_count Int64, inter_action_gap_sum_ms Float64, inter_action_gap_sum_of_squares_ms Float64, max_idle_gap_ms Float64, long_idle_gap_count Int64, quick_back_count Int64, page_visit_count Int64, unique_url_count AggregateFunction(uniqCombinedArray(12), Array(String)), login_path_visit_count Int64, signup_path_visit_count Int64, checkout_path_visit_count Int64, cart_path_visit_count Int64, billing_path_visit_count Int64, settings_path_visit_count Int64, account_path_visit_count Int64, error_path_visit_count Int64, not_found_path_visit_count Int64, admin_path_visit_count Int64, dashboard_path_visit_count Int64, onboarding_path_visit_count Int64, cancel_path_visit_count Int64, refund_path_visit_count Int64, console_error_count Int64, console_error_after_click_count Int64, console_warn_count Int64, network_request_count Int64, network_failed_request_count Int64, network_4xx_count Int64, network_5xx_count Int64, network_request_duration_sum Float64, network_request_duration_sum_of_squares Float64, network_request_duration_count Int64, mutation_count Int64, viewport_resize_count Int64, touch_event_count Int64, max_scroll_y Float64, unique_click_target_count AggregateFunction(uniqCombinedArray(12), Array(Int64)), unique_form_field_count AggregateFunction(uniqCombinedArray(12), Array(Int64)), text_selection_count Int64, selection_copy_count Int64, is_deleted UInt8) AS SELECT
@@ -6829,7 +6829,7 @@ CREATE TABLE posthog.session_replay_events (
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
_timestamp SimpleAggregateFunction(max, DateTime),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
@@ -6841,7 +6841,7 @@ CREATE TABLE posthog.session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = Distributed('posthog', 'posthog', 'sharded_session_replay_events', sipHash64(distinct_id));
CREATE TABLE posthog.session_replay_features (
session_id String,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/data.sql b/posthog/clickhouse/hcl/sql/prod-eu/data.sql
index 1751eac8d626..9826f278e032 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/data.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/data.sql
@@ -1632,7 +1632,7 @@ CREATE TABLE posthog.sharded_session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/{shard}/posthog.session_replay_events', '{replica}') ORDER BY (toDate(min_first_timestamp), team_id, session_id) PARTITION BY toYYYYMM(min_first_timestamp) SETTINGS index_granularity = 512, ttl_only_drop_parts = 1;
CREATE TABLE posthog.swap_person_distinct_id (
distinct_id String,
@@ -2304,9 +2304,9 @@ CREATE TABLE posthog.writable_session_replay_events (
size SimpleAggregateFunction(sum, Int64),
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
retention_period_days SimpleAggregateFunction(max, Nullable(Int64))
) ENGINE = Distributed('posthog', 'posthog', 'sharded_session_replay_events', sipHash64(distinct_id));
@@ -3089,7 +3089,7 @@ CREATE TABLE posthog.session_replay_events (
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
_timestamp SimpleAggregateFunction(max, DateTime),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
@@ -3101,7 +3101,7 @@ CREATE TABLE posthog.session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = Distributed('posthog', 'posthog', 'sharded_session_replay_events', sipHash64(distinct_id));
CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
FROM posthog.custom_metrics_test
diff --git a/posthog/clickhouse/hcl/sql/prod-us/data.sql b/posthog/clickhouse/hcl/sql/prod-us/data.sql
index 47bad17ee17b..ea8452a7737b 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/data.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/data.sql
@@ -1464,7 +1464,7 @@ CREATE TABLE posthog.sharded_session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/reshard/{shard}/posthog.session_replay_events', '{replica}') ORDER BY (toDate(min_first_timestamp), team_id, session_id) PARTITION BY toYYYYMM(min_first_timestamp) SETTINGS index_granularity = 512;
CREATE TABLE posthog.swap_person_distinct_id (
distinct_id String,
@@ -2107,9 +2107,9 @@ CREATE TABLE posthog.writable_session_replay_events (
size SimpleAggregateFunction(sum, Int64),
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
retention_period_days SimpleAggregateFunction(max, Nullable(Int64))
) ENGINE = Distributed('posthog_writable', 'posthog', 'sharded_session_replay_events', sipHash64(distinct_id));
@@ -2926,7 +2926,7 @@ CREATE TABLE posthog.session_replay_events (
message_count SimpleAggregateFunction(sum, Int64),
event_count SimpleAggregateFunction(sum, Int64),
_timestamp SimpleAggregateFunction(max, DateTime),
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
all_urls SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
block_first_timestamps SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
@@ -2938,7 +2938,7 @@ CREATE TABLE posthog.session_replay_events (
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
) ENGINE = Distributed('posthog', 'posthog', 'sharded_session_replay_events', sipHash64(distinct_id));
CREATE TABLE posthog.writable_raw_sessions_v3 (
team_id Int64,
diff --git a/posthog/clickhouse/migrations/0051_session_replay_source_mv_fix.py b/posthog/clickhouse/migrations/0051_session_replay_source_mv_fix.py
index 3dc9134bf120..ae345d489dc4 100644
--- a/posthog/clickhouse/migrations/0051_session_replay_source_mv_fix.py
+++ b/posthog/clickhouse/migrations/0051_session_replay_source_mv_fix.py
@@ -3,9 +3,6 @@ from posthog.session_recordings.sql.session_replay_event_migrations_sql import D
from posthog.session_recordings.sql.session_replay_event_sql import SESSION_REPLAY_EVENTS_TABLE_MV_SQL
operations = [
- # we have to drop materialized view because 0050 created it incorrectly
run_sql_with_exceptions(DROP_SESSION_REPLAY_EVENTS_TABLE_MV_SQL()),
- # now we can recreate it with explicit column definitions
- # that correctly identifies snapshot source as LowCardinality(Nullable(String))
run_sql_with_exceptions(SESSION_REPLAY_EVENTS_TABLE_MV_SQL()),
]
diff --git a/posthog/clickhouse/migrations/0315_add_snapshot_mode_to_session_replay_events.py b/posthog/clickhouse/migrations/0315_add_snapshot_mode_to_session_replay_events.py
index e39870864f53..2016d6c2b500 100644
--- a/posthog/clickhouse/migrations/0315_add_snapshot_mode_to_session_replay_events.py
+++ b/posthog/clickhouse/migrations/0315_add_snapshot_mode_to_session_replay_events.py
@@ -14,11 +14,6 @@ from posthog.session_recordings.sql.session_replay_event_sql import (
SESSION_REPLAY_EVENTS_WS_MV_SQL,
)
-ADD_SNAPSHOT_MODE = """
-ALTER TABLE {table_name}
-ADD COLUMN IF NOT EXISTS snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
-"""
-
operations = [
run_sql_with_exceptions(
DROP_SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
@@ -28,28 +23,13 @@ operations = [
),
run_sql_with_exceptions(DROP_SESSION_REPLAY_EVENTS_WS_MV_SQL, node_roles=[NodeRole.INGESTION_SMALL]),
run_sql_with_exceptions(DROP_KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE_SQL, node_roles=[NodeRole.INGESTION_SMALL]),
- run_sql_with_exceptions(
- ADD_SNAPSHOT_MODE.format(table_name="sharded_session_replay_events"),
- node_roles=[NodeRole.DATA],
- sharded=True,
- is_alter_on_replicated_table=True,
- ),
- run_sql_with_exceptions(
- ADD_SNAPSHOT_MODE.format(table_name="session_replay_events"),
- node_roles=[NodeRole.DATA],
- sharded=False,
- is_alter_on_replicated_table=False,
- ),
- run_sql_with_exceptions(
- ADD_SNAPSHOT_MODE.format(table_name="writable_session_replay_events"),
- node_roles=[NodeRole.INGESTION_SMALL],
- sharded=False,
- is_alter_on_replicated_table=False,
- ),
*(
[
run_sql_with_exceptions(KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE_SQL(), node_roles=[NodeRole.INGESTION_SMALL]),
- run_sql_with_exceptions(SESSION_REPLAY_EVENTS_WS_MV_SQL(), node_roles=[NodeRole.INGESTION_SMALL]),
+ run_sql_with_exceptions(
+ SESSION_REPLAY_EVENTS_WS_MV_SQL(exclude_columns=["snapshot_mode_v2"]),
+ node_roles=[NodeRole.INGESTION_SMALL],
+ ),
]
if run_mode().is_deployed_cloud
else [
@@ -57,7 +37,8 @@ operations = [
KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
),
run_sql_with_exceptions(
- SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
+ SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=False, exclude_columns=["snapshot_mode_v2"]),
+ node_roles=[NodeRole.INGESTION_SMALL],
),
]
),
diff --git a/posthog/clickhouse/migrations/0322_add_snapshot_mode_v2_to_session_replay_events.py b/posthog/clickhouse/migrations/0322_add_snapshot_mode_v2_to_session_replay_events.py
new file mode 100644
index 000000000000..f20e9d72024d
--- /dev/null
+++ b/posthog/clickhouse/migrations/0322_add_snapshot_mode_v2_to_session_replay_events.py
@@ -0,0 +1,77 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.run_mode import run_mode
+from posthog.session_recordings.sql.session_replay_event_migrations_sql import DROP_SESSION_REPLAY_EVENTS_TABLE_MV_SQL
+from posthog.session_recordings.sql.session_replay_event_sql import (
+ DROP_SESSION_REPLAY_EVENTS_WS_MV_SQL,
+ SESSION_REPLAY_EVENTS_TABLE_MV_SQL,
+ SESSION_REPLAY_EVENTS_WS_MV_SQL,
+)
+
+CHECK_REPLAY_SCHEMA = """
+SELECT throwIf(
+ countIf(name = 'snapshot_mode') > 0
+ OR countIf(
+ name = 'snapshot_source'
+ AND type = 'AggregateFunction(argMin, Nullable(String), DateTime64(6, ''UTC''))'
+ ) != 1,
+ 'Replay metadata schema is not ready on {table_name}. Complete the ClickHouse cleanup in docs/internal/mobile-replay-capture-mode.md before retrying migration 0322.'
+)
+FROM system.columns
+WHERE database = currentDatabase() AND table = '{table_name}'
+"""
+
+ADD_SNAPSHOT_MODE_V2 = """
+ALTER TABLE {table_name}
+ADD COLUMN IF NOT EXISTS snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
+"""
+
+operations = [
+ # Validate every replica before DDL; replicated ALTERs only run on one host per shard.
+ run_sql_with_exceptions(
+ CHECK_REPLAY_SCHEMA.format(table_name="sharded_session_replay_events"),
+ node_roles=[NodeRole.DATA],
+ require_hosts=True,
+ ),
+ run_sql_with_exceptions(
+ CHECK_REPLAY_SCHEMA.format(table_name="writable_session_replay_events"),
+ node_roles=[NodeRole.DATA, NodeRole.INGESTION_SMALL],
+ require_hosts=True,
+ ),
+ run_sql_with_exceptions(
+ CHECK_REPLAY_SCHEMA.format(table_name="session_replay_events"),
+ node_roles=[NodeRole.DATA],
+ require_hosts=True,
+ ),
+ run_sql_with_exceptions(
+ ADD_SNAPSHOT_MODE_V2.format(table_name="sharded_session_replay_events"),
+ node_roles=[NodeRole.DATA],
+ sharded=True,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ ADD_SNAPSHOT_MODE_V2.format(table_name="writable_session_replay_events"),
+ node_roles=[NodeRole.DATA, NodeRole.INGESTION_SMALL],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+ run_sql_with_exceptions(
+ ADD_SNAPSHOT_MODE_V2.format(table_name="session_replay_events"),
+ node_roles=[NodeRole.DATA],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+ run_sql_with_exceptions(
+ DROP_SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
+ ),
+ run_sql_with_exceptions(DROP_SESSION_REPLAY_EVENTS_WS_MV_SQL, node_roles=[NodeRole.INGESTION_SMALL]),
+ *(
+ [run_sql_with_exceptions(SESSION_REPLAY_EVENTS_WS_MV_SQL(), node_roles=[NodeRole.INGESTION_SMALL])]
+ if run_mode().is_deployed_cloud
+ else [
+ run_sql_with_exceptions(
+ SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
+ )
+ ]
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 5cd3683c56b6..481e560876b9 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0321_drop_metrics1_materialized_views
+0322_add_snapshot_mode_v2_to_session_replay_events
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 54b8e67a040f..25d88c87de9e 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -6323,10 +6323,10 @@
-- because we batch events we expect message_count to be lower than event_count
event_count SimpleAggregateFunction(sum, Int64),
-- which source the snapshots came from Mobile or Web. Web if absent
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
-- knowing something is mobile isn't enough, we need to know if e.g. RN or flutter
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
-- retention period for this session, in days. Useful to show TTL for the recording
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
@@ -6362,7 +6362,7 @@
`console_log_count` Int64, `console_warn_count` Int64,
`console_error_count` Int64, `size` Int64, `message_count` Int64,
`event_count` Int64,
- `snapshot_source` AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ `snapshot_source` AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
`snapshot_library` AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
`_timestamp` Nullable(DateTime)
,`retention_period_days` SimpleAggregateFunction(max, Nullable(Int64))
@@ -6371,7 +6371,7 @@
,`ai_tags_freeform` SimpleAggregateFunction(groupUniqArrayArray, Array(String))
,`ai_highlighted` SimpleAggregateFunction(max, UInt8)
,`surfacing_score` SimpleAggregateFunction(max, Nullable(Float32))
- ,`snapshot_mode` AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ ,`snapshot_mode_v2` AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
)
AS SELECT
session_id,
@@ -6404,7 +6404,7 @@
-- we can count the number of kafka messages instead of sending it explicitly
sum(message_count) as message_count,
sum(event_count) as event_count,
- argMinState(snapshot_source, first_timestamp) as snapshot_source,
+ argMinState(replay.snapshot_source, first_timestamp) as snapshot_source,
argMinState(snapshot_library, first_timestamp) as snapshot_library,
max(_timestamp) as _timestamp
,max(retention_period_days) as retention_period_days
@@ -6413,8 +6413,8 @@
,groupUniqArrayArray(ai_tags_freeform) as ai_tags_freeform
,max(ai_highlighted) as ai_highlighted
,max(surfacing_score) as surfacing_score
- ,argMinState(snapshot_mode, first_timestamp) as snapshot_mode
- FROM posthog_test.kafka_session_replay_events
+ ,argMinState(replay.snapshot_mode, first_timestamp) as snapshot_mode_v2
+ FROM posthog_test.kafka_session_replay_events AS replay
group by session_id, team_id
'''
@@ -8168,10 +8168,10 @@
-- because we batch events we expect message_count to be lower than event_count
event_count SimpleAggregateFunction(sum, Int64),
-- which source the snapshots came from Mobile or Web. Web if absent
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
-- knowing something is mobile isn't enough, we need to know if e.g. RN or flutter
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
-- retention period for this session, in days. Useful to show TTL for the recording
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
@@ -10271,10 +10271,10 @@
-- because we batch events we expect message_count to be lower than event_count
event_count SimpleAggregateFunction(sum, Int64),
-- which source the snapshots came from Mobile or Web. Web if absent
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
-- knowing something is mobile isn't enough, we need to know if e.g. RN or flutter
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
-- retention period for this session, in days. Useful to show TTL for the recording
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
@@ -13400,10 +13400,10 @@
-- because we batch events we expect message_count to be lower than event_count
event_count SimpleAggregateFunction(sum, Int64),
-- which source the snapshots came from Mobile or Web. Web if absent
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
-- knowing something is mobile isn't enough, we need to know if e.g. RN or flutter
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
-- retention period for this session, in days. Useful to show TTL for the recording
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
@@ -14298,10 +14298,10 @@
-- because we batch events we expect message_count to be lower than event_count
event_count SimpleAggregateFunction(sum, Int64),
-- which source the snapshots came from Mobile or Web. Web if absent
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
-- knowing something is mobile isn't enough, we need to know if e.g. RN or flutter
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
-- retention period for this session, in days. Useful to show TTL for the recording
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
diff --git a/posthog/clickhouse/test/test_session_replay_mode.py b/posthog/clickhouse/test/test_session_replay_mode.py
new file mode 100644
index 000000000000..c23d65f7ceb3
--- /dev/null
+++ b/posthog/clickhouse/test/test_session_replay_mode.py
@@ -0,0 +1,168 @@
+import re
+from collections.abc import Callable
+from importlib import import_module
+from uuid import uuid4
+
+import pytest
+
+from django.conf import settings
+
+from clickhouse_driver.errors import ServerException
+
+from posthog.clickhouse.client import sync_execute
+from posthog.session_recordings.sql.session_replay_event_sql import (
+ KAFKA_SESSION_REPLAY_EVENTS_TABLE_BASE_SQL,
+ SESSION_REPLAY_EVENTS_TABLE_BASE_SQL,
+ SESSION_REPLAY_EVENTS_TABLE_MV_SQL,
+ SESSION_REPLAY_EVENTS_WS_MV_SQL,
+)
+
+
[email protected]("mv_sql", [SESSION_REPLAY_EVENTS_TABLE_MV_SQL, SESSION_REPLAY_EVENTS_WS_MV_SQL])
+def test_replay_metadata_reads_old_parts_and_classified_blocks(mv_sql: Callable[..., str]) -> None:
+ suffix = uuid4().hex
+ source = f"replay_mode_source_{suffix}"
+ target = f"replay_mode_target_{suffix}"
+ view = f"replay_mode_mv_{suffix}"
+ database = settings.CLICKHOUSE_DATABASE
+ migration = import_module("posthog.clickhouse.migrations.0322_add_snapshot_mode_v2_to_session_replay_events")
+
+ def create_view(*, legacy: bool) -> None:
+ query = mv_sql(on_cluster=False, exclude_columns=["snapshot_mode_v2"] if legacy else [])
+ for original in ("session_replay_events_ws_mv", "session_replay_events_mv"):
+ query = query.replace(original, view)
+ for original in ("kafka_session_replay_events_ws", "kafka_session_replay_events"):
+ query = query.replace(original, source)
+ sync_execute(query.replace("writable_session_replay_events", target))
+
+ def insert_block(session_id: str, mode: str | None, timestamp: str) -> None:
+ sync_execute(
+ f"""
+ INSERT INTO {database}.{source}
+ (session_id, team_id, distinct_id, first_timestamp, last_timestamp, snapshot_source, snapshot_mode)
+ VALUES (%(session_id)s, 1, 'test-user', %(timestamp)s, %(timestamp)s, 'mobile', %(mode)s)
+ """,
+ {"session_id": session_id, "timestamp": timestamp, "mode": mode},
+ )
+
+ try:
+ sync_execute(
+ KAFKA_SESSION_REPLAY_EVENTS_TABLE_BASE_SQL.replace(
+ ") ENGINE = {engine}", "_timestamp DateTime\n) ENGINE = {engine}"
+ ).format(table_name=f"{database}.{source}", on_cluster_clause="", engine="Memory")
+ )
+ sync_execute(
+ SESSION_REPLAY_EVENTS_TABLE_BASE_SQL.replace(
+ " snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),\n", ""
+ ).format(
+ table_name=f"{database}.{target}",
+ on_cluster_clause="",
+ engine="AggregatingMergeTree ORDER BY (team_id, session_id, toDate(min_first_timestamp))",
+ )
+ )
+ create_view(legacy=True)
+ insert_block("historical", "screenshot", "2026-01-01 00:00:00")
+ sync_execute(f"DROP VIEW IF EXISTS {database}.{view}")
+ for _ in range(2):
+ sync_execute(migration.ADD_SNAPSHOT_MODE_V2.format(table_name=f"{database}.{target}"))
+ create_view(legacy=False)
+
+ insert_block("screenshots", None, "2026-01-02 00:00:00")
+ insert_block("screenshots", "screenshot", "2026-01-03 00:00:00")
+ insert_block("screenshots", None, "2026-01-04 00:00:00")
+ insert_block("wireframes", "wireframe", "2026-01-02 00:00:00")
+ insert_block("unknown", None, "2026-01-02 00:00:00")
+
+ query = f"""
+ SELECT session_id, argMinMerge(snapshot_source), argMinMerge(snapshot_mode_v2)
+ FROM {database}.{target}
+ GROUP BY session_id ORDER BY session_id
+ """
+ expected = [
+ ("historical", "mobile", None),
+ ("screenshots", "mobile", "screenshot"),
+ ("unknown", "mobile", None),
+ ("wireframes", "mobile", "wireframe"),
+ ]
+ assert sync_execute(query) == expected
+ sync_execute(f"OPTIMIZE TABLE {database}.{target} FINAL")
+ assert sync_execute(query) == expected
+ finally:
+ sync_execute(f"DROP VIEW IF EXISTS {database}.{view}")
+ sync_execute(f"DROP TABLE IF EXISTS {database}.{source}")
+ sync_execute(f"DROP TABLE IF EXISTS {database}.{target}")
+
+
+ "unprepared_table", ["sharded_session_replay_events", "writable_session_replay_events", "session_replay_events"]
+)
[email protected]("problem", ["source_type", "legacy_mode"])
+def test_mode_migration_rejects_unprepared_schema_before_changing_tables(unprepared_table: str, problem: str) -> None:
+ migration = import_module("posthog.clickhouse.migrations.0322_add_snapshot_mode_v2_to_session_replay_events")
+ suffix = uuid4().hex
+ tables = ["sharded_session_replay_events", "writable_session_replay_events", "session_replay_events"]
+ views = ["session_replay_events_mv", "session_replay_events_ws_mv"]
+ source = "kafka_session_replay_events"
+ names = {name: f"{name}_{suffix}" for name in [*tables, *views, source]}
+ name_pattern = re.compile(r"\b(" + "|".join(names) + r")\b")
+ source_column = " snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),\n"
+ mode_column = " snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),\n"
+ prepared_schema = SESSION_REPLAY_EVENTS_TABLE_BASE_SQL.replace(mode_column, "")
+
+ def create_target(table: str, *, prepared: bool) -> None:
+ schema = prepared_schema
+ if not prepared:
+ if problem == "source_type":
+ schema = schema.replace(
+ source_column, source_column.replace("Nullable(String)", "LowCardinality(Nullable(String))")
+ )
+ else:
+ schema = schema.replace(
+ source_column, source_column + mode_column.replace("snapshot_mode_v2", "snapshot_mode")
+ )
+ sync_execute(schema.format(table_name=names[table], on_cluster_clause="", engine="Memory"))
+
+ def run_migration() -> None:
+ for operation in migration.operations:
+ sync_execute(name_pattern.sub(lambda match: names[match[0]], operation._sql))
+
+ try:
+ for table in tables:
+ create_target(table, prepared=table != unprepared_table)
+ sync_execute(
+ KAFKA_SESSION_REPLAY_EVENTS_TABLE_BASE_SQL.replace(
+ ") ENGINE = {engine}", "_timestamp DateTime\n) ENGINE = {engine}"
+ ).format(table_name=names[source], on_cluster_clause="", engine="Memory")
+ )
+ for view in views:
+ sync_execute(f"CREATE VIEW {names[view]} AS SELECT 1")
+
+ with pytest.raises(ServerException, match="Replay metadata schema is not ready"):
+ run_migration()
+
+ assert sync_execute(
+ "SELECT count() FROM system.columns WHERE database = currentDatabase() "
+ "AND table IN %(tables)s AND name = 'snapshot_mode_v2'",
+ {"tables": tuple(names[table] for table in tables)},
+ ) == [(0,)]
+ assert sync_execute(
+ "SELECT count() FROM system.tables WHERE database = currentDatabase() "
+ "AND name IN %(views)s AND engine = 'View'",
+ {"views": tuple(names[view] for view in views)},
+ ) == [(2,)]
+
+ sync_execute(f"DROP TABLE {names[unprepared_table]}")
+ create_target(unprepared_table, prepared=True)
+ run_migration()
+ assert sync_execute(
+ "SELECT count() FROM system.columns WHERE database = currentDatabase() "
+ "AND table IN %(tables)s AND name = 'snapshot_mode_v2'",
+ {"tables": tuple(names[table] for table in tables)},
+ ) == [(3,)]
+ assert sync_execute(
+ "SELECT engine FROM system.tables WHERE database = currentDatabase() AND name = %(view)s",
+ {"view": names[views[0]]},
+ ) == [("MaterializedView",)]
+ finally:
+ for name in [*views, source, *tables]:
+ sync_execute(f"DROP TABLE IF EXISTS {names[name]}")
diff --git a/posthog/session_recordings/queries/test/session_replay_sql.py b/posthog/session_recordings/queries/test/session_replay_sql.py
index 07e20f8a3232..54cffccaba51 100644
--- a/posthog/session_recordings/queries/test/session_replay_sql.py
+++ b/posthog/session_recordings/queries/test/session_replay_sql.py
@@ -31,7 +31,7 @@ INSERT INTO sharded_session_replay_events (
console_error_count,
snapshot_source,
snapshot_library,
- snapshot_mode,
+ snapshot_mode_v2,
size,
block_urls,
block_first_timestamps,
@@ -56,9 +56,9 @@ SELECT
%(console_log_count)s,
%(console_warn_count)s,
%(console_error_count)s,
- argMinState(cast(%(snapshot_source)s, 'LowCardinality(Nullable(String))'), toDateTime64(%(first_timestamp)s, 6, 'UTC')),
- argMinState(cast(%(snapshot_library)s, 'LowCardinality(Nullable(String))'), toDateTime64(%(first_timestamp)s, 6, 'UTC')),
- argMinState(cast(NULL, 'LowCardinality(Nullable(String))'), toDateTime64(%(first_timestamp)s, 6, 'UTC')),
+ argMinState(cast(%(snapshot_source)s, 'Nullable(String)'), toDateTime64(%(first_timestamp)s, 6, 'UTC')),
+ argMinState(cast(%(snapshot_library)s, 'Nullable(String)'), toDateTime64(%(first_timestamp)s, 6, 'UTC')),
+ argMinState(cast(NULL, 'Nullable(String)'), toDateTime64(%(first_timestamp)s, 6, 'UTC')),
%(size)s,
%(block_urls)s,
%(block_first_timestamps)s,
diff --git a/posthog/session_recordings/sql/session_replay_event_migrations_sql.py b/posthog/session_recordings/sql/session_replay_event_migrations_sql.py
index f6ed5bb67dcd..b44c6b9fa3bc 100644
--- a/posthog/session_recordings/sql/session_replay_event_migrations_sql.py
+++ b/posthog/session_recordings/sql/session_replay_event_migrations_sql.py
@@ -96,7 +96,7 @@ ADD_EVENT_COUNT_SESSION_REPLAY_EVENTS_TABLE_SQL = lambda: ALTER_SESSION_REPLAY_A
# migration to add source column to the session replay table
ALTER_SESSION_REPLAY_ADD_SOURCE_COLUMN = """
ALTER TABLE {table_name} on CLUSTER '{cluster}'
- ADD COLUMN IF NOT EXISTS snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))
+ ADD COLUMN IF NOT EXISTS snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))
"""
ADD_SOURCE_DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL = lambda: ALTER_SESSION_REPLAY_ADD_SOURCE_COLUMN.format(
diff --git a/posthog/session_recordings/sql/session_replay_event_sql.py b/posthog/session_recordings/sql/session_replay_event_sql.py
index 4070d3e80f63..cdb2fa0268f2 100644
--- a/posthog/session_recordings/sql/session_replay_event_sql.py
+++ b/posthog/session_recordings/sql/session_replay_event_sql.py
@@ -93,10 +93,10 @@ CREATE TABLE IF NOT EXISTS {table_name} {on_cluster_clause}
-- because we batch events we expect message_count to be lower than event_count
event_count SimpleAggregateFunction(sum, Int64),
-- which source the snapshots came from Mobile or Web. Web if absent
- snapshot_source AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_source AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
-- knowing something is mobile isn't enough, we need to know if e.g. RN or flutter
snapshot_library AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
- snapshot_mode AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+ snapshot_mode_v2 AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
_timestamp SimpleAggregateFunction(max, DateTime),
-- retention period for this session, in days. Useful to show TTL for the recording
retention_period_days SimpleAggregateFunction(max, Nullable(Int64)),
@@ -151,16 +151,15 @@ def KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL(on_cluster=True):
)
-def SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=True, exclude_columns=None):
+def _session_replay_events_mv_sql(
+ *, view_name: str, source_table: str, on_cluster: bool, exclude_columns: list[str] | None
+) -> str:
exclude_columns = exclude_columns or []
target_table = "writable_session_replay_events"
on_cluster_clause = ON_CLUSTER_CLAUSE(on_cluster)
database = settings.CLICKHOUSE_DATABASE
- # ClickHouse is incorrectly expanding the type of the snapshot source column
- # Despite it being a LowCardinality(Nullable(String)) in writable_session_replay_events
- # The column expansion picks only Nullable(String) and so we can't select it
explictly_specify_columns = f"""(
`session_id` String, `team_id` Int64, `distinct_id` String,
`min_first_timestamp` DateTime64(6, 'UTC'),
@@ -175,7 +174,7 @@ def SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=True, exclude_columns=None):
`console_log_count` Int64, `console_warn_count` Int64,
`console_error_count` Int64, `size` Int64, `message_count` Int64,
`event_count` Int64,
-`snapshot_source` AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
+`snapshot_source` AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
`snapshot_library` AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
`_timestamp` Nullable(DateTime)
{",`retention_period_days` SimpleAggregateFunction(max, Nullable(Int64))" if "retention_period_days" not in exclude_columns else ""}
@@ -184,11 +183,11 @@ def SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=True, exclude_columns=None):
{",`ai_tags_freeform` SimpleAggregateFunction(groupUniqArrayArray, Array(String))" if "ai_tags_freeform" not in exclude_columns else ""}
{",`ai_highlighted` SimpleAggregateFunction(max, UInt8)" if "ai_highlighted" not in exclude_columns else ""}
{",`surfacing_score` SimpleAggregateFunction(max, Nullable(Float32))" if "surfacing_score" not in exclude_columns else ""}
-{",`snapshot_mode` AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))" if "snapshot_mode" not in exclude_columns else ""}
+{",`snapshot_mode_v2` AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC'))" if "snapshot_mode_v2" not in exclude_columns else ""}
)"""
return f"""
-CREATE MATERIALIZED VIEW IF NOT EXISTS session_replay_events_mv {on_cluster_clause}
+CREATE MATERIALIZED VIEW IF NOT EXISTS {view_name} {on_cluster_clause}
TO {database}.{target_table} {explictly_specify_columns}
AS SELECT
session_id,
@@ -221,7 +220,7 @@ sum(size) as size,
-- we can count the number of kafka messages instead of sending it explicitly
sum(message_count) as message_count,
sum(event_count) as event_count,
-argMinState(snapshot_source, first_timestamp) as snapshot_source,
+argMinState(replay.snapshot_source, first_timestamp) as snapshot_source,
argMinState(snapshot_library, first_timestamp) as snapshot_library,
max(_timestamp) as _timestamp
{",max(retention_period_days) as retention_period_days" if "retention_period_days" not in exclude_columns else ""}
@@ -230,12 +229,21 @@ max(_timestamp) as _timestamp
{",groupUniqArrayArray(ai_tags_freeform) as ai_tags_freeform" if "ai_tags_freeform" not in exclude_columns else ""}
{",max(ai_highlighted) as ai_highlighted" if "ai_highlighted" not in exclude_columns else ""}
{",max(surfacing_score) as surfacing_score" if "surfacing_score" not in exclude_columns else ""}
-{",argMinState(snapshot_mode, first_timestamp) as snapshot_mode" if "snapshot_mode" not in exclude_columns else ""}
-FROM {database}.kafka_session_replay_events
+{",argMinState(replay.snapshot_mode, first_timestamp) as snapshot_mode_v2" if "snapshot_mode_v2" not in exclude_columns else ""}
+FROM {database}.{source_table} AS replay
group by session_id, team_id
"""
+def SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster: bool = True, exclude_columns: list[str] | None = None) -> str:
+ return _session_replay_events_mv_sql(
+ view_name="session_replay_events_mv",
+ source_table="kafka_session_replay_events",
+ on_cluster=on_cluster,
+ exclude_columns=exclude_columns,
+ )
+
+
# Distributed engine tables are only created if CLICKHOUSE_REPLICATED
# This table is responsible for writing to sharded_session_replay_events based on a sharding key.
@@ -303,73 +311,10 @@ def KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE_SQL(on_cluster=False):
)
-def SESSION_REPLAY_EVENTS_WS_MV_SQL(on_cluster=False, exclude_columns=None):
- exclude_columns = exclude_columns or []
-
- target_table = "writable_session_replay_events"
- on_cluster_clause = ON_CLUSTER_CLAUSE(on_cluster)
- database = settings.CLICKHOUSE_DATABASE
-
- explictly_specify_columns = f"""(
-`session_id` String, `team_id` Int64, `distinct_id` String,
-`min_first_timestamp` DateTime64(6, 'UTC'),
-`max_last_timestamp` DateTime64(6, 'UTC'),
-`block_first_timestamps` SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
-`block_last_timestamps` SimpleAggregateFunction(groupArrayArray, Array(DateTime64(6, 'UTC'))),
-`block_urls` SimpleAggregateFunction(groupArrayArray, Array(String)),
-`first_url` AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
-`all_urls` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
-`click_count` Int64, `keypress_count` Int64,
-`mouse_activity_count` Int64, `active_milliseconds` Int64,
-`console_log_count` Int64, `console_warn_count` Int64,
-`console_error_count` Int64, `size` Int64, `message_count` Int64,
-`event_count` Int64,
-`snapshot_source` AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC')),
-`snapshot_library` AggregateFunction(argMin, Nullable(String), DateTime64(6, 'UTC')),
-`_timestamp` Nullable(DateTime)
-{",`retention_period_days` SimpleAggregateFunction(max, Nullable(Int64))" if "retention_period_days" not in exclude_columns else ""}
-{",`is_deleted` SimpleAggregateFunction(max, UInt8)" if "is_deleted" not in exclude_columns else ""}
-{",`ai_tags_fixed` SimpleAggregateFunction(groupUniqArrayArray, Array(String))" if "ai_tags_fixed" not in exclude_columns else ""}
-{",`ai_tags_freeform` SimpleAggregateFunction(groupUniqArrayArray, Array(String))" if "ai_tags_freeform" not in exclude_columns else ""}
-{",`ai_highlighted` SimpleAggregateFunction(max, UInt8)" if "ai_highlighted" not in exclude_columns else ""}
-{",`surfacing_score` SimpleAggregateFunction(max, Nullable(Float32))" if "surfacing_score" not in exclude_columns else ""}
-{",`snapshot_mode` AggregateFunction(argMin, LowCardinality(Nullable(String)), DateTime64(6, 'UTC'))" if "snapshot_mode" not in exclude_columns else ""}
-)"""
-
- return f"""
-CREATE MATERIALIZED VIEW IF NOT EXISTS {SESSION_REPLAY_EVENTS_WS_MV} {on_cluster_clause}
-TO {database}.{target_table} {explictly_specify_columns}
-AS SELECT
-session_id,
-team_id,
-any(distinct_id) as distinct_id,
-min(first_timestamp) AS min_first_timestamp,
-max(last_timestamp) AS max_last_timestamp,
-groupArray(if(block_url != '', first_timestamp, NULL)) AS block_first_timestamps,
-groupArray(if(block_url != '', last_timestamp, NULL)) AS block_last_timestamps,
-groupArray(block_url) AS block_urls,
-argMinState(first_url, first_timestamp) as first_url,
-groupUniqArrayArray(urls) as all_urls,
-sum(click_count) as click_count,
-sum(keypress_count) as keypress_count,
-sum(mouse_activity_count) as mouse_activity_count,
-sum(active_milliseconds) as active_milliseconds,
-sum(console_log_count) as console_log_count,
-sum(console_warn_count) as console_warn_count,
-sum(console_error_count) as console_error_count,
-sum(size) as size,
-sum(message_count) as message_count,
-sum(event_count) as event_count,
-argMinState(snapshot_source, first_timestamp) as snapshot_source,
-argMinState(snapshot_library, first_timestamp) as snapshot_library,
-max(_timestamp) as _timestamp
-{",max(retention_period_days) as retention_period_days" if "retention_period_days" not in exclude_columns else ""}
-{",max(is_deleted) as is_deleted" if "is_deleted" not in exclude_columns else ""}
-{",groupUniqArrayArray(ai_tags_fixed) as ai_tags_fixed" if "ai_tags_fixed" not in exclude_columns else ""}
-{",groupUniqArrayArray(ai_tags_freeform) as ai_tags_freeform" if "ai_tags_freeform" not in exclude_columns else ""}
-{",max(ai_highlighted) as ai_highlighted" if "ai_highlighted" not in exclude_columns else ""}
-{",max(surfacing_score) as surfacing_score" if "surfacing_score" not in exclude_columns else ""}
-{",argMinState(snapshot_mode, first_timestamp) as snapshot_mode" if "snapshot_mode" not in exclude_columns else ""}
-FROM {database}.{KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE}
-group by session_id, team_id
-"""
+def SESSION_REPLAY_EVENTS_WS_MV_SQL(on_cluster: bool = False, exclude_columns: list[str] | None = None) -> str:
+ return _session_replay_events_mv_sql(
+ view_name=SESSION_REPLAY_EVENTS_WS_MV,
+ source_table=KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE,
+ on_cluster=on_cluster,
+ exclude_columns=exclude_columns,
+ )
diff --git a/posthog/temporal/tests/session_replay/surfacing_scoring_sweep/ch_insert_helpers.py b/posthog/temporal/tests/session_replay/surfacing_scoring_sweep/ch_insert_helpers.py
index d1601a7122c5..c39b594e1f49 100644
--- a/posthog/temporal/tests/session_replay/surfacing_scoring_sweep/ch_insert_helpers.py
+++ b/posthog/temporal/tests/session_replay/surfacing_scoring_sweep/ch_insert_helpers.py
@@ -1,8 +1,4 @@
-"""CH insert helpers for surfacing_scoring_sweep integration tests.
-
-Partial inserts into writable_session_replay_events fail on argMin LC/String
-type mismatches — use sharded_session_replay_events with argMinState instead.
-"""
+"""CH insert helpers for surfacing_scoring_sweep integration tests."""
from __future__ import annotations
@@ -21,7 +17,7 @@ INSERT INTO sharded_session_replay_events (
first_url,
snapshot_source,
snapshot_library,
- snapshot_mode,
+ snapshot_mode_v2,
event_count,
surfacing_score
)
@@ -32,9 +28,9 @@ SELECT
now64(6) - INTERVAL 1 HOUR,
now64(6),
argMinState(cast(NULL, 'Nullable(String)'), now64(6) - INTERVAL 1 HOUR),
- argMinState(cast(NULL, 'LowCardinality(Nullable(String))'), now64(6) - INTERVAL 1 HOUR),
argMinState(cast(NULL, 'Nullable(String)'), now64(6) - INTERVAL 1 HOUR),
- argMinState(cast(NULL, 'LowCardinality(Nullable(String))'), now64(6) - INTERVAL 1 HOUR),
+ argMinState(cast(NULL, 'Nullable(String)'), now64(6) - INTERVAL 1 HOUR),
+ argMinState(cast(NULL, 'Nullable(String)'), now64(6) - INTERVAL 1 HOUR),
%(event_count)s,
%(surfacing_score)s
"""
@@ -49,7 +45,7 @@ INSERT INTO sharded_session_replay_events (
first_url,
snapshot_source,
snapshot_library,
- snapshot_mode,
+ snapshot_mode_v2,
event_count,
surfacing_score
)
@@ -60,9 +56,9 @@ SELECT
toDateTime64(%(start)s, 6, 'UTC'),
toDateTime64(%(end)s, 6, 'UTC'),
argMinState(cast(NULL, 'Nullable(String)'), toDateTime64(%(start)s, 6, 'UTC')),
- argMinState(cast(NULL, 'LowCardinality(Nullable(String))'), toDateTime64(%(start)s, 6, 'UTC')),
argMinState(cast(NULL, 'Nullable(String)'), toDateTime64(%(start)s, 6, 'UTC')),
- argMinState(cast(NULL, 'LowCardinality(Nullable(String))'), toDateTime64(%(start)s, 6, 'UTC')),
+ argMinState(cast(NULL, 'Nullable(String)'), toDateTime64(%(start)s, 6, 'UTC')),
+ argMinState(cast(NULL, 'Nullable(String)'), toDateTime64(%(start)s, 6, 'UTC')),
%(event_count)s,
%(surfacing_score)s
"""