以下是postgres日志
Process 10396 waits for RowShareLock on relation 17204 of database 16384; blocked by process 10377.
Process 10377 waits for ShareLock on transaction 149848948; blocked by process 10396.
Process 10396: insert into "completed_jobs" ("id", ....... "limitation_code") values ($1, ...... $22) returning "id"
Process 10377: UPDATE jobs SET status='pending', updated_at=$1 WHERE id=$2
我是从郎朗执行的。所以这是在分布式环境中。
更新是正常的执行,
_, err = tx.Exec("UPDATE jobs SET status='pending', updated_at=$1 WHERE id=$2", time.Now().UTC(), job.Id)
if err != nil {
log.Println(getMessagePrefix(job, nil), "Error updating job status to pending", err)
}
err = tx.Commit()
插入是在事务中,
tx, _ := db.Begin()
tx.Exec("UPDATE jobs SET status=$1 WHERE id=$6", status)
tx.Exec("INSERT INTO completed_jobs SELECT * FROM jobs WHERE id=$1", job.Id)
tx.Exec("DELETE FROM jobs WHERE id=$1", job.Id)
err := tx.Commit()
答案 0 :(得分:0)
在作业排队中,我通常使用以下策略来防止多个进程尝试访问相同的作业。通常,您需要跨事务控制来处理此问题。
作业队列系统中的并发性会带来大量问题。但是那两个解决了最糟糕的问题。