Monday, 5 September 2022

PostgreSQL - How to find and kill a hanging query?

First, check all the processes that are running:


SELECT * FROM pg_stat_activity;


So you can identify the PID of the hanging query you want to terminate, run this:


Option #1 (graceful):

SELECT pg_cancel_backend(PID);



Option #2 (forceful):

SELECT pg_terminate_backend(PID);


SELECT 

    pg_terminate_backend(pid) 

FROM 

    pg_stat_activity 

WHERE 

    -- don't kill my own connection!

    pid <> pg_backend_pid()

    AND state = 'idle'

No comments:

Snowflake - Cost Optimization

Reduce auto-suspend to 60 seconds Reduce virtual warehouse size Ensure minimum clusters are set to 1 Consolidate warehouses Separate warehou...