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(cohorts): apply full HogQL global settings to recalculation query (#76424)

posthog[bot]4c3fb48f7c65
diff --git a/products/cohorts/backend/models/util.py b/products/cohorts/backend/models/util.py
index 236d2439dae7..03523e02ca48 100644
--- a/products/cohorts/backend/models/util.py
+++ b/products/cohorts/backend/models/util.py
@@ -18,7 +18,7 @@ from pydantic import ValidationError as PydanticValidationError
from rest_framework.exceptions import ValidationError
from posthog.hogql import ast
-from posthog.hogql.constants import HogQLGlobalSettings, LimitContext
+from posthog.hogql.constants import HogQLGlobalSettings, LimitContext, get_default_hogql_global_settings
from posthog.hogql.hogql import HogQLContext
from posthog.hogql.modifiers import create_default_modifiers_for_team
from posthog.hogql.parser import parse_select
@@ -956,7 +956,20 @@ def _recalculate_cohortpeople_for_team_hogql(
cohort_id=cohort.pk,
team_id=team.id,
)
- hogql_global_settings = HogQLGlobalSettings()
+ settings = get_default_hogql_global_settings(team_id=team.id).model_dump(exclude_none=True)
+ # This runs INSERT INTO cohortpeople; readonly=2 (a HogQLGlobalSettings default) would make
+ # ClickHouse reject the write, so drop it — same as the preaggregation INSERT path.
+ settings.pop("readonly", None)
+ settings.update(
+ {
+ "max_execution_time": COHORT_QUERY_TIMEOUT_SECONDS,
+ "send_timeout": COHORT_QUERY_TIMEOUT_SECONDS,
+ "receive_timeout": COHORT_QUERY_TIMEOUT_SECONDS,
+ "optimize_on_insert": 0,
+ "max_bytes_ratio_before_external_group_by": 0.5,
+ "max_bytes_ratio_before_external_sort": 0.5,
+ }
+ )
return sync_execute(
recalculate_cohortpeople_sql,
@@ -966,16 +979,7 @@ def _recalculate_cohortpeople_for_team_hogql(
"team_id": team.id,
"new_version": pending_version,
},
- settings={
- "max_execution_time": COHORT_QUERY_TIMEOUT_SECONDS,
- "send_timeout": COHORT_QUERY_TIMEOUT_SECONDS,
- "receive_timeout": COHORT_QUERY_TIMEOUT_SECONDS,
- "optimize_on_insert": 0,
- "max_ast_elements": hogql_global_settings.max_ast_elements,
- "max_expanded_ast_elements": hogql_global_settings.max_expanded_ast_elements,
- "max_bytes_ratio_before_external_group_by": 0.5,
- "max_bytes_ratio_before_external_sort": 0.5,
- },
+ settings=settings,
workload=Workload.OFFLINE,
ch_user=ClickHouseUser.COHORTS,
team_id=team.id,