: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): scope duckgres eligibility to pending runs via lateral status lookups (#67139)
Eric Duong1ff548e130ee
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 4f75be588b82..4a6bcf8df2f0 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@@ -12,16 +12,31 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipelinefrom products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.jobs_db import (BATCH_TABLE,PARTITION_PRUNING_INTERVAL,- STATUS_VIEW as DELTA_STATUS_VIEW,+ STATUS_TABLE,PendingBatch,pending_batch_select_columns,unlock_advisory_locks,)DUCKGRES_STATUS_TABLE = "sourcebatchduckgresstatus"+# Latest-status view, still consumed by the reset_duckgres_failed_runs management+# command. The eligibility queries here use per-batch LATERAL lookups instead.DUCKGRES_STATUS_VIEW = "v_latest_source_batch_duckgres_status"DUCKGRES_APPLY_TABLE = "sourcebatchduckgresapply"++def _latest_status_lateral(status_table: str, batch_alias: str) -> str:+ """Latest status row for one batch via the (batch_id, created_at DESC, id DESC)+ index. Drop-in for a join to the DISTINCT ON v_latest_source_batch* view, but a+ per-batch lookup instead of materializing the whole view. SELECTs `_ls.*` so all+ downstream <alias>.<col> references (job_state, created_at, attempt, batch_id) work."""+ return (+ f"LATERAL (SELECT _ls.* FROM {status_table} _ls "+ f"WHERE _ls.batch_id = {batch_alias}.id "+ f"ORDER BY _ls.created_at DESC, _ls.id DESC LIMIT 1)"+ )++DUCKGRES_ADVISORY_LOCK_NAMESPACE = 0x44475300 # "DGS\0" in hex# Structured classification key written into duckgres status error_response by@@ -57,39 +72,62 @@ BLOCKED_LIVE_BATCH_CONDITION = f"""(# append their own CTEs/SELECT). Expects a %(team_ids)s bigint[] parameter# (NULL = no team filter).#+# - cand_runs: runs with pending duckgres work — a delta-succeeded batch that is+# not yet duckgres-succeeded. This is the driving set: every run the gate or+# supersede logic compares (candidates incl. final-batch markers, incomplete+# runs, replace_heads, victims) is delta-succeeded-and-not-duckgres-succeeded,+# hence a member of cand_runs. run_starts/failed_runs scope to it so the work+# the queries do is bounded by the live backlog, not the whole 14-day window.# - run_starts: per-run start time, the total order used for cross-run gating# (run_uuid tiebreak makes it total even for identical timestamps).# - failed_runs: runs terminally excluded from the sink — Delta-failed or# Duckgres-failed (including superseded).# - incomplete_runs: non-failed runs that still owe unapplied data batches;# these block newer runs of the same schema (cross-run head-of-line).-ELIGIBILITY_CTES = f"""run_starts AS MATERIALIZED (+ELIGIBILITY_CTES = f"""cand_runs AS MATERIALIZED (+ -- Runs with pending duckgres work: a delta-succeeded batch that is not yet+ -- duckgres-succeeded. Superset of every run the gate/supersede compares;+ -- run_starts/failed_runs scope to it so the work is bounded by the backlog.+ SELECT DISTINCT cb.run_uuid+ FROM {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 true+ WHERE cb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'+ AND (%(team_ids)s::bigint[] IS NULL OR cb.team_id = ANY(%(team_ids)s))+ AND (cdgs.job_state IS NULL OR cdgs.job_state <> 'succeeded')+ ),+ run_starts AS MATERIALIZED (SELECT b_rs.run_uuid, min(b_rs.created_at) AS started_atFROM {BATCH_TABLE} b_rsWHERE b_rs.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'AND (%(team_ids)s::bigint[] IS NULL OR b_rs.team_id = ANY(%(team_ids)s))+ AND b_rs.run_uuid IN (SELECT run_uuid FROM cand_runs)GROUP BY b_rs.run_uuid),failed_runs AS MATERIALIZED (- SELECT DISTINCT b2.run_uuid- FROM {BATCH_TABLE} b2- JOIN {DELTA_STATUS_VIEW} ds2 ON b2.id = ds2.batch_id- WHERE b2.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'- AND (%(team_ids)s::bigint[] IS NULL OR b2.team_id = ANY(%(team_ids)s))- AND ds2.job_state = 'failed'- UNION- SELECT DISTINCT b3.run_uuid- FROM {BATCH_TABLE} b3- JOIN {DUCKGRES_STATUS_VIEW} dgs3 ON b3.id = dgs3.batch_id- WHERE b3.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'- AND (%(team_ids)s::bigint[] IS NULL OR b3.team_id = ANY(%(team_ids)s))- AND dgs3.job_state = 'failed'+ SELECT cr.run_uuid FROM cand_runs cr+ WHERE EXISTS (+ SELECT 1 FROM {BATCH_TABLE} fb+ JOIN {_latest_status_lateral(STATUS_TABLE, "fb")} fds ON true+ WHERE fb.run_uuid = cr.run_uuid+ AND fb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'+ AND (%(team_ids)s::bigint[] IS NULL OR fb.team_id = ANY(%(team_ids)s))+ AND fds.job_state = 'failed'+ )+ OR EXISTS (+ SELECT 1 FROM {BATCH_TABLE} fb+ JOIN {_latest_status_lateral(DUCKGRES_STATUS_TABLE, "fb")} fdgs ON true+ WHERE fb.run_uuid = cr.run_uuid+ AND fb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'+ AND (%(team_ids)s::bigint[] IS NULL OR fb.team_id = ANY(%(team_ids)s))+ AND fdgs.job_state = 'failed'+ )),incomplete_runs AS MATERIALIZED (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 {DELTA_STATUS_VIEW} ods ON old.id = ods.batch_id+ 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@@ -98,7 +136,6 @@ ELIGIBILITY_CTES = f"""run_starts AS MATERIALIZED (AND oa.batch_index = old.batch_indexWHERE old.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'AND (%(team_ids)s::bigint[] IS NULL OR old.team_id = ANY(%(team_ids)s))- AND ods.job_state = 'succeeded'AND old.is_final_batch = falseAND oa.id IS NULLAND old.run_uuid NOT IN (SELECT run_uuid FROM failed_runs)@@ -148,9 +185,9 @@ class DuckgresBatchQueue:SELECT{pending_batch_select_columns("dgs")}FROM {BATCH_TABLE} b- JOIN {DELTA_STATUS_VIEW} ds ON b.id = ds.batch_id+ JOIN {_latest_status_lateral(STATUS_TABLE, "b")} ds ON trueJOIN run_starts rs_b ON rs_b.run_uuid = b.run_uuid- LEFT JOIN {DUCKGRES_STATUS_VIEW} dgs ON b.id = dgs.batch_id+ LEFT JOIN {_latest_status_lateral(DUCKGRES_STATUS_TABLE, "b")} dgs ON trueWHEREb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'AND (%(team_ids)s::bigint[] IS NULL OR b.team_id = ANY(%(team_ids)s))@@ -272,7 +309,7 @@ class DuckgresBatchQueue:replace_heads AS MATERIALIZED (SELECT nb.team_id, nb.schema_id, nb.run_uuid, rs.started_atFROM {BATCH_TABLE} nb- JOIN {DELTA_STATUS_VIEW} nds ON nb.id = nds.batch_id+ 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@@ -298,8 +335,8 @@ 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 {DELTA_STATUS_VIEW} ods ON old.id = ods.batch_id- LEFT JOIN {DUCKGRES_STATUS_VIEW} odgs ON old.id = odgs.batch_id+ JOIN {_latest_status_lateral(STATUS_TABLE, "old")} ods ON true+ LEFT JOIN {_latest_status_lateral(DUCKGRES_STATUS_TABLE, "old")} odgs ON trueLEFT JOIN {DUCKGRES_APPLY_TABLE} oaON oa.team_id = old.team_idAND oa.schema_id = old.schema_id@@ -350,7 +387,7 @@ class DuckgresBatchQueue:b.created_at,{BLOCKED_LIVE_BATCH_CONDITION} AS is_blockedFROM {BATCH_TABLE} b- JOIN {DELTA_STATUS_VIEW} ds ON b.id = ds.batch_id+ 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@@ -465,8 +502,8 @@ class DuckgresBatchQueue:INSERT INTO {DUCKGRES_STATUS_TABLE} (batch_id, job_state, attempt, exec_time, error_response, created_at)SELECT b.id, 'failed', 0, now(), %(error_response)s, now()FROM {BATCH_TABLE} b- JOIN {DELTA_STATUS_VIEW} ds ON b.id = ds.batch_id- LEFT JOIN {DUCKGRES_STATUS_VIEW} dgs ON b.id = dgs.batch_id+ JOIN {_latest_status_lateral(STATUS_TABLE, "b")} ds ON true+ LEFT JOIN {_latest_status_lateral(DUCKGRES_STATUS_TABLE, "b")} dgs ON trueWHEREb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'AND b.run_uuid = %(run_uuid)s@@ -524,7 +561,7 @@ class DuckgresBatchQueue:SELECT{pending_batch_select_columns("dgs")}FROM {BATCH_TABLE} b- JOIN {DUCKGRES_STATUS_VIEW} dgs ON b.id = dgs.batch_id+ JOIN {_latest_status_lateral(DUCKGRES_STATUS_TABLE, "b")} dgs ON trueWHEREb.created_at > now() - interval '{PARTITION_PRUNING_INTERVAL}'AND dgs.job_state = 'executing'