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/auxprocess.h"
#include "postmaster/interrupt.h"
#include "postmaster/pgarch.h"
#include "storage/condition_variable.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 "utils/guc.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/resowner.h"
#include "utils/timeout.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 (char *startup_data, size_t startup_data_len)
 
void PgArchWakeup (void)
 
void PgArchForceDirScan (void)
 

Variables

char * XLogArchiveLibrary = ""
 
char * arch_module_check_errdetail_string
 
static time_t last_sigterm_time = 0
 
static PgArchDataPgArch = NULL
 
static const ArchiveModuleCallbacksArchiveCallbacks
 
static ArchiveModuleStatearchive_module_state
 
static MemoryContext archive_context
 
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 68 of file pgarch.c.

◆ NUM_FILES_PER_DIRECTORY_SCAN

#define NUM_FILES_PER_DIRECTORY_SCAN   64

Definition at line 79 of file pgarch.c.

◆ NUM_ORPHAN_CLEANUP_RETRIES

#define NUM_ORPHAN_CLEANUP_RETRIES   3

Definition at line 74 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 61 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 62 of file pgarch.c.

Typedef Documentation

◆ PgArchData

typedef struct PgArchData PgArchData

Function Documentation

◆ HandlePgArchInterrupts()

static void HandlePgArchInterrupts ( void  )
static

Definition at line 857 of file pgarch.c.

860{
863
864 /* Perform logging of memory contexts of this process */
867
869 {
870 char *archiveLib = pstrdup(XLogArchiveLibrary);
871 bool archiveLibChanged;
872
873 ConfigReloadPending = false;
875
876 if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
878 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
879 errmsg("both \"archive_command\" and \"archive_library\" set"),
880 errdetail("Only one of \"archive_command\", \"archive_library\" may be set.")));
881
882 archiveLibChanged = strcmp(XLogArchiveLibrary, archiveLib) != 0;
883 pfree(archiveLib);
884
885 if (archiveLibChanged)
886 {
887 /*
888 * Ideally, we would simply unload the previous archive module and
889 * load the new one, but there is presently no mechanism for
890 * unloading a library (see the comment above
891 * internal_load_library()). To deal with this, we simply restart
892 * the archiver. The new archive module will be loaded when the
893 * new archiver process starts up. Note that this triggers the
894 * module's shutdown callback, if defined.
895 */
896 ereport(LOG,
897 (errmsg("restarting archiver process because value of "
898 "\"archive_library\" was changed")));
899
900 proc_exit(0);
901 }
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#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:40
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:39
void ProcessConfigFile(GucContext context)
Definition: guc-file.l:120
@ PGC_SIGHUP
Definition: guc.h:71
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:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1289
char * XLogArchiveLibrary
Definition: pgarch.c:92
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:496
char * XLogArchiveCommand
Definition: xlog.c:120

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 909 of file pgarch.c.

912{
913 ArchiveModuleInit archive_init;
914
915 if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
917 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
918 errmsg("both \"archive_command\" and \"archive_library\" set"),
919 errdetail("Only one of \"archive_command\", \"archive_library\" may be set.")));
920
921 /*
922 * If shell archiving is enabled, use our special initialization function.
923 * Otherwise, load the library and call its _PG_archive_module_init().
924 */
925 if (XLogArchiveLibrary[0] == '\0')
926 archive_init = shell_archive_init;
927 else
928 archive_init = (ArchiveModuleInit)
930 "_PG_archive_module_init", false, NULL);
931
932 if (archive_init == NULL)
934 (errmsg("archive modules have to define the symbol %s", "_PG_archive_module_init")));
935
936 ArchiveCallbacks = (*archive_init) ();
937
940 (errmsg("archive modules must register an archive callback")));
941
943 if (ArchiveCallbacks->startup_cb != NULL)
945
const ArchiveModuleCallbacks *(* ArchiveModuleInit)(void)
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:95
void * palloc0(Size size)
Definition: mcxt.c:1347
static const ArchiveModuleCallbacks * ArchiveCallbacks
Definition: pgarch.c:102
static ArchiveModuleState * archive_module_state
Definition: pgarch.c:103
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 814 of file pgarch.c.

