這篇文章主要講解了“怎么掌握PostgreSQL Locks的基礎(chǔ)知識(shí)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么掌握PostgreSQL Locks的基礎(chǔ)知識(shí)”吧!
公司主營業(yè)務(wù):網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出江孜免費(fèi)做網(wǎng)站回饋大家。
如下例所示,session 1執(zhí)行update語句,session 2 update相同的rows,session 3查詢locktype為transactionid的信息.
session 1
[local]:5432 pg12@testdb=# begin; ere relation=295053; BEGIN Time: 1.430 ms [local]:5432 pg12@testdb=#* -- SELECT * from t_lock where id < 10 FOR UPDATE; [local]:5432 pg12@testdb=#* select pg_backend_pid(); pg_backend_pid ---------------- 2475 (1 row) Time: 2.619 ms [local]:5432 pg12@testdb=#* update t_lock set id = 3000 where id = 3; UPDATE 4 Time: 7.892 ms [local]:5432 pg12@testdb=#* select pid,locktype,relation::regclass,mode,page,tuple,virtualxid,transactionid,virtualtransaction,granted,fastpath from pg_locks where relation=295053; -[ RECORD 1 ]------+----------------- pid | 2475 locktype | relation relation | t_lock mode | RowExclusiveLock page | tuple | virtualxid | transactionid | virtualtransaction | 3/2 granted | t fastpath | t Time: 9.013 ms
session 2
[local]:5432 pg12@testdb=# ---- session 2 [local]:5432 pg12@testdb=# begin; BEGIN Time: 1.117 ms [local]:5432 pg12@testdb=#* select pg_backend_pid(); pg_backend_pid ---------------- 2480 (1 row) Time: 1.825 ms [local]:5432 pg12@testdb=#* update t_lock set id = 3000 where id = 3; -- 阻塞/掛起
session 3
[local]:5432 pg12@testdb=# select * from pg_locks where pid <> pg_backend_pid() and locktype = 'transactionid'; locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath ---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+---------------+---------+---------- transactionid | | | | | | 669310 | | | | 3/2 | 2475 | ExclusiveLock | t | f transactionid | | | | | | 669312 | | | | 4/4 | 2480 | ExclusiveLock | t | f transactionid | | | | | | 669310 | | | | 4/4 | 2480 | ShareLock | f | f (3 rows) Time: 1.243 ms
可以看到,進(jìn)程2475中的事務(wù)669310和進(jìn)程2480中的669312分別持有transactionid的ExclusiveLock,進(jìn)程2480在等待事務(wù)ID=669310的lock(granted=f).
為什么會(huì)等待669310的ShareLock呢?回過頭來查看t_lock表的xmax信息:
[local]:5432 pg12@testdb=# select xmin,xmax,ctid from t_lock where id = 3; xmin | xmax | ctid --------+--------+--------- 669246 | 669310 | (0,3) 669247 | 669310 | (4,99) 669248 | 669310 | (8,195) 669252 | 669310 | (13,65) (4 rows) Time: 4.715 ms
可以看到 : 待更新的tuple.xmax = 669310.
回滾事務(wù)669310,再次查看xmax:
[local]:5432 pg12@testdb=# select xmin,xmax,ctid from t_lock where id = 3; xmin | xmax | ctid --------+--------+--------- 669246 | 669312 | (0,3) 669247 | 669312 | (4,99) 669248 | 669312 | (8,195) 669252 | 669312 | (13,65) (4 rows) Time: 1.182 ms [local]:5432 pg12@testdb=# SELECT pid,backend_xid,wait_event_type,wait_event,state,query FROM pg_stat_activity WHERE pid IN (2475,2480); -[ RECORD 1 ]---+------------------------------------------ pid | 2475 backend_xid | wait_event_type | Client wait_event | ClientRead state | idle query | rollback; -[ RECORD 2 ]---+------------------------------------------ pid | 2480 backend_xid | 669312 wait_event_type | Client wait_event | ClientRead state | idle in transaction query | update t_lock set id = 3000 where id = 3; Time: 5.434 ms
xmax被更新為669312.
感謝各位的閱讀,以上就是“怎么掌握PostgreSQL Locks的基礎(chǔ)知識(shí)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么掌握PostgreSQL Locks的基礎(chǔ)知識(shí)這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
文章標(biāo)題:怎么掌握PostgreSQLLocks的基礎(chǔ)知識(shí)
文章來源:http://jinyejixie.com/article32/iishpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、用戶體驗(yàn)、網(wǎng)站維護(hù)、網(wǎng)站排名、網(wǎng)站內(nèi)鏈、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)