Skip to content

Commit 4738773

Browse files
committed
Remove files signaling a standby promotion request at postmaster startup
This commit makes postmaster forcibly remove the files signaling a standby promotion request. Otherwise, the existence of those files can trigger a promotion too early, whether a user wants that or not. This removal of files is usually unnecessary because they can exist only during a few moments during a standby promotion. However there is a race condition: if pg_ctl promote is executed and creates the files during a promotion, the files can stay around even after the server is brought up to new master. Then, if new standby starts by using the backup taken from that master, the files can exist at the server startup and should be removed in order to avoid an unexpected promotion. Back-patch to 9.1 where promote signal file was introduced. Problem reported by Feike Steenbergen. Original patch by Michael Paquier, modified by me. Discussion: 20150528100705.4686.91426@wrigleys.postgresql.org
1 parent cb1b9b9 commit 4738773

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/backend/access/transam/xlog.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10152,6 +10152,16 @@ CheckForStandbyTrigger(void)
1015210152
return false;
1015310153
}
1015410154

10155+
/*
10156+
* Remove the files signaling a standby promotion request.
10157+
*/
10158+
void
10159+
RemovePromoteSignalFiles(void)
10160+
{
10161+
unlink(PROMOTE_SIGNAL_FILE);
10162+
unlink(FALLBACK_PROMOTE_SIGNAL_FILE);
10163+
}
10164+
1015510165
/*
1015610166
* Check to see if a promote request has arrived. Should be
1015710167
* called by postmaster after receiving SIGUSR1.

src/backend/postmaster/postmaster.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,27 @@ PostmasterMain(int argc, char *argv[])
11981198
*/
11991199
RemovePgTempFiles();
12001200

1201+
/*
1202+
* Forcibly remove the files signaling a standby promotion
1203+
* request. Otherwise, the existence of those files triggers
1204+
* a promotion too early, whether a user wants that or not.
1205+
*
1206+
* This removal of files is usually unnecessary because they
1207+
* can exist only during a few moments during a standby
1208+
* promotion. However there is a race condition: if pg_ctl promote
1209+
* is executed and creates the files during a promotion,
1210+
* the files can stay around even after the server is brought up
1211+
* to new master. Then, if new standby starts by using the backup
1212+
* taken from that master, the files can exist at the server
1213+
* startup and should be removed in order to avoid an unexpected
1214+
* promotion.
1215+
*
1216+
* Note that promotion signal files need to be removed before
1217+
* the startup process is invoked. Because, after that, they can
1218+
* be used by postmaster's SIGUSR1 signal handler.
1219+
*/
1220+
RemovePromoteSignalFiles();
1221+
12011222
/*
12021223
* If enabled, start up syslogger collection subprocess
12031224
*/

src/include/access/xlog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ extern XLogRecPtr GetRedoRecPtr(void);
316316
extern XLogRecPtr GetInsertRecPtr(void);
317317
extern XLogRecPtr GetFlushRecPtr(void);
318318
extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
319+
extern void RemovePromoteSignalFiles(void);
319320

320321
extern bool CheckPromoteSignal(void);
321322
extern void WakeupRecovery(void);

0 commit comments

Comments
 (0)