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): treat invalid object name (error 208) as non-retryable (#64026)

Tom Owers756ef0565422
diff --git a/posthog/temporal/data_imports/sources/mssql/source.py b/posthog/temporal/data_imports/sources/mssql/source.py
index 2dd7a9d0cb24..24fc31a273cb 100644
--- a/posthog/temporal/data_imports/sources/mssql/source.py
+++ b/posthog/temporal/data_imports/sources/mssql/source.py
@@ -55,6 +55,15 @@ class MSSQLSource(SQLSource[MSSQLSourceConfig], SSHTunnelMixin, ValidateDatabase
# same login can never succeed. Match the stable message text, not the object/database
# names that follow it.
"The SELECT permission was denied on the object": "Your SQL Server login doesn't have permission to read one of the tables or views being synced. Grant it SELECT access (for example via the db_datareader role or an explicit GRANT SELECT) on the objects you want to import, then re-enable the sync.",
+ # SQL Server error 208 — the SELECT we run during the sync references an object the
+ # server can't resolve. Either the table/view we're syncing was dropped or renamed
+ # after schema discovery, or (as seen in practice) the view we select from has a body
+ # that references another object the login can't reach (a cross-database table, or one
+ # the login lost permission to). Our query is built from validated identifiers
+ # discovered via information_schema, so this is the customer's schema/permissions, not
+ # a momentary blip — retrying replays the identical 208. Match the stable error text,
+ # not the volatile object name / procedure / line number in the rest of the message.
+ "Invalid object name": "One of the tables or views you're syncing references a database object that no longer exists or that this login can't access (SQL Server error 208). Check that the object still exists and that the connection user has permission to read it (including any tables a view depends on), then re-sync.",
"Cannot find the CREDENTIAL": "Cannot find the credential - check that it exists and you have permission to access it",
# Raised by the `sshtunnel` library (via the shared `open_ssh_tunnel` helper) when the
# SSH tunnel can't be brought up — the bastion host is unreachable, the host/port is
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 d6c8a142da32..bae3917f6329 100644
--- a/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
+++ b/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
@@ -498,3 +498,19 @@ class TestMSSQLSourceNonRetryableErrors:
def test_permission_denied_errors_are_non_retryable(self, error_msg):
non_retryable = MSSQLSource().get_non_retryable_errors()
assert any(pattern in error_msg for pattern in non_retryable.keys()), error_msg
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Real pymssql MSSQLDatabaseException for SQL Server error 208 raised mid-sync when the
+ # view being selected references an object the login can't resolve.
+ "SQL Server message 208, severity 16, state 1, procedure b'VentasAsesorMes', line 8: "
+ "b\"Invalid object name 'Imagiq.dbo.inv_cuedoc'.DB-Lib error message 20018, severity 16:\\n"
+ 'General SQL Server error: Check messages from the SQL Server\\n"',
+ # The table being synced was dropped/renamed after schema discovery.
+ "Invalid object name 'dbo.orders'.",
+ ],
+ )
+ def test_invalid_object_name_is_non_retryable(self, error_msg):
+ non_retryable = MSSQLSource().get_non_retryable_errors()
+ assert any(pattern in error_msg for pattern in non_retryable.keys()), error_msg