IT 관련/PostgreSQL

[PostgreSQL] 설정된 max_connections 대비 현재 연결 Session 비율 확인

상어 우두머리 2026. 2. 23. 11:56
728x90

# 1. max_connections 값 가져오기
MAX_CONN=$(psql -Atc "show max_connections;")

# 2. 현재 프로세스 개수 가져오기 (작성하신 명령어 활용)
CUR_CONN=$(ps -ef | grep postgres: | egrep -v '(grep|logger|checkpointer|background|walwriter|autovacuum|pg_auto_failover|logical|walsender)' | wc -l)

# 3. awk를 이용한 백분율 계산 (소수점 둘째 자리까지)
usage_percent=$(awk "BEGIN {printf \"%.2f\", ($CUR_CONN / $MAX_CONN) * 100}")

echo "max_connections : $MAX_CONN"
echo "현재 연결 Session : $CUR_CONN"
echo "현재 연결 비율: $usage_percent% ($CUR_CONN/$MAX_CONN)"

728x90