PostgreSQL查看当前锁信息

PostgreSQL查看当前锁信息

bash 复制代码
基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

查看当前锁信息的sql

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

行级共享锁

sql 复制代码
white=# BEGIN;
BEGIN
white=*# SELECT * FROM yewu1.t1 WHERE id = 1 FOR SHARE;
 id 
----
  1
(1 row)
sql 复制代码
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=# 

示例2

行级排他锁

bash 复制代码
white=# BEGIN;
BEGIN
white=*# SELECT * FROM yewu1.t1 WHERE id = 1 FOR UPDATE;
 id 
----
  1
(1 row)
sql 复制代码
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=# 

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

相关推荐
pk_xz12345620 分钟前
Shell 脚本中变量和字符串的入门介绍
linux·运维·服务器
小珑也要变强23 分钟前
Linux之sed命令详解
linux·运维·服务器
瓜牛_gn24 分钟前
mysql特性
数据库·mysql
奶糖趣多多1 小时前
Redis知识点
数据库·redis·缓存
Lary_Rock2 小时前
RK3576 LINUX RKNN SDK 测试
linux·运维·服务器
CoderIsArt2 小时前
Redis的三种模式:主从模式,哨兵与集群模式
数据库·redis·缓存
师太,答应老衲吧4 小时前
SQL实战训练之,力扣:2020. 无流量的帐户数(递归)
数据库·sql·leetcode
Channing Lewis5 小时前
salesforce case可以新建一个roll up 字段,统计出这个case下的email数量吗
数据库·salesforce
一坨阿亮6 小时前
Linux 使用中的问题
linux·运维
毕业设计制作和分享7 小时前
ssm《数据库系统原理》课程平台的设计与实现+vue
前端·数据库·vue.js·oracle·mybatis