: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): suggest incremental sync when postgres CDC lacks replication permission (#70232)
Tom Owersa581191a2966
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/adapter.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/adapter.pyindex f8d9ccacd212..ef3eae7032e4 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/adapter.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/adapter.py@@ -7,6 +7,7 @@ from collections.abc import Iteratorfrom contextlib import contextmanagerfrom typing import TYPE_CHECKING, Any, Literal+import psycopgimport structlogfrom products.warehouse_sources.backend.temporal.data_imports.cdc.errors import cdc_error_info@@ -45,6 +46,27 @@ logger = logging.getLogger(__name__)_retry_logger = structlog.get_logger(__name__)+def _slot_setup_error_message(exc: Exception) -> str:+ """User-facing message for a failed slot/publication setup.++ When the failure is a lack of replication privilege — the most common CDC blocker —+ point at the simplest fix rather than only echoing the raw error: switch the affected+ tables to Incremental sync, which needs only SELECT.+ """+ message = str(exc).lower()+ is_permission_error = isinstance(exc, psycopg.errors.InsufficientPrivilege) or (+ "permission denied" in message or "must be superuser" in message+ )+ if is_permission_error:+ return (+ f"Failed to create replication slot: {exc} "+ "The database user lacks permission to create logical replication slots. "+ "Either grant it replication access, or switch these tables to Incremental sync "+ "instead of CDC. Incremental needs only SELECT permission."+ )+ return f"Failed to create replication slot: {exc}"++def _split_qualified_table(qualified: str, default_schema: str) -> tuple[str, str]:"""Split a ``schema.table`` name into ``(schema, table)``, falling back to``default_schema`` for a bare table name."""@@ -233,7 +255,7 @@ class PostgresCDCAdapter:drop_publication(conn, pub_name)except Exception as rollback_error:logger.exception("Failed to roll back partial CDC slot/publication: %s", rollback_error)- return {}, f"Failed to create replication slot: {e}"+ return {}, _slot_setup_error_message(e)return resource_fields, None# self_managed: the publication is customer-owned; PostHog only creates the slot.@@ -258,7 +280,7 @@ class PostgresCDCAdapter:drop_slot(conn, slot_name)except Exception as rollback_error:logger.exception("Failed to roll back partial self-managed CDC slot: %s", rollback_error)- return {}, f"Failed to create replication slot: {e}"+ return {}, _slot_setup_error_message(e)return resource_fields, Nonedef cleanup_resources(self, source: ExternalDataSource) -> None:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/prerequisite_validator.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/prerequisite_validator.pyindex 0ae3bd64121a..2c4981f7f6fe 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/prerequisite_validator.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/prerequisite_validator.py@@ -161,7 +161,9 @@ def _check_replication_role(conn: psycopg.Connection) -> list[str]:return ["The database user cannot create logical replication slots. ""Grant one of: REPLICATION attribute (ALTER USER <username> WITH REPLICATION), "- "superuser, or membership in rds_replication / rds_superuser on RDS."+ "superuser, or membership in rds_replication / rds_superuser on RDS. "+ "If you can't grant replication access, switch these tables to Incremental sync "+ "instead of CDC. Incremental needs only SELECT permission."]return []diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_adapter.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_adapter.pyindex c07b5ead6ccf..2724939ba5d0 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_adapter.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_adapter.py@@ -5,7 +5,10 @@ from unittest.mock import MagicMock, patchimport psycopg.errors-from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.adapter import PostgresCDCAdapter+from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.adapter import (+ PostgresCDCAdapter,+ _slot_setup_error_message,+)_ADAPTER = "products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.adapter"_POSTGRES = "products.warehouse_sources.backend.temporal.data_imports.sources.postgres.postgres"@@ -168,6 +171,23 @@ class TestSetupResourcesPreflight:assert mock_drop_pub.call_args.args[1] == "p"+class TestSlotSetupErrorMessage:+ def test_permission_error_suggests_incremental_sync(self) -> None:+ error = _slot_setup_error_message(+ psycopg.errors.InsufficientPrivilege("permission denied to create replication slot")+ )+ assert "Incremental sync" in error+ assert "permission denied to create replication slot" in error++ def test_must_be_superuser_message_suggests_incremental_sync(self) -> None:+ error = _slot_setup_error_message(Exception("ERROR: must be superuser or replication role"))+ assert "Incremental sync" in error++ def test_non_permission_error_keeps_raw_message_only(self) -> None:+ error = _slot_setup_error_message(RuntimeError("connection reset"))+ assert error == "Failed to create replication slot: connection reset"++class TestRecreateSlot:@patch(f"{_ADAPTER}.create_slot_and_publication")@patch(f"{_ADAPTER}.create_slot", return_value="0/AA")