Skip to content

Alert Runbooks

One section per TaskQ Prometheus alert, in the shape an on-call reader needs first: what fired, how to confirm it against the database (not just the metric), how to remediate, and (for the backlog alerts) how to tell under-provisioned (add workers) from stalled (fix the sweep/database; scaling out makes it worse).

The shipped alert rules live in src/taskq/contrib/prometheus/rules.yaml (plain) and src/taskq/contrib/kubernetes/prometheus_rule.yaml (PrometheusRule CRD); the two carry identical alerts.

SQL examples assume the default schema (TASKQ_SCHEMA_NAME=taskq); substitute your schema. Timestamps in the jobs table are written by the database clock, which is why every age check below uses clock_timestamp() server-side instead of subtracting in your own clock domain.


TaskQQueueDepthHigh

What fired. max by (actor, queue) (taskq_jobs_oldest_pending_age_seconds) > 900 for 5 minutes: the oldest PENDING job of some (actor, queue) pair has been eligible for more than 15 minutes, sustained. The gauge is attributed per actor and per queue on purpose, not per queue: in a multi-worker fleet no worker refuses to start because an actor's declared queue has no consumer (no supervisor can know what consumes a queue), so a misrouted actor produces no refusal, no error and no failed job, and its rows pile up pending while every probe stays green. Age rather than depth, because a deep queue that drains is healthy throughput.

How to confirm.

  • Metric: taskq_jobs_oldest_pending_age_seconds{actor="...", queue="..."} climbing while the same queue's other actors stay flat is the misroute-or-starvation shape; the whole queue rising together is shared capacity. Read taskq_queue_live_workers{queue="..."} beside it: a zero there is the never-consumed case, which TaskQQueueUnserved owns.
  • SQL: the pending head of every (actor, queue) pair right now:
SELECT actor, queue, count(*) AS depth,
       extract(epoch FROM (clock_timestamp() - min(scheduled_at)))::int AS oldest_age_s
FROM taskq.jobs
WHERE status = 'pending' AND scheduled_at <= clock_timestamp()
GROUP BY actor, queue
ORDER BY oldest_age_s DESC;
  • The gauge is fed by the leader's actor_backlog sampler. If the series went absent or frozen exactly when the incident got worst, the sampler read is what died; check TaskQSweepTimeouts before trusting the (now silent) gauge.

How to remediate.

  1. No live worker subscribes to the queue: add the queue to some worker's TASKQ_QUEUES and restart it, or run a worker that serves it. The pile dispatches on its own once a consumer exists; nothing was lost.
  2. Live workers present and draining slowly: the queue is under-provisioned. Add workers or raise TASKQ_MAX_CONCURRENCY on the fleet that serves it.
  3. One actor aging while its queue-mates drain: check for a damper holding its rows back, the actor's own max_concurrent or a queue cap from taskq queues set-max-concurrent; the alert shows the head's age, the cap is the cause.
  4. Confirm recovery: the age falls under the threshold and the depth drains.

Do not clear the gauge by deleting or re-enqueueing the pending pile: the rows are eligible work that dispatches in order once capacity exists, and re-enqueueing duplicates them.


TaskQScheduledBacklogGrowing

What fired. taskq_jobs_oldest_due_age_seconds > 300 and (taskq_jobs_scheduled_count > (taskq_jobs_scheduled_count offset 5m) or changes(taskq_jobs_scheduled_count[5m]) == 0) for 5 minutes: the oldest due job has been waiting more than 5 minutes AND the scheduled job count is demonstrably not draining: either it is HIGHER than it was 5 minutes ago (promotion from scheduled to pending is not keeping up with arrivals) or it has not moved at all across those 5 minutes (promotion has stopped and no arrivals are landing net, the stalled plateau). Either way, due work is waiting while the scheduled backlog fails to shrink, not merely one slow-to-clear job.

An earlier form of this alert joined the oldest-due-age gauge against its own value 5 minutes back. That self-join is satisfied by a perfectly healthy, steadily draining backlog for the entire time its current straggler waits its turn: the age of "whichever job is currently oldest" climbs monotonically right up until that one job is promoted, regardless of how healthily everything behind it drains, so the join degenerated to a bare age > 300 threshold and paged on healthy operation. Count, not the single oldest item's age, is what distinguishes "stalled" from "one slow straggler": a scheduled count that is moving (falling as jobs promote, rising as arrivals land) is healthy flow no matter how long the current straggler has waited. What is never healthy is a due job aging past the threshold while the count never moves at all, and a strict growth comparison cannot see it: a flat count is never > itself 5 minutes back, so a promoter that has stopped completely while arrivals are absent (or throttled, or backpressured) would stay silent forever on the growth arm alone. That is the stalled plateau the changes(...) == 0 arm catches.

taskq_jobs_scheduled_count is a label-less twin of taskq_jobs_by_status{status="scheduled"}, sampled by the same leader tick; it exists so every operand of the alert carries an identical (empty) label set. Prometheus pairs the two sides of a vector and/or (or comparison) only when their label sets are identical, with no on/ignoring modifier here to reconcile a mismatch, and it reports a non-matching join as an empty result rather than an error, so a version of this alert that joined the per-status depth gauge directly against the label-less age gauge could never fire, however bad the stall.

How to confirm.

  • Metrics: taskq_jobs_oldest_due_age_seconds climbing; taskq_jobs_scheduled_count (equivalently taskq_jobs_by_status{status="scheduled"}) either rising while {status="pending"} is flat (outpaced by arrivals) or not moving at all (the stalled plateau).
  • SQL: jobs that are due for promotion right now:
SELECT count(*) FROM taskq.jobs
WHERE status = 'scheduled' AND scheduled_at <= clock_timestamp();

A persistently non-zero count on the leader is the confirmation; a healthy promotion sweep drives it to zero (or near zero) every second.

How to remediate. This alert is the derivative and the age: depth alone can be a legitimate burst, but due jobs aging past 5 minutes is not. Follow the triage below before touching replica counts.

Under-provisioned or stalled?

  • Under-provisioned → add workers: queue depth high, taskq_jobs_oldest_due_age_seconds low or falling, and taskq_maintenance_leader_sweep_last_success_seconds{sweep_name="scheduled_to_pending"} fresh (moving every second). Dispatch capacity is the bottleneck; scale out.
  • Stalled → fix the sweep/database, do NOT scale out: taskq_jobs_oldest_due_age_seconds climbing while the scheduled count rises or never moves. The usual signature is the sweep's last_success stamp gone stale: promotion is not running at all. A FRESH stamp with a frozen count is the subtler shape: the sweep completes but promotes nothing, so check TaskQSweepTimeouts and TaskQSweepDegraded and confirm with the due-jobs SQL above. No amount of extra workers promotes a scheduled job; only the leader's sweep does. Scaling a stalled engine adds database load without adding progress. See TaskQPromotionStalled and continue there.

TaskQPromotionStalled

What fired. time() - taskq_maintenance_leader_sweep_last_success_seconds{sweep_name="scheduled_to_pending"} > 120 for 2 minutes: the scheduled_to_pending sweep has not completed in 2 minutes. Three cadences are in play and they are not the same number: the promotion tick runs every second (worker/leader.py's scheduled_wake loop), sweep_interval — the unit the staleness bound is expressed in — defaults to 30 s, and three missed sweep_intervals — the /ready degraded criterion — is a stall, not slowness, while this alert's own threshold is the fixed 120 s. This is the same signature as the 12,732-job incident, where a second schema in one database silently held the maintenance lock and promotion stopped while everything else looked green.

How to confirm.

  • Metric: the last_success stamp for sweep_name="scheduled_to_pending" frozen while the process is up; taskq_jobs_by_status{status="scheduled"} climbing.
  • SQL: due jobs accumulating:
SELECT count(*), min(scheduled_at) FROM taskq.jobs
WHERE status = 'scheduled' AND scheduled_at <= clock_timestamp();
  • SQL: is anything holding this schema's maintenance advisory lock? The lock key is schema-qualified (hashtextextended('taskq:maintenance_leader:<schema>', 0), built by taskq.constants.schema_lock_name), so it appears with classid = 0:
SELECT pid, locktype, classid, objid, granted
FROM pg_locks
WHERE locktype = 'advisory' AND classid = 0;

A granted = false waiter with an old pid, or a holder whose pid maps to a session of this schema that should not be leader (a stuck partitioned holder), is the finding. The classic second-schema cause: another schema's deployment silently holding the one shared lock, fixed by the schema-qualified names (see TaskQLeaderLockContention); a holder from another schema now takes a different key and cannot stall this one.

Clock-domain caveat (both sides of the difference are wall clock). The alert's expression computes time() - <gauge> in the wall-clock domain on both operands: the gauge is stamped from time.time() inside the leader process (see the metric's description) and Prometheus's time() is the scrape-time wall clock. A backward clock step (an NTP correction, a manual change, a VM restored from a suspended state) moves time() behind the stamp, so the expression reads negative and stays under the 120 s threshold until wall time has caught up with the stale stamp: a sweep that stopped completing reads fresh for the whole catch-up window and this alert is muted for exactly that long. A forward step has the opposite polarity: it can fire the alert one jump early. This caveat is scoped to the Prometheus alert expression and to the sweep_last_success_seconds gauge it reads — both operands are wall clock. The readiness body's in-process maintenance view is deliberately NOT in that scope: worker/health.py computes its staleness from the monotonic ledger (_sweep_success_monotonic_cache, time.monotonic()), which cannot step backwards, so /ready's maintenance section is trustworthy through NTP corrections and every other wall-clock event (the two-ledger design — a wall stamp recorded beside every monotonic one at the same call site — is documented at obs/_otel.py's record_sweep_success). If a promotion stall is suspected during or right after a clock correction, do not trust the alert expression: run the due-jobs SQL below and watch the last_success stamp's own movement, which is a timestamp, not a difference — and trust /ready, whose maintenance view has no such exposure.

How to remediate.

  1. Check TaskQSweepTimeouts and TaskQSweepDegraded; if either is firing, the database cannot finish the sweep's batches and that is the root cause.
  2. Check {schema}.maintenance_leader.expires_at. A lease that has lapsed and that no pod takes over means the survivors cannot reach or write that table; check their election-attempt-failed logs and the application role's grants on it. Recovering the role needs nothing else: no privilege over other sessions, and no manual intervention in the database.
  3. If no lock contention and no timeouts: check leader health (sum(taskq_maintenance_leader_is_leader) == 1; the TaskQLeaderSplitBrainOrNoLeader alert covers the zero-leader case) and Postgres connectivity/latency from the leader pod.
  4. After the cause is fixed, confirm recovery: the last_success stamp moves again and the due-jobs count from the SQL above drains to zero.

Do not scale out. Promotion is a leader-side sweep, not worker capacity. Extra workers consume pending jobs faster but promote nothing.


TaskQSweepTimeouts

What fired. rate(taskq_maintenance_leader_sweep_timeouts_total[5m]) > 0 for 5 minutes: sweep batches are being aborted by deadlines (TimeoutError on the client) or server-side cancels (QueryCanceledError from statement_timeout): the database cannot finish bounded batches, or a gauge sampler's read did not complete. The sweep_name label says which: sweep names are aborted batches; the sampler names (queue_depth, backlog_detection, actor_backlog, reservation_slots, wf_progress, chain_barriers, terminal_hooks, cancel_pending, retry_ladder, ratelimit_buckets) are reads that did not happen, counted for every failure class; a dead sampler's gauges go stale or absent while nothing else names the loss, and the per-actor backlog read dying resolves TaskQQueueDepthHigh at the exact moment the incident it alerts on is killing the read, which is why that failure must land here. wf_progress is the sampler behind TaskQWorkflowBlockedStuck, chain_barriers behind the two armed-barrier gauges, terminal_hooks behind the two terminal-hook backlog gauges, and cancel_pending behind taskq_jobs_cancel_pending: while a sampler is dead its alert is reading a frozen (or absent) series, not a cleared one.

How to confirm.

  • Metric: taskq_maintenance_leader_sweep_timeouts_total rising, labeled by sweep_name; TaskQSweepDegraded often follows once the batch-size breaker latches. A sampler name rather than a batch name means a gauge read is failing; read that gauge beside the counter: an absent or frozen series while this rate rises is the dead-sampler signature, never a resolved alert.
  • SQL: what the sweeping session is doing when it dies (run while the rate is non-zero):
SELECT pid, state, wait_event_type, wait_event, now() - query_start AS age,
       left(query, 120) AS query_head
FROM pg_stat_activity
WHERE query ILIKE '%job_events%' OR query ILIKE '%taskq%';

Long age with wait_event_type = 'Lock' points at a lock pile-up; age near TASKQ_EVENT_WRITER_STATEMENT_TIMEOUT_MS with no wait event points at plain slowness (I/O, bloat, plan regression).

How to remediate.

  1. Identify the wait from the SQL above. Lock pile-up: find and clear the blocking session (pg_blocking_pids()). Slowness: check table bloat and indexes on the tables the sweep writes (job_events, jobs).
  2. If the database is genuinely slower than the batch budget, lower TASKQ_EVENT_WRITER_BATCH_SIZE (each batch gets smaller; the sweep drains the remainder across more batches) rather than raising TASKQ_EVENT_WRITER_STATEMENT_TIMEOUT_MS past the reclaim_event_visibility_delay margin; the margin is what keeps out-of-commit-order reclaim events from being silently missed.
  3. Remember the reduced tier is a degradation ceiling, not a fix: if TaskQSweepDegraded is firing alongside this alert, the worker has already latched to reduced batches and the database problem is still there.

TaskQSweepUnexpectedErrors

What fired. rate(taskq_maintenance_leader_sweep_unexpected_errors_total[5m]) > 0 for 5 minutes: prune-family sweep batches (the prune and archive_expiry sweeps) are being aborted by an error outside the deadline family, a constraint violation, a connection reset, or any other fault that is not a statement cancel. TaskQSweepTimeouts covers the deadline family only; this counter is the rest, and without it a drain stopped by this class reads healthy on every success-path metric: the row counters and success stamps only move on success, so a prune that keeps failing looks like a quiet day.

