Re: A new function to wait for the backend exit after termination

From: Fujii Masao <masao(dot)fujii(at)oss(dot)nttdata(dot)com>
To: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>, Magnus Hagander <magnus(at)hagander(dot)net>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: A new function to wait for the backend exit after termination
Date: 2020-10-28 13:11:42
Message-ID: d99121ed-5e6b-89c1-3fcd-4a53561edbd2@oss.nttdata.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2020/10/28 20:50, Bharath Rupireddy wrote:
> On Wed, Oct 21, 2020 at 6:43 PM Magnus Hagander <magnus(at)hagander(dot)net> wrote:
>>
>> I think it would be nicer to have a pg_terminate_backend(pid, wait=false), so a function with a second parameter which defaults to the current behaviour of not waiting. And it might be a good idea to also give it a timeout parameter?
>>
>
> Done.
>
>>
>>> 2. pg_wait_backend() -- which waits for a given backend process. Note that this function has to be used carefully after pg_terminate_backend(), if used on a backend that's not ternmited it simply keeps waiting in a loop.
>>
>> It seems this one also very much would need a timeout value.
>>
>
> Done.
>
>>
>> And surely we should show some sort of wait event when it's waiting.
>>
>
> Added two wait events.
>
>>
>>> If the backend is terminated within the user specified timeout then
>>> the function returns true, otherwise false.
>>
>> I’m suggesting an option for the second case to fail instead of returning false.
>>
>
> Done.

I prefer that false is returned when the timeout happens,
like pg_promote() does.

>
>>
>>> I could imagine, in theory at least, wanting to wait for a backend to go idle as well as for it disappearing. Scope creep in terms of this patch's goal but worth at least considering now.
>>
>> IIUC, do we need a new option, something like pg_wait_backend(pid,
>> timeout, waituntil) where "waituntil" if specified "idle" waits until
>> the given backend goes to idle mode, or "termination" waits until
>> termination?

Isn't this wait-for-idle mode fragile? Because there is no guarantee
that the backend is still in idle state when pg_wait_backend(idle) returns.

>>
>
> Done.
>
> Attaching a v2 patch herewith.
>
> Thoughts and feedback are welcome.

Thanks for the patch!

When the specified timeout is negative, the following error is thrown *after*
SIGTERM is signaled to the target backend. This seems strange to me.
The timeout value should be verified at the beginning of the function, instead.

ERROR: timeout cannot be negative

pg_terminate_backend(xxx, false) failed with the following error. I think
it's more helpful if the function can work even without the timeout value.
That is, what about redefining the function in src/backend/catalog/system_views.sql
and specifying the DEFAULT values for the arguments "wait" and "timeout"?
The similar function "pg_promote" would be good reference to you.

ERROR: function pg_terminate_backend(integer, boolean) does not exist at character 8

Regards,

--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message John Naylor 2020-10-28 13:26:09 Re: cutting down the TODO list thread
Previous Message Laurenz Albe 2020-10-28 13:05:05 Re: Disable WAL logging to speed up data loading