PostgreSQL查看等待锁的SQL和进程
查看等待锁的查询和进程:
The following query may be helpful to see what processes are blocking SQL statements (these only find row-level locks, not object-level locks).
SELECT blocked_locks.pid AS blocked_pid,
blocked_activity.usename AS blocked_user,
blocking_locks.pid AS blocking_pid,
blocking_activity.usename AS blocking_user,
blocked_activity.query AS blocked_statement,
blocking_activity.query AS current_statement_in_blocking_process
FROM pg_catalog.pg_locks blocked_locks
JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid
JOIN pg_catalog.pg_locks blocking_locks
ON blocking_locks.locktype = blocked_locks.locktype
AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE
AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
AND blocking_locks.pid != blocked_locks.pid JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
WHERE NOT blocked_locks.GRANTED;
简化版:
select w1.pid as waiting_pid,
w2.usename as waiting_user,
w2.query as waiting_statement,
b1.pid as blocking_pid,
b2.usename as blocking_user,
b2.query as blocking_statement
from pg_locks w1
join pg_stat_activity w2 on w1.pid=w2.pid
join pg_locks b1 on w1.transactionid=b1.transactonid and w1.pid!=b1.pid
join pg_stat_activity b2 on b1.pid=b2.pid
where not w1.granted;
在上面的SQL中,可能会疑问,为什么要w1.pid != b1.pid,事务ID相同,但PID不同,怎么理解呢:
官方文档说明,https://www.postgresql.org/docs/current/view-pg-locks.html,PID表示持有这把锁的或者等待这把锁的进程,这就解释了 w1.pid!=b1.pid ,表示筛选出持有的锁进程和等待锁的进程。但w1.transactionid=b1.transactonid怎么理解?怎么可能一个事务中对应两个进程?不应该是同一个锁吗?
那么再来理一理,首先无法确定同一个锁。这里的transaction表示作用在改锁上的事务ID,这么理解:
1)进程100,事务ID为111111,正在修改一行数据,持有了RowExclusiveLock,那么在pg_locks中那条记录的transactionid就是111111;
2)而进程101,事务ID为111119,也在修改这行数据,而事务111111没有提交,于是在等待这把锁,那么在pg_locks中记录的transactionid也是111111,而不是111119!
因此可以通过这个事务ID相等,且PID不等于持有事务ID的PID进程,就是正在等待的进程,进而通过PID相等找到pg_stat_activity中的query。而属于事务ID所属的PID则是正在持有锁的进程,对应可以找到正在执行的SQL。
Table 52.74. pg_locks Columns Name Type References Description
locktype text Type of the lockable object: relation, extend, page, tuple, transactionid, virtualxid, object, userlock, or advisory
database oid pg_database.oid OID of the database in which the lock target exists, or zero if the target is a shared object, or null if the target is a transaction ID
relation oid pg_class.oid OID of the relation targeted by the lock, or null if the target is not a relation or part of a relation
page integer Page number targeted by the lock within the relation, or null if the target is not a relation page or tuple
tuple smallint Tuple number targeted by the lock within the page, or null if the target is not a tuple
virtualxid text Virtual ID of the transaction targeted by the lock, or null if the target is not a virtual transaction ID
transactionid xid ID of the transaction targeted by the lock, or null if the target is not a transaction ID
classid oid pg_class.oid OID of the system catalog containing the lock target, or null if the target is not a general database object
objid oid any OID column OID of the lock target within its system catalog, or null if the target is not a general database object
objsubid smallint Column number targeted by the lock (the classid and objid refer to the table itself), or zero if the target is some other general database object, or null if the target is not a general database object
virtualtransaction text Virtual ID of the transaction that is holding or awaiting this lock
pid integer Process ID of the server process holding or awaiting this lock, or null if the lock is held by a prepared transaction
mode text Name of the lock mode held or desired by this process (see Section 13.3.1 and Section 13.2.3)
granted boolean True if lock is held, false if lock is awaited
fastpath boolean True if lock was taken via fast path, false if taken via main lock table
另外,可以使用来看看当前进程被哪些进程给锁住:pg_blocking_pids
pg_blocking_pids returns an array of the process IDs of the sessions that are blocking the server process with the specified process ID, or an empty array if there is no such server process or it is not blocked.
One server process blocks another if it either holds a lock that conflicts with the blocked process's lock request (hard block), or is waiting for a lock that would conflict with the blocked process's lock request and is ahead of it in the wait queue (soft block). When using parallel queries the result always lists client-visible process IDs (that is, pg_backend_pid results) even if the actual lock is held or awaited by a child worker process. As a result of that, there may be duplicated PIDs in the result. Also note that when a prepared transaction holds a conflicting lock, it will be represented by a zero process ID in the result of this function. Frequent calls to this function could have some impact on database performance, because it needs exclusive access to the lock manager's shared state for a short time.
PostgreSQL查看等待锁的SQL和进程的更多相关文章
- mysql 查询正在执行的事务以及等待锁 常用的sql语句
使用navicat测试学习: 首先使用set autocommit = 0;(取消自动提交,则当执行语句commit或者rollback执行提交事务或者回滚) 在打开一个执行update查询 正在 ...
- mysql查看被锁住的表
转: mysql查看被锁住的表 2019年05月14日 11:58:59 hlvy 阅读数 1068更多 分类专栏: mysql mysql 转:https://blog.51cto.com/mo ...
- Mysql 查看被锁住的表
MYSQL 查看被锁住的表 -- 本文章仅用于学习,记录 当你在mysql 执行查询语句的时候,简单的一句查询语句却卡很久,一直转圈圈的时候,这时候你就需要怀疑数据库的哪些进程,哪些事物被锁住 ...
- 知方可补不足~sqlserver中使用sp_who查看sql的进程
回到目录 在SQLSERVER中每个会话,即每个查询分析器窗口都会产生一个SQL进程,对于那些持续时间短的进程,它们转瞬即失,而对于持续时间比较长的,我们需要希望查看它的运行状态,就可以借助SQL提供 ...
- Postgresql 查看锁的过程
一.查看sql语句是否发生死锁 1.查看数据库的进程.SELECT * FROM pg_stat_activity WHERE datname='死锁的数据库ID ';检索出来的字段中,[wating ...
- sql server 查看锁表SQL【转】
1.select * from sys.dm_tran_locks或sp_LOCK 查看request_node 字段中为'X'(排他锁)或'IX'(意向排他锁)2.用sp_who2 + pid(进程 ...
- mysql查看被锁住的表,正在进行的进程,已经杀掉进程的方法
mysql查看被锁的进程 //查看所有进程show processlist; //查询是否锁表show OPEN TABLES where In_use > 0; //查看被锁住的 SELECT ...
- sqlserver(查看被锁进程)
-- ###### 查看被锁进程 ###### select 标志, 进程ID=spid,线程ID=kpid,块进程ID=blocked,数据库ID=dbid, 数据库名=db_name(dbid), ...
- sqlserver 出现sql被锁时,查看加锁和被锁的sql
原文:sqlserver 出现sql被锁时,查看加锁和被锁的sql DECLARE @spid INT DECLARE @blk INT DECLARE @count INT DECLARE @ind ...
随机推荐
- js拖拽文件夹上传
由于项目需要上传文件到服务器,于是便在文件上传的基础上增加了拖拽上传.拖拽上传当然属于文件上传的一部分,只不过在文件上传的基础上增加了拖拽的界面,主要在于前台的交互, 从拖拽的文件中获取文件列表然后调 ...
- codevs 3022 西天收费站 x
题目描述 Description 唐僧师徒四人终于发现西天就在眼前,但猴子突然发现前面有n个收费站(如来佛太可恶),在每个收费站用不同的方式要交的钱不同,输入 ...
- [科普] CPU, GPU, TPU的区别
Google Cloud 原文链接:https://cloud.google.com/blog/products/ai-machine-learning/what-makes-tpus-fine-tu ...
- [题解] [TJOI2011] 构造矩阵
题面 题解 很容易看出来是道网络流的题目, 要是没有这个字典序最小, 直接建图跑一遍就好了, 考虑如何输出字典序最小的方案 我们可以贪心地去选择, 若当前点可以选0就选0, 不能选0就选1, 有一点像 ...
- 关于java中对BigDecimal加减乘除的基本用法
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位有效数. 在实际应用中,需要对更大或者更小的数进 ...
- jmeter参数化之 CSV data set config
第一步:测试计划右键--创建线程组 第二步:选择线程组:右键--sample---创建:http request 配置协议类型和服务名称,method 和path 第三步:选择线程组下的http ...
- 20175215 2018-2019-2 第九周java课程学习总结
第十一章 JDBC与MySQL数据库 11.1 MySQL数据库管理系统 下载安装过程略 使用的是MySQL 5.6而非5.7 11.2 启动MySQL数据库服务器 启动和root用户过程略 11.3 ...
- Springdata-Jpa学习笔记
Respository接口 Respository是Springdata JPA中的顶层接口,提供了两种查询方法: 1)基于方法名称命名规则 2)基于@Qeury注解查询 1. 方法名称命名规则查询 ...
- ELK- elasticsearch 讲解,安装,插件head,bigdesk ,kopf,cerebro(kopf升级版)安装
ElasticSearch:简称es ,分布式全文搜索引擎,使用java语言开发,面向文档型数据库,一条数据就是一个文档,数据用json序列化后存储. 默认端口:9200 借助redis来理解 red ...
- P4138 [JOISC2014]挂饰
P4138 [JOISC2014]挂饰 ◦ N个装在手机上的挂饰.挂饰附有可以挂其他挂件的挂钩.每个挂件要么直接挂在手机上,要么挂在其他挂件的挂钩上.直接挂在手机上的挂件最多有1个. ...