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(web-analytics): warmer throughput, progress logging, and churn-safety (#74030)

Lucas Ricoy8090703ecd97
diff --git a/posthog/settings/dynamic_settings.py b/posthog/settings/dynamic_settings.py
index cd74cc855748..bfa215b653ac 100644
--- a/posthog/settings/dynamic_settings.py
+++ b/posthog/settings/dynamic_settings.py
@@ -319,6 +319,14 @@ CONSTANCE_CONFIG = {
"of more background compute.",
int,
),
+ "WEB_ANALYTICS_WARMING_SHAPE_CONCURRENCY": (
+ get_from_env("WEB_ANALYTICS_WARMING_SHAPE_CONCURRENCY", default=16, type_cast=int),
+ "Worker threads for the web analytics warm pass. Cold bucket builds dominate a first run "
+ "(each shape builds one bucket per day of its range), so a higher value drains the selection "
+ "faster but adds load to the offline ClickHouse pool. Clamped to 1-64; the pool is fixed per "
+ "pass, so a change applies when the next warming run starts — no redeploy needed.",
+ int,
+ ),
}
SETTINGS_ALLOWING_API_OVERRIDE = (
@@ -376,6 +384,7 @@ SETTINGS_ALLOWING_API_OVERRIDE = (
"WEB_ANALYTICS_WARMING_SELECTION_TTL_SECONDS",
"WEB_ANALYTICS_WARMING_MIN_QUERY_COUNT",
"WEB_ANALYTICS_WARMING_MAX_SHAPES",
+ "WEB_ANALYTICS_WARMING_SHAPE_CONCURRENCY",
)
# SECRET_SETTINGS can only be updated but will never be exposed through the API (we do store them plain text in the DB)
diff --git a/products/web_analytics/dags/cache_warming.py b/products/web_analytics/dags/cache_warming.py
index c7a10f650bcd..9ad2b8b89e7b 100644
--- a/products/web_analytics/dags/cache_warming.py
+++ b/products/web_analytics/dags/cache_warming.py
@@ -4,9 +4,10 @@ import json
import time
import threading
import statistics
-from concurrent.futures import ThreadPoolExecutor
+from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import TYPE_CHECKING, Optional
+from django.core.exceptions import ObjectDoesNotExist
from django.db import close_old_connections
from django.utils.dateparse import parse_datetime
@@ -65,7 +66,9 @@ WARMING_SHAPES_SELECTED_GAUGE = Gauge(
WARMING_QUERIES_COUNTER = Counter(
"posthog_web_analytics_warming_queries_total",
"Web analytics warming outcomes per query shape",
- ["outcome"], # warmed | skipped_fresh | skipped_duplicate | skipped_raw_low_demand | failed | unsupported
+ # warmed | skipped_fresh | skipped_duplicate | skipped_raw_low_demand |
+ # skipped_cold | skipped_already_warmed | failed | unsupported
+ ["outcome"],
)
logger = structlog.get_logger(__name__)
@@ -587,15 +590,56 @@ def get_warmable_queries_op(context: dagster.OpExecutionContext) -> list[dict]:
RAW_REPLAY_MIN_QUERY_COUNT = 10
# Worker threads for the warm pass. The pass is IO-bound (cache checks, CH
-# reads/inserts), so a small pool cuts wall time ~8x at the widened selection
-# size; kept well under the OFFLINE per-user query-slot budget so a build wave
-# can't starve other traffic (the same slot pool the inline-build saturation
-# incidents exhausted).
-WARMING_SHAPE_CONCURRENCY = 8
+# reads/inserts), so a pool cuts wall time at the widened selection size. A cold
+# first run is dominated by per-day bucket builds — hundreds of thousands of them
+# — so this is the main throughput lever, but raising it adds load to the offline
+# ClickHouse pool. Overridable via WEB_ANALYTICS_WARMING_SHAPE_CONCURRENCY without
+# a redeploy; the pool is fixed for the life of a pass, so a change applies when
+# the next run starts. This is the fallback when the setting is unset.
+WARMING_SHAPE_CONCURRENCY = 16
+
+# Heartbeat cadence for the warm pass. Cold bucket builds run ~1s each, so a full
+# selection can take hours; without a heartbeat the op is silent start to finish
+# and a long run is indistinguishable from a hung one.
+WARMING_PROGRESS_LOG_INTERVAL_SECONDS = 120
+
+
+def _team_still_exists(team_id: int) -> bool:
+ # Thin DB boundary so tests can pin the answer: pool worker threads hold their
+ # own connections, which can't see a TestCase's uncommitted rows.
+ return Team.objects.filter(pk=team_id).exists()
+
+
+class WarmQueriesConfig(dagster.Config):
+ """Launchpad knobs for targeted warming runs. The hourly schedule passes no
+ config, so it keeps the defaults; a manual launch can scope a run.
+
+ The concurrent-run guard makes launches of this job mutually exclusive with
+ the hourly schedule, so bound a manual backfill with `limit` — an unbounded
+ cold backfill can run for hours and starve the hourly refresh the whole time.
+ """
+
+ # full: warm everything selected (schedule default). refresh: only shapes
+ # already warmed once (cache entry exists) — cheap freshness pass, no cold
+ # builds. backfill: only never-warmed shapes (no cache entry) — coverage
+ # expansion without re-touching the warm set.
+ mode: str = "full"
+ # Restrict to specific teams (empty = all selected teams).
+ team_ids: list[int] = []
+ # Process at most this many shapes, hottest first (0 = no limit).
+ limit: int = 0
@dagster.op(retry_policy=cache_warming_retry_policy)
-def warm_queries_op(context: dagster.OpExecutionContext, queries: list[dict]) -> None:
+def warm_queries_op(context: dagster.OpExecutionContext, config: WarmQueriesConfig, queries: list[dict]) -> None:
+ if config.mode not in ("full", "refresh", "backfill"):
+ raise ValueError(f"Unknown warming mode {config.mode!r} (expected full, refresh, or backfill)")
+ if config.team_ids:
+ wanted = set(config.team_ids)
+ queries = [q for q in queries if q["team_id"] in wanted]
+ if config.limit > 0:
+ queries = queries[: config.limit]
+
team_ids = {q["team_id"] for q in queries}
teams: dict[int, Team] = {t.pk: t for t in Team.objects.filter(pk__in=team_ids)}
missing_teams = team_ids - teams.keys()
@@ -657,6 +701,18 @@ def warm_queries_op(context: dagster.OpExecutionContext, queries: list[dict]) ->
seen_cache_keys.add((team.pk, cache_key))
entry = QueryCache(team_id=team.pk, cache_key=cache_key).lookup().entry
+
+ # The cache entry doubles as the warm/cold discriminator: a shape
+ # warmed at least once has one (possibly stale); a never-warmed shape
+ # doesn't. refresh keeps the warm set fresh without paying for cold
+ # builds; backfill expands coverage without re-touching the warm set.
+ if config.mode == "refresh" and entry is None:
+ WARMING_QUERIES_COUNTER.labels(outcome="skipped_cold").inc()
+ return "skipped_cold"
+ if config.mode == "backfill" and entry is not None:
+ WARMING_QUERIES_COUNTER.labels(outcome="skipped_already_warmed").inc()
+ return "skipped_already_warmed"
+
cached_data = entry.as_full_response() if entry else None
if cached_data is not None:
@@ -670,6 +726,18 @@ def warm_queries_op(context: dagster.OpExecutionContext, queries: list[dict]) ->
WARMING_QUERIES_COUNTER.labels(outcome="warmed").inc()
return "warmed"
except Exception as e:
+ # A team deleted after the teams dict was loaded (the 14-day demand
+ # window churns teams out) surfaces as a DoesNotExist from
+ # get_cache_key: it reads a team extension via get-or-create, whose
+ # create hits the team foreign key and leaves the lookup raising the
+ # extension's DoesNotExist. That's not a warming failure — skip it
+ # quietly rather than logging a traceback and firing error tracking
+ # for every churned team. Verified against the DB rather than keyed on
+ # the exception type alone: other models raise DoesNotExist too (a
+ # cohort filter whose cohort was deleted mid-window), and for a live
+ # team those are genuine failures that must still report.
+ if isinstance(e, ObjectDoesNotExist) and not _team_still_exists(team.pk):
+ return "team_missing"
# Module logger, not context.log: Dagster's log manager isn't
# guaranteed thread-safe, and workers fail concurrently.
logger.exception(
@@ -685,26 +753,65 @@ def warm_queries_op(context: dagster.OpExecutionContext, queries: list[dict]) ->
# a long pass doesn't accumulate stale connections per thread.
close_old_connections()
+ # Clamped: a non-positive value would abort every run at pool construction and
+ # an oversized one can exhaust process threads. The pool is fixed for the life
+ # of the pass, so a settings change applies when the next run starts.
+ concurrency = min(
+ 64, max(1, get_instance_setting("WEB_ANALYTICS_WARMING_SHAPE_CONCURRENCY") or WARMING_SHAPE_CONCURRENCY)
+ )
outcomes: dict[str, int] = {}
- with ThreadPoolExecutor(max_workers=WARMING_SHAPE_CONCURRENCY) as pool:
- for outcome in pool.map(_warm_one, queries):
+ total = len(queries)
+ processed = 0
+ started_at = time.monotonic()
+ last_log_at = started_at
+ context.log.info(
+ f"Warming {total} shapes across {len(teams)} teams (mode={config.mode}, concurrency={concurrency})"
+ )
+ with ThreadPoolExecutor(max_workers=concurrency) as pool:
+ # Futures are consumed by completion, not input order: with pool.map one
+ # slow early shape would block this loop — and the heartbeat — while later
+ # workers finish thousands of shapes. Consuming on the op thread also keeps
+ # context.log here safe, unlike the worker-thread logging inside _warm_one.
+ futures = [pool.submit(_warm_one, query_info) for query_info in queries]
+ for future in as_completed(futures):
+ outcome = future.result()
outcomes[outcome] = outcomes.get(outcome, 0) + 1
+ processed += 1
+ now = time.monotonic()
+ if now - last_log_at >= WARMING_PROGRESS_LOG_INTERVAL_SECONDS:
+ elapsed = now - started_at
+ rate = processed / elapsed
+ eta_min = (total - processed) / rate / 60 if rate > 0 else 0
+ breakdown = ", ".join(f"{k}={v}" for k, v in sorted(outcomes.items()))
+ context.log.info(
+ f"Warming progress: {processed}/{total} ({100 * processed // total}%) "
+ f"at {rate:.0f}/s, ETA ~{eta_min:.0f}m — {breakdown}"
+ )
+ last_log_at = now
queries_warmed = outcomes.get("warmed", 0)
queries_skipped = outcomes.get("skipped_fresh", 0)
queries_failed = outcomes.get("failed", 0)
queries_unsupported = outcomes.get("unsupported", 0)
+ final_breakdown = ", ".join(f"{k}={v}" for k, v in sorted(outcomes.items()))
context.log.info(
- f"Warmed {queries_warmed} queries ({queries_skipped} already fresh, "
- f"{queries_failed} failed, {queries_unsupported} unsupported kinds)"
+ f"Warmed {queries_warmed} queries in {(time.monotonic() - started_at) / 60:.1f}m "
+ f"(mode={config.mode}: {final_breakdown})"
)
context.add_output_metadata(
{
"queries_warmed": queries_warmed,
"queries_skipped": queries_skipped,
+ "queries_skipped_duplicate": outcomes.get("skipped_duplicate", 0),
+ "queries_skipped_raw_low_demand": outcomes.get("skipped_raw_low_demand", 0),
+ "queries_skipped_cold": outcomes.get("skipped_cold", 0),
+ "queries_skipped_already_warmed": outcomes.get("skipped_already_warmed", 0),
+ "teams_missing": outcomes.get("team_missing", 0),
"queries_failed": queries_failed,
"queries_unsupported": queries_unsupported,
+ "concurrency": concurrency,
+ "mode": config.mode,
}
)
diff --git a/products/web_analytics/dags/tests/test_cache_warming.py b/products/web_analytics/dags/tests/test_cache_warming.py
index 9f884c23e67c..9e5097a777ea 100644
--- a/products/web_analytics/dags/tests/test_cache_warming.py
+++ b/products/web_analytics/dags/tests/test_cache_warming.py
@@ -8,9 +8,11 @@ import dagster
from parameterized import parameterized
from posthog.clickhouse.query_tagging import Feature, get_query_tags, reset_query_tags, tag_queries
+from posthog.models import Team
from products.web_analytics.backend.hogql_queries.web_lazy_precompute_common import is_background_warming_request
from products.web_analytics.dags.cache_warming import (
+ WarmQueriesConfig,
build_replay_runner,
deepen_to_widest_warmable_range,
get_warmable_queries_op,
@@ -395,6 +397,7 @@ class TestWarmQueriesOp(BaseTest):
mock_cm.return_value.lookup.return_value.entry = None
warm_queries_op(
dagster.build_op_context(),
+ WarmQueriesConfig(),
[
{
"team_id": self.team.pk,
@@ -411,6 +414,122 @@ class TestWarmQueriesOp(BaseTest):
self.assertEqual(runner.run.call_count, expected_runs)
+ @parameterized.expand(
+ [
+ # A churned team surfaces as a DoesNotExist from get_cache_key (the
+ # team-extension FK). That must be a quiet skip, not a logged failure
+ # plus an error-tracking event per churned team — which spammed
+ # tracebacks in prod. But DoesNotExist alone isn't proof the team is
+ # gone — other models raise it too (a cohort filter whose cohort was
+ # deleted) — so for a live team it must still report, as must any
+ # other error.
+ ("churned_team_does_not_exist", Team.DoesNotExist, False, 0),
+ ("live_team_other_model_does_not_exist", Team.DoesNotExist, True, 1),
+ ("genuine_failure", RuntimeError, True, 1),
+ ]
+ )
+ def test_churned_team_skipped_but_real_failure_reported(
+ self, _name: str, raised: type[Exception], team_exists: bool, expected_capture_calls: int
+ ) -> None:
+ runner = MagicMock()
+ runner.get_cache_key.side_effect = raised("boom")
+ with (
+ patch(
+ "products.web_analytics.dags.cache_warming.build_replay_runner",
+ return_value=(runner, {}, True),
+ ),
+ patch("products.web_analytics.dags.cache_warming.capture_exception") as mock_capture,
+ # Pinned because pool worker threads hold their own DB connections and
+ # can't see this TestCase's uncommitted team row.
+ patch("products.web_analytics.dags.cache_warming._team_still_exists", return_value=team_exists),
+ ):
+ warm_queries_op(
+ dagster.build_op_context(),
+ WarmQueriesConfig(),
+ [
+ {
+ "team_id": self.team.pk,
+ "query_json": {"kind": "WebOverviewQuery", "properties": []},
+ "query_count": 5,
+ "representative_query_count": 5,
+ "normalized_query_hash": "h",
+ }
+ ],
+ )
+
+ self.assertEqual(mock_capture.call_count, expected_capture_calls)
+ self.assertEqual(runner.run.call_count, 0)
+
+ @parameterized.expand(
+ [
+ # Mode gates on the cache entry (warm/cold discriminator). Inverting
+ # either condition is a real operational hazard: backfill re-running
+ # the warm set repeats the hours-long cold rebuild the mode exists to
+ # avoid, and refresh cold-building defeats its cheap-pass purpose.
+ ("full_runs_cold", "full", False, 1),
+ ("refresh_skips_cold", "refresh", False, 0),
+ ("refresh_runs_warm_stale", "refresh", True, 1),
+ ("backfill_runs_cold", "backfill", False, 1),
+ ("backfill_skips_warm", "backfill", True, 0),
+ ]
+ )
+ def test_mode_gates_on_warm_state(self, _name: str, mode: str, has_entry: bool, expected_runs: int) -> None:
+ runner = MagicMock()
+ runner.get_cache_key.return_value = f"key-{_name}"
+ runner._is_stale.return_value = True
+ with (
+ patch("products.web_analytics.dags.cache_warming.build_replay_runner", return_value=(runner, {}, True)),
+ patch("products.web_analytics.dags.cache_warming.QueryCache") as mock_cm,
+ ):
+ entry = None
+ if has_entry:
+ entry = MagicMock()
+ entry.as_full_response.return_value = {"last_refresh": "2026-07-01T00:00:00Z"}
+ mock_cm.return_value.lookup.return_value.entry = entry
+ warm_queries_op(
+ dagster.build_op_context(),
+ WarmQueriesConfig(mode=mode),
+ [
+ {
+ "team_id": self.team.pk,
+ "query_json": {"kind": "WebOverviewQuery", "properties": []},
+ "query_count": 5,
+ "representative_query_count": 5,
+ "normalized_query_hash": "h",
+ }
+ ],
+ )
+
+ self.assertEqual(runner.run.call_count, expected_runs)
+
+ def test_team_ids_and_limit_scope_the_run(self) -> None:
+ # A Launchpad run scoped to one team must not warm the fleet: launches
+ # are mutually exclusive with the hourly schedule, so an unscoped manual
+ # run starves it for the duration.
+ other_team = Team.objects.create(organization=self.organization, name="other")
+ runner = MagicMock()
+ runner.get_cache_key.side_effect = lambda: f"key-{len(runner.mock_calls)}"
+ shape = lambda team_id, n: { # noqa: E731
+ "team_id": team_id,
+ "query_json": {"kind": "WebOverviewQuery", "properties": [], "n": n},
+ "query_count": 5,
+ "representative_query_count": 5,
+ "normalized_query_hash": f"h{n}",
+ }
+ with (
+ patch("products.web_analytics.dags.cache_warming.build_replay_runner", return_value=(runner, {}, True)),
+ patch("products.web_analytics.dags.cache_warming.QueryCache") as mock_cm,
+ ):
+ mock_cm.return_value.lookup.return_value.entry = None
+ warm_queries_op(
+ dagster.build_op_context(),
+ WarmQueriesConfig(team_ids=[self.team.pk], limit=2),
+ [shape(self.team.pk, 1), shape(other_team.pk, 2), shape(self.team.pk, 3), shape(self.team.pk, 4)],
+ )
+
+ # 3 shapes match the team filter; limit=2 caps it.
+ self.assertEqual(runner.run.call_count, 2)
+
def test_duplicate_cache_keys_warm_once(self) -> None:
# Selection groups by raw JSON text, so two encodings of one query can
# both be selected; replaying both wastes ClickHouse capacity and
@@ -424,6 +543,7 @@ class TestWarmQueriesOp(BaseTest):
mock_cm.return_value.lookup.return_value.entry = None
warm_queries_op(
dagster.build_op_context(),
+ WarmQueriesConfig(),
[
{
"team_id": self.team.pk,
@@ -464,6 +584,7 @@ class TestWarmQueriesOp(BaseTest):
):
warm_queries_op(
dagster.build_op_context(),
+ WarmQueriesConfig(),
[{**shape, "normalized_query_hash": "a"}, {**shape, "normalized_query_hash": "b"}],
)
@@ -483,6 +604,7 @@ class TestWarmQueriesOp(BaseTest):
with patch("products.web_analytics.dags.cache_warming.get_query_runner_or_none", side_effect=capture_tags):
warm_queries_op(
dagster.build_op_context(),
+ WarmQueriesConfig(),
[
{
"team_id": self.team.pk,
@@ -501,6 +623,7 @@ class TestWarmQueriesOp(BaseTest):
# "unsupported", not "failed" — or every hourly run pages Sentry.
warm_queries_op(
dagster.build_op_context(),
+ WarmQueriesConfig(),
[{"team_id": self.team.pk, "query_json": {"kind": "WebVitalsQuery"}, "normalized_query_hash": "h"}],
)