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(data-imports): retry transient DB connection drop in repartition flag check (#75470)

Tom Owers623b4d5ac382
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py
index ea2f3bfec95c..f36867bd8626 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py
@@ -20,6 +20,7 @@ from dateutil import parser
from structlog.types import FilteringBoundLogger
from posthog.exceptions_capture import capture_exception
+from posthog.temporal.common.utils import retry_on_db_connection_drop
from posthog.utils import get_machine_id
from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob
@@ -76,7 +77,7 @@ def is_auto_repartition_enabled(schema: ExternalDataSchema) -> bool:
from posthog.models import Team
try:
- team = Team.objects.only("uuid", "organization_id").get(id=schema.team_id)
+ team = retry_on_db_connection_drop(lambda: Team.objects.only("uuid", "organization_id").get(id=schema.team_id))
except Team.DoesNotExist:
return False
try:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
index 2e9aad0eb6dd..3efceab0d936 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
@@ -4,7 +4,7 @@ import datetime
import tempfile
import pytest
-from unittest.mock import AsyncMock, patch
+from unittest.mock import AsyncMock, MagicMock, patch
from django.db import OperationalError
@@ -229,6 +229,25 @@ class TestRepartitionDetection:
assert schema.repartition_pending["partition_keys"] == ["id"]
+class TestIsAutoRepartitionEnabled:
+ def test_retries_once_on_transient_db_connection_drop(self, team):
+ # The Team lookup runs on a long-lived Temporal worker thread; a pooler-dropped connection
+ # raises OperationalError on first use. Without a retry this propagates out of
+ # is_auto_repartition_enabled uncaught (it's outside the function's Team.DoesNotExist/
+ # feature_enabled try blocks) instead of resolving the flag.
+ schema = _make_schema(team, {})
+ mock_queryset = MagicMock()
+ mock_queryset.get.side_effect = [OperationalError("server closed the connection unexpectedly"), team]
+
+ with (
+ patch("posthog.models.Team.objects.only", return_value=mock_queryset),
+ patch.object(ctrl.posthoganalytics, "feature_enabled", return_value=True),
+ ):
+ assert ctrl.is_auto_repartition_enabled(schema) is True
+
+ assert mock_queryset.get.call_count == 2
+
+
class TestRepartitionOOMHistoryTrigger:
def _detect(self, team, schema: ExternalDataSchema, delta: deltalake.DeltaTable) -> None:
async_to_sync(ctrl.maybe_flag_for_repartition)(schema, schema.source, _make_job(team, schema), delta, logger)