: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
fix(event-definitions): left join the enterprise table instead of full outer (#90387)
Stephen Schmidtc7c037998d64
diff --git a/posthog/api/event_definition.py b/posthog/api/event_definition.pyindex 0a01ecc71efe..3f96fd2e80bd 100644--- a/posthog/api/event_definition.py+++ b/posthog/api/event_definition.py@@ -78,8 +78,16 @@ def create_event_definitions_sql(# split this query's load across hundreds of fingerprints and none of them looks expensive.selected_fields = sorted(event_definition_fields)+ # LEFT, not FULL OUTER. The two return the same rows here, on two independent grounds:+ # `eventdefinition_ptr_id` is the child's primary key and a validated NOT NULL foreign key, so an+ # enterprise row without a base row cannot be committed; and even if one existed, the scope+ # filter below reads base-table columns, so that row's `COALESCE(project_id, team_id)` would be+ # NULL and it would be filtered out regardless of join type.+ # The join type does change the plan. A full join can be neither a nested loop nor a filter+ # pushed into the scan, which leaves a sequential scan of the whole table available to the+ # planner — and it picked that plan in production once statistics shifted.enterprise_join = (- "FULL OUTER JOIN ee_enterpriseeventdefinition ON posthog_eventdefinition.id=ee_enterpriseeventdefinition.eventdefinition_ptr_id"+ "LEFT JOIN ee_enterpriseeventdefinition ON posthog_eventdefinition.id=ee_enterpriseeventdefinition.eventdefinition_ptr_id"if is_enterpriseelse "")diff --git a/posthog/api/test/test_event_definition.py b/posthog/api/test/test_event_definition.pyindex b347533cf4d2..72525820a9de 100644--- a/posthog/api/test/test_event_definition.py+++ b/posthog/api/test/test_event_definition.py@@ -660,6 +660,11 @@ class TestCreateEventDefinitionsSql(SimpleTestCase):columns = [column.strip() for column in select_clause.split(",")]assert columns == sorted(columns)+ def test_joins_the_enterprise_table_with_a_left_join(self):+ sql = create_event_definitions_sql(EventDefinitionType.EVENT, is_enterprise=True)+ assert "LEFT JOIN ee_enterpriseeventdefinition" in sql+ assert "FULL OUTER JOIN" not in sql+class TestEventDefinitionExcludeStale(APIBaseTest):"""Stale filter tests need real wall-clock times so the Postgres NOW() comparison