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(query-cache): keep runs as different clickhouse users out of one flight (#122302)

Jimmy Zhu1c7115c9a388
diff --git a/posthog/hogql_queries/query_runner.py b/posthog/hogql_queries/query_runner.py
index 166cfc3a44fb..5e412c4563db 100644
--- a/posthog/hogql_queries/query_runner.py
+++ b/posthog/hogql_queries/query_runner.py
@@ -3456,7 +3456,11 @@ class QueryRunner(ABC, Generic[Q, R, CR]):
def single_flight_variant(self) -> str:
"""Separates single flights of runs that share a cache key but not their limits or results.
Runners add any input that changes either without reaching the cache key."""
- return self.workload.value
+ # Each ClickHouse user has its own concurrency limit, and a leader that its user's limit
+ # refuses or cancels leaves a follower from another user nothing to serve. The runner's own
+ # user wins over the tagged one, as it does when the query executes.
+ ch_user = self.ch_user.value if self.ch_user != ClickHouseUser.DEFAULT else get_query_tag_value("ch_user")
+ return self.workload.value if ch_user is None else f"{self.workload.value}:ch_user={ch_user}"
def requires_fresh_calculation(self) -> bool:
"""Runners whose results reflect live, mutable state that must never be served stale
diff --git a/posthog/hogql_queries/test/test_query_runner.py b/posthog/hogql_queries/test/test_query_runner.py
index 2930654676b5..e5f5299152b3 100644
--- a/posthog/hogql_queries/test/test_query_runner.py
+++ b/posthog/hogql_queries/test/test_query_runner.py
@@ -65,9 +65,9 @@ from posthog.hogql.parser import parse_select
from posthog.hogql.query_stats import get_active, record
from posthog.api.services.query import _run_query_runner
-from posthog.clickhouse.client.connection import Workload
+from posthog.clickhouse.client.connection import ClickHouseUser, Workload
from posthog.clickhouse.client.limit import ConcurrencyLimitExceeded
-from posthog.clickhouse.query_tagging import reset_query_tags, tag_queries
+from posthog.clickhouse.query_tagging import reset_query_tags, tag_queries, tags_context
from posthog.constants import AvailableFeature
from posthog.errors import ExposedCHQueryError, wrap_clickhouse_query_error
from posthog.event_usage import EventSource
@@ -2824,6 +2824,16 @@ class TestQuerySingleFlightRunner(BaseTest):
runner.get_cache_key(), budget_for_limit_context(runner.limit_context), runner.single_flight_variant()
)
+ @parameterized.expand([("tagged", None), ("passed_to_the_runner", ClickHouseUser.ALERTS_PLATFORM_INSIGHT)])
+ def test_runs_as_another_clickhouse_user_do_not_share_a_flight(self, _name, runner_user):
+ runner_class = setup_test_query_runner_class()
+ default = self._flight_of(runner_class(query={"some_attr": "bla"}, team=self.team))
+ other = runner_class(query={"some_attr": "bla"}, team=self.team, ch_user=runner_user or ClickHouseUser.DEFAULT)
+ with tags_context(ch_user=None if runner_user else ClickHouseUser.ALERTS_PLATFORM_INSIGHT):
+ flight = self._flight_of(other)
+
+ assert flight.lock_key != default.lock_key
+
@parameterized.expand([("success", None), ("failure", ClickHouseQueryTimeOut), ("interrupted", _LeaderInterrupted)])
def test_leader_releases_the_flight(self, _name, error_class):
runner_class = setup_test_query_runner_class()
diff --git a/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py b/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py
index b45173273a48..f309eb820e8c 100644
--- a/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py
@@ -174,8 +174,6 @@ class TrendsQueryRunner(AnalyticsQueryRunner[TrendsQueryResponse]):
def single_flight_variant(self) -> str:
# A caller can pass its own execution time, which does not reach the cache key.
variant = super().single_flight_variant()
- if self.ch_user != ClickHouseUser.DEFAULT:
- variant = f"{variant}:ch_user={self.ch_user.value}"
if self.hogql_settings is not None:
variant = f"{variant}:max_execution_time={self.hogql_settings.max_execution_time}"
return variant