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(temporal): treat a postgres deadlock as a transient db error (#100748)

Tom Owers2a79eb2bc63e
diff --git a/posthog/temporal/common/db_errors.py b/posthog/temporal/common/db_errors.py
index e89058060647..c2980da6e9f8 100644
--- a/posthog/temporal/common/db_errors.py
+++ b/posthog/temporal/common/db_errors.py
@@ -49,7 +49,11 @@ _TRANSIENT_SQLSTATE_PREFIXES = ("57P",)
# rather than by class prefix, because SQLSTATE class 25 (invalid transaction state) also covers
# codes that are real transaction-handling bugs, not infra hiccups. psycopg raises this under
# InternalError, not OperationalError, hence the wider isinstance check below.
-_TRANSIENT_SQLSTATES = ("25006",)
+#
+# deadlock_detected: Postgres picked one side of a lock-ordering race and rolled back our
+# transaction so the other side could proceed. The query itself isn't at fault, and retrying
+# resolves it because the race that caused it essentially never repeats identically.
+_TRANSIENT_SQLSTATES = ("25006", "40P01")
def _is_too_many_open_files_error(error: BaseException) -> bool:
diff --git a/posthog/temporal/common/test_db_errors.py b/posthog/temporal/common/test_db_errors.py
index d145b3eab050..29b66a679707 100644
--- a/posthog/temporal/common/test_db_errors.py
+++ b/posthog/temporal/common/test_db_errors.py
@@ -67,6 +67,7 @@ def test_is_transient_db_error_by_message(error: BaseException, expected: bool)
# 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),
+ (OperationalError, "40P01", True), # deadlock_detected — a lock-ordering race, retry resolves it
],
)
def test_is_transient_db_error_by_sqlstate(error_cls: type[Exception], sqlstate: str, expected: bool) -> None: