开发者

Any way to analyze locks after the fact?

Say I had a lock that lasted for a couple of hours and caused a lot of queries to wait for it, but it was released before I 开发者_如何学Chad the chance to investigate it. Is there any way to see where that lock was? Anything in particular to look for in the logs?


I had a similar problem in the past and wrote a simple bash script that checked on the database and saved a copy of the views I wanted with a timestamp on it every minute so I could later look at the data when the problem had passed. Here's the one I used, it grabs pg_stat_activity when there are more than 50 backends connected. Feel free to mangle it in whatever way works for you:

#!/bin/bash
threshold=50;
dt=`date +%Y%m%d%H%M%S`;
active=`/usr/bin/psql www -Atc "select count(*) from pg_stat_activity where current_query not ilike '%idle%';"`
if [[ active -gt threshold ]]; then
    echo "there are "$active" backends";
    echo "creating backup for pg_stat as pg_stat_bk_$dt"
    psql www -c "select * into monitoring.pg_stat_bk_$dt from pg_stat_activity where current_query not ilike '%idle%';"
fi
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