- Mastering PostgreSQL 10
- Hans Jürgen Sch?nig
- 393字
- 2021-06-30 19:03:50
Understanding basic locking
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.
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:

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 the 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 2017) 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 2.
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 code as outlined just now, no hit can be lost because PostgreSQL guarantees that one UPDATE is performed after the other.
It is also noteworthy that you can always run concurrent reads. Our two writes will not block reads.
- 大數(shù)據(jù)導(dǎo)論:思維、技術(shù)與應(yīng)用
- Big Data Analytics with Hadoop 3
- 現(xiàn)代測(cè)控電子技術(shù)
- 工業(yè)機(jī)器人工程應(yīng)用虛擬仿真教程:MotoSim EG-VRC
- 工業(yè)機(jī)器人現(xiàn)場(chǎng)編程(FANUC)
- 機(jī)器學(xué)習(xí)流水線實(shí)戰(zhàn)
- 完全掌握AutoCAD 2008中文版:綜合篇
- 21天學(xué)通Visual Basic
- 悟透JavaScript
- 電氣控制與PLC技術(shù)應(yīng)用
- 分析力!專業(yè)Excel的制作與分析實(shí)用法則
- Applied Data Visualization with R and ggplot2
- 分布式Java應(yīng)用
- 智能小車機(jī)器人制作大全(第2版)
- MySQL Management and Administration with Navicat