Commit 2a084772 authored by Fujii Masao's avatar Fujii Masao

Use standard SIGHUP and SIGTERM signal handlers in worker_spi.

Previously worker_spi used its custom signal handlers for SIGHUP and
SIGTERM. This commit makes worker_spi use the standard signal handlers,
to simplify the code.

Note that die() is used as the standard SIGTERM signal handler in
worker_spi instead of SignalHandlerForShutdownRequest() or bgworker_die().
Previously the exit handling was only able to exit from within the main loop,
and not from within the backend code it calls. This is why die() needs to
be used here, so worker_spi can respond to SIGTERM signal while it's
executing a query.

Maybe we can say that it's a bug that worker_spi could not respond to
SIGTERM during query execution. But since worker_spi is a just example
of the background worker code, we don't do the back-patch.

Thanks to Craig Ringer for the report and investigation of the issue.

Author: Bharath Rupireddy
Reviewed-by: Fujii Masao
Discussion: https://postgr.es/m/CALj2ACXDEZhAFOTDcqO9cFSRvrFLyYOnPKrcA1UG4uZn9hUAVg@mail.gmail.com
Discussion: https://postgr.es/m/CAGRY4nxsAe_1k_9g5b47orA0S011iBoHsXHFMH7cg7HV0O1bwQ@mail.gmail.com
parent 0926e96c
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
/* These are always necessary for a bgworker */ /* These are always necessary for a bgworker */
#include "miscadmin.h" #include "miscadmin.h"
#include "postmaster/bgworker.h" #include "postmaster/bgworker.h"
#include "postmaster/interrupt.h"
#include "storage/ipc.h" #include "storage/ipc.h"
#include "storage/latch.h" #include "storage/latch.h"
#include "storage/lwlock.h" #include "storage/lwlock.h"
...@@ -48,10 +49,6 @@ PG_FUNCTION_INFO_V1(worker_spi_launch); ...@@ -48,10 +49,6 @@ PG_FUNCTION_INFO_V1(worker_spi_launch);
void _PG_init(void); void _PG_init(void);
void worker_spi_main(Datum) pg_attribute_noreturn(); void worker_spi_main(Datum) pg_attribute_noreturn();
/* flags set by signal handlers */
static volatile sig_atomic_t got_sighup = false;
static volatile sig_atomic_t got_sigterm = false;
/* GUC variables */ /* GUC variables */
static int worker_spi_naptime = 10; static int worker_spi_naptime = 10;
static int worker_spi_total_workers = 2; static int worker_spi_total_workers = 2;
...@@ -64,38 +61,6 @@ typedef struct worktable ...@@ -64,38 +61,6 @@ typedef struct worktable
const char *name; const char *name;
} worktable; } worktable;
/*
* Signal handler for SIGTERM
* Set a flag to let the main loop to terminate, and set our latch to wake
* it up.
*/
static void
worker_spi_sigterm(SIGNAL_ARGS)
{
int save_errno = errno;
got_sigterm = true;
SetLatch(MyLatch);
errno = save_errno;
}
/*
* Signal handler for SIGHUP
* Set a flag to tell the main loop to reread the config file, and set
* our latch to wake it up.
*/
static void
worker_spi_sighup(SIGNAL_ARGS)
{
int save_errno = errno;
got_sighup = true;
SetLatch(MyLatch);
errno = save_errno;
}
/* /*
* Initialize workspace for a worker process: create the schema if it doesn't * Initialize workspace for a worker process: create the schema if it doesn't
* already exist. * already exist.
...@@ -179,8 +144,8 @@ worker_spi_main(Datum main_arg) ...@@ -179,8 +144,8 @@ worker_spi_main(Datum main_arg)
table->name = pstrdup("counted"); table->name = pstrdup("counted");
/* Establish signal handlers before unblocking signals. */ /* Establish signal handlers before unblocking signals. */
pqsignal(SIGHUP, worker_spi_sighup); pqsignal(SIGHUP, SignalHandlerForConfigReload);
pqsignal(SIGTERM, worker_spi_sigterm); pqsignal(SIGTERM, die);
/* We're now ready to receive signals */ /* We're now ready to receive signals */
BackgroundWorkerUnblockSignals(); BackgroundWorkerUnblockSignals();
...@@ -219,9 +184,10 @@ worker_spi_main(Datum main_arg) ...@@ -219,9 +184,10 @@ worker_spi_main(Datum main_arg)
table->name); table->name);
/* /*
* Main loop: do this until the SIGTERM handler tells us to terminate * Main loop: do this until SIGTERM is received and processed by
* ProcessInterrupts.
*/ */
while (!got_sigterm) for (;;)
{ {
int ret; int ret;
...@@ -242,9 +208,9 @@ worker_spi_main(Datum main_arg) ...@@ -242,9 +208,9 @@ worker_spi_main(Datum main_arg)
/* /*
* In case of a SIGHUP, just reload the configuration. * In case of a SIGHUP, just reload the configuration.
*/ */
if (got_sighup) if (ConfigReloadPending)
{ {
got_sighup = false; ConfigReloadPending = false;
ProcessConfigFile(PGC_SIGHUP); ProcessConfigFile(PGC_SIGHUP);
} }
...@@ -303,7 +269,7 @@ worker_spi_main(Datum main_arg) ...@@ -303,7 +269,7 @@ worker_spi_main(Datum main_arg)
pgstat_report_activity(STATE_IDLE, NULL); pgstat_report_activity(STATE_IDLE, NULL);
} }
proc_exit(1); /* Not reachable */
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment