IT 관련/PostgreSQL

[PostgreSQL] idle session 정리 (pg_terminate_backend(pid) / 프로세스 kill

상어 우두머리 2026. 2. 10. 09:45
728x90

idle 및 idle in transaction 이 쌓여있는 경우 max_connection 을 초과하기 전에는 db안에서 정리가 가능함.

초과한 경우에는 프로세스 킬로 죽여야함

--------------------------------------

DB 접속이 가능한 경우

- pg_terminate_backend() 로 정리

1개 세션만 정리
select pg_terminate_backend(pid명);

idle 세션 전체 정리
select pg_terminate_backend(pid) from pg_stat_activity where state = 'idle';

idle 및 idle in transaction 모두 정리
select pg_terminate_backend(pid) from pg_stat_activity where state in ('idle', 'idle in transaction');

-------------------------------------

접속 session이 max_connections를 초과하여 DB 접속이 불가능 한 경우

- 한번에 명령어 다 실행하지 말고 어떤게 잡히는지 확인 후 마지막 kill을 붙일 것

ps -ef | grep postgres | grep -v grep | awk '{print $2}' | xargs kill


728x90