From 60fe73b79ce8a65ee4eda34a2f4f204a95081603 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Wed, 9 Sep 2015 22:51:44 +0900 Subject: [PATCH] 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 --- src/backend/access/transam/xlog.c | 9 +++++++++ src/backend/postmaster/postmaster.c | 21 +++++++++++++++++++++ src/include/access/xlog.h | 1 + 3 files changed, 31 insertions(+) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 967e0c59efd..3e619c81e25 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -10806,6 +10806,15 @@ CheckForStandbyTrigger(void) return false; } +/* + * Remove the files signaling a standby promotion request. + */ +void +RemovePromoteSignalFiles(void) +{ + unlink(PROMOTE_SIGNAL_FILE); +} + /* * Check to see if a promote request has arrived. Should be * called by postmaster after receiving SIGUSR1. diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 80e3b6f2599..94ba381b0f0 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -1087,6 +1087,27 @@ PostmasterMain(int argc, char *argv[]) */ RemovePgTempFiles(); + /* + * Forcibly remove the files signaling a standby promotion + * request. Otherwise, the existence of those files triggers + * 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. + * + * Note that promotion signal files need to be removed before + * the startup process is invoked. Because, after that, they can + * be used by postmaster's SIGUSR1 signal handler. + */ + RemovePromoteSignalFiles(); + /* * If enabled, start up syslogger collection subprocess */ diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h index 8250254fa30..49bb5c4aac7 100644 --- a/src/include/access/xlog.h +++ b/src/include/access/xlog.h @@ -319,6 +319,7 @@ extern XLogRecPtr GetInsertRecPtr(void); extern XLogRecPtr GetFlushRecPtr(void); extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch); extern TimeLineID GetRecoveryTargetTLI(void); +extern void RemovePromoteSignalFiles(void); extern void HandleStartupProcInterrupts(void); extern void StartupProcessMain(void); -- 2.39.5