: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): stop alerting on read-replica recovery conflicts in partition sizing (#63771)
Tom Owers7bae62bff46b
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.pyindex 982a9350c4b0..2f5d4a4523ae 100644--- a/posthog/temporal/data_imports/sources/postgres/postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py@@ -145,6 +145,21 @@ def _is_connection_dropped_error(error: BaseException) -> bool:return False+def _is_recovery_conflict_error(error: BaseException) -> bool:+ """True if `error` is a Postgres standby recovery conflict.++ A read replica cancels an in-flight query when WAL replay needs to remove row+ versions the query might still read ("canceling statement due to conflict with+ recovery", SQLSTATE 40001 -> psycopg SerializationFailure). It's transient and+ expected on replicas — the row-streaming reader already retries it in-process and+ `get_non_retryable_errors` deliberately keeps the raw message retryable — so+ best-effort probes should degrade quietly rather than alert on a handled condition.+ """+ return isinstance(error, psycopg.errors.SerializationFailure) and "conflict with recovery" in " ".join(+ str(arg) for arg in error.args+ )++def _connect_with_dropped_retry(connect: Callable[[], psycopg.Connection],logger: FilteringBoundLogger,@@ -1408,7 +1423,12 @@ def _get_partition_settings(except psycopg.errors.QueryCanceled:raiseexcept Exception as e:- capture_exception(e)+ # A read replica can cancel this best-effort sizing query with a recovery conflict; it's+ # transient (the row-streaming reader retries it in-process) and expected on replicas, so+ # degrade quietly instead of flooding error tracking with a handled condition. Genuinely+ # unexpected failures are still captured.+ if not _is_recovery_conflict_error(e):+ capture_exception(e)logger.debug(f"_get_partition_settings: returning None due to error: {e}")return Nonediff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex 468a08f5b859..2445a73e4312 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -59,6 +59,7 @@ from posthog.temporal.data_imports.sources.postgres.postgres import (_is_connection_dropped_error,_is_partitioned_table,_is_read_replica,+ _is_recovery_conflict_error,_is_unsupported_function_error,_normalize_function_names,_rls_active_from_conn,@@ -1746,6 +1747,51 @@ class TestGetPartitionSettings:mock_partitioned.assert_called_once()assert result is sentinel+ def test_recovery_conflict_returns_none_without_capturing(self):+ # Regression: a read-replica recovery conflict cancels the best-effort sizing query.+ # It's transient and the row-streaming reader retries it in-process, so degrade to None+ # without flooding error tracking with a handled condition.+ logger = structlog.get_logger()+ cursor = mock.MagicMock()+ cursor.execute.side_effect = psycopg.errors.SerializationFailure(+ "canceling statement due to conflict with recovery"+ )++ with patch("posthog.temporal.data_imports.sources.postgres.postgres.capture_exception") as capture_mock:+ result = _get_partition_settings(cast(Any, cursor), "public", "t", logger, is_partitioned=False)++ assert result is None+ capture_mock.assert_not_called()++ def test_unexpected_error_is_still_captured(self):+ # A genuinely unexpected failure on the sizing query must still surface to error tracking.+ logger = structlog.get_logger()+ cursor = mock.MagicMock()+ cursor.execute.side_effect = Exception("some unexpected failure")++ with patch("posthog.temporal.data_imports.sources.postgres.postgres.capture_exception") as capture_mock:+ result = _get_partition_settings(cast(Any, cursor), "public", "t", logger, is_partitioned=False)++ assert result is None+ capture_mock.assert_called_once()+++class TestIsRecoveryConflictError:+ @pytest.mark.parametrize(+ "error,expected",+ [+ (psycopg.errors.SerializationFailure("canceling statement due to conflict with recovery"), True),+ (psycopg.errors.SerializationFailure("could not serialize access due to conflict with recovery"), True),+ # A non-recovery serialization failure (true 40001 write conflict) is not ours to swallow.+ (psycopg.errors.SerializationFailure("could not serialize access due to concurrent update"), False),+ # Right message, wrong type -> not a recovery conflict we recognise here.+ (psycopg.errors.QueryCanceled("canceling statement due to conflict with recovery"), False),+ (Exception("canceling statement due to conflict with recovery"), False),+ ],+ )+ def test_recognises_recovery_conflict(self, error, expected):+ assert _is_recovery_conflict_error(error) is expected+class TestPostgreSQLColumnToArrowField:@pytest.mark.parametrize(