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'

DBT - Models

Models are where your developers spend most of their time within a dbt environment. Models are primarily written as a select statement and ...