: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(flags): filter personless flag calls in lifecycle on person_mode (#118839)
Phil Haackff228ba38158
diff --git a/posthog/test/base.py b/posthog/test/base.pyindex 13156a921be3..9c6cca5ad8f5 100644--- a/posthog/test/base.py+++ b/posthog/test/base.py@@ -1816,6 +1816,7 @@ def _create_flag_evaluations(response: str | bool | None = True,properties: dict[str, Any] | None = None,person_properties: dict[str, str] | None = None,+ person_mode: str = "full",) -> None:"""Insert `count` $feature_flag_called rows for flag_key into flag_evaluations. Unlike _create_event, it writesthe rows immediately. Each row gets a new random person_id unless the caller passes one. Without a@@ -1826,7 +1827,7 @@ def _create_flag_evaluations(sync_execute("""INSERT INTO writable_flag_evaluations- (uuid, event, properties, timestamp, team_id, distinct_id, created_at, person_id, person_created_at, person_properties)+ (uuid, event, properties, timestamp, team_id, distinct_id, created_at, person_id, person_created_at, person_properties, person_mode)VALUES""",[@@ -1841,6 +1842,7 @@ def _create_flag_evaluations(str(person_id or uuid.uuid4()),person_created_at or ZERO_DATE,json.dumps(person_properties or {}),+ person_mode,)for _ in range(count)],diff --git a/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py b/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.pyindex 6bbe3d62852d..c792a1436d2d 100644--- a/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py+++ b/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py@@ -338,12 +338,10 @@ class LifecycleQueryRunner(AnalyticsQueryRunner[LifecycleQueryResponse]):@cached_propertydef event_filter(self) -> ast.Expr:event_filters: list[ast.Expr] = []- if not self.is_data_warehouse_series or self.data_warehouse_series.table_name == FLAG_EVALUATIONS_HOGQL_TABLE:+ if not self.is_data_warehouse_series:# Personless (anonymous) events are excluded: lifecycle classifies a user's activity# across periods, which requires a person profile. This makes lifecycle counts lower# than unique-user trends for projects with anonymous traffic.- # The flag calls table stores each event's properties. Other warehouse tables have no- # $process_person_profile.event_filters.append(ast.CompareOperation(left=ast.Field(chain=["properties", "$process_person_profile"]),@@ -351,6 +349,18 @@ class LifecycleQueryRunner(AnalyticsQueryRunner[LifecycleQueryResponse]):op=ast.CompareOperationOp.NotEq,))+ elif self.data_warehouse_series.table_name == FLAG_EVALUATIONS_HOGQL_TABLE:+ # Ingestion sets person_mode to 'full' exactly when it does not set $process_person_profile+ # to false, so this filter keeps the same rows as the events filter. The flag calls table+ # does not materialize $process_person_profile, so the property filter would parse the+ # properties JSON of every row. Other warehouse tables have no person mode.+ event_filters.append(+ ast.CompareOperation(+ left=ast.Field(chain=["person_mode"]),+ right=ast.Constant(value="full"),+ op=ast.CompareOperationOp.Eq,+ )+ )with self.timings.measure("date_range"):event_filters.append(parse_expr(diff --git a/products/product_analytics/backend/tests/test_flag_evaluations_insight_series.py b/products/product_analytics/backend/tests/test_flag_evaluations_insight_series.pyindex c361f6094409..6e8b6c56a92d 100644--- a/products/product_analytics/backend/tests/test_flag_evaluations_insight_series.py+++ b/products/product_analytics/backend/tests/test_flag_evaluations_insight_series.py@@ -357,14 +357,18 @@ class TestFlagEvaluationsInsightSeries(ClickhouseTestMixin, BaseTest):datetime(2025, 1, 9, 12, tzinfo=UTC),{"$group_0": "org-c"},)- # Lifecycle excludes this personless call. Counting it would add an active user or group on 2025-01-08.- _create_flag_evaluations(- self.team.pk,- "probe-flag",- timestamp=datetime(2025, 1, 8, 14, tzinfo=UTC),- distinct_id="anonymous",- properties={"$process_person_profile": False, "$group_0": "org-c"},- )+ # Lifecycle excludes both personless calls. Events lifecycle also drops a force-upgraded call, because that+ # call carries $process_person_profile=false. Counting either call would add an active user or group on+ # 2025-01-08.+ for person_mode in ["propertyless", "force_upgrade"]:+ _create_flag_evaluations(+ self.team.pk,+ "probe-flag",+ timestamp=datetime(2025, 1, 8, 14, tzinfo=UTC),+ distinct_id="anonymous",+ person_mode=person_mode,+ properties={"$process_person_profile": False, "$group_0": "org-c"},+ )query = LifecycleQuery(dateRange=DATE_RANGE,interval=IntervalType.DAY,@@ -378,12 +382,18 @@ class TestFlagEvaluationsInsightSeries(ClickhouseTestMixin, BaseTest):)events_query = query.model_copy(update={"series": [EventsNode(event="$feature_flag_called")]})- results = LifecycleQueryRunner(query=query, team=self.team).calculate().results+ with self.capture_select_queries() as queries:+ results = LifecycleQueryRunner(query=query, team=self.team).calculate().resultsevents_results = LifecycleQueryRunner(query=events_query, team=self.team).calculate().resultslifecycle = {r["status"]: r["data"] for r in results}assert lifecycle == expectedassert lifecycle == {r["status"]: r["data"] for r in events_results}+ # A filter on properties.$process_person_profile keeps the same rows. Only the SQL shows that the query reads+ # the person_mode column instead of parsing the properties JSON of every row.+ [sql] = [captured for captured in queries if "flag_evaluations" in captured]+ assert "person_mode" in sql+ assert "JSONExtractRaw" not in sql@parameterized.expand([