: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): fail fast when CDC source has no replication slot (#71095)
Tom Owersfbacf52b837b
diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.pyindex 7c34a55e9352..348739c9a085 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py@@ -54,6 +54,7 @@ from products.warehouse_sources.backend.temporal.data_imports.cdc.errors importCDCErrorCategory,CDCErrorInfo,CDCSchemaMergeError,+ CDCSlotNotConfiguredError,classify_cdc_error,)from products.warehouse_sources.backend.temporal.data_imports.cdc.naming import cdc_qualified_table_name@@ -710,6 +711,7 @@ class CDCExtractActivity:self.log.warning("cdc_orphan_reconcile_unexpected_failed", exc_info=True)try:+ self._require_configured_slot()self.reader.connect()self._load_pk_columns()@@ -1284,6 +1286,19 @@ class CDCExtractActivity:continueself._update_schema_sync_type_config(schema, updates={"cdc_last_log_position": self.last_end_lsn})+ def _require_configured_slot(self) -> None:+ """Fail fast when a CDC-enabled source has no replication slot name stored.++ Streaming an empty slot name surfaces as ``replication slot "" does not exist``, which+ the invalidation check reads as a recoverable slot drop — so recovery (and Repair CDC)+ run, only to dead-end because there is no slot name to recreate. Raise the non-retryable+ misconfiguration up front instead, with guidance that actually resolves it.+ """+ assert self.adapter is not None+ assert self.source is not None+ if not self.adapter.parse_cdc_config(self.source).slot_name:+ raise CDCSlotNotConfiguredError()+# ------------------------------------------------------------------# Failure / success finalization# ------------------------------------------------------------------@@ -1358,7 +1373,11 @@ class CDCExtractActivity:# the per-schema FAILED state + the cdc_broken marker the UI/health check read and pauses the# schedule, so it stops firing hourly against a resource that is gone (the same zombie the lag# safety net produces). Any other failure just fails this run's schemas.- marked_broken = info.category in (CDCErrorCategory.SLOT_MISSING, CDCErrorCategory.PUBLICATION_MISSING)+ marked_broken = info.category in (+ CDCErrorCategory.SLOT_MISSING,+ CDCErrorCategory.SLOT_NOT_CONFIGURED,+ CDCErrorCategory.PUBLICATION_MISSING,+ )if marked_broken:assert self.source is not Nonemark_cdc_broken(self.source, info.category.value, friendly)diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.pyindex b82ed7f77a6e..b7eb0c76fd64 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py@@ -28,6 +28,7 @@ class CDCErrorCategory(enum.StrEnum):CONNECTION_FAILED = "connection_failed"HOST_UNREACHABLE = "host_unreachable"SLOT_MISSING = "slot_missing"+ SLOT_NOT_CONFIGURED = "slot_not_configured"PUBLICATION_MISSING = "publication_missing"SLOT_IN_USE = "slot_in_use"WAL_DECODE_ERROR = "wal_decode_error"@@ -52,6 +53,17 @@ class CDCTransactionTooLargeError(Exception):"""+class CDCSlotNotConfiguredError(Exception):+ """Change data capture is enabled but the source has no replication slot name stored.++ Non-retryable: without a slot name there is nothing to stream from and nothing to recreate,+ so retrying — or the slot-invalidation recovery / Repair CDC paths — replays the same dead+ end. The extraction activity raises this before streaming (streaming an empty slot name would+ surface as a misleading "replication slot does not exist"). Defined here so the shared+ classifier owns the mapping to ``SLOT_NOT_CONFIGURED``.+ """++class CDCSchemaMergeError(Exception):"""A column's values can't be reconciled into one Parquet type across micro-batches.@@ -92,6 +104,11 @@ _CATEGORY_DEFAULTS: dict[CDCErrorCategory, tuple[str, bool]] = {"read. Use Repair CDC to recreate it and re-sync.",False,),+ CDCErrorCategory.SLOT_NOT_CONFIGURED: (+ "Change data capture is enabled for this source but no replication slot is configured, so "+ "changes can't be read. Disable and re-enable change data capture to set it up again.",+ False,+ ),CDCErrorCategory.PUBLICATION_MISSING: ("The publication used for change data capture no longer exists on the source database. ""Recreate it (self-managed), or use Repair CDC (PostHog-managed) to recreate it and re-sync.",@@ -149,6 +166,8 @@ def classify_cdc_error(exc: BaseException, adapter: CDCSourceAdapter | None) ->Falls back to a retryable ``unknown`` so a misclassification never strands a recoverable run."""for err in _iter_cause_chain(exc):+ if isinstance(err, CDCSlotNotConfiguredError):+ return cdc_error_info(CDCErrorCategory.SLOT_NOT_CONFIGURED)if isinstance(err, CDCTransactionTooLargeError):return cdc_error_info(CDCErrorCategory.TRANSACTION_TOO_LARGE)if isinstance(err, CDCSchemaMergeError):diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_errors.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_errors.pyindex c300780b6ef6..aa41b9d0b12b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_errors.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_errors.py@@ -6,6 +6,7 @@ from products.warehouse_sources.backend.temporal.data_imports.cdc.errors importCDCErrorCategory,CDCErrorInfo,CDCSchemaMergeError,+ CDCSlotNotConfiguredError,CDCTransactionTooLargeError,cdc_error_info,classify_cdc_error,@@ -34,6 +35,7 @@ class TestCDCErrorInfo:(CDCErrorCategory.CONNECTION_FAILED, True),(CDCErrorCategory.HOST_UNREACHABLE, False),(CDCErrorCategory.SLOT_MISSING, False),+ (CDCErrorCategory.SLOT_NOT_CONFIGURED, False),(CDCErrorCategory.PUBLICATION_MISSING, False),(CDCErrorCategory.SLOT_IN_USE, True),(CDCErrorCategory.WAL_DECODE_ERROR, False),@@ -84,6 +86,13 @@ class TestClassifyCDCError:assert info.category is CDCErrorCategory.TRANSACTION_TOO_LARGEassert info.retryable is False+ def test_slot_not_configured_is_classified_without_adapter(self):+ # A CDC-enabled source with no slot name is a config dead end, not a psycopg failure, so it+ # must classify to a non-retryable category regardless of engine adapter.+ info = classify_cdc_error(CDCSlotNotConfiguredError(), None)+ assert info.category is CDCErrorCategory.SLOT_NOT_CONFIGURED+ assert info.retryable is False+def test_schema_merge_error_is_non_retryable_via_cause_chain(self):# The activity wraps the Arrow type error as CDCSchemaMergeError; classification must# find it through the cause chain and mark the run non-retryable so it stops looping.diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.pyindex 486646c1b949..fcb74026ea98 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py@@ -1924,6 +1924,69 @@ class TestErrorClassification:assert mock_mark_broken.call_args.args[0] is sourceassert mock_mark_broken.call_args.args[1] == expected_reason+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.mark_cdc_broken")+ @patch(+ "products.warehouse_sources.backend.temporal.data_imports.cdc.activities.get_machine_id",+ return_value="machine-1",+ )+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.posthoganalytics")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.activity")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.get_cdc_adapter")+ @patch.object(CDCExtractActivity, "_get_cdc_schemas")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.ExternalDataSource")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.close_old_connections")+ def test_missing_slot_name_fails_before_streaming_without_recovery(+ self,+ mock_close_conns,+ MockSourceModel,+ mock_get_schemas,+ mock_get_adapter,+ mock_activity,+ mock_posthoganalytics,+ mock_get_machine_id,+ mock_mark_broken,+ ):+ # A CDC-enabled source whose stored slot name is empty must fail fast and non-retryably:+ # streaming an empty slot name reads as a recoverable slot drop, so recovery / Repair CDC+ # run only to dead-end with no slot name to recreate. Guard before streaming instead.+ source = _make_source(+ job_inputs={+ "host": "localhost",+ "port": 5432,+ "database": "testdb",+ "user": "test",+ "password": "test",+ "cdc_publication_name": "posthog_pub",+ }+ )+ MockSourceModel.objects.get.return_value = source+ schema = _make_schema("users", cdc_mode="streaming", source=source)+ mock_get_schemas.return_value = [schema]++ mock_reader = MagicMock()+ mock_adapter = MagicMock()+ mock_adapter.create_reader.return_value = mock_reader+ mock_adapter.is_slot_invalidation_error.return_value = False+ mock_adapter.parse_cdc_config = PostgresCDCAdapter().parse_cdc_config # reads the empty slot name+ mock_get_adapter.return_value = mock_adapter++ mock_activity.heartbeat = MagicMock()+ mock_activity.info.return_value = MagicMock(workflow_id="wf-1", workflow_run_id="run-1", attempt=1)++ inputs = CDCExtractInput(team_id=1, source_id=source.id)+ with pytest.raises(NonRetryableException) as exc_info:+ cdc_extract_activity(inputs)++ assert str(exc_info.value) == cdc_error_info(CDCErrorCategory.SLOT_NOT_CONFIGURED).friendly_message+ # Never streamed and never tried to recover — the guard fired first.+ mock_reader.connect.assert_not_called()+ mock_reader.read_changes.assert_not_called()+ mock_adapter.recreate_slot.assert_not_called()+ # Broken state persisted so the schedule stops firing against an unconfigured slot.+ mock_mark_broken.assert_called_once()+ assert mock_mark_broken.call_args.args[0] is source+ assert mock_mark_broken.call_args.args[1] == "slot_not_configured"+@patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.get_machine_id",return_value="machine-1",