Skip to content

Commit 2244c06

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 ed47666 commit 2244c06

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/backend/access/transam/xlog.c

+10
Original file line numberDiff line numberDiff line change
@@ -11336,6 +11336,16 @@ CheckForStandbyTrigger(void)
1133611336
return false;
1133711337
}
1133811338

11339+
/*
11340+
* Remove the files signaling a standby promotion request.
11341+
*/
11342+
void
11343+
RemovePromoteSignalFiles(void)
11344+
{
11345+
unlink(PROMOTE_SIGNAL_FILE);
11346+
unlink(FALLBACK_PROMOTE_SIGNAL_FILE);
11347+
}
11348+
1133911349
/*
1134011350
* Check to see if a promote request has arrived. Should be
1134111351
* called by postmaster after receiving SIGUSR1.

src/backend/postmaster/postmaster.c

+21
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,27 @@ PostmasterMain(int argc, char *argv[])
11701170
*/
11711171
RemovePgTempFiles();
11721172

1173+
/*
1174+
* Forcibly remove the files signaling a standby promotion
1175+
* request. Otherwise, the existence of those files triggers
1176+
* a promotion too early, whether a user wants that or not.
1177+
*
1178+
* This removal of files is usually unnecessary because they
1179+
* can exist only during a few moments during a standby
1180+
* promotion. However there is a race condition: if pg_ctl promote
1181+
* is executed and creates the files during a promotion,
1182+
* the files can stay around even after the server is brought up
1183+
* to new master. Then, if new standby starts by using the backup
1184+
* taken from that master, the files can exist at the server
1185+
* startup and should be removed in order to avoid an unexpected
1186+
* promotion.
1187+
*
1188+
* Note that promotion signal files need to be removed before
1189+
* the startup process is invoked. Because, after that, they can
1190+
* be used by postmaster's SIGUSR1 signal handler.
1191+
*/
1192+
RemovePromoteSignalFiles();
1193+
11731194
/*
11741195
* If enabled, start up syslogger collection subprocess
11751196
*/

src/include/access/xlog.h

+1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ extern XLogRecPtr GetRedoRecPtr(void);
336336
extern XLogRecPtr GetInsertRecPtr(void);
337337
extern XLogRecPtr GetFlushRecPtr(void);
338338
extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
339+
extern void RemovePromoteSignalFiles(void);
339340

340341
extern bool CheckPromoteSignal(void);
341342
extern void WakeupRecovery(void);

0 commit comments

Comments
 (0)