Re: Configuring bgw_restart_time

2019-08-13 Thread Craig Ringer
On Tue, 13 Aug 2019 at 20:37, Jeremy Finzel  wrote:

> I am working on an extension that uses background workers, and interested
> in adding code for it to auto-restart after a crash or server restart
> (similar to as-coded in worker_spi).
>

What pglogical does for this is use dynamic background workers with restart
turned off. It does its own worker exit and restart handling from a manager
worker that's an always-running static bgworker.

It's not ideal as it involves a considerable amount of extra work, but with
BDR we rapidly found that letting postgres itself restart bgworkers was
much too inflexible and hard to control. Especially given the issues around
soft-crash restarts and worker registration (see thread
https://www.postgresql.org/message-id/flat/534E6569.1080506%402ndquadrant.com)
.

But I'm also interested in being able to configure bgw_restart_time using a
> GUC without having to restart the server, using only SIGHUP.  For example,
> I want to normally have the worker restart after 10 seconds.  But if I am
> doing maintenance on the server (without a db restart), I perhaps want to
> change this to -1 (BGW_NEVER_RESTART), kill the worker, do my business,
> then restart the worker.
>

Instead of doing that I suggest having a SQL-callable function that sets a
flag in a shared memory segment used by the worker then sets the worker's
ProcLatch to wake it up if it's sleeping. The flag can *ask* it to exit
cleanly. If its exit code is 0 it will not be restarted.

You could also choose to have the worker exit with code 0 on SIGTERM, again
causing itself to be unregistered and not restarted.

-- 
 Craig Ringer   http://www.2ndQuadrant.com/
 2ndQuadrant - PostgreSQL Solutions for the Enterprise


Configuring bgw_restart_time

2019-08-13 Thread Jeremy Finzel
I am working on an extension that uses background workers, and interested
in adding code for it to auto-restart after a crash or server restart
(similar to as-coded in worker_spi).

But I'm also interested in being able to configure bgw_restart_time using a
GUC without having to restart the server, using only SIGHUP.  For example,
I want to normally have the worker restart after 10 seconds.  But if I am
doing maintenance on the server (without a db restart), I perhaps want to
change this to -1 (BGW_NEVER_RESTART), kill the worker, do my business,
then restart the worker.  Or another reason would be my background worker
has some bug and I want to disable it without having to restart my db
server.  For us as for many, a small outage for a db restart is expensive.

I have played around with this and done some digging around the codebase
in bgworker.c (with my limited knowledge thus far of the pg codebase), and
so far as I can tell, it isn't possible to change bgw_restart_time without
a server restart.  But I'm not sure if that's just because I don't know how
this code works, or if the current libraries actually don't support
modifying this part of the background worker.  I am setting the GUC in
_PG_init, but I can see that changing it after it has been registered has
no effect unless I restart the server.

If indeed this is possible, I'd be very grateful for some insight on how to
do it.  I may even try to add such an example to worker_spi.

Thanks!
Jeremy