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(welcome): drop the pk-ordered team id scans (#93005)

posthog[bot]8d4720f9ee46
diff --git a/posthog/api/welcome.py b/posthog/api/welcome.py
index c752fa1692ed..4daf448893dc 100644
--- a/posthog/api/welcome.py
+++ b/posthog/api/welcome.py
@@ -285,7 +285,9 @@ def _get_accessible_team_ids(organization: Organization, access_control: UserAcc
organization_id=str(organization.id),
)
return []
- return list(queryset.order_by("id").values_list("id", flat=True)[:_MAX_TEAMS_SCANNED])
+ # Sort after the bounded fetch: an `ORDER BY id ... LIMIT n` lets the planner walk the primary
+ # key and filter organization_id on the way, instead of seeking the organization index.
+ return sorted(queryset.values_list("id", flat=True)[:_MAX_TEAMS_SCANNED])
def _filter_visible(members: list[dict[str, Any]], organization: Organization, user: User) -> list[dict[str, Any]]:
@@ -520,7 +522,7 @@ def _get_products_in_use(organization: Organization) -> list[str]:
round-trip (vs. the prior N EXISTS queries), plus one extra pass over has_completed_onboarding_for
to surface JSONB-encoded onboarding keys.
"""
- team_ids = list(organization.teams.all().order_by("id").values_list("id", flat=True)[:_MAX_TEAMS_SCANNED])
+ team_ids = list(organization.teams.all().values_list("id", flat=True)[:_MAX_TEAMS_SCANNED])
if not team_ids:
return []
from posthog.models import Team