PostgreSQL Source Code  git master
xlogarchive.h File Reference
#include "access/xlogdefs.h"
Include dependency graph for xlogarchive.h:
This graph shows which files directly or indirectly include this file:

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 (const char *command, const char *commandName, bool failOnSignal)
 
void KeepFileRestoredFromArchive (const char *path, const 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 XLogArchiveIsReady (const char *xlog)
 
bool XLogArchiveIsReadyOrDone (const char *xlog)
 
void XLogArchiveCleanup (const char *xlog)
 

Function Documentation

◆ ExecuteRecoveryCommand()

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

Definition at line 287 of file xlogarchive.c.

References Assert, DEBUG3, ereport, errmsg(), errmsg_internal(), FATAL, GetOldestRestartPoint(), MAXPGPATH, strlcpy(), wait_result_is_any_signal(), wait_result_to_str(), wal_segment_size, WARNING, XLByteToSeg, and XLogFileName.

Referenced by CreateRestartPoint(), and StartupXLOG().

288 {
289  char xlogRecoveryCmd[MAXPGPATH];
290  char lastRestartPointFname[MAXPGPATH];
291  char *dp;
292  char *endp;
293  const char *sp;
294  int rc;
295  XLogSegNo restartSegNo;
296  XLogRecPtr restartRedoPtr;
297  TimeLineID restartTli;
298 
299  Assert(command && commandName);
300 
301  /*
302  * Calculate the archive file cutoff point for use during log shipping
303  * replication. All files earlier than this point can be deleted from the
304  * archive, though there is no requirement to do so.
305  */
306  GetOldestRestartPoint(&restartRedoPtr, &restartTli);
307  XLByteToSeg(restartRedoPtr, restartSegNo, wal_segment_size);
308  XLogFileName(lastRestartPointFname, restartTli, restartSegNo,
310 
311  /*
312  * construct the command to be executed
313  */
314  dp = xlogRecoveryCmd;
315  endp = xlogRecoveryCmd + MAXPGPATH - 1;
316  *endp = '\0';
317 
318  for (sp = command; *sp; sp++)
319  {
320  if (*sp == '%')
321  {
322  switch (sp[1])
323  {
324  case 'r':
325  /* %r: filename of last restartpoint */
326  sp++;
327  strlcpy(dp, lastRestartPointFname, endp - dp);
328  dp += strlen(dp);
329  break;
330  case '%':
331  /* convert %% to a single % */
332  sp++;
333  if (dp < endp)
334  *dp++ = *sp;
335  break;
336  default:
337  /* otherwise treat the % as not special */
338  if (dp < endp)
339  *dp++ = *sp;
340  break;
341  }
342  }
343  else
344  {
345  if (dp < endp)
346  *dp++ = *sp;
347  }
348  }
349  *dp = '\0';
350 
351  ereport(DEBUG3,
352  (errmsg_internal("executing %s \"%s\"", commandName, command)));
353 
354  /*
355  * execute the constructed command
356  */
357  rc = system(xlogRecoveryCmd);
358  if (rc != 0)
359  {
360  /*
361  * If the failure was due to any sort of signal, it's best to punt and
362  * abort recovery. See comments in RestoreArchivedFile().
363  */
364  ereport((failOnSignal && wait_result_is_any_signal(rc, true)) ? FATAL : WARNING,
365  /*------
366  translator: First %s represents a postgresql.conf parameter name like
367  "recovery_end_command", the 2nd is the value of that parameter, the
368  third an already translated error message. */
369  (errmsg("%s \"%s\": %s", commandName,
370  command, wait_result_to_str(rc))));
371  }
372 }
uint32 TimeLineID
Definition: xlogdefs.h:52
int wal_segment_size
Definition: xlog.c:117
#define DEBUG3
Definition: elog.h:23
void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli)
Definition: xlog.c:11529
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:32
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
uint64 XLogSegNo
Definition: xlogdefs.h:41
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:155
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:989
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:800
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
int errmsg(const char *fmt,...)
Definition: elog.c:902
bool wait_result_is_any_signal(int exit_status, bool include_command_not_found)
Definition: wait_error.c:111
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ KeepFileRestoredFromArchive()

void KeepFileRestoredFromArchive ( const char *  path,
const char *  xlogfname 
)

Definition at line 381 of file xlogarchive.c.

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

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

382 {
383  char xlogfpath[MAXPGPATH];
384  bool reload = false;
385  struct stat statbuf;
386 
387  snprintf(xlogfpath, MAXPGPATH, XLOGDIR "/%s", xlogfname);
388 
389  if (stat(xlogfpath, &statbuf) == 0)
390  {
391  char oldpath[MAXPGPATH];
392 
393 #ifdef WIN32
394  static unsigned int deletedcounter = 1;
395 
396  /*
397  * On Windows, if another process (e.g a walsender process) holds the
398  * file open in FILE_SHARE_DELETE mode, unlink will succeed, but the
399  * file will still show up in directory listing until the last handle
400  * is closed, and we cannot rename the new file in its place until
401  * that. To avoid that problem, rename the old file to a temporary
402  * name first. Use a counter to create a unique filename, because the
403  * same file might be restored from the archive multiple times, and a
404  * walsender could still be holding onto an old deleted version of it.
405  */
406  snprintf(oldpath, MAXPGPATH, "%s.deleted%u",
407  xlogfpath, deletedcounter++);
408  if (rename(xlogfpath, oldpath) != 0)
409  {
410  ereport(ERROR,
412  errmsg("could not rename file \"%s\" to \"%s\": %m",
413  xlogfpath, oldpath)));
414  }
415 #else
416  /* same-size buffers, so this never truncates */
417  strlcpy(oldpath, xlogfpath, MAXPGPATH);
418 #endif
419  if (unlink(oldpath) != 0)
420  ereport(FATAL,
422  errmsg("could not remove file \"%s\": %m",
423  xlogfpath)));
424  reload = true;
425  }
426 
427  durable_rename(path, xlogfpath, ERROR);
428 
429  /*
430  * Create .done file forcibly to prevent the restored segment from being
431  * archived again later.
432  */
434  XLogArchiveForceDone(xlogfname);
435  else
436  XLogArchiveNotify(xlogfname);
437 
438  /*
439  * If the existing file was replaced, since walsenders might have it open,
440  * request them to reload a currently-open segment. This is only required
441  * for WAL segments, walsenders don't hold other files open, but there's
442  * no harm in doing this too often, and we don't know what kind of a file
443  * we're dealing with here.
444  */
445  if (reload)
447 
448  /*
449  * Signal walsender that new WAL has arrived. Again, this isn't necessary
450  * if we restored something other than a WAL segment, but it does no harm
451  * either.
452  */
453  WalSndWakeup();
454 }
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:467
void WalSndRqstFileReload(void)
Definition: walsender.c:2994
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
int XLogArchiveMode
Definition: xlog.c:95
int errcode_for_file_access(void)
Definition: elog.c:714
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:659
void XLogArchiveForceDone(const char *xlog)
Definition: xlogarchive.c:517
#define XLOGDIR
static char xlogfpath[MAXPGPATH]
Definition: parsexlog.c:42
#define ereport(elevel,...)
Definition: elog.h:155
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define snprintf
Definition: port.h:215
void WalSndWakeup(void)
Definition: walsender.c:3114
#define stat
Definition: win32_port.h:275

