When reading about optimistic locks, I have come across examples which use versioning like below
UPDATE table SET column1 = new_value1, version = version + 1 WHERE id = some_id AND version = old_version
I am not able to understand how it is absolutely safe. If two concurrent transactions read the same old version during update and then both proceed to update column1 and version we will have incorrect update here. Unless both WHERE and SET operations are atomic at the database level(that is transaction does read and update atomically in one go), one transaction can read old version and but before it updates the new version, another transaction can also read same old version and then both will proceed to update the version leading to incorrect update.
what am I missing here? How is this handled in MySql, Postgress or MS Sql Server?