7 * Production-ready example of how to create a Warm Standby
8 * database server using continuous archiving as a
9 * replication mechanism
11 * We separate the parameters for archive and nextWALfile
12 * so that we can check the archive exists, even if the
13 * WAL file doesn't (yet).
15 * This program will be executed once in full for each file
16 * requested by the warm standby server.
18 * It is designed to cater to a variety of needs, as well
19 * providing a customizable section.
21 * Original author: Simon Riggs simon@2ndquadrant.com
22 * Current maintainer: Simon Riggs
24 #include "postgres_fe.h"
32 int getopt(int argc
, char *const argv
[], const char *optstring
);
45 /* Options and defaults */
46 int sleeptime
= 5; /* amount of time to sleep between file checks */
47 int waittime
= -1; /* how long we have been waiting, -1 no wait
49 int maxwaittime
= 0; /* how long are we prepared to wait for? */
50 int keepfiles
= 0; /* number of WAL files to keep, 0 keep all */
51 int maxretries
= 3; /* number of retries on restore command */
52 bool debug
= false; /* are we debugging? */
53 bool triggered
= false; /* have we been triggered? */
54 bool need_cleanup
= false; /* do we need to remove files from
57 static volatile sig_atomic_t signaled
= false;
59 char *archiveLocation
; /* where to find the archive? */
60 char *triggerPath
; /* where to find the trigger file? */
61 char *xlogFilePath
; /* where we are going to restore to */
62 char *nextWALFileName
; /* the file we need to get from archive */
63 char *restartWALFileName
; /* the file from which we can restart restore */
64 char *priorWALFileName
; /* the file we need to get from archive */
65 char WALFilePath
[MAXPGPATH
]; /* the file path including archive */
66 char restoreCommand
[MAXPGPATH
]; /* run this to restore */
67 char exclusiveCleanupFileName
[MAXPGPATH
]; /* the file we need to
70 #define RESTORE_COMMAND_COPY 0
71 #define RESTORE_COMMAND_LINK 1
72 int restoreCommandType
;
75 #define XLOG_HISTORY 1
76 #define XLOG_BACKUP_LABEL 2
79 #define SET_RESTORE_COMMAND(cmd, arg1, arg2) \
80 snprintf(restoreCommand, MAXPGPATH, cmd " \"%s\" \"%s\"", arg1, arg2)
84 /* =====================================================================
86 * Customizable section
88 * =====================================================================
90 * Currently, this section assumes that the Archive is a locally
91 * accessible directory. If you want to make other assumptions,
92 * such as using a vendor-specific archive and access API, these
93 * routines are the ones you'll need to change. You're
94 * enouraged to submit any changes to pgsql-patches@postgresql.org
95 * or personally to the current maintainer. Those changes may be
96 * folded in to later versions of this program.
99 #define XLOG_DATA_FNAME_LEN 24
100 /* Reworked from access/xlog_internal.h */
101 #define XLogFileName(fname, tli, log, seg) \
102 snprintf(fname, XLOG_DATA_FNAME_LEN + 1, "%08X%08X%08X", tli, log, seg)
105 * Initialize allows customized commands into the warm standby program.
107 * As an example, and probably the common case, we use either
108 * cp/ln commands on *nix, or copy/move command on Windows.
112 CustomizableInitialize(void)
115 snprintf(WALFilePath
, MAXPGPATH
, "%s\\%s", archiveLocation
, nextWALFileName
);
116 switch (restoreCommandType
)
118 case RESTORE_COMMAND_LINK
:
119 SET_RESTORE_COMMAND("mklink", WALFilePath
, xlogFilePath
);
120 case RESTORE_COMMAND_COPY
:
122 SET_RESTORE_COMMAND("copy", WALFilePath
, xlogFilePath
);
126 snprintf(WALFilePath
, MAXPGPATH
, "%s/%s", archiveLocation
, nextWALFileName
);
127 switch (restoreCommandType
)
129 case RESTORE_COMMAND_LINK
:
130 #if HAVE_WORKING_LINK
131 SET_RESTORE_COMMAND("ln -s -f", WALFilePath
, xlogFilePath
);
134 case RESTORE_COMMAND_COPY
:
136 SET_RESTORE_COMMAND("cp", WALFilePath
, xlogFilePath
);
142 * This code assumes that archiveLocation is a directory You may wish to
143 * add code to check for tape libraries, etc.. So, since it is a
144 * directory, we use stat to test if its accessible
146 if (stat(archiveLocation
, &stat_buf
) != 0)
148 fprintf(stderr
, "pg_standby: archiveLocation \"%s\" does not exist\n", archiveLocation
);
155 * CustomizableNextWALFileReady()
157 * Is the requested file ready yet?
160 CustomizableNextWALFileReady()
162 if (stat(WALFilePath
, &stat_buf
) == 0)
165 * If its a backup file, return immediately If its a regular file
166 * return only if its the right size already
168 if (strlen(nextWALFileName
) > 24 &&
169 strspn(nextWALFileName
, "0123456789ABCDEF") == 24 &&
170 strcmp(nextWALFileName
+ strlen(nextWALFileName
) - strlen(".backup"),
173 nextWALFileType
= XLOG_BACKUP_LABEL
;
176 else if (stat_buf
.st_size
== XLOG_SEG_SIZE
)
181 * Windows reports that the file has the right number of bytes
182 * even though the file is still being copied and cannot be opened
183 * by pg_standby yet. So we wait for sleeptime secs before
184 * attempting to restore. If that is not enough, we will rely on
185 * the retry/holdoff mechanism.
187 pg_usleep(sleeptime
* 1000000L);
189 nextWALFileType
= XLOG_DATA
;
194 * If still too small, wait until it is the correct size
196 if (stat_buf
.st_size
> XLOG_SEG_SIZE
)
200 fprintf(stderr
, "file size greater than expected\n");
210 #define MaxSegmentsPerLogFile ( 0xFFFFFFFF / XLOG_SEG_SIZE )
213 CustomizableCleanupPriorWALFiles(void)
216 * Work out name of prior file from current filename
218 if (nextWALFileType
== XLOG_DATA
)
225 * Assume its OK to keep failing. The failure situation may change
226 * over time, so we'd rather keep going on the main processing than
227 * fail because we couldnt clean up yet.
229 if ((xldir
= opendir(archiveLocation
)) != NULL
)
231 while ((xlde
= readdir(xldir
)) != NULL
)
234 * We ignore the timeline part of the XLOG segment identifiers
235 * in deciding whether a segment is still needed. This
236 * ensures that we won't prematurely remove a segment from a
237 * parent timeline. We could probably be a little more
238 * proactive about removing segments of non-parent timelines,
239 * but that would be a whole lot more complicated.
241 * We use the alphanumeric sorting property of the filenames
242 * to decide which ones are earlier than the
243 * exclusiveCleanupFileName file. Note that this means files
244 * are not removed in the order they were originally written,
245 * in case this worries you.
247 if (strlen(xlde
->d_name
) == XLOG_DATA_FNAME_LEN
&&
248 strspn(xlde
->d_name
, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN
&&
249 strcmp(xlde
->d_name
+ 8, exclusiveCleanupFileName
+ 8) < 0)
252 snprintf(WALFilePath
, MAXPGPATH
, "%s\\%s", archiveLocation
, xlde
->d_name
);
254 snprintf(WALFilePath
, MAXPGPATH
, "%s/%s", archiveLocation
, xlde
->d_name
);
258 fprintf(stderr
, "\nremoving \"%s\"", WALFilePath
);
260 rc
= unlink(WALFilePath
);
263 fprintf(stderr
, "\npg_standby: ERROR failed to remove \"%s\": %s",
264 WALFilePath
, strerror(errno
));
270 fprintf(stderr
, "\n");
273 fprintf(stderr
, "pg_standby: archiveLocation \"%s\" open error\n", archiveLocation
);
280 /* =====================================================================
281 * End of Customizable section
282 * =====================================================================
286 * SetWALFileNameForCleanup()
288 * Set the earliest WAL filename that we want to keep on the archive
289 * and decide whether we need_cleanup
292 SetWALFileNameForCleanup(void)
299 bool cleanup
= false;
301 if (restartWALFileName
)
304 * Don't do cleanup if the restartWALFileName provided
305 * is later than the xlog file requested. This is an error
306 * and we must not remove these files from archive.
307 * This shouldn't happen, but better safe than sorry.
309 if (strcmp(restartWALFileName
, nextWALFileName
) > 0)
312 strcpy(exclusiveCleanupFileName
, restartWALFileName
);
318 sscanf(nextWALFileName
, "%08X%08X%08X", &tli
, &log
, &seg
);
319 if (tli
> 0 && log
>= 0 && seg
> 0)
321 log_diff
= keepfiles
/ MaxSegmentsPerLogFile
;
322 seg_diff
= keepfiles
% MaxSegmentsPerLogFile
;
326 seg
= MaxSegmentsPerLogFile
- (seg_diff
- seg
);
344 XLogFileName(exclusiveCleanupFileName
, tli
, log
, seg
);
350 * CheckForExternalTrigger()
352 * Is there a trigger file?
355 CheckForExternalTrigger(void)
360 * Look for a trigger file, if that option has been selected
362 * We use stat() here because triggerPath is always a file rather than
363 * potentially being in an archive
365 if (triggerPath
&& stat(triggerPath
, &stat_buf
) == 0)
367 fprintf(stderr
, "trigger file found\n");
371 * If trigger file found, we *must* delete it. Here's why: When
372 * recovery completes, we will be asked again for the same file from
373 * the archive using pg_standby so must remove trigger file so we can
374 * reload file again and come up correctly.
376 rc
= unlink(triggerPath
);
379 fprintf(stderr
, "\n ERROR: could not remove \"%s\": %s", triggerPath
, strerror(errno
));
390 * RestoreWALFileForRecovery()
392 * Perform the action required to restore the file from archive
395 RestoreWALFileForRecovery(void)
402 fprintf(stderr
, "\nrunning restore :");
406 while (numretries
< maxretries
)
408 rc
= system(restoreCommand
);
413 fprintf(stderr
, " OK");
418 pg_usleep(numretries
++ * sleeptime
* 1000000L);
422 * Allow caller to add additional info
425 fprintf(stderr
, "not restored : ");
432 fprintf(stderr
, "\npg_standby allows Warm Standby servers to be configured\n");
433 fprintf(stderr
, "Usage:\n");
434 fprintf(stderr
, " pg_standby [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n");
435 fprintf(stderr
, " note space between ARCHIVELOCATION and NEXTWALFILE\n");
436 fprintf(stderr
, "with main intended use as a restore_command in the recovery.conf\n");
437 fprintf(stderr
, " restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n");
438 fprintf(stderr
, "e.g. restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n");
439 fprintf(stderr
, "\nOptions:\n");
440 fprintf(stderr
, " -c copies file from archive (default)\n");
441 fprintf(stderr
, " -d generate lots of debugging output (testing only)\n");
442 fprintf(stderr
, " -k NUMFILESTOKEEP if RESTARTWALFILE not used, removes files prior to limit (0 keeps all)\n");
443 fprintf(stderr
, " -l links into archive (leaves file in archive)\n");
444 fprintf(stderr
, " -r MAXRETRIES max number of times to retry, with progressive wait (default=3)\n");
445 fprintf(stderr
, " -s SLEEPTIME seconds to wait between file checks (min=1, max=60, default=5)\n");
446 fprintf(stderr
, " -t TRIGGERFILE defines a trigger file to initiate failover (no default)\n");
447 fprintf(stderr
, " -w MAXWAITTIME max seconds to wait for a file (0=no limit)(default=0)\n");
457 /*------------ MAIN ----------------------------------------*/
459 main(int argc
, char **argv
)
463 (void) signal(SIGINT
, sighandler
);
464 (void) signal(SIGQUIT
, sighandler
);
466 while ((c
= getopt(argc
, argv
, "cdk:lr:s:t:w:")) != -1)
470 case 'c': /* Use copy */
471 restoreCommandType
= RESTORE_COMMAND_COPY
;
473 case 'd': /* Debug mode */
476 case 'k': /* keepfiles */
477 keepfiles
= atoi(optarg
);
480 fprintf(stderr
, "usage: pg_standby -k keepfiles must be >= 0\n");
485 case 'l': /* Use link */
486 restoreCommandType
= RESTORE_COMMAND_LINK
;
488 case 'r': /* Retries */
489 maxretries
= atoi(optarg
);
492 fprintf(stderr
, "usage: pg_standby -r maxretries must be >= 0\n");
497 case 's': /* Sleep time */
498 sleeptime
= atoi(optarg
);
499 if (sleeptime
<= 0 || sleeptime
> 60)
501 fprintf(stderr
, "usage: pg_standby -s sleeptime incorrectly set\n");
506 case 't': /* Trigger file */
507 triggerPath
= optarg
;
508 if (CheckForExternalTrigger())
509 exit(1); /* Normal exit, with non-zero */
511 case 'w': /* Max wait time */
512 maxwaittime
= atoi(optarg
);
515 fprintf(stderr
, "usage: pg_standby -w maxwaittime incorrectly set\n");
528 * Parameter checking - after checking to see if trigger file present
537 * We will go to the archiveLocation to get nextWALFileName.
538 * nextWALFileName may not exist yet, which would not be an error, so we
539 * separate the archiveLocation and nextWALFileName so we can check
540 * separately whether archiveLocation exists, if not that is an error
544 archiveLocation
= argv
[optind
];
549 fprintf(stderr
, "pg_standby: must specify archiveLocation\n");
556 nextWALFileName
= argv
[optind
];
561 fprintf(stderr
, "pg_standby: use %%f to specify nextWALFileName\n");
568 xlogFilePath
= argv
[optind
];
573 fprintf(stderr
, "pg_standby: use %%p to specify xlogFilePath\n");
580 restartWALFileName
= argv
[optind
];
584 CustomizableInitialize();
586 need_cleanup
= SetWALFileNameForCleanup();
590 fprintf(stderr
, "\nTrigger file : %s", triggerPath
? triggerPath
: "<not set>");
591 fprintf(stderr
, "\nWaiting for WAL file : %s", nextWALFileName
);
592 fprintf(stderr
, "\nWAL file path : %s", WALFilePath
);
593 fprintf(stderr
, "\nRestoring to... : %s", xlogFilePath
);
594 fprintf(stderr
, "\nSleep interval : %d second%s",
595 sleeptime
, (sleeptime
> 1 ? "s" : " "));
596 fprintf(stderr
, "\nMax wait interval : %d %s",
597 maxwaittime
, (maxwaittime
> 0 ? "seconds" : "forever"));
598 fprintf(stderr
, "\nCommand for restore : %s", restoreCommand
);
599 fprintf(stderr
, "\nKeep archive history : ");
601 fprintf(stderr
, "%s and later", exclusiveCleanupFileName
);
603 fprintf(stderr
, "No cleanup required");
608 * Check for initial history file: always the first file to be requested
609 * It's OK if the file isn't there - all other files need to wait
611 if (strlen(nextWALFileName
) > 8 &&
612 strspn(nextWALFileName
, "0123456789ABCDEF") == 8 &&
613 strcmp(nextWALFileName
+ strlen(nextWALFileName
) - strlen(".history"),
616 nextWALFileType
= XLOG_HISTORY
;
617 if (RestoreWALFileForRecovery())
623 fprintf(stderr
, "history file not found\n");
633 while (!CustomizableNextWALFileReady() && !triggered
)
636 pg_usleep(sleeptime
* 1000000L);
643 fprintf(stderr
, "\nsignaled to exit\n");
652 fprintf(stderr
, "\nWAL file not present yet.");
654 fprintf(stderr
, " Checking for trigger file...");
658 waittime
+= sleeptime
;
660 if (!triggered
&& (CheckForExternalTrigger() || (waittime
>= maxwaittime
&& maxwaittime
> 0)))
663 if (debug
&& waittime
>= maxwaittime
&& maxwaittime
> 0)
664 fprintf(stderr
, "\nTimed out after %d seconds\n", waittime
);
673 exit(1); /* Normal exit, with non-zero */
676 * Once we have restored this file successfully we can remove some prior
677 * WAL files. If this restore fails we musn't remove any file because some
678 * of them will be requested again immediately after the failed restore,
679 * or when we restart recovery.
681 if (RestoreWALFileForRecovery() && need_cleanup
)
682 CustomizableCleanupPriorWALFiles();