1 /*-------------------------------------------------------------------------
4 * Functions for archiving WAL files and restoring from the archive.
7 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/access/transam/xlogarchive.c
12 *-------------------------------------------------------------------------
22 #include "access/xlog.h"
23 #include "access/xlog_internal.h"
24 #include "miscadmin.h"
25 #include "postmaster/startup.h"
26 #include "replication/walsender.h"
27 #include "storage/fd.h"
28 #include "storage/ipc.h"
29 #include "storage/lwlock.h"
30 #include "storage/pmsignal.h"
33 * Attempt to retrieve the specified file from off-line archival storage.
34 * If successful, fill "path" with its complete path (note that this will be
35 * a temp file name that doesn't follow the normal naming convention), and
38 * If not successful, fill "path" with the name of the normal on-line file
39 * (which may or may not actually exist, but we'll try to use it), and return
42 * For fixed-size files, the caller may pass the expected size as an
43 * additional crosscheck on successful recovery. If the file size is not
44 * known, set expectedSize = 0.
46 * When 'cleanupEnabled' is false, refrain from deleting any old WAL segments
47 * in the archive. This is used when fetching the initial checkpoint record,
48 * when we are not yet sure how far back we need the WAL.
51 RestoreArchivedFile(char *path, const char *xlogfname,
52 const char *recovername, off_t expectedSize,
55 char xlogpath[MAXPGPATH];
56 char xlogRestoreCmd[MAXPGPATH];
57 char lastRestartPointFname[MAXPGPATH];
64 XLogSegNo restartSegNo;
65 XLogRecPtr restartRedoPtr;
66 TimeLineID restartTli;
68 /* In standby mode, restore_command might not be supplied */
69 if (recoveryRestoreCommand == NULL)
73 * When doing archive recovery, we always prefer an archived log file even
74 * if a file of the same name exists in XLOGDIR. The reason is that the
75 * file in XLOGDIR could be an old, un-filled or partly-filled version
76 * that was copied and restored as part of backing up $PGDATA.
78 * We could try to optimize this slightly by checking the local copy
79 * lastchange timestamp against the archived copy, but we have no API to
80 * do this, nor can we guarantee that the lastchange timestamp was
81 * preserved correctly when we copied to archive. Our aim is robustness,
82 * so we elect not to do this.
84 * If we cannot obtain the log file from the archive, however, we will try
85 * to use the XLOGDIR file if it exists. This is so that we can make use
86 * of log segments that weren't yet transferred to the archive.
88 * Notice that we don't actually overwrite any files when we copy back
89 * from archive because the restore_command may inadvertently restore
90 * inappropriate xlogs, or they may be corrupt, so we may wish to fallback
91 * to the segments remaining in current XLOGDIR later. The
92 * copy-from-archive filename is always the same, ensuring that we don't
93 * run out of disk space on long recoveries.
95 snprintf(xlogpath, MAXPGPATH, XLOGDIR "/%s", recovername);
98 * Make sure there is no existing file named recovername.
100 if (stat(xlogpath, &stat_buf) != 0)
104 (errcode_for_file_access(),
105 errmsg("could not stat file \"%s\": %m",
110 if (unlink(xlogpath) != 0)
112 (errcode_for_file_access(),
113 errmsg("could not remove file \"%s\": %m",
118 * Calculate the archive file cutoff point for use during log shipping
119 * replication. All files earlier than this point can be deleted from the
120 * archive, though there is no requirement to do so.
122 * If cleanup is not enabled, initialise this with the filename of
123 * InvalidXLogRecPtr, which will prevent the deletion of any WAL files
124 * from the archive because of the alphabetic sorting property of WAL
127 * Once we have successfully located the redo pointer of the checkpoint
128 * from which we start recovery we never request a file prior to the redo
129 * pointer of the last restartpoint. When redo begins we know that we have
130 * successfully located it, so there is no need for additional status
131 * flags to signify the point when we can begin deleting WAL files from
136 GetOldestRestartPoint(&restartRedoPtr, &restartTli);
137 XLByteToSeg(restartRedoPtr, restartSegNo);
138 XLogFileName(lastRestartPointFname, restartTli, restartSegNo);
139 /* we shouldn't need anything earlier than last restart point */
140 Assert(strcmp(lastRestartPointFname, xlogfname) <= 0);
143 XLogFileName(lastRestartPointFname, 0, 0L);
146 * construct the command to be executed
149 endp = xlogRestoreCmd + MAXPGPATH - 1;
152 for (sp = recoveryRestoreCommand; *sp; sp++)
159 /* %p: relative path of target file */
161 StrNCpy(dp, xlogpath, endp - dp);
162 make_native_path(dp);
166 /* %f: filename of desired file */
168 StrNCpy(dp, xlogfname, endp - dp);
172 /* %r: filename of last restartpoint */
174 StrNCpy(dp, lastRestartPointFname, endp - dp);
178 /* convert %% to a single % */
184 /* otherwise treat the % as not special */
199 (errmsg_internal("executing restore command \"%s\"",
203 * Check signals before restore command and reset afterwards.
208 * Copy xlog from archival storage to XLOGDIR
210 rc = system(xlogRestoreCmd);
212 PostRestoreCommand();
217 * command apparently succeeded, but let's make sure the file is
218 * really there now and has the correct size.
220 if (stat(xlogpath, &stat_buf) == 0)
222 if (expectedSize > 0 && stat_buf.st_size != expectedSize)
227 * If we find a partial file in standby mode, we assume it's
228 * because it's just being copied to the archive, and keep
231 * Otherwise treat a wrong-sized file as FATAL to ensure the
232 * DBA would notice it, but is that too strong? We could try
233 * to plow ahead with a local copy of the file ... but the
234 * problem is that there probably isn't one, and we'd
235 * incorrectly conclude we've reached the end of WAL and we're
236 * done recovering ...
238 if (StandbyMode && stat_buf.st_size < expectedSize)
243 (errmsg("archive file \"%s\" has wrong size: %lu instead of %lu",
245 (unsigned long) stat_buf.st_size,
246 (unsigned long) expectedSize)));
252 (errmsg("restored log file \"%s\" from archive",
254 strcpy(path, xlogpath);
263 (errcode_for_file_access(),
264 errmsg("could not stat file \"%s\": %m",
270 * Remember, we rollforward UNTIL the restore fails so failure here is
271 * just part of the process... that makes it difficult to determine
272 * whether the restore failed because there isn't an archive to restore,
273 * or because the administrator has specified the restore program
274 * incorrectly. We have to assume the former.
276 * However, if the failure was due to any sort of signal, it's best to
277 * punt and abort recovery. (If we "return false" here, upper levels will
278 * assume that recovery is complete and start up the database!) It's
279 * essential to abort on child SIGINT and SIGQUIT, because per spec
280 * system() ignores SIGINT and SIGQUIT while waiting; if we see one of
281 * those it's a good bet we should have gotten it too.
283 * On SIGTERM, assume we have received a fast shutdown request, and exit
284 * cleanly. It's pure chance whether we receive the SIGTERM first, or the
285 * child process. If we receive it first, the signal handler will call
286 * proc_exit, otherwise we do it here. If we or the child process received
287 * SIGTERM for any other reason than a fast shutdown request, postmaster
288 * will perform an immediate shutdown when it sees us exiting
291 * Per the Single Unix Spec, shells report exit status > 128 when a called
292 * command died on a signal. Also, 126 and 127 are used to report
293 * problems such as an unfindable command; treat those as fatal errors
296 if (WIFSIGNALED(rc) && WTERMSIG(rc) == SIGTERM)
299 signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
301 ereport(signaled ? FATAL : DEBUG2,
302 (errmsg("could not restore file \"%s\" from archive: %s",
303 xlogfname, wait_result_to_str(rc))));
308 * if an archived file is not available, there might still be a version of
309 * this file in XLOGDIR, so return that as the filename to open.
311 * In many recovery scenarios we expect this to fail also, but if so that
312 * just means we've reached the end of WAL.
314 snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
319 * Attempt to execute an external shell command during recovery.
321 * 'command' is the shell command to be executed, 'commandName' is a
322 * human-readable name describing the command emitted in the logs. If
323 * 'failOnSignal' is true and the command is killed by a signal, a FATAL
324 * error is thrown. Otherwise a WARNING is emitted.
326 * This is currently used for recovery_end_command and archive_cleanup_command.
329 ExecuteRecoveryCommand(char *command, char *commandName, bool failOnSignal)
331 char xlogRecoveryCmd[MAXPGPATH];
332 char lastRestartPointFname[MAXPGPATH];
338 XLogSegNo restartSegNo;
339 XLogRecPtr restartRedoPtr;
340 TimeLineID restartTli;
342 Assert(command && commandName);
345 * Calculate the archive file cutoff point for use during log shipping
346 * replication. All files earlier than this point can be deleted from the
347 * archive, though there is no requirement to do so.
349 GetOldestRestartPoint(&restartRedoPtr, &restartTli);
350 XLByteToSeg(restartRedoPtr, restartSegNo);
351 XLogFileName(lastRestartPointFname, restartTli, restartSegNo);
354 * construct the command to be executed
356 dp = xlogRecoveryCmd;
357 endp = xlogRecoveryCmd + MAXPGPATH - 1;
360 for (sp = command; *sp; sp++)
367 /* %r: filename of last restartpoint */
369 StrNCpy(dp, lastRestartPointFname, endp - dp);
373 /* convert %% to a single % */
379 /* otherwise treat the % as not special */
394 (errmsg_internal("executing %s \"%s\"", commandName, command)));
397 * execute the constructed command
399 rc = system(xlogRecoveryCmd);
403 * If the failure was due to any sort of signal, it's best to punt and
404 * abort recovery. See also detailed comments on signals in
405 * RestoreArchivedFile().
407 signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
409 ereport((signaled && failOnSignal) ? FATAL : WARNING,
411 translator: First %s represents a recovery.conf parameter name like
412 "recovery_end_command", the 2nd is the value of that parameter, the
413 third an already translated error message. */
414 (errmsg("%s \"%s\": %s", commandName,
415 command, wait_result_to_str(rc))));
421 * A file was restored from the archive under a temporary filename (path),
422 * and now we want to keep it. Rename it under the permanent filename in
423 * in pg_wal (xlogfname), replacing any existing file with the same name.
426 KeepFileRestoredFromArchive(char *path, char *xlogfname)
428 char xlogfpath[MAXPGPATH];
432 snprintf(xlogfpath, MAXPGPATH, XLOGDIR "/%s", xlogfname);
434 if (stat(xlogfpath, &statbuf) == 0)
436 char oldpath[MAXPGPATH];
439 static unsigned int deletedcounter = 1;
442 * On Windows, if another process (e.g a walsender process) holds the
443 * file open in FILE_SHARE_DELETE mode, unlink will succeed, but the
444 * file will still show up in directory listing until the last handle
445 * is closed, and we cannot rename the new file in its place until
446 * that. To avoid that problem, rename the old file to a temporary
447 * name first. Use a counter to create a unique filename, because the
448 * same file might be restored from the archive multiple times, and a
449 * walsender could still be holding onto an old deleted version of it.
451 snprintf(oldpath, MAXPGPATH, "%s.deleted%u",
452 xlogfpath, deletedcounter++);
453 if (rename(xlogfpath, oldpath) != 0)
456 (errcode_for_file_access(),
457 errmsg("could not rename file \"%s\" to \"%s\": %m",
458 xlogfpath, oldpath)));
461 /* same-size buffers, so this never truncates */
462 strlcpy(oldpath, xlogfpath, MAXPGPATH);
464 if (unlink(oldpath) != 0)
466 (errcode_for_file_access(),
467 errmsg("could not remove file \"%s\": %m",
472 durable_rename(path, xlogfpath, ERROR);
475 * Create .done file forcibly to prevent the restored segment from being
476 * archived again later.
478 if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS)
479 XLogArchiveForceDone(xlogfname);
481 XLogArchiveNotify(xlogfname);
484 * If the existing file was replaced, since walsenders might have it open,
485 * request them to reload a currently-open segment. This is only required
486 * for WAL segments, walsenders don't hold other files open, but there's
487 * no harm in doing this too often, and we don't know what kind of a file
488 * we're dealing with here.
491 WalSndRqstFileReload();
494 * Signal walsender that new WAL has arrived. Again, this isn't necessary
495 * if we restored something other than a WAL segment, but it does no harm
504 * Create an archive notification file
506 * The name of the notification file is the message that will be picked up
507 * by the archiver, e.g. we write 0000000100000001000000C6.ready
508 * and the archiver then knows to archive XLOGDIR/0000000100000001000000C6,
509 * then when complete, rename it to 0000000100000001000000C6.done
512 XLogArchiveNotify(const char *xlog)
514 char archiveStatusPath[MAXPGPATH];
517 /* insert an otherwise empty file called <XLOG>.ready */
518 StatusFilePath(archiveStatusPath, xlog, ".ready");
519 fd = AllocateFile(archiveStatusPath, "w");
523 (errcode_for_file_access(),
524 errmsg("could not create archive status file \"%s\": %m",
525 archiveStatusPath)));
531 (errcode_for_file_access(),
532 errmsg("could not write archive status file \"%s\": %m",
533 archiveStatusPath)));
537 /* Notify archiver that it's got something to do */
538 if (IsUnderPostmaster)
539 SendPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER);
543 * Convenience routine to notify using segment number representation of filename
546 XLogArchiveNotifySeg(XLogSegNo segno)
548 char xlog[MAXFNAMELEN];
550 XLogFileName(xlog, ThisTimeLineID, segno);
551 XLogArchiveNotify(xlog);
555 * XLogArchiveForceDone
557 * Emit notification forcibly that an XLOG segment file has been successfully
558 * archived, by creating <XLOG>.done regardless of whether <XLOG>.ready
562 XLogArchiveForceDone(const char *xlog)
564 char archiveReady[MAXPGPATH];
565 char archiveDone[MAXPGPATH];
566 struct stat stat_buf;
569 /* Exit if already known done */
570 StatusFilePath(archiveDone, xlog, ".done");
571 if (stat(archiveDone, &stat_buf) == 0)
574 /* If .ready exists, rename it to .done */
575 StatusFilePath(archiveReady, xlog, ".ready");
576 if (stat(archiveReady, &stat_buf) == 0)
578 (void) durable_rename(archiveReady, archiveDone, WARNING);
582 /* insert an otherwise empty file called <XLOG>.done */
583 fd = AllocateFile(archiveDone, "w");
587 (errcode_for_file_access(),
588 errmsg("could not create archive status file \"%s\": %m",
595 (errcode_for_file_access(),
596 errmsg("could not write archive status file \"%s\": %m",
603 * XLogArchiveCheckDone
605 * This is called when we are ready to delete or recycle an old XLOG segment
606 * file or backup history file. If it is okay to delete it then return true.
607 * If it is not time to delete it, make sure a .ready file exists, and return
610 * If <XLOG>.done exists, then return true; else if <XLOG>.ready exists,
611 * then return false; else create <XLOG>.ready and return false.
613 * The reason we do things this way is so that if the original attempt to
614 * create <XLOG>.ready fails, we'll retry during subsequent checkpoints.
617 XLogArchiveCheckDone(const char *xlog)
619 char archiveStatusPath[MAXPGPATH];
620 struct stat stat_buf;
622 /* Always deletable if archiving is off */
623 if (!XLogArchivingActive())
626 /* First check for .done --- this means archiver is done with it */
627 StatusFilePath(archiveStatusPath, xlog, ".done");
628 if (stat(archiveStatusPath, &stat_buf) == 0)
631 /* check for .ready --- this means archiver is still busy with it */
632 StatusFilePath(archiveStatusPath, xlog, ".ready");
633 if (stat(archiveStatusPath, &stat_buf) == 0)
636 /* Race condition --- maybe archiver just finished, so recheck */
637 StatusFilePath(archiveStatusPath, xlog, ".done");
638 if (stat(archiveStatusPath, &stat_buf) == 0)
641 /* Retry creation of the .ready file */
642 XLogArchiveNotify(xlog);
649 * Check to see if an XLOG segment file is still unarchived.
650 * This is almost but not quite the inverse of XLogArchiveCheckDone: in
651 * the first place we aren't chartered to recreate the .ready file, and
652 * in the second place we should consider that if the file is already gone
653 * then it's not busy. (This check is needed to handle the race condition
654 * that a checkpoint already deleted the no-longer-needed file.)
657 XLogArchiveIsBusy(const char *xlog)
659 char archiveStatusPath[MAXPGPATH];
660 struct stat stat_buf;
662 /* First check for .done --- this means archiver is done with it */
663 StatusFilePath(archiveStatusPath, xlog, ".done");
664 if (stat(archiveStatusPath, &stat_buf) == 0)
667 /* check for .ready --- this means archiver is still busy with it */
668 StatusFilePath(archiveStatusPath, xlog, ".ready");
669 if (stat(archiveStatusPath, &stat_buf) == 0)
672 /* Race condition --- maybe archiver just finished, so recheck */
673 StatusFilePath(archiveStatusPath, xlog, ".done");
674 if (stat(archiveStatusPath, &stat_buf) == 0)
678 * Check to see if the WAL file has been removed by checkpoint, which
679 * implies it has already been archived, and explains why we can't see a
680 * status file for it.
682 snprintf(archiveStatusPath, MAXPGPATH, XLOGDIR "/%s", xlog);
683 if (stat(archiveStatusPath, &stat_buf) != 0 &&
691 * XLogArchiveIsReadyOrDone
693 * Check to see if an XLOG segment file has a .ready or .done file.
694 * This is similar to XLogArchiveIsBusy(), but returns true if the file
695 * is already archived or is about to be archived.
697 * This is currently only used at recovery. During normal operation this
698 * would be racy: the file might get removed or marked with .ready as we're
699 * checking it, or immediately after we return.
702 XLogArchiveIsReadyOrDone(const char *xlog)
704 char archiveStatusPath[MAXPGPATH];
705 struct stat stat_buf;
707 /* First check for .done --- this means archiver is done with it */
708 StatusFilePath(archiveStatusPath, xlog, ".done");
709 if (stat(archiveStatusPath, &stat_buf) == 0)
712 /* check for .ready --- this means archiver is still busy with it */
713 StatusFilePath(archiveStatusPath, xlog, ".ready");
714 if (stat(archiveStatusPath, &stat_buf) == 0)
717 /* Race condition --- maybe archiver just finished, so recheck */
718 StatusFilePath(archiveStatusPath, xlog, ".done");
719 if (stat(archiveStatusPath, &stat_buf) == 0)
728 * Check to see if an XLOG segment file has an archive notification (.ready)
732 XLogArchiveIsReady(const char *xlog)
734 char archiveStatusPath[MAXPGPATH];
735 struct stat stat_buf;
737 StatusFilePath(archiveStatusPath, xlog, ".ready");
738 if (stat(archiveStatusPath, &stat_buf) == 0)
747 * Cleanup archive notification file(s) for a particular xlog segment
750 XLogArchiveCleanup(const char *xlog)
752 char archiveStatusPath[MAXPGPATH];
754 /* Remove the .done file */
755 StatusFilePath(archiveStatusPath, xlog, ".done");
756 unlink(archiveStatusPath);
757 /* should we complain about failure? */
759 /* Remove the .ready file if present --- normally it shouldn't be */
760 StatusFilePath(archiveStatusPath, xlog, ".ready");
761 unlink(archiveStatusPath);
762 /* should we complain about failure? */