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(taxonomy): index feature flag property definitions by project (#92399)

José Sequeirae775f5483283
diff --git a/posthog/taxonomy/property_definition_api.py b/posthog/taxonomy/property_definition_api.py
index 0a4c117ed186..404c412cd923 100644
--- a/posthog/taxonomy/property_definition_api.py
+++ b/posthog/taxonomy/property_definition_api.py
@@ -724,7 +724,7 @@ class PropertyDefinitionViewSet(
QueryContext(
project_id=self.project_id,
table=(
- "ee_enterprisepropertydefinition FULL OUTER JOIN posthog_propertydefinition ON posthog_propertydefinition.id=ee_enterprisepropertydefinition.propertydefinition_ptr_id"
+ "posthog_propertydefinition LEFT JOIN ee_enterprisepropertydefinition ON posthog_propertydefinition.id=ee_enterprisepropertydefinition.propertydefinition_ptr_id"
if EE_AVAILABLE
else "posthog_propertydefinition"
),
diff --git a/products/event_definitions/backend/migrations/0011_propertydefinition_feature_flag_idx.py b/products/event_definitions/backend/migrations/0011_propertydefinition_feature_flag_idx.py
new file mode 100644
index 000000000000..cb4943320fcc
--- /dev/null
+++ b/products/event_definitions/backend/migrations/0011_propertydefinition_feature_flag_idx.py
@@ -0,0 +1,27 @@
+from django.db import migrations, models
+from django.db.models.expressions import F
+from django.db.models.functions import Coalesce
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("event_definitions", "0010_propertydefinition_warehouse_origin"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="propertydefinition",
+ index=models.Index(
+ Coalesce(F("project_id"), F("team_id")),
+ F("type"),
+ Coalesce(F("group_type_index"), -1),
+ F("name"),
+ condition=models.Q(("name__startswith", "$feature/")),
+ name="index_propdef_feature_flag",
+ ),
+ ),
+ ]
diff --git a/products/event_definitions/backend/migrations/max_migration.txt b/products/event_definitions/backend/migrations/max_migration.txt
index 16e7cad7755f..eb9c26a1202f 100644
--- a/products/event_definitions/backend/migrations/max_migration.txt
+++ b/products/event_definitions/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0010_propertydefinition_warehouse_origin
+0011_propertydefinition_feature_flag_idx
diff --git a/products/event_definitions/backend/models/property_definition.py b/products/event_definitions/backend/models/property_definition.py
index e1ee5d73fb41..05dabca11453 100644
--- a/products/event_definitions/backend/models/property_definition.py
+++ b/products/event_definitions/backend/models/property_definition.py
@@ -132,6 +132,18 @@ class PropertyDefinition(UUIDTModel):
fields=["name"],
opclasses=["gin_trgm_ops"],
), # To speed up DB-based fuzzy searching
+ # `is_feature_flag=true` lists filter on `name LIKE '$feature/%'`. Without this index the
+ # planner answers that prefix from the trigram GIN above, which is not scoped by project and
+ # visits every `$feature/` row across all teams. The condition must stay identical to the
+ # filter in `QueryContext.with_feature_flags`, or the planner cannot prove the index applies.
+ models.Index(
+ Coalesce(F("project_id"), F("team_id")),
+ F("type"),
+ Coalesce(F("group_type_index"), -1),
+ F("name"),
+ condition=models.Q(name__startswith="$feature/"),
+ name="index_propdef_feature_flag",
+ ),
]
constraints = [
models.CheckConstraint(