◆ RestoreArchivedFile()

bool RestoreArchivedFile ( char *  path,
const char *  xlogfname,
const char *  recovername,
off_t  expectedSize,
bool  cleanupEnabled 
)

Definition at line 53 of file xlogarchive.c.

References ArchiveRecoveryRequested, Assert, BuildRestoreCommand(), DEBUG1, DEBUG2, DEBUG3, elevel, elog, ereport, errcode_for_file_access(), errdetail(), errmsg(), errmsg_internal(), ERROR, FATAL, GetOldestRestartPoint(), LOG, MAXPGPATH, pfree(), PostRestoreCommand(), PreRestoreCommand(), proc_exit(), recoveryRestoreCommand, snprintf, stat::st_size, StandbyMode, stat, wait_result_is_any_signal(), wait_result_is_signal(), wait_result_to_str(), wal_segment_size, XLByteToSeg, XLOGDIR, and XLogFileName.

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

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

◆ XLogArchiveCheckDone()

bool XLogArchiveCheckDone ( const char *  xlog)

Definition at line 572 of file xlogarchive.c.

References GetRecoveryState(), MAXPGPATH, RECOVERY_STATE_ARCHIVE, stat, StatusFilePath, XLogArchiveNotify(), XLogArchivingActive, and XLogArchivingAlways.

Referenced by CleanupBackupHistory(), and RemoveOldXlogFiles().

