Up until now, you have seen how to handle locking, as well as some basic concurrency. In this section, you will learn about transaction isolation. To me, this is one of the most neglected topics in modern software development. Only a small fraction of software developers are actually aware of this issue, which in turn leads to mind-boggling bugs.
Here is an example of what can happen:
Transaction 1 |
Transaction 2 |
BEGIN; |
|
SELECT sum(balance) FROM t_account; |
|
User will see 300 |
BEGIN; |
INSERT INTO t_account (balance) VALUES (100); |
|
COMMIT; |
|
SELECT sum(balance) FROM t_account; |
|
User will see 400 |
|
COMMIT; |
Most users would actually expect the first transaction to always return 300, regardless of the second transaction. However, this isn't true. By default, PostgreSQL runs in the READ COMMITTED transaction isolation mode. This means that every statement inside a transaction will get a new snapshot of...