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(data-imports): classify a reused dead db connection as transient (#113047)

Tom Owersba0ed162eb33
diff --git a/posthog/temporal/common/db_errors.py b/posthog/temporal/common/db_errors.py
index ef42da0c2c97..bfbfe2f359ac 100644
--- a/posthog/temporal/common/db_errors.py
+++ b/posthog/temporal/common/db_errors.py
@@ -39,6 +39,13 @@ _TRANSIENT_DB_ERROR_MARKERS = (
# down" above, just raised by the pooler in front of Postgres rather than Postgres itself.
# A connect failure through a pooler, so no SQLSTATE — falls through to this message match.
"pooler is shutting down",
+ # psycopg's own message when a connection already marked BAD (by an earlier dropped-connection
+ # event on the same connection object) is used again: _check_connection_ok raises this straight
+ # off local state, with no new network round trip. Same dead-connection condition as "server
+ # closed the connection unexpectedly" above, just discovered on a later reuse of the connection
+ # instead of on the read/write that first found it dead. close_old_connections() at the top of
+ # the next activity attempt discards the broken connection, so a retry self-heals.
+ "the connection is closed",
)
# SQLSTATE class 57P (operator intervention): the server is shutting down or restarting and
diff --git a/posthog/temporal/common/test_db_errors.py b/posthog/temporal/common/test_db_errors.py
index 2ef33d4b3345..a1e1585b81b7 100644
--- a/posthog/temporal/common/test_db_errors.py
+++ b/posthog/temporal/common/test_db_errors.py
@@ -46,6 +46,10 @@ def _raised_from(error: BaseException, cause: BaseException) -> BaseException:
),
(OperationalError("connection failed: FATAL: password authentication failed for user"), False),
(OperationalError("no such database"), False),
+ # psycopg raises this straight off local state (no new network I/O) when a connection
+ # already marked dead by an earlier operation is reused — the same dropped-connection
+ # condition as "server closed the connection unexpectedly", just discovered later.
+ (OperationalError("the connection is closed"), True),
# The connect path's socket/selector setup raises a bare OSError, not an OperationalError,
# when this worker's own fd table is full — same condition already retried on a source's
# connect path (postgres.py::_is_too_many_open_files_error).