PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xlogarchive.c File Reference
#include "postgres.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "miscadmin.h"
#include "postmaster/startup.h"
#include "replication/walsender.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/pmsignal.h"
Include dependency graph for xlogarchive.c:

Go to the source code of this file.

Functions

bool RestoreArchivedFile (char *path, const char *xlogfname, const char *recovername, off_t expectedSize, bool cleanupEnabled)
 
void ExecuteRecoveryCommand (char *command, char *commandName, bool failOnSignal)
 
void KeepFileRestoredFromArchive (char *path, char *xlogfname)
 
void XLogArchiveNotify (const char *xlog)
 
void XLogArchiveNotifySeg (XLogSegNo segno)
 
void XLogArchiveForceDone (const char *xlog)
 
bool XLogArchiveCheckDone (const char *xlog)
 
bool XLogArchiveIsBusy (const char *xlog)
 
bool XLogArchiveIsReadyOrDone (const char *xlog)
 
bool XLogArchiveIsReady (const char *xlog)
 
void XLogArchiveCleanup (const char *xlog)
 

Function Documentation

void ExecuteRecoveryCommand ( char *  command,
char *  commandName,
bool  failOnSignal 
)

Definition at line 330 of file xlogarchive.c.

References Assert, DEBUG3, ereport, errmsg(), errmsg_internal(), FATAL, GetOldestRestartPoint(), MAXPGPATH, signaled, StrNCpy, wait_result_to_str(), WARNING, WEXITSTATUS, WIFSIGNALED, XLByteToSeg, and XLogFileName.

Referenced by CreateRestartPoint(), and StartupXLOG().

331 {
332  char xlogRecoveryCmd[MAXPGPATH];
333  char lastRestartPointFname[MAXPGPATH];
334  char *dp;
335  char *endp;
336  const char *sp;
337  int rc;
338  bool signaled;
339  XLogSegNo restartSegNo;
340  XLogRecPtr restartRedoPtr;
341  TimeLineID restartTli;
342 
343  Assert(command && commandName);
344 
345  /*
346  * Calculate the archive file cutoff point for use during log shipping
347  * replication. All files earlier than this point can be deleted from the
348  * archive, though there is no requirement to do so.
349  */
350  GetOldestRestartPoint(&restartRedoPtr, &restartTli);
351  XLByteToSeg(restartRedoPtr, restartSegNo);
352  XLogFileName(lastRestartPointFname, restartTli, restartSegNo);
353 
354  /*
355  * construct the command to be executed
356  */
357  dp = xlogRecoveryCmd;
358  endp = xlogRecoveryCmd + MAXPGPATH - 1;
359  *endp = '\0';
360 
361  for (sp = command; *sp; sp++)
362  {
363  if (*sp == '%')
364  {
365  switch (sp[1])
366  {
367  case 'r':
368  /* %r: filename of last restartpoint */
369  sp++;
370  StrNCpy(dp, lastRestartPointFname, endp - dp);
371  dp += strlen(dp);
372  break;
373  case '%':
374  /* convert %% to a single % */
375  sp++;
376  if (dp < endp)
377  *dp++ = *sp;
378  break;
379  default:
380  /* otherwise treat the % as not special */
381  if (dp < endp)
382  *dp++ = *sp;
383  break;
384  }
385  }
386  else
387  {
388  if (dp < endp)
389  *dp++ = *sp;
390  }
391  }
392  *dp = '\0';
393 
394  ereport(DEBUG3,
395  (errmsg_internal("executing %s \"%s\"", commandName, command)));
396 
397  /*
398  * execute the constructed command
399  */
400  rc = system(xlogRecoveryCmd);
401  if (rc != 0)
402  {
403  /*
404  * If the failure was due to any sort of signal, it's best to punt and
405  * abort recovery. See also detailed comments on signals in
406  * RestoreArchivedFile().
407  */
408  signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
409 
410  ereport((signaled && failOnSignal) ? FATAL : WARNING,
411  /*------
412  translator: First %s represents a recovery.conf parameter name like
413  "recovery_end_command", the 2nd is the value of that parameter, the
414  third an already translated error message. */
415  (errmsg("%s \"%s\": %s", commandName,
416  command, wait_result_to_str(rc))));
417  }
418 }
uint32 TimeLineID
Definition: xlogdefs.h:45
#define DEBUG3
Definition: elog.h:23
void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli)
Definition: xlog.c:11033
#define XLogFileName(fname, tli, logSegNo)
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:34
#define WIFSIGNALED(w)
Definition: win32.h:181
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
uint64 XLogSegNo
Definition: xlogdefs.h:34
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define XLByteToSeg(xlrp, logSegNo)
#define WEXITSTATUS(w)
Definition: win32.h:182
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:671
#define StrNCpy(dst, src, len)
Definition: c.h:826
int errmsg(const char *fmt,...)
Definition: elog.c:797
static volatile sig_atomic_t signaled
Definition: pg_standby.c:51
void KeepFileRestoredFromArchive ( char *  path,
char *  xlogfname 
)

