Selecting for Share and Update in PostgreSQL
Ref: http://shiroyasha.io/selecting-for-share-and-update-in-postgresql.html https://docs.oracle.com/cd/E17952_01/mysql-5.1-en/innodb-locking-reads.html A regular select statement does not give you enough protection if you want to query data and make a change in the database related to it. Other transactions can update or delete the data you just queried. PostgreSQL offers additional select statements that lock on read and provide an extra layer of safety. This article explores the select for share and select for update statements, locks that are created with these statements, and provide examples for using these two select statements. Safely Updating Data Sometimes, applications read data from the database, process the data, and save the result back in the database. This is a classic example where the select for update can provide additional safety. Let’s consider the following example: BEGIN ; SELECT * FROM purchases WHERE p...