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): read session traces with global in (#121912)

Bernat Torresecc4442db056
diff --git a/posthog/hogql_queries/ai/session_query_runner.py b/posthog/hogql_queries/ai/session_query_runner.py
index 4dc074c3858e..915f82431313 100644
--- a/posthog/hogql_queries/ai/session_query_runner.py
+++ b/posthog/hogql_queries/ai/session_query_runner.py
@@ -30,7 +30,7 @@ from posthog.hogql_queries.ai.sentiment_evaluations import (
get_sentiment_for_generation,
load_generation_sentiment_evaluations_for_traces,
)
-from posthog.hogql_queries.ai.utils import merge_heavy_properties
+from posthog.hogql_queries.ai.utils import merge_heavy_properties, with_global_in_subqueries
from posthog.hogql_queries.paginators import HogQLHasMorePaginator
from posthog.hogql_queries.query_runner import AnalyticsQueryRunner
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
@@ -282,7 +282,7 @@ class SessionQueryRunner(AnalyticsQueryRunner[SessionQueryResponse]):
ORDER BY first_timestamp DESC
""",
)
- return cast(ast.SelectQuery, query)
+ return cast(ast.SelectQuery, with_global_in_subqueries(query))
def get_cache_payload(self) -> dict[str, Any]:
payload = {**super().get_cache_payload(), "schema_version": 2}
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 4e744b6fd39a..5d94a6b8f705 100644
--- a/posthog/hogql_queries/ai/test/test_session_query_runner.py
+++ b/posthog/hogql_queries/ai/test/test_session_query_runner.py
@@ -130,6 +130,7 @@ class TestSessionQueryRunner(ClickhouseTestMixin, BaseTest):
select_queries = _select_queries_without_metadata(queries)
self.assertEqual(len(select_queries), 1)
self.assertIn("ai_events", select_queries[0])
+ self.assertIn("globalIn(ai_events.trace_id, (SELECT", select_queries[0])
self.assertEqual(len(response.results), 1)
trace = response.results[0]
self.assertEqual(trace.id, "trace-ai-events")
diff --git a/posthog/hogql_queries/ai/utils.py b/posthog/hogql_queries/ai/utils.py
index d11449f56920..d046ca6654d8 100644
--- a/posthog/hogql_queries/ai/utils.py
+++ b/posthog/hogql_queries/ai/utils.py
@@ -8,6 +8,7 @@ import orjson
from posthog.schema import PropertyOperator
from posthog.hogql import ast
+from posthog.hogql.visitor import TraversingVisitor
from posthog.caching.utils import ThresholdMode, is_stale
@@ -184,3 +185,19 @@ class TaxonomyCacheMixin(ABC):
def cache_target_age(self, last_refresh: Optional[datetime], lazy: bool = False) -> Optional[datetime]:
return None
+
+
+class _GlobalInSubqueries(TraversingVisitor):
+ def visit_compare_operation(self, node: ast.CompareOperation) -> None:
+ if node.op == ast.CompareOperationOp.In and isinstance(node.right, ast.SelectQuery):
+ node.op = ast.CompareOperationOp.GlobalIn
+ super().visit_compare_operation(node)
+
+
+def with_global_in_subqueries(
+ query: ast.SelectQuery | ast.SelectSetQuery,
+) -> ast.SelectQuery | ast.SelectSetQuery:
+ """Turn `x IN (SELECT ...)` into GLOBAL IN. A plain IN subquery over the Distributed ai_events
+ table does not narrow the outer read, so the outer read scans the whole table."""
+ _GlobalInSubqueries().visit(query)
+ return query
diff --git a/posthog/temporal/ai_observability/run_session_evaluation.py b/posthog/temporal/ai_observability/run_session_evaluation.py
index eda5baab8559..7358c08a632c 100644
--- a/posthog/temporal/ai_observability/run_session_evaluation.py
+++ b/posthog/temporal/ai_observability/run_session_evaluation.py
@@ -25,6 +25,7 @@ from posthog.clickhouse.query_tagging import Product, tags_context
from posthog.dataclasses import frozen
from posthog.hogql_queries.ai.ai_table_resolver import query_ai_events
from posthog.hogql_queries.ai.session_query_runner import SessionQueryRunner
+from posthog.hogql_queries.ai.utils import with_global_in_subqueries
from posthog.models.team import Team
from posthog.sync import database_sync_to_async
from posthog.temporal.ai_observability.evaluation_event_io import extract_event_io
@@ -165,7 +166,7 @@ def _sum_session_payload_bytes(
) -> int:
with tags_context(product=Product.LLM_ANALYTICS):
result = query_ai_events(
- query=parse_select(_SESSION_PAYLOAD_BYTES_SQL),
+ query=with_global_in_subqueries(parse_select(_SESSION_PAYLOAD_BYTES_SQL)),
placeholders={
"session_id": ast.Constant(value=session_id),
"date_from": ast.Constant(value=date_from),
@@ -220,7 +221,7 @@ def _count_session_events(
"""
with tags_context(product=Product.LLM_ANALYTICS):
result = query_ai_events(
- query=parse_select(_SESSION_EVENT_COUNT_SQL),
+ query=with_global_in_subqueries(parse_select(_SESSION_EVENT_COUNT_SQL)),
placeholders={
"session_id": ast.Constant(value=session_id),
"date_from": ast.Constant(value=date_from),
diff --git a/posthog/temporal/ai_observability/test_run_session_evaluation.py b/posthog/temporal/ai_observability/test_run_session_evaluation.py
index 7981289a4ace..4b7438a329db 100644
--- a/posthog/temporal/ai_observability/test_run_session_evaluation.py
+++ b/posthog/temporal/ai_observability/test_run_session_evaluation.py
@@ -12,6 +12,7 @@ from temporalio.exceptions import ApplicationError
from posthog.schema import LLMTrace, LLMTraceEvent
from posthog.hogql.constants import MAX_SELECT_TRACES_LIMIT_EXPORT
+from posthog.hogql.placeholders import replace_placeholders
from posthog.temporal.ai_observability.evaluation_hog import run_hog_eval_for_event
from posthog.temporal.ai_observability.evaluation_llm_judge import (
@@ -28,6 +29,7 @@ from posthog.temporal.ai_observability.run_session_evaluation import (
SessionFetchOutcome,
_count_session_events,
_SessionEventCount,
+ _sum_session_payload_bytes,
build_session_hog_globals,
execute_session_hog_eval_activity,
execute_session_llm_judge_activity,
@@ -235,6 +237,20 @@ class TestCountSessionEvents:
assert result.first_seen == first_seen
assert mock_query_ai_events.call_args.kwargs["fall_back_to_events"] is False
+ @pytest.mark.parametrize(
+ "fetch, rows",
+ [(_count_session_events, [[7, datetime(2026, 7, 1, tzinfo=UTC)]]), (_sum_session_payload_bytes, [[100]])],
+ )
+ def test_reads_the_session_traces_with_global_in(self, fetch, rows):
+ with patch(
+ "posthog.temporal.ai_observability.run_session_evaluation.query_ai_events",
+ return_value=Mock(results=rows),
+ ) as mock_query_ai_events:
+ fetch(Mock(), "s-1", FROZEN_NOW, FROZEN_NOW)
+
+ kwargs = mock_query_ai_events.call_args.kwargs
+ assert "globalIn(trace_id, (SELECT" in replace_placeholders(kwargs["query"], kwargs["placeholders"]).to_hogql()
+
class TestRunHogEvalOverRecentSessions:
@time_machine.travel(FROZEN_NOW, tick=False)