573 {
574  char archiveStatusPath[MAXPGPATH];
575  struct stat stat_buf;
576 
577  /* The file is always deletable if archive_mode is "off". */
578  if (!XLogArchivingActive())
579  return true;
580 
581  /*
582  * During archive recovery, the file is deletable if archive_mode is not
583  * "always".
584  */
585  if (!XLogArchivingAlways() &&
587  return true;
588 
589  /*
590  * At this point of the logic, note that we are either a primary with
591  * archive_mode set to "on" or "always", or a standby with archive_mode
592  * set to "always".
593  */
594 
595  /* First check for .done --- this means archiver is done with it */
596  StatusFilePath(archiveStatusPath, xlog, ".done");
597  if (stat(archiveStatusPath, &stat_buf) == 0)
598  return true;
599 
600  /* check for .ready --- this means archiver is still busy with it */
601  StatusFilePath(archiveStatusPath, xlog, ".ready");
602  if (stat(archiveStatusPath, &stat_buf) == 0)
603  return false;
604 
605  /* Race condition --- maybe archiver just finished, so recheck */
606  StatusFilePath(archiveStatusPath, xlog, ".done");
607  if (stat(archiveStatusPath, &stat_buf) == 0)
608  return true;
609 
610  /* Retry creation of the .ready file */
611  XLogArchiveNotify(xlog);
612  return false;
613 }
#define StatusFilePath(path, xlog, suffix)
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:467
#define XLogArchivingAlways()
Definition: xlog.h:183
struct stat stat_buf
Definition: pg_standby.c:100
#define MAXPGPATH
#define XLogArchivingActive()
Definition: xlog.h:180
RecoveryState GetRecoveryState(void)
Definition: xlog.c:8123
#define stat
Definition: win32_port.h:275

◆ XLogArchiveCleanup()

void XLogArchiveCleanup ( const char *  xlog)

Definition at line 719 of file xlogarchive.c.

References MAXPGPATH, and StatusFilePath.

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

720 {
721  char archiveStatusPath[MAXPGPATH];
722 
723  /* Remove the .done file */
724  StatusFilePath(archiveStatusPath, xlog, ".done");
725  unlink(archiveStatusPath);
726  /* should we complain about failure? */
727 
728  /* Remove the .ready file if present --- normally it shouldn't be */
729  StatusFilePath(archiveStatusPath, xlog, ".ready");
730  unlink(archiveStatusPath);
731  /* should we complain about failure? */
732 }
#define StatusFilePath(path, xlog, suffix)
#define MAXPGPATH

◆ XLogArchiveForceDone()

void XLogArchiveForceDone ( const char *  xlog)

Definition at line 517 of file xlogarchive.c.

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

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

518 {
519  char archiveReady[MAXPGPATH];
520  char archiveDone[MAXPGPATH];
521  struct stat stat_buf;
522  FILE *fd;
523 
524  /* Exit if already known done */
525  StatusFilePath(archiveDone, xlog, ".done");
526  if (stat(archiveDone, &stat_buf) == 0)
527  return;
528 
529  /* If .ready exists, rename it to .done */
530  StatusFilePath(archiveReady, xlog, ".ready");
531  if (stat(archiveReady, &stat_buf) == 0)
532  {
533  (void) durable_rename(archiveReady, archiveDone, WARNING);
534  return;
535  }
536 
537  /* insert an otherwise empty file called <XLOG>.done */
538  fd = AllocateFile(archiveDone, "w");
539  if (fd == NULL)
540  {
541  ereport(LOG,
543  errmsg("could not create archive status file \"%s\": %m",
544  archiveDone)));
545  return;
546  }
547  if (FreeFile(fd))
548  {
549  ereport(LOG,
551  errmsg("could not write archive status file \"%s\": %m",
552  archiveDone)));
553  return;
554  }
555 }
#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:100
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:714
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2322
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:659
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:155
int FreeFile(FILE *file)
Definition: fd.c:2521
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define stat
Definition: win32_port.h:275

◆ XLogArchiveIsBusy()

bool XLogArchiveIsBusy ( const char *  xlog)

Definition at line 626 of file xlogarchive.c.

References MAXPGPATH, snprintf, stat, StatusFilePath, and XLOGDIR.

Referenced by do_pg_stop_backup().

