: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-imports): prefetch schema source to stop lazy DB read in delta URI (#75810)
Tom Owersebcec2156cdb
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 7867d8bcd7ac..59009ea53125 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@@ -304,7 +304,11 @@ async def import_data_activity_sync(inputs: ImportDataActivityInputs) -> Pipelindef _get_models(job_id: str,) -> tuple[ExternalDataJob, ExternalDataSchema, ExternalDataSource, DataWarehouseTable | None]:- job = ExternalDataJob.objects.select_related("schema", "schema__table").get(id=job_id)+ # `schema__source` is prefetched so `job.folder_path()` (via `schema.source.source_type`, called+ # repeatedly through the run by `DeltaTableHelper._get_delta_table_uri`) never triggers a lazy+ # relation load later on a pooled connection the transaction pooler may have dropped mid-sync,+ # which raises a transient `OperationalError`/DNS failure.+ job = ExternalDataJob.objects.select_related("schema", "schema__table", "schema__source").get(id=job_id)schema: ExternalDataSchema | None = job.schemasource: ExternalDataSource | None = job.pipelineif schema is None: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 0da8c1ecfbd3..58cf0b0fb5c5 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@@ -1,12 +1,17 @@import uuidimport contextlibfrom datetime import datetime+from typing import Any, castimport pytest+from posthog.test.base import BaseTestfrom unittest import mockfrom posthog.temporal.common.errors import NonReportableError+from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob+from products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchema+from products.warehouse_sources.backend.models.external_data_source import ExternalDataSourcefrom products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arrow_utils import (SchemaColumnTypeChangedException,)@@ -90,6 +95,29 @@ def _inputs() -> ImportDataActivityInputs:)+class TestGetModelsPrefetchesSource(BaseTest):+ def test_folder_path_needs_no_query_after_load(self) -> None:+ # folder_path() reads schema.source.source_type. If _get_models stops prefetching+ # schema__source, that read fires a lazy SELECT later in the run, on a pooled app-DB+ # connection the transaction pooler can drop mid-sync — surfacing as a spurious+ # OperationalError/DNS failure far from where the real query would have been.+ source = ExternalDataSource.objects.create(+ source_id=str(uuid.uuid4()), connection_id=str(uuid.uuid4()), team=self.team, source_type="Stripe"+ )+ schema = ExternalDataSchema.objects.create(name="Invoice", team=self.team, source=source, sync_type_config={})+ job = ExternalDataJob.objects.create(+ team=self.team, pipeline=source, schema=schema, status=ExternalDataJob.Status.RUNNING+ )++ # Call the undecorated sync loader directly so the query capture runs on this thread.+ # `.func` is the wrapped sync callable on database_sync_to_async_pool's DatabaseSyncToAsync+ # (asgiref); it isn't on the decorator's static type, hence the cast.+ loaded_job, _, _, _ = cast(Any, module._get_models).func(str(job.id))++ with self.assertNumQueries(0):+ loaded_job.folder_path()++@pytest.mark.asyncioasync def test_non_retryable_setup_error_routes_through_handler():# A MongoDB mongodb+srv:// URI resolves DNS in the MongoClient constructor, so a deleted