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): move the lifecycle fence off the hot organization row (#88903)

Daniel9e82b58d2865
diff --git a/products/managed_warehouse/backend/logic/connection.py b/products/managed_warehouse/backend/logic/connection.py
index 780d2416e92a..aaca1a75dd0c 100644
--- a/products/managed_warehouse/backend/logic/connection.py
+++ b/products/managed_warehouse/backend/logic/connection.py
@@ -108,9 +108,11 @@ class _LifecycleSnapshot:
def _lifecycle_snapshot(organization_id: str | UUID, *, lock: bool = False) -> _LifecycleSnapshot:
+ # The organization existence check is deliberately a plain read: FOR UPDATE on a
+ # posthog_organization row conflicts with the FK KEY SHARE every org-scoped INSERT
+ # takes, so locking it from these sweeps dams unrelated writers org-wide. The
+ # lifecycle row below is the serialization fence.
organization_queryset = Organization.objects.only("id")
- if lock:
- organization_queryset = organization_queryset.select_for_update()
if not organization_queryset.filter(id=organization_id).exists():
return _LifecycleSnapshot(organization_exists=False, desired_active=False, generation=None)
lifecycle_queryset = ManagedWarehouseSourceLifecycle.objects.filter(organization_id=organization_id)
@@ -273,12 +275,17 @@ def _ensure_managed_source_locked(
def _locked_source_lifecycle(organization_id: str | UUID) -> ManagedWarehouseSourceLifecycle:
- # The organization row serializes first-time lifecycle-row creation as well as all
- # later generation reads/writes. This avoids relying on DuckgresServer for lifecycle
- # coordination and gives every team in an organization one common fence.
- organization = Organization.objects.select_for_update().only("id").get(id=organization_id)
- lifecycle, _created = ManagedWarehouseSourceLifecycle.objects.get_or_create(organization=organization)
- return lifecycle
+ # The lifecycle row itself is the common fence for every team in the organization:
+ # a quiet per-org singleton (OneToOne), so locking it serializes generation
+ # reads/writes without touching the hot posthog_organization row (whose FOR UPDATE
+ # would conflict with the FK KEY SHARE every org-scoped INSERT takes). First-time
+ # creation is race-safe via the OneToOne unique constraint; a freshly inserted row
+ # is already exclusively owned by this transaction.
+ organization = Organization.objects.only("id").get(id=organization_id)
+ lifecycle, created = ManagedWarehouseSourceLifecycle.objects.get_or_create(organization=organization)
+ if created:
+ return lifecycle
+ return ManagedWarehouseSourceLifecycle.objects.select_for_update().get(pk=lifecycle.pk)
def get_active_managed_warehouse_source_generation(*, organization_id: str | UUID) -> int | None:
@@ -313,10 +320,10 @@ def deactivate_managed_warehouse_source_lifecycle(
) -> int | None:
"""Mark the lifecycle inactive once and return the cleanup operation generation."""
with transaction.atomic():
- organization = Organization.objects.select_for_update().only("id").filter(id=organization_id).first()
- if organization is None:
+ try:
+ lifecycle = _locked_source_lifecycle(organization_id)
+ except Organization.DoesNotExist:
return None
- lifecycle, _created = ManagedWarehouseSourceLifecycle.objects.get_or_create(organization=organization)
if lifecycle.generation != expected_generation:
return None
if lifecycle.desired_active:
@@ -507,10 +514,12 @@ def soft_delete_managed_warehouse_sources(*, organization_id: str | UUID, expect
now = timezone.now()
cleanup_error: Exception | None = None
with transaction.atomic():
- organization = Organization.objects.select_for_update().only("id").filter(id=organization_id).first()
- if organization is None:
+ # Fence on the lifecycle row, not the hot organization row (see _locked_source_lifecycle).
+ if not Organization.objects.only("id").filter(id=organization_id).exists():
return
- lifecycle = ManagedWarehouseSourceLifecycle.objects.filter(organization=organization).first()
+ lifecycle = (
+ ManagedWarehouseSourceLifecycle.objects.select_for_update().filter(organization_id=organization_id).first()
+ )
if lifecycle is None or lifecycle.desired_active or lifecycle.generation != expected_generation:
return
@@ -533,10 +542,13 @@ def soft_delete_managed_warehouse_sources(*, organization_id: str | UUID, expect
def soft_delete_legacy_managed_warehouse_sources(*, organization_id: str | UUID) -> None:
now = timezone.now()
with transaction.atomic():
- organization = Organization.objects.select_for_update().only("id").filter(id=organization_id).first()
- if organization is None:
+ # Fence on the lifecycle row when present; the legacy fallback below serializes on
+ # the source rows it tombstones. Never the hot organization row.
+ if not Organization.objects.only("id").filter(id=organization_id).exists():
return
- lifecycle = ManagedWarehouseSourceLifecycle.objects.filter(organization=organization).first()
+ lifecycle = (
+ ManagedWarehouseSourceLifecycle.objects.select_for_update().filter(organization_id=organization_id).first()
+ )
if lifecycle is not None:
_soft_delete_sources_for_inactive_generation_locked(
organization_id=organization_id,