: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): only read a fan-out parent worth opening (#94473)
Daniel RCe0d2235b1dda
diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/stripe/test_stripe_source.py b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/stripe/test_stripe_source.pyindex 429244b34846..c8cb12e7041c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/stripe/test_stripe_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/stripe/test_stripe_source.py@@ -1215,6 +1215,12 @@ FANOUT_GATE = ("products.warehouse_sources.backend.temporal.data_imports.workflow_activities.""import_data_sync.is_fanout_warehouse_reuse_enabled")+# The mock account holds a handful of customers, far under the production size floor. These tests+# cover the conversion itself; the floor is policy, covered by the gate's own unit tests.+PARENT_SIZE_FLOOR = (+ "products.warehouse_sources.backend.temporal.data_imports.workflow_activities."+ "import_data_sync.MIN_WAREHOUSE_PARENT_ROWS"+)@sync_to_async@@ -1271,7 +1277,7 @@ async def _sync_parent_then_child(team, source, parent, child, mock_stripe_api,calls_before = len(mock_stripe_api.get_all_api_calls())expected_rows = sum(len(rows) for rows in CUSTOMER_BALANCE_TRANSACTIONS.values())- with mock.patch(FANOUT_GATE, return_value=reuse_enabled):+ with mock.patch(FANOUT_GATE, return_value=reuse_enabled), mock.patch(PARENT_SIZE_FLOOR, 0):response = await run_external_data_job_workflow(team=team,external_data_source=source,diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.pyindex 2f66013c024c..cf970ae33ad6 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py@@ -145,8 +145,19 @@ WAREHOUSE_READABLE_PARENT_SYNC_TYPES = frozenset()+# Opening the parent's Delta table costs a few seconds that paging the vendor listing does not:+# resolve the table, read the transaction log, start the scan. That cost is fixed, while the+# listing it replaces grows with the parent, so a small parent pays more than it saves. Measured+# after the conversion shipped, the swap costs ~3s a run against a listing of ~0.5s per 100-row+# page, putting break-even near 700 rows. This floor sits above it with margin.+MIN_WAREHOUSE_PARENT_ROWS = 1_000++def _parent_unusable_reason(parent: ExternalDataSchema | None) -> str | None:- """Why a fan-out child can't read this parent from the warehouse, or None when it can."""+ """Why a fan-out child can't read this parent from the warehouse, or None when it can.++ Reads the parent's row count, so callers have to run this where a query is allowed.+ """if parent is None:return "missing"if not parent.should_sync:@@ -155,6 +166,11 @@ def _parent_unusable_reason(parent: ExternalDataSchema | None) -> str | None:return "unsupported_sync_type"if not parent.initial_sync_complete:return "no_initial_sync"+ parent_rows = parent.table.row_count if parent.table else None+ if parent_rows is None or parent_rows < MIN_WAREHOUSE_PARENT_ROWS:+ # An unknown count is treated as too small: it cannot be shown to pay for the read, and+ # the API path it falls back to is what the child does today anyway.+ return "parent_too_small"return None@@ -184,7 +200,7 @@ async def _warehouse_parent_reuse_available(for parent_name in required_parents:parent = await database_sync_to_async_pool(get_schema_if_exists)(parent_name, team_id, source_id)- unusable_reason = _parent_unusable_reason(parent)+ unusable_reason = await database_sync_to_async_pool(_parent_unusable_reason)(parent)if unusable_reason is not None:await logger.ainfo("data_imports.fanout_parent_unusable",diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.pyindex a3c0287fea00..560b33ec5cf7 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py@@ -762,24 +762,27 @@ def _parent(should_sync: bool,initial_sync_complete: bool,sync_type: str = ExternalDataSchema.SyncType.INCREMENTAL,+ row_count: int | None = 50_000,) -> mock.MagicMock:parent = mock.MagicMock()parent.should_sync = should_syncparent.initial_sync_complete = initial_sync_completeparent.sync_type = sync_typeparent.is_incremental = sync_type == ExternalDataSchema.SyncType.INCREMENTAL+ parent.table = mock.MagicMock(row_count=row_count) if row_count is not None else Nonereturn parent@pytest.mark.asyncio@pytest.mark.parametrize("parent",- [None, "disabled", "never_synced", "append_mode", "cdc_mode"],+ [None, "disabled", "never_synced", "append_mode", "cdc_mode", "too_small", "unknown_size"],)async def test_unusable_parent_falls_back_to_the_api_path(parent):# A child enabled without its parent is a config that syncs today, so turning the flag on# must leave it working: fall back to the parent API instead of failing the run. Append and- # CDC parents hold more than one row per key, so the reader must not stream them either.+ # CDC parents hold more than one row per key, so the reader must not stream them either. A+ # parent under the size floor costs more to open than the listing it would replace.parent_obj = Noneif parent == "disabled":parent_obj = _parent(should_sync=False, initial_sync_complete=True)@@ -789,6 +792,10 @@ async def test_unusable_parent_falls_back_to_the_api_path(parent):parent_obj = _parent(should_sync=True, initial_sync_complete=True, sync_type=ExternalDataSchema.SyncType.APPEND)elif parent == "cdc_mode":parent_obj = _parent(should_sync=True, initial_sync_complete=True, sync_type=ExternalDataSchema.SyncType.CDC)+ elif parent == "too_small":+ parent_obj = _parent(should_sync=True, initial_sync_complete=True, row_count=999)+ elif parent == "unknown_size":+ parent_obj = _parent(should_sync=True, initial_sync_complete=True, row_count=None)with (mock.patch.object(module, "database_sync_to_async_pool", new=_passthrough),