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): treat recursive RLS policy errors as non-retryable (#76182)

Tom Owersb25847412a18
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 ee33e3a561ed..938319cabfa1 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
@@ -500,6 +500,24 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
'(PostgreSQL reported "permission denied"). Grant the connecting role SELECT on those tables '
"(for example: GRANT SELECT ON <table> TO <role>), then re-enable the sync."
),
+ # A row-level security policy on this table (or another table its policy queries) is
+ # self-referential, so Postgres can't evaluate it and raises SQLSTATE 42P17 on every
+ # read attempt. The raw psycopg message ("infinite recursion detected in policy for
+ # relation <name>") is what the activity-level check sees via `str(e)`; match that,
+ # excluding the volatile relation name. `InvalidObjectDefinition` — the psycopg
+ # exception class name — only appears once Temporal wraps the activity failure, and
+ # a wrapped message contains both substrings, so this key must come first: finalization
+ # (`update_external_data_job_model`) takes the friendly message from the first matching
+ # dict entry, and a `None` class-name match ahead of it would shadow the actionable one.
+ # The policy is fixed until the customer edits it, so retrying re-hits the same
+ # recursion every attempt.
+ "infinite recursion detected in policy": (
+ "A row-level security policy on one of your tables refers back to itself (or to "
+ "another table whose policy loops back to it), so PostgreSQL can't evaluate it "
+ '("infinite recursion detected in policy"). Fix the policy definition to remove the '
+ "self-reference, or grant the connecting role BYPASSRLS, then re-enable the sync."
+ ),
+ "InvalidObjectDefinition": None,
"Connection refused": None,
"No route to host": None,
"password authentication failed connection": None,
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 e5921a7e4b38..739bd6a17f18 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
@@ -959,6 +959,38 @@ class TestPostgresSourceNonRetryableErrors:
assert "USAGE" 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)) — no class name.
+ 'infinite recursion detected in policy for relation "list_members"',
+ 'infinite recursion detected in policy for relation "grocery_lists"',
+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.
+ 'InvalidObjectDefinition: infinite recursion detected in policy for relation "orders"',
+ ],
+ )
+ def test_recursive_rls_policy_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"Recursive RLS policy error should be non-retryable: {error_msg}"
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ 'infinite recursion detected in policy for relation "list_members"',
+ # Temporal-wrapped form matches both this pattern and the "InvalidObjectDefinition"
+ # class-name key; `update_external_data_job_model` takes the friendly message from the
+ # *first* matching dict entry, so the specific pattern must be ordered before the
+ # class-name key or its `None` value silently shadows this actionable message.
+ 'InvalidObjectDefinition: infinite recursion detected in policy for relation "orders"',
+ ],
+ )
+ def test_recursive_rls_policy_returns_friendly_message(self, source, error_msg):
+ non_retryable = source.get_non_retryable_errors()
+ first_match = next((reason for pattern, reason in non_retryable.items() if pattern in error_msg), None)
+ assert first_match is not None, "Recursive RLS policy error should surface an actionable message"
+ assert "BYPASSRLS" in first_match
+
@pytest.mark.parametrize(
"error_msg",
[