Definition at line 427 of file xlogarchive.c.

References ARCHIVE_MODE_ALWAYS, durable_rename(), ereport, errcode_for_file_access(), errmsg(), ERROR, FATAL, MAXPGPATH, snprintf(), strlcpy(), unlink(), WalSndRqstFileReload(), WalSndWakeup(), XLogArchiveForceDone(), XLogArchiveMode, XLogArchiveNotify(), XLOGDIR, and xlogfpath.

Referenced by readTimeLineHistory(), restoreTimeLineHistoryFiles(), and XLogFileRead().

428 {
429  char xlogfpath[MAXPGPATH];
430  bool reload = false;
431  struct stat statbuf;
432 
433  snprintf(xlogfpath, MAXPGPATH, XLOGDIR "/%s", xlogfname);
434 
435  if (stat(xlogfpath, &statbuf) == 0)
436  {
437  char oldpath[MAXPGPATH];
438 
439 #ifdef WIN32
440  static unsigned int deletedcounter = 1;
441 
442  /*
443  * On Windows, if another process (e.g a walsender process) holds the
444  * file open in FILE_SHARE_DELETE mode, unlink will succeed, but the
445  * file will still show up in directory listing until the last handle
446  * is closed, and we cannot rename the new file in its place until
447  * that. To avoid that problem, rename the old file to a temporary
448  * name first. Use a counter to create a unique filename, because the
449  * same file might be restored from the archive multiple times, and a
450  * walsender could still be holding onto an old deleted version of it.
451  */
452  snprintf(oldpath, MAXPGPATH, "%s.deleted%u",
453  xlogfpath, deletedcounter++);
454  if (rename(xlogfpath, oldpath) != 0)
455  {
456  ereport(ERROR,
458  errmsg("could not rename file \"%s\" to \"%s\": %m",
459  xlogfpath, oldpath)));
460  }
461 #else
462  /* same-size buffers, so this never truncates */
463  strlcpy(oldpath, xlogfpath, MAXPGPATH);
464 #endif
465  if (unlink(oldpath) != 0)
466  ereport(FATAL,
468  errmsg("could not remove file \"%s\": %m",
469  xlogfpath)));
470  reload = true;
471  }
472 
473  durable_rename(path, xlogfpath, ERROR);
474 
475  /*
476  * Create .done file forcibly to prevent the restored segment from being
477  * archived again later.
478  */
480  XLogArchiveForceDone(xlogfname);
481  else
482  XLogArchiveNotify(xlogfname);
483 
484  /*
485  * If the existing file was replaced, since walsenders might have it open,
486  * request them to reload a currently-open segment. This is only required
487  * for WAL segments, walsenders don't hold other files open, but there's
488  * no harm in doing this too often, and we don't know what kind of a file
489  * we're dealing with here.
490  */
491  if (reload)
493 
494  /*
495  * Signal walsender that new WAL has arrived. Again, this isn't necessary
496  * if we restored something other than a WAL segment, but it does no harm
497  * either.
498  */
499  WalSndWakeup();
500 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:513
void WalSndRqstFileReload(void)
Definition: walsender.c:2499
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
int XLogArchiveMode
Definition: xlog.c:93
int errcode_for_file_access(void)
Definition: elog.c:598
int unlink(const char *filename)
#define ereport(elevel, rest)
Definition: elog.h:122
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:593
void XLogArchiveForceDone(const char *xlog)
Definition: xlogarchive.c:563
#define XLOGDIR
static char xlogfpath[MAXPGPATH]
Definition: parsexlog.c:43
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg(const char *fmt,...)
Definition: elog.c:797
void WalSndWakeup(void)
Definition: walsender.c:2631
bool RestoreArchivedFile ( char *  path,
const char *  xlogfname,
const char *  recovername,
off_t  expectedSize,
bool  cleanupEnabled 
)

