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(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.py
index 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 instance
event_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)s
diff --git a/posthog/api/test/test_event_definition.py b/posthog/api/test/test_event_definition.py
index 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_time
from posthog.test.base import APIBaseTest
from unittest.mock import ANY, patch
+from django.test import SimpleTestCase
from django.utils import timezone
import dateutil.parser
from parameterized import parameterized
from rest_framework import status
+from posthog.api.event_definition import create_event_definitions_sql
from posthog.api.test.test_organization import create_organization
from posthog.api.test.test_team import create_team
from posthog.api.test.test_user import create_user
+from posthog.constants import EventDefinitionType
from posthog.models import ActivityLog, EventDefinition, Organization, Tag, Team
from 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() comparison
in `exclude_stale` matches the fixture last_seen_at values. The other test class is