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 chain options fallback error onto real failure (#64382)

Tom Owers8b1490f235ab
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.py
index b1a55954ec53..e33c6d423eb7 100644
--- a/posthog/temporal/data_imports/sources/postgres/postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py
@@ -376,9 +376,12 @@ def _connect_with_options_fallback(**connect_kwargs: Any) -> psycopg.Connection:
try:
return psycopg.connect(**connect_kwargs)
except psycopg.OperationalError as e:
- if connect_kwargs.get("options") and _is_options_startup_param_unsupported(e):
- return psycopg.connect(**{k: v for k, v in connect_kwargs.items() if k != "options"})
- raise
+ if not (connect_kwargs.get("options") and _is_options_startup_param_unsupported(e)):
+ raise
+ # Retry outside the `except` block: a genuine failure on the options-less connect (bad
+ # password, tenant not found) must propagate on its own, not chained to the benign — and now
+ # recovered — "options unsupported" error, which otherwise masks the real cause in error tracking.
+ return psycopg.connect(**{k: v for k, v in connect_kwargs.items() if k != "options"})
def _connect_to_postgres(
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index 85868403b450..71543cbe88ff 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -931,6 +931,27 @@ class TestConnectOptionsStartupParamFallback:
assert connect_mock.call_count == 1
+ def test_retry_failure_is_not_chained_to_options_error(self):
+ # When the options-less retry fails for a real reason (wrong password), that error must not
+ # carry the recovered "options unsupported" error as its context — the chained context
+ # otherwise surfaces in error tracking and masks the genuine, already-classified cause.
+ connect_mock = mock.MagicMock(
+ side_effect=[
+ psycopg.OperationalError(
+ "FATAL: Feature not supported: RDS Proxy currently doesn’t support command-line options."
+ ),
+ psycopg.OperationalError("FATAL: The password that was provided for the role postgres is wrong."),
+ ]
+ )
+
+ with patch("posthog.temporal.data_imports.sources.postgres.postgres.psycopg.connect", connect_mock):
+ with pytest.raises(psycopg.OperationalError) as exc_info:
+ _connect_with_options_fallback(host="db", options=FORCE_UTF8_CLIENT_ENCODING)
+
+ assert connect_mock.call_count == 2
+ assert "The password that was provided for the role" in str(exc_info.value)
+ assert exc_info.value.__context__ is None
+
class TestStatementTimeoutAsNonRetryable:
@pytest.mark.parametrize(