: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(postgres): stop retrying CDC forever on a permission error (#103887)
Tom Owers3f9206c65e68
diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.pyindex 5672d07d326f..5ead31b49853 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py@@ -33,6 +33,7 @@ class CDCErrorCategory(enum.StrEnum):SLOT_NOT_CONFIGURED = "slot_not_configured"PUBLICATION_MISSING = "publication_missing"SLOT_IN_USE = "slot_in_use"+ PERMISSION_DENIED = "permission_denied"WAL_DECODE_ERROR = "wal_decode_error"TRANSACTION_TOO_LARGE = "transaction_too_large"SCHEMA_MERGE_INCOMPATIBLE = "schema_merge_incompatible"@@ -152,6 +153,14 @@ _CATEGORY_DEFAULTS: dict[CDCErrorCategory, tuple[str, bool]] = {"The replication slot is currently in use by another connection. PostHog will retry shortly.",True,),+ CDCErrorCategory.PERMISSION_DENIED: (+ "The database role connected for change data capture lacks a privilege it needs on the "+ "source database. This is often table ownership: managing the publication (for example "+ "adding a table to it) requires owning every table in it, not just SELECT. Grant the "+ "missing privilege or ownership, or switch the affected tables to Incremental sync instead "+ "of CDC, then re-enable change data capture.",+ False,+ ),CDCErrorCategory.WAL_DECODE_ERROR: ("PostHog could not decode the change stream from the source database. This usually points to ""an unsupported column type or replication setting. Contact support if it persists.",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 386f355745ab..5d662c55ab91 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@@ -2532,16 +2532,16 @@ class TestErrorClassification:mock_posthoganalytics,mock_get_machine_id,):- # A revoked REPLICATION/SELECT grant surfaces as psycopg InsufficientPrivilege, which the- # adapter doesn't classify, so it loops as retryable UNKNOWN. Its SQLSTATE (42501) is what- # tells a human this is a permission error and not some other ProgrammingError.+ # Preserve coverage for unknown psycopg failures now that insufficient privileges have a+ # dedicated category. The SQLSTATE distinguishes an unclassified syntax error from other+ # ProgrammingError subclasses without capturing potentially sensitive exception text.source = _make_source()MockSourceModel.objects.get.return_value = sourceschema = _make_schema("users", cdc_mode="streaming", source=source)mock_get_schemas.return_value = [schema]mock_reader = MagicMock()- mock_reader.read_changes.side_effect = psycopg.errors.InsufficientPrivilege("permission denied")+ mock_reader.read_changes.side_effect = psycopg.errors.SyntaxError("invalid syntax")mock_reader.truncated_tables = []mock_adapter = MagicMock()mock_adapter.create_reader.return_value = mock_reader@@ -2557,13 +2557,13 @@ class TestErrorClassification:inputs = CDCExtractInput(team_id=1, source_id=source.id)with (patch("products.data_warehouse.backend.facade.tasks.schedule_external_data_failure_digest"),- pytest.raises(psycopg.errors.InsufficientPrivilege),+ pytest.raises(psycopg.errors.SyntaxError),):cdc_extract_activity(inputs)captured = mock_posthoganalytics.capture.call_args.kwargsassert captured["event"] == "cdc extraction unclassified error"- assert "42501" in captured["properties"]["sqlstates"]+ assert "42601" in captured["properties"]["sqlstates"]class TestSlotInvalidationRecovery:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/errors.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/errors.pyindex c4e6dedac76a..ed1ddeac5d53 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/errors.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/errors.py@@ -116,6 +116,14 @@ def classify_postgres_cdc_error(exc: BaseException) -> CDCErrorCategory | None:if any(marker in message for marker in _AUTH_MARKERS):return CDCErrorCategory.AUTH_FAILED+ # SQLSTATE 42501 (insufficient_privilege) — the connecting role lacks a privilege CDC needs.+ # Covers both "permission denied for table/view/schema ..." (missing SELECT/USAGE) and+ # "must be owner of table ..." (managing a publication requires table ownership, not just+ # SELECT). Deterministic until the customer grants the privilege, so it must not fall through+ # to the retryable UNKNOWN bucket, where a stuck grant would otherwise retry forever.+ if isinstance(exc, psycopg.errors.InsufficientPrivilege):+ return CDCErrorCategory.PERMISSION_DENIED+if isinstance(exc, psycopg.OperationalError):if any(marker in message for marker in _SSL_REQUIRED_MARKERS):return CDCErrorCategory.SSL_REQUIREDdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_errors.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_errors.pyindex 99e86f8a86e0..1d5202d7c410 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_errors.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_errors.py@@ -104,6 +104,20 @@ class TestClassifyPostgresCDCError:psycopg.errors.ObjectInUse('replication slot "posthog_slot" is active for PID 123'),CDCErrorCategory.SLOT_IN_USE,),+ (+ # Recreating a customer-owned publication (e.g. after slot invalidation recovery)+ # requires owning every table added to it, not just SELECT. This must not fall+ # through to the retryable UNKNOWN bucket, or recovery retries forever against the+ # same permission wall.+ "must_be_owner_is_non_retryable_permission_denied",+ psycopg.errors.InsufficientPrivilege("must be owner of table orders"),+ CDCErrorCategory.PERMISSION_DENIED,+ ),+ (+ "permission_denied_for_table_is_non_retryable_permission_denied",+ psycopg.errors.InsufficientPrivilege("permission denied for table orders"),+ CDCErrorCategory.PERMISSION_DENIED,+ ),("wal_decode_struct_error",struct.error("unpack requires a buffer of 4 bytes"),