Sathapanic Sriprom
Sathapanic Sriprom

Reputation: 365

Postgres: Acquire lock on target records before updating

I'm working with a PostgreSQL statement. My goal is to modify the statement to avoid the deadlock problem. Currently, there are 2 statements updating the same records. This must happen in some times and cannot be avoided because it's the real operation in the production.

What I want to do is to create the UPDATE statement that will obtain lock to all records that will be updated. I have tried SELECT FOR UPDATE, LOCK TABLE as exclusive mode , UPDATE in a LOOP". Neither has worked for me. The deadlock still happened. You guys who have any ideas for this?

Upvotes: 0

Views: 492

Answers (1)

Kouber Saparev
Kouber Saparev

Reputation: 8105

Mind using advisory locking, in case you cannot figure out the exact relations or records to lock.

Upvotes: 1

Related Questions