I have an n8n workflow that changes a PostgreSQL job from pending to processing before doing several API calls.
The problem is that if the n8n execution crashes halfway through, the job stays processing forever and never gets picked up again.
I’m considering adding a timeout:
UPDATE jobs
SET
status = ‘pending’,
worker_id = NULL,
locked_at = NULL
WHERE status = ‘processing’
AND locked_at < NOW() - INTERVAL ‘10 minutes’;
What is the best production approach for handling stuck jobs and automatic retries in n8n + PostgreSQL?
Should I use a lease/heartbeat system, retry counters, or both—and how can I make sure a recovered job isn’t processed by two workers at the same time?
Describe the problem/error/question
What is the error message (if any)?
Please share your workflow
(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)
I would use both a lease/timeout and a retry counter.
The lease handles crashed workers, while the retry counter prevents a permanently failing job from retrying forever.
Recover stuck jobs
UPDATE jobs
SET
status = ‘pending’,
worker_id = NULL,
locked_at = NULL
WHERE status = ‘processing’
AND locked_at < NOW() - INTERVAL ‘10 minutes’
AND attempts < 5;
Track attempts
When claiming a job:
UPDATE jobs
SET
status = ‘processing’,
worker_id = $1,
locked_at = NOW(),
attempts = attempts + 1
WHERE id = $2
AND status = ‘pending’
RETURNING *;
Prevent duplicate processing
Use FOR UPDATE SKIP LOCKED when multiple workers are claiming jobs so they don’t select the same row simultaneously.
This gives you reliable recovery without allowing jobs to retry indefinitely.
Hi @Greg_John
Set an Error Workflow on the job workflow so a failed execution releases its own lease instead of sitting out the full 10 minutes. Write the execution id onto the row when you claim it, {{ $execution.id }} into worker_id, then start the error workflow with an Error Trigger and release only that row:
UPDATE jobs
SET status = 'pending', worker_id = NULL, locked_at = NULL
WHERE worker_id = $1
AND status = 'processing';
$1 comes from {{ $json.execution.id }}, which is what the Error Trigger outputs. Matching on worker_id is what stops a late error workflow from unlocking a row that has already been reclaimed and handed to another run.
That covers node and expression failures. It does not fire when the n8n process itself dies or gets OOM-killed, so keep your timeout sweep for that case.