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): cache the static system-table access-scope list (#69437)

Paul D'Ambra80f4a46e05bc
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.py
index be5ef2d22b9b..02a4641cf8ca 100644
--- a/posthog/hogql/database/database.py
+++ b/posthog/hogql/database/database.py
@@ -150,6 +150,7 @@ from posthog.models.team.team import Team, WeekStartDay
from posthog.ph_client import feature_enabled_or_false
from posthog.rbac.user_access_control import NO_ACCESS_LEVEL, UserAccessControl
from posthog.schema_enums import DatabaseSerializedFieldType, PersonsOnEventsMode, SessionTableVersion
+from posthog.scopes import APIScopeObject
from posthog.synthetic_user import SyntheticUser
from products.data_tools.backend.models.join import DataWarehouseJoin
@@ -442,6 +443,23 @@ def _construct_database_root_node(*, include_posthog_tables: bool) -> TableNode:
return TableNode(children=children)
+@cache
+def _system_table_access_scopes() -> tuple[tuple[str, APIScopeObject], ...]:
+ """(table name, access scope) for the access-controlled Postgres system tables.
+
+ Cached for the process lifetime — this result directly gates table visibility in access-control
+ decisions, so every entry here MUST remain process-static. Do NOT make a system table's
+ access_scope dynamic (per-team, per-flag, or env-driven at call time): this cache would silently
+ serve stale scopes and bypass the restriction. Today SystemTables().children is a static
+ class-level dict of module-level PostgresTable constants, which satisfies that invariant.
+ """
+ return tuple(
+ (name, table_node.table.access_scope)
+ for name, table_node in SystemTables().children.items()
+ if isinstance(table_node.table, PostgresTable) and table_node.table.access_scope is not None
+ )
+
+
def _compute_system_table_access_decision(
team: "Team",
user: Optional["User | SyntheticUser"],
@@ -452,18 +470,12 @@ def _compute_system_table_access_decision(
reads are query-free) and the system-node table names to remove.
Pass user_access_control when it's already preloaded to reuse the instance and avoid an extra query."""
- system_children = SystemTables().children
+ scoped_tables = _system_table_access_scopes()
# Anonymous or synthetic principal: keep only access-controlled tables its scopes cover (none for anonymous / team token).
if user is None or isinstance(user, SyntheticUser):
readable_scopes = user.readable_system_table_access_scopes() if user is not None else set()
- return None, {
- name
- for name, table_node in system_children.items()
- if isinstance(table_node.table, PostgresTable)
- and table_node.table.access_scope is not None
- and table_node.table.access_scope not in readable_scopes
- }
+ return None, {name for name, access_scope in scoped_tables if access_scope not in readable_scopes}
user_access_control = user_access_control or UserAccessControl(user=user, team=team)
@@ -472,11 +484,8 @@ def _compute_system_table_access_decision(
return user_access_control, set()
denied: set[str] = set()
- for name, table_node in system_children.items():
- table = table_node.table
- if not isinstance(table, PostgresTable) or table.access_scope is None:
- continue # Not access-controlled, keep it
- access_level = user_access_control.access_level_for_resource(table.access_scope)
+ for name, access_scope in scoped_tables:
+ access_level = user_access_control.access_level_for_resource(access_scope)
if access_level and access_level != NO_ACCESS_LEVEL:
continue # User has access, keep it
denied.add(name)