我的编程空间,编程开发者的网络收藏夹
学习永远不晚

postgresql lock 锁等待查看

短信预约 信息系统项目管理师 报名、考试、查分时间动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

postgresql lock 锁等待查看

postgresql lock 锁等待查看

postgresql lock 锁等待查看

当SQL请求锁等待超过deadlock_timeout指定的时间时,报类似如下日志:

LOG: process xxx1 acquired RowExclusiveLock on relation xxx2 of database xxx3 after xxx4 ms at xxx
STATEMENT: INSERT ...........

解释: xxx1进程请求位于数据库xxx3中的xxx2对象的RowExclusiveLock锁,已等待xxx4秒。

如何检查或监控锁等待呢?

PostgreSQL提供了两个视图

    1. pg_locks展示锁信息,每一个被锁或者等待锁的对象一条记录。
    1. pg_stat_activity,每个会话一条记录,显示会话状态信息。 我们通过这两个视图可以查看锁,锁等待情况。同时可以了解发生锁冲突的情况。
  • 注意:pg_stat_activity.query反映的是当前正在执行或请求的SQL,而同一个事务中以前已经执行的SQL不能在pg_stat_activity中显示出来。所以如果你发现两个会话发生了冲突,但是他们的pg_stat_activity.query没有冲突的话,那就有可能是他们之间的某个事务之前的SQL获取的锁与另一个事务当前请求的QUERY发生了锁冲突。

通常锁的排查方法如下

    1. 开启审计日志log_statement = "all"
    1. psql 挂一个打印锁等待的窗口(sql语句参考如下)
    1. tail 挂一个日志观测窗口

查看锁等待sql

with
t_wait as
(
select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,
a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,
b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name
from pg_locks a,pg_stat_activity b where a.pid=b.pid and not a.granted
),
t_run as
(
select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,
a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,
b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name
from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted
),
t_overlap as
(
select r.* from t_wait w join t_run r on
(
r.locktype is not distinct from w.locktype and
r.database is not distinct from w.database and
r.relation is not distinct from w.relation and
r.page is not distinct from w.page and
r.tuple is not distinct from w.tuple and
r.virtualxid is not distinct from w.virtualxid and
r.transactionid is not distinct from w.transactionid and
r.classid is not distinct from w.classid and
r.objid is not distinct from w.objid and
r.objsubid is not distinct from w.objsubid and
r.pid <> w.pid
)
),
t_unionall as
(
select r.* from t_overlap r
union all
select w.* from t_wait w
)
select locktype,datname,relation::regclass,page,tuple,virtualxid,transactionid::text,classid::regclass,objid,objsubid,
string_agg(
"Pid: "||case when pid is null then "NULL" else pid::text end||chr(10)||
"Lock_Granted: "||case when granted is null then "NULL" else granted::text end||" , Mode: "||case when mode is null then "NULL" else mode::text end||" , FastPath: "||case when fastpath is null then "NULL" else fastpath::text end||" , VirtualTransaction: "||case when virtualtransaction is null then "NULL" else virtualtransaction::text end||" , Session_State: "||case when state is null then "NULL" else state::text end||chr(10)||
"Username: "||case when usename is null then "NULL" else usename::text end||" , Database: "||case when datname is null then "NULL" else datname::text end||" , Client_Addr: "||case when client_addr is null then "NULL" else client_addr::text end||" , Client_Port: "||case when client_port is null then "NULL" else client_port::text end||" , Application_Name: "||case when application_name is null then "NULL" else application_name::text end||chr(10)||
"Xact_Start: "||case when xact_start is null then "NULL" else xact_start::text end||" , Query_Start: "||case when query_start is null then "NULL" else query_start::text end||" , Xact_Elapse: "||case when (now()-xact_start) is null then "NULL" else (now()-xact_start)::text end||" , Query_Elapse: "||case when (now()-query_start) is null then "NULL" else (now()-query_start)::text end||chr(10)||
"SQL (Current SQL in Transaction): "||chr(10)||
case when query is null then "NULL" else query::text end,
chr(10)||"--------"||chr(10)
order by
( case mode
when "INVALID" then 0
when "AccessShareLock" then 1
when "RowShareLock" then 2
when "RowExclusiveLock" then 3
when "ShareUpdateExclusiveLock" then 4
when "ShareLock" then 5
when "ShareRowExclusiveLock" then 6
when "ExclusiveLock" then 7
when "AccessExclusiveLock" then 8
else 0
end ) desc,
(case when granted then 0 else 1 end)
) as lock_conflict
from t_unionall
group by
locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid;

