PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_receivewal.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include "libpq-fe.h"
#include "access/xlog_internal.h"
#include "getopt_long.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 disconnect_and_exit(code)
 
#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 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_abort = false
 
static bool do_create_slot = false
 
static bool slot_exists_ok = false
 
static bool do_drop_slot = false
 
static bool synchronous = false
 
static char * replication_slot = NULL
 

Macro Definition Documentation

#define disconnect_and_exit (   code)
Value:
{ \
if (conn != NULL) PQfinish(conn); \
exit(code); \
}
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3521
PGconn * conn
Definition: streamutil.c:42
#define NULL
Definition: c.h:229

Definition at line 55 of file pg_receivewal.c.

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

#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 62 of file pg_receivewal.c.

Referenced by FindStreamingStart().

#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 66 of file pg_receivewal.c.

Referenced by FindStreamingStart().

#define RECONNECT_SLEEP_TIME   5

Definition at line 31 of file pg_receivewal.c.

Referenced by main().

Function Documentation

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

Definition at line 166 of file pg_receivewal.c.

References _, Assert, closedir(), disconnect_and_exit, NULL, progname, and strerror().

Referenced by FindStreamingStart(), and main().

167 {
168  Assert(dest_dir != NULL && dest_folder != NULL);
169  if (closedir(dest_dir))
170  {
171  fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
172  progname, dest_folder, strerror(errno));
174  }
175 }
int closedir(DIR *)
Definition: dirent.c:113
const char * progname
Definition: pg_standby.c:37
#define disconnect_and_exit(code)
Definition: pg_receivewal.c:55
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
static XLogRecPtr FindStreamingStart ( uint32 tli)
static

Definition at line 186 of file pg_receivewal.c.

References _, basedir, buf, close, close_destination_dir(), dirent::d_name, disconnect_and_exit, fd(), get_destination_dir(), InvalidXLogRecPtr, IsCompressXLogFileName, IsPartialCompressXLogFileName, IsPartialXLogFileName, IsXLogFileName, MAXPGPATH, NULL, PG_BINARY, progname, read, readdir(), snprintf(), strerror(), XLogFromFileName, and XLogSegNoOffsetToRecPtr.

Referenced by StreamLog().

