1 /*-------------------------------------------------------------------------
5 * PostgreSQL WAL archiver
7 * All functions relating to archiver are included here
9 * - All functions executed by archiver process
11 * - archiver is forked from postmaster, and the two
12 * processes then communicate using signals. All functions
13 * executed by postmaster are included in this file.
15 * Initial author: Simon Riggs simon@2ndquadrant.com
17 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
18 * Portions Copyright (c) 1994, Regents of the University of California
22 * $PostgreSQL: pgsql/src/backend/postmaster/pgarch.c,v 1.4 2004/08/03 20:32:33 tgl Exp $
24 *-------------------------------------------------------------------------
34 #include "access/xlog_internal.h"
35 #include "libpq/pqsignal.h"
36 #include "miscadmin.h"
37 #include "postmaster/pgarch.h"
38 #include "postmaster/postmaster.h"
39 #include "storage/fd.h"
40 #include "storage/ipc.h"
41 #include "storage/pg_shmem.h"
42 #include "storage/pmsignal.h"
43 #include "utils/guc.h"
44 #include "utils/ps_status.h"
51 #define PGARCH_AUTOWAKE_INTERVAL 60 /* How often to force a poll of
52 * the archive status directory;
54 #define PGARCH_RESTART_INTERVAL 60 /* How often to attempt to restart
55 * a failed archiver; in seconds. */
58 * Archiver control info.
60 * We expect that archivable files within pg_xlog will have names between
61 * MIN_XFN_CHARS and MAX_XFN_CHARS in length, consisting only of characters
62 * appearing in VALID_XFN_CHARS. The status files in archive_status have
63 * corresponding names with ".ready" or ".done" appended.
66 #define MIN_XFN_CHARS 16
67 #define MAX_XFN_CHARS 40
68 #define VALID_XFN_CHARS "0123456789ABCDEF.history.backup"
70 #define NUM_ARCHIVE_RETRIES 3
77 static time_t last_pgarch_start_time;
80 * Flags set by interrupt handlers for later service in the main loop.
82 static volatile sig_atomic_t got_SIGHUP = false;
83 static volatile sig_atomic_t wakened = false;
86 * Local function forward declarations
90 static pid_t pgarch_forkexec(void);
93 NON_EXEC_STATIC void PgArchiverMain(int argc, char *argv[]);
94 static void pgarch_exit(SIGNAL_ARGS);
95 static void ArchSigHupHandler(SIGNAL_ARGS);
96 static void pgarch_waken(SIGNAL_ARGS);
97 static void pgarch_MainLoop(void);
98 static void pgarch_ArchiverCopyLoop(void);
99 static bool pgarch_archiveXlog(char *xlog);
100 static bool pgarch_readyXlog(char *xlog);
101 static void pgarch_archiveDone(char *xlog);
104 /* ------------------------------------------------------------
105 * Public functions called from postmaster follow
106 * ------------------------------------------------------------
112 * Called from postmaster at startup or after an existing archiver
113 * died. Attempt to fire up a fresh archiver process.
115 * Returns PID of child process, or 0 if fail.
117 * Note: if fail, we will be called again from the postmaster main loop.
126 * Do nothing if no archiver needed
128 if (!XLogArchivingActive())
132 * Do nothing if too soon since last archiver start. This is a
133 * safety valve to protect against continuous respawn attempts if the
134 * archiver is dying immediately at launch. Note that since we will
135 * be re-called from the postmaster main loop, we will get another
138 curtime = time(NULL);
139 if ((unsigned int) (curtime - last_pgarch_start_time) <
140 (unsigned int) PGARCH_RESTART_INTERVAL)
142 last_pgarch_start_time = curtime;
148 /* Specific beos actions before backend startup */
149 beos_before_backend_startup();
153 switch ((pgArchPid = pgarch_forkexec()))
155 switch ((pgArchPid = fork()))
160 /* Specific beos actions */
161 beos_backend_startup_failed();
164 (errmsg("could not fork archiver: %m")));
169 /* in postmaster child ... */
171 /* Specific beos actions after backend startup */
172 beos_backend_startup();
174 /* Close the postmaster's sockets */
175 ClosePostmasterPorts();
177 /* Drop our connection to postmaster's shared memory, as well */
178 PGSharedMemoryDetach();
180 PgArchiverMain(0, NULL);
185 return (int) pgArchPid;
188 /* shouldn't get here */
192 /* ------------------------------------------------------------
193 * Local functions called by archiver follow
194 * ------------------------------------------------------------
201 * pgarch_forkexec() -
203 * Format up the arglist for, then fork and exec, archive process
206 pgarch_forkexec(void)
211 av[ac++] = "postgres";
213 av[ac++] = "-forkarch";
215 av[ac++] = NULL; /* filled in by postmaster_forkexec */
218 Assert(ac < lengthof(av));
220 return postmaster_forkexec(ac, av);
223 #endif /* EXEC_BACKEND */
229 * The argc/argv parameters are valid only in EXEC_BACKEND case. However,
230 * since we don't use 'em, it hardly matters...
233 PgArchiverMain(int argc, char *argv[])
235 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
237 MyProcPid = getpid(); /* reset MyProcPid */
239 /* Lose the postmaster's on-exit routines */
243 * Ignore all signals usually bound to some action in the postmaster,
244 * except for SIGHUP, SIGUSR1 and SIGQUIT.
246 pqsignal(SIGHUP, ArchSigHupHandler);
247 pqsignal(SIGINT, SIG_IGN);
248 pqsignal(SIGTERM, SIG_IGN);
249 pqsignal(SIGQUIT, pgarch_exit);
250 pqsignal(SIGALRM, SIG_IGN);
251 pqsignal(SIGPIPE, SIG_IGN);
252 pqsignal(SIGUSR1, pgarch_waken);
253 pqsignal(SIGUSR2, SIG_IGN);
254 pqsignal(SIGCHLD, SIG_DFL);
255 pqsignal(SIGTTIN, SIG_DFL);
256 pqsignal(SIGTTOU, SIG_DFL);
257 pqsignal(SIGCONT, SIG_DFL);
258 pqsignal(SIGWINCH, SIG_DFL);
259 PG_SETMASK(&UnBlockSig);
262 * Identify myself via ps
264 init_ps_display("archiver process", "", "");
267 /* Init XLOG file paths --- needed in EXEC_BACKEND case */
275 /* SIGQUIT signal handler for archiver process */
277 pgarch_exit(SIGNAL_ARGS)
280 * For now, we just nail the doors shut and get out of town. It might
281 * seem cleaner to finish up any pending archive copies, but there's
282 * a nontrivial risk that init will kill us partway through.
287 /* SIGHUP: set flag to re-read config file at next convenient time */
289 ArchSigHupHandler(SIGNAL_ARGS)
294 /* SIGUSR1 signal handler for archiver process */
296 pgarch_waken(SIGNAL_ARGS)
304 * Main loop for archiver
307 pgarch_MainLoop(void)
309 time_t last_copy_time = 0;
313 * We run the copy loop immediately upon entry, in case there are
314 * unarchived files left over from a previous database run (or maybe
315 * the archiver died unexpectedly). After that we wait for a signal
316 * or timeout before doing more.
322 /* Check for config update */
326 ProcessConfigFile(PGC_SIGHUP);
327 if (!XLogArchivingActive())
328 break; /* user wants us to shut down */
331 /* Do what we're here for */
335 pgarch_ArchiverCopyLoop();
336 last_copy_time = time(NULL);
340 * There shouldn't be anything for the archiver to do except
341 * to wait for a signal, so we could use pause(3) here...
342 * ...however, the archiver exists to protect our data, so
343 * she wakes up occasionally to allow herself to be proactive.
344 * In particular this avoids getting stuck if a signal arrives
345 * just before we enter sleep().
349 sleep(PGARCH_AUTOWAKE_INTERVAL);
351 curtime = time(NULL);
352 if ((unsigned int) (curtime - last_copy_time) >=
353 (unsigned int) PGARCH_AUTOWAKE_INTERVAL)
356 } while (PostmasterIsAlive(true));
360 * pgarch_ArchiverCopyLoop
362 * Archives all outstanding xlogs then returns
365 pgarch_ArchiverCopyLoop(void)
367 char xlog[MAX_XFN_CHARS + 1];
370 * loop through all xlogs with archive_status of .ready
371 * and archive them...mostly we expect this to be a single
372 * file, though it is possible some backend will add
373 * files onto the list of those that need archiving while we
374 * are still copying earlier archives
376 while (pgarch_readyXlog(xlog))
382 if (pgarch_archiveXlog(xlog))
385 pgarch_archiveDone(xlog);
386 break; /* out of inner retry loop */
390 if (++failures >= NUM_ARCHIVE_RETRIES)
393 (errmsg("transaction log file \"%s\" could not be archived",
395 return; /* give up archiving for now */
397 sleep(1); /* wait a bit before retrying */
406 * Invokes system(3) to copy one archive file to wherever it should go
408 * Returns true if successful
411 pgarch_archiveXlog(char *xlog)
413 char xlogarchcmd[MAXPGPATH];
414 char pathname[MAXPGPATH];
420 snprintf(pathname, MAXPGPATH, "%s/%s", XLogDir, xlog);
423 * construct the command to be executed
426 endp = xlogarchcmd + MAXPGPATH - 1;
429 for (sp = XLogArchiveCommand; *sp; sp++)
436 /* %p: full path of source file */
438 StrNCpy(dp, pathname, endp-dp);
442 /* %f: filename of source file */
444 StrNCpy(dp, xlog, endp-dp);
448 /* convert %% to a single % */
454 /* otherwise treat the % as not special */
469 (errmsg_internal("executing archive command \"%s\"",
471 rc = system(xlogarchcmd);
474 (errmsg("archive command \"%s\" failed: return code %d",
479 (errmsg("archived transaction log file \"%s\"", xlog)));
487 * Return name of the oldest xlog file that has not yet been archived.
488 * No notification is set that file archiving is now in progress, so
489 * this would need to be extended if multiple concurrent archival
490 * tasks were created. If a failure occurs, we will completely
491 * re-copy the file at the next available opportunity.
493 * It is important that we return the oldest, so that we archive xlogs
494 * in order that they were written, for two reasons:
495 * 1) to maintain the sequential chain of xlogs required for recovery
496 * 2) because the oldest ones will sooner become candidates for
497 * recycling at time of checkpoint
499 * NOTE: the "oldest" comparison will presently consider all segments of
500 * a timeline with a smaller ID to be older than all segments of a timeline
501 * with a larger ID; the net result being that past timelines are given
502 * higher priority for archiving. This seems okay, or at least not
503 * obviously worth changing.
506 pgarch_readyXlog(char *xlog)
509 * open xlog status directory and read through list of
510 * xlogs that have the .ready suffix, looking for earliest file.
511 * It is possible to optimise this code, though only a single
512 * file is expected on the vast majority of calls, so....
514 char XLogArchiveStatusDir[MAXPGPATH];
515 char newxlog[MAX_XFN_CHARS + 6 + 1];
520 snprintf(XLogArchiveStatusDir, MAXPGPATH, "%s/archive_status", XLogDir);
521 rldir = AllocateDir(XLogArchiveStatusDir);
524 (errcode_for_file_access(),
525 errmsg("could not open archive status directory \"%s\": %m",
526 XLogArchiveStatusDir)));
529 while ((rlde = readdir(rldir)) != NULL)
531 int basenamelen = (int) strlen(rlde->d_name) - 6;
533 if (basenamelen >= MIN_XFN_CHARS &&
534 basenamelen <= MAX_XFN_CHARS &&
535 strspn(rlde->d_name, VALID_XFN_CHARS) >= basenamelen &&
536 strcmp(rlde->d_name + basenamelen, ".ready") == 0)
539 strcpy(newxlog, rlde->d_name);
542 if (strcmp(rlde->d_name, newxlog) < 0)
543 strcpy(newxlog, rlde->d_name);
550 /* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
551 not in released version */
552 if (GetLastError() == ERROR_NO_MORE_FILES)
557 (errcode_for_file_access(),
558 errmsg("could not read archive status directory \"%s\": %m",
559 XLogArchiveStatusDir)));
564 /* truncate off the .ready */
565 newxlog[strlen(newxlog) - 6] = '\0';
566 strcpy(xlog, newxlog);
574 * Emit notification that an xlog file has been successfully archived.
575 * We do this by renaming the status file from NNN.ready to NNN.done.
576 * Eventually, a checkpoint process will notice this and delete both the
577 * NNN.done file and the xlog file itself.
580 pgarch_archiveDone(char *xlog)
582 char rlogready[MAXPGPATH];
583 char rlogdone[MAXPGPATH];
585 StatusFilePath(rlogready, xlog, ".ready");
586 StatusFilePath(rlogdone, xlog, ".done");
587 if (rename(rlogready, rlogdone) < 0)
589 (errcode_for_file_access(),
590 errmsg("could not rename file \"%s\" to \"%s\": %m",
591 rlogready, rlogdone)));