PostgreSQL Source Code  git master
pgarch.c File Reference
#include "postgres.h"
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "lib/binaryheap.h"
#include "libpq/pqsignal.h"
#include "pgstat.h"
#include "postmaster/interrupt.h"
#include "postmaster/pgarch.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/shmem.h"
#include "storage/spin.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
Include dependency graph for pgarch.c:

Go to the source code of this file.

Data Structures

struct  PgArchData
 
struct  arch_files_state
 

Macros

#define PGARCH_AUTOWAKE_INTERVAL
 
#define PGARCH_RESTART_INTERVAL
 
#define NUM_ARCHIVE_RETRIES   3
 
#define NUM_ORPHAN_CLEANUP_RETRIES   3
 
#define NUM_FILES_PER_DIRECTORY_SCAN   64
 

Typedefs

typedef struct PgArchData PgArchData
 

Functions

static void pgarch_waken_stop (SIGNAL_ARGS)
 
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)
 
static void pgarch_die (int code, Datum arg)
 
static void HandlePgArchInterrupts (void)
 
static int ready_file_comparator (Datum a, Datum b, void *arg)
 
static void LoadArchiveLibrary (void)
 
static void pgarch_call_module_shutdown_cb (int code, Datum arg)
 
Size PgArchShmemSize (void)
 
void PgArchShmemInit (void)
 
bool PgArchCanRestart (void)
 
void PgArchiverMain (void)
 
void PgArchWakeup (void)
 
void PgArchForceDirScan (void)
 

Variables

char * XLogArchiveLibrary = ""
 
static time_t last_sigterm_time = 0
 
static PgArchDataPgArch = NULL
 
static ArchiveModuleCallbacks ArchiveContext
 
static struct arch_files_statearch_files = NULL
 
static volatile sig_atomic_t ready_to_stop = false
 

Macro Definition Documentation

◆ NUM_ARCHIVE_RETRIES

#define NUM_ARCHIVE_RETRIES   3

Definition at line 63 of file pgarch.c.

◆ NUM_FILES_PER_DIRECTORY_SCAN

#define NUM_FILES_PER_DIRECTORY_SCAN   64

Definition at line 74 of file pgarch.c.

◆ NUM_ORPHAN_CLEANUP_RETRIES

#define NUM_ORPHAN_CLEANUP_RETRIES   3

Definition at line 69 of file pgarch.c.

◆ PGARCH_AUTOWAKE_INTERVAL

#define PGARCH_AUTOWAKE_INTERVAL
Value:
60 /* How often to force a poll of the
* archive status directory; in seconds. */

Definition at line 56 of file pgarch.c.

◆ PGARCH_RESTART_INTERVAL

#define PGARCH_RESTART_INTERVAL
Value:
10 /* How often to attempt to restart a
* failed archiver; in seconds. */

Definition at line 57 of file pgarch.c.

Typedef Documentation

◆ PgArchData

typedef struct PgArchData PgArchData

Function Documentation

◆ HandlePgArchInterrupts()

static void HandlePgArchInterrupts ( void  )
static

Definition at line 776 of file pgarch.c.

