2004-07-19 04:47:16 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pgarch.c
|
|
|
|
*
|
|
|
|
* PostgreSQL WAL archiver
|
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* All functions relating to archiver are included here
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* - All functions executed by archiver process
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* - archiver is forked from postmaster, and the two
|
|
|
|
* processes then communicate using signals. All functions
|
|
|
|
* executed by postmaster are included in this file.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Initial author: Simon Riggs simon@2ndquadrant.com
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2012-01-02 00:01:58 +01:00
|
|
|
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
2004-07-19 04:47:16 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/postmaster/pgarch.c
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
2006-11-21 21:59:53 +01:00
|
|
|
#include <sys/wait.h>
|
2004-07-19 04:47:16 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
#include "access/xlog_internal.h"
|
2004-07-19 04:47:16 +02:00
|
|
|
#include "libpq/pqsignal.h"
|
|
|
|
#include "miscadmin.h"
|
2005-03-10 08:14:03 +01:00
|
|
|
#include "postmaster/fork_process.h"
|
2004-07-22 00:31:26 +02:00
|
|
|
#include "postmaster/pgarch.h"
|
2004-07-19 04:47:16 +02:00
|
|
|
#include "postmaster/postmaster.h"
|
|
|
|
#include "storage/fd.h"
|
|
|
|
#include "storage/ipc.h"
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
#include "storage/latch.h"
|
2004-07-19 04:47:16 +02:00
|
|
|
#include "storage/pg_shmem.h"
|
|
|
|
#include "storage/pmsignal.h"
|
|
|
|
#include "utils/guc.h"
|
|
|
|
#include "utils/ps_status.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* Timer definitions.
|
|
|
|
* ----------
|
|
|
|
*/
|
2005-10-15 04:49:52 +02:00
|
|
|
#define PGARCH_AUTOWAKE_INTERVAL 60 /* How often to force a poll of the
|
|
|
|
* archive status directory; in
|
|
|
|
* seconds. */
|
|
|
|
#define PGARCH_RESTART_INTERVAL 10 /* How often to attempt to restart a
|
|
|
|
* failed archiver; in seconds. */
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* Archiver control info.
|
|
|
|
*
|
|
|
|
* We expect that archivable files within pg_xlog will have names between
|
|
|
|
* MIN_XFN_CHARS and MAX_XFN_CHARS in length, consisting only of characters
|
|
|
|
* appearing in VALID_XFN_CHARS. The status files in archive_status have
|
|
|
|
* corresponding names with ".ready" or ".done" appended.
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
#define MIN_XFN_CHARS 16
|
2004-08-03 22:32:36 +02:00
|
|
|
#define MAX_XFN_CHARS 40
|
2004-08-29 07:07:03 +02:00
|
|
|
#define VALID_XFN_CHARS "0123456789ABCDEF.history.backup"
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
#define NUM_ARCHIVE_RETRIES 3
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* Local data
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
static time_t last_pgarch_start_time;
|
2008-01-11 01:54:09 +01:00
|
|
|
static time_t last_sigterm_time = 0;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Flags set by interrupt handlers for later service in the main loop.
|
|
|
|
*/
|
|
|
|
static volatile sig_atomic_t got_SIGHUP = false;
|
2008-01-11 01:54:09 +01:00
|
|
|
static volatile sig_atomic_t got_SIGTERM = false;
|
2004-07-19 04:47:16 +02:00
|
|
|
static volatile sig_atomic_t wakened = false;
|
2008-01-11 01:54:09 +01:00
|
|
|
static volatile sig_atomic_t ready_to_stop = false;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
/*
|
|
|
|
* Latch used by signal handlers to wake up the sleep in the main loop.
|
|
|
|
*/
|
|
|
|
static Latch mainloop_latch;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/* ----------
|
|
|
|
* Local function forward declarations
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
#ifdef EXEC_BACKEND
|
|
|
|
static pid_t pgarch_forkexec(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NON_EXEC_STATIC void PgArchiverMain(int argc, char *argv[]);
|
|
|
|
static void pgarch_exit(SIGNAL_ARGS);
|
|
|
|
static void ArchSigHupHandler(SIGNAL_ARGS);
|
2008-01-11 01:54:09 +01:00
|
|
|
static void ArchSigTermHandler(SIGNAL_ARGS);
|
2004-07-19 04:47:16 +02:00
|
|
|
static void pgarch_waken(SIGNAL_ARGS);
|
2008-01-11 01:54:09 +01:00
|
|
|
static void pgarch_waken_stop(SIGNAL_ARGS);
|
2004-07-19 04:47:16 +02:00
|
|
|
static void pgarch_MainLoop(void);
|
|
|
|
static void pgarch_ArchiverCopyLoop(void);
|
|
|
|
static bool pgarch_archiveXlog(char *xlog);
|
|
|
|
static bool pgarch_readyXlog(char *xlog);
|
|
|
|
static void pgarch_archiveDone(char *xlog);
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------
|
|
|
|
* Public functions called from postmaster follow
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgarch_start
|
|
|
|
*
|
|
|
|
* Called from postmaster at startup or after an existing archiver
|
|
|
|
* died. Attempt to fire up a fresh archiver process.
|
|
|
|
*
|
|
|
|
* Returns PID of child process, or 0 if fail.
|
|
|
|
*
|
|
|
|
* Note: if fail, we will be called again from the postmaster main loop.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pgarch_start(void)
|
|
|
|
{
|
|
|
|
time_t curtime;
|
|
|
|
pid_t pgArchPid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do nothing if no archiver needed
|
|
|
|
*/
|
|
|
|
if (!XLogArchivingActive())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Do nothing if too soon since last archiver start. This is a safety
|
2005-10-15 04:49:52 +02:00
|
|
|
* valve to protect against continuous respawn attempts if the archiver is
|
|
|
|
* dying immediately at launch. Note that since we will be re-called from
|
|
|
|
* the postmaster main loop, we will get another chance later.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
curtime = time(NULL);
|
|
|
|
if ((unsigned int) (curtime - last_pgarch_start_time) <
|
|
|
|
(unsigned int) PGARCH_RESTART_INTERVAL)
|
2004-08-29 07:07:03 +02:00
|
|
|
return 0;
|
2004-07-19 04:47:16 +02:00
|
|
|
last_pgarch_start_time = curtime;
|
|
|
|
|
|
|
|
#ifdef EXEC_BACKEND
|
|
|
|
switch ((pgArchPid = pgarch_forkexec()))
|
|
|
|
#else
|
2005-03-10 08:14:03 +01:00
|
|
|
switch ((pgArchPid = fork_process()))
|
2004-07-19 04:47:16 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("could not fork archiver: %m")));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifndef EXEC_BACKEND
|
|
|
|
case 0:
|
|
|
|
/* in postmaster child ... */
|
|
|
|
/* Close the postmaster's sockets */
|
2004-08-06 01:32:13 +02:00
|
|
|
ClosePostmasterPorts(false);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2006-01-04 22:06:32 +01:00
|
|
|
/* Lose the postmaster's on-exit routines */
|
|
|
|
on_exit_reset();
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/* Drop our connection to postmaster's shared memory, as well */
|
|
|
|
PGSharedMemoryDetach();
|
|
|
|
|
|
|
|
PgArchiverMain(0, NULL);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (int) pgArchPid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shouldn't get here */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------
|
|
|
|
* Local functions called by archiver follow
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef EXEC_BACKEND
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgarch_forkexec() -
|
|
|
|
*
|
|
|
|
* Format up the arglist for, then fork and exec, archive process
|
|
|
|
*/
|
|
|
|
static pid_t
|
|
|
|
pgarch_forkexec(void)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char *av[10];
|
|
|
|
int ac = 0;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
av[ac++] = "postgres";
|
|
|
|
|
2006-06-18 17:38:37 +02:00
|
|
|
av[ac++] = "--forkarch";
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
av[ac++] = NULL; /* filled in by postmaster_forkexec */
|
|
|
|
|
|
|
|
av[ac] = NULL;
|
|
|
|
Assert(ac < lengthof(av));
|
|
|
|
|
|
|
|
return postmaster_forkexec(ac, av);
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
#endif /* EXEC_BACKEND */
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PgArchiverMain
|
|
|
|
*
|
|
|
|
* The argc/argv parameters are valid only in EXEC_BACKEND case. However,
|
|
|
|
* since we don't use 'em, it hardly matters...
|
|
|
|
*/
|
|
|
|
NON_EXEC_STATIC void
|
|
|
|
PgArchiverMain(int argc, char *argv[])
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
IsUnderPostmaster = true; /* we are a postmaster subprocess now */
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
MyProcPid = getpid(); /* reset MyProcPid */
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
InitLatch(&mainloop_latch); /* initialize latch used in main loop */
|
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
MyStartTime = time(NULL); /* record Start Time for logging */
|
2007-08-03 01:39:45 +02:00
|
|
|
|
2006-11-21 21:59:53 +01:00
|
|
|
/*
|
|
|
|
* If possible, make this process a group leader, so that the postmaster
|
|
|
|
* can signal any child processes too.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_SETSID
|
|
|
|
if (setsid() < 0)
|
|
|
|
elog(FATAL, "setsid() failed: %m");
|
|
|
|
#endif
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
|
|
|
* Ignore all signals usually bound to some action in the postmaster,
|
2008-01-11 01:54:09 +01:00
|
|
|
* except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
|
|
|
pqsignal(SIGHUP, ArchSigHupHandler);
|
|
|
|
pqsignal(SIGINT, SIG_IGN);
|
2008-01-11 01:54:09 +01:00
|
|
|
pqsignal(SIGTERM, ArchSigTermHandler);
|
2004-08-29 07:07:03 +02:00
|
|
|
pqsignal(SIGQUIT, pgarch_exit);
|
|
|
|
pqsignal(SIGALRM, SIG_IGN);
|
|
|
|
pqsignal(SIGPIPE, SIG_IGN);
|
|
|
|
pqsignal(SIGUSR1, pgarch_waken);
|
2008-01-11 01:54:09 +01:00
|
|
|
pqsignal(SIGUSR2, pgarch_waken_stop);
|
2004-08-29 07:07:03 +02:00
|
|
|
pqsignal(SIGCHLD, SIG_DFL);
|
|
|
|
pqsignal(SIGTTIN, SIG_DFL);
|
|
|
|
pqsignal(SIGTTOU, SIG_DFL);
|
|
|
|
pqsignal(SIGCONT, SIG_DFL);
|
|
|
|
pqsignal(SIGWINCH, SIG_DFL);
|
|
|
|
PG_SETMASK(&UnBlockSig);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identify myself via ps
|
|
|
|
*/
|
2006-06-28 00:16:44 +02:00
|
|
|
init_ps_display("archiver process", "", "", "");
|
2004-08-29 07:07:03 +02:00
|
|
|
|
|
|
|
pgarch_MainLoop();
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
exit(0);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGQUIT signal handler for archiver process */
|
|
|
|
static void
|
|
|
|
pgarch_exit(SIGNAL_ARGS)
|
|
|
|
{
|
2008-01-11 01:54:09 +01:00
|
|
|
/* SIGQUIT means curl up and die ... */
|
|
|
|
exit(1);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/* SIGHUP signal handler for archiver process */
|
2004-07-19 04:47:16 +02:00
|
|
|
static void
|
|
|
|
ArchSigHupHandler(SIGNAL_ARGS)
|
|
|
|
{
|
2011-08-10 18:20:30 +02:00
|
|
|
int save_errno = errno;
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/* set flag to re-read config file at next convenient time */
|
2004-07-19 04:47:16 +02:00
|
|
|
got_SIGHUP = true;
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
SetLatch(&mainloop_latch);
|
2011-08-10 18:20:30 +02:00
|
|
|
|
|
|
|
errno = save_errno;
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/* SIGTERM signal handler for archiver process */
|
|
|
|
static void
|
|
|
|
ArchSigTermHandler(SIGNAL_ARGS)
|
|
|
|
{
|
2011-08-10 18:20:30 +02:00
|
|
|
int save_errno = errno;
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/*
|
|
|
|
* The postmaster never sends us SIGTERM, so we assume that this means
|
|
|
|
* that init is trying to shut down the whole system. If we hang around
|
|
|
|
* too long we'll get SIGKILL'd. Set flag to prevent starting any more
|
|
|
|
* archive commands.
|
|
|
|
*/
|
|
|
|
got_SIGTERM = true;
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
SetLatch(&mainloop_latch);
|
2011-08-10 18:20:30 +02:00
|
|
|
|
|
|
|
errno = save_errno;
|
2008-01-11 01:54:09 +01:00
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/* SIGUSR1 signal handler for archiver process */
|
|
|
|
static void
|
|
|
|
pgarch_waken(SIGNAL_ARGS)
|
|
|
|
{
|
2011-08-10 18:20:30 +02:00
|
|
|
int save_errno = errno;
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/* set flag that there is work to be done */
|
2004-07-19 04:47:16 +02:00
|
|
|
wakened = true;
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
SetLatch(&mainloop_latch);
|
2011-08-10 18:20:30 +02:00
|
|
|
|
|
|
|
errno = save_errno;
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/* SIGUSR2 signal handler for archiver process */
|
|
|
|
static void
|
|
|
|
pgarch_waken_stop(SIGNAL_ARGS)
|
|
|
|
{
|
2011-08-10 18:20:30 +02:00
|
|
|
int save_errno = errno;
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/* set flag to do a final cycle and shut down afterwards */
|
|
|
|
ready_to_stop = true;
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
SetLatch(&mainloop_latch);
|
2011-08-10 18:20:30 +02:00
|
|
|
|
|
|
|
errno = save_errno;
|
2008-01-11 01:54:09 +01:00
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* pgarch_MainLoop
|
|
|
|
*
|
|
|
|
* Main loop for archiver
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pgarch_MainLoop(void)
|
|
|
|
{
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
pg_time_t last_copy_time = 0;
|
2008-01-11 01:54:09 +01:00
|
|
|
bool time_to_stop;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We run the copy loop immediately upon entry, in case there are
|
2005-10-15 04:49:52 +02:00
|
|
|
* unarchived files left over from a previous database run (or maybe the
|
|
|
|
* archiver died unexpectedly). After that we wait for a signal or
|
|
|
|
* timeout before doing more.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
wakened = true;
|
|
|
|
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* There shouldn't be anything for the archiver to do except to wait for a
|
|
|
|
* signal ... however, the archiver exists to protect our data, so she
|
|
|
|
* wakes up occasionally to allow herself to be proactive.
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
do
|
|
|
|
{
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
ResetLatch(&mainloop_latch);
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/* When we get SIGUSR2, we do one more archive cycle, then exit */
|
|
|
|
time_to_stop = ready_to_stop;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/* Check for config update */
|
|
|
|
if (got_SIGHUP)
|
|
|
|
{
|
|
|
|
got_SIGHUP = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
|
|
|
}
|
|
|
|
|
2008-01-11 01:54:09 +01:00
|
|
|
/*
|
|
|
|
* If we've gotten SIGTERM, we normally just sit and do nothing until
|
|
|
|
* SIGUSR2 arrives. However, that means a random SIGTERM would
|
|
|
|
* disable archiving indefinitely, which doesn't seem like a good
|
2009-06-11 16:49:15 +02:00
|
|
|
* idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
|
|
|
|
* that the postmaster can start a new archiver if needed.
|
2008-01-11 01:54:09 +01:00
|
|
|
*/
|
|
|
|
if (got_SIGTERM)
|
|
|
|
{
|
|
|
|
time_t curtime = time(NULL);
|
|
|
|
|
|
|
|
if (last_sigterm_time == 0)
|
|
|
|
last_sigterm_time = curtime;
|
|
|
|
else if ((unsigned int) (curtime - last_sigterm_time) >=
|
|
|
|
(unsigned int) 60)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/* Do what we're here for */
|
2008-01-11 01:54:09 +01:00
|
|
|
if (wakened || time_to_stop)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
|
|
|
wakened = false;
|
|
|
|
pgarch_ArchiverCopyLoop();
|
|
|
|
last_copy_time = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
* Sleep until a signal is received, or until a poll is forced by
|
|
|
|
* PGARCH_AUTOWAKE_INTERVAL having passed since last_copy_time, or
|
|
|
|
* until postmaster dies.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2012-06-10 21:20:04 +02:00
|
|
|
if (!time_to_stop) /* Don't wait during last iteration */
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
pg_time_t curtime = (pg_time_t) time(NULL);
|
|
|
|
int timeout;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
timeout = PGARCH_AUTOWAKE_INTERVAL - (curtime - last_copy_time);
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
int rc;
|
2011-08-10 00:52:29 +02:00
|
|
|
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
rc = WaitLatch(&mainloop_latch,
|
2012-06-10 21:20:04 +02:00
|
|
|
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
|
2011-08-10 00:52:29 +02:00
|
|
|
timeout * 1000L);
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
if (rc & WL_TIMEOUT)
|
|
|
|
wakened = true;
|
|
|
|
}
|
|
|
|
else
|
2004-07-19 04:47:16 +02:00
|
|
|
wakened = true;
|
|
|
|
}
|
2008-01-11 01:54:09 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The archiver quits either when the postmaster dies (not expected)
|
|
|
|
* or after completing one more archiving cycle after receiving
|
|
|
|
* SIGUSR2.
|
|
|
|
*/
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
} while (PostmasterIsAlive() && !time_to_stop);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgarch_ArchiverCopyLoop
|
|
|
|
*
|
|
|
|
* Archives all outstanding xlogs then returns
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pgarch_ArchiverCopyLoop(void)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char xlog[MAX_XFN_CHARS + 1];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* loop through all xlogs with archive_status of .ready and archive
|
2005-10-15 04:49:52 +02:00
|
|
|
* them...mostly we expect this to be a single file, though it is possible
|
|
|
|
* some backend will add files onto the list of those that need archiving
|
|
|
|
* while we are still copying earlier archives
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
2006-05-30 19:08:14 +02:00
|
|
|
while (pgarch_readyXlog(xlog))
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int failures = 0;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2008-01-11 01:54:09 +01:00
|
|
|
/*
|
|
|
|
* Do not initiate any more archive commands after receiving
|
2009-06-11 16:49:15 +02:00
|
|
|
* SIGTERM, nor after the postmaster has died unexpectedly. The
|
|
|
|
* first condition is to try to keep from having init SIGKILL the
|
|
|
|
* command, and the second is to avoid conflicts with another
|
|
|
|
* archiver spawned by a newer postmaster.
|
2008-01-11 01:54:09 +01:00
|
|
|
*/
|
Introduce a pipe between postmaster and each backend, which can be used to
detect postmaster death. Postmaster keeps the write-end of the pipe open,
so when it dies, children get EOF in the read-end. That can conveniently
be waited for in select(), which allows eliminating some of the polling
loops that check for postmaster death. This patch doesn't yet change all
the loops to use the new mechanism, expect a follow-on patch to do that.
This changes the interface to WaitLatch, so that it takes as argument a
bitmask of events that it waits for. Possible events are latch set, timeout,
postmaster death, and socket becoming readable or writeable.
The pipe method behaves slightly differently from the kill() method
previously used in PostmasterIsAlive() in the case that postmaster has died,
but its parent has not yet read its exit code with waitpid(). The pipe
returns EOF as soon as the process dies, but kill() continues to return
true until waitpid() has been called (IOW while the process is a zombie).
Because of that, change PostmasterIsAlive() to use the pipe too, otherwise
WaitLatch() would return immediately with WL_POSTMASTER_DEATH, while
PostmasterIsAlive() would claim it's still alive. That could easily lead to
busy-waiting while postmaster is in zombie state.
Peter Geoghegan with further changes by me, reviewed by Fujii Masao and
Florian Pflug.
2011-07-08 17:27:49 +02:00
|
|
|
if (got_SIGTERM || !PostmasterIsAlive())
|
2006-05-30 19:08:14 +02:00
|
|
|
return;
|
|
|
|
|
2010-05-11 18:42:28 +02:00
|
|
|
/*
|
|
|
|
* Check for config update. This is so that we'll adopt a new
|
|
|
|
* setting for archive_command as soon as possible, even if there
|
|
|
|
* is a backlog of files to be archived.
|
|
|
|
*/
|
|
|
|
if (got_SIGHUP)
|
|
|
|
{
|
|
|
|
got_SIGHUP = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can't do anything if no command ... */
|
|
|
|
if (!XLogArchiveCommandSet())
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("archive_mode enabled, yet archive_command is not set")));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
if (pgarch_archiveXlog(xlog))
|
|
|
|
{
|
|
|
|
/* successful */
|
|
|
|
pgarch_archiveDone(xlog);
|
|
|
|
break; /* out of inner retry loop */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (++failures >= NUM_ARCHIVE_RETRIES)
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
2004-11-09 14:01:27 +01:00
|
|
|
(errmsg("transaction log file \"%s\" could not be archived: too many failures",
|
2004-07-19 04:47:16 +02:00
|
|
|
xlog)));
|
|
|
|
return; /* give up archiving for now */
|
|
|
|
}
|
2004-11-18 18:13:38 +01:00
|
|
|
pg_usleep(1000000L); /* wait a bit before retrying */
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgarch_archiveXlog
|
|
|
|
*
|
|
|
|
* Invokes system(3) to copy one archive file to wherever it should go
|
|
|
|
*
|
|
|
|
* Returns true if successful
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
pgarch_archiveXlog(char *xlog)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char xlogarchcmd[MAXPGPATH];
|
|
|
|
char pathname[MAXPGPATH];
|
2007-12-18 01:49:34 +01:00
|
|
|
char activitymsg[MAXFNAMELEN + 16];
|
2004-08-29 07:07:03 +02:00
|
|
|
char *dp;
|
|
|
|
char *endp;
|
2004-07-19 04:47:16 +02:00
|
|
|
const char *sp;
|
2004-08-29 07:07:03 +02:00
|
|
|
int rc;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* construct the command to be executed
|
|
|
|
*/
|
|
|
|
dp = xlogarchcmd;
|
|
|
|
endp = xlogarchcmd + MAXPGPATH - 1;
|
|
|
|
*endp = '\0';
|
|
|
|
|
|
|
|
for (sp = XLogArchiveCommand; *sp; sp++)
|
|
|
|
{
|
|
|
|
if (*sp == '%')
|
|
|
|
{
|
|
|
|
switch (sp[1])
|
|
|
|
{
|
|
|
|
case 'p':
|
2006-11-10 23:32:20 +01:00
|
|
|
/* %p: relative path of source file */
|
2004-07-19 04:47:16 +02:00
|
|
|
sp++;
|
2007-02-10 15:58:55 +01:00
|
|
|
strlcpy(dp, pathname, endp - dp);
|
2004-08-12 21:03:44 +02:00
|
|
|
make_native_path(dp);
|
2004-07-19 04:47:16 +02:00
|
|
|
dp += strlen(dp);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
/* %f: filename of source file */
|
|
|
|
sp++;
|
2007-02-10 15:58:55 +01:00
|
|
|
strlcpy(dp, xlog, endp - dp);
|
2004-07-19 04:47:16 +02:00
|
|
|
dp += strlen(dp);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
/* convert %% to a single % */
|
|
|
|
sp++;
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* otherwise treat the % as not special */
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
ereport(DEBUG3,
|
2004-08-29 07:07:03 +02:00
|
|
|
(errmsg_internal("executing archive command \"%s\"",
|
2004-07-19 04:47:16 +02:00
|
|
|
xlogarchcmd)));
|
2007-12-18 01:49:34 +01:00
|
|
|
|
|
|
|
/* Report archive activity in PS display */
|
|
|
|
snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
|
|
|
|
set_ps_display(activitymsg, false);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
rc = system(xlogarchcmd);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
2006-11-21 21:59:53 +01:00
|
|
|
/*
|
|
|
|
* If either the shell itself, or a called command, died on a signal,
|
2007-11-15 22:14:46 +01:00
|
|
|
* abort the archiver. We do this because system() ignores SIGINT and
|
2006-11-21 21:59:53 +01:00
|
|
|
* SIGQUIT while waiting; so a signal is very likely something that
|
2007-11-15 22:14:46 +01:00
|
|
|
* should have interrupted us too. If we overreact it's no big deal,
|
2006-11-21 21:59:53 +01:00
|
|
|
* the postmaster will just start the archiver again.
|
|
|
|
*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Per the Single Unix Spec, shells report exit status > 128 when a
|
|
|
|
* called command died on a signal.
|
2006-11-21 21:59:53 +01:00
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
int lev = (WIFSIGNALED(rc) || WEXITSTATUS(rc) > 128) ? FATAL : LOG;
|
2006-11-21 21:59:53 +01:00
|
|
|
|
2007-12-12 10:39:54 +01:00
|
|
|
if (WIFEXITED(rc))
|
|
|
|
{
|
|
|
|
ereport(lev,
|
2007-12-12 17:53:14 +01:00
|
|
|
(errmsg("archive command failed with exit code %d",
|
|
|
|
WEXITSTATUS(rc)),
|
|
|
|
errdetail("The failed archive command was: %s",
|
|
|
|
xlogarchcmd)));
|
2007-12-12 10:39:54 +01:00
|
|
|
}
|
|
|
|
else if (WIFSIGNALED(rc))
|
|
|
|
{
|
|
|
|
#if defined(WIN32)
|
2007-12-12 17:53:14 +01:00
|
|
|
ereport(lev,
|
2009-06-11 16:49:15 +02:00
|
|
|
(errmsg("archive command was terminated by exception 0x%X",
|
|
|
|
WTERMSIG(rc)),
|
|
|
|
errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
|
|
|
|
errdetail("The failed archive command was: %s",
|
|
|
|
xlogarchcmd)));
|
2007-12-12 10:39:54 +01:00
|
|
|
#elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST
|
2007-12-12 17:53:14 +01:00
|
|
|
ereport(lev,
|
|
|
|
(errmsg("archive command was terminated by signal %d: %s",
|
|
|
|
WTERMSIG(rc),
|
2009-06-11 16:49:15 +02:00
|
|
|
WTERMSIG(rc) < NSIG ? sys_siglist[WTERMSIG(rc)] : "(unknown)"),
|
2007-12-12 17:53:14 +01:00
|
|
|
errdetail("The failed archive command was: %s",
|
|
|
|
xlogarchcmd)));
|
2007-12-12 10:39:54 +01:00
|
|
|
#else
|
2007-12-12 17:53:14 +01:00
|
|
|
ereport(lev,
|
|
|
|
(errmsg("archive command was terminated by signal %d",
|
|
|
|
WTERMSIG(rc)),
|
|
|
|
errdetail("The failed archive command was: %s",
|
|
|
|
xlogarchcmd)));
|
2007-12-12 10:39:54 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(lev,
|
2009-06-11 16:49:15 +02:00
|
|
|
(errmsg("archive command exited with unrecognized status %d",
|
|
|
|
rc),
|
|
|
|
errdetail("The failed archive command was: %s",
|
|
|
|
xlogarchcmd)));
|
2007-12-12 10:39:54 +01:00
|
|
|
}
|
2006-11-21 21:59:53 +01:00
|
|
|
|
2007-12-18 01:49:34 +01:00
|
|
|
snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
|
|
|
|
set_ps_display(activitymsg, false);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
2007-11-24 22:37:04 +01:00
|
|
|
ereport(DEBUG1,
|
2004-08-29 07:07:03 +02:00
|
|
|
(errmsg("archived transaction log file \"%s\"", xlog)));
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2007-12-18 01:49:34 +01:00
|
|
|
snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
|
|
|
|
set_ps_display(activitymsg, false);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
return true;
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgarch_readyXlog
|
|
|
|
*
|
|
|
|
* Return name of the oldest xlog file that has not yet been archived.
|
|
|
|
* No notification is set that file archiving is now in progress, so
|
|
|
|
* this would need to be extended if multiple concurrent archival
|
|
|
|
* tasks were created. If a failure occurs, we will completely
|
|
|
|
* re-copy the file at the next available opportunity.
|
|
|
|
*
|
|
|
|
* It is important that we return the oldest, so that we archive xlogs
|
|
|
|
* in order that they were written, for two reasons:
|
|
|
|
* 1) to maintain the sequential chain of xlogs required for recovery
|
|
|
|
* 2) because the oldest ones will sooner become candidates for
|
|
|
|
* recycling at time of checkpoint
|
2004-07-22 00:31:26 +02:00
|
|
|
*
|
|
|
|
* NOTE: the "oldest" comparison will presently consider all segments of
|
|
|
|
* a timeline with a smaller ID to be older than all segments of a timeline
|
|
|
|
* with a larger ID; the net result being that past timelines are given
|
|
|
|
* higher priority for archiving. This seems okay, or at least not
|
|
|
|
* obviously worth changing.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
pgarch_readyXlog(char *xlog)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* open xlog status directory and read through list of xlogs that have the
|
|
|
|
* .ready suffix, looking for earliest file. It is possible to optimise
|
|
|
|
* this code, though only a single file is expected on the vast majority
|
|
|
|
* of calls, so....
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
char XLogArchiveStatusDir[MAXPGPATH];
|
2004-08-29 07:07:03 +02:00
|
|
|
char newxlog[MAX_XFN_CHARS + 6 + 1];
|
|
|
|
DIR *rldir;
|
|
|
|
struct dirent *rlde;
|
|
|
|
bool found = false;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
|
2004-07-19 04:47:16 +02:00
|
|
|
rldir = AllocateDir(XLogArchiveStatusDir);
|
|
|
|
if (rldir == NULL)
|
|
|
|
ereport(ERROR,
|
2004-08-29 07:07:03 +02:00
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not open archive status directory \"%s\": %m",
|
|
|
|
XLogArchiveStatusDir)));
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2005-06-19 23:34:03 +02:00
|
|
|
while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int basenamelen = (int) strlen(rlde->d_name) - 6;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
if (basenamelen >= MIN_XFN_CHARS &&
|
|
|
|
basenamelen <= MAX_XFN_CHARS &&
|
|
|
|
strspn(rlde->d_name, VALID_XFN_CHARS) >= basenamelen &&
|
|
|
|
strcmp(rlde->d_name + basenamelen, ".ready") == 0)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
strcpy(newxlog, rlde->d_name);
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strcmp(rlde->d_name, newxlog) < 0)
|
|
|
|
strcpy(newxlog, rlde->d_name);
|
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
FreeDir(rldir);
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
/* truncate off the .ready */
|
|
|
|
newxlog[strlen(newxlog) - 6] = '\0';
|
|
|
|
strcpy(xlog, newxlog);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgarch_archiveDone
|
|
|
|
*
|
|
|
|
* Emit notification that an xlog file has been successfully archived.
|
|
|
|
* We do this by renaming the status file from NNN.ready to NNN.done.
|
|
|
|
* Eventually, a checkpoint process will notice this and delete both the
|
|
|
|
* NNN.done file and the xlog file itself.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pgarch_archiveDone(char *xlog)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char rlogready[MAXPGPATH];
|
|
|
|
char rlogdone[MAXPGPATH];
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
StatusFilePath(rlogready, xlog, ".ready");
|
|
|
|
StatusFilePath(rlogdone, xlog, ".done");
|
2004-08-29 07:07:03 +02:00
|
|
|
if (rename(rlogready, rlogdone) < 0)
|
|
|
|
ereport(WARNING,
|
2004-07-19 04:47:16 +02:00
|
|
|
(errcode_for_file_access(),
|
2004-08-01 19:45:43 +02:00
|
|
|
errmsg("could not rename file \"%s\" to \"%s\": %m",
|
2004-07-22 00:31:26 +02:00
|
|
|
rlogready, rlogdone)));
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|