#如果觉得写SQL麻烦,可以将它创建为视图

create view v_locks_monitor as
with
t_wait as
(
select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,
a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,
b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name
from pg_locks a,pg_stat_activity b where a.pid=b.pid and not a.granted
),
t_run as
(
select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,
a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,
b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name
from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted
),
t_overlap as
(
select r.* from t_wait w join t_run r on
(
r.locktype is not distinct from w.locktype and
r.database is not distinct from w.database and
r.relation is not distinct from w.relation and
r.page is not distinct from w.page and
r.tuple is not distinct from w.tuple and
r.virtualxid is not distinct from w.virtualxid and
r.transactionid is not distinct from w.transactionid and
r.classid is not distinct from w.classid and
r.objid is not distinct from w.objid and
r.objsubid is not distinct from w.objsubid and
r.pid <> w.pid
)
),
t_unionall as
(
select r.* from t_overlap r
union all
select w.* from t_wait w
)
select locktype,datname,relation::regclass,page,tuple,virtualxid,transactionid::text,classid::regclass,objid,objsubid,
string_agg(
"Pid: "||case when pid is null then "NULL" else pid::text end||chr(10)||
"Lock_Granted: "||case when granted is null then "NULL" else granted::text end||" , Mode: "||case when mode is null then "NULL" else mode::text end||" , FastPath: "||case when fastpath is null then "NULL" else fastpath::text end||" , VirtualTransaction: "||case when virtualtransaction is null then "NULL" else virtualtransaction::text end||" , Session_State: "||case when state is null then "NULL" else state::text end||chr(10)||
"Username: "||case when usename is null then "NULL" else usename::text end||" , Database: "||case when datname is null then "NULL" else datname::text end||" , Client_Addr: "||case when client_addr is null then "NULL" else client_addr::text end||" , Client_Port: "||case when client_port is null then "NULL" else client_port::text end||" , Application_Name: "||case when application_name is null then "NULL" else application_name::text end||chr(10)||
"Xact_Start: "||case when xact_start is null then "NULL" else xact_start::text end||" , Query_Start: "||case when query_start is null then "NULL" else query_start::text end||" , Xact_Elapse: "||case when (now()-xact_start) is null then "NULL" else (now()-xact_start)::text end||" , Query_Elapse: "||case when (now()-query_start) is null then "NULL" else (now()-query_start)::text end||chr(10)||
"SQL (Current SQL in Transaction): "||chr(10)||
case when query is null then "NULL" else query::text end,
chr(10)||"--------"||chr(10)
order by
( case mode
when "INVALID" then 0
when "AccessShareLock" then 1
when "RowShareLock" then 2
when "RowExclusiveLock" then 3
when "ShareUpdateExclusiveLock" then 4
when "ShareLock" then 5
when "ShareRowExclusiveLock" then 6
when "ExclusiveLock" then 7
when "AccessExclusiveLock" then 8
else 0
end ) desc,
(case when granted then 0 else 1 end)
) as lock_conflict
from t_unionall
group by
locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ;

#eg:

create table table_lock(id int primary key, info text);
insert into table_lock values (1,"a");
#session A
begin;
update table_lock set info="aa" where id=1;
select * from table_lock;
#session B
begin;
select * from table_lock;
#session C
begin;
insert into table_lock values (2,"b");
#session D
begin;
truncate table_lock;
waiting......
#or
#ALTER TABLE XXX RENAME TO XXXXX;
#session E
select * from table_lock;
waiting......

#eg:

Pid: 1980
Lock_Granted: false , Mode: AccessExclusiveLock , FastPath: false , VirtualTransaction: 9/4 , Session_State: active
Username: test , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql
Xact_Start: 2019-02-11 15:35:33.054468+08 , Query_Start: 2019-02-11 15:35:34.283192+08 , Xact_Elapse: 00:01:18.422846 , Query_Elapse: 00:01:17.194122
SQL (Current SQL in Transaction):
truncate table_lock;
--------
Pid: 1894
Lock_Granted: true , Mode: RowExclusiveLock , FastPath: false , VirtualTransaction: 5/128 , Session_State: idle in transaction
Username: test , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql
Xact_Start: 2019-02-11 15:17:48.342793+08 , Query_Start: 2019-02-11 15:17:48.344543+08 , Xact_Elapse: 00:19:03.134521 , Query_Elapse: 00:19:03.132771
SQL (Current SQL in Transaction):
insert into table_lock values (2,"b");
--------
    1. 前面的锁查询SQL,已经清晰的显示了每一个发生了锁等待的对象,Lock_Granted: true阻塞了Lock_Granted: false
    1. 同时按锁的大小排序,第一行的锁最大(Mode: AccessExclusiveLock级别最高)

