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): don't capture InFailedSqlTransaction in RLS check (#64039)

Tom Owersdc3320c46d7e
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.py
index 04d9dc901ba9..8756d24cfce3 100644
--- a/posthog/temporal/data_imports/sources/postgres/postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py
@@ -718,11 +718,20 @@ def _rls_active_from_conn(
result[display_name] = bool(rls_active)
return result
except Exception as e:
+ # This runs on a connection shared with earlier best-effort metadata lookups (PK + index
+ # discovery). When one of those fails on a non-Postgres engine — e.g. a Redshift-incompatible
+ # `pg_catalog` query — its exception is caught upstream but the connection's transaction is
+ # left in `INERROR`, so our first statement here fails with `InFailedSqlTransaction` purely as
+ # a downstream symptom. That's an already-handled condition, not a bug in this lookup, so
+ # don't re-capture it (mirrors `_get_partition_settings`).
+ #
# Postgres-wire-compatible engines (DuckDB/Flight-SQL proxies, etc.) accept our connection
# but don't implement `row_security_active`. RLS is a Postgres-only concept there, so a
# missing-function error is an expected "no RLS" answer, not a bug — degrade quietly rather
# than flooding error tracking. Still capture genuinely unexpected failures.
- if not _is_unsupported_function_error(e, "row_security_active"):
+ if not isinstance(e, psycopg.errors.InFailedSqlTransaction) and not _is_unsupported_function_error(
+ e, "row_security_active"
+ ):
capture_exception(e)
return {}
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index e9ba964370ce..5ba8c68999b4 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -3694,6 +3694,22 @@ class TestRlsActiveFromConnErrorHandling:
assert result == {}
capture_mock.assert_called_once()
+ def test_failed_sql_transaction_is_not_captured(self):
+ # This lookup shares a connection with earlier best-effort metadata queries (PK + index
+ # discovery). When one of those fails on a non-Postgres engine (e.g. Redshift) its exception
+ # is caught upstream but leaves the transaction aborted, so our first statement here raises
+ # InFailedSqlTransaction as a downstream symptom. That's already handled, not a bug — don't
+ # flood error tracking with it.
+ conn = self._conn_raising(
+ psycopg.errors.InFailedSqlTransaction(
+ "current transaction is aborted, commands ignored until end of transaction block"
+ )
+ )
+ with patch("posthog.temporal.data_imports.sources.postgres.postgres.capture_exception") as capture_mock:
+ result = _rls_active_from_conn(cast(Any, conn), "public", ["t"])
+ assert result == {}
+ capture_mock.assert_not_called()
+
class TestGetRowsInitialConnectRetry:
# Regression: the main server-cursor read path opened its initial connection with a bare