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): treat exhausted connection drops as retryable, not error noise (#80402)

Tom Owersf7d76e9f5f23
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 e2e263a9c3f6..01561127124a 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
@@ -806,9 +806,22 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
# `_is_dropped_or_connection_limit`. A slot frees the moment another connection closes, so a
# sustained shortage that outlasts that budget is still transient — the same
# reaches-here-only-after-internal-retries-exhaust case as the two entries above.
+ #
+ # A mid-stream or connect-time connection drop ("server closed the connection unexpectedly",
+ # the SSL-flavoured "SSL connection has been closed unexpectedly") is retried in-process by
+ # `_connect_with_dropped_retry` / the offset-chunking fallback (both keyed on
+ # `_CONNECTION_DROPPED_ERROR_SUBSTRINGS` in postgres.py). Once that budget is exhausted the
+ # raw psycopg `OperationalError` reaches `_handle_import_error`, which — without a match here
+ # — logs it at `exception` and reports it to error tracking on every occurrence, even though
+ # Temporal retries the whole activity and the drop is transient and self-recovering (a pooler
+ # idle cull, failover, or network blip). Classify it retryable so it logs as a warning
+ # instead. These stay clear of `get_non_retryable_errors` deliberately (see the note there),
+ # so the sync keeps retrying rather than being disabled.
return {
"terminating connection due to",
"the database system is shutting down",
+ "server closed the connection unexpectedly",
+ "SSL connection has been closed unexpectedly",
*_CONNECTION_LIMIT_ERROR_SUBSTRINGS,
}
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 0f960d6a07e7..c826d893af1f 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
@@ -31,6 +31,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arr
TemporaryFileSizeExceedsLimitException,
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.consts import DEFAULT_CHUNK_SIZE
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import error_message_matches
from products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.predicates import (
ColumnTypeCategory,
ValidatedRowFilter,
@@ -424,6 +425,21 @@ class TestPostgresSourceNonRetryableErrors:
is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
assert is_non_retryable, f"Permanent error should be non-retryable: {error_msg}"
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ 'OperationalError: connection failed: connection to server at "db.example.com", port 5432 failed: server closed the connection unexpectedly',
+ 'OperationalError: connection failed: connection to server at "db.example.com", port 5432 failed: SSL connection has been closed unexpectedly',
+ ],
+ )
+ def test_exhausted_connection_drops_are_classified_retryable(self, source, error_msg):
+ # These transient drops are retried in-process, then re-raised once that budget is exhausted.
+ # If they drop out of get_retryable_errors, _handle_import_error logs the self-recovering
+ # failure as a tracked exception again instead of a warning. They must also stay out of
+ # get_non_retryable_errors so the sync keeps retrying rather than being disabled.
+ assert error_message_matches(error_msg, source.get_retryable_errors())
+ assert not error_message_matches(error_msg, source.get_non_retryable_errors().keys())
+
@pytest.mark.parametrize(
"error_msg",
[