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 reporting best-effort RLS probe timeouts to error tracking (#87016)

Tom Owersf451065f9924
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
index 551a63bb727b..547ad536bf28 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
@@ -1195,6 +1195,20 @@ def _is_unsupported_statement_timeout_error(error: Exception) -> bool:
return "statement_timeout" in message and "not supported" in message
+def _is_statement_timeout_error(error: BaseException) -> bool:
+ """True when the guarding `SET LOCAL statement_timeout` on a best-effort catalog scan fires.
+
+ Distinct from `_is_unsupported_statement_timeout_error`, which recognises an engine
+ rejecting the `SET` itself. This recognises the `SET` succeeding and the guarded query
+ running long enough to hit it — the guard doing exactly what it's there for, on the same
+ best-effort metadata scan `_xmin_capable_tables_from_conn` already degrades quietly for.
+ """
+ return (
+ isinstance(error, psycopg.errors.QueryCanceled)
+ and "statement timeout" in " ".join(str(arg) for arg in error.args).lower()
+ )
+
+
def _rls_active_from_conn(
connection: psycopg.Connection,
schema: str | None,
@@ -1271,13 +1285,19 @@ def _rls_active_from_conn(
# 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.
+ # than flooding error tracking. A genuine statement timeout is the same kind of expected
+ # outcome: this lookup is best-effort like the PK/xmin/index lookups it runs alongside, and
+ # they all run under the same 30s SET LOCAL guard against a runaway catalog scan — hitting
+ # it is the guard working, not new information about a bug here (mirrors
+ # `_xmin_capable_tables_from_conn`, which already degrades quietly for it). Still capture
+ # genuinely unexpected failures.
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")
and not _is_unsupported_statement_timeout_error(e)
+ and not _is_statement_timeout_error(e)
):
capture_exception(e)
return {}
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 844d1c668e11..255fa23d9a0d 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
@@ -100,6 +100,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.p
_is_options_startup_param_unsupported,
_is_partitioned_table,
_is_read_replica,
+ _is_statement_timeout_error,
_is_unsupported_function_error,
_is_unsupported_statement_timeout_error,
_next_recovery_conflict_chunk_size,
@@ -7527,6 +7528,25 @@ class TestIsUnsupportedStatementTimeoutError:
assert _is_unsupported_statement_timeout_error(error) is expected
+class TestIsStatementTimeoutError:
+ @pytest.mark.parametrize(
+ "error,expected",
+ [
+ (psycopg.errors.QueryCanceled("canceling statement due to statement timeout"), True),
+ # A recovery conflict is also QueryCanceled but a different message entirely.
+ (psycopg.errors.QueryCanceled("canceling statement due to conflict with recovery"), False),
+ # The engine-rejects-the-SET case is a different exception type, not a cancellation.
+ (
+ psycopg.errors.FeatureNotSupported('setting configuration parameter "statement_timeout" not supported'),
+ False,
+ ),
+ (psycopg.OperationalError("connection reset by peer"), False),
+ ],
+ )
+ def test_recognises_statement_timeout(self, error, expected):
+ assert _is_statement_timeout_error(error) is expected
+
+
class TestRlsActiveFromConnErrorHandling:
@staticmethod
def _conn_raising(exc: Exception):
@@ -7561,6 +7581,19 @@ class TestRlsActiveFromConnErrorHandling:
assert result == {}
capture_mock.assert_not_called()
+ def test_statement_timeout_error_is_not_captured(self):
+ # This lookup runs under the same 30s SET LOCAL statement_timeout guard as the sibling
+ # PK/xmin/index best-effort probes, which already degrade quietly when it fires (see
+ # _xmin_capable_tables_from_conn). Hitting it here is the guard working as intended, not a
+ # bug — must not flood error tracking.
+ conn = self._conn_raising(psycopg.errors.QueryCanceled("canceling statement due to statement timeout"))
+ with patch(
+ "products.warehouse_sources.backend.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()
+
def test_unexpected_error_is_still_captured(self):
conn = self._conn_raising(Exception("connection reset by peer"))
with patch(