Any application that actually cares about consistency of its data must be doing this.
Or just do it one step so the update increments the value in place. That way you have an implicit lock.
Any application that actually cares about consistency of its data must be doing this.
Or just do it one step so the update increments the value in place. That way you have an implicit lock.
Note that "one step" updates (e.g., SET followers_count = followers_count + 1) are still not in place. They are regular transactional updates that rewrite the entire row. Still, they can achieve higher throughput because there is no application/database roundtrip between locking and committing.
There are multiple flavors in PostgreSQL with different locking semantics. And the exact locking semantics may differ between database(for instance postgres does not have gap locks, while MySQL does) so you'd have to read the docs if that detail matters to you.
what FOR UPDATE does is (simplified) to lock a mutex for each row the select statement returns which are released once the transaction commits
this has the effect that parallel running transactions have to wait until the new computed value is visible before reading it and in turn there is no problem with lost updates
just to be clear this is a very simplified explanation in many ways
https://www.jooq.org/doc/latest/manual/sql-execution/crud-wi...