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(web-analytics): force refresh bypasses lazy-precompute serve-stale grace (#73693)

posthog[bot]8f3bf54d90bd
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_lazy_precompute_common.py b/products/web_analytics/backend/hogql_queries/test/test_web_lazy_precompute_common.py
index 851f827bae5b..6406db6abb47 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_lazy_precompute_common.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_lazy_precompute_common.py
@@ -25,7 +25,7 @@ from posthog.hogql import ast
from posthog import redis
from posthog.clickhouse.query_tagging import Feature, get_query_tag_value, reset_query_tags, tags_context
-from posthog.hogql_queries.query_runner import AnalyticsQueryRunner
+from posthog.hogql_queries.query_runner import AnalyticsQueryRunner, ExecutionMode
from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (
LazyComputationResult,
@@ -543,6 +543,26 @@ class TestWebEnsurePrecomputed(BaseTest):
else:
assert grace is None, f"background context {tags} must not be served stale"
+ @parameterized.expand(
+ [
+ # A user-initiated force refresh (the "Reload" button -> force_blocking for
+ # non-insight web tiles) must bypass the grace so it recomputes fresh instead
+ # of being handed the very stale row it is trying to clear (the reported bug).
+ ("force_blocking", ExecutionMode.CALCULATE_BLOCKING_ALWAYS.value, None),
+ ("force_async", ExecutionMode.CALCULATE_ASYNC_ALWAYS.value, None),
+ # A normal (non-forced) read must keep the grace, or every read pays a
+ # synchronous recompute and the serve-stale optimization is lost.
+ ("blocking", ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE.value, STALE_WHILE_REVALIDATE_SECONDS),
+ ]
+ )
+ @mock.patch(f"{_COMMON}.ensure_precomputed")
+ def test_forced_refresh_bypasses_stale_grace(self, _name, execution_mode, expected_grace, mock_ensure):
+ mock_ensure.return_value = LazyComputationResult(ready=True, job_ids=[])
+ reset_query_tags()
+ with tags_context(execution_mode=execution_mode):
+ web_ensure_precomputed(team=self.team, ttl_seconds={"default": 3600}, table=None)
+ assert mock_ensure.call_args.kwargs["stale_while_revalidate_seconds"] == expected_grace
+
class TestStaleRevalidationEnqueue(BaseTest):
def setUp(self):
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 6b025995206f..a61bc0f6f536 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
@@ -29,7 +29,8 @@ from posthog.hogql.property import get_property_type, property_to_expr
from posthog.hogql.transforms.preaggregated_table_transformation import is_integer_timezone
from posthog import redis
-from posthog.clickhouse.query_tagging import tag_queries
+from posthog.clickhouse.query_tagging import get_query_tag_value, tag_queries
+from posthog.hogql_queries.query_runner import ExecutionMode
from posthog.models import Team
from products.access_control.backend.facade.api import team_has_property_access_rules
@@ -222,10 +223,31 @@ def is_background_warming_request() -> bool:
return shared_is_background_warming_request(BACKGROUND_WARMING_TRIGGERS)
+# Execution modes that mean "the user explicitly asked to recompute, disregard any
+# cache". The frontend's forced reload (`reloadAll`) maps a web-analytics tile to
+# `force_blocking` — web tiles are non-insight nodes, so they never take the async
+# path — but both force_* modes are treated as forced so an async-dispatched force
+# refresh is covered too.
+FORCED_REFRESH_EXECUTION_MODES = frozenset(
+ {ExecutionMode.CALCULATE_BLOCKING_ALWAYS.value, ExecutionMode.CALCULATE_ASYNC_ALWAYS.value}
+)
+
+
+def is_forced_refresh_request() -> bool:
+ """True when this is an explicit user-initiated force refresh.
+
+ Read off the `execution_mode` query tag the query runner stamps before
+ `_calculate`. A forced refresh must bypass the serve-stale grace so it
+ recomputes fresh instead of being handed the very stale row it is clearing.
+ """
+ return get_query_tag_value("execution_mode") in FORCED_REFRESH_EXECUTION_MODES
+
+
# One revalidation per (team, family, query shape) per window: a dashboard burst — or a
-# user hammering forced refresh on a stale tile — fires many stale serves for the same
-# shape and they must collapse to a single background rebuild. Keyed per shape (not per
-# request) so two different stale families in one request each still get their refresh.
+# user hammering forced refresh, whose reads now fall through to the live query and enqueue
+# a background warm each — fires many enqueues for the same shape and they must collapse to
+# a single background rebuild. Keyed per shape (not per request) so two different stale
+# families in one request each still get their refresh.
REVALIDATION_DEBOUNCE_SECONDS = 10 * 60
# The shape debounce alone does not bound DISTINCT shapes: filters and date ranges are
@@ -332,9 +354,20 @@ def web_ensure_precomputed(*, team: Team, **kwargs: Any) -> LazyComputationResul
family = kwargs.pop("family", None)
background = is_background_warming_request()
if "stale_while_revalidate_seconds" not in kwargs:
- kwargs["stale_while_revalidate_seconds"] = resolve_stale_while_revalidate_seconds(
- STALE_WHILE_REVALIDATE_SECONDS, BACKGROUND_WARMING_TRIGGERS
- )
+ if is_forced_refresh_request():
+ # An explicit user-initiated force refresh must never be handed a
+ # complete-but-stale row — that is exactly the state the user is trying
+ # to clear (the reported bug: repeated Reload clicks kept serving the
+ # same stale overview tile). Disabling the grace makes the ensure report
+ # a miss for an expired window, so the read falls through to the live
+ # query and recomputes fresh. Applies to every web family (overview +
+ # stats) so a forced refresh can't leave the tile and tables drifting on
+ # different freshness clocks.
+ kwargs["stale_while_revalidate_seconds"] = None
+ else:
+ kwargs["stale_while_revalidate_seconds"] = resolve_stale_while_revalidate_seconds(
+ STALE_WHILE_REVALIDATE_SECONDS, BACKGROUND_WARMING_TRIGGERS
+ )
# User-facing requests never compute inline: they are served from covering
# READY jobs (fresh or within the stale grace) or told "miss" immediately so
# the caller falls back to the live query. Construction happens only on