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(experiments): drop set-level limit that broke the session context union (#71763)

Marcel Poelker9ac04942187d
diff --git a/products/experiments/backend/session_context.py b/products/experiments/backend/session_context.py
index 366bac5de6bb..c9735891b3e3 100644
--- a/products/experiments/backend/session_context.py
+++ b/products/experiments/backend/session_context.py
@@ -386,7 +386,9 @@ def _query_exposure_event_branches(
return {}
query = ast.SelectSetQuery.create_from_queries(branches, "UNION ALL")
- query.limit = ast.Constant(value=MAX_EXPOSURE_ROWS)
+ # No set-level LIMIT here: the printer emits it directly after the last branch's own
+ # LIMIT, which ClickHouse rejects as a syntax error. The per-branch limits above are
+ # the backstop; total output is already bounded by each flag's defined variants.
response = execute_hogql_query(query, team=team, user=user)
exposures: dict[int, list[tuple[str, datetime]]] = {}
diff --git a/products/experiments/backend/test/test_session_context_api.py b/products/experiments/backend/test/test_session_context_api.py
index 44554194a003..a6c27ab23b33 100644
--- a/products/experiments/backend/test/test_session_context_api.py
+++ b/products/experiments/backend/test/test_session_context_api.py
@@ -403,6 +403,50 @@ class TestSessionExperimentContext(ClickhouseTestMixin, APILicensedTest):
assert by_id[custom_experiment.id]["first_exposure_timestamp"] == "2026-01-01T10:05:00Z"
assert all(result["variant"] == "test" for result in results)
+ def test_multiple_custom_criteria_experiments_resolve_in_one_request(self) -> None:
+ self._create_recording()
+ # Two custom-criteria experiments force a real multi-branch UNION ALL — a single branch
+ # collapses to a plain SelectQuery, so only this shape proves the set query compiles
+ # (a set-level LIMIT after the last branch's LIMIT 500ed the endpoint in production).
+ first = self._create_experiment(
+ exposure_criteria={
+ "exposure_config": {
+ "kind": "ExperimentEventExposureConfig",
+ "event": "checkout started",
+ "properties": [],
+ }
+ }
+ )
+ second = self._create_experiment(
+ key="pricing-banner",
+ name="Pricing banner",
+ exposure_criteria={
+ "exposure_config": {
+ "kind": "ExperimentEventExposureConfig",
+ "event": "pricing viewed",
+ "properties": [],
+ }
+ },
+ )
+ self._create_session_event(
+ event="checkout started",
+ timestamp="2026-01-01T10:05:00Z",
+ properties={"$feature/checkout-cta": "test"},
+ )
+ self._create_session_event(
+ event="pricing viewed",
+ timestamp="2026-01-01T10:07:00Z",
+ properties={"$feature/pricing-banner": "control"},
+ )
+ flush_persons_and_events()
+
+ response = self._get_session_context()
+ assert response.status_code == status.HTTP_200_OK
+ results = response.json()["results"]
+ by_id = {result["experiment_id"]: result for result in results}
+ assert by_id[first.id]["first_exposure_timestamp"] == "2026-01-01T10:05:00Z"
+ assert by_id[second.id]["first_exposure_timestamp"] == "2026-01-01T10:07:00Z"
+
def test_action_exposure_criteria_defines_exposure_timestamp(self) -> None:
self._create_recording()
action = Action.objects.create(team=self.team, name="Purchased", steps_json=[{"event": "purchase"}])