How to confirm.

  • Metric: taskq_maintenance_leader_sweep_unexpected_errors_total rising, labeled by sweep_name (prune or archive_expiry). The prune-failed / archive-expiry-failed error log lines carry the error repr next to each increment; the exception class is the diagnosis.
  • Cadence: one increment per aborted batch, and the first aborted batch ends the sweep attempt, so the counter advances on the retry ladder (TASKQ_SWEEP_BREAKER_* backoff, 60s doubling to a 1800s cap) rather than per batch per second. A rate that stays positive over the 5m window is a persistent failure, not a busy drain.
  • Latch: the batch-size breaker counts these failures the same as deadline failures, so TaskQSweepDegraded firing alongside means the worker has latched to the reduced tier and is still failing there.

How to remediate.

  1. Read the exception class on the prune-failed log line. A UniqueViolationError against the archive tables usually means duplicate archive targets: check for a second writer inserting into jobs_archive (a sibling taskq deployment pointed at the same schema), or a partially-restored backup.
  2. A connection-class error (PostgresConnectionError, OSError): treat as a database or network incident; the retry ladder resumes the drain when the connection is back.
  3. Anything else: the error class is new, the reduced tier is the safety net that keeps the batches small while you look. If the sweep stays stopped, the retention backlog grows silently behind TaskQSweepDegraded; escalate before the backlog hits dispatch latency.

TaskQSweepDegraded

What fired. taskq_maintenance_leader_sweep_batch_size < taskq_maintenance_leader_sweep_batch_size_configured (for 0m, page immediately): a sweep is running at the reduced batch tier. The worker itself is reporting an unhealthy database: the batch-size breaker only latches after repeated batch cancellations, and it does not unlatch for the rest of the process lifetime. Both series are emitted by the same worker under the same sweep_name label, so the comparison always tracks that worker's own TASKQ_EVENT_WRITER_BATCH_SIZE configuration; no threshold to maintain.

How to confirm.

  • Metric: taskq_maintenance_leader_sweep_batch_size per sweep_name below the same worker's taskq_maintenance_leader_sweep_batch_size_configured (the configured TASKQ_EVENT_WRITER_BATCH_SIZE).
  • SQL: the sweep is still making progress, just slower:
SELECT count(*) FROM taskq.jobs
WHERE status = 'scheduled' AND scheduled_at <= clock_timestamp();

Combined with TaskQSweepTimeouts history: the breaker latched because batches kept being cancelled within TASKQ_SWEEP_BREAKER_WINDOW_SECS.

How to remediate.

  1. Treat this as the worker's own verdict on the database: find and fix the underlying slowness (see TaskQSweepTimeouts).
  2. Restart the worker (or let the orchestrator recycle it) after the database is healthy again; the breaker does not unlatch, so the reduced tier persists until a fresh process.

TaskQDispatchLatencyHigh

What fired. histogram_quantile(0.99, rate(taskq_dispatch_duration_seconds_bucket[5m])) > 0.05 for 5 minutes, by queue: the dispatch query's p99 exceeds 50 ms. The histogram times the claim round trip's SQL execution only, not actor run time, so this is the database getting slow at handing out work. The threshold is set near where the claim's FOR UPDATE SKIP LOCKED plan stops being index-shaped: a healthy dispatch round is sub-millisecond, so a 50 ms p99 is a plan or contention problem, not a tuning nit.

How to confirm.

  • Metric: taskq_dispatch_duration_seconds p99 by queue. One queue high while the rest are flat points at that queue's claim path; every queue high at once points at the database.
  • SQL: what the dispatching sessions are waiting on while the alert fires:
SELECT pid, state, wait_event_type, wait_event,
       now() - query_start AS age, left(query, 120) AS query_head
FROM pg_stat_activity
WHERE query ILIKE '%SKIP LOCKED%' OR query ILIKE '%taskq%';

wait_event_type = 'Lock' with short ages is dispatcher-on-dispatcher contention; ages near the threshold with an I/O wait is heap or index pressure on jobs. - Plan check: the claim reads the pending partial index (jobs_dispatch_idx). A bloated table or a dropped index shows as a sequential-scan plan; EXPLAIN the claim query to verify it uses an index scan with sub-millisecond latency.

How to remediate.

  1. Every queue slow: treat it as a database incident first. TaskQSweepTimeouts and TaskQRateLimitDependencyOutage read the same Postgres; a latency problem there degrades dispatch with it, and fixing dispatch alone fixes nothing.
  2. One queue slow: check the population the claim has to consider. A very deep pending backlog on that queue, a large TASKQ_DISPATCH_OVERSAMPLE, and table bloat all grow the candidate set the claim probes before it locks.
  3. Confirm recovery: the p99 falls back under 50 ms.

Do not scale dispatchers out to fix this: more concurrent claim rounds multiply contention on the same rows and push the p99 up, not down. Capacity adds consumption rate; it does not make the claim faster.


TaskQClaimLatencyDegraded

What fired. taskq_claim_degradation_ratio > 10 for 5 minutes, by queue: this worker's claim-query latency p99 is more than ten times its own rolling 24-hour baseline (minute-bucketed p99s, kept for a day). The ratio is the PREDICTOR of the MVCC death spiral: a long or overlapping transaction pins the MVCC horizon, VACUUM cannot reclaim, and the claim query's B-tree scans degenerate through the dead tuples the queue's own churn leaves behind - brandur.org/postgres-queues measured 15x lock-time degradation this way, and PlanetScale's 2026 re-run confirmed SKIP LOCKED only "lifts the floor, not the ceiling" (identical dead-tuple scans under both). The ratio exists because an absolute threshold cannot see this failure: a fleet that degrades slowly makes each new latency its own normal (and TaskQDispatchLatencyHigh, the absolute twin, stays silent).

How to confirm.

  • Metric: taskq_claim_degradation_ratio{queue="..."} climbing past 10 while taskq_claim_claims_per_second{queue="..."} falls is the spiral's last visible stage (the claim path slowing until throughput collapses). The percentiles (taskq_claim_latency_p50_seconds and its p95/p99 siblings) show the distribution moving as a whole: p50 moving with p99 is the whole scan degrading, not one straggler.
  • SQL: find the transaction pinning the horizon - the one thing every dead tuple needs gone before VACUUM can touch it:
SELECT pid, state,
       now() - xact_start AS xact_age,
       now() - state_change AS idle_age,
       left(query, 120) AS query_head
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
  AND now() - xact_start > interval '5 minutes'
ORDER BY xact_start;

A long-running transaction of any shape is the pin: an idle-in-transaction app session holding a connection mid-transaction, a long migration, a pg_dump with a snapshot, a stuck LISTEN/event-loop transaction. - Confirm the dead-tuple pressure it causes:

SELECT n_dead_tup, n_live_tup, last_autovacuum
FROM pg_stat_user_tables
WHERE relname = 'jobs' AND schemaname = 'taskq';

n_dead_tup climbing while last_autovacuum stalls in the past is the horizon pinned; the claim scans pay for every one of those rows.

Why the timeouts do not save you. Postgres ships three relevant knobs, and none of them catches this failure:

  • idle_in_transaction_session_timeout (default 0 = off) fires only when a session sits IDLE inside a transaction - an actively working long transaction (a batch migration, a COPY) never trips it;
  • transaction_timeout (default 0 = off) caps a transaction's total lifetime, but the pinning transactions in production are usually legitimate - a 20-minute migration is not a leak - so operators leave the knob off, and an app pool's idle-in-transaction session may not be the one pinning anything anyway;
  • statement_timeout bounds ONE statement, not the transaction: a transaction that runs a million short statements never trips it while pinning the horizon for its whole life.

And even when a timeout fires, it fixes the future, not the present: the dead tuples accumulated while the horizon was pinned stay until VACUUM reaches them.

How to remediate.

  1. Kill or complete the pinning transaction: SELECT pg_terminate_backend(<pid>); for an app-held one (the app retries; that is what retries are for), or let the migration finish. The horizon unpins the moment the last snapshot that can see the dead tuples ends.
  2. Confirm VACUUM drains: n_dead_tup falls and taskq_claim_degradation_ratio returns toward 1. The ratio falls within a claim-window of the vacuum, which is the confirmation the incident is over - do not re-page on the absolute gauge's slow decay.
  3. Check what held the horizon: an application session left idle in transaction needs idle_in_transaction_session_timeout set on THAT role (ALTER ROLE app SET idle_in_transaction_session_timeout = '5min'), which is a targeted fix that cannot break legitimate long migrations.

Opt-in mitigation: the claim cursor (TASKQ_CLAIM_CURSOR_RESET_SECONDS, default off) bounds the claim query's candidates at the worker's per-queue high-water mark of claimed ids, so the candidate probe's seek can land on live tuples instead of walking the dead zone. It is a selection predicate: a below-cursor row strands until the jitter reset forgets the cursor (the configured interval, ±50%), and round-robin queues are exempt. Its end-to-end benefit is plan-dependent because other claim-query surfaces may still walk dead tuples. Enable it only when the fleet's claim plans benefit from the candidate bound; it does not replace ending the pinning transaction and letting VACUUM drain.


TaskQLeaderLockContention

What fired. sum(rate(taskq_leader_lock_contention_total[10m])) > 0 and sum(taskq_maintenance_leader_is_leader) < 1 sustained for 10 minutes: maintenance-lock acquisitions are being lost AND no worker holds leadership. The counter is recorded by the losing side at every maintenance acquisition point, labeled by lock.

The leader-count operand is what makes this alertable at all. A healthy multi-worker fleet has exactly one winner per election round and every other worker records a loss, so a lost-acquire rate above zero is the normal steady state of any fleet larger than one worker; an alert on that rate alone pages on health and trains operators to silence it. Losses while the leader count has fallen below one is the genuine signature: nobody ever wins. Both operands are summed to fleet-wide scalars so the vector join pairs; an and between series carrying different label sets never matches, and Prometheus reports that as an empty result rather than an error. The lock key is schema-qualified (taskq:maintenance_leader:<schema>), built by taskq.constants.schema_lock_name, so contention is always between sessions of the same schema: a second schema in the same database takes a different key and cannot starve this one (the cross-schema silent starvation the unqualified lock name allowed is fixed). What sustained contention on a schema-qualified lock means: same-schema double-election attempts that keep losing (two pods of this deployment racing each other every heartbeat), or a stuck/long-held session sitting on this schema's key.

How to confirm.

  • Metric: taskq_leader_lock_contention_total rising, labeled by lock; confirm the label is your schema's taskq:maintenance_leader:<schema>. Read it beside sum(taskq_maintenance_leader_is_leader): a rising rate with that sum at 1 is an ordinary fleet electing a leader, not an incident. A rate that equals the election attempt rate (taskq_leader_election_attempts_total) fleet-wide, with the sum at 0, means no worker ever wins.
  • SQL: advisory lock holders and waiters:
SELECT l.pid, l.classid, l.objid, l.granted, a.application_name,
       now() - a.backend_start AS session_age
FROM pg_locks l JOIN pg_stat_activity a ON a.pid = l.pid
WHERE l.locktype = 'advisory';

The lock is a single bigint key (hashtextextended('taskq:maintenance_leader:<schema>', 0)), so it appears with classid = 0; match objid against SELECT hashtextextended('taskq:maintenance_leader:<your-schema>', 0) to pick this deployment's row out of the set. Contention is recorded once per distinct holder a pod finds in its way, so a stable fleet (however many followers it has) records nothing after it settles, and a handover records one event per pod. A sustained rate therefore means the observed holder keeps changing (handover churn), or that this pod cannot even read the lease row to observe a stable holder; its probe fails every cycle; check its leader-retry lines and connectivity.

How to remediate.

  1. Identify the winning session from the SQL above. Because the key is schema-qualified, a holder belonging to another schema or deployment sharing this database is not the cause: it holds a different key. The holder is a session of this same schema.
  2. Cross-check it against {schema}.maintenance_leader. A pod that follows a live lease records no contention at all, and a pod that finds the row absent wins it outright on its next cycle, so a sustained rate means the counter's remaining shapes: the row's holder keeps changing (handover churn), or the counting pod cannot read the row at all. A courtesy lock held by a session no lease row accounts for never produces it: the lease is what confers the role, so nothing waits on that session, but a lock nobody owns still points at a pod that elected under an older release, or at a session one left behind.
  3. Upgrade discipline: the schema-qualified names replaced the unqualified (taskq:maintenance_leader) ones outright, and the two are NOT overlap-compatible: a mixed old/new fleet holds different keys, so old and new releases can both act as leader of the same schema at once (sweeps stay row-safe under FOR UPDATE SKIP LOCKED; cron gains a double-fire window because its lock is what serialises ticks). Adopt a release that changes lock names by restarting the fleet onto it, not by rolling it; the window is the deploy, not the steady state (see taskq.constants.schema_lock_name).
  4. Confirm recovery: taskq_leader_lock_contention_total stops rising and sum(taskq_maintenance_leader_is_leader) == 1 again (see TaskQLeaderSplitBrainOrNoLeader).

TaskQLeaderSplitBrainOrNoLeader

What fired. sum(taskq_maintenance_leader_is_leader) != 1 for 2 minutes, severity critical. The gauge reads 1 on the elected leader pod and 0 on every follower, labeled by worker_id, so the fleet-wide sum is the leader count. 0 means no worker holds the maintenance role: promotion, cron, reclaim and prune are all stopped. Above 1 means two pods both believe they are leader. Both are split-brain conditions in the operational sense: the single-serialiser contract everything else assumes is not holding.

