2004-07-19 04:47:16 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pgarch.c
|
|
|
|
*
|
|
|
|
* PostgreSQL WAL archiver
|
|
|
|
*
|
|
|
|
* All functions relating to archiver are included here
|
|
|
|
*
|
|
|
|
* - All functions executed by archiver process
|
|
|
|
*
|
|
|
|
* - archiver is forked from postmaster, and the two
|
|
|
|
* processes then communicate using signals. All functions
|
|
|
|
* executed by postmaster are included in this file.
|
|
|
|
*
|
|
|
|
* Initial author: Simon Riggs simon@2ndquadrant.com
|
|
|
|
*
|
2021-01-02 19:06:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2021, 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"
|
2021-11-11 21:02:53 +01:00
|
|
|
#include "lib/binaryheap.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"
|
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 "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/pmsignal.h"
|
2021-04-03 05:01:14 +02:00
|
|
|
#include "storage/proc.h"
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
#include "storage/procsignal.h"
|
2021-04-03 05:01:14 +02:00
|
|
|
#include "storage/shmem.h"
|
2021-11-11 21:02:53 +01:00
|
|
|
#include "storage/spin.h"
|
2004-07-19 04:47:16 +02:00
|
|
|
#include "utils/guc.h"
|
|
|
|
#include "utils/ps_status.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* Timer definitions.
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
#define PGARCH_AUTOWAKE_INTERVAL 60 /* How often to force a poll of the
|
|
|
|
* archive status directory; in seconds. */
|
2004-12-02 23:40:18 +01:00
|
|
|
#define PGARCH_RESTART_INTERVAL 10 /* How often to attempt to restart a
|
2004-07-19 04:47:16 +02:00
|
|
|
* failed archiver; in seconds. */
|
|
|
|
|
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
|
|
|
|
|
2021-11-11 21:02:53 +01:00
|
|
|
/*
|
|
|
|
* Maximum number of .ready files to gather per directory scan.
|
|
|
|
*/
|
|
|
|
#define NUM_FILES_PER_DIRECTORY_SCAN 64
|
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
/* Shared memory area for archiver process */
|
|
|
|
typedef struct PgArchData
|
|
|
|
{
|
|
|
|
int pgprocno; /* pgprocno of archiver process */
|
2021-11-11 21:02:53 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Forces a directory scan in pgarch_readyXlog(). Protected by
|
|
|
|
* arch_lck.
|
|
|
|
*/
|
|
|
|
bool force_dir_scan;
|
|
|
|
|
|
|
|
slock_t arch_lck;
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
} PgArchData;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* Local data
|
|
|
|
* ----------
|
|
|
|
*/
|
2008-01-11 01:54:09 +01:00
|
|
|
static time_t last_sigterm_time = 0;
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
static PgArchData *PgArch = NULL;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2021-11-11 21:02:53 +01:00
|
|
|
/*
|
|
|
|
* Stuff for tracking multiple files to archive from each scan of
|
|
|
|
* archive_status. Minimizing the number of directory scans when there are
|
|
|
|
* many files to archive can significantly improve archival rate.
|
|
|
|
*
|
|
|
|
* arch_heap is a max-heap that is used during the directory scan to track
|
|
|
|
* the highest-priority files to archive. After the directory scan
|
|
|
|
* completes, the file names are stored in ascending order of priority in
|
|
|
|
* arch_files. pgarch_readyXlog() returns files from arch_files until it
|
|
|
|
* is empty, at which point another directory scan must be performed.
|
2021-12-29 23:02:50 +01:00
|
|
|
*
|
|
|
|
* We only need this data in the archiver process, so make it a palloc'd
|
|
|
|
* struct rather than a bunch of static arrays.
|
2021-11-11 21:02:53 +01:00
|
|
|
*/
|
2021-12-29 23:02:50 +01:00
|
|
|
struct arch_files_state
|
|
|
|
{
|
|
|
|
binaryheap *arch_heap;
|
|
|
|
int arch_files_size; /* number of live entries in arch_files[] */
|
|
|
|
char *arch_files[NUM_FILES_PER_DIRECTORY_SCAN];
|
|
|
|
/* buffers underlying heap, and later arch_files[], entries: */
|
|
|
|
char arch_filenames[NUM_FILES_PER_DIRECTORY_SCAN][MAX_XFN_CHARS + 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct arch_files_state *arch_files = NULL;
|
2021-11-11 21:02:53 +01:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* Flags set by interrupt handlers for later service in the main loop.
|
|
|
|
*/
|
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
|
|
|
|
* ----------
|
|
|
|
*/
|
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);
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
static void pgarch_die(int code, Datum arg);
|
2021-06-18 10:57:09 +02:00
|
|
|
static void HandlePgArchInterrupts(void);
|
2021-11-11 21:02:53 +01:00
|
|
|
static int ready_file_comparator(Datum a, Datum b, void *arg);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
/* Report shared memory space needed by PgArchShmemInit */
|
|
|
|
Size
|
|
|
|
PgArchShmemSize(void)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
Size size = 0;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
size = add_size(size, sizeof(PgArchData));
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
return size;
|
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
/* Allocate and initialize archiver-related shared memory */
|
|
|
|
void
|
|
|
|
PgArchShmemInit(void)
|
|
|
|
{
|
|
|
|
bool found;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
PgArch = (PgArchData *)
|
|
|
|
ShmemInitStruct("Archiver Data", PgArchShmemSize(), &found);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
/* First time through, so initialize */
|
|
|
|
MemSet(PgArch, 0, PgArchShmemSize());
|
|
|
|
PgArch->pgprocno = INVALID_PGPROCNO;
|
2021-11-11 21:02:53 +01:00
|
|
|
SpinLockInit(&PgArch->arch_lck);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
* PgArchCanRestart
|
|
|
|
*
|
|
|
|
* Return true and archiver is allowed to restart if enough time has
|
|
|
|
* passed since it was launched last to reach PGARCH_RESTART_INTERVAL.
|
|
|
|
* Otherwise return false.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
* This is a safety valve to protect against continuous respawn attempts if the
|
|
|
|
* archiver is dying immediately at launch. Note that since we will retry to
|
|
|
|
* launch the archiver from the postmaster main loop, we will get another
|
|
|
|
* chance later.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
bool
|
|
|
|
PgArchCanRestart(void)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
static time_t last_pgarch_start_time = 0;
|
|
|
|
time_t curtime = time(NULL);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
/*
|
|
|
|
* Return false and don't restart archiver if too soon since last archiver
|
|
|
|
* start.
|
|
|
|
*/
|
|
|
|
if ((unsigned int) (curtime - last_pgarch_start_time) <
|
|
|
|
(unsigned int) PGARCH_RESTART_INTERVAL)
|
|
|
|
return false;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
last_pgarch_start_time = curtime;
|
|
|
|
return true;
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
/* Main entry point for archiver process */
|
|
|
|
void
|
|
|
|
PgArchiverMain(void)
|
2004-07-19 04:47:16 +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-07-19 04:47:16 +02:00
|
|
|
*/
|
2019-12-17 19:14:28 +01:00
|
|
|
pqsignal(SIGHUP, SignalHandlerForConfigReload);
|
2004-07-19 04:47:16 +02:00
|
|
|
pqsignal(SIGINT, SIG_IGN);
|
2019-12-17 19:14:28 +01:00
|
|
|
pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
|
Centralize setup of SIGQUIT handling for postmaster child processes.
We decided that the policy established in commit 7634bd4f6 for
the bgwriter, checkpointer, walwriter, and walreceiver processes,
namely that they should accept SIGQUIT at all times, really ought
to apply uniformly to all postmaster children. Therefore, get
rid of the duplicative and inconsistent per-process code for
establishing that signal handler and removing SIGQUIT from BlockSig.
Instead, make InitPostmasterChild do it.
The handler set up by InitPostmasterChild is SignalHandlerForCrashExit,
which just summarily does _exit(2). In interactive backends, we
almost immediately replace that with quickdie, since we would prefer
to try to tell the client that we're dying. However, this patch is
changing the behavior of autovacuum (both launcher and workers), as
well as walsenders. Those processes formerly also used quickdie,
but AFAICS that was just mindless copy-and-paste: they don't have
any interactive client that's likely to benefit from being told this.
The stats collector continues to be an outlier, in that it thinks
SIGQUIT means normal exit. That should probably be changed for
consistency, but there's another patch set where that's being
dealt with, so I didn't do so here.
Discussion: https://postgr.es/m/644875.1599933441@sss.pgh.pa.us
2020-09-16 22:04:36 +02:00
|
|
|
/* SIGQUIT handler was already set up by InitPostmasterChild */
|
2004-07-19 04:47:16 +02:00
|
|
|
pqsignal(SIGALRM, SIG_IGN);
|
|
|
|
pqsignal(SIGPIPE, SIG_IGN);
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
pqsignal(SIGUSR1, procsignal_sigusr1_handler);
|
2008-01-11 01:54:09 +01:00
|
|
|
pqsignal(SIGUSR2, pgarch_waken_stop);
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
|
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-07-19 04:47:16 +02:00
|
|
|
pqsignal(SIGCHLD, SIG_DFL);
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
|
|
|
|
/* Unblock signals (they were blocked when the postmaster forked us) */
|
2004-07-19 04:47:16 +02:00
|
|
|
PG_SETMASK(&UnBlockSig);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
/* We shouldn't be launched unnecessarily. */
|
|
|
|
Assert(XLogArchivingActive());
|
|
|
|
|
|
|
|
/* Arrange to clean up at archiver exit */
|
|
|
|
on_shmem_exit(pgarch_die, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Advertise our pgprocno so that backends can use our latch to wake us up
|
|
|
|
* while we're sleeping.
|
|
|
|
*/
|
|
|
|
PgArch->pgprocno = MyProc->pgprocno;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2021-12-29 23:02:50 +01:00
|
|
|
/* Create workspace for pgarch_readyXlog() */
|
|
|
|
arch_files = palloc(sizeof(struct arch_files_state));
|
|
|
|
arch_files->arch_files_size = 0;
|
|
|
|
|
2021-11-11 21:02:53 +01:00
|
|
|
/* Initialize our max-heap for prioritizing files to archive. */
|
2021-12-29 23:02:50 +01:00
|
|
|
arch_files->arch_heap = binaryheap_allocate(NUM_FILES_PER_DIRECTORY_SCAN,
|
|
|
|
ready_file_comparator, NULL);
|
2021-11-11 21:02:53 +01:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
pgarch_MainLoop();
|
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
proc_exit(0);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
/*
|
|
|
|
* Wake up the archiver
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PgArchWakeup(void)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
int arch_pgprocno = PgArch->pgprocno;
|
2011-08-10 18:20:30 +02:00
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
/*
|
|
|
|
* We don't acquire ProcArrayLock here. It's actually fine because
|
|
|
|
* procLatch isn't ever freed, so we just can potentially set the wrong
|
|
|
|
* process' (or no process') latch. Even in that case the archiver will
|
|
|
|
* be relaunched shortly and will start archiving.
|
|
|
|
*/
|
|
|
|
if (arch_pgprocno != INVALID_PGPROCNO)
|
|
|
|
SetLatch(&ProcGlobal->allProcs[arch_pgprocno].procLatch);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01: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
|
|
|
|
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
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2004-07-19 04:47:16 +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;
|
|
|
|
|
2021-06-18 10:57:09 +02:00
|
|
|
/* Check for barrier events and config update */
|
|
|
|
HandlePgArchInterrupts();
|
2004-07-19 04:47:16 +02:00
|
|
|
|
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
|
|
|
|
* idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
|
|
|
|
* that the postmaster can start a new archiver if needed.
|
|
|
|
*/
|
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 */
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
pgarch_ArchiverCopyLoop();
|
|
|
|
last_copy_time = time(NULL);
|
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
|
|
|
* 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
|
|
|
*/
|
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 (!time_to_stop) /* Don't wait during last iteration */
|
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
|
|
|
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)
|
|
|
|
{
|
2011-08-10 00:52:29 +02:00
|
|
|
int rc;
|
|
|
|
|
2015-01-14 18:45:22 +01:00
|
|
|
rc = WaitLatch(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
|
|
|
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
|
2016-10-04 16:50:13 +02:00
|
|
|
timeout * 1000L,
|
|
|
|
WAIT_EVENT_ARCHIVER_MAIN);
|
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
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
char xlog[MAX_XFN_CHARS + 1];
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2021-11-11 21:02:53 +01:00
|
|
|
/* force directory scan in the first call to pgarch_readyXlog() */
|
2021-12-29 23:02:50 +01:00
|
|
|
arch_files->arch_files_size = 0;
|
2021-11-11 21:02:53 +01:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* loop through all xlogs with archive_status of .ready and archive
|
|
|
|
* 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
|
|
|
|
*/
|
2006-05-30 19:08:14 +02:00
|
|
|
while (pgarch_readyXlog(xlog))
|
2004-07-19 04:47:16 +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
|
|
|
|
* 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.
|
|
|
|
*/
|
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
|
|
|
/*
|
2021-06-18 10:57:09 +02:00
|
|
|
* Check for barrier events and 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.
|
2010-05-11 18:42:28 +02:00
|
|
|
*/
|
2021-06-18 10:57:09 +02:00
|
|
|
HandlePgArchInterrupts();
|
2010-05-11 18:42:28 +02:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell the collector about the WAL file that we successfully
|
|
|
|
* archived
|
|
|
|
*/
|
|
|
|
pgstat_send_archiver(xlog, false);
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
break; /* out of inner retry loop */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-28 18:58:22 +01:00
|
|
|
/*
|
|
|
|
* Tell the collector about the WAL file that we failed to
|
|
|
|
* archive
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char xlogarchcmd[MAXPGPATH];
|
|
|
|
char pathname[MAXPGPATH];
|
2007-12-18 01:49:34 +01:00
|
|
|
char activitymsg[MAXFNAMELEN + 16];
|
2004-07-19 04:47:16 +02:00
|
|
|
char *dp;
|
|
|
|
char *endp;
|
|
|
|
const char *sp;
|
|
|
|
int rc;
|
|
|
|
|
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,
|
|
|
|
(errmsg_internal("executing archive command \"%s\"",
|
|
|
|
xlogarchcmd)));
|
2007-12-18 01:49:34 +01:00
|
|
|
|
|
|
|
/* Report archive activity in PS display */
|
|
|
|
snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
|
2020-03-11 16:36:40 +01:00
|
|
|
set_ps_display(activitymsg);
|
2007-12-18 01:49:34 +01:00
|
|
|
|
2021-11-22 02:28:21 +01:00
|
|
|
pgstat_report_wait_start(WAIT_EVENT_ARCHIVE_COMMAND);
|
2004-07-19 04:47:16 +02:00
|
|
|
rc = system(xlogarchcmd);
|
2021-11-22 02:28:21 +01:00
|
|
|
pgstat_report_wait_end();
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
if (rc != 0)
|
|
|
|
{
|
2006-11-21 21:59:53 +01:00
|
|
|
/*
|
|
|
|
* If either the shell itself, or a called command, died on a signal,
|
|
|
|
* abort the archiver. We do this because system() ignores SIGINT and
|
|
|
|
* 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,
|
|
|
|
(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,
|
2007-12-12 17:53:14 +01: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);
|
2020-03-11 16:36:40 +01:00
|
|
|
set_ps_display(activitymsg);
|
2007-12-18 01:49:34 +01:00
|
|
|
|
2004-07-19 04:47:16 +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);
|
2020-03-11 16:36:40 +01:00
|
|
|
set_ps_display(activitymsg);
|
2007-12-18 01:49:34 +01:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
char XLogArchiveStatusDir[MAXPGPATH];
|
2004-07-19 04:47:16 +02:00
|
|
|
DIR *rldir;
|
|
|
|
struct dirent *rlde;
|
2021-11-11 21:02:53 +01:00
|
|
|
bool force_dir_scan;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2021-11-11 21:02:53 +01:00
|
|
|
/*
|
|
|
|
* If a directory scan was requested, clear the stored file names and
|
|
|
|
* proceed.
|
|
|
|
*/
|
|
|
|
SpinLockAcquire(&PgArch->arch_lck);
|
|
|
|
force_dir_scan = PgArch->force_dir_scan;
|
|
|
|
PgArch->force_dir_scan = false;
|
|
|
|
SpinLockRelease(&PgArch->arch_lck);
|
|
|
|
|
|
|
|
if (force_dir_scan)
|
2021-12-29 23:02:50 +01:00
|
|
|
arch_files->arch_files_size = 0;
|
2021-11-11 21:02:53 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we still have stored file names from the previous directory scan,
|
|
|
|
* try to return one of those. We check to make sure the status file
|
|
|
|
* is still present, as the archive_command for a previous file may
|
|
|
|
* have already marked it done.
|
|
|
|
*/
|
2021-12-29 23:02:50 +01:00
|
|
|
while (arch_files->arch_files_size > 0)
|
2021-11-11 21:02:53 +01:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char status_file[MAXPGPATH];
|
|
|
|
char *arch_file;
|
|
|
|
|
2021-12-29 23:02:50 +01:00
|
|
|
arch_files->arch_files_size--;
|
|
|
|
arch_file = arch_files->arch_files[arch_files->arch_files_size];
|
2021-11-11 21:02:53 +01:00
|
|
|
StatusFilePath(status_file, arch_file, ".ready");
|
|
|
|
|
|
|
|
if (stat(status_file, &st) == 0)
|
|
|
|
{
|
|
|
|
strcpy(xlog, arch_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (errno != ENOENT)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not stat file \"%s\": %m", status_file)));
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:02:50 +01:00
|
|
|
/* arch_heap is probably empty, but let's make sure */
|
|
|
|
binaryheap_reset(arch_files->arch_heap);
|
|
|
|
|
2021-11-11 21:02:53 +01:00
|
|
|
/*
|
|
|
|
* Open the archive status directory and read through the list of files
|
|
|
|
* with the .ready suffix, looking for the earliest files.
|
|
|
|
*/
|
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
|
|
|
{
|
|
|
|
int basenamelen = (int) strlen(rlde->d_name) - 6;
|
2018-12-24 12:24:16 +01:00
|
|
|
char basename[MAX_XFN_CHARS + 1];
|
2021-11-11 21:02:53 +01:00
|
|
|
char *arch_file;
|
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';
|
|
|
|
|
|
|
|
/*
|
2021-11-11 21:02:53 +01:00
|
|
|
* Store the file in our max-heap if it has a high enough priority.
|
2018-12-24 12:24:16 +01:00
|
|
|
*/
|
2021-12-29 23:02:50 +01:00
|
|
|
if (arch_files->arch_heap->bh_size < NUM_FILES_PER_DIRECTORY_SCAN)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2021-11-11 21:02:53 +01:00
|
|
|
/* If the heap isn't full yet, quickly add it. */
|
2021-12-29 23:02:50 +01:00
|
|
|
arch_file = arch_files->arch_filenames[arch_files->arch_heap->bh_size];
|
2021-11-11 21:02:53 +01:00
|
|
|
strcpy(arch_file, basename);
|
2021-12-29 23:02:50 +01:00
|
|
|
binaryheap_add_unordered(arch_files->arch_heap, CStringGetDatum(arch_file));
|
2021-11-11 21:02:53 +01:00
|
|
|
|
|
|
|
/* If we just filled the heap, make it a valid one. */
|
2021-12-29 23:02:50 +01:00
|
|
|
if (arch_files->arch_heap->bh_size == NUM_FILES_PER_DIRECTORY_SCAN)
|
|
|
|
binaryheap_build(arch_files->arch_heap);
|
2018-12-24 12:24:16 +01:00
|
|
|
}
|
2021-12-29 23:02:50 +01:00
|
|
|
else if (ready_file_comparator(binaryheap_first(arch_files->arch_heap),
|
2021-11-11 21:02:53 +01:00
|
|
|
CStringGetDatum(basename), NULL) > 0)
|
2018-12-24 12:24:16 +01:00
|
|
|
{
|
2021-11-11 21:02:53 +01:00
|
|
|
/*
|
|
|
|
* Remove the lowest priority file and add the current one to
|
|
|
|
* the heap.
|
|
|
|
*/
|
2021-12-29 23:02:50 +01:00
|
|
|
arch_file = DatumGetCString(binaryheap_remove_first(arch_files->arch_heap));
|
2021-11-11 21:02:53 +01:00
|
|
|
strcpy(arch_file, basename);
|
2021-12-29 23:02:50 +01:00
|
|
|
binaryheap_add(arch_files->arch_heap, CStringGetDatum(arch_file));
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
FreeDir(rldir);
|
|
|
|
|
2021-11-11 21:02:53 +01:00
|
|
|
/* If no files were found, simply return. */
|
2021-12-29 23:02:50 +01:00
|
|
|
if (arch_files->arch_heap->bh_size == 0)
|
2021-11-11 21:02:53 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we didn't fill the heap, we didn't make it a valid one. Do that
|
|
|
|
* now.
|
|
|
|
*/
|
2021-12-29 23:02:50 +01:00
|
|
|
if (arch_files->arch_heap->bh_size < NUM_FILES_PER_DIRECTORY_SCAN)
|
|
|
|
binaryheap_build(arch_files->arch_heap);
|
2021-11-11 21:02:53 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill arch_files array with the files to archive in ascending order
|
|
|
|
* of priority.
|
|
|
|
*/
|
2021-12-29 23:02:50 +01:00
|
|
|
arch_files->arch_files_size = arch_files->arch_heap->bh_size;
|
|
|
|
for (int i = 0; i < arch_files->arch_files_size; i++)
|
|
|
|
arch_files->arch_files[i] = DatumGetCString(binaryheap_remove_first(arch_files->arch_heap));
|
2021-11-11 21:02:53 +01:00
|
|
|
|
|
|
|
/* Return the highest priority file. */
|
2021-12-29 23:02:50 +01:00
|
|
|
arch_files->arch_files_size--;
|
|
|
|
strcpy(xlog, arch_files->arch_files[arch_files->arch_files_size]);
|
2021-11-11 21:02:53 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ready_file_comparator
|
|
|
|
*
|
|
|
|
* Compares the archival priority of the given files to archive. If "a"
|
|
|
|
* has a higher priority than "b", a negative value will be returned. If
|
|
|
|
* "b" has a higher priority than "a", a positive value will be returned.
|
|
|
|
* If "a" and "b" have equivalent values, 0 will be returned.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ready_file_comparator(Datum a, Datum b, void *arg)
|
|
|
|
{
|
|
|
|
char *a_str = DatumGetCString(a);
|
|
|
|
char *b_str = DatumGetCString(b);
|
|
|
|
bool a_history = IsTLHistoryFileName(a_str);
|
|
|
|
bool b_history = IsTLHistoryFileName(b_str);
|
|
|
|
|
|
|
|
/* Timeline history files always have the highest priority. */
|
|
|
|
if (a_history != b_history)
|
|
|
|
return a_history ? -1 : 1;
|
|
|
|
|
|
|
|
/* Priority is given to older files. */
|
|
|
|
return strcmp(a_str, b_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PgArchForceDirScan
|
|
|
|
*
|
|
|
|
* When called, the next call to pgarch_readyXlog() will perform a
|
|
|
|
* directory scan. This is useful for ensuring that important files such
|
|
|
|
* as timeline history files are archived as quickly as possible.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PgArchForceDirScan(void)
|
|
|
|
{
|
|
|
|
SpinLockAcquire(&PgArch->arch_lck);
|
|
|
|
PgArch->force_dir_scan = true;
|
|
|
|
SpinLockRelease(&PgArch->arch_lck);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
char rlogready[MAXPGPATH];
|
|
|
|
char rlogdone[MAXPGPATH];
|
|
|
|
|
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
|
|
|
}
|
Make archiver process an auxiliary process.
This commit changes WAL archiver process so that it's treated as
an auxiliary process and can use shared memory. This is an infrastructure
patch required for upcoming shared-memory based stats collector patch
series. These patch series basically need any processes including archiver
that can report the statistics to access to shared memory. Since this patch
itself is useful to simplify the code and when users monitor the status of
archiver, it's committed separately in advance.
This commit simplifies the code for WAL archiving. For example, previously
backends need to signal to archiver via postmaster when they notify
archiver that there are some WAL files to archive. On the other hand,
this commit removes that signal to postmaster and enables backends to
notify archier directly using shared latch.
Also, as the side of this change, the information about archiver process
becomes viewable at pg_stat_activity view.
Author: Kyotaro Horiguchi
Reviewed-by: Andres Freund, Álvaro Herrera, Julien Rouhaud, Tomas Vondra, Arthur Zakirov, Fujii Masao
Discussion: https://postgr.es/m/20180629.173418.190173462.horiguchi.kyotaro@lab.ntt.co.jp
2021-03-15 05:13:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgarch_die
|
|
|
|
*
|
|
|
|
* Exit-time cleanup handler
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pgarch_die(int code, Datum arg)
|
|
|
|
{
|
|
|
|
PgArch->pgprocno = INVALID_PGPROCNO;
|
|
|
|
}
|
2021-06-18 10:57:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Interrupt handler for WAL archiver process.
|
|
|
|
*
|
|
|
|
* This is called in the loops pgarch_MainLoop and pgarch_ArchiverCopyLoop.
|
|
|
|
* It checks for barrier events and config update, but not shutdown request
|
|
|
|
* because how to handle shutdown request is different between those loops.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
HandlePgArchInterrupts(void)
|
|
|
|
{
|
|
|
|
if (ProcSignalBarrierPending)
|
|
|
|
ProcessProcSignalBarrier();
|
|
|
|
|
|
|
|
if (ConfigReloadPending)
|
|
|
|
{
|
|
|
|
ConfigReloadPending = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
|
|
|
}
|
|
|
|
}
|