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): retry transient connection drop when advancing CDC slot (#64122)

Tom Owers3dd3e9cf8949
diff --git a/posthog/temporal/data_imports/sources/postgres/cdc/stream_reader.py b/posthog/temporal/data_imports/sources/postgres/cdc/stream_reader.py
index 618e666616d7..8cadce3e097d 100644
--- a/posthog/temporal/data_imports/sources/postgres/cdc/stream_reader.py
+++ b/posthog/temporal/data_imports/sources/postgres/cdc/stream_reader.py
@@ -141,12 +141,20 @@ class PgCDCStreamReader:
lsn=sql.Literal(position),
)
- advance_conn = _connect_to_postgres(
- host=self._effective_host,
- port=self._effective_port,
- database=self._params.database,
- user=self._params.user,
- password=self._params.password,
+ # This short-lived connection reaches the source through the same SSH tunnel /
+ # connection pooler as the initial connect, either of which can drop it with a
+ # transient "server closed the connection unexpectedly". Mirror connect() and
+ # absorb those drops in-process rather than failing the whole extraction
+ # activity; permanent errors (auth, SSL-required) are re-raised immediately.
+ advance_conn = _connect_with_dropped_retry(
+ lambda: _connect_to_postgres(
+ host=self._effective_host,
+ port=self._effective_port,
+ database=self._params.database,
+ user=self._params.user,
+ password=self._params.password,
+ ),
+ logger,
)
try:
with advance_conn.cursor() as cur:
diff --git a/posthog/temporal/data_imports/sources/postgres/cdc/tests/test_stream_reader.py b/posthog/temporal/data_imports/sources/postgres/cdc/tests/test_stream_reader.py
index 69851bb8eb44..c3c61deeab1c 100644
--- a/posthog/temporal/data_imports/sources/postgres/cdc/tests/test_stream_reader.py
+++ b/posthog/temporal/data_imports/sources/postgres/cdc/tests/test_stream_reader.py
@@ -64,3 +64,51 @@ class TestPgCDCStreamReaderConnect:
reader.connect()
assert connect.call_count == 1
+
+
+class TestPgCDCStreamReaderConfirmPosition:
+ def test_confirm_position_retries_transient_dropped_connection(self, params):
+ good_conn = mock.MagicMock()
+ connect = mock.MagicMock(
+ side_effect=[
+ # The short-lived slot-advance connection reaches the source through the
+ # same tunnel / pooler as the initial connect, so it can hit the same
+ # transient drop while advancing the replication slot after a run.
+ psycopg.OperationalError("server closed the connection unexpectedly"),
+ good_conn,
+ ]
+ )
+
+ reader = PgCDCStreamReader(params)
+ with (
+ patch(
+ "posthog.temporal.data_imports.sources.postgres.cdc.stream_reader._connect_to_postgres",
+ connect,
+ ),
+ patch("posthog.temporal.data_imports.sources.postgres.postgres.time.sleep"),
+ ):
+ reader.confirm_position("0/1234ABCD")
+
+ assert connect.call_count == 2
+ good_conn.commit.assert_called_once()
+ good_conn.close.assert_called_once()
+
+ def test_confirm_position_does_not_retry_permanent_error(self, params):
+ connect = mock.MagicMock(
+ side_effect=psycopg.OperationalError(
+ 'connection to server at "10.0.0.1" failed: FATAL: password authentication failed'
+ )
+ )
+
+ reader = PgCDCStreamReader(params)
+ with (
+ patch(
+ "posthog.temporal.data_imports.sources.postgres.cdc.stream_reader._connect_to_postgres",
+ connect,
+ ),
+ patch("posthog.temporal.data_imports.sources.postgres.postgres.time.sleep"),
+ ):
+ with pytest.raises(psycopg.OperationalError):
+ reader.confirm_position("0/1234ABCD")
+
+ assert connect.call_count == 1