2004-05-30 00:48:23 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* bgwriter.c
|
|
|
|
*
|
2007-11-15 22:14:46 +01:00
|
|
|
* The background writer (bgwriter) is new as of Postgres 8.0. It attempts
|
2004-05-30 00:48:23 +02:00
|
|
|
* to keep regular backends from having to write out dirty shared buffers
|
|
|
|
* (which they would only do when needing to free a shared buffer to read in
|
|
|
|
* another page). In the best scenario all writes from shared buffers will
|
2004-08-29 07:07:03 +02:00
|
|
|
* be issued by the background writer process. However, regular backends are
|
2004-05-30 00:48:23 +02:00
|
|
|
* still empowered to issue writes if the bgwriter fails to maintain enough
|
|
|
|
* clean shared buffers.
|
|
|
|
*
|
2011-11-01 18:14:47 +01:00
|
|
|
* As of Postgres 9.2 the bgwriter no longer handles checkpoints.
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
|
|
|
* The bgwriter is started by the postmaster as soon as the startup subprocess
|
2009-06-26 22:29:04 +02:00
|
|
|
* finishes, or as soon as recovery begins if we are doing archive recovery.
|
|
|
|
* It remains alive until the postmaster commands it to terminate.
|
2011-11-01 18:14:47 +01:00
|
|
|
* Normal termination is by SIGUSR2, which instructs the bgwriter to exit(0).
|
|
|
|
* Emergency termination is by SIGQUIT; like any
|
2004-05-30 00:48:23 +02:00
|
|
|
* backend, the bgwriter will simply abort and exit on SIGQUIT.
|
|
|
|
*
|
|
|
|
* If the bgwriter exits unexpectedly, the postmaster treats that the same
|
|
|
|
* as a backend crash: shared memory may be corrupted, so remaining backends
|
2004-05-31 05:48:10 +02:00
|
|
|
* should be killed by SIGQUIT and then a recovery cycle started. (Even if
|
|
|
|
* shared memory isn't corrupted, we have lost information about which
|
|
|
|
* files need to be fsync'd for the next checkpoint, and so a system
|
|
|
|
* restart needs to be forced.)
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
|
|
|
*
|
2012-01-02 00:01:58 +01:00
|
|
|
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/postmaster/bgwriter.c
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
2007-06-28 02:02:40 +02:00
|
|
|
#include <sys/time.h>
|
2004-06-03 04:08:07 +02:00
|
|
|
#include <time.h>
|
2006-11-21 21:59:53 +01:00
|
|
|
#include <unistd.h>
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
#include "access/xlog_internal.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "libpq/pqsignal.h"
|
|
|
|
#include "miscadmin.h"
|
2007-03-30 20:34:56 +02:00
|
|
|
#include "pgstat.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "postmaster/bgwriter.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "storage/bufmgr.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "storage/ipc.h"
|
2011-09-04 07:13:16 +02:00
|
|
|
#include "storage/lwlock.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "storage/pmsignal.h"
|
2012-01-26 17:19:48 +01:00
|
|
|
#include "storage/proc.h"
|
2011-09-04 07:13:16 +02:00
|
|
|
#include "storage/shmem.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "storage/smgr.h"
|
2011-09-04 07:13:16 +02:00
|
|
|
#include "storage/spin.h"
|
|
|
|
#include "utils/guc.h"
|
2004-07-31 02:45:57 +02:00
|
|
|
#include "utils/memutils.h"
|
2005-12-08 20:19:22 +01:00
|
|
|
#include "utils/resowner.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GUC parameters
|
|
|
|
*/
|
|
|
|
int BgWriterDelay = 200;
|
|
|
|
|
2012-01-26 17:19:48 +01:00
|
|
|
/*
|
|
|
|
* Time to sleep between bgwriter rounds, when it has no work to do.
|
|
|
|
*/
|
|
|
|
#define BGWRITER_HIBERNATE_MS 10000
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
|
|
|
* Flags set by interrupt handlers for later service in the main loop.
|
|
|
|
*/
|
|
|
|
static volatile sig_atomic_t got_SIGHUP = false;
|
|
|
|
static volatile sig_atomic_t shutdown_requested = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private state
|
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
static bool am_bg_writer = false;
|
2004-05-31 05:48:10 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/* Prototypes for private functions */
|
|
|
|
|
2012-01-26 17:19:48 +01:00
|
|
|
static void BgWriterNap(bool hibernating);
|
2007-06-28 02:02:40 +02:00
|
|
|
|
|
|
|
/* Signal handlers */
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
static void bg_quickdie(SIGNAL_ARGS);
|
|
|
|
static void BgSigHupHandler(SIGNAL_ARGS);
|
|
|
|
static void ReqShutdownHandler(SIGNAL_ARGS);
|
2012-01-26 17:19:48 +01:00
|
|
|
static void bgwriter_sigusr1_handler(SIGNAL_ARGS);
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main entry point for bgwriter process
|
|
|
|
*
|
|
|
|
* This is invoked from BootstrapMain, which has already created the basic
|
|
|
|
* execution environment, but not enabled signals yet.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
BackgroundWriterMain(void)
|
|
|
|
{
|
2004-07-31 02:45:57 +02:00
|
|
|
sigjmp_buf local_sigjmp_buf;
|
2005-09-13 00:20:16 +02:00
|
|
|
MemoryContext bgwriter_context;
|
2012-01-26 17:19:48 +01:00
|
|
|
bool hibernating;
|
2004-07-31 02:45:57 +02:00
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
am_bg_writer = true;
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2006-11-21 21:59:53 +01:00
|
|
|
/*
|
|
|
|
* If possible, make this process a group leader, so that the postmaster
|
2007-11-15 22:14:46 +01:00
|
|
|
* can signal any child processes too. (bgwriter probably never has any
|
|
|
|
* child processes, but for consistency we make all postmaster child
|
|
|
|
* processes do this.)
|
2006-11-21 21:59:53 +01:00
|
|
|
*/
|
|
|
|
#ifdef HAVE_SETSID
|
|
|
|
if (setsid() < 0)
|
|
|
|
elog(FATAL, "setsid() failed: %m");
|
|
|
|
#endif
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
2012-01-26 17:19:48 +01:00
|
|
|
* Properly accept or ignore signals the postmaster might send us.
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
2012-01-26 17:19:48 +01:00
|
|
|
* bgwriter doesn't participate in ProcSignal signalling, but a SIGUSR1
|
|
|
|
* handler is still needed for latch wakeups.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
pqsignal(SIGHUP, BgSigHupHandler); /* set flag to read config file */
|
2011-11-01 18:14:47 +01:00
|
|
|
pqsignal(SIGINT, SIG_IGN); /* as of 9.2 no longer requests checkpoint */
|
|
|
|
pqsignal(SIGTERM, ReqShutdownHandler); /* shutdown */
|
2004-05-30 00:48:23 +02:00
|
|
|
pqsignal(SIGQUIT, bg_quickdie); /* hard crash time */
|
|
|
|
pqsignal(SIGALRM, SIG_IGN);
|
|
|
|
pqsignal(SIGPIPE, SIG_IGN);
|
2012-01-26 17:19:48 +01:00
|
|
|
pqsignal(SIGUSR1, bgwriter_sigusr1_handler);
|
2011-11-01 19:48:47 +01:00
|
|
|
pqsignal(SIGUSR2, SIG_IGN);
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset some signals that are accepted by postmaster but not here
|
|
|
|
*/
|
|
|
|
pqsignal(SIGCHLD, SIG_DFL);
|
|
|
|
pqsignal(SIGTTIN, SIG_DFL);
|
|
|
|
pqsignal(SIGTTOU, SIG_DFL);
|
|
|
|
pqsignal(SIGCONT, SIG_DFL);
|
|
|
|
pqsignal(SIGWINCH, SIG_DFL);
|
|
|
|
|
|
|
|
/* We allow SIGQUIT (quickdie) at all times */
|
|
|
|
sigdelset(&BlockSig, SIGQUIT);
|
|
|
|
|
2012-01-26 17:19:48 +01:00
|
|
|
/*
|
|
|
|
* Advertise our latch that backends can use to wake us up while we're
|
|
|
|
* sleeping.
|
|
|
|
*/
|
|
|
|
ProcGlobal->bgwriterLatch = &MyProc->procLatch;
|
|
|
|
|
2005-12-08 20:19:22 +01:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Create a resource owner to keep track of our resources (currently only
|
|
|
|
* buffer pins).
|
2005-12-08 20:19:22 +01:00
|
|
|
*/
|
|
|
|
CurrentResourceOwner = ResourceOwnerCreate(NULL, "Background Writer");
|
|
|
|
|
2005-09-13 00:20:16 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Create a memory context that we will do all our work in. We do this so
|
|
|
|
* that we can reset the context during error recovery and thereby avoid
|
|
|
|
* possible memory leaks. Formerly this code just ran in
|
2005-09-13 00:20:16 +02:00
|
|
|
* TopMemoryContext, but resetting that would be a really bad idea.
|
|
|
|
*/
|
|
|
|
bgwriter_context = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
"Background Writer",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
MemoryContextSwitchTo(bgwriter_context);
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
|
|
|
* If an exception is encountered, processing resumes here.
|
2004-07-31 02:45:57 +02:00
|
|
|
*
|
|
|
|
* See notes in postgres.c about the design of this coding.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2004-07-31 02:45:57 +02:00
|
|
|
if (sigsetjmp(local_sigjmp_buf, 1) != 0)
|
2004-05-30 00:48:23 +02:00
|
|
|
{
|
2004-07-31 02:45:57 +02:00
|
|
|
/* Since not using PG_TRY, must reset error stack by hand */
|
|
|
|
error_context_stack = NULL;
|
|
|
|
|
|
|
|
/* Prevent interrupts while cleaning up */
|
|
|
|
HOLD_INTERRUPTS();
|
|
|
|
|
|
|
|
/* Report the error to the server log */
|
|
|
|
EmitErrorReport();
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These operations are really just a minimal subset of
|
2004-08-29 07:07:03 +02:00
|
|
|
* AbortTransaction(). We don't have very many resources to worry
|
2005-12-08 20:19:22 +01:00
|
|
|
* about in bgwriter, but we do have LWLocks, buffers, and temp files.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
LWLockReleaseAll();
|
|
|
|
AbortBufferIO();
|
|
|
|
UnlockBuffers();
|
2005-12-08 20:19:22 +01:00
|
|
|
/* buffer pins are released here: */
|
|
|
|
ResourceOwnerRelease(CurrentResourceOwner,
|
|
|
|
RESOURCE_RELEASE_BEFORE_LOCKS,
|
|
|
|
false, true);
|
|
|
|
/* we needn't bother with the other ResourceOwnerRelease phases */
|
|
|
|
AtEOXact_Buffers(false);
|
|
|
|
AtEOXact_Files();
|
2007-09-11 19:15:33 +02:00
|
|
|
AtEOXact_HashTables(false);
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Now return to normal top-level context and clear ErrorContext for
|
|
|
|
* next time.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2005-09-13 00:20:16 +02:00
|
|
|
MemoryContextSwitchTo(bgwriter_context);
|
2004-07-31 02:45:57 +02:00
|
|
|
FlushErrorState();
|
|
|
|
|
2005-09-13 00:20:16 +02:00
|
|
|
/* Flush any leaked data in the top-level context */
|
|
|
|
MemoryContextResetAndDeleteChildren(bgwriter_context);
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/* Now we can allow interrupts again */
|
2004-05-30 00:48:23 +02:00
|
|
|
RESUME_INTERRUPTS();
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Sleep at least 1 second after any error. A write error is likely
|
|
|
|
* to be repeated, and we don't want to be filling the error logs as
|
|
|
|
* fast as we can.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
pg_usleep(1000000L);
|
2006-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Close all open files after any error. This is helpful on Windows,
|
|
|
|
* where holding deleted files open causes various strange errors.
|
|
|
|
* It's not clear we need it elsewhere, but shouldn't hurt.
|
|
|
|
*/
|
|
|
|
smgrcloseall();
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/* We can now handle ereport(ERROR) */
|
|
|
|
PG_exception_stack = &local_sigjmp_buf;
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unblock signals (they were blocked when the postmaster forked us)
|
|
|
|
*/
|
|
|
|
PG_SETMASK(&UnBlockSig);
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* Use the recovery target timeline ID during recovery
|
|
|
|
*/
|
|
|
|
if (RecoveryInProgress())
|
|
|
|
ThisTimeLineID = GetRecoveryTargetTLI();
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Loop forever
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2012-01-26 17:19:48 +01:00
|
|
|
hibernating = false;
|
2004-05-30 00:48:23 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2012-01-26 17:19:48 +01:00
|
|
|
bool lapped;
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
2004-05-31 05:48:10 +02:00
|
|
|
* Emergency bailout if postmaster has died. This is to avoid the
|
|
|
|
* necessity for manual cleanup of all postmaster children.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
if (!PostmasterIsAlive())
|
2004-05-31 05:48:10 +02:00
|
|
|
exit(1);
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
if (got_SIGHUP)
|
|
|
|
{
|
|
|
|
got_SIGHUP = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
2011-03-19 02:43:45 +01:00
|
|
|
/* update global shmem state for sync rep */
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
if (shutdown_requested)
|
|
|
|
{
|
2006-11-30 19:29:12 +01:00
|
|
|
/*
|
|
|
|
* From here on, elog(ERROR) should end with exit(1), not send
|
|
|
|
* control back to the sigsetjmp block above
|
|
|
|
*/
|
|
|
|
ExitOnAnyError = true;
|
2004-05-30 00:48:23 +02:00
|
|
|
/* Normal exit from the bgwriter is here */
|
|
|
|
proc_exit(0); /* done */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-11-01 18:14:47 +01:00
|
|
|
* Do one cycle of dirty-buffer writing.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2012-01-26 17:19:48 +01:00
|
|
|
if (hibernating && bgwriter_lru_maxpages > 0)
|
|
|
|
ResetLatch(&MyProc->procLatch);
|
|
|
|
lapped = BgBufferSync();
|
|
|
|
|
|
|
|
if (lapped && !hibernating)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* BgBufferSync did nothing. Since there doesn't seem to be any
|
|
|
|
* work for the bgwriter to do, go into slower-paced
|
|
|
|
* "hibernation" mode, where we sleep for much longer times than
|
|
|
|
* bgwriter_delay says. Fewer wakeups saves electricity. If a
|
|
|
|
* backend starts dirtying pages again, it will wake us up by
|
|
|
|
* setting our latch.
|
|
|
|
*
|
|
|
|
* The latch is kept set during productive cycles where buffers
|
|
|
|
* are written, and only reset before going into a longer sleep.
|
|
|
|
* That ensures that when there's a constant trickle of activity,
|
|
|
|
* the SetLatch() calls that backends have to do will see the
|
|
|
|
* latch as already set, and are not slowed down by having to
|
|
|
|
* actually set the latch and signal us.
|
|
|
|
*/
|
|
|
|
hibernating = true;
|
2007-06-28 02:02:40 +02:00
|
|
|
|
2012-01-26 17:19:48 +01:00
|
|
|
/*
|
|
|
|
* Take one more short nap and perform one more bgwriter cycle -
|
|
|
|
* someone might've dirtied a buffer just after we finished the
|
|
|
|
* previous bgwriter cycle, while the latch was still set. If
|
|
|
|
* we still find nothing to do after this cycle, the next sleep
|
|
|
|
* will be longer.
|
|
|
|
*/
|
|
|
|
BgWriterNap(false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!lapped && hibernating)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Woken up from hibernation. Set the latch just in case it's
|
|
|
|
* not set yet (usually we wake up from hibernation because a
|
|
|
|
* backend already set the latch, but not necessarily).
|
|
|
|
*/
|
|
|
|
SetLatch(&MyProc->procLatch);
|
|
|
|
hibernating = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a short or long nap, depending on whether there was any work
|
|
|
|
* to do.
|
|
|
|
*/
|
|
|
|
BgWriterNap(hibernating);
|
2007-06-28 02:02:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BgWriterNap -- Nap for the configured time or until a signal is received.
|
2012-01-26 17:19:48 +01:00
|
|
|
*
|
|
|
|
* If 'hibernating' is false, sleeps for bgwriter_delay milliseconds.
|
|
|
|
* Otherwise sleeps longer, but also wakes up if the process latch is set.
|
2007-06-28 02:02:40 +02:00
|
|
|
*/
|
|
|
|
static void
|
2012-01-26 17:19:48 +01:00
|
|
|
BgWriterNap(bool hibernating)
|
2007-06-28 02:02:40 +02:00
|
|
|
{
|
|
|
|
long udelay;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send off activity statistics to the stats collector
|
|
|
|
*/
|
|
|
|
pgstat_send_bgwriter();
|
|
|
|
|
|
|
|
/*
|
2012-01-26 17:19:48 +01:00
|
|
|
* If there was no work to do in the previous bgwriter cycle, take a
|
|
|
|
* longer nap.
|
|
|
|
*/
|
|
|
|
if (hibernating)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We wake on a buffer being dirtied. It's possible that some
|
|
|
|
* useful work will become available for the bgwriter to do without
|
|
|
|
* a buffer actually being dirtied, like when a dirty buffer's usage
|
|
|
|
* count is decremented to zero or it's unpinned. This corner case
|
|
|
|
* is judged as too marginal to justify adding additional SetLatch()
|
|
|
|
* calls in very hot code paths, cheap though those calls may be.
|
|
|
|
*
|
|
|
|
* We still wake up periodically, so that BufferAlloc stats are
|
|
|
|
* updated reasonably promptly.
|
|
|
|
*/
|
|
|
|
int res = WaitLatch(&MyProc->procLatch,
|
|
|
|
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
|
|
|
|
BGWRITER_HIBERNATE_MS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only do a quick return if timeout was reached (or postmaster died)
|
|
|
|
* to ensure that no less than BgWriterDelay ms has passed between
|
|
|
|
* BgBufferSyncs - WaitLatch() might have returned instantaneously.
|
|
|
|
*/
|
|
|
|
if (res & (WL_TIMEOUT | WL_POSTMASTER_DEATH))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nap for the configured time.
|
2007-06-28 02:02:40 +02:00
|
|
|
*
|
|
|
|
* On some platforms, signals won't interrupt the sleep. To ensure we
|
|
|
|
* respond reasonably promptly when someone signals us, break down the
|
|
|
|
* sleep into 1-second increments, and check for interrupts after each
|
|
|
|
* nap.
|
|
|
|
*/
|
2012-01-26 17:19:48 +01:00
|
|
|
udelay = BgWriterDelay * 1000L;
|
2007-06-28 02:02:40 +02:00
|
|
|
|
|
|
|
while (udelay > 999999L)
|
|
|
|
{
|
2011-11-01 18:14:47 +01:00
|
|
|
if (got_SIGHUP || shutdown_requested)
|
2007-06-28 02:02:40 +02:00
|
|
|
break;
|
|
|
|
pg_usleep(1000000L);
|
|
|
|
udelay -= 1000000L;
|
|
|
|
}
|
|
|
|
|
2011-11-01 18:14:47 +01:00
|
|
|
if (!(got_SIGHUP || shutdown_requested))
|
2007-06-28 02:02:40 +02:00
|
|
|
pg_usleep(udelay);
|
|
|
|
}
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* signal handler routines
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bg_quickdie() occurs when signalled SIGQUIT by the postmaster.
|
|
|
|
*
|
|
|
|
* Some backend has bought the farm,
|
|
|
|
* so we need to stop what we're doing and exit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bg_quickdie(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
PG_SETMASK(&BlockSig);
|
|
|
|
|
|
|
|
/*
|
2009-05-15 17:56:39 +02:00
|
|
|
* We DO NOT want to run proc_exit() callbacks -- we're here because
|
|
|
|
* shared memory may be corrupted, so we don't want to try to clean up our
|
|
|
|
* transaction. Just nail the windows shut and get out of town. Now that
|
|
|
|
* there's an atexit callback to prevent third-party code from breaking
|
|
|
|
* things by calling exit() directly, we have to reset the callbacks
|
|
|
|
* explicitly to make this work as intended.
|
|
|
|
*/
|
|
|
|
on_exit_reset();
|
|
|
|
|
|
|
|
/*
|
2006-11-21 01:49:55 +01:00
|
|
|
* Note we do exit(2) not exit(0). This is to force the postmaster into a
|
2005-10-15 04:49:52 +02:00
|
|
|
* system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
|
|
|
|
* backend. This is necessary precisely because we don't clean up our
|
2009-05-15 17:56:39 +02:00
|
|
|
* shared memory state. (The "dead man switch" mechanism in pmsignal.c
|
2009-06-11 16:49:15 +02:00
|
|
|
* should ensure the postmaster sees this as a crash, too, but no harm in
|
|
|
|
* being doubly sure.)
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2006-11-21 01:49:55 +01:00
|
|
|
exit(2);
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGHUP: set flag to re-read config file at next convenient time */
|
|
|
|
static void
|
|
|
|
BgSigHupHandler(SIGNAL_ARGS)
|
|
|
|
{
|
2012-01-26 17:19:48 +01:00
|
|
|
int save_errno = errno;
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
got_SIGHUP = true;
|
2012-01-26 17:19:48 +01:00
|
|
|
if (MyProc)
|
|
|
|
SetLatch(&MyProc->procLatch);
|
|
|
|
|
|
|
|
errno = save_errno;
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
|
2011-11-01 19:48:47 +01:00
|
|
|
/* SIGTERM: set flag to shutdown and exit */
|
2004-05-30 00:48:23 +02:00
|
|
|
static void
|
|
|
|
ReqShutdownHandler(SIGNAL_ARGS)
|
|
|
|
{
|
2012-01-26 17:19:48 +01:00
|
|
|
int save_errno = errno;
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
shutdown_requested = true;
|
2012-01-26 17:19:48 +01:00
|
|
|
if (MyProc)
|
|
|
|
SetLatch(&MyProc->procLatch);
|
|
|
|
|
|
|
|
errno = save_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGUSR1: used for latch wakeups */
|
|
|
|
static void
|
|
|
|
bgwriter_sigusr1_handler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
int save_errno = errno;
|
|
|
|
|
|
|
|
latch_sigusr1_handler();
|
|
|
|
|
|
|
|
errno = save_errno;
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|