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(marketing-analytics): read the org id off the team for the dedup flag (#98138)

posthog[bot]9e222c36e1bc
diff --git a/posthog/hogql/database/schema/marketing_costs_precomputed.py b/posthog/hogql/database/schema/marketing_costs_precomputed.py
index ed20d9b0340c..09a6b7e97fe3 100644
--- a/posthog/hogql/database/schema/marketing_costs_precomputed.py
+++ b/posthog/hogql/database/schema/marketing_costs_precomputed.py
@@ -47,13 +47,15 @@ def costs_dedup_v2_enabled(team: "Team | None") -> bool:
cached = getattr(team, _FLAG_CACHE_ATTR, None)
if cached is not None:
return cached
- organization = team.organization
+ # organization_id, not team.organization.id: the FK dereference costs a Postgres round trip per
+ # cache-key build, and it raises Organization.DoesNotExist when the organization row is gone.
+ organization_id = str(team.organization_id)
enabled = bool(
posthoganalytics.feature_enabled(
COSTS_DEDUP_V2_FLAG,
str(team.uuid),
- groups={"organization": str(organization.id)},
- group_properties={"organization": {"id": str(organization.id)}},
+ groups={"organization": organization_id},
+ group_properties={"organization": {"id": organization_id}},
only_evaluate_locally=True,
send_feature_flag_events=False,
)
diff --git a/posthog/hogql/database/schema/test/test_marketing_costs_precomputed.py b/posthog/hogql/database/schema/test/test_marketing_costs_precomputed.py
new file mode 100644
index 000000000000..9da1dbe10836
--- /dev/null
+++ b/posthog/hogql/database/schema/test/test_marketing_costs_precomputed.py
@@ -0,0 +1,15 @@
+from posthog.test.base import BaseTest
+
+from posthog.hogql.database.schema.marketing_costs_precomputed import costs_dedup_v2_enabled
+
+from posthog.models.team import Team
+
+
+class TestCostsDedupV2Flag(BaseTest):
+ def test_reads_the_organization_id_off_the_team_without_a_query(self):
+ # The flag sits on the cache-key path of every query. A `team.organization` dereference here
+ # costs one Postgres query per cache-key build, and raises when the organization row is gone.
+ team = Team.objects.get(pk=self.team.pk)
+
+ with self.assertNumQueries(0):
+ assert costs_dedup_v2_enabled(team) is False