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:3516
PGconn * conn
Definition: streamutil.c:42
#define NULL
Definition: c.h:226

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:226
#define Assert(condition)
Definition: c.h:671
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.
238  * For compressed segments, look at the last 4 bytes of the compressed
239  * file, which is where the uncompressed size is located for gz files
240  * with a size lower than 4GB, and then compare it to the size of a
241  * completed segment. The 4 last bytes correspond to the ISIZE member
242  * according to http://www.zlib.org/rfc-gzip.html.
243  */
244  if (!ispartial && !iscompress)
245  {
246  struct stat statbuf;
247  char fullpath[MAXPGPATH];
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];
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:65
uint64 XLogSegNo
Definition: xlogdefs.h:34
static char * basedir
Definition: pg_receivewal.c:34
unsigned int uint32
Definition: c.h:265
static DIR * get_destination_dir(char *dest_folder)
#define NULL
Definition: c.h:226
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:17
#define XLogFromFileName(fname, tli, logSegNo)
#define _(x)
Definition: elog.c:84
#define read(a, b, c)
Definition: win32.h:18
#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:226
#define Assert(condition)
Definition: c.h:671
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char **  argv 
)

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

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

References time_to_abort.

Referenced by main().

448 {
449  time_to_abort = true;
450 }
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:265
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(), PQfinish(), progname, ReceiveXlogStream(), replication_slot, StreamCtl::replication_slot, RunIdentifySystem(), StreamCtl::standby_message_timeout, standby_message_timeout, StreamCtl::startpos, 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;
413  stream.synchronous = synchronous;
414  stream.do_sync = true;
415  stream.mark_done = false;
417  stream.do_sync);
418  stream.partial_suffix = ".partial";
420  stream.temp_slot = false;
421 
422  ReceiveXlogStream(conn, &stream);
423 
424  if (!stream.walmethod->finish())
425  {
426  fprintf(stderr,
427  _("%s: could not finish writing WAL files: %s\n"),
428  progname, strerror(errno));
429  return;
430  }
431 
432  PQfinish(conn);
433 
435  pg_free(stream.walmethod);
436 
437  conn = NULL;
438 }
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:3516
#define MemSet(start, val, len)
Definition: c.h:853
static bool synchronous
Definition: pg_receivewal.c:43
XLogRecPtr startpos
Definition: receivelog.h:32
char * partial_suffix
Definition: receivelog.h:46
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:33
PGconn * conn
Definition: streamutil.c:42
#define disconnect_and_exit(code)
Definition: pg_receivewal.c:55
char * replication_slot
Definition: receivelog.h:47
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:265
stream_stop_callback stream_stop
Definition: receivelog.h:43
WalWriteMethod * walmethod
Definition: receivelog.h:45
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:97
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
Definition: receivelog.c:433
static int compresslevel
Definition: pg_receivewal.c:36
bool synchronous
Definition: receivelog.h:38
void pg_free(void *ptr)
Definition: fe_memutils.c:105
const char * strerror(int errnum)
Definition: strerror.c:19
int standby_message_timeout
Definition: receivelog.h:36
bool temp_slot
Definition: receivelog.h:48
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:360
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 transaction 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 transaction 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 transaction 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().