Home > Database > Mysql Tutorial > PostgreSQL锁查询与杀掉进程说明_MySQL

PostgreSQL锁查询与杀掉进程说明_MySQL

PHP中文网
Release: 2016-05-27 19:11:43
Original
1687 people have browsed it

查询表中存在的锁

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';
Copy after login

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

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

 locktype | database |  pid  |      mode       | relation | relname
----------+----------+-------+-----------------+----------+---------
 relation |   439791 | 26752 | AccessShareLock |  2851428 |table_name
 relation |   439791 | 26752 | ExclusiveLock   |  2851428 |table_name
Copy after login

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

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

如下:

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)
Copy after login

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

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

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

杀掉方法:在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
Copy after login

以上就是PostgreSQL锁查询与杀掉进程说明_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template