: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(warehouse-sources): stop retrying postgres syncs that need SNI routing (#98794)
Tom Owers18dd50b368c3
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.pyindex 966bf5a2691c..ac49ddb499e1 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py@@ -165,6 +165,18 @@ PostgresErrors = {'"postgres.<project-ref>"). Update the username to the pooler username shown in your '"Supabase dashboard and try again."),+ # Some multi-tenant Postgres providers route connections by TLS SNI and reject one that+ # carries none, naming the hostname to use instead: "FATAL: this server requires connecting+ # via <hostname>". SNI is only sent when the configured host is a hostname, so this fires+ # when the host is set to a raw IP address. `get_non_retryable_errors` already handles this+ # on the streaming path; map it here too so validation returns an actionable message instead+ # of the generic fallback. The volatile hostname is excluded from the match.+ "requires connecting via": (+ "Your database provider requires connecting through a specific hostname for routing "+ '("requires connecting via ..."). This usually happens when the host is configured as an '+ "IP address instead of a hostname. Update the host to the hostname your database "+ "provider gave you and try again."+ ),# Some poolers (for example Supabase's transaction pooler on port 6543) reject bad credentials# during the SASL/SCRAM exchange with "FATAL: SASL authentication failed" instead of libpq's# "password authentication failed for user", so none of the password keys above substring-match@@ -206,6 +218,17 @@ PostgresErrors = {"the database system is starting up": "Your database is starting up or recovering. Wait a moment and try again.","SSL/TLS connection is required": "SSL/TLS connection is required but your database does not support it. Please enable SSL/TLS on your PostgreSQL server.","server does not support SSL, but SSL was required": "SSL/TLS connection is required but your database does not support it. Please enable SSL/TLS on your PostgreSQL server.",+ # The plaintext half of the SNI rejection mapped above: with sslmode=prefer libpq retries+ # without SSL after a failed encrypted attempt, and a provider that requires TLS refuses that+ # too with "FATAL: SSL/TLS connection required. Connect with sslmode=require or higher." The+ # key above ("SSL/TLS connection is required") is our own `SSLRequiredError` copy and carries an+ # extra word the server message lacks, so it never substring-matched this. Placed after the+ # "requires connecting via" entry so the host guidance wins when a message carries both.+ "SSL/TLS connection required": (+ 'Your database refused an unencrypted connection ("SSL/TLS connection required"). PostHog '+ "only tries an unencrypted connection after an encrypted one fails, so check that the host "+ "is the hostname your database provider gave you rather than an IP address, then try again."+ ),# An invalid SSL-negotiation response means the host/port isn't a PostgreSQL server speaking SSL# (wrong port, an HTTP/proxy/edge endpoint, or a TCP proxy fronting a paused/deleted database).# Map it to an actionable message so validation stops surfacing this expected user/upstream@@ -466,6 +489,20 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa'"postgres.<project-ref>"). Update the user for this source to the pooler username '"shown in your Supabase dashboard, then re-enable the sync."),+ # Some multi-tenant Postgres providers route connections by TLS SNI and reject one+ # that carries none, naming the hostname to use instead: "FATAL: this server requires+ # connecting via <hostname>". SNI is only sent when the configured host is a hostname+ # (see `pinned_host_kwargs`), so this fires when the host is set to a raw IP address —+ # with sslmode=prefer, libpq then falls back to a second, unencrypted attempt the+ # provider also rejects for requiring SSL/TLS. Deterministic until the customer+ # switches the host to the hostname named in the message, so retrying just re-hits it.+ # Match the stable fragment and exclude the volatile hostname.+ "requires connecting via": (+ "Your database provider requires connecting through a specific hostname for "+ 'routing ("requires connecting via ..."). This usually happens when the host is '+ "configured as an IP address instead of a hostname. Update the host for this "+ "source to the hostname your database provider gave you, then re-enable the sync."+ ),"error received from server in SCRAM exchange: Wrong password": _INVALID_CREDENTIALS_ERROR,# The server (commonly Supabase's Supavisor transaction pooler on port 6543) rejects the# SASL/SCRAM credential exchange with "FATAL: SASL authentication failed" instead of@@ -774,6 +811,20 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa"wrong port, or a database that's paused or deleted behind a TCP proxy. Check your ""host and port, then re-enable the sync."),+ # The plaintext half of the SNI rejection mapped above: with sslmode=prefer libpq+ # retries without SSL after a failed encrypted attempt, and a provider that requires+ # TLS refuses that too with "FATAL: SSL/TLS connection required. Connect with+ # sslmode=require or higher." The "SSL/TLS connection is required" key below is our own+ # `SSLRequiredError` copy and carries an extra word the server message lacks, so it+ # never substring-matched this, and the failure kept being retried. Placed after the+ # "requires connecting via" entry so the host guidance wins when both wordings arrive+ # together.+ "SSL/TLS connection required": (+ 'Your database refused an unencrypted connection ("SSL/TLS connection required"). '+ "PostHog only tries an unencrypted connection after an encrypted one fails, so "+ "check that the host for this source is the hostname your database provider gave "+ "you rather than an IP address, then re-enable the sync."+ ),"SSLRequiredError": None,"SSL/TLS connection is required": None,_SSH_GATEWAY_SESSION_ERROR: _SSH_GATEWAY_UNREACHABLE_MESSAGE,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.pyindex f28da1e7a107..bbb6e6b75e06 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@@ -1080,6 +1080,57 @@ class TestPostgresSourceNonRetryableErrors:assert friendly, "Branch connection restriction error should surface an actionable message"assert "branch" in friendly[0]+ @pytest.mark.parametrize(+ "error_msg",+ [+ # A multi-tenant Postgres provider rejects a connection with no SNI (the host was+ # configured as a raw IP), naming the hostname to use instead, then also rejects the+ # sslmode=prefer fallback attempt for lacking SSL/TLS. Host/IP, port, and the named+ # hostname are volatile; the rejection reason is stable.+ 'connection failed: connection to server at "34.200.85.18", port 5432 failed: FATAL: '+ "this server requires connecting via org123.dw.us.example.com\n"+ 'connection to server at "34.200.85.18", port 5432 failed: FATAL: SSL/TLS connection '+ "required. Connect with sslmode=require or higher.",+ ],+ )+ def test_sni_hostname_routing_rejection_is_non_retryable(self, source, error_msg):+ non_retryable = source.get_non_retryable_errors()+ assert "requires connecting via" in non_retryable+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())+ assert is_non_retryable, f"SNI hostname routing rejection should be non-retryable: {error_msg}"++ def test_sni_hostname_routing_rejection_returns_friendly_message(self, source):+ non_retryable = source.get_non_retryable_errors()+ error_msg = (+ 'connection failed: connection to server at "34.200.85.18", port 5432 failed: FATAL: '+ "this server requires connecting via org123.dw.us.example.com"+ )+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]+ assert friendly, "SNI hostname routing rejection should surface an actionable message"+ assert "hostname" in friendly[0]++ def test_sni_hostname_routing_rejection_wins_over_the_plaintext_refusal(self, source):+ non_retryable = source.get_non_retryable_errors()+ error_msg = (+ 'connection failed: connection to server at "34.200.85.18", port 5432 failed: FATAL: '+ "this server requires connecting via org123.dw.us.example.com\n"+ 'connection to server at "34.200.85.18", port 5432 failed: FATAL: SSL/TLS connection '+ "required. Connect with sslmode=require or higher."+ )+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]+ assert friendly, "SNI hostname routing rejection should surface an actionable message"+ assert "requires connecting via" in friendly[0]++ def test_plaintext_refusal_alone_is_non_retryable(self, source):+ non_retryable = source.get_non_retryable_errors()+ error_msg = (+ 'connection failed: connection to server at "34.200.85.18", port 5432 failed: FATAL: '+ "SSL/TLS connection required. Connect with sslmode=require or higher."+ )+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]+ assert friendly, "A refused unencrypted connection should surface an actionable message"+ assert "unencrypted connection" in friendly[0]+@pytest.mark.parametrize("error_msg",[@@ -4668,6 +4719,27 @@ class TestValidateCredentialsErrorMapping:"PostHog couldn't resolve your database host right now. Check the host name, then try ""again in a moment.",),+ # A provider that routes by TLS SNI refuses a connection carrying none, and the+ # sslmode=prefer fallback then draws a second, plaintext refusal. Both wordings arrive+ # in one message and the host guidance must be the one selected.+ (+ 'connection failed: connection to server at "203.0.113.10", port 5432 failed: FATAL: '+ "this server requires connecting via org123.dw.us.example.com\n"+ 'connection to server at "203.0.113.10", port 5432 failed: FATAL: SSL/TLS connection '+ "required. Connect with sslmode=require or higher.",+ "Your database provider requires connecting through a specific hostname for routing "+ '("requires connecting via ..."). This usually happens when the host is configured as an '+ "IP address instead of a hostname. Update the host to the hostname your database "+ "provider gave you and try again.",+ ),+ # The plaintext refusal on its own, without the host line.+ (+ 'connection failed: connection to server at "203.0.113.10", port 5432 failed: FATAL: '+ "SSL/TLS connection required. Connect with sslmode=require or higher.",+ 'Your database refused an unencrypted connection ("SSL/TLS connection required"). PostHog '+ "only tries an unencrypted connection after an encrypted one fails, so check that the host "+ "is the hostname your database provider gave you rather than an IP address, then try again.",+ ),# Unmapped errors fall back to the generic message.("some brand new failure",