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 call_archive_module_shutdown_callback (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

◆ call_archive_module_shutdown_callback()

static void call_archive_module_shutdown_callback ( int  code,
Datum  arg 
)
static

Definition at line 855 of file pgarch.c.

858 {
859  if (ArchiveContext.shutdown_cb != NULL)
static ArchiveModuleCallbacks ArchiveContext
Definition: pgarch.c:98
ArchiveShutdownCB shutdown_cb
Definition: pgarch.h:57

References ArchiveContext, and ArchiveModuleCallbacks::shutdown_cb.

Referenced by HandlePgArchInterrupts(), and PgArchiverMain().

◆ HandlePgArchInterrupts()

static void HandlePgArchInterrupts ( void  )
static

Definition at line 770 of file pgarch.c.

773 {
776 
777  /* Perform logging of memory contexts of this process */
780 
782  {
783  char *archiveLib = pstrdup(XLogArchiveLibrary);
784  bool archiveLibChanged;
785 
786  ConfigReloadPending = false;
788 
789  archiveLibChanged = strcmp(XLogArchiveLibrary, archiveLib) != 0;
790  pfree(archiveLib);
791 
792  if (archiveLibChanged)
793  {
794  /*
795  * Call the currently loaded archive module's shutdown callback,
796  * if one is defined.
797  */
799 
800  /*
801  * Ideally, we would simply unload the previous archive module and
802  * load the new one, but there is presently no mechanism for
803  * unloading a library (see the comment above
804  * internal_load_library()). To deal with this, we simply restart
805  * the archiver. The new archive module will be loaded when the
806  * new archiver process starts up.
807  */
808  ereport(LOG,
809  (errmsg("restarting archiver process because value of "
810  "\"archive_library\" was changed")));
811 
812  proc_exit(0);
813  }
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define LOG
Definition: elog.h:25
#define ereport(elevel,...)
Definition: elog.h:143
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:38
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37
@ PGC_SIGHUP
Definition: guc.h:72
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:1305
void pfree(void *pointer)
Definition: mcxt.c:1175
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1041
char * XLogArchiveLibrary
Definition: pgarch.c:89
static void call_archive_module_shutdown_callback(int code, Datum arg)
Definition: pgarch.c:855
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:467

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

Referenced by pgarch_ArchiverCopyLoop(), and pgarch_MainLoop().

◆ LoadArchiveLibrary()

static void LoadArchiveLibrary ( void  )
static

Definition at line 821 of file pgarch.c.

824 {
825  ArchiveModuleInit archive_init;
826 
827  memset(&ArchiveContext, 0, sizeof(ArchiveModuleCallbacks));
828 
829  /*
830  * If shell archiving is enabled, use our special initialization function.
831  * Otherwise, load the library and call its _PG_archive_module_init().
832  */
833  if (XLogArchiveLibrary[0] == '\0')
834  archive_init = shell_archive_init;
835  else
836  archive_init = (ArchiveModuleInit)
838  "_PG_archive_module_init", false, NULL);
839 
840  if (archive_init == NULL)
841  ereport(ERROR,
842  (errmsg("archive modules have to declare the _PG_archive_module_init symbol")));
843 
844  (*archive_init) (&ArchiveContext);
845 
846  if (ArchiveContext.archive_file_cb == NULL)
847  ereport(ERROR,
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
#define ERROR
Definition: elog.h:33
void(* ArchiveModuleInit)(ArchiveModuleCallbacks *cb)
Definition: pgarch.h:64
void shell_archive_init(ArchiveModuleCallbacks *cb)
Definition: shell_archive.c:28
ArchiveFileCB archive_file_cb
Definition: pgarch.h:56

References ArchiveModuleCallbacks::archive_file_cb, ArchiveContext, ereport, errmsg(), ERROR, load_external_function(), shell_archive_init(), and XLogArchiveLibrary.

Referenced by PgArchiverMain().

◆ pgarch_archiveDone()

static void pgarch_archiveDone ( char *  xlog)
static

Definition at line 739 of file pgarch.c.

742 {
743  char rlogready[MAXPGPATH];
744  char rlogdone[MAXPGPATH];
745 
746  StatusFilePath(rlogready, xlog, ".ready");
747  StatusFilePath(rlogdone, xlog, ".done");
#define MAXPGPATH
#define StatusFilePath(path, xlog, suffix)

References durable_rename(), MAXPGPATH, StatusFilePath, and WARNING.

Referenced by pgarch_ArchiverCopyLoop().

◆ pgarch_ArchiverCopyLoop()

static void pgarch_ArchiverCopyLoop ( void  )
static

Definition at line 382 of file pgarch.c.

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

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

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

Referenced by pgarch_ArchiverCopyLoop().

◆ pgarch_die()

static void pgarch_die ( int  code,
Datum  arg 
)
static

Definition at line 756 of file pgarch.c.

759 {

References INVALID_PGPROCNO, PgArch, and PgArchData::pgprocno.

Referenced by PgArchiverMain().

◆ pgarch_MainLoop()

static void pgarch_MainLoop ( void  )
static

Definition at line 302 of file pgarch.c.

305 {
306  pg_time_t last_copy_time = 0;
307  bool time_to_stop;
308 
309  /*
310  * There shouldn't be anything for the archiver to do except to wait for a
311  * signal ... however, the archiver exists to protect our data, so she
312  * wakes up occasionally to allow herself to be proactive.
313  */
314  do
315  {
317 
318  /* When we get SIGUSR2, we do one more archive cycle, then exit */
320 
321  /* Check for barrier events and config update */
323 
324  /*
325  * If we've gotten SIGTERM, we normally just sit and do nothing until
326  * SIGUSR2 arrives. However, that means a random SIGTERM would
327  * disable archiving indefinitely, which doesn't seem like a good
328  * idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
329  * that the postmaster can start a new archiver if needed.
330  */
332  {
333  time_t curtime = time(NULL);
334 
335  if (last_sigterm_time == 0)
336  last_sigterm_time = curtime;
337  else if ((unsigned int) (curtime - last_sigterm_time) >=
338  (unsigned int) 60)
339  break;
340  }
341 
342  /* Do what we're here for */
344  last_copy_time = time(NULL);
345 
346  /*
347  * Sleep until a signal is received, or until a poll is forced by
348  * PGARCH_AUTOWAKE_INTERVAL having passed since last_copy_time, or
349  * until postmaster dies.
350  */
351  if (!time_to_stop) /* Don't wait during last iteration */
352  {
353  pg_time_t curtime = (pg_time_t) time(NULL);
354  int timeout;
355 
356  timeout = PGARCH_AUTOWAKE_INTERVAL - (curtime - last_copy_time);
357  if (timeout > 0)
358  {
359  int rc;
360 
361  rc = WaitLatch(MyLatch,
363  timeout * 1000L,
365  if (rc & WL_POSTMASTER_DEATH)
366  time_to_stop = true;
367  }
368  }
369 
370  /*
371  * The archiver quits either when the postmaster dies (not expected)
372  * or after completing one more archiving cycle after receiving
373  * SIGUSR2.
374  */
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 bool 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:382
#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 558 of file pgarch.c.

561 {
562  char XLogArchiveStatusDir[MAXPGPATH];
563  DIR *rldir;
564  struct dirent *rlde;
565  bool force_dir_scan;
566 
567  /*
568  * If a directory scan was requested, clear the stored file names and
569  * proceed.
570  */
572  force_dir_scan = PgArch->force_dir_scan;
573  PgArch->force_dir_scan = false;
575 
576  if (force_dir_scan)
578 
579  /*
580  * If we still have stored file names from the previous directory scan,
581  * try to return one of those. We check to make sure the status file is
582  * still present, as the archive_command for a previous file may have
583  * already marked it done.
584  */
585  while (arch_files->arch_files_size > 0)
586  {
587  struct stat st;
588  char status_file[MAXPGPATH];
589  char *arch_file;
590 
593  StatusFilePath(status_file, arch_file, ".ready");
594 
595  if (stat(status_file, &st) == 0)
596  {
597  strcpy(xlog, arch_file);
598  return true;
599  }
600  else if (errno != ENOENT)
601  ereport(ERROR,
603  errmsg("could not stat file \"%s\": %m", status_file)));
604  }
605 
606  /* arch_heap is probably empty, but let's make sure */
608 
609  /*
610  * Open the archive status directory and read through the list of files
611  * with the .ready suffix, looking for the earliest files.
612  */
613  snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
614  rldir = AllocateDir(XLogArchiveStatusDir);
615 
616  while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
617  {
618  int basenamelen = (int) strlen(rlde->d_name) - 6;
619  char basename[MAX_XFN_CHARS + 1];
620  char *arch_file;
621 
622  /* Ignore entries with unexpected number of characters */
623  if (basenamelen < MIN_XFN_CHARS ||
624  basenamelen > MAX_XFN_CHARS)
625  continue;
626 
627  /* Ignore entries with unexpected characters */
628  if (strspn(rlde->d_name, VALID_XFN_CHARS) < basenamelen)
629  continue;
630 
631  /* Ignore anything not suffixed with .ready */
632  if (strcmp(rlde->d_name + basenamelen, ".ready") != 0)
633  continue;
634 
635  /* Truncate off the .ready */
636  memcpy(basename, rlde->d_name, basenamelen);
637  basename[basenamelen] = '\0';
638 
639  /*
640  * Store the file in our max-heap if it has a high enough priority.
641  */
643  {
644  /* If the heap isn't full yet, quickly add it. */
646  strcpy(arch_file, basename);
648 
649  /* If we just filled the heap, make it a valid one. */
652  }
654  CStringGetDatum(basename), NULL) > 0)
655  {
656  /*
657  * Remove the lowest priority file and add the current one to the
658  * heap.
659  */
661  strcpy(arch_file, basename);
663  }
664  }
665  FreeDir(rldir);
666 
667  /* If no files were found, simply return. */
668  if (arch_files->arch_heap->bh_size == 0)
669  return false;
670 
671  /*
672  * If we didn't fill the heap, we didn't make it a valid one. Do that
673  * now.
674  */
677 
678  /*
679  * Fill arch_files array with the files to archive in ascending order of
680  * priority.
681  */
683  for (int i = 0; i < arch_files->arch_files_size; i++)
685 
686  /* Return the highest priority file. */
689 
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
int errcode_for_file_access(void)
Definition: elog.c:716
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2725
int FreeDir(DIR *dir)
Definition: fd.c:2777
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2659
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:700
#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
#define CStringGetDatum(X)
Definition: postgres.h:622
#define DatumGetCString(X)
Definition: postgres.h:610
#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 285 of file pgarch.c.

288 {
289  int save_errno = errno;
290 
291  /* set flag to do a final cycle and shut down afterwards */
292  ready_to_stop = true;
293  SetLatch(MyLatch);
294 
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 723 of file pgarch.c.

726 {
728  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 
256  {
257  pgarch_MainLoop();
258  }
260 
262 
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
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc(Size size)
Definition: mcxt.c:1068
static void pgarch_die(int code, Datum arg)
Definition: pgarch.c:756
static void pgarch_MainLoop(void)
Definition: pgarch.c:302
static void pgarch_waken_stop(SIGNAL_ARGS)
Definition: pgarch.c:285
static void LoadArchiveLibrary(void)
Definition: pgarch.c:821
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:638
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
PGPROC * MyProc
Definition: proc.c:68
int pgprocno
Definition: proc.h:188
int pgprocno
Definition: pgarch.c:79
#define SIGCHLD
Definition: win32_port.h:177
#define SIGHUP
Definition: win32_port.h:167
#define SIG_DFL
Definition: win32_port.h:162
#define SIGPIPE
Definition: win32_port.h:172
#define SIGUSR1
Definition: win32_port.h:179
#define SIGALRM
Definition: win32_port.h:173
#define SIGUSR2
Definition: win32_port.h:180
#define SIG_IGN
Definition: win32_port.h:164
#define XLogArchivingActive()
Definition: xlog.h:94

References arch_files, arch_files_state::arch_files_size, arch_files_state::arch_heap, Assert(), binaryheap_allocate(), call_archive_module_shutdown_callback(), LoadArchiveLibrary(), MyProc, NUM_FILES_PER_DIRECTORY_SCAN, on_shmem_exit(), palloc(), PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, 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:1019
Size PgArchShmemSize(void)
Definition: pgarch.c:149
#define INVALID_PGPROCNO
Definition: proc.h:83
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:551
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 268 of file pgarch.c.

271 {
272  int arch_pgprocno = PgArch->pgprocno;
273 
274  /*
275  * We don't acquire ProcArrayLock here. It's actually fine because
276  * procLatch isn't ever freed, so we just can potentially set the wrong
277  * process' (or no process') latch. Even in that case the archiver will
278  * be relaunched shortly and will start archiving.
279  */
280  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 700 of file pgarch.c.

703 {
704  char *a_str = DatumGetCString(a);
705  char *b_str = DatumGetCString(b);
706  bool a_history = IsTLHistoryFileName(a_str);
707  bool b_history = IsTLHistoryFileName(b_str);
708 
709  /* Timeline history files always have the highest priority. */
710  if (a_history != b_history)
711  return a_history ? -1 : 1;
712 
713  /* Priority is given to older files. */
int b
Definition: isn.c:70
int a
Definition: isn.c:69
#define IsTLHistoryFileName(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().