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 connect-time refusal during a source shutdown (#76050)

Tom Owers62efce190498
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 48918180f928..17b49c4c5b50 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
@@ -392,10 +392,14 @@ def _is_connection_limit_error(error: BaseException) -> bool:
# database system is starting up"), a server replaying WAL after a crash ("the database system is
# in recovery mode"), or a hot standby that has accepted the connection attempt but not yet reached
# a consistent recovery point ("the database system is not yet accepting connections", DETAIL
-# "Consistent recovery state has not been yet reached"). All are transient: the server begins
-# accepting connections within seconds once startup/recovery completes, so a fresh connect after a
-# short backoff succeeds. libpq surfaces these as a bare OperationalError at connect time (no
-# SQLSTATE-mapped subclass), so match on the stable message. Deliberately NOT the permanent
+# "Consistent recovery state has not been yet reached"). The same SQLSTATE also covers the mirror
+# case at the other end of the server's lifecycle: a smart/fast shutdown in progress refuses new
+# connections with "the database system is shutting down" while existing backends are terminated
+# (that termination surfaces separately as "terminating connection due to administrator command",
+# already retried via `_CONNECTION_DROPPED_ERROR_SUBSTRINGS`). All are transient: the server begins
+# accepting connections again within seconds once startup/recovery/restart completes, so a fresh
+# connect after a short backoff succeeds. libpq surfaces these as a bare OperationalError at connect
+# time (no SQLSTATE-mapped subclass), so match on the stable message. Deliberately NOT the permanent
# hot-standby-disabled refusal — that reads "the database system is not accepting connections"
# (no "yet") with DETAIL "Hot standby mode is disabled" and stays non-retryable (see source.py's
# `get_non_retryable_errors`); none of the substrings below appear in it.
@@ -403,6 +407,7 @@ _SERVER_STARTING_UP_ERROR_SUBSTRINGS = (
"the database system is starting up",
"the database system is not yet accepting connections",
"the database system is in recovery mode",
+ "the database system is shutting down",
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
index 938319cabfa1..130e1ff8df84 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
@@ -769,7 +769,12 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
# self-recovering, so classify it here too — otherwise `_handle_import_error` logs it at
# `exception` on every occurrence, flooding error tracking with a self-recovering failure
# (e.g. a cloud provider terminating a backend for maintenance or failover).
- return {"terminating connection due to"}
+ # "the database system is shutting down" is the connect-time sibling of the same restart: a
+ # smart/fast shutdown refuses new connections while the source is going down, which the
+ # offset-chunking reconnect also retries in-process (`_SERVER_STARTING_UP_ERROR_SUBSTRINGS`
+ # in postgres.py) — this is the same whole-activity-retry fallback for when that budget is
+ # exhausted (e.g. a longer maintenance window).
+ return {"terminating connection due to", "the database system is shutting down"}
def reconcile_schema_metadata(
self,
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 17f46c4c45a5..966d11d7ec8c 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
@@ -1214,6 +1214,30 @@ class TestPostgresSourceRetryableErrors:
is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
assert not is_non_retryable, f"Admin-shutdown error should not be non-retryable: {error_msg}"
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # SQLSTATE 57P03: connect-time refusal while a smart/fast shutdown is in progress — the
+ # connect-time sibling of the admin-shutdown case above. The offset-chunking reconnect
+ # already retries this in-process (`_SERVER_STARTING_UP_ERROR_SUBSTRINGS` in
+ # postgres.py); this is the whole-activity-retry fallback for when that budget is
+ # exhausted (e.g. a longer maintenance window).
+ 'connection failed: connection to server at "10.0.0.1", port 5432 failed: '
+ "FATAL: the database system is shutting down",
+ "OperationalError: the database system is shutting down",
+ ],
+ )
+ def test_server_shutting_down_is_classified_retryable(self, source, error_msg):
+ retryable = source.get_retryable_errors()
+ is_retryable = any(pattern in error_msg for pattern in retryable)
+ assert is_retryable, f"Server-shutting-down error should be classified retryable: {error_msg}"
+
+ def test_server_shutting_down_is_not_also_non_retryable(self, source):
+ error_msg = "the database system is shutting down"
+ non_retryable = source.get_non_retryable_errors()
+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
+ assert not is_non_retryable, f"Server-shutting-down error should not be non-retryable: {error_msg}"
+
def _raise_eof() -> None:
# Indirection so the `yield` below stays reachable under mypy's warn_unreachable — at runtime
@@ -1671,6 +1695,14 @@ class TestDroppedOrConnectTimeout:
'connection failed: connection to server at "10.0.0.1", port 5432 failed: '
"FATAL: the database system is starting up"
),
+ # The mirror-image 57P03 refusal at shutdown: a smart/fast shutdown in progress refuses
+ # new connections the same way a not-yet-started server does. Transient — the source
+ # accepts connections again once it restarts — so the offset-chunking reconnect must
+ # retry it in-process instead of failing the whole activity.
+ psycopg.OperationalError(
+ 'connection failed: connection to server at "10.0.0.1", port 5432 failed: '
+ "FATAL: the database system is shutting down"
+ ),
],
)
def test_transient_connect_path_errors_are_retryable(self, error):