How to confirm.

  • Metric: taskq_maintenance_leader_is_leader per worker_id. All zeros, or two 1s. The zero case with taskq_leader_lock_contention_total rising is no worker ever winning the election; see TaskQLeaderLockContention.
  • SQL: who the database says holds the role, and who holds the advisory lock:
SELECT worker_id, elected_at, expires_at FROM taskq.maintenance_leader;

SELECT pid, granted, now() - backend_start AS session_age
FROM pg_locks l JOIN pg_stat_activity a ON a.pid = l.pid
WHERE locktype = 'advisory' AND classid = 0;

A healthy read is exactly one lease row with expires_at in the future, held (via the schema-qualified bigint key) by that pod. Two lease rows with overlapping terms, or a pod still reporting is_leader = 1 after its own row lapsed, is the finding. - The above-one shape has a known mundane cause: a fleet mid-upgrade across releases whose lock names differ holds different keys and can both act as leader of the same schema at once. Rule the mixed-fleet case out first (see the upgrade discipline in TaskQLeaderLockContention) before hunting anything exotic.

How to remediate.

  1. Zero leaders: work the contention path, not the gauge. Follow TaskQLeaderLockContention and TaskQPromotionStalled. Everything leader-side is stopped while this reads 0, so pending and scheduled work piles up, but nothing is lost; workers keep consuming what is already pending.
  2. Two leaders: identify the stale believer from the metric's worker_id and restart that pod. The usual shape is a pod partitioned from Postgres right after winning: its in-process is_leader event stays set until a renewal fails, while the lease row it left behind lapses and a successor takes over. The sweeps are row-safe under FOR UPDATE SKIP LOCKED either way; the one real double-fire window is cron, whose advisory lock is what serialises ticks, so check cron_schedules-driven jobs for duplicates on the overlap interval before declaring no damage.
  3. Confirm recovery: the sum returns to exactly 1 and the per-sweep last_success stamps move again.

TaskQRateLimitDependencyOutage

What fired. rate(taskq_ratelimit_acquire_dependency_failures_total[5m]) > 0 for 5 minutes: rate-limit acquires are failing because the limiter's store (Redis, or the PG fallback behind it) could not answer, and the worker failed the acquire closed as a denial. Every rate-limited dispatch is snoozing while the outage lasts: no work is lost, but nothing rate-limited moves either, and the queue looks calm while it piles up behind the limiter.

How to confirm.

  • Metric: taskq_ratelimit_acquire_dependency_failures_total rising, labeled by error_type (the exception class name). Read it beside taskq_reservation_denials_total{source="rate_limit"}: denials with this counter flat are ordinary contention; denials with this counter rising are an outage masquerading as contention; the two must be told apart before anyone scales a bucket.
  • The error_type label names the failure class (a Redis connection error, a timeout): it distinguishes "the store is unreachable" from "the store is slow".
  • Check the store from a worker pod, not from your laptop: the outage is between the worker's network position and the store (DNS, NetworkPolicy, the store itself).

How to remediate.

  1. Restore the store dependency: Redis connectivity from the worker pods first (the common cause), then the store itself. The PG fallback fails the same closed way when Postgres is the sick dependency; check TaskQSweepTimeouts / TaskQDispatchLatencyHigh before touching Redis.
  2. Do NOT raise bucket limits or disable rate limiting during the outage: the denials are the limiter failing closed (the configured safe behavior), and widening limits cannot create store capacity.
  3. Snoozed dispatches retry on their own once acquires succeed again; confirm recovery by watching taskq_ratelimit_acquire_dependency_failures_total flatten and the snoozed backlog drain (taskq_jobs_by_status{status="scheduled"} falling).

TaskQProgressPublishFailures

What fired. rate(taskq_progress_publish_failures_total[5m]) > 0 for 5 minutes: Redis publish round trips for progress events are failing. The counter is bumped on every failed publish attempt, labeled by channel (per_job or global) and error_type (the exception class name). The progress-publish-failure WARNING beside it is window-gated to one line per channel per minute, because a sustained Redis death fails every publish of every job and a log line per attempt is a flood, not a signal; the counter is the per-attempt aggregate the alert reads.

What it means. Only the real-time fanout degrades: the admin UI's live progress views fall back to polling at TASKQ_ADMIN_UI_POLLING_INTERVAL_SECONDS, so pages stay correct and merely stale between polls. The progress rows themselves persist on a separate Postgres path, counted by taskq_progress_flush_failures_total (a stage="per_job" versus stage="pool" distinction, a different incident); a job's settled progress is not lost by a publish failure.

How to confirm.

  • Metric: taskq_progress_publish_failures_total by channel, error_type. per_job failures drop live updates for individual jobs' viewers; global failures drop the fleet-wide progress channel. error_type distinguishes the shapes: connection classes are network between the worker and Redis, ResponseError is Redis answering with an error (read-only replica, maxmemory), timeout classes are saturation.
  • The admin UI shows the degradation directly: a page whose SSE stream cannot deliver events still polls, so "the page updates, only late" is the published symptom of this alert, never of the flush counter.

How to remediate.

  1. Restore Redis from the worker pods' position (the outage is between them and the store, so test from there, not from your laptop): connectivity first, then capacity (maxmemory evictions, a read-only replica after a failover).
  2. Do not disable progress emission to quiet the alert: the publish is fire-and-forget and its failure mode (a stale admin page that still polls) is strictly better than flying without progress telemetry.
  3. Confirm recovery: the rate returns to 0 and the live views resume updating between polls.

TaskQCronLockContention

What fired. rate(taskq_cron_lock_contention_total[10m]) > 0 for 10 minutes: cron ticks are returning without firing because another session holds the cron advisory lock, sustained. A brief low rate is the benign leader-handover overlap; a rate sustained at the tick cadence means cron is not running anywhere: the lock is transaction-scoped and releases on COMMIT/ROLLBACK, which never happens if the holding session was partitioned without a FIN. That is the fleet-wide cron stall: every schedule silently stops firing, and the signals an operator would check first (taskq_cron_disabled_schedules, taskq_cron_consecutive_failures) deliberately stay still in that mode.

How to confirm.

  • Metric: taskq_cron_lock_contention_total rising at roughly the tick rate (one contention per tick attempt), not brief bursts. taskq_cron_disabled_schedules staying 0 while no cron fired lines appear is the same stall seen from the other side.
  • SQL: the cron lock holder (the lock name is taskq:cron:<schema>, a single bigint key via hashtextextended, so it appears with classid = 0):
SELECT l.pid, l.granted, a.state, a.wait_event_type,
       now() - a.backend_start AS session_age,
       left(a.query, 120) AS query_head
FROM pg_locks l JOIN pg_stat_activity a ON a.pid = l.pid
WHERE l.locktype = 'advisory' AND l.classid = 0
  AND l.objid = (SELECT hashtextextended('taskq:cron:<your-schema>', 0));

A long-lived granted session whose session_age far exceeds worker liveness (partitioned without a FIN) is the finding; a healthy leader's hold is transaction-scoped and momentary.

How to remediate.

  1. Terminate the stale holder if it is dead weight (SELECT pg_terminate_backend(<pid>);); the server reaps partitioned sessions only on its tcp_keepalives_* schedule, which can outlast a maintenance window.
  2. If the holder is a healthy worker of THIS schema: two pods both running cron loops against the same schema points at a deployment/election misconfiguration: one cron loop per schema is the contract; fix the deployment, do not kill the session.
  3. Confirm recovery: taskq_cron_lock_contention_total stops rising, cron fired lines resume, and missed schedules catch up (due schedules fire immediately once the lock is free; cron does not skip missed ticks by default; see the cron guide for TASKQ_CRON_TICK_LIMIT if the backlog is large).

TaskQCronBudgetDeferrals

