Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(clickhouse): add expression bloom filter for $session_id (#74973)

Andy Zhaoc4f7a0e1cf99
diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
index 09b2695764ee..2b5157e573c4 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
@@ -5281,6 +5281,11 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ index "bloom_filter_$session_id" {
+ expr = "nullIf(nullIf(`$session_id`, ''), 'null')"
+ type = "bloom_filter"
+ granularity = 1
+ }
index "minmax_$group_0" {
expr = "`$group_0`"
type = "minmax"
diff --git a/posthog/clickhouse/hcl/roles/data/shared/tables.hcl b/posthog/clickhouse/hcl/roles/data/shared/tables.hcl
index 3e084138f40f..ce2a4ea548ad 100644
--- a/posthog/clickhouse/hcl/roles/data/shared/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/data/shared/tables.hcl
@@ -340,6 +340,11 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ index "bloom_filter_$session_id" {
+ expr = "nullIf(nullIf(`$session_id`, ''), 'null')"
+ type = "bloom_filter"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.events"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sql
index 461b3940b6a2..a56f32ad1b5a 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql
@@ -983,6 +983,7 @@ CREATE TABLE posthog.sharded_events (
INDEX bloom_filter_$ai_experiment_id `mat_$ai_experiment_id` TYPE bloom_filter GRANULARITY 1,
INDEX minmax_$ai_experiment_id `mat_$ai_experiment_id` TYPE minmax GRANULARITY 1,
INDEX minmax_$session_id_uuid `$session_id_uuid` TYPE minmax GRANULARITY 1,
+ INDEX bloom_filter_$session_id nullIf(nullIf(`$session_id`, ''), 'null') TYPE bloom_filter GRANULARITY 1,
INDEX minmax_$group_0 `$group_0` TYPE minmax GRANULARITY 1,
INDEX minmax_$group_1 `$group_1` TYPE minmax GRANULARITY 1,
INDEX minmax_$group_2 `$group_2` TYPE minmax GRANULARITY 1,
diff --git a/posthog/clickhouse/migrations/0293_add_session_id_bloom_filter_index.py b/posthog/clickhouse/migrations/0293_add_session_id_bloom_filter_index.py
new file mode 100644
index 000000000000..cb302b9d9184
--- /dev/null
+++ b/posthog/clickhouse/migrations/0293_add_session_id_bloom_filter_index.py
@@ -0,0 +1,19 @@
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+
+# HogQL reads the $session_id property as nullIf(nullIf(`$session_id`, ''), 'null'), and skip-index analysis only
+# engages when the index expression matches that operand exactly, so the index is on the expression rather than the
+# bare column (which stays invisible through the wrapper).
+ADD_BLOOM_FILTER_INDEX_SHARDED_EVENTS = """
+ALTER TABLE sharded_events
+ADD INDEX IF NOT EXISTS `bloom_filter_$session_id` nullIf(nullIf(`$session_id`, ''), 'null')
+TYPE bloom_filter
+GRANULARITY 1
+"""
+
+operations = [
+ run_sql_with_exceptions(
+ ADD_BLOOM_FILTER_INDEX_SHARDED_EVENTS,
+ sharded=True,
+ is_alter_on_replicated_table=True,
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 2ad3f6b92359..13e3feef566a 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0292_flag_evaluations
+0293_add_session_id_bloom_filter_index