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(aio): stop the session view from building the full hogql catalog (#122289)

Bernat Torres490f730c7fff
diff --git a/posthog/hogql_queries/ai/sentiment_evaluations.py b/posthog/hogql_queries/ai/sentiment_evaluations.py
index 5b660ba30112..737e79f6bfa1 100644
--- a/posthog/hogql_queries/ai/sentiment_evaluations.py
+++ b/posthog/hogql_queries/ai/sentiment_evaluations.py
@@ -152,6 +152,7 @@ def load_generation_sentiment_evaluations_for_traces(
user=user,
query_type=query_type,
fall_back_to_events=True,
+ posthog_tables_only=True,
timings=timings,
modifiers=modifiers,
limit_context=limit_context,
@@ -203,6 +204,7 @@ def load_trace_sentiment_evaluations(
user=user,
query_type=query_type,
fall_back_to_events=True,
+ posthog_tables_only=True,
timings=timings,
modifiers=modifiers,
limit_context=limit_context,
diff --git a/posthog/hogql_queries/ai/session_query_runner.py b/posthog/hogql_queries/ai/session_query_runner.py
index 915f82431313..410a6f36c14c 100644
--- a/posthog/hogql_queries/ai/session_query_runner.py
+++ b/posthog/hogql_queries/ai/session_query_runner.py
@@ -88,12 +88,9 @@ class SessionQueryRunner(AnalyticsQueryRunner[SessionQueryResponse]):
query = self._build_query()
- # Evaluation reads one session by id, so it skips the full per-team catalog build (see
- # query_ai_events). Interactive reads keep the full catalog because their request modifiers
- # can name warehouse tables.
- database = (
- Database.create_for_posthog_tables(self.team, modifiers=self.modifiers) if self.for_evaluation else None
- )
+ # The query reads only ai_events or events and takes no property filters, so it skips the full
+ # per-team catalog build, which takes seconds on a project with a large data warehouse.
+ database = Database.create_for_posthog_tables(self.team, modifiers=self.modifiers)
with self.timings.measure("session_query_ai_events_execute"), tags_context(product=Product.LLM_ANALYTICS):
tag_queries(ai_query_source="dedicated_table")
query_result = self.paginator.execute_hogql_query(
@@ -135,7 +132,9 @@ class SessionQueryRunner(AnalyticsQueryRunner[SessionQueryResponse]):
timings=self.timings,
modifiers=self.modifiers,
limit_context=self.limit_context,
- context=HogQLContext(team_id=self.team.pk, user=self.user, use_new_events_schema=events_schema),
+ context=HogQLContext(
+ team_id=self.team.pk, user=self.user, use_new_events_schema=events_schema, database=database
+ ),
)
columns: list[str] = query_result.columns or []
diff --git a/posthog/hogql_queries/ai/test/test_sentiment_evaluations.py b/posthog/hogql_queries/ai/test/test_sentiment_evaluations.py
index da3ef6009d16..d56116995334 100644
--- a/posthog/hogql_queries/ai/test/test_sentiment_evaluations.py
+++ b/posthog/hogql_queries/ai/test/test_sentiment_evaluations.py
@@ -14,12 +14,13 @@ GenerationResultRow = tuple[object, object, object, object, object, object]
def _load_trace_sentiment_from_generation_results(generation_results: list[GenerationResultRow]) -> dict[str, object]:
result = SimpleNamespace(results=[["trace-1", generation_results]])
- with patch("posthog.hogql_queries.ai.sentiment_evaluations.query_ai_events", return_value=result):
+ with patch("posthog.hogql_queries.ai.sentiment_evaluations.query_ai_events", return_value=result) as query:
sentiment_by_trace_id = load_trace_sentiment_evaluations(
team=cast(Team, SimpleNamespace(id=1)),
trace_ids=["trace-1"],
)
+ assert query.call_args.kwargs["posthog_tables_only"] is True
return cast(dict[str, object], sentiment_by_trace_id["trace-1"])
diff --git a/posthog/hogql_queries/ai/test/test_session_query_runner.py b/posthog/hogql_queries/ai/test/test_session_query_runner.py
index 5d94a6b8f705..0fd478ac8f0b 100644
--- a/posthog/hogql_queries/ai/test/test_session_query_runner.py
+++ b/posthog/hogql_queries/ai/test/test_session_query_runner.py
@@ -339,10 +339,11 @@ class TestSessionQueryRunner(ClickhouseTestMixin, BaseTest):
]
)
- response = SessionQueryRunner(
- team=self.team,
- query=SessionQuery(sessionId="session-sentiment", includeSentiment=True),
- ).calculate()
+ with patch.object(Database, "create_for", side_effect=AssertionError("built the full HogQL catalog")):
+ response = SessionQueryRunner(
+ team=self.team,
+ query=SessionQuery(sessionId="session-sentiment", includeSentiment=True),
+ ).calculate()
self.assertEqual(len(response.results), 1)
trace = response.results[0]
@@ -525,7 +526,10 @@ class TestSessionQueryRunner(ClickhouseTestMixin, BaseTest):
),
)
- with self.capture_select_queries() as queries:
+ with (
+ self.capture_select_queries() as queries,
+ patch.object(Database, "create_for", side_effect=AssertionError("built the full HogQL catalog")),
+ ):
runner.calculate()
select_queries = _select_queries_without_metadata(queries)