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(error-tracking): skip the discarded row read in the issue state overlay (#99880)

posthog[bot]baf313980cdb
diff --git a/products/error_tracking/backend/facade/api.py b/products/error_tracking/backend/facade/api.py
index 2d6d97ca55ee..ed58e0529ad3 100644
--- a/products/error_tracking/backend/facade/api.py
+++ b/products/error_tracking/backend/facade/api.py
@@ -131,8 +131,8 @@ def _to_issue_assignment_notification(assignment) -> contracts.ErrorTrackingIssu
)
-def list_issues(team_id: int) -> list[contracts.ErrorTrackingIssuePreview]:
- issues = logic.list_issues(team_id)
+def list_issues(team_id: int, limit: int = logic.MAX_LISTED_ISSUES) -> list[contracts.ErrorTrackingIssuePreview]:
+ issues = logic.list_issues(team_id, limit=limit)
return [_to_issue_preview(issue) for issue in issues]
diff --git a/products/error_tracking/backend/hogql_queries/issue_state_overlay.py b/products/error_tracking/backend/hogql_queries/issue_state_overlay.py
index c88dc4fdf381..13c57a555070 100644
--- a/products/error_tracking/backend/hogql_queries/issue_state_overlay.py
+++ b/products/error_tracking/backend/hogql_queries/issue_state_overlay.py
@@ -68,9 +68,21 @@ def latest_issue_state_watermark(team_id: int) -> datetime.datetime | None:
def load_recent_issue_states(team_id: int, *, current_time: datetime.datetime | None = None) -> list[RecentIssueState]:
threshold = (current_time or timezone.now()) - RECENT_ISSUE_STATE_WINDOW
- issues = (
+ recent_issue_ids = list(
ErrorTrackingIssue.objects.using(DEFAULT_DB_ALIAS)
.filter(team_id=team_id, state_updated_at__gte=threshold)
+ .values_list("id", flat=True)[: MAX_RECENT_ISSUE_STATES + 1]
+ )
+
+ RECENT_ISSUE_STATE_ROW_COUNT.observe(len(recent_issue_ids))
+ # Stop before the row read, which costs an assignment probe plus the unbounded name and
+ # description columns per row — all of it discarded once the bound skips the overlay.
+ if len(recent_issue_ids) > MAX_RECENT_ISSUE_STATES:
+ return []
+
+ issues = (
+ ErrorTrackingIssue.objects.using(DEFAULT_DB_ALIAS)
+ .filter(team_id=team_id, id__in=recent_issue_ids)
.select_related("assignment")
.only(
"id",
@@ -81,7 +93,7 @@ def load_recent_issue_states(team_id: int, *, current_time: datetime.datetime |
"description",
"assignment__user_id",
"assignment__role_id",
- )[: MAX_RECENT_ISSUE_STATES + 1]
+ )
)
recent_states: list[RecentIssueState] = []
@@ -108,7 +120,4 @@ def load_recent_issue_states(team_id: int, *, current_time: datetime.datetime |
)
)
- RECENT_ISSUE_STATE_ROW_COUNT.observe(len(recent_states))
- if len(recent_states) > MAX_RECENT_ISSUE_STATES:
- return []
return recent_states
diff --git a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py
index 41c5cbe5f207..dd20a354cb07 100644
--- a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py
+++ b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py
@@ -730,7 +730,7 @@ class TestErrorTrackingQueryRunner(ClickhouseTestMixin, NonAtomicBaseTestKeepIde
state_updated_at=now(),
)
- with self.assertNumQueries(1):
+ with self.assertNumQueries(2):
recent_states = load_recent_issue_states(self.team.pk)
self.assertEqual(
@@ -749,7 +749,9 @@ class TestErrorTrackingQueryRunner(ClickhouseTestMixin, NonAtomicBaseTestKeepIde
ErrorTrackingIssue.objects.create(
team=self.team, status=ErrorTrackingIssue.Status.RESOLVED, state_updated_at=now()
)
- self.assertEqual(load_recent_issue_states(self.team.pk), [])
+ # Over the bound the rows are never read, so the id probe is the only query.
+ with self.assertNumQueries(1):
+ self.assertEqual(load_recent_issue_states(self.team.pk), [])
@time_machine.travel("2022-01-10T12:11:00", tick=False)
def test_recent_issue_state_applies_to_more_than_fifty_fingerprints(self):
diff --git a/products/error_tracking/backend/logic/__init__.py b/products/error_tracking/backend/logic/__init__.py
index c495becd8393..341b4f20789e 100644
--- a/products/error_tracking/backend/logic/__init__.py
+++ b/products/error_tracking/backend/logic/__init__.py
@@ -51,6 +51,11 @@ class ErrorTrackingIssueNotFoundError(Exception):
pass
+# The issue list carries a per-row first_seen subquery, so an unbounded read scales with the
+# team's whole issue table. Cap it, and let callers that need more page with a narrower filter.
+MAX_LISTED_ISSUES = 1000
+
+
def get_issue_list_queryset(team_id: int) -> QuerySet[ErrorTrackingIssue]:
return ErrorTrackingIssue.objects.with_first_seen().select_related("assignment").filter(team_id=team_id)
@@ -65,8 +70,8 @@ def get_issue_detail_queryset(team_id: int) -> QuerySet[ErrorTrackingIssue]:
)
-def list_issues(team_id: int) -> QuerySet[ErrorTrackingIssue]:
- return get_issue_list_queryset(team_id)
+def list_issues(team_id: int, limit: int = MAX_LISTED_ISSUES) -> list[ErrorTrackingIssue]:
+ return list(get_issue_list_queryset(team_id).order_by("-created_at")[:limit])
def list_issues_created_since(team_id: int, since: datetime, limit: int) -> list[ErrorTrackingIssue]:
diff --git a/products/error_tracking/backend/test/test_facade_api.py b/products/error_tracking/backend/test/test_facade_api.py
index 314811dd648b..bf4027f72056 100644
--- a/products/error_tracking/backend/test/test_facade_api.py
+++ b/products/error_tracking/backend/test/test_facade_api.py
@@ -48,6 +48,14 @@ class TestErrorTrackingFacadeAPI(BaseTest):
assert issues[0].assignee.id == self.user.id
assert issues[0].assignee.type == "user"
+ def test_list_issues_caps_results_and_returns_newest_first(self):
+ self._create_issue(team=self.team, name="Older issue")
+ newer = self._create_issue(team=self.team, name="Newer issue")
+
+ issues = api.list_issues(team_id=self.team.id, limit=1)
+
+ assert [issue.id for issue in issues] == [newer.id]
+
def test_get_issue_returns_contract(self):
issue = self._create_issue(team=self.team, name="Unhandled TypeError")