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 drops while opening setup connection (#63960)

Tom Owersc74a763005f2
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.py
index 13a62325a6fe..5a6d858af757 100644
--- a/posthog/temporal/data_imports/sources/postgres/postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py
@@ -1930,7 +1930,12 @@ def postgres_source(
# letting Temporal re-run setup straight back into the same wall.
setup_recovery_conflicts = 0
while True:
- connection = _open_setup_connection()
+ # Opening the setup connection can itself hit a transient drop ("server closed the
+ # connection unexpectedly", idle cull, failover) — the same class of error the read
+ # path already recovers from. Retry the connect in-process with bounded backoff,
+ # mirroring `offset_chunking`, so a momentary blip during setup doesn't fail the whole
+ # activity. Permanent errors (auth failures, SSL-required) re-raise immediately.
+ connection = _connect_with_dropped_retry(_open_setup_connection, logger)
try:
with connection:
with connection.cursor() as cursor:
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index 5d98b2d99ff9..454745f0688a 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -398,6 +398,44 @@ class TestPostgresSourceSetupRecoveryConflictRetry:
assert connect_mock.call_count == 1
+ def test_connection_dropped_while_opening_setup_connection_is_retried(self):
+ # A transient drop while opening the setup connection ("server closed the connection
+ # unexpectedly") is the same class of error the read path already recovers from. The setup
+ # connect must retry in-process with bounded backoff instead of failing on the first drop.
+ err = psycopg.OperationalError(
+ 'connection failed: connection to server at "3.151.121.165", port 5432 failed: '
+ "server closed the connection unexpectedly"
+ )
+
+ with patch(
+ "posthog.temporal.data_imports.sources.postgres.postgres.psycopg.connect",
+ side_effect=err,
+ ) as connect_mock:
+ with patch("posthog.temporal.data_imports.sources.postgres.postgres.time.sleep"):
+ with pytest.raises(psycopg.OperationalError):
+ self._call_postgres_source()
+
+ # `_connect_with_dropped_retry` defaults to 5 attempts; before the fix the drop escaped on
+ # the first connect (call_count == 1).
+ assert connect_mock.call_count == 5
+
+ def test_permanent_error_while_opening_setup_connection_is_not_retried(self):
+ # A permanent connect failure (bad password) must not be retried by the dropped-connection
+ # handler — it should propagate on the first attempt.
+ err = psycopg.OperationalError(
+ 'connection to server at "10.0.0.1" failed: FATAL: password authentication failed for user "u"'
+ )
+
+ with patch(
+ "posthog.temporal.data_imports.sources.postgres.postgres.psycopg.connect",
+ side_effect=err,
+ ) as connect_mock:
+ with patch("posthog.temporal.data_imports.sources.postgres.postgres.time.sleep"):
+ with pytest.raises(psycopg.OperationalError):
+ self._call_postgres_source()
+
+ assert connect_mock.call_count == 1
+
class TestIsConnectionDroppedError:
@pytest.mark.parametrize(