Last active
September 26, 2021 17:56
-
-
Save marcomalva/4a1e905533c1b7540b2734ddd261e097 to your computer and use it in GitHub Desktop.
[PSQL - Count Number of Connections]Queries to obtain current/maximum count of connections with PostgreSQL #SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- tuning advise: https://www.cybertec-postgresql.com/en/tuning-max_connections-in-postgresql/ | |
| -- | |
| -- get number of connections by database instance + total | |
| select datname, count(*) as cnt_conn, sum(count(*)) over () as tot_conn from pg_stat_activity group by datname order by datname; | |
| -- get more details, see column numbackends for number of connections | |
| select * from pg_stat_database order by datname; | |
| select datname, numbackends, sum(numbackends) over() as tot_conn from pg_stat_database order by datname; | |
| -- combine the two above | |
| select coalesce(a.datname,b.datname) as datname, a.numbackends as cnt_conn_1, b.cnt_conn as cnt_conn_2, sum(a.numbackends) over() as tot_conn_1, b.tot_conn_2 | |
| from pg_stat_database a full outer join (select datname, count(*) as cnt_conn, sum(count(*)) over () as tot_conn_2 from pg_stat_activity s group by datname) b on a.datname = b.datname | |
| order by coalesce(a.datname,b.datname); | |
| -- show max number of connections, e.g. 600 | |
| show max_connections; | |
| -- show private memory per connect, the private memory available for each operation during query execution is limited by work_mem, e.g. 4 MB | |
| show work_mem; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment