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-imports): avoid lazy schema fetch in table statistics activity (#74354)

Tom Owers44dcc3ce318d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
index 6e0da2eea076..b4ed050fa417 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
@@ -211,6 +211,9 @@ def compute_table_statistics_sync(team_id: int, schema_id: uuid.UUID) -> dict[st
if job is None:
emit_completed("skipped", reason="no_job")
return {"status": "skipped", "reason": "no_job"}
+ # Reuse the already-loaded, select_related schema instead of letting job.folder_path() lazily
+ # fetch job.schema on a pooled connection a transaction pooler may have dropped in the meantime.
+ job.schema = schema
resource_name = schema.resolved_s3_folder_name or schema.name
delta_table_helper = DeltaTableHelper(resource_name=resource_name, job=job, logger=log)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
index 45f610dc4267..876e517ce8f3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
@@ -6,6 +6,8 @@ from decimal import Decimal
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
+from django.db import connection
+from django.test.utils import CaptureQueriesContext
from django.utils import timezone
import pyarrow as pa
@@ -192,6 +194,29 @@ class TestComputeTableStatisticsSync:
assert stat.computed_for_delta_version == 12
assert stat.column_type == "Int64"
+ def test_job_reuses_prefetched_schema_to_avoid_lazy_query(self) -> None:
+ # job is fetched without select_related("schema"), so job.folder_path() (which reads
+ # job.schema.source.source_type) would otherwise fire a lazy SELECT on a pooled connection a
+ # transaction pooler may have dropped mid-run, raising a transient OperationalError.
+ team = self._team()
+ schema, table, _ = self._schema_table_job(team)
+ add_actions = pa.table({"num_records": [1], "null_count.amount": [0], "min.amount": [1], "max.amount": [1]})
+ captured: dict = {}
+
+ def _capture_helper(*, resource_name, job, logger):
+ captured["job"] = job
+ return self._mock_delta(add_actions)
+
+ with (
+ patch.object(comp, "statistics_enabled", return_value=True),
+ patch(DELTA_HELPER_PATH, side_effect=_capture_helper),
+ ):
+ compute_table_statistics_sync(team.id, schema.id)
+
+ with CaptureQueriesContext(connection) as ctx:
+ captured["job"].folder_path()
+ assert len(ctx.captured_queries) == 0
+
def test_runs_without_ai_data_processing_consent(self) -> None:
# Statistics never leave our infra, so — unlike enrichment — they must NOT be gated on AI consent.
# Guards against someone copy-pasting enrichment's consent gate into this path.