如何在 PostgreSQL 中通过多个并行会话展示 MVCC 的工作?

How to showcase the work of MVCC with several parallel sessions in PostgreSQL?

我需要借助将同时访问相同 table 的并行会话来演示 MVCC 在 PostgreSQL 中的工作。

我不知道该怎么做。 请告诉我。

会话 1:

CREATE TABLE test(id integer);

INSERT INTO test VALUES (1);

START TRANSACTION;

SELECT ctid, xmin, xmax, id FROM test;
 ctid  | xmin | xmax | id 
-------+------+------+----
 (0,1) | 5163 |    0 |  1
(1 row)

此行版本由事务 5163 创建。它是 table 块 0 中的项目 1。

UPDATE test SET id = 2;

SELECT ctid, xmin, xmax, id FROM test;
 ctid  | xmin | xmax | id 
-------+------+------+----
 (0,2) | 5164 |    0 |  2
(1 row)

更新插入了一个新的行版本。这是 table 区块 0 中的项目 2,由事务 5164(此事务)创建。

第 2 节:

SELECT ctid, xmin, xmax, id FROM test;
 ctid  | xmin | xmax | id 
-------+------+------+----
 (0,1) | 5163 | 5164 |  1
(1 row)

会话 2 仍然看到旧行版本,因为删除事务 5164 尚未提交。

会话 1:

COMMIT;

第 2 节:

SELECT ctid, xmin, xmax, id FROM test;
 ctid  | xmin | xmax | id 
-------+------+------+----
 (0,2) | 5164 |    0 |  2
(1 row)

现在会话 1 已提交,会话 2 也会看到新的行版本。