处理方法

  • 确认会话状态 查看状态可用以下语句
select pid, state from pg_stat_activity;
# 查看当前会话的pid
highgo=# select pg_backend_pid();
#idle in transaction状态

or

如果他们想手动或定时杀idle的会话,可以用这个语句

SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE
pid <> pg_backend_pid()
AND state in ("idle", "idle in transaction", "idle in transaction (aborted)", "disabled")
AND state_change < current_timestamp - INTERVAL "15" MINUTE;
  • 注意会话的类型state in ("idle", "idle in transaction", "idle in transaction (aborted)", "disabled")

state text Current overall state of this backend. Possible values are: active: The backend is executing a query. idle: The backend is waiting for a new client command. idle in transaction: The backend is in a transaction, but is not currently executing a query. idle in transaction (aborted): This state is similar to idle in transaction, except one of the statements in the transaction caused an error. fastpath function call: The backend is executing a fast-path function. disabled: This state is reported if track_activities is disabled in this backend.

要快速解出这种状态,terminate最大的锁对应的PID即可。

select pg_terminate_backend(2066);
postgres=# select pg_terminate_backend(1980);
-[ RECORD 1 ]--------+--
pg_terminate_backend | t
  • 注意:此处不要使用操作系统命令kill -9,其会造成所有活动进程被终止,数据库重启。

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

postgresql lock 锁等待查看

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

postgresql lock 锁等待查看

postgresql lock 锁等待查看当SQL请求锁等待超过deadlock_timeout指定的时间时,报类似如下日志:LOG: process xxx1 acquired RowExclusiveLock on relation xxx2 of dat
postgresql lock 锁等待查看
2014-11-24

PostgreSQL中的死锁和锁等待

开始之前明确一下死锁和锁等待这两个事件的异同相同的之处:两者都是当前事物在试图请求被其他事物已经占用的锁,从而造成当前事物无法执行的现象不同的之处:死锁是相关session双方或者多方中必然要牺牲(回滚)至少一个事务,否则双方(或者多方)都无法执行;锁等待则不
PostgreSQL中的死锁和锁等待
2021-09-17

mysql死锁(dead lock)与锁等待(lock wait)的出现解决

目录死锁(dead lock)前台报错事后追查innodb statuserror.log锁等待(lock wait)1个参数innodb_lock_wait_timeout怎么处理级联锁或大量锁很多人都分不清死锁和锁等待的区别,也有不同I
mysql死锁(dead lock)与锁等待(lock wait)的出现解决
2024-09-09

MySQL8.0锁等待排查的实现

目录前言1. data_locks2. data_lock_waits3. sys.innodb_lock_waits4. 状态变量5. 状态变量 bug总结前言mysql 5.7 版本的时候锁等待排查用的元数据,主要存储在 inform
MySQL8.0锁等待排查的实现
2024-09-09

Mysql事物锁等待超时Lock wait timeout exceeded;怎么办

小编给大家分享一下Mysql事物锁等待超时Lock wait timeout exceeded;怎么办,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!问题场景问题出现环境:1、在同一事务内先后对同一条数据进行插入和更新操作;
2023-06-29

Lock wait timeout exceeded - 如何解决MySQL报错:锁等待超时

摘要:在使用MySQL数据库时,有时会遇到锁等待超时的问题。这个问题通常发生在多个事务同时尝试修改同一行数据时,其中一个事务会等待另一个事务的锁释放。本文将介绍如何解决MySQL报错中的锁等待超时问题,并提供具体的代码示例。一、什么是锁等待
2023-10-21

Lock wait timeout exceeded; try restarting transaction - 如何解决MySQL报错:锁等待超时,尝试重新启动事务

正文:MySQL作为一种常用的关系型数据库管理系统,被广泛应用于各种类型的应用程序。然而,在使用MySQL时,我们可能会遇到各种错误和异常。其中一个常见的错误是“Lock wait timeout exceeded; try restart
2023-10-21

编程热搜

目录