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(engineering-analytics): build one hogql catalog per request (#109164)

Raúl Negrón-Otero9d74a49dcf14
diff --git a/products/engineering_analytics/backend/logic/queries/_curated.py b/products/engineering_analytics/backend/logic/queries/_curated.py
index b639fb921538..de08cbf1cefb 100644
--- a/products/engineering_analytics/backend/logic/queries/_curated.py
+++ b/products/engineering_analytics/backend/logic/queries/_curated.py
@@ -20,6 +20,9 @@ from typing import TYPE_CHECKING
from posthog.schema import HogQLQueryResponse
from posthog.hogql import ast
+from posthog.hogql.context import HogQLContext
+from posthog.hogql.database.database import Database
+from posthog.hogql.modifiers import create_default_modifiers_for_team
from posthog.hogql.parser import parse_select
from posthog.hogql.query import execute_hogql_query
@@ -172,6 +175,7 @@ class CuratedGitHubSource:
self._trunk_quarantine_resolved = False
self._depot_job_attempts_table: depot_ci.DepotJobAttempts | None = None
self._depot_job_attempts_resolved = False
+ self._database: Database | None = None
@property
def team(self) -> Team:
@@ -609,6 +613,17 @@ class CuratedGitHubSource:
raise QueryWorkLimitExceededError
self._queries_remaining -= 1
uac = self._user_access_control
+ user = uac.user if uac is not None else None
+ bypass_warehouse_access_control = uac is None
+ if self._database is None:
+ self._database = Database.create_for(
+ team=self._team,
+ user=user,
+ user_access_control=uac,
+ modifiers=create_default_modifiers_for_team(self._team),
+ bypass_warehouse_access_control=bypass_warehouse_access_control,
+ trigger="engineering_analytics",
+ )
with tags_context(product=Product.ENGINEERING_ANALYTICS, feature=Feature.QUERY, team_id=self._team.pk):
return execute_hogql_query(
query=parse_select(sql, placeholders=placeholders),
@@ -617,15 +632,16 @@ class CuratedGitHubSource:
# The logs table lives on a separate ClickHouse cluster (Workload.LOGS); warehouse
# reads use the default. Callers pass the workload that matches the tables they query.
workload=workload,
- # Forward the real user, not just the access control: a userless build drops the access
- # control and fails closed (see _compute_system_table_access_decision), so the user is what
- # lets HogQL honor the per-table warehouse ACL.
- user=uac.user if uac is not None else None,
+ user=user,
user_access_control=uac,
- # No user means a system / Temporal / CLI caller (the facade's documented userless path).
- # There is no principal to honor the ACL with, so bypass it rather than fail closed and
- # strip the tables — bypass is set ONLY in this genuinely userless case.
- bypass_warehouse_access_control=uac is None,
+ bypass_warehouse_access_control=bypass_warehouse_access_control,
+ context=HogQLContext(
+ team_id=self._team.pk,
+ user=user,
+ user_access_control=uac,
+ bypass_warehouse_access_control=bypass_warehouse_access_control,
+ database=self._database,
+ ),
)
diff --git a/products/engineering_analytics/backend/tests/test_logic_workflows.py b/products/engineering_analytics/backend/tests/test_logic_workflows.py
index acf45efc0782..490b1f46e681 100644
--- a/products/engineering_analytics/backend/tests/test_logic_workflows.py
+++ b/products/engineering_analytics/backend/tests/test_logic_workflows.py
@@ -9,6 +9,8 @@ from unittest import mock
from parameterized import parameterized
+from posthog.hogql.database.database import Database
+
from products.engineering_analytics.backend.facade import api
from products.engineering_analytics.backend.facade.contracts import DeliveryStage
from products.engineering_analytics.backend.logic import build_workflow_health
@@ -273,7 +275,9 @@ class TestWorkflowEndpointsWarehouse(_EndpointsWarehouseMixin, BaseTest):
],
)
- overview = api.get_repo_overview(team=self.team, include_series=False)
+ with mock.patch.object(Database, "create_for", wraps=Database.create_for) as build_catalog:
+ overview = api.get_repo_overview(team=self.team, include_series=False)
+ assert build_catalog.call_count == 1 # one catalog for every query of the request
assert overview.merged_pr_count == 2 # 80 and 81; 82 merged long before the window
assert overview.merged_pr_count_prev == 0
assert overview.median_open_to_merge_seconds == pytest.approx(8 * 86400) # bot PR 81 excluded