Definition at line 52 of file xlogarchive.c.

References Assert, DEBUG1, DEBUG2, DEBUG3, elevel, ereport, errcode_for_file_access(), errmsg(), errmsg_internal(), FATAL, GetOldestRestartPoint(), LOG, make_native_path(), MAXPGPATH, NULL, PostRestoreCommand(), PreRestoreCommand(), proc_exit(), recoveryRestoreCommand, signaled, snprintf(), StandbyMode, StrNCpy, unlink(), wait_result_to_str(), WEXITSTATUS, WIFSIGNALED, WTERMSIG, XLByteToSeg, XLOGDIR, and XLogFileName.

Referenced by existsTimeLineHistory(), readTimeLineHistory(), restoreTimeLineHistoryFiles(), writeTimeLineHistory(), and XLogFileRead().

55 {
56  char xlogpath[MAXPGPATH];
57  char xlogRestoreCmd[MAXPGPATH];
58  char lastRestartPointFname[MAXPGPATH];
59  char *dp;
60  char *endp;
61  const char *sp;
62  int rc;
63  bool signaled;
64  struct stat stat_buf;
65  XLogSegNo restartSegNo;
66  XLogRecPtr restartRedoPtr;
67  TimeLineID restartTli;
68 
69  /* In standby mode, restore_command might not be supplied */
71  goto not_available;
72 
73  /*
74  * When doing archive recovery, we always prefer an archived log file even
75  * if a file of the same name exists in XLOGDIR. The reason is that the
76  * file in XLOGDIR could be an old, un-filled or partly-filled version
77  * that was copied and restored as part of backing up $PGDATA.
78  *
79  * We could try to optimize this slightly by checking the local copy
80  * lastchange timestamp against the archived copy, but we have no API to
81  * do this, nor can we guarantee that the lastchange timestamp was
82  * preserved correctly when we copied to archive. Our aim is robustness,
83  * so we elect not to do this.
84  *
85  * If we cannot obtain the log file from the archive, however, we will try
86  * to use the XLOGDIR file if it exists. This is so that we can make use
87  * of log segments that weren't yet transferred to the archive.
88  *
89  * Notice that we don't actually overwrite any files when we copy back
90  * from archive because the restore_command may inadvertently restore
91  * inappropriate xlogs, or they may be corrupt, so we may wish to fallback
92  * to the segments remaining in current XLOGDIR later. The
93  * copy-from-archive filename is always the same, ensuring that we don't
94  * run out of disk space on long recoveries.
95  */
96  snprintf(xlogpath, MAXPGPATH, XLOGDIR "/%s", recovername);
97 
98  /*
99  * Make sure there is no existing file named recovername.
100  */
101  if (stat(xlogpath, &stat_buf) != 0)
102  {
103  if (errno != ENOENT)
104  ereport(FATAL,
106  errmsg("could not stat file \"%s\": %m",
107  xlogpath)));
108  }
109  else
110  {
111  if (unlink(xlogpath) != 0)
112  ereport(FATAL,
114  errmsg("could not remove file \"%s\": %m",
115  xlogpath)));
116  }
117 
118  /*
119  * Calculate the archive file cutoff point for use during log shipping
120  * replication. All files earlier than this point can be deleted from the
121  * archive, though there is no requirement to do so.
122  *
123  * If cleanup is not enabled, initialise this with the filename of
124  * InvalidXLogRecPtr, which will prevent the deletion of any WAL files
125  * from the archive because of the alphabetic sorting property of WAL
126  * filenames.
127  *
128  * Once we have successfully located the redo pointer of the checkpoint
129  * from which we start recovery we never request a file prior to the redo
130  * pointer of the last restartpoint. When redo begins we know that we have
131  * successfully located it, so there is no need for additional status
132  * flags to signify the point when we can begin deleting WAL files from
133  * the archive.
134  */
135  if (cleanupEnabled)
136  {
137  GetOldestRestartPoint(&restartRedoPtr, &restartTli);
138  XLByteToSeg(restartRedoPtr, restartSegNo);
139  XLogFileName(lastRestartPointFname, restartTli, restartSegNo);
140  /* we shouldn't need anything earlier than last restart point */
141  Assert(strcmp(lastRestartPointFname, xlogfname) <= 0);
142  }
143  else
144  XLogFileName(lastRestartPointFname, 0, 0L);
145 
146  /*
147  * construct the command to be executed
148  */
149  dp = xlogRestoreCmd;
150  endp = xlogRestoreCmd + MAXPGPATH - 1;
151  *endp = '\0';
152 
153  for (sp = recoveryRestoreCommand; *sp; sp++)
154  {
155  if (*sp == '%')
156  {
157  switch (sp[1])
158  {
159  case 'p':
160  /* %p: relative path of target file */
161  sp++;
162  StrNCpy(dp, xlogpath, endp - dp);
163  make_native_path(dp);
164  dp += strlen(dp);
165  break;
166  case 'f':
167  /* %f: filename of desired file */
168  sp++;
169  StrNCpy(dp, xlogfname, endp - dp);
170  dp += strlen(dp);
171  break;
172  case 'r':
173  /* %r: filename of last restartpoint */
174  sp++;
175  StrNCpy(dp, lastRestartPointFname, endp - dp);
176  dp += strlen(dp);
177  break;
178  case '%':
179  /* convert %% to a single % */
180  sp++;
181  if (dp < endp)
182  *dp++ = *sp;
183  break;
184  default:
185  /* otherwise treat the % as not special */
186  if (dp < endp)
187  *dp++ = *sp;
188  break;
189  }
190  }
191  else
192  {
193  if (dp < endp)
194  *dp++ = *sp;
195  }
196  }
197  *dp = '\0';
198 
199  ereport(DEBUG3,
200  (errmsg_internal("executing restore command \"%s\"",
201  xlogRestoreCmd)));
202 
203  /*
204  * Check signals before restore command and reset afterwards.
205  */
207 
208  /*
209  * Copy xlog from archival storage to XLOGDIR
210  */
211  rc = system(xlogRestoreCmd);
212 
214 
215  if (rc == 0)
216  {
217  /*
218  * command apparently succeeded, but let's make sure the file is
219  * really there now and has the correct size.
220  */
221  if (stat(xlogpath, &stat_buf) == 0)
222  {
223  if (expectedSize > 0 && stat_buf.st_size != expectedSize)
224  {
225  int elevel;
226 
227  /*
228  * If we find a partial file in standby mode, we assume it's
229  * because it's just being copied to the archive, and keep
230  * trying.
231  *
232  * Otherwise treat a wrong-sized file as FATAL to ensure the
233  * DBA would notice it, but is that too strong? We could try
234  * to plow ahead with a local copy of the file ... but the
235  * problem is that there probably isn't one, and we'd
236  * incorrectly conclude we've reached the end of WAL and we're
237  * done recovering ...
238  */
239  if (StandbyMode && stat_buf.st_size < expectedSize)
240  elevel = DEBUG1;
241  else
242  elevel = FATAL;
243  ereport(elevel,
244  (errmsg("archive file \"%s\" has wrong size: %lu instead of %lu",
245  xlogfname,
246  (unsigned long) stat_buf.st_size,
247  (unsigned long) expectedSize)));
248  return false;
249  }
250  else
251  {
252  ereport(LOG,
253  (errmsg("restored log file \"%s\" from archive",
254  xlogfname)));
255  strcpy(path, xlogpath);
256  return true;
257  }
258  }
259  else
260  {
261  /* stat failed */
262  if (errno != ENOENT)
263  ereport(FATAL,
265  errmsg("could not stat file \"%s\": %m",
266  xlogpath)));
267  }
268  }
269 
270  /*
271  * Remember, we rollforward UNTIL the restore fails so failure here is
272  * just part of the process... that makes it difficult to determine
273  * whether the restore failed because there isn't an archive to restore,
274  * or because the administrator has specified the restore program
275  * incorrectly. We have to assume the former.
276  *
277  * However, if the failure was due to any sort of signal, it's best to
278  * punt and abort recovery. (If we "return false" here, upper levels will
279  * assume that recovery is complete and start up the database!) It's
280  * essential to abort on child SIGINT and SIGQUIT, because per spec
281  * system() ignores SIGINT and SIGQUIT while waiting; if we see one of
282  * those it's a good bet we should have gotten it too.
283  *
284  * On SIGTERM, assume we have received a fast shutdown request, and exit
285  * cleanly. It's pure chance whether we receive the SIGTERM first, or the
286  * child process. If we receive it first, the signal handler will call
287  * proc_exit, otherwise we do it here. If we or the child process received
288  * SIGTERM for any other reason than a fast shutdown request, postmaster
289  * will perform an immediate shutdown when it sees us exiting
290  * unexpectedly.
291  *
292  * Per the Single Unix Spec, shells report exit status > 128 when a called
293  * command died on a signal. Also, 126 and 127 are used to report
294  * problems such as an unfindable command; treat those as fatal errors
295  * too.
296  */
297  if (WIFSIGNALED(rc) && WTERMSIG(rc) == SIGTERM)
298  proc_exit(1);
299 
300  signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
301 
302  ereport(signaled ? FATAL : DEBUG2,
303  (errmsg("could not restore file \"%s\" from archive: %s",
304  xlogfname, wait_result_to_str(rc))));
305 
306 not_available:
307 
308  /*
309  * if an archived file is not available, there might still be a version of
310  * this file in XLOGDIR, so return that as the filename to open.
311  *
312  * In many recovery scenarios we expect this to fail also, but if so that
313  * just means we've reached the end of WAL.
314  */
315  snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
316  return false;
317 }
bool StandbyMode
Definition: xlog.c:276
#define DEBUG1
Definition: elog.h:25
uint32 TimeLineID
Definition: xlogdefs.h:45
#define DEBUG3
Definition: elog.h:23
void make_native_path(char *path)
Definition: path.c:166
void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli)
Definition: xlog.c:11033
#define XLogFileName(fname, tli, logSegNo)
void proc_exit(int code)
Definition: ipc.c:99
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define LOG
Definition: elog.h:26
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:34
void PostRestoreCommand(void)
Definition: startup.c:240
struct stat stat_buf
Definition: pg_standby.c:101
#define WIFSIGNALED(w)
Definition: win32.h:181
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
char * recoveryRestoreCommand
Definition: xlog.c:256
#define DEBUG2
Definition: elog.h:24
uint64 XLogSegNo
Definition: xlogdefs.h:34
int errcode_for_file_access(void)
Definition: elog.c:598
int unlink(const char *filename)
#define ereport(elevel, rest)
Definition: elog.h:122
static int elevel
Definition: vacuumlazy.c:136
#define XLOGDIR
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define XLByteToSeg(xlrp, logSegNo)
#define WEXITSTATUS(w)
Definition: win32.h:182
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:671
#define StrNCpy(dst, src, len)
Definition: c.h:826
void PreRestoreCommand(void)
Definition: startup.c:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define WTERMSIG(w)
Definition: win32.h:183
static volatile sig_atomic_t signaled
Definition: pg_standby.c:51
bool XLogArchiveCheckDone ( const char *  xlog)

