public inbox for [email protected]  
help / color / mirror / Atom feed
Re: psql help
3+ messages / 3 participants
[nested] [flat]

* Re: psql help
@ 2024-07-05 01:16 David G. Johnston <[email protected]>
  2024-07-05 03:54 ` RE: psql help Murthy Nunna <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: David G. Johnston @ 2024-07-05 01:16 UTC (permalink / raw)
  To: Murthy Nunna <[email protected]>; +Cc: [email protected] <[email protected]>

On Thu, Jul 4, 2024 at 4:56 PM Murthy Nunna <[email protected]> wrote:

>
>
> How can I rewrite the above in psql
>

The only real trick is using a psql variable instead of the shell-injection
of the environment variable.  Use the --set CLI argument to assign the
environment variable to a psql variable then refer to it in the query using
:'timout_mins'

Removing the limit 1 should be as simple as not typing limit 1 when you
bring the query into the psql script.

David J.


^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* RE: psql help
  2024-07-05 01:16 Re: psql help David G. Johnston <[email protected]>
@ 2024-07-05 03:54 ` Murthy Nunna <[email protected]>
  2024-07-05 11:05   ` Re: psql help hubert depesz lubaczewski <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: Murthy Nunna @ 2024-07-05 03:54 UTC (permalink / raw)
  To: [email protected] <[email protected]>; David G. Johnston <[email protected]>

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;





From: David G. Johnston <[email protected]>
Sent: Thursday, July 4, 2024 8:17 PM
To: Murthy Nunna <[email protected]>
Cc: [email protected]
Subject: Re: psql help


[EXTERNAL] – This message is from an external sender
On Thu, Jul 4, 2024 at 4:56 PM Murthy Nunna <[email protected]<mailto:[email protected]>> wrote:

How can I rewrite the above in psql

The only real trick is using a psql variable instead of the shell-injection of the environment variable.  Use the --set CLI argument to assign the environment variable to a psql variable then refer to it in the query using :'timout_mins'

Removing the limit 1 should be as simple as not typing limit 1 when you bring the query into the psql script.

David J.



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: psql help
  2024-07-05 01:16 Re: psql help David G. Johnston <[email protected]>
  2024-07-05 03:54 ` RE: psql help Murthy Nunna <[email protected]>
@ 2024-07-05 11:05   ` hubert depesz lubaczewski <[email protected]>
  0 siblings, 0 replies; 3+ messages in thread

From: hubert depesz lubaczewski @ 2024-07-05 11:05 UTC (permalink / raw)
  To: Murthy Nunna <[email protected]>; +Cc: [email protected] <[email protected]>

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







^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2024-07-05 11:05 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-07-05 01:16 Re: psql help David G. Johnston <[email protected]>
2024-07-05 03:54 ` Murthy Nunna <[email protected]>
2024-07-05 11:05   ` hubert depesz lubaczewski <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox