Postgresql -> deadlock from simple update. I can't get the cause Postgresql -> deadlock from simple update. I can't get the cause postgresql postgresql

Postgresql -> deadlock from simple update. I can't get the cause


Session A tries to update ids 10, 2, 30, 4 and session B tries with 40, 30, 20, 10

They both try to lock their respective rows ready for update and A gets 10 and is waiting for 30 while B gets 30 and is waiting for 10. Deadlock.

Your fundamental issue is that you are trying to update (some of) the same ids in concurrent transactions.

Without knowing your database structure and precisely what you're trying to do, it's difficult to suggest the best solution. Typically, you would either make sure different backends don't update the same rows, or reduce timeouts and just retry after a random pause.


In most of case deadlock arrives because of circular wait between rows will going to update so if u want to resolve deadlock you can simply use ordering on rows which you want to update

UPDATE link SET page_id = ?, placed_at = now() WHERE id IN ( SELECT id FROM link ... order by page_id ) AND page_id IS NOT NULL