Definition at line 618 of file xlogarchive.c.

References MAXPGPATH, StatusFilePath, XLogArchiveNotify(), and XLogArchivingActive.

Referenced by CleanupBackupHistory(), and RemoveOldXlogFiles().

619 {
620  char archiveStatusPath[MAXPGPATH];
621  struct stat stat_buf;
622 
623  /* Always deletable if archiving is off */
624  if (!XLogArchivingActive())
625  return true;
626 
627  /* First check for .done --- this means archiver is done with it */
628  StatusFilePath(archiveStatusPath, xlog, ".done");
629  if (stat(archiveStatusPath, &stat_buf) == 0)
630  return true;
631 
632  /* check for .ready --- this means archiver is still busy with it */
633  StatusFilePath(archiveStatusPath, xlog, ".ready");
634  if (stat(archiveStatusPath, &stat_buf) == 0)
635  return false;
636 
637  /* Race condition --- maybe archiver just finished, so recheck */
638  StatusFilePath(archiveStatusPath, xlog, ".done");
639  if (stat(archiveStatusPath, &stat_buf) == 0)
640  return true;
641 
642  /* Retry creation of the .ready file */
643  XLogArchiveNotify(xlog);
644  return false;
645 }
#define StatusFilePath(path, xlog, suffix)
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:513
struct stat stat_buf
Definition: pg_standby.c:101
#define MAXPGPATH
#define XLogArchivingActive()
Definition: xlog.h:134
void XLogArchiveCleanup ( const char *  xlog)

