PostgreSQL鎖等待監控珍藏級SQL-誰堵塞了誰

德哥發表於2017-05-21

標籤

PostgreSQL , pg_locks , pg_stat_activity , 鎖監控 , 誰堵塞了誰


背景

在資料庫中,通過鎖以及多版本併發控制可以保護資料的一致性,例如A正在查詢資料,B就無法對A訪問的物件執行DDL。A正在更新某條記錄,B就不能刪除或更新這條記錄。

鎖是資料庫自動管理的,同時資料庫還提供了AD LOCK或者LOCK語法,允許使用者自己控制鎖。

例如AD lock的應用可以參考如下:

《PostgreSQL 使用advisory lock實現行級讀寫堵塞》

《PostgreSQL 無縫自增ID的實現 – by advisory lock》

《PostgreSQL 使用advisory lock或skip locked消除行鎖衝突, 提高几十倍併發更新效率》

《聊一聊雙十一背後的技術 – 不一樣的秒殺技術, 裸秒》

當然,如果應用程式邏輯設計不慎,就可能導致嚴重的鎖等待,或者死鎖的產生。

如果你發現SQL請求大多數時候處於等待鎖的狀態,那麼可能出現了業務邏輯的問題。

如何檢查或監控鎖等待呢?

PostgreSQL提供了兩個檢視

1. pg_locks展示鎖資訊,每一個被鎖或者等待鎖的物件一條記錄。

2. pg_stat_activity,每個會話一條記錄,顯示會話狀態資訊。

我們通過這兩個檢視可以檢視鎖,鎖等待情況。同時可以瞭解發生鎖衝突的情況。

pg_stat_activity.query反映的是當前正在執行或請求的SQL,而同一個事務中以前已經執行的SQL不能在pg_stat_activity中顯示出來。

所以如果你發現兩個會話發生了衝突,但是他們的pg_stat_activity.query沒有衝突的話,那就有可能是他們之間的某個事務之前的SQL獲取的鎖與另一個事務當前請求的QUERY發生了鎖衝突。

如果追蹤詳細的鎖衝突資訊:

1. 可以通過lock trace跟蹤鎖等待的詳細資訊,

《PostgreSQL Developer Options (debug, trace, system table mod and so on…) 詳解》

2. 通過資料庫日誌(開啟lock_timeout, log_lockwait引數)(csvlog)跟蹤鎖等待資訊,

3. 或者通過資料庫日誌(開啟log_statements=`all`,SQL審計)追蹤事務中所有的SQL (csvlog),分析事務之間的鎖衝突。

4. 通過SQL檢視持鎖,等鎖的事務狀態。

鎖的釋放時機:

大多數鎖要等待事務結束後釋放,某些輕量級鎖(資料庫自動控制)是隨用隨釋放的。

檢視當前事務鎖等待、持鎖資訊的SQL

這條SQL非常有用,建議DBA珍藏。

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 ;  

例子

postgres=# create table locktest(id int primary key, info text);  
CREATE TABLE  
postgres=# insert into locktest values (1,`a`);  
INSERT 0 1  

會話A

postgres=# begin;  
BEGIN  
postgres=# update locktest set info=`a` where id=1;  
UPDATE 1  
postgres=# select * from locktest ;  
 id | info   
----+------  
  1 | a  
(1 row)  

會話B

postgres=# begin;  
BEGIN  
postgres=# select * from locktest ;  
 id | info   
----+------  
  1 | a  
(1 row)  

會話C

postgres=# begin;  
BEGIN  
postgres=# insert into locktest values (2,`test`);  
INSERT 0 1  

會話D

postgres=# begin;  
BEGIN  
postgres=# truncate locktest ;  
  
waiting......  

會話E

postgres=# select * from locktest ;  
  
waiting......  

會話F

