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(warehouse-sources): cast jsonb_build_object args in retire_backfill_run (#73299)

Eric Duong93196e19c68f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/backfill_queue.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/backfill_queue.py
index 9dd0a94a3535..a85ec33340bb 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/backfill_queue.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/backfill_queue.py
@@ -235,7 +235,10 @@ def retire_backfill_run(conn: psycopg.Connection[Any], *, run_uuid: str) -> None
conn.execute(
f"""
INSERT INTO sourcebatchduckgresstatus (batch_id, job_state, attempt, error_response)
- SELECT b.id, 'failed', 0, jsonb_build_object('error', %(reason)s, 'kind', %(kind)s)
+ -- jsonb_build_object is VARIADIC "any", so bare placeholders are
+ -- type-indeterminate to Postgres ("could not determine data type of
+ -- parameter"); cast the string args explicitly.
+ SELECT b.id, 'failed', 0, jsonb_build_object('error', %(reason)s::text, 'kind', %(kind)s::text)
FROM {BATCH_TABLE} b
LEFT JOIN sourcebatchduckgresapply a
ON a.team_id = b.team_id AND a.schema_id = b.schema_id
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 b7905bb93a0d..0217ff501e2d 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
@@ -7,6 +7,11 @@ import psycopg
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.duckgres.backfill import (
_has_inflight_replace_run,
)
+from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.duckgres.backfill_queue import (
+ KIND_RETIRED_BY_REPLAN,
+ REASON_RETIRED_BY_REPLAN,
+ retire_backfill_run,
+)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.duckgres.jobs_db import (
DUCKGRES_APPLY_TABLE,
DUCKGRES_LEASE_TABLE,
@@ -1146,4 +1151,39 @@ class TestOrgConcurrencyBudget:
# org-a: 1 live lease of budget 1 -> saturated; org-b: 1 of 2 -> not.
assert await DuckgresBatchQueue.count_orgs_at_budget(conn, team_org_budgets=one_each) == 1
+
+
[email protected]_db(transaction=True)
+class TestRetireBackfillRun:
+ @pytest.mark.asyncio
+ async def test_marks_unapplied_chunks_failed_with_replan_reason(
+ self, conn: psycopg.AsyncConnection[Any], _db_url: str
+ ) -> None:
+ # Regression: retire_backfill_run built error_response with bare
+ # placeholders inside jsonb_build_object (a VARIADIC "any" function), so
+ # Postgres raised IndeterminateDatatype ("could not determine data type
+ # of parameter $1") on EVERY replan of a schema that still had a live
+ # backfill_run_uuid -- silently blocking operator un-sticking. The
+ # ::text casts fix it; this test executes the real SQL that regressed.
+ run_uuid = "run-to-retire"
+ b0 = await _insert_batch(conn, run_uuid=run_uuid, batch_index=0)
+ b1 = await _insert_batch(conn, run_uuid=run_uuid, batch_index=1)
+ # An already-applied chunk must be left alone (the a.id IS NULL filter).
+ applied = await _insert_batch(conn, run_uuid=run_uuid, batch_index=2)
+ await _mark_applied_raw(conn, batch_id=applied, run_uuid=run_uuid, batch_index=2)
+
+ # retire_backfill_run takes a *sync* psycopg connection (it runs inline
+ # in the consumer fetch path), so open one against the same test DB.
+ with psycopg.Connection.connect(_db_url, autocommit=True) as sync_conn:
+ retire_backfill_run(sync_conn, run_uuid=run_uuid)
+
+ cur = await conn.execute(f"SELECT batch_id, job_state, error_response FROM {DUCKGRES_STATUS_TABLE}")
+ rows = await cur.fetchall()
+
+ retired = {str(batch_id): (job_state, err) for batch_id, job_state, err in rows}
+ assert set(retired) == {str(b0), str(b1)} # applied chunk untouched
+ for job_state, err in retired.values():
+ assert job_state == "failed"
+ assert err["error"] == REASON_RETIRED_BY_REPLAN
+ assert err["kind"] == KIND_RETIRED_BY_REPLAN
assert await DuckgresBatchQueue.count_orgs_at_budget(conn, team_org_budgets=[]) == 0