187 {
188  DIR *dir;
189  struct dirent *dirent;
190  XLogSegNo high_segno = 0;
191  uint32 high_tli = 0;
192  bool high_ispartial = false;
193 
195 
196  while (errno = 0, (dirent = readdir(dir)) != NULL)
197  {
198  uint32 tli;
199  XLogSegNo segno;
200  bool ispartial;
201  bool iscompress;
202 
203  /*
204  * Check if the filename looks like an xlog file, or a .partial file.
205  */
206  if (IsXLogFileName(dirent->d_name))
207  {
208  ispartial = false;
209  iscompress = false;
210  }
211  else if (IsPartialXLogFileName(dirent->d_name))
212  {
213  ispartial = true;
214  iscompress = false;
215  }
216  else if (IsCompressXLogFileName(dirent->d_name))
217  {
218  ispartial = false;
219  iscompress = true;
220  }
221  else if (IsPartialCompressXLogFileName(dirent->d_name))
222  {
223  ispartial = true;
224  iscompress = true;
225  }
226  else
227  continue;
228 
229  /*
230  * Looks like an xlog file. Parse its position.
231  */
232  XLogFromFileName(dirent->d_name, &tli, &segno);
233 
234  /*
235  * Check that the segment has the right size, if it's supposed to be
236  * completed. For non-compressed segments just check the on-disk size
237  * and see if it matches a completed segment. For compressed segments,
238  * look at the last 4 bytes of the compressed file, which is where the
239  * uncompressed size is located for gz files with a size lower than
240  * 4GB, and then compare it to the size of a completed segment. The 4
241  * last bytes correspond to the ISIZE member according to
242  * http://www.zlib.org/rfc-gzip.html.
243  */
244  if (!ispartial && !iscompress)
245  {
246  struct stat statbuf;
247  char fullpath[MAXPGPATH * 2];
248 
249  snprintf(fullpath, sizeof(fullpath), "%s/%s", basedir, dirent->d_name);
250  if (stat(fullpath, &statbuf) != 0)
251  {
252  fprintf(stderr, _("%s: could not stat file \"%s\": %s\n"),
253  progname, fullpath, strerror(errno));
255  }
256 
257  if (statbuf.st_size != XLOG_SEG_SIZE)
258  {
259  fprintf(stderr,
260  _("%s: segment file \"%s\" has incorrect size %d, skipping\n"),
261  progname, dirent->d_name, (int) statbuf.st_size);
262  continue;
263  }
264  }
265  else if (!ispartial && iscompress)
266  {
267  int fd;
268  char buf[4];
269  int bytes_out;
270  char fullpath[MAXPGPATH * 2];
271 
272  snprintf(fullpath, sizeof(fullpath), "%s/%s", basedir, dirent->d_name);
273 
274  fd = open(fullpath, O_RDONLY | PG_BINARY);
275  if (fd < 0)
276  {
277  fprintf(stderr, _("%s: could not open compressed file \"%s\": %s\n"),
278  progname, fullpath, strerror(errno));
280  }
281  if (lseek(fd, (off_t) (-4), SEEK_END) < 0)
282  {
283  fprintf(stderr, _("%s: could not seek compressed file \"%s\": %s\n"),
284  progname, fullpath, strerror(errno));
286  }
287  if (read(fd, (char *) buf, sizeof(buf)) != sizeof(buf))
288  {
289  fprintf(stderr, _("%s: could not read compressed file \"%s\": %s\n"),
290  progname, fullpath, strerror(errno));
292  }
293 
294  close(fd);
295  bytes_out = (buf[3] << 24) | (buf[2] << 16) |
296  (buf[1] << 8) | buf[0];
297 
298  if (bytes_out != XLOG_SEG_SIZE)
299  {
300  fprintf(stderr,
301  _("%s: compressed segment file \"%s\" has incorrect uncompressed size %d, skipping\n"),
302  progname, dirent->d_name, bytes_out);
303  continue;
304  }
305  }
306 
307  /* Looks like a valid segment. Remember that we saw it. */
308  if ((segno > high_segno) ||
309  (segno == high_segno && tli > high_tli) ||
310  (segno == high_segno && tli == high_tli && high_ispartial && !ispartial))
311  {
312  high_segno = segno;
313  high_tli = tli;
314  high_ispartial = ispartial;
315  }
316  }
317 
318  if (errno)
319  {
320  fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
321  progname, basedir, strerror(errno));
323  }
324 
326 
327  if (high_segno > 0)
328  {
329  XLogRecPtr high_ptr;
330 
331  /*
332  * Move the starting pointer to the start of the next segment, if the
333  * highest one we saw was completed. Otherwise start streaming from
334  * the beginning of the .partial segment.
335  */
336  if (!high_ispartial)
337  high_segno++;
338 
339  XLogSegNoOffsetToRecPtr(high_segno, 0, high_ptr);
340 
341  *tli = high_tli;
342  return high_ptr;
343  }
344  else
345  return InvalidXLogRecPtr;
346 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define IsPartialCompressXLogFileName(fname)
Definition: pg_receivewal.c:66
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
const char * progname
Definition: pg_standby.c:37
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:1038
#define XLogSegNoOffsetToRecPtr(segno, offset, dest)
Definition: xlog_internal.h:95
Definition: dirent.c:25
#define IsXLogFileName(fname)
#define MAXPGPATH
#define disconnect_and_exit(code)
Definition: pg_receivewal.c:55
static char * buf
Definition: pg_test_fsync.c:66
uint64 XLogSegNo
Definition: xlogdefs.h:34
static char * basedir
Definition: pg_receivewal.c:34
unsigned int uint32
Definition: c.h:268
static DIR * get_destination_dir(char *dest_folder)
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
struct dirent * readdir(DIR *)
Definition: dirent.c:78
#define IsPartialXLogFileName(fname)
const char * strerror(int errnum)
Definition: strerror.c:19
char d_name[MAX_PATH]
Definition: dirent.h:14
#define close(a)
Definition: win32.h:12
#define XLogFromFileName(fname, tli, logSegNo)
#define _(x)
Definition: elog.c:84
#define read(a, b, c)
Definition: win32.h:13
#define IsCompressXLogFileName(fname)
Definition: pg_receivewal.c:62
static DIR * get_destination_dir ( char *  dest_folder)
static

Definition at line 145 of file pg_receivewal.c.

References _, Assert, basedir, disconnect_and_exit, NULL, opendir(), progname, and strerror().

