: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): re-raise connection errors from the duplicate-PK probe (#65068)
Tom Owersefc3d19b04af
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.pyindex d4483baa5163..2ef8cd73847b 100644--- a/posthog/temporal/data_imports/sources/postgres/postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py@@ -1831,6 +1831,12 @@ def _has_duplicate_primary_keys(return row is not Noneexcept psycopg.errors.QueryCanceled:raise+ except psycopg.OperationalError:+ # A connection-level failure here (e.g. a foreign-data-wrapper server refusing a new+ # connection with "too many connections") means the probe never ran — swallowing it as+ # "no duplicate keys" would be a false negative. Propagate it so the activity's retry+ # path handles it; these are transient and stay retryable.+ raiseexcept Exception as e:capture_exception(e)return Falsediff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex 71699849d4d6..cef4ac188951 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -4088,6 +4088,31 @@ class TestHasDuplicatePrimaryKeys:result = _has_duplicate_primary_keys(cast(Any, dj_cursor), "public", "test_dup_table", ["id"], logger)assert result is True+ def test_reraises_connection_errors_without_capturing(self):+ logger = structlog.get_logger()+ cursor = MagicMock()+ # postgres_fdw surfaces a saturated foreign server while executing the probe query: the+ # remote connection couldn't be established, so the probe never ran. This must propagate+ # (transient, stays retryable), not be swallowed as "no duplicate keys" + captured as noise.+ cursor.execute.side_effect = psycopg.errors.SqlclientUnableToEstablishSqlconnection(+ 'could not connect to server "posthog_fdw_payment"\n'+ 'DETAIL: connection to server at "10.0.0.1", port 5432 failed: '+ 'FATAL: too many connections for role "posthog_fdw_reader"'+ )+ with patch("posthog.temporal.data_imports.sources.postgres.postgres.capture_exception") as mock_capture:+ with pytest.raises(psycopg.OperationalError):+ _has_duplicate_primary_keys(cast(Any, cursor), "public", "orders", ["id"], logger)+ mock_capture.assert_not_called()++ def test_captures_and_returns_false_on_non_connection_error(self):+ logger = structlog.get_logger()+ cursor = MagicMock()+ cursor.execute.side_effect = psycopg.errors.UndefinedColumn('column "id" does not exist')+ with patch("posthog.temporal.data_imports.sources.postgres.postgres.capture_exception") as mock_capture:+ result = _has_duplicate_primary_keys(cast(Any, cursor), "public", "orders", ["id"], logger)+ assert result is False+ mock_capture.assert_called_once()+class TestIsReadReplica:@pytest.mark.django_db