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): read the ranking score once in the recommended inbox sort (#118947)

posthog[bot]ff26b518413f
diff --git a/products/signals/backend/ranking/inbox_sort.py b/products/signals/backend/ranking/inbox_sort.py
index d80361ff35e4..64ac93889efa 100644
--- a/products/signals/backend/ranking/inbox_sort.py
+++ b/products/signals/backend/ranking/inbox_sort.py
@@ -6,14 +6,15 @@ gives the priority order. The EU instance gives the priority order without evalu
The merge key lets a client merge rows from different rankers without the raw score. It is the score
decile then the priority rank, so it never says more about the score than one decimal place. Each
-head ranker sorts by the same decile, so the row order does not say more either. Keep
-`score_decile` and `inbox_merge_key` in step.
+head ranker sorts by the same decile, so the row order does not say more either. The database
+computes the decile with `score_decile`, and `inbox_merge_key` reads that value.
"""
from typing import Literal, get_args
-from django.db.models import Case, F, FloatField, Value, When
-from django.db.models.functions import Floor
+from django.contrib.postgres.fields import ArrayField
+from django.db.models import FloatField, Func, IntegerField, Value
+from django.db.models.expressions import Combinable
import structlog
import posthoganalytics
@@ -69,23 +70,18 @@ def resolve_recommended_ranker(team: Team, user: User) -> InboxRanker:
return "priority"
-def score_decile(field: str) -> Case:
- """The SQL form of the decile in `inbox_merge_key`. NULL stays NULL."""
- return Case(
- When(**{f"{field}__isnull": True}, then=Value(None)),
- When(**{f"{field}__lt": 0}, then=Value(0.0)),
- When(**{f"{field}__gte": (_DECILES - 1) / _DECILES}, then=Value(float(_DECILES - 1))),
- default=Floor(F(field) * _DECILES),
- output_field=FloatField(),
- )
+def score_decile(score: Combinable) -> Func:
+ """The score decile, 0 to 9. A score below 0 gives 0, a score of 0.9 or more gives 9, and NULL stays NULL.
+ `width_bucket` reads the score once. A CASE repeats the score in each branch, so when the score is a
+ correlated subquery, Postgres runs the subquery once per branch.
+ """
+ thresholds = Value([float(bound) for bound in range(1, _DECILES)], output_field=ArrayField(FloatField()))
+ return Func(score * _DECILES, thresholds, function="width_bucket", output_field=IntegerField())
-def inbox_merge_key(score: float | None, priority_rank: int | None) -> int:
+
+def inbox_merge_key(decile: int | None, priority_rank: int | None) -> int:
"""Lower keys rank first. A report without a score sorts after every scored report."""
- if score is None:
- decile_rank = _DECILES
- else:
- decile = min(max(int(score * _DECILES), 0), _DECILES - 1)
- decile_rank = _DECILES - 1 - decile
+ decile_rank = _DECILES if decile is None else _DECILES - 1 - decile
priority = _PRIORITY_SLOTS - 1 if priority_rank is None else min(max(priority_rank, 0), _PRIORITY_SLOTS - 1)
return decile_rank * _PRIORITY_SLOTS + priority
diff --git a/products/signals/backend/serializers.py b/products/signals/backend/serializers.py
index 51191b2768a0..c600b1278c5c 100644
--- a/products/signals/backend/serializers.py
+++ b/products/signals/backend/serializers.py
@@ -2315,8 +2315,8 @@ class SignalReportListSerializer(SignalReportSerializer):
ranker = self.context.get("inbox_ranker")
if ranker is None:
return None
- score = None if ranker == "priority" else getattr(obj, f"ranking_{ranker}_score", None)
- return inbox_merge_key(score, getattr(obj, "priority_sort_rank", None))
+ decile = None if ranker == "priority" else getattr(obj, f"ranking_{ranker}_score_decile", None)
+ return inbox_merge_key(decile, getattr(obj, "priority_sort_rank", None))
class SignalReportSectionCountsSerializer(serializers.Serializer):
diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.py
index 306ee6035234..79cdc7ad3f90 100644
--- a/products/signals/backend/test/test_signal_report_api.py
+++ b/products/signals/backend/test/test_signal_report_api.py
@@ -1840,9 +1840,19 @@ class TestSignalReportListAPI(APIBaseTest):
with patch("products.signals.backend.ranking.inbox_sort.posthoganalytics") as analytics:
get_flag = analytics.get_feature_flag
get_flag.return_value = outcome
- response = self.client.get(self._list_url(status="ready", sort="recommended"))
+ with CaptureQueriesContext(connection) as ctx:
+ response = self.client.get(self._list_url(status="ready", sort="recommended"))
assert response.status_code == status.HTTP_200_OK
+ list_sql = [
+ query["sql"]
+ for query in ctx.captured_queries
+ if query["sql"].startswith('SELECT "signals_signalreport"."id"') and "ORDER BY" in query["sql"]
+ ]
+ assert len(list_sql) == 1
+ # The sort reads every matching report, so a score subquery repeated in the ORDER BY
+ # multiplies the artefact walks for the whole team.
+ assert list_sql[0].count("'ranking_score'") == 1
assert get_flag.call_args.args[1] == self.user.distinct_id
body = response.json()
assert body["ranker"] == outcome
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index 68f19f55aab6..159669663034 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -2414,9 +2414,19 @@ class SignalReportViewSet(
if annotation not in ordered_fields or annotation in annotated:
continue
annotated.add(annotation)
- queryset = queryset.annotate(**{annotation: self._latest_ranking_value(head, "scores")})
+ latest_score = self._latest_ranking_value(head, "scores")
+ if self._inbox_ranker is not None:
+ queryset = queryset.annotate(
+ **{self._ranking_decile_annotation(annotation): score_decile(latest_score)}
+ )
+ else:
+ queryset = queryset.annotate(**{annotation: latest_score})
return queryset
+ @staticmethod
+ def _ranking_decile_annotation(score_annotation: str) -> str:
+ return f"{score_annotation}_decile"
+
@staticmethod
def _latest_ranking_value(head: str, key: Literal["scores", "lifts"]) -> Subquery:
# The served model's value for one head from the latest ranking_score artefact, as
@@ -2683,7 +2693,7 @@ class SignalReportViewSet(
name = clause.lstrip("-")
if name in ranking_annotations:
# The recommended preset sorts by the score decile, so the order shows no more than `inbox_merge_key`.
- expression = score_decile(name) if self._inbox_ranker is not None else F(name)
+ expression = F(self._ranking_decile_annotation(name) if self._inbox_ranker is not None else name)
# Unscored reports sort after every scored report, in both directions.
order_by.append(
expression.desc(nulls_last=True) if clause.startswith("-") else expression.asc(nulls_last=True)