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(warehouse): drop the xmin cursor when a reset wipes the table (#95369)

Daniel RCc7fcc871cb46
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
index beb5e07d203d..f83e79ce214a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
@@ -1532,6 +1532,7 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
# CDC snapshot schemas fall through to run initial full_refresh via postgres_source()
require_ssl = source_requires_ssl(schema.source, config)
+ table_rebuild_pending = inputs.reset_pipeline or schema.delta_revive_required is not None
# Prefer the per-row `schema_metadata.source_schema` so multi-schema warehouse sources work
# without needing to encode the schema in `config.schema`. Falls back to `config.schema` for
@@ -1558,9 +1559,14 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
row_filters=inputs.row_filters,
# xmin state is read straight off the schema here (the generic `SourceInputs` stays
# Postgres-agnostic). xmin rides the normal full per-schema path — no CDC dispatch.
+ # A reset, and a pending corrupt-delta revive, both delete the Delta table before
+ # this read, so the cursor has to go with it: kept, the read covers only the window
+ # since the last run, and the overwrite collapses the table to that slice. The
+ # activity drops the incremental cursor for both cases for the same reason; the xmin
+ # cursor is dropped here because it is read here.
is_xmin=schema.is_xmin,
- xmin_last_value=schema.xmin_last_value,
- xmin_num_wraparound=schema.xmin_num_wraparound,
+ xmin_last_value=None if table_rebuild_pending else schema.xmin_last_value,
+ xmin_num_wraparound=None if table_rebuild_pending else schema.xmin_num_wraparound,
byte_bounded_extraction=inputs.byte_bounded_extraction,
activity_attempt=inputs.activity_attempt,
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/test_import_data.py b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/test_import_data.py
index 9fe451bbcb07..24b1ac9c0f58 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/test_import_data.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/test_import_data.py
@@ -26,7 +26,12 @@ from products.warehouse_sources.backend.temporal.data_imports.workflow_activitie
@sync_to_async
-def _setup(team: Team, job_inputs: dict[Any, Any]) -> ImportDataActivityInputs:
+def _setup(
+ team: Team,
+ job_inputs: dict[Any, Any],
+ sync_type: str | None = None,
+ sync_type_config: dict[str, Any] | None = None,
+) -> ImportDataActivityInputs:
source = ExternalDataSource.objects.create(
team=team,
source_id="source_id",
@@ -54,6 +59,8 @@ def _setup(team: Team, job_inputs: dict[Any, Any]) -> ImportDataActivityInputs:
should_sync=True,
status=ExternalDataSchema.Status.COMPLETED,
last_synced_at="2024-01-01",
+ sync_type=sync_type,
+ sync_type_config=sync_type_config or {},
)
job = ExternalDataJob.objects.create(
team=team,
@@ -68,6 +75,52 @@ def _setup(team: Team, job_inputs: dict[Any, Any]) -> ImportDataActivityInputs:
return ImportDataActivityInputs(team_id=team.pk, schema_id=schema.pk, source_id=source.pk, run_id=str(job.pk))
[email protected]_db(transaction=True)
+ "rebuild_trigger",
+ [
+ {"reset_pipeline": True},
+ {
+ "delta_revive_required": {
+ "reason": "hollow",
+ "missing_path": "p",
+ "detected_at": "2026-01-01T00:00:00+00:00",
+ }
+ },
+ ],
+ ids=["reset", "corrupt_delta_revive"],
+)
+async def test_table_rebuild_drops_the_xmin_cursor(activity_environment, team, rebuild_trigger, **kwargs):
+ # Both triggers delete the Delta table before the read. Keeping the cursor makes that read the
+ # window since the last run, and the overwrite collapses the table to that slice.
+ activity_inputs = await _setup(
+ team,
+ {"host": "host.com", "port": 5432, "user": "u", "password": "p", "database": "db", "schema": "public"},
+ sync_type=ExternalDataSchema.SyncType.XMIN,
+ sync_type_config={**rebuild_trigger, "xmin_last_value": 100, "xmin_ceiling": 100, "xmin_num_wraparound": 0},
+ )
+
+ with (
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.postgres.source.postgres_source"
+ ) as mock_postgres_source,
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.workflow_activities.import_data_sync._run"
+ ),
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins._is_host_safe",
+ return_value=(True, None),
+ ),
+ ):
+ await activity_environment.run(import_data_activity_sync, activity_inputs)
+
+ called_with = mock_postgres_source.call_args.kwargs
+ assert called_with["is_xmin"] is True
+ assert called_with["xmin_last_value"] is None
+ assert called_with["xmin_num_wraparound"] is None
+
+
@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_job_inputs_with_whitespace(activity_environment, team, **kwargs):