PostgreSQL Source Code  git master
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 "access/xlogarchive.h"
#include "common/archive.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 (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 XLogArchiveIsReadyOrDone (const char *xlog)
 
bool XLogArchiveIsReady (const char *xlog)
 
void XLogArchiveCleanup (const char *xlog)
 

Function Documentation

◆ ExecuteRecoveryCommand()

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

Definition at line 286 of file xlogarchive.c.

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

Referenced by CreateRestartPoint(), and StartupXLOG().

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

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

◆ 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(), errmsg(), errmsg_internal(), ERROR, FATAL, GetOldestRestartPoint(), LOG, MAXPGPATH, pfree(), PostRestoreCommand(), PreRestoreCommand(), proc_exit(), recoveryRestoreCommand, snprintf, 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: %lu instead of %lu",
206  xlogfname,
207  (unsigned long) stat_buf.st_size,
208  (unsigned long) 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  if (errno != ENOENT)
224  ereport(FATAL,
226  errmsg("could not stat file \"%s\": %m",
227  xlogpath)));
228  }
229  }
230 
231  /*
232  * Remember, we rollforward UNTIL the restore fails so failure here is
233  * just part of the process... that makes it difficult to determine
234  * whether the restore failed because there isn't an archive to restore,
235  * or because the administrator has specified the restore program
236  * incorrectly. We have to assume the former.
237  *
238  * However, if the failure was due to any sort of signal, it's best to
239  * punt and abort recovery. (If we "return false" here, upper levels will
240  * assume that recovery is complete and start up the database!) It's
241  * essential to abort on child SIGINT and SIGQUIT, because per spec
242  * system() ignores SIGINT and SIGQUIT while waiting; if we see one of
243  * those it's a good bet we should have gotten it too.
244  *
245  * On SIGTERM, assume we have received a fast shutdown request, and exit
246  * cleanly. It's pure chance whether we receive the SIGTERM first, or the
247  * child process. If we receive it first, the signal handler will call
248  * proc_exit, otherwise we do it here. If we or the child process received
249  * SIGTERM for any other reason than a fast shutdown request, postmaster
250  * will perform an immediate shutdown when it sees us exiting
251  * unexpectedly.
252  *
253  * We treat hard shell errors such as "command not found" as fatal, too.
254  */
255  if (wait_result_is_signal(rc, SIGTERM))
256  proc_exit(1);
257 
259  (errmsg("could not restore file \"%s\" from archive: %s",
260  xlogfname, wait_result_to_str(rc))));
261 
262 not_available:
263 
264  /*
265  * if an archived file is not available, there might still be a version of
266  * this file in XLOGDIR, so return that as the filename to open.
267  *
268  * In many recovery scenarios we expect this to fail also, but if so that
269  * just means we've reached the end of WAL.
270  */
271  snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
272  return false;
273 }
bool ArchiveRecoveryRequested
Definition: xlog.c:265
bool StandbyMode
Definition: xlog.c:300
#define DEBUG1
Definition: elog.h:25
uint32 TimeLineID
Definition: xlogdefs.h:52
int wal_segment_size
Definition: xlog.c:116
#define DEBUG3
Definition: elog.h:23
void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli)
Definition: xlog.c:11512
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:1056
bool wait_result_is_signal(int exit_status, int signum)
Definition: wait_error.c:92
void PostRestoreCommand(void)
Definition: startup.c:228
#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:279
char * BuildRestoreCommand(const char *restoreCommand, const char *xlogpath, const char *xlogfname, const char *lastRestartPointFname)
Definition: archive.c:39
#define DEBUG2
Definition: elog.h:24
uint64 XLogSegNo
Definition: xlogdefs.h:41
int errcode_for_file_access(void)
Definition: elog.c:633
#define stat(a, b)
Definition: win32_port.h:255
static int elevel
Definition: vacuumlazy.c:323
#define XLOGDIR
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg_internal(const char *fmt,...)
Definition: elog.c:911
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
void PreRestoreCommand(void)
Definition: startup.c:214
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
bool wait_result_is_any_signal(int exit_status, bool include_command_not_found)
Definition: wait_error.c:111
#define snprintf
Definition: port.h:193
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogArchiveCheckDone()

bool XLogArchiveCheckDone ( const char *  xlog)

Definition at line 571 of file xlogarchive.c.

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

Referenced by CleanupBackupHistory(), and RemoveOldXlogFiles().

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

◆ XLogArchiveCleanup()

void XLogArchiveCleanup ( const char *  xlog)

Definition at line 718 of file xlogarchive.c.

References MAXPGPATH, and StatusFilePath.

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

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

◆ XLogArchiveForceDone()

void XLogArchiveForceDone ( const char *  xlog)

Definition at line 516 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(), WalReceiverMain(), and XLogWalRcvWrite().

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

◆ XLogArchiveIsBusy()

bool XLogArchiveIsBusy ( const char *  xlog)

Definition at line 625 of file xlogarchive.c.

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

Referenced by do_pg_stop_backup().

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

◆ XLogArchiveIsReady()

bool XLogArchiveIsReady ( const char *  xlog)

Definition at line 700 of file xlogarchive.c.

References MAXPGPATH, stat, and StatusFilePath.

Referenced by RemoveNonParentXlogFiles().

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

◆ XLogArchiveIsReadyOrDone()

bool XLogArchiveIsReadyOrDone ( const char *  xlog)

Definition at line 670 of file xlogarchive.c.

References MAXPGPATH, stat, and StatusFilePath.

Referenced by StartupXLOG().

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

◆ XLogArchiveNotify()

void XLogArchiveNotify ( const char *  xlog)

Definition at line 466 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(), WalReceiverMain(), writeTimeLineHistory(), XLogArchiveCheckDone(), XLogArchiveNotifySeg(), and XLogWalRcvWrite().

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

◆ XLogArchiveNotifySeg()

void XLogArchiveNotifySeg ( XLogSegNo  segno)

Definition at line 500 of file xlogarchive.c.

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

Referenced by XLogWrite().

501 {
502  char xlog[MAXFNAMELEN];
503 
505  XLogArchiveNotify(xlog);
506 }
int wal_segment_size
Definition: xlog.c:116
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:466
#define MAXFNAMELEN
TimeLineID ThisTimeLineID
Definition: xlog.c:191
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)