Definition at line 751 of file xlogarchive.c.

References MAXPGPATH, StatusFilePath, and unlink().

Referenced by CleanupBackupHistory(), exitArchiveRecovery(), RemoveXlogFile(), and StartupXLOG().

752 {
753  char archiveStatusPath[MAXPGPATH];
754 
755  /* Remove the .done file */
756  StatusFilePath(archiveStatusPath, xlog, ".done");
757  unlink(archiveStatusPath);
758  /* should we complain about failure? */
759 
760  /* Remove the .ready file if present --- normally it shouldn't be */
761  StatusFilePath(archiveStatusPath, xlog, ".ready");
762  unlink(archiveStatusPath);
763  /* should we complain about failure? */
764 }
#define StatusFilePath(path, xlog, suffix)
#define MAXPGPATH
int unlink(const char *filename)
void XLogArchiveForceDone ( const char *  xlog)

Definition at line 563 of file xlogarchive.c.

References AllocateFile(), durable_rename(), ereport, errcode_for_file_access(), errmsg(), fd(), FreeFile(), LOG, MAXPGPATH, NULL, StatusFilePath, and WARNING.

Referenced by KeepFileRestoredFromArchive(), WalReceiverMain(), and XLogWalRcvWrite().

564 {
565  char archiveReady[MAXPGPATH];
566  char archiveDone[MAXPGPATH];
567  struct stat stat_buf;
568  FILE *fd;
569 
570  /* Exit if already known done */
571  StatusFilePath(archiveDone, xlog, ".done");
572  if (stat(archiveDone, &stat_buf) == 0)
573  return;
574 
575  /* If .ready exists, rename it to .done */
576  StatusFilePath(archiveReady, xlog, ".ready");
577  if (stat(archiveReady, &stat_buf) == 0)
578  {
579  (void) durable_rename(archiveReady, archiveDone, WARNING);
580  return;
581  }
582 
583  /* insert an otherwise empty file called <XLOG>.done */
584  fd = AllocateFile(archiveDone, "w");
585  if (fd == NULL)
586  {
587  ereport(LOG,
589  errmsg("could not create archive status file \"%s\": %m",
590  archiveDone)));
591  return;
592  }
593  if (FreeFile(fd))
594  {
595  ereport(LOG,
597  errmsg("could not write archive status file \"%s\": %m",
598  archiveDone)));
599  return;
600  }
601 }
#define StatusFilePath(path, xlog, suffix)
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
struct stat stat_buf
Definition: pg_standby.c:101
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2043
#define ereport(elevel, rest)
Definition: elog.h:122
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:593
#define WARNING
Definition: elog.h:40
#define NULL
Definition: c.h:226
int FreeFile(FILE *file)
Definition: fd.c:2226
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool XLogArchiveIsBusy ( const char *  xlog)

