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): gate queued rewrites by their staging flag (#78449)

Daniel RC99fe94773733
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 308cb82ac9e3..04d3a496e5f1 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
@@ -48,6 +48,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.rep
WAREHOUSE_AUTO_REPARTITION_FLAG,
base_event_props,
capture_repartition_event,
+ is_auto_coarsen_enabled,
is_auto_repartition_enabled,
maybe_flag_for_repartition,
target_partition_bytes,
@@ -275,16 +276,26 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterin
# The flag has to stop a queued rewrite too, not only detection. Once a table is flagged, the
# rewrite runs ahead of extraction on every sync, so a rewrite that can't finish delays the sync
# by the full activity budget indefinitely; the flag is the only lever support has to release
- # such a table, and it does nothing here if it only gates detection. Two exclusions: a staged
- # swap must always be driven to completion because temp is the source of truth in that window
- # and live may already be deleted, and an operator-staged rewrite ignores the rollout flag
- # because they staged it knowing that syncing on the old layout is the worse option.
- if not enabled and pending is not None and swap is None and pending.get("trigger_reason") != "admin":
- logger.info(
- f"repartition: queued rewrite skipped, controller disabled by feature flag schema_id={schema.id}",
- schema_id=str(schema.id),
- )
- return
+ # such a table, and it does nothing here if it only gates detection. Each auto-staged trigger
+ # family answers to the flag that staged it, and any other reason fails open: operator-staged
+ # work (admin, coarsening nominations) was queued knowing that syncing on the old layout is the
+ # worse option, so it must never dead-end on a rollout flag. A staged swap is always driven to
+ # completion because temp is the source of truth in that window and live may already be deleted.
+ if pending is not None and swap is None:
+ reason = pending.get("trigger_reason")
+ if reason in ("proactive_threshold", "oom_history"):
+ release = not enabled
+ elif reason == "coarsening":
+ release = not is_auto_coarsen_enabled(schema)
+ else:
+ release = False
+ if release:
+ logger.info(
+ f"repartition: queued rewrite skipped, controller disabled by feature flag schema_id={schema.id}",
+ schema_id=str(schema.id),
+ trigger_reason=reason,
+ )
+ return
# Fast no-op path: nothing queued and the gate says no on-disk measurement is needed (flag off, or
# CDC). Return here — before fetching the job and reading the delta log — so the common healthy
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
index b207b1d14a6b..593b93d0bfd3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
@@ -239,16 +239,20 @@ class TestBudgetExhaustion:
class TestFeatureFlagGate:
@parameterized.expand(
[
- ("flag_off_releases_a_queued_rewrite", False, None, "proactive_threshold", False),
- ("flag_off_still_finishes_a_staged_swap", False, {"state": "ready"}, "proactive_threshold", True),
- ("flag_off_does_not_block_an_operator", False, None, "admin", True),
- ("flag_on_rewrites_as_usual", True, None, "proactive_threshold", True),
+ ("flag_off_releases_a_queued_rewrite", False, True, None, "proactive_threshold", False),
+ ("flag_off_still_finishes_a_staged_swap", False, True, {"state": "ready"}, "proactive_threshold", True),
+ ("flag_off_does_not_block_an_operator", False, True, None, "admin", True),
+ ("flag_on_rewrites_as_usual", True, True, None, "proactive_threshold", True),
+ ("flag_off_does_not_release_a_nomination", False, False, None, "coarsening_requested", True),
+ ("coarsen_flag_off_releases_a_queued_coarsen", True, False, None, "coarsening", False),
+ ("repartition_flag_off_keeps_a_queued_coarsen", False, True, None, "coarsening", True),
]
)
@patch(f"{MODULE}.capture_repartition_event")
@patch(f"{MODULE}.HeartbeaterSync")
@patch(f"{MODULE}.repartition_table_in_place", new_callable=AsyncMock)
@patch(f"{MODULE}.DeltaTableRef")
+ @patch(f"{MODULE}.is_auto_coarsen_enabled")
@patch(f"{MODULE}.is_auto_repartition_enabled")
@patch(f"{MODULE}.ExternalDataJob")
@patch(f"{MODULE}.ExternalDataSchema")
@@ -256,18 +260,21 @@ class TestFeatureFlagGate:
self,
_name: str,
enabled: bool,
+ coarsen_enabled: bool,
swap: dict | None,
trigger_reason: str,
expect_rewrite: bool,
mock_schema_model: MagicMock,
_mock_job_model: MagicMock,
mock_enabled: MagicMock,
+ mock_coarsen_enabled: MagicMock,
_mock_helper_cls: MagicMock,
mock_repartition: AsyncMock,
_mock_heartbeater: MagicMock,
_mock_capture_event: MagicMock,
) -> None:
mock_enabled.return_value = enabled
+ mock_coarsen_enabled.return_value = coarsen_enabled
schema = _schema(
name="public.usages",
s3_folder_name="usages",