PostgreSQL锁查询与杀掉进程说明

查询表中存在的锁

select a.locktype,a.database,a.pid,a.mode,a.relation,b.relname

from pg_locks a

join pg_class b on a.relation = b.oid

where upper(b.relname) = 'TABLE_NAME';

以上为查询某表上是否存在锁的SQL语句。

查到后发现确实存在锁,如下:

 locktype | database |  pid  |      mode      | relation | relname

----------+----------+-------+-----------------+----------+---------

 relation |  439791 | 26752 | AccessShareLock |  2851428 |table_name

 relation |  439791 | 26752 | ExclusiveLock  |  2851428 |table_name

再根据上面查出来的pid去表pg_stat_activity查询一下该锁对应的SQL语句:

select usename,current_query ,query_start,procpid,client_addr from pg_stat_activity where procpid=17509;

如下:

  usename  |  current_query                      |          query_start          | procpid |  client_addr 

-----------+---------------------------------------------------------------------------------------------------------------+-------------------------------+---------+----------------

 gpcluster | DELETE FROM TABLE_NAME WHERE A = 1  | 2011-05-14 09:35:47.721173+08 |  17509 | 192.168.165.18

(1 row)

通过以上可以发现,就是上面的锁导致该语句一直挂在那里。然后把该锁结束掉后,应用很快跑完。

然后核查应用的代码,发现代码里面两个事务都没有提交操作。后增加提交操作后,重新跑数,很快跑完。

如果要杀死,首先向相关人员确认这个语句是否为关键进程。

杀掉方法:在PG数据库mydb服务器中,查询这个进程PID然后Kill掉。

> ps -ef|grep 17509

postgres 17509  4868  1 Nov18 ?        00:11:19 postgres: postgres mydb 192.168.165.18(56059) SELECT             

postgres 30832 30800  0 15:18 pts/3    00:00:00 grep 17509

> ps -ef|grep 17509

postgres 17509  4868  1 Nov18 ?        00:11:19 postgres: postgres mydb 192.168.165.18(56059) SELECT             

postgres 30838 30800  0 15:19 pts/3    00:00:00 grep 17509

> kill -9 17509

------------------------------------华丽丽的分割线------------------------------------

------------------------------------华丽丽的分割线------------------------------------

PostgreSQL 的详细介绍请点这里
PostgreSQL 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2015-11/125329.htm