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(temporal): treat postgres connect-timeout as a transient db error (#129689)

Tom Owers8e2ca973ac67
diff --git a/posthog/temporal/common/db_errors.py b/posthog/temporal/common/db_errors.py
index 2ca259a3c074..ec145632f49c 100644
--- a/posthog/temporal/common/db_errors.py
+++ b/posthog/temporal/common/db_errors.py
@@ -56,6 +56,12 @@ _TRANSIENT_DB_ERROR_MARKERS = (
# (errno -2, "Name or service not known") and EAI_NODATA (errno -5, "No address associated with
# hostname") stay unmatched, because both can mean a host that never resolves.
"Temporary failure in name resolution",
+ # psycopg's own message when its connect-time deadline elapses while the TCP/TLS handshake is
+ # still polling (psycopg/generators.py::_connect). Raised standalone, with no "connection
+ # failed:" prefix, because libpq never got far enough to report a server-side error — distinct
+ # from the excluded prefix above, which wraps a definite failure response. A handshake that
+ # hasn't finished is a load/network blip that clears on retry, not a configuration problem.
+ "connection timeout expired",
)
# 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 d89d24d54520..7b1452bb30ab 100644
--- a/posthog/temporal/common/test_db_errors.py
+++ b/posthog/temporal/common/test_db_errors.py
@@ -107,6 +107,16 @@ def _raised_from(error: BaseException, cause: BaseException) -> BaseException:
),
(psycopg.errors.ProtocolViolation("query_wait_timeout"), True),
(psycopg.errors.SyntaxErrorOrAccessRuleViolation("syntax error"), False),
+ # psycopg raises this directly (no "connection failed:" prefix) when its own connect
+ # deadline elapses while still polling the socket for a handshake that never completed.
+ (psycopg.errors.ConnectionTimeout("connection timeout expired"), True),
+ (
+ _raised_from(
+ OperationalError("connection timeout expired"),
+ psycopg.errors.ConnectionTimeout("connection timeout expired"),
+ ),
+ True,
+ ),
],
)
def test_is_transient_db_error_by_message(error: BaseException, expected: bool) -> None: