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.

perf(clickhouse): put the $session_id bloom filter on the bare column (#94535)

Andy Zhao360228b4ed20
diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
index 544a7a618e5c..996d8d8390c5 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
@@ -5496,8 +5496,8 @@ database "posthog" {
granularity = 1
}
index "bloom_filter_$session_id" {
- expr = "nullIf(nullIf(`$session_id`, ''), 'null')"
- type = "bloom_filter"
+ expr = "`$session_id`"
+ type = "bloom_filter(0.01)"
granularity = 1
}
index "minmax_$group_0" {
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 4795d1fb89ee..33e05ced5a1f 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -9883,8 +9883,8 @@ SQL
granularity = 1
}
index "bloom_filter_$session_id" {
- expr = "nullIf(nullIf(`$session_id`, ''), 'null')"
- type = "bloom_filter"
+ expr = "`$session_id`"
+ type = "bloom_filter(0.01)"
granularity = 1
}
index "minmax_$group_0" {
diff --git a/posthog/clickhouse/hcl/roles/data/shared/tables.hcl b/posthog/clickhouse/hcl/roles/data/shared/tables.hcl
index c4787ca2f3c8..28432650b3dd 100644
--- a/posthog/clickhouse/hcl/roles/data/shared/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/data/shared/tables.hcl
@@ -211,8 +211,8 @@ database "posthog" {
granularity = 1
}
index "bloom_filter_$session_id" {
- expr = "nullIf(nullIf(`$session_id`, ''), 'null')"
- type = "bloom_filter"
+ expr = "`$session_id`"
+ type = "bloom_filter(0.01)"
granularity = 1
}
engine "replicated_replacing_merge_tree" {
diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sql
index b925c1afdf0a..8d178dd20276 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql
@@ -1027,7 +1027,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 bloom_filter_$session_id `$session_id` TYPE bloom_filter(0.01) 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/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 65c66268443a..1ae5c9a64d98 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -1774,7 +1774,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 bloom_filter_$session_id `$session_id` TYPE bloom_filter(0.01) 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/0314_session_id_bloom_filter_on_bare_column.py b/posthog/clickhouse/migrations/0314_session_id_bloom_filter_on_bare_column.py
new file mode 100644
index 000000000000..277627a0c64d
--- /dev/null
+++ b/posthog/clickhouse/migrations/0314_session_id_bloom_filter_on_bare_column.py
@@ -0,0 +1,23 @@
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+
+DROP_EXPRESSION_BLOOM_FILTER_INDEX = "ALTER TABLE sharded_events DROP INDEX IF EXISTS `bloom_filter_$session_id`"
+
+ADD_BARE_COLUMN_BLOOM_FILTER_INDEX = """
+ALTER TABLE sharded_events
+ADD INDEX IF NOT EXISTS `bloom_filter_$session_id` `$session_id`
+TYPE bloom_filter(0.01)
+GRANULARITY 1
+"""
+
+operations = [
+ run_sql_with_exceptions(
+ DROP_EXPRESSION_BLOOM_FILTER_INDEX,
+ sharded=True,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ ADD_BARE_COLUMN_BLOOM_FILTER_INDEX,
+ 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 e86622ea942d..12f07a5bdbdc 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0313_logs_pattern_buckets
+0314_session_id_bloom_filter_on_bare_column
diff --git a/posthog/clickhouse/test/test_0314.py b/posthog/clickhouse/test/test_0314.py
new file mode 100644
index 000000000000..ad8aea0d9038
--- /dev/null
+++ b/posthog/clickhouse/test/test_0314.py
@@ -0,0 +1,55 @@
+import importlib
+
+from posthog.test.base import ClickhouseDestroyTablesMixin, _create_event, flush_persons_and_events
+
+from parameterized import parameterized
+
+from posthog.hogql.test.test_property_skip_indexes import _run_explain_and_get_skip_indexes
+
+from posthog.clickhouse.client import sync_execute
+
+module = importlib.import_module("posthog.clickhouse.migrations.0314_session_id_bloom_filter_on_bare_column")
+
+INDEX_FROM_0293 = """
+ALTER TABLE sharded_events
+ADD INDEX IF NOT EXISTS `bloom_filter_$session_id` nullIf(nullIf(`$session_id`, ''), 'null')
+TYPE bloom_filter GRANULARITY 1
+"""
+SESSION_ID = "019f9a50-0000-7000-8000-000000000001"
+
+
+def _apply_migration() -> None:
+ sync_execute(module.DROP_EXPRESSION_BLOOM_FILTER_INDEX)
+ sync_execute(module.ADD_BARE_COLUMN_BLOOM_FILTER_INDEX)
+
+
+def _session_id_bloom_filter_indexes() -> list[tuple[str, str]]:
+ return sync_execute(
+ "SELECT name, expr FROM system.data_skipping_indices "
+ "WHERE database = currentDatabase() AND table = 'sharded_events' AND type = 'bloom_filter' "
+ "AND position(expr, '$session_id') > 0"
+ )
+
+
+class Test0314(ClickhouseDestroyTablesMixin):
+ def test_replaces_the_expression_index_with_one_on_the_bare_column_and_can_be_reapplied(self):
+ sync_execute(INDEX_FROM_0293)
+ _apply_migration()
+ _apply_migration()
+ assert _session_id_bloom_filter_indexes() == [("bloom_filter_$session_id", "`$session_id`")]
+
+ @parameterized.expand(
+ [
+ ("has", "has(%(session_ids)s, `$session_id`)"),
+ ("equals", "equals(`$session_id`, %(session_id)s)"),
+ ]
+ )
+ def test_bare_column_index_engages_under_default_hogql_settings(self, _name: str, predicate: str):
+ _apply_migration()
+ _create_event(team=self.team, distinct_id="d", event="$pageview", properties={"$session_id": SESSION_ID})
+ flush_persons_and_events()
+ values = {"team_id": self.team.pk, "session_ids": [SESSION_ID], "session_id": SESSION_ID}
+ skip_indexes = _run_explain_and_get_skip_indexes(
+ f"SELECT count() FROM events WHERE team_id = %(team_id)s AND {predicate}", values
+ )
+ assert "bloom_filter_$session_id" in skip_indexes