779 {
782 
783  /* Perform logging of memory contexts of this process */
786 
788  {
789  char *archiveLib = pstrdup(XLogArchiveLibrary);
790  bool archiveLibChanged;
791 
792  ConfigReloadPending = false;
794 
795  if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
796  ereport(ERROR,
797  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
798  errmsg("both archive_command and archive_library set"),
799  errdetail("Only one of archive_command, archive_library may be set.")));
800 
801  archiveLibChanged = strcmp(XLogArchiveLibrary, archiveLib) != 0;
802  pfree(archiveLib);
803 
804  if (archiveLibChanged)
805  {
806  /*
807  * Ideally, we would simply unload the previous archive module and
808  * load the new one, but there is presently no mechanism for
809  * unloading a library (see the comment above
810  * internal_load_library()). To deal with this, we simply restart
811  * the archiver. The new archive module will be loaded when the
812  * new archiver process starts up. Note that this triggers the
813  * module's shutdown callback, if defined.
814  */
815  ereport(LOG,
816  (errmsg("restarting archiver process because value of "
817  "\"archive_library\" was changed")));
818 
819  proc_exit(0);
820  }
int errdetail(const char *fmt,...)
Definition: elog.c:1039
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define LOG
Definition: elog.h:27
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:38
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void proc_exit(int code)
Definition: ipc.c:104
char * pstrdup(const char *in)
Definition: mcxt.c:1483
void pfree(void *pointer)
Definition: mcxt.c:1306
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1172
char * XLogArchiveLibrary
Definition: pgarch.c:89
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:467
char * XLogArchiveCommand
Definition: xlog.c:123

References ConfigReloadPending, ereport, errcode(), errdetail(), errmsg(), ERROR, LOG, LogMemoryContextPending, pfree(), PGC_SIGHUP, proc_exit(), ProcessConfigFile(), ProcessLogMemoryContextInterrupt(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, pstrdup(), XLogArchiveCommand, and XLogArchiveLibrary.

Referenced by pgarch_ArchiverCopyLoop(), and pgarch_MainLoop().

◆ LoadArchiveLibrary()

static void LoadArchiveLibrary ( void  )
static

Definition at line 828 of file pgarch.c.

831 {
832  ArchiveModuleInit archive_init;
833 
834  if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
835  ereport(ERROR,
836  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
837  errmsg("both archive_command and archive_library set"),
838  errdetail("Only one of archive_command, archive_library may be set.")));
839 
840  memset(&ArchiveContext, 0, sizeof(ArchiveModuleCallbacks));
841 
842  /*
843  * If shell archiving is enabled, use our special initialization function.
844  * Otherwise, load the library and call its _PG_archive_module_init().
845  */
846  if (XLogArchiveLibrary[0] == '\0')
847  archive_init = shell_archive_init;
848  else
849  archive_init = (ArchiveModuleInit)
851  "_PG_archive_module_init", false, NULL);
852 
853  if (archive_init == NULL)
854  ereport(ERROR,
855  (errmsg("archive modules have to define the symbol %s", "_PG_archive_module_init")));
856 
857  (*archive_init) (&ArchiveContext);
858 
859  if (ArchiveContext.archive_file_cb == NULL)
860  ereport(ERROR,
861  (errmsg("archive modules must register an archive callback")));
862 
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
static ArchiveModuleCallbacks ArchiveContext
Definition: pgarch.c:98
void(* ArchiveModuleInit)(ArchiveModuleCallbacks *cb)
Definition: pgarch.h:64
void shell_archive_init(ArchiveModuleCallbacks *cb)
Definition: shell_archive.c:29
ArchiveFileCB archive_file_cb
Definition: pgarch.h:56

References ArchiveModuleCallbacks::archive_file_cb, ArchiveContext, before_shmem_exit(), ereport, errcode(), errdetail(), errmsg(), ERROR, load_external_function(), pgarch_call_module_shutdown_cb(), shell_archive_init(), XLogArchiveCommand, and XLogArchiveLibrary.

Referenced by PgArchiverMain().

◆ pgarch_archiveDone()

static void pgarch_archiveDone ( char *  xlog)
static

Definition at line 733 of file pgarch.c.

736 {
737  char rlogready[MAXPGPATH];
738  char rlogdone[MAXPGPATH];
739 
740  StatusFilePath(rlogready, xlog, ".ready");
741  StatusFilePath(rlogdone, xlog, ".done");
742 
743  /*
744  * To avoid extra overhead, we don't durably rename the .ready file to
745  * .done. Archive commands and libraries must gracefully handle attempts
746  * to re-archive files (e.g., if the server crashes just before this
747  * function is called), so it should be okay if the .ready file reappears
748  * after a crash.
749  */
750  if (rename(rlogready, rlogdone) < 0)
753  errmsg("could not rename file \"%s\" to \"%s\": %m",
int errcode_for_file_access(void)
Definition: elog.c:718
#define WARNING
Definition: elog.h:32
#define MAXPGPATH
static void StatusFilePath(char *path, const char *xlog, const char *suffix)

References ereport, errcode_for_file_access(), errmsg(), MAXPGPATH, StatusFilePath(), and WARNING.

Referenced by pgarch_ArchiverCopyLoop().

◆ pgarch_ArchiverCopyLoop()

static void pgarch_ArchiverCopyLoop ( void  )
static

Definition at line 376 of file pgarch.c.

379 {
380  char xlog[MAX_XFN_CHARS + 1];
381 
382  /* force directory scan in the first call to pgarch_readyXlog() */
384 
385  /*
386  * loop through all xlogs with archive_status of .ready and archive
387  * them...mostly we expect this to be a single file, though it is possible
388  * some backend will add files onto the list of those that need archiving
389  * while we are still copying earlier archives
390  */
391  while (pgarch_readyXlog(xlog))
392  {
393  int failures = 0;
394  int failures_orphan = 0;
395 
396  for (;;)
397  {
398  struct stat stat_buf;
399  char pathname[MAXPGPATH];
400 
401  /*
402  * Do not initiate any more archive commands after receiving
403  * SIGTERM, nor after the postmaster has died unexpectedly. The
404  * first condition is to try to keep from having init SIGKILL the
405  * command, and the second is to avoid conflicts with another
406  * archiver spawned by a newer postmaster.
407  */
409  return;
410 
411  /*
412  * Check for barrier events and config update. This is so that
413  * we'll adopt a new setting for archive_command as soon as
414  * possible, even if there is a backlog of files to be archived.
415  */
417 
418  /* can't do anything if not configured ... */
419  if (ArchiveContext.check_configured_cb != NULL &&
421  {
423  (errmsg("archive_mode enabled, yet archiving is not configured")));
424  return;
425  }
426 
427  /*
428  * Since archive status files are not removed in a durable manner,
429  * a system crash could leave behind .ready files for WAL segments
430  * that have already been recycled or removed. In this case,
431  * simply remove the orphan status file and move on. unlink() is
432  * used here as even on subsequent crashes the same orphan files
433  * would get removed, so there is no need to worry about
434  * durability.
435  */
436  snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
437  if (stat(pathname, &stat_buf) != 0 && errno == ENOENT)
438  {
439  char xlogready[MAXPGPATH];
440 
441  StatusFilePath(xlogready, xlog, ".ready");
442  if (unlink(xlogready) == 0)
443  {
445  (errmsg("removed orphan archive status file \"%s\"",
446  xlogready)));
447 
448  /* leave loop and move to the next status file */
449  break;
450  }
451 
452  if (++failures_orphan >= NUM_ORPHAN_CLEANUP_RETRIES)
453  {
455  (errmsg("removal of orphan archive status file \"%s\" failed too many times, will try again later",
456  xlogready)));
457 
458  /* give up cleanup of orphan status files */
459  return;
460  }
461 
462  /* wait a bit before retrying */
463  pg_usleep(1000000L);
464  continue;
465  }
466 
467  if (pgarch_archiveXlog(xlog))
468  {
469  /* successful */
470  pgarch_archiveDone(xlog);
471 
472  /*
473  * Tell the cumulative stats system about the WAL file that we
474  * successfully archived
475  */
476  pgstat_report_archiver(xlog, false);
477 
478  break; /* out of inner retry loop */
479  }
480  else
481  {
482  /*
483  * Tell the cumulative stats system about the WAL file that we
484  * failed to archive
485  */
486  pgstat_report_archiver(xlog, true);
487 
488  if (++failures >= NUM_ARCHIVE_RETRIES)
489  {
491  (errmsg("archiving write-ahead log file \"%s\" failed too many times, will try again later",
492  xlog)));
493  return; /* give up archiving for now */
494  }
495  pg_usleep(1000000L); /* wait a bit before retrying */
496  }
497  }
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
static bool pgarch_archiveXlog(char *xlog)
Definition: pgarch.c:507
static bool pgarch_readyXlog(char *xlog)
Definition: pgarch.c:552
#define NUM_ARCHIVE_RETRIES
Definition: pgarch.c:63
static struct arch_files_state * arch_files
Definition: pgarch.c:124
static void HandlePgArchInterrupts(void)
Definition: pgarch.c:776
static void pgarch_archiveDone(char *xlog)
Definition: pgarch.c:733
#define NUM_ORPHAN_CLEANUP_RETRIES
Definition: pgarch.c:69
#define MAX_XFN_CHARS
Definition: pgarch.h:26
void pgstat_report_archiver(const char *xlog, bool failed)
#define PostmasterIsAlive()
Definition: pmsignal.h:102
#define snprintf
Definition: port.h:238
void pg_usleep(long microsec)
Definition: signal.c:53
ArchiveCheckConfiguredCB check_configured_cb
Definition: pgarch.h:55
int arch_files_size
Definition: pgarch.c:118
#define stat
Definition: win32_port.h:286
#define XLOGDIR

References arch_files, arch_files_state::arch_files_size, ArchiveContext, ArchiveModuleCallbacks::check_configured_cb, ereport, errmsg(), HandlePgArchInterrupts(), MAX_XFN_CHARS, MAXPGPATH, NUM_ARCHIVE_RETRIES, NUM_ORPHAN_CLEANUP_RETRIES, pg_usleep(), pgarch_archiveDone(), pgarch_archiveXlog(), pgarch_readyXlog(), pgstat_report_archiver(), PostmasterIsAlive, ShutdownRequestPending, snprintf, stat, StatusFilePath(), WARNING, and XLOGDIR.

Referenced by pgarch_MainLoop().

◆ pgarch_archiveXlog()

static bool pgarch_archiveXlog ( char *  xlog)
static

Definition at line 507 of file pgarch.c.

510 {
511  char pathname[MAXPGPATH];
512  char activitymsg[MAXFNAMELEN + 16];
513  bool ret;
514 
515  snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
516 
517  /* Report archive activity in PS display */
518  snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
519  set_ps_display(activitymsg);
520 
521  ret = ArchiveContext.archive_file_cb(xlog, pathname);
522  if (ret)
523  snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
524  else
525  snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
526  set_ps_display(activitymsg);
527 
void set_ps_display(const char *activity)
Definition: ps_status.c:342
#define MAXFNAMELEN

References ArchiveModuleCallbacks::archive_file_cb, ArchiveContext, MAXFNAMELEN, MAXPGPATH, set_ps_display(), snprintf, and XLOGDIR.

Referenced by pgarch_ArchiverCopyLoop().

◆ pgarch_call_module_shutdown_cb()

static void pgarch_call_module_shutdown_cb ( int  code,
Datum  arg 
)
static

Definition at line 868 of file pgarch.c.

871 {
872  if (ArchiveContext.shutdown_cb != NULL)
ArchiveShutdownCB shutdown_cb
Definition: pgarch.h:57

References ArchiveContext, and ArchiveModuleCallbacks::shutdown_cb.

Referenced by LoadArchiveLibrary().

◆ pgarch_die()

static void pgarch_die ( int  code,
Datum  arg 
)
static

Definition at line 762 of file pgarch.c.

765 {

References INVALID_PGPROCNO, PgArch, and PgArchData::pgprocno.

Referenced by PgArchiverMain().

◆ pgarch_MainLoop()

static void pgarch_MainLoop ( void  )
static

Definition at line 296 of file pgarch.c.

299 {
300  pg_time_t last_copy_time = 0;
301  bool time_to_stop;
302 
303  /*
304  * There shouldn't be anything for the archiver to do except to wait for a
305  * signal ... however, the archiver exists to protect our data, so she
306  * wakes up occasionally to allow herself to be proactive.
307  */
308  do
309  {
311 
312  /* When we get SIGUSR2, we do one more archive cycle, then exit */
314 
315  /* Check for barrier events and config update */
317 
318  /*
319  * If we've gotten SIGTERM, we normally just sit and do nothing until
320  * SIGUSR2 arrives. However, that means a random SIGTERM would
321  * disable archiving indefinitely, which doesn't seem like a good
322  * idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
323  * that the postmaster can start a new archiver if needed.
324  */
326  {
327  time_t curtime = time(NULL);
328 
329  if (last_sigterm_time == 0)
330  last_sigterm_time = curtime;
331  else if ((unsigned int) (curtime - last_sigterm_time) >=
332  (unsigned int) 60)
333  break;
334  }
335 
336  /* Do what we're here for */
338  last_copy_time = time(NULL);
339 
340  /*
341  * Sleep until a signal is received, or until a poll is forced by
342  * PGARCH_AUTOWAKE_INTERVAL having passed since last_copy_time, or
343  * until postmaster dies.
344  */
345  if (!time_to_stop) /* Don't wait during last iteration */
346  {
347  pg_time_t curtime = (pg_time_t) time(NULL);
348  int timeout;
349 
350  timeout = PGARCH_AUTOWAKE_INTERVAL - (curtime - last_copy_time);
351  if (timeout > 0)
352  {
353  int rc;
354 
355  rc = WaitLatch(MyLatch,
357  timeout * 1000L,
359  if (rc & WL_POSTMASTER_DEATH)
360  time_to_stop = true;
361  }
362  }
363 
364  /*
365  * The archiver quits either when the postmaster dies (not expected)
366  * or after completing one more archiving cycle after receiving
367  * SIGUSR2.
368  */
struct Latch * MyLatch
Definition: globals.c:58
void ResetLatch(Latch *latch)
Definition: latch.c:683
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:476
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
static volatile sig_atomic_t time_to_stop
Definition: pg_receivewal.c:48
static volatile sig_atomic_t ready_to_stop
Definition: pgarch.c:129
static void pgarch_ArchiverCopyLoop(void)
Definition: pgarch.c:376
#define PGARCH_AUTOWAKE_INTERVAL
Definition: pgarch.c:56
static time_t last_sigterm_time
Definition: pgarch.c:96
int64 pg_time_t
Definition: pgtime.h:23
@ WAIT_EVENT_ARCHIVER_MAIN
Definition: wait_event.h:38

References HandlePgArchInterrupts(), last_sigterm_time, MyLatch, pgarch_ArchiverCopyLoop(), PGARCH_AUTOWAKE_INTERVAL, ready_to_stop, ResetLatch(), ShutdownRequestPending, time_to_stop, WAIT_EVENT_ARCHIVER_MAIN, WaitLatch(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

Referenced by PgArchiverMain().

◆ pgarch_readyXlog()

static bool pgarch_readyXlog ( char *  xlog)
static

Definition at line 552 of file pgarch.c.

555 {
556  char XLogArchiveStatusDir[MAXPGPATH];
557  DIR *rldir;
558  struct dirent *rlde;
559  bool force_dir_scan;
560 
561  /*
562  * If a directory scan was requested, clear the stored file names and
563  * proceed.
564  */
566  force_dir_scan = PgArch->force_dir_scan;
567  PgArch->force_dir_scan = false;
569 
570  if (force_dir_scan)
572 
573  /*
574  * If we still have stored file names from the previous directory scan,
575  * try to return one of those. We check to make sure the status file is
576  * still present, as the archive_command for a previous file may have
577  * already marked it done.
578  */
579  while (arch_files->arch_files_size > 0)
580  {
581  struct stat st;
582  char status_file[MAXPGPATH];
583  char *arch_file;
584 
587  StatusFilePath(status_file, arch_file, ".ready");
588 
589  if (stat(status_file, &st) == 0)
590  {
591  strcpy(xlog, arch_file);
592  return true;
593  }
594  else if (errno != ENOENT)
595  ereport(ERROR,
597  errmsg("could not stat file \"%s\": %m", status_file)));
598  }
599 
600  /* arch_heap is probably empty, but let's make sure */
602 
603  /*
604  * Open the archive status directory and read through the list of files
605  * with the .ready suffix, looking for the earliest files.
606  */
607  snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
608  rldir = AllocateDir(XLogArchiveStatusDir);
609 
610  while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
611  {
612  int basenamelen = (int) strlen(rlde->d_name) - 6;
613  char basename[MAX_XFN_CHARS + 1];
614  char *arch_file;
615 
616  /* Ignore entries with unexpected number of characters */
617  if (basenamelen < MIN_XFN_CHARS ||
618  basenamelen > MAX_XFN_CHARS)
619  continue;
620 
621  /* Ignore entries with unexpected characters */
622  if (strspn(rlde->d_name, VALID_XFN_CHARS) < basenamelen)
623  continue;
624 
625  /* Ignore anything not suffixed with .ready */
626  if (strcmp(rlde->d_name + basenamelen, ".ready") != 0)
627  continue;
628 
629  /* Truncate off the .ready */
630  memcpy(basename, rlde->d_name, basenamelen);
631  basename[basenamelen] = '\0';
632 
633  /*
634  * Store the file in our max-heap if it has a high enough priority.
635  */
637  {
638  /* If the heap isn't full yet, quickly add it. */
640  strcpy(arch_file, basename);
642 
643  /* If we just filled the heap, make it a valid one. */
646  }
648  CStringGetDatum(basename), NULL) > 0)
649  {
650  /*
651  * Remove the lowest priority file and add the current one to the
652  * heap.
653  */
655  strcpy(arch_file, basename);
657  }
658  }
659  FreeDir(rldir);
660 
661  /* If no files were found, simply return. */
662  if (arch_files->arch_heap->bh_size == 0)
663  return false;
664 
665  /*
666  * If we didn't fill the heap, we didn't make it a valid one. Do that
667  * now.
668  */
671 
672  /*
673  * Fill arch_files array with the files to archive in ascending order of
674  * priority.
675  */
677  for (int i = 0; i < arch_files->arch_files_size; i++)
679 
680  /* Return the highest priority file. */
683 
void binaryheap_build(binaryheap *heap)
Definition: binaryheap.c:125
void binaryheap_add_unordered(binaryheap *heap, Datum d)
Definition: binaryheap.c:109
void binaryheap_reset(binaryheap *heap)
Definition: binaryheap.c:56
Datum binaryheap_remove_first(binaryheap *heap)
Definition: binaryheap.c:173
Datum binaryheap_first(binaryheap *heap)
Definition: binaryheap.c:158
void binaryheap_add(binaryheap *heap, Datum d)
Definition: binaryheap.c:141
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2709
int FreeDir(DIR *dir)
Definition: fd.c:2761
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2643
int i
Definition: isn.c:73
static PgArchData * PgArch
Definition: pgarch.c:97
static int ready_file_comparator(Datum a, Datum b, void *arg)
Definition: pgarch.c:694
#define NUM_FILES_PER_DIRECTORY_SCAN
Definition: pgarch.c:74
#define MIN_XFN_CHARS
Definition: pgarch.h:25
#define VALID_XFN_CHARS
Definition: pgarch.h:27
static char * DatumGetCString(Datum X)
Definition: postgres.h:683
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:698
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
Definition: dirent.c:26
bool force_dir_scan
Definition: pgarch.c:84
slock_t arch_lck
Definition: pgarch.c:86
char arch_filenames[NUM_FILES_PER_DIRECTORY_SCAN][MAX_XFN_CHARS+1]
Definition: pgarch.c:121
char * arch_files[NUM_FILES_PER_DIRECTORY_SCAN]
Definition: pgarch.c:119
binaryheap * arch_heap
Definition: pgarch.c:117
int bh_size
Definition: binaryheap.h:32
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15

References AllocateDir(), arch_files_state::arch_filenames, arch_files_state::arch_files, arch_files, arch_files_state::arch_files_size, arch_files_state::arch_heap, PgArchData::arch_lck, binaryheap::bh_size, binaryheap_add(), binaryheap_add_unordered(), binaryheap_build(), binaryheap_first(), binaryheap_remove_first(), binaryheap_reset(), CStringGetDatum(), dirent::d_name, DatumGetCString(), ereport, errcode_for_file_access(), errmsg(), ERROR, PgArchData::force_dir_scan, FreeDir(), i, MAX_XFN_CHARS, MAXPGPATH, MIN_XFN_CHARS, NUM_FILES_PER_DIRECTORY_SCAN, PgArch, ReadDir(), ready_file_comparator(), snprintf, SpinLockAcquire, SpinLockRelease, stat, StatusFilePath(), VALID_XFN_CHARS, and XLOGDIR.

Referenced by pgarch_ArchiverCopyLoop().

◆ pgarch_waken_stop()

static void pgarch_waken_stop ( SIGNAL_ARGS  )
static

Definition at line 279 of file pgarch.c.

282 {
283  int save_errno = errno;
284 
285  /* set flag to do a final cycle and shut down afterwards */
286  ready_to_stop = true;
287  SetLatch(MyLatch);
288 
void SetLatch(Latch *latch)
Definition: latch.c:591

References MyLatch, ready_to_stop, and SetLatch().

Referenced by PgArchiverMain().

◆ PgArchCanRestart()

bool PgArchCanRestart ( void  )

Definition at line 189 of file pgarch.c.

192 {
193  static time_t last_pgarch_start_time = 0;
194  time_t curtime = time(NULL);
195 
196  /*
197  * Return false and don't restart archiver if too soon since last archiver
198  * start.
199  */
200  if ((unsigned int) (curtime - last_pgarch_start_time) <
201  (unsigned int) PGARCH_RESTART_INTERVAL)
202  return false;
203 
204  last_pgarch_start_time = curtime;
#define PGARCH_RESTART_INTERVAL
Definition: pgarch.c:57

References PGARCH_RESTART_INTERVAL.

◆ PgArchForceDirScan()

void PgArchForceDirScan ( void  )

Definition at line 717 of file pgarch.c.

720 {
722  PgArch->force_dir_scan = true;

References PgArchData::arch_lck, PgArchData::force_dir_scan, PgArch, SpinLockAcquire, and SpinLockRelease.

Referenced by XLogArchiveNotify().

◆ PgArchiverMain()

void PgArchiverMain ( void  )

Definition at line 209 of file pgarch.c.

212 {
213  /*
214  * Ignore all signals usually bound to some action in the postmaster,
215  * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
216  */
218  pqsignal(SIGINT, SIG_IGN);
220  /* SIGQUIT handler was already set up by InitPostmasterChild */
225 
226  /* Reset some signals that are accepted by postmaster but not here */
228 
229  /* Unblock signals (they were blocked when the postmaster forked us) */
231 
232  /* We shouldn't be launched unnecessarily. */
234 
235  /* Arrange to clean up at archiver exit */
237 
238  /*
239  * Advertise our pgprocno so that backends can use our latch to wake us up
240  * while we're sleeping.
241  */
243 
244  /* Create workspace for pgarch_readyXlog() */
245  arch_files = palloc(sizeof(struct arch_files_state));
247 
248  /* Initialize our max-heap for prioritizing files to archive. */
250  ready_file_comparator, NULL);
251 
252  /* Load the archive_library. */
254 
255  pgarch_MainLoop();
256 
sigset_t UnBlockSig
Definition: pqsignal.c:22
binaryheap * binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
Definition: binaryheap.c:32
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:108
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc(Size size)
Definition: mcxt.c:1199
static void pgarch_die(int code, Datum arg)
Definition: pgarch.c:762
static void pgarch_MainLoop(void)
Definition: pgarch.c:296
static void pgarch_waken_stop(SIGNAL_ARGS)
Definition: pgarch.c:279
static void LoadArchiveLibrary(void)
Definition: pgarch.c:828
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define PG_SETMASK(mask)
Definition: pqsignal.h:18
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:638
PGPROC * MyProc
Definition: proc.c:68
int pgprocno
Definition: proc.h:191
int pgprocno
Definition: pgarch.c:79
#define SIGCHLD
Definition: win32_port.h:186
#define SIGHUP
Definition: win32_port.h:176
#define SIG_DFL
Definition: win32_port.h:171
#define SIGPIPE
Definition: win32_port.h:181
#define SIGUSR1
Definition: win32_port.h:188
#define SIGALRM
Definition: win32_port.h:182
#define SIGUSR2
Definition: win32_port.h:189
#define SIG_IGN
Definition: win32_port.h:173
#define XLogArchivingActive()
Definition: xlog.h:94

References arch_files, arch_files_state::arch_files_size, arch_files_state::arch_heap, Assert(), binaryheap_allocate(), LoadArchiveLibrary(), MyProc, NUM_FILES_PER_DIRECTORY_SCAN, on_shmem_exit(), palloc(), PG_SETMASK, PgArch, pgarch_die(), pgarch_MainLoop(), pgarch_waken_stop(), PgArchData::pgprocno, PGPROC::pgprocno, pqsignal(), proc_exit(), procsignal_sigusr1_handler(), ready_file_comparator(), SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, UnBlockSig, and XLogArchivingActive.

Referenced by AuxiliaryProcessMain().

◆ PgArchShmemInit()

void PgArchShmemInit ( void  )

Definition at line 160 of file pgarch.c.

163 {
164  bool found;
165 
166  PgArch = (PgArchData *)
167  ShmemInitStruct("Archiver Data", PgArchShmemSize(), &found);
168 
169  if (!found)
170  {
171  /* First time through, so initialize */
#define MemSet(start, val, len)
Definition: c.h:953
Size PgArchShmemSize(void)
Definition: pgarch.c:149
#define INVALID_PGPROCNO
Definition: proc.h:85
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
#define SpinLockInit(lock)
Definition: spin.h:60

References PgArchData::arch_lck, INVALID_PGPROCNO, MemSet, PgArch, PgArchShmemSize(), PgArchData::pgprocno, ShmemInitStruct(), and SpinLockInit.

Referenced by CreateSharedMemoryAndSemaphores().

◆ PgArchShmemSize()

Size PgArchShmemSize ( void  )

Definition at line 149 of file pgarch.c.

152 {
153  Size size = 0;
154 
155  size = add_size(size, sizeof(PgArchData));
156 
size_t Size
Definition: c.h:541
Size add_size(Size s1, Size s2)
Definition: shmem.c:502

References add_size().

Referenced by CalculateShmemSize(), and PgArchShmemInit().

◆ PgArchWakeup()

void PgArchWakeup ( void  )

Definition at line 262 of file pgarch.c.

265 {
266  int arch_pgprocno = PgArch->pgprocno;
267 
268  /*
269  * We don't acquire ProcArrayLock here. It's actually fine because
270  * procLatch isn't ever freed, so we just can potentially set the wrong
271  * process' (or no process') latch. Even in that case the archiver will
272  * be relaunched shortly and will start archiving.
273  */
274  if (arch_pgprocno != INVALID_PGPROCNO)

References PROC_HDR::allProcs, INVALID_PGPROCNO, PgArch, PgArchData::pgprocno, ProcGlobal, PGPROC::procLatch, and SetLatch().

Referenced by XLogArchiveNotify().

◆ ready_file_comparator()

static int ready_file_comparator ( Datum  a,
Datum  b,
void *  arg 
)
static

Definition at line 694 of file pgarch.c.

697 {
698  char *a_str = DatumGetCString(a);
699  char *b_str = DatumGetCString(b);
700  bool a_history = IsTLHistoryFileName(a_str);
701  bool b_history = IsTLHistoryFileName(b_str);
702 
703  /* Timeline history files always have the highest priority. */
704  if (a_history != b_history)
705  return a_history ? -1 : 1;
706 
707  /* Priority is given to older files. */
int b
Definition: isn.c:70
int a
Definition: isn.c:69
static bool IsTLHistoryFileName(const char *fname)

References a, b, DatumGetCString(), and IsTLHistoryFileName().

Referenced by pgarch_readyXlog(), and PgArchiverMain().

Variable Documentation

◆ arch_files

struct arch_files_state* arch_files = NULL
static

Definition at line 124 of file pgarch.c.

Referenced by pgarch_ArchiverCopyLoop(), pgarch_readyXlog(), and PgArchiverMain().

◆ ArchiveContext

◆ last_sigterm_time

time_t last_sigterm_time = 0
static

Definition at line 96 of file pgarch.c.

Referenced by pgarch_MainLoop().

◆ PgArch

PgArchData* PgArch = NULL
static

◆ ready_to_stop

volatile sig_atomic_t ready_to_stop = false
static

Definition at line 129 of file pgarch.c.

Referenced by pgarch_MainLoop(), and pgarch_waken_stop().

◆ XLogArchiveLibrary

char* XLogArchiveLibrary = ""

Definition at line 89 of file pgarch.c.

Referenced by HandlePgArchInterrupts(), and LoadArchiveLibrary().