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
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, 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>
|
Ensure cleanup of orphan archive status files
When a WAL segment is recycled, its ".ready" and ".done" status files
get also automatically removed, however this is not done in a durable
manner. Hence, in a subsequent crash, it could be possible that a
".ready" status file is still around with its corresponding segment
already gone.
If the backend reaches such a state, the archive command would most
likely complain about a segment non-existing and would keep retrying,
causing WAL segments to bloat pg_wal/, potentially making Postgres crash
hard when running out of space.
As status files are removed after each individual segment, using
durable_unlink() does not completely close the window either, as a crash
could happen between the moment the WAL segment is recycled and the
moment its status files are removed. This has also some performance
impact with the additional fsync() calls needed to make the removal in a
durable manner. Doing the cleanup at recovery is not cost-free either
as this makes crash recovery potentially take longer than necessary.
So, instead, as per an idea of Stephen Frost, make the archiver aware of
orphan status files and remove them on-the-fly if the corresponding
segment goes missing. Removal failures follow a model close to what
happens for WAL segments, where multiple attempts are done before giving
up temporarily, and where a successful orphan removal makes the archiver
move immediately to the next WAL segment thought as ready to be
archived.
Author: Michael Paquier
Reviewed-by: Nathan Bossart, Andres Freund, Stephen Frost, Kyotaro
Horiguchi
Discussion: https://postgr.es/m/20180928032827.GF1500@paquier.xyz
2018-12-10 07:00:59 +01:00
|
|
|
#include <sys/stat.h>
|
2004-07-19 04:47:16 +02:00
|
|
|
#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>
|
|
|
|
|
2012-12-13 13:59:13 +01:00
|
|
|
#include "access/xlog.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"
|
2014-01-28 18:58:22 +01:00
|
|
|
#include "pgstat.h"
|
2005-03-10 08:14:03 +01:00
|
|
|
#include "postmaster/fork_process.h"
|
2019-12-17 19:14:28 +01:00
|
|
|
#include "postmaster/interrupt.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"
|
2014-03-18 12:58:53 +01:00
|
|
|
#include "storage/dsm.h"
|
2004-07-19 04:47:16 +02:00
|
|
|
#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.
|
|
|
|
* ----------
|
|
|
|
*/
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +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
|
|
|
|
Ensure cleanup of orphan archive status files
When a WAL segment is recycled, its ".ready" and ".done" status files
get also automatically removed, however this is not done in a durable
manner. Hence, in a subsequent crash, it could be possible that a
".ready" status file is still around with its corresponding segment
already gone.
If the backend reaches such a state, the archive command would most
likely complain about a segment non-existing and would keep retrying,
causing WAL segments to bloat pg_wal/, potentially making Postgres crash
hard when running out of space.
As status files are removed after each individual segment, using
durable_unlink() does not completely close the window either, as a crash
could happen between the moment the WAL segment is recycled and the
moment its status files are removed. This has also some performance
impact with the additional fsync() calls needed to make the removal in a
durable manner. Doing the cleanup at recovery is not cost-free either
as this makes crash recovery potentially take longer than necessary.
So, instead, as per an idea of Stephen Frost, make the archiver aware of
orphan status files and remove them on-the-fly if the corresponding
segment goes missing. Removal failures follow a model close to what
happens for WAL segments, where multiple attempts are done before giving
up temporarily, and where a successful orphan removal makes the archiver
move immediately to the next WAL segment thought as ready to be
archived.
Author: Michael Paquier
Reviewed-by: Nathan Bossart, Andres Freund, Stephen Frost, Kyotaro
Horiguchi
Discussion: https://postgr.es/m/20180928032827.GF1500@paquier.xyz
2018-12-10 07:00:59 +01:00
|
|
|
/*
|
|
|
|
* Maximum number of retries allowed when attempting to archive a WAL
|
|
|
|
* file.
|
|
|
|
*/
|
2004-07-19 04:47:16 +02:00
|
|
|
#define NUM_ARCHIVE_RETRIES 3
|
|
|
|
|
Ensure cleanup of orphan archive status files
When a WAL segment is recycled, its ".ready" and ".done" status files
get also automatically removed, however this is not done in a durable
manner. Hence, in a subsequent crash, it could be possible that a
".ready" status file is still around with its corresponding segment
already gone.
If the backend reaches such a state, the archive command would most
likely complain about a segment non-existing and would keep retrying,
causing WAL segments to bloat pg_wal/, potentially making Postgres crash
hard when running out of space.
As status files are removed after each individual segment, using
durable_unlink() does not completely close the window either, as a crash
could happen between the moment the WAL segment is recycled and the
moment its status files are removed. This has also some performance
impact with the additional fsync() calls needed to make the removal in a
durable manner. Doing the cleanup at recovery is not cost-free either
as this makes crash recovery potentially take longer than necessary.
So, instead, as per an idea of Stephen Frost, make the archiver aware of
orphan status files and remove them on-the-fly if the corresponding
segment goes missing. Removal failures follow a model close to what
happens for WAL segments, where multiple attempts are done before giving
up temporarily, and where a successful orphan removal makes the archiver
move immediately to the next WAL segment thought as ready to be
archived.
Author: Michael Paquier
Reviewed-by: Nathan Bossart, Andres Freund, Stephen Frost, Kyotaro
Horiguchi
Discussion: https://postgr.es/m/20180928032827.GF1500@paquier.xyz
2018-12-10 07:00:59 +01:00
|
|
|
/*
|
|
|
|
* Maximum number of retries allowed when attempting to remove an
|
|
|
|
* orphan archive status file.
|
|
|
|
*/
|
|
|
|
#define NUM_ORPHAN_CLEANUP_RETRIES 3
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* 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 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
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* Local function forward declarations
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
#ifdef EXEC_BACKEND
|
|
|
|
static pid_t pgarch_forkexec(void);
|
|
|
|
#endif
|
|
|
|
|
2015-03-26 19:03:19 +01:00
|
|
|
NON_EXEC_STATIC void PgArchiverMain(int argc, char *argv[]) pg_attribute_noreturn();
|
2004-07-19 04:47:16 +02:00
|
|
|
static void pgarch_exit(SIGNAL_ARGS);
|
|
|
|
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 ... */
|
2015-01-13 13:12:37 +01:00
|
|
|
InitPostmasterChild();
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/* Close the postmaster's sockets */
|
2004-08-06 01:32:13 +02:00
|
|
|
ClosePostmasterPorts(false);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/* Drop our connection to postmaster's shared memory, as well */
|
2014-03-18 12:58:53 +01:00
|
|
|
dsm_detach_all();
|
2004-07-19 04:47:16 +02:00
|
|
|
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);
|
|
|
|
}
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +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
|
|
|
/*
|
|
|
|
* 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
|
|
|
*/
|
2019-12-17 19:14:28 +01:00
|
|
|
pqsignal(SIGHUP, SignalHandlerForConfigReload);
|
2004-08-29 07:07:03 +02:00
|
|
|
pqsignal(SIGINT, SIG_IGN);
|
2019-12-17 19:14:28 +01:00
|
|
|
pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
|
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);
|
Leave SIGTTIN/SIGTTOU signal handling alone in postmaster child processes.
For reasons lost in the mists of time, most postmaster child processes
reset SIGTTIN/SIGTTOU signal handling to SIG_DFL, with the major exception
that backend sessions do not. It seems like a pretty bad idea for any
postmaster children to do that: if stderr is connected to the terminal,
and the user has put the postmaster in background, any log output would
result in the child process freezing up. Hence, switch them all to
doing what backends do, ie, nothing. This allows them to inherit the
postmaster's SIG_IGN setting. On the other hand, manually-launched
processes such as standalone backends will have default processing,
which seems fine.
In passing, also remove useless resets of SIGCONT and SIGWINCH signal
processing. Perhaps the postmaster once changed those to something
besides SIG_DFL, but it doesn't now, so these are just wasted (and
confusing) syscalls.
Basically, this propagates the changes made in commit 8e2998d8a from
backends to other postmaster children. Probably the only reason these
calls now exist elsewhere is that I missed changing pgstat.c along with
postgres.c at the time.
Given the lack of field complaints that can be traced to this, I don't
presently feel a need to back-patch.
Discussion: https://postgr.es/m/5627.1542477392@sss.pgh.pa.us
2018-11-17 22:23:55 +01:00
|
|
|
/* Reset some signals that are accepted by postmaster but not here */
|
2004-08-29 07:07:03 +02:00
|
|
|
pqsignal(SIGCHLD, SIG_DFL);
|
|
|
|
PG_SETMASK(&UnBlockSig);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identify myself via ps
|
|
|
|
*/
|
2017-09-20 03:15:09 +02:00
|
|
|
init_ps_display("archiver", "", "", "");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2015-01-14 18:45:22 +01:00
|
|
|
SetLatch(MyLatch);
|
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;
|
2015-01-14 18:45:22 +01:00
|
|
|
SetLatch(MyLatch);
|
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
|
|
|
|
{
|
2015-01-14 18:45:22 +01:00
|
|
|
ResetLatch(MyLatch);
|
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
|
|
|
|
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 */
|
2019-12-17 19:03:57 +01:00
|
|
|
if (ConfigReloadPending)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2019-12-17 19:03:57 +01:00
|
|
|
ConfigReloadPending = false;
|
2004-07-19 04:47:16 +02:00
|
|
|
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
|
|
|
*/
|
2019-12-17 19:14:28 +01:00
|
|
|
if (ShutdownRequestPending)
|
2008-01-11 01:54:09 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-01-14 18:45:22 +01:00
|
|
|
rc = WaitLatch(MyLatch,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
|
2016-10-04 16:50:13 +02:00
|
|
|
timeout * 1000L,
|
|
|
|
WAIT_EVENT_ARCHIVER_MAIN);
|
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;
|
Add WL_EXIT_ON_PM_DEATH pseudo-event.
Users of the WaitEventSet and WaitLatch() APIs can now choose between
asking for WL_POSTMASTER_DEATH and then handling it explicitly, or asking
for WL_EXIT_ON_PM_DEATH to trigger immediate exit on postmaster death.
This reduces code duplication, since almost all callers want the latter.
Repair all code that was previously ignoring postmaster death completely,
or requesting the event but ignoring it, or requesting the event but then
doing an unconditional PostmasterIsAlive() call every time through its
event loop (which is an expensive syscall on platforms for which we don't
have USE_POSTMASTER_DEATH_SIGNAL support).
Assert that callers of WaitLatchXXX() under the postmaster remember to
ask for either WL_POSTMASTER_DEATH or WL_EXIT_ON_PM_DEATH, to prevent
future bugs.
The only process that doesn't handle postmaster death is syslogger. It
waits until all backends holding the write end of the syslog pipe
(including the postmaster) have closed it by exiting, to be sure to
capture any parting messages. By using the WaitEventSet API directly
it avoids the new assertion, and as a by-product it may be slightly
more efficient on platforms that have epoll().
Author: Thomas Munro
Reviewed-by: Kyotaro Horiguchi, Heikki Linnakangas, Tom Lane
Discussion: https://postgr.es/m/CAEepm%3D1TCviRykkUb69ppWLr_V697rzd1j3eZsRMmbXvETfqbQ%40mail.gmail.com,
https://postgr.es/m/CAEepm=2LqHzizbe7muD7-2yHUbTOoF7Q+qkSD5Q41kuhttRTwA@mail.gmail.com
2018-11-23 08:16:41 +01:00
|
|
|
if (rc & WL_POSTMASTER_DEATH)
|
|
|
|
time_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
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
Add WL_EXIT_ON_PM_DEATH pseudo-event.
Users of the WaitEventSet and WaitLatch() APIs can now choose between
asking for WL_POSTMASTER_DEATH and then handling it explicitly, or asking
for WL_EXIT_ON_PM_DEATH to trigger immediate exit on postmaster death.
This reduces code duplication, since almost all callers want the latter.
Repair all code that was previously ignoring postmaster death completely,
or requesting the event but ignoring it, or requesting the event but then
doing an unconditional PostmasterIsAlive() call every time through its
event loop (which is an expensive syscall on platforms for which we don't
have USE_POSTMASTER_DEATH_SIGNAL support).
Assert that callers of WaitLatchXXX() under the postmaster remember to
ask for either WL_POSTMASTER_DEATH or WL_EXIT_ON_PM_DEATH, to prevent
future bugs.
The only process that doesn't handle postmaster death is syslogger. It
waits until all backends holding the write end of the syslog pipe
(including the postmaster) have closed it by exiting, to be sure to
capture any parting messages. By using the WaitEventSet API directly
it avoids the new assertion, and as a by-product it may be slightly
more efficient on platforms that have epoll().
Author: Thomas Munro
Reviewed-by: Kyotaro Horiguchi, Heikki Linnakangas, Tom Lane
Discussion: https://postgr.es/m/CAEepm%3D1TCviRykkUb69ppWLr_V697rzd1j3eZsRMmbXvETfqbQ%40mail.gmail.com,
https://postgr.es/m/CAEepm=2LqHzizbe7muD7-2yHUbTOoF7Q+qkSD5Q41kuhttRTwA@mail.gmail.com
2018-11-23 08:16:41 +01:00
|
|
|
} while (!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;
|
Ensure cleanup of orphan archive status files
When a WAL segment is recycled, its ".ready" and ".done" status files
get also automatically removed, however this is not done in a durable
manner. Hence, in a subsequent crash, it could be possible that a
".ready" status file is still around with its corresponding segment
already gone.
If the backend reaches such a state, the archive command would most
likely complain about a segment non-existing and would keep retrying,
causing WAL segments to bloat pg_wal/, potentially making Postgres crash
hard when running out of space.
As status files are removed after each individual segment, using
durable_unlink() does not completely close the window either, as a crash
could happen between the moment the WAL segment is recycled and the
moment its status files are removed. This has also some performance
impact with the additional fsync() calls needed to make the removal in a
durable manner. Doing the cleanup at recovery is not cost-free either
as this makes crash recovery potentially take longer than necessary.
So, instead, as per an idea of Stephen Frost, make the archiver aware of
orphan status files and remove them on-the-fly if the corresponding
segment goes missing. Removal failures follow a model close to what
happens for WAL segments, where multiple attempts are done before giving
up temporarily, and where a successful orphan removal makes the archiver
move immediately to the next WAL segment thought as ready to be
archived.
Author: Michael Paquier
Reviewed-by: Nathan Bossart, Andres Freund, Stephen Frost, Kyotaro
Horiguchi
Discussion: https://postgr.es/m/20180928032827.GF1500@paquier.xyz
2018-12-10 07:00:59 +01:00
|
|
|
int failures_orphan = 0;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
Ensure cleanup of orphan archive status files
When a WAL segment is recycled, its ".ready" and ".done" status files
get also automatically removed, however this is not done in a durable
manner. Hence, in a subsequent crash, it could be possible that a
".ready" status file is still around with its corresponding segment
already gone.
If the backend reaches such a state, the archive command would most
likely complain about a segment non-existing and would keep retrying,
causing WAL segments to bloat pg_wal/, potentially making Postgres crash
hard when running out of space.
As status files are removed after each individual segment, using
durable_unlink() does not completely close the window either, as a crash
could happen between the moment the WAL segment is recycled and the
moment its status files are removed. This has also some performance
impact with the additional fsync() calls needed to make the removal in a
durable manner. Doing the cleanup at recovery is not cost-free either
as this makes crash recovery potentially take longer than necessary.
So, instead, as per an idea of Stephen Frost, make the archiver aware of
orphan status files and remove them on-the-fly if the corresponding
segment goes missing. Removal failures follow a model close to what
happens for WAL segments, where multiple attempts are done before giving
up temporarily, and where a successful orphan removal makes the archiver
move immediately to the next WAL segment thought as ready to be
archived.
Author: Michael Paquier
Reviewed-by: Nathan Bossart, Andres Freund, Stephen Frost, Kyotaro
Horiguchi
Discussion: https://postgr.es/m/20180928032827.GF1500@paquier.xyz
2018-12-10 07:00:59 +01:00
|
|
|
struct stat stat_buf;
|
|
|
|
char pathname[MAXPGPATH];
|
|
|
|
|
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
|
|
|
*/
|
2019-12-17 19:14:28 +01:00
|
|
|
if (ShutdownRequestPending || !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.
|
|
|
|
*/
|
2019-12-17 19:03:57 +01:00
|
|
|
if (ConfigReloadPending)
|
2010-05-11 18:42:28 +02:00
|
|
|
{
|
2019-12-17 19:03:57 +01:00
|
|
|
ConfigReloadPending = false;
|
2010-05-11 18:42:28 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Ensure cleanup of orphan archive status files
When a WAL segment is recycled, its ".ready" and ".done" status files
get also automatically removed, however this is not done in a durable
manner. Hence, in a subsequent crash, it could be possible that a
".ready" status file is still around with its corresponding segment
already gone.
If the backend reaches such a state, the archive command would most
likely complain about a segment non-existing and would keep retrying,
causing WAL segments to bloat pg_wal/, potentially making Postgres crash
hard when running out of space.
As status files are removed after each individual segment, using
durable_unlink() does not completely close the window either, as a crash
could happen between the moment the WAL segment is recycled and the
moment its status files are removed. This has also some performance
impact with the additional fsync() calls needed to make the removal in a
durable manner. Doing the cleanup at recovery is not cost-free either
as this makes crash recovery potentially take longer than necessary.
So, instead, as per an idea of Stephen Frost, make the archiver aware of
orphan status files and remove them on-the-fly if the corresponding
segment goes missing. Removal failures follow a model close to what
happens for WAL segments, where multiple attempts are done before giving
up temporarily, and where a successful orphan removal makes the archiver
move immediately to the next WAL segment thought as ready to be
archived.
Author: Michael Paquier
Reviewed-by: Nathan Bossart, Andres Freund, Stephen Frost, Kyotaro
Horiguchi
Discussion: https://postgr.es/m/20180928032827.GF1500@paquier.xyz
2018-12-10 07:00:59 +01:00
|
|
|
/*
|
|
|
|
* Since archive status files are not removed in a durable manner,
|
|
|
|
* a system crash could leave behind .ready files for WAL segments
|
|
|
|
* that have already been recycled or removed. In this case,
|
|
|
|
* simply remove the orphan status file and move on. unlink() is
|
|
|
|
* used here as even on subsequent crashes the same orphan files
|
|
|
|
* would get removed, so there is no need to worry about
|
|
|
|
* durability.
|
|
|
|
*/
|
|
|
|
snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
|
|
|
|
if (stat(pathname, &stat_buf) != 0 && errno == ENOENT)
|
|
|
|
{
|
|
|
|
char xlogready[MAXPGPATH];
|
|
|
|
|
|
|
|
StatusFilePath(xlogready, xlog, ".ready");
|
|
|
|
if (unlink(xlogready) == 0)
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("removed orphan archive status file \"%s\"",
|
|
|
|
xlogready)));
|
|
|
|
|
|
|
|
/* leave loop and move to the next status file */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++failures_orphan >= NUM_ORPHAN_CLEANUP_RETRIES)
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("removal of orphan archive status file \"%s\" failed too many times, will try again later",
|
|
|
|
xlogready)));
|
|
|
|
|
|
|
|
/* give up cleanup of orphan status files */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait a bit before retrying */
|
|
|
|
pg_usleep(1000000L);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
if (pgarch_archiveXlog(xlog))
|
|
|
|
{
|
|
|
|
/* successful */
|
|
|
|
pgarch_archiveDone(xlog);
|
2014-01-28 18:58:22 +01:00
|
|
|
|
2014-05-06 18:12:18 +02:00
|
|
|
/*
|
|
|
|
* Tell the collector about the WAL file that we successfully
|
|
|
|
* archived
|
|
|
|
*/
|
2014-01-28 18:58:22 +01:00
|
|
|
pgstat_send_archiver(xlog, false);
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
break; /* out of inner retry loop */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-06 18:12:18 +02:00
|
|
|
/*
|
|
|
|
* Tell the collector about the WAL file that we failed to
|
|
|
|
* archive
|
|
|
|
*/
|
2014-01-28 18:58:22 +01:00
|
|
|
pgstat_send_archiver(xlog, true);
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
if (++failures >= NUM_ARCHIVE_RETRIES)
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
2017-05-12 17:49:56 +02:00
|
|
|
(errmsg("archiving write-ahead log file \"%s\" failed too many times, will try again later",
|
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,
|
2014-05-06 18:12:18 +02: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
|
Improve detection of child-process SIGPIPE failures.
Commit ffa4cbd62 added logic to detect SIGPIPE failure of a COPY child
process, but it only worked correctly if the SIGPIPE occurred in the
immediate child process. Depending on the shell in use and the
complexity of the shell command string, we might instead get back
an exit code of 128 + SIGPIPE, representing a shell error exit
reporting SIGPIPE in the child process.
We could just hack up ClosePipeToProgram() to add the extra case,
but it seems like this is a fairly general issue deserving a more
general and better-documented solution. I chose to add a couple
of functions in src/common/wait_error.c, which is a natural place
to know about wait-result encodings, that will test for either a
specific child-process signal type or any child-process signal failure.
Then, adjust other places that were doing ad-hoc tests of this type
to use the common functions.
In RestoreArchivedFile, this fixes a race condition affecting whether
the process will report an error or just silently proc_exit(1): before,
that depended on whether the intermediate shell got SIGTERM'd itself
or reported a child process failing on SIGTERM.
Like the previous patch, back-patch to v10; we could go further
but there seems no real need to.
Per report from Erik Rijkers.
Discussion: https://postgr.es/m/f3683f87ab1701bea5d86a7742b22432@xs4all.nl
2018-12-16 20:32:14 +01:00
|
|
|
* should have interrupted us too. Also die if the shell got a hard
|
|
|
|
* "command not found" type of error. If we overreact it's no big
|
|
|
|
* deal, the postmaster will just start the archiver again.
|
2006-11-21 21:59:53 +01:00
|
|
|
*/
|
Improve detection of child-process SIGPIPE failures.
Commit ffa4cbd62 added logic to detect SIGPIPE failure of a COPY child
process, but it only worked correctly if the SIGPIPE occurred in the
immediate child process. Depending on the shell in use and the
complexity of the shell command string, we might instead get back
an exit code of 128 + SIGPIPE, representing a shell error exit
reporting SIGPIPE in the child process.
We could just hack up ClosePipeToProgram() to add the extra case,
but it seems like this is a fairly general issue deserving a more
general and better-documented solution. I chose to add a couple
of functions in src/common/wait_error.c, which is a natural place
to know about wait-result encodings, that will test for either a
specific child-process signal type or any child-process signal failure.
Then, adjust other places that were doing ad-hoc tests of this type
to use the common functions.
In RestoreArchivedFile, this fixes a race condition affecting whether
the process will report an error or just silently proc_exit(1): before,
that depended on whether the intermediate shell got SIGTERM'd itself
or reported a child process failing on SIGTERM.
Like the previous patch, back-patch to v10; we could go further
but there seems no real need to.
Per report from Erik Rijkers.
Discussion: https://postgr.es/m/f3683f87ab1701bea5d86a7742b22432@xs4all.nl
2018-12-16 20:32:14 +01:00
|
|
|
int lev = wait_result_is_any_signal(rc, true) ? 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,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +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
|
|
|
#else
|
2007-12-12 17:53:14 +01:00
|
|
|
ereport(lev,
|
Modernize our code for looking up descriptive strings for Unix signals.
At least as far back as the 2008 spec, POSIX has defined strsignal(3)
for looking up descriptive strings for signal numbers. We hadn't gotten
the word though, and were still using the crufty old sys_siglist array,
which is in no standard even though most Unixen provide it.
Aside from not being formally standards-compliant, this was just plain
ugly because it involved #ifdef's at every place using the code.
To eliminate the #ifdef's, create a portability function pg_strsignal,
which wraps strsignal(3) if available and otherwise falls back to
sys_siglist[] if available. The set of Unixen with neither API is
probably empty these days, but on any platform with neither, you'll
just get "unrecognized signal". All extant callers print the numeric
signal number too, so no need to work harder than that.
Along the way, upgrade pg_basebackup's child-error-exit reporting
to match the rest of the system.
Discussion: https://postgr.es/m/25758.1544983503@sss.pgh.pa.us
2018-12-17 01:38:57 +01:00
|
|
|
(errmsg("archive command was terminated by signal %d: %s",
|
|
|
|
WTERMSIG(rc), pg_strsignal(WTERMSIG(rc))),
|
2007-12-12 17:53:14 +01:00
|
|
|
errdetail("The failed archive command was: %s",
|
|
|
|
xlogarchcmd)));
|
2007-12-12 10:39:54 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(lev,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +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;
|
|
|
|
}
|
2017-05-12 17:49:56 +02:00
|
|
|
elog(DEBUG1, "archived write-ahead 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
|
|
|
*
|
2018-12-24 12:24:16 +01:00
|
|
|
* NOTE: the "oldest" comparison will consider any .history file to be older
|
|
|
|
* than any other file except another .history file. Segments on a timeline
|
|
|
|
* with a smaller ID will be older than all segments on 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
|
|
|
DIR *rldir;
|
|
|
|
struct dirent *rlde;
|
|
|
|
bool found = false;
|
2018-12-24 12:24:16 +01:00
|
|
|
bool historyFound = 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);
|
|
|
|
|
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;
|
2018-12-24 12:24:16 +01:00
|
|
|
char basename[MAX_XFN_CHARS + 1];
|
|
|
|
bool ishistory;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2018-12-24 12:24:16 +01:00
|
|
|
/* Ignore entries with unexpected number of characters */
|
|
|
|
if (basenamelen < MIN_XFN_CHARS ||
|
|
|
|
basenamelen > MAX_XFN_CHARS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Ignore entries with unexpected characters */
|
|
|
|
if (strspn(rlde->d_name, VALID_XFN_CHARS) < basenamelen)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Ignore anything not suffixed with .ready */
|
|
|
|
if (strcmp(rlde->d_name + basenamelen, ".ready") != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Truncate off the .ready */
|
|
|
|
memcpy(basename, rlde->d_name, basenamelen);
|
|
|
|
basename[basenamelen] = '\0';
|
|
|
|
|
|
|
|
/* Is this a history file? */
|
|
|
|
ishistory = IsTLHistoryFileName(basename);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Consume the file to archive. History files have the highest
|
|
|
|
* priority. If this is the first file or the first history file
|
|
|
|
* ever, copy it. In the presence of a history file already chosen as
|
|
|
|
* target, ignore all other files except history files which have been
|
|
|
|
* generated for an older timeline than what is already chosen as
|
|
|
|
* target to archive.
|
|
|
|
*/
|
|
|
|
if (!found || (ishistory && !historyFound))
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2018-12-24 12:24:16 +01:00
|
|
|
strcpy(xlog, basename);
|
|
|
|
found = true;
|
|
|
|
historyFound = ishistory;
|
|
|
|
}
|
|
|
|
else if (ishistory || !historyFound)
|
|
|
|
{
|
|
|
|
if (strcmp(basename, xlog) < 0)
|
|
|
|
strcpy(xlog, basename);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
FreeDir(rldir);
|
|
|
|
|
|
|
|
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");
|
2016-03-10 03:53:53 +01:00
|
|
|
(void) durable_rename(rlogready, rlogdone, WARNING);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|