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): skip the list count when the page shows every match (#118678)

posthog[bot]405f86cf1325
diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.py
index fa85e9703ac6..f3a20b99e6a0 100644
--- a/products/signals/backend/test/test_signal_report_api.py
+++ b/products/signals/backend/test/test_signal_report_api.py
@@ -2237,6 +2237,35 @@ class TestSignalReportListAPI(APIBaseTest):
assert body["count"] == 3
assert len(body["results"]) == 1
+ @parameterized.expand(
+ [
+ ("short_first_page", 10, 0, 3, False, False),
+ ("short_last_page", 2, 2, 1, False, False),
+ ("exact_full_page", 3, 0, 3, True, False),
+ ("full_page_with_more", 2, 0, 2, True, True),
+ ("offset_past_the_end", 2, 5, 0, True, False),
+ ]
+ )
+ def test_search_page_counts_without_a_second_scan_when_the_page_is_short(
+ self, _name: str, limit: int, offset: int, rows: int, runs_count_query: bool, has_next: bool
+ ) -> None:
+ for i in range(3):
+ self._create_report(title=f"Checkout failure {i}")
+ self._create_report(title="Unrelated")
+
+ with CaptureQueriesContext(connection) as ctx:
+ response = self.client.get(self._list_url(search="checkout", limit=limit, offset=offset))
+
+ assert response.status_code == status.HTTP_200_OK
+ body = response.json()
+ assert body["count"] == 3
+ assert len(body["results"]) == rows
+ assert (body["next"] is not None) == has_next
+ count_queries = [
+ q for q in ctx.captured_queries if 'COUNT(*) AS "__count" FROM "signals_signalreport"' in q["sql"]
+ ]
+ assert bool(count_queries) == runs_count_query
+
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 4bcff6f67dbe..93fea05cdda7 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -1131,6 +1131,24 @@ class SignalReportReingestionStatusSerializer(serializers.Serializer):
class SignalReportListPagination(LimitOffsetPagination):
+ def paginate_queryset(self, queryset, request, view=None):
+ # Fetch the page before the count. A page that ends before the limit already gives the
+ # total, so the second scan is not needed. A `search` filter rechecks the text of every
+ # candidate report, and that scan costs seconds in a large team.
+ self.limit = self.get_limit(request)
+ if self.limit is None:
+ return None
+ self.offset = self.get_offset(request)
+ self.request = request
+ page = list(queryset[self.offset : self.offset + self.limit])
+ if len(page) < self.limit and (page or self.offset == 0):
+ self.count = self.offset + len(page)
+ else:
+ self.count = self.get_count(queryset)
+ if self.count > self.limit and self.template is not None:
+ self.display_page_controls = True
+ return page
+
def get_paginated_response_schema(self, schema):
paginated = super().get_paginated_response_schema(schema)
paginated["properties"]["ranker"] = {