Review funkt

This commit is contained in:
2026-08-09 21:56:00 +00:00
parent f522ae9953
commit fe9dfa4519
11366 changed files with 2335077 additions and 97 deletions

View File

@@ -0,0 +1,3 @@
-- Insert a flow (tree) of jobs atomically. Param: $1 entries (jsonb array,
-- ordered roots-first). Returns the job ids in input order.
SELECT id FROM add_flow($1::jsonb) AS t(id);

View File

@@ -0,0 +1,5 @@
-- Insert a single job, routing it to waiting or delayed; returns its id.
SELECT add_job(
$1, $2, $3, $4::jsonb, $5::jsonb, $6, $7, $8, $9,
$10, $11, $12, $13, $14, $15
) AS id;

View File

@@ -0,0 +1,6 @@
-- Register/update a scheduler and enqueue its next iteration; returns the
-- delayed job id and its delay. Params: $1 queue, $2 scheduler_id,
-- $3 next_millis, $4 template_data (jsonb), $5 template_opts (jsonb),
-- $6 opts (jsonb), $7 delayed_opts (jsonb), $8 now, $9 producer_id.
SELECT job_id, delay
FROM add_job_scheduler($1, $2, $3, $4, $5, $6, $7, $8, $9);

View File

@@ -0,0 +1,3 @@
-- Fast set-based bulk insert of INDEPENDENT jobs (no parents, no dedup).
-- Param: $1 queue, $2 entries (jsonb array). Returns the job ids in input order.
SELECT id FROM add_jobs_bulk($1, $2::jsonb) AS t(id);

View File

@@ -0,0 +1,14 @@
-- Append a log line at the next per-job ordinal; returns its index.
-- Params: $1 queue, $2 job_id, $3 row.
-- The unqualified table name resolves in the configured BullMQ schema via
-- search_path (default schema: bullmq).
INSERT INTO job_log (queue, job_id, idx, row)
VALUES (
$1, $2,
COALESCE(
(SELECT MAX(idx) + 1 FROM job_log WHERE queue = $1 AND job_id = $2),
0
),
$3
)
RETURNING idx;

View File

@@ -0,0 +1,3 @@
-- Reschedule a delayed job. Params: $1 queue, $2 id, $3 delay, $4 now.
-- Returns 0 ok, -1 missing, -3 not delayed.
SELECT change_delay($1, $2, $3, $4) AS code;

View File

@@ -0,0 +1,3 @@
-- Change a job's priority (and reposition via lifo). Params: $1 queue, $2 id,
-- $3 priority, $4 lifo. Returns 0 ok, -1 missing.
SELECT change_priority($1, $2, $3, $4) AS code;

View File

@@ -0,0 +1,3 @@
-- Remove jobs of a state older than a timestamp, up to a limit (0 = all);
-- returns removed ids. Params: $1 queue, $2 type, $3 timestamp, $4 limit.
SELECT id FROM clean($1, $2, $3, $4) AS t(id);

View File

@@ -0,0 +1,9 @@
-- Clear a job's logs, keeping the latest $3 entries (NULL/0 → remove all).
-- Params: $1 queue, $2 job_id, $3 keepLogs.
DELETE FROM job_log
WHERE queue = $1 AND job_id = $2
AND idx < COALESCE(
(SELECT MAX(idx) FROM job_log WHERE queue = $1 AND job_id = $2)
- $3 + 1,
idx + 1
);

View File

@@ -0,0 +1,4 @@
-- Record one finished job into the queue/kind metrics (per-minute deltas).
-- Params: $1 queue, $2 kind ('completed' | 'failed'), $3 maxDataPoints,
-- $4 finish timestamp (epoch ms).
SELECT collect_metrics($1, $2, $3, $4);

View File

