当前位置:   article > 正文

PostgreSQL查看当前锁信息

PostgreSQL查看当前锁信息

PostgreSQL查看当前锁信息

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

查看当前锁信息的sql

SELECT 
    pg_stat_activity.datname,
    pg_locks.pid,
    pg_class.relname,
    pg_locks.transactionid,
    pg_locks.granted,
    pg_locks.mode,
    pg_stat_activity.query as query_snippet,
    age(now(), pg_stat_activity.query_start) as age 
FROM 
    pg_stat_activity, pg_locks LEFT OUTER JOIN pg_class 
ON 
    (pg_locks.relation = pg_class.oid) 
WHERE 
    pg_stat_activity.pid = pg_locks.pid
    AND pg_stat_activity.pid <> pg_backend_pid() 
ORDER BY 
    query_start;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

示例1

行级共享锁

white=# BEGIN;
BEGIN
white=*# SELECT * FROM yewu1.t1 WHERE id = 1 FOR SHARE;
 id 
----
  1
(1 row)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
postgres=# SELECT 
postgres-#     pg_stat_activity.datname,
postgres-#     pg_locks.pid,
postgres-#     pg_class.relname,
postgres-#     pg_locks.transactionid,
postgres-#     pg_locks.granted,
postgres-#     pg_locks.mode,
postgres-#     pg_stat_activity.query as query_snippet,
postgres-#     age(now(), pg_stat_activity.query_start) as age 
postgres-# FROM 
postgres-#     pg_stat_activity, pg_locks LEFT OUTER JOIN pg_class 
postgres-# ON 
postgres-#     (pg_locks.relation = pg_class.oid) 
postgres-# WHERE 
postgres-#     pg_stat_activity.pid = pg_locks.pid
postgres-#     AND pg_stat_activity.pid <> pg_backend_pid() 
postgres-# ORDER BY 
postgres-#     query_start;
 datname | pid  | relname | transactionid | granted |     mode      |                 query_snippet                  |       age       
---------+------+---------+---------------+---------+---------------+------------------------------------------------+-----------------
 white   | 6320 |         |               | t       | RowShareLock  | SELECT * FROM yewu1.t1 WHERE id = 1 FOR SHARE; | 00:00:15.839986
 white   | 6320 |         |               | t       | ExclusiveLock | SELECT * FROM yewu1.t1 WHERE id = 1 FOR SHARE; | 00:00:15.839986
 white   | 6320 |         |        268808 | t       | ExclusiveLock | SELECT * FROM yewu1.t1 WHERE id = 1 FOR SHARE; | 00:00:15.839986
(3 rows)

postgres=# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

示例2

行级排他锁

white=# BEGIN;
BEGIN
white=*# SELECT * FROM yewu1.t1 WHERE id = 1 FOR UPDATE;
 id 
----
  1
(1 row)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
postgres=# SELECT 
postgres-#     pg_stat_activity.datname,
postgres-#     pg_locks.pid,
postgres-#     pg_class.relname,
postgres-#     pg_locks.transactionid,
postgres-#     pg_locks.granted,
postgres-#     pg_locks.mode,
postgres-#     pg_stat_activity.query as query_snippet,
postgres-#     age(now(), pg_stat_activity.query_start) as age 
postgres-# FROM 
postgres-#     pg_stat_activity, pg_locks LEFT OUTER JOIN pg_class 
postgres-# ON 
postgres-#     (pg_locks.relation = pg_class.oid) 
postgres-# WHERE 
postgres-#     pg_stat_activity.pid = pg_locks.pid
postgres-#     AND pg_stat_activity.pid <> pg_backend_pid() 
postgres-# ORDER BY 
postgres-#     query_start;
 datname | pid  | relname | transactionid | granted |     mode      |                  query_snippet                  |       age       
---------+------+---------+---------------+---------+---------------+-------------------------------------------------+-----------------
 white   | 6320 |         |               | t       | RowShareLock  | SELECT * FROM yewu1.t1 WHERE id = 1 FOR UPDATE; | 00:00:19.223913
 white   | 6320 |         |               | t       | ExclusiveLock | SELECT * FROM yewu1.t1 WHERE id = 1 FOR UPDATE; | 00:00:19.223913
 white   | 6320 |         |        268809 | t       | ExclusiveLock | SELECT * FROM yewu1.t1 WHERE id = 1 FOR UPDATE; | 00:00:19.223913
(3 rows)

postgres=# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

谨记:心存敬畏,行有所止。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/697707
推荐阅读
相关标签
  

闽ICP备14008679号