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(mcp-analytics): build the activity overview database once per request (#124269)

Paul D'Ambrad2d237deb9c9
diff --git a/products/mcp_analytics/backend/logic.py b/products/mcp_analytics/backend/logic.py
index 1143bf572900..7fffd660b9c7 100644
--- a/products/mcp_analytics/backend/logic.py
+++ b/products/mcp_analytics/backend/logic.py
@@ -11,12 +11,16 @@ from django.utils import timezone
from posthog.schema import AnyPropertyFilterDiscriminated, DateRange, EventPropertyFilter
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
from posthog.clickhouse.query_tagging import Feature, Product, tags_context
from posthog.hogql_queries.utils.caller_context import map_in_caller_context
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
+from posthog.models.instance_setting import get_instance_setting
from posthog.models.person import Person
from posthog.models.person.util import get_persons_mapped_by_distinct_id
from posthog.models.team.team import Team
@@ -653,14 +657,32 @@ def _extract_error_message(raw: Any) -> str | None:
def _run_activity_query(
- team: Team, sql: str, name: str, placeholders: dict[str, ast.Expr], user: User | None
+ team: Team,
+ sql: str,
+ name: str,
+ placeholders: dict[str, ast.Expr],
+ user: User | None,
+ database: Database | None,
) -> list[Any]:
query = parse_select(sql, placeholders={**placeholders})
+ # Each thread needs its own context, because the executor writes to it while it runs.
+ context = HogQLContext(team_id=team.pk, user=user, database=database)
with tags_context(product=Product.MCP_ANALYTICS, feature=Feature.QUERY, team_id=team.id, name=name):
- response = execute_hogql_query(query=query, team=team, user=user)
+ response = execute_hogql_query(query=query, team=team, user=user, context=context)
return response.results or []
+def _shared_activity_database(team: Team, user: User | None) -> Database | None:
+ if not get_instance_setting("HOGQL_SHARED_INSIGHT_DATABASE_ENABLED"):
+ return None
+ return Database.create_for(
+ team=team,
+ user=user,
+ modifiers=create_default_modifiers_for_team(team),
+ trigger="mcp_activity_overview",
+ )
+
+
def get_activity_overview(
team: Team,
properties: list[AnyPropertyFilterDiscriminated] | None = None,
@@ -698,9 +720,15 @@ def get_activity_overview(
filter_test_accounts,
)
+ # Building the database costs more Python time than most of these queries spend in
+ # ClickHouse, and the threads share the GIL, so build it once for all five.
+ database = _shared_activity_database(team, user)
+
def run_query(spec: tuple[str, str, object]) -> list[Any]:
assert isinstance(spec[2], dict)
- return _run_activity_query(team, spec[0], spec[1], {**spec[2], "shared_filters": shared_filters}, user)
+ return _run_activity_query(
+ team, spec[0], spec[1], {**spec[2], "shared_filters": shared_filters}, user, database
+ )
stats_rows, missing_capability_rows, top_tools_rows, clients_rows, recent_calls_rows = map_in_caller_context(
run_query,
diff --git a/products/mcp_analytics/backend/tests/test_api.py b/products/mcp_analytics/backend/tests/test_api.py
index 775f72e326a7..4ee4c19d691c 100644
--- a/products/mcp_analytics/backend/tests/test_api.py
+++ b/products/mcp_analytics/backend/tests/test_api.py
@@ -12,6 +12,8 @@ from parameterized import parameterized
from posthog.schema import AnyPropertyFilterDiscriminated, EventPropertyFilter, PersonPropertyFilter, PropertyOperator
+from posthog.hogql.database.database import Database
+
from posthog.models.utils import uuid7
from posthog.test.persons import create_person
from posthog.utils import generate_cache_key
@@ -821,8 +823,10 @@ class TestActivityOverview(_MCPAnalyticsTeamScopedTestMixin, ClickhouseTestMixin
properties={"$session_id": str(uuid7()), "$mcp_tool_name": "query_run"},
)
- overview = api.get_activity_overview(self.team)
+ with patch.object(Database, "create_for", wraps=Database.create_for) as create_database:
+ overview = api.get_activity_overview(self.team)
+ assert create_database.call_count == 1
assert overview.stats == contracts.ActivityStats(
total_calls=2,
distinct_tools=2,