: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-warehouse): index externaldatajob latest-run + incremental scans (#73246)
Tom Owers94e84e413b14
diff --git a/products/warehouse_sources/backend/migrations/0093_externaldatajob_latest_run_idx.py b/products/warehouse_sources/backend/migrations/0093_externaldatajob_latest_run_idx.pynew file mode 100644index 000000000000..007128f5b1e9--- /dev/null+++ b/products/warehouse_sources/backend/migrations/0093_externaldatajob_latest_run_idx.py@@ -0,0 +1,20 @@+from django.db import migrations, models++from posthog.migration_helpers import SafeAddIndexConcurrently+++class Migration(migrations.Migration):+ # Concurrent index builds cannot run inside a transaction. Lives in its own+ # migration per PostHog policy (don't mix CONCURRENTLY operations with regular DDL).+ atomic = False+ dependencies = [("warehouse_sources", "0092_repin_lightspeed_retail_api_version")]++ operations = [+ SafeAddIndexConcurrently(+ model_name="externaldatajob",+ index=models.Index(+ fields=["team", "pipeline", "status", "-created_at"],+ name="idx_extdatajob_latest_run",+ ),+ ),+ ]diff --git a/products/warehouse_sources/backend/migrations/0094_externaldatajob_updated_at_idx.py b/products/warehouse_sources/backend/migrations/0094_externaldatajob_updated_at_idx.pynew file mode 100644index 000000000000..93a73ea2681c--- /dev/null+++ b/products/warehouse_sources/backend/migrations/0094_externaldatajob_updated_at_idx.py@@ -0,0 +1,20 @@+from django.db import migrations, models++from posthog.migration_helpers import SafeAddIndexConcurrently+++class Migration(migrations.Migration):+ # Concurrent index builds cannot run inside a transaction. Lives in its own+ # migration per PostHog policy (don't mix CONCURRENTLY operations with regular DDL).+ atomic = False+ dependencies = [("warehouse_sources", "0093_externaldatajob_latest_run_idx")]++ operations = [+ SafeAddIndexConcurrently(+ model_name="externaldatajob",+ index=models.Index(+ fields=["updated_at"],+ name="idx_extdatajob_updated_at",+ ),+ ),+ ]diff --git a/products/warehouse_sources/backend/migrations/max_migration.txt b/products/warehouse_sources/backend/migrations/max_migration.txtindex cf346b0e8cba..fb006ad127d9 100644--- a/products/warehouse_sources/backend/migrations/max_migration.txt+++ b/products/warehouse_sources/backend/migrations/max_migration.txt@@ -1 +1 @@-0092_repin_lightspeed_retail_api_version+0094_externaldatajob_updated_at_idxdiff --git a/products/warehouse_sources/backend/models/external_data_job.py b/products/warehouse_sources/backend/models/external_data_job.pyindex eb4945a24ac3..0fd4051ec420 100644--- a/products/warehouse_sources/backend/models/external_data_job.py+++ b/products/warehouse_sources/backend/models/external_data_job.py@@ -47,6 +47,21 @@ class ExternalDataJob(CreatedMetaFields, UpdatedMetaFields, UUIDTModel):class Meta:db_table = "posthog_externaldatajob"+ indexes = [+ # Serves the hot "latest run for a pipeline" lookup (get_latest_run_if_exists):+ # equality on team/pipeline/status, ordered by created_at DESC with LIMIT.+ models.Index(+ fields=["team", "pipeline", "status", "-created_at"],+ name="idx_extdatajob_latest_run",+ ),+ # Serves the incremental-sync watermark scan when the warehouse imports this+ # table (WHERE updated_at > <cursor> ORDER BY updated_at): without it the read+ # is a full seq scan + sort of the whole table on every run.+ models.Index(+ fields=["updated_at"],+ name="idx_extdatajob_updated_at",+ ),+ ]def folder_path(self) -> str:if self.schema:diff --git a/products/warehouse_sources/backend/tests/test_migration_0052.py b/products/warehouse_sources/backend/tests/test_migration_0052.pyindex 9b8920ae97fa..f8d0d841be0c 100644--- a/products/warehouse_sources/backend/tests/test_migration_0052.py+++ b/products/warehouse_sources/backend/tests/test_migration_0052.py@@ -1,9 +1,9 @@from typing import Any-from posthog.test.base import TestMigrations+from posthog.test.base import NonAtomicTestMigrations-class BackfillGoogleAdsIncrementalLookbackMigrationTest(TestMigrations):+class BackfillGoogleAdsIncrementalLookbackMigrationTest(NonAtomicTestMigrations):"""0051 backfills a default incremental lookback onto existing Google Ads incremental statsschemas, which were created before the source set one and so re-fetch only the newest day,freezing every prior day at its first-imported, not-yet-final value. It must touch only Google@@ -11,6 +11,9 @@ class BackfillGoogleAdsIncrementalLookbackMigrationTest(TestMigrations):soft-deleted row, and never an explicit user value (including 0, which means "no overlap")."""+ # NonAtomic: setUp rewinds from the current schema down to migrate_from, which crosses the+ # concurrent (atomic = False) index migrations later in the app — reversing a CREATE INDEX+ # CONCURRENTLY can't run inside TestMigrations' wrapping transaction.migrate_from = "0051_warehousecolumnstatistics"migrate_to = "0052_backfill_google_ads_incremental_lookback"