活动连接数和剩余连接数


21

我想获取有关一段时间内连接峰值数量的统计信息。

我知道这种pg_stat_activity观点,select count(*) from pg_stat_activity但我认为这种方法不是很聪明。

还有其他视图或表格可以提供我需要的信息吗?

Answers:


39

该SQL将帮助您

select max_conn,used,res_for_super,max_conn-used-res_for_super res_for_normal 
from 
  (select count(*) used from pg_stat_activity) t1,
  (select setting::int res_for_super from pg_settings where name=$$superuser_reserved_connections$$) t2,
  (select setting::int max_conn from pg_settings where name=$$max_connections$$) t3

结果:

max_conn | used | res_for_super | res_for_normal 
---------+------+---------------+----------------
  100    |    2 |             3 |             95
(1 row)

您可以将其放在shell中:

#!/bin/bash
for (( c=1; c<=3600; c++ ))
do
     gsql -U pgdba -W pgdba -p 6432 -c "sql" >> /home/pgdba/res_data.log
     sleep 1  # once per second
done

或者您可以将结果记录到表中,然后执行

postgres=# copy restbl to '/home/pgdba/res.csv' csv header;

获取结果csv文件。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.