怎么掌握PostgreSQL Locks的基础知识

34次阅读
没有评论

共计 3294 个字符,预计需要花费 9 分钟才能阅读完成。

这篇文章主要讲解了“怎么掌握 PostgreSQL Locks 的基础知识”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着丸趣 TV 小编的思路慢慢深入,一起来研究和学习“怎么掌握 PostgreSQL Locks 的基础知识”吧!

如下例所示,session 1 执行 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

可以看到, 进程 2475 中的事务 669310 和进程 2480 中的 669312 分别持有 transactionid 的 ExclusiveLock, 进程 2480 在等待事务 ID=669310 的 lock(granted=f).
为什么会等待 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.
回滚事务 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 的基础知识”的内容了,经过本文的学习后,相信大家对怎么掌握 PostgreSQL Locks 的基础知识这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是丸趣 TV,丸趣 TV 小编将为大家推送更多相关知识点的文章,欢迎关注!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-26发表,共计3294字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)