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): retry read-only-transaction db errors instead of reporting them (#88297)

Tom Owers4c629893dc05
diff --git a/posthog/temporal/common/test_db_errors.py b/posthog/temporal/common/test_db_errors.py
index d096febebfb1..417c1c108ed2 100644
--- a/posthog/temporal/common/test_db_errors.py
+++ b/posthog/temporal/common/test_db_errors.py
@@ -43,15 +43,19 @@ def test_is_transient_db_error_by_message(error: BaseException, expected: bool)
@pytest.mark.parametrize(
- "sqlstate,expected",
+ "error_cls,sqlstate,expected",
[
- ("57P03", True), # cannot_connect_now (server starting up/shutting down)
- ("3D000", False), # invalid_catalog_name — persistent misconfiguration
- ("08P01", False), # protocol_violation — shared with genuine protocol bugs, so message-only
+ (OperationalError, "57P03", True), # cannot_connect_now (server starting up/shutting down)
+ (OperationalError, "3D000", False), # invalid_catalog_name — persistent misconfiguration
+ (OperationalError, "08P01", False), # protocol_violation — shared with genuine protocol bugs, so message-only
+ # read_only_sql_transaction: a primary briefly refusing writes/locks mid-failover. psycopg
+ # classes this under its own InternalError rather than OperationalError.
+ (InternalError, "25006", True),
+ (InternalError, "42601", False), # syntax_error — a real bug, must keep reaching error tracking
],
)
-def test_is_transient_db_error_by_sqlstate(sqlstate: str, expected: bool) -> None:
- error = OperationalError("some driver-specific message")
+def test_is_transient_db_error_by_sqlstate(error_cls: type[Exception], sqlstate: str, expected: bool) -> None:
+ error = error_cls("some driver-specific message")
error.__cause__ = _WithSqlstate(sqlstate)
assert is_transient_db_error(error) is expected
diff --git a/posthog/temporal/tests/common/test_posthog_client_transient_db.py b/posthog/temporal/tests/common/test_posthog_client_transient_db.py
index 23d3b93ca7ef..f6c5d5e8981c 100644
--- a/posthog/temporal/tests/common/test_posthog_client_transient_db.py
+++ b/posthog/temporal/tests/common/test_posthog_client_transient_db.py
@@ -1,7 +1,7 @@
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
-from django.db import InterfaceError, OperationalError
+from django.db import InterfaceError, InternalError, OperationalError
import psycopg.errors
from parameterized import parameterized
@@ -11,8 +11,8 @@ from posthog.dataclasses import frozen
from posthog.temporal.common.posthog_client import _PostHogClientActivityInboundInterceptor
-def _wrapped_by_django(message: str, cause: Exception) -> OperationalError:
- error = OperationalError(message)
+def _wrapped_by_django(message: str, cause: Exception, error_cls: type[Exception] = OperationalError) -> Exception:
+ error = error_cls(message)
error.__cause__ = cause
return error
@@ -55,6 +55,18 @@ class TestTransientDatabaseErrorReporting:
psycopg.errors.AdminShutdown("terminating connection due to administrator command"),
),
),
+ # psycopg classes read_only_sql_transaction (a primary briefly refusing writes mid
+ # failover) under its own InternalError, not OperationalError — Django follows suit.
+ (
+ "read_only_transaction_failover",
+ _wrapped_by_django(
+ "cannot execute SELECT FOR UPDATE in a read-only transaction",
+ psycopg.errors.ReadOnlySqlTransaction(
+ "cannot execute SELECT FOR UPDATE in a read-only transaction"
+ ),
+ error_cls=InternalError,
+ ),
+ ),
]
)
async def test_transient_db_errors_are_not_reported(self, _name, error):