Skip to content

perf(mcp-analytics): build the activity overview database once per request - #124269

Merged
trunk-io[bot] merged 1 commit into
masterfrom
posthog/mcp-activity-shared-hogql-database
Oct 27, 2026
Merged

trunk-io[bot] merged 1 commit into
masterfrom
posthog/mcp-activity-shared-hogql-database

Conversation

@pauldambra

Copy link
Copy Markdown
Member

Problem

  • People who open MCP analytics wait for the Activity summary, and the summary stays a skeleton until the activity overview request returns.
  • Traces of that request show that the HogQL database build takes about as long as the ClickHouse work.
  • The request runs five queries in parallel threads, and each thread builds its own database. The five builds compete for the GIL.

Changes

  • The activity overview now builds one HogQL database and shares it across its five queries. Nothing a person sees changes, except the response time.
  • Each query still gets its own HogQLContext, because the executor writes to the context while it runs.
  • The database is built with the calling user, so property-level access control still follows that member.
  • The existing HOGQL_SHARED_INSIGHT_DATABASE_ENABLED kill switch turns the sharing off. Then each query builds its own database, as before.

How did you test this code?

Test rationale: the existing TestActivityOverview.test_aggregates_window_and_extracts_error_messages now also asserts that one call builds the database once. It fails if a later change makes each query build its own database again. The existing filter and access-control tests in the same file cover that the shared database gives the same results.

  • Ran: hogli ci:preflight --strict (ruff, format, semgrep) on the two changed files.
  • Not run: the database-backed test suite and mypy. This machine has no local Postgres or ClickHouse, so CI runs them.

Release status

  • No feature flag controls this change

Docs update

None.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Agent: Claude Code, Opus 5.5 (claude-opus-5-5)

  • The work started from a latency report on the Activity summary. APM traces of the endpoint showed the server time did not change during the reported slowdown. The traces also showed the five per-query database builds, which this PR removes.
  • Skills invoked: posthog:exploring-apm-traces, paul-pair, writing-tests, writing-pr-descriptions.
  • Duplicate search: gh pr list found no open PR for this endpoint's latency. fix(mcp-analytics): resolve harness labels after grouping identity fields #122543 changes harness labels, which is other behavior.
  • Public artifact: the diff and this body hold no customer data from the session.

Created with PostHog Desktop

…quest

get_activity_overview runs five HogQL queries in parallel threads, and
each one built its own HogQL database. Traces show that build at about
220 ms per query, more than most of the ClickHouse work, and the threads
compete for the GIL. Build one database and share it across the five
queries, behind the existing shared-database kill switch.

Generated-By: PostHog Desktop
Task-Id: 92fc31f1-8930-45dc-8176-0be023916660
@pauldambra pauldambra self-assigned this Oct 27, 2026
@trunk-io

trunk-io Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

😎 Merged successfully - details.

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

🤖 CI report

⚠️ Trunk lane — backend Python lane

This PR is assigned to the backend Python lane. It runs backend Python tests and may merge in parallel with PRs in other lanes.

✅ Duplication (Python) — clean

New Python code duplication introduced by this branch. Fails at 70+ tokens in app code, or 150+ tokens when both copies live in test files. Advisory while the gate proves itself: extract a shared helper instead of copying.

✅ Duplication (TypeScript) — clean

New TypeScript code duplication introduced by this branch. Fails at 70+ tokens in app code, or 150+ tokens when both copies live in test files. Advisory while the gate proves itself: extract a shared helper instead of copying.

⚠️ Backend coverage — 93.0% of changed backend lines covered — 1 uncovered

🧪 Backend test coverage

Patch coverage — changed backend lines (products + core): ███████████████████░ 93.0% (15 / 16)

File Patch Uncovered changed lines
products/mcp_analytics/backend/logic.py 91.7% 677

🤖 Agents: add a test only if an uncovered line exposes a realistic regression that existing tests miss. Otherwise explain why no new test is needed under "How did you test this code?". Gap list: the patch-coverage artifact on this run (gh run download 353353773637798 -n patch-coverage), or the coverage-data block at the end of this comment.

