PostgreSQL Source Code  git master
pg_receivewal.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/xlog_internal.h"
#include "common/file_perm.h"
#include "common/logging.h"
#include "getopt_long.h"
#include "libpq-fe.h"
#include "receivelog.h"
#include "streamutil.h"
Include dependency graph for pg_receivewal.c:

Go to the source code of this file.

Macros

#define RECONNECT_SLEEP_TIME   5
 
#define IsCompressXLogFileName(fname)
 
#define IsPartialCompressXLogFileName(fname)
 

Functions

static void usage (void)
 
static DIRget_destination_dir (char *dest_folder)
 
static void close_destination_dir (DIR *dest_dir, char *dest_folder)
 
static XLogRecPtr FindStreamingStart (uint32 *tli)
 
static void StreamLog (void)
 
static bool stop_streaming (XLogRecPtr segendpos, uint32 timeline, bool segment_finished)
 
static void disconnect_atexit (void)
 
static void sigint_handler (int signum)
 
int main (int argc, char **argv)
 

Variables

static char * basedir = NULL
 
static int verbose = 0
 
static int compresslevel = 0
 
static int noloop = 0
 
static int standby_message_timeout = 10 * 1000
 
static volatile bool time_to_stop = false
 
static bool do_create_slot = false
 
static bool slot_exists_ok = false
 
static bool do_drop_slot = false
 
static bool do_sync = true
 
static bool synchronous = false
 
static char * replication_slot = NULL
 
static XLogRecPtr endpos = InvalidXLogRecPtr
 

Macro Definition Documentation

◆ IsCompressXLogFileName

#define IsCompressXLogFileName (   fname)
Value:
(strlen(fname) == XLOG_FNAME_LEN + strlen(".gz") && \
strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
strcmp((fname) + XLOG_FNAME_LEN, ".gz") == 0)
#define XLOG_FNAME_LEN

Definition at line 65 of file pg_receivewal.c.

Referenced by FindStreamingStart().

◆ IsPartialCompressXLogFileName

#define IsPartialCompressXLogFileName (   fname)
Value:
(strlen(fname) == XLOG_FNAME_LEN + strlen(".gz.partial") && \
strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
strcmp((fname) + XLOG_FNAME_LEN, ".gz.partial") == 0)
#define XLOG_FNAME_LEN

Definition at line 69 of file pg_receivewal.c.

Referenced by FindStreamingStart().

◆ RECONNECT_SLEEP_TIME

#define RECONNECT_SLEEP_TIME   5

Definition at line 31 of file pg_receivewal.c.

Referenced by main().

Function Documentation

◆ close_destination_dir()

static void close_destination_dir ( DIR dest_dir,
char *  dest_folder 
)
static

Definition at line 180 of file pg_receivewal.c.

References Assert, closedir(), and pg_log_error.

Referenced by FindStreamingStart(), and main().

181 {
182  Assert(dest_dir != NULL && dest_folder != NULL);
183  if (closedir(dest_dir))
184  {
185  pg_log_error("could not close directory \"%s\": %m", dest_folder);
186  exit(1);
187  }
188 }
#define pg_log_error(...)
Definition: logging.h:79
int closedir(DIR *)
Definition: dirent.c:113
#define Assert(condition)
Definition: c.h:739

◆ disconnect_atexit()

static void disconnect_atexit ( void  )
static

Definition at line 58 of file pg_receivewal.c.

References conn, and PQfinish().

Referenced by main().

