锁监控
来自 PostgreSQL 维基
跳转到导航跳转到搜索在线查看当前锁
pg_locks 视图
查看 pg_locks 将显示授予了哪些锁以及哪些进程正在等待获取锁。这是一个开始查找锁问题的好查询。
select relation::regclass, * from pg_locks where not granted;
pg_stat_activity 视图
- 如果您交叉引用 pg_stat_activity 中的信息,则更容易弄清楚哪些进程持有或正在等待锁。
被阻塞和阻塞活动的组合
以下查询可能有助于查看哪些进程正在阻塞 SQL 语句(这些查询仅查找行级锁,而不是对象级锁)。
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;
以下是该数据的另一种视图,其中包括 application_name
在每个事务开始之前设置 application_name 变量,可以使您了解哪些逻辑进程阻塞了另一个进程。它可以是开始事务的源代码行信息,或任何其他有助于您将 application_name 与代码匹配的信息。
SET application_name='%your_logical_name%';
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,
blocked_activity.application_name AS blocked_application,
blocking_activity.application_name AS blocking_application
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;
注意:虽然此查询在大多数情况下都能正常工作,但它仍然存在一些正确性问题 [1],特别是在 9.6 版本上。
以下是该数据的另一种视图,其中包括状态持续时间的信息
SELECT a.datname,
l.relation::regclass,
l.transactionid,
l.mode,
l.GRANTED,
a.usename,
a.query,
a.query_start,
age(now(), a.query_start) AS "age",
a.pid
FROM pg_stat_activity a
JOIN pg_locks l ON l.pid = a.pid
ORDER BY a.query_start;
对于低于 9.0 版本的 PostgreSQL
select a.datname,
c.relname,
l.transactionid,
l.mode,
l.granted,
a.usename,
a.current_query,
a.query_start,
age(now(), a.query_start) as "age",
a.procpid
from pg_stat_activity a
join pg_locks l on l.pid = a.procpid
join pg_class c on c.oid = l.relation
order by a.query_start;
日志记录以供日后分析
- 如果您怀疑间歇性锁偶尔会导致问题,但无法在这些实时视图中捕获它们,则设置 log_lock_waits 和相关的 deadlock_timeout 参数会有所帮助。然后,缓慢的锁获取将在数据库日志中显示,以便日后分析。