Cosmetic adjustments for postmaster's handling of checkpointer.
authorTom Lane
Fri, 11 May 2012 21:46:08 +0000 (17:46 -0400)
committerTom Lane
Fri, 11 May 2012 21:46:37 +0000 (17:46 -0400)
Correct some comments, order some operations a bit more consistently.
No functional changes.

src/backend/postmaster/bgwriter.c
src/backend/postmaster/postmaster.c

index 5daf8c6177b624ad928a3fe5029e2d48d345f178..9ad7178aeb8a1ccfb9f70cfd4f18e3fc82e2d15e 100644 (file)
@@ -15,9 +15,9 @@
  * The bgwriter is started by the postmaster as soon as the startup subprocess
  * finishes, or as soon as recovery begins if we are doing archive recovery.
  * It remains alive until the postmaster commands it to terminate.
- * Normal termination is by SIGUSR2, which instructs the bgwriter to exit(0).
- * Emergency termination is by SIGQUIT; like any
- * backend, the bgwriter will simply abort and exit on SIGQUIT.
+ * Normal termination is by SIGTERM, which instructs the bgwriter to exit(0).
+ * Emergency termination is by SIGQUIT; like any 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
index d59ee68df549ef8c6d1135e7fe4148b8b37437c4..dd7dd5560faf03443df89f9216a31f2d51ad183f 100644 (file)
@@ -239,13 +239,13 @@ static bool RecoveryError = false;        /* T if WAL recovery failed */
  * hot standby during archive recovery.
  *
  * When the startup process is ready to start archive recovery, it signals the
- * postmaster, and we switch to PM_RECOVERY state. The background writer is
- * launched, while the startup process continues applying WAL. If Hot Standby
- * is enabled, then, after reaching a consistent point in WAL redo, startup
- * process signals us again, and we switch to PM_HOT_STANDBY state and
- * begin accepting connections to perform read-only queries.  When archive
- * recovery is finished, the startup process exits with exit code 0 and we
- * switch to PM_RUN state.
+ * postmaster, and we switch to PM_RECOVERY state. The background writer and
+ * checkpointer are launched, while the startup process continues applying WAL.
+ * If Hot Standby is enabled, then, after reaching a consistent point in WAL
+ * redo, startup process signals us again, and we switch to PM_HOT_STANDBY
+ * state and begin accepting connections to perform read-only queries.  When
+ * archive recovery is finished, the startup process exits with exit code 0
+ * and we switch to PM_RUN state.
  *
  * Normal child backends can only be launched when we are in PM_RUN or
  * PM_HOT_STANDBY state.  (We also allow launch of normal
@@ -1038,7 +1038,7 @@ PostmasterMain(int argc, char *argv[])
     * handling setup of child processes.  See tcop/postgres.c,
     * bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
     * postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c,
-    * postmaster/syslogger.c and postmaster/checkpointer.c
+    * postmaster/syslogger.c and postmaster/checkpointer.c.
     */
    pqinitmask();
    PG_SETMASK(&BlockSig);
@@ -1373,10 +1373,10 @@ ServerLoop(void)
        /*
         * If no background writer process is running, and we are not in a
         * state that prevents it, start one.  It doesn't matter if this
-        * fails, we'll just try again later.
+        * fails, we'll just try again later.  Likewise for the checkpointer.
         */
        if (pmState == PM_RUN || pmState == PM_RECOVERY ||
-            pmState == PM_HOT_STANDBY)
+           pmState == PM_HOT_STANDBY)
        {
            if (BgWriterPID == 0)
                BgWriterPID = StartBackgroundWriter();
@@ -1386,7 +1386,8 @@ ServerLoop(void)
 
        /*
         * Likewise, if we have lost the walwriter process, try to start a new
-        * one.
+        * one.  But this is needed only in normal operation (else we cannot
+        * be writing any new WAL).
         */
        if (WalWriterPID == 0 && pmState == PM_RUN)
            WalWriterPID = StartWalWriter();
@@ -2131,11 +2132,12 @@ pmdie(SIGNAL_ARGS)
                /* and the autovac launcher too */
                if (AutoVacPID != 0)
                    signal_child(AutoVacPID, SIGTERM);
+               /* and the bgwriter too */
+               if (BgWriterPID != 0)
+                   signal_child(BgWriterPID, SIGTERM);
                /* and the walwriter too */
                if (WalWriterPID != 0)
                    signal_child(WalWriterPID, SIGTERM);
-               if (BgWriterPID != 0)
-                   signal_child(BgWriterPID, SIGTERM);
 
                /*
                 * If we're in recovery, we can't kill the startup process
@@ -2174,13 +2176,17 @@ pmdie(SIGNAL_ARGS)
 
            if (StartupPID != 0)
                signal_child(StartupPID, SIGTERM);
-           if (WalReceiverPID != 0)
-               signal_child(WalReceiverPID, SIGTERM);
            if (BgWriterPID != 0)
                signal_child(BgWriterPID, SIGTERM);
+           if (WalReceiverPID != 0)
+               signal_child(WalReceiverPID, SIGTERM);
            if (pmState == PM_RECOVERY)
            {
-               /* only checkpointer is active in this state */
+               /*
+                * Only startup, bgwriter, and checkpointer should be active
+                * in this state; we just signaled the first two, and we don't
+                * want to kill checkpointer yet.
+                */
                pmState = PM_WAIT_BACKENDS;
            }
            else if (pmState == PM_RUN ||
@@ -2362,7 +2368,7 @@ reaper(SIGNAL_ARGS)
            }
 
            /*
-            * Crank up background tasks, if we didn't do that already
+            * Crank up the background tasks, if we didn't do that already
             * when we entered consistent recovery state.  It doesn't matter
             * if this fails, we'll just try again later.
             */
@@ -2370,13 +2376,13 @@ reaper(SIGNAL_ARGS)
                BgWriterPID = StartBackgroundWriter();
            if (CheckpointerPID == 0)
                CheckpointerPID = StartCheckpointer();
+           if (WalWriterPID == 0)
+               WalWriterPID = StartWalWriter();
 
            /*
             * Likewise, start other special children as needed.  In a restart
             * situation, some of them may be alive already.
             */
-           if (WalWriterPID == 0)
-               WalWriterPID = StartWalWriter();
            if (!IsBinaryUpgrade && AutoVacuumingActive() && AutoVacPID == 0)
                AutoVacPID = StartAutoVacLauncher();
            if (XLogArchivingActive() && PgArchPID == 0)
@@ -2392,7 +2398,9 @@ reaper(SIGNAL_ARGS)
        }
 
        /*
-        * Was it the bgwriter?
+        * Was it the bgwriter?  Normal exit can be ignored; we'll start a
+        * new one at the next iteration of the postmaster's main loop, if
+        * necessary.  Any other exit condition is treated as a crash.
         */
        if (pid == BgWriterPID)
        {
@@ -4228,13 +4236,13 @@ sigusr1_handler(SIGNAL_ARGS)
        FatalError = false;
 
        /*
-        * Crank up the background writers. It doesn't matter if this fails,
+        * Crank up the background tasks.  It doesn't matter if this fails,
         * we'll just try again later.
         */
-       Assert(CheckpointerPID == 0);
-       CheckpointerPID = StartCheckpointer();
        Assert(BgWriterPID == 0);
        BgWriterPID = StartBackgroundWriter();
+       Assert(CheckpointerPID == 0);
+       CheckpointerPID = StartCheckpointer();
 
        pmState = PM_RECOVERY;
    }