: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(subscriptions): look up person property names by project key (#115872)
posthog[bot]d8936ef7250c
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py b/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.pyindex 0d0e9970caed..d3586a92d0b5 100644--- a/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py@@ -4,7 +4,7 @@ from dataclasses import dataclassfrom datetime import UTC, datetime, timedelta, tzinfofrom typing import Optional, Union-from django.db.models import BigIntegerField, F, Q+from django.db.models import BigIntegerField, F, IntegerField, Qfrom django.db.models.functions import Coalesceimport structlog@@ -339,8 +339,15 @@ def _no_data_event_names(team: Team, limit: int) -> list[str]:def _person_property_names(team: Team, limit: int) -> list[str]:names = (- PropertyDefinition.objects.filter(team_id=team.pk, type=PropertyDefinition.Type.PERSON)- .order_by("name")+ PropertyDefinition.objects.alias(+ effective_project_id=Coalesce("project_id", "team_id", output_field=BigIntegerField()),+ group_type_index_key=Coalesce("group_type_index", -1, output_field=IntegerField()),+ )+ .filter(effective_project_id=team.project_id, type=PropertyDefinition.Type.PERSON, group_type_index_key=-1)+ # The filter and the order are the key of index_property_def_query_proj, so Postgres reads `limit` entries+ # and stops. Do not order by name alone: that plan uses posthog_propdef_proj_uniq, where `type` comes after+ # `name`, so in a project with many event properties and few person properties it walks the whole project.+ .order_by(F("query_usage_30_day").desc(nulls_last=True), "name").values_list("name", flat=True)[:limit])sanitized = (sanitize_user_text(name, EVENT_NAME_MAX_LENGTH) for name in names)diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_spec_generator.py b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_spec_generator.pyindex 61023b2d43af..1021fa61951c 100644--- a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_spec_generator.py+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_spec_generator.py@@ -152,6 +152,26 @@ class TestPersonPropertyNames(APIBaseTest):assert "country" in namesassert "$browser" not in names+ @parameterized.expand(+ [+ ("other_project", False, ["mine"]),+ ("sibling_environment", True, ["from_other", "mine"]),+ ]+ )+ def test_reads_properties_across_the_project_only(+ self, _name: str, same_project: bool, expected: list[str]+ ) -> None:+ if same_project:+ other_team = Team.objects.create(organization=self.organization, project=self.team.project, name="other")+ else:+ other_team = Team.objects.create(organization=self.organization, name="other")+ PropertyDefinition.objects.create(+ team=other_team, project=other_team.project, name="from_other", type=PropertyDefinition.Type.PERSON+ )+ PropertyDefinition.objects.create(team=self.team, name="mine", type=PropertyDefinition.Type.PERSON)++ assert _person_property_names(self.team, limit=30) == expected+class TestGroupTypeLabels(APIBaseTest):@patch(