Home > front end >  PostgreSQL - Does row locking depends on update syntax in transaction?
PostgreSQL - Does row locking depends on update syntax in transaction?

Time:03-31

I have a table called user_table with 2 columns: id (integer) and data_value (integer).

Here are two transactions that end up with the same results:

-- TRANSACTION 1

BEGIN;

UPDATE user_table 
SET data_value = 100
WHERE id = 0;

UPDATE user_table 
SET data_value = 200
WHERE id = 1;

COMMIT;

-- TRANSACTION 2

BEGIN;

UPDATE user_table AS user_with_old_value SET
    data_value = user_with_new_value.data_value
FROM (VALUES
    (0, 100),
    (1, 200)  
) AS user_with_new_value(id, data_value) 
WHERE user_with_new_value.id = user_with_old_value.id;

COMMIT;

I would like to know if there is a difference on the lock applied on the rows.

If I understand it correctly, transaction 1 will first lock user 0, then lock user 1, then free both locks.

But what does transaction 2 do ? Does it do the same thing or does it do: lock user 0 and user 1, then free both locks ?

There is a difference because if i have two concurent transactions, if i write my queries as the first transaction, i might encounter deadlocks issues. But if I write my transactions like the second one, can I run into deadlocks issues ?

If it does the same thing, is there a way to write this transaction so that at the beginning of a transaction, before doing anything, the transaction checks for each rows it needs to update, waits until all this rows are not locked, then lock all rows at the same time ?

links: the syntax of the second transaction comes from: Update multiple rows in same query using PostgreSQL

CodePudding user response:

Both transactions lock the same two rows, and both can run into a deadlock. The difference is that the first transaction always locks the two rows in a certain order.

If your objective is to avoid deadlocks the first transaction is better: if you make a rule that any transaction must update the user with the lower id first, then no such two transactions can deadlock with each other (but they can still deadlock with other transactions that do not obey that rule).

  • Related