• 售前

  • 售后

热门帖子
入门百科

Postgresql - 检察锁表信息的实现

[复制链接]
hecgdge4 显示全部楼层 发表于 2021-10-26 13:05:22 |阅读模式 打印 上一主题 下一主题
查看表锁信息,是DBA常用的脚本之一。
实验环境:
CentOS 7
PG 10.4
先通过A窗口实行
  1. mytest=# begin;
  2. BEGIN
  3. mytest=# update t1 set col1 = 'a' where id =1 ;
  4. UPDATE 1
  5. mytest=#
复制代码
打开B窗口实行
  1. mytest=# begin;
  2. BEGIN
  3. mytest=# update t1 set col1 = 'b' where id =2;
  4. UPDATE 1
  5. mytest=# update t1 set col1 = 'b' where id =1;
复制代码
等候了
说明只锁住了行,对于更新其他行没有影响。
再打开一个窗口查看信息
  1. SELECT
  2. a.datname,
  3. locktype,
  4. virtualtransaction,
  5. transactionid,
  6. nspname,
  7. relname,
  8. mode,
  9. granted,
  10. cast(date_trunc('second',query_start) AS timestamp) AS query_start
  11. FROM
  12. pg_locks
  13. LEFT OUTER JOIN pg_class ON (pg_locks.relation = pg_class.oid)
  14. LEFT OUTER JOIN pg_namespace ON (pg_namespace.oid = pg_class.relnamespace),
  15. pg_stat_activity a
  16. WHERE NOT pg_locks.pid = pg_backend_pid()
  17. AND pg_locks.pid=a.pid;
  18. datname | locktype | virtualtransaction | transactionid | nspname | relname | mode | granted | query_start
  19. ---------+---------------+--------------------+---------------+---------+---------+------------------+---------+---------------------
  20. mytest | relation | 7/332 | | public | t1 | RowExclusiveLock | t | 2018-06-28 06:29:58
  21. mytest | virtualxid | 7/332 | | | | ExclusiveLock | t | 2018-06-28 06:29:58
  22. mytest | relation | 6/42 | | public | t1 | RowExclusiveLock | t | 2018-06-28 06:29:35
  23. mytest | virtualxid | 6/42 | | | | ExclusiveLock | t | 2018-06-28 06:29:35
  24. mytest | transactionid | 7/332 | 712 | | | ExclusiveLock | t | 2018-06-28 06:29:58
  25. mytest | transactionid | 6/42 | 711 | | | ExclusiveLock | t | 2018-06-28 06:29:35
  26. mytest | transactionid | 7/332 | 711 | | | ShareLock | f | 2018-06-28 06:29:58
  27. mytest | tuple | 7/332 | | public | t1 | ExclusiveLock | t | 2018-06-28 06:29:58
  28. (8 rows)
复制代码
增补:如何查看PostgreSQL正在实行的SQL以及锁信息
查看当前正在运行的SQL
  1. SELECT
  2. procpid,
  3. start,
  4. now() - start AS lap,
  5. current_query
  6. FROM
  7. (SELECT
  8. backendid,
  9. pg_stat_get_backend_pid(S.backendid) AS procpid,
  10. pg_stat_get_backend_activity_start(S.backendid) AS start,
  11. pg_stat_get_backend_activity(S.backendid) AS current_query
  12. FROM
  13. (SELECT pg_stat_get_backend_idset() AS backendid) AS S
  14. ) AS S
  15. WHERE
  16. current_query <> '<IDLE>'
  17. ORDER BY
  18. lap DESC;
  19. procpid:进程id
  20. start:进程开始时间
  21. lap:经过时间
  22. current_query:执行中的sql
  23. 怎样停止正在执行的sql
  24. SELECT pg_cancel_backend(进程id);
  25. 或者用系统函数
  26. kill -9 进程id;
复制代码
查看数据库现在是否有锁
  1. -- 查看当前事务锁等待、持锁信息的SQL
  2. with  
  3. t_wait as  
  4. (  
  5. select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,  
  6. a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,  
  7. b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name  
  8.   from pg_locks a,pg_stat_activity b where a.pid=b.pid and not a.granted  
  9. ),  
  10. t_run as  
  11. (  
  12. select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,  
  13. a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,  
  14. b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name  
  15.   from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted  
  16. ),  
  17. t_overlap as  
  18. (  
  19. select r.* from t_wait w join t_run r on  
  20. (  
  21.   r.locktype is not distinct from w.locktype and  
  22.   r.database is not distinct from w.database and  
  23.   r.relation is not distinct from w.relation and  
  24.   r.page is not distinct from w.page and  
  25.   r.tuple is not distinct from w.tuple and  
  26.   r.virtualxid is not distinct from w.virtualxid and  
  27.   r.transactionid is not distinct from w.transactionid and  
  28.   r.classid is not distinct from w.classid and  
  29.   r.objid is not distinct from w.objid and  
  30.   r.objsubid is not distinct from w.objsubid and  
  31.   r.pid <> w.pid  
  32. )  
  33. ),  
  34. t_unionall as  
  35. (  
  36. select r.* from t_overlap r  
  37. union all  
  38. select w.* from t_wait w  
  39. )  
  40. select locktype,datname,relation::regclass,page,tuple,virtualxid,transactionid::text,classid::regclass,objid,objsubid,  
  41. string_agg(  
  42. 'Pid: '||case when pid is null then 'NULL' else pid::text end||chr(10)||  
  43. 'Lock_Granted: '||case when granted is null then 'NULL' else granted::text end||' , Mode: '||case when mode is null then 'NULL' else mode::text end||' , FastPath: '||case when fastpath is null then 'NULL' else fastpath::text end||' , VirtualTransaction: '||case when virtualtransaction is null then 'NULL' else virtualtransaction::text end||' , Session_State: '||case when state is null then 'NULL' else state::text end||chr(10)||  
  44. 'Username: '||case when usename is null then 'NULL' else usename::text end||' , Database: '||case when datname is null then 'NULL' else datname::text end||' , Client_Addr: '||case when client_addr is null then 'NULL' else client_addr::text end||' , Client_Port: '||case when client_port is null then 'NULL' else client_port::text end||' , Application_Name: '||case when application_name is null then 'NULL' else application_name::text end||chr(10)||  
  45. 'Xact_Start: '||case when xact_start is null then 'NULL' else xact_start::text end||' , Query_Start: '||case when query_start is null then 'NULL' else query_start::text end||' , Xact_Elapse: '||case when (now()-xact_start) is null then 'NULL' else (now()-xact_start)::text end||' , Query_Elapse: '||case when (now()-query_start) is null then 'NULL' else (now()-query_start)::text end||chr(10)||  
  46. 'SQL (Current SQL in Transaction): '||chr(10)||
  47. case when query is null then 'NULL' else query::text end,  
  48. chr(10)||'--------'||chr(10)  
  49. order by  
  50. ( case mode  
  51.   when 'INVALID' then 0  
  52.   when 'AccessShareLock' then 1  
  53.   when 'RowShareLock' then 2  
  54.   when 'RowExclusiveLock' then 3  
  55.   when 'ShareUpdateExclusiveLock' then 4  
  56.   when 'ShareLock' then 5  
  57.   when 'ShareRowExclusiveLock' then 6  
  58.   when 'ExclusiveLock' then 7  
  59.   when 'AccessExclusiveLock' then 8  
  60.   else 0  
  61. end ) desc,  
  62. (case when granted then 0 else 1 end)
  63. ) as lock_conflict
  64. from t_unionall  
  65. group by  
  66. locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ;
复制代码
以上为个人经验,希望能给各人一个参考,也希望各人多多支持脚本之家。如有错误或未考虑完全的地方,望不吝见教。

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作