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 Supavisor connection-refused on in-process reconnect (#67112)

Tom Owersfcacbdc2c856
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
index f893b2b570d6..edc959f71067 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
@@ -192,6 +192,14 @@ _CONNECTION_DROPPED_ERROR_SUBSTRINGS = (
# "{:error, :etimedout}". Same transient class as the libpq drops above — recover by
# reconnecting. The Erlang-tuple wording is the stable, low-false-positive signal.
"{:error, :etimedout}",
+ # The connection-refused sibling of "{:error, :etimedout}": Supavisor reaches us fine but the
+ # TCP connect to its upstream backend is refused while the backend is briefly down (failover,
+ # restart, idle cull), surfacing as a ConnectionFailure carrying "{:error, :econnrefused}". This
+ # is distinct from libpq's bare English "Connection refused" (a permanent wrong-host/port
+ # misconfiguration that stays non-retryable, see source.py): here the pooler is reachable and the
+ # source was streaming moments earlier in the same sync, so a fresh reconnect recovers. Match the
+ # Erlang-tuple wording, the stable low-false-positive signal.
+ "{:error, :econnrefused}",
# Neon's proxy reports a compute that didn't finish waking from scale-to-zero before the
# auth handshake deadline as a ConnectionFailure (SQLSTATE 08006, an OperationalError):
# "Failed to connect to database: authentication did not complete within <n>ms". The wake is
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
index acd0885aa429..1d7c01a67b42 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
@@ -1070,6 +1070,14 @@ class TestIsConnectionDroppedError:
# ConnectionFailure (08006, an OperationalError) carrying the Erlang-tuple reason
# "{:error, :etimedout}" — a transient drop the in-process recovery must catch.
psycopg.errors.ConnectionFailure("Failed to connect to database: {:error, :etimedout}"),
+ # The connection-refused sibling: Supavisor's TCP connect to its upstream backend is
+ # refused while the backend is briefly down, carrying "{:error, :econnrefused}". Same
+ # transient class as :etimedout — the in-process recovery reconnect must catch it rather
+ # than letting it escape and fail the whole sync.
+ psycopg.errors.ConnectionFailure(
+ 'connection failed: connection to server at "10.0.0.1", port 5432 failed: '
+ "FATAL: Failed to connect to database: {:error, :econnrefused}"
+ ),
# Neon's proxy reports a compute that didn't wake from scale-to-zero before the auth
# deadline as a ConnectionFailure — a transient drop the in-process recovery must catch.
psycopg.errors.ConnectionFailure(
@@ -1098,6 +1106,11 @@ class TestIsConnectionDroppedError:
# non-recoverable — the InternalError_ match is scoped to the known pooler codes
# ("(EDBHANDLEREXITED)" / "(ECHECKOUTRETRIES)"), not every XX000.
psycopg.errors.InternalError_("XX000: internal error: something went wrong"),
+ # libpq's bare English "Connection refused" is a permanent wrong-host/port
+ # misconfiguration (non-retryable in source.py) and must NOT be confused with Supavisor's
+ # transient Erlang-tuple "{:error, :econnrefused}" — broadening the match to a plain
+ # "refused" substring would wrongly retry it.
+ psycopg.OperationalError('connection to server at "10.0.0.1", port 5432 failed: Connection refused'),
],
)
def test_unrelated_errors_are_not_detected(self, error):