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:3630
PGconn * conn
Definition: streamutil.c:43
#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 167 of file pg_receivewal.c.

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

Referenced by FindStreamingStart(), and main().

168 {
169  Assert(dest_dir != NULL && dest_folder != NULL);
170  if (closedir(dest_dir))
171  {
172  fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
173  progname, dest_folder, strerror(errno));
175  }
176 }
int closedir(DIR *)
Definition: dirent.c:111
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:676
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
static XLogRecPtr FindStreamingStart ( uint32 tli)
static

Definition at line 187 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().

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

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

Referenced by FindStreamingStart(), and main().

147 {
148  DIR *dir;
149 
150  Assert(dest_folder != NULL);
151  dir = opendir(dest_folder);
152  if (dir == NULL)
153  {
154  fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
155  progname, basedir, strerror(errno));
157  }
158 
159  return dir;
160 }
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:676
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char **  argv 
)

Definition at line 456 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.

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

References time_to_abort.

Referenced by main().

450 {
451  time_to_abort = true;
452 }
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 (verbose && 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  if (verbose)
134  fprintf(stderr, _("%s: received interrupt signal, exiting\n"),
135  progname);
136  return true;
137  }
138  return false;
139 }
#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 353 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().

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