Remove files signaling a standby promotion request at postmaster startup
authorFujii Masao
Wed, 9 Sep 2015 13:51:44 +0000 (22:51 +0900)
committerFujii Masao
Wed, 9 Sep 2015 13:52:35 +0000 (22:52 +0900)
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[email protected]

src/backend/access/transam/xlog.c
src/backend/postmaster/postmaster.c
src/include/access/xlog.h

index d8876f2f0218117e13ca8a267ad98c73ee71d5db..cc845d23e20452cb41604e491f7908b6e77db8bb 100644 (file)
@@ -11336,6 +11336,16 @@ CheckForStandbyTrigger(void)
    return false;
 }
 
+/*
+ * Remove the files signaling a standby promotion request.
+ */
+void
+RemovePromoteSignalFiles(void)
+{
+   unlink(PROMOTE_SIGNAL_FILE);
+   unlink(FALLBACK_PROMOTE_SIGNAL_FILE);
+}
+
 /*
  * Check to see if a promote request has arrived. Should be
  * called by postmaster after receiving SIGUSR1.
index fceb162a3211bea8c00e9593c143f507c121374c..41dbdf4bf9eeb54ae0a774ab21fc1c1362aa55f9 100644 (file)
@@ -1170,6 +1170,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
     */
index 84b1110870821830fa0fb979b4bbaa788ca4811b..d2241498e14bd28b9bd53366016e552659dce1e3 100644 (file)
@@ -336,6 +336,7 @@ extern XLogRecPtr GetRedoRecPtr(void);
 extern XLogRecPtr GetInsertRecPtr(void);
 extern XLogRecPtr GetFlushRecPtr(void);
 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
+extern void RemovePromoteSignalFiles(void);
 
 extern bool CheckPromoteSignal(void);
 extern void WakeupRecovery(void);