title: PostgreSQL - Does row locking depends on update syntax in transaction?
body: <p>I have a table called user_table with 2 columns: id (integer) and data_value (integer).</p>
  <p>Here are two transactions that end up with the same results:</p>
  <pre><code>-- 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;
  </code></pre>
  <p>I would like to know if there is a difference on the lock applied on the rows.</p>
  <p>If I understand it correctly, transaction 1 will first lock user 0, then lock user 1, then free both locks.</p>
  <p>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 ?</p>
  <p>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 ?</p>
  <p>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 ?</p>
  <p><em>links:</em>
  <em>the syntax of the second transaction comes from: <a href="https://stackoverflow.com/questions/18797608/update-multiple-rows-in-same-query-using-postgresql">Update multiple rows in same query using PostgreSQL</a></em></p>