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): merge untagged overview reads into direct_scan in SQL (#100486)

Juraj Majerik404143c548df
diff --git a/posthog/api/debug_ch_queries.py b/posthog/api/debug_ch_queries.py
index 6b8140df54b4..fecefe14cd7b 100644
--- a/posthog/api/debug_ch_queries.py
+++ b/posthog/api/debug_ch_queries.py
@@ -858,9 +858,13 @@ class DebugCHQueries(viewsets.ViewSet):
# nosemgrep: clickhouse-fstring-param-audit - skip_reason_counts is built from the PrecomputeSkipReason enum
reads_sql = f"""
SELECT
+ -- Untagged rows fold into direct_scan inside the GROUP BY, so the percentiles
+ -- cover the merged population. Merging groups after the fact can only sum counts,
+ -- not recombine percentiles.
coalesce(
nullIf(toString(log_comment.experiment_exposures_path), ''),
- ifNull(toString(log_comment.experiment_execution_path), '')
+ nullIf(toString(log_comment.experiment_execution_path), ''),
+ 'direct_scan'
) AS exposures_path,
count() AS reads,
countIf(exception_code != 0) AS failed_reads,
@@ -931,8 +935,10 @@ class DebugCHQueries(viewsets.ViewSet):
metric_events = {"precomputed": 0, "direct_scan": 0, "not_applicable": 0}
for raw_row in reads_response:
row = dict(zip(reads_columns, raw_row))
- path = row["exposures_path"] or "direct_scan"
- entry = reads_by_path.setdefault(path, _empty_path_entry())
+ # One SQL group per path (the SQL normalizes untagged rows to direct_scan), so the
+ # per-stat assignment below never overwrites another group's percentiles. An unexpected
+ # path surfaces as its own key rather than being silently folded in.
+ entry = reads_by_path.setdefault(row["exposures_path"], _empty_path_entry())
entry["reads"] += row["reads"]
entry["failed_reads"] += row["failed_reads"]
entry["attempted"] += row["attempted"]
diff --git a/posthog/api/test/test_debug_ch_queries.py b/posthog/api/test/test_debug_ch_queries.py
index 109ab599636c..d1aef893f8f7 100644
--- a/posthog/api/test/test_debug_ch_queries.py
+++ b/posthog/api/test/test_debug_ch_queries.py
@@ -257,6 +257,30 @@ class TestDebugCHQuery(APIBaseTest):
for stat in ("avg_duration_ms", "p50_duration_ms", "p90_duration_ms", "avg_read_bytes"):
self.assertIsNone(entry[stat])
+ def test_precompute_overview_surfaces_unexpected_paths_instead_of_overwriting_direct_scan(self):
+ # The SQL folds untagged rows into direct_scan inside the GROUP BY. If an untagged ('')
+ # group still reaches the merge loop, it must surface as its own bucket — the old
+ # `or "direct_scan"` fallback folded it in by replacing the percentiles with the last
+ # row's (counts summed, stats lied).
+ self.user.is_staff = True
+ self.user.save()
+ no_skips = (0,) * 7
+
+ def read_row(path, p50):
+ return (path, 3, 0, *no_skips, 3, 3, 0, 0, p50, p50, p50, 1024.0, 4096)
+
+ with patch(
+ "posthog.api.debug_ch_queries.sync_execute",
+ side_effect=[[read_row("direct_scan", 100.0), read_row("", 999.0)], []],
+ ):
+ resp = self.client.get("/api/debug_ch_queries/precompute_overview/?hours=24")
+
+ self.assertEqual(resp.status_code, HTTP_200_OK, resp.content)
+ by_path = resp.json()["reads"]["by_exposures_path"]
+ self.assertEqual(by_path["direct_scan"]["p50_duration_ms"], 100.0)
+ self.assertEqual(by_path[""]["p50_duration_ms"], 999.0)
+ self.assertEqual(resp.json()["reads"]["total"], 6)
+
@patch("posthog.api.debug_ch_queries.sync_execute", return_value=[])
def test_slowest_queries_pat_with_scope_and_staff_allowed(self, _mock_execute):
self.user.is_staff = True