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/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 329 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().

330 {
331  char xlogRecoveryCmd[MAXPGPATH];
332  char lastRestartPointFname[MAXPGPATH];
333  char *dp;
334  char *endp;
335  const char *sp;
336  int rc;
337  bool signaled;
338  XLogSegNo restartSegNo;
339  XLogRecPtr restartRedoPtr;
340  TimeLineID restartTli;
341 
342  Assert(command && commandName);
343 
344  /*
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.
348  */
349  GetOldestRestartPoint(&restartRedoPtr, &restartTli);
350  XLByteToSeg(restartRedoPtr, restartSegNo);
351  XLogFileName(lastRestartPointFname, restartTli, restartSegNo);
352 
353  /*
354  * construct the command to be executed
355  */
356  dp = xlogRecoveryCmd;
357  endp = xlogRecoveryCmd + MAXPGPATH - 1;
358  *endp = '\0';
359 
360  for (sp = command; *sp; sp++)
361  {
362  if (*sp == '%')
363  {
364  switch (sp[1])
365  {
366  case 'r':
367  /* %r: filename of last restartpoint */
368  sp++;
369  StrNCpy(dp, lastRestartPointFname, endp - dp);
370  dp += strlen(dp);
371  break;
372  case '%':
373  /* convert %% to a single % */
374  sp++;
375  if (dp < endp)
376  *dp++ = *sp;
377  break;
378  default:
379  /* otherwise treat the % as not special */
380  if (dp < endp)
381  *dp++ = *sp;
382  break;
383  }
384  }
385  else
386  {
387  if (dp < endp)
388  *dp++ = *sp;
389  }
390  }
391  *dp = '\0';
392 
393  ereport(DEBUG3,
394  (errmsg_internal("executing %s \"%s\"", commandName, command)));
395 
396  /*
397  * execute the constructed command
398  */
399  rc = system(xlogRecoveryCmd);
400  if (rc != 0)
401  {
402  /*
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().
406  */
407  signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
408 
409  ereport((signaled && failOnSignal) ? FATAL : WARNING,
410  /*------
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))));
416  }
417 }
uint32 TimeLineID
Definition: xlogdefs.h:45
#define DEBUG3
Definition: elog.h:23
void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli)
Definition: xlog.c:11133
#define XLogFileName(fname, tli, logSegNo)
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:32
#define WIFSIGNALED(w)
Definition: win32.h:173
#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:174
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
#define StrNCpy(dst, src, len)
Definition: c.h:830
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 426 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().

427 {
428  char xlogfpath[MAXPGPATH];
429  bool reload = false;
430  struct stat statbuf;
431 
432  snprintf(xlogfpath, MAXPGPATH, XLOGDIR "/%s", xlogfname);
433 
434  if (stat(xlogfpath, &statbuf) == 0)
435  {
436  char oldpath[MAXPGPATH];
437 
438 #ifdef WIN32
439  static unsigned int deletedcounter = 1;
440 
441  /*
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.
450  */
451  snprintf(oldpath, MAXPGPATH, "%s.deleted%u",
452  xlogfpath, deletedcounter++);
453  if (rename(xlogfpath, oldpath) != 0)
454  {
455  ereport(ERROR,
457  errmsg("could not rename file \"%s\" to \"%s\": %m",
458  xlogfpath, oldpath)));
459  }
460 #else
461  /* same-size buffers, so this never truncates */
462  strlcpy(oldpath, xlogfpath, MAXPGPATH);
463 #endif
464  if (unlink(oldpath) != 0)
465  ereport(FATAL,
467  errmsg("could not remove file \"%s\": %m",
468  xlogfpath)));
469  reload = true;
470  }
471 
472  durable_rename(path, xlogfpath, ERROR);
473 
474  /*
475  * Create .done file forcibly to prevent the restored segment from being
476  * archived again later.
477  */
479  XLogArchiveForceDone(xlogfname);
480  else
481  XLogArchiveNotify(xlogfname);
482 
483  /*
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.
489  */
490  if (reload)
492 
493  /*
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
496  * either.
497  */
498  WalSndWakeup();
499 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:512
void WalSndRqstFileReload(void)
Definition: walsender.c:2869
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
int XLogArchiveMode
Definition: xlog.c:94
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:562
#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:3017
bool RestoreArchivedFile ( char *  path,
const char *  xlogfname,
const char *  recovername,
off_t  expectedSize,
bool  cleanupEnabled 
)

Definition at line 51 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().

54 {
55  char xlogpath[MAXPGPATH];
56  char xlogRestoreCmd[MAXPGPATH];
57  char lastRestartPointFname[MAXPGPATH];
58  char *dp;
59  char *endp;
60  const char *sp;
61  int rc;
62  bool signaled;
63  struct stat stat_buf;
64  XLogSegNo restartSegNo;
65  XLogRecPtr restartRedoPtr;
66  TimeLineID restartTli;
67 
68  /* In standby mode, restore_command might not be supplied */
70  goto not_available;
71 
72  /*
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.
77  *
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.
83  *
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.
87  *
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.
94  */
95  snprintf(xlogpath, MAXPGPATH, XLOGDIR "/%s", recovername);
96 
97  /*
98  * Make sure there is no existing file named recovername.
99  */
100  if (stat(xlogpath, &stat_buf) != 0)
101  {
102  if (errno != ENOENT)
103  ereport(FATAL,
105  errmsg("could not stat file \"%s\": %m",
106  xlogpath)));
107  }
108  else
109  {
110  if (unlink(xlogpath) != 0)
111  ereport(FATAL,
113  errmsg("could not remove file \"%s\": %m",
114  xlogpath)));
115  }
116 
117  /*
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.
121  *
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
125  * filenames.
126  *
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
132  * the archive.
133  */
134  if (cleanupEnabled)
135  {
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);
141  }
142  else
143  XLogFileName(lastRestartPointFname, 0, 0L);
144 
145  /*
146  * construct the command to be executed
147  */
148  dp = xlogRestoreCmd;
149  endp = xlogRestoreCmd + MAXPGPATH - 1;
150  *endp = '\0';
151 
152  for (sp = recoveryRestoreCommand; *sp; sp++)
153  {
154  if (*sp == '%')
155  {
156  switch (sp[1])
157  {
158  case 'p':
159  /* %p: relative path of target file */
160  sp++;
161  StrNCpy(dp, xlogpath, endp - dp);
162  make_native_path(dp);
163  dp += strlen(dp);
164  break;
165  case 'f':
166  /* %f: filename of desired file */
167  sp++;
168  StrNCpy(dp, xlogfname, endp - dp);
169  dp += strlen(dp);
170  break;
171  case 'r':
172  /* %r: filename of last restartpoint */
173  sp++;
174  StrNCpy(dp, lastRestartPointFname, endp - dp);
175  dp += strlen(dp);
176  break;
177  case '%':
178  /* convert %% to a single % */
179  sp++;
180  if (dp < endp)
181  *dp++ = *sp;
182  break;
183  default:
184  /* otherwise treat the % as not special */
185  if (dp < endp)
186  *dp++ = *sp;
187  break;
188  }
189  }
190  else
191  {
192  if (dp < endp)
193  *dp++ = *sp;
194  }
195  }
196  *dp = '\0';
197 
198  ereport(DEBUG3,
199  (errmsg_internal("executing restore command \"%s\"",
200  xlogRestoreCmd)));
201 
202  /*
203  * Check signals before restore command and reset afterwards.
204  */
206 
207  /*
208  * Copy xlog from archival storage to XLOGDIR
209  */
210  rc = system(xlogRestoreCmd);
211 
213 
214  if (rc == 0)
215  {
216  /*
217  * command apparently succeeded, but let's make sure the file is
218  * really there now and has the correct size.
219  */
220  if (stat(xlogpath, &stat_buf) == 0)
221  {
222  if (expectedSize > 0 && stat_buf.st_size != expectedSize)
223  {
224  int elevel;
225 
226  /*
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
229  * trying.
230  *
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 ...
237  */
238  if (StandbyMode && stat_buf.st_size < expectedSize)
239  elevel = DEBUG1;
240  else
241  elevel = FATAL;
242  ereport(elevel,
243  (errmsg("archive file \"%s\" has wrong size: %lu instead of %lu",
244  xlogfname,
245  (unsigned long) stat_buf.st_size,
246  (unsigned long) expectedSize)));
247  return false;
248  }
249  else
250  {
251  ereport(LOG,
252  (errmsg("restored log file \"%s\" from archive",
253  xlogfname)));
254  strcpy(path, xlogpath);
255  return true;
256  }
257  }
258  else
259  {
260  /* stat failed */
261  if (errno != ENOENT)
262  ereport(FATAL,
264  errmsg("could not stat file \"%s\": %m",
265  xlogpath)));
266  }
267  }
268 
269  /*
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.
275  *
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.
282  *
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
289  * unexpectedly.
290  *
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
294  * too.
295  */
296  if (WIFSIGNALED(rc) && WTERMSIG(rc) == SIGTERM)
297  proc_exit(1);
298 
299  signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
300 
301  ereport(signaled ? FATAL : DEBUG2,
302  (errmsg("could not restore file \"%s\" from archive: %s",
303  xlogfname, wait_result_to_str(rc))));
304 
305 not_available:
306 
307  /*
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.
310  *
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.
313  */
314  snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
315  return false;
316 }
bool StandbyMode
Definition: xlog.c:277
#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:11133
#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:32
void PostRestoreCommand(void)
Definition: startup.c:241
struct stat stat_buf
Definition: pg_standby.c:101
#define WIFSIGNALED(w)
Definition: win32.h:173
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
char * recoveryRestoreCommand
Definition: xlog.c:257
#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:137
#define XLOGDIR
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define XLByteToSeg(xlrp, logSegNo)
#define WEXITSTATUS(w)
Definition: win32.h:174
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
#define StrNCpy(dst, src, len)
Definition: c.h:830
void PreRestoreCommand(void)
Definition: startup.c:227
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define WTERMSIG(w)
Definition: win32.h:175
static volatile sig_atomic_t signaled
Definition: pg_standby.c:51
bool XLogArchiveCheckDone ( const char *  xlog)

Definition at line 617 of file xlogarchive.c.

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

Referenced by CleanupBackupHistory(), and RemoveOldXlogFiles().

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

References MAXPGPATH, StatusFilePath, and unlink().

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

751 {
752  char archiveStatusPath[MAXPGPATH];
753 
754  /* Remove the .done file */
755  StatusFilePath(archiveStatusPath, xlog, ".done");
756  unlink(archiveStatusPath);
757  /* should we complain about failure? */
758 
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? */
763 }
#define StatusFilePath(path, xlog, suffix)
#define MAXPGPATH
int unlink(const char *filename)
void XLogArchiveForceDone ( const char *  xlog)

Definition at line 562 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().

563 {
564  char archiveReady[MAXPGPATH];
565  char archiveDone[MAXPGPATH];
566  struct stat stat_buf;
567  FILE *fd;
568 
569  /* Exit if already known done */
570  StatusFilePath(archiveDone, xlog, ".done");
571  if (stat(archiveDone, &stat_buf) == 0)
572  return;
573 
574  /* If .ready exists, rename it to .done */
575  StatusFilePath(archiveReady, xlog, ".ready");
576  if (stat(archiveReady, &stat_buf) == 0)
577  {
578  (void) durable_rename(archiveReady, archiveDone, WARNING);
579  return;
580  }
581 
582  /* insert an otherwise empty file called <XLOG>.done */
583  fd = AllocateFile(archiveDone, "w");
584  if (fd == NULL)
585  {
586  ereport(LOG,
588  errmsg("could not create archive status file \"%s\": %m",
589  archiveDone)));
590  return;
591  }
592  if (FreeFile(fd))
593  {
594  ereport(LOG,
596  errmsg("could not write archive status file \"%s\": %m",
597  archiveDone)));
598  return;
599  }
600 }
#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:2094
#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:229
int FreeFile(FILE *file)
Definition: fd.c:2277
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool XLogArchiveIsBusy ( const char *  xlog)

Definition at line 657 of file xlogarchive.c.

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

Referenced by do_pg_stop_backup().

658 {
659  char archiveStatusPath[MAXPGPATH];
660  struct stat stat_buf;
661 
662  /* First check for .done --- this means archiver is done with it */
663  StatusFilePath(archiveStatusPath, xlog, ".done");
664  if (stat(archiveStatusPath, &stat_buf) == 0)
665  return false;
666 
667  /* check for .ready --- this means archiver is still busy with it */
668  StatusFilePath(archiveStatusPath, xlog, ".ready");
669  if (stat(archiveStatusPath, &stat_buf) == 0)
670  return true;
671 
672  /* Race condition --- maybe archiver just finished, so recheck */
673  StatusFilePath(archiveStatusPath, xlog, ".done");
674  if (stat(archiveStatusPath, &stat_buf) == 0)
675  return false;
676 
677  /*
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.
681  */
682  snprintf(archiveStatusPath, MAXPGPATH, XLOGDIR "/%s", xlog);
683  if (stat(archiveStatusPath, &stat_buf) != 0 &&
684  errno == ENOENT)
685  return false;
686 
687  return true;
688 }
#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 732 of file xlogarchive.c.

References MAXPGPATH, and StatusFilePath.

Referenced by RemoveNonParentXlogFiles().

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

Definition at line 702 of file xlogarchive.c.

References MAXPGPATH, and StatusFilePath.

Referenced by StartupXLOG().

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

Definition at line 512 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().

513 {
514  char archiveStatusPath[MAXPGPATH];
515  FILE *fd;
516 
517  /* insert an otherwise empty file called <XLOG>.ready */
518  StatusFilePath(archiveStatusPath, xlog, ".ready");
519  fd = AllocateFile(archiveStatusPath, "w");
520  if (fd == NULL)
521  {
522  ereport(LOG,
524  errmsg("could not create archive status file \"%s\": %m",
525  archiveStatusPath)));
526  return;
527  }
528  if (FreeFile(fd))
529  {
530  ereport(LOG,
532  errmsg("could not write archive status file \"%s\": %m",
533  archiveStatusPath)));
534  return;
535  }
536 
537  /* Notify archiver that it's got something to do */
538  if (IsUnderPostmaster)
540 }
#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:2094
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
int FreeFile(FILE *file)
Definition: fd.c:2277
int errmsg(const char *fmt,...)
Definition: elog.c:797
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:113
void XLogArchiveNotifySeg ( XLogSegNo  segno)

Definition at line 546 of file xlogarchive.c.

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

Referenced by XLogWrite().

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