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(signals): sort report ids before loading inbox list rows (#126430)

Dylan Martindf0af0b0d405
diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.py
index c08c3ddc7dfd..e0c40b081ea0 100644
--- a/products/signals/backend/test/test_signal_report_api.py
+++ b/products/signals/backend/test/test_signal_report_api.py
@@ -1676,7 +1676,7 @@ class TestSignalReportListAPI(APIBaseTest):
list_sql = [
query["sql"]
for query in ctx.captured_queries
- if query["sql"].startswith('SELECT "signals_signalreport"."id"') and "ORDER BY" in query["sql"]
+ if query["sql"].startswith('SELECT "signals_signalreport"."id" AS "pk"')
]
assert len(list_sql) == 1
# The sort reads every matching report, so a priority subquery repeated per CASE branch
@@ -1856,7 +1856,7 @@ class TestSignalReportListAPI(APIBaseTest):
list_sql = [
query["sql"]
for query in ctx.captured_queries
- if query["sql"].startswith('SELECT "signals_signalreport"."id"') and "ORDER BY" in query["sql"]
+ if query["sql"].startswith('SELECT "signals_signalreport"."id" AS "pk"')
]
assert len(list_sql) == 1
# The sort reads every matching report, so a score subquery repeated in the ORDER BY
@@ -2628,6 +2628,23 @@ class TestSignalReportListAPI(APIBaseTest):
]
assert bool(count_queries) == runs_count_query
+ @parameterized.expand([("plain", {}), ("search", {"search": "checkout"})])
+ def test_page_sort_selects_ids_without_joining_one_to_one_rows(self, _name: str, params: dict[str, str]) -> None:
+ for i in range(3):
+ self._create_report(title=f"Checkout failure {i}")
+
+ with CaptureQueriesContext(connection) as ctx:
+ response = self.client.get(self._list_url(ordering="-created_at", limit=2, **params))
+
+ assert response.status_code == status.HTTP_200_OK
+ assert [r["title"] for r in response.json()["results"]] == ["Checkout failure 2", "Checkout failure 1"]
+ sort_queries = [
+ q["sql"] for q in ctx.captured_queries if q["sql"].startswith('SELECT "signals_signalreport"."id" AS "pk"')
+ ]
+ assert len(sort_queries) == 1
+ assert "signals_signalreporttrackerissue" not in sort_queries[0]
+ assert "signals_signalreportrefund" not in sort_queries[0]
+
def test_filter_actionability_absent_returns_all(self):
a = self._create_report(title="A")
self._actionability_artefact(a, actionability="immediately_actionable")
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index b6710ce86d20..56a259a24665 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -1346,10 +1346,9 @@ class SignalReportListPagination(FacadeLimitOffsetPagination):
return None
self.offset = self.get_offset(request)
self.request = request
- if request.query_params.get("search"):
- page, window_count = self._search_page(queryset, self.offset, self.limit)
- else:
- page, window_count = list(queryset[self.offset : self.offset + self.limit]), None
+ page, window_count = self._page_by_ids(
+ queryset, self.offset, self.limit, with_window_count=bool(request.query_params.get("search"))
+ )
if len(page) < self.limit and (page or self.offset == 0):
self.count = self.offset + len(page)
elif window_count is not None:
@@ -1360,17 +1359,27 @@ class SignalReportListPagination(FacadeLimitOffsetPagination):
self.display_page_controls = True
return page
- def _search_page(self, queryset, offset: int, limit: int) -> tuple[list[SignalReport], int | None]:
- # Ids only, because COUNT(*) OVER () buffers every match and full report rows spill to disk.
- rows = list(
- queryset.annotate(search_total=Window(Count("*"))).values_list("pk", "search_total")[
- offset : offset + limit
- ]
- )
- if not rows:
+ def _page_by_ids(
+ self, queryset, offset: int, limit: int, *, with_window_count: bool
+ ) -> tuple[list[SignalReport], int | None]:
+ # The sort covers every matching report in the team, so it selects ids only. Full rows would
+ # carry the wide report columns and the refund and tracker issue joins through that sort, and
+ # COUNT(*) OVER () would buffer them and spill to disk. The full rows load for the page alone.
+ if with_window_count:
+ rows = list(
+ queryset.annotate(search_total=Window(Count("*"))).values_list("pk", "search_total")[
+ offset : offset + limit
+ ]
+ )
+ page_ids = [pk for pk, _ in rows]
+ window_count = rows[0][1] if rows else None
+ else:
+ page_ids = list(queryset.values_list("pk", flat=True)[offset : offset + limit])
+ window_count = None
+ if not page_ids:
return [], None
- reports = {report.pk: report for report in queryset.filter(pk__in=[pk for pk, _ in rows]).order_by()}
- return [reports[pk] for pk, _ in rows if pk in reports], rows[0][1]
+ reports = {report.pk: report for report in queryset.filter(pk__in=page_ids).order_by()}
+ return [reports[pk] for pk in page_ids if pk in reports], window_count
def get_paginated_response_schema(self, schema):
paginated = super().get_paginated_response_schema(schema)