Per-product line coverage (touched products)
Product Coverage Lines
platform_features ██░░░░░░░░░░░░░░░░░░ 12.1% 7 / 58
demo ███████████░░░░░░░░░ 56.4% 1,542 / 2,736
data_tools ████████████░░░░░░░░ 60.9% 95 / 156
warehouse_sources_queue █████████████░░░░░░░ 65.2% 2,161 / 3,314
authorization ████████████████░░░░ 79.7% 181 / 227
batch_exports ████████████████░░░░ 81.6% 22,900 / 28,059
engineering_analytics █████████████████░░░ 85.0% 11,463 / 13,482
commands █████████████████░░░ 85.5% 1,618 / 1,893
mcp_analytics █████████████████░░░ 86.2% 5,732 / 6,651
ml_inference █████████████████░░░ 86.3% 524 / 607
apm █████████████████░░░ 86.6% 1,616 / 1,866
posthog_ai ██████████████████░░ 87.9% 4,296 / 4,886
notebooks ██████████████████░░ 88.5% 15,330 / 17,321
dashboards ██████████████████░░ 88.6% 7,800 / 8,800
mcp_registry ██████████████████░░ 89.0% 1,596 / 1,794
warehouse_suggestions ██████████████████░░ 89.2% 3,275 / 3,672
webmcp ██████████████████░░ 89.3% 292 / 327
cohorts ██████████████████░░ 89.6% 8,568 / 9,559
product_tours ██████████████████░░ 89.7% 1,355 / 1,510
signals ██████████████████░░ 90.0% 89,149 / 99,033
visual_review ██████████████████░░ 90.6% 9,858 / 10,875
tasks ██████████████████░░ 90.7% 80,114 / 88,335
managed_warehouse ██████████████████░░ 91.0% 12,251 / 13,458
data_warehouse ██████████████████░░ 91.1% 15,326 / 16,822
canvas ██████████████████░░ 91.2% 8,189 / 8,980
aeo ██████████████████░░ 91.2% 1,602 / 1,756
autoresearch ██████████████████░░ 91.4% 11,618 / 12,705
data_modeling ██████████████████░░ 91.5% 10,755 / 11,758
cdp ██████████████████░░ 91.5% 10,282 / 11,238
today ██████████████████░░ 91.6% 3,976 / 4,341
business_knowledge ██████████████████░░ 91.6% 10,242 / 11,177
metrics ██████████████████░░ 91.7% 10,627 / 11,590
slack_app ██████████████████░░ 91.7% 21,646 / 23,601
wizard ██████████████████░░ 91.7% 6,870 / 7,490
streamlit_apps ██████████████████░░ 91.9% 3,139 / 3,415
error_tracking ██████████████████░░ 92.1% 19,004 / 20,643
conversations ██████████████████░░ 92.3% 30,540 / 33,101
review_hog ██████████████████░░ 92.3% 17,915 / 19,413
stamphog ███████████████████░ 92.6% 8,869 / 9,579
early_access_features ███████████████████░ 92.6% 1,339 / 1,446
managed_migrations ███████████████████░ 92.6% 1,630 / 1,760
alerts_platform ███████████████████░ 92.8% 8,192 / 8,830
notifications ███████████████████░ 92.8% 1,217 / 1,311
alerts ███████████████████░ 92.9% 10,060 / 10,831
web_analytics ███████████████████░ 93.3% 26,068 / 27,943
ai_training ███████████████████░ 93.3% 598 / 641
cross_project_dashboards ███████████████████░ 93.4% 880 / 942
surveys ███████████████████░ 93.4% 6,941 / 7,429
exports ███████████████████░ 93.6% 4,166 / 4,452
ai_gateway ███████████████████░ 93.6% 787 / 841
approvals ███████████████████░ 93.7% 4,994 / 5,331
context_layer ███████████████████░ 93.8% 3,981 / 4,244
mcp_store ███████████████████░ 93.9% 9,188 / 9,784
marketing_analytics ███████████████████░ 93.9% 21,234 / 22,605
ai_observability ███████████████████░ 94.0% 29,391 / 31,266
billing_alerts ███████████████████░ 94.0% 2,322 / 2,470
customer_analytics ███████████████████░ 94.1% 27,901 / 29,660
messaging ███████████████████░ 94.1% 4,959 / 5,271
workflows ███████████████████░ 94.3% 24,695 / 26,180
legal_documents ███████████████████░ 94.4% 2,313 / 2,451
logs ███████████████████░ 94.4% 19,309 / 20,457
replay_vision ███████████████████░ 94.4% 32,837 / 34,772
actions ███████████████████░ 94.7% 1,004 / 1,060
reminders ███████████████████░ 94.8% 760 / 802
experiments ███████████████████░ 94.8% 40,350 / 42,564
endpoints ███████████████████░ 94.9% 9,369 / 9,875
growth ███████████████████░ 94.9% 11,348 / 11,960
annotations ███████████████████░ 95.1% 1,095 / 1,151
skills ███████████████████░ 95.2% 7,630 / 8,015
uptime_monitoring ███████████████████░ 95.3% 306 / 321
access_control ███████████████████░ 95.4% 8,106 / 8,493
subscriptions ███████████████████░ 95.6% 13,601 / 14,228
data_catalog ███████████████████░ 95.7% 4,713 / 4,927
product_analytics ███████████████████░ 95.8% 29,875 / 31,195
feature_flags ███████████████████░ 95.8% 28,730 / 29,983
revenue_analytics ███████████████████░ 95.9% 1,877 / 1,958
data_quality ███████████████████░ 96.0% 8,561 / 8,915
tracing ███████████████████░ 96.3% 4,404 / 4,572
warehouse_sources ███████████████████░ 96.5% 481,271 / 498,684
security ███████████████████░ 96.7% 1,549 / 1,602
account_audits ███████████████████░ 96.7% 770 / 796
pulse ███████████████████░ 97.1% 2,023 / 2,083
analytics_platform ████████████████████ 98.0% 2,828 / 2,885
groups ████████████████████ 98.2% 1,838 / 1,871
field_notes ████████████████████ 99.4% 172 / 173

