: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(postgres): re-raise connection drops from the child-partition PK fallback (#65362)
Tom Owersd96f156a3e19
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.pyindex 536ea2ee95d2..cebb895b70fd 100644--- a/posthog/temporal/data_imports/sources/postgres/postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py@@ -1759,6 +1759,13 @@ def _get_primary_keys(cursor.execute(child_partition_pk_query)child_pk_rows = cursor.fetchall()except Exception as e:+ # A transient connection drop here means the fallback never ran — swallowing it would+ # capture noise and wrongly report "no primary key" off a dead cursor. Re-raise so the+ # setup retry loop reconnects (mirrors the unwrapped primary query above and the+ # duplicate-PK probe). Genuine query-incompatibility errors (e.g. an engine that can't+ # bind this pg_catalog query) still degrade to best-effort.+ if _is_connection_dropped_error(e):+ raisecapture_exception(e)logger.warning(f"Child-partition fallback query failed for {table_name}: {e}")if len(child_pk_rows) > 0:diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex 49b6fa15824e..09f004144a71 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -3905,6 +3905,39 @@ class TestGetPrimaryKeys:result = _get_primary_keys(cast(Any, dj_cursor), "public", "test_partitioned_inconsistent_pk", logger)assert result is None+ def test_reraises_connection_drop_during_child_partition_fallback(self):+ logger = structlog.get_logger()+ cursor = MagicMock()+ # Primary PK query returns no rows, so the child-partition fallback runs.+ cursor.fetchall.return_value = []+ # The fallback query then hits a transient connection drop. It must propagate to the setup+ # retry loop (stays retryable), not be swallowed as "no primary key" + captured as noise.+ cursor.execute.side_effect = [None, psycopg.OperationalError("the connection is lost")]+ module = "posthog.temporal.data_imports.sources.postgres.postgres"+ with (+ patch(f"{module}._explain_query"),+ patch(f"{module}.capture_exception") as mock_capture,+ ):+ with pytest.raises(psycopg.OperationalError, match="the connection is lost"):+ _get_primary_keys(cast(Any, cursor), "public", "events", logger)+ mock_capture.assert_not_called()++ def test_captures_and_degrades_on_non_connection_error_in_child_partition_fallback(self):+ logger = structlog.get_logger()+ cursor = MagicMock()+ cursor.fetchall.return_value = []+ # A genuine query-incompatibility error in the best-effort fallback still degrades to+ # "no primary key" and is captured — only transient drops are re-raised.+ cursor.execute.side_effect = [None, psycopg.errors.UndefinedColumn("boom")]+ module = "posthog.temporal.data_imports.sources.postgres.postgres"+ with (+ patch(f"{module}._explain_query"),+ patch(f"{module}.capture_exception") as mock_capture,+ ):+ result = _get_primary_keys(cast(Any, cursor), "public", "events", logger)+ assert result is None+ mock_capture.assert_called_once()+class TestGetLeadingIndexColumns:"""Unit tests for the leading-index-column helper used to flag unindexed