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 "archive/archive_module.h"
#include "archive/shell_archive.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 const ArchiveModuleCallbacksArchiveCallbacks
 
static ArchiveModuleStatearchive_module_state
 
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 65 of file pgarch.c.

◆ NUM_FILES_PER_DIRECTORY_SCAN

#define NUM_FILES_PER_DIRECTORY_SCAN   64

Definition at line 76 of file pgarch.c.

◆ NUM_ORPHAN_CLEANUP_RETRIES

#define NUM_ORPHAN_CLEANUP_RETRIES   3

Definition at line 71 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 58 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 59 of file pgarch.c.

Typedef Documentation

◆ PgArchData

typedef struct PgArchData PgArchData

Function Documentation

◆ HandlePgArchInterrupts()

static void HandlePgArchInterrupts ( void  )
static

Definition at line 769 of file pgarch.c.

772 {
775 
776  /* Perform logging of memory contexts of this process */
779 
781  {
782  char *archiveLib = pstrdup(XLogArchiveLibrary);
783  bool archiveLibChanged;
784 
785  ConfigReloadPending = false;
787 
788  if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
789  ereport(ERROR,
790  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
791  errmsg("both archive_command and archive_library set"),
792  errdetail("Only one of archive_command, archive_library may be set.")));
793 
794  archiveLibChanged = strcmp(XLogArchiveLibrary, archiveLib) != 0;
795  pfree(archiveLib);
796 
797  if (archiveLibChanged)
798  {
799  /*
800  * Ideally, we would simply unload the previous archive module and
801  * load the new one, but there is presently no mechanism for
802  * unloading a library (see the comment above
803  * internal_load_library()). To deal with this, we simply restart
804  * the archiver. The new archive module will be loaded when the
805  * new archiver process starts up. Note that this triggers the
806  * module's shutdown callback, if defined.
807  */
808  ereport(LOG,
809  (errmsg("restarting archiver process because value of "
810  "\"archive_library\" was changed")));
811 
812  proc_exit(0);
813  }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define LOG
Definition: elog.h:31
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
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:1644
void pfree(void *pointer)
Definition: mcxt.c:1456
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1199
char * XLogArchiveLibrary
Definition: pgarch.c:91
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:468
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 821 of file pgarch.c.

824 {
825  ArchiveModuleInit archive_init;
826 
827  if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
828  ereport(ERROR,
829  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
830  errmsg("both archive_command and archive_library set"),
831  errdetail("Only one of archive_command, archive_library may be set.")));
832 
833  /*
834  * If shell archiving is enabled, use our special initialization function.
835  * Otherwise, load the library and call its _PG_archive_module_init().
836  */
837  if (XLogArchiveLibrary[0] == '\0')
838  archive_init = shell_archive_init;
839  else
840  archive_init = (ArchiveModuleInit)
842  "_PG_archive_module_init", false, NULL);
843 
844  if (archive_init == NULL)
845  ereport(ERROR,
846  (errmsg("archive modules have to define the symbol %s", "_PG_archive_module_init")));
847 
848  ArchiveCallbacks = (*archive_init) ();
849 
850  if (ArchiveCallbacks->archive_file_cb == NULL)
851  ereport(ERROR,
852  (errmsg("archive modules must register an archive callback")));
853 
855  if (ArchiveCallbacks->startup_cb != NULL)
857 
const ArchiveModuleCallbacks *(* ArchiveModuleInit)(void)
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
void * palloc0(Size size)
Definition: mcxt.c:1257
static const ArchiveModuleCallbacks * ArchiveCallbacks
Definition: pgarch.c:100
static ArchiveModuleState * archive_module_state
Definition: pgarch.c:101
const ArchiveModuleCallbacks * shell_archive_init(void)
Definition: shell_archive.c:40
ArchiveFileCB archive_file_cb
ArchiveStartupCB startup_cb

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

Referenced by PgArchiverMain().

◆ pgarch_archiveDone()

static void pgarch_archiveDone ( char *  xlog)
static

Definition at line 726 of file pgarch.c.

