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(marketing-analytics): materialize the touchpoints precompute once per read, not once per goal (#70559)

Javier Bahamondesa771072e66fd
diff --git a/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py b/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py
index 6f5a70a7de46..da35391135ba 100644
--- a/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py
+++ b/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py
@@ -1,5 +1,6 @@
import math
import uuid
+import threading
from collections.abc import Sequence
from dataclasses import (
dataclass,
@@ -31,6 +32,7 @@ from posthog.models import PropertyDefinition, Team, User
from products.access_control.backend.property_access_control import get_restricted_property_names
from products.actions.backend.models.action import Action
from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (
+ LazyComputationResult,
LazyComputationTable,
ensure_precomputed,
)
@@ -155,6 +157,50 @@ def build_touchpoints_precompute_query() -> ast.SelectQuery:
)
+class SharedTouchpointsPrecompute:
+ """One touchpoints materialization shared by every conversion goal in a request.
+
+ `build_touchpoints_precompute_query()` takes no goal input and the range depends only on the
+ team's attribution window, so the call every goal makes is byte-identical. Each goal was driving
+ its own `ensure_precomputed` for the same window: redundant ClickHouse work, and concurrent
+ materializations of the same window risk landing under separate job_ids.
+
+ The first goal to ask does the work; the rest reuse its result. Goals run in a thread pool, hence
+ the lock.
+ """
+
+ def __init__(self, team: Team, config: MarketingAnalyticsConfig) -> None:
+ self._team = team
+ self._config = config
+ self._lock = threading.Lock()
+ self._result: Optional[LazyComputationResult] = None
+ self._range: Optional[tuple[datetime, datetime]] = None
+
+ def get(self, date_from: datetime, date_to: datetime) -> LazyComputationResult:
+ with self._lock:
+ if self._result is None:
+ window = timedelta(days=self._config.attribution_window_days)
+ self._range = (date_from, date_to)
+ self._result = ensure_precomputed(
+ team=self._team,
+ insert_query=build_touchpoints_precompute_query(),
+ time_range_start=date_from - window,
+ time_range_end=date_to,
+ ttl_seconds=PRECOMPUTE_TTL_SECONDS,
+ table=LazyComputationTable.MARKETING_TOUCHPOINTS_PREAGGREGATED,
+ )
+ elif self._range != (date_from, date_to):
+ # One handle is scoped to one read, whose goals all share its date range. Handing back
+ # the first caller's window for a different range would attribute one window's
+ # touchpoints to another — silently, and in the shape of the double-counting bug this
+ # class exists to prevent.
+ raise ValueError(
+ f"SharedTouchpointsPrecompute is scoped to one date range per read: "
+ f"materialized {self._range}, asked for {(date_from, date_to)}"
+ )
+ return self._result
+
+
@dataclass
class ConversionGoalProcessor:
"""
@@ -291,10 +337,15 @@ class ConversionGoalProcessor:
additional_conditions: Sequence[ast.Expr],
date_from: Optional[datetime] = None,
date_to: Optional[datetime] = None,
+ touchpoints: Optional[SharedTouchpointsPrecompute] = None,
) -> ast.SelectQuery:
- """Generate main CTE query for conversion goal."""
+ """Generate main CTE query for conversion goal.
+
+ `touchpoints` lets callers running several goals share one touchpoints materialization. When
+ omitted the goal materializes its own, so standalone callers keep working unchanged.
+ """
if self.goal.kind in ["EventsNode", "ActionsNode"]:
- return self._generate_array_based_query(additional_conditions, date_from, date_to)
+ return self._generate_array_based_query(additional_conditions, date_from, date_to, touchpoints)
return self._generate_direct_query(additional_conditions)
def build_array_collection_query(self, additional_conditions: Sequence[ast.Expr]) -> ast.SelectQuery:
@@ -329,10 +380,11 @@ class ConversionGoalProcessor:
additional_conditions: Sequence[ast.Expr],
date_from: Optional[datetime] = None,
date_to: Optional[datetime] = None,
+ touchpoints: Optional[SharedTouchpointsPrecompute] = None,
) -> ast.SelectQuery:
"""Generate array-based query with attribution logic for Events/Actions"""
if self.config.attribution_window_days > 0:
- return self._generate_funnel_query(additional_conditions, date_from, date_to)
+ return self._generate_funnel_query(additional_conditions, date_from, date_to, touchpoints)
return self._generate_direct_query(additional_conditions)
def _generate_funnel_query(
@@ -340,6 +392,7 @@ class ConversionGoalProcessor:
additional_conditions: Sequence[ast.Expr],
date_from: Optional[datetime] = None,
date_to: Optional[datetime] = None,
+ touchpoints: Optional[SharedTouchpointsPrecompute] = None,
) -> ast.SelectQuery:
"""Generate multi-step funnel query with attribution window.
@@ -349,7 +402,7 @@ class ConversionGoalProcessor:
# `_should_use_precompute` returns False unless both dates are set; narrow for mypy.
assert date_from is not None and date_to is not None
try:
- precomputed = self._build_attribution_from_precomputes(date_from, date_to)
+ precomputed = self._build_attribution_from_precomputes(date_from, date_to, touchpoints)
if precomputed is not None:
return precomputed
except Exception:
@@ -488,7 +541,12 @@ class ConversionGoalProcessor:
where=ast.And(exprs=where_exprs),
)
- def _build_attribution_from_precomputes(self, date_from: datetime, date_to: datetime) -> Optional[ast.SelectQuery]:
+ def _build_attribution_from_precomputes(
+ self,
+ date_from: datetime,
+ date_to: datetime,
+ touchpoints: Optional[SharedTouchpointsPrecompute] = None,
+ ) -> Optional[ast.SelectQuery]:
"""Reusable-precompute read path: ensure both the config-agnostic touchpoints and the per-goal
conversions are materialized, then attribute at read time by feeding a precompute-sourced array
collection through the existing pipeline (all modes). Neither precompute depends on attribution
@@ -499,15 +557,13 @@ class ConversionGoalProcessor:
# Touchpoints extend back by the attribution window; conversions only span the query range.
# Both ensure_precomputed calls can materialize inline (PG + Redis + ClickHouse) when a slice
# is stale, so they are timed separately from the AST work that follows.
+ #
+ # Touchpoints are config-agnostic, so a multi-goal read shares one materialization. Without a
+ # shared handle each goal materializes the same window itself, which is what a standalone
+ # caller gets.
+ shared_touchpoints = touchpoints or SharedTouchpointsPrecompute(self.team, self.config)
with self.timings.measure("ma_ensure_touchpoints"):
- touchpoints_result = ensure_precomputed(
- team=self.team,
- insert_query=build_touchpoints_precompute_query(),
- time_range_start=date_from - window,
- time_range_end=date_to,
- ttl_seconds=PRECOMPUTE_TTL_SECONDS,
- table=LazyComputationTable.MARKETING_TOUCHPOINTS_PREAGGREGATED,
- )
+ touchpoints_result = shared_touchpoints.get(date_from, date_to)
if not touchpoints_result.ready:
return None
diff --git a/products/marketing_analytics/backend/hogql_queries/conversion_goals_aggregator.py b/products/marketing_analytics/backend/hogql_queries/conversion_goals_aggregator.py
index 8619cc54741b..c3ab3c545a29 100644
--- a/products/marketing_analytics/backend/hogql_queries/conversion_goals_aggregator.py
+++ b/products/marketing_analytics/backend/hogql_queries/conversion_goals_aggregator.py
@@ -10,7 +10,7 @@ from posthog.settings import TEST
from products.marketing_analytics.backend.hogql_queries.constants import UNIFIED_CONVERSION_GOALS_CTE_ALIAS
from .adapters.factory import MarketingSourceFactory
-from .conversion_goal_processor import ConversionGoalProcessor
+from .conversion_goal_processor import ConversionGoalProcessor, SharedTouchpointsPrecompute
from .marketing_analytics_config import MarketingAnalyticsConfig
# Cap on parallel per-goal ensure_precomputed workers.
@@ -35,6 +35,11 @@ class ConversionGoalsAggregator:
if not self.processors:
raise ValueError("Cannot create unified CTE without conversion goal processors")
+ # The touchpoints precompute is config-agnostic: every goal drives the exact same
+ # ensure_precomputed for the same window. Share one handle so it is materialized once for the
+ # whole read instead of once per goal.
+ touchpoints = SharedTouchpointsPrecompute(self.processors[0].team, self.config)
+
# Step 1: Generate individual conversion goal queries, parallelised across goals
# so ensure_precomputed's PG+Redis+ClickHouse round-trips collapse to max(overhead).
def _build_base_query(processor: ConversionGoalProcessor) -> ast.SelectQuery:
@@ -49,6 +54,7 @@ class ConversionGoalsAggregator:
additional_conditions,
date_from=date_range.date_from(),
date_to=date_range.date_to(),
+ touchpoints=touchpoints,
)
# Skip the pool in TEST: Django's per-thread DB connections don't see
diff --git a/products/marketing_analytics/backend/hogql_queries/test_conversion_goals_aggregator.py b/products/marketing_analytics/backend/hogql_queries/test_conversion_goals_aggregator.py
index 5cf236b34228..067b5d5f9c98 100644
--- a/products/marketing_analytics/backend/hogql_queries/test_conversion_goals_aggregator.py
+++ b/products/marketing_analytics/backend/hogql_queries/test_conversion_goals_aggregator.py
@@ -1,7 +1,7 @@
import re
import uuid
import itertools
-from datetime import datetime
+from datetime import UTC, datetime, timedelta
import pytest
from posthog.test.base import BaseTest, ClickhouseTestMixin
@@ -24,9 +24,13 @@ from posthog.clickhouse.preaggregation.marketing_touchpoints_sql import TRUNCATE
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from products.actions.backend.models.action import Action
+from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (
+ LazyComputationResult,
+ LazyComputationTable,
+)
from products.analytics_platform.backend.models.preaggregation_job import PreaggregationJob
-from .conversion_goal_processor import ConversionGoalProcessor
+from .conversion_goal_processor import ConversionGoalProcessor, SharedTouchpointsPrecompute
from .conversion_goals_aggregator import ConversionGoalsAggregator
from .marketing_analytics_config import MarketingAnalyticsConfig
@@ -624,3 +628,41 @@ class TestConversionGoalsAggregator(ClickhouseTestMixin, BaseTest):
assert "Holiday Promo" in sql_string, "SQL should contain mapped campaign name"
assert "spring_sale_2024" in sql_string, "SQL should contain original campaign name in mapping"
assert "holiday_campaign" in sql_string, "SQL should contain original campaign name in mapping"
+
+ def test_touchpoints_precompute_materialized_once_across_goals(self):
+ processors = [
+ self._create_test_processor(self._create_test_conversion_goal("goal1", "Sign Ups"), 0),
+ self._create_test_processor(self._create_test_conversion_goal("goal2", "Purchases"), 1),
+ ]
+ aggregator = ConversionGoalsAggregator(processors, self.config)
+
+ with patch(
+ "products.marketing_analytics.backend.hogql_queries.conversion_goal_processor.ensure_precomputed",
+ side_effect=lambda **kwargs: LazyComputationResult(ready=True, job_ids=[uuid.uuid4()]),
+ ) as ensure:
+ aggregator.generate_unified_cte(self.date_range, self._create_mock_additional_conditions_getter())
+
+ tables = [call.kwargs["table"] for call in ensure.call_args_list]
+ # Touchpoints are config-agnostic, so both goals share one materialization. Conversions embed the
+ # goal, so each keeps its own — sharing those would serve one goal's conversions for the other.
+ assert tables.count(LazyComputationTable.MARKETING_TOUCHPOINTS_PREAGGREGATED) == 1
+ assert tables.count(LazyComputationTable.MARKETING_CONVERSIONS_PREAGGREGATED) == len(processors)
+
+ def test_shared_touchpoints_rejects_a_second_date_range(self):
+ # The handle caches the first materialization, so reusing it for another range would attribute
+ # one window's touchpoints to another. Silently, if it just returned the cached result.
+ shared = SharedTouchpointsPrecompute(self.team, self.config)
+ date_from = datetime(2024, 1, 1, tzinfo=UTC)
+ date_to = datetime(2024, 1, 31, tzinfo=UTC)
+
+ with patch(
+ "products.marketing_analytics.backend.hogql_queries.conversion_goal_processor.ensure_precomputed",
+ side_effect=lambda **kwargs: LazyComputationResult(ready=True, job_ids=[uuid.uuid4()]),
+ ) as ensure:
+ first = shared.get(date_from, date_to)
+ assert shared.get(date_from, date_to) is first
+
+ with pytest.raises(ValueError, match="one date range per read"):
+ shared.get(date_from, date_to + timedelta(days=1))
+
+ assert ensure.call_count == 1