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.

chore(web-analytics): document the query serving strategy ladder (#73239)

Lucas Ricoy27eeabc3e870
diff --git a/docs/internal/web-analytics-query-serving.md b/docs/internal/web-analytics-query-serving.md
new file mode 100644
index 000000000000..2d61f6558ad4
--- /dev/null
+++ b/docs/internal/web-analytics-query-serving.md
@@ -0,0 +1,156 @@
+# Web analytics query serving: the strategy ladder
+
+How a web analytics query gets served: every strategy in the current stack, the order they're tried in, and the exact conditions that route a request down each path.
+Source of truth for the code: `products/web_analytics/backend/hogql_queries/`.
+For deep precompute internals (schemas, bucketing, insert variants), see [products/web_analytics/PRECOMPUTATION.md](../../products/web_analytics/PRECOMPUTATION.md).
+
+Every strategy tags its ClickHouse queries with a `query_type` that lands in `system.query_log → log_comment.query_type`, so the tag reference at the bottom doubles as a triage tool.
+
+## The serving tiers
+
+Cheapest first.
+A request walks down this ladder and stops at the first tier whose conditions it satisfies.
+
+| Tier | What it is | Typical latency |
+| ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- |
+| 0. Result cache | Django/HogQL cached response for the exact query fingerprint | ~ms |
+| 1. Lazy precompute | Hourly UTC buckets (vitals: team-tz daily) on the aux cluster, built by background daily-UTC jobs, TTL-refreshed | ~80–200ms |
+| 2. Preaggregated tables (**deprecated**) | Daily preagg tables, modifier-gated; no new enrollments — retained only for the largest existing customers until lazy precompute fully replaces it | ~100ms–1s |
+| 3. Live fast paths | Session-id-set (filtered) and no-join (unfiltered) shapes that avoid the full events↔sessions join | ~1–20s |
+| 4. Full join | The original events↔sessions join; always works, slowest | ~5–60s |
+
+## Request flow
+
+```text
+request
+ │
+ ▼
+[0] Django result cache ──hit──▶ return cached (~ms)
+ │ miss/stale
+ ▼
+[1] Lazy precompute gate
+ │ enrollment: org flag `web-analytics-precompute-toggle` (or env allowlist);
+ │ per-query toggle defaults to opt-out for enrolled teams (#72645) —
+ │ only an explicit useWebAnalyticsPrecompute: false rejects
+ │ shape: family dispatch (see per-runner tables), no conversion goal,
+ │ no sampling, integer timezone, `sessionsV2JoinMode` ≠ uuid,
+ │ range ≤ 90d, filters events-evaluable
+ │ (restricted teams: single exact $host only)
+ │ freshness: all day-buckets fresh per TTL band ──▶ serve *_lazy_query (~80–200ms)
+ │ expired within 6h SWR grace ──▶ serve stale + enqueue revalidation
+ │ miss (NEVER builds inline — enqueues debounced background warm)
+ ▼
+[2] Preaggregated tables (deprecated)
+ │ useWebAnalyticsPreAggregatedTables modifier + supported shape
+ │ not enrolled / unsupported
+ ▼
+[3] Session-id-set fast path (filtered queries)
+ │ team on WEB_ANALYTICS_SESSION_ID_SET_TEAM_IDS or the
+ │ `web-analytics-session-id-set` rollout flag, filters events-evaluable;
+ │ preflight selectivity query first (*_session_id_set_preflight)
+ │ unfiltered / not allowlisted / preflight fails
+ ▼
+[4] No-join fast path (unfiltered queries)
+ │ no property filters, no conversion goal, no session-table fields;
+ │ WEB_ANALYTICS_NO_JOIN_TEAM_IDS + rollout % (100% on Cloud)
+ │ needs session fields with filters
+ ▼
+[5] Full events↔sessions join (unconditional fallback)
+```
+
+**The one-way rule (#72959):** user-facing reads never build precompute buckets inline — `run_inserts` is true only for background-warming requests.
+A miss costs one live-path serve; the background warm makes the next identical request a bucket hit.
+The dashboard "enqueues precompute" as a side effect; it never waits on it.
+
+## Per-runner dispatch
+
+### WebOverviewQuery (`web_overview.py`)
+
+| # | Strategy | Conditions | Tag |
+| --- | -------------------------- | -------------------------------------------------------------------- | ----------------------------------- |
+| 1 | Lazy precompute | Shared gate only — overview has no extra shape restrictions | `web_overview_lazy_query` |
+| 2 | Preaggregated (deprecated) | Modifier on + no conversion goal | `web_overview_preaggregated_query` |
+| 3 | Session-id-set | Filtered + allowlisted + preflight passes (sets `sessionIdPushdown`) | `web_overview_session_id_set_query` |
+| 4 | No-join | Unfiltered, no conversion goal | `web_overview_no_join_query` |
+| 5 | Full join | Fallback | `web_overview_query` |
+
+### WebStatsTableQuery (`stats_table.py`) — three lazy families, tried in order
+
+| # | Strategy | Conditions | Tag |
+| --- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------- |
+| 1 | Paths lazy | `breakdownBy` ∈ {Page, InitialPage} + `includeBounceRate`; rejects avg-time-on-page, scroll depth, unsupported orderBy; top-K 10,000 paths/day cap for high-cardinality teams | `web_stats_paths_lazy_query` |
+| 2 | Frustration lazy | `breakdownBy` = FrustrationMetrics; rejects unsupported orderBy | `web_stats_frustration_lazy_query` |
+| 3 | Simple-breakdown lazy | ~18 supported breakdowns (DeviceType, Browser, OS, Country, Region, City, Viewport, Timezone, Language, ExitPage, InitialChannelType, InitialReferringDomain/URL, InitialUTM\_\*); rejects bounce rate, avg time, scroll depth | `web_stats_lazy_query` |
+| 4 | Preaggregated (deprecated) | Modifier on + no avg-time-on-page + no conversion goal | `stats_table_preaggregated*` |
+| 5 | Session-id-set | Page breakdown ± avg time, filtered + allowlisted + preflight | `stats_table_session_id_set_path_bounce[_and_avg_time]` |
+| 6 | No-join | Unfiltered: path-bounce, path-bounce+avg-time, or simple breakdown without session fields | `stats_table_no_join_*` |
+| 7 | Full join | Fallback per shape | `stats_table_path_bounce`, `stats_table_entry_bounce`, `stats_table_channel_type`, `stats_table_simple_breakdown`, … |
+
+### Goals, vitals, external clicks
+
+| Runner | Tier 1 | Fallback | Notes |
+| --------------------------- | ----------------------------- | ---------------------------------------- | ------------------------------------------------------------- |
+| WebGoalsQuery | Lazy (`web_goals_lazy_query`) | Live (`web_goals_query`) | Needs actions configured; no fast-path shapes exist |
+| WebVitalsPathBreakdownQuery | Lazy | Live (`web_vitals_path_breakdown_query`) | Requires day-aligned range; exempt from integer-timezone gate |
+| WebExternalClicksTableQuery | — | Live (`external_clicks_query`) | Live-only; no precompute family |
+
+## Lazy precompute freshness (summary)
+
+Full details in [PRECOMPUTATION.md](../../products/web_analytics/PRECOMPUTATION.md); the operative numbers:
+
+| Day age | TTL |
+| -------------- | ------ |
+| Today (0d) | 4h |
+| Yesterday (1d) | 6h |
+| 2–7d | 5d |
+| 8–14d | 7d |
+| 15–21d | 10d |
+| 22–35d | 12–14d |
+| 36d+ | 21d |
+
+- Stale-while-revalidate: 6h grace; user reads inside it get the stale row instantly (tagged `precompute_stale=true`) with a Celery revalidation enqueued (10-min debounce). Background warmers are never served stale — they are the refresh.
+- Session settling: 24h forward pad on event scans, matching the SDK session length cap.
+- OOM protection: a team that OOMs during a build gets Redis-pinned for 14 days to 1-day insert windows.
+- Max range: 90 days; wider requests are permanently live.
+
+## Background warming systems
+
+Four writers keep buckets warm; user reads only ever consume.
+
+| System | Trigger tag | When | What it does |
+| -------------------------------------------------------------------- | ---------------------------------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| Eager baseline warmer (Dagster, `eager_web_analytics_precompute.py`) | `webAnalyticsEagerBaselineWarming` | Hourly at :05 | Pre-warms the fixed dashboard matrix (overview, goals, vitals, one stats query per breakdown) over a trailing 28d window for flag-enrolled teams (cap 200, 45-min cycle budget) |
+| Hourly demand warmer (Dagster, `cache_warming.py`) | `webAnalyticsQueryWarming` | Hourly | Selects hot shapes from query_log (kind `Web%`, ≥2 hits in 2 days; raw-path shapes keep a ≥10 bar), expands sub-30d ranges to −30d, replays via an 8-worker pool with the opt-in injected |
+| Warm-behind on miss | (background warming request) | On any user-read miss | Debounced rebuild of exactly the shape that missed; self-heals first-hit misses in ~30–60s |
+| Stale revalidation | `webAnalyticsStaleRevalidation` | On stale-grace serves | Refreshes expired buckets after serving the stale copy |
+
+## Flags and team allowlists
+
+| Gate | Type | Controls |
+| ----------------------------------------------------- | ----------------- | --------------------------------------------------------------------------------------------------------------- |
+| `web-analytics-precompute-toggle` | Org feature flag | Lazy precompute enrollment; enrolled teams read by default (opt-out). Evaluated locally, fails closed |
+| `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` | Env allowlist | Flag-independent precompute enrollment (shared with Dagster warmers, where flag evaluation is unreliable) |
+| `WEB_ANALYTICS_LAZY_PRECOMPUTE_UNRESTRICTED_TEAM_IDS` | Env allowlist | Lifts the single-`$host` filter-shape restriction — any filter combo becomes precomputable (own cache key each) |
+| `WEB_ANALYTICS_SESSION_ID_SET_TEAM_IDS` | Env allowlist | Filtered fast path (live tier) |
+| `web-analytics-session-id-set` | Team rollout flag | Filtered fast path enrollment without the env allowlist — either gate admits the team |
+| `WEB_ANALYTICS_NO_JOIN_TEAM_IDS` + rollout % | Env + percentage | Unfiltered fast path — 100% on Cloud |
+| `useWebAnalyticsPrecompute` | Per-query field | User opt-out switch (WebAnalyticsMenu toggle); explicit `false` always wins |
+| `useWebAnalyticsPreAggregatedTables` | Query modifier | Preaggregated-tables tier (deprecated — largest existing customers only, no new enrollments) |
+
+## query_type tag reference
+
+Suffix conventions: `*_lazy_query` = bucket read (served from precompute), `*_lazy_insert` = bucket build (background only), `*_preflight` = selectivity probe.
+
+| Family | Precompute | Live tags |
+| --------------- | ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| Overview | `web_overview_lazy_query/insert`, `web_overview_preaggregated_query` | `web_overview_no_join_query`, `web_overview_session_id_set_query` (+`_preflight`), `web_overview_query` |
+| Stats table | `web_stats_paths_lazy_*`, `web_stats_frustration_lazy_*`, `web_stats_lazy_*`, `stats_table_preaggregated*` | `stats_table_no_join_*`, `stats_table_session_id_set_*` (+`_preflight`), `stats_table_path_bounce[_and_avg_time]`, `stats_table_entry_bounce`, `stats_table_channel_type`, `stats_table_frustration_metrics`, `stats_table_simple_breakdown` |
+| Goals | `web_goals_lazy_query/insert` | `web_goals_query` |
+| Vitals | `web_vitals_paths_lazy_query/insert` | `web_vitals_path_breakdown_query` |
+| External clicks | — | `external_clicks_query` |
+
+## Reading a slow tile
+
+Find the request in query_log and check `query_type`.
+A `*_lazy_query` taking seconds is a bucket-read problem (rare).
+A fast-path or full-join tag on an enrolled team means the lazy gate rejected the query (filters, avg-time-on-page, >90d range, opt-out) or the buckets weren't fresh — in which case a background warm is already in flight and the next identical request should hit.
diff --git a/products/web_analytics/PRECOMPUTATION.md b/products/web_analytics/PRECOMPUTATION.md
index 525e755a4388..1ef11711499c 100644
--- a/products/web_analytics/PRECOMPUTATION.md
+++ b/products/web_analytics/PRECOMPUTATION.md
@@ -2,10 +2,14 @@
PostHog web analytics has two parallel precomputation systems. They target the same problem (avoid scanning raw events on every dashboard load) but use different mechanisms and apply to different query shapes.
+For how these tiers fit into the full serving ladder (fast paths, full join, dispatch order, query_type tags), see [docs/internal/web-analytics-query-serving.md](../../docs/internal/web-analytics-query-serving.md).
+
## The two systems
### v2 pre-aggregated tables
+**Status: deprecated.** No new enrollments — retained only for the largest existing customers until lazy computation fully replaces it, at which point this system goes away.
+
DAG-warmed ClickHouse tables (`web_pre_aggregated_stats`, `web_pre_aggregated_bounces`) that store hourly-rollup data computed in the background. Gated per-team by the `useWebAnalyticsPreAggregatedTables` modifier plus the `SETTINGS_WEB_ANALYTICS_PRE_AGGREGATED_TABLES` feature flag.
- **Owned by**: web analytics team
@@ -15,24 +19,25 @@ DAG-warmed ClickHouse tables (`web_pre_aggregated_stats`, `web_pre_aggregated_bo
### Lazy computation
-The newer general-purpose framework at `products/analytics_platform/backend/lazy_computation/`. Computes precomputed buckets on first read, caches them in a dedicated CH table per query family, and serves subsequent reads from the cache. Gated per-org by the `web-analytics-precompute-toggle` PostHog feature flag (evaluated against the team's organization).
+The newer general-purpose framework at `products/analytics_platform/backend/lazy_computation/`. Builds precomputed buckets in the background, caches them in a dedicated CH table per query family, and serves reads from the cache; a user read that misses serves the live path and enqueues a debounced background warm. Gated per-org by the `web-analytics-precompute-toggle` PostHog feature flag (evaluated against the team's organization).
- **Owned by**: web analytics team, riding on the analytics_platform framework
-- **Population**: synchronous, on first read miss; subsequent reads hit the cache
-- **Coverage** (today): `web_overview_query` and the PATHS (`WebStatsBreakdown.PAGE` + `includeBounceRate`) tile of `web_stats_table_query`. See `posthog/hogql_queries/web_analytics/web_overview_lazy_precompute.py` and `web_stats_paths_lazy_precompute.py`
+- **Population**: background-only since #72959 — user reads never build inline; a miss serves live and enqueues a debounced background warm. The hourly demand-driven warmer (`dags/cache_warming.py`) and stale revalidation keep hot shapes fresh
+- **Coverage** (today): overview, goals, vitals path breakdown, and three stats-table families — paths (`WebStatsBreakdown.PAGE`/`INITIAL_PAGE` + `includeBounceRate`), frustration metrics, and simple breakdowns. See the `*_lazy_precompute.py` modules in `products/web_analytics/backend/hogql_queries/`
- **Adoption** (as of 2026-05): freshly enabled, org feature flag gates further rollout
## When to use which
Both systems can coexist. The runner tries each in order; if both miss or are disabled the runner falls through to a raw events scan.
-| You want… | Use |
-| --------------------------------------------------------------------- | --------------------------------------------------- |
-| A new query family where the cache shape is bounded and stable | lazy computation |
-| Coverage of an existing query family that v2 already handles | v2 if a team already has v2 enabled; otherwise lazy |
-| Per-team custom precompute logic (uncommon) | lazy computation |
-| Background warming on a schedule | v2 |
-| First-read latency budget that includes a precompute cost (~1.3x raw) | lazy computation accepts this; v2 doesn't have it |
+| You want… | Use |
+| -------------------------------------------------------------- | --------------------------------------------------------------------------------- |
+| A new query family where the cache shape is bounded and stable | lazy computation |
+| Coverage of an existing query family that v2 already handles | v2 if a team already has v2 enabled; otherwise lazy |
+| Per-team custom precompute logic (uncommon) | lazy computation |
+| Background warming on a schedule | either — lazy has the hourly eager + demand warmers; v2 uses its own Dagster DAGs |
+
+A miss never builds inline on either system: since #72959 `run_inserts` is true only for background-warming requests, so the first read of a cold shape serves the live path while the warm happens behind it.
## Lazy computation for web overview (current production path)
@@ -92,14 +97,17 @@ When the gate returns False the runner silently falls through to v2 / raw. **Tod
Different freshness per how recent the data is:
-| Window | TTL | Rationale |
-| --------- | ------ | ----------------------------------------------- |
-| Today | 15 min | Dashboard refresh feels current |
-| Yesterday | 1 hr | Recently-stabilized data, occasional re-compute |
-| Last 7d | 1 day | Stable enough that hourly recompute is wasteful |
-| Older | 7 days | Functionally static |
+| Day age | TTL | Rationale |
+| --------- | ------- | -------------------------------------------------------------------------------- |
+| Today | 4 h | Must outlast the hourly eager warmer so user reads never race a refresh |
+| Yesterday | 6 h | Distinct from today's TTL so `split_ranges_by_ttl` keeps the two days separate |
+| 2–7d | 5 days | Session-final (24h session cap) — recomputing an immutable window buys nothing |
+| 8–14d | 7 days | Distinct per-week TTLs force weekly job boundaries, keeping INSERT scans bounded |
+| 15–21d | 10 days | — |
+| 22–35d | 12–14 d | — |
+| 36d+ | 21 days | Bounded in practice by hash rotations (AST-affecting deploys rebuild everything) |
-Stored via the `LAZY_TTL_SECONDS` dict; consumed by `lazy_computation_executor.parse_ttl_schedule` against the team's timezone.
+Stored via the `LAZY_TTL_SECONDS` dict in `web_lazy_precompute_common.py` (see the comment there for the full freshness-vs-job-sizing reasoning); consumed by `lazy_computation_executor.parse_ttl_schedule` against the team's timezone.
### Read path
@@ -134,7 +142,7 @@ If we later move back to HogQL (after the consistency story is settled), the Hog
## Adding lazy computation to another web analytics query family
-Reference implementation: `posthog/hogql_queries/web_analytics/web_overview_lazy_precompute.py`.
+Reference implementation: `products/web_analytics/backend/hogql_queries/web_overview_lazy_precompute.py`.
Roughly:
@@ -253,7 +261,7 @@ The runner re-partitions the resulting `(band, path, value)` tuples into the `go
## Eager baseline warming (hourly Dagster job)
-The lazy path computes on first read, but for high-traffic teams the dashboard's main tiles are requested constantly — there's no reason to make the first user of every cycle pay the INSERT cost. The eager job pre-warms the same lazy precompute cache (and the Django response cache) for a fixed query matrix, ahead of users.
+A cold or expired shape's first read serves the live path and only warms in the background — but for high-traffic teams the dashboard's main tiles are requested constantly, and there's no reason to let the first user of every cycle take that live-path miss. The eager job pre-warms the same lazy precompute cache (and the Django response cache) for a fixed query matrix, ahead of users.
- **Location**: `products/web_analytics/dags/eager_web_analytics_precompute.py`
- **Schedule**: `5 * * * *` (hourly, offset 5 min from the existing `cache_warming_schedule` at `0 * * * *`); skipped if a prior run is still in flight (`check_for_concurrent_runs`).
@@ -271,12 +279,12 @@ This job is complementary to `cache_warming.py`, which replays whatever queries
## Related code
-- `posthog/hogql_queries/web_analytics/web_overview.py` — runner
-- `posthog/hogql_queries/web_analytics/web_overview_lazy_precompute.py` — overview lazy path
-- `posthog/hogql_queries/web_analytics/web_overview_pre_aggregated.py` — overview v2 path
-- `posthog/hogql_queries/web_analytics/stats_table.py` — stats table runner
-- `posthog/hogql_queries/web_analytics/web_stats_paths_lazy_precompute.py` — PATHS lazy path
-- `posthog/hogql_queries/web_analytics/web_lazy_precompute_common.py` — shared eligibility gate + helpers
+- `products/web_analytics/backend/hogql_queries/web_overview.py` — runner
+- `products/web_analytics/backend/hogql_queries/web_overview_lazy_precompute.py` — overview lazy path
+- `products/web_analytics/backend/hogql_queries/web_overview_pre_aggregated.py` — overview v2 path
+- `products/web_analytics/backend/hogql_queries/stats_table.py` — stats table runner
+- `products/web_analytics/backend/hogql_queries/web_stats_paths_lazy_precompute.py` — PATHS lazy path
+- `products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py` — shared eligibility gate + helpers
- `posthog/clickhouse/preaggregation/web_overview_preaggregated_sql.py` — overview schema
- `posthog/clickhouse/preaggregation/web_stats_paths_preaggregated_sql.py` — PATHS schema
- `products/web_analytics/backend/hogql_queries/web_vitals_path_breakdown.py` — vitals runner