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(property-vals): swap text index for ngrambf_v1 on property_value (#59922)

Andy Zhao4ebf91ec2e0f
diff --git a/posthog/clickhouse/migrations/0262_property_values_swap_text_index_for_ngrambf.py b/posthog/clickhouse/migrations/0262_property_values_swap_text_index_for_ngrambf.py
new file mode 100644
index 000000000000..9ea5d93e18bd
--- /dev/null
+++ b/posthog/clickhouse/migrations/0262_property_values_swap_text_index_for_ngrambf.py
@@ -0,0 +1,30 @@
+from posthog import settings
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.property_values import TABLE_NAME
+
+if settings.CLOUD_DEPLOYMENT == "DEV":
+ _ROLES = [NodeRole.DATA]
+else:
+ _ROLES = [NodeRole.AUX]
+
+operations = [
+ run_sql_with_exceptions(
+ f"ALTER TABLE {TABLE_NAME} DROP INDEX IF EXISTS idx_property_value",
+ node_roles=_ROLES,
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {TABLE_NAME} ADD INDEX IF NOT EXISTS idx_property_value_ngrambf lower(property_value) TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1",
+ node_roles=_ROLES,
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {TABLE_NAME} MATERIALIZE INDEX idx_property_value_ngrambf",
+ node_roles=_ROLES,
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 263d0bb811c0..91079858efe4 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0261_conversion_goal_attributed_preaggregated
+0262_property_values_swap_text_index_for_ngrambf
diff --git a/posthog/clickhouse/property_values.py b/posthog/clickhouse/property_values.py
index 8b7051863c97..47aac16d955b 100644
--- a/posthog/clickhouse/property_values.py
+++ b/posthog/clickhouse/property_values.py
@@ -44,14 +44,13 @@ def PROPERTY_VALUES_TABLE_SQL() -> str:
ORDER BY (team_id, property_type, property_key, property_value)
TTL last_seen + INTERVAL 30 DAY DELETE
SETTINGS
- index_granularity = 8192,
- enable_full_text_index = 1
+ index_granularity = 8192
"""
).format(
table_name=TABLE_NAME,
engine=AggregatingMergeTree(TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED),
extra_fields=""",
- INDEX idx_property_value property_value TYPE text(tokenizer = ngrams(3), preprocessor = lower(property_value)) GRANULARITY 1""",
+ INDEX idx_property_value_ngrambf lower(property_value) TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1""",
)
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 071c0e5d8002..4bbc45dfdc0c 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -3708,14 +3708,13 @@
`property_count` SimpleAggregateFunction(sum, UInt64),
`last_seen` SimpleAggregateFunction(max, DateTime) DEFAULT now()
,
- INDEX idx_property_value property_value TYPE text(tokenizer = ngrams(3), preprocessor = lower(property_value)) GRANULARITY 1
+ INDEX idx_property_value_ngrambf lower(property_value) TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.property_values', '{replica}-{shard}')
ORDER BY (team_id, property_type, property_key, property_value)
TTL last_seen + INTERVAL 30 DAY DELETE
SETTINGS
- index_granularity = 8192,
- enable_full_text_index = 1
+ index_granularity = 8192
'''
# ---
@@ -8261,14 +8260,13 @@
`property_count` SimpleAggregateFunction(sum, UInt64),
`last_seen` SimpleAggregateFunction(max, DateTime) DEFAULT now()
,
- INDEX idx_property_value property_value TYPE text(tokenizer = ngrams(3), preprocessor = lower(property_value)) GRANULARITY 1
+ INDEX idx_property_value_ngrambf lower(property_value) TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.property_values', '{replica}-{shard}')
ORDER BY (team_id, property_type, property_key, property_value)
TTL last_seen + INTERVAL 30 DAY DELETE
SETTINGS
- index_granularity = 8192,
- enable_full_text_index = 1
+ index_granularity = 8192
'''
# ---