59 {
60  if (conn != NULL)
61  PQfinish(conn);
62 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
PGconn * conn
Definition: streamutil.c:54

◆ FindStreamingStart()

static XLogRecPtr FindStreamingStart ( uint32 tli)
static

Definition at line 199 of file pg_receivewal.c.

References basedir, buf, close, close_destination_dir(), dirent::d_name, fd(), get_destination_dir(), InvalidXLogRecPtr, IsCompressXLogFileName, IsPartialCompressXLogFileName, IsPartialXLogFileName, IsXLogFileName, MAXPGPATH, PG_BINARY, pg_log_error, pg_log_warning, read, readdir(), snprintf, stat, WalSegSz, XLogFromFileName, and XLogSegNoOffsetToRecPtr.

Referenced by StreamLog().

200 {
201  DIR *dir;
202  struct dirent *dirent;
203  XLogSegNo high_segno = 0;
204  uint32 high_tli = 0;
205  bool high_ispartial = false;
206 
208 
209  while (errno = 0, (dirent = readdir(dir)) != NULL)
210  {
211  uint32 tli;
212  XLogSegNo segno;
213  bool ispartial;
214  bool iscompress;
215 
216  /*
217  * Check if the filename looks like an xlog file, or a .partial file.
218  */
219  if (IsXLogFileName(dirent->d_name))
220  {
221  ispartial = false;
222  iscompress = false;
223  }
224  else if (IsPartialXLogFileName(dirent->d_name))
225  {
226  ispartial = true;
227  iscompress = false;
228  }
229  else if (IsCompressXLogFileName(dirent->d_name))
230  {
231  ispartial = false;
232  iscompress = true;
233  }
234  else if (IsPartialCompressXLogFileName(dirent->d_name))
235  {
236  ispartial = true;
237  iscompress = true;
238  }
239  else
240  continue;
241 
242  /*
243  * Looks like an xlog file. Parse its position.
244  */
245  XLogFromFileName(dirent->d_name, &tli, &segno, WalSegSz);
246 
247  /*
248  * Check that the segment has the right size, if it's supposed to be
249  * completed. For non-compressed segments just check the on-disk size
250  * and see if it matches a completed segment. For compressed segments,
251  * look at the last 4 bytes of the compressed file, which is where the
252  * uncompressed size is located for gz files with a size lower than
253  * 4GB, and then compare it to the size of a completed segment. The 4
254  * last bytes correspond to the ISIZE member according to
255  * http://www.zlib.org/rfc-gzip.html.
256  */
257  if (!ispartial && !iscompress)
258  {
259  struct stat statbuf;
260  char fullpath[MAXPGPATH * 2];
261 
262  snprintf(fullpath, sizeof(fullpath), "%s/%s", basedir, dirent->d_name);
263  if (stat(fullpath, &statbuf) != 0)
264  {
265  pg_log_error("could not stat file \"%s\": %m", fullpath);
266  exit(1);
267  }
268 
269  if (statbuf.st_size != WalSegSz)
270  {
271  pg_log_warning("segment file \"%s\" has incorrect size %d, skipping",
272  dirent->d_name, (int) statbuf.st_size);
273  continue;
274  }
275  }
276  else if (!ispartial && iscompress)
277  {
278  int fd;
279  char buf[4];
280  int bytes_out;
281  char fullpath[MAXPGPATH * 2];
282  int r;
283 
284  snprintf(fullpath, sizeof(fullpath), "%s/%s", basedir, dirent->d_name);
285 
286  fd = open(fullpath, O_RDONLY | PG_BINARY, 0);
287  if (fd < 0)
288  {
289  pg_log_error("could not open compressed file \"%s\": %m",
290  fullpath);
291  exit(1);
292  }
293  if (lseek(fd, (off_t) (-4), SEEK_END) < 0)
294  {
295  pg_log_error("could not seek in compressed file \"%s\": %m",
296  fullpath);
297  exit(1);
298  }
299  r = read(fd, (char *) buf, sizeof(buf));
300  if (r != sizeof(buf))
301  {
302  if (r < 0)
303  pg_log_error("could not read compressed file \"%s\": %m",
304  fullpath);
305  else
306  pg_log_error("could not read compressed file \"%s\": read %d of %zu",
307  fullpath, r, sizeof(buf));
308  exit(1);
309  }
310 
311  close(fd);
312  bytes_out = (buf[3] << 24) | (buf[2] << 16) |
313  (buf[1] << 8) | buf[0];
314 
315  if (bytes_out != WalSegSz)
316  {
317  pg_log_warning("compressed segment file \"%s\" has incorrect uncompressed size %d, skipping",
318  dirent->d_name, bytes_out);
319  continue;
320  }
321  }
322 
323  /* Looks like a valid segment. Remember that we saw it. */
324  if ((segno > high_segno) ||
325  (segno == high_segno && tli > high_tli) ||
326  (segno == high_segno && tli == high_tli && high_ispartial && !ispartial))
327  {
328  high_segno = segno;
329  high_tli = tli;
330  high_ispartial = ispartial;
331  }
332  }
333 
334  if (errno)
335  {
336  pg_log_error("could not read directory \"%s\": %m", basedir);
337  exit(1);
338  }
339 
341 
342  if (high_segno > 0)
343  {
344  XLogRecPtr high_ptr;
345 
346  /*
347  * Move the starting pointer to the start of the next segment, if the
348  * highest one we saw was completed. Otherwise start streaming from
349  * the beginning of the .partial segment.
350  */
351  if (!high_ispartial)
352  high_segno++;
353 
354  XLogSegNoOffsetToRecPtr(high_segno, 0, WalSegSz, high_ptr);
355 
356  *tli = high_tli;
357  return high_ptr;
358  }
359  else
360  return InvalidXLogRecPtr;
361 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define pg_log_error(...)
Definition: logging.h:79
#define IsPartialCompressXLogFileName(fname)
Definition: pg_receivewal.c:69
static void close_destination_dir(DIR *dest_dir, char *dest_folder)
Definition: dirent.h:9
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1222
Definition: dirent.c:25
#define IsXLogFileName(fname)
#define MAXPGPATH
#define XLogFromFileName(fname, tli, logSegNo, wal_segsz_bytes)
static char * buf
Definition: pg_test_fsync.c:67
uint64 XLogSegNo
Definition: xlogdefs.h:41
static char * basedir
Definition: pg_receivewal.c:34
unsigned int uint32
Definition: c.h:359
#define stat(a, b)
Definition: win32_port.h:255
static DIR * get_destination_dir(char *dest_folder)
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
struct dirent * readdir(DIR *)
Definition: dirent.c:77
int WalSegSz
Definition: pg_standby.c:38
#define IsPartialXLogFileName(fname)
char d_name[MAX_PATH]
Definition: dirent.h:14
#define close(a)
Definition: win32.h:12
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define snprintf
Definition: port.h:192
#define read(a, b, c)
Definition: win32.h:13
#define IsCompressXLogFileName(fname)
Definition: pg_receivewal.c:65

◆ get_destination_dir()

static DIR * get_destination_dir ( char *  dest_folder)
static

Definition at line 160 of file pg_receivewal.c.

References Assert, basedir, opendir(), and pg_log_error.

Referenced by FindStreamingStart(), and main().

161 {
162  DIR *dir;
163 
164  Assert(dest_folder != NULL);
165  dir = opendir(dest_folder);
166  if (dir == NULL)
167  {
168  pg_log_error("could not open directory \"%s\": %m", basedir);
169  exit(1);
170  }
171 
172  return dir;
173 }
#define pg_log_error(...)
Definition: logging.h:79
Definition: dirent.c:25
DIR * opendir(const char *)
Definition: dirent.c:33
static char * basedir
Definition: pg_receivewal.c:34
#define Assert(condition)
Definition: c.h:739

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 467 of file pg_receivewal.c.

References _, basedir, close_destination_dir(), compresslevel, conn, connection_string, CreateReplicationSlot(), dbgetpassword, dbhost, dbport, dbuser, disconnect_atexit(), do_create_slot, do_drop_slot, do_sync, DropReplicationSlot(), endpos, fprintf, get_destination_dir(), get_progname(), GetConnection(), getopt_long(), no_argument, noloop, optarg, optind, pg_log_error, pg_log_info, pg_logging_init(), pg_mode_mask, pg_strdup(), PG_TEXTDOMAIN, pg_usleep(), pqsignal(), progname, RECONNECT_SLEEP_TIME, replication_slot, required_argument, RetrieveWalSegSize(), RunIdentifySystem(), set_pglocale_pgservice(), sigint_handler(), slot_exists_ok, standby_message_timeout, StreamLog(), synchronous, time_to_stop, usage(), and verbose.

468 {
469  static struct option long_options[] = {
470  {"help", no_argument, NULL, '?'},
471  {"version", no_argument, NULL, 'V'},
472  {"directory", required_argument, NULL, 'D'},
473  {"dbname", required_argument, NULL, 'd'},
474  {"endpos", required_argument, NULL, 'E'},
475  {"host", required_argument, NULL, 'h'},
476  {"port", required_argument, NULL, 'p'},
477  {"username", required_argument, NULL, 'U'},
478  {"no-loop", no_argument, NULL, 'n'},
479  {"no-password", no_argument, NULL, 'w'},
480  {"password", no_argument, NULL, 'W'},
481  {"status-interval", required_argument, NULL, 's'},
482  {"slot", required_argument, NULL, 'S'},
483  {"verbose", no_argument, NULL, 'v'},
484  {"compress", required_argument, NULL, 'Z'},
485 /* action */
486  {"create-slot", no_argument, NULL, 1},
487  {"drop-slot", no_argument, NULL, 2},
488  {"if-not-exists", no_argument, NULL, 3},
489  {"synchronous", no_argument, NULL, 4},
490  {"no-sync", no_argument, NULL, 5},
491  {NULL, 0, NULL, 0}
492  };
493 
494  int c;
495  int option_index;
496  char *db_name;
497  uint32 hi,
498  lo;
499 
500  pg_logging_init(argv[0]);
501  progname = get_progname(argv[0]);
502  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
503 
504  if (argc > 1)
505  {
506  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
507  {
508  usage();
509  exit(0);
510  }
511  else if (strcmp(argv[1], "-V") == 0 ||
512  strcmp(argv[1], "--version") == 0)
513  {
514  puts("pg_receivewal (PostgreSQL) " PG_VERSION);
515  exit(0);
516  }
517  }
518 
519  while ((c = getopt_long(argc, argv, "D:d:E:h:p:U:s:S:nwWvZ:",
520  long_options, &option_index)) != -1)
521  {
522  switch (c)
523  {
524  case 'D':
526  break;
527  case 'd':
529  break;
530  case 'h':
532  break;
533  case 'p':
534  if (atoi(optarg) <= 0)
535  {
536  pg_log_error("invalid port number \"%s\"", optarg);
537  exit(1);
538  }
540  break;
541  case 'U':
543  break;
544  case 'w':
545  dbgetpassword = -1;
546  break;
547  case 'W':
548  dbgetpassword = 1;
549  break;
550  case 's':
551  standby_message_timeout = atoi(optarg) * 1000;
552  if (standby_message_timeout < 0)
553  {
554  pg_log_error("invalid status interval \"%s\"", optarg);
555  exit(1);
556  }
557  break;
558  case 'S':
560  break;
561  case 'E':
562  if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
563  {
564  pg_log_error("could not parse end position \"%s\"", optarg);
565  exit(1);
566  }
567  endpos = ((uint64) hi) << 32 | lo;
568  break;
569  case 'n':
570  noloop = 1;
571  break;
572  case 'v':
573  verbose++;
574  break;
575  case 'Z':
576  compresslevel = atoi(optarg);
577  if (compresslevel < 0 || compresslevel > 9)
578  {
579  pg_log_error("invalid compression level \"%s\"", optarg);
580  exit(1);
581  }
582  break;
583 /* action */
584  case 1:
585  do_create_slot = true;
586  break;
587  case 2:
588  do_drop_slot = true;
589  break;
590  case 3:
591  slot_exists_ok = true;
592  break;
593  case 4:
594  synchronous = true;
595  break;
596  case 5:
597  do_sync = false;
598  break;
599  default:
600 
601  /*
602  * getopt_long already emitted a complaint
603  */
604  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
605  progname);
606  exit(1);
607  }
608  }
609 
610  /*
611  * Any non-option arguments?
612  */
613  if (optind < argc)
614  {
615  pg_log_error("too many command-line arguments (first is \"%s\")",
616  argv[optind]);
617  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
618  progname);
619  exit(1);
620  }
621 
623  {
624  pg_log_error("cannot use --create-slot together with --drop-slot");
625  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
626  progname);
627  exit(1);
628  }
629 
630  if (replication_slot == NULL && (do_drop_slot || do_create_slot))
631  {
632  /* translator: second %s is an option name */
633  pg_log_error("%s needs a slot to be specified using --slot",
634  do_drop_slot ? "--drop-slot" : "--create-slot");
635  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
636  progname);
637  exit(1);
638  }
639 
640  if (synchronous && !do_sync)
641  {
642  pg_log_error("cannot use --synchronous together with --no-sync");
643  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
644  progname);
645  exit(1);
646  }
647 
648  /*
649  * Required arguments
650  */
651  if (basedir == NULL && !do_drop_slot && !do_create_slot)
652  {
653  pg_log_error("no target directory specified");
654  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
655  progname);
656  exit(1);
657  }
658 
659 #ifndef HAVE_LIBZ
660  if (compresslevel != 0)
661  {
662  pg_log_error("this build does not support compression");
663  exit(1);
664  }
665 #endif
666 
667  /*
668  * Check existence of destination folder.
669  */
670  if (!do_drop_slot && !do_create_slot)
671  {
673 
675  }
676 
677 #ifndef WIN32
678  pqsignal(SIGINT, sigint_handler);
679 #endif
680 
681  /*
682  * Obtain a connection before doing anything.
683  */
684  conn = GetConnection();
685  if (!conn)
686  /* error message already written in GetConnection() */
687  exit(1);
688  atexit(disconnect_atexit);
689 
690  /*
691  * Run IDENTIFY_SYSTEM to make sure we've successfully have established a
692  * replication connection and haven't connected using a database specific
693  * connection.
694  */
695  if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
696  exit(1);
697 
698  /*
699  * Set umask so that directories/files are created with the same
700  * permissions as directories/files in the source data directory.
701  *
702  * pg_mode_mask is set to owner-only by default and then updated in
703  * GetConnection() where we get the mode from the server-side with
704  * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
705  */
706  umask(pg_mode_mask);
707 
708  /* determine remote server's xlog segment size */
709  if (!RetrieveWalSegSize(conn))
710  exit(1);
711 
712  /*
713  * Check that there is a database associated with connection, none should
714  * be defined in this context.
715  */
716  if (db_name)
717  {
718  pg_log_error("replication connection using slot \"%s\" is unexpectedly database specific",
720  exit(1);
721  }
722 
723  /*
724  * Drop a replication slot.
725  */
726  if (do_drop_slot)
727  {
728  if (verbose)
729  pg_log_info("dropping replication slot \"%s\"", replication_slot);
730 
732  exit(1);
733  exit(0);
734  }
735 
736  /* Create a replication slot */
737  if (do_create_slot)
738  {
739  if (verbose)
740  pg_log_info("creating replication slot \"%s\"", replication_slot);
741 
742  if (!CreateReplicationSlot(conn, replication_slot, NULL, false, true, false,
744  exit(1);
745  exit(0);
746  }
747 
748  /*
749  * Don't close the connection here so that subsequent StreamLog() can
750  * reuse it.
751  */
752 
753  while (true)
754  {
755  StreamLog();
756  if (time_to_stop)
757  {
758  /*
759  * We've been Ctrl-C'ed or end of streaming position has been
760  * willingly reached, so exit without an error code.
761  */
762  exit(0);
763  }
764  else if (noloop)
765  {
766  pg_log_error("disconnected");
767  exit(1);
768  }
769  else
770  {
771  /* translator: check source for value for %d */
772  pg_log_info("disconnected; waiting %d seconds to try again",
774  pg_usleep(RECONNECT_SLEEP_TIME * 1000000);
775  }
776  }
777 }
static char * replication_slot
Definition: pg_receivewal.c:45
static int standby_message_timeout
Definition: pg_receivewal.c:38
const char * get_progname(const char *argv0)
Definition: path.c:453
static void DropReplicationSlot(DropReplicationSlotCmd *cmd)
Definition: walsender.c:1075
#define pg_log_error(...)
Definition: logging.h:79
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
static void usage(void)
Definition: pg_receivewal.c:75
void pg_logging_init(const char *argv0)
Definition: logging.c:39
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:407
static bool synchronous
Definition: pg_receivewal.c:44
const char * progname
Definition: pg_standby.c:36
#define fprintf
Definition: port.h:196
static void close_destination_dir(DIR *dest_dir, char *dest_folder)
static void sigint_handler(int signum)
static bool slot_exists_ok
Definition: pg_receivewal.c:41
static void StreamLog(void)
void pg_usleep(long microsec)
Definition: signal.c:53
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
Definition: dirent.c:25
char * connection_string
Definition: streamutil.c:46
bool RetrieveWalSegSize(PGconn *conn)
Definition: streamutil.c:277
PGconn * conn
Definition: streamutil.c:54
static XLogRecPtr endpos
Definition: pg_receivewal.c:46
static int noloop
Definition: pg_receivewal.c:37
char * c
static char * basedir
Definition: pg_receivewal.c:34
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:359
static volatile bool time_to_stop
Definition: pg_receivewal.c:39
static bool do_drop_slot
Definition: pg_receivewal.c:42
int dbgetpassword
Definition: streamutil.c:51
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1166
char * dbport
Definition: streamutil.c:49
static void disconnect_atexit(void)
Definition: pg_receivewal.c:58
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static DIR * get_destination_dir(char *dest_folder)
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:105
char * dbhost
Definition: streamutil.c:47
#define RECONNECT_SLEEP_TIME
Definition: pg_receivewal.c:31
static int compresslevel
Definition: pg_receivewal.c:36
static bool do_create_slot
Definition: pg_receivewal.c:40
static void CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
Definition: walsender.c:875
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
char * dbuser
Definition: streamutil.c:48
char * optarg
Definition: getopt.c:52
static bool do_sync
Definition: pg_receivewal.c:43
static int verbose
Definition: pg_receivewal.c:35
#define _(x)
Definition: elog.c:87
int pg_mode_mask
Definition: file_perm.c:25
#define pg_log_info(...)
Definition: logging.h:87

◆ sigint_handler()

static void sigint_handler ( int  signum)
static

Definition at line 460 of file pg_receivewal.c.

References time_to_stop.

Referenced by main().

461 {
462  time_to_stop = true;
463 }
static volatile bool time_to_stop
Definition: pg_receivewal.c:39

◆ stop_streaming()

static bool stop_streaming ( XLogRecPtr  segendpos,
uint32  timeline,
bool  segment_finished 
)
static

Definition at line 109 of file pg_receivewal.c.

References endpos, InvalidXLogRecPtr, pg_log_info, time_to_stop, verbose, and XLogRecPtrIsInvalid.

Referenced by StreamLog().

110 {
111  static uint32 prevtimeline = 0;
112  static XLogRecPtr prevpos = InvalidXLogRecPtr;
113 
114  /* we assume that we get called once at the end of each segment */
115  if (verbose && segment_finished)
116  pg_log_info("finished segment at %X/%X (timeline %u)",
117  (uint32) (xlogpos >> 32), (uint32) xlogpos,
118  timeline);
119 
120  if (!XLogRecPtrIsInvalid(endpos) && endpos < xlogpos)
121  {
122  if (verbose)
123  pg_log_info("stopped log streaming at %X/%X (timeline %u)",
124  (uint32) (xlogpos >> 32), (uint32) xlogpos,
125  timeline);
126  time_to_stop = true;
127  return true;
128  }
129 
130  /*
131  * Note that we report the previous, not current, position here. After a
132  * timeline switch, xlogpos points to the beginning of the segment because
133  * that's where we always begin streaming. Reporting the end of previous
134  * timeline isn't totally accurate, because the next timeline can begin
135  * slightly before the end of the WAL that we received on the previous
136  * timeline, but it's close enough for reporting purposes.
137  */
138  if (verbose && prevtimeline != 0 && prevtimeline != timeline)
139  pg_log_info("switched to timeline %u at %X/%X",
140  timeline,
141  (uint32) (prevpos >> 32), (uint32) prevpos);
142 
143  prevtimeline = timeline;
144  prevpos = xlogpos;
145 
146  if (time_to_stop)
147  {
148  if (verbose)
149  pg_log_info("received interrupt signal, exiting");
150  return true;
151  }
152  return false;
153 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static XLogRecPtr endpos
Definition: pg_receivewal.c:46
unsigned int uint32
Definition: c.h:359
static volatile bool time_to_stop
Definition: pg_receivewal.c:39
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static int verbose
Definition: pg_receivewal.c:35
#define pg_log_info(...)
Definition: logging.h:87

◆ StreamLog()

static void StreamLog ( void  )
static

Definition at line 367 of file pg_receivewal.c.

References basedir, CheckServerVersionForStreaming(), compresslevel, conn, CreateWalDirectoryMethod(), StreamCtl::do_sync, do_sync, FindStreamingStart(), WalWriteMethod::finish, FreeWalDirectoryMethod(), GetConnection(), InvalidXLogRecPtr, StreamCtl::mark_done, MemSet, StreamCtl::partial_suffix, pg_free(), pg_log_info, PGINVALID_SOCKET, PQfinish(), ReceiveXlogStream(), replication_slot, StreamCtl::replication_slot, RunIdentifySystem(), StreamCtl::standby_message_timeout, standby_message_timeout, StreamCtl::startpos, StreamCtl::stop_socket, stop_streaming(), StreamCtl::stream_stop, StreamCtl::synchronous, synchronous, StreamCtl::timeline, verbose, StreamCtl::walmethod, WalSegSz, and XLogSegmentOffset.

Referenced by main().

368 {
369  XLogRecPtr serverpos;
370  TimeLineID servertli;
371  StreamCtl stream;
372 
373  MemSet(&stream, 0, sizeof(stream));
374 
375  /*
376  * Connect in replication mode to the server
377  */
378  if (conn == NULL)
379  conn = GetConnection();
380  if (!conn)
381  /* Error message already written in GetConnection() */
382  return;
383 
385  {
386  /*
387  * Error message already written in CheckServerVersionForStreaming().
388  * There's no hope of recovering from a version mismatch, so don't
389  * retry.
390  */
391  exit(1);
392  }
393 
394  /*
395  * Identify server, obtaining start LSN position and current timeline ID
396  * at the same time, necessary if not valid data can be found in the
397  * existing output directory.
398  */
399  if (!RunIdentifySystem(conn, NULL, &servertli, &serverpos, NULL))
400  exit(1);
401 
402  /*
403  * Figure out where to start streaming.
404  */
405  stream.startpos = FindStreamingStart(&stream.timeline);
406  if (stream.startpos == InvalidXLogRecPtr)
407  {
408  stream.startpos = serverpos;
409  stream.timeline = servertli;
410  }
411 
412  /*
413  * Always start streaming at the beginning of a segment
414  */
415  stream.startpos -= XLogSegmentOffset(stream.startpos, WalSegSz);
416 
417  /*
418  * Start the replication
419  */
420  if (verbose)
421  pg_log_info("starting log streaming at %X/%X (timeline %u)",
422  (uint32) (stream.startpos >> 32), (uint32) stream.startpos,
423  stream.timeline);
424 
425  stream.stream_stop = stop_streaming;
426  stream.stop_socket = PGINVALID_SOCKET;
428  stream.synchronous = synchronous;
429  stream.do_sync = do_sync;
430  stream.mark_done = false;
432  stream.do_sync);
433  stream.partial_suffix = ".partial";
435 
436  ReceiveXlogStream(conn, &stream);
437 
438  if (!stream.walmethod->finish())
439  {
440  pg_log_info("could not finish writing WAL files: %m");
441  return;
442  }
443 
444  PQfinish(conn);
445  conn = NULL;
446 
448  pg_free(stream.walmethod);
449 
450  conn = NULL;
451 }
static char * replication_slot
Definition: pg_receivewal.c:45
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static int standby_message_timeout
Definition: pg_receivewal.c:38
uint32 TimeLineID
Definition: xlogdefs.h:52
bool do_sync
Definition: receivelog.h:38
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:407
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
#define MemSet(start, val, len)
Definition: c.h:962
static bool synchronous
Definition: pg_receivewal.c:44
XLogRecPtr startpos
Definition: receivelog.h:31
char * partial_suffix
Definition: receivelog.h:47
static bool stop_streaming(XLogRecPtr segendpos, uint32 timeline, bool segment_finished)
TimeLineID timeline
Definition: receivelog.h:32
WalWriteMethod * CreateWalDirectoryMethod(const char *basedir, int compression, bool sync)
Definition: walmethods.c:349
PGconn * conn
Definition: streamutil.c:54
char * replication_slot
Definition: receivelog.h:48
bool mark_done
Definition: receivelog.h:37
static XLogRecPtr FindStreamingStart(uint32 *tli)
static char * basedir
Definition: pg_receivewal.c:34
unsigned int uint32
Definition: c.h:359
stream_stop_callback stream_stop
Definition: receivelog.h:41
WalWriteMethod * walmethod
Definition: receivelog.h:46
#define PGINVALID_SOCKET
Definition: port.h:33
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:105
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
Definition: receivelog.c:436
static int compresslevel
Definition: pg_receivewal.c:36
bool synchronous
Definition: receivelog.h:36
void pg_free(void *ptr)
Definition: fe_memutils.c:105
bool(* finish)(void)
Definition: walmethods.h:75
pgsocket stop_socket
Definition: receivelog.h:43
int WalSegSz
Definition: pg_standby.c:38
static bool do_sync
Definition: pg_receivewal.c:43
int standby_message_timeout
Definition: receivelog.h:35
static int verbose
Definition: pg_receivewal.c:35
#define pg_log_info(...)
Definition: logging.h:87
void FreeWalDirectoryMethod(void)
Definition: walmethods.c:373
bool CheckServerVersionForStreaming(PGconn *conn)
Definition: receivelog.c:358

◆ usage()

static void usage ( void  )
static

Definition at line 75 of file pg_receivewal.c.

References _, printf, progname, and standby_message_timeout.

Referenced by main().

76 {
77  printf(_("%s receives PostgreSQL streaming write-ahead logs.\n\n"),
78  progname);
79  printf(_("Usage:\n"));
80  printf(_(" %s [OPTION]...\n"), progname);
81  printf(_("\nOptions:\n"));
82  printf(_(" -D, --directory=DIR receive write-ahead log files into this directory\n"));
83  printf(_(" -E, --endpos=LSN exit after receiving the specified LSN\n"));
84  printf(_(" --if-not-exists do not error if slot already exists when creating a slot\n"));
85  printf(_(" -n, --no-loop do not loop on connection lost\n"));
86  printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
87  printf(_(" -s, --status-interval=SECS\n"
88  " time between status packets sent to server (default: %d)\n"), (standby_message_timeout / 1000));
89  printf(_(" -S, --slot=SLOTNAME replication slot to use\n"));
90  printf(_(" --synchronous flush write-ahead log immediately after writing\n"));
91  printf(_(" -v, --verbose output verbose messages\n"));
92  printf(_(" -V, --version output version information, then exit\n"));
93  printf(_(" -Z, --compress=0-9 compress logs with given compression level\n"));
94  printf(_(" -?, --help show this help, then exit\n"));
95  printf(_("\nConnection options:\n"));
96  printf(_(" -d, --dbname=CONNSTR connection string\n"));
97  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
98  printf(_(" -p, --port=PORT database server port number\n"));
99  printf(_(" -U, --username=NAME connect as specified database user\n"));
100  printf(_(" -w, --no-password never prompt for password\n"));
101  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
102  printf(_("\nOptional actions:\n"));
103  printf(_(" --create-slot create a new replication slot (for the slot's name see --slot)\n"));
104  printf(_(" --drop-slot drop the replication slot (for the slot's name see --slot)\n"));
105  printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
106 }
static int standby_message_timeout
Definition: pg_receivewal.c:38
#define printf(...)
Definition: port.h:198
const char * progname
Definition: pg_standby.c:36
#define _(x)
Definition: elog.c:87

Variable Documentation

◆ basedir

char* basedir = NULL
static

Definition at line 34 of file pg_receivewal.c.

Referenced by FindStreamingStart(), get_destination_dir(), main(), and StreamLog().

◆ compresslevel

int compresslevel = 0
static

Definition at line 36 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

◆ do_create_slot

bool do_create_slot = false
static

Definition at line 40 of file pg_receivewal.c.

Referenced by main().

◆ do_drop_slot

bool do_drop_slot = false
static

Definition at line 42 of file pg_receivewal.c.

Referenced by main().

◆ do_sync

bool do_sync = true
static

Definition at line 43 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

◆ endpos

◆ noloop

int noloop = 0
static

Definition at line 37 of file pg_receivewal.c.

Referenced by main().

◆ replication_slot

char* replication_slot = NULL
static

Definition at line 45 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

◆ slot_exists_ok

bool slot_exists_ok = false
static

Definition at line 41 of file pg_receivewal.c.

Referenced by main().

◆ standby_message_timeout

int standby_message_timeout = 10 * 1000
static

Definition at line 38 of file pg_receivewal.c.

Referenced by main(), StreamLog(), and usage().

◆ synchronous

bool synchronous = false
static

Definition at line 44 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

◆ time_to_stop

volatile bool time_to_stop = false
static

Definition at line 39 of file pg_receivewal.c.

Referenced by main(), pgarch_MainLoop(), sigint_handler(), and stop_streaming().

◆ verbose

int verbose = 0
static

Definition at line 35 of file pg_receivewal.c.

Referenced by main(), stop_streaming(), and StreamLog().