PG 锁冲突案例

背景

用户反馈环境中 update 语句无法正常执行, 执行会卡住

定位分析

采用德哥经典锁冲突 SQL ,查看了下, 结果如下

Pid: 39521                                                                                                                                              
Lock_Granted: true , Mode: ExclusiveLock , FastPath: false , VirtualTransaction: 21/273 , Session_State: idle in transaction                            
Username: mtpoccphopr , Database: testdb , Client_Addr: 192.168.0.100/32 , Client_Port: 55979 , Application_Name: PostgreSQL JDBC Driver              
Xact_Start: 2023-08-31 18:09:42.404895+08 , Query_Start: 2023-08-31 18:29:29.616356+08 , Xact_Elapse: 00:29:53.317839 , Query_Elapse: 00:10:06.106378   
SQL (Current SQL in Transaction):                                                                                                                       
select * from testa where id=1;
--------                                                                                                                                                
Pid: 90924                                                                                                                                              
Lock_Granted: false , Mode: ShareLock , FastPath: false , VirtualTransaction: 24/29 , Session_State: active                                             
Username: pub_test , Database: testdb , Client_Addr: 192.168.0.100/32 , Client_Port: 33246 , Application_Name: DBeaver 7.3.1 - SQLEditor <Script-29.sq
Xact_Start: 2023-08-31 18:38:39.098571+08 , Query_Start: 2023-08-31 18:38:39.098856+08 , Xact_Elapse: 00:00:56.624163 , Query_Elapse: 00:00:56.623878   
SQL (Current SQL in Transaction):                                                                                                                       
update testb set name='xxx' where id=1;

可以看到 Application Name : 为 PostgreSQL JDBC Driver , 说明是应用层的连接, 而查看 当前 SQL 是一个 select 语句, 与 update 语句不会冲突, 这个事务状态处于"idle in transaction"状态, 所以推测是这个没有及时提交的事务代码块中存在 某个 SQL 与
后面 被堵塞的 update 语句冲突, 只不过当前事务最后运行的是一个 select 查询语句, 但是没有及时提交

解决方法:

select pg_terminate_backend(39521);

德哥经典锁冲突 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 ;  

你可能感兴趣的:(postgresql)