I have a self-hosted n8n setup where workflows process jobs stored in PostgreSQL. I’m using multiple workers, and sometimes a worker crashes or loses its connection while processing a job.
The problem is that the job can remain in processing even though the worker never completed it. If I simply retry all failed jobs, I’m worried about two workers processing the same job simultaneously.
My current table looks like:
CREATE TABLE jobs (
id BIGSERIAL PRIMARY KEY,
status TEXT NOT NULL,
locked_at TIMESTAMP,
worker_id TEXT,
updated_at TIMESTAMP DEFAULT NOW()
);
I’m considering claiming jobs using:
UPDATE jobs
SET
status = 'processing',
locked_at = NOW(),
worker_id = $1
WHERE id = $2
AND status = 'pending'
RETURNING *;
Then periodically resetting jobs where locked_at is older than a certain threshold.
What would be the safest production pattern for job claiming, worker failures, and retries in n8n + PostgreSQL?
Would SELECT … FOR UPDATE SKIP LOCKED be better here, and how would you prevent a recovered/stale worker from updating a job that has already been picked up by another worker?
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.)
Share the output returned by the last node
Information on your n8n setup
- n8n version:
- Database (default: SQLite):
- n8n EXECUTIONS_PROCESS setting (default: own, main):
- Running n8n via (Docker, npm, n8n cloud, desktop app):
- Operating system: