微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL PARSE等待一例

某系统发送异常,经查有大量waiting.
skyurs=# select procpid,query_start,waiting,current_query from pg_stat_activity;
procpid | query_start | waiting | current_qu
ery
---------+-------------------------------+---------+--------------------------------------------------------------------------------
--------------------------------------------------------------------------
22048 | 2010-08-27 14:30:24.673425+08 | f | create table urs_test_user_list as select user_id,count(user_id) from urs_user_
relation a where relation=1 group by user_id having(count(user_id)>50) \r+
| | | and (select count(*) from urs_user_relation where relation=1 and buddy_id=a.use
r_id limit 50)=50\r +
| | | limit 400000\r
+
| | |
2634 | 2010-08-27 19:27:06.622968+08 | t | select buddy_id,group_id,relation,appinfo,extinfo,create_time from skyurs
.urs_user_relation where user_id=$1 and relation=1
12932 | 2010-08-27 20:05:50.278318+08 | t | select buddy_id,create_time from skyurs
.urs_user_relation where user_id=$1 and relation=1

建表语句:create table urs_test_user_list as select user_id,count(user_id) from urs_user_
relation a where relation=1 group by user_id having(count(user_id)>50) \r+
| | | and (select count(*) from urs_user_relation where relation=1 and buddy_id=a.use
r_id limit 50)=50\r +
| | | limit 400000\
把表 urs_user_relation给锁了,导致相关的SELECT都处于等待锁状态.
skyurs=# select pg_terminate_backend(22048);
pg_terminate_backend
----------------------
t
(1 row)
干掉之后,锁消失。
结束后查看数据库日志:
出现大量:
2010-08-28 10:56:53.735 CST,"skyurs",17107,"192.168.171.39:40721",4c77adcc.42d3,3,"PARSE",2010-08-27 20:21:32 CST,74/157,
LOG,00000,"duration: 52520677.542 ms parse <unnamed>: select buddy_id,create_time from skyu
rs.urs_user_relation where user_id=$1 and relation=1",""

总结:
1. 生产环境跑长sql必须引起注意.这种sql必须人在现场看牢。以及时响应。
2. 这个建表语句可以改一下,先把表建好,再用insert into select语法写入,这样的话锁没这么大。
当然,Postgresql应该降低这类情形下的锁级别才对。
3. 可以考虑配置超时机制,超时的sql返回错误,以免锁过长时间才发现
4. 监控超时sql,及时反馈给DBA处理。

原文地址:https://www.jb51.cc/postgresql/196552.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