Report-only. Patch coverage = changed backend lines covered vs origin/master. Sorted lowest first.
Known gaps: lines covered only by Temporal tests show as uncovered; core line numbers may drift if master changed the same file.

@pauldambra
pauldambra marked this pull request as ready for review October 27, 2026 19:47
@parameterai

parameterai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Risk: No findings

This PR builds one shared HogQL Database per activity-overview request and passes it to all five parallel queries (each with its own HogQLContext), gated by the existing HOGQL_SHARED_INSIGHT_DATABASE_ENABLED kill switch. I traced the security-relevant behavior and found no issues: the database is rebuilt per request with the calling user, its modifiers and access-control inputs are identical to what each per-query executor previously built, and the pattern matches the already-established shared-database design in query_runner.py.

Sentinel reviewed 19afdd5 · Review settings

@pauldambra pauldambra added posthog-review Run a Standard PostHog Review stamphog Request AI approval (no full review) labels Oct 27, 2026
@pr-assigner-resolver-posthog
pr-assigner-resolver-posthog Bot requested a review from a team October 27, 2026 19:48

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog Review · Standard · reviewed 19afdd5: no findings.

This Standard review started from the posthog-review label. Comment @posthog review --deep for a Deep review, or @posthog help to see all commands.

@posthog posthog Bot removed the posthog-review Run a Standard PostHog Review label Oct 27, 2026

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

The security-sensitive database reuse has current-head independent review and owning-team authorship. The code preserves caller identity and separate query contexts, with no unresolved substantive concerns.

  • Author wrote 0% of the modified lines and has 7 merged PRs in these paths (familiarity MODERATE).
  • PostHog Review (Standard) reviewed the current head: no findings.
  • posthog[bot] reviewed the current head.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
size ✓ 34L, 1F substantive, 40L/2F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1b-small (40L, 2F, single-area, perf)
stamphog 2.6.1 .stamphog/policy.yml @ 19afdd5 · reviewed head 19afdd5

@pauldambra

Copy link
Copy Markdown
Member Author

Note

🤖 Automated comment by QA Swarm — not written by a human

Multi-perspective review: router (cheap-first pass) + delegated lenses (qa-team, paul-reviewer, xp-reviewer, security-audit, engineering-systems-thinking as warranted)

Verdict: ✅ APPROVE (round 1 @ 19afdd5)

No actionable findings. Sharing one Database across the five threads, with a separate HogQLContext for each thread, is the pattern that trends series, funnels compare and the web page directory already use behind the same kill switch.

Key findings

No inline findings. Two notes need no change:

  • 🟢 LOW: under TEST=True, map_in_caller_context runs serially. So the test proves the database is built once, but it cannot prove thread safety. All tests that use this helper have the same limit.
  • ⚪ NIT: _shared_activity_database uses the default access control from Database.create_for. This is the same default the executor used before.

Convergence

Both reviewers found no race. During parse, resolve, print and run, the database gets only locked lazy writes (foreign keys and revenue views). The unlocked tables.add_child calls happen only for the error-tracking recent issue state table, the prompt_jev tables and information_schema. These five queries read only events, so they never reach those calls.

Reviewer summaries

Reviewer Assessment
🧭 router (sonnet) Danger MEDIUM (shared state across threads), confidence HIGH. Modifiers, access control and timezone are the same as before. It sent one concurrency check to a stronger model.
general/concurrency (opus) No actionable risk. It searched every write to Database that happens while a query runs, and found production callers that share one database across threads in the same way.

Automated by QA Swarm — not a human review

@trunk-io

trunk-io Bot commented Oct 27, 2026

Copy link
Copy Markdown

Static Badge   Static Badge   Static Badge

View Full Report ↗︎ ⋅ Docs

@trunk-io
trunk-io Bot merged commit d2d237d into master Oct 27, 2026
384 checks passed
@trunk-io
trunk-io Bot deleted the posthog/mcp-activity-shared-hogql-database branch October 27, 2026 23:39
@deployment-status-posthog

deployment-status-posthog Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-10-28 00:08 UTC Run
prod-us ✅ Deployed 2026-10-28 00:45 UTC Run
prod-eu ✅ Deployed 2026-10-28 00:40 UTC Run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stamphog Request AI approval (no full review)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant