: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(data-warehouse): catch Supabase direct host and point users at the pooler (#62423)
Tom Owersdffb58ee145a
diff --git a/posthog/temporal/data_imports/sources/supabase/source.py b/posthog/temporal/data_imports/sources/supabase/source.pyindex ce39245ef41c..3ce384db1368 100644--- a/posthog/temporal/data_imports/sources/supabase/source.py+++ b/posthog/temporal/data_imports/sources/supabase/source.py@@ -1,14 +1,44 @@+import re+from typing import Optional+from posthog.schema import (ExternalDataSourceType as SchemaExternalDataSourceType,SourceConfig,+ SourceFieldFileUploadConfig,SourceFieldInputConfig,+ SourceFieldOauthConfig,+ SourceFieldSelectConfig,+ SourceFieldSSHTunnelConfig,+ SourceFieldSwitchGroupConfig,)from posthog.temporal.data_imports.sources.common.registry import SourceRegistry+from posthog.temporal.data_imports.sources.generated_configs import PostgresSourceConfigfrom posthog.temporal.data_imports.sources.postgres.source import PostgresSourcefrom products.data_warehouse.backend.types import ExternalDataSourceType+_SourceField = (+ SourceFieldInputConfig+ | SourceFieldSwitchGroupConfig+ | SourceFieldSelectConfig+ | SourceFieldOauthConfig+ | SourceFieldFileUploadConfig+ | SourceFieldSSHTunnelConfig+)++# Supabase's direct connection host (`db.<ref>.supabase.co`) is IPv6-only and so is+# unreachable from PostHog's IPv4 egress — by far the biggest cause of Supabase+# connection failures. Users need the connection pooler host instead.+_SUPABASE_DIRECT_HOST_RE = re.compile(r"^db\.[a-z0-9]+\.supabase\.co$", re.IGNORECASE)++_SUPABASE_POOLER_HOST_CAPTION = (+ "Use the **Session pooler** host from Supabase (Project settings → Database → "+ "Connection pooling), e.g. `aws-0-<region>.pooler.supabase.com`, with username "+ "`postgres.<project-ref>`. The direct host `db.<ref>.supabase.co` is IPv6-only and "+ "won't be reachable."+)+@SourceRegistry.registerclass SupabaseSource(PostgresSource):@@ -19,14 +49,22 @@ class SupabaseSource(PostgresSource):def source_type(self) -> ExternalDataSourceType:return ExternalDataSourceType.SUPABASE+ @staticmethod+ def _adjust_field(field: _SourceField) -> _SourceField:+ if isinstance(field, SourceFieldInputConfig) and field.name == "schema":+ return field.model_copy(update={"required": True, "label": "Schema", "caption": None})+ if isinstance(field, SourceFieldInputConfig) and field.name == "host":+ return field.model_copy(+ update={+ "placeholder": "aws-0-us-east-1.pooler.supabase.com",+ "caption": _SUPABASE_POOLER_HOST_CAPTION,+ }+ )+ return field+@propertydef get_source_config(self) -> SourceConfig:- fields = [- field.model_copy(update={"required": True, "label": "Schema", "caption": None})- if isinstance(field, SourceFieldInputConfig) and field.name == "schema"- else field- for field in super().get_source_config.fields- ]+ fields = [self._adjust_field(field) for field in super().get_source_config.fields]return SourceConfig(name=SchemaExternalDataSourceType.SUPABASE,@@ -37,3 +75,17 @@ class SupabaseSource(PostgresSource):releaseStatus="beta",featureFlag="supabase-dwh",)++ def validate_credentials(+ self, config: PostgresSourceConfig, team_id: int, schema_name: Optional[str] = None+ ) -> tuple[bool, str | None]:+ # Catch the IPv6-only direct host before the generic Postgres connect attempt+ # times out with an opaque "could not connect to the host on the port given".+ if _SUPABASE_DIRECT_HOST_RE.match((config.host or "").strip()):+ return False, (+ "This looks like the Supabase direct database host (db.<ref>.supabase.co), which is "+ "IPv6-only and unreachable from PostHog. Use the Session pooler host instead "+ "(aws-0-<region>.pooler.supabase.com) with username postgres.<project-ref> — find it "+ "under Project settings → Database → Connection pooling."+ )+ return super().validate_credentials(config, team_id, schema_name=schema_name)diff --git a/posthog/temporal/data_imports/sources/supabase/test_supabase_source.py b/posthog/temporal/data_imports/sources/supabase/test_supabase_source.pyindex 8c1958fd5f57..47336cdb11e5 100644--- a/posthog/temporal/data_imports/sources/supabase/test_supabase_source.py+++ b/posthog/temporal/data_imports/sources/supabase/test_supabase_source.py@@ -1,14 +1,71 @@+import pytest+from unittest import mock+from posthog.schema import SourceFieldInputConfig+from posthog.temporal.data_imports.sources.postgres.source import PostgresSourcefrom posthog.temporal.data_imports.sources.supabase.source import SupabaseSource-def test_supabase_requires_schema_field():- source_config = SupabaseSource().get_source_config- schema_field = next(- field for field in source_config.fields if isinstance(field, SourceFieldInputConfig) and field.name == "schema"+def _field(name: str) -> SourceFieldInputConfig:+ return next(+ field+ for field in SupabaseSource().get_source_config.fields+ if isinstance(field, SourceFieldInputConfig) and field.name == name)++def test_supabase_requires_schema_field():+ schema_field = _field("schema")+assert schema_field.required is Trueassert schema_field.label == "Schema"assert schema_field.caption is None+++def test_supabase_host_field_points_at_the_pooler():+ host_field = _field("host")++ assert "pooler.supabase.com" in host_field.placeholder+ assert host_field.caption is not None+ assert "pooler" in host_field.caption.lower()+++ "host",+ [+ "db.abcdefghijklmnop.supabase.co",+ "DB.ABCDEFGH.SUPABASE.CO",+ " db.abcdefgh.supabase.co ",+ ],+)+def test_direct_host_is_rejected_with_pooler_guidance(host):+ config = mock.MagicMock(host=host)++ with mock.patch.object(PostgresSource, "validate_credentials") as super_validate:+ success, error = SupabaseSource().validate_credentials(config, team_id=1)++ assert success is False+ assert error is not None+ assert "pooler" in error.lower()+ # We short-circuit before attempting the generic Postgres connection.+ super_validate.assert_not_called()+++ "host",+ [+ "aws-0-us-east-1.pooler.supabase.com",+ "db.example.com",+ "my-db.internal",+ ],+)+def test_pooler_and_other_hosts_delegate_to_postgres(host):+ config = mock.MagicMock(host=host)++ with mock.patch.object(PostgresSource, "validate_credentials", return_value=(True, None)) as super_validate:+ success, error = SupabaseSource().validate_credentials(config, team_id=1)++ assert success is True+ assert error is None+ super_validate.assert_called_once()