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(experiments): Remove retention preaggregation kill-switch flag (#102073)

Juraj Majerik59c773e21f48
diff --git a/posthog/constants.py b/posthog/constants.py
index 463aea2865cc..bb5da91e5c9b 100644
--- a/posthog/constants.py
+++ b/posthog/constants.py
@@ -336,9 +336,6 @@ SUBSCRIPTION_AI_PROMPT_FEATURE_FLAG_KEY = "ai-subscriptions"
# Enable only after every subscriptions worker has deployed the gallery claim boundary. Older workers
# share the v2 activity name and would otherwise send the legacy layout during a rolling deployment.
SUBSCRIPTION_SLACK_GALLERY_FEATURE_FLAG_KEY = "subscription-slack-gallery"
-EXPERIMENTS_RETENTION_METRIC_EVENTS_PREAGGREGATION_FEATURE_FLAG_KEY = (
- "experiments-retention-metric-events-preaggregation"
-)
GENERATED_DASHBOARD_PREFIX = "Generated Dashboard"
ENRICHED_DASHBOARD_INSIGHT_IDENTIFIER = "Feature Viewed"
diff --git a/products/experiments/backend/hogql_queries/LAZY_COMPUTATION.md b/products/experiments/backend/hogql_queries/LAZY_COMPUTATION.md
index 5794c1bedc93..ee962042c81d 100644
--- a/products/experiments/backend/hogql_queries/LAZY_COMPUTATION.md
+++ b/products/experiments/backend/hogql_queries/LAZY_COMPUTATION.md
@@ -268,4 +268,4 @@ GROUP BY variant
7. **Future: GROUP BY (entity_id, variant)**: Currently the query groups by entity_id only and resolves the variant during precomputation (e.g., argMin for first-seen). This bakes the variant handling strategy into the stored data, so switching between "first seen" and "exclude multiple" requires recomputation. A better approach is to GROUP BY (entity_id, variant) so we store one row per user-variant pair, then resolve multiple variants at query time.
-8. **Metric events**: The same lazy computation system also precomputes the metric_events half into `experiment_metric_events_preaggregated`, for ordered funnel metrics (one row per matching event with step indicators in `steps`), count/sum mean metrics (one row per matching event with its value in `numeric_value`), and retention metrics (one row per event matching the start or completion predicate, flagged as `steps[1]`/`steps[2]`; start anchoring, the retention window, and maturity stay read-time). Eligibility is gated by `_metric_events_precompute_applicable()` in `experiment_query_runner.py`; breakdowns, CUPED, and data warehouse sources fall back to a direct scan of the metric_events CTE. Retention is additionally gated behind the default-off `experiments-retention-metric-events-preaggregation` flag so it can be disabled without turning off funnel/mean. The scan extends past the experiment end by the conversion window — plus, for retention, `retention_window_end`, since a completion can land that much after a start event.
+8. **Metric events**: The same lazy computation system also precomputes the metric_events half into `experiment_metric_events_preaggregated`, for ordered funnel metrics (one row per matching event with step indicators in `steps`), count/sum mean metrics (one row per matching event with its value in `numeric_value`), and retention metrics (one row per event matching the start or completion predicate, flagged as `steps[1]`/`steps[2]`; start anchoring, the retention window, and maturity stay read-time). Eligibility is gated by `_metric_events_precompute_applicable()` in `experiment_query_runner.py`; breakdowns, CUPED, and data warehouse sources fall back to a direct scan of the metric_events CTE. The scan extends past the experiment end by the conversion window — plus, for retention, `retention_window_end`, since a completion can land that much after a start event.
diff --git a/products/experiments/backend/hogql_queries/METRIC_EVENTS_PRECOMPUTATION.md b/products/experiments/backend/hogql_queries/METRIC_EVENTS_PRECOMPUTATION.md
index 33cb0006ad4a..2e31f36eef65 100644
--- a/products/experiments/backend/hogql_queries/METRIC_EVENTS_PRECOMPUTATION.md
+++ b/products/experiments/backend/hogql_queries/METRIC_EVENTS_PRECOMPUTATION.md
@@ -202,7 +202,7 @@ The exposure precomputation does NOT need this extension — exposures only occu
Implemented for **ordered funnels**, **count/sum/avg/min/max mean metrics** (per-event value stored in `numeric_value`, deduplicated on read by event identity since replayed build rows would double sums and skew averages; the aggregation itself runs at read time, so all five math types store identical rows), **dau/unique_session mean metrics** (the read counts distinct IDs from `entity_id`/`session_id`, which every mean build stores; `numeric_value` holds the same constant a count metric stores, so a count metric and an ID-math metric on the same source share build jobs), and **retention metrics**. Unordered funnels, unique-group and HogQL math, and ratio metrics are not precomputed; breakdowns, CUPED, and data warehouse sources always fall back to a direct scan.
-Retention stores one row per event matching the start or completion predicate, with two flags in `steps` (`steps[1]` = matched start_event, `steps[2]` = matched completion_event; one event can match both). The read path swaps the two raw-events CTE sources for flag-filtered reads of the precomputed table; start anchoring (FIRST_SEEN/LAST_SEEN), the per-user retention window, the maturity gate, and the same-event exclusion all stay read-time, so they behave identically on both paths. The scan extension past the experiment end is `conversion_window + retention_window_end` rather than the conversion window alone, and retention is gated behind the default-off `experiments-retention-metric-events-preaggregation` flag (fail-safe: absent or unevaluable means direct scan) so it can be disabled independently of funnel/mean.
+Retention stores one row per event matching the start or completion predicate, with two flags in `steps` (`steps[1]` = matched start_event, `steps[2]` = matched completion_event; one event can match both). The read path swaps the two raw-events CTE sources for flag-filtered reads of the precomputed table; start anchoring (FIRST_SEEN/LAST_SEEN), the per-user retention window, the maturity gate, and the same-event exclusion all stay read-time, so they behave identically on both paths. The scan extension past the experiment end is `conversion_window + retention_window_end` rather than the conversion window alone, capped at 90 days (`METRIC_EVENTS_MAX_WINDOW_EXTENSION_SECONDS`); oversized windows fall back to a direct scan.
## Key files
diff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.py
index 06e8d6c9de5d..0eaabb3398bf 100644
--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py
+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py
@@ -36,7 +36,6 @@ from posthog.hogql.modifiers import create_default_modifiers_for_team
from posthog.hogql.query import execute_hogql_query
from posthog.clickhouse.query_tagging import Product, tag_queries, tags_context
-from posthog.constants import EXPERIMENTS_RETENTION_METRIC_EVENTS_PREAGGREGATION_FEATURE_FLAG_KEY
from posthog.exceptions_capture import capture_exception
from posthog.hogql_queries.query_runner import QueryRunner
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
@@ -491,28 +490,6 @@ class ExperimentQueryRunner(ExperimentResultsCacheMixin, QueryRunner):
return PrecomputeSkipReason.GROUP_AGGREGATION
return None # precompute was attempted; a direct path means the build failed / wasn't ready
- def _retention_metric_events_precomputation_enabled(self) -> bool:
- """Kill switch for retention metric-events pre-aggregation, independent of funnel/mean.
-
- Default-off and fail-safe: returns False unless the flag is explicitly enabled, so a
- flag-eval failure (or the flag not existing yet) leaves retention metric events on the
- direct-scan path while funnel/mean metric events and exposures keep using their
- precomputed tables.
- """
- return bool(
- posthoganalytics.feature_enabled(
- EXPERIMENTS_RETENTION_METRIC_EVENTS_PREAGGREGATION_FEATURE_FLAG_KEY,
- str(self.team.uuid),
- groups={"organization": str(self.team.organization_id), "project": str(self.team.id)},
- group_properties={
- "organization": {"id": str(self.team.organization_id)},
- "project": {"id": str(self.team.id), "uuid": str(self.team.uuid)},
- },
- only_evaluate_locally=True,
- send_feature_flag_events=False,
- )
- )
-
def _metric_events_precompute_applicable(self) -> bool:
"""
Metric-events precompute supports ordered funnels, mean metrics with
@@ -563,9 +540,7 @@ class ExperimentQueryRunner(ExperimentResultsCacheMixin, QueryRunner):
extension_seconds = get_conversion_window_seconds(self.metric) + conversion_window_to_seconds(
self.metric.retention_window_end, self.metric.retention_window_unit
)
- if extension_seconds > METRIC_EVENTS_MAX_WINDOW_EXTENSION_SECONDS:
- return False
- return self._retention_metric_events_precomputation_enabled()
+ return extension_seconds <= METRIC_EVENTS_MAX_WINDOW_EXTENSION_SECONDS
return False
@property
diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/base.py b/products/experiments/backend/hogql_queries/test/experiment_query_runner/base.py
index e0a333efe63c..f378acb4b7d8 100644
--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/base.py
+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/base.py
@@ -3,7 +3,6 @@ from pathlib import Path
from typing import Any, Optional
from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, _create_person
-from unittest.mock import patch
from django.test import override_settings
from django.utils import timezone
@@ -21,10 +20,7 @@ from posthog.models.team.extensions import get_or_create_team_extension
from products.analytics_platform.backend.models.preaggregation_job import PreaggregationJob
from products.data_tools.backend.models.join import DataWarehouseJoin
-from products.experiments.backend.hogql_queries.experiment_query_runner import (
- MIN_PRECOMPUTATION_DURATION_SECONDS,
- ExperimentQueryRunner,
-)
+from products.experiments.backend.hogql_queries.experiment_query_runner import MIN_PRECOMPUTATION_DURATION_SECONDS
from products.experiments.backend.models.experiment import Experiment
from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig
from products.feature_flags.backend.models.feature_flag import FeatureFlag
@@ -93,16 +89,6 @@ class ExperimentQueryRunnerBaseTest(ClickhouseTestMixin, APIBaseTest):
config = get_or_create_team_extension(self.team, TeamExperimentsConfig)
config.experiment_precomputation_enabled = True
config.save()
- # Retention metric-events pre-aggregation has an additional default-off
- # kill-switch flag; force it on so tests still exercise the precomputed
- # metric-events path.
- if not getattr(self, "_retention_metric_events_precompute_patched", False):
- patcher = patch.object(
- ExperimentQueryRunner, "_retention_metric_events_precomputation_enabled", return_value=True
- )
- patcher.start()
- self.addCleanup(patcher.stop)
- self._retention_metric_events_precompute_patched = True
def _disable_precomputation(self):
config = get_or_create_team_extension(self.team, TeamExperimentsConfig)
diff --git a/products/experiments/backend/hogql_queries/test/test_experiment_retention_metric_events_preaggregation.py b/products/experiments/backend/hogql_queries/test/test_experiment_retention_metric_events_preaggregation.py
index 40305e647c61..4d26f72a1fad 100644
--- a/products/experiments/backend/hogql_queries/test/test_experiment_retention_metric_events_preaggregation.py
+++ b/products/experiments/backend/hogql_queries/test/test_experiment_retention_metric_events_preaggregation.py
@@ -345,19 +345,4 @@ class TestExperimentRetentionMetricEventsPreaggregation(ExperimentQueryRunnerBas
runner = self._build_runner(experiment, metric)
- with patch.object(ExperimentQueryRunner, "_retention_metric_events_precomputation_enabled", return_value=True):
- assert runner._metric_events_precompute_applicable() is applicable
-
- def test_retention_metric_events_precompute_disabled_without_flag(self):
- feature_flag = self.create_feature_flag(key="retention-metric-events-kill-switch")
- experiment = self.create_experiment(
- feature_flag=feature_flag,
- start_date=datetime(2024, 1, 1),
- end_date=datetime(2024, 1, 10),
- )
-
- runner = self._build_runner(experiment, _retention_metric())
-
- # Fail-safe kill switch: with the flag absent/unevaluable, an otherwise
- # eligible retention metric must stay on the direct-scan path.
- assert runner._metric_events_precompute_applicable() is False
+ assert runner._metric_events_precompute_applicable() is applicable