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 accurate diagnosis for function-permission denials (#64694)

Tom Owers689878b5730d
diff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.py
index fd5af3b8085c..34c19d48b266 100644
--- a/posthog/temporal/data_imports/sources/postgres/source.py
+++ b/posthog/temporal/data_imports/sources/postgres/source.py
@@ -300,6 +300,18 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
# Match that message substring so the role-lacks-SELECT case is caught at both layers
# and we stop retrying instead of re-reading into the same denial every attempt.
"InsufficientPrivilege": None,
+ # A view selected for sync calls a function the connecting role can't execute (SQLSTATE
+ # 42501) — most often a view that decrypts secrets via Supabase Vault / pgsodium
+ # (`crypto_aead_det_decrypt`). Distinct from the table/view SELECT denial below: granting
+ # SELECT won't help, so it needs its own EXECUTE-oriented message and must precede the
+ # generic "permission denied for" so that message is the one selected.
+ "permission denied for function": (
+ "PostHog's database role isn't allowed to execute a function used by one or more of the "
+ 'tables or views you selected to sync (PostgreSQL reported "permission denied for function"). '
+ "This often happens when a view decrypts secrets (for example Supabase Vault's "
+ "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."
+ ),
"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/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index 2bac406f4e60..94ead0affffc 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -527,6 +527,36 @@ class TestPostgresSourceNonRetryableErrors:
assert friendly, "Permission-denied error should surface an actionable message"
assert "SELECT" in friendly[0]
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Raw psycopg message (what the activity-level check sees via str(e)).
+ "permission denied for function crypto_aead_det_decrypt",
+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.
+ "InsufficientPrivilege: permission denied for function crypto_aead_det_decrypt",
+ ],
+ )
+ def test_permission_denied_for_function_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-function error should be non-retryable: {error_msg}"
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ "permission denied for function crypto_aead_det_decrypt",
+ "InsufficientPrivilege: permission denied for function crypto_aead_det_decrypt",
+ ],
+ )
+ def test_permission_denied_for_function_returns_execute_message(self, source, error_msg):
+ non_retryable = source.get_non_retryable_errors()
+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]
+ assert friendly, "Permission-denied-for-function error should surface an actionable message"
+ # The function-permission message must win over the generic table-SELECT message and advise
+ # EXECUTE rather than the misleading "GRANT SELECT ON <table>".
+ assert "EXECUTE" in friendly[0]
+ assert "GRANT SELECT" not in friendly[0]
+
@pytest.mark.parametrize(
"error_msg",
[