Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sPgkr-005nie-A7 for pgsql-general@arkaria.postgresql.org; Fri, 05 Jul 2024 11:05:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sPgkp-008kf1-AZ for pgsql-general@arkaria.postgresql.org; Fri, 05 Jul 2024 11:05:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sPgko-008ket-Uh for pgsql-general@lists.postgresql.org; Fri, 05 Jul 2024 11:05:27 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sPgkn-000XjW-WE for pgsql-general@lists.postgresql.org; Fri, 05 Jul 2024 11:05:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description; bh=taeGPncAMKqB58UFMNvePEDeSqJHpvuEk9v8RQsYm6c=; b=ivEd4GHrvF5DGgTFjGoqwuAbfg LNeG+5fOFFl9l6zFbEqDWuZjqRKBQ4hHK0Je7YgZXUGmhG+CM5qFy/2wulxJGW14t8DT+W6coabSM HPnkHaYT3H/wUm3bdCmlBqtE1LjZOj25RecavJrFUdy1OzQlLOvwA9AvHq+xgr0VS1Ac=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1sPgkm-004xit-0Z; Fri, 05 Jul 2024 13:05:24 +0200 Date: Fri, 5 Jul 2024 13:05:24 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Murthy Nunna Cc: "pgsql-general@lists.postgresql.org" Subject: Re: psql help Message-ID: Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jul 05, 2024 at 03:54:56AM +0000, Murthy Nunna wrote: > Sorry, there is no problem with the following statement and the environment variable. It works fine. But it terminates only one PID due to LIMIT 1. I want to terminate all pids that meet this criteria. If I remove LIMIT 1, pg_terminate_backend(pid) will not work as it expects only one pid at a time. So, the question is how to rewrite this psql so it loops through all pids one pid at a time? Thanks in advance for your help. > > SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity > WHERE pid IN (select unnest(pg_blocking_pids(pid)) from pg_stat_activity where cardinality(pg_blocking_pids(pid)) > 0) > and usename = 'DBUSER_10' > and now() - state_change >= interval $a'${TIMEOUT_MINS}'$a > order by now() - state_change >= interval $a'${TIMEOUT_MINS}'$a desc limit 1; Did you try? I don't see any reason why it wouldn't work with just 'limit 1' removed. Best regards, depesz