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): make the spill threshold reachable (#90729)

Javier Bahamondes10dea3ffb140
diff --git a/products/marketing_analytics/backend/hogql_queries/attribution_paths_query_runner.py b/products/marketing_analytics/backend/hogql_queries/attribution_paths_query_runner.py
index aaa11a80514b..62e74327d03f 100644
--- a/products/marketing_analytics/backend/hogql_queries/attribution_paths_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/attribution_paths_query_runner.py
@@ -22,11 +22,11 @@ from posthog.schema import (
)
from posthog.hogql import ast
-from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, HogQLGlobalSettings, LimitContext
+from posthog.hogql.constants import HogQLGlobalSettings, LimitContext
from posthog.hogql.query import execute_hogql_query
from .attribution_base import PERSON_ARRAYS_CTE, PERSON_CONVERSION_COUNT, AttributionQueryRunnerBase
-from .constants import PAGINATION_EXTRA
+from .constants import MARKETING_SPILL_AFTER_BYTES, PAGINATION_EXTRA
# Paths longer than this are grouped on their most recent steps — the same truncation direction as
# MAX_TOUCHPOINTS_PER_PERSON, and for the same reason: the touches nearest the conversion are the ones
@@ -300,7 +300,7 @@ class MarketingAnalyticsAttributionPathsQueryRunner(
context=self._shared_hogql_context,
# The per-person touchpoint arrays are unbounded, so let the GROUP BY spill to disk
# rather than hit the memory limit. Same guard funnels, retention and paths use.
- settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY),
+ settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MARKETING_SPILL_AFTER_BYTES),
)
# Mapped by column name, not tuple position, so adding a column can't shift every later one.
diff --git a/products/marketing_analytics/backend/hogql_queries/attribution_table_query_runner.py b/products/marketing_analytics/backend/hogql_queries/attribution_table_query_runner.py
index e7e17f332710..b8bdf42f5f77 100644
--- a/products/marketing_analytics/backend/hogql_queries/attribution_table_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/attribution_table_query_runner.py
@@ -29,7 +29,7 @@ from posthog.schema import (
)
from posthog.hogql import ast
-from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, HogQLGlobalSettings, LimitContext
+from posthog.hogql.constants import HogQLGlobalSettings, LimitContext
from posthog.hogql.query import execute_hogql_query
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
@@ -42,7 +42,7 @@ from .attribution_weights import (
build_position_based_weights,
build_time_decay_weights,
)
-from .constants import DEFAULT_LIMIT, PAGINATION_EXTRA
+from .constants import DEFAULT_LIMIT, MARKETING_SPILL_AFTER_BYTES, PAGINATION_EXTRA
# Column order for the table's model groups: single-touch first, then multi-touch, so the columns read
# left to right from the crudest split to the most nuanced.
@@ -480,7 +480,7 @@ class MarketingAnalyticsAttributionQueryRunner(AttributionQueryRunnerBase[Market
context=self._shared_hogql_context,
# The per-person touchpoint arrays are unbounded, so let the GROUP BY spill to disk
# rather than hit the memory limit. Same guard funnels, retention and paths use.
- settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY),
+ settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MARKETING_SPILL_AFTER_BYTES),
)
# Mapped by column name, not tuple position, so adding a column can't shift every later one.
diff --git a/products/marketing_analytics/backend/hogql_queries/constants.py b/products/marketing_analytics/backend/hogql_queries/constants.py
index 79f3f4724196..8b88dde3e885 100644
--- a/products/marketing_analytics/backend/hogql_queries/constants.py
+++ b/products/marketing_analytics/backend/hogql_queries/constants.py
@@ -760,3 +760,10 @@ def to_marketing_analytics_data(
changeFromPreviousPct=change_from_previous_pct,
hasComparison=has_comparison,
)
+
+
+# Spill the GROUP BY to disk past this much memory. Deliberately far below the shared
+# MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY (22 GiB): these queries peak around 1.5 GiB, so a threshold above
+# their peak never fires, and one above the per-query memory limit could never fire at all.
+# `test_spill_threshold_is_reachable` locks that relationship.
+MARKETING_SPILL_AFTER_BYTES = 512 * 1024 * 1024
diff --git a/products/marketing_analytics/backend/hogql_queries/marketing_analytics_aggregated_query_runner.py b/products/marketing_analytics/backend/hogql_queries/marketing_analytics_aggregated_query_runner.py
index 5f91866134ed..1b899ec43172 100644
--- a/products/marketing_analytics/backend/hogql_queries/marketing_analytics_aggregated_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/marketing_analytics_aggregated_query_runner.py
@@ -13,13 +13,14 @@ from posthog.schema import (
)
from posthog.hogql import ast
-from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, HogQLGlobalSettings
+from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.query import execute_hogql_query
from .constants import (
BASE_COLUMN_MAPPING,
HIERARCHY_BASE_COLUMNS,
HIERARCHY_DRILL_DOWN_LEVELS,
+ MARKETING_SPILL_AFTER_BYTES,
ROAS_COLUMN,
UNIFIED_CONVERSION_GOALS_CTE_ALIAS,
to_marketing_analytics_data,
@@ -167,7 +168,7 @@ class MarketingAnalyticsAggregatedQueryRunner(
limit_context=self.limit_context,
# These group by high-cardinality campaign dimensions, so let the GROUP BY spill
# to disk rather than hit the memory limit.
- settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY),
+ settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MARKETING_SPILL_AFTER_BYTES),
)
results = response.results or []
diff --git a/products/marketing_analytics/backend/hogql_queries/marketing_analytics_table_query_runner.py b/products/marketing_analytics/backend/hogql_queries/marketing_analytics_table_query_runner.py
index bec9ad693e77..eac07c934902 100644
--- a/products/marketing_analytics/backend/hogql_queries/marketing_analytics_table_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/marketing_analytics_table_query_runner.py
@@ -14,7 +14,7 @@ from posthog.schema import (
)
from posthog.hogql import ast
-from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, HogQLGlobalSettings
+from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.query import execute_hogql_query
from posthog.hogql_queries.paginators import HogQLHasMorePaginator
@@ -24,6 +24,7 @@ from .constants import (
CHANNEL_SESSIONS_CTE_NAME,
DEFAULT_LIMIT,
DRILL_DOWN_LEVEL_CONFIG,
+ MARKETING_SPILL_AFTER_BYTES,
PAGINATION_EXTRA,
SESSIONS_COLUMN_ALIAS,
TOTAL_SESSIONS_FIELD,
@@ -91,7 +92,7 @@ class MarketingAnalyticsTableQueryRunner(MarketingAnalyticsBaseQueryRunner[Marke
context=self._shared_hogql_context,
# These group by high-cardinality campaign dimensions, so let the GROUP BY spill
# to disk rather than hit the memory limit.
- settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY),
+ settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MARKETING_SPILL_AFTER_BYTES),
)
results = response.results or []
diff --git a/products/marketing_analytics/backend/hogql_queries/non_integrated_conversions_table_query_runner.py b/products/marketing_analytics/backend/hogql_queries/non_integrated_conversions_table_query_runner.py
index 12e89a00e704..be8079228c68 100644
--- a/products/marketing_analytics/backend/hogql_queries/non_integrated_conversions_table_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/non_integrated_conversions_table_query_runner.py
@@ -13,12 +13,18 @@ from posthog.schema import (
)
from posthog.hogql import ast
-from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, HogQLGlobalSettings
+from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.query import execute_hogql_query
from posthog.hogql_queries.paginators import HogQLHasMorePaginator
-from .constants import DEFAULT_LIMIT, PAGINATION_EXTRA, UNIFIED_CONVERSION_GOALS_CTE_ALIAS, to_marketing_analytics_data
+from .constants import (
+ DEFAULT_LIMIT,
+ MARKETING_SPILL_AFTER_BYTES,
+ PAGINATION_EXTRA,
+ UNIFIED_CONVERSION_GOALS_CTE_ALIAS,
+ to_marketing_analytics_data,
+)
from .conversion_goals_aggregator import ConversionGoalsAggregator
from .marketing_analytics_base_query_runner import MarketingAnalyticsBaseQueryRunner
@@ -87,7 +93,7 @@ class NonIntegratedConversionsTableQueryRunner(
limit_context=self.limit_context,
# These group by high-cardinality campaign dimensions, so let the GROUP BY spill
# to disk rather than hit the memory limit.
- settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY),
+ settings=HogQLGlobalSettings(max_bytes_before_external_group_by=MARKETING_SPILL_AFTER_BYTES),
)
results = response.results or []
diff --git a/products/marketing_analytics/backend/hogql_queries/test_query_settings.py b/products/marketing_analytics/backend/hogql_queries/test_query_settings.py
new file mode 100644
index 000000000000..08acbcdf6267
--- /dev/null
+++ b/products/marketing_analytics/backend/hogql_queries/test_query_settings.py
@@ -0,0 +1,106 @@
+import sys
+
+from posthog.test.base import BaseTest
+from unittest.mock import patch
+
+from parameterized import parameterized
+
+from posthog.schema import (
+ BaseMathType,
+ ConversionGoalFilter1,
+ DateRange,
+ MarketingAnalyticsAttributionBreakdown,
+ MarketingAnalyticsAttributionPathsQuery,
+ MarketingAnalyticsAttributionQuery,
+ MarketingAnalyticsTableQuery,
+)
+
+from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY
+
+from products.marketing_analytics.backend.hogql_queries.attribution_paths_query_runner import (
+ MarketingAnalyticsAttributionPathsQueryRunner,
+)
+from products.marketing_analytics.backend.hogql_queries.attribution_table_query_runner import (
+ MarketingAnalyticsAttributionQueryRunner,
+)
+from products.marketing_analytics.backend.hogql_queries.constants import MARKETING_SPILL_AFTER_BYTES
+from products.marketing_analytics.backend.hogql_queries.marketing_analytics_table_query_runner import (
+ MarketingAnalyticsTableQueryRunner,
+)
+
+GOAL_ID = "goal-1"
+
+
+class _Stop(Exception):
+ pass
+
+
+class TestMarketingQuerySettings(BaseTest):
+ CLASS_DATA_LEVEL_SETUP = False
+
+ def setUp(self):
+ super().setUp()
+ config = self.team.marketing_analytics_config
+ config.conversion_goals = [
+ ConversionGoalFilter1(
+ kind="EventsNode",
+ event="purchase",
+ name="Purchases",
+ conversion_goal_id=GOAL_ID,
+ conversion_goal_name="Purchases",
+ schema_map={},
+ math=BaseMathType.TOTAL,
+ ).model_dump()
+ ]
+ config.save()
+
+ # A spill threshold above the per-query memory limit can never fire: the query hits the limit
+ # first. The shared 22 GiB constant is sized for funnels, whose peak is an order of magnitude
+ # above these queries, and using it here shipped a setting that did nothing.
+ def test_spill_threshold_is_reachable(self):
+ peak_bytes = 2 * 1024 * 1024 * 1024
+ self.assertLess(MARKETING_SPILL_AFTER_BYTES, peak_bytes)
+ self.assertLess(MARKETING_SPILL_AFTER_BYTES, MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY)
+
+ @parameterized.expand(
+ [
+ ("attribution_table", MarketingAnalyticsAttributionQueryRunner),
+ ("attribution_paths", MarketingAnalyticsAttributionPathsQueryRunner),
+ ("marketing_table", MarketingAnalyticsTableQueryRunner),
+ ]
+ )
+ def test_the_runner_passes_the_spill_threshold(self, name, runner_class):
+ # The settings travel beside the SQL rather than inside it, so a snapshot of the printed
+ # query cannot show whether they were passed at all.
+ date_range = DateRange(date_from="2023-01-01", date_to="2023-01-31")
+ query: (
+ MarketingAnalyticsTableQuery | MarketingAnalyticsAttributionPathsQuery | MarketingAnalyticsAttributionQuery
+ )
+ if runner_class is MarketingAnalyticsTableQueryRunner:
+ query = MarketingAnalyticsTableQuery(dateRange=date_range, properties=[], select=[])
+ elif runner_class is MarketingAnalyticsAttributionPathsQueryRunner:
+ query = MarketingAnalyticsAttributionPathsQuery(
+ conversionGoalId=GOAL_ID, dateRange=date_range, properties=[]
+ )
+ else:
+ query = MarketingAnalyticsAttributionQuery(
+ conversionGoalId=GOAL_ID,
+ breakdownBy=MarketingAnalyticsAttributionBreakdown.CHANNEL,
+ dateRange=date_range,
+ properties=[],
+ )
+
+ captured = {}
+
+ def _capture(**kwargs):
+ captured.update(kwargs)
+ raise _Stop
+
+ runner = runner_class(query=query, team=self.team)
+ with patch.object(sys.modules[runner_class.__module__], "execute_hogql_query", _capture):
+ with self.assertRaises(_Stop):
+ runner.calculate()
+
+ settings = captured.get("settings")
+ assert settings is not None, f"{name} passed no settings to ClickHouse"
+ self.assertEqual(settings.max_bytes_before_external_group_by, MARKETING_SPILL_AFTER_BYTES)