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.

chore(data-warehouse): rewrite postgres queue README for the lease model (#85406)

estefaniabcd30f3fa992
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/README.md b/products/warehouse_sources/backend/temporal/data_imports/pipelines/README.md
index 1b028b6b6f4c..30b5b49d67cd 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/README.md
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/README.md
@@ -22,7 +22,6 @@ This page is the map; each package's own docstrings and READMEs carry the detail
| v2 extract+load (`PipelineNonDLT`) | `pipeline_v2/` | Step 4 when `pipeline_version == V2`; writes Delta inline per chunk, so extract and load are one activity. The workflow finalizes. |
| v3 extract (`PipelineV3`) | `pipeline_v3/pipeline.py`, `pipeline_v3/s3/`, `pipeline_v3/postgres_queue/producer.py` | Step 4 when `pipeline_version == V3`, under the sync lock; writes parquet batches to S3 and enqueues them (`ExportSignalMessage`, `pipeline_v3/messages.py`) on the Postgres load queue. |
| v3 load | `pipeline_v3/postgres_queue/`, `pipeline_v3/batch_consumer.py`, `pipeline_v3/load/` | Its own deployment (`run_warehouse_sources_load`), not the workflow: claims queued batches, writes Delta via `core/delta/`, finalizes the job, releases the lock, starts post-import. |
-| DuckLake load (duckgres) | `pipeline_v3/duckgres/` | A second queue-consumer deployment (`run_warehouse_sources_duckgres_load`) applying the same batches to the DuckLake store, plus its backfill planner. |
| CDC | `../cdc/` | Outside the scheduled flow: the `cdc-extraction` Temporal workflow tails the source WAL and enqueues to the same Postgres queue (jobs are created as V3). The load consumer writes the snapshot table and the `_cdc` SCD2 companion (`core/delta/scd2.py`). |
| Webhooks | `run_webhook_s3_sink` command; sink behind the `data_warehouse` facade | Buffered ingress: deliveries are buffered to S3 continuously, and the next scheduled sync drains them as ordinary source items (webhook-only schemas skip table resets, `common/extract.py`). |
| Chunk sinks (CDP, person properties) | `core/sinks.py`, `core/cdp_producer.py`, `core/person_property_row_sink.py` | Gated taps on both write loops, one `stage_chunk` call per chunk (see `ChunkSink`). Downstream: `dwh-cdp-producer-job` produces staged chunks to Kafka for HogFunctions/HogFlows; `sync-warehouse-person-properties` plus `run_person_property_update_consumer` upsert person properties. |
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/README.md b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/README.md
index f5b75821256e..76826d60ed42 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/README.md
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/README.md
@@ -4,6 +4,9 @@
The Kafka-based consumer for warehouse sources load worked but was fragile. Because a single load could run long, we had to keep pushing max.poll.interval.ms higher (along with session timeouts and max.poll.records) just to stop the broker from evicting consumers mid-batch, every increase was a bandaid, not a fix. On top of that, most of the real logic lived outside the message: retry state in Redis, DLQ routing through a second producer, schema-level locks and progress tracking in yet another store. The message itself was barely more than a pointer; Kafka was carrying the notification while everything that actually mattered happened elsewhere. Every configuration knob we added was compensating for a mismatch between what Kafka gives you and what we actually need.
+The v3 Kafka transport is not a fallback: it was deleted outright in [#77651](https://github.com/PostHog/posthog/pull/77651).
+This queue is the only load path for v3.
+
## RudderStack's approach
RudderStack has been running warehouse loading on Postgres queues for 6+ years, handling 100K events/second at peak.
@@ -18,155 +21,92 @@ You can find more on:
## Our solution
-We took RudderStack's two-table model (jobs + append-only status) but kept things simpler (for now, at least)
+We took RudderStack's two-table model (jobs + append-only status) but kept things simpler (for now, at least).
+All SQL lives in `jobs_db.py`; the polling/retry/recovery engine is `../batch_consumer.py` and the Delta-sink adapter is `consumer.py`.
### What we use
-- **Two tables**: `sourcebatch` (the jobs) and `sourcebatchstatus` (append-only status log). A `DISTINCT ON` view (`v_latest_source_batch_status`) gives us the latest status per batch.
-- **Advisory locks** for cross-pod coordination: `pg_try_advisory_lock(namespace, hashtext(team_id:schema_id))` ensures no two pods process the same (team_id, schema_id) simultaneously. Session-level locks held for the duration of group processing, released explicitly after.
-- **Async consumer** (`consumer.py`): single asyncio process that polls, groups batches by (team_id, schema_id), processes groups concurrently via `asyncio.gather`, batches within a group sequentially.
-- **Recovery sweep**: periodic check (every 30s) for batches stuck in `executing` whose advisory lock is not held, those were owned by a crashed pod. Uses a separate connection so it doesn't interfere with the main lock session.
+- **Two tables, with state denormalized onto the jobs row**: `sourcebatch` (the jobs) and `sourcebatchstatus` (append-only status log).
+ Every status write is a single-statement dual write (`build_status_dual_write_sql`): insert the status row, then mirror `latest_state`, `latest_attempt` and `state_changed_at` onto the batch row.
+ Migration `0006_sourcebatch_latest_state` added those columns plus the partial indexes `sb_claimable_idx`, `sb_run_gate_idx` and `sb_schema_busy_idx`; `0008_sourcebatch_superseded` added `superseded` and `sb_failed_changed_idx`.
+ Migrations live in `products/warehouse_sources_queue/backend/migrations/`.
+ The `DISTINCT ON` view `v_latest_source_batch_status` still exists (created in `0001_initial`), but no query path reads it any more: the claim query and every sweep run off the denormalized columns, and per-batch latest-status lookups use a lateral `LIMIT 1` probe (`latest_status_lateral`).
+ The view is only useful for ad-hoc SQL when debugging by hand.
+- **Group leases** for cross-pod coordination: a row in `sourcegrouplease` keyed by `(team_id, schema_id)`, with a 300s TTL (`LEASE_TTL_SECONDS`).
+ A lease is claimed via a conditional upsert inside the claim query, renewed by the consumer heartbeat, and reclaimable by any pod once it expires.
+ This replaced the original session-level `pg_try_advisory_lock(namespace, hashtext(team_id:schema_id))` design: advisory-lock ownership is tied to a live server session, so it could be orphaned indefinitely on SIGKILL, pgbouncer session lingering, or node loss, wedging the whole loader fleet.
+ An abandoned lease simply expires instead (see the `jobs_db.py` module docstring).
+- **Claim-or-renew in one statement**: `get_unprocessed_and_lock` selects narrow claim candidates from the denormalized columns (with per-team round-robin fairness, head-of-line gating per run, a failed-run gate, and a schema-busy gate), then claims or renews the group leases for the winners inside a single writable CTE.
+ The candidate CTE is `MATERIALIZED` so its `LIMIT` fully resolves before the lease upsert runs, and candidate groups are deduplicated because `INSERT ... ON CONFLICT DO UPDATE` cannot touch the same lease row twice in one statement.
+ (The old README's `MATERIALIZED` rationale, preventing `pg_try_advisory_lock` from acquiring phantom locks in a `WHERE` clause, no longer applies: there are no advisory locks.)
+- **Async consumer**: single asyncio process that polls every ~2s, groups batches by `(team_id, schema_id)`, processes groups concurrently, batches within a group sequentially.
+ Each batch renews the lease on entry, heartbeats it (and re-inserts `executing` status) during processing, and verifies ownership before writing `succeeded`.
+- **Three sweeps**, not one:
+ - **Recovery sweep** (every 30s, `get_stale_executing`): batches whose latest status is `executing`, older than the 300s grace window, with no live lease over their group.
+ The sweep deletes the expired lease first so a resurrecting owner cannot renew it, then re-queues the batch as `waiting_retry` (with a compare-and-swap on the observed `state_changed_at`) or fails the run at max attempts.
+ - **Reconcile sweep** (every 300s, `reconcile_failed_runs` + `get_failed_runs`): finds runs with a `failed` queue batch whose `ExternalDataJob` was left non-terminal and marks the job failed.
+ It is single-flighted fleet-wide through a sentinel lease row (`try_acquire_reconcile_sweep_slot`, team_id 0 / `__reconcile-sweep__`, 240s slot TTL): the sweeps reconcile global state, so N pods running them concurrently is pure duplicated load, exactly when the queue DB can least afford it.
+ - **Stranded-run sweep** (same cadence and connection, `get_stale_stranded_runs`): runs the loader abandoned, meaning non-terminal batches, no live lease, and no loader progress for 6 hours.
+ These have no `failed` batch for the reconcile sweep to key on (the extraction died before a final batch), so without this pass they would strand until the retention prune.
- **Sync producer** (`producer.py`): runs inside Temporal activities, plain `psycopg.Connection` with autocommit. Each `send_batch_notification` is a single INSERT.
-- **New DB**: we created a new DB to store these two new tables
+- **New DB**: we created a new DB to store these tables.
+- **Daily range partitioning** on `created_at`: both tables use `PARTITION BY RANGE (created_at)` with daily partitions and a DEFAULT partition catching rows that miss one.
+ A Temporal scheduled workflow (`warehouse-sources-queue-partition-management`, daily at 8 AM UTC) creates the next 7 days of partitions, drops partitions older than 7 days, and prunes the matching S3 extraction prefixes on the same retention.
+ `DROP TABLE partition` is O(1) metadata-only: no vacuum, no dead tuples.
+- **Claim eligibility coupled to retention**: a batch is only claimable (or recovery-sweepable) while younger than `CLAIM_ELIGIBILITY_INTERVAL` (`6 days 12 hours`, `jobs_db.py`), which must stay below the 7-day retention window (`RETENTION_DAYS` in `posthog/temporal/warehouse_sources_queue_partition_management/activities.py`).
+ Otherwise a claimed batch's extraction parquet may already be deleted from S3 when the loader reads it.
+ `test_eligibility_window_stays_below_retention_window` in `test_jobs_db.py` enforces the coupling.
+- **Partition pruning bounds**: consumer queries include `created_at > now() - interval '14 days'` (2x the retention) so the planner can skip dropped partitions.
+
+### Query cost scales with the answer, not with history
+
+The old claim "our poll query is cheap enough with proper indexing" did not survive contact with a production backlog.
+Three changes in August 2026 restructured the hot queries after a production loader stall:
+
+- [#82765](https://github.com/PostHog/posthog/pull/82765): the claim query's fairness sort ran over full-width rows (~1 KB of `metadata` each), so a backlog made every poll sort megabytes to keep ~50 rows, spilling past `work_mem` to disk. Candidates are now narrow `(id, created_at)` pairs, joined back only for the LIMIT winners.
+- [#83022](https://github.com/PostHog/posthog/pull/83022): the failed-run reconcile ran a per-batch latest-status probe for every failed batch in the lookback window, so a failure storm made each sweep take minutes. It now picks winners from the denormalized columns and probes status only for the LIMIT winners.
+- [#83067](https://github.com/PostHog/posthog/pull/83067): the stranded-run sweep gated raw batch rows, which the planner turned into a hash anti-join over every retained failed batch. It now aggregates into candidate runs first, so each gate is one index probe per run.
+
+The shared lesson: every query on these tables must scale with the size of its answer (the claimable set, the candidate runs), never with retained failure history, because failure history is largest exactly when the fleet is least healthy.
+The `jobs_db.py` docstrings on `_state_claim_candidates_sql`, `get_failed_runs` and `_stranded_candidate_runs_sql` carry the details, and plan-shape tests in `test_jobs_db.py` pin the query shapes.
+
+### Adding a `sourcebatch` index
-- **Daily range partitioning** on `created_at`: both tables use `PARTITION BY RANGE (created_at)` with daily partitions. Composite PK `(id, created_at)` is required by Postgres for partitioned tables. A DEFAULT partition catches rows that miss a partition. A Temporal scheduled workflow (`warehouse-sources-queue-partition-management`) runs every 6 hours to create the next 7 days of partitions and drop partitions older than 7 days. `DROP TABLE partition` is O(1) metadata-only — no vacuum, no dead tuples.
-- **MATERIALIZED CTE for advisory locks**: `pg_try_advisory_lock` in a WHERE clause with LIMIT can acquire phantom locks on rows later discarded. All consumer queries use a MATERIALIZED CTE to fully resolve candidates before probing locks.
-- **Partition pruning bounds**: all consumer queries include `created_at > now() - interval '14 days'` (2x the 7-day retention) so the planner can skip dropped partitions.
+Never create a new index directly on the partitioned parent in one shot: a plain `CREATE INDEX` there recurses into every partition under a lock that blocks the claim path for the length of the build, and `CREATE INDEX CONCURRENTLY` is not supported on a partitioned parent.
+The required pattern, established with `0008_sourcebatch_superseded` (read it as the reference implementation): create the parent index with `ON ONLY` (metadata only, invalid), build a matching index on each partition with `CREATE INDEX CONCURRENTLY`, then `ALTER INDEX ... ATTACH PARTITION` each one; attaching the last partition flips the parent index valid.
+The migration must be `atomic = False` and re-runnable (a cancelled `CONCURRENTLY` build leaves an invalid index that must be dropped and rebuilt).
### What we left out
- **COPY bulk inserts**: the producer inserts one row per batch. At our volume, row-level INSERT is fine. (We can always move to bulk insert if needed)
- **Compaction**: RudderStack runs a background process to merge/drop completed datasets, it stores tables up to 100k rows and then they roll to the next one. We don't need it at the moment, but if we end up implementing rolling datasets, we will implement this compaction too.
-- **Caching layers**: RudderStack uses "no jobs" caches and active pipeline caches to reduce query load. Our poll query is cheap enough with proper indexing.
-- **Recursive CTE loose index scans**: their trick for finding distinct pipeline IDs efficiently. Our `get_unprocessed_and_lock` query is simple enough without it.
+- **Caching layers**: RudderStack uses "no jobs" caches and active pipeline caches to reduce query load. We have not needed them, but only because the claim path was restructured to run off partial indexes and denormalized state (see above); "proper indexing" alone was not enough.
+- **Recursive CTE loose index scans**: their trick for finding distinct pipeline IDs efficiently. Our claim query gets the same effect from the partial indexes.
- **Active Partitions**: RudderStack designs a partition and then it assigns each partition to one processor instance, this is similar to how Kafka partitions work. We don't need this, this will be an overkill as we don't have any specifics for a partition.
### Architecture
![Postgres queue architecture](./20260423%20-%20PostgresArchitecture.png)
-## Things to look out for during roll out
+This diagram predates the lease model: it shows `pg_try_advisory_lock` coordination, `SELECT ... FOR UPDATE SKIP LOCKED`, and status UPDATEs, none of which exist any more.
+The overall topology (Temporal producer activity, two tables, a fleet of consumer pods polling in a loop) is still right; read the "What we use" section above for the current coordination design.
+
+## Metrics and health signals
+
+The consumer engine emits nine `warehouse_pg_consumer_*` metrics (`metrics.py`): `batches_processed_total`, `batch_processing_duration_seconds`, `batch_retry_total`, `runs_failed_total`, `poll_duration_seconds`, `poll_batches_fetched`, `poll_failures_total`, `active_groups`, `recovery_sweeps_total`.
+Other sinks get that nine-metric engine set under their own `{prefix}_pg_consumer_*` names via `make_consumer_metrics`, so dashboards and KEDA queries never conflate two consumers' series.
+Two further counters exist only under the `warehouse_pg_consumer_*` prefix, incremented by the Delta consumer's reconcile sweep (`consumer.py`) rather than by the shared engine: `runs_reconciled_total` and `runs_terminalized_stale_total`.
+
+The headline health signal is `warehouse_pg_queue_oldest_unclaimed_batch_seconds`: the age of the oldest batch no consumer has picked up yet.
+It is the loader's data-freshness signal and rises whenever loading stalls, regardless of cause, so its alert fires even when every other signal looks green.
+Its depth companion `warehouse_pg_queue_claimable_batches` says how much work sits behind that head; a stall and a burst look identical on age alone.
+Every pod reports the same queue-wide values on the reconcile cadence; aggregate both gauges with `max()`.
+Failed polls record their elapsed time in `poll_duration_seconds`, so degraded polls stay visible in the latency percentiles; `poll_failures_total` carries the reason label and is the alertable poll-health counter.
+The maintenance queries (sweeps, reconcile passes, probes) report through `warehouse_pg_queue_query_duration_seconds` (labeled per query, observed on failure and timeout too) and `warehouse_pg_queue_query_failures_total`; the August 2026 stall came from a query with no latency signal at all.
+
+For ad-hoc inspection (state summaries, active runs, leases, force-release), use the `manage_warehouse_queue` management command.
+
+## Things to look out for
- **Partition health**: monitor that the `warehouse-sources-queue-partition-management` Temporal schedule is running. Alert on rows landing in DEFAULT partitions (means partition creation failed).
-- **DB Load**: we will keep an eye on the load and the locks to make sure the tables can hold the full load of warehouse sources
-
-## Extra. Paths (CLAUDE GENERATED)
-
-All state transitions are **inserts** into `sourcebatch`; rows in `sourcebatch` are never updated.
-The latest state per batch comes from the view `v_latest_source_batch_status` (`DISTINCT ON (batch_id) ... ORDER BY batch_id ASC, id DESC`).
-The consumer uses an **autocommit** connection, a **session-level** `pg_try_advisory_lock(ns, hashtext("team_id:schema_id"))`, and polls every ~2s for up to `poll_limit` pending batches via `get_unprocessed_and_lock`.
-
-### Happy path
-
-```text
-t=0 Producer (Temporal activity, autocommit):
- INSERT batch 1 (run=R, team=T, schema=S, batch_index=1)
- INSERT batch 2 (run=R, team=T, schema=S, batch_index=2)
- No status rows yet -> the view reports these as unprocessed.
-
-t=1 Consumer A poll tick (get_unprocessed_and_lock):
- SELECT ... FROM batch b
- LEFT JOIN v_last_external_data_job_batch_status s ON b.id = s.batch_id
- WHERE (s.batch_id IS NULL OR s.job_state = 'waiting_retry')
- AND b.run_uuid NOT IN (<runs with any failed batch>)
- AND pg_try_advisory_lock(ns, hashtext(b.team_id || ':' || b.schema_id))
- ORDER BY b.id ASC LIMIT N;
- -> returns batches 1 and 2; advisory lock for (T,S) is held on this session.
-
-t=2 _group_by_key groups results by (team_id, schema_id):
- groups[(T,S)] = [batch 1, batch 2]
- asyncio.gather runs each group via _process_group; batches within
- a group are awaited sequentially.
-
-t=3 _process_single(batch 1):
- INSERT status(batch=1, job_state='executing', attempt=1) -- pre-increment
- await process_batch(batch 1) -- loads parquet into Delta Lake
- INSERT status(batch=1, job_state='succeeded', attempt=1)
-
-t=4 _process_single(batch 2): same sequence, attempt=1 -> succeeded.
-
-t=5 End of _process_group (finally):
- SELECT pg_advisory_unlock(ns, hashtext('T:S'))
- (Also released at shutdown via pg_advisory_unlock_all.)
-
-t=6 Producer inserted batches 3, 4 in the meantime.
- They are picked up on a later poll tick -- by this pod or another.
- The advisory lock arbitrates which pod drains the group at any
- given moment; there is no inner loop that picks up new work
- mid-group.
-```
-
-### Transient failure + retry
-
-```text
-tX _process_single(batch K):
- INSERT status(K, 'executing', attempt=1)
- await process_batch(K) -- raises (e.g. S3 read timeout)
- Caught in _process_single:
- if attempt >= max_attempts (3) -> _fail_run
- else -> INSERT status(K, 'waiting_retry', attempt=1, error_response={...})
-
-tX+1 Next poll: batch K matches (s.job_state = 'waiting_retry'), lock still
- acquirable -> returned again. _process_single runs with
- attempt = latest_attempt + 1 = 2.
-```
-
-### Max retries exceeded
-
-```text
- _process_single on the final attempt raises -> _fail_run(batch, reason):
- fail_run(conn, run_uuid) issues a single INSERT ... SELECT that
- appends a 'failed' status for every batch in the run whose current
- state is NULL / waiting / waiting_retry / executing.
- _update_job_status_to_failed marks the ExternalDataJob as FAILED
- via the Django ORM (sync_to_async).
- Subsequent polls skip the whole run via the
- NOT IN (<runs with any failed batch>) filter.
-```
-
-### OOM / pod crash mid-batch (recovery sweep)
-
-```text
-t=0 _process_single(batch K):
- INSERT status(K, 'executing', attempt=k) -- commits (autocommit)
- load starts
-
-t=1 OS OOM-killer SIGKILLs the pod.
- Postgres detects the connection drop and auto-releases the
- session-level advisory lock. Latest state of K stays 'executing'
- in the view -- that row is durable.
-
-t=2 Every 30s, _recovery_loop on some consumer runs _recovery_sweep
- against its dedicated _recovery_conn:
- SELECT b.*, COALESCE(s.attempt, 0) FROM batch b
- JOIN v_last_external_data_job_batch_status s ON ...
- WHERE s.job_state = 'executing'
- AND pg_try_advisory_lock(ns, hashtext(b.team_id || ':' || b.schema_id))
- ORDER BY b.id ASC;
- Only orphans pass the filter -- if another pod is still processing
- (T,S), pg_try_advisory_lock returns false. Each returned row's
- probe lock is immediately released with pg_advisory_unlock.
-
-t=3 For every orphan:
- if latest_attempt >= max_attempts -> _fail_run
- (reason: "max retries exceeded (likely OOM)")
- else -> INSERT status(K, 'waiting_retry', attempt=latest_attempt)
- No +1: the 'executing' insert at t=0 already consumed an attempt.
-
-t=4 On the next main poll, batch K is picked up via the
- 'waiting_retry' branch and retried (attempt becomes
- latest_attempt + 1 on re-execute).
-```
-
-### Graceful shutdown (SIGTERM / SIGINT)
-
-```text
- Signal handler sets self._shutdown.
- Main loop checks _shutdown.is_set() and exits after the current
- asyncio.gather completes. Inside _process_group, the per-batch
- loop also checks _shutdown and breaks early, leaving remaining
- batches in the group for the next consumer (their state is
- still 'waiting' -- no 'executing' row was written).
- _close cancels the recovery task, runs pg_advisory_unlock_all
- on the main connection, and closes both connections.
-```
+- **Poll duration vs lease TTL**: leases are claimed when the poll query starts, so a poll slower than half the 300s TTL hands groups over mostly expired; the consumer logs `poll_duration_approaching_lease_ttl` when this starts happening.