walwriter_latch.v1.patch
text/x-patch
Filename: walwriter_latch.v1.patch
Type: text/x-patch
Part: 0
diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c
index 1411677..2027dd7 100644
--- a/src/backend/postmaster/walwriter.c
+++ b/src/backend/postmaster/walwriter.c
@@ -52,6 +52,7 @@
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "storage/ipc.h"
+#include "storage/latch.h"
#include "storage/lwlock.h"
#include "storage/pmsignal.h"
#include "storage/smgr.h"
@@ -79,7 +80,11 @@ static void WalShutdownHandler(SIGNAL_ARGS);
/*
- * Main entry point for walwriter process
+ * Latch used by signal handlers to wake up the sleep in the main loop.
+ */
+static Latch mainloop_latch;
+
+/* Main entry point for walwriter process
*
* This is invoked from BootstrapMain, which has already created the basic
* execution environment, but not enabled signals yet.
@@ -217,12 +222,16 @@ WalWriterMain(void)
PG_SETMASK(&UnBlockSig);
/*
+ * Initialize latch used in loop below
+ */
+ InitLatch(&mainloop_latch);
+
+ /*
* Loop forever
*/
for (;;)
{
- long udelay;
-
+ ResetLatch(&mainloop_latch);
/*
* Emergency bailout if postmaster has died. This is to avoid the
* necessity for manual cleanup of all postmaster children.
@@ -249,20 +258,8 @@ WalWriterMain(void)
*/
XLogBackgroundFlush();
- /*
- * Delay until time to do something more, but fall out of delay
- * reasonably quickly if signaled.
- */
- udelay = WalWriterDelay * 1000L;
- while (udelay > 999999L)
- {
- if (got_SIGHUP || shutdown_requested)
- break;
- pg_usleep(1000000L);
- udelay -= 1000000L;
- }
- if (!(got_SIGHUP || shutdown_requested))
- pg_usleep(udelay);
+ /* We handle all expected signals, so WalWriterDelay timeout won't be invalidated */
+ WaitLatch(&mainloop_latch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, WalWriterDelay * 1000L);
}
}
@@ -309,6 +306,8 @@ static void
WalSigHupHandler(SIGNAL_ARGS)
{
got_SIGHUP = true;
+ /* let the waiting loop iterate */
+ SetLatch(&mainloop_latch);
}
/* SIGTERM: set flag to exit normally */
@@ -316,4 +315,6 @@ static void
WalShutdownHandler(SIGNAL_ARGS)
{
shutdown_requested = true;
+ /* let the waiting loop iterate */
+ SetLatch(&mainloop_latch);
}