Sample Header Ad - 728x90

How to count the number of connections to a postgres database?

2 votes
1 answer
65 views
The configuration file *postgresql.conf* contains a setting for max_connections. I have a number of related questions... 1. In the context of this value, what exactly constitutes a 'connection'? 2. What's the best way to calculate it? 3. What are the symptoms/error messages that might be expected on a database that needs to have max_connections increased? I've seen several posting on how to count the number of current connections... 1) SELECT count(*) from pg_stat_activity; 2) SELECT count(distinct(numbackends)) FROM pg_stat_database; 3) SELECT sum(numbackends) FROM pg_stat_database WHERE datname is not null; 4) SELECT count(*) FROM pg_stat_activity WHERE datname is not null; 5) select sum(numbackends) from pg_stat_database; They often produce different results, and take varying times to execute. Which query should give the most accurate results, and how can I tell if I need to increase max_connections?
Asked by ConanTheGerbil (1303 rep)
Apr 10, 2025, 06:14 PM
Last activity: Apr 11, 2025, 06:11 AM