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): reset rows_synced on retried v3 incremental extractions (#84250)

estefaniaa8ee90ee1894
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
index bf55b91bd274..75badbc83e80 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
@@ -255,12 +255,20 @@ async def reset_rows_synced_if_needed(
is_incremental: bool,
reset_pipeline: bool,
should_resume: bool,
+ *,
+ incremental_cursor_staged: bool = False,
) -> None:
- # Reset the rows_synced count - this may not be 0 if the job restarted due to a heartbeat timeout
+ # Reset the rows_synced count - this may not be 0 if the job restarted due to a heartbeat timeout.
+ #
+ # Incremental syncs are exempt only when the durable cursor advances per batch (pipeline v2), so
+ # a retried attempt resumes past the rows already counted. When the cursor is staged and only
+ # promoted on completion (pipeline v3), a retried attempt re-extracts the whole window from
+ # batch 0, so keeping the previous attempt's count double-counts every re-read row —
+ # `rows_synced` feeds billed usage via `Sum("rows_synced")` in usage reports.
if (
job.rows_synced is not None
and job.rows_synced != 0
- and (not is_incremental or reset_pipeline is True)
+ and (not is_incremental or reset_pipeline is True or incremental_cursor_staged)
and not should_resume
):
job.rows_synced = 0
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
index fd5ae1c01420..cd73c5890e6e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
@@ -23,6 +23,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.common.e
handle_reset_or_full_refresh,
persist_primary_keys,
report_heartbeat_timeout,
+ reset_rows_synced_if_needed,
resolve_primary_keys,
trim_source_job_inputs,
validate_incremental_sync,
@@ -497,6 +498,63 @@ class TestHandleCorruptedDeltaLog:
assert ph.capture.call_args.kwargs["properties"]["made_non_billable"] is False
+# transaction=True: the helper saves the job via the async thread pool, which can't see an
+# atomic TestCase's uncommitted rows.
[email protected]_db(transaction=True)
+class TestResetRowsSyncedIfNeeded:
+ def _job_with_leftover_count(self, team) -> ExternalDataJob:
+ source = ExternalDataSource.objects.create(
+ source_id=str(uuid.uuid4()), connection_id=str(uuid.uuid4()), team=team, source_type="Postgres"
+ )
+ schema = ExternalDataSchema.objects.create(name="orders", team=team, source=source, sync_type_config={})
+ return ExternalDataJob.objects.create(
+ team=team,
+ pipeline=source,
+ schema=schema,
+ status=ExternalDataJob.Status.RUNNING,
+ rows_synced=1234,
+ billable=True,
+ )
+
+ @pytest.mark.parametrize(
+ "_name,is_incremental,reset_pipeline,should_resume,incremental_cursor_staged,expect_reset",
+ [
+ # Staged-cursor (v3) incremental retry re-extracts the whole window from batch 0, so a
+ # leftover count from the previous attempt would double-count every re-read row — and
+ # rows_synced feeds billed usage. This is the regression case.
+ ("staged_cursor_incremental_retry_resets", True, False, False, True, True),
+ # A resumable source picks up the previous attempt's staged batches, so its rows stay counted.
+ ("resumable_source_keeps_count", True, False, True, True, False),
+ # Durable-cursor (v2) incremental retry resumes past the rows already counted.
+ ("durable_cursor_incremental_retry_keeps_count", True, False, False, False, False),
+ ("full_refresh_restart_resets", False, False, False, False, True),
+ ("reset_pipeline_resets", True, True, False, False, True),
+ ],
+ )
+ def test_reset_conditions(
+ self,
+ _name: str,
+ is_incremental: bool,
+ reset_pipeline: bool,
+ should_resume: bool,
+ incremental_cursor_staged: bool,
+ expect_reset: bool,
+ team,
+ ) -> None:
+ job = self._job_with_leftover_count(team)
+
+ async_to_sync(reset_rows_synced_if_needed)(
+ job,
+ is_incremental,
+ reset_pipeline,
+ should_resume,
+ incremental_cursor_staged=incremental_cursor_staged,
+ )
+
+ job.refresh_from_db()
+ assert job.rows_synced == (0 if expect_reset else 1234)
+
+
# transaction=True: the webhook-first branch clears the reset flag via update_sync_type_config_keys,
# which writes from the async thread pool and can't see an atomic TestCase's uncommitted rows.
@pytest.mark.django_db(transaction=True)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/pipeline.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/pipeline.py
index 1d5d714c4473..79bc88eec761 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/pipeline.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/pipeline.py
@@ -272,7 +272,15 @@ class PipelineV3(Generic[ResumableData]):
try:
await self._sinks.clear()
- await reset_rows_synced_if_needed(self._job, self._is_incremental, self._reset_pipeline, should_resume)
+ # v3 stages the incremental cursor until job completion, so a retried attempt
+ # re-extracts from batch 0 and the previous attempt's count must not be kept.
+ await reset_rows_synced_if_needed(
+ self._job,
+ self._is_incremental,
+ self._reset_pipeline,
+ should_resume,
+ incremental_cursor_staged=True,
+ )
validate_incremental_sync(
self._is_incremental,