729 {
730  char rlogready[MAXPGPATH];
731  char rlogdone[MAXPGPATH];
732 
733  StatusFilePath(rlogready, xlog, ".ready");
734  StatusFilePath(rlogdone, xlog, ".done");
735 
736  /*
737  * To avoid extra overhead, we don't durably rename the .ready file to
738  * .done. Archive commands and libraries must gracefully handle attempts
739  * to re-archive files (e.g., if the server crashes just before this
740  * function is called), so it should be okay if the .ready file reappears
741  * after a crash.
742  */
743  if (rename(rlogready, rlogdone) < 0)
746  errmsg("could not rename file \"%s\" to \"%s\": %m",
int errcode_for_file_access(void)
Definition: elog.c:881
#define WARNING
Definition: elog.h:36
#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 369 of file pgarch.c.

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

References arch_files, arch_files_state::arch_files_size, archive_module_state, ArchiveCallbacks, 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 500 of file pgarch.c.

503 {
504  char pathname[MAXPGPATH];
505  char activitymsg[MAXFNAMELEN + 16];
506  bool ret;
507 
508  snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
509 
510  /* Report archive activity in PS display */
511  snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
512  set_ps_display(activitymsg);
513 
514  ret = ArchiveCallbacks->archive_file_cb(archive_module_state, xlog, pathname);
515  if (ret)
516  snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
517  else
518  snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
519  set_ps_display(activitymsg);
520 
static void set_ps_display(const char *activity)
Definition: ps_status.h:40
#define MAXFNAMELEN

References ArchiveModuleCallbacks::archive_file_cb, archive_module_state, ArchiveCallbacks, 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 863 of file pgarch.c.

866 {
867  if (ArchiveCallbacks->shutdown_cb != NULL)
ArchiveShutdownCB shutdown_cb

References archive_module_state, ArchiveCallbacks, and ArchiveModuleCallbacks::shutdown_cb.

Referenced by LoadArchiveLibrary().

◆ pgarch_die()

static void pgarch_die ( int  code,
Datum  arg 
)
static

Definition at line 755 of file pgarch.c.

758 {

References INVALID_PGPROCNO, PgArch, and PgArchData::pgprocno.

Referenced by PgArchiverMain().

◆ pgarch_MainLoop()

static void pgarch_MainLoop ( void  )
static

Definition at line 299 of file pgarch.c.

302 {
303  bool time_to_stop;
304 
305  /*
306  * There shouldn't be anything for the archiver to do except to wait for a
307  * signal ... however, the archiver exists to protect our data, so it
308  * wakes up occasionally to allow itself to be proactive.
309  */
310  do
311  {
313 
314  /* When we get SIGUSR2, we do one more archive cycle, then exit */
316 
317  /* Check for barrier events and config update */
319 
320  /*
321  * If we've gotten SIGTERM, we normally just sit and do nothing until
322  * SIGUSR2 arrives. However, that means a random SIGTERM would
323  * disable archiving indefinitely, which doesn't seem like a good
324  * idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
325  * that the postmaster can start a new archiver if needed.
326  */
328  {
329  time_t curtime = time(NULL);
330 
331  if (last_sigterm_time == 0)
332  last_sigterm_time = curtime;
333  else if ((unsigned int) (curtime - last_sigterm_time) >=
334  (unsigned int) 60)
335  break;
336  }
337 
338  /* Do what we're here for */
340 
341  /*
342  * Sleep until a signal is received, or until a poll is forced by
343  * PGARCH_AUTOWAKE_INTERVAL, or until postmaster dies.
344  */
345  if (!time_to_stop) /* Don't wait during last iteration */
346  {
347  int rc;
348 
349  rc = WaitLatch(MyLatch,
351  PGARCH_AUTOWAKE_INTERVAL * 1000L,
352  WAIT_EVENT_ARCHIVER_MAIN);
353  if (rc & WL_POSTMASTER_DEATH)
354  time_to_stop = true;
355  }
356 
357  /*
358  * The archiver quits either when the postmaster dies (not expected)
359  * or after completing one more archiving cycle after receiving
360  * SIGUSR2.
361  */
struct Latch * MyLatch
Definition: globals.c:58
void ResetLatch(Latch *latch)
Definition: latch.c:697
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:490
#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:132
static void pgarch_ArchiverCopyLoop(void)
Definition: pgarch.c:369
#define PGARCH_AUTOWAKE_INTERVAL
Definition: pgarch.c:58
static time_t last_sigterm_time
Definition: pgarch.c:98

References HandlePgArchInterrupts(), last_sigterm_time, MyLatch, pgarch_ArchiverCopyLoop(), PGARCH_AUTOWAKE_INTERVAL, ready_to_stop, ResetLatch(), ShutdownRequestPending, time_to_stop, 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 545 of file pgarch.c.

548 {
549  char XLogArchiveStatusDir[MAXPGPATH];
550  DIR *rldir;
551  struct dirent *rlde;
552  bool force_dir_scan;
553 
554  /*
555  * If a directory scan was requested, clear the stored file names and
556  * proceed.
557  */
559  force_dir_scan = PgArch->force_dir_scan;
560  PgArch->force_dir_scan = false;
562 
563  if (force_dir_scan)
565 
566  /*
567  * If we still have stored file names from the previous directory scan,
568  * try to return one of those. We check to make sure the status file is
569  * still present, as the archive_command for a previous file may have
570  * already marked it done.
571  */
572  while (arch_files->arch_files_size > 0)
573  {
574  struct stat st;
575  char status_file[MAXPGPATH];
576  char *arch_file;
577 
580  StatusFilePath(status_file, arch_file, ".ready");
581 
582  if (stat(status_file, &st) == 0)
583  {
584  strcpy(xlog, arch_file);
585  return true;
586  }
587  else if (errno != ENOENT)
588  ereport(ERROR,
590  errmsg("could not stat file \"%s\": %m", status_file)));
591  }
592 
593  /* arch_heap is probably empty, but let's make sure */
595 
596  /*
597  * Open the archive status directory and read through the list of files
598  * with the .ready suffix, looking for the earliest files.
599  */
600  snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
601  rldir = AllocateDir(XLogArchiveStatusDir);
602 
603  while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
604  {
605  int basenamelen = (int) strlen(rlde->d_name) - 6;
606  char basename[MAX_XFN_CHARS + 1];
607  char *arch_file;
608 
609  /* Ignore entries with unexpected number of characters */
610  if (basenamelen < MIN_XFN_CHARS ||
611  basenamelen > MAX_XFN_CHARS)
612  continue;
613 
614  /* Ignore entries with unexpected characters */
615  if (strspn(rlde->d_name, VALID_XFN_CHARS) < basenamelen)
616  continue;
617 
618  /* Ignore anything not suffixed with .ready */
619  if (strcmp(rlde->d_name + basenamelen, ".ready") != 0)
620  continue;
621 
622  /* Truncate off the .ready */
623  memcpy(basename, rlde->d_name, basenamelen);
624  basename[basenamelen] = '\0';
625 
626  /*
627  * Store the file in our max-heap if it has a high enough priority.
628  */
630  {
631  /* If the heap isn't full yet, quickly add it. */
633  strcpy(arch_file, basename);
635 
636  /* If we just filled the heap, make it a valid one. */
639  }
641  CStringGetDatum(basename), NULL) > 0)
642  {
643  /*
644  * Remove the lowest priority file and add the current one to the
645  * heap.
646  */
648  strcpy(arch_file, basename);
650  }
651  }
652  FreeDir(rldir);
653 
654  /* If no files were found, simply return. */
655  if (arch_files->arch_heap->bh_size == 0)
656  return false;
657 
658  /*
659  * If we didn't fill the heap, we didn't make it a valid one. Do that
660  * now.
661  */
664 
665  /*
666  * Fill arch_files array with the files to archive in ascending order of
667  * priority.
668  */
670  for (int i = 0; i < arch_files->arch_files_size; i++)
672 
673  /* Return the highest priority file. */
676 
void binaryheap_build(binaryheap *heap)
Definition: binaryheap.c:138
void binaryheap_reset(binaryheap *heap)
Definition: binaryheap.c:63
bh_node_type binaryheap_first(binaryheap *heap)
Definition: binaryheap.c:177
void binaryheap_add(binaryheap *heap, bh_node_type d)
Definition: binaryheap.c:154
bh_node_type binaryheap_remove_first(binaryheap *heap)
Definition: binaryheap.c:192
void binaryheap_add_unordered(binaryheap *heap, bh_node_type d)
Definition: binaryheap.c:116
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2854
int FreeDir(DIR *dir)
Definition: fd.c:2906
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2788
int i
Definition: isn.c:73
static PgArchData * PgArch
Definition: pgarch.c:99
static int ready_file_comparator(Datum a, Datum b, void *arg)
Definition: pgarch.c:687
#define NUM_FILES_PER_DIRECTORY_SCAN
Definition: pgarch.c:76
#define MIN_XFN_CHARS
Definition: pgarch.h:25
#define VALID_XFN_CHARS
Definition: pgarch.h:27
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
#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:86
slock_t arch_lck
Definition: pgarch.c:88
char arch_filenames[NUM_FILES_PER_DIRECTORY_SCAN][MAX_XFN_CHARS+1]
Definition: pgarch.c:124
char * arch_files[NUM_FILES_PER_DIRECTORY_SCAN]
Definition: pgarch.c:122
binaryheap * arch_heap
Definition: pgarch.c:120
int bh_size
Definition: binaryheap.h:44
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 282 of file pgarch.c.

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

References MyLatch, ready_to_stop, and SetLatch().

Referenced by PgArchiverMain().

◆ PgArchCanRestart()

bool PgArchCanRestart ( void  )

Definition at line 192 of file pgarch.c.

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

References PGARCH_RESTART_INTERVAL.

◆ PgArchForceDirScan()

void PgArchForceDirScan ( void  )

Definition at line 710 of file pgarch.c.

713 {
715  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 212 of file pgarch.c.

215 {
216  /*
217  * Ignore all signals usually bound to some action in the postmaster,
218  * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
219  */
221  pqsignal(SIGINT, SIG_IGN);
223  /* SIGQUIT handler was already set up by InitPostmasterChild */
228 
229  /* Reset some signals that are accepted by postmaster but not here */
231 
232  /* Unblock signals (they were blocked when the postmaster forked us) */
233  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
234 
235  /* We shouldn't be launched unnecessarily. */
237 
238  /* Arrange to clean up at archiver exit */
240 
241  /*
242  * Advertise our pgprocno so that backends can use our latch to wake us up
243  * while we're sleeping.
244  */
246 
247  /* Create workspace for pgarch_readyXlog() */
248  arch_files = palloc(sizeof(struct arch_files_state));
250 
251  /* Initialize our max-heap for prioritizing files to archive. */
253  ready_file_comparator, NULL);
254 
255  /* Load the archive_library. */
257 
258  pgarch_MainLoop();
259 
sigset_t UnBlockSig
Definition: pqsignal.c:22
binaryheap * binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
Definition: binaryheap.c:39
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:109
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:1226
static void pgarch_die(int code, Datum arg)
Definition: pgarch.c:755
static void pgarch_MainLoop(void)
Definition: pgarch.c:299
static void pgarch_waken_stop(SIGNAL_ARGS)
Definition: pgarch.c:282
static void LoadArchiveLibrary(void)
Definition: pgarch.c:821
pqsigfunc pqsignal(int signo, pqsigfunc func)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:639
PGPROC * MyProc
Definition: proc.c:66
int pgprocno
Definition: proc.h:191
int pgprocno
Definition: pgarch.c:81
#define SIGCHLD
Definition: win32_port.h:178
#define SIGHUP
Definition: win32_port.h:168
#define SIG_DFL
Definition: win32_port.h:163
#define SIGPIPE
Definition: win32_port.h:173
#define SIGUSR1
Definition: win32_port.h:180
#define SIGALRM
Definition: win32_port.h:174
#define SIGUSR2
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:165
#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(), 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 163 of file pgarch.c.

166 {
167  bool found;
168 
169  PgArch = (PgArchData *)
170  ShmemInitStruct("Archiver Data", PgArchShmemSize(), &found);
171 
172  if (!found)
173  {
174  /* First time through, so initialize */
#define MemSet(start, val, len)
Definition: c.h:1009
Size PgArchShmemSize(void)
Definition: pgarch.c:152
#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 152 of file pgarch.c.

155 {
156  Size size = 0;
157 
158  size = add_size(size, sizeof(PgArchData));
159 
size_t Size
Definition: c.h:594
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 265 of file pgarch.c.

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

690 {
691  char *a_str = DatumGetCString(a);
692  char *b_str = DatumGetCString(b);
693  bool a_history = IsTLHistoryFileName(a_str);
694  bool b_history = IsTLHistoryFileName(b_str);
695 
696  /* Timeline history files always have the highest priority. */
697  if (a_history != b_history)
698  return a_history ? -1 : 1;
699 
700  /* 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 127 of file pgarch.c.

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

◆ archive_module_state

ArchiveModuleState* archive_module_state
static

◆ ArchiveCallbacks

const ArchiveModuleCallbacks* ArchiveCallbacks
static

◆ last_sigterm_time

time_t last_sigterm_time = 0
static

Definition at line 98 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 132 of file pgarch.c.

Referenced by pgarch_MainLoop(), and pgarch_waken_stop().

◆ XLogArchiveLibrary

char* XLogArchiveLibrary = ""

Definition at line 91 of file pgarch.c.

Referenced by HandlePgArchInterrupts(), and LoadArchiveLibrary().