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(postgres): give schema-permission denials a USAGE-oriented message (#67660)

Tom Owers1949fb0d8eb1
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
index 86facc699ae8..20e959c7488d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
@@ -361,6 +361,19 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
"crypto_aead_det_decrypt). Grant the connecting role EXECUTE on that function, or remove the "
"view that uses it from the sync, then re-enable the sync."
),
+ # A selected table lives in a schema the connecting role can't access (SQLSTATE 42501,
+ # "permission denied for schema <name>") — most often a non-public schema like `extensions`
+ # holding an extension's objects. USAGE on the schema is a prerequisite for reading anything
+ # inside it, so granting SELECT on the table alone won't help — distinct from the table/view
+ # SELECT denial below, and it must precede the generic "permission denied for" so this
+ # USAGE-oriented message is the one selected.
+ "permission denied for schema": (
+ "PostHog's database role isn't allowed to access a schema that contains one or more of the "
+ 'tables you selected to sync (PostgreSQL reported "permission denied for schema"). Grant the '
+ "connecting role USAGE on that schema and SELECT on the tables in it (for example: "
+ "GRANT USAGE ON SCHEMA <schema> TO <role>), or remove those tables from the sync, then "
+ "re-enable the sync."
+ ),
"permission denied for": (
"PostHog's database role isn't allowed to read one or more of the tables you selected to sync "
'(PostgreSQL reported "permission denied"). Grant the connecting role SELECT on those tables '
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.py
index a6a6c1891088..8181248e4a71 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
@@ -716,6 +716,30 @@ class TestPostgresSourceNonRetryableErrors:
assert "EXECUTE" in friendly[0]
assert "GRANT SELECT" not in friendly[0]
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Raw psycopg message (what the activity-level check sees via str(e)).
+ "permission denied for schema extensions",
+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.
+ "InsufficientPrivilege: permission denied for schema extensions",
+ ],
+ )
+ def test_permission_denied_for_schema_errors_are_non_retryable(self, source, error_msg):
+ non_retryable = source.get_non_retryable_errors()
+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
+ assert is_non_retryable, f"Permission-denied-for-schema error should be non-retryable: {error_msg}"
+
+ def test_permission_denied_for_schema_returns_usage_message(self, source):
+ non_retryable = source.get_non_retryable_errors()
+ error_msg = "permission denied for schema extensions"
+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]
+ assert friendly, "Permission-denied-for-schema error should surface an actionable message"
+ # The schema-USAGE message must win over the generic table-SELECT message and advise USAGE
+ # rather than the misleading "GRANT SELECT ON <table>".
+ assert "USAGE" in friendly[0]
+ assert "GRANT SELECT" not in friendly[0]
+
@pytest.mark.parametrize(
"error_msg",
[