PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_standby.c
Go to the documentation of this file.
1 /*
2  * contrib/pg_standby/pg_standby.c
3  *
4  *
5  * pg_standby.c
6  *
7  * Production-ready example of how to create a Warm Standby
8  * database server using continuous archiving as a
9  * replication mechanism
10  *
11  * We separate the parameters for archive and nextWALfile
12  * so that we can check the archive exists, even if the
13  * WAL file doesn't (yet).
14  *
15  * This program will be executed once in full for each file
16  * requested by the warm standby server.
17  *
18  * It is designed to cater to a variety of needs, as well
19  * providing a customizable section.
20  *
21  * Original author: Simon Riggs simon@2ndquadrant.com
22  * Current maintainer: Simon Riggs
23  */
24 #include "postgres_fe.h"
25 
26 #include <ctype.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <sys/time.h>
32 
33 #include "pg_getopt.h"
34 
35 #include "access/xlog_internal.h"
36 
37 const char *progname;
38 
39 int WalSegSz = -1;
40 
41 /* Options and defaults */
42 int sleeptime = 5; /* amount of time to sleep between file checks */
43 int waittime = -1; /* how long we have been waiting, -1 no wait
44  * yet */
45 int maxwaittime = 0; /* how long are we prepared to wait for? */
46 int keepfiles = 0; /* number of WAL files to keep, 0 keep all */
47 int maxretries = 3; /* number of retries on restore command */
48 bool debug = false; /* are we debugging? */
49 bool need_cleanup = false; /* do we need to remove files from
50  * archive? */
51 
52 #ifndef WIN32
53 static volatile sig_atomic_t signaled = false;
54 #endif
55 
56 char *archiveLocation; /* where to find the archive? */
57 char *triggerPath; /* where to find the trigger file? */
58 char *xlogFilePath; /* where we are going to restore to */
59 char *nextWALFileName; /* the file we need to get from archive */
60 char *restartWALFileName; /* the file from which we can restart restore */
61 char *priorWALFileName; /* the file we need to get from archive */
62 char WALFilePath[MAXPGPATH * 2]; /* the file path including archive */
63 char restoreCommand[MAXPGPATH]; /* run this to restore */
64 char exclusiveCleanupFileName[MAXFNAMELEN]; /* the file we need to get
65  * from archive */
66 
67 /*
68  * Two types of failover are supported (smart and fast failover).
69  *
70  * The content of the trigger file determines the type of failover. If the
71  * trigger file contains the word "smart" (or the file is empty), smart
72  * failover is chosen: pg_standby acts as cp or ln command itself, on
73  * successful completion all the available WAL records will be applied
74  * resulting in zero data loss. But, it might take a long time to finish
75  * recovery if there's a lot of unapplied WAL.
76  *
77  * On the other hand, if the trigger file contains the word "fast", the
78  * recovery is finished immediately even if unapplied WAL files remain. Any
79  * transactions in the unapplied WAL files are lost.
80  *
81  * An empty trigger file performs smart failover. SIGUSR or SIGINT triggers
82  * fast failover. A timeout causes fast failover (smart failover would have
83  * the same effect, since if the timeout is reached there is no unapplied WAL).
84  */
85 #define NoFailover 0
86 #define SmartFailover 1
87 #define FastFailover 2
88 
89 static int Failover = NoFailover;
90 
91 #define RESTORE_COMMAND_COPY 0
92 #define RESTORE_COMMAND_LINK 1
94 
95 #define XLOG_DATA 0
96 #define XLOG_HISTORY 1
97 #define XLOG_BACKUP_LABEL 2
99 
100 #define SET_RESTORE_COMMAND(cmd, arg1, arg2) \
101  snprintf(restoreCommand, MAXPGPATH, cmd " \"%s\" \"%s\"", arg1, arg2)
102 
103 struct stat stat_buf;
104 
105 static bool SetWALFileNameForCleanup(void);
106 static bool SetWALSegSize(void);
107 
108 
109 /* =====================================================================
110  *
111  * Customizable section
112  *
113  * =====================================================================
114  *
115  * Currently, this section assumes that the Archive is a locally
116  * accessible directory. If you want to make other assumptions,
117  * such as using a vendor-specific archive and access API, these
118  * routines are the ones you'll need to change. You're
119  * encouraged to submit any changes to pgsql-hackers@postgresql.org
120  * or personally to the current maintainer. Those changes may be
121  * folded in to later versions of this program.
122  */
123 
124 /*
125  * Initialize allows customized commands into the warm standby program.
126  *
127  * As an example, and probably the common case, we use either
128  * cp/ln commands on *nix, or copy/move command on Windows.
129  */
130 static void
132 {
133 #ifdef WIN32
135  switch (restoreCommandType)
136  {
139  break;
141  default:
143  break;
144  }
145 #else
147  switch (restoreCommandType)
148  {
150 #if HAVE_WORKING_LINK
152  break;
153 #endif
155  default:
157  break;
158  }
159 #endif
160 
161  /*
162  * This code assumes that archiveLocation is a directory You may wish to
163  * add code to check for tape libraries, etc.. So, since it is a
164  * directory, we use stat to test if it's accessible
165  */
166  if (stat(archiveLocation, &stat_buf) != 0)
167  {
168  fprintf(stderr, "%s: archive location \"%s\" does not exist\n", progname, archiveLocation);
169  fflush(stderr);
170  exit(2);
171  }
172 }
173 
174 /*
175  * CustomizableNextWALFileReady()
176  *
177  * Is the requested file ready yet?
178  */
179 static bool
181 {
182  if (stat(WALFilePath, &stat_buf) == 0)
183  {
184  /*
185  * If we've not seen any WAL segments, we don't know the WAL segment
186  * size, which we need. If it looks like a WAL segment, determine size
187  * of segments for the cluster.
188  */
190  {
191  if (SetWALSegSize())
192  {
193  /*
194  * Successfully determined WAL segment size. Can compute
195  * cleanup cutoff now.
196  */
198  if (debug)
199  {
200  fprintf(stderr,
201  _("WAL segment size: %d \n"), WalSegSz);
202  fprintf(stderr, "Keep archive history: ");
203 
204  if (need_cleanup)
205  fprintf(stderr, "%s and later\n",
207  else
208  fprintf(stderr, "no cleanup required\n");
209  }
210  }
211  }
212 
213  /*
214  * If it's a backup file, return immediately. If it's a regular file
215  * return only if it's the right size already.
216  */
218  {
220  return true;
221  }
222  else if (WalSegSz > 0 && stat_buf.st_size == WalSegSz)
223  {
224 #ifdef WIN32
225 
226  /*
227  * Windows 'cp' sets the final file size before the copy is
228  * complete, and not yet ready to be opened by pg_standby. So we
229  * wait for sleeptime secs before attempting to restore. If that
230  * is not enough, we will rely on the retry/holdoff mechanism.
231  * GNUWin32's cp does not have this problem.
232  */
233  pg_usleep(sleeptime * 1000000L);
234 #endif
236  return true;
237  }
238 
239  /*
240  * If still too small, wait until it is the correct size
241  */
242  if (WalSegSz > 0 && stat_buf.st_size > WalSegSz)
243  {
244  if (debug)
245  {
246  fprintf(stderr, "file size greater than expected\n");
247  fflush(stderr);
248  }
249  exit(3);
250  }
251  }
252 
253  return false;
254 }
255 
256 static void
258 {
259  /*
260  * Work out name of prior file from current filename
261  */
262  if (nextWALFileType == XLOG_DATA)
263  {
264  int rc;
265  DIR *xldir;
266  struct dirent *xlde;
267 
268  /*
269  * Assume it's OK to keep failing. The failure situation may change
270  * over time, so we'd rather keep going on the main processing than
271  * fail because we couldn't clean up yet.
272  */
273  if ((xldir = opendir(archiveLocation)) != NULL)
274  {
275  while (errno = 0, (xlde = readdir(xldir)) != NULL)
276  {
277  /*
278  * We ignore the timeline part of the XLOG segment identifiers
279  * in deciding whether a segment is still needed. This
280  * ensures that we won't prematurely remove a segment from a
281  * parent timeline. We could probably be a little more
282  * proactive about removing segments of non-parent timelines,
283  * but that would be a whole lot more complicated.
284  *
285  * We use the alphanumeric sorting property of the filenames
286  * to decide which ones are earlier than the
287  * exclusiveCleanupFileName file. Note that this means files
288  * are not removed in the order they were originally written,
289  * in case this worries you.
290  */
291  if (IsXLogFileName(xlde->d_name) &&
292  strcmp(xlde->d_name + 8, exclusiveCleanupFileName + 8) < 0)
293  {
294 #ifdef WIN32
295  snprintf(WALFilePath, sizeof(WALFilePath), "%s\\%s", archiveLocation, xlde->d_name);
296 #else
297  snprintf(WALFilePath, sizeof(WALFilePath), "%s/%s", archiveLocation, xlde->d_name);
298 #endif
299 
300  if (debug)
301  fprintf(stderr, "\nremoving file \"%s\"", WALFilePath);
302 
303  rc = unlink(WALFilePath);
304  if (rc != 0)
305  {
306  fprintf(stderr, "\n%s: ERROR: could not remove file \"%s\": %s\n",
307  progname, WALFilePath, strerror(errno));
308  break;
309  }
310  }
311  }
312 
313  if (errno)
314  fprintf(stderr, "%s: could not read archive location \"%s\": %s\n",
316  if (debug)
317  fprintf(stderr, "\n");
318  }
319  else
320  fprintf(stderr, "%s: could not open archive location \"%s\": %s\n",
322 
323  if (closedir(xldir))
324  fprintf(stderr, "%s: could not close archive location \"%s\": %s\n",
326 
327  fflush(stderr);
328  }
329 }
330 
331 /* =====================================================================
332  * End of Customizable section
333  * =====================================================================
334  */
335 
336 /*
337  * SetWALFileNameForCleanup()
338  *
339  * Set the earliest WAL filename that we want to keep on the archive
340  * and decide whether we need_cleanup
341  */
342 static bool
344 {
345  uint32 tli = 1,
346  log = 0,
347  seg = 0;
348  uint32 log_diff = 0,
349  seg_diff = 0;
350  bool cleanup = false;
351  int max_segments_per_logfile = (0xFFFFFFFF / WalSegSz);
352 
353  if (restartWALFileName)
354  {
355  /*
356  * Don't do cleanup if the restartWALFileName provided is later than
357  * the xlog file requested. This is an error and we must not remove
358  * these files from archive. This shouldn't happen, but better safe
359  * than sorry.
360  */
361  if (strcmp(restartWALFileName, nextWALFileName) > 0)
362  return false;
363 
365  return true;
366  }
367 
368  if (keepfiles > 0)
369  {
370  sscanf(nextWALFileName, "%08X%08X%08X", &tli, &log, &seg);
371  if (tli > 0 && seg > 0)
372  {
373  log_diff = keepfiles / max_segments_per_logfile;
374  seg_diff = keepfiles % max_segments_per_logfile;
375  if (seg_diff > seg)
376  {
377  log_diff++;
378  seg = max_segments_per_logfile - (seg_diff - seg);
379  }
380  else
381  seg -= seg_diff;
382 
383  if (log >= log_diff)
384  {
385  log -= log_diff;
386  cleanup = true;
387  }
388  else
389  {
390  log = 0;
391  seg = 0;
392  }
393  }
394  }
395 
397 
398  return cleanup;
399 }
400 
401 /*
402  * Try to set the wal segment size from the WAL file specified by WALFilePath.
403  *
404  * Return true if size could be determined, false otherwise.
405  */
406 static bool
408 {
409  bool ret_val = false;
410  int fd;
411 
412  /* malloc this buffer to ensure sufficient alignment: */
413  char *buf = (char *) pg_malloc(XLOG_BLCKSZ);
414 
415  Assert(WalSegSz == -1);
416 
417  if ((fd = open(WALFilePath, O_RDWR, 0)) < 0)
418  {
419  fprintf(stderr, "%s: could not open WAL file \"%s\": %s\n",
420  progname, WALFilePath, strerror(errno));
421  pg_free(buf);
422  return false;
423  }
424 
425  errno = 0;
426  if (read(fd, buf, XLOG_BLCKSZ) == XLOG_BLCKSZ)
427  {
428  XLogLongPageHeader longhdr = (XLogLongPageHeader) buf;
429 
430  WalSegSz = longhdr->xlp_seg_size;
431 
433  {
434  /* successfully retrieved WAL segment size */
435  ret_val = true;
436  }
437  else
438  fprintf(stderr,
439  "%s: WAL segment size must be a power of two between 1MB and 1GB, but the WAL file header specifies %d bytes\n",
440  progname, WalSegSz);
441  }
442  else
443  {
444  /*
445  * Don't complain loudly, this is to be expected for segments being
446  * created.
447  */
448  if (errno != 0)
449  {
450  if (debug)
451  fprintf(stderr, "could not read file \"%s\": %s\n",
452  WALFilePath, strerror(errno));
453  }
454  else
455  {
456  if (debug)
457  fprintf(stderr, "not enough data in file \"%s\"\n",
458  WALFilePath);
459  }
460  }
461 
462  fflush(stderr);
463 
464  close(fd);
465  pg_free(buf);
466  return ret_val;
467 }
468 
469 /*
470  * CheckForExternalTrigger()
471  *
472  * Is there a trigger file? Sets global 'Failover' variable to indicate
473  * what kind of a trigger file it was. A "fast" trigger file is turned
474  * into a "smart" file as a side-effect.
475  */
476 static void
478 {
479  char buf[32];
480  int fd;
481  int len;
482 
483  /*
484  * Look for a trigger file, if that option has been selected
485  *
486  * We use stat() here because triggerPath is always a file rather than
487  * potentially being in an archive
488  */
489  if (!triggerPath || stat(triggerPath, &stat_buf) != 0)
490  return;
491 
492  /*
493  * An empty trigger file performs smart failover. There's a little race
494  * condition here: if the writer of the trigger file has just created the
495  * file, but not yet written anything to it, we'll treat that as smart
496  * shutdown even if the other process was just about to write "fast" to
497  * it. But that's fine: we'll restore one more WAL file, and when we're
498  * invoked next time, we'll see the word "fast" and fail over immediately.
499  */
500  if (stat_buf.st_size == 0)
501  {
503  fprintf(stderr, "trigger file found: smart failover\n");
504  fflush(stderr);
505  return;
506  }
507 
508  if ((fd = open(triggerPath, O_RDWR, 0)) < 0)
509  {
510  fprintf(stderr, "WARNING: could not open \"%s\": %s\n",
511  triggerPath, strerror(errno));
512  fflush(stderr);
513  return;
514  }
515 
516  if ((len = read(fd, buf, sizeof(buf) - 1)) < 0)
517  {
518  fprintf(stderr, "WARNING: could not read \"%s\": %s\n",
519  triggerPath, strerror(errno));
520  fflush(stderr);
521  close(fd);
522  return;
523  }
524  buf[len] = '\0';
525 
526  if (strncmp(buf, "smart", 5) == 0)
527  {
529  fprintf(stderr, "trigger file found: smart failover\n");
530  fflush(stderr);
531  close(fd);
532  return;
533  }
534 
535  if (strncmp(buf, "fast", 4) == 0)
536  {
538 
539  fprintf(stderr, "trigger file found: fast failover\n");
540  fflush(stderr);
541 
542  /*
543  * Turn it into a "smart" trigger by truncating the file. Otherwise if
544  * the server asks us again to restore a segment that was restored
545  * already, we would return "not found" and upset the server.
546  */
547  if (ftruncate(fd, 0) < 0)
548  {
549  fprintf(stderr, "WARNING: could not read \"%s\": %s\n",
550  triggerPath, strerror(errno));
551  fflush(stderr);
552  }
553  close(fd);
554 
555  return;
556  }
557  close(fd);
558 
559  fprintf(stderr, "WARNING: invalid content in \"%s\"\n", triggerPath);
560  fflush(stderr);
561  return;
562 }
563 
564 /*
565  * RestoreWALFileForRecovery()
566  *
567  * Perform the action required to restore the file from archive
568  */
569 static bool
571 {
572  int rc = 0;
573  int numretries = 0;
574 
575  if (debug)
576  {
577  fprintf(stderr, "running restore: ");
578  fflush(stderr);
579  }
580 
581  while (numretries <= maxretries)
582  {
583  rc = system(restoreCommand);
584  if (rc == 0)
585  {
586  if (debug)
587  {
588  fprintf(stderr, "OK\n");
589  fflush(stderr);
590  }
591  return true;
592  }
593  pg_usleep(numretries++ * sleeptime * 1000000L);
594  }
595 
596  /*
597  * Allow caller to add additional info
598  */
599  if (debug)
600  fprintf(stderr, "not restored\n");
601  return false;
602 }
603 
604 static void
605 usage(void)
606 {
607  printf("%s allows PostgreSQL warm standby servers to be configured.\n\n", progname);
608  printf("Usage:\n");
609  printf(" %s [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n", progname);
610  printf("\nOptions:\n");
611  printf(" -c copy file from archive (default)\n");
612  printf(" -d generate lots of debugging output (testing only)\n");
613  printf(" -k NUMFILESTOKEEP if RESTARTWALFILE is not used, remove files prior to limit\n"
614  " (0 keeps all)\n");
615  printf(" -l does nothing; use of link is now deprecated\n");
616  printf(" -r MAXRETRIES max number of times to retry, with progressive wait\n"
617  " (default=3)\n");
618  printf(" -s SLEEPTIME seconds to wait between file checks (min=1, max=60,\n"
619  " default=5)\n");
620  printf(" -t TRIGGERFILE trigger file to initiate failover (no default)\n");
621  printf(" -V, --version output version information, then exit\n");
622  printf(" -w MAXWAITTIME max seconds to wait for a file (0=no limit) (default=0)\n");
623  printf(" -?, --help show this help, then exit\n");
624  printf("\n"
625  "Main intended use as restore_command in recovery.conf:\n"
626  " restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n"
627  "e.g.\n"
628  " restore_command = 'pg_standby /mnt/server/archiverdir %%f %%p %%r'\n");
629  printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
630 }
631 
632 #ifndef WIN32
633 static void
635 {
636  signaled = true;
637 }
638 
639 /* We don't want SIGQUIT to core dump */
640 static void
642 {
643  pqsignal(SIGINT, SIG_DFL);
644  kill(getpid(), SIGINT);
645 }
646 #endif
647 
648 /*------------ MAIN ----------------------------------------*/
649 int
650 main(int argc, char **argv)
651 {
652  int c;
653 
654  progname = get_progname(argv[0]);
655 
656  if (argc > 1)
657  {
658  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
659  {
660  usage();
661  exit(0);
662  }
663  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
664  {
665  puts("pg_standby (PostgreSQL) " PG_VERSION);
666  exit(0);
667  }
668  }
669 
670 #ifndef WIN32
671 
672  /*
673  * You can send SIGUSR1 to trigger failover.
674  *
675  * Postmaster uses SIGQUIT to request immediate shutdown. The default
676  * action is to core dump, but we don't want that, so trap it and commit
677  * suicide without core dump.
678  *
679  * We used to use SIGINT and SIGQUIT to trigger failover, but that turned
680  * out to be a bad idea because postmaster uses SIGQUIT to request
681  * immediate shutdown. We still trap SIGINT, but that may change in a
682  * future release.
683  *
684  * There's no way to trigger failover via signal on Windows.
685  */
686  (void) pqsignal(SIGUSR1, sighandler);
687  (void) pqsignal(SIGINT, sighandler); /* deprecated, use SIGUSR1 */
689 #endif
690 
691  while ((c = getopt(argc, argv, "cdk:lr:s:t:w:")) != -1)
692  {
693  switch (c)
694  {
695  case 'c': /* Use copy */
697  break;
698  case 'd': /* Debug mode */
699  debug = true;
700  break;
701  case 'k': /* keepfiles */
702  keepfiles = atoi(optarg);
703  if (keepfiles < 0)
704  {
705  fprintf(stderr, "%s: -k keepfiles must be >= 0\n", progname);
706  exit(2);
707  }
708  break;
709  case 'l': /* Use link */
710 
711  /*
712  * Link feature disabled, possibly permanently. Linking causes
713  * a problem after recovery ends that is not currently
714  * resolved by PostgreSQL. 25 Jun 2009
715  */
716 #ifdef NOT_USED
718 #endif
719  break;
720  case 'r': /* Retries */
721  maxretries = atoi(optarg);
722  if (maxretries < 0)
723  {
724  fprintf(stderr, "%s: -r maxretries must be >= 0\n", progname);
725  exit(2);
726  }
727  break;
728  case 's': /* Sleep time */
729  sleeptime = atoi(optarg);
730  if (sleeptime <= 0 || sleeptime > 60)
731  {
732  fprintf(stderr, "%s: -s sleeptime incorrectly set\n", progname);
733  exit(2);
734  }
735  break;
736  case 't': /* Trigger file */
738  break;
739  case 'w': /* Max wait time */
740  maxwaittime = atoi(optarg);
741  if (maxwaittime < 0)
742  {
743  fprintf(stderr, "%s: -w maxwaittime incorrectly set\n", progname);
744  exit(2);
745  }
746  break;
747  default:
748  fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
749  exit(2);
750  break;
751  }
752  }
753 
754  /*
755  * Parameter checking - after checking to see if trigger file present
756  */
757  if (argc == 1)
758  {
759  fprintf(stderr, "%s: not enough command-line arguments\n", progname);
760  exit(2);
761  }
762 
763  /*
764  * We will go to the archiveLocation to get nextWALFileName.
765  * nextWALFileName may not exist yet, which would not be an error, so we
766  * separate the archiveLocation and nextWALFileName so we can check
767  * separately whether archiveLocation exists, if not that is an error
768  */
769  if (optind < argc)
770  {
771  archiveLocation = argv[optind];
772  optind++;
773  }
774  else
775  {
776  fprintf(stderr, "%s: must specify archive location\n", progname);
777  fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
778  exit(2);
779  }
780 
781  if (optind < argc)
782  {
783  nextWALFileName = argv[optind];
784  optind++;
785  }
786  else
787  {
788  fprintf(stderr, "%s: must specify WAL file name as second non-option argument (use \"%%f\")\n", progname);
789  fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
790  exit(2);
791  }
792 
793  if (optind < argc)
794  {
795  xlogFilePath = argv[optind];
796  optind++;
797  }
798  else
799  {
800  fprintf(stderr, "%s: must specify xlog destination as third non-option argument (use \"%%p\")\n", progname);
801  fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
802  exit(2);
803  }
804 
805  if (optind < argc)
806  {
807  restartWALFileName = argv[optind];
808  optind++;
809  }
810 
812 
813  if (debug)
814  {
815  fprintf(stderr, "Trigger file: %s\n", triggerPath ? triggerPath : "<not set>");
816  fprintf(stderr, "Waiting for WAL file: %s\n", nextWALFileName);
817  fprintf(stderr, "WAL file path: %s\n", WALFilePath);
818  fprintf(stderr, "Restoring to: %s\n", xlogFilePath);
819  fprintf(stderr, "Sleep interval: %d second%s\n",
820  sleeptime, (sleeptime > 1 ? "s" : " "));
821  fprintf(stderr, "Max wait interval: %d %s\n",
822  maxwaittime, (maxwaittime > 0 ? "seconds" : "forever"));
823  fprintf(stderr, "Command for restore: %s\n", restoreCommand);
824  fflush(stderr);
825  }
826 
827  /*
828  * Check for initial history file: always the first file to be requested
829  * It's OK if the file isn't there - all other files need to wait
830  */
832  {
835  exit(0);
836  else
837  {
838  if (debug)
839  {
840  fprintf(stderr, "history file not found\n");
841  fflush(stderr);
842  }
843  exit(1);
844  }
845  }
846 
847  /*
848  * Main wait loop
849  */
850  for (;;)
851  {
852  /* Check for trigger file or signal first */
854 #ifndef WIN32
855  if (signaled)
856  {
858  if (debug)
859  {
860  fprintf(stderr, "signaled to exit: fast failover\n");
861  fflush(stderr);
862  }
863  }
864 #endif
865 
866  /*
867  * Check for fast failover immediately, before checking if the
868  * requested WAL file is available
869  */
870  if (Failover == FastFailover)
871  exit(1);
872 
874  {
875  /*
876  * Once we have restored this file successfully we can remove some
877  * prior WAL files. If this restore fails we mustn't remove any
878  * file because some of them will be requested again immediately
879  * after the failed restore, or when we restart recovery.
880  */
882  {
883  if (need_cleanup)
885 
886  exit(0);
887  }
888  else
889  {
890  /* Something went wrong in copying the file */
891  exit(1);
892  }
893  }
894 
895  /* Check for smart failover if the next WAL file was not available */
896  if (Failover == SmartFailover)
897  exit(1);
898 
899  if (sleeptime <= 60)
900  pg_usleep(sleeptime * 1000000L);
901 
902  waittime += sleeptime;
903  if (waittime >= maxwaittime && maxwaittime > 0)
904  {
906  if (debug)
907  {
908  fprintf(stderr, "Timed out after %d seconds: fast failover\n",
909  waittime);
910  fflush(stderr);
911  }
912  }
913  if (debug)
914  {
915  fprintf(stderr, "WAL file not present yet.");
916  if (triggerPath)
917  fprintf(stderr, " Checking for trigger file...");
918  fprintf(stderr, "\n");
919  fflush(stderr);
920  }
921  }
922 }
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:97
static void usage(void)
Definition: pg_standby.c:605
#define SIGUSR1
Definition: win32.h:202
int main(int argc, char **argv)
Definition: pg_standby.c:650
static void CheckForExternalTrigger(void)
Definition: pg_standby.c:477
#define NoFailover
Definition: pg_standby.c:85
int waittime
Definition: pg_standby.c:43
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int keepfiles
Definition: pg_standby.c:46
const char * get_progname(const char *argv0)
Definition: path.c:453
static bool CustomizableNextWALFileReady(void)
Definition: pg_standby.c:180
int restoreCommandType
Definition: pg_standby.c:93
static void sigquit_handler(int sig)
Definition: pg_standby.c:641
char exclusiveCleanupFileName[MAXFNAMELEN]
Definition: pg_standby.c:64
int closedir(DIR *)
Definition: dirent.c:111
#define XLogFileNameById(fname, tli, log, seg)
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
int sleeptime
Definition: pg_standby.c:42
const char * progname
Definition: pg_standby.c:37
Definition: dirent.h:9
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define SIGQUIT
Definition: win32.h:189
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:72
static void CustomizableInitialize(void)
Definition: pg_standby.c:131
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
#define XLOG_BACKUP_LABEL
Definition: pg_standby.c:97
void pg_usleep(long microsec)
Definition: signal.c:53
#define SmartFailover
Definition: pg_standby.c:86
#define RESTORE_COMMAND_COPY
Definition: pg_standby.c:91
int maxretries
Definition: pg_standby.c:47
int optind
Definition: getopt.c:51
static bool RestoreWALFileForRecovery(void)
Definition: pg_standby.c:570
Definition: dirent.c:25
bool debug
Definition: pg_standby.c:48
#define IsXLogFileName(fname)
struct stat stat_buf
Definition: pg_standby.c:103
static int Failover
Definition: pg_standby.c:89
#define XLOG_DATA
Definition: pg_standby.c:95
#define MAXPGPATH
DIR * opendir(const char *)
Definition: dirent.c:33
#define SET_RESTORE_COMMAND(cmd, arg1, arg2)
Definition: pg_standby.c:100
char * c
static char * buf
Definition: pg_test_fsync.c:67
static void CustomizableCleanupPriorWALFiles(void)
Definition: pg_standby.c:257
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char restoreCommand[MAXPGPATH]
Definition: pg_standby.c:63
unsigned int uint32
Definition: c.h:258
#define FastFailover
Definition: pg_standby.c:87
#define IsBackupHistoryFileName(fname)
#define MAXFNAMELEN
#define XLOG_HISTORY
Definition: pg_standby.c:96
static void cleanup(void)
Definition: bootstrap.c:873
static int sig
Definition: pg_ctl.c:88
#define SIG_DFL
Definition: win32.h:183
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define ftruncate(a, b)
Definition: win32.h:59
bool need_cleanup
Definition: pg_standby.c:49
#define Assert(condition)
Definition: c.h:681
char WALFilePath[MAXPGPATH *2]
Definition: pg_standby.c:62
struct dirent * readdir(DIR *)
Definition: dirent.c:77
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define RESTORE_COMMAND_LINK
Definition: pg_standby.c:92
char * nextWALFileName
Definition: pg_standby.c:59
int WalSegSz
Definition: pg_standby.c:39
#define IsTLHistoryFileName(fname)
static void sighandler(int sig)
Definition: pg_standby.c:634
char * optarg
Definition: getopt.c:53
const char * strerror(int errnum)
Definition: strerror.c:19
char * xlogFilePath
Definition: pg_standby.c:58
char * triggerPath
Definition: pg_standby.c:57
char d_name[MAX_PATH]
Definition: dirent.h:14
#define close(a)
Definition: win32.h:12
char * archiveLocation
Definition: pg_standby.c:56
#define _(x)
Definition: elog.c:84
char * priorWALFileName
Definition: pg_standby.c:61
#define read(a, b, c)
Definition: win32.h:13
char * restartWALFileName
Definition: pg_standby.c:60
int nextWALFileType
Definition: pg_standby.c:98
static bool SetWALFileNameForCleanup(void)
Definition: pg_standby.c:343
static volatile sig_atomic_t signaled
Definition: pg_standby.c:53
static bool SetWALSegSize(void)
Definition: pg_standby.c:407
int maxwaittime
Definition: pg_standby.c:45