Branch data Line data Source code
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-2026, 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 "archive/archive_module.h"
35 : : #include "archive/shell_archive.h"
36 : : #include "lib/binaryheap.h"
37 : : #include "libpq/pqsignal.h"
38 : : #include "pgstat.h"
39 : : #include "postmaster/auxprocess.h"
40 : : #include "postmaster/interrupt.h"
41 : : #include "postmaster/pgarch.h"
42 : : #include "storage/condition_variable.h"
43 : : #include "storage/aio_subsys.h"
44 : : #include "storage/fd.h"
45 : : #include "storage/ipc.h"
46 : : #include "storage/latch.h"
47 : : #include "storage/pmsignal.h"
48 : : #include "storage/proc.h"
49 : : #include "storage/procsignal.h"
50 : : #include "storage/shmem.h"
51 : : #include "storage/subsystems.h"
52 : : #include "utils/guc.h"
53 : : #include "utils/memutils.h"
54 : : #include "utils/ps_status.h"
55 : : #include "utils/resowner.h"
56 : : #include "utils/timeout.h"
57 : : #include "utils/wait_event.h"
58 : :
59 : :
60 : : /* ----------
61 : : * Timer definitions.
62 : : * ----------
63 : : */
64 : : #define PGARCH_AUTOWAKE_INTERVAL 60 /* How often to force a poll of the
65 : : * archive status directory; in seconds. */
66 : : #define PGARCH_RESTART_INTERVAL 10 /* How often to attempt to restart a
67 : : * failed archiver; in seconds. */
68 : :
69 : : /*
70 : : * Maximum number of retries allowed when attempting to archive a WAL
71 : : * file.
72 : : */
73 : : #define NUM_ARCHIVE_RETRIES 3
74 : :
75 : : /*
76 : : * Maximum number of retries allowed when attempting to remove an
77 : : * orphan archive status file.
78 : : */
79 : : #define NUM_ORPHAN_CLEANUP_RETRIES 3
80 : :
81 : : /*
82 : : * Maximum number of .ready files to gather per directory scan.
83 : : */
84 : : #define NUM_FILES_PER_DIRECTORY_SCAN 64
85 : :
86 : : /* Shared memory area for archiver process */
87 : : typedef struct PgArchData
88 : : {
89 : : int pgprocno; /* proc number of archiver process */
90 : :
91 : : /*
92 : : * Forces a directory scan in pgarch_readyXlog().
93 : : */
94 : : pg_atomic_uint32 force_dir_scan;
95 : : } PgArchData;
96 : :
97 : : char *XLogArchiveLibrary = "";
98 : : char *arch_module_check_errdetail_string;
99 : :
100 : :
101 : : /* ----------
102 : : * Local data
103 : : * ----------
104 : : */
105 : : static time_t last_sigterm_time = 0;
106 : : static PgArchData *PgArch = NULL;
107 : : static const ArchiveModuleCallbacks *ArchiveCallbacks;
108 : : static ArchiveModuleState *archive_module_state;
109 : : static MemoryContext archive_context;
110 : :
111 : :
112 : : /*
113 : : * Stuff for tracking multiple files to archive from each scan of
114 : : * archive_status. Minimizing the number of directory scans when there are
115 : : * many files to archive can significantly improve archival rate.
116 : : *
117 : : * arch_heap is a max-heap that is used during the directory scan to track
118 : : * the highest-priority files to archive. After the directory scan
119 : : * completes, the file names are stored in ascending order of priority in
120 : : * arch_files. pgarch_readyXlog() returns files from arch_files until it
121 : : * is empty, at which point another directory scan must be performed.
122 : : *
123 : : * We only need this data in the archiver process, so make it a palloc'd
124 : : * struct rather than a bunch of static arrays.
125 : : */
126 : : struct arch_files_state
127 : : {
128 : : binaryheap *arch_heap;
129 : : int arch_files_size; /* number of live entries in arch_files[] */
130 : : char *arch_files[NUM_FILES_PER_DIRECTORY_SCAN];
131 : : /* buffers underlying heap, and later arch_files[], entries: */
132 : : char arch_filenames[NUM_FILES_PER_DIRECTORY_SCAN][MAX_XFN_CHARS + 1];
133 : : };
134 : :
135 : : static struct arch_files_state *arch_files = NULL;
136 : :
137 : : /*
138 : : * Flags set by interrupt handlers for later service in the main loop.
139 : : */
140 : : static volatile sig_atomic_t ready_to_stop = false;
141 : :
142 : : /* ----------
143 : : * Local function forward declarations
144 : : * ----------
145 : : */
146 : : static void pgarch_waken_stop(SIGNAL_ARGS);
147 : : static void pgarch_MainLoop(void);
148 : : static void pgarch_ArchiverCopyLoop(void);
149 : : static bool pgarch_archiveXlog(char *xlog);
150 : : static bool pgarch_readyXlog(char *xlog);
151 : : static void pgarch_archiveDone(char *xlog);
152 : : static void pgarch_die(int code, Datum arg);
153 : : static void ProcessPgArchInterrupts(void);
154 : : static int ready_file_comparator(Datum a, Datum b, void *arg);
155 : : static void LoadArchiveLibrary(void);
156 : : static void pgarch_call_module_shutdown_cb(int code, Datum arg);
157 : :
158 : : static void PgArchShmemRequest(void *arg);
159 : : static void PgArchShmemInit(void *arg);
160 : :
161 : : const ShmemCallbacks PgArchShmemCallbacks = {
162 : : .request_fn = PgArchShmemRequest,
163 : : .init_fn = PgArchShmemInit,
164 : : };
165 : :
166 : : /* Register shared memory space needed by the archiver */
167 : : static void
168 : 1355 : PgArchShmemRequest(void *arg)
169 : : {
170 : 1355 : ShmemRequestStruct(.name = "Archiver Data",
171 : : .size = sizeof(PgArchData),
172 : : .ptr = (void **) &PgArch,
173 : : );
174 : 1355 : }
175 : :
176 : : /* Initialize archiver-related shared memory */
177 : : static void
178 : 1351 : PgArchShmemInit(void *arg)
179 : : {
180 [ + - + - : 2702 : MemSet(PgArch, 0, sizeof(PgArchData));
+ - + - +
+ ]
181 : 1351 : PgArch->pgprocno = INVALID_PROC_NUMBER;
182 : 1351 : pg_atomic_init_u32(&PgArch->force_dir_scan, 0);
183 : 1351 : }
184 : :
185 : : /*
186 : : * PgArchCanRestart
187 : : *
188 : : * Return true, indicating archiver is allowed to restart, if enough time has
189 : : * passed since it was last launched to reach PGARCH_RESTART_INTERVAL.
190 : : * Otherwise return false.
191 : : *
192 : : * This is a safety valve to protect against continuous respawn attempts if the
193 : : * archiver is dying immediately at launch. Note that since we will retry to
194 : : * launch the archiver from the postmaster main loop, we will get another
195 : : * chance later.
196 : : */
197 : : bool
198 : 58 : PgArchCanRestart(void)
199 : : {
200 : : static time_t last_pgarch_start_time = 0;
201 : 58 : time_t curtime = time(NULL);
202 : :
203 : : /*
204 : : * If first time through, or time somehow went backwards, always update
205 : : * last_pgarch_start_time to match the current clock and allow archiver
206 : : * start. Otherwise allow it only once enough time has elapsed.
207 : : */
208 [ - + ]: 58 : if (last_pgarch_start_time == 0 ||
209 [ # # ]: 0 : curtime < last_pgarch_start_time ||
210 [ # # ]: 0 : curtime - last_pgarch_start_time >= PGARCH_RESTART_INTERVAL)
211 : : {
212 : 58 : last_pgarch_start_time = curtime;
213 : 58 : return true;
214 : : }
215 : 0 : return false;
216 : : }
217 : :
218 : :
219 : : /* Main entry point for archiver process */
220 : : void
221 : 19 : PgArchiverMain(const void *startup_data, size_t startup_data_len)
222 : : {
223 : : Assert(startup_data_len == 0);
224 : :
225 : 19 : AuxiliaryProcessMainCommon();
226 : :
227 : : /*
228 : : * Ignore all signals usually bound to some action in the postmaster,
229 : : * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
230 : : */
231 : 19 : pqsignal(SIGHUP, SignalHandlerForConfigReload);
232 : 19 : pqsignal(SIGINT, PG_SIG_IGN);
233 : 19 : pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
234 : : /* SIGQUIT handler was already set up by InitPostmasterChild */
235 : 19 : pqsignal(SIGALRM, PG_SIG_IGN);
236 : 19 : pqsignal(SIGPIPE, PG_SIG_IGN);
237 : 19 : pqsignal(SIGUSR1, procsignal_sigusr1_handler);
238 : 19 : pqsignal(SIGUSR2, pgarch_waken_stop);
239 : :
240 : : /* Unblock signals (they were blocked when the postmaster forked us) */
241 : 19 : sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
242 : :
243 : : /* We shouldn't be launched unnecessarily. */
244 : : Assert(XLogArchivingActive());
245 : :
246 : : /* Arrange to clean up at archiver exit */
247 : 19 : on_shmem_exit(pgarch_die, 0);
248 : :
249 : : /*
250 : : * Advertise our proc number so that backends can use our latch to wake us
251 : : * up while we're sleeping.
252 : : */
253 : 19 : PgArch->pgprocno = MyProcNumber;
254 : :
255 : : /* Create workspace for pgarch_readyXlog() */
256 : 19 : arch_files = palloc_object(struct arch_files_state);
257 : 19 : arch_files->arch_files_size = 0;
258 : :
259 : : /* Initialize our max-heap for prioritizing files to archive. */
260 : 19 : arch_files->arch_heap = binaryheap_allocate(NUM_FILES_PER_DIRECTORY_SCAN,
261 : : ready_file_comparator, NULL);
262 : :
263 : : /* Initialize our memory context. */
264 : 19 : archive_context = AllocSetContextCreate(TopMemoryContext,
265 : : "archiver",
266 : : ALLOCSET_DEFAULT_SIZES);
267 : :
268 : : /* Load the archive_library. */
269 : 19 : LoadArchiveLibrary();
270 : :
271 : 19 : pgarch_MainLoop();
272 : :
273 : 19 : proc_exit(0);
274 : : }
275 : :
276 : : /*
277 : : * Wake up the archiver
278 : : */
279 : : void
280 : 490 : PgArchWakeup(void)
281 : : {
282 : 490 : 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 [ + + ]: 490 : if (arch_pgprocno != INVALID_PROC_NUMBER)
291 : 468 : SetLatch(&GetPGProcByNumber(arch_pgprocno)->procLatch);
292 : 490 : }
293 : :
294 : :
295 : : /* SIGUSR2 signal handler for archiver process */
296 : : static void
297 : 19 : pgarch_waken_stop(SIGNAL_ARGS)
298 : : {
299 : : /* set flag to do a final cycle and shut down afterwards */
300 : 19 : ready_to_stop = true;
301 : 19 : SetLatch(MyLatch);
302 : 19 : }
303 : :
304 : : /*
305 : : * pgarch_MainLoop
306 : : *
307 : : * Main loop for archiver
308 : : */
309 : : static void
310 : 19 : pgarch_MainLoop(void)
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 : : {
321 : 87 : ResetLatch(MyLatch);
322 : :
323 : : /* When we get SIGUSR2, we do one more archive cycle, then exit */
324 : 87 : time_to_stop = ready_to_stop;
325 : :
326 : : /* Check for barrier events and config update */
327 : 87 : ProcessPgArchInterrupts();
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. Also exit
335 : : * if time unexpectedly goes backward.
336 : : */
337 [ - + ]: 87 : if (ShutdownRequestPending)
338 : : {
339 : 0 : time_t curtime = time(NULL);
340 : :
341 [ # # ]: 0 : if (last_sigterm_time == 0)
342 : 0 : last_sigterm_time = curtime;
343 [ # # ]: 0 : else if (curtime < last_sigterm_time ||
344 [ # # ]: 0 : curtime - last_sigterm_time >= 60)
345 : : break;
346 : : }
347 : :
348 : : /* Do what we're here for */
349 : 87 : pgarch_ArchiverCopyLoop();
350 : :
351 : : /*
352 : : * Sleep until a signal is received, or until a poll is forced by
353 : : * PGARCH_AUTOWAKE_INTERVAL, or until postmaster dies.
354 : : */
355 [ + + ]: 87 : if (!time_to_stop) /* Don't wait during last iteration */
356 : : {
357 : : int rc;
358 : :
359 : 68 : rc = WaitLatch(MyLatch,
360 : : WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
361 : : PGARCH_AUTOWAKE_INTERVAL * 1000L,
362 : : WAIT_EVENT_ARCHIVER_MAIN);
363 [ - + ]: 68 : if (rc & WL_POSTMASTER_DEATH)
364 : 0 : time_to_stop = true;
365 : : }
366 : :
367 : : /*
368 : : * The archiver quits either when the postmaster dies (not expected)
369 : : * or after completing one more archiving cycle after receiving
370 : : * SIGUSR2.
371 : : */
372 [ + + ]: 87 : } while (!time_to_stop);
373 : 19 : }
374 : :
375 : : /*
376 : : * pgarch_ArchiverCopyLoop
377 : : *
378 : : * Archives all outstanding xlogs then returns
379 : : */
380 : : static void
381 : 87 : pgarch_ArchiverCopyLoop(void)
382 : : {
383 : : char xlog[MAX_XFN_CHARS + 1];
384 : :
385 : : /* force directory scan in the first call to pgarch_readyXlog() */
386 : 87 : arch_files->arch_files_size = 0;
387 : :
388 : : /*
389 : : * loop through all xlogs with archive_status of .ready and archive
390 : : * them...mostly we expect this to be a single file, though it is possible
391 : : * some backend will add files onto the list of those that need archiving
392 : : * while we are still copying earlier archives
393 : : */
394 [ + + ]: 460 : while (pgarch_readyXlog(xlog))
395 : : {
396 : 378 : int failures = 0;
397 : 378 : int failures_orphan = 0;
398 : :
399 : : for (;;)
400 : 5 : {
401 : : struct stat stat_buf;
402 : : char pathname[MAXPGPATH];
403 : :
404 : : /*
405 : : * Do not initiate any more archive commands after receiving
406 : : * SIGTERM, nor after the postmaster has died unexpectedly. The
407 : : * first condition is to try to keep from having init SIGKILL the
408 : : * command, and the second is to avoid conflicts with another
409 : : * archiver spawned by a newer postmaster.
410 : : */
411 [ + - - + ]: 383 : if (ShutdownRequestPending || !PostmasterIsAlive())
412 : 5 : return;
413 : :
414 : : /*
415 : : * Check for barrier events and config update. This is so that
416 : : * we'll adopt a new setting for archive_command as soon as
417 : : * possible, even if there is a backlog of files to be archived.
418 : : */
419 : 383 : ProcessPgArchInterrupts();
420 : :
421 : : /* Reset variables that might be set by the callback */
422 : 383 : arch_module_check_errdetail_string = NULL;
423 : :
424 : : /* can't do anything if not configured ... */
425 [ + - ]: 383 : if (ArchiveCallbacks->check_configured_cb != NULL &&
426 [ + + ]: 383 : !ArchiveCallbacks->check_configured_cb(archive_module_state))
427 : : {
428 [ + - + - ]: 3 : ereport(WARNING,
429 : : (errmsg("\"archive_mode\" enabled, yet archiving is not configured"),
430 : : arch_module_check_errdetail_string ?
431 : : errdetail_internal("%s", arch_module_check_errdetail_string) : 0));
432 : 3 : return;
433 : : }
434 : :
435 : : /*
436 : : * Since archive status files are not removed in a durable manner,
437 : : * a system crash could leave behind .ready files for WAL segments
438 : : * that have already been recycled or removed. In this case,
439 : : * simply remove the orphan status file and move on. unlink() is
440 : : * used here as even on subsequent crashes the same orphan files
441 : : * would get removed, so there is no need to worry about
442 : : * durability.
443 : : */
444 : 380 : snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
445 [ - + - - ]: 380 : if (stat(pathname, &stat_buf) != 0 && errno == ENOENT)
446 : 0 : {
447 : : char xlogready[MAXPGPATH];
448 : :
449 : 0 : StatusFilePath(xlogready, xlog, ".ready");
450 [ # # ]: 0 : if (unlink(xlogready) == 0)
451 : : {
452 [ # # ]: 0 : ereport(WARNING,
453 : : (errmsg("removed orphan archive status file \"%s\"",
454 : : xlogready)));
455 : :
456 : : /* leave loop and move to the next status file */
457 : 0 : break;
458 : : }
459 : :
460 [ # # ]: 0 : if (++failures_orphan >= NUM_ORPHAN_CLEANUP_RETRIES)
461 : : {
462 [ # # ]: 0 : ereport(WARNING,
463 : : (errmsg("removal of orphan archive status file \"%s\" failed too many times, will try again later",
464 : : xlogready)));
465 : :
466 : : /* give up cleanup of orphan status files */
467 : 0 : return;
468 : : }
469 : :
470 : : /* wait a bit before retrying */
471 : 0 : pg_usleep(1000000L);
472 : 0 : continue;
473 : : }
474 : :
475 [ + + ]: 380 : if (pgarch_archiveXlog(xlog))
476 : : {
477 : : /* successful */
478 : 373 : pgarch_archiveDone(xlog);
479 : :
480 : : /*
481 : : * Tell the cumulative stats system about the WAL file that we
482 : : * successfully archived
483 : : */
484 : 373 : pgstat_report_archiver(xlog, false);
485 : :
486 : 373 : break; /* out of inner retry loop */
487 : : }
488 : : else
489 : : {
490 : : /*
491 : : * Tell the cumulative stats system about the WAL file that we
492 : : * failed to archive
493 : : */
494 : 7 : pgstat_report_archiver(xlog, true);
495 : :
496 [ + + ]: 7 : if (++failures >= NUM_ARCHIVE_RETRIES)
497 : : {
498 [ + - ]: 2 : ereport(WARNING,
499 : : (errmsg("archiving write-ahead log file \"%s\" failed too many times, will try again later",
500 : : xlog)));
501 : 2 : return; /* give up archiving for now */
502 : : }
503 : 5 : pg_usleep(1000000L); /* wait a bit before retrying */
504 : : }
505 : : }
506 : : }
507 : : }
508 : :
509 : : /*
510 : : * pgarch_archiveXlog
511 : : *
512 : : * Invokes archive_file_cb to copy one archive file to wherever it should go
513 : : *
514 : : * Returns true if successful
515 : : */
516 : : static bool
517 : 380 : pgarch_archiveXlog(char *xlog)
518 : : {
519 : : sigjmp_buf local_sigjmp_buf;
520 : : MemoryContext oldcontext;
521 : : char pathname[MAXPGPATH];
522 : : char activitymsg[MAXFNAMELEN + 16];
523 : : bool ret;
524 : :
525 : 380 : snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", xlog);
526 : :
527 : : /* Report archive activity in PS display */
528 : 380 : snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
529 : 380 : set_ps_display(activitymsg);
530 : :
531 : 380 : oldcontext = MemoryContextSwitchTo(archive_context);
532 : :
533 : : /*
534 : : * Since the archiver operates at the bottom of the exception stack,
535 : : * ERRORs turn into FATALs and cause the archiver process to restart.
536 : : * However, using ereport(ERROR, ...) when there are problems is easy to
537 : : * code and maintain. Therefore, we create our own exception handler to
538 : : * catch ERRORs and return false instead of restarting the archiver
539 : : * whenever there is a failure.
540 : : *
541 : : * We assume ERRORs from the archiving callback are the most common
542 : : * exceptions experienced by the archiver, so we opt to handle exceptions
543 : : * here instead of PgArchiverMain() to avoid reinitializing the archiver
544 : : * too frequently. We could instead add a sigsetjmp() block to
545 : : * PgArchiverMain() and use PG_TRY/PG_CATCH here, but the extra code to
546 : : * avoid the odd archiver restart doesn't seem worth it.
547 : : */
548 [ - + ]: 380 : if (sigsetjmp(local_sigjmp_buf, 1) != 0)
549 : : {
550 : : /* Since not using PG_TRY, must reset error stack by hand */
551 : 0 : error_context_stack = NULL;
552 : :
553 : : /* Prevent interrupts while cleaning up */
554 : 0 : HOLD_INTERRUPTS();
555 : :
556 : : /* Report the error to the server log. */
557 : 0 : EmitErrorReport();
558 : :
559 : : /*
560 : : * Try to clean up anything the archive module left behind. We try to
561 : : * cover anything that an archive module could conceivably have left
562 : : * behind, but it is of course possible that modules could be doing
563 : : * unexpected things that require additional cleanup. Module authors
564 : : * should be sure to do any extra required cleanup in a PG_CATCH block
565 : : * within the archiving callback, and they are encouraged to notify
566 : : * the pgsql-hackers mailing list so that we can add it here.
567 : : */
568 : 0 : disable_all_timeouts(false);
569 : 0 : LWLockReleaseAll();
570 : 0 : ConditionVariableCancelSleep();
571 : 0 : pgstat_report_wait_end();
572 : 0 : pgaio_error_cleanup();
573 : 0 : ReleaseAuxProcessResources(false);
574 : 0 : AtEOXact_Files(false);
575 : 0 : AtEOXact_HashTables(false);
576 : :
577 : : /*
578 : : * Return to the original memory context and clear ErrorContext for
579 : : * next time.
580 : : */
581 : 0 : MemoryContextSwitchTo(oldcontext);
582 : 0 : FlushErrorState();
583 : :
584 : : /* Flush any leaked data */
585 : 0 : MemoryContextReset(archive_context);
586 : :
587 : : /* Remove our exception handler */
588 : 0 : PG_exception_stack = NULL;
589 : :
590 : : /* Now we can allow interrupts again */
591 : 0 : RESUME_INTERRUPTS();
592 : :
593 : : /* Report failure so that the archiver retries this file */
594 : 0 : ret = false;
595 : : }
596 : : else
597 : : {
598 : : /* Enable our exception handler */
599 : 380 : PG_exception_stack = &local_sigjmp_buf;
600 : :
601 : : /* Archive the file! */
602 : 380 : ret = ArchiveCallbacks->archive_file_cb(archive_module_state,
603 : : xlog, pathname);
604 : :
605 : : /* Remove our exception handler */
606 : 380 : PG_exception_stack = NULL;
607 : :
608 : : /* Reset our memory context and switch back to the original one */
609 : 380 : MemoryContextSwitchTo(oldcontext);
610 : 380 : MemoryContextReset(archive_context);
611 : : }
612 : :
613 [ + + ]: 380 : if (ret)
614 : 373 : snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
615 : : else
616 : 7 : snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
617 : 380 : set_ps_display(activitymsg);
618 : :
619 : 380 : return ret;
620 : : }
621 : :
622 : : /*
623 : : * pgarch_readyXlog
624 : : *
625 : : * Return name of the oldest xlog file that has not yet been archived.
626 : : * No notification is set that file archiving is now in progress, so
627 : : * this would need to be extended if multiple concurrent archival
628 : : * tasks were created. If a failure occurs, we will completely
629 : : * re-copy the file at the next available opportunity.
630 : : *
631 : : * It is important that we return the oldest, so that we archive xlogs
632 : : * in order that they were written, for two reasons:
633 : : * 1) to maintain the sequential chain of xlogs required for recovery
634 : : * 2) because the oldest ones will sooner become candidates for
635 : : * recycling at time of checkpoint
636 : : *
637 : : * NOTE: the "oldest" comparison will consider any .history file to be older
638 : : * than any other file except another .history file. Segments on a timeline
639 : : * with a smaller ID will be older than all segments on a timeline with a
640 : : * larger ID; the net result being that past timelines are given higher
641 : : * priority for archiving. This seems okay, or at least not obviously worth
642 : : * changing.
643 : : */
644 : : static bool
645 : 460 : pgarch_readyXlog(char *xlog)
646 : : {
647 : : char XLogArchiveStatusDir[MAXPGPATH];
648 : : DIR *rldir;
649 : : struct dirent *rlde;
650 : :
651 : : /*
652 : : * If a directory scan was requested, clear the stored file names and
653 : : * proceed.
654 : : */
655 [ + + ]: 460 : if (pg_atomic_exchange_u32(&PgArch->force_dir_scan, 0) == 1)
656 : 3 : arch_files->arch_files_size = 0;
657 : :
658 : : /*
659 : : * If we still have stored file names from the previous directory scan,
660 : : * try to return one of those. We check to make sure the status file is
661 : : * still present, as the archive_command for a previous file may have
662 : : * already marked it done.
663 : : */
664 [ + + ]: 460 : while (arch_files->arch_files_size > 0)
665 : : {
666 : : struct stat st;
667 : : char status_file[MAXPGPATH];
668 : : char *arch_file;
669 : :
670 : 306 : arch_files->arch_files_size--;
671 : 306 : arch_file = arch_files->arch_files[arch_files->arch_files_size];
672 : 306 : StatusFilePath(status_file, arch_file, ".ready");
673 : :
674 [ + - ]: 306 : if (stat(status_file, &st) == 0)
675 : : {
676 : 306 : strcpy(xlog, arch_file);
677 : 306 : return true;
678 : : }
679 [ # # ]: 0 : else if (errno != ENOENT)
680 [ # # ]: 0 : ereport(ERROR,
681 : : (errcode_for_file_access(),
682 : : errmsg("could not stat file \"%s\": %m", status_file)));
683 : : }
684 : :
685 : : /* arch_heap is probably empty, but let's make sure */
686 : 154 : binaryheap_reset(arch_files->arch_heap);
687 : :
688 : : /*
689 : : * Open the archive status directory and read through the list of files
690 : : * with the .ready suffix, looking for the earliest files.
691 : : */
692 : 154 : snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
693 : 154 : rldir = AllocateDir(XLogArchiveStatusDir);
694 : :
695 [ + + ]: 1250 : while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
696 : : {
697 : 1096 : int basenamelen = (int) strlen(rlde->d_name) - 6;
698 : : char basename[MAX_XFN_CHARS + 1];
699 : : char *arch_file;
700 : :
701 : : /* Ignore entries with unexpected number of characters */
702 [ + + - + ]: 1096 : if (basenamelen < MIN_XFN_CHARS ||
703 : : basenamelen > MAX_XFN_CHARS)
704 : 687 : continue;
705 : :
706 : : /* Ignore entries with unexpected characters */
707 [ - + ]: 770 : if (strspn(rlde->d_name, VALID_XFN_CHARS) < basenamelen)
708 : 0 : continue;
709 : :
710 : : /* Ignore anything not suffixed with .ready */
711 [ + + ]: 770 : if (strcmp(rlde->d_name + basenamelen, ".ready") != 0)
712 : 361 : continue;
713 : :
714 : : /* Truncate off the .ready */
715 : 409 : memcpy(basename, rlde->d_name, basenamelen);
716 : 409 : basename[basenamelen] = '\0';
717 : :
718 : : /*
719 : : * Store the file in our max-heap if it has a high enough priority.
720 : : */
721 [ + + ]: 409 : if (binaryheap_size(arch_files->arch_heap) < NUM_FILES_PER_DIRECTORY_SCAN)
722 : : {
723 : : /* If the heap isn't full yet, quickly add it. */
724 : 381 : arch_file = arch_files->arch_filenames[binaryheap_size(arch_files->arch_heap)];
725 : 381 : strcpy(arch_file, basename);
726 : 381 : binaryheap_add_unordered(arch_files->arch_heap, CStringGetDatum(arch_file));
727 : :
728 : : /* If we just filled the heap, make it a valid one. */
729 [ + + ]: 381 : if (binaryheap_size(arch_files->arch_heap) == NUM_FILES_PER_DIRECTORY_SCAN)
730 : 2 : binaryheap_build(arch_files->arch_heap);
731 : : }
732 [ + + ]: 28 : else if (ready_file_comparator(binaryheap_first(arch_files->arch_heap),
733 : : CStringGetDatum(basename), NULL) > 0)
734 : : {
735 : : /*
736 : : * Remove the lowest priority file and add the current one to the
737 : : * heap.
738 : : */
739 : 25 : arch_file = DatumGetCString(binaryheap_remove_first(arch_files->arch_heap));
740 : 25 : strcpy(arch_file, basename);
741 : 25 : binaryheap_add(arch_files->arch_heap, CStringGetDatum(arch_file));
742 : : }
743 : : }
744 : 154 : FreeDir(rldir);
745 : :
746 : : /* If no files were found, simply return. */
747 [ + + ]: 154 : if (binaryheap_empty(arch_files->arch_heap))
748 : 82 : return false;
749 : :
750 : : /*
751 : : * If we didn't fill the heap, we didn't make it a valid one. Do that
752 : : * now.
753 : : */
754 [ + + ]: 72 : if (binaryheap_size(arch_files->arch_heap) < NUM_FILES_PER_DIRECTORY_SCAN)
755 : 70 : binaryheap_build(arch_files->arch_heap);
756 : :
757 : : /*
758 : : * Fill arch_files array with the files to archive in ascending order of
759 : : * priority.
760 : : */
761 : 72 : arch_files->arch_files_size = binaryheap_size(arch_files->arch_heap);
762 [ + + ]: 453 : for (int i = 0; i < arch_files->arch_files_size; i++)
763 : 381 : arch_files->arch_files[i] = DatumGetCString(binaryheap_remove_first(arch_files->arch_heap));
764 : :
765 : : /* Return the highest priority file. */
766 : 72 : arch_files->arch_files_size--;
767 : 72 : strcpy(xlog, arch_files->arch_files[arch_files->arch_files_size]);
768 : :
769 : 72 : return true;
770 : : }
771 : :
772 : : /*
773 : : * ready_file_comparator
774 : : *
775 : : * Compares the archival priority of the given files to archive. If "a"
776 : : * has a higher priority than "b", a negative value will be returned. If
777 : : * "b" has a higher priority than "a", a positive value will be returned.
778 : : * If "a" and "b" have equivalent values, 0 will be returned.
779 : : */
780 : : static int
781 : 2614 : ready_file_comparator(Datum a, Datum b, void *arg)
782 : : {
783 : 2614 : char *a_str = DatumGetCString(a);
784 : 2614 : char *b_str = DatumGetCString(b);
785 : 2614 : bool a_history = IsTLHistoryFileName(a_str);
786 : 2614 : bool b_history = IsTLHistoryFileName(b_str);
787 : :
788 : : /* Timeline history files always have the highest priority. */
789 [ + + ]: 2614 : if (a_history != b_history)
790 [ + + ]: 3 : return a_history ? -1 : 1;
791 : :
792 : : /* Priority is given to older files. */
793 : 2611 : return strcmp(a_str, b_str);
794 : : }
795 : :
796 : : /*
797 : : * PgArchForceDirScan
798 : : *
799 : : * When called, the next call to pgarch_readyXlog() will perform a
800 : : * directory scan. This is useful for ensuring that important files such
801 : : * as timeline history files are archived as quickly as possible.
802 : : */
803 : : void
804 : 16 : PgArchForceDirScan(void)
805 : : {
806 : 16 : pg_atomic_write_membarrier_u32(&PgArch->force_dir_scan, 1);
807 : 16 : }
808 : :
809 : : /*
810 : : * pgarch_archiveDone
811 : : *
812 : : * Emit notification that an xlog file has been successfully archived.
813 : : * We do this by renaming the status file from NNN.ready to NNN.done.
814 : : * Eventually, a checkpoint process will notice this and delete both the
815 : : * NNN.done file and the xlog file itself.
816 : : */
817 : : static void
818 : 373 : pgarch_archiveDone(char *xlog)
819 : : {
820 : : char rlogready[MAXPGPATH];
821 : : char rlogdone[MAXPGPATH];
822 : :
823 : 373 : StatusFilePath(rlogready, xlog, ".ready");
824 : 373 : StatusFilePath(rlogdone, xlog, ".done");
825 : :
826 : : /*
827 : : * To avoid extra overhead, we don't durably rename the .ready file to
828 : : * .done. Archive commands and libraries must gracefully handle attempts
829 : : * to re-archive files (e.g., if the server crashes just before this
830 : : * function is called), so it should be okay if the .ready file reappears
831 : : * after a crash.
832 : : */
833 [ - + ]: 373 : if (rename(rlogready, rlogdone) < 0)
834 [ # # ]: 0 : ereport(WARNING,
835 : : (errcode_for_file_access(),
836 : : errmsg("could not rename file \"%s\" to \"%s\": %m",
837 : : rlogready, rlogdone)));
838 : 373 : }
839 : :
840 : :
841 : : /*
842 : : * pgarch_die
843 : : *
844 : : * Exit-time cleanup handler
845 : : */
846 : : static void
847 : 19 : pgarch_die(int code, Datum arg)
848 : : {
849 : 19 : PgArch->pgprocno = INVALID_PROC_NUMBER;
850 : 19 : }
851 : :
852 : : /*
853 : : * Interrupt handler for WAL archiver process.
854 : : *
855 : : * This is called in the loops pgarch_MainLoop and pgarch_ArchiverCopyLoop.
856 : : * It checks for barrier events, config update and request for logging of
857 : : * memory contexts, but not shutdown request because how to handle
858 : : * shutdown request is different between those loops.
859 : : */
860 : : static void
861 : 470 : ProcessPgArchInterrupts(void)
862 : : {
863 [ + + ]: 470 : if (ProcSignalBarrierPending)
864 : 2 : ProcessProcSignalBarrier();
865 : :
866 : : /* Perform logging of memory contexts of this process */
867 [ - + ]: 470 : if (LogMemoryContextPending)
868 : 0 : ProcessLogMemoryContextInterrupt();
869 : :
870 [ + + ]: 470 : if (ConfigReloadPending)
871 : : {
872 : 3 : char *archiveLib = pstrdup(XLogArchiveLibrary);
873 : : bool archiveLibChanged;
874 : :
875 : 3 : ConfigReloadPending = false;
876 : 3 : ProcessConfigFile(PGC_SIGHUP);
877 : :
878 [ - + - - ]: 3 : if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
879 [ # # ]: 0 : ereport(ERROR,
880 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
881 : : errmsg("both \"archive_command\" and \"archive_library\" set"),
882 : : errdetail("Only one of \"archive_command\", \"archive_library\" may be set.")));
883 : :
884 : 3 : archiveLibChanged = strcmp(XLogArchiveLibrary, archiveLib) != 0;
885 : 3 : pfree(archiveLib);
886 : :
887 [ - + ]: 3 : if (archiveLibChanged)
888 : : {
889 : : /*
890 : : * Ideally, we would simply unload the previous archive module and
891 : : * load the new one, but there is presently no mechanism for
892 : : * unloading a library (see the comment above
893 : : * internal_load_library()). To deal with this, we simply restart
894 : : * the archiver. The new archive module will be loaded when the
895 : : * new archiver process starts up. Note that this triggers the
896 : : * module's shutdown callback, if defined.
897 : : */
898 [ # # ]: 0 : ereport(LOG,
899 : : (errmsg("restarting archiver process because value of "
900 : : "\"archive_library\" was changed")));
901 : :
902 : 0 : proc_exit(0);
903 : : }
904 : : }
905 : 470 : }
906 : :
907 : : /*
908 : : * LoadArchiveLibrary
909 : : *
910 : : * Loads the archiving callbacks into our local ArchiveCallbacks.
911 : : */
912 : : static void
913 : 19 : LoadArchiveLibrary(void)
914 : : {
915 : : ArchiveModuleInit archive_init;
916 : :
917 [ + + - + ]: 19 : if (XLogArchiveLibrary[0] != '\0' && XLogArchiveCommand[0] != '\0')
918 [ # # ]: 0 : ereport(ERROR,
919 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
920 : : errmsg("both \"archive_command\" and \"archive_library\" set"),
921 : : errdetail("Only one of \"archive_command\", \"archive_library\" may be set.")));
922 : :
923 : : /*
924 : : * If shell archiving is enabled, use our special initialization function.
925 : : * Otherwise, load the library and call its _PG_archive_module_init().
926 : : */
927 [ + + ]: 19 : if (XLogArchiveLibrary[0] == '\0')
928 : 18 : archive_init = shell_archive_init;
929 : : else
930 : 1 : archive_init = (ArchiveModuleInit)
931 : 1 : load_external_function(XLogArchiveLibrary,
932 : : "_PG_archive_module_init", false, NULL);
933 : :
934 [ - + ]: 19 : if (archive_init == NULL)
935 [ # # ]: 0 : ereport(ERROR,
936 : : (errmsg("archive modules have to define the symbol %s", "_PG_archive_module_init")));
937 : :
938 : 19 : ArchiveCallbacks = (*archive_init) ();
939 : :
940 [ - + ]: 19 : if (ArchiveCallbacks->archive_file_cb == NULL)
941 [ # # ]: 0 : ereport(ERROR,
942 : : (errmsg("archive modules must register an archive callback")));
943 : :
944 : 19 : archive_module_state = palloc0_object(ArchiveModuleState);
945 [ - + ]: 19 : if (ArchiveCallbacks->startup_cb != NULL)
946 : 0 : ArchiveCallbacks->startup_cb(archive_module_state);
947 : :
948 : 19 : before_shmem_exit(pgarch_call_module_shutdown_cb, 0);
949 : 19 : }
950 : :
951 : : /*
952 : : * Call the shutdown callback of the loaded archive module, if defined.
953 : : */
954 : : static void
955 : 19 : pgarch_call_module_shutdown_cb(int code, Datum arg)
956 : : {
957 [ + + ]: 19 : if (ArchiveCallbacks->shutdown_cb != NULL)
958 : 18 : ArchiveCallbacks->shutdown_cb(archive_module_state);
959 : 19 : }
|