当前位置: 首页 > 图灵资讯 > 技术篇> postgresql 锁表查询语句

postgresql 锁表查询语句

来源:图灵教育
时间:2023-06-20 09:30:24

1 找到锁表的pid

select pid from pg_locks l join pg_class t on l.relation = t.oid where t.relkind = 'r' and t.relname = 'lockedtable';

2 找到锁表的句子

select pid, state, usename, query, query_start from pg_stat_activity where pid in ( select pid from pg_locks l join pg_class t on l.relation = t.oid and t.relkind = 'r' where t.relname =  'lockedtable');

3 搜索所有活动的被锁表

select pid, state, usename, query, query_start from pg_stat_activity where pid in (  select pid from pg_locks l   join pg_class t on l.relation = t.oid   and t.relkind = 'r' );

4 解锁

SELECT pg_cancel_backend(pid);

参考:postgresql 锁表查询语句 - wolbo -

用一个例子演示会更清晰