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 schema-lag column/table errors as transient (#102603)

Tom Owers8d6c37e2b21b
diff --git a/posthog/temporal/common/db_errors.py b/posthog/temporal/common/db_errors.py
index c92efb60da5b..d516b6478bfa 100644
--- a/posthog/temporal/common/db_errors.py
+++ b/posthog/temporal/common/db_errors.py
@@ -1,6 +1,8 @@
import errno
-from django.db import InterfaceError, InternalError, OperationalError
+from django.db import InterfaceError, InternalError, OperationalError, ProgrammingError
+
+import psycopg.errors
# Substrings identifying transient Postgres failures. pgbouncer kills queries that wait too long
# for a backend connection with `query_wait_timeout`, and surfaces dropped/reset backend
@@ -81,6 +83,15 @@ def is_transient_db_error(error: BaseException) -> bool:
for _ in range(_MAX_CAUSE_CHAIN_DEPTH):
if _is_too_many_open_files_error(error):
return True
+ # SQLSTATE 42703/42P01: a migration adding a column/table and the activity code that reads
+ # it ship in the same deploy, but a worker can roll out ahead of the migration completing.
+ # Every activity here already retries via Temporal's retry policy, and the query succeeds
+ # once the migration lands, so this is a self-healing race, not a bug. Mirrors the
+ # schema-lag handling in batch_consumer.py, loop_retention.py and task_auto_archive.py.
+ if isinstance(error, ProgrammingError) and isinstance(
+ error.__cause__, psycopg.errors.UndefinedColumn | psycopg.errors.UndefinedTable
+ ):
+ return True
if isinstance(error, OperationalError | InterfaceError | InternalError):
sqlstate = getattr(error.__cause__, "sqlstate", None)
if isinstance(sqlstate, str) and (
diff --git a/posthog/temporal/common/test_db_errors.py b/posthog/temporal/common/test_db_errors.py
index a773db36bb13..edf3b10313a2 100644
--- a/posthog/temporal/common/test_db_errors.py
+++ b/posthog/temporal/common/test_db_errors.py
@@ -2,8 +2,9 @@ import errno
import pytest
-from django.db import InterfaceError, InternalError, OperationalError
+from django.db import InterfaceError, InternalError, OperationalError, ProgrammingError
+import psycopg.errors
from temporalio.exceptions import ApplicationError
from posthog.temporal.common.db_errors import is_transient_db_error
@@ -120,3 +121,20 @@ def test_is_transient_db_error_by_sqlstate(error_cls: type[Exception], sqlstate:
error = error_cls("some driver-specific message")
error.__cause__ = _WithSqlstate(sqlstate)
assert is_transient_db_error(error) is expected
+
+
+ "cause,expected",
+ [
+ # A migration adding a column and the activity code reading it ship in the same deploy;
+ # a worker rolling out ahead of the migration completing gets this from Postgres.
+ (psycopg.errors.UndefinedColumn("column posthog_externaldataschema.auto_disabled_at does not exist"), True),
+ (psycopg.errors.UndefinedTable('relation "sourcebatch" does not exist'), True),
+ # A real bug (e.g. a typo in raw SQL) must keep reaching error tracking.
+ (psycopg.errors.SyntaxErrorOrAccessRuleViolation("syntax error"), False),
+ ],
+)
+def test_is_transient_db_error_for_schema_lag(cause: Exception, expected: bool) -> None:
+ error = ProgrammingError(str(cause))
+ error.__cause__ = cause
+ assert is_transient_db_error(error) is expected