Have tried researching the existing questions, but did not find posts with a definitive answer.
What we want to implement is to atomically update the value of two rows in one postgres table. we would like to implement logic similar to the blow. We can capture the whole logic in one transaction and set the transaction level to serializable to guarantee atomicity. My understand is that that will have the database to lock the whole table, and reduce concurrency and throughput. What is the best practice for increasing concurrency while gauaranteeing atomicity?
create table accounts (
id int,
remain double
}
remain = account1.remain
if val <= remain:
account1.remain = account1.remain - val
account2.remain = account2.remain + val