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-warehouse): don't repartition finer on OOM history when partitions are tiny (#75049)

Daniel RC1fa2bbfe4e26
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 68bbd195ceb0..ea2f3bfec95c 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
@@ -46,6 +46,13 @@ REPARTITION_COOLDOWN_SECONDS = 24 * 60 * 60
# doesn't re-attempt the rewrite on every sync forever.
MAX_REPARTITION_ATTEMPTS = 3
+# Worst-case assumed ratio between a partition's in-memory working set and its compressed at-rest
+# size — the amplification that justifies the oom_history trigger at all. When even this generous
+# multiple of the largest partition fits the budget, partition size can't be what's killing the
+# worker (heartbeat timeouts also come from slow merges, deploys, and pod churn), so repartitioning
+# finer can't help.
+OOM_WORKING_SET_AMPLIFICATION = 100
+
def target_partition_bytes() -> int:
return int(getattr(settings, "DATA_WAREHOUSE_TARGET_PARTITION_BYTES", 500_000_000))
@@ -199,6 +206,22 @@ async def maybe_flag_for_repartition(
)
return
+ # A tiny largest partition can't be the OOM cause: without this guard, oom_history drives the
+ # scheme finer tier by tier until it bottoms out (e.g. datetime at hour) and then emits a
+ # skipped event + exception on every cooldown expiry forever — while the extra partitions
+ # multiply per-partition merge commits, slowing the sync that produced the timeouts.
+ if not over_budget and max_bytes * OOM_WORKING_SET_AMPLIFICATION < budget:
+ await logger.adebug(
+ f"repartition: OOM history present but largest partition is far under budget, "
+ f"partitioning is not the cause — leaving layout alone schema_id={schema.id} "
+ f"max_partition_bytes={max_bytes} budget_bytes={budget} recent_oom_count={oom_count}",
+ schema_id=str(schema.id),
+ max_partition_bytes=max_bytes,
+ budget_bytes=budget,
+ recent_oom_count=oom_count,
+ )
+ return
+
trigger_reason = "proactive_threshold" if over_budget else "oom_history"
if enabled is None:
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 5585023f1773..2e9aad0eb6dd 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
@@ -248,7 +248,9 @@ class TestRepartitionOOMHistoryTrigger:
with tempfile.TemporaryDirectory() as d:
delta = _write_partitioned_delta(f"{d}/t", ["0", "1"])
with (
- patch.object(ctrl, "target_partition_bytes", return_value=10**12), # well within the size budget
+ # Within the size budget, but close enough that the amplified working set exceeds it —
+ # a bigger budget would (correctly) hit the tiny-partition guard instead.
+ patch.object(ctrl, "target_partition_bytes", return_value=10_000),
patch.object(ctrl, "repartition_oom_threshold", return_value=3),
patch.object(ctrl, "is_auto_repartition_enabled", return_value=True),
patch.object(ctrl, "capture_repartition_event"),
@@ -284,7 +286,8 @@ class TestRepartitionOOMHistoryTrigger:
with tempfile.TemporaryDirectory() as d:
delta = _write_partitioned_delta(f"{d}/t", ["0", "1"])
with (
- patch.object(ctrl, "target_partition_bytes", return_value=10**12),
+ # Small enough that the tiny-partition guard doesn't mask the revive guard under test.
+ patch.object(ctrl, "target_partition_bytes", return_value=10_000),
patch.object(ctrl, "repartition_oom_threshold", return_value=3),
patch.object(ctrl, "is_auto_repartition_enabled", return_value=True),
patch.object(ctrl, "capture_repartition_event"),
@@ -294,6 +297,35 @@ class TestRepartitionOOMHistoryTrigger:
schema.refresh_from_db()
assert schema.repartition_pending is None
+ def test_tiny_partitions_do_not_flag_on_oom_history(self, team):
+ # deals/contacts loop from prod: heartbeat timeouts recorded as OOMs on a table whose largest
+ # partition is KBs against a 500 MB budget. Without the amplification guard the trigger steps
+ # the scheme finer until it bottoms out at hour, then emits skipped + capture_exception daily
+ # forever. The guard must be a quiet no-op: nothing pending, no events at all.
+ schema = _make_schema(
+ team,
+ {"partitioning_enabled": True, "partition_mode": "md5", "partition_count": 2, "partitioning_keys": ["id"]},
+ )
+ for _ in range(3):
+ ExternalDataSchemaOOMEvent.objects.for_team(schema.team_id).create(team_id=schema.team_id, schema=schema)
+
+ with tempfile.TemporaryDirectory() as d:
+ delta = _write_partitioned_delta(f"{d}/t", ["0", "1"])
+ with (
+ patch.object(
+ ctrl, "target_partition_bytes", return_value=10**12
+ ), # partitions orders of magnitude under
+ patch.object(ctrl, "repartition_oom_threshold", return_value=3),
+ patch.object(ctrl, "is_auto_repartition_enabled", return_value=True),
+ patch.object(ctrl, "capture_repartition_event") as capture,
+ ):
+ self._detect(team, schema, delta)
+
+ schema.refresh_from_db()
+ assert schema.repartition_pending is None
+ assert schema.max_partition_bytes is not None # observability measurement still recorded
+ assert capture.call_args_list == []
+
# An Exception-derived cancellation, named exactly `CancelledError`: models how `async_to_sync` can
# surface a worker-shutdown cancel so it slips past a plain BaseException catch. `_is_cancellation`