4 * Production-ready example of how to create a Warm Standby
5 * database server using continuous archiving as a
6 * replication mechanism
8 * We separate the parameters for archive and nextWALfile
9 * so that we can check the archive exists, even if the
10 * WAL file doesn't (yet).
12 * This program will be executed once in full for each file
13 * requested by the warm standby server.
15 * It is designed to cater to a variety of needs, as well
16 * providing a customizable section.
18 * Original author: Simon Riggs simon@2ndquadrant.com
19 * Current maintainer: Simon Riggs
21 #include "postgres_fe.h"
29 int getopt(int argc
, char *const argv
[], const char *optstring
);
42 /* Options and defaults */
43 int sleeptime
= 5; /* amount of time to sleep between file checks */
44 int waittime
= -1; /* how long we have been waiting, -1 no wait
46 int maxwaittime
= 0; /* how long are we prepared to wait for? */
47 int keepfiles
= 0; /* number of WAL files to keep, 0 keep all */
48 int maxretries
= 3; /* number of retries on restore command */
49 bool debug
= false; /* are we debugging? */
50 bool triggered
= false; /* have we been triggered? */
51 bool need_cleanup
= false; /* do we need to remove files from
54 static volatile sig_atomic_t signaled
= false;
56 char *archiveLocation
; /* where to find the archive? */
57 char *triggerPath
; /* where to find the trigger file? */
58 char *xlogFilePath
; /* where we are going to restore to */
59 char *nextWALFileName
; /* the file we need to get from archive */
60 char *restartWALFileName
; /* the file from which we can restart restore */
61 char *priorWALFileName
; /* the file we need to get from archive */
62 char WALFilePath
[MAXPGPATH
]; /* the file path including archive */
63 char restoreCommand
[MAXPGPATH
]; /* run this to restore */
64 char exclusiveCleanupFileName
[MAXPGPATH
]; /* the file we need to
67 #define RESTORE_COMMAND_COPY 0
68 #define RESTORE_COMMAND_LINK 1
69 int restoreCommandType
;
72 #define XLOG_HISTORY 1
73 #define XLOG_BACKUP_LABEL 2
76 #define SET_RESTORE_COMMAND(cmd, arg1, arg2) \
77 snprintf(restoreCommand, MAXPGPATH, cmd " \"%s\" \"%s\"", arg1, arg2)
81 /* =====================================================================
83 * Customizable section
85 * =====================================================================
87 * Currently, this section assumes that the Archive is a locally
88 * accessible directory. If you want to make other assumptions,
89 * such as using a vendor-specific archive and access API, these
90 * routines are the ones you'll need to change. You're
91 * enouraged to submit any changes to pgsql-patches@postgresql.org
92 * or personally to the current maintainer. Those changes may be
93 * folded in to later versions of this program.
96 #define XLOG_DATA_FNAME_LEN 24
97 /* Reworked from access/xlog_internal.h */
98 #define XLogFileName(fname, tli, log, seg) \
99 snprintf(fname, XLOG_DATA_FNAME_LEN + 1, "%08X%08X%08X", tli, log, seg)
102 * Initialize allows customized commands into the warm standby program.
104 * As an example, and probably the common case, we use either
105 * cp/ln commands on *nix, or copy/move command on Windows.
109 CustomizableInitialize(void)
112 snprintf(WALFilePath
, MAXPGPATH
, "%s\\%s", archiveLocation
, nextWALFileName
);
113 switch (restoreCommandType
)
115 case RESTORE_COMMAND_LINK
:
116 SET_RESTORE_COMMAND("mklink", WALFilePath
, xlogFilePath
);
117 case RESTORE_COMMAND_COPY
:
119 SET_RESTORE_COMMAND("copy", WALFilePath
, xlogFilePath
);
123 snprintf(WALFilePath
, MAXPGPATH
, "%s/%s", archiveLocation
, nextWALFileName
);
124 switch (restoreCommandType
)
126 case RESTORE_COMMAND_LINK
:
127 #if HAVE_WORKING_LINK
128 SET_RESTORE_COMMAND("ln -s -f", WALFilePath
, xlogFilePath
);
131 case RESTORE_COMMAND_COPY
:
133 SET_RESTORE_COMMAND("cp", WALFilePath
, xlogFilePath
);
139 * This code assumes that archiveLocation is a directory You may wish to
140 * add code to check for tape libraries, etc.. So, since it is a
141 * directory, we use stat to test if its accessible
143 if (stat(archiveLocation
, &stat_buf
) != 0)
145 fprintf(stderr
, "pg_standby: archiveLocation \"%s\" does not exist\n", archiveLocation
);
152 * CustomizableNextWALFileReady()
154 * Is the requested file ready yet?
157 CustomizableNextWALFileReady()
159 if (stat(WALFilePath
, &stat_buf
) == 0)
162 * If its a backup file, return immediately If its a regular file
163 * return only if its the right size already
165 if (strlen(nextWALFileName
) > 24 &&
166 strspn(nextWALFileName
, "0123456789ABCDEF") == 24 &&
167 strcmp(nextWALFileName
+ strlen(nextWALFileName
) - strlen(".backup"),
170 nextWALFileType
= XLOG_BACKUP_LABEL
;
173 else if (stat_buf
.st_size
== XLOG_SEG_SIZE
)
178 * Windows reports that the file has the right number of bytes
179 * even though the file is still being copied and cannot be opened
180 * by pg_standby yet. So we wait for sleeptime secs before
181 * attempting to restore. If that is not enough, we will rely on
182 * the retry/holdoff mechanism.
184 pg_usleep(sleeptime
* 1000000L);
186 nextWALFileType
= XLOG_DATA
;
191 * If still too small, wait until it is the correct size
193 if (stat_buf
.st_size
> XLOG_SEG_SIZE
)
197 fprintf(stderr
, "file size greater than expected\n");
207 #define MaxSegmentsPerLogFile ( 0xFFFFFFFF / XLOG_SEG_SIZE )
210 CustomizableCleanupPriorWALFiles(void)
213 * Work out name of prior file from current filename
215 if (nextWALFileType
== XLOG_DATA
)
222 * Assume its OK to keep failing. The failure situation may change
223 * over time, so we'd rather keep going on the main processing than
224 * fail because we couldnt clean up yet.
226 if ((xldir
= opendir(archiveLocation
)) != NULL
)
228 while ((xlde
= readdir(xldir
)) != NULL
)
231 * We ignore the timeline part of the XLOG segment identifiers
232 * in deciding whether a segment is still needed. This
233 * ensures that we won't prematurely remove a segment from a
234 * parent timeline. We could probably be a little more
235 * proactive about removing segments of non-parent timelines,
236 * but that would be a whole lot more complicated.
238 * We use the alphanumeric sorting property of the filenames
239 * to decide which ones are earlier than the
240 * exclusiveCleanupFileName file. Note that this means files
241 * are not removed in the order they were originally written,
242 * in case this worries you.
244 if (strlen(xlde
->d_name
) == XLOG_DATA_FNAME_LEN
&&
245 strspn(xlde
->d_name
, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN
&&
246 strcmp(xlde
->d_name
+ 8, exclusiveCleanupFileName
+ 8) < 0)
249 snprintf(WALFilePath
, MAXPGPATH
, "%s\\%s", archiveLocation
, xlde
->d_name
);
251 snprintf(WALFilePath
, MAXPGPATH
, "%s/%s", archiveLocation
, xlde
->d_name
);
255 fprintf(stderr
, "\nremoving \"%s\"", WALFilePath
);
257 rc
= unlink(WALFilePath
);
260 fprintf(stderr
, "\npg_standby: ERROR failed to remove \"%s\": %s",
261 WALFilePath
, strerror(errno
));
267 fprintf(stderr
, "\n");
270 fprintf(stderr
, "pg_standby: archiveLocation \"%s\" open error\n", archiveLocation
);
277 /* =====================================================================
278 * End of Customizable section
279 * =====================================================================
283 * SetWALFileNameForCleanup()
285 * Set the earliest WAL filename that we want to keep on the archive
286 * and decide whether we need_cleanup
289 SetWALFileNameForCleanup(void)
296 bool cleanup
= false;
298 if (restartWALFileName
)
300 strcpy(exclusiveCleanupFileName
, restartWALFileName
);
306 sscanf(nextWALFileName
, "%08X%08X%08X", &tli
, &log
, &seg
);
307 if (tli
> 0 && log
>= 0 && seg
> 0)
309 log_diff
= keepfiles
/ MaxSegmentsPerLogFile
;
310 seg_diff
= keepfiles
% MaxSegmentsPerLogFile
;
314 seg
= MaxSegmentsPerLogFile
- seg_diff
;
332 XLogFileName(exclusiveCleanupFileName
, tli
, log
, seg
);
338 * CheckForExternalTrigger()
340 * Is there a trigger file?
343 CheckForExternalTrigger(void)
348 * Look for a trigger file, if that option has been selected
350 * We use stat() here because triggerPath is always a file rather than
351 * potentially being in an archive
353 if (triggerPath
&& stat(triggerPath
, &stat_buf
) == 0)
355 fprintf(stderr
, "trigger file found\n");
359 * If trigger file found, we *must* delete it. Here's why: When
360 * recovery completes, we will be asked again for the same file from
361 * the archive using pg_standby so must remove trigger file so we can
362 * reload file again and come up correctly.
364 rc
= unlink(triggerPath
);
367 fprintf(stderr
, "\n ERROR: could not remove \"%s\": %s", triggerPath
, strerror(errno
));
378 * RestoreWALFileForRecovery()
380 * Perform the action required to restore the file from archive
383 RestoreWALFileForRecovery(void)
390 fprintf(stderr
, "\nrunning restore :");
394 while (numretries
< maxretries
)
396 rc
= system(restoreCommand
);
401 fprintf(stderr
, " OK");
406 pg_usleep(numretries
++ * sleeptime
* 1000000L);
410 * Allow caller to add additional info
413 fprintf(stderr
, "not restored : ");
420 fprintf(stderr
, "\npg_standby allows Warm Standby servers to be configured\n");
421 fprintf(stderr
, "Usage:\n");
422 fprintf(stderr
, " pg_standby [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n");
423 fprintf(stderr
, " note space between ARCHIVELOCATION and NEXTWALFILE\n");
424 fprintf(stderr
, "with main intended use as a restore_command in the recovery.conf\n");
425 fprintf(stderr
, " restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n");
426 fprintf(stderr
, "e.g. restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n");
427 fprintf(stderr
, "\nOptions:\n");
428 fprintf(stderr
, " -c copies file from archive (default)\n");
429 fprintf(stderr
, " -d generate lots of debugging output (testing only)\n");
430 fprintf(stderr
, " -k NUMFILESTOKEEP if RESTARTWALFILE not used, removes files prior to limit (0 keeps all)\n");
431 fprintf(stderr
, " -l links into archive (leaves file in archive)\n");
432 fprintf(stderr
, " -r MAXRETRIES max number of times to retry, with progressive wait (default=3)\n");
433 fprintf(stderr
, " -s SLEEPTIME seconds to wait between file checks (min=1, max=60, default=5)\n");
434 fprintf(stderr
, " -t TRIGGERFILE defines a trigger file to initiate failover (no default)\n");
435 fprintf(stderr
, " -w MAXWAITTIME max seconds to wait for a file (0=no limit)(default=0)\n");
445 /*------------ MAIN ----------------------------------------*/
447 main(int argc
, char **argv
)
451 (void) signal(SIGINT
, sighandler
);
452 (void) signal(SIGQUIT
, sighandler
);
454 while ((c
= getopt(argc
, argv
, "cdk:lr:s:t:w:")) != -1)
458 case 'c': /* Use copy */
459 restoreCommandType
= RESTORE_COMMAND_COPY
;
461 case 'd': /* Debug mode */
464 case 'k': /* keepfiles */
465 keepfiles
= atoi(optarg
);
468 fprintf(stderr
, "usage: pg_standby -k keepfiles must be >= 0\n");
473 case 'l': /* Use link */
474 restoreCommandType
= RESTORE_COMMAND_LINK
;
476 case 'r': /* Retries */
477 maxretries
= atoi(optarg
);
480 fprintf(stderr
, "usage: pg_standby -r maxretries must be >= 0\n");
485 case 's': /* Sleep time */
486 sleeptime
= atoi(optarg
);
487 if (sleeptime
<= 0 || sleeptime
> 60)
489 fprintf(stderr
, "usage: pg_standby -s sleeptime incorrectly set\n");
494 case 't': /* Trigger file */
495 triggerPath
= optarg
;
496 if (CheckForExternalTrigger())
497 exit(1); /* Normal exit, with non-zero */
499 case 'w': /* Max wait time */
500 maxwaittime
= atoi(optarg
);
503 fprintf(stderr
, "usage: pg_standby -w maxwaittime incorrectly set\n");
516 * Parameter checking - after checking to see if trigger file present
525 * We will go to the archiveLocation to get nextWALFileName.
526 * nextWALFileName may not exist yet, which would not be an error, so we
527 * separate the archiveLocation and nextWALFileName so we can check
528 * separately whether archiveLocation exists, if not that is an error
532 archiveLocation
= argv
[optind
];
537 fprintf(stderr
, "pg_standby: must specify archiveLocation\n");
544 nextWALFileName
= argv
[optind
];
549 fprintf(stderr
, "pg_standby: use %%f to specify nextWALFileName\n");
556 xlogFilePath
= argv
[optind
];
561 fprintf(stderr
, "pg_standby: use %%p to specify xlogFilePath\n");
568 restartWALFileName
= argv
[optind
];
572 CustomizableInitialize();
574 need_cleanup
= SetWALFileNameForCleanup();
578 fprintf(stderr
, "\nTrigger file : %s", triggerPath
? triggerPath
: "<not set>");
579 fprintf(stderr
, "\nWaiting for WAL file : %s", nextWALFileName
);
580 fprintf(stderr
, "\nWAL file path : %s", WALFilePath
);
581 fprintf(stderr
, "\nRestoring to... : %s", xlogFilePath
);
582 fprintf(stderr
, "\nSleep interval : %d second%s",
583 sleeptime
, (sleeptime
> 1 ? "s" : " "));
584 fprintf(stderr
, "\nMax wait interval : %d %s",
585 maxwaittime
, (maxwaittime
> 0 ? "seconds" : "forever"));
586 fprintf(stderr
, "\nCommand for restore : %s", restoreCommand
);
587 fprintf(stderr
, "\nKeep archive history : %s and later", exclusiveCleanupFileName
);
592 * Check for initial history file: always the first file to be requested
593 * It's OK if the file isn't there - all other files need to wait
595 if (strlen(nextWALFileName
) > 8 &&
596 strspn(nextWALFileName
, "0123456789ABCDEF") == 8 &&
597 strcmp(nextWALFileName
+ strlen(nextWALFileName
) - strlen(".history"),
600 nextWALFileType
= XLOG_HISTORY
;
601 if (RestoreWALFileForRecovery())
607 fprintf(stderr
, "history file not found\n");
617 while (!CustomizableNextWALFileReady() && !triggered
)
620 pg_usleep(sleeptime
* 1000000L);
627 fprintf(stderr
, "\nsignaled to exit\n");
636 fprintf(stderr
, "\nWAL file not present yet.");
638 fprintf(stderr
, " Checking for trigger file...");
642 waittime
+= sleeptime
;
644 if (!triggered
&& (CheckForExternalTrigger() || (waittime
>= maxwaittime
&& maxwaittime
> 0)))
647 if (debug
&& waittime
>= maxwaittime
&& maxwaittime
> 0)
648 fprintf(stderr
, "\nTimed out after %d seconds\n", waittime
);
657 exit(1); /* Normal exit, with non-zero */
660 * Once we have restored this file successfully we can remove some prior
661 * WAL files. If this restore fails we musn't remove any file because some
662 * of them will be requested again immediately after the failed restore,
663 * or when we restart recovery.
665 if (RestoreWALFileForRecovery() && need_cleanup
)
666 CustomizableCleanupPriorWALFiles();