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(postgres): treat exhausted CDC connect timeouts as non-retryable (#105642)

Tom Owersc08ddd28c94e
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.py
index 9d0814749921..474d6d2811c2 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
@@ -35,6 +35,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.c
slot_exists,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.postgres import (
+ _is_dropped_or_connect_timeout,
_retry_on_connection_dropped,
source_requires_ssl,
)
@@ -230,9 +231,16 @@ class PostgresCDCAdapter:
# connection (the slot invalidation that triggered recovery), so a transient drop
# mid-recreate — the server terminating our backend on a deploy/failover, an idle cull —
# is likely. drop_slot runs first on every attempt, so retrying is idempotent; absorb the
- # drop in-process instead of failing the whole recovery. Permanent errors (auth, a missing
- # customer-owned publication) don't match the predicate and re-raise immediately.
- consistent_point = _retry_on_connection_dropped(_recreate, _retry_logger)
+ # drop in-process instead of failing the whole recovery. Widen the predicate to also
+ # retry connect-time timeouts: cdc_pg_connection opens with the same _connect_to_postgres
+ # the main streaming path uses, and classify_postgres_cdc_error now treats an exhausted
+ # ConnectionTimeout as non-retryable on the assumption every reconnect already timed out —
+ # without retrying it here first, a single transient connect timeout would abort recovery
+ # instead of reaching that exhausted state. Permanent errors (auth, a missing
+ # customer-owned publication) don't match either predicate and re-raise immediately.
+ consistent_point = _retry_on_connection_dropped(
+ _recreate, _retry_logger, is_retryable=_is_dropped_or_connect_timeout
+ )
# Every schema is reset to snapshot before this runs, so no change from the dead slot is owed
# to the legacy lane: the new slot starts on the buffer, as a new source does.
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.py
index ed1ddeac5d53..818be0f39d63 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
@@ -131,6 +131,16 @@ def classify_postgres_cdc_error(exc: BaseException) -> CDCErrorCategory | None:
return CDCErrorCategory.HOST_UNREACHABLE
if _QUOTA_EXCEEDED_MARKER in message:
return CDCErrorCategory.QUOTA_EXCEEDED
+ # psycopg raises ConnectionTimeout only from connect() itself, and the reader already
+ # retries it in-process first (stream_reader._open_streaming_connection wraps every
+ # connect in _connect_with_dropped_retry, which widens its predicate to connect-time
+ # timeouts). Reaching here means every one of those reconnects also timed out — a
+ # persistently unreachable host, not a one-off blip — so treat it like the other
+ # unreachable-host markers above instead of retrying the same wall forever. Mirrors the
+ # non-retryable treatment on the batch path (PostgresSource.get_non_retryable_errors's
+ # "connection timeout expired" entry).
+ if isinstance(exc, psycopg.errors.ConnectionTimeout):
+ return CDCErrorCategory.HOST_UNREACHABLE
return CDCErrorCategory.CONNECTION_FAILED
return None
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.py
index 1eddae9f54c2..d182e598a15d 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
@@ -318,6 +318,36 @@ class TestRecreateSlot:
assert mock_create_slot.call_count == 2
assert mock_drop.call_count == 2
+ @patch(f"{_POSTGRES}.time.sleep")
+ @patch(f"{_ADAPTER}.create_slot")
+ @patch(f"{_ADAPTER}.publication_exists", return_value=True)
+ @patch(f"{_ADAPTER}.drop_slot")
+ @patch(f"{_ADAPTER}.cdc_pg_connection", new_callable=_fake_conn)
+ def test_retries_recreation_after_transient_connect_timeout(
+ self, _conn, mock_drop, _pub_exists, mock_create_slot, _sleep
+ ) -> None:
+ # classify_postgres_cdc_error now treats ConnectionTimeout as non-retryable, on the
+ # assumption every in-process reconnect already timed out (mirroring the main streaming
+ # path's _connect_with_dropped_retry). Recreation must retry a connect timeout the same
+ # way it retries a mid-stream drop, or a single transient timeout here would abort
+ # recovery instead of reaching that exhausted state.
+ mock_create_slot.side_effect = [
+ psycopg.errors.ConnectionTimeout("connection timeout expired"),
+ "0/DD",
+ ]
+ source = _source(
+ cdc_enabled=True,
+ cdc_management_mode="posthog",
+ cdc_slot_name="posthog_slot",
+ cdc_publication_name="posthog_pub",
+ )
+
+ fields = PostgresCDCAdapter().recreate_slot(source, tables=["users"])
+
+ assert fields == {"cdc_consistent_point": "0/DD", "cdc_ingest_mode": "buffered"}
+ assert mock_create_slot.call_count == 2
+ assert mock_drop.call_count == 2
+
class TestAlterPublicationMembership:
@patch(f"{_ADAPTER}.add_table_to_publication")
diff --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.py
index 1d5202d7c410..57e091198c15 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
@@ -84,6 +84,16 @@ class TestClassifyPostgresCDCError:
HostNotAllowedError("Database host not allowed: resolves to a private address"),
CDCErrorCategory.HOST_UNREACHABLE,
),
+ (
+ # ConnectionTimeout only ever reaches here after _connect_with_dropped_retry has
+ # already exhausted its in-process reconnect attempts, so it means the host is
+ # persistently unreachable, not a transient blip. Must not fall through to the
+ # retryable CONNECTION_FAILED bucket, or the workflow retries into the same wall
+ # forever instead of surfacing an actionable message.
+ "connect_timeout_exhausted_is_non_retryable_host",
+ psycopg.errors.ConnectionTimeout("connection timeout expired"),
+ CDCErrorCategory.HOST_UNREACHABLE,
+ ),
(
"ssh_tunnel_host_not_allowed_is_a_tunnel_failure",
HostNotAllowedError("SSH tunnel host not allowed: resolves to a private address"),