Referenced by FindStreamingStart(), and main().

146 {
147  DIR *dir;
148 
149  Assert(dest_folder != NULL);
150  dir = opendir(dest_folder);
151  if (dir == NULL)
152  {
153  fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
154  progname, basedir, strerror(errno));
156  }
157 
158  return dir;
159 }
const char * progname
Definition: pg_standby.c:37
Definition: dirent.c:25
#define disconnect_and_exit(code)
Definition: pg_receivewal.c:55
DIR * opendir(const char *)
Definition: dirent.c:33
static char * basedir
Definition: pg_receivewal.c:34
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char **  argv 
)

Definition at line 455 of file pg_receivewal.c.

References _, basedir, close_destination_dir(), compresslevel, conn, connection_string, CreateReplicationSlot(), dbgetpassword, dbhost, dbport, dbuser, disconnect_and_exit, do_create_slot, do_drop_slot, DropReplicationSlot(), get_destination_dir(), get_progname(), GetConnection(), getopt_long(), no_argument, noloop, NULL, optarg, optind, pg_strdup(), PG_TEXTDOMAIN, pg_usleep(), pqsignal(), progname, RECONNECT_SLEEP_TIME, replication_slot, required_argument, RunIdentifySystem(), set_pglocale_pgservice(), sigint_handler(), slot_exists_ok, standby_message_timeout, StreamLog(), synchronous, time_to_abort, usage(), and verbose.

