: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): retry recovery conflicts hit while connecting for offset chunking (#64676)
Tom Owers7d7870338d3e
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.pyindex 9e7efa83774e..32476ba39fd7 100644--- a/posthog/temporal/data_imports/sources/postgres/postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py@@ -191,15 +191,16 @@ _CONNECTION_DROPPED_ERROR_TYPES = ()-def _safe_close_connection(connection: psycopg.Connection) -> None:+def _safe_close_connection(connection: psycopg.Connection | None) -> None:"""Close a connection without raising.Prefer this over Connection.__exit__ for teardown in exception handlers:__exit__ attempts a commit/rollback first, which can itself raise on abroken connection and mask the original error. close() just releases the- socket.+ socket. Accepts None so callers can close a connection that was never opened+ (a connect that raised before assigning) without a None-check at each site."""- if connection.closed:+ if connection is None or connection.closed:returntry:connection.close()@@ -2563,18 +2564,22 @@ def postgres_source(successive_errors = 0successive_conn_errors = 0floor_retries = 0- connection = _connect_with_dropped_retry(get_connection, logger)- # Autocommit so each LIMIT/OFFSET query runs as its own statement- # and no transaction stays open across the slow delta-merge that- # happens between yields. A held transaction is what gets the- # backend culled by idle_in_transaction_session_timeout, producing- # the "server conn crashed?" ProtocolViolation on the next fetch.- connection.autocommit = True+ # Open lazily inside the loop so a recovery conflict (or connection drop) raised by+ # the connect itself is caught by the handlers below. A hot standby can cancel the+ # connection's own startup with "conflict with recovery" (SerializationFailure) when+ # we reconnect mid-recovery — opening outside the loop let that escape the whole+ # fallback even though it's the same transient condition the loop already retries.+ connection: psycopg.Connection | None = Nonewhile True:try:- if connection.closed:- logger.debug("Postgres connection was closed, reopening...")+ if connection is None or connection.closed:+ logger.debug("Opening Postgres connection for offset chunking...")connection = get_connection()+ # Autocommit so each LIMIT/OFFSET query runs as its own statement and no+ # transaction stays open across the slow delta-merge that happens between+ # yields. A held transaction is what gets the backend culled by+ # idle_in_transaction_session_timeout, producing the "server conn+ # crashed?" ProtocolViolation on the next fetch.connection.autocommit = True# Use psycopg.Cursor directly to bypass cursor_factory: on adiff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex 94ead0affffc..e1a0c20517c4 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -83,6 +83,7 @@ from posthog.temporal.data_imports.sources.postgres.postgres import (_recovery_conflict_abort_error,_rls_active_from_conn,_role_subject_to_rls,+ _safe_close_connection,_statement_timeout_as_non_retryable,filter_postgres_incremental_fields,get_foreign_keys,@@ -1245,6 +1246,154 @@ class TestServerCursorStatementTimeout:assert expected_substr in str(exc_info.value)+class TestOffsetChunkingConnectRecoveryConflict:+ """A recovery conflict raised by the connect itself in the offset-chunking fallback — a hot+ standby cancelling the new connection's startup with "conflict with recovery" — must be retried+ in-process, not escape `get_rows`. The bootstrap connect used to run outside the retry loop, so+ the conflict bypassed the loop's recovery-conflict handler and failed the whole sync.+ """++ _RECOVERY_CONFLICT = "canceling statement due to conflict with recovery"++ class _NamedCursor:+ def __init__(self):+ col = mock.Mock()+ col.name = "id"+ self.description = [col]++ def execute(self, *args, **kwargs):+ # The initial server-cursor read on a read replica hits the recovery conflict, which+ # routes get_rows into the offset-chunking fallback.+ raise psycopg.errors.SerializationFailure("canceling statement due to conflict with recovery")++ def fetchmany(self, _n):+ return []++ def __enter__(self):+ return self++ def __exit__(self, *args):+ return False++ class _OffsetCursor:+ # Unnamed client cursor used by the offset-chunking path; yields no rows so the loop ends.+ def __init__(self):+ col = mock.Mock()+ col.name = "id"+ self.description = [col]++ def execute(self, *args, **kwargs):+ return None++ def fetchall(self):+ return []++ def __enter__(self):+ return self++ def __exit__(self, *args):+ return False++ class _Connection:+ def __init__(self):+ self.autocommit = False+ self.closed = False+ self.broken = False+ self.adapters = mock.Mock()++ def cursor(self, *args, **kwargs):+ if "name" in kwargs:+ return TestOffsetChunkingConnectRecoveryConflict._NamedCursor()+ # Unnamed setup cursor (metadata probes are patched out) stays benign.+ return mock.MagicMock()++ def commit(self):+ return None++ def close(self):+ self.closed = True++ def __enter__(self):+ return self++ def __exit__(self, *args):+ return False++ def test_recovery_conflict_on_offset_chunking_connect_is_retried_in_process(self):+ @contextmanager+ def fake_tunnel():+ yield ("localhost", 5432)++ fake_table = mock.Mock()+ fake_table.to_arrow_schema.return_value = pa.schema([pa.field("id", pa.int64())])+ fake_table.type = "table"+ fake_table.columns = []+ fake_table.__contains__ = mock.Mock(return_value=False)++ connection = self._Connection()+ connect_calls = {"n": 0}++ def connect_side_effect(*args, **kwargs):+ connect_calls["n"] += 1+ # Calls 1 (setup) and 2 (initial server-cursor read) succeed; the offset-chunking+ # bootstrap connect hits the recovery conflict twice before succeeding.+ if connect_calls["n"] in (3, 4):+ raise psycopg.errors.SerializationFailure(self._RECOVERY_CONFLICT)+ return connection++ module = "posthog.temporal.data_imports.sources.postgres.postgres"+ with (+ patch(f"{module}.psycopg.connect", side_effect=connect_side_effect) as connect_mock,+ patch(f"{module}.psycopg.Cursor", side_effect=lambda _conn: self._OffsetCursor()),+ patch(f"{module}._get_table", return_value=fake_table),+ patch(f"{module}._is_read_replica", return_value=True),+ patch(f"{module}._get_primary_keys", return_value=["id"]),+ patch(f"{module}._is_partitioned_table", return_value=False),+ patch(f"{module}._get_table_chunk_size", return_value=1000),+ patch(f"{module}._get_rows_to_sync", return_value=10),+ patch(f"{module}._role_subject_to_rls", return_value=False),+ patch(f"{module}._get_partition_settings", return_value=None),+ patch(f"{module}.time.sleep"),+ ):+ response = postgres_source(+ tunnel=lambda: fake_tunnel(),+ user="u",+ password="p",+ database="db",+ sslmode="prefer",+ schema="public",+ table_names=["companies"],+ should_use_incremental_field=False,+ logger=structlog.get_logger(),+ db_incremental_field_last_value=None,+ team_id=1,+ )+ # Before the fix the connect-time conflict escaped offset_chunking and raised here.+ list(cast(Iterable[Any], response.items()))++ # 1 setup + 1 initial read + 3 offset-chunking connects (2 conflicts + 1 success).+ assert connect_mock.call_count == 5+++class TestSafeCloseConnection:+ def test_none_is_a_noop(self):+ # The offset-chunking path can reach a teardown handler with no connection (a connect that+ # raised before assigning), so closing None must not raise.+ _safe_close_connection(None)++ def test_already_closed_is_a_noop(self):+ connection = mock.Mock()+ connection.closed = True+ _safe_close_connection(connection)+ connection.close.assert_not_called()++ def test_open_connection_is_closed(self):+ connection = mock.Mock()+ connection.closed = False+ _safe_close_connection(connection)+ connection.close.assert_called_once()++class TestPostgresSourceForPipelineSchemaResolution:@pytest.fixturedef source(self):