服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - PostgreSQL - Postgresql - 查看锁表信息的实现

Postgresql - 查看锁表信息的实现

2021-03-02 20:33|ChuckChen| PostgreSQL

这篇文章主要介绍了Postgresql 查看锁表信息的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

查看表锁信息,是DBA常用的脚本之一。

实验环境:

CentOS 7

PG 10.4

先通过A窗口执行

?
1
2
3
4
5
mytest=# begin;
BEGIN
mytest=# update t1 set col1 = 'a' where id =1 ;
UPDATE 1
mytest=#

打开B窗口执行

?
1
2
3
4
5
mytest=# begin;
BEGIN
mytest=# update t1 set col1 = 'b' where id =2;
UPDATE 1
mytest=# update t1 set col1 = 'b' where id =1;

等待了

说明只锁住了行,对于更新其他行没有影响。

再打开一个窗口查看信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
SELECT
a.datname,
locktype,
virtualtransaction,
transactionid,
nspname,
relname,
mode,
granted,
cast(date_trunc('second',query_start) AS timestamp) AS query_start
FROM
pg_locks
LEFT OUTER JOIN pg_class ON (pg_locks.relation = pg_class.oid)
LEFT OUTER JOIN pg_namespace ON (pg_namespace.oid = pg_class.relnamespace),
pg_stat_activity a
WHERE NOT pg_locks.pid = pg_backend_pid()
AND pg_locks.pid=a.pid;
datname | locktype | virtualtransaction | transactionid | nspname | relname | mode | granted | query_start
---------+---------------+--------------------+---------------+---------+---------+------------------+---------+---------------------
mytest | relation | 7/332 | | public | t1 | RowExclusiveLock | t | 2018-06-28 06:29:58
mytest | virtualxid | 7/332 | | | | ExclusiveLock | t | 2018-06-28 06:29:58
mytest | relation | 6/42 | | public | t1 | RowExclusiveLock | t | 2018-06-28 06:29:35
mytest | virtualxid | 6/42 | | | | ExclusiveLock | t | 2018-06-28 06:29:35
mytest | transactionid | 7/332 | 712 | | | ExclusiveLock | t | 2018-06-28 06:29:58
mytest | transactionid | 6/42 | 711 | | | ExclusiveLock | t | 2018-06-28 06:29:35
mytest | transactionid | 7/332 | 711 | | | ShareLock | f | 2018-06-28 06:29:58
mytest | tuple | 7/332 | | public | t1 | ExclusiveLock | t | 2018-06-28 06:29:58
(8 rows)

补充:如何查看PostgreSQL正在执行的SQL以及锁信息

查看当前正在运行的SQL

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
SELECT
procpid,
start,
now() - start AS lap,
current_query
FROM
(SELECT
backendid,
pg_stat_get_backend_pid(S.backendid) AS procpid,
pg_stat_get_backend_activity_start(S.backendid) AS start,
pg_stat_get_backend_activity(S.backendid) AS current_query
FROM
(SELECT pg_stat_get_backend_idset() AS backendid) AS S
) AS S
WHERE
current_query <> '<IDLE>'
ORDER BY
lap DESC;
procpid:进程id
start:进程开始时间
lap:经过时间
current_query:执行中的sql
怎样停止正在执行的sql
SELECT pg_cancel_backend(进程id);
或者用系统函数
kill -9 进程id;

查看数据库目前是否有锁

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- 查看当前事务锁等待、持锁信息的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
  when 'AccessShareLock' then
  when 'RowShareLock' then
  when 'RowExclusiveLock' then
  when 'ShareUpdateExclusiveLock' then
  when 'ShareLock' then
  when 'ShareRowExclusiveLock' then
  when 'ExclusiveLock' then
  when 'AccessExclusiveLock' then
  else
 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 ;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/chuckchen1222/article/details/80847535

延伸 · 阅读

精彩推荐
  • PostgreSQL分布式 PostgreSQL之Citus 架构

    分布式 PostgreSQL之Citus 架构

    节点 Citus 是一种 PostgreSQL 扩展,它允许数据库服务器(称为节点)在“无共享(shared nothing)”架构中相互协调。这些节点形成一个集群,允许 PostgreSQL 保存比单...

    未知802023-05-07
  • PostgreSQL深入理解PostgreSQL的MVCC并发处理方式

    深入理解PostgreSQL的MVCC并发处理方式

    这篇文章主要介绍了深入理解PostgreSQL的MVCC并发处理方式,文中同时介绍了MVCC的缺点,需要的朋友可以参考下 ...

    PostgreSQL教程网3622020-04-25
  • PostgreSQLpostgresql 中的to_char()常用操作

    postgresql 中的to_char()常用操作

    这篇文章主要介绍了postgresql 中的to_char()常用操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    J符离13432021-04-12
  • PostgreSQLRDS PostgreSQL一键大版本升级技术解密

    RDS PostgreSQL一键大版本升级技术解密

    一、PostgreSQL行业位置 (一)行业位置 在讨论PostgreSQL(下面简称为PG)在整个数据库行业的位置之前,我们先看一下阿里云数据库在全球的数据库行业里的...

    未知1192023-05-07
  • PostgreSQLPostgreSQL标准建表语句分享

    PostgreSQL标准建表语句分享

    这篇文章主要介绍了PostgreSQL标准建表语句分享,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    码上得天下7962021-02-27
  • PostgreSQLPostgresql查询效率计算初探

    Postgresql查询效率计算初探

    这篇文章主要给大家介绍了关于Postgresql查询效率计算的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Postgresql具有一定的参考学习价...

    轨迹4622020-05-03
  • PostgreSQLPostgresql开启远程访问的步骤全纪录

    Postgresql开启远程访问的步骤全纪录

    postgre一般默认为本地连接,不支持远程访问,所以如果要开启远程访问,需要更改安装文件的配置。下面这篇文章主要给大家介绍了关于Postgresql开启远程...

    我勒个去6812020-04-30
  • PostgreSQLpostgresql 数据库中的数据转换

    postgresql 数据库中的数据转换

    postgres8.3以后,字段数据之间的默认转换取消了。如果需要进行数据变换的话,在postgresql数据库中,我们可以用"::"来进行字段数据的类型转换。...

    postgresql教程网12482021-10-08