817{
818 char rlogready[MAXPGPATH];
819 char rlogdone[MAXPGPATH];
820
821 StatusFilePath(rlogready, xlog, ".ready");
822 StatusFilePath(rlogdone, xlog, ".done");
823
824 /*
825 * To avoid extra overhead, we don't durably rename the .ready file to
826 * .done. Archive commands and libraries must gracefully handle attempts
827 * to re-archive files (e.g., if the server crashes just before this
828 * function is called), so it should be okay if the .ready file reappears
829 * after a crash.
830 */
831 if (rename(rlogready, rlogdone) < 0)
834 errmsg("could not rename file \"%s\" to \"%s\": %m",
int errcode_for_file_access(void)
Definition: elog.c:876
#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 378 of file pgarch.c.

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

References arch_files, arch_files_state::arch_files_size, arch_module_check_errdetail_string, archive_module_state, ArchiveCallbacks, ArchiveModuleCallbacks::check_configured_cb, ereport, errdetail_internal(), 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 514 of file pgarch.c.

517{
518 sigjmp_buf local_sigjmp_buf;
519 MemoryContext oldcontext;
520 char pathname[MAXPGPATH];
521 char activitymsg[MAXFNAMELEN + 16];
522 bool ret;
523
524 snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
525
526 /* Report archive activity in PS display */
527 snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
528 set_ps_display(activitymsg);
529
531
532 /*
533 * Since the archiver operates at the bottom of the exception stack,
534 * ERRORs turn into FATALs and cause the archiver process to restart.
535 * However, using ereport(ERROR, ...) when there are problems is easy to
536 * code and maintain. Therefore, we create our own exception handler to
537 * catch ERRORs and return false instead of restarting the archiver
538 * whenever there is a failure.
539 *
540 * We assume ERRORs from the archiving callback are the most common
541 * exceptions experienced by the archiver, so we opt to handle exceptions
542 * here instead of PgArchiverMain() to avoid reinitializing the archiver
543 * too frequently. We could instead add a sigsetjmp() block to
544 * PgArchiverMain() and use PG_TRY/PG_CATCH here, but the extra code to
545 * avoid the odd archiver restart doesn't seem worth it.
546 */
547 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
548 {
549 /* Since not using PG_TRY, must reset error stack by hand */
550 error_context_stack = NULL;
551
552 /* Prevent interrupts while cleaning up */
554
555 /* Report the error to the server log. */
557
558 /*
559 * Try to clean up anything the archive module left behind. We try to
560 * cover anything that an archive module could conceivably have left
561 * behind, but it is of course possible that modules could be doing
562 * unexpected things that require additional cleanup. Module authors
563 * should be sure to do any extra required cleanup in a PG_CATCH block
564 * within the archiving callback, and they are encouraged to notify
565 * the pgsql-hackers mailing list so that we can add it here.
566 */
572 AtEOXact_Files(false);
573 AtEOXact_HashTables(false);
574
575 /*
576 * Return to the original memory context and clear ErrorContext for
577 * next time.
578 */
579 MemoryContextSwitchTo(oldcontext);
581
582 /* Flush any leaked data */
584
585 /* Remove our exception handler */
586 PG_exception_stack = NULL;
587
588 /* Now we can allow interrupts again */
590
591 /* Report failure so that the archiver retries this file */
592 ret = false;
593 }
594 else
595 {
596 /* Enable our exception handler */
597 PG_exception_stack = &local_sigjmp_buf;
598
599 /* Archive the file! */
601 xlog, pathname);
602
603 /* Remove our exception handler */
604 PG_exception_stack = NULL;
605
606 /* Reset our memory context and switch back to the original one */
607 MemoryContextSwitchTo(oldcontext);
609 }
610
611 if (ret)
612 snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
613 else
614 snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
615 set_ps_display(activitymsg);
616
bool ConditionVariableCancelSleep(void)
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1912
void EmitErrorReport(void)
Definition: elog.c:1687
ErrorContextCallback * error_context_stack
Definition: elog.c:94
void FlushErrorState(void)
Definition: elog.c:1867
sigjmp_buf * PG_exception_stack
Definition: elog.c:96
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3187
void LWLockReleaseAll(void)
Definition: lwlock.c:1876
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:383
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:135
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:133
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static MemoryContext archive_context
Definition: pgarch.c:104
static void set_ps_display(const char *activity)
Definition: ps_status.h:40
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:1002
void disable_all_timeouts(bool keep_indicators)
Definition: timeout.c:751
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101
#define MAXFNAMELEN

References archive_context, ArchiveModuleCallbacks::archive_file_cb, archive_module_state, ArchiveCallbacks, AtEOXact_Files(), AtEOXact_HashTables(), ConditionVariableCancelSleep(), disable_all_timeouts(), EmitErrorReport(), error_context_stack, FlushErrorState(), HOLD_INTERRUPTS, LWLockReleaseAll(), MAXFNAMELEN, MAXPGPATH, MemoryContextReset(), MemoryContextSwitchTo(), PG_exception_stack, pgstat_report_wait_end(), ReleaseAuxProcessResources(), RESUME_INTERRUPTS, 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 951 of file pgarch.c.

954{
955 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 843 of file pgarch.c.

846{

References INVALID_PROC_NUMBER, PgArch, and PgArchData::pgprocno.

Referenced by PgArchiverMain().

◆ pgarch_MainLoop()

static void pgarch_MainLoop ( void  )
static

Definition at line 308 of file pgarch.c.

311{
312 bool time_to_stop;
313
314 /*
315 * There shouldn't be anything for the archiver to do except to wait for a
316 * signal ... however, the archiver exists to protect our data, so it
317 * wakes up occasionally to allow itself to be proactive.
318 */
319 do
320 {
322
323 /* When we get SIGUSR2, we do one more archive cycle, then exit */
325
326 /* Check for barrier events and config update */
328
329 /*
330 * If we've gotten SIGTERM, we normally just sit and do nothing until
331 * SIGUSR2 arrives. However, that means a random SIGTERM would
332 * disable archiving indefinitely, which doesn't seem like a good
333 * idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
334 * that the postmaster can start a new archiver if needed.
335 */
337 {
338 time_t curtime = time(NULL);
339
340 if (last_sigterm_time == 0)
341 last_sigterm_time = curtime;
342 else if ((unsigned int) (curtime - last_sigterm_time) >=
343 (unsigned int) 60)
344 break;
345 }
346
347 /* Do what we're here for */
349
350 /*
351 * Sleep until a signal is received, or until a poll is forced by
352 * PGARCH_AUTOWAKE_INTERVAL, or until postmaster dies.
353 */
354 if (!time_to_stop) /* Don't wait during last iteration */
355 {
356 int rc;
357
358 rc = WaitLatch(MyLatch,
361 WAIT_EVENT_ARCHIVER_MAIN);
362 if (rc & WL_POSTMASTER_DEATH)
363 time_to_stop = true;
364 }
365
366 /*
367 * The archiver quits either when the postmaster dies (not expected)
368 * or after completing one more archiving cycle after receiving
369 * SIGUSR2.
370 */
struct Latch * MyLatch
Definition: globals.c:62
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:127
#define WL_POSTMASTER_DEATH
Definition: latch.h:131
static volatile sig_atomic_t time_to_stop
Definition: pg_receivewal.c:48
static volatile sig_atomic_t ready_to_stop
Definition: pgarch.c:135
static void pgarch_ArchiverCopyLoop(void)
Definition: pgarch.c:378
#define PGARCH_AUTOWAKE_INTERVAL
Definition: pgarch.c:61
static time_t last_sigterm_time
Definition: pgarch.c:100

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 641 of file pgarch.c.

644{
645 char XLogArchiveStatusDir[MAXPGPATH];
646 DIR *rldir;
647 struct dirent *rlde;
648
649 /*
650 * If a directory scan was requested, clear the stored file names and
651 * proceed.
652 */
655
656 /*
657 * If we still have stored file names from the previous directory scan,
658 * try to return one of those. We check to make sure the status file is
659 * still present, as the archive_command for a previous file may have
660 * already marked it done.
661 */
662 while (arch_files->arch_files_size > 0)
663 {
664 struct stat st;
665 char status_file[MAXPGPATH];
666 char *arch_file;
667
670 StatusFilePath(status_file, arch_file, ".ready");
671
672 if (stat(status_file, &st) == 0)
673 {
674 strcpy(xlog, arch_file);
675 return true;
676 }
677 else if (errno != ENOENT)
680 errmsg("could not stat file \"%s\": %m", status_file)));
681 }
682
683 /* arch_heap is probably empty, but let's make sure */
685
686 /*
687 * Open the archive status directory and read through the list of files
688 * with the .ready suffix, looking for the earliest files.
689 */
690 snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
691 rldir = AllocateDir(XLogArchiveStatusDir);
692
693 while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
694 {
695 int basenamelen = (int) strlen(rlde->d_name) - 6;
696 char basename[MAX_XFN_CHARS + 1];
697 char *arch_file;
698
699 /* Ignore entries with unexpected number of characters */
700 if (basenamelen < MIN_XFN_CHARS ||
701 basenamelen > MAX_XFN_CHARS)
702 continue;
703
704 /* Ignore entries with unexpected characters */
705 if (strspn(rlde->d_name, VALID_XFN_CHARS) < basenamelen)
706 continue;
707
708 /* Ignore anything not suffixed with .ready */
709 if (strcmp(rlde->d_name + basenamelen, ".ready") != 0)
710 continue;
711
712 /* Truncate off the .ready */
713 memcpy(basename, rlde->d_name, basenamelen);
714 basename[basenamelen] = '\0';
715
716 /*
717 * Store the file in our max-heap if it has a high enough priority.
718 */
720 {
721 /* If the heap isn't full yet, quickly add it. */
723 strcpy(arch_file, basename);
725
726 /* If we just filled the heap, make it a valid one. */
729 }
731 CStringGetDatum(basename), NULL) > 0)
732 {
733 /*
734 * Remove the lowest priority file and add the current one to the
735 * heap.
736 */
738 strcpy(arch_file, basename);
740 }
741 }
742 FreeDir(rldir);
743
744 /* If no files were found, simply return. */
745 if (arch_files->arch_heap->bh_size == 0)
746 return false;
747
748 /*
749 * If we didn't fill the heap, we didn't make it a valid one. Do that
750 * now.
751 */
754
755 /*
756 * Fill arch_files array with the files to archive in ascending order of
757 * priority.
758 */
760 for (int i = 0; i < arch_files->arch_files_size; i++)
762
763 /* Return the highest priority file. */
766
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:330
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
int FreeDir(DIR *dir)
Definition: fd.c:2983
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2865
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2931
int i
Definition: isn.c:72
static PgArchData * PgArch
Definition: pgarch.c:101
static int ready_file_comparator(Datum a, Datum b, void *arg)
Definition: pgarch.c:777
#define NUM_FILES_PER_DIRECTORY_SCAN
Definition: pgarch.c:79
#define MIN_XFN_CHARS
Definition: pgarch.h:25
#define VALID_XFN_CHARS
Definition: pgarch.h:27
static char * DatumGetCString(Datum X)
Definition: postgres.h:340
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
Definition: dirent.c:26
pg_atomic_uint32 force_dir_scan
Definition: pgarch.c:89
char arch_filenames[NUM_FILES_PER_DIRECTORY_SCAN][MAX_XFN_CHARS+1]
Definition: pgarch.c:127
char * arch_files[NUM_FILES_PER_DIRECTORY_SCAN]
Definition: pgarch.c:125
binaryheap * arch_heap
Definition: pgarch.c:123
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, 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, pg_atomic_exchange_u32(), PgArch, ReadDir(), ready_file_comparator(), snprintf, 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 295 of file pgarch.c.

298{
299 /* set flag to do a final cycle and shut down afterwards */
300 ready_to_stop = true;

References MyLatch, ready_to_stop, and SetLatch().

Referenced by PgArchiverMain().

◆ PgArchCanRestart()

bool PgArchCanRestart ( void  )

Definition at line 195 of file pgarch.c.

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

References PGARCH_RESTART_INTERVAL.

Referenced by LaunchMissingBackgroundProcesses().

◆ PgArchForceDirScan()

void PgArchForceDirScan ( void  )

Definition at line 800 of file pgarch.c.

803{

References PgArchData::force_dir_scan, pg_atomic_write_membarrier_u32(), and PgArch.

Referenced by XLogArchiveNotify().

◆ PgArchiverMain()

void PgArchiverMain ( char *  startup_data,
size_t  startup_data_len 
)

Definition at line 215 of file pgarch.c.

218{
219 Assert(startup_data_len == 0);
220
223
224 /*
225 * Ignore all signals usually bound to some action in the postmaster,
226 * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
227 */
229 pqsignal(SIGINT, SIG_IGN);
231 /* SIGQUIT handler was already set up by InitPostmasterChild */
232 pqsignal(SIGALRM, SIG_IGN);
233 pqsignal(SIGPIPE, SIG_IGN);
236
237 /* Reset some signals that are accepted by postmaster but not here */
238 pqsignal(SIGCHLD, SIG_DFL);
239
240 /* Unblock signals (they were blocked when the postmaster forked us) */
241 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
242
243 /* We shouldn't be launched unnecessarily. */
245
246 /* Arrange to clean up at archiver exit */
248
249 /*
250 * Advertise our proc number so that backends can use our latch to wake us
251 * up while we're sleeping.
252 */
254
255 /* Create workspace for pgarch_readyXlog() */
256 arch_files = palloc(sizeof(struct arch_files_state));
258
259 /* Initialize our max-heap for prioritizing files to archive. */
262
263 /* Initialize our memory context. */
265 "archiver",
267
268 /* Load the archive_library. */
270
272
void AuxiliaryProcessMainCommon(void)
Definition: auxprocess.c:39
sigset_t UnBlockSig
Definition: pqsignal.c:22
binaryheap * binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
Definition: binaryheap.c:39
#define Assert(condition)
Definition: c.h:815
ProcNumber MyProcNumber
Definition: globals.c:89
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:105
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * palloc(Size size)
Definition: mcxt.c:1317
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
@ B_ARCHIVER
Definition: miscadmin.h:360
BackendType MyBackendType
Definition: miscinit.c:64
static void pgarch_die(int code, Datum arg)
Definition: pgarch.c:843
static void pgarch_MainLoop(void)
Definition: pgarch.c:308
static void pgarch_waken_stop(SIGNAL_ARGS)
Definition: pgarch.c:295
static void LoadArchiveLibrary(void)
Definition: pgarch.c:909
#define pqsignal
Definition: port.h:520
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:671
int pgprocno
Definition: pgarch.c:84
#define SIGCHLD
Definition: win32_port.h:168
#define SIGHUP
Definition: win32_port.h:158
#define SIGPIPE
Definition: win32_port.h:163
#define SIGUSR1
Definition: win32_port.h:170
#define SIGALRM
Definition: win32_port.h:164
#define SIGUSR2
Definition: win32_port.h:171
#define XLogArchivingActive()
Definition: xlog.h:99

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, arch_files, arch_files_state::arch_files_size, arch_files_state::arch_heap, archive_context, Assert, AuxiliaryProcessMainCommon(), B_ARCHIVER, binaryheap_allocate(), LoadArchiveLibrary(), MyBackendType, MyProcNumber, NUM_FILES_PER_DIRECTORY_SCAN, on_shmem_exit(), palloc(), PgArch, pgarch_die(), pgarch_MainLoop(), pgarch_waken_stop(), PgArchData::pgprocno, pqsignal, proc_exit(), procsignal_sigusr1_handler(), ready_file_comparator(), SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, TopMemoryContext, UnBlockSig, and XLogArchivingActive.

◆ PgArchShmemInit()

void PgArchShmemInit ( void  )

Definition at line 166 of file pgarch.c.

169{
170 bool found;
171
172 PgArch = (PgArchData *)
173 ShmemInitStruct("Archiver Data", PgArchShmemSize(), &found);
174
175 if (!found)
176 {
177 /* First time through, so initialize */
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:221
#define MemSet(start, val, len)
Definition: c.h:977
Size PgArchShmemSize(void)
Definition: pgarch.c:155
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382

References PgArchData::force_dir_scan, INVALID_PROC_NUMBER, MemSet, pg_atomic_init_u32(), PgArch, PgArchShmemSize(), PgArchData::pgprocno, and ShmemInitStruct().

Referenced by CreateOrAttachShmemStructs().

◆ PgArchShmemSize()

Size PgArchShmemSize ( void  )

Definition at line 155 of file pgarch.c.

158{
159 Size size = 0;
160
161 size = add_size(size, sizeof(PgArchData));
162
size_t Size
Definition: c.h:562
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
static pg_noinline void Size size
Definition: slab.c:607

References add_size(), and size.

Referenced by CalculateShmemSize(), and PgArchShmemInit().

◆ PgArchWakeup()

void PgArchWakeup ( void  )

Definition at line 278 of file pgarch.c.

281{
282 int arch_pgprocno = PgArch->pgprocno;
283
284 /*
285 * We don't acquire ProcArrayLock here. It's actually fine because
286 * procLatch isn't ever freed, so we just can potentially set the wrong
287 * process' (or no process') latch. Even in that case the archiver will
288 * be relaunched shortly and will start archiving.
289 */
290 if (arch_pgprocno != INVALID_PROC_NUMBER)

References PROC_HDR::allProcs, INVALID_PROC_NUMBER, 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 777 of file pgarch.c.

780{
781 char *a_str = DatumGetCString(a);
782 char *b_str = DatumGetCString(b);
783 bool a_history = IsTLHistoryFileName(a_str);
784 bool b_history = IsTLHistoryFileName(b_str);
785
786 /* Timeline history files always have the highest priority. */
787 if (a_history != b_history)
788 return a_history ? -1 : 1;
789
790 /* Priority is given to older files. */
int b
Definition: isn.c:69
int a
Definition: isn.c:68
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 130 of file pgarch.c.

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

◆ arch_module_check_errdetail_string

char* arch_module_check_errdetail_string

Definition at line 93 of file pgarch.c.

Referenced by pgarch_ArchiverCopyLoop().

◆ archive_context

MemoryContext archive_context
static

Definition at line 104 of file pgarch.c.

Referenced by pgarch_archiveXlog(), 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 100 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 135 of file pgarch.c.

Referenced by pgarch_MainLoop(), and pgarch_waken_stop().

◆ XLogArchiveLibrary

char* XLogArchiveLibrary = ""

Definition at line 92 of file pgarch.c.

Referenced by HandlePgArchInterrupts(), and LoadArchiveLibrary().