]> granicus.if.org Git - postgresql/blob - src/backend/access/transam/xlogarchive.c
Fix archive_cleanup_command.
[postgresql] / src / backend / access / transam / xlogarchive.c
1 /*-------------------------------------------------------------------------
2  *
3  * xlogarchive.c
4  *              Functions for archiving WAL files and restoring from the archive.
5  *
6  *
7  * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/backend/access/transam/xlogarchive.c
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #include "postgres.h"
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/wait.h>
20 #include <signal.h>
21 #include <unistd.h>
22
23 #include "access/xlog_internal.h"
24 #include "miscadmin.h"
25 #include "postmaster/startup.h"
26 #include "storage/fd.h"
27 #include "storage/ipc.h"
28 #include "storage/lwlock.h"
29 #include "storage/pmsignal.h"
30
31 /*
32  * Attempt to retrieve the specified file from off-line archival storage.
33  * If successful, fill "path" with its complete path (note that this will be
34  * a temp file name that doesn't follow the normal naming convention), and
35  * return TRUE.
36  *
37  * If not successful, fill "path" with the name of the normal on-line file
38  * (which may or may not actually exist, but we'll try to use it), and return
39  * FALSE.
40  *
41  * For fixed-size files, the caller may pass the expected size as an
42  * additional crosscheck on successful recovery.  If the file size is not
43  * known, set expectedSize = 0.
44  *
45  * When 'cleanupEnabled' is false, refrain from deleting any old WAL segments
46  * in the archive. This is used when fetching the initial checkpoint record,
47  * when we are not yet sure how far back we need the WAL.
48  */
49 bool
50 RestoreArchivedFile(char *path, const char *xlogfname,
51                                         const char *recovername, off_t expectedSize,
52                                         bool cleanupEnabled)
53 {
54         char            xlogpath[MAXPGPATH];
55         char            xlogRestoreCmd[MAXPGPATH];
56         char            lastRestartPointFname[MAXPGPATH];
57         char       *dp;
58         char       *endp;
59         const char *sp;
60         int                     rc;
61         bool            signaled;
62         struct stat stat_buf;
63         XLogSegNo       restartSegNo;
64         XLogRecPtr      restartRedoPtr;
65         TimeLineID      restartTli;
66
67         /* In standby mode, restore_command might not be supplied */
68         if (recoveryRestoreCommand == NULL)
69                 goto not_available;
70
71         /*
72          * When doing archive recovery, we always prefer an archived log file even
73          * if a file of the same name exists in XLOGDIR.  The reason is that the
74          * file in XLOGDIR could be an old, un-filled or partly-filled version
75          * that was copied and restored as part of backing up $PGDATA.
76          *
77          * We could try to optimize this slightly by checking the local copy
78          * lastchange timestamp against the archived copy, but we have no API to
79          * do this, nor can we guarantee that the lastchange timestamp was
80          * preserved correctly when we copied to archive. Our aim is robustness,
81          * so we elect not to do this.
82          *
83          * If we cannot obtain the log file from the archive, however, we will try
84          * to use the XLOGDIR file if it exists.  This is so that we can make use
85          * of log segments that weren't yet transferred to the archive.
86          *
87          * Notice that we don't actually overwrite any files when we copy back
88          * from archive because the restore_command may inadvertently
89          * restore inappropriate xlogs, or they may be corrupt, so we may wish to
90          * fallback to the segments remaining in current XLOGDIR later. The
91          * copy-from-archive filename is always the same, ensuring that we don't
92          * run out of disk space on long recoveries.
93          */
94         snprintf(xlogpath, MAXPGPATH, XLOGDIR "/%s", recovername);
95
96         /*
97          * Make sure there is no existing file named recovername.
98          */
99         if (stat(xlogpath, &stat_buf) != 0)
100         {
101                 if (errno != ENOENT)
102                         ereport(FATAL,
103                                         (errcode_for_file_access(),
104                                          errmsg("could not stat file \"%s\": %m",
105                                                         xlogpath)));
106         }
107         else
108         {
109                 if (unlink(xlogpath) != 0)
110                         ereport(FATAL,
111                                         (errcode_for_file_access(),
112                                          errmsg("could not remove file \"%s\": %m",
113                                                         xlogpath)));
114         }
115
116         /*
117          * Calculate the archive file cutoff point for use during log shipping
118          * replication. All files earlier than this point can be deleted from the
119          * archive, though there is no requirement to do so.
120          *
121          * If cleanup is not enabled, initialise this with the filename of
122          * InvalidXLogRecPtr, which will prevent the deletion of any WAL files
123          * from the archive because of the alphabetic sorting property of WAL
124          * filenames.
125          *
126          * Once we have successfully located the redo pointer of the checkpoint
127          * from which we start recovery we never request a file prior to the redo
128          * pointer of the last restartpoint. When redo begins we know that we have
129          * successfully located it, so there is no need for additional status
130          * flags to signify the point when we can begin deleting WAL files from
131          * the archive.
132          */
133         if (cleanupEnabled)
134         {
135                 GetOldestRestartPoint(&restartRedoPtr, &restartTli);
136                 XLByteToSeg(restartRedoPtr, restartSegNo);
137                 XLogFileName(lastRestartPointFname, restartTli, restartSegNo);
138                 /* we shouldn't need anything earlier than last restart point */
139                 Assert(strcmp(lastRestartPointFname, xlogfname) <= 0);
140         }
141         else
142                 XLogFileName(lastRestartPointFname, 0, 0L);
143
144         /*
145          * construct the command to be executed
146          */
147         dp = xlogRestoreCmd;
148         endp = xlogRestoreCmd + MAXPGPATH - 1;
149         *endp = '\0';
150
151         for (sp = recoveryRestoreCommand; *sp; sp++)
152         {
153                 if (*sp == '%')
154                 {
155                         switch (sp[1])
156                         {
157                                 case 'p':
158                                         /* %p: relative path of target file */
159                                         sp++;
160                                         StrNCpy(dp, xlogpath, endp - dp);
161                                         make_native_path(dp);
162                                         dp += strlen(dp);
163                                         break;
164                                 case 'f':
165                                         /* %f: filename of desired file */
166                                         sp++;
167                                         StrNCpy(dp, xlogfname, endp - dp);
168                                         dp += strlen(dp);
169                                         break;
170                                 case 'r':
171                                         /* %r: filename of last restartpoint */
172                                         sp++;
173                                         StrNCpy(dp, lastRestartPointFname, endp - dp);
174                                         dp += strlen(dp);
175                                         break;
176                                 case '%':
177                                         /* convert %% to a single % */
178                                         sp++;
179                                         if (dp < endp)
180                                                 *dp++ = *sp;
181                                         break;
182                                 default:
183                                         /* otherwise treat the % as not special */
184                                         if (dp < endp)
185                                                 *dp++ = *sp;
186                                         break;
187                         }
188                 }
189                 else
190                 {
191                         if (dp < endp)
192                                 *dp++ = *sp;
193                 }
194         }
195         *dp = '\0';
196
197         ereport(DEBUG3,
198                         (errmsg_internal("executing restore command \"%s\"",
199                                                          xlogRestoreCmd)));
200
201         /*
202          * Check signals before restore command and reset afterwards.
203          */
204         PreRestoreCommand();
205
206         /*
207          * Copy xlog from archival storage to XLOGDIR
208          */
209         rc = system(xlogRestoreCmd);
210
211         PostRestoreCommand();
212
213         if (rc == 0)
214         {
215                 /*
216                  * command apparently succeeded, but let's make sure the file is
217                  * really there now and has the correct size.
218                  */
219                 if (stat(xlogpath, &stat_buf) == 0)
220                 {
221                         if (expectedSize > 0 && stat_buf.st_size != expectedSize)
222                         {
223                                 int                     elevel;
224
225                                 /*
226                                  * If we find a partial file in standby mode, we assume it's
227                                  * because it's just being copied to the archive, and keep
228                                  * trying.
229                                  *
230                                  * Otherwise treat a wrong-sized file as FATAL to ensure the
231                                  * DBA would notice it, but is that too strong? We could try
232                                  * to plow ahead with a local copy of the file ... but the
233                                  * problem is that there probably isn't one, and we'd
234                                  * incorrectly conclude we've reached the end of WAL and we're
235                                  * done recovering ...
236                                  */
237                                 if (StandbyMode && stat_buf.st_size < expectedSize)
238                                         elevel = DEBUG1;
239                                 else
240                                         elevel = FATAL;
241                                 ereport(elevel,
242                                                 (errmsg("archive file \"%s\" has wrong size: %lu instead of %lu",
243                                                                 xlogfname,
244                                                                 (unsigned long) stat_buf.st_size,
245                                                                 (unsigned long) expectedSize)));
246                                 return false;
247                         }
248                         else
249                         {
250                                 ereport(LOG,
251                                                 (errmsg("restored log file \"%s\" from archive",
252                                                                 xlogfname)));
253                                 strcpy(path, xlogpath);
254                                 return true;
255                         }
256                 }
257                 else
258                 {
259                         /* stat failed */
260                         if (errno != ENOENT)
261                                 ereport(FATAL,
262                                                 (errcode_for_file_access(),
263                                                  errmsg("could not stat file \"%s\": %m",
264                                                                 xlogpath)));
265                 }
266         }
267
268         /*
269          * Remember, we rollforward UNTIL the restore fails so failure here is
270          * just part of the process... that makes it difficult to determine
271          * whether the restore failed because there isn't an archive to restore,
272          * or because the administrator has specified the restore program
273          * incorrectly.  We have to assume the former.
274          *
275          * However, if the failure was due to any sort of signal, it's best to
276          * punt and abort recovery.  (If we "return false" here, upper levels will
277          * assume that recovery is complete and start up the database!) It's
278          * essential to abort on child SIGINT and SIGQUIT, because per spec
279          * system() ignores SIGINT and SIGQUIT while waiting; if we see one of
280          * those it's a good bet we should have gotten it too.
281          *
282          * On SIGTERM, assume we have received a fast shutdown request, and exit
283          * cleanly. It's pure chance whether we receive the SIGTERM first, or the
284          * child process. If we receive it first, the signal handler will call
285          * proc_exit, otherwise we do it here. If we or the child process received
286          * SIGTERM for any other reason than a fast shutdown request, postmaster
287          * will perform an immediate shutdown when it sees us exiting
288          * unexpectedly.
289          *
290          * Per the Single Unix Spec, shells report exit status > 128 when a called
291          * command died on a signal.  Also, 126 and 127 are used to report
292          * problems such as an unfindable command; treat those as fatal errors
293          * too.
294          */
295         if (WIFSIGNALED(rc) && WTERMSIG(rc) == SIGTERM)
296                 proc_exit(1);
297
298         signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
299
300         ereport(signaled ? FATAL : DEBUG2,
301                 (errmsg("could not restore file \"%s\" from archive: return code %d",
302                                 xlogfname, rc)));
303
304 not_available:
305
306         /*
307          * if an archived file is not available, there might still be a version of
308          * this file in XLOGDIR, so return that as the filename to open.
309          *
310          * In many recovery scenarios we expect this to fail also, but if so that
311          * just means we've reached the end of WAL.
312          */
313         snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
314         return false;
315 }
316
317 /*
318  * Attempt to execute an external shell command during recovery.
319  *
320  * 'command' is the shell command to be executed, 'commandName' is a
321  * human-readable name describing the command emitted in the logs. If
322  * 'failOnSignal' is true and the command is killed by a signal, a FATAL
323  * error is thrown. Otherwise a WARNING is emitted.
324  *
325  * This is currently used for recovery_end_command and archive_cleanup_command.
326  */
327 void
328 ExecuteRecoveryCommand(char *command, char *commandName, bool failOnSignal)
329 {
330         char            xlogRecoveryCmd[MAXPGPATH];
331         char            lastRestartPointFname[MAXPGPATH];
332         char       *dp;
333         char       *endp;
334         const char *sp;
335         int                     rc;
336         bool            signaled;
337         XLogSegNo       restartSegNo;
338         XLogRecPtr      restartRedoPtr;
339         TimeLineID      restartTli;
340
341         Assert(command && commandName);
342
343         /*
344          * Calculate the archive file cutoff point for use during log shipping
345          * replication. All files earlier than this point can be deleted from the
346          * archive, though there is no requirement to do so.
347          */
348         GetOldestRestartPoint(&restartRedoPtr, &restartTli);
349         XLByteToSeg(restartRedoPtr, restartSegNo);
350         XLogFileName(lastRestartPointFname, restartTli, restartSegNo);
351
352         /*
353          * construct the command to be executed
354          */
355         dp = xlogRecoveryCmd;
356         endp = xlogRecoveryCmd + MAXPGPATH - 1;
357         *endp = '\0';
358
359         for (sp = command; *sp; sp++)
360         {
361                 if (*sp == '%')
362                 {
363                         switch (sp[1])
364                         {
365                                 case 'r':
366                                         /* %r: filename of last restartpoint */
367                                         sp++;
368                                         StrNCpy(dp, lastRestartPointFname, endp - dp);
369                                         dp += strlen(dp);
370                                         break;
371                                 case '%':
372                                         /* convert %% to a single % */
373                                         sp++;
374                                         if (dp < endp)
375                                                 *dp++ = *sp;
376                                         break;
377                                 default:
378                                         /* otherwise treat the % as not special */
379                                         if (dp < endp)
380                                                 *dp++ = *sp;
381                                         break;
382                         }
383                 }
384                 else
385                 {
386                         if (dp < endp)
387                                 *dp++ = *sp;
388                 }
389         }
390         *dp = '\0';
391
392         ereport(DEBUG3,
393                         (errmsg_internal("executing %s \"%s\"", commandName, command)));
394
395         /*
396          * execute the constructed command
397          */
398         rc = system(xlogRecoveryCmd);
399         if (rc != 0)
400         {
401                 /*
402                  * If the failure was due to any sort of signal, it's best to punt and
403                  * abort recovery. See also detailed comments on signals in
404                  * RestoreArchivedFile().
405                  */
406                 signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
407
408                 ereport((signaled && failOnSignal) ? FATAL : WARNING,
409                 /*------
410                    translator: First %s represents a recovery.conf parameter name like
411                   "recovery_end_command", and the 2nd is the value of that parameter. */
412                                 (errmsg("%s \"%s\": return code %d", commandName,
413                                                 command, rc)));
414         }
415 }
416
417
418 /*
419  * XLogArchiveNotify
420  *
421  * Create an archive notification file
422  *
423  * The name of the notification file is the message that will be picked up
424  * by the archiver, e.g. we write 0000000100000001000000C6.ready
425  * and the archiver then knows to archive XLOGDIR/0000000100000001000000C6,
426  * then when complete, rename it to 0000000100000001000000C6.done
427  */
428 void
429 XLogArchiveNotify(const char *xlog)
430 {
431         char            archiveStatusPath[MAXPGPATH];
432         FILE       *fd;
433
434         /* insert an otherwise empty file called <XLOG>.ready */
435         StatusFilePath(archiveStatusPath, xlog, ".ready");
436         fd = AllocateFile(archiveStatusPath, "w");
437         if (fd == NULL)
438         {
439                 ereport(LOG,
440                                 (errcode_for_file_access(),
441                                  errmsg("could not create archive status file \"%s\": %m",
442                                                 archiveStatusPath)));
443                 return;
444         }
445         if (FreeFile(fd))
446         {
447                 ereport(LOG,
448                                 (errcode_for_file_access(),
449                                  errmsg("could not write archive status file \"%s\": %m",
450                                                 archiveStatusPath)));
451                 return;
452         }
453
454         /* Notify archiver that it's got something to do */
455         if (IsUnderPostmaster)
456                 SendPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER);
457 }
458
459 /*
460  * Convenience routine to notify using segment number representation of filename
461  */
462 void
463 XLogArchiveNotifySeg(XLogSegNo segno)
464 {
465         char            xlog[MAXFNAMELEN];
466
467         XLogFileName(xlog, ThisTimeLineID, segno);
468         XLogArchiveNotify(xlog);
469 }
470
471 /*
472  * XLogArchiveCheckDone
473  *
474  * This is called when we are ready to delete or recycle an old XLOG segment
475  * file or backup history file.  If it is okay to delete it then return true.
476  * If it is not time to delete it, make sure a .ready file exists, and return
477  * false.
478  *
479  * If <XLOG>.done exists, then return true; else if <XLOG>.ready exists,
480  * then return false; else create <XLOG>.ready and return false.
481  *
482  * The reason we do things this way is so that if the original attempt to
483  * create <XLOG>.ready fails, we'll retry during subsequent checkpoints.
484  */
485 bool
486 XLogArchiveCheckDone(const char *xlog)
487 {
488         char            archiveStatusPath[MAXPGPATH];
489         struct stat stat_buf;
490
491         /* Always deletable if archiving is off */
492         if (!XLogArchivingActive())
493                 return true;
494
495         /* First check for .done --- this means archiver is done with it */
496         StatusFilePath(archiveStatusPath, xlog, ".done");
497         if (stat(archiveStatusPath, &stat_buf) == 0)
498                 return true;
499
500         /* check for .ready --- this means archiver is still busy with it */
501         StatusFilePath(archiveStatusPath, xlog, ".ready");
502         if (stat(archiveStatusPath, &stat_buf) == 0)
503                 return false;
504
505         /* Race condition --- maybe archiver just finished, so recheck */
506         StatusFilePath(archiveStatusPath, xlog, ".done");
507         if (stat(archiveStatusPath, &stat_buf) == 0)
508                 return true;
509
510         /* Retry creation of the .ready file */
511         XLogArchiveNotify(xlog);
512         return false;
513 }
514
515 /*
516  * XLogArchiveIsBusy
517  *
518  * Check to see if an XLOG segment file is still unarchived.
519  * This is almost but not quite the inverse of XLogArchiveCheckDone: in
520  * the first place we aren't chartered to recreate the .ready file, and
521  * in the second place we should consider that if the file is already gone
522  * then it's not busy.  (This check is needed to handle the race condition
523  * that a checkpoint already deleted the no-longer-needed file.)
524  */
525 bool
526 XLogArchiveIsBusy(const char *xlog)
527 {
528         char            archiveStatusPath[MAXPGPATH];
529         struct stat stat_buf;
530
531         /* First check for .done --- this means archiver is done with it */
532         StatusFilePath(archiveStatusPath, xlog, ".done");
533         if (stat(archiveStatusPath, &stat_buf) == 0)
534                 return false;
535
536         /* check for .ready --- this means archiver is still busy with it */
537         StatusFilePath(archiveStatusPath, xlog, ".ready");
538         if (stat(archiveStatusPath, &stat_buf) == 0)
539                 return true;
540
541         /* Race condition --- maybe archiver just finished, so recheck */
542         StatusFilePath(archiveStatusPath, xlog, ".done");
543         if (stat(archiveStatusPath, &stat_buf) == 0)
544                 return false;
545
546         /*
547          * Check to see if the WAL file has been removed by checkpoint, which
548          * implies it has already been archived, and explains why we can't see a
549          * status file for it.
550          */
551         snprintf(archiveStatusPath, MAXPGPATH, XLOGDIR "/%s", xlog);
552         if (stat(archiveStatusPath, &stat_buf) != 0 &&
553                 errno == ENOENT)
554                 return false;
555
556         return true;
557 }
558
559 /*
560  * XLogArchiveCleanup
561  *
562  * Cleanup archive notification file(s) for a particular xlog segment
563  */
564 void
565 XLogArchiveCleanup(const char *xlog)
566 {
567         char            archiveStatusPath[MAXPGPATH];
568
569         /* Remove the .done file */
570         StatusFilePath(archiveStatusPath, xlog, ".done");
571         unlink(archiveStatusPath);
572         /* should we complain about failure? */
573
574         /* Remove the .ready file if present --- normally it shouldn't be */
575         StatusFilePath(archiveStatusPath, xlog, ".ready");
576         unlink(archiveStatusPath);
577         /* should we complain about failure? */
578 }