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): checkpoint what a resumed repartition temp holds (#102147)

Tom Owers47a8b615d31a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py
index 368d366dd6dc..7a40a955b124 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py
@@ -1395,7 +1395,13 @@ async def repartition_table_in_place(
claim_token=claim_token,
checkpoint={
"temp_uri": temp_uri,
- "rows_written": rows_so_far,
+ # The field records what temp holds, but `rows_so_far` counts only the rows this
+ # call appended, so a resumed attempt has to add back the prefix it inherited.
+ # Recording the appended count alone makes the checkpoint go backwards mid-resume,
+ # and `_retrying_a_killed_attempt` then reads an advancing rewrite as a stalled one
+ # and stands its retry down until the next sync, whose merge invalidates the
+ # checkpoint and restarts the rewrite from row 0.
+ "rows_written": skip_rows + rows_so_far,
"target": resolved_target.to_dict(),
"live_version": checkpoint_version,
"held_at": datetime.now(UTC).isoformat(),
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py
index 22a2ea3fff48..81d44d05e781 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py
@@ -1881,6 +1881,54 @@ class TestRewriteCheckpointResume:
schema.clear_repartition_rewrite.assert_called_once() # obsolete once temp is complete
assert result["outcome"] == "completed"
+ def test_a_resumed_rewrite_checkpoints_what_temp_holds_not_what_it_appended(self, tmp_path):
+ # The checkpoint records the rows temp holds, and the rewrite reports only the rows it appended
+ # itself, so a resume has to add back the prefix it inherited. Recording the appended count
+ # alone makes the checkpoint go backwards, which reads as a rewrite that stopped advancing:
+ # the retry of a killed attempt stands down, the next sync's merge invalidates the checkpoint,
+ # and the rewrite restarts from row 0 until the attempt cap abandons the table.
+ live = _write_month_partitioned(
+ str(tmp_path / "live"),
+ [
+ (1, datetime.datetime(2024, 1, 5)),
+ (2, datetime.datetime(2024, 2, 2)),
+ (3, datetime.datetime(2024, 3, 3)),
+ ],
+ )
+ table_ref = _make_table_ref(get_delta_table=AsyncMock(return_value=live))
+ target = RepartitionTarget(
+ partition_keys=["created_at"], trigger_reason="t", partition_mode="datetime", partition_format="day"
+ )
+ schema = self._base_schema(
+ repartition_rewrite={
+ "temp_uri": "s3://bucket/live__repartitioned_old",
+ "rows_written": 1,
+ "target": target.to_dict(),
+ "live_version": live.version(),
+ },
+ )
+
+ async def rewrite_appending_two(**kwargs):
+ await kwargs["save_checkpoint"](2, target)
+ return 2, target
+
+ with (
+ patch.object(repartition_module, "_purge_stale_temp_tables", new=AsyncMock()),
+ patch.object(repartition_module, "_current_claim_token", return_value="tok"),
+ _patch_finalize(),
+ patch.object(repartition_module, "_valid_delta_row_count", new=AsyncMock(side_effect=[1, 3])),
+ patch.object(repartition_module, "save_repartition_checkpoint_if_claimed", return_value=True) as saved,
+ patch.object(repartition_module, "_rewrite_into_temp", new=AsyncMock(side_effect=rewrite_appending_two)),
+ patch.object(repartition_module, "_swap_temp_into_live", new=AsyncMock()),
+ ):
+ asyncio.run(
+ repartition_table_in_place(
+ table_ref=table_ref, schema=schema, target=target, logger=logger, claim_token="tok"
+ )
+ )
+
+ assert saved.call_args.kwargs["checkpoint"]["rows_written"] == 3
+
def test_discards_the_checkpoint_when_the_live_version_moved_on(self, tmp_path):
# Version moved on (a merge committed between attempts) → the recorded prefix no longer lines up
# with the current scan. The checkpoint must be discarded and a fresh rebuild started into our