Right query to get the current number of connectio

2019-01-29 23:39发布

Which of the following two is more accurate?

select numbackends from pg_stat_database;

select count(*) from pg_stat_activity;

4条回答
疯言疯语
2楼-- · 2019-01-29 23:59

The following query is very helpful

select  * from
(select count(*) used from pg_stat_activity) q1,
(select setting::int res_for_super from pg_settings where name=$$superuser_reserved_connections$$) q2,
(select setting::int max_conn from pg_settings where name=$$max_connections$$) q3;
查看更多
男人必须洒脱
3楼-- · 2019-01-30 00:03

From looking at the source code, it seems like the pg_stat_database query gives you the number of connections to the current database for all users. On the other hand, the pg_stat_activity query gives the number of connections to the current database for the querying user only.

查看更多
Lonely孤独者°
4楼-- · 2019-01-30 00:19

No of tcp connections will help you. Remember that it is not for a particular database

netstat -a -n | find /c "127.0.0.1:13306"

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-30 00:22

Those two requires aren't equivalent. The equivalent version of the first one would be:

SELECT sum(numbackends) FROM pg_stat_database;

In that case, I would expect that version to be slightly faster than the second one, simply because it has fewer rows to count. But you are not likely going to be able to measure a difference.

Both queries are based on exactly the same data, so they will be equally accurate.

查看更多
登录 后发表回答