3737 *
3838 *
3939 * IDENTIFICATION
40- * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.365 2004/02/08 22:28:56 neilc Exp $
40+ * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.366 2004/02/11 22:25:02 tgl Exp $
4141 *
4242 * NOTES
4343 *
@@ -1079,7 +1079,8 @@ ServerLoop(void)
10791079timeout .tv_usec = 0 ;
10801080
10811081if (CheckPointPID == 0 && checkpointed &&
1082- Shutdown == NoShutdown && !FatalError && random_seed != 0 )
1082+ StartupPID == 0 && Shutdown == NoShutdown &&
1083+ !FatalError && random_seed != 0 )
10831084{
10841085time_t now = time (NULL );
10851086
@@ -1113,7 +1114,8 @@ ServerLoop(void)
11131114 * this fails, we'll just try again later.
11141115 */
11151116if (BgWriterPID == 0 && BgWriterPercent > 0 &&
1116- Shutdown == NoShutdown && !FatalError && random_seed != 0 )
1117+ StartupPID == 0 && Shutdown == NoShutdown &&
1118+ !FatalError && random_seed != 0 )
11171119{
11181120BgWriterPID = StartBackgroundWriter ();
11191121}
@@ -1772,13 +1774,16 @@ pmdie(SIGNAL_ARGS)
17721774 *
17731775 * Wait for children to end their work and ShutdownDataBase.
17741776 */
1775- if (BgWriterPID != 0 )
1776- kill (BgWriterPID ,SIGTERM );
17771777if (Shutdown >=SmartShutdown )
17781778break ;
17791779Shutdown = SmartShutdown ;
17801780ereport (LOG ,
17811781(errmsg ("received smart shutdown request" )));
1782+
1783+ /* Must tell bgwriter to quit, or it never will... */
1784+ if (BgWriterPID != 0 )
1785+ kill (BgWriterPID ,SIGTERM );
1786+
17821787if (DLGetHead (BackendList ))/* let reaper() handle this */
17831788break ;
17841789
@@ -1803,47 +1808,39 @@ pmdie(SIGNAL_ARGS)
18031808/*
18041809 * Fast Shutdown:
18051810 *
1806- *abort all children with SIGTERM (rollback active transactions
1811+ *Abort all children with SIGTERM (rollback active transactions
18071812 * and exit) and ShutdownDataBase when they are gone.
18081813 */
1809- if (BgWriterPID != 0 )
1810- kill (BgWriterPID ,SIGTERM );
18111814if (Shutdown >=FastShutdown )
18121815break ;
1816+ Shutdown = FastShutdown ;
18131817ereport (LOG ,
18141818(errmsg ("received fast shutdown request" )));
1815- if (DLGetHead (BackendList ))/* let reaper() handle this */
1819+
1820+ if (DLGetHead (BackendList ))
18161821{
1817- Shutdown = FastShutdown ;
18181822if (!FatalError )
18191823{
18201824ereport (LOG ,
18211825(errmsg ("aborting any active transactions" )));
18221826SignalChildren (SIGTERM );
1827+ /* reaper() does the rest */
18231828}
18241829break ;
18251830}
1826- if (Shutdown > NoShutdown )
1827- {
1828- Shutdown = FastShutdown ;
1829- break ;
1830- }
1831- Shutdown = FastShutdown ;
18321831
18331832/*
18341833 * No children left. Shutdown data base system.
1834+ *
1835+ * Unlike the previous case, it is not an error for the shutdown
1836+ * process to be running already (we could get SIGTERM followed
1837+ * shortly later by SIGINT).
18351838 */
18361839if (StartupPID > 0 || FatalError )/* let reaper() handle
18371840 * this */
18381841break ;
1839- if (ShutdownPID > 0 )
1840- {
1841- elog (PANIC ,"shutdown process %d already running" ,
1842- (int )ShutdownPID );
1843- abort ();
1844- }
1845-
1846- ShutdownPID = ShutdownDataBase ();
1842+ if (ShutdownPID == 0 )
1843+ ShutdownPID = ShutdownDataBase ();
18471844break ;
18481845
18491846case SIGQUIT :
@@ -1854,8 +1851,6 @@ pmdie(SIGNAL_ARGS)
18541851 * abort all children with SIGQUIT and exit without attempt to
18551852 * properly shutdown data base system.
18561853 */
1857- if (BgWriterPID != 0 )
1858- kill (BgWriterPID ,SIGQUIT );
18591854ereport (LOG ,
18601855(errmsg ("received immediate shutdown request" )));
18611856if (ShutdownPID > 0 )
@@ -1973,12 +1968,6 @@ reaper(SIGNAL_ARGS)
19731968CheckPointPID = 0 ;
19741969checkpointed = time (NULL );
19751970
1976- if (BgWriterPID == 0 && BgWriterPercent > 0 &&
1977- Shutdown == NoShutdown && !FatalError && random_seed != 0 )
1978- {
1979- BgWriterPID = StartBackgroundWriter ();
1980- }
1981-
19821971/*
19831972 * Go to shutdown mode if a shutdown request was pending.
19841973 */
@@ -2101,8 +2090,8 @@ CleanupProc(int pid,
21012090if (!FatalError )
21022091{
21032092LogChildExit (LOG ,
2104- (pid == CheckPointPID ) ?gettext ("checkpoint process" ) :
2105- (pid == BgWriterPID ) ?gettext ("bgwriter process" ) :
2093+ (pid == CheckPointPID ) ?gettext ("checkpoint process" ) :
2094+ (pid == BgWriterPID ) ?gettext ("bgwriter process" ) :
21062095gettext ("server process" ),
21072096pid ,exitstatus );
21082097ereport (LOG ,
@@ -2844,7 +2833,8 @@ sigusr1_handler(SIGNAL_ARGS)
28442833 * is currently disabled
28452834 */
28462835if (CheckPointPID == 0 && checkpointed &&
2847- Shutdown == NoShutdown && !FatalError && random_seed != 0 )
2836+ StartupPID == 0 && Shutdown == NoShutdown &&
2837+ !FatalError && random_seed != 0 )
28482838{
28492839CheckPointPID = CheckPointDataBase ();
28502840/* note: if fork fails, CheckPointPID stays 0; nothing happens */
@@ -2975,6 +2965,7 @@ CountChildren(void)
29752965if (bp -> pid != MyProcPid )
29762966cnt ++ ;
29772967}
2968+ /* Checkpoint and bgwriter will be in the list, discount them */
29782969if (CheckPointPID != 0 )
29792970cnt -- ;
29802971if (BgWriterPID != 0 )
@@ -2983,10 +2974,10 @@ CountChildren(void)
29832974}
29842975
29852976/*
2986- * Fire off a subprocess for startup/shutdown/checkpoint.
2977+ * Fire off a subprocess for startup/shutdown/checkpoint/bgwriter .
29872978 *
29882979 * Return value of SSDataBase is subprocess' PID, or 0 if failed to start subprocess
2989- * (0 is returned only for checkpoint case ).
2980+ * (0 is returned only for checkpoint/bgwriter cases ).
29902981 *
29912982 * note: in the EXEC_BACKEND case, we delay the fork until argument list has been
29922983 *established