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-warehouse): co-claim consecutive live batches in the duckgres sink (#72985)

Eric Duongcd9b9d165b84
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.py
index 6709094bf554..1e8aa24cf1c0 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
@@ -286,13 +286,14 @@ class DuckgresBatchQueue:
is the hard backstop. Teams without a mapping row are uncapped
(None = no caps at all, for tests/dev).
- Intra-run head-of-line: LIVE batches stay strictly ordered (any
- unapplied lower batch_index blocks). Backfill CHUNKS relax this so a
- whole run drains in one claim: a pending predecessor blocks a chunk
- only when it cannot be returned AHEAD of it in this same fetch (see the
- gate's inline comments). Co-claimable predecessors sort earlier, land
- in the same group, and the group is processed strictly in order with a
- halt on first non-success — so chunk 0's CREATE still applies first.
+ Intra-run head-of-line: a pending predecessor blocks a batch only when
+ it cannot be returned AHEAD of it in this same fetch (see the gate's
+ inline comments) — for LIVE batches and backfill CHUNKS alike, so a
+ run's consecutive delta-succeeded prefix drains in one claim (bounded
+ by ``limit``). Co-claimable predecessors sort earlier, land in the same
+ group, and the group is processed strictly in order with a halt on
+ first non-success — so chunk 0's CREATE (and a live run's lowest
+ batch) still applies first, and nothing ever applies past a gap.
Cross-run head-of-line: a batch is ineligible while an older run (by run
start time) of the same (team_id, schema_id) still has unapplied,
@@ -420,13 +421,14 @@ class DuckgresBatchQueue:
)
AND a.id IS NULL
AND (
- -- LIVE batches: any unapplied predecessor blocks.
- {LIVE_BATCH_SQL_PREDICATE}
- -- Backfill chunks: block only on predecessors that
- -- cannot be co-claimed AHEAD of this chunk —
- -- not delta-succeeded (fail closed; enqueue_chunks
- -- writes chunks pre-succeeded atomically):
- OR ds_prev.job_state IS DISTINCT FROM 'succeeded'
+ -- Both batch kinds co-claim: a predecessor blocks
+ -- only when it cannot be returned AHEAD of this
+ -- batch in this same fetch (live batches gained
+ -- this in 2026-07; one-at-a-time head-of-line
+ -- capped a group at ~1 batch per fetch rotation
+ -- and could not keep up with a fast producer).
+ -- Not delta-succeeded (fail closed):
+ ds_prev.job_state IS DISTINCT FROM 'succeeded'
-- sorting later in the fetch order (a reconcile
-- replay re-inserts dropped chunks with a fresh
-- created_at):
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.py
index 5b0997efdf6e..b7905bb93a0d 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
@@ -262,7 +262,11 @@ class TestDuckgresBatchQueueEligibility:
assert batches[0].latest_attempt == 1
@pytest.mark.asyncio
- async def test_enforces_prior_batch_apply_order(self, conn):
+ async def test_co_claims_consecutive_live_prefix_in_one_fetch(self, conn):
+ # A live run's delta-succeeded prefix drains in ONE claim (the chunk
+ # co-claim semantics): each fetch-rotation wait amortizes across the
+ # prefix instead of gating every batch — the head-of-line throughput
+ # ceiling behind the 2026-07 team-2 workflow_histories backlog.
first_id = await _insert_batch(conn, batch_index=0)
second_id = await _insert_batch(conn, batch_index=1)
await BatchQueue.update_status(conn, batch_id=first_id, job_state="succeeded", attempt=1)
@@ -270,8 +274,55 @@ class TestDuckgresBatchQueueEligibility:
batches = await DuckgresBatchQueue.get_delta_succeeded_and_lock(conn, owner_token="owner-a")
+ assert [batch.batch_index for batch in batches] == [0, 1]
+
+ @pytest.mark.asyncio
+ async def test_live_prefix_stops_at_a_non_delta_succeeded_gap(self, conn):
+ # Only the CONSECUTIVE delta-succeeded prefix is claimable: a gap fails
+ # closed exactly like the chunk gate, so ordering can never skip a hole.
+ first_id = await _insert_batch(conn, batch_index=0)
+ await _insert_batch(conn, batch_index=1)
+ third_id = await _insert_batch(conn, batch_index=2)
+ await BatchQueue.update_status(conn, batch_id=first_id, job_state="succeeded", attempt=1)
+ await BatchQueue.update_status(conn, batch_id=third_id, job_state="succeeded", attempt=1)
+
+ batches = await DuckgresBatchQueue.get_delta_succeeded_and_lock(conn, owner_token="owner-a")
+
assert [batch.batch_index for batch in batches] == [0]
+ @pytest.mark.asyncio
+ async def test_live_batch_blocked_behind_executing_predecessor(self, conn):
+ first_id = await _insert_batch(conn, batch_index=0)
+ second_id = await _insert_batch(conn, batch_index=1)
+ await BatchQueue.update_status(conn, batch_id=first_id, job_state="succeeded", attempt=1)
+ await BatchQueue.update_status(conn, batch_id=second_id, job_state="succeeded", attempt=1)
+ await DuckgresBatchQueue.update_status(conn, batch_id=first_id, job_state="executing", attempt=1)
+
+ batches = await DuckgresBatchQueue.get_delta_succeeded_and_lock(conn, owner_token="owner-a")
+
+ assert batches == []
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "backoff_seconds,expected_indexes",
+ [
+ (3600, []), # predecessor inside its backoff window gates the run
+ (0, [0, 1]), # backoff elapsed: predecessor and successor co-claim
+ ],
+ )
+ async def test_retry_backoff_gates_successor_live_batches(self, conn, backoff_seconds, expected_indexes):
+ first_id = await _insert_batch(conn, batch_index=0)
+ second_id = await _insert_batch(conn, batch_index=1)
+ await BatchQueue.update_status(conn, batch_id=first_id, job_state="succeeded", attempt=1)
+ await BatchQueue.update_status(conn, batch_id=second_id, job_state="succeeded", attempt=1)
+ await DuckgresBatchQueue.update_status(conn, batch_id=first_id, job_state="waiting_retry", attempt=1)
+
+ batches = await DuckgresBatchQueue.get_delta_succeeded_and_lock(
+ conn, owner_token="owner-a", retry_backoff_base_seconds=backoff_seconds
+ )
+
+ assert [b.batch_index for b in batches] == expected_indexes
+
@pytest.mark.asyncio
async def test_final_marker_waits_for_matching_data_batch_apply(self, conn):
data_id = await _insert_batch(conn, batch_index=0, is_final_batch=False)
@@ -280,13 +331,9 @@ class TestDuckgresBatchQueueEligibility:
await BatchQueue.update_status(conn, batch_id=final_id, job_state="succeeded", attempt=1)
batches = await DuckgresBatchQueue.get_delta_succeeded_and_lock(conn, owner_token="owner-a")
- assert [str(batch.id) for batch in batches] == [data_id]
-
- await DuckgresBatchQueue.mark_applied(conn, batch=batches[0])
- await DuckgresBatchQueue.unlock_for_batches(conn, batches=batches, owner_token="owner-a")
-
- batches = await DuckgresBatchQueue.get_delta_succeeded_and_lock(conn, owner_token="owner-a")
- assert [str(batch.id) for batch in batches] == [final_id]
+ # Co-claimed in one fetch, data batch strictly first — the group loop
+ # applies in order and halts before the final on any non-success.
+ assert [str(batch.id) for batch in batches] == [data_id, final_id]
@pytest.mark.asyncio
async def test_delta_failed_run_is_skipped(self, conn):