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 SELECT permission denied as non-retryable (#64023)

Tom Owersf34b7fae48ba
diff --git a/posthog/temporal/data_imports/sources/mssql/source.py b/posthog/temporal/data_imports/sources/mssql/source.py
index 237127164f0f..2dd7a9d0cb24 100644
--- a/posthog/temporal/data_imports/sources/mssql/source.py
+++ b/posthog/temporal/data_imports/sources/mssql/source.py
@@ -49,6 +49,12 @@ class MSSQLSource(SQLSource[MSSQLSourceConfig], SSHTunnelMixin, ValidateDatabase
# hostname is wrong), not a momentary blip, so retrying the job won't recover it.
"Adaptive Server is unavailable or does not exist": "Could not reach your SQL Server. Check that the server is running and reachable, and that PostHog's IP addresses are allowed through its firewall / security group.",
"Login failed for user": None,
+ # SQL Server error 229 — the login PostHog connects with was authenticated but lacks
+ # SELECT permission on a table/view being synced. This is a server-side GRANT the
+ # customer has to make (db_datareader or an explicit GRANT SELECT); retrying with the
+ # 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.",
"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 08a8b57e0647..d6c8a142da32 100644
--- a/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
+++ b/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
@@ -482,3 +482,19 @@ class TestMSSQLSourceNonRetryableErrors:
def test_connection_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 229 on a table.
+ "SQL Server message 229, severity 14, state 5, procedure b'', line 1:\n"
+ "b\"The SELECT permission was denied on the object 'ExistenciasProductoMagiQ', "
+ "database 'VirtualMedios', schema 'dbo'.DB-Lib error message 20018, severity 14:\n"
+ 'General SQL Server error: Check messages from the SQL Server\n"',
+ # Different object/database names must still match the stable substring.
+ "The SELECT permission was denied on the object 'zzz_segtieint', database 'VirtualMedios', schema 'dbo'.",
+ ],
+ )
+ 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