PostgreSQL Source Code  git master
pg_receivewal.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <limits.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 "fe_utils/option_utils.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 67 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 71 of file pg_receivewal.c.

Referenced by FindStreamingStart().

◆ RECONNECT_SLEEP_TIME

#define RECONNECT_SLEEP_TIME   5

Definition at line 33 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 183 of file pg_receivewal.c.

References Assert, closedir(), and pg_log_error.

Referenced by FindStreamingStart(), and main().

184 {
185  Assert(dest_dir != NULL && dest_folder != NULL);
186  if (closedir(dest_dir))
187  {
188  pg_log_error("could not close directory \"%s\": %m", dest_folder);
189  exit(1);
190  }
191 }
#define pg_log_error(...)
Definition: logging.h:80
int closedir(DIR *)
Definition: dirent.c:123
#define Assert(condition)
Definition: c.h:804

◆ disconnect_atexit()

static void disconnect_atexit ( void  )
static

Definition at line 60 of file pg_receivewal.c.

References conn, and PQfinish().

Referenced by main().

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

◆ FindStreamingStart()

static XLogRecPtr FindStreamingStart ( uint32 tli)
static

Definition at line 202 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::st_size, stat, WalSegSz, XLogFromFileName, and XLogSegNoOffsetToRecPtr.

Referenced by StreamLog().

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

◆ get_destination_dir()

static DIR * get_destination_dir ( char *  dest_folder)
static

Definition at line 163 of file pg_receivewal.c.

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

Referenced by FindStreamingStart(), and main().

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 471 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, option_parse_int(), 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.

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

◆ sigint_handler()

static void sigint_handler ( int  signum)
static

Definition at line 464 of file pg_receivewal.c.

References time_to_stop.

Referenced by main().

465 {
466  time_to_stop = true;
467 }
static volatile bool time_to_stop
Definition: pg_receivewal.c:41

◆ stop_streaming()

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

Definition at line 112 of file pg_receivewal.c.

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

Referenced by StreamLog().

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

◆ StreamLog()

static void StreamLog ( void  )
static

Definition at line 370 of file pg_receivewal.c.

References basedir, CheckServerVersionForStreaming(), compresslevel, conn, CreateWalDirectoryMethod(), StreamCtl::do_sync, do_sync, FindStreamingStart(), WalWriteMethod::finish, FreeWalDirectoryMethod(), GetConnection(), InvalidXLogRecPtr, LSN_FORMAT_ARGS, 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::sysidentifier, StreamCtl::timeline, verbose, StreamCtl::walmethod, WalSegSz, and XLogSegmentOffset.

Referenced by main().

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

◆ usage()

static void usage ( void  )
static

Definition at line 77 of file pg_receivewal.c.

References _, printf, progname, and standby_message_timeout.

Referenced by main().

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

Variable Documentation

◆ basedir

char* basedir = NULL
static

Definition at line 36 of file pg_receivewal.c.

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

◆ compresslevel

int compresslevel = 0
static

Definition at line 38 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

◆ do_create_slot

bool do_create_slot = false
static

Definition at line 42 of file pg_receivewal.c.

Referenced by main().

◆ do_drop_slot

bool do_drop_slot = false
static

Definition at line 44 of file pg_receivewal.c.

Referenced by main().

◆ do_sync

bool do_sync = true
static

Definition at line 45 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

◆ endpos

◆ noloop

int noloop = 0
static

Definition at line 39 of file pg_receivewal.c.

Referenced by main().

◆ replication_slot

char* replication_slot = NULL
static

Definition at line 47 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

◆ slot_exists_ok

bool slot_exists_ok = false
static

Definition at line 43 of file pg_receivewal.c.

Referenced by main().

◆ standby_message_timeout

int standby_message_timeout = 10 * 1000
static

Definition at line 40 of file pg_receivewal.c.

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

◆ synchronous

bool synchronous = false
static

Definition at line 46 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

◆ time_to_stop

volatile bool time_to_stop = false
static

Definition at line 41 of file pg_receivewal.c.

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

◆ verbose

int verbose = 0
static

Definition at line 37 of file pg_receivewal.c.

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