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): handle NULL row count from sp_spaceused on views (#63687)

Tom Owers9eb721e30435
diff --git a/posthog/temporal/data_imports/sources/mssql/mssql.py b/posthog/temporal/data_imports/sources/mssql/mssql.py
index a41522516ea8..16e0c8eedbe6 100644
--- a/posthog/temporal/data_imports/sources/mssql/mssql.py
+++ b/posthog/temporal/data_imports/sources/mssql/mssql.py
@@ -598,6 +598,13 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Con
# sp_spaceused returns: name, rows, reserved, data, index_size, unused
_, total_rows, _, data_size, _, _ = result
+ # Views (and other storage-less objects) return NULL for rows/data
+ # from sp_spaceused. Treat that as "no stats available" and fall
+ # back to safe defaults rather than crashing on int(None).
+ if total_rows is None or data_size is None:
+ logger.debug("fetch_table_stats: sp_spaceused returned NULL row count or data size")
+ return None
+
total_rows = int(total_rows)
# Parse size with unit (e.g. "1024.45 MB" -> 1024.45, "MB")
diff --git a/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py b/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
index 4d1133f1f7cc..d3caf630acc6 100644
--- a/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
+++ b/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
@@ -302,6 +302,14 @@ class TestFetchTableStats:
cursor.fetchone.return_value = ("t", "1000", "40 ZB", "32 ZB", "8 ZB", "0 ZB")
assert impl.fetch_table_stats(cursor, "dbo", "t", logger) is None
+ def test_returns_none_when_view_returns_null_stats(self, impl, cursor, logger, mocker):
+ # sp_spaceused on a view returns NULL for rows/reserved/data. This must be a graceful
+ # skip — not an int(None) crash routed through capture_exception (which floods error tracking).
+ capture = mocker.patch("posthog.temporal.data_imports.sources.mssql.mssql.capture_exception")
+ cursor.fetchone.return_value = ("vw_thing", None, None, None, "0 KB", "0 KB")
+ assert impl.fetch_table_stats(cursor, "dbo", "vw_thing", logger) is None
+ capture.assert_not_called()
+
def test_returns_none_on_exception(self, impl, cursor, logger):
cursor.execute.side_effect = RuntimeError("boom")
assert impl.fetch_table_stats(cursor, "dbo", "t", logger) is None