[Reprint] postgresql view the user connection and kill the session connection

postgresql view the user connection and kill the session connection

Disclaimer: This article is a blogger original article, follow the  CC 4.0 by-sa  copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/DB_su/article/details/78204101

Connections sizing parameters:

max_connection

View total number of connections:

select count(*) from pg_stat_activity;

View all connected users:

select * from pg_stat_activity;

The results set displays the database name, user, IP address, start time of the current connections, wait events, queries etc. 
pg_stat_activity actually a view.

End Process connection

pg_terminate_backend is an internal method pg, plus a call pg_cancel_backend, this method has been present in the previous version 8.4. The difference between these two methods is that, pg_cancel_backend just cancel the current query a particular process, but can not release the database connection. But pg_terminate_backend can kill this process pg background, freeing up valuable resources connected

SELECT pg_terminate_backend(15278)

Kill all the idle process of:

postgres=# select pg_terminate_backend(pid) from pg_stat_activity where state=’idle’;

pg_terminate_backend



(2 rows)

Or PG mydb database server, find the process PID and then kill.

ps -ef | grep 15278 
kill -9 15278

pgadmin can also view all current connections.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11417787.html