What fired. A sustained rate of taskq_cron_budget_deferrals_total (one increment per cron fire the tick's funded factory budget could not fund a grant for). A brief burst is benign: catch-up backlogs drain in tick-sized batches and each draining tick defers whatever its factories' waits could not fit. A rate that persists for minutes means one schedule's payload factory is monopolizing the tick budget every tick: it is planned ahead of its peers (an older next_fire_at, or a catch-up crawl), consumes most of the funded factory budget, and still SUCCEEDS, so it never strikes, never auto-disables, and never frees the budget. Its peers then defer on every tick: delayed (the deferral advances next_fire_at one leader tick, the owed slot stays inside the catch-up window), never struck, never disabled: quiet starvation with no auto-disable rescue, which is exactly why the counter exists. The monopolizer itself looks perfectly healthy (cron fired lines, consecutive_failures = 0).

How to confirm.

  • Metric: rate(taskq_cron_budget_deferrals_total[5m]) non-zero and flat, not decaying: a decaying rate is a catch-up drain ending. The actor label names the STARVING schedule's actor (per-schedule attribution is on the log line, not the label).
  • Logs: cron-fire-budget-deferred events with the same schedule_id every ~1s, while a NEIGHBOUR schedule's cron fired lines keep appearing; the neighbour whose fires sit immediately beside the deferrals in the timeline is the monopolizer. Its factory's duration is visible in the gap between consecutive cron fired lines.
  • SQL: the order the tick plans in (the monopolizer is the enabled, due row at the front):
SELECT id, actor, name, payload_factory, next_fire_at,
       now() - next_fire_at AS overdue_by, consecutive_failures
FROM taskq.cron_schedules
WHERE enabled AND next_fire_at <= now()
ORDER BY next_fire_at
LIMIT 10;

The monopolizer is the due row with a payload_factory and the oldest next_fire_at (or one advancing by one catch-up slot per tick); the deferring schedules are the factory-backed rows behind it.

How to remediate.

  1. Tighten TASKQ_CRON_PAYLOAD_FACTORY_TIMEOUT to below the monopolizing factory's real duration (read it off the cron fired timeline gaps). The factory then outruns its granted deadline, takes the strike it has earned, and auto-disables after TASKQ_CRON_AUTO_DISABLE_THRESHOLD ticks, freeing its peers. This is the intended consequence, not collateral damage: a payload factory slower than the operator-declared budget is a defect of that schedule.
  2. If the monopolizer's duration is legitimate, raise TASKQ_DISPATCHER_COMMAND_TIMEOUT so the funded factory budget (90% of it) fits the monopolizer plus a fundable grant (a quarter of the funded budget) for its peers, which then fire in the same tick. Check the watchdog interplay documented on that setting before widening it.
  3. Or move the slow work out of the factory: payload factories run inside the leader's tick, holding the cron advisory lock for the whole planning batch. Work slower than a fraction of a tick belongs in the job the schedule enqueues, not in the payload build.
  4. Confirm recovery: taskq_cron_budget_deferrals_total stops rising, the deferring schedules' cron fired lines resume, and their owed slots fire (deferred slots are retried, not skipped; nothing was lost).

TaskQCronSkippedSlots

What fired. A sustained rate of taskq_cron_skipped_slots_total (one increment per cron fire attempt, carrying that attempt's full drop count, labeled by actor): the schedule's next_fire_at fell behind now() - TASKQ_CRON_CATCH_UP_WINDOW, and every schedule occurrence between the owed slot and the recomputed one is a fire the system could not even attempt. The count is in FIRE UNITS — a */5 schedule reported as 3 means three dropped fires (about 15 minutes of schedule time). Skipped slots are never replayed: the recompute lands next_fire_at in the future, so the count IS the record of what was dropped. A brief burst is a leader failover or a paused fleet crossing the window once; a rate sustained for 5 minutes means the schedule is losing the race every period — the runaway fan-out: a cron-triggered sync whose actor's jobs clear slower than the cron period fires them.

How to confirm.

  • Metric: rate(taskq_cron_skipped_slots_total[5m]) — sustained non-zero is the runaway; a spike that decays to zero is a one-off (failover, paused fleet). The depth (how far behind the actor's worst schedule was at the skip) is taskq_cron_slots_behind{actor} — a depth that keeps CLIMBING period over period is the fan-out compounding; a depth that returns to 0 on the next fire is a one-time drop.
  • Logs: the cron missed slots skipped WARNING carries skipped_slots and schedule_id per drop; the cron fired line for the same fire repeats the depth, so one schedule's timeline shows exactly which periods were dropped.
  • SQL: the clearance comparison — is the actor's job completion rate slower than the cron period? — is the runaway predicate in src/taskq/insights.py (the per-schedule fires-vs-clearance SQL; fires are attributable to their schedule through the durable metadata->>'cron_schedule_id' stamp on every cron-enqueued job). Run it to name the schedule and quantify the gap:
SELECT id, actor, name, cron_expr, next_fire_at,
       now() - next_fire_at AS overdue_by, last_fired_at
FROM taskq.cron_schedules
WHERE enabled AND next_fire_at <= now()
ORDER BY next_fire_at
LIMIT 10;
  • Distinguish the sibling signals: taskq.cron.budget_deferrals is a budget-shaped lag (the fire is DEFERRED one tick and retried — nothing dropped); taskq.cron.skipped_slots is a real drop (the slot is gone). A schedule can carry both.

How to remediate.

  1. Widen the fan-out's capacity: the actor's jobs take longer than the cron period, so the backlog compounds period over period — raise the actor's concurrency (a second worker on its queue) or make each job cheaper, until jobs clear faster than the period. The taskq.jobs.actor_backlog / taskq.jobs.oldest_pending_age_seconds gauges show the pile-up on the job plane while the counter shows the drops on the cron plane.
  2. Lengthen the cron period on the schedule (or fan out from one fire into a batched job) so the period fits the actor's real clearance time.
  3. If the drops are acceptable (the schedule only needs the LATEST occurrence, and stale ones may be dropped — a common and legitimate shape for sync triggers), widen TASKQ_CRON_CATCH_UP_WINDOW to stop the drops, or leave it: the counter then documents the drops instead of hiding them. What the system never does is drop a slot without counting it.
  4. Confirm recovery: the counter's rate returns to 0 and taskq_cron_slots_behind re-observes the actor at 0 on its next clean fire.

TaskQCronScheduleDisabled

What fired. taskq_cron_disabled_schedules > 0 (for 0m, immediately): one or more cron schedules have been auto-disabled after TASKQ_CRON_AUTO_DISABLE_THRESHOLD (default 3) consecutive payload_factory failures. The gauge is refreshed by the leader's cron tick from the database's own count of disabled rows, so it settles on what every process has left behind after each tick. A disabled schedule enqueues nothing: its jobs stop firing, silently, on the schedule's own cadence.

How to confirm.

  • Metric: the gauge value itself. It does not decay; it moves when the database's set of disabled schedules changes, and it returns to 0 when the last disabled schedule is re-enabled.
  • SQL: which schedules are disabled and why:
SELECT actor, name, consecutive_failures, last_fire_error, next_fire_at
FROM taskq.cron_schedules
WHERE NOT enabled
ORDER BY actor, name;

last_fire_error carries the exception text of the strike that disabled the schedule; the cron schedule auto-disabled ERROR log lines and the per-tick cron fire failed lines name the same schedules. - The strikes are consecutive: a single success resets the count, so a schedule that reached auto-disable failed three ticks in a row. A factory raising every tick is a defect of that factory's code or configuration, not a transient dependency; a rate-limit or database outage during the ticks instead shows up in the alerts that own those dependencies.

How to remediate.

  1. Fix the payload_factory the last_fire_error names and deploy the correction before re-enabling. Re-enabling a still-broken schedule spends three more strikes on the same defect and lands back here.
  2. Check whether a restart already resolved it. Since the ownership model, the schedule row carries disabled_by: 'auto' (the cron loop's failure-count auto-disable) is reverted automatically the next time a worker boot re-declares a code-owned schedule (enabled=true, consecutive_failures=0, the marker cleared, and a cron-schedule-auto-disable-reverted log line at the boot). So if the failure was a transient blip (failover, saturation) and the fixed or still-declared code re-registers the schedule, the alert resolves on the next boot and this section's step 2 is unnecessary. A row with disabled_by='operator' is deliberate operator intent: no restart re-enables it, only the steps below do. A disabled row with disabled_by=NULL is a mixed-version deploy's shape (an old pod cannot write the marker): a boot re-enables it when it carries the old failure arm's fingerprint (consecutive_failures at or past the threshold, last_fire_error set) and leaves it alone otherwise; disabled rows that predate marker tracking were stamped 'operator' by migration 01.00.19_05. During a rolling deploy the same recovery also runs at every new-release leadership assumption (log line cron-schedule-auto-disable-reverted-at-takeover), because the old leader can keep firing -- and keep striking -- after every new pod has booted: a fingerprinted NULL row resolves when a new pod next leads, not only at the next restart.

SELECT actor, name, enabled, disabled_by, consecutive_failures, last_fire_error
FROM taskq.cron_schedules
WHERE disabled_by IS NOT NULL OR NOT enabled
ORDER BY actor, name;
3. Re-enable the schedule: the schedule handle's enable() resets consecutive_failures to 0, clears last_fire_error and disabled_by, and the admin UI's schedules page exposes the same action. Missed fires inside TASKQ_CRON_CATCH_UP_WINDOW catch up on the next tick after enabling; older misses are skipped by design. 4. Confirm recovery: the gauge returns to 0 and cron fired lines resume for the re-enabled schedule.

Do not raise TASKQ_CRON_AUTO_DISABLE_THRESHOLD to keep a broken schedule alive: the auto-disable exists so a permanently failing factory stops consuming tick budget every second, and a larger budget only delays the same silence.


TaskQRunningLeaseExpired

What fired. taskq_jobs_running_lease_expired > 0 for 5 minutes: running jobs whose lock lease is past expiry, sustained, with no cancel in flight that recovery still owes grace (rows in a cancel phase — cancel_phase != 0 on the column, or a workflow node carrying the pre-column metadata.cancel_phase mark — are carved out of the gauge only while they are inside the grace ladder, cancel grace + cleanup grace + 60 s past lease expiry, because recovery deliberately waits that long for a cancelling row before it pre-empts it; a cancelling row past the whole ladder is counted, since at that point the cancel is genuinely stuck). So an expired lease mid-cancel is the cancellation protocol working, not an incident (a cancel that never completes pages elsewhere: for a VANILLA claim, TaskQAbandonedJobs when its worker is alive to escalate through the phases; for a WORKFLOW node, whose terminal belongs to the engine and never reaches mark_abandoned, the cancel-abandon-not-applied WARNING while the holder is live and then the leader's wf_cancelled_claims arm, which settles it once the grace ladder has elapsed; TaskQHeartbeatMisses when the worker died mid-cancel; the reclaim sweep honors the row to cancelled either way). The ladder bound matters because this sampler runs on every worker while wf_cancelled_claims is leader-gated: without it a leaderless window hides a stranded cancel. A healthy fleet reads 0: the leader's reclaim sweep (sweep_name="expired_locks") drains expired leases within a tick or two of expiry, so a sustained non-zero count means reclaim is not draining. Work is claimed and stuck in running while health probes stay green: the zombie-running shape.

How to confirm.

  • Metric: taskq_jobs_running_lease_expired (sampled by every worker, so one flapping series is a sampling artifact; the alert fires on the sustained value). Cross-check both recovery owners' health: taskq_maintenance_leader_sweep_last_success_seconds{sweep_name="expired_locks"} and taskq_maintenance_leader_sweep_last_success_seconds{sweep_name="wf_cancelled_claims"}. A fresh stamp means the sweep runs but rows regrow faster than it drains (workers dying or wedging mid-run); a stale stamp means the sweep itself is stopped (see TaskQPromotionStalled: the same signature, different sweep). The wf_cancelled_claims arm is leader-gated, so its stamp going stale across a leaderless window is the ordinary explanation for cancel-marked workflow nodes climbing the gauge.
  • SQL: the zombies and their holders (the gauge's own predicate, ladder included):
SELECT id, actor, locked_by_worker, lock_expires_at,
       now() - lock_expires_at AS overdue_by, attempt, max_attempts
FROM taskq.jobs
WHERE status = 'running' AND lock_expires_at < statement_timestamp()
  AND (
        (cancel_phase = 0
         AND (NOT (metadata ? 'flow_id' AND step_key IS NOT NULL
                   AND step_key <> '__flow__')
              OR NOT metadata ? 'cancel_phase'))
     OR lock_expires_at < statement_timestamp()
            - interval '30 seconds' - interval '10 seconds' - interval '60 seconds'
      )
ORDER BY lock_expires_at;

Cancelling rows the sweep is still waiting out (expected, not zombies: inside the same grace ladder the reclaim sweep and the wf_cancelled_claims arm apply, on either cancel carrier):

SELECT id, actor, cancel_phase, cancel_requested_at, lock_expires_at
FROM taskq.jobs
WHERE status = 'running' AND lock_expires_at < statement_timestamp()
  AND (cancel_phase != 0
       OR (metadata ? 'flow_id' AND step_key IS NOT NULL
           AND step_key <> '__flow__' AND metadata ? 'cancel_phase'))
  AND lock_expires_at >= statement_timestamp()
        - interval '30 seconds' - interval '10 seconds' - interval '60 seconds'
ORDER BY lock_expires_at;
  • The admin /jobs page shows the per-row lease facts and holding worker. Its display-only expired badge is the immediate lock_expires_at/cancel_phase check, not the gauge's grace-ladder predicate, so a cancelling row inside the ladder can carry the badge without being a zombie.
  • A long-running caller-owned emit is a benign cause that reads exactly like this. ctx.emit_batch(conn=...) writes its page inside the caller's own transaction, and that row write is a lock held until the caller commits — so the worker's heartbeat cannot renew the source row and its lease freezes at the emit instant. While that transaction is open the reclaim sweep cannot see the row at all: its FOR UPDATE SKIP LOCKED candidate lock steps over the locked row, and the row's wf_caller_emit_hold mark is uncommitted (MVCC-invisible to every other session). Only once the page COMMITS with the lease still expired does the sweep grant the row one ordinary lease (taskq_jobs_caller_page_deferrals_total rises for the actor then) and leave it to the holder's next renewal. A caller that keeps working past the lease therefore shows up here until it commits or rolls back. Confirm the shape before hunting a zombie:
SELECT pid, state, xact_start, now() - xact_start AS xact_age,
       left(query, 120) AS query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL AND now() - xact_start > interval '60 seconds'
ORDER BY xact_start;

A matching long-lived idle-in-transaction backend on the caller's connection, together with taskq_jobs_caller_page_deferrals_total moving and heartbeat-lease-renewal-skipped lines naming the skipped count, is that cause. The remedy is on the caller: keep a conn= transaction short (one page per commit is the documented shape).

  • If the stuck rows are workflow nodes with a cancel in flight, the workflow engine's own failures are named: workflow-cancel-terminal-failed (the operator-cancel terminal rolled back) and workflow-cancel-write-failed (the shutdown release could not reach the workflow door), each carrying the settlement stage and the pool-checkout bound acquire_timeout_s. See observability.md §Key log events.

A note on the two clocks this runbook lives between: a holder can read as dead on the liveness surfaces (the admin banner, taskq doctor's "no live worker serves", taskq.queue.live_workers) while its RUNNING rows are not yet zombies. Liveness is the worker row's heartbeat window (TASKQ_ADMIN_WORKER_LIVENESS_SECONDS, default 30 s); the reclaim is the ROW's own lease (lock_expires_at, default lease 60 s) or its per-job heartbeat deadline - whichever fires first governs, and neither consults the banner. So "doctor says no live worker" plus a stuck running row inside its lease is the designed shape, not a contradiction: the row's job follows the lease (this runbook), the queue's pending work follows liveness (TaskQStrandedJobs). The worker row itself is removed later still, by the stale-worker sweep's own window (heartbeat_interval * (max_heartbeat_failures + 3), 60 s at the defaults) - the band between the liveness edge and that removal is where the surfaces legitimately disagree.

How to remediate.

  1. If TaskQHeartbeatMisses is firing or the holders' workers are gone: the jobs self-heal: the reclaim sweep transitions them (retryable → pending after a backoff; exhausted → crashed). The alert's value is that it stays non-zero when that does NOT happen.
  2. If the reclaim sweep is stalled or timing out: follow TaskQPromotionStalled and TaskQSweepTimeouts; fix the sweep/database; the zombies are a symptom.
  3. If the sweep is healthy and the count still regrows: jobs are repeatedly outliving their lease: the lease (lock_lease-shaped settings) is shorter than the actor's real run time and heartbeats are not renewing fast enough. Check TaskQLockExpiringSoon (it reads the measured lease left at each renewal: lock_lease minus the gap since the previous one, so firing means renewals are landing late, from a slow heartbeat pool, failed ticks or a blocked event loop; taskq_worker_event_loop_lag_seconds and taskq_heartbeat_misses_total say which) and widen the lease/heartbeat budget for those actors; do not restart workers to "clear" the gauge: the same jobs will zombie again.
  4. Confirm recovery: taskq_jobs_running_lease_expired returns to 0 and stays there across several sweep intervals.

TaskQLeaseRenewalSkipsAtRisk

What fired. rate(taskq_heartbeat_lease_renewal_skipped_total{at_risk="true"}[5m]) > 0 for two minutes: heartbeat beats are stepping over due job locks whose earliest reclaim deadline — the lock lease, or an active per-job heartbeat_timeout when that is sooner — will pass before the next beat can renew them. The renewal is one lock-first statement whose losers are SKIP LOCKED, so a row another transaction holds is simply not renewed by this beat — and it may be far below the renewal threshold, which is why this series exists beside TaskQLockExpiringSoon (that histogram is a beat-cadence measure and cannot see a row the beat never touched). The two-minute pending window is deliberate: TaskQRunningLeaseExpired pages five minutes after a lease has already expired, so a longer window here could never precede it. When a job's own heartbeat_timeout is shorter than its lease, the lease stays live (so TaskQRunningLeaseExpired does not fire) and skipped beats still commit (so TaskQHeartbeatMisses does not fire); this series is then the primary warning while the reclaim sweep acts on that shorter deadline.

What it usually is. A caller-owned emit transaction: ctx.emit_batch(conn=...) writes its page inside the caller's own transaction, and that row write is a lock held until the caller commits. The heartbeat cannot renew the source row, and its reclaim deadline freezes at the emit instant or, when the job sets a heartbeat_timeout, keeps running toward that deadline. While the caller's transaction is open the reclaim sweep cannot see the row at all (its FOR UPDATE SKIP LOCKED candidate lock steps over it); once that page commits with the lease still expired, the sweep grants the row one lease instead of reclaiming it (taskq_jobs_caller_page_deferrals_total rises for the actor, and the recovery_reclaim_deferred line names the count). That is the protocol working — the series is a warning precisely so an operator sees it before TaskQRunningLeaseExpired pages five minutes after a real expiry.

How to confirm.

  • Metrics: taskq_heartbeat_lease_renewal_skipped_total{at_risk="true"} beside taskq_jobs_caller_page_deferrals_total. Read the deferrals LAST, not first: while the caller's transaction is still open, its row's wf_caller_emit_hold mark is uncommitted (MVCC-invisible to every other session) and the row lock is what the reclaim sweep's FOR UPDATE SKIP LOCKED steps over, so deferrals stay flat for as long as the hold lasts. They rise only AFTER a late page commits and the mark becomes visible — which is exactly the page that no longer needs the deferral. Flat deferrals therefore do NOT rule out a caller hold; the open transaction itself is the evidence.
  • Log: the heartbeat-lease-renewal-skipped WARNING names the beat's skipped, skipped_at_risk and skipped_safe counts and the soonest remaining deadline (a lock lease or an active heartbeat_timeout, whichever is nearer). It fires when an at-risk skip appears and then at most once a minute while the hold persists (suppressed beats log heartbeat-lease-renewal-skipped-benign at debug), so its cadence is a reminder, never a per-beat rate.
  • SQL: the transactions holding the rows — lead with this, since it is the only direct evidence while the caller transaction is still open:
SELECT pid, state, xact_start, now() - xact_start AS xact_age,
       left(query, 120) AS query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL AND now() - xact_start > interval '60 seconds'
ORDER BY xact_start;

A long-lived idle in transaction backend on the caller's connection is the signature.

How to remediate.

  1. If the holders are caller-owned emit transactions (the pg_stat_activity read above names them): shorten them. The documented shape is one page per commit (async with conn.transaction(): ... emit_batch(..., conn=conn)); a body that keeps application work in the same transaction as many pages holds the row — and the run's admission lock — for the whole span.
  2. If no open caller transaction is visible, treat it as ordinary lock contention: check for a stalled heartbeat pool or a blocked event loop (taskq_worker_event_loop_lag_seconds, taskq_heartbeat_misses_total) before widening any interval.
  3. Confirm recovery: the at-risk rate returns to 0 and the WARNING's reminder stops; taskq_jobs_caller_page_deferrals_total may tick up once as the last held page commits.

TaskQFailedJobRateHigh

What fired. sum(rate(messaging_client_consumed_messages_total{outcome="failed"}[5m])) / sum(rate(messaging_client_consumed_messages_total[5m])) > 0.01 for 5 minutes: more than 1% of consumed attempts ended in a terminal failure: a non-retryable exception class, or the retry budget (max_attempts / schedule_to_close) exhausted. Retried failures are not in this share: they end as outcome="scheduled" and are counted by TaskQRetryRateHigh.

How to confirm.

  • Metric: messaging_client_consumed_messages_total{outcome="failed"} by actor, and taskq_jobs_attempt_failures_total{retryable="false"} by actor, error_type; the second names the exception class per actor.
  • Database, the terminal rows and their reason:
SELECT actor, error_class, count(*)
FROM taskq.jobs
WHERE status = 'failed' AND finished_at > clock_timestamp() - interval '15 minutes'
GROUP BY actor, error_class ORDER BY count(*) DESC;

error_class = 'DeadlineExceeded' is a schedule_to_close budget too tight for the actor's retry curve (taskq_jobs_timeouts_total{kind="schedule_to_close"} rises with it); a PayloadValidationError is a producer shipping a payload the actor's schema rejects; anything else is the actor's own exception.

How to remediate.

  1. One actor, one error_class: fix that actor or its dependency; failed rows can be retried with taskq job retry, the embedded admin UI's Retry button, or backend.retry_job() once the cause is fixed (the standalone taskq ui serve's Retry button answers 503 — no Backend there; see admin-ui.md). A workflow graph node is not a taskq job retry target: re-arm a failed node in a non-cancelled run with taskq flows retry <run_id> <node_key> (other terminal node states, cancelled runs, and a __flow__ root need a new run).
  2. DeadlineExceeded dominating: widen schedule_to_close (or the retry base/cap) for the actor; see ops.md: Timeouts.
  3. Many actors at once: a shared dependency (database, downstream API); check TaskQRetryRateHigh and TaskQDispatchLatencyHigh first; the terminal share is the tail end of the same incident once budgets run out.

TaskQRetryRateHigh

What fired. sum(rate(taskq_jobs_attempt_failures_total{retryable="true"}[5m])) / sum(rate(messaging_client_consumed_messages_total[5m])) > 0.1 for 10 minutes: more than 10% of consumed attempts raised and were rescheduled for another try. Each retry burns an attempt, a backoff delay and a worker slot, so a sustained rate is a dependency failing under retry cover: the jobs still complete, the terminal-failed share stays flat, and nothing else fires until budgets run out.

How to confirm.

  • Metric: taskq_jobs_attempt_failures_total{retryable="true"} by actor, error_type; the exception class per actor is the diagnosis (ConnectionError / TimeoutError on one actor is its downstream; asyncpg classes across actors is the database).
  • Database, jobs currently waiting on a retry and what they last raised:
SELECT actor, error_class, count(*), min(scheduled_at) AS next_try
FROM taskq.jobs
WHERE status = 'scheduled' AND attempt > 1
GROUP BY actor, error_class ORDER BY count(*) DESC;

How to remediate.

  1. Fix or wait out the dependency the error_type names; retries recover on their own once it answers again. Do not raise max_attempts to "make it go away": that spends more slots on the same failure.
  2. If the rate is one actor with a transient class that is really permanent (a bad payload that will never succeed), classify it: non_retryable_exceptions on the actor, or a retry_classifier; see ops.md: Classifying failures.
  3. Confirm recovery: the retried share falls back under the threshold and taskq_jobs_by_status{status="scheduled"} drains.

TaskQAbandonedJobs

What fired. rate(taskq_jobs_abandoned_total[5m]) > 0 for 5 minutes: a job was abandoned: an operator-requested cancel outlasted both grace periods (the actor was asked to stop, then forced with task.cancel(), and still never exited), so the running attempt was taken from it. Shutdowns never produce this: a deploy releases (interrupts) in-flight jobs back to the fleet. A retry or snooze never reaches this series either; those are outcome="scheduled" on the consumed-messages counter, so any rate here is a real actor ignoring cancellation.

How to confirm.

  • Metric: taskq_jobs_abandoned_total by actor; recorded by the abandon write itself, on both backends.
  • Database, the abandoned rows and the attempts that were taken away:
SELECT j.id, j.actor, j.finished_at, a.worker_id, a.duration_ms
FROM taskq.jobs j
JOIN taskq.job_attempts a ON a.job_id = j.id AND a.attempt = j.attempt
WHERE j.status = 'abandoned' AND j.error_class = 'CancelAbandoned'
ORDER BY j.finished_at DESC LIMIT 50;

How to remediate.

  1. The named actor does not yield to cancellation: it is blocking the event loop (a sync call without asyncio.to_thread), swallowing CancelledError, or running a native call that cannot be interrupted. Make it cooperative: poll ctx.cancellation_requested (or await ctx.cancel_event) in long loops, keep blocking work off the loop; see ops.md: Thread-unsafe native libraries.
  2. The abandoned row is terminal; the actor's coroutine may still be running in the worker until the process restarts. If it holds resources, restart that worker (taskq_active_jobs on its health socket shows the stuck slot).
  3. If the graces are too short for a well-behaved actor's cleanup, widen TASKQ_CANCELLATION_GRACE_PERIOD / TASKQ_CLEANUP_GRACE_PERIOD, but only after (1) is ruled out.

TaskQHeartbeatMisses

What fired. rate(taskq_heartbeat_misses_total[5m]) > 0 for 5 minutes, severity critical: a worker's heartbeat renewal ticks are failing. The counter is bumped once per failed beat in the heartbeat loop, on every transient Postgres error class (connection resets, statement timeouts, server-side cancels). At TASKQ_MAX_HEARTBEAT_FAILURES (default 3) consecutive failures the worker does not limp on: it self-isolates, handing its running jobs back to the fleet (retryable rows re-pended, exhausted rows crashed) and terminating, so a sustained miss escalates from lost renewals to lost capacity within a few beats.

How to confirm.

  • Metric: rate(taskq_heartbeat_misses_total[5m]). A blip that decays within a beat or two is one transient error; a flat sustained rate is a worker that cannot reach Postgres. The counter carries no worker label, so read it beside the logs of the replica that is reporting it.
  • Logs: heartbeat-tick-failure WARNING lines with error_class naming the Postgres failure, then heartbeat-failures-approaching-limit at half the budget, then the self-isolation. At the defaults the whole escalation fits in about 30 seconds (3 beats x 10 s).
  • SQL: which workers the database still considers alive:
SELECT id, hostname, pid, worker_label, last_seen_at,
       now() - last_seen_at AS silent_for
FROM taskq.workers
ORDER BY last_seen_at DESC;

A row going silent for more than a beat while its process is up is the confirmation.

How to remediate.

  1. Read error_class on the heartbeat-tick-failure lines. Connection-refused classes are network or DNS between the pod and Postgres; server-shutdown classes are the database restarting under the fleet; timeout classes are latency, and TaskQDispatchLatencyHigh or TaskQSweepTimeouts firing alongside points at a shared database incident.
  2. A worker that crossed the budget has already isolated: it exits, the orchestrator restarts it, and its re-pended jobs dispatch elsewhere. The capacity dip is the design working; do not pin a whole fleet's queue set on one replica.
  3. If misses cluster at deploys or failovers, check the lease arithmetic the settings loader enforces: lock_lease must cover max(heartbeat_interval, heartbeat_command_timeout) + (max_heartbeat_failures + 1) * (heartbeat_interval + heartbeat_command_timeout) (58 s at the defaults). A lease tighter than that turns a beat hiccup into TaskQRunningLeaseExpired.
  4. Confirm recovery: the rate returns to 0 and the affected taskq.workers row's last_seen_at is fresh again.

Do not raise TASKQ_MAX_HEARTBEAT_FAILURES to quiet this alert: the budget is the worker's deadline for handing its jobs back while their leases are still sane, and widening it widens the window in which a dead worker's jobs look running.


TaskQLockExpiringSoon

What fired. histogram_quantile(0.99, rate(taskq_lock_expires_in_seconds_bucket[5m])) < 30 for 5 minutes: the lease left on job locks at the moment the heartbeat renewed them has a p99 below 30 s. The histogram records, on every successful beat, lock_lease minus the measured gap since the previous beat's UPDATE, both stamps taken at the same point so network latency cancels; a sample near 0 means the beat landed just before the leases it renewed expired. The reclaim sweep takes a job the moment the gap reaches the lease, so this alert is the leading edge of TaskQRunningLeaseExpired: renewals are landing late, and the budget is being spent.

How to confirm.

  • Metric: the p99 of taskq_lock_expires_in_seconds against your configured TASKQ_LOCK_LEASE (default 60 s): a p99 of 30 s means half the lease is gone to lateness. Read taskq_heartbeat_misses_total (failed ticks) and taskq_worker_event_loop_lag_seconds (a blocked loop) beside it; those two say WHICH kind of lateness this is.
  • First check your cadence before debugging lateness. The threshold (30 s) sits exactly on the histogram's le="30" bucket edge, and a sample is quantized into that bucket: a HEALTHY configuration whose steady-state remaining lease — lock_lease − heartbeat_interval — lands in (20, 30] seconds serves a p99 of ~29.9 s and this alert pages forever on a fleet with nothing wrong. Example: TASKQ_HEARTBEAT_INTERVAL=5 with TASKQ_LOCK_LEASE=33 (the cascade floor for that cadence) reads ~28 s at every beat. If your cadence's lease-minus-interval falls in that window, override the threshold in your own rule configuration to below your cadence's steady-state value (a p99 < lock_lease − heartbeat_interval − margin shape); the shipped default is tuned for the default cadence (60 s lease − 10 s interval = ~50 s, which quantiles to ~59.9 s — safely above the line).
  • The sample is stamped on every successful beat whether or not the renewal threshold renewed any rows, so the histogram measures the beat cadence itself: a late beat lowers it exactly as a failed one does.
  • SQL: what the fleet's running locks have left:
SELECT id, actor, locked_by_worker, lock_expires_at,
       extract(epoch FROM (lock_expires_at - clock_timestamp()))::int AS remaining_s
FROM taskq.jobs
WHERE status = 'running'
ORDER BY lock_expires_at
LIMIT 20;

How to remediate.

  1. taskq_heartbeat_misses_total rising with it: the heartbeat pool cannot answer in time. Follow TaskQHeartbeatMisses (connectivity first, then the heartbeat pool's TASKQ_HEARTBEAT_POOL_SIZE and TASKQ_HEARTBEAT_COMMAND_TIMEOUT).
  2. Heartbeats clean but taskq_worker_event_loop_lag_seconds high: the beats are late because the loop is blocked. Find the blocking actor (the worker's event-loop-stall-attributed warnings name it and the admin UI's Stall hotspots column aggregates it); this is an actor defect, not a lease misconfiguration.
  3. Neither, and Postgres latency is the story: widen the budget through the enforced arithmetic (lock_lease >= max(heartbeat_interval, heartbeat_command_timeout) + (max_heartbeat_failures + 1) * (heartbeat_interval + heartbeat_command_timeout)); a settings load that fails the invariant is the loader keeping the lease ahead of the isolation cascade, not a bug.
  4. Confirm recovery: the p99 climbs back above the threshold, toward the full lease.

Do not fix this by raising TASKQ_LOCK_LEASE alone when the cause is a blocked event loop: a longer lease moves the reclaim deadline out over the same wedged beat, and the actor stays wedged twice as long before anything notices.


TaskQQueueUnserved

What fired. taskq_queue_depth{queue!="_other_"} > 0 unless on(queue) taskq_queue_live_workers > 0 for 2 minutes: a queue holds pending or scheduled jobs and no live worker subscribes to it: no worker row whose last_seen_at is inside TASKQ_ADMIN_WORKER_LIVENESS_SECONDS (default 30 s, three heartbeats) lists the queue in its queues. Nothing will consume the work. Both gauges come from the same leader sampler tick, so the join never compares two moments; a worker row that stopped heartbeating does not count even before the stale-worker sweep removes it.

How to confirm.

  • Metric: taskq_queue_depth{queue="<q>"} beside taskq_queue_live_workers{queue="<q>"} (absent, or 0).
  • Database: who last served the queue and when:
SELECT id, hostname, pid, worker_label, last_seen_at,
       last_seen_at > clock_timestamp() - interval '30 seconds' AS live
FROM taskq.workers
WHERE '<q>' = ANY(queues)
ORDER BY last_seen_at DESC;

No rows: nothing ever subscribed (a producer enqueues onto a queue name nobody runs, or the queue was dropped from every TASKQ_QUEUES at the last deploy). Rows, none live: the replicas serving it are down or partitioned from Postgres; check their /ready and TaskQHeartbeatMisses. - The admin UI's queues page shows the same condition as the "pending jobs but no alive worker" banner.

How to remediate.

  1. Start (or scale up) a worker whose TASKQ_QUEUES includes the queue, or add the queue to an existing worker's subscription. The backlog drains on its own once a live worker subscribes; nothing was lost.
  2. If the queue name is a producer mistake (a typo, a stale config), re-route the jobs: taskq actor-config move-queue for a whole actor, or re-enqueue. The stranded-jobs detector names the actors involved (taskq_jobs_stranded{reason="unserved_queue"}).
  3. Confirm recovery: taskq_queue_live_workers{queue="<q>"} > 0 and the depth falling.

TaskQStrandedJobs

What fired. taskq_jobs_stranded > 0 for 5 minutes: pending/scheduled jobs that cannot make progress, with the reason on the label. reason="no_actor_config": the actor has no actor_config row (deregistered with taskq actor-config deregister, or never registered by any worker), so the dispatch CTE, which derives its candidates from actor_config, never sees the rows. reason="unserved_queue": the queue dispatch routes the actor on (the actor's current assignment for a re-pended row, the row's own queue otherwise) has no live worker subscribed. reason="stamp_unresolved": the row carries a metadata.hold stamp — the held representation's marker — whose wf_signals hold is no longer held (delivered, abandoned, cancelled, or absent). These three reasons are dispatch-invisible: neither dispatch nor the deadline sweep will ever touch such rows (the fleet's dispatch fence refuses every stamped row). reason="workflow_root_missing" is a different class of fault — control-plane data corruption, not a routing condition: the rows are workflow nodes whose own run has lost its __flow__ root row. Such a row is refused by every claim fence — fleet dispatch, the claimable probes, the in-process runner — because the body resolves from the root's metadata.workflow stamp and a root-less claim could never reach one; the RUN can make no progress either, and no worker knob repairs it.

A pre-upgrade estate that already carries these rows pays a dispatch cost while they remain: on an affected (actor, queue) whose claimable probe finds nothing routable, the probe walks the refused pending range and probes each refused row's root before it can answer "nothing remains". Each such round is therefore proportional to the orphan backlog, not to the actor/queue count — measured at roughly 27-35 ms and 60-80k buffer touches per round for 30,000 fully-refused pending rows, against ~0.03 ms for an empty queue with no refused rows. Clearing the orphans (step 3) removes the metric and that per-round scan together.

How to confirm.

  • Metric: taskq_jobs_stranded by actor, reason; sampled by the leader every TASKQ_STRANDED_JOBS_INTERVAL; the stranded-jobs-no-actor-config / stranded-jobs-unserved-queue / stranded-jobs-stamp-unresolved / stranded-jobs-workflow-root-missing log events carry the same counts — the second names the queues, the third and fourth name up to 10 affected run ids (lexicographic, bounded). The heal's own event=wf-hold-stamp-reclaimed carries the cleared batch's job_ids and flow_ids (up to 50), so a stamp row's recovery correlates with its detection.
  • Database:
-- no_actor_config
SELECT j.actor, count(*) FROM taskq.jobs j
WHERE j.status IN ('pending', 'scheduled')
  AND NOT EXISTS (SELECT 1 FROM taskq.actor_config ac WHERE ac.actor = j.actor)
GROUP BY j.actor;

-- stamp_unresolved: rows stamped metadata.hold whose hold is no longer held
SELECT (j.metadata->>'flow_id') AS flow_id, count(*)
FROM taskq.jobs j
WHERE j.status IN ('pending', 'scheduled')
  AND j.step_key IS NOT NULL AND j.step_key <> '__flow__'
  AND j.metadata ? 'hold'
  AND NOT EXISTS (
    SELECT 1 FROM taskq.wf_signals live
    WHERE live.id::text = j.metadata->>'hold' AND live.status = 'held')
GROUP BY 1;

-- workflow_root_missing: workflow nodes whose run's own __flow__ root row is gone
SELECT (j.metadata->>'flow_id') AS flow_id, count(*)
FROM taskq.jobs j
WHERE j.status IN ('pending', 'scheduled')
  AND j.step_key IS NOT NULL AND j.step_key <> '__flow__'
  AND j.metadata ? 'flow_id'
  AND j.metadata->>'flow_id' ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
  AND NOT EXISTS (
    SELECT 1 FROM taskq.jobs root
    WHERE root.id = (j.metadata->>'flow_id')::uuid AND root.step_key = '__flow__')
GROUP BY 1;

How to remediate.

  1. no_actor_config: run a worker that registers the actor (registration writes the row at boot), or, if the actor is gone for good, cancel the rows (JobsClient.cancel_where(JobFilter(actor=...)), taskq job cancel-where, or the embedded admin UI's cancel action) so they stop counting. That bulk verb skips workflow rows — a workflow node or __flow__ root belongs to the workflow cancel door, and the admin job page offers the run link instead of the generic cancel form — so cancel those runs with cancel_workflow_run(flow_id=...) / taskq flows cancel <flow_id>. wf_after_terminal_effect leaf jobs and vanilla jobs carrying a coincidental metadata.flow_id are ordinary matches and are cancelled by the bulk verb as usual.
  2. unserved_queue: follow TaskQQueueUnserved: subscribe a live worker to the named queue, or move the actor's assignment with taskq actor-config move-queue.
  3. workflow_root_missing (control-plane recovery, never a worker knob): restore the exact missing __flow__ root row and its original flow identity through a validated database recovery/restore procedure after stopping the affected writers, and investigate the destructive path that dropped it. If faithful restoration is impossible, stop every writer for the affected run and have the application owner review the exact orphan set before any write. The following PostgreSQL recipe targets only non-root, nonterminal, non-after-terminal-effect rows whose metadata->>'flow_id' is the reviewed run id and whose __flow__ root is still absent. It locks and prints the target set before mutation; ROLL BACK if that set is not exactly the approved set. It then terminalises those orphans as cancelled, fences only their exact current claims, and writes one state-change event per winner in the same transaction. The example assumes the default taskq schema and psql; for a non-default schema the application owner must review the schema-qualified names before running it. Substitute the reviewed UUID for <reviewed-flow-uuid>.

    \set flow_id '<reviewed-flow-uuid>'
    BEGIN;
    SET LOCAL lock_timeout = '5s';
    SET LOCAL statement_timeout = '30s';
    
    CREATE TEMP TABLE wf_orphan_quarantine (
        job_id uuid PRIMARY KEY,
        from_state text NOT NULL
    ) ON COMMIT DROP;
    
    INSERT INTO wf_orphan_quarantine (job_id, from_state)
    SELECT j.id, j.status::text
    FROM taskq.jobs AS j
    WHERE j.metadata->>'flow_id' = :'flow_id'
      AND j.step_key IS NOT NULL
      AND j.step_key <> '__flow__'
      AND j.status NOT IN ('succeeded','failed','cancelled','crashed','abandoned')
      AND NOT j.metadata ? 'wf_after_terminal_effect'
      AND NOT EXISTS (
          SELECT 1 FROM taskq.jobs AS root
          WHERE root.id = :'flow_id'::uuid AND root.step_key = '__flow__'
      )
    ORDER BY j.id
    FOR UPDATE;
    
    -- Review every id and state now. If this is not exactly the approved set: ROLLBACK.
    SELECT job_id, from_state FROM wf_orphan_quarantine ORDER BY job_id;
    
    WITH flipped AS (
        UPDATE taskq.jobs AS j
        SET status = 'cancelled',
            finished_at = clock_timestamp(),
            error_class = 'WorkflowCancelled',
            error_message = 'orphaned workflow node quarantined after root loss; recreate as a new run',
            metadata = j.metadata - 'wf_emit_reservation'
        FROM wf_orphan_quarantine AS t
        WHERE j.id = t.job_id
          AND j.status NOT IN ('succeeded','failed','cancelled','crashed','abandoned')
          AND NOT j.metadata ? 'wf_after_terminal_effect'
          AND j.metadata->>'flow_id' = :'flow_id'
          AND NOT EXISTS (
              SELECT 1 FROM taskq.jobs AS root
              WHERE root.id = :'flow_id'::uuid AND root.step_key = '__flow__'
          )
        RETURNING j.id, t.from_state, j.claim_epoch, j.step_key, j.map_index
    ), current_claim AS (
        SELECT f.id AS job_id, l.id AS ledger_id, l.updated_at AS claimed_at,
               l.attempt AS claim_attempt
        FROM flipped AS f
        JOIN taskq.wf_step_ledger AS l
          ON l.job_id = f.id
         AND l.claim_epoch = f.claim_epoch
         AND l.step_key = f.step_key
         AND l.map_index IS NOT DISTINCT FROM f.map_index
        WHERE l.status IN ('running','awaited','crashed')
          AND NOT EXISTS (
              SELECT 1 FROM taskq.wf_step_ledger AS newer
              WHERE newer.job_id = f.id
                AND newer.claim_epoch = f.claim_epoch
                AND newer.step_key = f.step_key
                AND newer.map_index IS NOT DISTINCT FROM f.map_index
                AND newer.status IN ('running','awaited','crashed')
                AND (newer.attempt, newer.updated_at, newer.id)
                  > (l.attempt, l.updated_at, l.id)
          )
    ), fenced AS (
        UPDATE taskq.wf_step_ledger AS l
        SET status = 'fenced', error_class = 'WorkflowCancelled',
            updated_at = clock_timestamp()
        FROM current_claim AS c
        WHERE l.id = c.ledger_id AND l.updated_at = c.claimed_at
        RETURNING l.id, l.job_id, l.attempt AS claim_attempt
    ), written AS (
        INSERT INTO taskq.job_events (job_id, occurred_at, kind, detail)
        SELECT f.id, clock_timestamp(), 'state_change',
               jsonb_strip_nulls(jsonb_build_object(
                   'from_state', f.from_state,
                   'to_state', 'cancelled',
                   'error_class', 'WorkflowCancelled',
                   'reason', 'workflow_root_missing_quarantine',
                   'claim_attempt', c.claim_attempt
               ))
        FROM flipped AS f
        LEFT JOIN current_claim AS c ON c.job_id = f.id
        RETURNING job_id, detail
    )
    -- One visible result per reviewed winner. Do not COMMIT a half-recovery.
    SELECT w.job_id,
           w.detail ? 'claim_attempt' AS had_current_claim,
           NOT (w.detail ? 'claim_attempt') OR EXISTS (
               SELECT 1 FROM fenced AS f
               WHERE f.job_id = w.job_id
                 AND f.claim_attempt = (w.detail->>'claim_attempt')::integer
           ) AS fenced
    FROM written AS w
    ORDER BY w.job_id;
    
    -- Commit only if every reviewed id is now cancelled and no target remains active.
    SELECT t.job_id, j.status
    FROM wf_orphan_quarantine AS t
    JOIN taskq.jobs AS j ON j.id = t.job_id
    ORDER BY t.job_id;
    COMMIT;
    

    Any missing id or non-cancelled status means the recovery did not cover the approved set: ROLLBACK rather than issuing a second unreviewed write. This recipe must not touch a root row, another flow or child flow, terminal history, a materialised after-terminal effect, older-epoch or sibling substep/map ledger history, unrelated current claims, job_attempts, or the capacity and rotation records. Those records remain forensic evidence. Recreate replacement work as a new run; do not resurrect orphaned bodies under the missing flow id. Confirm the root-missing detector is clear on its next tick and then resolve any independently reported no-actor/unserved condition.

    fenced must be true for every row whose had_current_claim is true and the row count must equal the reviewed set; any disagreement is a half-recovery — ROLLBACK and report it, never COMMIT.

    This SQL remedy does not implement the product's cooperative phase-1 cancellation: it hard-terminalises a selected running row, whereas shipped cancellation leaves that row running. That is safe only because every writer for the run is stopped. A body still executing under a missed stop can commit external effects after its row is cancelled, and its own ledger terminal write will then refuse against the fenced claim. Lease columns, attempt, and the park marker keys remain as forensic evidence; the residue is inert because every lease-consuming maintenance arm is status='running'-gated and exact-park identity is status='scheduled'-gated.

    Actor registration, TASKQ_QUEUES, and queue moves do not repair the run. Per-run cancellation is NOT the cleanup mechanism here: taskq flows cancel <run_id> flips the root row first and then refuses loudly with a non-zero exit when the root is missing — it never touches the surviving nodes. 4. stamp_unresolved (no operator SQL — the leader's arm owns it): the row is pending, due and unclaimable until wf_hold_stamp_reconcile clears the dead stamp. That arm is drain=True and re-invoked up to sweep_drain_batches times per sweep_interval, at batch_size = event_writer_batch_size, so it clears up to event_writer_batch_size x sweep_drain_batches rows per tick (100 x 8 = 800 at the defaults) and a wider backlog drains over further ticks. Its event=wf-hold-stamp-reclaimed WARNING names the cleared job_ids/flow_ids; if the reason persists, check the arm's own sweep_wf_hold_stamp_reconcile_failed warning and TaskQSweepTimeouts — the arm is failing, not the stamp. 5. Confirm recovery: the series clears on the next detector tick (an empty reading is published, not a frozen last value) and the stranded-jobs-cleared event logs. The reasons interact in one direction only: a row reported as stamp_unresolved is ALREADY routable (its actor config exists and a live worker serves its routing queue — otherwise a routing reason would have won), while repairing workflow_root_missing, no_actor_config or unserved_queue can expose a latent stamp_unresolved row on the next tick. The detector re-reports it under the new reason and the reconcile tick clears it.


Event-loop stall attribution (worker warnings)

What fires. No alert: the worker's lag watchdog warns on its own. A warning named event-loop-stall-attributed logs once per stall at the warn tier and again at the terminal trip tier, alongside the existing worker-watchdog-lag-warn and worker-watchdog-trip events. Every attribution also bumps taskq_worker_loop_stall_attributions_total{actor, kind}, and the heartbeat merges the worker's rolling tally (top 20 actors by count, with per-kind counts) into its workers row metadata, so the hotspots are visible fleet-wide in /admin/workers and taskq doctor without scraping each worker.

What it means. The event loop could not schedule (a beat was late past the warn budget), and the watchdog attributes the stall to the actor whose frame sat under the work holding the interpreter. The kind label separates the two shapes:

  • blocking_call: the actor's synchronous call RELEASED the GIL (time.sleep, a socket or HTTP wait, a subprocess). The sampling is exact: the watchdog thread kept running and caught the blocking frame.
  • gil_held: the synchronous work HELD the GIL (a C extension that does not release it, such as a large document parse, or a hot pure-Python loop). The watchdog's own wakeups starved; the sample is approximate and points at (or just after) the C call.

The warning's frame field is file:line:function of the deepest non-taskq frame, and actor/job_id name the registered actor and (when exactly one running job matched) the job.

How to remediate.

  1. blocking_call: move the blocking call off the event loop (asyncio.to_thread / run_in_executor) or make the actor async.
  2. gil_held: the actor holds the GIL in a long synchronous computation: chunk it or move it off the loop.
  3. Confirm recovery: the per-actor rate of taskq_worker_loop_stall_attributions_total flattens, and the worker's Stall hotspots column in /admin/workers stops growing.

TaskQWorkflowBlockedStuck

What fired. taskq_wf_progress_nodes_total{state="blocked"} > 0 for 30 minutes: workflow node rows are sitting in a BLOCKED representation — join-wait (pending + deps_pending > 0), a blocked-with-reason stamp (metadata.blocking_reason set to 'orphan_parent', 'failed_parent', or 'flow_dead'), or held (metadata ? 'hold' — the dispatch fence the hold stamp sets) — with no transition for half an hour. The rule carries no progress gate: the gauge aggregates by DECLARED workflow and never by run, so no expression can tell a healthy sibling run's completion from the stuck run's own progress — a gate on it muted the alert for every stuck run in a workflow with a healthy sibling. A healthy multi-hour streaming import (its armed barrier plus the consumer waiting on it) therefore pages here at the 30-minute mark: raise the rule's for: in your own copy, or add a workflow!="<the long-running one>" variant. One stalled armed barrier also has its own fleet-level rule, TaskQWorkflowChainBarrierStalled. A fired join's 'join' marker (deps_pending = 0) and 'body_unavailable' are NOT blocked rows on their own: both derive state="pending" unless the same row is also held (metadata ? 'hold'), which is a block in its own right and makes the row unclaimable. The gauge is sampled by the maintenance leader on the admin's surface, dimensioned by the DECLARED workflow (_other_ carries the unregistered collapse; never per-node labels).

How to confirm.

  • Metric: taskq_wf_progress_nodes_total{state="blocked"} per workflow. A healthy fleet reads 0, and a count that moves with the run's other states is progress. A pinned count is a pin only once the run's other states freeze: a multi-hour streaming import legitimately holds its armed barrier plus its downstream consumer blocked for the import's whole duration, so read the per-run rows (below) before concluding the run is wedged.
  • SQL: the blocked nodes and their reasons (the run id from the workflow's run key / the admin's runs page):
SELECT id, step_key, status, deps_pending,
       metadata->>'blocking_reason' AS blocking_reason,
       metadata->>'failed_parent'   AS failed_parent
FROM taskq.jobs
WHERE (metadata->>'flow_id')::uuid = $1
ORDER BY id;
  • Read metadata.blocking_reason:
  • 'join' — the join is waiting on its parents: check the parents' states (the per-node rollup query, docs/guides/insights.md's blocked-by recipe). A parent stuck mid-ladder is normal; a parent gone (pruned, never inserted) wedges the join — the sweep stamps it 'orphan_parent'. Only a 'join' row with deps_pending > 0 counts as state="blocked": once the counter reaches 0 the marker stays as the fire record and the gauge reads it state="pending" — unless the row also carries the hold stamp (metadata ? 'hold'), in which case it is still state="blocked" and is NOT claimable until the hold resolves.
  • 'orphan_parent' — an edge points at a parent row that does not exist (a misnamed child): the join can never fire; fix the definition and re-run.
  • 'failed_parent' — the fail-closed cascade or the sweep's heal resolved this join because a parent reached a FAILURE terminal (failed, crashed, or abandoned) on a non-absorbing edge; the terminal-flow fence also uses this stamp to name a failure-terminal parent on an absorbing edge. The record names the parent in metadata.failed_parent / metadata.failed_step. Other unresolved work can keep the root running; a live fail-closed run converges to failed. The retained pending join row derives state="blocked" — so this alert keeps firing for that workflow until the row is cleared (see How to remediate).
  • 'flow_dead' — the run's root terminalized while this join still waited: the flow is dead, so the fire can never happen. Same alert-visible consequence as 'failed_parent' (a retained pending row), with no parent left to retry. A flow_dead CHAIN BARRIER is the exception a retry can repair — see How to remediate.
  • 'chain_barrier' — an ARMED chain barrier: it waits for its chain source to finish emitting and for every emitted record's branch to reach a terminal state. Healthy while the chain drains; a candidate that stops moving is a stalled chain (the source stalled, or a chain branch is stuck mid-ladder). Read taskq.workflows.chain_barriers_armed beside taskq.workflows.oldest_armed_barrier_age_seconds for the fleet view, and the barrier's own metadata.wf_barrier_source / wf_barrier_steps for the chain it drains.
  • A join over a CANCELLED parent is not a retained block: the sweep terminalizes it (status='cancelled', error_class='CancelledByCancelledParent', the parent named in metadata.cancelled_parent). Once other live work finishes, the run converges to cancelled unless an unabsorbed failure takes precedence and makes it failed. Find those rows with the same query — status = 'cancelled' AND error_class = 'CancelledByCancelledParent' — and note that the retained blocking_reason='join' on them is the joined-row family marker, not a wait. The same marker also lands on the converged __flow__ run root (with a message naming the graph origin), so add step_key != '__flow__' when you want the join rows only.
  • 'body_unavailable' — the join FIRED but its reducer body resolved NOWHERE: the delivery continued, the record is loud, and the row is not state="blocked" (it derives state="pending", unless the same row is also held — then the hold is the block). This is a deployment defect: the workflow's definitions are not imported in every worker process — the fire arm resolves bodies from the REGISTERED definition (D1); make every worker carry the same definitions.
  • If a run's rows are blocked but its flow root is running with NO live nodes, the sweep's maintenance leg will resolve the root at its next tick; a root still running past a tick or two is the sweep's own health (see TaskQPromotionStalled — the same signature, different sweep).

What not to do. Do not hand-flip deps_pending to 0: the counter is a CACHE of the edge ledger's truth, and a hand-zeroed join fires over a partial record. Resolve the parents while the run is still non-terminal (or, for an already-terminal failed root, use taskq flows retry on the failed parent; a cancelled root refuses retry) and let the re-derivation do the write: a cancel terminalises rows only while the root is still non-terminal — on a terminal root it is an idempotent no-op that does not clear a retained fenced join.

The stuck-run walk-through (the run-level face of the same alert — a run whose nodes are blocked is what this alert measures):

  • CLI: taskq flows status <run_id> — every stuck node names its waiting-on state + its remedy (the held rows name the gate + the hold id + the deadline; the join-waits name the deps counter; the failures name the claim headroom, attempt vs max_attempts, which the CLI labels "ladder headroom").
  • CLI: taskq flows holds <run_id> — the pending HITL holds. A hold past its expires_at is the expiry sweep's input (the sweep resolves it expired per the gate's on_timeout).
  • Admin: the run page (/taskq/workflows/<id>) renders the same derivation + the audit trail (who resolved what, when).

How to remediate.

  1. The run holds on a human: taskq flows resolve <hold_id> '<decision json>' --app myapp.workflows:app (the typed door — a wrong payload is refused with the named error and the hold survives).
  2. A node failed: taskq flows retry <run_id> <node> (the ledger is kept; neither count resets, so a transient node whose ladder ran out gets one more body attempt and its first crash is terminal; the blocked closure re-opens).
  3. A join waiting on the fence's record (failed_parent): retry the failed parent — taskq flows retry <run_id> <node> re-opens a failed root (running) and re-stamps its 'failed_parent' closure back to 'join' on the non-terminal rows. The same retry re-arms every 'flow_dead'-fenced CHAIN BARRIER of the run (a barrier is outside any branch's descendant closure, so a closure-only re-arm would leave it fenced with a marker no settle seat recognises and wedge the run). The re-stamped row is join-wait again (deps_pending > 0), so it still derives state="blocked" — the series clears when the retried parent completes and the rederive resolves the join, not when the retry returns. The join fires only if every parent-edge resolved; if the earlier fail-closed cascade cancelled a peer, the rederive instead terminalizes the unreachable join with CancelledByCancelledParent. A parent that ended crashed or abandoned is NOT re-armable (taskq flows retry re-opens status='failed' rows only), and a cancelled root refuses retry: re-run the work under a new run_key. Debris shapes with no shipped CLI verb that clears them: an 'orphan_parent' join (the parent never existed — fix the definition and start a new run), a 'flow_dead' ordinary join on a terminal or live root (outside the retried branch and not a barrier), and any 'failed_parent' join on a run you will not retry. Those retained pending rows keep both state="blocked" and the prune root-hold active until they are terminalised directly (a hand UPDATE restoring or clearing the stamp); taskq flows status <run_id> reports such a row truthfully as a stale flow_dead fence when the root is not terminal. A 'flow_dead' row that waits on a chain barrier is NOT debris while that barrier's failed owner is still retryable — including on a live root: retrying an unrelated failed branch reopens the root to running and leaves the barrier failed_parent-fenced, but retrying the barrier's own failed chain step re-arms it and returns the consumer to its join wait, and taskq flows status <run_id> prescribes exactly that retry. The same gap is noted in docs/guides/maintenance-sweeps.md.
  4. The run should never finish: taskq flows cancel <run_id> --reason ... (the cascade resolves the held signals in the same snapshot; the audit row carries the reason). The cancel terminalises pending nodes only while the run root is still non-terminal — on an already-terminal root it is an idempotent no-op and does NOT clear a retained pending fenced join.

The hold-expiry face (a resolved-loud expired under the same alert's held arm): the signals' expiry sweep resolved hold rows past their deadline — a human did not answer in the gate's window, and the gate's on_timeout policy took over (fail → the node fails through the ladder; the sweep-enforced cap). Confirm with taskq flows holds <run_id> (the pending set) vs the run's wf_signals rows with status='expired'; the node panel's attempt ledger shows the expiry's terminal. Remediate by re-running the node (taskq flows retry <run_id> <node>) — the re-run registers a NEW hold (a NEW epoch; the stale payload cannot answer it — the epoch is the fence). If the deadline was too tight for the humans, declare a longer timeout_s= at the gate (or accept the W1 warning and wait forever — deliberately, with the warning read).

Nothing is lost by waiting: a held run's budget is PAUSED (the hold counts for nothing on wake), and the rows are the truth — the driver picks the work up when the resolve lands.


TaskQWorkflowChainBarrierStalled

What fired. taskq_workflows_oldest_armed_barrier_age_seconds > 3600 and taskq_workflows_chain_barriers_armed > 0 for 10 minutes: at least one chain barrier has stayed ARMED for over an hour. An armed barrier waits on REAL work — its chain source finishing its emission pages, and every emitted record's chain branch reaching a terminal state — so an hour of that means either the source stopped emitting or a chain branch is stuck mid-ladder. Both operands are fleet-level and label-free: the armed COUNT and the age of the single OLDEST armed barrier, sampled on every worker (src/taskq/worker/_leader_sweeps.py). That is why this rule exists beside TaskQWorkflowBlockedStuck: the node gauge aggregates per declared workflow, so one healthy sibling run's completions cannot mask a single stalled barrier here.

How to confirm.

  • Metric: taskq.workflows.chain_barriers_armed and taskq.workflows.oldest_armed_barrier_age_seconds, read together. A deep-but-moving chain and a single stuck barrier look the same in the age gauge alone — a healthy long-running streaming import keeps climbing while it stays armed. The fault is a rising age beside an armed count that never falls and a run whose other states stopped moving.
  • SQL: the armed barriers and the chain they wait on:
SELECT id, step_key, created_at,
       metadata->>'flow_id'          AS flow_id,
       metadata->>'wf_barrier_source' AS barrier_source,
       metadata->>'wf_barrier_steps'  AS barrier_steps
FROM taskq.jobs
WHERE status = 'pending'
  AND metadata->>'blocking_reason' = 'chain_barrier'
ORDER BY created_at;

Then read the barrier's source row and its emitted chain rows (taskq flows status <run_id> does both, and names what each row is waiting on). Rows still reaching succeeded between two checks are a chain that is draining, not a stall. - Confirm the sampler itself is alive: an absent taskq.workflows.chain_barriers_armed is a FAILED sample (the series never reports a healthy-looking zero on failure), and those failures count on taskq_maintenance_leader_sweep_timeouts_total{sweep_name="chain_barriers"}.

What not to do. Do not cancel the run because the barrier is old — cancel resolves a live source's remaining work and the run converges to cancelled. Do not retry a chain row that has not failed: taskq flows retry re-opens status='failed' rows only.

How to remediate.

  1. A chain branch failed and the barrier is 'failed_parent'-fenced: taskq flows retry <run_id> <the step named by metadata.failed_step>. The retry re-arms the barrier and returns its consumer to join wait; the run then drains or terminalises per the fail-closed rules.
  2. The source stopped emitting (its row is running under a dead lease, or the emitting process died): follow A workflow run's mid-run hard crash — reclaim re-pends the killed attempt.
  3. A chain row is stuck mid-ladder: taskq flows status <run_id> names its claim headroom (attempt vs max_attempts). Let the ladder run out (it terminalises failed), then step 1.
  4. The chain is genuinely long and healthy: raise the rule's 3600 s threshold in your own copy. Age alone cannot distinguish a healthy multi-hour chain from a stalled one, which is why this page asks you to confirm first.

A workflow run's mid-run hard crash (the operator's page)

The executing process died mid-attempt (SIGKILL, OOM, the box went away). The run's rows stay running with a lock lease owned by a dead worker. THIS is the runbook the guides' resume sections assume; work top to bottom.

  1. Do NOT re-drive in-process and wait. FlowRunner.drive(flow_id, execute=True) on a fresh runner runs NO lock-reclaim sweep — it ticks to its max_ticks budget (~110 s at the 5000-tick default), returns the string "max_ticks" (a bounded give-up, not a status — CHECK the return), and changes nothing. The in-process driver is the dev-loop door for LIVE runs, not a recovery tool.
  2. The reclaim is the WORKER-hosted sweep's job, and it costs the lease arithmetic. The maintenance leader's expired-lock sweep waits out the dead claim's lock lease (TASKQ_LOCK_LEASE, 60 s at the default) plus the sweep cadence and the failed-beat cascade margin the lease must cover. MEASURED WALL: ~2 minutes from the kill to the killed child re-pending (122 s for a 40-site fan at the defaults) — a "stuck" run for the first two minutes after a crash is the mechanism working, not wedged. Confirm takeover: the maintenance_leader row names a LIVE worker, then the expired sweep's counters move.
  3. What the reclaim does with the killed child: the row re-pends (running→scheduled→pending) while retry budget remains — the next claim advances attempt (the body-failure ladder is untouched), the ledger's idempotency absorbs any partially-written effects of the killed attempt (safe by the step ledger). Budget exhausted (attempt >= max_attempts) → the row terminalizes crashed (the FAILED class — see the workflow guide's collect-contract note for what a crash-class child means to a collect join).
  4. Resume through the fleet door: drive(flow_id, execute=False) on a runner (the orchestration-only drive — the tick runs the sweep arms, the fleet's workers execute the re-pended work), or keep a worker running; the re-pended rows dispatch like any work. The kill-retry cycle is bounded by the child's claim counter (attempt < max_attempts on a transient step), not by its body-failure ladder (retries.md §12) — budget max_attempts= for the crashes you expect, knowing the same setting also sizes the ladder.
  5. The manual verbs refuse live rows: taskq flows retry <run_id> <node> re-opens status='failed' rows only — a live row and any other terminal (cancelled, succeeded, crashed, abandoned) is refused with "nothing re-opened: node … is not a re-armable terminal … , unknown to run …, or is the run's own root", and taskq job retry refuses a running job — retrying a live row would double-run it — and refuses any workflow graph node or __flow__ root outright (WorkflowRetryRequiredError; the flows retry verb is for the terminal-failed child described below, not every terminal state). A running row under a dead lease is step 2's patient, not a retry target. A terminal-FAILED child (the ladder spent) is the retry verbs' customer; a crashed/abandoned one is not (re-run under a new run_key).
  6. When the re-drive is not wanted — cancel + re-issue: taskq flows cancel <run_id> --reason ... (the cascade resolves the live rows in the same snapshot), then re-issue the run under a NEW run_key (a terminal run's key is never re-used). The re-issue's re-execution is absorbed by the workflow's idempotent design (the step ledger replays ctx.substep records; idempotent upserts in the bodies make data effects exact) — this is the scenario-proven path when the fleet is down and the driver is your only instrument.

A schema change under a live fleet: the online NOT NULL pattern

Not an alert: the runbook you reach for when a change to a hot TaskQ table (jobs first) needs a NOT NULL column while workers keep dispatching. jobs carries fillfactor = 80 and two BEFORE UPDATE OF status triggers, and a claim round runs against it every dispatch interval, so a DDL that takes AccessExclusiveLock for a full table scan is a write outage — every enqueue, claim and terminal write queues behind it until the scan ends.

PostgreSQL 18 lets you separate "enforce it from now on" from "prove the rows already there satisfy it". Two statements, in this order:

-- AFTER the backfill, enforce for subsequent writes, catalogue-only
-- (measured: AccessExclusiveLock, no scan).
ALTER TABLE taskq.jobs
  ADD CONSTRAINT jobs_tenant_id_nn NOT NULL tenant_id NOT VALID;

-- Prove the existing rows: reads and writes continue (measured:
-- ShareUpdateExclusiveLock — it conflicts with VACUUM and maintenance DDL, not DML).
ALTER TABLE taskq.jobs VALIDATE CONSTRAINT jobs_tenant_id_nn;

Things that trip operators:

  • ALTER COLUMN ... SET NOT NULL NOT VALID is a syntax error. The NOT VALID form exists only for ADD CONSTRAINT ... NOT NULL col. A bare SET NOT NULL is the blocking version: AccessExclusiveLock plus a full scan, held for as long as the table takes to walk.
  • Backfill before adding the constraint. Deploy writers that populate the column, add it (metadata-only — no rewrite without a DEFAULT), and backfill in bounded batches while it is still nullable. Then add and validate the constraint. NOT VALID skips the initial scan, not enforcement on later writes: adding it early makes even an unrelated status update fail on every row still waiting for its backfill (pinned on live PG 18 in tests/test_pg18_storage_and_skip_scan.py). Validating before the backfill likewise fails with the offending rows, which is the constraint doing its job.
  • Hypertables propagate, chunk by chunk. Measured on timescale/timescaledb:2.32.1-pg18: the NOT VALID constraint lands on every existing chunk, VALIDATE CONSTRAINT on the parent validates the parent and all chunks, and chunks created afterwards inherit the validated constraint. The validation walk still touches every chunk's rows, so run it in a low-churn window; on a large job_events hypertable check the retention floor first — validating chunks the retention job is about to drop is wasted work.
  • Check the verdict, do not assume it. pg_constraint reports convalidated = true per relation (parent and, on a hypertable, each chunk). A false on any chunk means that chunk's rows have not been proven and the constraint is still only forward-enforcing.
  • Watch the table afterwards. taskq doctor's hot-table health line reports the new-page-update ratio: an added column that no longer fits the page's free space shows up there as a jump, and the fix is the fillfactor conversation in ops.md, not another DDL round.

Repacking a bloated hot table on PG 19: REPACK (CONCURRENTLY)

Not an alert: the DBA operation you reach for when jobs (or an archive table) has inherited bloat a lowered fillfactor cannot recover — the repair VACUUM FULL used to own, at lock severities a live fleet cannot afford. PostgreSQL 19 ships REPACK (CONCURRENTLY) as a server command. TaskQ deliberately ships no CLI wrapper for it: this is a run-in-psql maintenance operation with sharp edges, not a fleet verb, and the version gate is the server's, not TaskQ's.

REPACK (CONCURRENTLY) is NOT MVCC-safe — this is the headline

A transaction using a snapshot taken before repacking started reads the table as empty after the repack commits. The rewrite swaps the relation, and the old snapshot has no valid view of the new one. Concretely: a long-running admin query, an open psql session, a repeatable-read read replica transaction — anything whose snapshot predates the repack's start — sees zero rows from the repacked table once the repack commits, not the old rows. Plan around it: no long-lived transaction may span the repack on a table anyone reads concurrently. This is the property that makes it unsuitable to ever run automatically from a job runner.

The operation and its preconditions on PostgreSQL 19:

-- Set a lock budget FIRST: the command takes short locks repeatedly, and an
-- unbounded wait on a hot table queues every enqueue behind it.
SET lock_timeout = '5s';

-- Name the table. ALWAYS. (See the bare-REPACK warning below.)
REPACK (CONCURRENTLY) taskq.jobs;

What refuses, and what to do instead:

  • Bare REPACK with no table name and no option. The bare form rewrites every table and materialized view in the current database on which the role has MAINTAIN, holding ACCESS EXCLUSIVE on each for the whole non-concurrent rewrite. Never run it against a live database; always name the table (the syntax above).
  • TimescaleDB hypertables. REPACK (CONCURRENTLY) does not accept a hypertable parent — it is a rewrite of one storage relation, and a hypertable is many chunks. Repack per chunk (each chunk is a plain table) or rely on the columnstore compression + retention drop cycle TaskQ's hypertable mode arms instead (see timescaledb.md).
  • Partitioned tables. CONCURRENTLY refuses the partitioned parent; repack each leaf partition individually (each named explicitly). Non-concurrent REPACK on a parent instead recurses through its partitions, with the corresponding locking cost.
  • Execution context and replica identity. REPACK (CONCURRENTLY) cannot run inside a transaction block. Keep the statements above in autocommit psql, and name a table with a primary key or index-based replica identity: without one, concurrent repack refuses the table because it cannot capture and apply concurrent changes safely.
  • Invalid indexes. Any INVALID index on the table aborts the repack. Drop it or REINDEX it first — check pg_index.indisvalid for the table before you start (taskq migrate up's failure report lists them after a failed no-transaction migration, but nothing inspects them up front).
  • Privileges. The command requires the MAINTAIN privilege on the table — not merely the connection role's write grants. A migration/automation role without it fails at the start, not mid-rewrite.
  • lock_timeout. Set one (as above) for every repack against a live fleet: the command's brief locks must queue behind nothing, and an unbounded wait on the hot jobs table is a self-inflicted write outage — the same hazard the online NOT NULL runbook guards against.
  • A busy table can fail the run late. The concurrent form can fail after most of the work is done: DDL another transaction runs on the same table during the repack fails it, and so does crossing roughly 104 million rows updated or deleted while it runs. taskq.jobs is exactly that table on a live fleet — repack it in a low-churn window, keep deploys out of that window (a pre-deploy taskq migrate up is DDL on these same tables), and watch SELECT * FROM pg_stat_progress_repack; instead of guessing how far along it is.
  • A free repack replication slot. CONCURRENTLY captures the changes that land while it runs and refuses when max_repack_replication_slots (default 5, set at server start) has no slot free for it — the shape a server whose slots are already committed to replicas or CDC meets.
  • Disk for the copy. REPACK writes a full copy of the table and every index before the swap, plus the captured concurrent changes for CONCURRENTLY, so the host needs free space at least equal to the table plus its indexes. On a managed volume that is an autoscaling event or a failed repack, not a shrug.

Afterwards, re-check taskq doctor's hot-table health line: the new-page-update ratio should drop as updates land in realigned pages.


Read-your-writes on a read replica: PG 19 WAIT FOR LSN

Not an alert: the pattern for teams adding read replicas to a TaskQ database and needing a read-after-write guarantee across the replication hop. PostgreSQL 19 ships the top-level WAIT FOR LSN command for the standby. TaskQ ships no wrapper and no HTTP LSN-routing service for this — it is session-level psql/driver discipline, run by the application that needs the guarantee.

The pattern on PostgreSQL 19:

-- 1. On the PRIMARY, after the commit you must read, capture the insert LSN:
SELECT pg_current_wal_insert_lsn();

-- 2. On the REPLICA, BEFORE opening the read's own snapshot (before any
--    statement that takes a snapshot or a lock on that session):
WAIT FOR LSN '0/1B2C3D4E' WITH (TIMEOUT '5000ms', NO_THROW);
-- Read only when the returned status is 'success'.

The details that bite:

  • Execute it pre-snapshot, pre-lock. WAIT must be a top-level command, not a function, procedure or DO body. Run it outside a transaction or as the first statement of one, before the reading transaction acquires a snapshot or any lock. In driver terms that is a fresh autocommit connection, or BEGIN; WAIT ...; followed by the reads — never a wait after the read's snapshot already exists.
  • NO_THROW returns a status string. The possible results are success, timeout, and "not in recovery" — with spaces in the last value, not an underscored identifier and not NULL. Require success before reading; a primary returns not in recovery, and a timeout returns timeout. Without NO_THROW, those two unsuccessful outcomes raise instead, which is the safer default when the caller has no explicit status-handling branch. NO_THROW changes only how those two outcomes are reported: a malformed LSN, an unrecognised option, or a wait issued while the session already holds a lock still raises, so a caller's error handling cannot treat the command as exception-free.
  • Timeouts below 1 ms silently become forever. A timeout value <= 0.5 ms rounds to 0, and 0 means wait indefinitely — a lost replication stream then hangs the caller instead of failing. Use >= 1 ms, and treat a small timeout as the fail-closed choice: the caller retries or serves stale, bounded.
  • Timeline switches are the edge. WAIT compares only the numeric LSN; it has no notion of which timeline produced it. A promotion returns not in recovery (or raises without NO_THROW), while a standby following an upstream timeline switch can report success once its numeric position passes the target regardless of timeline. After a failover, re-capture the LSN from the promoted primary and validate the expected timeline where that distinction matters — never assume an old-timeline target proves the new timeline contains your write.

TaskQ's own reads (the admin UI, insights) run against the primary DSNs you configure; TaskQ never reads a replica. This runbook exists for applications that put their own read path on a replica alongside TaskQ's tables.


  • Observability: the metrics these alerts evaluate, including the sweep sample-population rule.
  • Configuration: TASKQ_EVENT_WRITER_*, TASKQ_SWEEP_* and TASKQ_CRON_TICK_LIMIT knobs referenced above.
  • Troubleshooting: symptom-first diagnosis paths.

The workflow rows' alert anchoring (the F-R3 discipline): every ## TaskQ… heading in this file's alert rows names a rule SHIPPED in BOTH src/taskq/contrib/prometheus/rules.yaml and src/taskq/contrib/kubernetes/prometheus_rule.yaml — a runbook row an alert cannot fire is a page that lies (the pinned cross-check: tests/test_prometheus_metrics_review.py's runbook-anchor pin). The former TaskQWfRunStuck / TaskQWfHoldExpired rows had no shipped rule to fire — their operational content (the stuck-run walk-through, the hold-expiry face) lives in TaskQWorkflowBlockedStuck's row, the alert that actually measures the blocked/held shape.