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(analytics-platform): move the serve-stale eligibility gate next to the lazy executor (#70563)

Javier Bahamondesbf7d71eaa129
diff --git a/products/analytics_platform/backend/lazy_computation/stale_policy.py b/products/analytics_platform/backend/lazy_computation/stale_policy.py
new file mode 100644
index 000000000000..e2d1e5cf2180
--- /dev/null
+++ b/products/analytics_platform/backend/lazy_computation/stale_policy.py
@@ -0,0 +1,40 @@
+"""Who may be served stale, and for how long.
+
+The executor's `stale_while_revalidate_seconds` is the *serve* half of RFC 5861: a request that would
+otherwise materialize inline gets its complete-but-stale rows back instantly instead of blocking. The
+rule for *who* may take that grace is the same in every product, and getting it wrong is a data-freezing
+bug rather than a slow query — a background refresher served its own stale rows persists them as a fresh
+result and never recomputes, so nothing refreshes again. Hence one implementation, here, next to the
+executor, instead of one per product.
+
+Products supply the triggers of their own warmers; the CACHE_WARMUP feature tag is the primary,
+product-agnostic gate. Callers that take the grace must have a revalidation path (a background refresher,
+or an enqueue on `stale=True`), otherwise they serve stale until the grace runs out.
+"""
+
+from posthog.clickhouse.query_tagging import Feature, get_query_tag_value
+
+# Refreshers every product inherits. The generic insight cache warmer's CACHE_WARMUP feature tag does not
+# always survive to the ensure call (lazy modules re-stamp feature=QUERY before ensuring), but its trigger
+# does — without it, warmer runs get served stale and persist that into the insight cache as fresh.
+SHARED_BACKGROUND_WARMING_TRIGGERS = frozenset({"warmingV2"})
+
+
+def is_background_warming_request(extra_triggers: frozenset[str] = frozenset()) -> bool:
+ """True when this request *is* a refresh mechanism rather than a consumer of one.
+
+ `extra_triggers` are the calling product's own warmer triggers, unioned with the shared set. The
+ feature tag is the primary gate: it classifies refreshers by category, including warmers the caller
+ does not know by name.
+ """
+ if get_query_tag_value("feature") == Feature.CACHE_WARMUP:
+ return True
+ return get_query_tag_value("trigger") in (SHARED_BACKGROUND_WARMING_TRIGGERS | extra_triggers)
+
+
+def resolve_stale_while_revalidate_seconds(
+ grace_seconds: float,
+ extra_triggers: frozenset[str] = frozenset(),
+) -> float | None:
+ """The serve-stale grace this request may take: None for refreshers, `grace_seconds` for user reads."""
+ return None if is_background_warming_request(extra_triggers) else grace_seconds
diff --git a/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py b/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py
index 632a9975147a..4f074ca9960f 100644
--- a/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py
+++ b/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py
@@ -28,7 +28,7 @@ from posthog.hogql.property import property_to_expr
from posthog.hogql.transforms.preaggregated_table_transformation import is_integer_timezone
from posthog import redis
-from posthog.clickhouse.query_tagging import Feature, get_query_tag_value, tag_queries
+from posthog.clickhouse.query_tagging import tag_queries
from posthog.models import Team
from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (
@@ -37,6 +37,10 @@ from products.analytics_platform.backend.lazy_computation.lazy_computation_execu
ensure_precomputed,
parse_ttl_schedule,
)
+from products.analytics_platform.backend.lazy_computation.stale_policy import (
+ is_background_warming_request as shared_is_background_warming_request,
+ resolve_stale_while_revalidate_seconds,
+)
logger = structlog.get_logger(__name__)
@@ -110,23 +114,17 @@ def list_oom_pinned_team_ids() -> list[int]:
# so the two cannot drift apart.
REVALIDATION_TRIGGER = "webAnalyticsStaleRevalidation"
-# Requests tagged with any of these triggers ARE the refresh mechanism: they must never
-# be served stale (they'd freeze the cache serving stale to themselves) and they keep
-# the framework's full wait budget. This named set is the belt; the primary gate in
-# `is_background_warming_request` is the CACHE_WARMUP feature tag, which classifies
-# refreshers by category — including warmers this module doesn't know by name (e.g.
-# the generic insight cache warmer, trigger "warmingV2"), which would otherwise be
-# served stale and persist it into the insight cache under a fresh timestamp.
+# Web's own refreshers. These ARE the refresh mechanism: they must never be served stale
+# (they'd freeze the cache serving stale to themselves) and they keep the framework's full
+# wait budget. Warmers shared across products (e.g. the generic insight cache warmer,
+# trigger "warmingV2") live in SHARED_BACKGROUND_WARMING_TRIGGERS and are unioned in by
+# `is_background_warming_request`. This named set is the belt; the primary gate there is the
+# CACHE_WARMUP feature tag, which classifies refreshers by category — including warmers this
+# module doesn't know by name.
BACKGROUND_WARMING_TRIGGERS = frozenset(
{
"webAnalyticsEagerBaselineWarming",
"webAnalyticsQueryWarming",
- # The generic insight cache warmer. Its Feature.CACHE_WARMUP tag does NOT
- # survive to the ensure call (the lazy modules re-stamp feature=QUERY before
- # ensuring), but the trigger does — without it here, warmer runs would be
- # served stale and persist stale rows into the insight cache as a fresh
- # blocking result.
- "warmingV2",
REVALIDATION_TRIGGER,
}
)
@@ -160,9 +158,7 @@ WEB_ANALYTICS_LAZY_PRECOMPUTE_REVALIDATION_ENQUEUE_FAILED = Counter(
def is_background_warming_request() -> bool:
- if get_query_tag_value("feature") == Feature.CACHE_WARMUP:
- return True
- return get_query_tag_value("trigger") in BACKGROUND_WARMING_TRIGGERS
+ return shared_is_background_warming_request(BACKGROUND_WARMING_TRIGGERS)
# One revalidation per (team, family, query shape) per window: a dashboard burst — or a
@@ -231,8 +227,8 @@ def web_ensure_precomputed(*, team: Team, **kwargs: Any) -> LazyComputationResul
result to `handle_stale_served` so the background revalidation actually happens.
"""
if "stale_while_revalidate_seconds" not in kwargs:
- kwargs["stale_while_revalidate_seconds"] = (
- None if is_background_warming_request() else STALE_WHILE_REVALIDATE_SECONDS
+ kwargs["stale_while_revalidate_seconds"] = resolve_stale_while_revalidate_seconds(
+ STALE_WHILE_REVALIDATE_SECONDS, BACKGROUND_WARMING_TRIGGERS
)
pinned = is_team_oom_pinned(team.id)
if "ttl_seconds" in kwargs: