PostgreSQL Source Code  git master
pgarch.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pgarch.c
4  *
5  * PostgreSQL WAL archiver
6  *
7  * All functions relating to archiver are included here
8  *
9  * - All functions executed by archiver process
10  *
11  * - archiver is forked from postmaster, and the two
12  * processes then communicate using signals. All functions
13  * executed by postmaster are included in this file.
14  *
15  * Initial author: Simon Riggs simon@2ndquadrant.com
16  *
17  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  *
21  * IDENTIFICATION
22  * src/backend/postmaster/pgarch.c
23  *
24  *-------------------------------------------------------------------------
25  */
26 #include "postgres.h"
27 
28 #include <time.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 
32 #include "access/xlog.h"
33 #include "access/xlog_internal.h"
34 #include "lib/binaryheap.h"
35 #include "libpq/pqsignal.h"
36 #include "pgstat.h"
37 #include "postmaster/interrupt.h"
38 #include "postmaster/pgarch.h"
39 #include "storage/fd.h"
40 #include "storage/ipc.h"
41 #include "storage/latch.h"
42 #include "storage/pmsignal.h"
43 #include "storage/proc.h"
44 #include "storage/procsignal.h"
45 #include "storage/shmem.h"
46 #include "storage/spin.h"
47 #include "utils/guc.h"
48 #include "utils/memutils.h"
49 #include "utils/ps_status.h"
50 
51 
52 /* ----------
53  * Timer definitions.
54  * ----------
55  */
56 #define PGARCH_AUTOWAKE_INTERVAL 60 /* How often to force a poll of the
57  * archive status directory; in seconds. */
58 #define PGARCH_RESTART_INTERVAL 10 /* How often to attempt to restart a
59  * failed archiver; in seconds. */
60 
61 /*
62  * Maximum number of retries allowed when attempting to archive a WAL
63  * file.
64  */
65 #define NUM_ARCHIVE_RETRIES 3
66 
67 /*
68  * Maximum number of retries allowed when attempting to remove an
69  * orphan archive status file.
70  */
71 #define NUM_ORPHAN_CLEANUP_RETRIES 3
72 
73 /*
74  * Maximum number of .ready files to gather per directory scan.
75  */
76 #define NUM_FILES_PER_DIRECTORY_SCAN 64
77 
78 /* Shared memory area for archiver process */
79 typedef struct PgArchData
80 {
81  int pgprocno; /* pgprocno of archiver process */
82 
83  /*
84  * Forces a directory scan in pgarch_readyXlog(). Protected by arch_lck.
85  */
87 
90 
91 char *XLogArchiveLibrary = "";
92 
93 
94 /* ----------
95  * Local data
96  * ----------
97  */
98 static time_t last_sigterm_time = 0;
99 static PgArchData *PgArch = NULL;
101 
102 
103 /*
104  * Stuff for tracking multiple files to archive from each scan of
105  * archive_status. Minimizing the number of directory scans when there are
106  * many files to archive can significantly improve archival rate.
107  *
108  * arch_heap is a max-heap that is used during the directory scan to track
109  * the highest-priority files to archive. After the directory scan
110  * completes, the file names are stored in ascending order of priority in
111  * arch_files. pgarch_readyXlog() returns files from arch_files until it
112  * is empty, at which point another directory scan must be performed.
113  *
114  * We only need this data in the archiver process, so make it a palloc'd
115  * struct rather than a bunch of static arrays.
116  */
118 {
120  int arch_files_size; /* number of live entries in arch_files[] */
122  /* buffers underlying heap, and later arch_files[], entries: */
124 };
125 
126 static struct arch_files_state *arch_files = NULL;
127 
128 /*
129  * Flags set by interrupt handlers for later service in the main loop.
130  */
131 static volatile sig_atomic_t ready_to_stop = false;
132 
133 /* ----------
134  * Local function forward declarations
135  * ----------
136  */
137 static void pgarch_waken_stop(SIGNAL_ARGS);
138 static void pgarch_MainLoop(void);
139 static void pgarch_ArchiverCopyLoop(void);
140 static bool pgarch_archiveXlog(char *xlog);
141 static bool pgarch_readyXlog(char *xlog);
142 static void pgarch_archiveDone(char *xlog);
143 static void pgarch_die(int code, Datum arg);
144 static void HandlePgArchInterrupts(void);
145 static int ready_file_comparator(Datum a, Datum b, void *arg);
146 static void LoadArchiveLibrary(void);
147 static void pgarch_call_module_shutdown_cb(int code, Datum arg);
148 
149 /* Report shared memory space needed by PgArchShmemInit */
150 Size
151 PgArchShmemSize(void)
152 {
153  Size size = 0;
154 
155  size = add_size(size, sizeof(PgArchData));
156 
157  return size;
158 }
159 
160 /* Allocate and initialize archiver-related shared memory */
161 void
162 PgArchShmemInit(void)
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 */
175  }
176 }
177 
178 /*
179  * PgArchCanRestart
180  *
181  * Return true and archiver is allowed to restart if enough time has
182  * passed since it was launched last to reach PGARCH_RESTART_INTERVAL.
183  * Otherwise return false.
184  *
185  * This is a safety valve to protect against continuous respawn attempts if the
186  * archiver is dying immediately at launch. Note that since we will retry to
187  * launch the archiver from the postmaster main loop, we will get another
188  * chance later.
189  */
190 bool
191 PgArchCanRestart(void)
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;
205  return true;
206 }
207 
208 
209 /* Main entry point for archiver process */
210 void
211 PgArchiverMain(void)
212 {
213  /*
214  * Ignore all signals usually bound to some action in the postmaster,
215  * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
216  */
218  pqsignal(SIGINT, SIG_IGN);
220  /* SIGQUIT handler was already set up by InitPostmasterChild */
225 
226  /* Reset some signals that are accepted by postmaster but not here */
228 
229  /* Unblock signals (they were blocked when the postmaster forked us) */
231 
232  /* We shouldn't be launched unnecessarily. */
234 
235  /* Arrange to clean up at archiver exit */
237 
238  /*
239  * Advertise our pgprocno so that backends can use our latch to wake us up
240  * while we're sleeping.
241  */
243 
244  /* Create workspace for pgarch_readyXlog() */
245  arch_files = palloc(sizeof(struct arch_files_state));
247 
248  /* Initialize our max-heap for prioritizing files to archive. */
250  ready_file_comparator, NULL);
251 
252  /* Load the archive_library. */
254 
255  pgarch_MainLoop();
256 
257  proc_exit(0);
258 }
259 
260 /*
261  * Wake up the archiver
262  */
263 void
264 PgArchWakeup(void)
265 {
266  int arch_pgprocno = PgArch->pgprocno;
267 
268  /*
269  * We don't acquire ProcArrayLock here. It's actually fine because
270  * procLatch isn't ever freed, so we just can potentially set the wrong
271  * process' (or no process') latch. Even in that case the archiver will
272  * be relaunched shortly and will start archiving.
273  */
274  if (arch_pgprocno != INVALID_PGPROCNO)
275  SetLatch(&ProcGlobal->allProcs[arch_pgprocno].procLatch);
276 }
277 
278 
279 /* SIGUSR2 signal handler for archiver process */
280 static void
282 {
283  int save_errno = errno;
284 
285  /* set flag to do a final cycle and shut down afterwards */
286  ready_to_stop = true;
287  SetLatch(MyLatch);
288 
289  errno = save_errno;
290 }
291 
292 /*
293  * pgarch_MainLoop
294  *
295  * Main loop for archiver
296  */
297 static void
298 pgarch_MainLoop(void)
299 {
300  pg_time_t last_copy_time = 0;
301  bool time_to_stop;
302 
303  /*
304  * There shouldn't be anything for the archiver to do except to wait for a
305  * signal ... however, the archiver exists to protect our data, so she
306  * wakes up occasionally to allow herself to be proactive.
307  */
308  do
309  {
311 
312  /* When we get SIGUSR2, we do one more archive cycle, then exit */
314 
315  /* Check for barrier events and config update */
317 
318  /*
319  * If we've gotten SIGTERM, we normally just sit and do nothing until
320  * SIGUSR2 arrives. However, that means a random SIGTERM would
321  * disable archiving indefinitely, which doesn't seem like a good
322  * idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
323  * that the postmaster can start a new archiver if needed.
324  */
326  {
327  time_t curtime = time(NULL);
328 
329  if (last_sigterm_time == 0)
330  last_sigterm_time = curtime;
331  else if ((unsigned int) (curtime - last_sigterm_time) >=
332  (unsigned int) 60)
333  break;
334  }
335 
336  /* Do what we're here for */
338  last_copy_time = time(NULL);
339 
340  /*
341  * Sleep until a signal is received, or until a poll is forced by
342  * PGARCH_AUTOWAKE_INTERVAL having passed since last_copy_time, or
343  * until postmaster dies.
344  */
345  if (!time_to_stop) /* Don't wait during last iteration */
346  {
347  pg_time_t curtime = (pg_time_t) time(NULL);
348  int timeout;
349 
350  timeout = PGARCH_AUTOWAKE_INTERVAL - (curtime - last_copy_time);
351  if (timeout > 0)
352  {
353  int rc;
354 
355  rc = WaitLatch(MyLatch,
357  timeout * 1000L,
359  if (rc & WL_POSTMASTER_DEATH)
360  time_to_stop = true;
361  }
362  }
363 
364  /*
365  * The archiver quits either when the postmaster dies (not expected)
366  * or after completing one more archiving cycle after receiving
367  * SIGUSR2.
368  */
369  } while (!time_to_stop);
370 }
371 
372 /*
373  * pgarch_ArchiverCopyLoop
374  *
375  * Archives all outstanding xlogs then returns
376  */
377 static void
379 {
380  char xlog[MAX_XFN_CHARS + 1];
381 
382  /* force directory scan in the first call to pgarch_readyXlog() */
384 
385  /*
386  * loop through all xlogs with archive_status of .ready and archive
387  * them...mostly we expect this to be a single file, though it is possible
388  * some backend will add files onto the list of those that need archiving
389  * while we are still copying earlier archives
390  */
391  while (pgarch_readyXlog(xlog))
392  {
393  int failures = 0;
394  int failures_orphan = 0;
395 
396  for (;;)
397  {
398  struct stat stat_buf;
399  char pathname[MAXPGPATH];
400 
401  /*
402  * Do not initiate any more archive commands after receiving
403  * SIGTERM, nor after the postmaster has died unexpectedly. The
404  * first condition is to try to keep from having init SIGKILL the
405  * command, and the second is to avoid conflicts with another
406  * archiver spawned by a newer postmaster.
407  */
409  return;
410 
411  /*
412  * Check for barrier events and config update. This is so that
413  * we'll adopt a new setting for archive_command as soon as
414  * possible, even if there is a backlog of files to be archived.
415  */
417 
418  /* can't do anything if not configured ... */
419  if (ArchiveContext.check_configured_cb != NULL &&
421  {
423  (errmsg("archive_mode enabled, yet archiving is not configured")));
424  return;
425  }
426 
427  /*
428  * Since archive status files are not removed in a durable manner,
429  * a system crash could leave behind .ready files for WAL segments
430  * that have already been recycled or removed. In this case,
431  * simply remove the orphan status file and move on. unlink() is
432  * used here as even on subsequent crashes the same orphan files
433  * would get removed, so there is no need to worry about
434  * durability.
435  */
436  snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
437  if (stat(pathname, &stat_buf) != 0 && errno == ENOENT)
438  {
439  char xlogready[MAXPGPATH];
440 
441  StatusFilePath(xlogready, xlog, ".ready");
442  if (unlink(xlogready) == 0)
443  {
445  (errmsg("removed orphan archive status file \"%s\"",
446  xlogready)));
447 
448  /* leave loop and move to the next status file */
449  break;
450  }
451 
452  if (++failures_orphan >= NUM_ORPHAN_CLEANUP_RETRIES)
453  {
455  (errmsg("removal of orphan archive status file \"%s\" failed too many times, will try again later",
456  xlogready)));
457 
458  /* give up cleanup of orphan status files */
459  return;
460  }
461 
462  /* wait a bit before retrying */
463  pg_usleep(1000000L);
464  continue;
465  }
466 
467  if (pgarch_archiveXlog(xlog))
468  {
469  /* successful */
470  pgarch_archiveDone(xlog);
471 
472  /*
473  * Tell the cumulative stats system about the WAL file that we
474  * successfully archived
475  */
476  pgstat_report_archiver(xlog, false);
477 
478  break; /* out of inner retry loop */
479  }
480  else
481  {
482  /*
483  * Tell the cumulative stats system about the WAL file that we
484  * failed to archive
485  */
486  pgstat_report_archiver(xlog, true);
487 
488  if (++failures >= NUM_ARCHIVE_RETRIES)
489  {
491  (errmsg("archiving write-ahead log file \"%s\" failed too many times, will try again later",
492  xlog)));
493  return; /* give up archiving for now */
494  }
495  pg_usleep(1000000L); /* wait a bit before retrying */
496  }
497  }
498  }
499 }
500 
501 /*
502  * pgarch_archiveXlog
503  *
504  * Invokes archive_file_cb to copy one archive file to wherever it should go
505  *
506  * Returns true if successful
507  */
508 static bool
509 pgarch_archiveXlog(char *xlog)
510 {
511  char pathname[MAXPGPATH];
512  char activitymsg[MAXFNAMELEN + 16];
513  bool ret;
514 
515  snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
516 
517  /* Report archive activity in PS display */
518  snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
519  set_ps_display(activitymsg);
520 
521  ret = ArchiveContext.archive_file_cb(xlog, pathname);
522  if (ret)
523  snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
524  else
525  snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
526  set_ps_display(activitymsg);
527 
528  return ret;
529 }
530 
531 /*
532  * pgarch_readyXlog
533  *
534  * Return name of the oldest xlog file that has not yet been archived.
535  * No notification is set that file archiving is now in progress, so
536  * this would need to be extended if multiple concurrent archival
537  * tasks were created. If a failure occurs, we will completely
538  * re-copy the file at the next available opportunity.
539  *
540  * It is important that we return the oldest, so that we archive xlogs
541  * in order that they were written, for two reasons:
542  * 1) to maintain the sequential chain of xlogs required for recovery
543  * 2) because the oldest ones will sooner become candidates for
544  * recycling at time of checkpoint
545  *
546  * NOTE: the "oldest" comparison will consider any .history file to be older
547  * than any other file except another .history file. Segments on a timeline
548  * with a smaller ID will be older than all segments on a timeline with a
549  * larger ID; the net result being that past timelines are given higher
550  * priority for archiving. This seems okay, or at least not obviously worth
551  * changing.
552  */
553 static bool
554 pgarch_readyXlog(char *xlog)
555 {
556  char XLogArchiveStatusDir[MAXPGPATH];
557  DIR *rldir;
558  struct dirent *rlde;
559  bool force_dir_scan;
560 
561  /*
562  * If a directory scan was requested, clear the stored file names and
563  * proceed.
564  */
566  force_dir_scan = PgArch->force_dir_scan;
567  PgArch->force_dir_scan = false;
569 
570  if (force_dir_scan)
572 
573  /*
574  * If we still have stored file names from the previous directory scan,
575  * try to return one of those. We check to make sure the status file is
576  * still present, as the archive_command for a previous file may have
577  * already marked it done.
578  */
579  while (arch_files->arch_files_size > 0)
580  {
581  struct stat st;
582  char status_file[MAXPGPATH];
583  char *arch_file;
584 
587  StatusFilePath(status_file, arch_file, ".ready");
588 
589  if (stat(status_file, &st) == 0)
590  {
591  strcpy(xlog, arch_file);
592  return true;
593  }
594  else if (errno != ENOENT)
595  ereport(ERROR,
597  errmsg("could not stat file \"%s\": %m", status_file)));
598  }
599 
600  /* arch_heap is probably empty, but let's make sure */
602 
603  /*
604  * Open the archive status directory and read through the list of files
605  * with the .ready suffix, looking for the earliest files.
606  */
607  snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
608  rldir = AllocateDir(XLogArchiveStatusDir);
609 
610  while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
611  {
612  int basenamelen = (int) strlen(rlde->d_name) - 6;
613  char basename[MAX_XFN_CHARS + 1];
614  char *arch_file;
615 
616  /* Ignore entries with unexpected number of characters */
617  if (basenamelen < MIN_XFN_CHARS ||
618  basenamelen > MAX_XFN_CHARS)
619  continue;
620 
621  /* Ignore entries with unexpected characters */
622  if (strspn(rlde->d_name, VALID_XFN_CHARS) < basenamelen)
623  continue;
624 
625  /* Ignore anything not suffixed with .ready */
626  if (strcmp(rlde->d_name + basenamelen, ".ready") != 0)
627  continue;
628 
629  /* Truncate off the .ready */
630  memcpy(basename, rlde->d_name, basenamelen);
631  basename[basenamelen] = '\0';
632 
633  /*
634  * Store the file in our max-heap if it has a high enough priority.
635  */
637  {
638  /* If the heap isn't full yet, quickly add it. */
640  strcpy(arch_file, basename);
642 
643  /* If we just filled the heap, make it a valid one. */
646  }
648  CStringGetDatum(basename), NULL) > 0)
649  {
650  /*
651  * Remove the lowest priority file and add the current one to the
652  * heap.
653  */
655  strcpy(arch_file, basename);
657  }
658  }
659  FreeDir(rldir);
660 
661  /* If no files were found, simply return. */
662  if (arch_files->arch_heap->bh_size == 0)
663  return false;
664 
665  /*
666  * If we didn't fill the heap, we didn't make it a valid one. Do that
667  * now.
668  */
671 
672  /*
673  * Fill arch_files array with the files to archive in ascending order of
674  * priority.
675  */
677  for (int i = 0; i < arch_files->arch_files_size; i++)
679 
680  /* Return the highest priority file. */
683 
684  return true;
685 }
686 
687 /*
688  * ready_file_comparator
689  *
690  * Compares the archival priority of the given files to archive. If "a"
691  * has a higher priority than "b", a negative value will be returned. If
692  * "b" has a higher priority than "a", a positive value will be returned.
693  * If "a" and "b" have equivalent values, 0 will be returned.
694  */
695 static int
697 {
698  char *a_str = DatumGetCString(a);
699  char *b_str = DatumGetCString(b);
700  bool a_history = IsTLHistoryFileName(a_str);
701  bool b_history = IsTLHistoryFileName(b_str);
702 
703  /* Timeline history files always have the highest priority. */
704  if (a_history != b_history)
705  return a_history ? -1 : 1;
706 
707  /* Priority is given to older files. */
708  return strcmp(a_str, b_str);
709 }
710 
711 /*
712  * PgArchForceDirScan
713  *
714  * When called, the next call to pgarch_readyXlog() will perform a
715  * directory scan. This is useful for ensuring that important files such
716  * as timeline history files are archived as quickly as possible.
717  */
718 void
719 PgArchForceDirScan(void)
720 {
722  PgArch->force_dir_scan = true;
724 }
725 
726 /*
727  * pgarch_archiveDone
728  *
729  * Emit notification that an xlog file has been successfully archived.
730  * We do this by renaming the status file from NNN.ready to NNN.done.
731  * Eventually, a checkpoint process will notice this and delete both the
732  * NNN.done file and the xlog file itself.
733  */
734 static void
735 pgarch_archiveDone(char *xlog)
736 {
737  char rlogready[MAXPGPATH];
738  char rlogdone[MAXPGPATH];
739 
740  StatusFilePath(rlogready, xlog, ".ready");
741  StatusFilePath(rlogdone, xlog, ".done");
742 
743  /*
744  * To avoid extra overhead, we don't durably rename the .ready file to
745  * .done. Archive commands and libraries must gracefully handle attempts
746  * to re-archive files (e.g., if the server crashes just before this
747  * function is called), so it should be okay if the .ready file reappears
748  * after a crash.
749  */
750  if (rename(rlogready, rlogdone) < 0)
753  errmsg("could not rename file \"%s\" to \"%s\": %m",
754  rlogready, rlogdone)));
755 }
756 
757 
758 /*
759  * pgarch_die
760  *
761  * Exit-time cleanup handler
762  */
763 static void
764 pgarch_die(int code, Datum arg)
765 {
767 }
768 
769 /*
770  * Interrupt handler for WAL archiver process.
771  *
772  * This is called in the loops pgarch_MainLoop and pgarch_ArchiverCopyLoop.
773  * It checks for barrier events, config update and request for logging of
774  * memory contexts, but not shutdown request because how to handle
775  * shutdown request is different between those loops.
776  */
777 static void
779 {
782 
783  /* Perform logging of memory contexts of this process */
786 
788  {
789  char *archiveLib = pstrdup(XLogArchiveLibrary);
790  bool archiveLibChanged;
791 
792  ConfigReloadPending = false;
794 
795  if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
796  ereport(ERROR,
797  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
798  errmsg("both archive_command and archive_library set"),
799  errdetail("Only one of archive_command, archive_library may be set.")));
800 
801  archiveLibChanged = strcmp(XLogArchiveLibrary, archiveLib) != 0;
802  pfree(archiveLib);
803 
804  if (archiveLibChanged)
805  {
806  /*
807  * Ideally, we would simply unload the previous archive module and
808  * load the new one, but there is presently no mechanism for
809  * unloading a library (see the comment above
810  * internal_load_library()). To deal with this, we simply restart
811  * the archiver. The new archive module will be loaded when the
812  * new archiver process starts up. Note that this triggers the
813  * module's shutdown callback, if defined.
814  */
815  ereport(LOG,
816  (errmsg("restarting archiver process because value of "
817  "\"archive_library\" was changed")));
818 
819  proc_exit(0);
820  }
821  }
822 }
823 
824 /*
825  * LoadArchiveLibrary
826  *
827  * Loads the archiving callbacks into our local ArchiveContext.
828  */
829 static void
830 LoadArchiveLibrary(void)
831 {
832  ArchiveModuleInit archive_init;
833 
834  if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
835  ereport(ERROR,
836  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
837  errmsg("both archive_command and archive_library set"),
838  errdetail("Only one of archive_command, archive_library may be set.")));
839 
840  memset(&ArchiveContext, 0, sizeof(ArchiveModuleCallbacks));
841 
842  /*
843  * If shell archiving is enabled, use our special initialization function.
844  * Otherwise, load the library and call its _PG_archive_module_init().
845  */
846  if (XLogArchiveLibrary[0] == '\0')
847  archive_init = shell_archive_init;
848  else
849  archive_init = (ArchiveModuleInit)
851  "_PG_archive_module_init", false, NULL);
852 
853  if (archive_init == NULL)
854  ereport(ERROR,
855  (errmsg("archive modules have to define the symbol %s", "_PG_archive_module_init")));
856 
857  (*archive_init) (&ArchiveContext);
858 
859  if (ArchiveContext.archive_file_cb == NULL)
860  ereport(ERROR,
861  (errmsg("archive modules must register an archive callback")));
862 
864 }
865 
866 /*
867  * Call the shutdown callback of the loaded archive module, if defined.
868  */
869 static void
871 {
872  if (ArchiveContext.shutdown_cb != NULL)
874 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
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
binaryheap * binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
Definition: binaryheap.c:32
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
#define SIGNAL_ARGS
Definition: c.h:1281
#define MemSet(start, val, len)
Definition: c.h:953
size_t Size
Definition: c.h:541
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
int errcode_for_file_access(void)
Definition: elog.c:758
int errdetail(const char *fmt,...)
Definition: elog.c:1079
int errcode(int sqlerrcode)
Definition: elog.c:735
int errmsg(const char *fmt,...)
Definition: elog.c:946
#define LOG
Definition: elog.h:27
#define WARNING
Definition: elog.h:32
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2709
int FreeDir(DIR *dir)
Definition: fd.c:2761
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2643
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:38
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37
struct Latch * MyLatch
Definition: globals.c:58
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:108
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
void proc_exit(int code)
Definition: ipc.c:104
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int i
Definition: isn.c:73
void SetLatch(Latch *latch)
Definition: latch.c:591
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
Assert(fmt[strlen(fmt) - 1] !='\n')
char * pstrdup(const char *in)
Definition: mcxt.c:1483
void pfree(void *pointer)
Definition: mcxt.c:1306
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1172
void * palloc(Size size)
Definition: mcxt.c:1199
void * arg
#define MAXPGPATH
static volatile sig_atomic_t time_to_stop
Definition: pg_receivewal.c:48
static bool pgarch_archiveXlog(char *xlog)
Definition: pgarch.c:507
static void pgarch_die(int code, Datum arg)
Definition: pgarch.c:762
#define PGARCH_RESTART_INTERVAL
Definition: pgarch.c:57
bool PgArchCanRestart(void)
Definition: pgarch.c:189
static bool pgarch_readyXlog(char *xlog)
Definition: pgarch.c:552
static PgArchData * PgArch
Definition: pgarch.c:97
static void pgarch_MainLoop(void)
Definition: pgarch.c:296
#define NUM_ARCHIVE_RETRIES
Definition: pgarch.c:63
struct PgArchData PgArchData
static void pgarch_waken_stop(SIGNAL_ARGS)
Definition: pgarch.c:279
static volatile sig_atomic_t ready_to_stop
Definition: pgarch.c:129
void PgArchiverMain(void)
Definition: pgarch.c:209
static struct arch_files_state * arch_files
Definition: pgarch.c:124
char * XLogArchiveLibrary
Definition: pgarch.c:89
Size PgArchShmemSize(void)
Definition: pgarch.c:149
static void LoadArchiveLibrary(void)
Definition: pgarch.c:828
static void pgarch_ArchiverCopyLoop(void)
Definition: pgarch.c:376
static int ready_file_comparator(Datum a, Datum b, void *arg)
Definition: pgarch.c:694
#define NUM_FILES_PER_DIRECTORY_SCAN
Definition: pgarch.c:74
void PgArchForceDirScan(void)
Definition: pgarch.c:717
static ArchiveModuleCallbacks ArchiveContext
Definition: pgarch.c:98
static void HandlePgArchInterrupts(void)
Definition: pgarch.c:776
void PgArchShmemInit(void)
Definition: pgarch.c:160
static void pgarch_call_module_shutdown_cb(int code, Datum arg)
Definition: pgarch.c:868
void PgArchWakeup(void)
Definition: pgarch.c:262
static void pgarch_archiveDone(char *xlog)
Definition: pgarch.c:733
#define NUM_ORPHAN_CLEANUP_RETRIES
Definition: pgarch.c:69
#define PGARCH_AUTOWAKE_INTERVAL
Definition: pgarch.c:56
static time_t last_sigterm_time
Definition: pgarch.c:96
#define MIN_XFN_CHARS
Definition: pgarch.h:25
void(* ArchiveModuleInit)(ArchiveModuleCallbacks *cb)
Definition: pgarch.h:64
#define MAX_XFN_CHARS
Definition: pgarch.h:26
#define VALID_XFN_CHARS
Definition: pgarch.h:27
void pgstat_report_archiver(const char *xlog, bool failed)
int64 pg_time_t
Definition: pgtime.h:23
#define PostmasterIsAlive()
Definition: pmsignal.h:102
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define snprintf
Definition: port.h:238
static char * DatumGetCString(Datum X)
Definition: postgres.h:683
uintptr_t Datum
Definition: postgres.h:412
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:698
#define PG_SETMASK(mask)
Definition: pqsignal.h:18
#define INVALID_PGPROCNO
Definition: proc.h:85
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:467
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:638
void set_ps_display(const char *activity)
Definition: ps_status.c:342
int slock_t
Definition: s_lock.h:754
void shell_archive_init(ArchiveModuleCallbacks *cb)
Definition: shell_archive.c:29
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
void pg_usleep(long microsec)
Definition: signal.c:53
#define SpinLockInit(lock)
Definition: spin.h:60
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
PGPROC * MyProc
Definition: proc.c:68
PROC_HDR * ProcGlobal
Definition: proc.c:80
ArchiveFileCB archive_file_cb
Definition: pgarch.h:56
ArchiveShutdownCB shutdown_cb
Definition: pgarch.h:57
ArchiveCheckConfiguredCB check_configured_cb
Definition: pgarch.h:55
Definition: dirent.c:26
int pgprocno
Definition: proc.h:191
Latch procLatch
Definition: proc.h:170
PGPROC * allProcs
Definition: proc.h:362
int pgprocno
Definition: pgarch.c:79
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
int arch_files_size
Definition: pgarch.c:118
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
@ WAIT_EVENT_ARCHIVER_MAIN
Definition: wait_event.h:38
#define SIGCHLD
Definition: win32_port.h:186
#define SIGHUP
Definition: win32_port.h:176
#define stat
Definition: win32_port.h:286
#define SIG_DFL
Definition: win32_port.h:171
#define SIGPIPE
Definition: win32_port.h:181
#define SIGUSR1
Definition: win32_port.h:188
#define SIGALRM
Definition: win32_port.h:182
#define SIGUSR2
Definition: win32_port.h:189
#define SIG_IGN
Definition: win32_port.h:173
char * XLogArchiveCommand
Definition: xlog.c:123
#define XLogArchivingActive()
Definition: xlog.h:94
static bool IsTLHistoryFileName(const char *fname)
#define MAXFNAMELEN
#define XLOGDIR
static void StatusFilePath(char *path, const char *xlog, const char *suffix)