: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): recognize native psycopg errors as transient db errors (#111823)
Tom Owers319a78637316
diff --git a/posthog/temporal/common/db_errors.py b/posthog/temporal/common/db_errors.pyindex d516b6478bfa..ef42da0c2c97 100644--- a/posthog/temporal/common/db_errors.py+++ b/posthog/temporal/common/db_errors.py@@ -2,6 +2,7 @@ import errnofrom django.db import InterfaceError, InternalError, OperationalError, ProgrammingError+import psycopgimport psycopg.errors# Substrings identifying transient Postgres failures. pgbouncer kills queries that wait too long@@ -92,8 +93,21 @@ def is_transient_db_error(error: BaseException) -> bool:error.__cause__, psycopg.errors.UndefinedColumn | psycopg.errors.UndefinedTable):return True- if isinstance(error, OperationalError | InterfaceError | InternalError):- sqlstate = getattr(error.__cause__, "sqlstate", None)+ # Code that talks to Postgres through a raw psycopg connection instead of Django's ORM+ # (e.g. the warehouse-sources postgres queue producer) raises psycopg's own exception+ # classes directly, never wrapped in Django's — so both class families are checked here,+ # and sqlstate is read off the error itself first since a native psycopg error carries it+ # directly, falling back to __cause__ for Django's wrapped errors.+ if isinstance(+ error,+ OperationalError+ | InterfaceError+ | InternalError+ | psycopg.OperationalError+ | psycopg.InterfaceError+ | psycopg.InternalError,+ ):+ sqlstate = getattr(error, "sqlstate", None) or getattr(error.__cause__, "sqlstate", None)if isinstance(sqlstate, str) and (sqlstate.startswith(_TRANSIENT_SQLSTATE_PREFIXES) or sqlstate in _TRANSIENT_SQLSTATES):diff --git a/posthog/temporal/common/test_db_errors.py b/posthog/temporal/common/test_db_errors.pyindex edf3b10313a2..2ef33d4b3345 100644--- a/posthog/temporal/common/test_db_errors.py+++ b/posthog/temporal/common/test_db_errors.py@@ -75,6 +75,17 @@ def _raised_from(error: BaseException, cause: BaseException) -> BaseException:_raised_from(ApplicationError("Failed to emit $ai_evaluation"), KeyError("team_id")),False,),+ # Code using a raw psycopg connection instead of Django's ORM (e.g. the warehouse-sources+ # postgres queue producer) raises psycopg's own exception classes directly, never wrapped+ # in Django's OperationalError/InterfaceError/InternalError.+ (+ psycopg.errors.ProtocolViolation(+ "server login has been failing, cached error: connect failed (server_login_retry)"+ ),+ True,+ ),+ (psycopg.errors.ProtocolViolation("query_wait_timeout"), True),+ (psycopg.errors.SyntaxErrorOrAccessRuleViolation("syntax error"), False),],)def test_is_transient_db_error_by_message(error: BaseException, expected: bool) -> None:diff --git a/posthog/temporal/tests/common/test_posthog_client_transient_db.py b/posthog/temporal/tests/common/test_posthog_client_transient_db.pyindex f6c5d5e8981c..3573fb5ee506 100644--- a/posthog/temporal/tests/common/test_posthog_client_transient_db.py+++ b/posthog/temporal/tests/common/test_posthog_client_transient_db.py@@ -67,6 +67,15 @@ class TestTransientDatabaseErrorReporting:error_cls=InternalError,),),+ # Code using a raw psycopg connection instead of Django's ORM (e.g. the+ # warehouse-sources postgres queue producer) raises psycopg's own exception classes+ # directly, never wrapped in Django's OperationalError/InterfaceError/InternalError.+ (+ "native_psycopg_pgbouncer_login_retry",+ psycopg.errors.ProtocolViolation(+ "server login has been failing, cached error: connect failed (server_login_retry)"+ ),+ ),])async def test_transient_db_errors_are_not_reported(self, _name, error):