Skip to content

: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.

fix(mssql): stop retrying a dead connection when fetching table stats (#88618)

Tom Owers429758280486
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/mssql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/mssql.py
index 2a338eb69934..d9ae7570bdfe 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/mssql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/mssql.py
@@ -719,7 +719,12 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Con
cursor.execute(
"EXEC sp_spaceused %(full_table_name)s, @updateusage = 'TRUE'", {"full_table_name": full_table_name}
)
- except Exception:
+ except Exception as e:
+ # A dead connection (DB-Lib 20047) needs a fresh connection, not a retry on the
+ # same dead cursor — retrying here raises a confusing secondary InterfaceError
+ # ("Not connected to any MS SQL server") instead of the real, transient cause.
+ if isinstance(e, pymssql.Error) and _is_transient_connection_error(e):
+ raise
# If @updateusage parameter fails, try the older version
cursor.execute("EXEC sp_spaceused %(full_table_name)s", {"full_table_name": full_table_name})
@@ -762,6 +767,12 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Con
total_bytes = int(size_value * multiplier)
return TableStats(table_size_bytes=total_bytes, row_count=total_rows)
except Exception as e:
+ # A transient connection death recovers on the next sync attempt with a fresh
+ # connection (see `retry_on_transient_connection_error`); table stats are best-effort,
+ # so skip capturing this known, self-recovering error as tracked noise.
+ if isinstance(e, pymssql.Error) and _is_transient_connection_error(e):
+ logger.debug(f"fetch_table_stats: transient MSSQL connection death, returning None: {e}")
+ return None
logger.debug(f"fetch_table_stats: Error: {e}. Returning None", exc_info=e)
capture_exception(e)
return None
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql.py
index 4a5c0f2c32f5..87411f4a3aba 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql.py
@@ -402,6 +402,21 @@ class TestFetchTableStats:
cursor.execute.side_effect = RuntimeError("boom")
assert impl.fetch_table_stats(cursor, "dbo", "t", logger) is None
+ def test_transient_connection_death_skips_retry_and_capture(self, impl, cursor, logger, mocker):
+ # A dead connection (DB-Lib 20047) must not retry sp_spaceused on the same dead cursor
+ # (which raises a confusing secondary InterfaceError) or get captured as tracked noise —
+ # it's the same self-recovering error `retry_on_transient_connection_error` already
+ # handles at the schema-discovery layer.
+ capture = mocker.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.mssql.mssql.capture_exception"
+ )
+ cursor.execute.side_effect = pymssql.OperationalError(
+ 20047, b"DB-Lib error message 20047, severity 9:\nDBPROCESS is dead or not enabled\n"
+ )
+ assert impl.fetch_table_stats(cursor, "dbo", "t", logger) is None
+ assert cursor.execute.call_count == 1
+ capture.assert_not_called()
+
class TestFetchAverageRowSize:
"""MSSQL's `fetch_average_row_size` samples a separate `TOP 100` query