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 repartition schema/job fetch on transient DB drop (#75744)

Tom Owersb7363a2c02b9
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 ee81a2bb154c..80f6b648f01b 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
@@ -652,6 +652,54 @@ class TestRepartitionActivity:
# The activity minted a fencing claim before starting the rewrite.
assert schema.repartition_claim is not None
+ def test_schema_fetch_retries_once_on_transient_db_connection_drop(self, team):
+ # The schema fetch runs on a long-lived Temporal worker thread, so a pooler-dropped
+ # connection can raise OperationalError on first use. Unlike every DB read past this point,
+ # this one sits outside the activity's transient-error handling (only ExternalDataSchema.
+ # DoesNotExist is caught here), so without a retry it escaped uncaught as an activity
+ # failure instead of resolving on a fresh connection.
+ schema = _make_schema(team, {})
+ mock_queryset = MagicMock()
+ mock_queryset.get.side_effect = [OperationalError("server closed the connection unexpectedly"), schema]
+ with (
+ patch.object(ExternalDataSchema.objects, "select_related", return_value=mock_queryset),
+ patch.object(repartition_table, "is_auto_repartition_enabled", return_value=False),
+ patch.object(repartition_table, "capture_repartition_event"),
+ ):
+ ActivityEnvironment().run(maybe_repartition_table_activity, self._inputs(team, schema))
+ assert mock_queryset.get.call_count == 2
+
+ def test_job_fetch_retries_once_on_transient_db_connection_drop(self, team):
+ # Same failure mode as the schema fetch above, for the job fetch a few lines later: a
+ # pooler-dropped connection must resolve on retry rather than escape as an activity failure.
+ schema = _make_schema(team, {})
+ schema.set_repartition_pending(
+ {
+ "partition_mode": "md5",
+ "partition_count": 4,
+ "partition_keys": ["id"],
+ "trigger_reason": "test",
+ "attempts": 0,
+ }
+ )
+ job = _make_job(team, schema)
+ mock_get = MagicMock(side_effect=[OperationalError("server closed the connection unexpectedly"), job])
+ mocked = AsyncMock(return_value={"outcome": "completed"})
+ with (
+ patch.object(ExternalDataJob.objects, "get", mock_get),
+ patch.object(repartition_table, "HeartbeaterSync"),
+ patch.object(repartition_table, "repartition_table_in_place", new=mocked),
+ patch.object(repartition_table, "capture_repartition_event"),
+ ):
+ ActivityEnvironment().run(
+ maybe_repartition_table_activity,
+ RepartitionActivityInputs(
+ team_id=team.id, schema_id=str(schema.id), job_id=str(job.id), source_id=str(schema.source_id)
+ ),
+ )
+ assert mock_get.call_count == 2
+ mocked.assert_awaited_once()
+
def test_superseded_after_claim_check_blip_is_not_recorded_as_failure(self, team):
# _still_claimant is conservative and reports True on a transient DB read, so a zombie can reach
# the failure handler even after a newer attempt took the claim. _handle_failure's authoritative
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
index f0163aa3acba..4de381c6c83f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
@@ -27,6 +27,7 @@ from temporalio.exceptions import ApplicationError
from posthog.exceptions_capture import capture_exception
from posthog.temporal.common.heartbeat_sync import HeartbeaterSync
from posthog.temporal.common.logger import get_logger
+from posthog.temporal.common.utils import retry_on_db_connection_drop
from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob
from products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchema
@@ -189,7 +190,9 @@ def maybe_repartition_table_activity(inputs: RepartitionActivityInputs) -> None:
def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: FilteringBoundLogger) -> None:
try:
- schema = ExternalDataSchema.objects.select_related("source").get(id=inputs.schema_id)
+ schema = retry_on_db_connection_drop(
+ lambda: ExternalDataSchema.objects.select_related("source").get(id=inputs.schema_id)
+ )
except ExternalDataSchema.DoesNotExist:
logger.warning(
f"repartition: schema not found, skipping activity schema_id={inputs.schema_id}",
@@ -236,7 +239,7 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterin
return
try:
- job = ExternalDataJob.objects.get(id=inputs.job_id)
+ job = retry_on_db_connection_drop(lambda: ExternalDataJob.objects.get(id=inputs.job_id))
except ExternalDataJob.DoesNotExist:
logger.warning(
f"repartition: job not found, skipping activity job_id={inputs.job_id}",