Header And Logo

PostgreSQL
| The world's most advanced open source database.

walwriter.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * walwriter.c
00004  *
00005  * The WAL writer background process is new as of Postgres 8.3.  It attempts
00006  * to keep regular backends from having to write out (and fsync) WAL pages.
00007  * Also, it guarantees that transaction commit records that weren't synced
00008  * to disk immediately upon commit (ie, were "asynchronously committed")
00009  * will reach disk within a knowable time --- which, as it happens, is at
00010  * most three times the wal_writer_delay cycle time.
00011  *
00012  * Note that as with the bgwriter for shared buffers, regular backends are
00013  * still empowered to issue WAL writes and fsyncs when the walwriter doesn't
00014  * keep up. This means that the WALWriter is not an essential process and
00015  * can shutdown quickly when requested.
00016  *
00017  * Because the walwriter's cycle is directly linked to the maximum delay
00018  * before async-commit transactions are guaranteed committed, it's probably
00019  * unwise to load additional functionality onto it.  For instance, if you've
00020  * got a yen to create xlog segments further in advance, that'd be better done
00021  * in bgwriter than in walwriter.
00022  *
00023  * The walwriter is started by the postmaster as soon as the startup subprocess
00024  * finishes.  It remains alive until the postmaster commands it to terminate.
00025  * Normal termination is by SIGTERM, which instructs the walwriter to exit(0).
00026  * Emergency termination is by SIGQUIT; like any backend, the walwriter will
00027  * simply abort and exit on SIGQUIT.
00028  *
00029  * If the walwriter exits unexpectedly, the postmaster treats that the same
00030  * as a backend crash: shared memory may be corrupted, so remaining backends
00031  * should be killed by SIGQUIT and then a recovery cycle started.
00032  *
00033  *
00034  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00035  *
00036  *
00037  * IDENTIFICATION
00038  *    src/backend/postmaster/walwriter.c
00039  *
00040  *-------------------------------------------------------------------------
00041  */
00042 #include "postgres.h"
00043 
00044 #include <signal.h>
00045 #include <sys/time.h>
00046 #include <time.h>
00047 #include <unistd.h>
00048 
00049 #include "access/xlog.h"
00050 #include "libpq/pqsignal.h"
00051 #include "miscadmin.h"
00052 #include "postmaster/walwriter.h"
00053 #include "storage/bufmgr.h"
00054 #include "storage/fd.h"
00055 #include "storage/ipc.h"
00056 #include "storage/lwlock.h"
00057 #include "storage/proc.h"
00058 #include "storage/smgr.h"
00059 #include "utils/guc.h"
00060 #include "utils/hsearch.h"
00061 #include "utils/memutils.h"
00062 #include "utils/resowner.h"
00063 
00064 
00065 /*
00066  * GUC parameters
00067  */
00068 int         WalWriterDelay = 200;
00069 
00070 /*
00071  * Number of do-nothing loops before lengthening the delay time, and the
00072  * multiplier to apply to WalWriterDelay when we do decide to hibernate.
00073  * (Perhaps these need to be configurable?)
00074  */
00075 #define LOOPS_UNTIL_HIBERNATE       50
00076 #define HIBERNATE_FACTOR            25
00077 
00078 /*
00079  * Flags set by interrupt handlers for later service in the main loop.
00080  */
00081 static volatile sig_atomic_t got_SIGHUP = false;
00082 static volatile sig_atomic_t shutdown_requested = false;
00083 
00084 /* Signal handlers */
00085 static void wal_quickdie(SIGNAL_ARGS);
00086 static void WalSigHupHandler(SIGNAL_ARGS);
00087 static void WalShutdownHandler(SIGNAL_ARGS);
00088 static void walwriter_sigusr1_handler(SIGNAL_ARGS);
00089 
00090 /*
00091  * Main entry point for walwriter process
00092  *
00093  * This is invoked from AuxiliaryProcessMain, which has already created the
00094  * basic execution environment, but not enabled signals yet.
00095  */
00096 void
00097 WalWriterMain(void)
00098 {
00099     sigjmp_buf  local_sigjmp_buf;
00100     MemoryContext walwriter_context;
00101     int         left_till_hibernate;
00102     bool        hibernating;
00103 
00104     /*
00105      * If possible, make this process a group leader, so that the postmaster
00106      * can signal any child processes too.  (walwriter probably never has any
00107      * child processes, but for consistency we make all postmaster child
00108      * processes do this.)
00109      */
00110 #ifdef HAVE_SETSID
00111     if (setsid() < 0)
00112         elog(FATAL, "setsid() failed: %m");
00113 #endif
00114 
00115     /*
00116      * Properly accept or ignore signals the postmaster might send us
00117      *
00118      * We have no particular use for SIGINT at the moment, but seems
00119      * reasonable to treat like SIGTERM.
00120      */
00121     pqsignal(SIGHUP, WalSigHupHandler); /* set flag to read config file */
00122     pqsignal(SIGINT, WalShutdownHandler);       /* request shutdown */
00123     pqsignal(SIGTERM, WalShutdownHandler);      /* request shutdown */
00124     pqsignal(SIGQUIT, wal_quickdie);    /* hard crash time */
00125     pqsignal(SIGALRM, SIG_IGN);
00126     pqsignal(SIGPIPE, SIG_IGN);
00127     pqsignal(SIGUSR1, walwriter_sigusr1_handler);
00128     pqsignal(SIGUSR2, SIG_IGN); /* not used */
00129 
00130     /*
00131      * Reset some signals that are accepted by postmaster but not here
00132      */
00133     pqsignal(SIGCHLD, SIG_DFL);
00134     pqsignal(SIGTTIN, SIG_DFL);
00135     pqsignal(SIGTTOU, SIG_DFL);
00136     pqsignal(SIGCONT, SIG_DFL);
00137     pqsignal(SIGWINCH, SIG_DFL);
00138 
00139     /* We allow SIGQUIT (quickdie) at all times */
00140     sigdelset(&BlockSig, SIGQUIT);
00141 
00142     /*
00143      * Create a resource owner to keep track of our resources (not clear that
00144      * we need this, but may as well have one).
00145      */
00146     CurrentResourceOwner = ResourceOwnerCreate(NULL, "Wal Writer");
00147 
00148     /*
00149      * Create a memory context that we will do all our work in.  We do this so
00150      * that we can reset the context during error recovery and thereby avoid
00151      * possible memory leaks.  Formerly this code just ran in
00152      * TopMemoryContext, but resetting that would be a really bad idea.
00153      */
00154     walwriter_context = AllocSetContextCreate(TopMemoryContext,
00155                                               "Wal Writer",
00156                                               ALLOCSET_DEFAULT_MINSIZE,
00157                                               ALLOCSET_DEFAULT_INITSIZE,
00158                                               ALLOCSET_DEFAULT_MAXSIZE);
00159     MemoryContextSwitchTo(walwriter_context);
00160 
00161     /*
00162      * If an exception is encountered, processing resumes here.
00163      *
00164      * This code is heavily based on bgwriter.c, q.v.
00165      */
00166     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
00167     {
00168         /* Since not using PG_TRY, must reset error stack by hand */
00169         error_context_stack = NULL;
00170 
00171         /* Prevent interrupts while cleaning up */
00172         HOLD_INTERRUPTS();
00173 
00174         /* Report the error to the server log */
00175         EmitErrorReport();
00176 
00177         /*
00178          * These operations are really just a minimal subset of
00179          * AbortTransaction().  We don't have very many resources to worry
00180          * about in walwriter, but we do have LWLocks, and perhaps buffers?
00181          */
00182         LWLockReleaseAll();
00183         AbortBufferIO();
00184         UnlockBuffers();
00185         /* buffer pins are released here: */
00186         ResourceOwnerRelease(CurrentResourceOwner,
00187                              RESOURCE_RELEASE_BEFORE_LOCKS,
00188                              false, true);
00189         /* we needn't bother with the other ResourceOwnerRelease phases */
00190         AtEOXact_Buffers(false);
00191         AtEOXact_SMgr();
00192         AtEOXact_Files();
00193         AtEOXact_HashTables(false);
00194 
00195         /*
00196          * Now return to normal top-level context and clear ErrorContext for
00197          * next time.
00198          */
00199         MemoryContextSwitchTo(walwriter_context);
00200         FlushErrorState();
00201 
00202         /* Flush any leaked data in the top-level context */
00203         MemoryContextResetAndDeleteChildren(walwriter_context);
00204 
00205         /* Now we can allow interrupts again */
00206         RESUME_INTERRUPTS();
00207 
00208         /*
00209          * Sleep at least 1 second after any error.  A write error is likely
00210          * to be repeated, and we don't want to be filling the error logs as
00211          * fast as we can.
00212          */
00213         pg_usleep(1000000L);
00214 
00215         /*
00216          * Close all open files after any error.  This is helpful on Windows,
00217          * where holding deleted files open causes various strange errors.
00218          * It's not clear we need it elsewhere, but shouldn't hurt.
00219          */
00220         smgrcloseall();
00221     }
00222 
00223     /* We can now handle ereport(ERROR) */
00224     PG_exception_stack = &local_sigjmp_buf;
00225 
00226     /*
00227      * Unblock signals (they were blocked when the postmaster forked us)
00228      */
00229     PG_SETMASK(&UnBlockSig);
00230 
00231     /*
00232      * Reset hibernation state after any error.
00233      */
00234     left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
00235     hibernating = false;
00236     SetWalWriterSleeping(false);
00237 
00238     /*
00239      * Advertise our latch that backends can use to wake us up while we're
00240      * sleeping.
00241      */
00242     ProcGlobal->walwriterLatch = &MyProc->procLatch;
00243 
00244     /*
00245      * Loop forever
00246      */
00247     for (;;)
00248     {
00249         long        cur_timeout;
00250         int         rc;
00251 
00252         /*
00253          * Advertise whether we might hibernate in this cycle.  We do this
00254          * before resetting the latch to ensure that any async commits will
00255          * see the flag set if they might possibly need to wake us up, and
00256          * that we won't miss any signal they send us.  (If we discover work
00257          * to do in the last cycle before we would hibernate, the global flag
00258          * will be set unnecessarily, but little harm is done.)  But avoid
00259          * touching the global flag if it doesn't need to change.
00260          */
00261         if (hibernating != (left_till_hibernate <= 1))
00262         {
00263             hibernating = (left_till_hibernate <= 1);
00264             SetWalWriterSleeping(hibernating);
00265         }
00266 
00267         /* Clear any already-pending wakeups */
00268         ResetLatch(&MyProc->procLatch);
00269 
00270         /*
00271          * Process any requests or signals received recently.
00272          */
00273         if (got_SIGHUP)
00274         {
00275             got_SIGHUP = false;
00276             ProcessConfigFile(PGC_SIGHUP);
00277         }
00278         if (shutdown_requested)
00279         {
00280             /* Normal exit from the walwriter is here */
00281             proc_exit(0);       /* done */
00282         }
00283 
00284         /*
00285          * Do what we're here for; then, if XLogBackgroundFlush() found useful
00286          * work to do, reset hibernation counter.
00287          */
00288         if (XLogBackgroundFlush())
00289             left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
00290         else if (left_till_hibernate > 0)
00291             left_till_hibernate--;
00292 
00293         /*
00294          * Sleep until we are signaled or WalWriterDelay has elapsed.  If we
00295          * haven't done anything useful for quite some time, lengthen the
00296          * sleep time so as to reduce the server's idle power consumption.
00297          */
00298         if (left_till_hibernate > 0)
00299             cur_timeout = WalWriterDelay;       /* in ms */
00300         else
00301             cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
00302 
00303         rc = WaitLatch(&MyProc->procLatch,
00304                        WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
00305                        cur_timeout);
00306 
00307         /*
00308          * Emergency bailout if postmaster has died.  This is to avoid the
00309          * necessity for manual cleanup of all postmaster children.
00310          */
00311         if (rc & WL_POSTMASTER_DEATH)
00312             exit(1);
00313     }
00314 }
00315 
00316 
00317 /* --------------------------------
00318  *      signal handler routines
00319  * --------------------------------
00320  */
00321 
00322 /*
00323  * wal_quickdie() occurs when signalled SIGQUIT by the postmaster.
00324  *
00325  * Some backend has bought the farm,
00326  * so we need to stop what we're doing and exit.
00327  */
00328 static void
00329 wal_quickdie(SIGNAL_ARGS)
00330 {
00331     PG_SETMASK(&BlockSig);
00332 
00333     /*
00334      * We DO NOT want to run proc_exit() callbacks -- we're here because
00335      * shared memory may be corrupted, so we don't want to try to clean up our
00336      * transaction.  Just nail the windows shut and get out of town.  Now that
00337      * there's an atexit callback to prevent third-party code from breaking
00338      * things by calling exit() directly, we have to reset the callbacks
00339      * explicitly to make this work as intended.
00340      */
00341     on_exit_reset();
00342 
00343     /*
00344      * Note we do exit(2) not exit(0).  This is to force the postmaster into a
00345      * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
00346      * backend.  This is necessary precisely because we don't clean up our
00347      * shared memory state.  (The "dead man switch" mechanism in pmsignal.c
00348      * should ensure the postmaster sees this as a crash, too, but no harm in
00349      * being doubly sure.)
00350      */
00351     exit(2);
00352 }
00353 
00354 /* SIGHUP: set flag to re-read config file at next convenient time */
00355 static void
00356 WalSigHupHandler(SIGNAL_ARGS)
00357 {
00358     int         save_errno = errno;
00359 
00360     got_SIGHUP = true;
00361     if (MyProc)
00362         SetLatch(&MyProc->procLatch);
00363 
00364     errno = save_errno;
00365 }
00366 
00367 /* SIGTERM: set flag to exit normally */
00368 static void
00369 WalShutdownHandler(SIGNAL_ARGS)
00370 {
00371     int         save_errno = errno;
00372 
00373     shutdown_requested = true;
00374     if (MyProc)
00375         SetLatch(&MyProc->procLatch);
00376 
00377     errno = save_errno;
00378 }
00379 
00380 /* SIGUSR1: used for latch wakeups */
00381 static void
00382 walwriter_sigusr1_handler(SIGNAL_ARGS)
00383 {
00384     int         save_errno = errno;
00385 
00386     latch_sigusr1_handler();
00387 
00388     errno = save_errno;
00389 }