456 {
457  static struct option long_options[] = {
458  {"help", no_argument, NULL, '?'},
459  {"version", no_argument, NULL, 'V'},
460  {"directory", required_argument, NULL, 'D'},
461  {"dbname", required_argument, NULL, 'd'},
462  {"host", required_argument, NULL, 'h'},
463  {"port", required_argument, NULL, 'p'},
464  {"username", required_argument, NULL, 'U'},
465  {"no-loop", no_argument, NULL, 'n'},
466  {"no-password", no_argument, NULL, 'w'},
467  {"password", no_argument, NULL, 'W'},
468  {"status-interval", required_argument, NULL, 's'},
469  {"slot", required_argument, NULL, 'S'},
470  {"verbose", no_argument, NULL, 'v'},
471  {"compress", required_argument, NULL, 'Z'},
472 /* action */
473  {"create-slot", no_argument, NULL, 1},
474  {"drop-slot", no_argument, NULL, 2},
475  {"if-not-exists", no_argument, NULL, 3},
476  {"synchronous", no_argument, NULL, 4},
477  {NULL, 0, NULL, 0}
478  };
479 
480  int c;
481  int option_index;
482  char *db_name;
483 
484  progname = get_progname(argv[0]);
485  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
486 
487  if (argc > 1)
488  {
489  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
490  {
491  usage();
492  exit(0);
493  }
494  else if (strcmp(argv[1], "-V") == 0 ||
495  strcmp(argv[1], "--version") == 0)
496  {
497  puts("pg_receivewal (PostgreSQL) " PG_VERSION);
498  exit(0);
499  }
500  }
501 
502  while ((c = getopt_long(argc, argv, "D:d:h:p:U:s:S:nwWvZ:",
503  long_options, &option_index)) != -1)
504  {
505  switch (c)
506  {
507  case 'D':
509  break;
510  case 'd':
512  break;
513  case 'h':
515  break;
516  case 'p':
517  if (atoi(optarg) <= 0)
518  {
519  fprintf(stderr, _("%s: invalid port number \"%s\"\n"),
520  progname, optarg);
521  exit(1);
522  }
524  break;
525  case 'U':
527  break;
528  case 'w':
529  dbgetpassword = -1;
530  break;
531  case 'W':
532  dbgetpassword = 1;
533  break;
534  case 's':
535  standby_message_timeout = atoi(optarg) * 1000;
536  if (standby_message_timeout < 0)
537  {
538  fprintf(stderr, _("%s: invalid status interval \"%s\"\n"),
539  progname, optarg);
540  exit(1);
541  }
542  break;
543  case 'S':
545  break;
546  case 'n':
547  noloop = 1;
548  break;
549  case 'v':
550  verbose++;
551  break;
552  case 'Z':
553  compresslevel = atoi(optarg);
554  if (compresslevel < 0 || compresslevel > 9)
555  {
556  fprintf(stderr, _("%s: invalid compression level \"%s\"\n"),
557  progname, optarg);
558  exit(1);
559  }
560  break;
561 /* action */
562  case 1:
563  do_create_slot = true;
564  break;
565  case 2:
566  do_drop_slot = true;
567  break;
568  case 3:
569  slot_exists_ok = true;
570  break;
571  case 4:
572  synchronous = true;
573  break;
574  default:
575 
576  /*
577  * getopt_long already emitted a complaint
578  */
579  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
580  progname);
581  exit(1);
582  }
583  }
584 
585  /*
586  * Any non-option arguments?
587  */
588  if (optind < argc)
589  {
590  fprintf(stderr,
591  _("%s: too many command-line arguments (first is \"%s\")\n"),
592  progname, argv[optind]);
593  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
594  progname);
595  exit(1);
596  }
597 
599  {
600  fprintf(stderr, _("%s: cannot use --create-slot together with --drop-slot\n"), progname);
601  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
602  progname);
603  exit(1);
604  }
605 
607  {
608  /* translator: second %s is an option name */
609  fprintf(stderr, _("%s: %s needs a slot to be specified using --slot\n"), progname,
610  do_drop_slot ? "--drop-slot" : "--create-slot");
611  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
612  progname);
613  exit(1);
614  }
615 
616  /*
617  * Required arguments
618  */
619  if (basedir == NULL && !do_drop_slot && !do_create_slot)
620  {
621  fprintf(stderr, _("%s: no target directory specified\n"), progname);
622  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
623  progname);
624  exit(1);
625  }
626 
627 #ifndef HAVE_LIBZ
628  if (compresslevel != 0)
629  {
630  fprintf(stderr,
631  _("%s: this build does not support compression\n"),
632  progname);
633  exit(1);
634  }
635 #endif
636 
637  /*
638  * Check existence of destination folder.
639  */
640  if (!do_drop_slot && !do_create_slot)
641  {
643 
645  }
646 
647 #ifndef WIN32
648  pqsignal(SIGINT, sigint_handler);
649 #endif
650 
651  /*
652  * Obtain a connection before doing anything.
653  */
654  conn = GetConnection();
655  if (!conn)
656  /* error message already written in GetConnection() */
657  exit(1);
658 
659  /*
660  * Run IDENTIFY_SYSTEM to make sure we've successfully have established a
661  * replication connection and haven't connected using a database specific
662  * connection.
663  */
664  if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
666 
667  /*
668  * Check that there is a database associated with connection, none should
669  * be defined in this context.
670  */
671  if (db_name)
672  {
673  fprintf(stderr,
674  _("%s: replication connection using slot \"%s\" is unexpectedly database specific\n"),
677  }
678 
679  /*
680  * Drop a replication slot.
681  */
682  if (do_drop_slot)
683  {
684  if (verbose)
685  fprintf(stderr,
686  _("%s: dropping replication slot \"%s\"\n"),
688 
692  }
693 
694  /* Create a replication slot */
695  if (do_create_slot)
696  {
697  if (verbose)
698  fprintf(stderr,
699  _("%s: creating replication slot \"%s\"\n"),
701 
706  }
707 
708  /*
709  * Don't close the connection here so that subsequent StreamLog() can
710  * reuse it.
711  */
712 
713  while (true)
714  {
715  StreamLog();
716  if (time_to_abort)
717  {
718  /*
719  * We've been Ctrl-C'ed. That's not an error, so exit without an
720  * errorcode.
721  */
722  exit(0);
723  }
724  else if (noloop)
725  {
726  fprintf(stderr, _("%s: disconnected\n"), progname);
727  exit(1);
728  }
729  else
730  {
731  fprintf(stderr,
732  /* translator: check source for value for %d */
733  _("%s: disconnected; waiting %d seconds to try again\n"),
735  pg_usleep(RECONNECT_SLEEP_TIME * 1000000);
736  }
737  }
738 }
static char * replication_slot
Definition: pg_receivewal.c:44
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:1036
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:72
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:242
static bool synchronous
Definition: pg_receivewal.c:43
const char * progname
Definition: pg_standby.c:37
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:51
Definition: dirent.c:25
char * connection_string
Definition: streamutil.c:34
PGconn * conn
Definition: streamutil.c:42
#define disconnect_and_exit(code)
Definition: pg_receivewal.c:55
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
static bool do_drop_slot
Definition: pg_receivewal.c:42
int dbgetpassword
Definition: streamutil.c:39
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1012
char * dbport
Definition: streamutil.c:37
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static DIR * get_destination_dir(char *dest_folder)
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:97
#define NULL
Definition: c.h:229
char * dbhost
Definition: streamutil.c:35
#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:842
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * dbuser
Definition: streamutil.c:36
char * optarg
Definition: getopt.c:53
static int verbose
Definition: pg_receivewal.c:35
#define _(x)
Definition: elog.c:84
static volatile bool time_to_abort
Definition: pg_receivewal.c:39
static void sigint_handler ( int  signum)
static

Definition at line 448 of file pg_receivewal.c.

References time_to_abort.

Referenced by main().

449 {
450  time_to_abort = true;
451 }
static volatile bool time_to_abort
Definition: pg_receivewal.c:39
static bool stop_streaming ( XLogRecPtr  segendpos,
uint32  timeline,
bool  segment_finished 
)
static

Definition at line 104 of file pg_receivewal.c.

References _, InvalidXLogRecPtr, progname, time_to_abort, and verbose.

Referenced by StreamLog().

105 {
106  static uint32 prevtimeline = 0;
107  static XLogRecPtr prevpos = InvalidXLogRecPtr;
108 
109  /* we assume that we get called once at the end of each segment */
110  if (verbose && segment_finished)
111  fprintf(stderr, _("%s: finished segment at %X/%X (timeline %u)\n"),
112  progname, (uint32) (xlogpos >> 32), (uint32) xlogpos,
113  timeline);
114 
115  /*
116  * Note that we report the previous, not current, position here. After a
117  * timeline switch, xlogpos points to the beginning of the segment because
118  * that's where we always begin streaming. Reporting the end of previous
119  * timeline isn't totally accurate, because the next timeline can begin
120  * slightly before the end of the WAL that we received on the previous
121  * timeline, but it's close enough for reporting purposes.
122  */
123  if (prevtimeline != 0 && prevtimeline != timeline)
124  fprintf(stderr, _("%s: switched to timeline %u at %X/%X\n"),
125  progname, timeline,
126  (uint32) (prevpos >> 32), (uint32) prevpos);
127 
128  prevtimeline = timeline;
129  prevpos = xlogpos;
130 
131  if (time_to_abort)
132  {
133  fprintf(stderr, _("%s: received interrupt signal, exiting\n"),
134  progname);
135  return true;
136  }
137  return false;
138 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
const char * progname
Definition: pg_standby.c:37
unsigned int uint32
Definition: c.h:268
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static int verbose
Definition: pg_receivewal.c:35
#define _(x)
Definition: elog.c:84
static volatile bool time_to_abort
Definition: pg_receivewal.c:39
static void StreamLog ( void  )
static

Definition at line 352 of file pg_receivewal.c.

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

Referenced by main().

353 {
354  XLogRecPtr serverpos;
355  TimeLineID servertli;
356  StreamCtl stream;
357 
358  MemSet(&stream, 0, sizeof(stream));
359 
360  /*
361  * Connect in replication mode to the server
362  */
363  if (conn == NULL)
364  conn = GetConnection();
365  if (!conn)
366  /* Error message already written in GetConnection() */
367  return;
368 
370  {
371  /*
372  * Error message already written in CheckServerVersionForStreaming().
373  * There's no hope of recovering from a version mismatch, so don't
374  * retry.
375  */
377  }
378 
379  /*
380  * Identify server, obtaining start LSN position and current timeline ID
381  * at the same time, necessary if not valid data can be found in the
382  * existing output directory.
383  */
384  if (!RunIdentifySystem(conn, NULL, &servertli, &serverpos, NULL))
386 
387  /*
388  * Figure out where to start streaming.
389  */
390  stream.startpos = FindStreamingStart(&stream.timeline);
391  if (stream.startpos == InvalidXLogRecPtr)
392  {
393  stream.startpos = serverpos;
394  stream.timeline = servertli;
395  }
396 
397  /*
398  * Always start streaming at the beginning of a segment
399  */
400  stream.startpos -= stream.startpos % XLOG_SEG_SIZE;
401 
402  /*
403  * Start the replication
404  */
405  if (verbose)
406  fprintf(stderr,
407  _("%s: starting log streaming at %X/%X (timeline %u)\n"),
408  progname, (uint32) (stream.startpos >> 32), (uint32) stream.startpos,
409  stream.timeline);
410 
411  stream.stream_stop = stop_streaming;
412  stream.stop_socket = PGINVALID_SOCKET;
414  stream.synchronous = synchronous;
415  stream.do_sync = true;
416  stream.mark_done = false;
418  stream.do_sync);
419  stream.partial_suffix = ".partial";
421  stream.temp_slot = false;
422 
423  ReceiveXlogStream(conn, &stream);
424 
425  if (!stream.walmethod->finish())
426  {
427  fprintf(stderr,
428  _("%s: could not finish writing WAL files: %s\n"),
429  progname, strerror(errno));
430  return;
431  }
432 
433  PQfinish(conn);
434 
436  pg_free(stream.walmethod);
437 
438  conn = NULL;
439 }
static char * replication_slot
Definition: pg_receivewal.c:44
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static int standby_message_timeout
Definition: pg_receivewal.c:38
uint32 TimeLineID
Definition: xlogdefs.h:45
bool do_sync
Definition: receivelog.h:40
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:242
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3521
#define MemSet(start, val, len)
Definition: c.h:857
static bool synchronous
Definition: pg_receivewal.c:43
XLogRecPtr startpos
Definition: receivelog.h:32
char * partial_suffix
Definition: receivelog.h:49
static bool stop_streaming(XLogRecPtr segendpos, uint32 timeline, bool segment_finished)
const char * progname
Definition: pg_standby.c:37
TimeLineID timeline
Definition: receivelog.h:33
WalWriteMethod * CreateWalDirectoryMethod(const char *basedir, int compression, bool sync)
Definition: walmethods.c:346
bool(* finish)(void)
Definition: walmethods.h:75
PGconn * conn
Definition: streamutil.c:42
#define disconnect_and_exit(code)
Definition: pg_receivewal.c:55
char * replication_slot
Definition: receivelog.h:50
bool mark_done
Definition: receivelog.h:39
static XLogRecPtr FindStreamingStart(uint32 *tli)
static char * basedir
Definition: pg_receivewal.c:34
unsigned int uint32
Definition: c.h:268
stream_stop_callback stream_stop
Definition: receivelog.h:43
WalWriteMethod * walmethod
Definition: receivelog.h:48
#define PGINVALID_SOCKET
Definition: port.h:24
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:97
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
Definition: receivelog.c:444
static int compresslevel
Definition: pg_receivewal.c:36
bool synchronous
Definition: receivelog.h:38
void pg_free(void *ptr)
Definition: fe_memutils.c:105
pgsocket stop_socket
Definition: receivelog.h:45
const char * strerror(int errnum)
Definition: strerror.c:19
int standby_message_timeout
Definition: receivelog.h:36
bool temp_slot
Definition: receivelog.h:51
static int verbose
Definition: pg_receivewal.c:35
#define _(x)
Definition: elog.c:84
void FreeWalDirectoryMethod(void)
Definition: walmethods.c:370
bool CheckServerVersionForStreaming(PGconn *conn)
Definition: receivelog.c:364
static void usage ( void  )
static

Definition at line 72 of file pg_receivewal.c.

References _, progname, and standby_message_timeout.

Referenced by main().

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

Variable Documentation

char* basedir = NULL
static

Definition at line 34 of file pg_receivewal.c.

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

int compresslevel = 0
static

Definition at line 36 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

bool do_create_slot = false
static

Definition at line 40 of file pg_receivewal.c.

Referenced by main().

bool do_drop_slot = false
static

Definition at line 42 of file pg_receivewal.c.

Referenced by main().

int noloop = 0
static

Definition at line 37 of file pg_receivewal.c.

Referenced by main().

char* replication_slot = NULL
static

Definition at line 44 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

bool slot_exists_ok = false
static

Definition at line 41 of file pg_receivewal.c.

Referenced by main().

int standby_message_timeout = 10 * 1000
static

Definition at line 38 of file pg_receivewal.c.

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

bool synchronous = false
static

Definition at line 43 of file pg_receivewal.c.

Referenced by main(), and StreamLog().

volatile bool time_to_abort = false
static

Definition at line 39 of file pg_receivewal.c.

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

int verbose = 0
static

Definition at line 35 of file pg_receivewal.c.

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