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): stop CDC retrying an unreachable SSH gateway forever (#78583)

Tom Owers1e306deb57cd
diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py
index b7eb0c76fd64..9680cce2a6f1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py
@@ -27,6 +27,7 @@ class CDCErrorCategory(enum.StrEnum):
SSL_REQUIRED = "ssl_required"
CONNECTION_FAILED = "connection_failed"
HOST_UNREACHABLE = "host_unreachable"
+ SSH_TUNNEL_FAILED = "ssh_tunnel_failed"
SLOT_MISSING = "slot_missing"
SLOT_NOT_CONFIGURED = "slot_not_configured"
PUBLICATION_MISSING = "publication_missing"
@@ -99,6 +100,13 @@ _CATEGORY_DEFAULTS: dict[CDCErrorCategory, tuple[str, bool]] = {
"address), then re-enable change data capture.",
False,
),
+ CDCErrorCategory.SSH_TUNNEL_FAILED: (
+ "Could not connect to your SSH tunnel — PostHog couldn't open a session to the SSH gateway. "
+ "Check that the SSH host and port point to a reachable SSH server (not the database port), "
+ "that the bastion is running, and that PostHog's IP addresses are allowed through its "
+ "firewall, then re-enable change data capture.",
+ False,
+ ),
CDCErrorCategory.SLOT_MISSING: (
"The replication slot no longer exists on the source database, so changes can no longer be "
"read. Use Repair CDC to recreate it and re-sync.",
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 fd38a407d149..c6d9abe44c3a 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
@@ -12,6 +12,7 @@ import struct
import psycopg
import psycopg.errors
+from sshtunnel import BaseSSHTunnelForwarderError
from products.warehouse_sources.backend.temporal.data_imports.cdc.errors import CDCErrorCategory
@@ -39,6 +40,13 @@ _HOST_UNREACHABLE_MARKERS = (
"no route to host",
)
+# sshtunnel raises BaseSSHTunnelForwarderError("Could not establish session to SSH gateway") when it
+# can't open a session to the bastion — the SSH host/port is wrong or unreachable, the bastion is
+# down, or its firewall blocks PostHog's IPs. Deterministic for the configured tunnel, so it's
+# non-retryable. Mirrors the non-retryable treatment on the batch path
+# (PostgresSource.get_non_retryable_errors's `_SSH_GATEWAY_SESSION_ERROR` entry).
+_SSH_GATEWAY_SESSION_ERROR_MARKER = "could not establish session to ssh gateway"
+
def classify_postgres_cdc_error(exc: BaseException) -> CDCErrorCategory | None:
"""Classify a single Postgres exception into a ``CDCErrorCategory``.
@@ -50,6 +58,12 @@ def classify_postgres_cdc_error(exc: BaseException) -> CDCErrorCategory | None:
if isinstance(exc, struct.error):
return CDCErrorCategory.WAL_DECODE_ERROR
+ # sshtunnel's own exception, not a psycopg one — must be checked before the psycopg guard below.
+ if isinstance(exc, BaseSSHTunnelForwarderError):
+ if _SSH_GATEWAY_SESSION_ERROR_MARKER in str(exc).lower():
+ return CDCErrorCategory.SSH_TUNNEL_FAILED
+ return None
+
# Guard all string-based checks: only psycopg exceptions carry these message patterns.
# A non-psycopg exception whose message happens to contain e.g. "does not exist" would
# otherwise be misclassified as non-retryable SLOT_MISSING, permanently stopping retries.
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 3bf4781c2864..564b2be302fd 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
@@ -2,6 +2,7 @@ import struct
import psycopg.errors
from parameterized import parameterized
+from sshtunnel import BaseSSHTunnelForwarderError
from products.warehouse_sources.backend.temporal.data_imports.cdc.errors import CDCErrorCategory
from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.errors import (
@@ -74,6 +75,16 @@ class TestClassifyPostgresCDCError:
struct.error("unpack requires a buffer of 4 bytes"),
CDCErrorCategory.WAL_DECODE_ERROR,
),
+ (
+ "ssh_gateway_session_failure_is_non_retryable",
+ BaseSSHTunnelForwarderError("Could not establish session to SSH gateway"),
+ CDCErrorCategory.SSH_TUNNEL_FAILED,
+ ),
+ (
+ "unrecognized_ssh_tunnel_error_falls_back_to_unknown",
+ BaseSSHTunnelForwarderError("Some other sshtunnel failure"),
+ None,
+ ),
(
"unrelated_runtime_error",
RuntimeError("S3 write failed"),