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.

fix(insights): sort unordered insight list on an indexed column (#105670)

posthog[bot]bfa602e8af35
diff --git a/products/product_analytics/backend/presentation/insight.py b/products/product_analytics/backend/presentation/insight.py
index 5b2eb4c226f6..9b7abd05e125 100644
--- a/products/product_analytics/backend/presentation/insight.py
+++ b/products/product_analytics/backend/presentation/insight.py
@@ -1903,7 +1903,10 @@ class InsightViewSet(
if not order:
if self.request.GET.get("search"):
return queryset
- return queryset.order_by("order")
+ # `order` is a vestigial nullable column with no index, so sorting on it forces a full
+ # scan and sort of the project. `-last_modified_at` is covered by dashboarditem_team_lmod_idx.
+ # `-pk` breaks ties so paginated pages stay stable when timestamps collide.
+ return queryset.order_by("-last_modified_at", "-pk")
if order == "-last_viewed_at":
return queryset.order_by(F("last_viewed_at").desc(nulls_last=True))
diff --git a/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr b/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
index 92060f8aad50..490a0c1a7c4f 100644
--- a/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
+++ b/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
@@ -1195,8 +1195,8 @@
LEFT OUTER JOIN "posthog_user" ON ("posthog_dashboarditem"."created_by_id" = "posthog_user"."id")
WHERE ("posthog_team"."project_id" = 99999
AND NOT ("posthog_dashboarditem"."deleted"))
- ORDER BY "posthog_dashboarditem"."order" ASC,
- "posthog_dashboarditem"."id" ASC
+ ORDER BY "posthog_dashboarditem"."last_modified_at" DESC,
+ "posthog_dashboarditem"."id" DESC
LIMIT 100
'''
# ---
diff --git a/products/product_analytics/backend/tests/api/test_insight.py b/products/product_analytics/backend/tests/api/test_insight.py
index 669f8183f9e7..13ddb81d5bdb 100644
--- a/products/product_analytics/backend/tests/api/test_insight.py
+++ b/products/product_analytics/backend/tests/api/test_insight.py
@@ -1010,6 +1010,31 @@ class TestInsight(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
"explicit order=-id should override relevance ranking and put newer insight first"
)
+ def test_list_without_order_sorts_by_last_modified_at_descending(self):
+ now = timezone.now()
+ older = Insight.objects.create(
+ name="older",
+ team=self.team,
+ filters={"events": [{"id": "$pageview"}]},
+ order=1,
+ last_modified_at=now - timedelta(days=2),
+ )
+ newer = Insight.objects.create(
+ name="newer",
+ team=self.team,
+ filters={"events": [{"id": "$pageview"}]},
+ order=2,
+ last_modified_at=now - timedelta(days=1),
+ )
+
+ response = self.client.get(f"/api/projects/{self.team.id}/insights/")
+ assert response.status_code == status.HTTP_200_OK
+ result_ids = [r["id"] for r in response.json()["results"]]
+
+ assert result_ids.index(newer.id) < result_ids.index(older.id), (
+ "the default list order must be newest-modified first, not the vestigial `order` column"
+ )
+
def test_list_filter_by_search_hides_similar_matches_when_exact_matches_exist(self):
for name in ("dashboard overview", "sales dashboard", "dahsboard metrics", "Engineering metrics"):
Insight.objects.create(name=name, team=self.team, filters={"events": [{"id": "$pageview"}]})