We begin the process by getting a list of every process ID, duration, and query currently running for longer than 2 seconds. Though 2 seconds is arbitrary; it helps filter out short and fast queries that we aren't interested in. If we examine the queries listed in these results, we may decide that one or more need to be canceled or disconnected. The results should resemble this output:
If this is the case, the pid column conveys important information necessary to target the client connection. We begin by invoking pg_cancel_backend in an attempt to terminate the currently running query. Often, this is enough to clear locks or ...