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): treat pgbouncer's cached login-retry error as transient (#83875)

Tom Owers57b518a1f3da
diff --git a/posthog/temporal/common/db_errors.py b/posthog/temporal/common/db_errors.py
index e377a34d3d34..0e70c1ebc988 100644
--- a/posthog/temporal/common/db_errors.py
+++ b/posthog/temporal/common/db_errors.py
@@ -12,6 +12,11 @@ _TRANSIENT_DB_ERROR_MARKERS = (
"connection reset by peer",
"the database system is starting up",
"the database system is shutting down",
+ # pgbouncer's server_login_retry cooldown: a backend connect attempt failed, so pgbouncer
+ # caches the failure and hands it to every client asking for a connection until the cooldown
+ # (default 15s) elapses and it retries the backend itself. Self-heals without our retry doing
+ # anything special, so it's transient by construction, not a symptom of the underlying cause.
+ "server login has been failing, cached error",
)
# 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
new file mode 100644
index 000000000000..1eff874602f0
--- /dev/null
+++ b/posthog/temporal/common/test_db_errors.py
@@ -0,0 +1,45 @@
+import pytest
+
+from django.db import InterfaceError, OperationalError
+
+from posthog.temporal.common.db_errors import is_transient_db_error
+
+
+class _WithSqlstate(Exception):
+ def __init__(self, sqlstate: str) -> None:
+ super().__init__(sqlstate)
+ self.sqlstate = sqlstate
+
+
+ "error,expected",
+ [
+ (ValueError("query_wait_timeout"), False),
+ (OperationalError("query_wait_timeout"), True),
+ (OperationalError("server closed the connection unexpectedly"), True),
+ (InterfaceError("connection reset by peer"), True),
+ (OperationalError("the database system is starting up"), True),
+ (OperationalError("the database system is shutting down"), True),
+ (
+ OperationalError("server login has been failing, cached error: server conn crashed? (server_login_retry)"),
+ True,
+ ),
+ (OperationalError("connection failed: FATAL: password authentication failed for user"), False),
+ (OperationalError("no such database"), False),
+ ],
+)
+def test_is_transient_db_error_by_message(error: BaseException, expected: bool) -> None:
+ assert is_transient_db_error(error) is expected
+
+
+ "sqlstate,expected",
+ [
+ ("57P03", True), # cannot_connect_now (server starting up/shutting down)
+ ("3D000", False), # invalid_catalog_name — persistent misconfiguration
+ ],
+)
+def test_is_transient_db_error_by_sqlstate(sqlstate: str, expected: bool) -> None:
+ error = OperationalError("some driver-specific message")
+ error.__cause__ = _WithSqlstate(sqlstate)
+ assert is_transient_db_error(error) is expected