Understanding basic locking
In this section, you will learn about basic locking mechanisms. The goal is to make you understand how locking works in general and how to get simple applications right.
To show how things work, a simple table can be created. For demonstration purposes, I will add one row to the table:
test=# CREATE TABLE t_test (id int); CREATE TABLE test=# INSERT INTO t_test VALUES (1); INSERT 0 1
The first important thing is that tables can be read concurrently. Many users reading the same data at the same time won't block each other. This allows PostgreSQL to handle thousands of users without problems.
Note
Multiple users can read the same data at the same time without blocking each other.
The question now is: what happens if reads and writes occur at the same time? Here is an example:
Transaction 1 | Transaction 2 |
|
|
| |
User will see |
|
| User will see |
|
|
Two transactions are opened. The first one will...