- Mastering PostgreSQL 12
- Hans Jürgen Sch?nig
- 392字
- 2021-08-20 10:00:21
Understanding basic locking
In this section, you will learn about basic locking mechanisms. The goal is to understand how locking works in general and how to get simple applications right.
To show you how things work, we will create a simple table. For demonstrative purposes, I will add one row to the table using a simple INSERT command:
test=# CREATE TABLE t_test (id int); CREATE TABLE
test=# INSERT INTO t_test VALUES (0); 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 any problems.
The question now is what happens if reads and writes occur at the same time? Here is an example. Let's assume that the table contains one row and its id = 0:

Two transactions are opened. The first one will change a row. However, this is not a problem as the second transaction can proceed. It will return the old row as it was before UPDATE. This behavior is called Multi-Version Concurrency Control (MVCC).
There is also a second important aspect—many commercial or open source databases are still (as of 2018) unable to handle concurrent reads and writes. In PostgreSQL, this is absolutely not a problem—reads and writes can coexist.
After the transaction has been committed, the table will contain 1.
What will happen if two people change data at the same time? Here is an example:

Suppose you want to count the number of hits on a website. If you run the preceding code, no hit will be lost because PostgreSQL guarantees that one UPDATE statement is performed after the other.
It is also worth noting that you can always run concurrent reads. Our two writes will not block reads.
- ABB工業(yè)機(jī)器人編程全集
- R Machine Learning By Example
- 并行數(shù)據(jù)挖掘及性能優(yōu)化:關(guān)聯(lián)規(guī)則與數(shù)據(jù)相關(guān)性分析
- PyTorch深度學(xué)習(xí)實(shí)戰(zhàn)
- 西門子S7-200 SMART PLC實(shí)例指導(dǎo)學(xué)與用
- CompTIA Linux+ Certification Guide
- 運(yùn)動(dòng)控制系統(tǒng)應(yīng)用與實(shí)踐
- Excel 2007常見技法與行業(yè)應(yīng)用實(shí)例精講
- Visual FoxPro程序設(shè)計(jì)
- Linux Shell編程從初學(xué)到精通
- 智能鼠原理與制作(進(jìn)階篇)
- 智慧未來
- 手把手教你學(xué)Photoshop CS3
- Hadoop Beginner's Guide
- Advanced Deep Learning with Keras