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(data-warehouse): accept qualified table names in postgres discovery (#60257)

Daniel RC429d97a9b578
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.py
index 6dbbfa263be1..bdb7821719c0 100644
--- a/posthog/temporal/data_imports/sources/postgres/postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py
@@ -414,7 +414,16 @@ def _get_discovered_tables(
if names is None:
return all_tables, qualify_with_schema
- return {name: all_tables[name] for name in names if name in all_tables}, qualify_with_schema
+ # Match qualified (`schema.table`) and bare `table` names — keys may differ after multi-schema migration.
+ filtered: dict[str, tuple[str | None, str, str]] = {}
+ for name in names:
+ if name in all_tables:
+ filtered[name] = all_tables[name]
+ elif "." in name:
+ _, _, unqualified = name.partition(".")
+ if unqualified in all_tables:
+ filtered[name] = all_tables[unqualified]
+ return filtered, qualify_with_schema
def _row_counts_from_conn(
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index 526587a7d80d..a05d49ebe0c3 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -442,6 +442,64 @@ class TestPostgresSchemaDiscovery:
assert schemas["analytics.events"].source_schema == "analytics"
assert schemas["analytics.events"].source_table_name == "events"
+ @pytest.mark.parametrize(
+ "selected_schema,requested_name,fetchall_results,expected_keys",
+ [
+ # Qualified lookup against a schema that's keyed unqualified (config.schema set).
+ # This is the multi-schema migration scenario: row name was rewritten to
+ # `public.tracking_link` while the source still has `schema="public"` configured.
+ (
+ "public",
+ "public.tracking_link",
+ (
+ [("public", "tracking_link")],
+ [("public", "tracking_link", "id", "integer", "NO", 1)],
+ ),
+ {"public.tracking_link"},
+ ),
+ # Unqualified lookup against an unqualified keyspace — the legacy path.
+ (
+ "public",
+ "tracking_link",
+ (
+ [("public", "tracking_link")],
+ [("public", "tracking_link", "id", "integer", "NO", 1)],
+ ),
+ {"tracking_link"},
+ ),
+ # Qualified lookup against a qualified keyspace (no config.schema, multi-schema mode).
+ (
+ "",
+ "public.tracking_link",
+ (
+ [("public", "tracking_link")],
+ [("public", "tracking_link", "id", "integer", "NO", 1)],
+ ),
+ {"public.tracking_link"},
+ ),
+ ],
+ )
+ def test_get_schemas_accepts_qualified_and_unqualified_names(
+ self, selected_schema, requested_name, fetchall_results, expected_keys
+ ):
+ connection = self._mock_connection(*fetchall_results)
+
+ with mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.postgres._connect_to_postgres",
+ return_value=connection,
+ ):
+ schemas = get_schemas(
+ host="localhost",
+ port=5432,
+ database="postgres",
+ user="postgres",
+ password="postgres",
+ schema=selected_schema,
+ names=[requested_name],
+ )
+
+ assert set(schemas.keys()) == expected_keys
+
def test_get_foreign_keys_qualifies_target_table_names_when_schema_is_blank(self):
connection = self._mock_connection(
[("public", "users"), ("analytics", "events")],