: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): surface leaked transaction nesting as retryable during setup (#70293)
Tom Owerscc7ac40e012b
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 13d14b2b9307..f1a1849d647e 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@@ -410,11 +410,17 @@ def _raise_if_setup_connection_broken(connection: psycopg.Connection) -> None:swallow the follow-up "connection closed" errors, so discovery finishes "successfully"and the implicit commit in the enclosing `with connection:` raises a misleading`ProgrammingError: Explicit commit() forbidden within a Transaction context`, burying- the real cause. Detect the broken connection first and raise the actual- dropped-connection error (transient, so it stays retryable) — the activity then retries- on a fresh connection instead of failing on a self-inflicted commit error.+ the real cause.++ A hard drop flips the connection to `broken`, but a drop that lands precisely while+ psycopg is entering or leaving the probe's `transaction()` block can leave the nesting+ counter incremented while `broken` stays False (the connection is `INERROR`, not BAD).+ `commit()` refuses outright whenever that counter is non-zero, so checking `broken` alone+ misses this case. Detect either signal and raise the actual dropped-connection error+ (transient, so it stays retryable) — the activity then retries on a fresh connection+ instead of failing on the self-inflicted commit error."""- if connection.broken:+ if connection.broken or getattr(connection, "_num_transactions", 0) > 0:raise psycopg.OperationalError("connection to server was lost during table metadata discovery")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 0abfa0b35d55..3db0b2e68217 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@@ -1465,6 +1465,7 @@ class TestRaiseIfSetupConnectionBroken:def test_broken_connection_raises_retryable_dropped_error(self):connection = mock.MagicMock()connection.broken = True+ connection._num_transactions = 0with pytest.raises(psycopg.OperationalError) as exc_info:_raise_if_setup_connection_broken(cast(Any, connection))@@ -1475,11 +1476,27 @@ class TestRaiseIfSetupConnectionBroken:message = str(exc_info.value)assert not any(key in message for key in PostgresSource().get_non_retryable_errors())+ def test_leaked_transaction_counter_raises_retryable_dropped_error(self):+ # A drop while psycopg was entering/leaving a probe's transaction() block leaves the+ # nesting counter incremented without flipping `broken` — the exact state (`INERROR`,+ # not BAD) that made the exit-commit raise the masked ProgrammingError in production.+ connection = mock.MagicMock()+ connection.broken = False+ connection._num_transactions = 1++ with pytest.raises(psycopg.OperationalError) as exc_info:+ _raise_if_setup_connection_broken(cast(Any, connection))++ assert _is_connection_dropped_error(exc_info.value) is True+ message = str(exc_info.value)+ assert not any(key in message for key in PostgresSource().get_non_retryable_errors())+def test_healthy_connection_is_a_noop(self):connection = mock.MagicMock()connection.broken = False+ connection._num_transactions = 0- # A healthy connection must not raise.+ # A healthy connection with no leaked transaction nesting must not raise._raise_if_setup_connection_broken(cast(Any, connection))