PostgreSQL VMCC多版本并发控制

(Multi-Version Concurrency Control多版本并发控制)

 

介绍

Maintain data consistency internally

         •    While querying a database each transaction sees a

                   snapshot of data (a database version) as it was some

                   time ago

         •    Prevent transaction from viewing inconsistent data

         •    Provides transaction isolation in concurrent transactions

         •    Readers does not block writers and writers does not

                   block readers

用例

1同时启动两个服务

比如,打开两个命令窗口。

2分别在两个窗口下查询t表的内容,此时是相同的。

highgo=# select * from t;  

 id |  name

----+---------

  2 | jasmine

  3 | lily

  4 | jasmin

  5 | lil

(4 行记录)

3在窗口1中执行BEGIN;

然后执行插入,

此时在窗口1中查询到了插入的数值,但在窗口2中查不到。

然后执行END;此时在窗口2中就可以查到插入的数据了。

highgo=# begin;

BEGIN

highgo=# insert into t(name) values('qq'),('hh');

INSERT 0 2

highgo=# select * from t;

 id |  name

----+---------

  2 | jasmine

  3 | lily

  4 | jasmin

  5 | lil

  8 | qq

  9 | hh

(6 行记录)

highgo=# end   end;之后另一个事务中才能查询到本事务对数据的更改。

COMMIT

4再窗口1中做了BEGIN;后,对id=1的行进行更新;

此时在窗口2中也对id=1的行更新时,执行会等待;

我们在窗口1中执行end;之后窗口2中对id=1的行的更行接着就会执行。

highgo=# begin;

BEGIN

highgo=# update t set id=1 where id=2;

UPDATE 1

highgo=# end   end;之后另一个事务才能对同一数据进行更新。

COMMIT

highgo=#

原文地址:https://www.cnblogs.com/liuyuanyuanGOGO/p/postgresql_vmcc.html