: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(taxonomy): scope event-definition queries through the project unique index (#89086)
Sven Lange95f7904a94f8
diff --git a/posthog/api/event_definition.py b/posthog/api/event_definition.pyindex c42df79aa586..3278b23f012f 100644--- a/posthog/api/event_definition.py+++ b/posthog/api/event_definition.py@@ -92,11 +92,15 @@ def create_event_definitions_sql(f"{order_expression} {order_direction} NULLS {'FIRST' if order_direction == 'ASC' else 'LAST'}")+ # COALESCE(project_id, team_id) is the leading expression of the unique index+ # `event_definition_proj_uniq`, so the planner can seek that index for the project scope and+ # for any `name` equality in `conditions`. The equivalent form+ # `project_id = X OR (project_id IS NULL AND team_id = X)` matches no index at all.return f"""SELECT {",".join(event_definition_fields)}FROM posthog_eventdefinition{enterprise_join}- WHERE (project_id = %(project_id)s OR (project_id IS NULL AND team_id = %(project_id)s))+ WHERE COALESCE(project_id, team_id) = %(project_id)s{conditions}ORDER BY {",".join(additional_ordering)}"""diff --git a/posthog/api/test/test_event_definition.py b/posthog/api/test/test_event_definition.pyindex 93667d1bc2ff..33139bdda3d0 100644--- a/posthog/api/test/test_event_definition.py+++ b/posthog/api/test/test_event_definition.py@@ -67,6 +67,27 @@ class TestEventDefinitionAPI(APIBaseTest):)assert abs((dateutil.parser.isoparse(response_item["created_at"]) - timezone.now()).total_seconds()) < 1+ def test_list_event_definitions_scopes_by_project_across_environments(self):+ # Rows written before the project backfill have project_id NULL and scope by team_id; rows written+ # since carry project_id. Both must be visible from any environment of the project, and a sibling+ # project's rows must not. Guards the scope predicate against a rewrite to only one of the columns.+ other_env = Team.objects.create(+ organization=self.organization, project_id=self.demo_team.project_id, name="staging env"+ )+ other_project_team = create_team(organization=self.organization)+ EventDefinition.objects.create(team=other_env, project_id=self.demo_team.project_id, name="from_other_env")+ EventDefinition.objects.create(+ team=other_project_team, project_id=other_project_team.project_id, name="from_other_project"+ )++ response = self.client.get("/api/projects/@current/event_definitions/")++ assert response.status_code == status.HTTP_200_OK+ result_names = {r["name"] for r in response.json()["results"]}+ assert "from_other_env" in result_names+ assert "from_other_project" not in result_names+ assert {d["name"] for d in self.EXPECTED_EVENT_DEFINITIONS} <= result_names+def test_list_event_definitions_with_excluded_properties(self):response = self.client.get('/api/projects/@current/event_definitions/?excluded_properties=["installed_app", "purchase"]'