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): skip column statistics when the team was deleted (#107179)

Tom Owersf36b610f0b57
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
index 47795e0b5889..2a2d05e4d7c9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
@@ -185,8 +185,15 @@ def compute_table_statistics_sync(team_id: int, schema_id: uuid.UUID) -> dict[st
log = logger.bind(team_id=team_id, schema_id=str(schema_id))
# A plain read, so it's safe to retry outright on the Team/Organization join losing a
- # Postgres deadlock race against an unrelated writer of either table.
- team = _get_team(team_id)
+ # Postgres deadlock race against an unrelated writer of either table. The team can also be
+ # legitimately gone by the time this fire-and-forget child workflow runs (deleted between the
+ # post-import gate check and now) — that's not a bug, so skip like the other not-found cases
+ # below rather than let DoesNotExist reach the activity's except block and error tracking.
+ try:
+ team = _get_team(team_id)
+ except Team.DoesNotExist:
+ log.info("warehouse_statistics.skipped", reason="team_deleted")
+ return {"status": "skipped", "reason": "team_deleted"}
event_props: dict[str, Any] = {"schema_id": str(schema_id)}
def emit_completed(status: str, **props: Any) -> None:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
index 678058c16fea..0bebcaff657f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
@@ -327,6 +327,17 @@ class TestComputeTableStatisticsSync:
== 7
)
+ def test_skipped_when_team_deleted(self) -> None:
+ # The gate that decides whether to start this child workflow runs in an earlier activity;
+ # the team can be deleted in the gap before this one runs. That must skip like the other
+ # not-found cases here, not raise Team.DoesNotExist into the activity's error-tracking path.
+ team = self._team()
+ schema, table, _ = self._schema_table_job(team)
+ deleted_team_id = team.id
+ team.delete()
+ result = compute_table_statistics_sync(deleted_team_id, schema.id)
+ assert result == {"status": "skipped", "reason": "team_deleted"}
+
def test_skipped_when_no_table(self) -> None:
team = self._team()
source = ExternalDataSource.objects.create(