postgres=#   x  
Expanded display is on.  
postgres=# select * from v_locks_monitor ;  
-[ RECORD 1 ]-+------------------------------------------------------------------------------------------------------------------------------------------------------  
locktype      | relation  
datname       | postgres  
relation      | locktest  
page          |   
tuple         |   
virtualxid    |   
transactionid |   
classid       |   
objid         |   
objsubid      |   
string_agg    | Pid: 23043                                                                                                                                           +  
              | Granted: false , Mode: AccessExclusiveLock , FastPath: false , VirtualTransaction: 4/1450064 , Session_State: active                                 +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:43:43.735829+08 , Query_Start: 2017-05-21 21:43:50.965797+08 , Xact_Elapse: 00:01:11.919991 , Query_Elapse: 00:01:04.690023+  
              | Query: truncate locktest ;                                                                                                                           +  
              | --------                                                                                                                                             +  
              | Pid: 40698                                                                                                                                           +  
              | Granted: true , Mode: RowExclusiveLock , FastPath: false , VirtualTransaction: 6/1031925 , Session_State: idle in transaction                        +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:43:15.173798+08 , Query_Start: 2017-05-21 21:43:24.338804+08 , Xact_Elapse: 00:01:40.482022 , Query_Elapse: 00:01:31.317016+  
              | Query: insert into locktest values (2,`test`);                                                                                                       +  
              | --------                                                                                                                                             +  
              | Pid: 17515                                                                                                                                           +  
              | Granted: true , Mode: RowExclusiveLock , FastPath: false , VirtualTransaction: 3/5671759 , Session_State: idle in transaction                        +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:42:19.199124+08 , Query_Start: 2017-05-21 21:42:47.820125+08 , Xact_Elapse: 00:02:36.456696 , Query_Elapse: 00:02:07.835695+  
              | Query: select * from locktest ;                                                                                                                      +  
              | --------                                                                                                                                             +  
              | Pid: 17515                                                                                                                                           +  
              | Granted: true , Mode: RowExclusiveLock , FastPath: false , VirtualTransaction: 3/5671759 , Session_State: idle in transaction                        +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:42:19.199124+08 , Query_Start: 2017-05-21 21:42:47.820125+08 , Xact_Elapse: 00:02:36.456696 , Query_Elapse: 00:02:07.835695+  
              | Query: select * from locktest ;                                                                                                                      +  
              | --------                                                                                                                                             +  
              | Pid: 40698                                                                                                                                           +  
              | Granted: true , Mode: RowExclusiveLock , FastPath: false , VirtualTransaction: 6/1031925 , Session_State: idle in transaction                        +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:43:15.173798+08 , Query_Start: 2017-05-21 21:43:24.338804+08 , Xact_Elapse: 00:01:40.482022 , Query_Elapse: 00:01:31.317016+  
              | Query: insert into locktest values (2,`test`);                                                                                                       +  
              | --------                                                                                                                                             +  
              | Pid: 40199                                                                                                                                           +  
              | Granted: true , Mode: AccessShareLock , FastPath: false , VirtualTransaction: 5/1029276 , Session_State: idle in transaction                         +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:43:01.745129+08 , Query_Start: 2017-05-21 21:43:05.928125+08 , Xact_Elapse: 00:01:53.910691 , Query_Elapse: 00:01:49.727695+  
              | Query: select * from locktest ;                                                                                                                      +  
              | --------                                                                                                                                             +  
              | Pid: 17515                                                                                                                                           +  
              | Granted: true , Mode: AccessShareLock , FastPath: false , VirtualTransaction: 3/5671759 , Session_State: idle in transaction                         +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:42:19.199124+08 , Query_Start: 2017-05-21 21:42:47.820125+08 , Xact_Elapse: 00:02:36.456696 , Query_Elapse: 00:02:07.835695+  
              | Query: select * from locktest ;                                                                                                                      +  
              | --------                                                                                                                                             +  
              | Pid: 40199                                                                                                                                           +  
              | Granted: true , Mode: AccessShareLock , FastPath: false , VirtualTransaction: 5/1029276 , Session_State: idle in transaction                         +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:43:01.745129+08 , Query_Start: 2017-05-21 21:43:05.928125+08 , Xact_Elapse: 00:01:53.910691 , Query_Elapse: 00:01:49.727695+  
              | Query: select * from locktest ;                                                                                                                      +  
              | --------                                                                                                                                             +  
              | Pid: 17515                                                                                                                                           +  
              | Granted: true , Mode: AccessShareLock , FastPath: false , VirtualTransaction: 3/5671759 , Session_State: idle in transaction                         +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:42:19.199124+08 , Query_Start: 2017-05-21 21:42:47.820125+08 , Xact_Elapse: 00:02:36.456696 , Query_Elapse: 00:02:07.835695+  
              | Query: select * from locktest ;                                                                                                                      +  
              | --------                                                                                                                                             +  
              | Pid: 24781                                                                                                                                           +  
              | Granted: false , Mode: AccessShareLock , FastPath: false , VirtualTransaction: 7/1025270 , Session_State: active                                     +  
              | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +  
              | Xact_Start: 2017-05-21 21:44:20.725834+08 , Query_Start: 2017-05-21 21:44:20.725834+08 , Xact_Elapse: 00:00:34.929986 , Query_Elapse: 00:00:34.929986+  
              | Query: select * from locktest ;  

處理方法

1. 前面的鎖查詢SQL,已經清晰的顯示了每一個發生了鎖等待的物件,按鎖的大小排序,要快速解出這種狀態,terminate最大的鎖對應的PID即可。

postgres=#   select pg_terminate_backend(23043);  
-[ RECORD 1 ]--------+--  
pg_terminate_backend | t  

會話D

FATAL:  terminating connection due to administrator command  
server closed the connection unexpectedly  
        This probably means the server terminated abnormally  
        before or while processing the request.  
The connection to the server was lost. Attempting reset: Succeeded.  

幹掉23043後,大家都清淨了

postgres=# select * from v_locks_monitor ;  
(0 rows)  

Greenplum

