: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 the connection-drop probe rollback masking the real error (#78709)
Tom Owers932f9f20ac61
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.pyindex 743442e23733..32e453b6825d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py@@ -2642,12 +2642,17 @@ def _get_table(# statement with QueryCanceled mid-discovery. Session, not LOCAL: the connection is autocommit,# so a LOCAL timeout has nothing to bind to and a scoping transaction's own `BEGIN` would itself# run under the short default. Best-effort: engines without statement_timeout (e.g. DuckDB)- # reject the SET, so clear any aborted transaction and fall back to the default.+ # reject the SET, so clear any aborted transaction and fall back to the default. A genuine+ # connection drop/limit (mirrors `_schemas_from_conn`) fails this statement too, and rolling+ # that back raises a misleading "the connection is lost" that buries the real cause — re-raise+ # instead so the discovery retry recovers on a fresh connection.try:cursor.execute(sql.SQL("SET statement_timeout = {timeout}").format(timeout=sql.Literal(METADATA_STATEMENT_TIMEOUT_MS)))- except psycopg.Error:+ except psycopg.Error as e:+ if _is_connection_dropped_error(e) or _is_connection_limit_error(e):+ raisecursor.connection.rollback()is_mat_view_query = sql.SQL(diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.pyindex 00a89607fd04..938e4b6a2790 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py@@ -6049,6 +6049,24 @@ class TestGetTable:)assert set_timeout_idx < first_probe_idx < info_schema_idx+ @pytest.mark.django_db+ def test_dropped_connection_on_statement_timeout_reraises_without_rollback(self):+ # Before the fix, any `psycopg.Error` on the protective `SET statement_timeout` triggered a+ # blind `cursor.connection.rollback()`. When the error meant the connection itself was+ # dropped, rolling back a dead socket raised a fresh, misleading "the connection is lost"+ # that buried the real cause instead of letting the caller retry on a fresh connection+ # (mirrors the same fix already applied to `_schemas_from_conn`).+ logger = structlog.get_logger()+ cursor = mock.MagicMock()+ drop_error = psycopg.OperationalError("server closed the connection unexpectedly")+ cursor.execute.side_effect = drop_error++ with pytest.raises(psycopg.OperationalError) as exc_info:+ _get_table(cast(Any, cursor), "public", "test_get_table_dropped_conn", logger)++ assert exc_info.value is drop_error+ cursor.connection.rollback.assert_not_called()+@pytest.mark.django_dbdef test_schemas_from_conn_runs_under_scoped_statement_timeout(self):"""`_schemas_from_conn` (the discovery path `sync_new_schemas_activity` and credential