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.

feat(experiments): show scan window per query and precompute build (#67048)

Juraj Majerik965fd4ea0f38
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 1a25291d02fc..29c6d09a587f 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -5023,7 +5023,7 @@ snapshots:
scenes-app-dashboards--edit--dark:
hash: v1.k794b7964.6aa70ce359a5c4a0c09a57a8629d73f8a152d204d9d22d44d0d4777f6eeb0c06.BZXugeWh6SNKv6iKcjcRl4pEjlyPgbD9HQHJCVuHcZ8
scenes-app-dashboards--edit--light:
- hash: v1.k794b7964.b42a16df47349f0098289f0917478e2bfa25c4c0372db6293dd5c7973aab779f.W5J45Zee4_WYg_lH-7LwmFxAflRM8l-nXybTFHT10T8
+ hash: v1.k794b7964.825b5c49c10013e1f40d5f37b53fa764b6237fd9c380aed87b5d6e2698668329.3v7ug4MyDTt9sO28DLytChnDH3gsZlhVF7yUZmx_nR8
scenes-app-dashboards--erroring--dark:
hash: v1.k794b7964.a356bf2ce40e53018919ebc890579c0ad6e493385f546d26b1f70471bf99f726.UER3Vv5UUZ1POzJv4_TuxrLBcLfLa8WTZ_hyUX9t72s
scenes-app-dashboards--erroring--light:
diff --git a/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx b/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx
index 7cb3466b9e72..b81eae572575 100644
--- a/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx
+++ b/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx
@@ -6,6 +6,7 @@ import { LemonButton, LemonDialog, LemonTabs } from '@posthog/lemon-ui'
import { CodeSnippet, Language } from 'lib/components/CodeSnippet'
import { CopyToClipboardInline } from 'lib/components/CopyToClipboard'
import { LinkMetabaseQuery } from 'lib/components/MetabaseQueryLink'
+import { dayjs } from 'lib/dayjs'
import { LemonInput } from 'lib/lemon-ui/LemonInput'
import { LemonSelect } from 'lib/lemon-ui/LemonSelect'
import { LemonSwitch } from 'lib/lemon-ui/LemonSwitch'
@@ -101,6 +102,19 @@ function outcome(item: SlowestQuery): { label: string; type: LemonTagType } {
return { label: 'Build failed', type: 'warning' }
}
+// Compact day-span of a scan window, exact dates on hover. Renders nothing for pre-tag (empty) rows.
+function ScanWindow({ from, to }: { from: string; to: string }): JSX.Element | null {
+ if (!from || !to) {
+ return null
+ }
+ const days = Math.max(1, dayjs(to).diff(dayjs(from), 'day'))
+ return (
+ <Tooltip title={`${from} → ${to}`}>
+ <span className="font-mono text-xs">{days}d</span>
+ </Tooltip>
+ )
+}
+
function QueryStats({
read_bytes,
read_rows,
@@ -266,6 +280,13 @@ export function QueryPerformance(): JSX.Element {
)
},
},
+ {
+ title: 'Scan window',
+ width: 110,
+ render: function ScanWindowCol(_, item): JSX.Element | null {
+ return <ScanWindow from={item.experiment_scan_date_from} to={item.experiment_scan_date_to} />
+ },
+ },
{
title: 'Organization',
render: function OrgCell(_, item) {
@@ -408,6 +429,20 @@ export function QueryPerformance(): JSX.Element {
return <span className="font-mono">{Math.round(item.execution_time)}</span>
},
},
+ {
+ title: 'Scan window',
+ width: 110,
+ render: function SubQueryWindow(_, item): JSX.Element | null {
+ return <ScanWindow from={item.precompute_window_start} to={item.precompute_window_end} />
+ },
+ },
+ {
+ title: 'Read',
+ width: 110,
+ render: function SubQueryRead(_, item): JSX.Element {
+ return <span className="font-mono">{humanizeBytes(item.read_bytes)}</span>
+ },
+ },
{
title: 'Status',
render: function SubQueryStatus(_, item) {
diff --git a/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts b/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts
index 5bb72d91f9ad..aa0efa1756c3 100644
--- a/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts
+++ b/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts
@@ -35,6 +35,10 @@ export interface SlowestQuery {
experiment_query_surface: string
experiment_precompute_table: string
experiment_precompute_skip_reason: string
+ experiment_scan_date_from: string
+ experiment_scan_date_to: string
+ precompute_window_start: string
+ precompute_window_end: string
experiment_query_group_id: string
experiment_metric_type: string
experiment_funnel_order_type: string | null
diff --git a/posthog/api/debug_ch_queries.py b/posthog/api/debug_ch_queries.py
index b6f0402f0816..b32226b91847 100644
--- a/posthog/api/debug_ch_queries.py
+++ b/posthog/api/debug_ch_queries.py
@@ -492,7 +492,11 @@ class DebugCHQueries(viewsets.ViewSet):
argMax(JSONExtractString(log_comment, 'experiment_query_surface'), type) AS experiment_query_surface,
argMax(JSONExtractString(log_comment, 'experiment_precompute_table'), type) AS experiment_precompute_table,
argMax(JSONExtractString(log_comment, 'experiment_query_group_id'), type) AS experiment_query_group_id,
- argMax(JSONExtractString(log_comment, 'experiment_precompute_skip_reason'), type) AS experiment_precompute_skip_reason
+ argMax(JSONExtractString(log_comment, 'experiment_precompute_skip_reason'), type) AS experiment_precompute_skip_reason,
+ argMax(JSONExtractString(log_comment, 'experiment_scan_date_from'), type) AS experiment_scan_date_from,
+ argMax(JSONExtractString(log_comment, 'experiment_scan_date_to'), type) AS experiment_scan_date_to,
+ argMax(JSONExtractString(log_comment, 'precompute_window_start'), type) AS precompute_window_start,
+ argMax(JSONExtractString(log_comment, 'precompute_window_end'), type) AS precompute_window_end
FROM (
SELECT
query_id, query, query_start_time, query_duration_ms, exception,
@@ -525,7 +529,9 @@ class DebugCHQueries(viewsets.ViewSet):
g.experiment_metric_events_path, g.experiment_query_surface, g.experiment_precompute_table,
g.experiment_query_group_id, r.total_duration_ms,
g.read_bytes, g.read_rows, g.exception_code, g.memory_usage,
- g.experiment_precompute_skip_reason
+ g.experiment_precompute_skip_reason,
+ g.experiment_scan_date_from, g.experiment_scan_date_to,
+ g.precompute_window_start, g.precompute_window_end
FROM grouped AS g
INNER JOIN ranked AS r ON g.grp = r.grp
ORDER BY
@@ -587,6 +593,10 @@ class DebugCHQueries(viewsets.ViewSet):
"exception_code": row[22],
"memory_usage": row[23],
"experiment_precompute_skip_reason": row[24],
+ "experiment_scan_date_from": row[25],
+ "experiment_scan_date_to": row[26],
+ "precompute_window_start": row[27],
+ "precompute_window_end": row[28],
"sub_queries": [],
}
diff --git a/posthog/clickhouse/query_tagging.py b/posthog/clickhouse/query_tagging.py
index 8460e445eb4f..73e75a4ea5fe 100644
--- a/posthog/clickhouse/query_tagging.py
+++ b/posthog/clickhouse/query_tagging.py
@@ -409,6 +409,10 @@ class QueryTags(BaseModel):
clickhouse_exception_type: Optional[str] = None
client_query_id: Optional[str] = None
cohort_id: Optional[int] = None
+ # lazy-computation / preaggregation builds: the time window a single build INSERT covers (ISO).
+ # Generic across products (experiments, marketing, web analytics) since they share the executor.
+ precompute_window_start: Optional[str] = None
+ precompute_window_end: Optional[str] = None
entity_math: Optional[list[str]] = None
# replays
@@ -438,6 +442,10 @@ class QueryTags(BaseModel):
# "min_runtime", "data_warehouse"; None/absent when precompute was attempted (so a direct path then
# means the build failed or wasn't ready — derivable from the precompute_build sub-queries).
experiment_precompute_skip_reason: Optional[str] = None
+ # Analysis window of the read (ISO), for the query-performance UI. The build sub-queries carry their
+ # own per-chunk window in the generic precompute_window_start/end fields above.
+ experiment_scan_date_from: Optional[str] = None
+ experiment_scan_date_to: Optional[str] = None
# Shared id linking a top-level query to its precompute-build sub-queries. Generated once per
# top-level evaluation; sub-queries inherit it through the tag context. Lets the query-performance
# UI group the (synchronous) build INSERTs under the read that triggered them.
diff --git a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py
index c262c29f1ed4..269b5f785ac2 100644
--- a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py
+++ b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py
@@ -723,7 +723,12 @@ def run_lazy_computation_insert(
)
set_ch_query_started(job.id)
- with tags_context(client_query_id=str(job.id), team_id=team.id):
+ with tags_context(
+ client_query_id=str(job.id),
+ team_id=team.id,
+ precompute_window_start=str(job.time_range_start),
+ precompute_window_end=str(job.time_range_end),
+ ):
sync_execute(
insert_sql,
values,
diff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.py
index 3964492e7780..ecaf185bca0c 100644
--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py
+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py
@@ -482,6 +482,8 @@ class ExperimentQueryRunner(QueryRunner):
experiment_metric_events_path=metric_events_path,
experiment_execution_path=exposures_path,
experiment_precompute_skip_reason=self._precompute_skip_reason(),
+ experiment_scan_date_from=self.date_range.date_from,
+ experiment_scan_date_to=self.date_range.date_to,
)
experiment_query_debug = get_experiment_query_debug(experiment_query_ast, self.team)
self.hogql = experiment_query_debug[0]