2007-02-08 23:16:19 +08:00
|
|
|
/*
|
2008-12-16 06:13:02 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/pg_standby/pg_standby.c,v 1.16 2008/12/15 22:13:02 momjian Exp $
|
2008-05-17 09:28:26 +08:00
|
|
|
*
|
|
|
|
*
|
2007-02-08 23:16:19 +08:00
|
|
|
* pg_standby.c
|
2007-11-16 05:14:46 +08:00
|
|
|
*
|
2007-02-08 23:16:19 +08:00
|
|
|
* Production-ready example of how to create a Warm Standby
|
2007-11-16 05:14:46 +08:00
|
|
|
* database server using continuous archiving as a
|
2007-02-08 23:16:19 +08:00
|
|
|
* replication mechanism
|
|
|
|
*
|
|
|
|
* We separate the parameters for archive and nextWALfile
|
2007-11-16 05:14:46 +08:00
|
|
|
* so that we can check the archive exists, even if the
|
2007-02-08 23:16:19 +08:00
|
|
|
* WAL file doesn't (yet).
|
|
|
|
*
|
|
|
|
* This program will be executed once in full for each file
|
|
|
|
* requested by the warm standby server.
|
|
|
|
*
|
|
|
|
* It is designed to cater to a variety of needs, as well
|
|
|
|
* providing a customizable section.
|
2007-11-16 05:14:46 +08:00
|
|
|
*
|
|
|
|
* Original author: Simon Riggs simon@2ndquadrant.com
|
|
|
|
* Current maintainer: Simon Riggs
|
2007-02-08 23:16:19 +08:00
|
|
|
*/
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2007-11-16 05:14:46 +08:00
|
|
|
int getopt(int argc, char *const argv[], const char *optstring);
|
2007-02-08 23:16:19 +08:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
#endif /* ! WIN32 */
|
|
|
|
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
|
|
|
/* Options and defaults */
|
2007-11-16 05:14:46 +08:00
|
|
|
int sleeptime = 5; /* amount of time to sleep between file checks */
|
|
|
|
int waittime = -1; /* how long we have been waiting, -1 no wait
|
|
|
|
* yet */
|
|
|
|
int maxwaittime = 0; /* how long are we prepared to wait for? */
|
|
|
|
int keepfiles = 0; /* number of WAL files to keep, 0 keep all */
|
|
|
|
int maxretries = 3; /* number of retries on restore command */
|
|
|
|
bool debug = false; /* are we debugging? */
|
|
|
|
bool triggered = false; /* have we been triggered? */
|
|
|
|
bool need_cleanup = false; /* do we need to remove files from
|
|
|
|
* archive? */
|
2007-09-27 06:36:30 +08:00
|
|
|
|
|
|
|
static volatile sig_atomic_t signaled = false;
|
2007-02-08 23:16:19 +08:00
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
char *archiveLocation; /* where to find the archive? */
|
|
|
|
char *triggerPath; /* where to find the trigger file? */
|
|
|
|
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 */
|
|
|
|
char *priorWALFileName; /* the file we need to get from archive */
|
|
|
|
char WALFilePath[MAXPGPATH]; /* the file path including archive */
|
|
|
|
char restoreCommand[MAXPGPATH]; /* run this to restore */
|
|
|
|
char exclusiveCleanupFileName[MAXPGPATH]; /* the file we need to
|
|
|
|
* get from archive */
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
#define RESTORE_COMMAND_COPY 0
|
|
|
|
#define RESTORE_COMMAND_LINK 1
|
2007-11-16 05:14:46 +08:00
|
|
|
int restoreCommandType;
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
#define XLOG_DATA 0
|
|
|
|
#define XLOG_HISTORY 1
|
|
|
|
#define XLOG_BACKUP_LABEL 2
|
2007-11-16 05:14:46 +08:00
|
|
|
int nextWALFileType;
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
#define SET_RESTORE_COMMAND(cmd, arg1, arg2) \
|
2007-07-16 16:40:52 +08:00
|
|
|
snprintf(restoreCommand, MAXPGPATH, cmd " \"%s\" \"%s\"", arg1, arg2)
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
/* =====================================================================
|
|
|
|
*
|
|
|
|
* Customizable section
|
|
|
|
*
|
|
|
|
* =====================================================================
|
|
|
|
*
|
|
|
|
* Currently, this section assumes that the Archive is a locally
|
|
|
|
* accessible directory. If you want to make other assumptions,
|
|
|
|
* such as using a vendor-specific archive and access API, these
|
|
|
|
* routines are the ones you'll need to change. You're
|
2007-11-16 05:14:46 +08:00
|
|
|
* enouraged to submit any changes to pgsql-patches@postgresql.org
|
|
|
|
* or personally to the current maintainer. Those changes may be
|
2007-02-08 23:16:19 +08:00
|
|
|
* folded in to later versions of this program.
|
|
|
|
*/
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
#define XLOG_DATA_FNAME_LEN 24
|
2007-02-08 23:16:19 +08:00
|
|
|
/* Reworked from access/xlog_internal.h */
|
|
|
|
#define XLogFileName(fname, tli, log, seg) \
|
|
|
|
snprintf(fname, XLOG_DATA_FNAME_LEN + 1, "%08X%08X%08X", tli, log, seg)
|
|
|
|
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* Initialize allows customized commands into the warm standby program.
|
2007-02-08 23:16:19 +08:00
|
|
|
*
|
2007-11-16 05:14:46 +08:00
|
|
|
* As an example, and probably the common case, we use either
|
|
|
|
* cp/ln commands on *nix, or copy/move command on Windows.
|
2007-02-08 23:16:19 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CustomizableInitialize(void)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
snprintf(WALFilePath, MAXPGPATH, "%s\\%s", archiveLocation, nextWALFileName);
|
|
|
|
switch (restoreCommandType)
|
|
|
|
{
|
|
|
|
case RESTORE_COMMAND_LINK:
|
2007-11-16 05:14:46 +08:00
|
|
|
SET_RESTORE_COMMAND("mklink", WALFilePath, xlogFilePath);
|
2008-12-16 05:11:54 +08:00
|
|
|
break;
|
2007-02-08 23:16:19 +08:00
|
|
|
case RESTORE_COMMAND_COPY:
|
|
|
|
default:
|
2007-11-16 05:14:46 +08:00
|
|
|
SET_RESTORE_COMMAND("copy", WALFilePath, xlogFilePath);
|
2007-02-08 23:16:19 +08:00
|
|
|
break;
|
2007-11-16 05:14:46 +08:00
|
|
|
}
|
2007-02-08 23:16:19 +08:00
|
|
|
#else
|
|
|
|
snprintf(WALFilePath, MAXPGPATH, "%s/%s", archiveLocation, nextWALFileName);
|
|
|
|
switch (restoreCommandType)
|
|
|
|
{
|
|
|
|
case RESTORE_COMMAND_LINK:
|
|
|
|
#if HAVE_WORKING_LINK
|
2007-11-16 05:14:46 +08:00
|
|
|
SET_RESTORE_COMMAND("ln -s -f", WALFilePath, xlogFilePath);
|
2007-02-08 23:16:19 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case RESTORE_COMMAND_COPY:
|
|
|
|
default:
|
2007-11-16 05:14:46 +08:00
|
|
|
SET_RESTORE_COMMAND("cp", WALFilePath, xlogFilePath);
|
2007-02-08 23:16:19 +08:00
|
|
|
break;
|
2007-11-16 05:14:46 +08:00
|
|
|
}
|
2007-02-08 23:16:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* This code assumes that archiveLocation is a directory You may wish to
|
|
|
|
* add code to check for tape libraries, etc.. So, since it is a
|
|
|
|
* directory, we use stat to test if its accessible
|
2007-02-08 23:16:19 +08:00
|
|
|
*/
|
|
|
|
if (stat(archiveLocation, &stat_buf) != 0)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "pg_standby: archiveLocation \"%s\" does not exist\n", archiveLocation);
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
2007-11-16 05:14:46 +08:00
|
|
|
exit(2);
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CustomizableNextWALFileReady()
|
2007-11-16 05:14:46 +08:00
|
|
|
*
|
2007-02-08 23:16:19 +08:00
|
|
|
* Is the requested file ready yet?
|
|
|
|
*/
|
2007-11-16 05:14:46 +08:00
|
|
|
static bool
|
2007-02-08 23:16:19 +08:00
|
|
|
CustomizableNextWALFileReady()
|
|
|
|
{
|
|
|
|
if (stat(WALFilePath, &stat_buf) == 0)
|
|
|
|
{
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* If its a backup file, return immediately If its a regular file
|
|
|
|
* return only if its the right size already
|
2007-02-08 23:16:19 +08:00
|
|
|
*/
|
|
|
|
if (strlen(nextWALFileName) > 24 &&
|
|
|
|
strspn(nextWALFileName, "0123456789ABCDEF") == 24 &&
|
2007-11-16 05:14:46 +08:00
|
|
|
strcmp(nextWALFileName + strlen(nextWALFileName) - strlen(".backup"),
|
|
|
|
".backup") == 0)
|
2007-02-08 23:16:19 +08:00
|
|
|
{
|
|
|
|
nextWALFileType = XLOG_BACKUP_LABEL;
|
2007-11-16 05:14:46 +08:00
|
|
|
return true;
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|
2007-11-16 05:14:46 +08:00
|
|
|
else if (stat_buf.st_size == XLOG_SEG_SIZE)
|
|
|
|
{
|
2007-02-08 23:16:19 +08:00
|
|
|
#ifdef WIN32
|
2007-11-16 05:14:46 +08:00
|
|
|
|
|
|
|
/*
|
2008-12-16 06:13:02 +08:00
|
|
|
* Windows 'cp' sets the final file size before the copy is
|
|
|
|
* complete, and not yet ready to be opened by pg_standby.
|
|
|
|
* So we wait for sleeptime secs before attempting to restore.
|
|
|
|
* If that is not enough, we will rely on the retry/holdoff
|
|
|
|
* mechanism. GNUWin32's cp does not have this problem.
|
2007-11-16 05:14:46 +08:00
|
|
|
*/
|
|
|
|
pg_usleep(sleeptime * 1000000L);
|
2007-02-08 23:16:19 +08:00
|
|
|
#endif
|
2007-11-16 05:14:46 +08:00
|
|
|
nextWALFileType = XLOG_DATA;
|
|
|
|
return true;
|
|
|
|
}
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If still too small, wait until it is the correct size
|
|
|
|
*/
|
|
|
|
if (stat_buf.st_size > XLOG_SEG_SIZE)
|
|
|
|
{
|
|
|
|
if (debug)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "file size greater than expected\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
2007-11-16 05:14:46 +08:00
|
|
|
exit(3);
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MaxSegmentsPerLogFile ( 0xFFFFFFFF / XLOG_SEG_SIZE )
|
|
|
|
|
|
|
|
static void
|
|
|
|
CustomizableCleanupPriorWALFiles(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Work out name of prior file from current filename
|
|
|
|
*/
|
2007-09-27 06:36:30 +08:00
|
|
|
if (nextWALFileType == XLOG_DATA)
|
2007-02-08 23:16:19 +08:00
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
int rc;
|
|
|
|
DIR *xldir;
|
|
|
|
struct dirent *xlde;
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* Assume its OK to keep failing. The failure situation may change
|
|
|
|
* over time, so we'd rather keep going on the main processing than
|
|
|
|
* fail because we couldnt clean up yet.
|
2007-02-08 23:16:19 +08:00
|
|
|
*/
|
|
|
|
if ((xldir = opendir(archiveLocation)) != NULL)
|
|
|
|
{
|
|
|
|
while ((xlde = readdir(xldir)) != NULL)
|
|
|
|
{
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* We ignore the timeline part of the XLOG segment identifiers
|
|
|
|
* in deciding whether a segment is still needed. This
|
|
|
|
* ensures that we won't prematurely remove a segment from a
|
|
|
|
* parent timeline. We could probably be a little more
|
|
|
|
* proactive about removing segments of non-parent timelines,
|
|
|
|
* but that would be a whole lot more complicated.
|
2007-02-08 23:16:19 +08:00
|
|
|
*
|
2007-11-16 05:14:46 +08:00
|
|
|
* We use the alphanumeric sorting property of the filenames
|
|
|
|
* to decide which ones are earlier than the
|
|
|
|
* exclusiveCleanupFileName file. Note that this means files
|
|
|
|
* are not removed in the order they were originally written,
|
|
|
|
* in case this worries you.
|
2007-02-08 23:16:19 +08:00
|
|
|
*/
|
|
|
|
if (strlen(xlde->d_name) == XLOG_DATA_FNAME_LEN &&
|
|
|
|
strspn(xlde->d_name, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN &&
|
2007-11-16 05:14:46 +08:00
|
|
|
strcmp(xlde->d_name + 8, exclusiveCleanupFileName + 8) < 0)
|
2007-02-08 23:16:19 +08:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
snprintf(WALFilePath, MAXPGPATH, "%s\\%s", archiveLocation, xlde->d_name);
|
|
|
|
#else
|
|
|
|
snprintf(WALFilePath, MAXPGPATH, "%s/%s", archiveLocation, xlde->d_name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (debug)
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "\nremoving \"%s\"", WALFilePath);
|
2007-03-04 02:50:45 +08:00
|
|
|
|
|
|
|
rc = unlink(WALFilePath);
|
2007-09-27 06:36:30 +08:00
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\npg_standby: ERROR failed to remove \"%s\": %s",
|
|
|
|
WALFilePath, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|
|
|
|
}
|
2007-09-27 06:36:30 +08:00
|
|
|
if (debug)
|
|
|
|
fprintf(stderr, "\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|
|
|
|
else
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "pg_standby: archiveLocation \"%s\" open error\n", archiveLocation);
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
closedir(xldir);
|
2007-09-27 06:36:30 +08:00
|
|
|
fflush(stderr);
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* =====================================================================
|
|
|
|
* End of Customizable section
|
|
|
|
* =====================================================================
|
|
|
|
*/
|
|
|
|
|
2007-09-27 06:36:30 +08:00
|
|
|
/*
|
|
|
|
* SetWALFileNameForCleanup()
|
2007-11-16 05:14:46 +08:00
|
|
|
*
|
2007-09-27 06:36:30 +08:00
|
|
|
* Set the earliest WAL filename that we want to keep on the archive
|
2007-11-16 05:14:46 +08:00
|
|
|
* and decide whether we need_cleanup
|
2007-09-27 06:36:30 +08:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
SetWALFileNameForCleanup(void)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
uint32 tli = 1,
|
|
|
|
log = 0,
|
|
|
|
seg = 0;
|
|
|
|
uint32 log_diff = 0,
|
|
|
|
seg_diff = 0;
|
|
|
|
bool cleanup = false;
|
2007-09-27 06:36:30 +08:00
|
|
|
|
|
|
|
if (restartWALFileName)
|
|
|
|
{
|
2008-05-09 22:27:47 +08:00
|
|
|
/*
|
|
|
|
* Don't do cleanup if the restartWALFileName provided
|
|
|
|
* is later than the xlog file requested. This is an error
|
|
|
|
* and we must not remove these files from archive.
|
|
|
|
* This shouldn't happen, but better safe than sorry.
|
|
|
|
*/
|
|
|
|
if (strcmp(restartWALFileName, nextWALFileName) > 0)
|
|
|
|
return false;
|
|
|
|
|
2007-09-27 06:36:30 +08:00
|
|
|
strcpy(exclusiveCleanupFileName, restartWALFileName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keepfiles > 0)
|
|
|
|
{
|
|
|
|
sscanf(nextWALFileName, "%08X%08X%08X", &tli, &log, &seg);
|
|
|
|
if (tli > 0 && log >= 0 && seg > 0)
|
|
|
|
{
|
|
|
|
log_diff = keepfiles / MaxSegmentsPerLogFile;
|
|
|
|
seg_diff = keepfiles % MaxSegmentsPerLogFile;
|
2007-11-16 05:14:46 +08:00
|
|
|
if (seg_diff > seg)
|
2007-09-27 06:36:30 +08:00
|
|
|
{
|
|
|
|
log_diff++;
|
2008-07-08 23:11:58 +08:00
|
|
|
seg = MaxSegmentsPerLogFile - (seg_diff - seg);
|
2007-09-27 06:36:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
seg -= seg_diff;
|
|
|
|
|
|
|
|
if (log >= log_diff)
|
|
|
|
{
|
|
|
|
log -= log_diff;
|
|
|
|
cleanup = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log = 0;
|
|
|
|
seg = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XLogFileName(exclusiveCleanupFileName, tli, log, seg);
|
|
|
|
|
|
|
|
return cleanup;
|
|
|
|
}
|
|
|
|
|
2007-02-08 23:16:19 +08:00
|
|
|
/*
|
|
|
|
* CheckForExternalTrigger()
|
2007-11-16 05:14:46 +08:00
|
|
|
*
|
2007-02-08 23:16:19 +08:00
|
|
|
* Is there a trigger file?
|
|
|
|
*/
|
2007-11-16 05:14:46 +08:00
|
|
|
static bool
|
2007-02-08 23:16:19 +08:00
|
|
|
CheckForExternalTrigger(void)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
int rc;
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* Look for a trigger file, if that option has been selected
|
2007-02-08 23:16:19 +08:00
|
|
|
*
|
2007-11-16 05:14:46 +08:00
|
|
|
* We use stat() here because triggerPath is always a file rather than
|
|
|
|
* potentially being in an archive
|
2007-02-08 23:16:19 +08:00
|
|
|
*/
|
|
|
|
if (triggerPath && stat(triggerPath, &stat_buf) == 0)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "trigger file found\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* If trigger file found, we *must* delete it. Here's why: When
|
|
|
|
* recovery completes, we will be asked again for the same file from
|
|
|
|
* the archive using pg_standby so must remove trigger file so we can
|
|
|
|
* reload file again and come up correctly.
|
2007-02-08 23:16:19 +08:00
|
|
|
*/
|
|
|
|
rc = unlink(triggerPath);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
2007-05-31 23:13:06 +08:00
|
|
|
fprintf(stderr, "\n ERROR: could not remove \"%s\": %s", triggerPath, strerror(errno));
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RestoreWALFileForRecovery()
|
2007-11-16 05:14:46 +08:00
|
|
|
*
|
2007-02-08 23:16:19 +08:00
|
|
|
* Perform the action required to restore the file from archive
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
RestoreWALFileForRecovery(void)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
int rc = 0;
|
|
|
|
int numretries = 0;
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\nrunning restore :");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (numretries < maxretries)
|
|
|
|
{
|
|
|
|
rc = system(restoreCommand);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
if (debug)
|
|
|
|
{
|
2007-09-27 06:36:30 +08:00
|
|
|
fprintf(stderr, " OK");
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-11-16 05:14:46 +08:00
|
|
|
pg_usleep(numretries++ * sleeptime * 1000000L);
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow caller to add additional info
|
|
|
|
*/
|
|
|
|
if (debug)
|
|
|
|
fprintf(stderr, "not restored : ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-09-27 06:36:30 +08:00
|
|
|
usage(void)
|
2007-02-08 23:16:19 +08:00
|
|
|
{
|
|
|
|
fprintf(stderr, "\npg_standby allows Warm Standby servers to be configured\n");
|
|
|
|
fprintf(stderr, "Usage:\n");
|
2007-09-27 06:36:30 +08:00
|
|
|
fprintf(stderr, " pg_standby [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n");
|
|
|
|
fprintf(stderr, " note space between ARCHIVELOCATION and NEXTWALFILE\n");
|
|
|
|
fprintf(stderr, "with main intended use as a restore_command in the recovery.conf\n");
|
|
|
|
fprintf(stderr, " restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n");
|
|
|
|
fprintf(stderr, "e.g. restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
fprintf(stderr, "\nOptions:\n");
|
|
|
|
fprintf(stderr, " -c copies file from archive (default)\n");
|
|
|
|
fprintf(stderr, " -d generate lots of debugging output (testing only)\n");
|
2007-09-27 06:36:30 +08:00
|
|
|
fprintf(stderr, " -k NUMFILESTOKEEP if RESTARTWALFILE not used, removes files prior to limit (0 keeps all)\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
fprintf(stderr, " -l links into archive (leaves file in archive)\n");
|
2007-09-27 06:36:30 +08:00
|
|
|
fprintf(stderr, " -r MAXRETRIES max number of times to retry, with progressive wait (default=3)\n");
|
|
|
|
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");
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sighandler(int sig)
|
|
|
|
{
|
|
|
|
signaled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------ MAIN ----------------------------------------*/
|
2007-11-16 05:14:46 +08:00
|
|
|
int
|
2007-02-08 23:16:19 +08:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
(void) signal(SIGINT, sighandler);
|
|
|
|
(void) signal(SIGQUIT, sighandler);
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "cdk:lr:s:t:w:")) != -1)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'c': /* Use copy */
|
|
|
|
restoreCommandType = RESTORE_COMMAND_COPY;
|
|
|
|
break;
|
|
|
|
case 'd': /* Debug mode */
|
|
|
|
debug = true;
|
|
|
|
break;
|
|
|
|
case 'k': /* keepfiles */
|
|
|
|
keepfiles = atoi(optarg);
|
2007-09-27 06:36:30 +08:00
|
|
|
if (keepfiles < 0)
|
2007-02-08 23:16:19 +08:00
|
|
|
{
|
2007-09-27 06:36:30 +08:00
|
|
|
fprintf(stderr, "usage: pg_standby -k keepfiles must be >= 0\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l': /* Use link */
|
|
|
|
restoreCommandType = RESTORE_COMMAND_LINK;
|
|
|
|
break;
|
|
|
|
case 'r': /* Retries */
|
|
|
|
maxretries = atoi(optarg);
|
|
|
|
if (maxretries < 0)
|
|
|
|
{
|
2007-09-27 06:36:30 +08:00
|
|
|
fprintf(stderr, "usage: pg_standby -r maxretries must be >= 0\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's': /* Sleep time */
|
|
|
|
sleeptime = atoi(optarg);
|
|
|
|
if (sleeptime <= 0 || sleeptime > 60)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: pg_standby -s sleeptime incorrectly set\n");
|
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't': /* Trigger file */
|
|
|
|
triggerPath = optarg;
|
|
|
|
if (CheckForExternalTrigger())
|
2007-11-16 05:14:46 +08:00
|
|
|
exit(1); /* Normal exit, with non-zero */
|
|
|
|
break;
|
2007-02-08 23:16:19 +08:00
|
|
|
case 'w': /* Max wait time */
|
|
|
|
maxwaittime = atoi(optarg);
|
|
|
|
if (maxwaittime < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: pg_standby -w maxwaittime incorrectly set\n");
|
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
/*
|
2007-02-08 23:16:19 +08:00
|
|
|
* Parameter checking - after checking to see if trigger file present
|
|
|
|
*/
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We will go to the archiveLocation to get nextWALFileName.
|
2007-11-16 05:14:46 +08:00
|
|
|
* nextWALFileName may not exist yet, which would not be an error, so we
|
|
|
|
* separate the archiveLocation and nextWALFileName so we can check
|
2007-02-08 23:16:19 +08:00
|
|
|
* separately whether archiveLocation exists, if not that is an error
|
|
|
|
*/
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
archiveLocation = argv[optind];
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "pg_standby: must specify archiveLocation\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
nextWALFileName = argv[optind];
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "pg_standby: use %%f to specify nextWALFileName\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
xlogFilePath = argv[optind];
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "pg_standby: use %%p to specify xlogFilePath\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2007-09-27 06:36:30 +08:00
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
restartWALFileName = argv[optind];
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
2007-02-08 23:16:19 +08:00
|
|
|
CustomizableInitialize();
|
|
|
|
|
2007-09-27 06:36:30 +08:00
|
|
|
need_cleanup = SetWALFileNameForCleanup();
|
|
|
|
|
2007-02-08 23:16:19 +08:00
|
|
|
if (debug)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "\nTrigger file : %s", triggerPath ? triggerPath : "<not set>");
|
|
|
|
fprintf(stderr, "\nWaiting for WAL file : %s", nextWALFileName);
|
|
|
|
fprintf(stderr, "\nWAL file path : %s", WALFilePath);
|
|
|
|
fprintf(stderr, "\nRestoring to... : %s", xlogFilePath);
|
|
|
|
fprintf(stderr, "\nSleep interval : %d second%s",
|
|
|
|
sleeptime, (sleeptime > 1 ? "s" : " "));
|
|
|
|
fprintf(stderr, "\nMax wait interval : %d %s",
|
|
|
|
maxwaittime, (maxwaittime > 0 ? "seconds" : "forever"));
|
2007-02-08 23:16:19 +08:00
|
|
|
fprintf(stderr, "\nCommand for restore : %s", restoreCommand);
|
2008-05-09 22:27:47 +08:00
|
|
|
fprintf(stderr, "\nKeep archive history : ");
|
|
|
|
if (need_cleanup)
|
|
|
|
fprintf(stderr, "%s and later", exclusiveCleanupFileName);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "No cleanup required");
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for initial history file: always the first file to be requested
|
|
|
|
* It's OK if the file isn't there - all other files need to wait
|
|
|
|
*/
|
|
|
|
if (strlen(nextWALFileName) > 8 &&
|
|
|
|
strspn(nextWALFileName, "0123456789ABCDEF") == 8 &&
|
|
|
|
strcmp(nextWALFileName + strlen(nextWALFileName) - strlen(".history"),
|
|
|
|
".history") == 0)
|
|
|
|
{
|
|
|
|
nextWALFileType = XLOG_HISTORY;
|
|
|
|
if (RestoreWALFileForRecovery())
|
|
|
|
exit(0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "history file not found\n");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
/*
|
2007-02-08 23:16:19 +08:00
|
|
|
* Main wait loop
|
|
|
|
*/
|
|
|
|
while (!CustomizableNextWALFileReady() && !triggered)
|
|
|
|
{
|
|
|
|
if (sleeptime <= 60)
|
2007-11-16 05:14:46 +08:00
|
|
|
pg_usleep(sleeptime * 1000000L);
|
2007-02-08 23:16:19 +08:00
|
|
|
|
|
|
|
if (signaled)
|
|
|
|
{
|
2007-09-27 06:36:30 +08:00
|
|
|
triggered = true;
|
2007-02-08 23:16:19 +08:00
|
|
|
if (debug)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "\nsignaled to exit\n");
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "\nWAL file not present yet.");
|
2007-02-08 23:16:19 +08:00
|
|
|
if (triggerPath)
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, " Checking for trigger file...");
|
2007-02-08 23:16:19 +08:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
waittime += sleeptime;
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-02-08 23:16:19 +08:00
|
|
|
if (!triggered && (CheckForExternalTrigger() || (waittime >= maxwaittime && maxwaittime > 0)))
|
|
|
|
{
|
|
|
|
triggered = true;
|
|
|
|
if (debug && waittime >= maxwaittime && maxwaittime > 0)
|
2007-11-16 05:14:46 +08:00
|
|
|
fprintf(stderr, "\nTimed out after %d seconds\n", waittime);
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
/*
|
|
|
|
* Action on exit
|
2007-02-08 23:16:19 +08:00
|
|
|
*/
|
|
|
|
if (triggered)
|
2007-11-16 05:14:46 +08:00
|
|
|
exit(1); /* Normal exit, with non-zero */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Once we have restored this file successfully we can remove some prior
|
|
|
|
* WAL files. If this restore fails we musn't remove any file because some
|
|
|
|
* of them will be requested again immediately after the failed restore,
|
|
|
|
* or when we restart recovery.
|
2007-07-16 06:54:21 +08:00
|
|
|
*/
|
2007-09-27 06:36:30 +08:00
|
|
|
if (RestoreWALFileForRecovery() && need_cleanup)
|
2007-07-16 06:54:21 +08:00
|
|
|
CustomizableCleanupPriorWALFiles();
|
|
|
|
|
|
|
|
return 0;
|
2007-02-08 23:16:19 +08:00
|
|
|
}
|