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(hogql): gate access-control cache key on org entitlement (#66066)

Alex Lider170957b3bc88
diff --git a/posthog/hogql_queries/query_runner.py b/posthog/hogql_queries/query_runner.py
index e21c85065707..65a4f137f518 100644
--- a/posthog/hogql_queries/query_runner.py
+++ b/posthog/hogql_queries/query_runner.py
@@ -95,6 +95,7 @@ from posthog.clickhouse.client.limit import (
get_org_app_concurrency_limit,
)
from posthog.clickhouse.query_tagging import get_query_tag_value, is_api_key_access_method, tag_queries
+from posthog.constants import AvailableFeature
from posthog.errors import QueryErrorCategory, classify_query_error, clickhouse_error_type
from posthog.event_usage import AnalyticsProps, groups, report_user_or_team_action
from posthog.exceptions_capture import capture_exception
@@ -2240,6 +2241,16 @@ class AnalyticsQueryRunner(QueryRunner, Generic[AR]):
def get_cache_payload(self) -> dict:
payload = super().get_cache_payload()
+ # Don't include restricted resources/objects in cache_payload if the ACCESS_CONTROL
+ # feature is unavailable and a user is provided (i.e. not a userless query or a project token)
+ user = cast("Optional[User | SyntheticUser]", self.user)
+ if (
+ user is not None
+ and not isinstance(user, SyntheticUser)
+ and not self.team.organization.is_feature_available(AvailableFeature.ACCESS_CONTROL)
+ ):
+ return payload
+
# Partition only by the access-controlled tables this query reads that the user is restricted
# from - so queries on events, persons and other non-access-controlled tables share one cache
# entry (incl. userless cache warming).
diff --git a/posthog/hogql_queries/test/test_query_runner.py b/posthog/hogql_queries/test/test_query_runner.py
index 64f203394cf4..330d226e3a24 100644
--- a/posthog/hogql_queries/test/test_query_runner.py
+++ b/posthog/hogql_queries/test/test_query_runner.py
@@ -1552,3 +1552,20 @@ class TestQueryRunnerAccessControlFingerprint(BaseTest):
assert "system.notebooks" in database._denied_tables
ac_queries = [q["sql"] for q in ctx.captured_queries if "ee_accesscontrol" in q["sql"]]
assert ac_queries == [], ac_queries
+
+ def test_unentitled_org_skips_access_control_fingerprint(self):
+ # Without the ACCESS_CONTROL entitlement no user can be restricted, so computing the cache key
+ # must not issue the access-control read
+ self.organization.available_product_features = []
+ self.organization.save()
+
+ runner = self._runner(self.user, base=AnalyticsQueryRunner)
+ with CaptureQueriesContext(connection) as ctx:
+ runner.get_cache_key()
+
+ ac_queries = [q["sql"] for q in ctx.captured_queries if "ee_accesscontrol" in q["sql"]]
+ assert ac_queries == [], ac_queries
+
+ # Entitlement is read off the team's org, so the gate must short-circuit without building
+ # user_access_control (whose membership/preload reads are what we're avoiding per query).
+ assert runner._user_access_control is None