Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

basic SQL atomicity "UPDATE ... SET .. WHERE ..."

Tags:

sql

atomic

I have a rather basic and general question about atomicity of "UPDATE ... SET .. WHERE ..." statement.

having a table (without extra constraint),

+----------+
| id | name|
+----------+
|  1 |  a  |
+----+-----+

now, I would execute following 4 statements "at the same time" (concurrently).

UPDATE table SET name='b1' WHERE name='a'
UPDATE table SET name='b2' WHERE name='a'
UPDATE table SET name='b3' WHERE name='a'
UPDATE table SET name='b4' WHERE name='a'

is there only one UPDATE statement would be executed with table update? or, is it possible that more than one UPDATE statements can really update the table?

should I need extra transaction or lock to let only one UPDATE write values into table?

thanks

[EDIT] the 4 UPDATE statements are executed parallel from different processes. [EDIT] with Postgresql

like image 507
rnd_nr_gen Avatar asked Nov 23 '25 14:11

rnd_nr_gen


1 Answers

One of these statements will lock the record (or the page, or the whole table, depending on your engine and locking granularity) and will be executed.

The others will wait for the resource to be freed.

When the lucky statement will commit, the others will either reread the table and do nothing (if your transaction isolation mode is set to READ COMMITTED) or fail to serialize the transaction (if the transaction isolation level is SERIALIZABLE).

like image 140
Quassnoi Avatar answered Nov 26 '25 04:11

Quassnoi