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 warehouse table on CDC re-snapshot (#65585)

Daniel RC62394d8c333b
diff --git a/posthog/temporal/data_imports/cdc/activities.py b/posthog/temporal/data_imports/cdc/activities.py
index f674deae085e..7fb008e31ef0 100644
--- a/posthog/temporal/data_imports/cdc/activities.py
+++ b/posthog/temporal/data_imports/cdc/activities.py
@@ -816,6 +816,12 @@ class CDCExtractActivity:
"""Put a schema back into snapshot mode so its own schedule re-syncs it from scratch."""
schema.sync_type_config["cdc_mode"] = "snapshot"
schema.sync_type_config.pop("cdc_last_log_position", None)
+ # A source TRUNCATE (or a lost slot) leaves the warehouse table in place, so without
+ # reset_pipeline the re-snapshot takes the incremental-merge path and upserts the
+ # current rows over the stale ones — pre-truncate rows never deleted. Forcing it makes
+ # the batch import wipe the table first (handle_reset_or_full_refresh, which also clears
+ # the flag). The {name}_cdc companion is reset independently by the snapshot seed.
+ schema.sync_type_config["reset_pipeline"] = True
if clear_deferred_runs:
schema.sync_type_config.pop("cdc_deferred_runs", None)
schema.initial_sync_complete = False
diff --git a/posthog/temporal/data_imports/cdc/tests/test_extract_activity.py b/posthog/temporal/data_imports/cdc/tests/test_extract_activity.py
index 6ff526e7a569..01bf2fa1e8d2 100644
--- a/posthog/temporal/data_imports/cdc/tests/test_extract_activity.py
+++ b/posthog/temporal/data_imports/cdc/tests/test_extract_activity.py
@@ -900,6 +900,7 @@ class TestCDCExtractActivity:
cdc_extract_activity(inputs)
assert schema.sync_type_config["cdc_mode"] == "snapshot"
+ assert schema.sync_type_config["reset_pipeline"] is True
assert schema.initial_sync_complete is False
mock_reader.confirm_position.assert_called_once_with("0/500")
@@ -948,8 +949,9 @@ class TestCDCExtractActivity:
inputs = CDCExtractInput(team_id=1, source_id=source.id)
cdc_extract_activity(inputs)
- # Schema should be set back to snapshot mode
+ # Schema should be set back to snapshot mode and forced to re-snapshot from scratch
assert schema.sync_type_config["cdc_mode"] == "snapshot"
+ assert schema.sync_type_config["reset_pipeline"] is True
assert schema.initial_sync_complete is False
assert "cdc_last_log_position" not in schema.sync_type_config
@@ -1107,6 +1109,7 @@ class TestCDCExtractActivity:
cdc_extract_activity(inputs)
assert schema.sync_type_config.get("cdc_mode") == "snapshot"
+ assert schema.sync_type_config.get("reset_pipeline") is True
assert schema.sync_type_config.get("cdc_last_log_position") is None
@patch("posthog.temporal.data_imports.cdc.activities.activity")
@@ -1659,6 +1662,7 @@ class TestSlotInvalidationRecovery:
source.save.assert_called()
assert schema.sync_type_config["cdc_mode"] == "snapshot"
+ assert schema.sync_type_config["reset_pipeline"] is True
assert "cdc_last_log_position" not in schema.sync_type_config
assert "cdc_deferred_runs" not in schema.sync_type_config
assert schema.initial_sync_complete is False
diff --git a/products/data_warehouse/backend/api/external_data_schema.py b/products/data_warehouse/backend/api/external_data_schema.py
index c188ab84203a..0a3dce2aabf9 100644
--- a/products/data_warehouse/backend/api/external_data_schema.py
+++ b/products/data_warehouse/backend/api/external_data_schema.py
@@ -1074,8 +1074,11 @@ class ExternalDataSchemaSerializer(serializers.ModelSerializer):
# Always force a full re-snapshot on re-enable: while removed from the
# publication the replication slot kept advancing, so any changes made
# during that window are permanently lost regardless of how short it was.
+ # reset_pipeline wipes the warehouse table first — otherwise the snapshot
+ # merges current rows over the stale pre-disable ones and never drops deletes.
if should_sync is True and not newly_set_to_cdc:
instance.sync_type_config["cdc_mode"] = "snapshot"
+ instance.sync_type_config["reset_pipeline"] = True
instance.initial_sync_complete = False
instance.save(update_fields=["sync_type_config", "initial_sync_complete", "updated_at"])
diff --git a/products/data_warehouse/backend/api/test/test_external_data_schema.py b/products/data_warehouse/backend/api/test/test_external_data_schema.py
index 2f5bd777552d..e2b69352ab32 100644
--- a/products/data_warehouse/backend/api/test/test_external_data_schema.py
+++ b/products/data_warehouse/backend/api/test/test_external_data_schema.py
@@ -2556,6 +2556,58 @@ class TestUpdateExternalDataSchema:
assert schema.sync_type_config.get("reset_pipeline") is None
mock_trigger.assert_not_called()
+ def test_update_cdc_schema_reenable_triggers_reset_pipeline(self, team, user, client: HttpClient, temporal):
+ client.force_login(user)
+ source = ExternalDataSource.objects.create(
+ team=team,
+ source_type=ExternalDataSourceType.POSTGRES,
+ job_inputs={
+ "schema": "public",
+ "cdc_enabled": True,
+ "cdc_management_mode": "posthog",
+ "cdc_slot_name": "test_slot",
+ "cdc_publication_name": "test_pub",
+ },
+ )
+ schema = ExternalDataSchema.objects.create(
+ name="public.events",
+ team=team,
+ source=source,
+ should_sync=False,
+ initial_sync_complete=True,
+ status=ExternalDataSchema.Status.COMPLETED,
+ sync_type=ExternalDataSchema.SyncType.CDC,
+ sync_type_config={"cdc_mode": "streaming", "primary_key_columns": ["id"]},
+ )
+
+ with (
+ mock.patch(
+ "products.data_warehouse.backend.api.external_data_schema.is_cdc_enabled_for_team",
+ return_value=True,
+ ),
+ mock.patch("posthog.temporal.data_imports.sources.postgres.cdc.adapter.PostgresCDCAdapter.add_table"),
+ mock.patch(
+ "products.data_warehouse.backend.api.external_data_schema.external_data_workflow_exists",
+ return_value=False,
+ ),
+ mock.patch("products.data_warehouse.backend.api.external_data_schema.sync_external_data_job_workflow"),
+ mock.patch("products.data_warehouse.backend.api.external_data_schema.sync_cdc_extraction_schedule"),
+ ):
+ response = client.patch(
+ f"/api/environments/{team.pk}/external_data_schemas/{schema.id}",
+ data={"should_sync": True},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200, response.content
+
+ schema.refresh_from_db()
+ assert schema.should_sync is True
+ # Re-enable must wipe the warehouse table, not merge current rows over stale pre-disable ones.
+ assert schema.sync_type_config.get("reset_pipeline") is True
+ assert schema.sync_type_config["cdc_mode"] == "snapshot"
+ assert schema.initial_sync_complete is False
+
class TestCancelExternalDataSchema(APIBaseTest):
@mock.patch("products.data_warehouse.backend.api.external_data_schema.cancel_external_data_workflow")