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 a read-only writer as a transient DB error (#88296)

Tom Owers966292729ff7
diff --git a/posthog/temporal/common/test_db_errors.py b/posthog/temporal/common/test_db_errors.py
index 417c1c108ed2..750580df7ccc 100644
--- a/posthog/temporal/common/test_db_errors.py
+++ b/posthog/temporal/common/test_db_errors.py
@@ -47,30 +47,19 @@ def test_is_transient_db_error_by_message(error: BaseException, expected: bool)
[
(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.
+ (OperationalError, "08P01", False), # protocol_violation — shared with genuine protocol bugs
+ # read_only_sql_transaction: Django wraps psycopg's ReadOnlySqlTransaction as InternalError,
+ # not OperationalError/InterfaceError — a primary/replica failover briefly rejects writes
+ # with this exact SQLSTATE until promotion completes. Exercises the class-independent path.
(InternalError, "25006", True),
(InternalError, "42601", False), # syntax_error — a real bug, must keep reaching error tracking
+ # in_failed_sql_transaction shares class 25 with the code above but means a prior statement
+ # in the same transaction already failed — a real defect, not a self-healing infra blip.
+ # Guards against widening the match to the whole class-25 prefix instead of the exact code.
+ (InternalError, "25P02", False),
],
)
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
-
-
-def test_is_transient_db_error_for_read_only_transaction_failover() -> None:
- # psycopg raises this under InternalError, not OperationalError — a primary/replica failover
- # briefly rejects writes with this exact SQLSTATE until promotion completes.
- error = InternalError("cannot execute INSERT in a read-only transaction")
- error.__cause__ = _WithSqlstate("25006")
- assert is_transient_db_error(error) is True
-
-
-def test_is_transient_db_error_rejects_other_internal_errors() -> None:
- # Class 25 (invalid transaction state) has other codes that are real bugs, not infra hiccups —
- # only the exact read-only-transaction code should be treated as transient.
- error = InternalError("current transaction is aborted")
- error.__cause__ = _WithSqlstate("25P02")
- assert is_transient_db_error(error) is False