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): add dagster job to clear precompute oom pins (#69969)

Lucas Ricoya0d0005231c6
diff --git a/posthog/dags/locations/web_analytics.py b/posthog/dags/locations/web_analytics.py
index 0426486155e3..9e8b252e44e4 100644
--- a/posthog/dags/locations/web_analytics.py
+++ b/posthog/dags/locations/web_analytics.py
@@ -6,6 +6,7 @@ from products.marketing_analytics.dags import marketing_precompute
from products.web_analytics.dags import (
cache_favicons,
cache_warming,
+ clear_oom_pins,
eager_web_analytics_precompute,
web_analytics_watchdog,
web_dimensional_precompute,
@@ -58,6 +59,7 @@ defs = dagster.Definitions(
web_dimensional_precompute.web_dimensional_precompute_job,
marketing_precompute.marketing_precompute_job,
cache_favicons.cache_authorized_domain_favicons_job,
+ clear_oom_pins.web_analytics_clear_precompute_oom_pins_job,
],
schedules=schedules,
loggers=loggers,
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 eec2613dfabc..632a9975147a 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
@@ -74,6 +74,25 @@ def pin_team_oom(team_id: int) -> None:
logger.warning("web_precompute.oom_pin_failed", team_id=team_id, exc_info=True)
+def clear_team_oom_pin(team_id: int) -> bool:
+ """Remove a team's OOM pin so its next inserts run at full window width again.
+
+ Returns whether a pin existed. Unlike the read/write helpers this is NOT
+ best-effort: it is only called from operator tooling, where a Redis failure
+ should surface instead of silently reporting the pin as cleared."""
+ return redis.get_client().delete(_oom_pin_key(team_id)) > 0
+
+
+def list_oom_pinned_team_ids() -> list[int]:
+ """Team ids currently OOM-pinned, via a prefix SCAN (operator tooling only)."""
+ prefix = TEAM_OOM_PIN_REDIS_PREFIX
+ team_ids = []
+ for key in redis.get_client().scan_iter(match=f"{prefix}*"):
+ key_str = key.decode() if isinstance(key, bytes) else key
+ team_ids.append(int(key_str[len(prefix) :]))
+ return sorted(team_ids)
+
+
# --- Stale-while-revalidate (RFC 5861) -------------------------------------------------
#
# The lazy executor's `stale_while_revalidate_seconds` is the *serve* half: user-facing
diff --git a/products/web_analytics/dags/clear_oom_pins.py b/products/web_analytics/dags/clear_oom_pins.py
new file mode 100644
index 000000000000..58afb93d46f7
--- /dev/null
+++ b/products/web_analytics/dags/clear_oom_pins.py
@@ -0,0 +1,63 @@
+"""Operator job to list or clear web-analytics precompute OOM pins.
+
+A pinned team's precompute inserts are capped to 1-day windows (set reactively after a
+MEMORY_LIMIT_EXCEEDED insert, self-expiring in 14 days). Clear a pin when the underlying
+pressure is gone, e.g. after a cluster upscale or a stored-row cap reduction, so the
+team's inserts run at full band width again.
+
+Run manually from the Dagster UI. With no run config it only lists the pinned teams (dry
+run); clearing requires explicitly setting `team_id` or `clear_all: true`, which is the
+confirmation step — every pin exists because that team recently OOMed a full-width
+insert, so a global clear re-exposes all of them at once.
+"""
+
+import dagster
+
+from posthog.dags.common import JobOwners
+
+from products.web_analytics.backend.hogql_queries.web_lazy_precompute_common import (
+ clear_team_oom_pin,
+ list_oom_pinned_team_ids,
+)
+
+
+class ClearOomPinsConfig(dagster.Config):
+ team_id: int | None = None
+ clear_all: bool = False
+
+
+def clear_precompute_oom_pins_op(context: dagster.OpExecutionContext, config: ClearOomPinsConfig) -> None:
+ if config.team_id is not None and config.clear_all:
+ raise dagster.Failure("Pass team_id or clear_all, not both.")
+
+ pinned = list_oom_pinned_team_ids()
+ context.log.info(f"OOM-pinned teams: {pinned or 'none'}")
+
+ if config.team_id is not None:
+ if clear_team_oom_pin(config.team_id):
+ context.log.info(f"Cleared OOM pin for team {config.team_id}")
+ else:
+ context.log.info(f"Team {config.team_id} was not pinned; nothing to clear")
+ return
+
+ if config.clear_all:
+ for team_id in pinned:
+ clear_team_oom_pin(team_id)
+ context.log.info(f"Cleared {len(pinned)} OOM pin(s)")
+ return
+
+ context.log.info("Dry run: no team_id or clear_all in run config, nothing cleared")
+
+
+ name="web_analytics_clear_precompute_oom_pins",
+ description=(
+ "List or clear web-analytics precompute OOM pins. Without run config this is a "
+ "dry run that lists pinned teams; set `team_id` to clear one pin or "
+ "`clear_all: true` to clear every pin."
+ ),
+ tags={"owner": JobOwners.TEAM_WEB_ANALYTICS.value},
+)
+def web_analytics_clear_precompute_oom_pins_job() -> None:
+ clear_precompute_oom_pins_op()
diff --git a/products/web_analytics/dags/tests/test_clear_oom_pins.py b/products/web_analytics/dags/tests/test_clear_oom_pins.py
new file mode 100644
index 000000000000..d1d5dc181ec6
--- /dev/null
+++ b/products/web_analytics/dags/tests/test_clear_oom_pins.py
@@ -0,0 +1,50 @@
+import dagster
+
+from posthog import redis
+
+from products.web_analytics.backend.hogql_queries.web_lazy_precompute_common import (
+ _oom_pin_key,
+ is_team_oom_pinned,
+ pin_team_oom,
+)
+from products.web_analytics.dags.clear_oom_pins import web_analytics_clear_precompute_oom_pins_job
+
+TEAMS = (901901, 901902)
+
+
+def _run(config: dict) -> dagster.ExecuteInProcessResult:
+ return web_analytics_clear_precompute_oom_pins_job.execute_in_process(
+ run_config={"ops": {"clear_precompute_oom_pins_op": {"config": config}}},
+ raise_on_error=False,
+ )
+
+
+class TestClearOomPinsJob:
+ def setup_method(self):
+ for team_id in TEAMS:
+ pin_team_oom(team_id)
+
+ def teardown_method(self):
+ for team_id in TEAMS:
+ redis.get_client().delete(_oom_pin_key(team_id))
+
+ def test_dry_run_clears_nothing(self):
+ result = _run({})
+ assert result.success
+ assert all(is_team_oom_pinned(t) for t in TEAMS)
+
+ def test_clears_single_team(self):
+ result = _run({"team_id": TEAMS[0]})
+ assert result.success
+ assert is_team_oom_pinned(TEAMS[0]) is False
+ assert is_team_oom_pinned(TEAMS[1]) is True
+
+ def test_clear_all(self):
+ result = _run({"clear_all": True})
+ assert result.success
+ assert not any(is_team_oom_pinned(t) for t in TEAMS)
+
+ def test_conflicting_config_fails_and_clears_nothing(self):
+ result = _run({"team_id": TEAMS[0], "clear_all": True})
+ assert not result.success
+ assert all(is_team_oom_pinned(t) for t in TEAMS)