*** contrib/pg_standby/pg_standby.c.orig	2007-11-15 16:14:30.000000000 -0500
--- contrib/pg_standby/pg_standby.c	2008-04-02 12:30:34.000000000 -0400
***************
*** 55,60 ****
--- 55,61 ----
  
  char	   *archiveLocation;	/* where to find the archive? */
  char	   *triggerPath;		/* where to find the trigger file? */
+ char	   *auxiliaryCommand;		/* run this auxiliary command before the restore command */
  char	   *xlogFilePath;		/* where we are going to restore to */
  char	   *nextWALFileName;	/* the file we need to get from archive */
  char	   *restartWALFileName; /* the file from which we can restart restore */
***************
*** 375,380 ****
--- 376,421 ----
  }
  
  /*
+  * RunAuxiliaryCommand()
+  *
+  *	  Perform the auxiliary action required before restoring the file from archive
+  */
+ static bool
+ RunAuxiliaryCommand(void)
+ {
+ 	int			rc = 0;
+ 	int			numretries = 0;
+ 
+ 	if (debug)
+ 	{
+ 		fprintf(stderr, "\nrunning aux command	:");
+ 		fflush(stderr);
+ 	}
+ 
+ 	while (numretries < maxretries)
+ 	{
+ 		rc = system(auxiliaryCommand);
+ 		if (rc == 0)
+ 		{
+ 			if (debug)
+ 			{
+ 				fprintf(stderr, " OK");
+ 				fflush(stderr);
+ 			}
+ 			return true;
+ 		}
+ 		pg_usleep(numretries++ * sleeptime * 1000000L);
+ 	}
+ 
+ 	/*
+ 	 * Log the failure, but allow caller to add additional info
+ 	 */
+ 	if (debug)
+ 		fprintf(stderr, "execution not successful	: ");
+ 	return false;
+ }
+ 
+ /*
   * RestoreWALFileForRecovery()
   *
   *	  Perform the action required to restore the file from archive
***************
*** 433,438 ****
--- 474,481 ----
  	fprintf(stderr, "  -s SLEEPTIME		seconds to wait between file checks (min=1, max=60, default=5)\n");
  	fprintf(stderr, "  -t TRIGGERFILE	defines a trigger file to initiate failover (no default)\n");
  	fprintf(stderr, "  -w MAXWAITTIME	max seconds to wait for a file (0=no limit)(default=0)\n");
+ 	fprintf(stderr, "  -x AUXILIARYCOMMAND	after the NEXTWALFILE is available, this command (if specified) must run\n");
+ 	fprintf(stderr, "			successfully (retry if necessary) before the copy/link is executed (no default)\n");
  	fflush(stderr);
  }
  
***************
*** 451,457 ****
  	(void) signal(SIGINT, sighandler);
  	(void) signal(SIGQUIT, sighandler);
  
! 	while ((c = getopt(argc, argv, "cdk:lr:s:t:w:")) != -1)
  	{
  		switch (c)
  		{
--- 494,500 ----
  	(void) signal(SIGINT, sighandler);
  	(void) signal(SIGQUIT, sighandler);
  
! 	while ((c = getopt(argc, argv, "cdk:lr:s:t:w:x:")) != -1)
  	{
  		switch (c)
  		{
***************
*** 505,510 ****
--- 548,556 ----
  					exit(2);
  				}
  				break;
+ 			case 'x':			/* Auxiliary command */
+ 				auxiliaryCommand = optarg;
+ 				break;
  			default:
  				usage();
  				exit(2);
***************
*** 583,588 ****
--- 629,635 ----
  				sleeptime, (sleeptime > 1 ? "s" : " "));
  		fprintf(stderr, "\nMax wait interval	: %d %s",
  				maxwaittime, (maxwaittime > 0 ? "seconds" : "forever"));
+ 		fprintf(stderr, "\nAuxiliary command	: %s", auxiliaryCommand ? auxiliaryCommand : "<not set>");
  		fprintf(stderr, "\nCommand for restore	: %s", restoreCommand);
  		fprintf(stderr, "\nKeep archive history	: %s and later", exclusiveCleanupFileName);
  		fflush(stderr);
***************
*** 662,667 ****
--- 709,717 ----
  	 * of them will be requested again immediately after the failed restore,
  	 * or when we restart recovery.
  	 */
+ 	if (auxiliaryCommand)
+ 		RunAuxiliaryCommand();
+ 	
  	if (RestoreWALFileForRecovery() && need_cleanup)
  		CustomizableCleanupPriorWALFiles();
  
