: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
perf(warehouse-sources): read latest_state in duckgres eligibility scans (#73931)
Eric Duong70856e346b95
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/jobs_db.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/jobs_db.pyindex 6931b100cb65..3bc79a647c4d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/jobs_db.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/jobs_db.py@@ -186,9 +186,9 @@ def _eligibility_ctes(scoped: bool) -> str:-- run_starts/failed_runs scope to it so the work is bounded by the backlog.SELECT DISTINCT cb.run_uuidFROM {BATCH_TABLE} cb- JOIN {_latest_status_lateral(STATUS_TABLE, "cb")} cds ON cds.job_state = 'succeeded'LEFT JOIN {_latest_status_lateral(DUCKGRES_STATUS_TABLE, "cb")} cdgs ON trueWHERE cb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'+ AND cb.latest_state = 'succeeded'{_team_scope("cb", scoped=scoped)}AND (cdgs.job_state IS NULL OR cdgs.job_state <> 'succeeded')),@@ -204,11 +204,10 @@ def _eligibility_ctes(scoped: bool) -> str:SELECT cr.run_uuid FROM cand_runs crWHERE EXISTS (SELECT 1 FROM {BATCH_TABLE} fb- JOIN {_latest_status_lateral(STATUS_TABLE, "fb")} fds ON trueWHERE fb.run_uuid = cr.run_uuidAND fb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'{_team_scope("fb", scoped=scoped)}- AND fds.job_state = 'failed'+ AND fb.latest_state = 'failed')OR EXISTS (SELECT 1 FROM {BATCH_TABLE} fb@@ -223,7 +222,6 @@ def _eligibility_ctes(scoped: bool) -> str:SELECT old.team_id, old.schema_id, old.run_uuid, rs_ir.started_at,bool_or((old.metadata->>'duckgres_backfill') IS NOT NULL) AS is_backfill_runFROM {BATCH_TABLE} old- JOIN {_latest_status_lateral(STATUS_TABLE, "old")} ods ON ods.job_state = 'succeeded'JOIN run_starts rs_ir ON rs_ir.run_uuid = old.run_uuidLEFT JOIN {DUCKGRES_APPLY_TABLE} oaON oa.team_id = old.team_id@@ -231,6 +229,7 @@ def _eligibility_ctes(scoped: bool) -> str:AND oa.run_uuid = old.run_uuidAND oa.batch_index = old.batch_indexWHERE old.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'+ AND old.latest_state = 'succeeded'{_team_scope("old", scoped=scoped)}AND old.is_final_batch = falseAND oa.id IS NULL@@ -351,7 +350,6 @@ class DuckgresBatchQueue:SELECT{pending_batch_select_columns("dgs")}FROM {BATCH_TABLE} b- JOIN {_latest_status_lateral(STATUS_TABLE, "b")} ds ON trueJOIN run_starts rs_b ON rs_b.run_uuid = b.run_uuidLEFT JOIN {_latest_status_lateral(DUCKGRES_STATUS_TABLE, "b")} dgs ON trueWHERE@@ -391,7 +389,7 @@ class DuckgresBatchQueue:AND u.live >= m.budget)AND NOT {BLOCKED_LIVE_BATCH_CONDITION}- AND ds.job_state = 'succeeded'+ AND b.latest_state = 'succeeded'AND (dgs.batch_id IS NULLOR (@@ -623,7 +621,6 @@ class DuckgresBatchQueue:replace_heads AS MATERIALIZED (SELECT nb.team_id, nb.schema_id, nb.run_uuid, rs.started_atFROM {BATCH_TABLE} nb- JOIN {_latest_status_lateral(STATUS_TABLE, "nb")} nds ON trueJOIN run_starts rs ON rs.run_uuid = nb.run_uuidLEFT JOIN {DUCKGRES_APPLY_TABLE} naON na.team_id = nb.team_id@@ -632,7 +629,7 @@ class DuckgresBatchQueue:AND na.batch_index = nb.batch_indexWHERE nb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'{_team_scope("nb", scoped=scoped)}- AND nds.job_state = 'succeeded'+ AND nb.latest_state = 'succeeded'AND nb.batch_index = 0AND nb.is_final_batch = falseAND nb.is_resume = false@@ -649,7 +646,6 @@ class DuckgresBatchQueue:JOIN replace_heads rhON rh.team_id = old.team_id AND rh.schema_id = old.schema_idJOIN run_starts ors ON ors.run_uuid = old.run_uuid- JOIN {_latest_status_lateral(STATUS_TABLE, "old")} ods ON trueLEFT JOIN {_latest_status_lateral(DUCKGRES_STATUS_TABLE, "old")} odgs ON trueLEFT JOIN {DUCKGRES_APPLY_TABLE} oaON oa.team_id = old.team_id@@ -659,7 +655,7 @@ class DuckgresBatchQueue:WHERE old.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'AND old.run_uuid <> rh.run_uuidAND (ors.started_at, old.run_uuid) < (rh.started_at, rh.run_uuid)- AND ods.job_state = 'succeeded'+ AND old.latest_state = 'succeeded'AND old.run_uuid NOT IN (SELECT run_uuid FROM failed_runs)AND (old.is_final_batch = true OR oa.id IS NULL)AND (odgs.batch_id IS NULL OR odgs.job_state = 'waiting_retry')@@ -713,7 +709,6 @@ class DuckgresBatchQueue:AND b.schema_id = ANY(%(failing_schema_ids)s)) AS is_failingFROM {BATCH_TABLE} b- JOIN {_latest_status_lateral(STATUS_TABLE, "b")} ds ON trueLEFT JOIN {DUCKGRES_APPLY_TABLE} aON a.team_id = b.team_idAND a.schema_id = b.schema_id@@ -722,7 +717,7 @@ class DuckgresBatchQueue:WHERE b.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'{_team_scope("b", scoped=scoped)}AND (%(eligible_schema_ids)s::varchar[] IS NULL OR b.schema_id = ANY(%(eligible_schema_ids)s))- AND ds.job_state = 'succeeded'+ AND b.latest_state = 'succeeded'AND b.is_final_batch = falseAND a.id IS NULLAND b.run_uuid NOT IN (SELECT run_uuid FROM failed_runs)diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/test_jobs_db.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/test_jobs_db.pyindex 3ccbdc5d09e9..c45925fe4fa8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/test_jobs_db.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/test_jobs_db.py@@ -515,6 +515,24 @@ class TestDuckgresTeamFilterAndBacklog:assert (count, blocked, failing) == (0, 0, 0)assert oldest_age is None and blocked_age is None+ @pytest.mark.asyncio+ async def test_backlog_stats_reflects_latest_delta_status_not_first(self, conn):+ """Eligibility now reads the denormalized ``latest_state`` column instead+ of a per-row LATERAL join to the delta status table. Pin the exact case+ that denormalization must still get right: a batch with a multi-row+ status history (e.g. executing -> succeeded) is eligible because its+ LATEST row is 'succeeded', while a batch stuck on its first ('executing')+ row is not — the same answer the old latest-status LATERAL gave."""+ multi_history_id = await _insert_batch(conn, run_uuid="run-latest-succeeded", batch_index=0)+ await BatchQueue.update_status(conn, batch_id=multi_history_id, job_state="executing", attempt=1)+ await BatchQueue.update_status(conn, batch_id=multi_history_id, job_state="succeeded", attempt=1)++ still_executing_id = await _insert_batch(conn, run_uuid="run-still-executing", batch_index=0)+ await BatchQueue.update_status(conn, batch_id=still_executing_id, job_state="executing", attempt=1)++ count, _, blocked, _, failing = await DuckgresBatchQueue.get_backlog_stats(conn, team_ids=[1])+ assert (count, blocked, failing) == (1, 0, 0)+class TestTeamScopeSargable:"""The team filter must become a plain, sargable ``= ANY(...)`` when