: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(postgres): retry transient SSL connection drops on connect (#64605)
Tom Owersa0697029cab8
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.pyindex a7886db95410..3892d3aec70d 100644--- a/posthog/temporal/data_imports/sources/postgres/postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py@@ -155,6 +155,13 @@ _CONNECTION_DROPPED_ERROR_SUBSTRINGS = ("connection to server was lost","connection to server was closed","consuming input failed",+ # The SSL-flavoured drop: an established TLS connection closed mid-handshake or mid-stream+ # (pooler/firewall idle cull, failover, network blip). libpq surfaces it bare as "... failed:+ # SSL connection has been closed unexpectedly" — no "consuming input failed" prefix — so the+ # substring above doesn't catch it. Same transient class; recover by reconnecting. A genuine+ # no-SSL-support source fails with a different message ("server does not support SSL") and, on+ # require_ssl sources, is converted to SSLRequiredError before reaching this check.+ "ssl connection has been closed unexpectedly","no connection to the server","terminating connection due to",# psycopg's own message when libpq finds the socket already gone (PGconn.socketdiff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.pyindex f2aed09b91aa..0b6666690d95 100644--- a/posthog/temporal/data_imports/sources/postgres/source.py+++ b/posthog/temporal/data_imports/sources/postgres/source.py@@ -303,13 +303,14 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa"No primary key defined for table": None,"failed: timeout expired": None,# NOTE: "SSL connection has been closed unexpectedly" is intentionally NOT listed here.- # It denotes an established SSL connection being dropped mid-stream (idle cull by a- # pooler, failover, network blip) — a transient condition that recovers on a fresh- # attempt. It is the SSL-flavoured sibling of the "consuming input failed" drops handled- # by `_CONNECTION_DROPPED_ERROR_SUBSTRINGS` in postgres.py. Marking it non-retryable- # permanently disabled syncs on a transient blip during schema discovery, which has no- # in-process reconnect. A genuinely unsupported-SSL source fails at connect time with a- # different message and is caught via "SSLRequiredError" / "SSL/TLS connection is required".+ # It denotes an established SSL connection being dropped on connect or mid-stream (idle+ # cull by a pooler, failover, network blip) — a transient condition that recovers on a+ # fresh attempt. It is the SSL-flavoured sibling of the libpq drops in+ # `_CONNECTION_DROPPED_ERROR_SUBSTRINGS`, where it is listed so the in-process reconnect+ # retries it during schema discovery and sync setup instead of failing the activity.+ # Marking it non-retryable would permanently disable syncs on a transient blip. A+ # genuinely unsupported-SSL source fails at connect time with a different message and is+ # caught via "SSLRequiredError" / "SSL/TLS connection is required"."Address not in tenant allow_list": None,"FATAL: no such database": None,"does not exist": None,diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex ba005dc0a34b..5ac4f29ed771 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -912,6 +912,13 @@ class TestIsConnectionDroppedError:psycopg.OperationalError("connection to server was lost"),psycopg.OperationalError("connection to server was closed unexpectedly"),psycopg.OperationalError("consuming input failed: EOF detected"),+ # libpq's bare SSL drop, exactly as it reaches the discovery/setup connect — no+ # "consuming input failed" prefix. A transient TLS close (pooler/firewall idle cull,+ # failover) the in-process reconnect must catch, not the permanent no-SSL-support case.+ psycopg.OperationalError(+ 'connection failed: connection to server at "10.0.0.1", port 5432 failed: '+ "SSL connection has been closed unexpectedly"+ ),psycopg.OperationalError("terminating connection due to administrator command"),# psycopg's message when libpq finds the socket already gone (raised from# PGconn.socket inside the commit at the end of get_connection). A transient@@ -2202,14 +2209,22 @@ class TestPostgresSchemaDiscovery:for dropped in dropping_connections:dropped.close.assert_called_once()- def test_get_schemas_retries_transient_connection_drop_on_connect(self):- # A transient drop on the discovery connect ("server closed the connection unexpectedly")- # is the same class of error the import read path already recovers from. Discovery must- # retry the connect in-process and recover instead of failing the whole run — and surfacing- # as captured error-tracking noise — on the first blip.+ @pytest.mark.parametrize(+ "drop_message",+ [+ "server closed the connection unexpectedly",+ # The SSL-flavoured sibling, exactly as it reached discovery in production. Before the+ # fix it wasn't in `_CONNECTION_DROPPED_ERROR_SUBSTRINGS`, so the retry didn't catch it+ # and the first blip surfaced as captured error-tracking noise.+ "SSL connection has been closed unexpectedly",+ ],+ )+ def test_get_schemas_retries_transient_connection_drop_on_connect(self, drop_message):+ # A transient drop on the discovery connect is the same class of error the import read path+ # already recovers from. Discovery must retry the connect in-process and recover instead of+ # failing the whole run — and surfacing as captured error-tracking noise — on the first blip.drop = psycopg.OperationalError(- 'connection failed: connection to server at "66.33.22.246", port 11212 failed: '- "server closed the connection unexpectedly"+ f'connection failed: connection to server at "66.33.22.246", port 11212 failed: {drop_message}')connection = self._mock_connection([("public", "users")],