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=# 

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

相关推荐
Youkiup几秒前
【linux 常用命令】
linux·运维·服务器
qq_297504614 分钟前
【解决】Linux更新系统内核后Nvidia-smi has failed...
linux·运维·服务器
_oP_i9 分钟前
.NET Core 项目配置到 Jenkins
运维·jenkins·.netcore
风_流沙11 分钟前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch
weixin_4373982117 分钟前
Linux扩展——shell编程
linux·运维·服务器·bash
亽仒凣凣19 分钟前
Windows安装Redis图文教程
数据库·windows·redis
小燚~19 分钟前
ubuntu开机进入initramfs状态
linux·运维·ubuntu
小林熬夜学编程26 分钟前
【Linux网络编程】第十四弹---构建功能丰富的HTTP服务器:从状态码处理到服务函数扩展
linux·运维·服务器·c语言·网络·c++·http
亦世凡华、28 分钟前
MySQL--》如何在MySQL中打造高效优化索引
数据库·经验分享·mysql·索引·性能分析
YashanDB30 分钟前
【YashanDB知识库】Mybatis-Plus调用YashanDB怎么设置分页
数据库·yashandb·崖山数据库