In this section, you will learn basic locking mechanisms. The goal is to 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.
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. Let us assume that the table contains one row and its id = 0:
Transaction 1 |
Transaction... |