@@ -0,0 +1,6 @@
-- Unconditionally remove a deduplication key (Queue.removeDeduplicationKey /
-- the deprecated removeDebounceKey). Params: $1 queue, $2 dedup_id. Returns the
-- removed key (0 or 1 row).
DELETE FROM dedup
WHERE queue = $1 AND dedup_id = $2
RETURNING dedup_id;

View File

@@ -0,0 +1,2 @@
-- Remove waiting (and optionally delayed) jobs. Params: $1 queue, $2 delayed.
SELECT drain($1, $2);

View File

@@ -0,0 +1,3 @@
-- Refresh an active job's lock; returns 1 on success, 0 if the lock was lost.
-- Params: $1 queue, $2 id, $3 token, $4 lock_ms, $5 now_ms.
SELECT extend_lock($1, $2, $3, $4, $5) AS n;

View File

@@ -0,0 +1,25 @@
-- Refresh multiple active-job locks in one round-trip; returns the ids whose
-- locks could not be renewed. Params: $1 queue, $2 ids (text[]), $3 tokens
-- (text[]), $4 lock_ms, $5 now_ms.
WITH input AS (
SELECT ids.job_id, toks.token, ids.ord
FROM unnest($2::text[]) WITH ORDINALITY AS ids(job_id, ord)
JOIN unnest($3::text[]) WITH ORDINALITY AS toks(token, ord)
USING (ord)
),
updated AS (
UPDATE job
SET locked_until_ms = $5::bigint + $4::bigint
FROM input
WHERE queue = $1
AND id = input.job_id
AND state = 'active'
AND lock_token = input.token
RETURNING id
)
SELECT input.job_id AS id
FROM input
LEFT JOIN updated
ON updated.id = input.job_id
WHERE updated.id IS NULL
ORDER BY input.ord;

View File

@@ -0,0 +1,11 @@
-- Connected BullMQ clients, mirroring Redis `CLIENT LIST` for worker/queue
-- discovery. Each long-lived worker / QueueEvents listener names its dedicated
-- connection via `application_name` (the PostgreSQL analogue of
-- `CLIENT SETNAME`), so listing the named sessions on this database reproduces
-- the same discovery surface. Pooled query connections leave `application_name`
-- empty and are excluded. One row per connection; the backend formats them into
-- `name=<application_name>` lines for the shared client-list parser.
SELECT application_name
FROM pg_stat_activity
WHERE datname = current_database()
AND application_name <> '';

View File

@@ -0,0 +1,13 @@
-- Job counts by state for a queue. Param: $1 queue.
-- "prioritized" = waiting with priority > 0; "waiting" = waiting with priority 0.
SELECT
COUNT(*) FILTER (WHERE state = 'active') AS active,
COUNT(*) FILTER (WHERE state = 'completed') AS completed,
COUNT(*) FILTER (WHERE state = 'failed') AS failed,
COUNT(*) FILTER (WHERE state = 'delayed') AS delayed,
COUNT(*) FILTER (WHERE state = 'waiting' AND priority = 0) AS waiting,
COUNT(*) FILTER (WHERE state = 'waiting' AND priority > 0) AS prioritized,
COUNT(*) FILTER (WHERE state = 'waiting-children') AS "waiting-children",
(SELECT value FROM meta WHERE queue = $1 AND field = 'paused') AS paused
FROM job
WHERE queue = $1;

View File

@@ -0,0 +1,14 @@
-- Waiting-job counts per priority. Params: $1 queue, $2 priorities (bigint[]).
-- Returns one row per requested priority, in the input array order, with the
-- number of waiting jobs at that priority. Pausing is an O(1) meta flag that
-- leaves jobs in the 'waiting' state, so the counts are identical whether or
-- not the queue is paused (mirrors Redis, where pausing does not touch the
-- wait/prioritized sets).
SELECT COUNT(j.id) AS cnt
FROM unnest($2::bigint[]) WITH ORDINALITY AS pr(priority, ord)
LEFT JOIN job j
ON j.queue = $1
AND j.state = 'waiting'
AND j.priority = pr.priority
GROUP BY pr.ord
ORDER BY pr.ord;

