Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5f60086

Browse files
committed
Minor adjustments to make failures in startup/shutdown behave more cleanly.
StartupXLOG and ShutdownXLOG no longer need to be critical sections, because in all contexts where they are invoked, elog(ERROR) would be translated to elog(FATAL) anyway. (One change in bgwriter.c is needed to make this true: set ExitOnAnyError before trying to exit. This is a good fix anyway since the existing code would have gone into an infinite loop on elog(ERROR) during shutdown.) That avoids a misleading report of PANIC during semi-orderly failures. Modify the postmaster to include the startup process in the set of processes that get SIGTERM when a fast shutdown is requested, and also fix it to not try to restart the bgwriter if the bgwriter fails while trying to write the shutdown checkpoint. Net result is that "pg_ctl stop -m fast" does something reasonable for a system in warm standby mode, and so should Unix system shutdown (ie, universal SIGTERM). Per gripe from Stephen Harris and some corner-case testing of my own.
1 parent ef148d6 commit 5f60086

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/backend/access/transam/xlog.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.257 2006/11/21 20:59:52 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.258 2006/11/30 18:29:11 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -4651,8 +4651,6 @@ StartupXLOG(void)
46514651
uint32 freespace;
46524652
TransactionId oldestActiveXID;
46534653

4654-
CritSectionCount++;
4655-
46564654
/*
46574655
* Read control file and check XLOG status looks valid.
46584656
*
@@ -5188,7 +5186,6 @@ StartupXLOG(void)
51885186

51895187
ereport(LOG,
51905188
(errmsg("database system is ready")));
5191-
CritSectionCount--;
51925189

51935190
/* Shut down readFile facility, free space */
51945191
if (readFile >= 0)
@@ -5426,12 +5423,10 @@ ShutdownXLOG(int code, Datum arg)
54265423
ereport(LOG,
54275424
(errmsg("shutting down")));
54285425

5429-
CritSectionCount++;
54305426
CreateCheckPoint(true, true);
54315427
ShutdownCLOG();
54325428
ShutdownSUBTRANS();
54335429
ShutdownMultiXact();
5434-
CritSectionCount--;
54355430

54365431
ereport(LOG,
54375432
(errmsg("database system is shut down")));
@@ -5605,10 +5600,7 @@ CreateCheckPoint(bool shutdown, bool force)
56055600
*
56065601
* This I/O could fail for various reasons. If so, we will fail to
56075602
* complete the checkpoint, but there is no reason to force a system
5608-
* panic. Accordingly, exit critical section while doing it. (If we are
5609-
* doing a shutdown checkpoint, we probably *should* panic --- but that
5610-
* will happen anyway because we'll still be inside the critical section
5611-
* established by ShutdownXLOG.)
5603+
* panic. Accordingly, exit critical section while doing it.
56125604
*/
56135605
END_CRIT_SECTION();
56145606

src/backend/postmaster/bgwriter.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.31 2006/11/21 20:59:52 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.32 2006/11/30 18:29:12 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -350,6 +350,12 @@ BackgroundWriterMain(void)
350350
}
351351
if (shutdown_requested)
352352
{
353+
/*
354+
* From here on, elog(ERROR) should end with exit(1), not send
355+
* control back to the sigsetjmp block above
356+
*/
357+
ExitOnAnyError = true;
358+
/* Close down the database */
353359
ShutdownXLOG(0, 0);
354360
DumpFreeSpaceMap(0, 0);
355361
/* Normal exit from the bgwriter is here */

src/backend/postmaster/postmaster.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.504 2006/11/28 12:54:41 petere Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.505 2006/11/30 18:29:12 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -1934,8 +1934,13 @@ pmdie(SIGNAL_ARGS)
19341934
* Note: if we previously got SIGTERM then we may send SIGUSR2 to
19351935
* the bgwriter a second time here. This should be harmless.
19361936
*/
1937-
if (StartupPID != 0 || FatalError)
1938-
break; /* let reaper() handle this */
1937+
if (StartupPID != 0)
1938+
{
1939+
signal_child(StartupPID, SIGTERM);
1940+
break; /* let reaper() do the rest */
1941+
}
1942+
if (FatalError)
1943+
break; /* let reaper() handle this case */
19391944
/* Start the bgwriter if not running */
19401945
if (BgWriterPID == 0)
19411946
BgWriterPID = StartBackgroundWriter();
@@ -2108,6 +2113,21 @@ reaper(SIGNAL_ARGS)
21082113
*/
21092114
HandleChildCrash(pid, exitstatus,
21102115
_("background writer process"));
2116+
2117+
/*
2118+
* If the bgwriter crashed while trying to write the shutdown
2119+
* checkpoint, we may as well just stop here; any recovery
2120+
* required will happen on next postmaster start.
2121+
*/
2122+
if (Shutdown > NoShutdown &&
2123+
!DLGetHead(BackendList) && AutoVacPID == 0)
2124+
{
2125+
ereport(LOG,
2126+
(errmsg("abnormal database system shutdown")));
2127+
ExitPostmaster(1);
2128+
}
2129+
2130+
/* Else, proceed as in normal crash recovery */
21112131
continue;
21122132
}
21132133

0 commit comments

Comments
 (0)