PostgreSQL鎖查詢與殺掉進程說明

來源:互聯網
上載者:User

標籤:postgresql pid 鎖 block

查詢表中存在的鎖


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


本文出自 “yiyi” 部落格,請務必保留此出處http://heyiyi.blog.51cto.com/205455/1714737

PostgreSQL鎖查詢與殺掉進程說明

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.