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 PAM authentication failure as non-retryable (#64546)

Tom Owersa26f2b1e4566
diff --git a/posthog/tasks/test/test_poll_query_performance.py b/posthog/tasks/test/test_poll_query_performance.py
index 98a63907d3b0..31729cdb1f1e 100644
--- a/posthog/tasks/test/test_poll_query_performance.py
+++ b/posthog/tasks/test/test_poll_query_performance.py
@@ -69,10 +69,16 @@ class TestPollQueryPerformanceTask(SimpleTestCase):
posthog.tasks.tasks.poll_query_performance("DIFFERENT TIME")
mock_logger_error.assert_called_once_with("Poll query performance task terminating: another poller is running")
+ # Mock the inner ClickHouse-querying function: these tests exercise the task wrapper's
+ # timing/scheduling logic, not the query. Without this the wrapper runs a real query against a
+ # ClickHouse test database that this SimpleTestCase never sets up, so the call raises, gets
+ # logged, and the logging consumes a value from the fixed-length `time.time_ns` side_effect —
+ # exhausting it and surfacing as StopIteration under --reruns.
+ @patch("posthog.tasks.poll_query_performance.poll_query_performance")
@patch("posthog.tasks.tasks.logger.error")
@patch("posthog.tasks.tasks.poll_query_performance.apply_async")
def test_poll_query_performance_runs_and_restarts_itself_with_delay(
- self, mock_apply_async: MagicMock, mock_logger_error: MagicMock
+ self, mock_apply_async: MagicMock, mock_logger_error: MagicMock, mock_poll_query_performance_nontask: MagicMock
) -> None:
redis_client = get_client()
key = 1234
@@ -87,6 +93,7 @@ class TestPollQueryPerformanceTask(SimpleTestCase):
Polling._encode_redis_key(mock_apply_async.call_args.kwargs["args"][0]),
)
+ @patch("posthog.tasks.poll_query_performance.poll_query_performance")
@patch("posthog.tasks.tasks.logger.error")
@patch("posthog.tasks.tasks.poll_query_performance.delay")
# Stub the inner query worker the task delegates to (a different module that happens to share
@@ -95,7 +102,7 @@ class TestPollQueryPerformanceTask(SimpleTestCase):
@patch("posthog.tasks.poll_query_performance.poll_query_performance", MagicMock())
@patch("time.time_ns", MagicMock(side_effect=[int(1e9), int(4e9)]))
def test_poll_query_performance_runs_and_restarts_itself_with_no_delay_if_it_takes_too_long(
- self, mock_delay: MagicMock, mock_logger_error: MagicMock
+ self, mock_delay: MagicMock, mock_logger_error: MagicMock, mock_poll_query_performance_nontask: MagicMock
) -> None:
redis_client = get_client()
key = 1234
diff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.py
index eeba8f0b35c4..2fa27fcb2343 100644
--- a/posthog/temporal/data_imports/sources/postgres/source.py
+++ b/posthog/temporal/data_imports/sources/postgres/source.py
@@ -236,6 +236,19 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
"require a pooler-specific username such as postgres.<project-ref>. Check your "
"credentials, then re-enable the sync."
),
+ # A Postgres server configured with `pam` auth in pg_hba.conf rejects bad credentials with
+ # "FATAL: PAM authentication failed for user <user>" instead of PostgreSQL's
+ # "password authentication failed for user", so the password key above doesn't
+ # substring-match it and Temporal keeps retrying a credential mismatch only the customer
+ # can fix. PAM delegates to an external module (system password db, LDAP, etc.); a
+ # rejection here is a deterministic auth failure, not a transient blip. Match the stable
+ # fragment and exclude the volatile user/host.
+ "PAM authentication failed": (
+ "Your database rejected the credentials during PAM authentication "
+ '("PAM authentication failed"). Your PostgreSQL server authenticates this user '
+ "through PAM (for example against the system password database or LDAP), and it "
+ "rejected the username or password. Check your credentials, then re-enable the sync."
+ ),
"could not translate host name": None,
"timeout expired connection to server at": None,
"password authentication failed for user": None,
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index a0366364babb..44fcfc116c6f 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -378,6 +378,33 @@ class TestPostgresSourceNonRetryableErrors:
assert friendly, "SASL authentication failure should surface an actionable message"
assert "credentials" in friendly[0]
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Postgres configured with `pam` auth in pg_hba.conf rejects bad credentials with this
+ # wording instead of "password authentication failed for user". Host/port are volatile.
+ 'connection failed: connection to server at "98.87.250.60", port 5432 failed: FATAL: PAM authentication failed for user "postgres"',
+ 'OperationalError: connection failed: connection to server at "10.0.0.1", port 5432 failed: FATAL: PAM authentication failed for user "myuser"',
+ ],
+ )
+ def test_pam_authentication_failed_is_non_retryable(self, source, error_msg):
+ # The PostgreSQL "password authentication failed for user" key doesn't substring-match the
+ # PAM wording, so confirm the dedicated key recognises it independent of host/port.
+ non_retryable = source.get_non_retryable_errors()
+ assert "PAM authentication failed" in non_retryable
+ assert "password authentication failed for user" not in error_msg
+ assert any(pattern in error_msg for pattern in non_retryable.keys())
+
+ def test_pam_authentication_failed_returns_friendly_message(self, source):
+ non_retryable = source.get_non_retryable_errors()
+ error_msg = (
+ 'connection failed: connection to server at "98.87.250.60", port 5432 failed: '
+ 'FATAL: PAM authentication failed for user "postgres"'
+ )
+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]
+ assert friendly, "PAM authentication failure should surface an actionable message"
+ assert "credentials" in friendly[0]
+
def test_supavisor_enotfound_tenant_user_uses_new_key(self, source):
# The older tenant/user patterns don't cover the newer "(ENOTFOUND) tenant/user" wording,
# so confirm it's specifically the new key that recognises this message.