627 {
628  char archiveStatusPath[MAXPGPATH];
629  struct stat stat_buf;
630 
631  /* First check for .done --- this means archiver is done with it */
632  StatusFilePath(archiveStatusPath, xlog, ".done");
633  if (stat(archiveStatusPath, &stat_buf) == 0)
634  return false;
635 
636  /* check for .ready --- this means archiver is still busy with it */
637  StatusFilePath(archiveStatusPath, xlog, ".ready");
638  if (stat(archiveStatusPath, &stat_buf) == 0)
639  return true;
640 
641  /* Race condition --- maybe archiver just finished, so recheck */
642  StatusFilePath(archiveStatusPath, xlog, ".done");
643  if (stat(archiveStatusPath, &stat_buf) == 0)
644  return false;
645 
646  /*
647  * Check to see if the WAL file has been removed by checkpoint, which
648  * implies it has already been archived, and explains why we can't see a
649  * status file for it.
650  */
651  snprintf(archiveStatusPath, MAXPGPATH, XLOGDIR "/%s", xlog);
652  if (stat(archiveStatusPath, &stat_buf) != 0 &&
653  errno == ENOENT)
654  return false;
655 
656  return true;
657 }
#define StatusFilePath(path, xlog, suffix)
struct stat stat_buf
Definition: pg_standby.c:100
#define MAXPGPATH
#define XLOGDIR
#define snprintf
Definition: port.h:215
#define stat
Definition: win32_port.h:275

◆ XLogArchiveIsReady()

bool XLogArchiveIsReady ( const char *  xlog)

Definition at line 701 of file xlogarchive.c.

References MAXPGPATH, stat, and StatusFilePath.

Referenced by RemoveNonParentXlogFiles().

702 {
703  char archiveStatusPath[MAXPGPATH];
704  struct stat stat_buf;
705 
706  StatusFilePath(archiveStatusPath, xlog, ".ready");
707  if (stat(archiveStatusPath, &stat_buf) == 0)
708  return true;
709 
710  return false;
711 }
#define StatusFilePath(path, xlog, suffix)
struct stat stat_buf
Definition: pg_standby.c:100
#define MAXPGPATH
#define stat
Definition: win32_port.h:275

◆ XLogArchiveIsReadyOrDone()

bool XLogArchiveIsReadyOrDone ( const char *  xlog)

Definition at line 671 of file xlogarchive.c.

References MAXPGPATH, stat, and StatusFilePath.

Referenced by StartupXLOG().

672 {
673  char archiveStatusPath[MAXPGPATH];
674  struct stat stat_buf;
675 
676  /* First check for .done --- this means archiver is done with it */
677  StatusFilePath(archiveStatusPath, xlog, ".done");
678  if (stat(archiveStatusPath, &stat_buf) == 0)
679  return true;
680 
681  /* check for .ready --- this means archiver is still busy with it */
682  StatusFilePath(archiveStatusPath, xlog, ".ready");
683  if (stat(archiveStatusPath, &stat_buf) == 0)
684  return true;
685 
686  /* Race condition --- maybe archiver just finished, so recheck */
687  StatusFilePath(archiveStatusPath, xlog, ".done");
688  if (stat(archiveStatusPath, &stat_buf) == 0)
689  return true;
690 
691  return false;
692 }
#define StatusFilePath(path, xlog, suffix)
struct stat stat_buf
Definition: pg_standby.c:100
#define MAXPGPATH
#define stat
Definition: win32_port.h:275

◆ XLogArchiveNotify()

void XLogArchiveNotify ( const char *  xlog)

Definition at line 467 of file xlogarchive.c.

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

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

468 {
469  char archiveStatusPath[MAXPGPATH];
470  FILE *fd;
471 
472  /* insert an otherwise empty file called <XLOG>.ready */
473  StatusFilePath(archiveStatusPath, xlog, ".ready");
474  fd = AllocateFile(archiveStatusPath, "w");
475  if (fd == NULL)
476  {
477  ereport(LOG,
479  errmsg("could not create archive status file \"%s\": %m",
480  archiveStatusPath)));
481  return;
482  }
483  if (FreeFile(fd))
484  {
485  ereport(LOG,
487  errmsg("could not write archive status file \"%s\": %m",
488  archiveStatusPath)));
489  return;
490  }
491 
492  /* Notify archiver that it's got something to do */
493  if (IsUnderPostmaster)
495 }
#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:109
int errcode_for_file_access(void)
Definition: elog.c:714
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2322
#define ereport(elevel,...)
Definition: elog.h:155
int FreeFile(FILE *file)
Definition: fd.c:2521
int errmsg(const char *fmt,...)
Definition: elog.c:902
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:146

◆ XLogArchiveNotifySeg()

void XLogArchiveNotifySeg ( XLogSegNo  segno)

Definition at line 501 of file xlogarchive.c.

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

Referenced by XLogWrite().

502 {
503  char xlog[MAXFNAMELEN];
504 
506  XLogArchiveNotify(xlog);
507 }
int wal_segment_size
Definition: xlog.c:117
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:467
#define MAXFNAMELEN
TimeLineID ThisTimeLineID
Definition: xlog.c:192
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)