共計 1548 個字符,預計需要花費 4 分鐘才能閱讀完成。
本篇內容主要講解“PostgreSQL 中 Tuple 可見性判斷分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“PostgreSQL 中 Tuple 可見性判斷分析”吧!
一、xmin/xmax 是當前事務
Tuple.xmin 或 xmax 是當前事務的情況, 事務狀態為 IN_PROGRESS, 其判斷邏輯如下:
插入未提交
If xmin == 當前事務 Then
If xmax == 當前事務 Then
元組不可見
Else
元組可見
End If
End If
如下例所示:
15:40:39 (xdb@[local]:5432)testdb=#
15:40:40 (xdb@[local]:5432)testdb=# begin;
BEGIN
15:40:44 (xdb@[local]:5432)testdb=#* insert into t_session1 values(1);
INSERT 0 1
15:40:52 (xdb@[local]:5432)testdb=#* update t_session1 set id = 0;
UPDATE 1
15:41:02 (xdb@[local]:5432)testdb=#* select lp,lp_off,t_xmin,t_xmax from heap_page_items(get_raw_page( t_session1 ,0));
lp | lp_off | t_xmin | t_xmax
----+--------+--------+--------
1 | 8160 | 2370 | 2370 -- 2370 插入數據,2370 更新數據, 該 Tuple 不可見
2 | 8128 | 2370 | 0 -- 2370 更新后的數據, 該 Tuple 可見
(2 rows)
插入已提交
If xmax == 當前事務 Then
元組不可見
End If
15:41:11 (xdb@[local]:5432)testdb=#* delete from t_session1;
DELETE 1
15:41:36 (xdb@[local]:5432)testdb=#* select lp,lp_off,t_xmin,t_xmax from heap_page_items(get_raw_page( t_session1 ,0));
lp | lp_off | t_xmin | t_xmax
----+--------+--------+--------
1 | 8160 | 2370 | 2370 -- 2370 更新該 Tuple, 不可見
2 | 8128 | 2370 | 2370 -- 2370 刪除該 Tuple, 不可見
(2 rows)
15:41:38 (xdb@[local]:5432)testdb=#* commit;
COMMIT
注意: 在這種情況下 (xmax == 當前事務),xmin 狀態不可能是 ABORTED, 因為不可能 Update/Delete 不存在的元組 (事務未提交可視為不存在).
二、xmin xmax 非當前事務
xmin 和 xmax 均不是當前事務, 假定快照為 ST1:ST2:XIP[], 其判斷邏輯如下:
If xmin.STATUS == COMMITTED Then
If xmax ST1 xmax.STATUS == COMMITTED Then
元組不可見
If xmax ? XIP[] xmax.STATUS == COMMITTED Then
元組不可見
Else
元組可見
End If
Else
元組不可見
End If
到此,相信大家對“PostgreSQL 中 Tuple 可見性判斷分析”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!