: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): sort the event definitions select column list (#90873)
Stephen Schmidtcfb63b9df8a0
diff --git a/posthog/api/event_definition.py b/posthog/api/event_definition.pyindex 8ac4cf764172..0a01ecc71efe 100644--- a/posthog/api/event_definition.py+++ b/posthog/api/event_definition.py@@ -73,6 +73,10 @@ def create_event_definitions_sql(}# Django relies on PK being present in the result set to tell if it's a saved instanceevent_definition_fields.add("id as pk")+ # Sorted because a set iterates in an order that depends on the process hash seed. Unsorted,+ # every worker emits a different statement text, so pg_stat_statements and Performance Insights+ # split this query's load across hundreds of fingerprints and none of them looks expensive.+ selected_fields = sorted(event_definition_fields)enterprise_join = ("FULL OUTER JOIN ee_enterpriseeventdefinition ON posthog_eventdefinition.id=ee_enterpriseeventdefinition.eventdefinition_ptr_id"@@ -97,7 +101,7 @@ def create_event_definitions_sql(# 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)}+ SELECT {",".join(selected_fields)}FROM posthog_eventdefinition{enterprise_join}WHERE COALESCE(project_id, team_id) = %(project_id)sdiff --git a/posthog/api/test/test_event_definition.py b/posthog/api/test/test_event_definition.pyindex 44bfb3aaf6c8..b347533cf4d2 100644--- a/posthog/api/test/test_event_definition.py+++ b/posthog/api/test/test_event_definition.py@@ -7,15 +7,18 @@ from freezegun.api import freeze_timefrom posthog.test.base import APIBaseTestfrom unittest.mock import ANY, patch+from django.test import SimpleTestCasefrom django.utils import timezoneimport dateutil.parserfrom parameterized import parameterizedfrom rest_framework import status+from posthog.api.event_definition import create_event_definitions_sqlfrom posthog.api.test.test_organization import create_organizationfrom posthog.api.test.test_team import create_teamfrom posthog.api.test.test_user import create_user+from posthog.constants import EventDefinitionTypefrom posthog.models import ActivityLog, EventDefinition, Organization, Tag, Teamfrom products.actions.backend.models.action import Action@@ -649,6 +652,15 @@ class TestEventDefinitionAPI(APIBaseTest):assert not ActivityLog.objects.filter(scope="EventDefinition", item_id=str(ed.id)).exists()+class TestCreateEventDefinitionsSql(SimpleTestCase):+ @parameterized.expand([("enterprise", True), ("open source", False)])+ def test_selects_columns_in_a_stable_order(self, _name: str, is_enterprise: bool):+ sql = create_event_definitions_sql(EventDefinitionType.EVENT, is_enterprise=is_enterprise)+ select_clause = sql.split("SELECT ", 1)[1].split("FROM", 1)[0]+ columns = [column.strip() for column in select_clause.split(",")]+ assert columns == sorted(columns)++class TestEventDefinitionExcludeStale(APIBaseTest):"""Stale filter tests need real wall-clock times so the Postgres NOW() comparisonin `exclude_stale` matches the fixture last_seen_at values. The other test class is