Definition at line 658 of file xlogarchive.c.

References MAXPGPATH, snprintf(), StatusFilePath, and XLOGDIR.

Referenced by do_pg_stop_backup().

659 {
660  char archiveStatusPath[MAXPGPATH];
661  struct stat stat_buf;
662 
663  /* First check for .done --- this means archiver is done with it */
664  StatusFilePath(archiveStatusPath, xlog, ".done");
665  if (stat(archiveStatusPath, &stat_buf) == 0)
666  return false;
667 
668  /* check for .ready --- this means archiver is still busy with it */
669  StatusFilePath(archiveStatusPath, xlog, ".ready");
670  if (stat(archiveStatusPath, &stat_buf) == 0)
671  return true;
672 
673  /* Race condition --- maybe archiver just finished, so recheck */
674  StatusFilePath(archiveStatusPath, xlog, ".done");
675  if (stat(archiveStatusPath, &stat_buf) == 0)
676  return false;
677 
678  /*
679  * Check to see if the WAL file has been removed by checkpoint, which
680  * implies it has already been archived, and explains why we can't see a
681  * status file for it.
682  */
683  snprintf(archiveStatusPath, MAXPGPATH, XLOGDIR "/%s", xlog);
684  if (stat(archiveStatusPath, &stat_buf) != 0 &&
685  errno == ENOENT)
686  return false;
687 
688  return true;
689 }
#define StatusFilePath(path, xlog, suffix)
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
struct stat stat_buf
Definition: pg_standby.c:101
#define MAXPGPATH
#define XLOGDIR
bool XLogArchiveIsReady ( const char *  xlog)

