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): stop reporting a failover-induced read-only write as a delta maintenance bug (#88306)

Tom Owersd5b179a4c635
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/errors.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/errors.py
index 158529fa7568..f1ee98c766f6 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/errors.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/errors.py
@@ -1,5 +1,6 @@
-from django.db import InterfaceError, OperationalError
+from django.db import InterfaceError, InternalError, OperationalError
+import psycopg.errors
import botocore.exceptions
import deltalake.exceptions
@@ -144,7 +145,15 @@ def is_transient_maintenance_error(error: BaseException) -> bool:
`is_transient_delta_maintenance_error` above), and app-DB connection blips (DNS, pooler drops) hit
while resolving `job.folder_path()` on a pooled connection — the same `OperationalError`/`InterfaceError`
classification used for this failure class in `repartition_table.py`'s `_is_transient_infra_error`.
+
+ Also covers a primary-DB failover briefly routing the watermark's `select_for_update()` onto a
+ connection that has become a read-only standby: Postgres raises `ReadOnlySqlTransaction`
+ (SQLSTATE 25006) for that, which psycopg classifies under `InternalError` rather than
+ `OperationalError`, so it needs its own check — a bare `InternalError` isinstance check would be
+ too broad and swallow real corruption errors (e.g. `DataCorrupted`) that share the same base class.
"""
if isinstance(error, OperationalError | InterfaceError):
return True
+ if isinstance(error, InternalError) and isinstance(error.__cause__, psycopg.errors.ReadOnlySqlTransaction):
+ return True
return is_transient_object_store_error(error) or is_transient_delta_maintenance_error(error)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_delta_errors.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_delta_errors.py
index 43e93f6fb359..a8cba5db3d11 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_delta_errors.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_delta_errors.py
@@ -1,6 +1,7 @@
-from django.db import InterfaceError, OperationalError
+from django.db import InterfaceError, InternalError, OperationalError
import deltalake
+import psycopg.errors
import botocore.exceptions
from parameterized import parameterized
@@ -14,6 +15,13 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.del
)
+def _internal_error_with_cause(cause: BaseException) -> InternalError:
+ """Mirrors how Django's DatabaseErrorWrapper re-raises a psycopg error: `raise dj_exc_value ... from exc_value`."""
+ error = InternalError("cannot execute SELECT FOR UPDATE in a read-only transaction")
+ error.__cause__ = cause
+ return error
+
+
class TestIsTransientObjectStoreError:
@parameterized.expand(
[
@@ -172,6 +180,23 @@ class TestIsTransientMaintenanceError:
True,
),
("genuine_bug", RuntimeError("maintenance blew up"), False),
+ # A primary failover briefly turns the write connection into a read-only standby mid
+ # watermark-persist — Postgres raises ReadOnlySqlTransaction (25006), which psycopg
+ # classifies under InternalError rather than OperationalError.
+ (
+ "watermark_persist_during_failover",
+ _internal_error_with_cause(
+ psycopg.errors.ReadOnlySqlTransaction("cannot execute SELECT FOR UPDATE in a read-only transaction")
+ ),
+ True,
+ ),
+ # Other InternalError subtypes (e.g. real corruption) must not be swept up by the
+ # ReadOnlySqlTransaction check just because they share the same Django exception class.
+ (
+ "internal_error_unrelated_cause_not_matched",
+ _internal_error_with_cause(psycopg.errors.DataCorrupted("index is corrupted")),
+ False,
+ ),
]
)
def test_classifies_transient_errors(self, _name: str, error: Exception, expected: bool):