View File

@@ -0,0 +1,7 @@
-- The current "winner" job id for a deduplication key, or none when absent or
-- expired. Params: $1 queue, $2 dedup_id, $3 now (ms).
SELECT job_id
FROM dedup
WHERE queue = $1
AND dedup_id = $2
AND (expire_at_ms IS NULL OR expire_at_ms > $3);

View File

@@ -0,0 +1,8 @@
-- All of a parent job's child dependencies with their status and stored value.
-- Params: $1 parent_queue, $2 parent_id. Status maps to the public categories:
-- processed → processed (value = return value), pending → unprocessed,
-- ignored → ignored (value = reason), failed → failed.
SELECT status::text AS status, child_key, value
FROM job_dependency
WHERE parent_queue = $1 AND parent_id = $2
ORDER BY child_id;

View File

@@ -0,0 +1,7 @@
-- One page of a parent's child dependencies in a single status category.
-- Params: $1 parent_queue, $2 parent_id, $3 status, $4 offset, $5 count.
SELECT child_key, value
FROM job_dependency
WHERE parent_queue = $1 AND parent_id = $2 AND status = $3::dep_status
ORDER BY child_id
OFFSET $4 LIMIT $5;

View File

@@ -0,0 +1,9 @@
-- Child-dependency counts per status for a parent job.
-- Params: $1 parent_queue, $2 parent_id. ("unprocessed" = pending children.)
SELECT
COUNT(*) FILTER (WHERE status = 'processed') AS processed,
COUNT(*) FILTER (WHERE status = 'pending') AS unprocessed,
COUNT(*) FILTER (WHERE status = 'ignored') AS ignored,
COUNT(*) FILTER (WHERE status = 'failed') AS failed
FROM job_dependency
WHERE parent_queue = $1 AND parent_id = $2;

View File

