溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

分析PostgreSQL中的tuple locks

發(fā)布時間:2021-11-05 15:43:41 來源:億速云 閱讀:435 作者:iii 欄目:關系型數據庫

這篇文章主要介紹“分析PostgreSQL中的tuple locks”,在日常操作中,相信很多人在分析PostgreSQL中的tuple locks問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”分析PostgreSQL中的tuple locks”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

相對于tables或者其他數據庫對象,元組的locks并不太容易處理,問題是事務可能在某個時間點
鎖定大批量的tuples,因此不可能在共享內存中存儲這些鎖信息。為了突破這個限制,PG使用了兩級機制。
第一級:把鎖信息存儲在tuple header實現(xiàn):把當前事務的XID存儲在XMAX中,同時設置特別的
infomask bits來區(qū)分已刪除的tuples和這些被鎖定的tuples。
第二級:如有并發(fā)操作,則使用MultiXact。
這種機制可以容納同時被鎖定的任意數量的元組。

示例
插入數據,并執(zhí)行update,update操作會lock tuples

[local:/data/run/pg12]:5120 pg12@testdb=# begin;
BEGIN
[local:/data/run/pg12]:5120 pg12@testdb=#* insert into t values(1),(2);
INSERT 0 2
[local:/data/run/pg12]:5120 pg12@testdb=#* commit;
COMMIT
[local:/data/run/pg12]:5120 pg12@testdb=# begin;
BEGIN
[local:/data/run/pg12]:5120 pg12@testdb=#* update t set id = 2;
UPDATE 2
[local:/data/run/pg12]:5120 pg12@testdb=#* 
[local:/data/run/pg12]:5120 pg12@testdb=#* select txid_current();
   1512070049
-- 
[local:/data/run/pg12]:5120 pg12@testdb=# select xmin,xmax from t;
    xmin    |    xmax    
------------+------------
 1512070048 | 1512070049
 1512070048 | 1512070049
(2 rows)
[local:/data/run/pg12]:5120 pg12@testdb=#

鎖信息

[local:/data/run/pg12]:5120 pg12@testdb=# select pid,mode,locktype,relation,page,tuple,transactionid from pg_locks where pid <> pg_backend_pid();
 pid  |       mode       |   locktype    | relation | page | tuple | transactionid 
------+------------------+---------------+----------+------+-------+---------------
 1877 | RowExclusiveLock | relation      |    74856 |      |       |              
 1877 | ExclusiveLock    | virtualxid    |          |      |       |              
 1877 | ExclusiveLock    | transactionid |          |      |       |    1512070049
(3 rows)

到此,關于“分析PostgreSQL中的tuple locks”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI