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): degrade primary-key discovery without capturing exception (#63750)

Tom Owers3581ab165c88
diff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.py
index 171346a3fae5..bc58d1b92eda 100644
--- a/posthog/temporal/data_imports/sources/postgres/source.py
+++ b/posthog/temporal/data_imports/sources/postgres/source.py
@@ -404,7 +404,14 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
pk_columns_by_table[display_name] = pk_columns
tables_with_pks = set(pk_columns_by_table.keys())
except Exception as e:
- capture_exception(e)
+ # Best-effort, like the foreign-key and index lookups: some
+ # Postgres-wire-compatible engines reject our `pg_catalog` PK query
+ # (e.g. a DuckDB/DuckLake backend can't bind `ANY(indkey)` →
+ # "Binder Error: UNNEST not supported here"). Losing the `supports_cdc`
+ # hint is harmless, so warn rather than capturing it as an exception.
+ structlog.get_logger().warning(
+ "Failed to detect primary key columns for Postgres schemas", exc_info=e
+ )
pk_columns_by_table = {}
tables_with_pks = set()
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index 727242a1f8d4..ca5af4d7f498 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -1098,6 +1098,70 @@ class TestPostgresSourceGetSchemasDegradesGracefully:
assert schemas[0].supports_cdc is False
mock_capture.assert_not_called()
+ def test_primary_key_discovery_failure_degrades_without_capturing_exception(self, source):
+ # Some Postgres-wire-compatible engines reject our pg_catalog PK query (e.g. a
+ # DuckDB/DuckLake backend can't bind `ANY(indkey)` and raises the binder error
+ # below). PK discovery is best-effort and already falls back to no-CDC, so the
+ # failure must be logged, not flooded into error tracking as a captured exception.
+ discovered = {
+ "public.users": PostgresDiscoveredSchema(
+ source_catalog=None,
+ source_schema="public",
+ source_table_name="users",
+ columns=[("id", "integer", False)],
+ )
+ }
+
+ tunnel_cm = mock.MagicMock()
+ tunnel_cm.__enter__.return_value = ("localhost", 5432)
+ tunnel_cm.__exit__.return_value = None
+
+ conn_cm = mock.MagicMock()
+ conn_cm.__enter__.return_value = mock.MagicMock()
+ conn_cm.__exit__.return_value = None
+
+ unnest_error = psycopg.errors.InternalError_(
+ "flight execute: rpc error: code = InvalidArgument desc = failed to prepare query: "
+ "Binder Error: UNNEST not supported here"
+ )
+
+ with (
+ mock.patch.object(source, "with_ssh_tunnel", return_value=tunnel_cm),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source.get_postgres_schemas",
+ return_value=discovered,
+ ),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source.get_postgres_foreign_keys",
+ return_value={},
+ ),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source.pg_connection",
+ return_value=conn_cm,
+ ),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source.get_primary_key_columns",
+ side_effect=unnest_error,
+ ),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source.get_leading_index_columns",
+ return_value={"users": set()},
+ ),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source._rls_active_from_conn",
+ return_value={},
+ ),
+ mock.patch("posthog.temporal.data_imports.sources.postgres.source.capture_exception") as mock_capture,
+ ):
+ schemas = source.get_schemas(self._config(), team_id=1)
+
+ assert len(schemas) == 1
+ assert schemas[0].name == "public.users"
+ # PK discovery failed, so CDC must not be advertised — but the listing still succeeds.
+ assert schemas[0].supports_cdc is False
+ # The handled, best-effort failure must not be captured as an exception.
+ mock_capture.assert_not_called()
+
class TestGetSslmode:
@pytest.mark.parametrize(