Need to write PostgreSQL Stored Procedure to find and kill Idle queries. Below are the query to find and kill PID:
To find list of PID:
SELECT pid FROM pg_stat_activity where datname='dbdataanalytics' and state='idle' and state_change<=current_date-1
To kill The PID:
SELECT pg_terminate_backend(pid) FROM idle_connections
But I need to run this every day automatically. Please help me write stored procedure.
You should upgrade to PostgreSQL v14 and set the idle_session_timeout
parameter. Even better would be to fix the connection leak in your application.
To terminate all connections that have been idle for more then 5 minutes, tun
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle'
AND state_change < current_timestamp - INTERVAL '5 minutes';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.