On Wed, Jun 10, 2015 at 11:12 PM, Alvaro Herrera
<alvhe...@2ndquadrant.com> wrote:
> Fujii Masao wrote:
>> On Tue, Jun 9, 2015 at 5:21 AM, Alvaro Herrera <alvhe...@2ndquadrant.com> 
>> wrote:
>> > Fujii Masao wrote:
>
>> > Can't we create
>> > some common function that would be called both here and on ServerLoop?
>>
>> Agreed. So, what about the attached patch?
>
> No attachment ...

Ooops... Attached.

>> OTOH, in the other places where archiver is started up,
>> we can reach there during not only recovery but also normal processing.
>> So the conditions that we need to check are different.
>
> I think it would be simpler to centralize knowledge in a single
> function, and have that function take an argument indicating whether
> we're in recovery or normal processing, instead of spreading it to every
> place that can possibly start the archiver.

Agreed. The attached patch defines the macro to check whether archiver is
allowed to start up or not, and uses it everywhere except sigusr1_handler.
I made sigusr1_handler use a different condition because only it tries to
start archiver in PM_STARTUP postmaster state and it looks a bit messy
to add the check of that state into the centralized check condition.

Regards,

-- 
Fujii Masao
*** a/src/backend/postmaster/postmaster.c
--- b/src/backend/postmaster/postmaster.c
***************
*** 406,411 **** static pid_t StartChildProcess(AuxProcType type);
--- 406,422 ----
  static void StartAutovacuumWorker(void);
  static void InitPostmasterDeathWatchHandle(void);
  
+ /*
+  * Archiver is allowed to start up at the current postmaster state?
+  *
+  * If WAL archiving is enabled always, we are allowed to start archiver
+  * even during recovery.
+  */
+ #define PgArchStartupAllowed()	\
+ 	((XLogArchivingActive() && pmState == PM_RUN) ||	\
+ 	 (XLogArchivingStandby() &&	\
+ 	  (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY)))
+ 
  #ifdef EXEC_BACKEND
  
  #ifdef WIN32
***************
*** 1649,1669 **** ServerLoop(void)
  		if (PgStatPID == 0 && pmState == PM_RUN)
  			PgStatPID = pgstat_start();
  
! 		/*
! 		 * If we have lost the archiver, try to start a new one.
! 		 *
! 		 * If WAL archiving is enabled always, we try to start a new archiver
! 		 * even during recovery.
! 		 */
! 		if (PgArchPID == 0 && wal_level >= WAL_LEVEL_ARCHIVE)
! 		{
! 			if ((pmState == PM_RUN && XLogArchiveMode > ARCHIVE_MODE_OFF) ||
! 				((pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY) &&
! 				 XLogArchiveMode == ARCHIVE_MODE_ALWAYS))
! 			{
  				PgArchPID = pgarch_start();
- 			}
- 		}
  
  		/* If we need to signal the autovacuum launcher, do so now */
  		if (avlauncher_needs_signal)
--- 1660,1668 ----
  		if (PgStatPID == 0 && pmState == PM_RUN)
  			PgStatPID = pgstat_start();
  
! 		/* If we have lost the archiver, try to start a new one. */
! 		if (PgArchPID == 0 && PgArchStartupAllowed())
  				PgArchPID = pgarch_start();
  
  		/* If we need to signal the autovacuum launcher, do so now */
  		if (avlauncher_needs_signal)
***************
*** 2669,2675 **** reaper(SIGNAL_ARGS)
  			 */
  			if (!IsBinaryUpgrade && AutoVacuumingActive() && AutoVacPID == 0)
  				AutoVacPID = StartAutoVacLauncher();
! 			if (XLogArchivingActive() && PgArchPID == 0)
  				PgArchPID = pgarch_start();
  			if (PgStatPID == 0)
  				PgStatPID = pgstat_start();
--- 2668,2674 ----
  			 */
  			if (!IsBinaryUpgrade && AutoVacuumingActive() && AutoVacPID == 0)
  				AutoVacPID = StartAutoVacLauncher();
! 			if (PgArchStartupAllowed() && PgArchPID == 0)
  				PgArchPID = pgarch_start();
  			if (PgStatPID == 0)
  				PgStatPID = pgstat_start();
***************
*** 2810,2816 **** reaper(SIGNAL_ARGS)
  			if (!EXIT_STATUS_0(exitstatus))
  				LogChildExit(LOG, _("archiver process"),
  							 pid, exitstatus);
! 			if (XLogArchivingActive() && pmState == PM_RUN)
  				PgArchPID = pgarch_start();
  			continue;
  		}
--- 2809,2815 ----
  			if (!EXIT_STATUS_0(exitstatus))
  				LogChildExit(LOG, _("archiver process"),
  							 pid, exitstatus);
! 			if (PgArchStartupAllowed())
  				PgArchPID = pgarch_start();
  			continue;
  		}
***************
*** 4833,4843 **** sigusr1_handler(SIGNAL_ARGS)
  		 * files.
  		 */
  		Assert(PgArchPID == 0);
! 		if (wal_level >= WAL_LEVEL_ARCHIVE &&
! 			XLogArchiveMode == ARCHIVE_MODE_ALWAYS)
! 		{
  			PgArchPID = pgarch_start();
- 		}
  
  		pmState = PM_RECOVERY;
  	}
--- 4832,4839 ----
  		 * files.
  		 */
  		Assert(PgArchPID == 0);
! 		if (XLogArchivingStandby())
  			PgArchPID = pgarch_start();
  
  		pmState = PM_RECOVERY;
  	}
*** a/src/include/access/xlog.h
--- b/src/include/access/xlog.h
***************
*** 128,133 **** extern int	wal_level;
--- 128,135 ----
  
  #define XLogArchivingActive() \
  	(XLogArchiveMode > ARCHIVE_MODE_OFF && wal_level >= WAL_LEVEL_ARCHIVE)
+ #define XLogArchivingStandby() \
+ 	(XLogArchiveMode == ARCHIVE_MODE_ALWAYS && wal_level >= WAL_LEVEL_ARCHIVE)
  #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
  
  /*
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to