LCOV - code coverage report
Current view: top level - src/backend/postmaster - walwriter.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 34 53 64.2 %
Date: 2020-05-29 01:06:25 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * walwriter.c
       4             :  *
       5             :  * The WAL writer background process is new as of Postgres 8.3.  It attempts
       6             :  * to keep regular backends from having to write out (and fsync) WAL pages.
       7             :  * Also, it guarantees that transaction commit records that weren't synced
       8             :  * to disk immediately upon commit (ie, were "asynchronously committed")
       9             :  * will reach disk within a knowable time --- which, as it happens, is at
      10             :  * most three times the wal_writer_delay cycle time.
      11             :  *
      12             :  * Note that as with the bgwriter for shared buffers, regular backends are
      13             :  * still empowered to issue WAL writes and fsyncs when the walwriter doesn't
      14             :  * keep up. This means that the WALWriter is not an essential process and
      15             :  * can shutdown quickly when requested.
      16             :  *
      17             :  * Because the walwriter's cycle is directly linked to the maximum delay
      18             :  * before async-commit transactions are guaranteed committed, it's probably
      19             :  * unwise to load additional functionality onto it.  For instance, if you've
      20             :  * got a yen to create xlog segments further in advance, that'd be better done
      21             :  * in bgwriter than in walwriter.
      22             :  *
      23             :  * The walwriter is started by the postmaster as soon as the startup subprocess
      24             :  * finishes.  It remains alive until the postmaster commands it to terminate.
      25             :  * Normal termination is by SIGTERM, which instructs the walwriter to exit(0).
      26             :  * Emergency termination is by SIGQUIT; like any backend, the walwriter will
      27             :  * simply abort and exit on SIGQUIT.
      28             :  *
      29             :  * If the walwriter exits unexpectedly, the postmaster treats that the same
      30             :  * as a backend crash: shared memory may be corrupted, so remaining backends
      31             :  * should be killed by SIGQUIT and then a recovery cycle started.
      32             :  *
      33             :  *
      34             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
      35             :  *
      36             :  *
      37             :  * IDENTIFICATION
      38             :  *    src/backend/postmaster/walwriter.c
      39             :  *
      40             :  *-------------------------------------------------------------------------
      41             :  */
      42             : #include "postgres.h"
      43             : 
      44             : #include <signal.h>
      45             : #include <unistd.h>
      46             : 
      47             : #include "access/xlog.h"
      48             : #include "libpq/pqsignal.h"
      49             : #include "miscadmin.h"
      50             : #include "pgstat.h"
      51             : #include "postmaster/interrupt.h"
      52             : #include "postmaster/walwriter.h"
      53             : #include "storage/bufmgr.h"
      54             : #include "storage/condition_variable.h"
      55             : #include "storage/fd.h"
      56             : #include "storage/ipc.h"
      57             : #include "storage/lwlock.h"
      58             : #include "storage/proc.h"
      59             : #include "storage/procsignal.h"
      60             : #include "storage/smgr.h"
      61             : #include "utils/guc.h"
      62             : #include "utils/hsearch.h"
      63             : #include "utils/memutils.h"
      64             : #include "utils/resowner.h"
      65             : 
      66             : 
      67             : /*
      68             :  * GUC parameters
      69             :  */
      70             : int         WalWriterDelay = 200;
      71             : int         WalWriterFlushAfter = 128;
      72             : 
      73             : /*
      74             :  * Number of do-nothing loops before lengthening the delay time, and the
      75             :  * multiplier to apply to WalWriterDelay when we do decide to hibernate.
      76             :  * (Perhaps these need to be configurable?)
      77             :  */
      78             : #define LOOPS_UNTIL_HIBERNATE       50
      79             : #define HIBERNATE_FACTOR            25
      80             : 
      81             : /*
      82             :  * Main entry point for walwriter process
      83             :  *
      84             :  * This is invoked from AuxiliaryProcessMain, which has already created the
      85             :  * basic execution environment, but not enabled signals yet.
      86             :  */
      87             : void
      88         366 : WalWriterMain(void)
      89             : {
      90             :     sigjmp_buf  local_sigjmp_buf;
      91             :     MemoryContext walwriter_context;
      92             :     int         left_till_hibernate;
      93             :     bool        hibernating;
      94             : 
      95             :     /*
      96             :      * Properly accept or ignore signals the postmaster might send us
      97             :      *
      98             :      * We have no particular use for SIGINT at the moment, but seems
      99             :      * reasonable to treat like SIGTERM.
     100             :      */
     101         366 :     pqsignal(SIGHUP, SignalHandlerForConfigReload);
     102         366 :     pqsignal(SIGINT, SignalHandlerForShutdownRequest);
     103         366 :     pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
     104         366 :     pqsignal(SIGQUIT, SignalHandlerForCrashExit);
     105         366 :     pqsignal(SIGALRM, SIG_IGN);
     106         366 :     pqsignal(SIGPIPE, SIG_IGN);
     107         366 :     pqsignal(SIGUSR1, procsignal_sigusr1_handler);
     108         366 :     pqsignal(SIGUSR2, SIG_IGN); /* not used */
     109             : 
     110             :     /*
     111             :      * Reset some signals that are accepted by postmaster but not here
     112             :      */
     113         366 :     pqsignal(SIGCHLD, SIG_DFL);
     114             : 
     115             :     /* We allow SIGQUIT (quickdie) at all times */
     116         366 :     sigdelset(&BlockSig, SIGQUIT);
     117             : 
     118             :     /*
     119             :      * Create a memory context that we will do all our work in.  We do this so
     120             :      * that we can reset the context during error recovery and thereby avoid
     121             :      * possible memory leaks.  Formerly this code just ran in
     122             :      * TopMemoryContext, but resetting that would be a really bad idea.
     123             :      */
     124         366 :     walwriter_context = AllocSetContextCreate(TopMemoryContext,
     125             :                                               "Wal Writer",
     126             :                                               ALLOCSET_DEFAULT_SIZES);
     127         366 :     MemoryContextSwitchTo(walwriter_context);
     128             : 
     129             :     /*
     130             :      * If an exception is encountered, processing resumes here.
     131             :      *
     132             :      * This code is heavily based on bgwriter.c, q.v.
     133             :      */
     134         366 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
     135             :     {
     136             :         /* Since not using PG_TRY, must reset error stack by hand */
     137           0 :         error_context_stack = NULL;
     138             : 
     139             :         /* Prevent interrupts while cleaning up */
     140           0 :         HOLD_INTERRUPTS();
     141             : 
     142             :         /* Report the error to the server log */
     143           0 :         EmitErrorReport();
     144             : 
     145             :         /*
     146             :          * These operations are really just a minimal subset of
     147             :          * AbortTransaction().  We don't have very many resources to worry
     148             :          * about in walwriter, but we do have LWLocks, and perhaps buffers?
     149             :          */
     150           0 :         LWLockReleaseAll();
     151           0 :         ConditionVariableCancelSleep();
     152           0 :         pgstat_report_wait_end();
     153           0 :         AbortBufferIO();
     154           0 :         UnlockBuffers();
     155           0 :         ReleaseAuxProcessResources(false);
     156           0 :         AtEOXact_Buffers(false);
     157           0 :         AtEOXact_SMgr();
     158           0 :         AtEOXact_Files(false);
     159           0 :         AtEOXact_HashTables(false);
     160             : 
     161             :         /*
     162             :          * Now return to normal top-level context and clear ErrorContext for
     163             :          * next time.
     164             :          */
     165           0 :         MemoryContextSwitchTo(walwriter_context);
     166           0 :         FlushErrorState();
     167             : 
     168             :         /* Flush any leaked data in the top-level context */
     169           0 :         MemoryContextResetAndDeleteChildren(walwriter_context);
     170             : 
     171             :         /* Now we can allow interrupts again */
     172           0 :         RESUME_INTERRUPTS();
     173             : 
     174             :         /*
     175             :          * Sleep at least 1 second after any error.  A write error is likely
     176             :          * to be repeated, and we don't want to be filling the error logs as
     177             :          * fast as we can.
     178             :          */
     179           0 :         pg_usleep(1000000L);
     180             : 
     181             :         /*
     182             :          * Close all open files after any error.  This is helpful on Windows,
     183             :          * where holding deleted files open causes various strange errors.
     184             :          * It's not clear we need it elsewhere, but shouldn't hurt.
     185             :          */
     186           0 :         smgrcloseall();
     187             :     }
     188             : 
     189             :     /* We can now handle ereport(ERROR) */
     190         366 :     PG_exception_stack = &local_sigjmp_buf;
     191             : 
     192             :     /*
     193             :      * Unblock signals (they were blocked when the postmaster forked us)
     194             :      */
     195         366 :     PG_SETMASK(&UnBlockSig);
     196             : 
     197             :     /*
     198             :      * Reset hibernation state after any error.
     199             :      */
     200         366 :     left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
     201         366 :     hibernating = false;
     202         366 :     SetWalWriterSleeping(false);
     203             : 
     204             :     /*
     205             :      * Advertise our latch that backends can use to wake us up while we're
     206             :      * sleeping.
     207             :      */
     208         366 :     ProcGlobal->walwriterLatch = &MyProc->procLatch;
     209             : 
     210             :     /*
     211             :      * Loop forever
     212             :      */
     213             :     for (;;)
     214       17228 :     {
     215             :         long        cur_timeout;
     216             : 
     217             :         /*
     218             :          * Advertise whether we might hibernate in this cycle.  We do this
     219             :          * before resetting the latch to ensure that any async commits will
     220             :          * see the flag set if they might possibly need to wake us up, and
     221             :          * that we won't miss any signal they send us.  (If we discover work
     222             :          * to do in the last cycle before we would hibernate, the global flag
     223             :          * will be set unnecessarily, but little harm is done.)  But avoid
     224             :          * touching the global flag if it doesn't need to change.
     225             :          */
     226       17594 :         if (hibernating != (left_till_hibernate <= 1))
     227             :         {
     228          30 :             hibernating = (left_till_hibernate <= 1);
     229          30 :             SetWalWriterSleeping(hibernating);
     230             :         }
     231             : 
     232             :         /* Clear any already-pending wakeups */
     233       17594 :         ResetLatch(MyLatch);
     234             : 
     235       17594 :         HandleMainLoopInterrupts();
     236             : 
     237             :         /*
     238             :          * Do what we're here for; then, if XLogBackgroundFlush() found useful
     239             :          * work to do, reset hibernation counter.
     240             :          */
     241       17234 :         if (XLogBackgroundFlush())
     242        9472 :             left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
     243        7762 :         else if (left_till_hibernate > 0)
     244        7744 :             left_till_hibernate--;
     245             : 
     246             :         /*
     247             :          * Sleep until we are signaled or WalWriterDelay has elapsed.  If we
     248             :          * haven't done anything useful for quite some time, lengthen the
     249             :          * sleep time so as to reduce the server's idle power consumption.
     250             :          */
     251       17234 :         if (left_till_hibernate > 0)
     252       17198 :             cur_timeout = WalWriterDelay;   /* in ms */
     253             :         else
     254          36 :             cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
     255             : 
     256       17234 :         (void) WaitLatch(MyLatch,
     257             :                          WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
     258             :                          cur_timeout,
     259             :                          WAIT_EVENT_WAL_WRITER_MAIN);
     260             :     }
     261             : }

Generated by: LCOV version 1.13