Definition at line 733 of file xlogarchive.c.

References MAXPGPATH, and StatusFilePath.

Referenced by RemoveNonParentXlogFiles().

734 {
735  char archiveStatusPath[MAXPGPATH];
736  struct stat stat_buf;
737 
738  StatusFilePath(archiveStatusPath, xlog, ".ready");
739  if (stat(archiveStatusPath, &stat_buf) == 0)
740  return true;
741 
742  return false;
743 }
#define StatusFilePath(path, xlog, suffix)
struct stat stat_buf
Definition: pg_standby.c:101
#define MAXPGPATH
bool XLogArchiveIsReadyOrDone ( const char *  xlog)

Definition at line 703 of file xlogarchive.c.

References MAXPGPATH, and StatusFilePath.

Referenced by StartupXLOG().

704 {
705  char archiveStatusPath[MAXPGPATH];
706  struct stat stat_buf;
707 
708  /* First check for .done --- this means archiver is done with it */
709  StatusFilePath(archiveStatusPath, xlog, ".done");
710  if (stat(archiveStatusPath, &stat_buf) == 0)
711  return true;
712 
713  /* check for .ready --- this means archiver is still busy with it */
714  StatusFilePath(archiveStatusPath, xlog, ".ready");
715  if (stat(archiveStatusPath, &stat_buf) == 0)
716  return true;
717 
718  /* Race condition --- maybe archiver just finished, so recheck */
719  StatusFilePath(archiveStatusPath, xlog, ".done");
720  if (stat(archiveStatusPath, &stat_buf) == 0)
721  return true;
722 
723  return false;
724 }
#define StatusFilePath(path, xlog, suffix)
struct stat stat_buf
Definition: pg_standby.c:101
#define MAXPGPATH
void XLogArchiveNotify ( const char *  xlog)

Definition at line 513 of file xlogarchive.c.

References AllocateFile(), ereport, errcode_for_file_access(), errmsg(), fd(), FreeFile(), IsUnderPostmaster, LOG, MAXPGPATH, NULL, PMSIGNAL_WAKEN_ARCHIVER, SendPostmasterSignal(), and StatusFilePath.

Referenced by KeepFileRestoredFromArchive(), StartupXLOG(), WalReceiverMain(), writeTimeLineHistory(), XLogArchiveCheckDone(), XLogArchiveNotifySeg(), and XLogWalRcvWrite().

514 {
515  char archiveStatusPath[MAXPGPATH];
516  FILE *fd;
517 
518  /* insert an otherwise empty file called <XLOG>.ready */
519  StatusFilePath(archiveStatusPath, xlog, ".ready");
520  fd = AllocateFile(archiveStatusPath, "w");
521  if (fd == NULL)
522  {
523  ereport(LOG,
525  errmsg("could not create archive status file \"%s\": %m",
526  archiveStatusPath)));
527  return;
528  }
529  if (FreeFile(fd))
530  {
531  ereport(LOG,
533  errmsg("could not write archive status file \"%s\": %m",
534  archiveStatusPath)));
535  return;
536  }
537 
538  /* Notify archiver that it's got something to do */
539  if (IsUnderPostmaster)
541 }
#define StatusFilePath(path, xlog, suffix)
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define MAXPGPATH
bool IsUnderPostmaster
Definition: globals.c:100
int errcode_for_file_access(void)
Definition: elog.c:598
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2043
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
int FreeFile(FILE *file)
Definition: fd.c:2226
int errmsg(const char *fmt,...)
Definition: elog.c:797
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:113
void XLogArchiveNotifySeg ( XLogSegNo  segno)

Definition at line 547 of file xlogarchive.c.

References MAXFNAMELEN, ThisTimeLineID, XLogArchiveNotify(), and XLogFileName.

Referenced by XLogWrite().

548 {
549  char xlog[MAXFNAMELEN];
550 
551  XLogFileName(xlog, ThisTimeLineID, segno);
552  XLogArchiveNotify(xlog);
553 }
#define XLogFileName(fname, tli, logSegNo)
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:513
#define MAXFNAMELEN
TimeLineID ThisTimeLineID
Definition: xlog.c:178