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(managed-warehouse): stop taking FOR UPDATE on posthog_team in the half-hourly reconcile (#88480)

Danielec84c8aa4ef5
diff --git a/products/managed_warehouse/backend/logic/connection.py b/products/managed_warehouse/backend/logic/connection.py
index 94d7062fb169..780d2416e92a 100644
--- a/products/managed_warehouse/backend/logic/connection.py
+++ b/products/managed_warehouse/backend/logic/connection.py
@@ -339,7 +339,11 @@ def ensure_managed_warehouse_direct_source(
lifecycle = _locked_source_lifecycle(organization_id)
if not lifecycle.desired_active or lifecycle.generation != expected_generation:
return None
- Team.objects.select_for_update().only("id").get(id=team_id, organization_id=organization_id)
+ # Deliberately NOT select_for_update: FOR UPDATE on a posthog_team row blocks the
+ # FK KEY SHARE lock every team-scoped INSERT takes, so a long hold here stalls all
+ # writers for the team. The lifecycle row lock above is the serialization anchor;
+ # a plain existence check is all the team row provides here.
+ Team.objects.only("id").get(id=team_id, organization_id=organization_id)
return _ensure_managed_source_locked(
team_id=team_id,
convert_active_legacy=lifecycle.legacy_conversion_generation == expected_generation,
@@ -352,7 +356,9 @@ def _reconcile_managed_warehouse_source(*, team_id: int, organization_id: str |
if not lifecycle_snapshot.desired_active:
return
with transaction.atomic():
- team = Team.objects.select_for_update().only("id").filter(id=team_id, organization_id=organization_id).first()
+ # Plain read on purpose — see ensure_managed_warehouse_direct_source for why the
+ # team row must never be locked from these periodic sweeps.
+ team = Team.objects.only("id").filter(id=team_id, organization_id=organization_id).first()
if team is None:
return
@@ -400,7 +406,10 @@ def _reconcile_managed_warehouse_source(*, team_id: int, organization_id: str |
with transaction.atomic():
if _lifecycle_snapshot(organization_id, lock=True) != lifecycle_snapshot:
return
- team = Team.objects.select_for_update().only("id").filter(id=team_id, organization_id=organization_id).first()
+ # Plain read on purpose — this atomic block runs get_or_create loops and
+ # reconcile_postgres_schemas and can hold its locks for minutes; taking the team
+ # row here would stall every writer for the team the whole time.
+ team = Team.objects.only("id").filter(id=team_id, organization_id=organization_id).first()
if team is None:
return
source = (