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(replay-vision): scope the event description lookup by coalesce (#90365)

Stephen Schmidt2559f677b4ff
diff --git a/products/event_definitions/backend/models/property_definition.py b/products/event_definitions/backend/models/property_definition.py
index eda8db0dbaa2..e1ee5d73fb41 100644
--- a/products/event_definitions/backend/models/property_definition.py
+++ b/products/event_definitions/backend/models/property_definition.py
@@ -19,12 +19,16 @@ PERSON_EMAIL_PROPERTY_NAME = "email"
def effective_project_id_expr() -> Coalesce:
"""
- The project scope this table is indexed by, for `.alias(effective_project_id=...)`.
+ The project scope the taxonomy tables are indexed by, for `.alias(effective_project_id=...)`.
It has to stay identical to the leading column of `posthog_propdef_proj_uniq` and of
`index_property_def_query_proj` below, or a filter on it stops being a seek and becomes a
- range walk. Keep it next to those index definitions for that reason. `EventProperty` carries
- the same pair of columns and the same expression indexes, so it uses this too.
+ range walk. Keep it next to those index definitions for that reason. `EventProperty` and
+ `EventDefinition` carry the same pair of columns, and `event_definition_proj_uniq` leads with
+ the same expression, so they use this too.
+
+ Always prefer this to `Q(project_id=X) | Q(project_id__isnull=True, team_id=X)`. The two select
+ the same rows, but no index covers the OR form, so Postgres bitmap-ORs its way through instead.
Returns a new expression per call, so callers never share one instance across querysets.
"""
diff --git a/products/replay_vision/backend/temporal/team_context.py b/products/replay_vision/backend/temporal/team_context.py
index ec0ddc1d3f96..b0f3be445073 100644
--- a/products/replay_vision/backend/temporal/team_context.py
+++ b/products/replay_vision/backend/temporal/team_context.py
@@ -11,12 +11,12 @@ from collections.abc import Mapping
from functools import cache
from typing import Any
-from django.db.models import Q
-
from posthog.llm.semantic_enrichment import get_team_business_context
from posthog.models import Team
from posthog.settings import EE_AVAILABLE
+from products.event_definitions.backend.models import effective_project_id_expr
+
# CoreMemory.text is model-capped at 10k chars; cap lower since the preamble is resent on every scan step.
_MAX_PRODUCT_CONTEXT_LEN = 4000
_MAX_EVENT_DESCRIPTIONS = 50
@@ -102,9 +102,10 @@ def fetch_event_descriptions(team: Team, columns: list[str], rows: list[list[Any
custom_names = session_custom_event_names(columns, rows)
found = dict(
- EnterpriseEventDefinition.objects.filter(
+ EnterpriseEventDefinition.objects.alias(effective_project_id=effective_project_id_expr())
+ .filter(
# Definitions are project-scoped with a team-scoped legacy fallback; mirrors posthog/api/event_definition.py.
- Q(project_id=team.project_id) | Q(project_id__isnull=True, team_id=team.project_id),
+ effective_project_id=team.project_id,
name__in=custom_names,
)
.exclude(description__isnull=True)
diff --git a/tach.toml b/tach.toml
index b3222068b647..98ca1eb100d4 100644
--- a/tach.toml
+++ b/tach.toml
@@ -705,6 +705,7 @@ depends_on = [
"products.alerts",
"products.cdp",
"products.cohorts",
+ "products.event_definitions",
"products.experiments",
"products.exports",
"products.notifications",