Telemetry
SQL recipe collection

Background jobs SQL recipes

Measure retries, failures, queue age, backlog growth, stalled work, and recovery time for workers, imports, cron tasks, and async jobs.

Common event contract

Fields that keep these queries reusable

  • timestamp_utc, job_id, job_name, queue_name, and status
  • scheduled_at, started_at, completed_at, duration_ms, and attempt
  • error_type, worker_name, item_count, and release

Definitions before SQL

Decisions the query cannot make for you

  1. 1Choose one identifier that connects every attempt of the same logical job.
  2. 2Document terminal statuses so stalled-job queries do not flag completed work.
  3. 3Separate queue wait from execution time before setting thresholds.

Recommended sequence

Build detection first, then diagnosis

Analysis patterns

Make the result explain a decision

Model the job lifecycle

Keep scheduled, started, retried, completed, failed, and discarded outcomes distinct so one query can reconstruct each logical job.

Separate stock from flow

Measure the current backlog independently from jobs created and resolved during each time bucket.

Alert on sustained delay

Use queue age, consecutive missed schedules, or repeated retries instead of a single transient worker error.

Complete recipes

Copy the query, then validate the assumptions

Beginnerjob_runs

Measure Background Job Retry and Failure Rate

Find unreliable jobs by comparing successful runs, retries, failures, and tail duration.

Which background jobs consume the most retries or still fail?

See SQL and result
Intermediatejob_events

Find Stalled Background Jobs With SQL

Join job start and finish events to identify work that exceeded its expected completion window.

Which jobs started but never produced a terminal event?

See SQL and result
Beginnerjob_runs

Measure Queue Wait Time by Job

Separate time spent waiting in a queue from execution duration and compare p50 and p95 delay by job name.

Which jobs wait longest before a worker starts them?

See SQL and result
Intermediatejob_events

Measure Dead-Letter Queue Growth

Compare dead-letter creation and resolution to find queues accumulating unrecoverable work.

Which queues are adding dead-letter jobs faster than they are resolving them?

See SQL and result
Intermediatecron_runs

Detect Missed Cron Schedules

Compare consecutive cron-run events with each schedule interval to find late or missing executions.

Which scheduled jobs ran later than their documented interval?

See SQL and result
Intermediatejob_attempts

Detect Background-Job Retry Storms

Find time buckets where repeated job attempts create disproportionate queue work and failures.

Which job types are spending the most work on retries right now?

See SQL and result

Adapt the event contract before the threshold

Keep the analysis pattern, but validate table names, field types, business definitions, time windows, and minimum-volume rules against your own events. Every published query is also planned and executed against an empty typed table with the pinned engine.