@@ -0,0 +1,7 @@
-- Ignored children of a parent as (child_key → failure reason) pairs. The reason
-- is stored as a JSON scalar string, so `#>> '{}'` extracts the raw text
-- (mirrors the Redis `<jobId>:failed` hash). Params: $1 parent_queue, $2 parent_id.
SELECT child_key, COALESCE(value #>> '{}', value::text) AS reason
FROM job_dependency
WHERE parent_queue = $1 AND parent_id = $2 AND status = 'ignored'
ORDER BY child_id;

View File

@@ -0,0 +1,2 @@
-- A job's full row. Params: $1 queue, $2 id.
SELECT * FROM job WHERE queue = $1 AND id = $2;

View File

@@ -0,0 +1,6 @@
-- A page of a job's logs, oldest first.
-- Params: $1 queue, $2 job_id, $3 offset, $4 limit.
SELECT row FROM job_log
WHERE queue = $1 AND job_id = $2
ORDER BY idx ASC
OFFSET $3 LIMIT $4;

View File

@@ -0,0 +1,2 @@
-- Total number of log lines for a job. Params: $1 queue, $2 job_id.
SELECT COUNT(*) AS count FROM job_log WHERE queue = $1 AND job_id = $2;

View File

@@ -0,0 +1,6 @@
-- A page of a job's logs, newest first.
-- Params: $1 queue, $2 job_id, $3 offset, $4 limit.
SELECT row FROM job_log
WHERE queue = $1 AND job_id = $2
ORDER BY idx DESC
OFFSET $3 LIMIT $4;

View File

@@ -0,0 +1,6 @@
-- Fetch a single scheduler's stored metadata and next-run score.
-- Params: $1 queue, $2 scheduler_id.
SELECT name, iteration_count, limit_count, start_date_ms, end_date_ms, tz,
pattern, every_ms, offset_ms, template_data, template_opts, next_run_ms
FROM scheduler
WHERE queue = $1 AND scheduler_id = $2;

View File

@@ -0,0 +1,2 @@
-- Number of registered schedulers in a queue. Param: $1 queue.
SELECT count(*)::int AS count FROM scheduler WHERE queue = $1;

View File

@@ -0,0 +1,10 @@
-- Fetch a page of schedulers ordered by next-run time. Params: $1 queue,
-- $2 asc (boolean), $3 offset, $4 count (NULL = all remaining).
SELECT scheduler_id, next_run_ms
FROM scheduler
WHERE queue = $1
ORDER BY CASE WHEN $2 THEN next_run_ms END ASC,
CASE WHEN NOT $2 THEN next_run_ms END DESC,
scheduler_id
OFFSET $3
LIMIT $4;

View File

@@ -0,0 +1,14 @@
-- Metrics for a queue/kind: the cumulative meta `count` and the per-minute data
-- points (newest first), sliced to [start, end] with Redis LRANGE semantics
-- (index 0 is the newest point; a negative end means "to the oldest").
-- Params: $1 queue, $2 kind ('completed' | 'failed'), $3 start, $4 end.
SELECT
COALESCE((SELECT count FROM metrics
WHERE queue = $1 AND kind = $2), 0)::bigint AS total,
COALESCE((
SELECT CASE
WHEN $4 < 0 THEN data[($3 + 1) : ]
ELSE data[($3 + 1) : ($4 + 1)]
END
FROM metrics WHERE queue = $1 AND kind = $2
), ARRAY[]::bigint[]) AS data;

View File

@@ -0,0 +1,8 @@
-- Processed children of a parent as (child_key → serialized value) pairs. The
-- value is returned as JSON text so the caller can JSON.parse it (mirrors the
-- Redis `<jobId>:processed` hash of stringified values). Params: $1 parent_queue,
-- $2 parent_id.
SELECT child_key, value::text AS value
FROM job_dependency
WHERE parent_queue = $1 AND parent_id = $2 AND status = 'processed'
ORDER BY child_id;

View File

@@ -0,0 +1,2 @@
-- The full queue metadata hash. Param: $1 queue.
SELECT field, value FROM meta WHERE queue = $1;

View File

@@ -0,0 +1,2 @@
-- A single queue metadata field's value. Params: $1 queue, $2 field.
SELECT value FROM meta WHERE queue = $1 AND field = $2;

View File

@@ -0,0 +1,3 @@
-- Several queue metadata fields. Params: $1 queue, $2 fields (text[]).
SELECT field, value FROM meta
WHERE queue = $1 AND field = ANY($2::text[]);

View File

@@ -0,0 +1,3 @@
-- Job ids in a state, sliced by [start, end]. Params: $1 queue, $2 type,
-- $3 start, $4 end, $5 asc.
SELECT id FROM get_range($1, $2, $3, $4, $5) AS t(id);

View File

@@ -0,0 +1,3 @@
-- Current rate-limit ttl in ms (Redis getRateLimitTtl semantics). Params:
-- $1 queue, $2 maxJobs (0 = unspecified → meta `max` / raw window), $3 now_ms.
SELECT rate_limit_ttl($1, $2, $3) AS ttl;

View File

@@ -0,0 +1,2 @@
-- A job's state and priority. Params: $1 queue, $2 id.
SELECT state, priority FROM job WHERE queue = $1 AND id = $2;

View File

@@ -0,0 +1,4 @@
-- Whether a queue metadata field exists. Params: $1 queue, $2 field.
SELECT EXISTS(
SELECT 1 FROM meta WHERE queue = $1 AND field = $2
) AS exists;

View File

@@ -0,0 +1,11 @@
-- Whether the queue has a claimable (waiting, non-paused) job right now. Used
-- by waitForJob to close the race where a NOTIFY fires before the listener is
-- established (mirrors the atomicity of Redis's blocking pop). Param: $1 queue.
SELECT
EXISTS(
SELECT 1 FROM job WHERE queue = $1 AND state = 'waiting'
)
AND NOT EXISTS(
SELECT 1 FROM meta
WHERE queue = $1 AND field = 'paused' AND value = '1'
) AS present;

View File

@@ -0,0 +1,4 @@
-- A job's finished state and result/reason (for waitUntilFinished / isFinished).
-- Params: $1 queue, $2 id.
SELECT state, return_value, failed_reason
FROM job WHERE queue = $1 AND id = $2;

View File

@@ -0,0 +1,6 @@
-- Whether a job is currently in the given state. Params: $1 queue, $2 id,
-- $3 state.
SELECT EXISTS(
SELECT 1 FROM job
WHERE queue = $1 AND id = $2 AND state = $3::job_state
) AS present;

View File

@@ -0,0 +1,15 @@
-- Whether a (non-prioritized) waiting job is in the wait or paused list. The
-- queue has no separate paused list: a waiting job belongs to the paused list
-- when the queue is paused, and to the wait list otherwise. $3 selects which
-- list to test (true = paused, false = wait). Params: $1 queue, $2 id, $3 paused.
SELECT EXISTS(
SELECT 1 FROM job j
WHERE j.queue = $1 AND j.id = $2
AND j.state = 'waiting' AND j.priority = 0
AND (
EXISTS(
SELECT 1 FROM meta m
WHERE m.queue = $1 AND m.field = 'paused' AND m.value = '1'
)
) = $3
) AS present;

View File

@@ -0,0 +1,6 @@
-- Whether a job is prioritized (waiting with a non-zero priority).
-- Params: $1 queue, $2 id.
SELECT EXISTS(
SELECT 1 FROM job
WHERE queue = $1 AND id = $2 AND state = 'waiting' AND priority > 0
) AS present;

View File

@@ -0,0 +1,4 @@
-- Whether an id corresponds to a registered scheduler. Params: $1 queue, $2 id.
SELECT EXISTS (
SELECT 1 FROM scheduler WHERE queue = $1 AND scheduler_id = $2
) AS exists;

View File

@@ -0,0 +1,10 @@
-- Whether the queue is "maxed": it has a global concurrency limit and the
-- number of active jobs has reached it (mirrors isQueueMaxed). Returns false
-- when no concurrency limit is configured.
-- Params: $1 queue.
SELECT COALESCE(
(SELECT count(*) FROM job WHERE queue = $1 AND state = 'active')
>= (SELECT value::integer FROM meta
WHERE queue = $1 AND field = 'concurrency'),
false
) AS maxed;

View File

@@ -0,0 +1,3 @@
-- Subscribe to the shared event-stream channel. Producers
-- `pg_notify('bullmq_events', <queue>)`; a consumer filters for its own queue.
LISTEN bullmq_events;

View File

@@ -0,0 +1,4 @@
-- Subscribe to the shared job-notification channel (fixed name keeps this
-- portable). Producers `pg_notify('bullmq_jobs', <queue>)`; a worker filters
-- notifications for its own queue.
LISTEN bullmq_jobs;

View File

@@ -0,0 +1,4 @@
-- Move an active job back to wait (Job.moveToWait / dynamic rate limit).
-- Params: $1 queue, $2 id, $3 token ('0' bypasses the lock check), $4 now_ms.
-- Returns the limiter window ms (>=0), or -1 when the job no longer exists.
SELECT move_active_to_wait($1, $2, $3, $4) AS n;

View File

@@ -0,0 +1,4 @@
-- Two-phase stalled-job recovery (mark on one pass, reclaim on the next);
-- returns the reclaimed ids. Params: $1 queue, $2 max_stalled_count,
-- $3 now_ms, $4 max_check_time_ms (stalledInterval).
SELECT id FROM move_stalled_jobs_to_wait($1, $2, $3, $4) AS t(id);

View File

@@ -0,0 +1,4 @@
-- Claim the next ready job for a worker (0 or 1 rows), honouring the limiter.
-- Params: $1 queue, $2 token, $3 lock_ms, $4 now_ms, $5 worker name,
-- $6 limiter max (worker option, NULL if none), $7 limiter duration ms.
SELECT * FROM move_to_active($1, $2, $3, $4, $5, $6, $7);

View File

@@ -0,0 +1,4 @@
-- Finish an active job successfully; returns the finished-at timestamp.
-- Params: $1 queue, $2 id, $3 token, $4 return_value (jsonb), $5 finished_on,
-- $6 remove_all, $7 keep_age (s), $8 keep_count.
SELECT move_to_completed($1, $2, $3, $4::jsonb, $5, $6, $7, $8) AS finished_on;

View File

@@ -0,0 +1,9 @@
-- Finish an active job successfully AND claim the next ready job in one
-- transaction (one commit) — the fused equivalent of move_to_completed followed
-- by move_to_active, mirroring Redis's moveToFinished. Returns 0 or 1 job rows.
-- Params: $1 queue, $2 id, $3 token, $4 return_value (jsonb), $5 finished_on,
-- $6 remove_all, $7 keep_age (s), $8 keep_count,
-- $9 lock_ms, $10 now_ms, $11 worker name,
-- $12 limiter max (worker option, NULL if none), $13 limiter duration ms.
SELECT * FROM move_to_completed_fetch(
$1, $2, $3, $4::jsonb, $5, $6, $7, $8, $9, $10, $11, $12, $13);

View File

@@ -0,0 +1,4 @@
-- Re-queue an active job to the delayed state (retry-with-delay / manual delay).
-- Params: $1 queue, $2 id, $3 token, $4 process_at, $5 delay,
-- $6 skip_attempt, $7 failed_reason, $8 stacktrace (jsonb).
SELECT move_to_delayed($1, $2, $3, $4, $5, $6, $7, $8::jsonb) AS n;

View File

@@ -0,0 +1,4 @@
-- Mark an active job failed; returns the finished-at timestamp.
-- Params: $1 queue, $2 id, $3 token, $4 failed_reason, $5 stacktrace (jsonb),
-- $6 finished_on, $7 remove_all, $8 keep_age (s), $9 keep_count.
SELECT move_to_failed($1, $2, $3, $4, $5::jsonb, $6, $7, $8, $9) AS finished_on;

View File

@@ -0,0 +1,9 @@
-- Fail (or retry) an active job AND claim the next ready job in one transaction
-- (one commit) — the fused equivalent of move_to_failed followed by
-- move_to_active, mirroring Redis's moveToFinished. Returns 0 or 1 job rows.
-- Params: $1 queue, $2 id, $3 token, $4 failed_reason, $5 stacktrace (jsonb),
-- $6 finished_on, $7 remove_all, $8 keep_age (s), $9 keep_count,
-- $10 lock_ms, $11 now_ms, $12 worker name,
-- $13 limiter max (worker option, NULL if none), $14 limiter duration ms.
SELECT * FROM move_to_failed_fetch(
$1, $2, $3, $4, $5::jsonb, $6, $7, $8, $9, $10, $11, $12, $13, $14);

View File

@@ -0,0 +1,3 @@
-- Move an active parent to waiting-children if it has pending children.
-- Params: $1 queue, $2 id, $3 token. Returns 1 (should wait), 0 (proceed).
SELECT move_to_waiting_children($1, $2, $3) AS code;

View File

@@ -0,0 +1,2 @@
-- The timestamp of the next delayed job, or NULL if there are none.
SELECT next_delay($1) AS next_delay;

View File

@@ -0,0 +1,3 @@
-- The worker "no job" signal: effective rate-limit ttl + next delayed-job time.
-- Params: $1 queue, $2 limiter max (worker option, NULL if none), $3 now_ms.
SELECT rate_limit_ttl, next_delay FROM next_signal($1, $2, $3);

View File

@@ -0,0 +1,4 @@
-- Obliterate a queue: delete up to $2 jobs; returns -1 (not paused), -2 (active
-- jobs without force), 1 (more to delete), or 0 (done). Params: $1 queue,
-- $2 count, $3 force.
SELECT obliterate($1, $2, $3) AS cursor;

View File

@@ -0,0 +1,19 @@
-- A parent job's child dependencies of a given status, sliced [offset, limit]
-- and joined to the child job rows (for fetchJobs). Params: $1 parent_queue,
-- $2 parent_id, $3 status ('pending' | 'processed'), $4 offset, $5 limit
-- (NULL = no limit). `total` is the full number of matching dependencies
-- (COUNT(*) OVER(), computed before the slice). `dep_value` is the stored
-- result for processed children. The remaining columns are the child
-- `job` row consumed by rowToJobJson.
SELECT
d.child_key,
d.value AS dep_value,
COUNT(*) OVER() AS total,
j.*
FROM job_dependency d
LEFT JOIN job j
ON j.queue = d.child_queue AND j.id = d.child_id
WHERE d.parent_queue = $1 AND d.parent_id = $2 AND d.status = $3::dep_status
ORDER BY d.child_key
OFFSET $4
LIMIT $5;

View File

@@ -0,0 +1,2 @@
-- Pause/resume the queue. Params: $1 queue, $2 paused.
SELECT pause($1, $2);

View File

@@ -0,0 +1,3 @@
-- Promote a delayed job to waiting. Params: $1 queue, $2 id.
-- Returns 0 ok, -1 missing, -3 not delayed.
SELECT promote($1, $2) AS code;

View File

@@ -0,0 +1,3 @@
-- Move up to `count` delayed jobs to waiting; returns the number moved.
-- Params: $1 queue, $2 count.
SELECT promote_jobs($1, $2) AS n;

View File

@@ -0,0 +1,3 @@
-- Append a custom event to the stream; returns the event id.
-- Params: $1 queue, $2 event, $3 data (jsonb).
SELECT publish_event($1, $2, $3::jsonb) AS id;

View File

@@ -0,0 +1,6 @@
-- A page of events newer than a cursor, oldest first.
-- Params: $1 queue, $2 cursor (exclusive), $3 limit.
SELECT id, event, data FROM event
WHERE queue = $1 AND id > $2
ORDER BY id ASC
LIMIT $3;

View File

@@ -0,0 +1,3 @@
-- The current max event id for a queue (used to resolve the '$' cursor).
-- Param: $1 queue.
SELECT COALESCE(MAX(id), 0)::bigint AS max FROM event WHERE queue = $1;

View File

@@ -0,0 +1,3 @@
-- Remove a job (and optionally its children). Returns 1 if removed, else 0.
-- Params: $1 queue, $2 id, $3 remove_children.
SELECT remove($1, $2, $3) AS n;

View File

@@ -0,0 +1,4 @@
-- Break a child's dependency link to its parent. Params: $1 queue, $2 job_id,
-- $3 parent_key, $4 now. Returns 0 (removed) or 1 (no relationship); raises on
-- missing job (-1) / missing parent (-5).
SELECT remove_child_dependency($1, $2, $3, $4) AS n;

View File

@@ -0,0 +1,9 @@
-- Conditionally remove a deduplication key, only when the given job is still
-- its (live) winner (mirrors removeDeduplicationKeyIfNeededOnRemoval and the
-- job-instance Job.removeDeduplicationKey, where Redis GET returns nil for an
-- expired key). Params: $1 queue, $2 dedup_id, $3 job_id, $4 now (ms). Returns
-- the removed key (0 or 1 row).
DELETE FROM dedup
WHERE queue = $1 AND dedup_id = $2 AND job_id = $3
AND (expire_at_ms IS NULL OR expire_at_ms > $4)
RETURNING dedup_id;

View File

@@ -0,0 +1,3 @@
-- Remove a scheduler and its still-pending job (emitting `removed` events);
-- returns 1 if the scheduler existed, 0 otherwise. Params: $1 queue, $2 id.
SELECT remove_job_scheduler($1, $2) AS removed;

View File

@@ -0,0 +1,2 @@
-- Remove queue metadata fields. Params: $1 queue, $2 fields (text[]).
DELETE FROM meta WHERE queue = $1 AND field = ANY($2::text[]);

View File

@@ -0,0 +1,6 @@
-- Clear the limiter window; returns the number of rows removed (0 or 1).
-- Param: $1 queue.
WITH d AS (
DELETE FROM rate_limit WHERE queue = $1 RETURNING 1
)
SELECT count(*)::int AS n FROM d;

View File

@@ -0,0 +1,3 @@
-- Recursively remove a parent's still-pending children. Params: $1 queue,
-- $2 job_id.
SELECT remove_unprocessed_children($1, $2);

View File

@@ -0,0 +1,4 @@
-- Re-queue a finished job (failed/completed) back to wait. Params: $1 queue,
-- $2 id, $3 state, $4 lifo, $5 reset_attempts_made, $6 reset_attempts_started.
-- Returns 1 ok, -1 missing, -3 not in the expected state.
SELECT reprocess_job($1, $2, $3, $4, $5, $6) AS code;

View File

@@ -0,0 +1,3 @@
-- Re-queue an active job back to waiting immediately (retry now).
-- Params: $1 queue, $2 id, $3 token, $4 lifo, $5 failed_reason, $6 stacktrace (jsonb).
SELECT retry_job($1, $2, $3, $4, $5, $6::jsonb) AS n;

View File

@@ -0,0 +1,3 @@
-- Move up to `count` finished jobs back to waiting; returns the number moved.
-- Params: $1 queue, $2 state, $3 count, $4 timestamp.
SELECT retry_jobs($1, $2, $3, $4) AS n;

View File

@@ -0,0 +1,4 @@
-- Upsert queue metadata fields. Params: $1 queue, $2 fields (text[]), $3 values (text[]).
INSERT INTO meta (queue, field, value)
SELECT $1, f, v FROM unnest($2::text[], $3::text[]) AS t(f, v)
ON CONFLICT (queue, field) DO UPDATE SET value = EXCLUDED.value;

View File

@@ -0,0 +1,3 @@
-- Force the limiter window (dynamic / manual rate limit).
-- Params: $1 queue, $2 expire ms, $3 now_ms.
SELECT set_rate_limit($1, $2, $3);

View File

@@ -0,0 +1,3 @@
-- Trim a job's oldest logs. Params: $1 queue, $2 job_id, $3 min_idx_to_keep.
DELETE FROM job_log
WHERE queue = $1 AND job_id = $2 AND idx < $3;

View File

@@ -0,0 +1,3 @@
-- Replace a job's data payload. Params: $1 queue, $2 id, $3 data (jsonb).
UPDATE job SET data = $3::jsonb WHERE queue = $1 AND id = $2
RETURNING id;

View File

@@ -0,0 +1,5 @@
-- Advance an existing scheduler to its next iteration (no template change);
-- returns the new delayed job id, or NULL if the scheduler is gone.
-- Params: $1 queue, $2 scheduler_id, $3 next_millis, $4 template_data (jsonb),
-- $5 delayed_opts (jsonb), $6 now, $7 producer_id.
SELECT update_job_scheduler_next_millis($1, $2, $3, $4, $5, $6, $7) AS job_id;

View File

@@ -0,0 +1,3 @@
-- Update a job's progress and emit a 'progress' event. Returns the number of
-- rows updated (0 = missing job). Params: $1 queue, $2 id, $3 progress (jsonb).
SELECT update_progress($1, $2, $3::jsonb) AS updated;