Skip to content

Commit 06c65e4

Browse files
committed
refactor: improve roles.sql
Add back active_connections, smarter connection_limit.
1 parent b7ca2a8 commit 06c65e4

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/lib/sql/roles.sql

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,30 @@ SELECT
77
rolcanlogin AS can_login,
88
rolreplication AS is_replication_role,
99
rolbypassrls AS can_bypass_rls,
10-
rolconnlimit AS connection_limit,
10+
active_connections,
11+
CASE WHEN rolconnlimit = -1 THEN max_db_connections :: int4
12+
ELSE rolconnlimit
13+
END AS connection_limit,
1114
rolpassword AS password,
1215
rolvaliduntil AS valid_until,
1316
rolconfig AS config,
14-
oid
17+
oid AS id
1518
FROM
1619
pg_catalog.pg_roles
20+
INNER JOIN LATERAL (
21+
SELECT
22+
count(*) AS active_connections
23+
FROM
24+
pg_stat_activity
25+
WHERE
26+
state = 'active'
27+
AND pg_roles.rolname = pg_stat_activity.usename
28+
) AS active_connections ON 1 = 1
29+
INNER JOIN LATERAL (
30+
SELECT
31+
setting AS max_db_connections
32+
FROM
33+
pg_settings
34+
WHERE
35+
name = 'max_connections'
36+
) AS max_db_connections ON 1 = 1

0 commit comments

Comments
 (0)