如果是Greenplum,由於版本問題,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.transactionid,a.mppsessionid,a.mppiswriter,a.gp_segment_id,     
  b.procpid,b.sess_id,b.waiting_reason,b.current_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.mppsessionid=b.sess_id 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.transactionid,a.mppsessionid,a.mppiswriter,a.gp_segment_id,     
  b.procpid,b.sess_id,b.waiting_reason,b.current_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.mppsessionid=b.sess_id 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.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.mppsessionid <> w.mppsessionid   
  )    
),    
t_unionall as    
(    
  select r.* from t_overlap r    
  union all    
  select w.* from t_wait w    
)    
select locktype,datname,relation::regclass,page,tuple,textin(xidout(transactionid)),classid::regclass,objid,objsubid,   
string_agg(   
`Gp_Segment_Id: `||case when gp_segment_id is null then `NULL` else gp_segment_id::text end||chr(10)|| 
`MppIsWriter: `||case when mppiswriter is null then `NULL` when mppiswriter is true then `TRUE` else `FALSE` end||chr(10)|| 
`MppSessionId: `||case when mppsessionid is null then `NULL` else mppsessionid::text end||chr(10)|| 
`ProcPid: `||case when procpid is null then `NULL` else procpid::text end||chr(10)|| 
`Pid: `||case when pid is null then `NULL` else pid::text end||chr(10)||   
`Lock_Granted: `||case when granted is null then `NULL` when granted is true then `TRUE` else `FALSE` end||` , Mode: `||case when mode is null then `NULL` else mode::text end||` , Waiting_Reason: `||case when waiting_reason is null then `NULL` else waiting_reason::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 current_query is null then `NULL` else current_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::regclass,page,tuple,textin(xidout(transactionid)),classid::regclass,objid,objsubid ;  

測試

-[ RECORD 1 ]-+------------------------------------------------------------------------------------------------------------------------------------------------------
locktype      | relation
datname       | postgres
relation      | locktest
page          | 
tuple         | 
textin        | 
classid       | 
objid         | 
objsubid      | 
lock_conflict | Gp_Segment_Id: -1
              | MppIsWriter: TRUE
              | MppSessionId: 46
              | ProcPid: 100310
              | Pid: 100310
              | Lock_Granted: TRUE , Mode: ExclusiveLock , Waiting_Reason: NULL
              | Username: dege.zzz , Database: postgres , Client_Addr: 127.0.0.1/32 , Client_Port: 51220 , Application_Name: psql
              | Xact_Start: 2017-05-22 14:59:50.067908+08 , Query_Start: 2017-05-22 15:00:01.568904+08 , Xact_Elapse: 00:00:37.858031 , Query_Elapse: 00:00:26.357035
              | SQL (Current SQL in Transaction): 
              | <IDLE> in transaction
              | --------
              | Gp_Segment_Id: -1
              | MppIsWriter: TRUE
              | MppSessionId: 47
              | ProcPid: 112053
              | Pid: 112053
              | Lock_Granted: FALSE , Mode: ExclusiveLock , Waiting_Reason: lock
              | Username: dege.zzz , Database: postgres , Client_Addr: 127.0.0.1/32 , Client_Port: 51518 , Application_Name: psql
              | Xact_Start: 2017-05-22 15:00:06.994012+08 , Query_Start: 2017-05-22 15:00:19.6+08 , Xact_Elapse: 00:00:20.931927 , Query_Elapse: 00:00:08.325939
              | SQL (Current SQL in Transaction): 
              | update locktest set info=`b` where id=2;
              | --------
              | Gp_Segment_Id: 0
              | MppIsWriter: TRUE
              | MppSessionId: 46
              | ProcPid: 100310, master的pid
              | Pid: 111641, segment的pid
              | Lock_Granted: TRUE , Mode: RowExclusiveLock , Waiting_Reason: NULL
              | Username: dege.zzz , Database: postgres , Client_Addr: 127.0.0.1/32 , Client_Port: 51220 , Application_Name: psql
              | Xact_Start: 2017-05-22 14:59:50.067908+08 , Query_Start: 2017-05-22 15:00:01.568904+08 , Xact_Elapse: 00:00:37.858031 , Query_Elapse: 00:00:26.357035
              | SQL (Current SQL in Transaction): 
              | <IDLE> in transaction

關注gp_segment_id=-1的,長時間等待,殺掉procpid即可。

postgres=# select pg_terminate_backend(100310);
-[ RECORD 1 ]--------+--
pg_terminate_backend | t

參考

https://www.postgresql.org/docs/9.6/static/view-pg-locks.html

https://www.postgresql.org/docs/9.6/static/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW

https://www.postgresql.org/docs/9.6/static/mvcc.html

《PostgreSQL Developer Options (debug, trace, system table mod and so on…) 詳解》

《PostgreSQL 使用advisory lock實現行級讀寫堵塞》

《PostgreSQL 無縫自增ID的實現 – by advisory lock》

《PostgreSQL 使用advisory lock或skip locked消除行鎖衝突, 提高几十倍併發更新效率》

《聊一聊雙十一背後的技術 – 不一樣的秒殺技術, 裸秒》

《Compare PostgreSQL and Oracle dead lock detect and transaction》

《PostgreSQL lock waiting order》

《PostgreSQL row lock and htup.t_infomask thinking》


相關文章