: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(postgres): recover from idle-in-transaction backend culls (#63758)
Tom Owers6ab00679e3a9
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.pyindex f384e065b281..d9ca04721531 100644--- a/posthog/temporal/data_imports/sources/postgres/postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py@@ -128,6 +128,19 @@ _CONNECTION_DROPPED_ERROR_SUBSTRINGS = ("terminating connection due to",)+# Exception types that can carry a connection-dropped error. ProtocolViolation is+# PgBouncer's synthetic error packet; OperationalError is libpq detecting the dead+# socket. IdleInTransactionSessionTimeout (SQLSTATE 25P03) is what Postgres raises+# when the source's idle_in_transaction_session_timeout culls our backend while a+# server-side cursor holds a transaction open across the slow delta-merge between+# yields — psycopg maps it to InternalError, not OperationalError, so it must be+# named explicitly or the type-based catch below would miss it.+_CONNECTION_DROPPED_ERROR_TYPES = (+ psycopg.errors.ProtocolViolation,+ psycopg.OperationalError,+ psycopg.errors.IdleInTransactionSessionTimeout,+)+def _safe_close_connection(connection: psycopg.Connection) -> None:"""Close a connection without raising.@@ -151,7 +164,13 @@ def _is_connection_dropped_error(error: BaseException) -> bool:psycopg surfaces these as ProtocolViolation (PgBouncer's synthetic errorpacket) or OperationalError (libpq detecting the dead socket), so we match ontype and message rather than a single SQLSTATE.++ IdleInTransactionSessionTimeout is the exception: it carries SQLSTATE 25P03 and+ unambiguously means the source terminated our backend for holding a transaction+ open too long, so the type alone is enough — no message match required."""+ if isinstance(error, psycopg.errors.IdleInTransactionSessionTimeout):+ return Trueif isinstance(error, psycopg.errors.ProtocolViolation | psycopg.OperationalError):message = " ".join(str(arg) for arg in error.args).lower()return any(substring in message for substring in _CONNECTION_DROPPED_ERROR_SUBSTRINGS)@@ -193,7 +212,7 @@ def _connect_with_dropped_retry(while True:try:return connect()- except (psycopg.errors.ProtocolViolation, psycopg.OperationalError) as e:+ except _CONNECTION_DROPPED_ERROR_TYPES as e:if not _is_connection_dropped_error(e):raiseattempt += 1@@ -2334,7 +2353,7 @@ def postgres_source(if timeout_error is not None:raise timeout_error from eraise- except (psycopg.errors.ProtocolViolation, psycopg.OperationalError) as e:+ except _CONNECTION_DROPPED_ERROR_TYPES as e:if not _is_connection_dropped_error(e):_safe_close_connection(connection)raise@@ -2467,7 +2486,7 @@ def postgres_source(returnraise- except (psycopg.errors.ProtocolViolation, psycopg.OperationalError) as e:+ except _CONNECTION_DROPPED_ERROR_TYPES as e:# The server cursor holds a transaction open across the slow# delta-merge between yields; the source can cull the backend# (idle_in_transaction_session_timeout / PgBouncer) and the nextdiff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex 9da578fa749e..79cb03c24289 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -454,6 +454,11 @@ class TestIsConnectionDroppedError:psycopg.OperationalError("consuming input failed: EOF detected"),psycopg.OperationalError("terminating connection due to administrator command"),psycopg.errors.ProtocolViolation("SERVER CONN CRASHED?"),+ # SQLSTATE 25P03: the source's idle_in_transaction_session_timeout culled our+ # backend mid-stream. psycopg maps this to InternalError, not OperationalError,+ # so it's detected by type alone — even with no message to match on.+ psycopg.errors.IdleInTransactionSessionTimeout("terminating connection due to idle-in-transaction timeout"),+ psycopg.errors.IdleInTransactionSessionTimeout(),],)def test_connection_dropped_errors_are_detected(self, error):