: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
fix(insights): use denormalized team_id in dashboard tile sync signal (#66848)
posthog[bot]96d3efe243b2
diff --git a/products/product_analytics/backend/models/insight_caching_state.py b/products/product_analytics/backend/models/insight_caching_state.pyindex baba711f302c..5b800ba1f31d 100644--- a/products/product_analytics/backend/models/insight_caching_state.py+++ b/products/product_analytics/backend/models/insight_caching_state.py@@ -71,7 +71,10 @@ def sync_insight(sender, instance: Insight, **kwargs):@mutable_receiver(post_save, sender=DashboardTile)def sync_dashboard_tile(sender, instance: DashboardTile, **kwargs):- _queue_sync_insight_caching_state(instance.dashboard.team_id, dashboard_tile_id=instance.pk)+ # Use the denormalized team_id (always populated in DashboardTile.save() before this+ # signal fires) rather than instance.dashboard.team_id, which triggers a DB fetch of the+ # dashboard row on every tile save.+ _queue_sync_insight_caching_state(instance.team_id, dashboard_tile_id=instance.pk)@mutable_receiver(post_save, sender=Dashboard)diff --git a/products/product_analytics/backend/models/test/test_insight_caching_state.py b/products/product_analytics/backend/models/test/test_insight_caching_state.pyindex 19621e3c5ec7..3d7dd988dc78 100644--- a/products/product_analytics/backend/models/test/test_insight_caching_state.py+++ b/products/product_analytics/backend/models/test/test_insight_caching_state.py@@ -1,7 +1,10 @@from typing import Optional, castfrom posthog.test.base import BaseTest+from unittest.mock import patch+from django.db import connection+from django.test.utils import CaptureQueriesContextfrom django.utils.timezone import nowfrom posthog.models import SharingConfiguration@@ -76,6 +79,30 @@ class TestInsightCachingState(BaseTest):assert self.get_caching_state() is None+ def test_dashboard_tile_save_signal_does_not_query_dashboard_table(self):+ with mute_selected_signals():+ dashboard = Dashboard.objects.create(team=self.team)+ insight = Insight.objects.create(team=self.team, filters=filters)+ tile = DashboardTile.objects.create(dashboard=dashboard, insight=insight)++ # Re-fetch so the dashboard relation is not already cached on the instance; the+ # post_save signal must read the denormalized team_id rather than dereferencing+ # instance.dashboard, which would issue an extra SELECT on the dashboard table.+ tile = DashboardTile.objects.get(pk=tile.pk)+ tile.color = "red"++ # Stub the downstream sync task so the only work captured is save() + the signal+ # itself (the task legitimately reads dashboard.deleted, which is out of scope here).+ with patch(+ "products.product_analytics.backend.models.insight_caching_state._queue_sync_insight_caching_state"+ ) as queue_mock:+ with CaptureQueriesContext(connection) as ctx:+ tile.save(update_fields=["color"])++ queue_mock.assert_called_once_with(self.team.pk, dashboard_tile_id=tile.pk)+ # Quoted to distinguish the dashboard table from posthog_dashboardtile.+ assert not any('"posthog_dashboard"' in query["sql"] for query in ctx.captured_queries)+def test_sharing_configuration_insight(self):with mute_selected_signals():insight = Insight.objects.create(team=self.team, filters=filters)