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): stop capturing RLS lookup errors on a dropped connection (#64457)

Tom Owers722383e7c6e9
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.py
index 6554b3731740..b1a55954ec53 100644
--- a/posthog/temporal/data_imports/sources/postgres/postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py
@@ -801,15 +801,21 @@ def _rls_active_from_conn(
# 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`).
+ # a downstream symptom. A harsher variant of the same thing: an earlier probe (or a transient
+ # drop — SSH-tunnel hiccup, idle cull, failover) leaves the shared connection closed/broken,
+ # so our `cursor()` call surfaces as `OperationalError: the connection is closed`. Both are
+ # already-handled downstream symptoms, not bugs in this lookup, so don't re-capture them
+ # (mirrors `_get_partition_settings` and the caller's own connection-level handler).
#
# 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 isinstance(e, psycopg.errors.InFailedSqlTransaction) and not _is_unsupported_function_error(
- e, "row_security_active"
+ if (
+ not connection.closed
+ and not connection.broken
+ and 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 accdfa941323..85868403b450 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -4330,6 +4330,8 @@ class TestRlsActiveFromConnErrorHandling:
@staticmethod
def _conn_raising(exc: Exception):
conn = mock.MagicMock()
+ conn.closed = False
+ conn.broken = False
conn.cursor.return_value.__enter__.return_value.execute.side_effect = exc
return conn
@@ -4365,6 +4367,22 @@ class TestRlsActiveFromConnErrorHandling:
assert result == {}
capture_mock.assert_not_called()
+ @pytest.mark.parametrize("attr", ["closed", "broken"])
+ def test_dropped_connection_is_not_captured(self, attr):
+ # The shared connection can be dropped by an earlier best-effort probe or a transient blip
+ # (SSH-tunnel hiccup, idle cull), so our `cursor()` call raises `OperationalError: the
+ # connection is closed`. That's a downstream symptom the caller already degrades quietly —
+ # don't flood error tracking with it.
+ conn = mock.MagicMock()
+ conn.closed = False
+ conn.broken = False
+ setattr(conn, attr, True)
+ conn.cursor.side_effect = psycopg.OperationalError("the connection is closed")
+ 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