pg_terminate_backend( pid integer, timeout bigint DEFAULT 0 );
We can find the PID by using the following query on everyone favourite view pg_stat_activity.
I added the time_in_state column to show how long the session has been in its current state.
=> SELECT pid, usename, client_addr, application_name, state, wait_event,
now() - state_change AS time_in_state
FROM pg_stat_activity
order by time_in_state desc;
Once we decide on the pid to kill. pass the PID and the TIMEOUT you want.
=> SELECT pg_terminate_backend(5866,3000);
pg_terminate_backend
----------------------
t
(1 row)
Why is the timeout important? Looking at the docs
"If timeout is not specified or zero, this function returns true whether the process actually terminates or not, indicating only that the sending of the signal was successful. "
So we should add a timeout in ms, so when know it really works.
"If the timeout is specified (in milliseconds) and greater than zero, the function waits until the process is actually terminated or until the given time has passed. If the process is terminated, the function returns true. On timeout, a warning is emitted and false is returned."
https://www.postgresql.org/docs/15/functions-admin.html
******************************************
keywords: postgres kill session
******************************************
rdbms version: 15
******************************************
No comments:
Post a Comment