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.

perf(marketing-analytics): let query GROUP BY spill to disk (#90592)

Javier Bahamondes18b0d5a13f70
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 ba6b95c6a120..dac92d0027ad 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,7 +22,7 @@ from posthog.schema import (
)
from posthog.hogql import ast
-from posthog.hogql.constants import LimitContext
+from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, HogQLGlobalSettings, LimitContext
from posthog.hogql.query import execute_hogql_query
from .attribution_base import PERSON_ARRAYS_CTE, AttributionQueryRunnerBase
@@ -295,6 +295,9 @@ class MarketingAnalyticsAttributionPathsQueryRunner(
modifiers=self.modifiers,
limit_context=self.limit_context or LimitContext.QUERY,
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),
)
# 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 9ab5deff4abb..bcdb3923586d 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 LimitContext
+from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, HogQLGlobalSettings, LimitContext
from posthog.hogql.query import execute_hogql_query
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
@@ -476,6 +476,9 @@ class MarketingAnalyticsAttributionQueryRunner(AttributionQueryRunnerBase[Market
modifiers=self.modifiers,
limit_context=self.limit_context or LimitContext.QUERY,
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),
)
# 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/marketing_analytics_aggregated_query_runner.py b/products/marketing_analytics/backend/hogql_queries/marketing_analytics_aggregated_query_runner.py
index a52d785d1ed5..5f91866134ed 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,6 +13,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.query import execute_hogql_query
from .constants import (
@@ -164,6 +165,9 @@ class MarketingAnalyticsAggregatedQueryRunner(
timings=self.timings,
modifiers=self.modifiers,
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),
)
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 8f8cd07a0f98..cd5badedefbd 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,6 +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.query import execute_hogql_query
from posthog.hogql_queries.insights.paginators import HogQLHasMorePaginator
@@ -88,6 +89,9 @@ class MarketingAnalyticsTableQueryRunner(MarketingAnalyticsBaseQueryRunner[Marke
modifiers=self.modifiers,
limit_context=self.limit_context,
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),
)
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 b0e3f7c9bd4f..b9baaeeb87dc 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,6 +13,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.query import execute_hogql_query
from posthog.hogql_queries.insights.paginators import HogQLHasMorePaginator
@@ -84,6 +85,9 @@ class NonIntegratedConversionsTableQueryRunner(
timings=self.timings,
modifiers=self.modifiers,
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),
)
results = response.results or []