PostgreSQL Source Code  git master
pg_recvlogical.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_recvlogical.c - receive data from a logical decoding slot in a streaming
4  * fashion and write it to a local file.
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/bin/pg_basebackup/pg_recvlogical.c
10  *-------------------------------------------------------------------------
11  */
12 
13 #include "postgres_fe.h"
14 
15 #include <dirent.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #ifdef HAVE_SYS_SELECT_H
19 #include <sys/select.h>
20 #endif
21 
22 #include "access/xlog_internal.h"
23 #include "common/fe_memutils.h"
24 #include "common/file_perm.h"
25 #include "common/logging.h"
26 #include "getopt_long.h"
27 #include "libpq-fe.h"
28 #include "libpq/pqsignal.h"
29 #include "pqexpbuffer.h"
30 #include "streamutil.h"
31 
32 /* Time to sleep between reconnection attempts */
33 #define RECONNECT_SLEEP_TIME 5
34 
35 /* Global Options */
36 static char *outfile = NULL;
37 static int verbose = 0;
38 static int noloop = 0;
39 static int standby_message_timeout = 10 * 1000; /* 10 sec = default */
40 static int fsync_interval = 10 * 1000; /* 10 sec = default */
43 static bool do_create_slot = false;
44 static bool slot_exists_ok = false;
45 static bool do_start_slot = false;
46 static bool do_drop_slot = false;
47 static char *replication_slot = NULL;
48 
49 /* filled pairwise with option, value. value may be NULL */
50 static char **options;
51 static size_t noptions = 0;
52 static const char *plugin = "test_decoding";
53 
54 /* Global State */
55 static int outfd = -1;
56 static volatile sig_atomic_t time_to_abort = false;
57 static volatile sig_atomic_t output_reopen = false;
58 static bool output_isfile;
60 static bool output_needs_fsync = false;
63 
64 static void usage(void);
65 static void StreamLogicalLog(void);
68  bool keepalive, XLogRecPtr lsn);
69 
70 static void
71 usage(void)
72 {
73  printf(_("%s controls PostgreSQL logical decoding streams.\n\n"),
74  progname);
75  printf(_("Usage:\n"));
76  printf(_(" %s [OPTION]...\n"), progname);
77  printf(_("\nAction to be performed:\n"));
78  printf(_(" --create-slot create a new replication slot (for the slot's name see --slot)\n"));
79  printf(_(" --drop-slot drop the replication slot (for the slot's name see --slot)\n"));
80  printf(_(" --start start streaming in a replication slot (for the slot's name see --slot)\n"));
81  printf(_("\nOptions:\n"));
82  printf(_(" -E, --endpos=LSN exit after receiving the specified LSN\n"));
83  printf(_(" -f, --file=FILE receive log into this file, - for stdout\n"));
84  printf(_(" -F --fsync-interval=SECS\n"
85  " time between fsyncs to the output file (default: %d)\n"), (fsync_interval / 1000));
86  printf(_(" --if-not-exists do not error if slot already exists when creating a slot\n"));
87  printf(_(" -I, --startpos=LSN where in an existing slot should the streaming start\n"));
88  printf(_(" -n, --no-loop do not loop on connection lost\n"));
89  printf(_(" -o, --option=NAME[=VALUE]\n"
90  " pass option NAME with optional value VALUE to the\n"
91  " output plugin\n"));
92  printf(_(" -P, --plugin=PLUGIN use output plugin PLUGIN (default: %s)\n"), plugin);
93  printf(_(" -s, --status-interval=SECS\n"
94  " time between status packets sent to server (default: %d)\n"), (standby_message_timeout / 1000));
95  printf(_(" -S, --slot=SLOTNAME name of the logical replication slot\n"));
96  printf(_(" -v, --verbose output verbose messages\n"));
97  printf(_(" -V, --version output version information, then exit\n"));
98  printf(_(" -?, --help show this help, then exit\n"));
99  printf(_("\nConnection options:\n"));
100  printf(_(" -d, --dbname=DBNAME database to connect to\n"));
101  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
102  printf(_(" -p, --port=PORT database server port number\n"));
103  printf(_(" -U, --username=NAME connect as specified database user\n"));
104  printf(_(" -w, --no-password never prompt for password\n"));
105  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
106  printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
107 }
108 
109 /*
110  * Send a Standby Status Update message to server.
111  */
112 static bool
113 sendFeedback(PGconn *conn, TimestampTz now, bool force, bool replyRequested)
114 {
115  static XLogRecPtr last_written_lsn = InvalidXLogRecPtr;
116  static XLogRecPtr last_fsync_lsn = InvalidXLogRecPtr;
117 
118  char replybuf[1 + 8 + 8 + 8 + 8 + 1];
119  int len = 0;
120 
121  /*
122  * we normally don't want to send superfluous feedback, but if it's
123  * because of a timeout we need to, otherwise wal_sender_timeout will kill
124  * us.
125  */
126  if (!force &&
127  last_written_lsn == output_written_lsn &&
128  last_fsync_lsn != output_fsync_lsn)
129  return true;
130 
131  if (verbose)
132  pg_log_info("confirming write up to %X/%X, flush to %X/%X (slot %s)",
136 
137  replybuf[len] = 'r';
138  len += 1;
139  fe_sendint64(output_written_lsn, &replybuf[len]); /* write */
140  len += 8;
141  fe_sendint64(output_fsync_lsn, &replybuf[len]); /* flush */
142  len += 8;
143  fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */
144  len += 8;
145  fe_sendint64(now, &replybuf[len]); /* sendTime */
146  len += 8;
147  replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */
148  len += 1;
149 
151  last_written_lsn = output_written_lsn;
152  last_fsync_lsn = output_fsync_lsn;
153 
154  if (PQputCopyData(conn, replybuf, len) <= 0 || PQflush(conn))
155  {
156  pg_log_error("could not send feedback packet: %s",
157  PQerrorMessage(conn));
158  return false;
159  }
160 
161  return true;
162 }
163 
164 static void
166 {
167  if (conn != NULL)
168  PQfinish(conn);
169 }
170 
171 static bool
173 {
175 
177 
178  if (fsync_interval <= 0)
179  return true;
180 
181  if (!output_needs_fsync)
182  return true;
183 
184  output_needs_fsync = false;
185 
186  /* can only fsync if it's a regular file */
187  if (!output_isfile)
188  return true;
189 
190  if (fsync(outfd) != 0)
191  {
192  pg_log_fatal("could not fsync file \"%s\": %m", outfile);
193  exit(1);
194  }
195 
196  return true;
197 }
198 
199 /*
200  * Start the log streaming
201  */
202 static void
204 {
205  PGresult *res;
206  char *copybuf = NULL;
207  TimestampTz last_status = -1;
208  int i;
209  PQExpBuffer query;
210 
213 
214  query = createPQExpBuffer();
215 
216  /*
217  * Connect in replication mode to the server
218  */
219  if (!conn)
220  conn = GetConnection();
221  if (!conn)
222  /* Error message already written in GetConnection() */
223  return;
224 
225  /*
226  * Start the replication
227  */
228  if (verbose)
229  pg_log_info("starting log streaming at %X/%X (slot %s)",
230  (uint32) (startpos >> 32), (uint32) startpos,
232 
233  /* Initiate the replication stream at specified location */
234  appendPQExpBuffer(query, "START_REPLICATION SLOT \"%s\" LOGICAL %X/%X",
235  replication_slot, (uint32) (startpos >> 32), (uint32) startpos);
236 
237  /* print options if there are any */
238  if (noptions)
239  appendPQExpBufferStr(query, " (");
240 
241  for (i = 0; i < noptions; i++)
242  {
243  /* separator */
244  if (i > 0)
245  appendPQExpBufferStr(query, ", ");
246 
247  /* write option name */
248  appendPQExpBuffer(query, "\"%s\"", options[(i * 2)]);
249 
250  /* write option value if specified */
251  if (options[(i * 2) + 1] != NULL)
252  appendPQExpBuffer(query, " '%s'", options[(i * 2) + 1]);
253  }
254 
255  if (noptions)
256  appendPQExpBufferChar(query, ')');
257 
258  res = PQexec(conn, query->data);
259  if (PQresultStatus(res) != PGRES_COPY_BOTH)
260  {
261  pg_log_error("could not send replication command \"%s\": %s",
262  query->data, PQresultErrorMessage(res));
263  PQclear(res);
264  goto error;
265  }
266  PQclear(res);
267  resetPQExpBuffer(query);
268 
269  if (verbose)
270  pg_log_info("streaming initiated");
271 
272  while (!time_to_abort)
273  {
274  int r;
275  int bytes_left;
276  int bytes_written;
278  int hdr_len;
279  XLogRecPtr cur_record_lsn = InvalidXLogRecPtr;
280 
281  if (copybuf != NULL)
282  {
283  PQfreemem(copybuf);
284  copybuf = NULL;
285  }
286 
287  /*
288  * Potentially send a status message to the master
289  */
290  now = feGetCurrentTimestamp();
291 
292  if (outfd != -1 &&
295  {
296  if (!OutputFsync(now))
297  goto error;
298  }
299 
300  if (standby_message_timeout > 0 &&
301  feTimestampDifferenceExceeds(last_status, now,
303  {
304  /* Time to send feedback! */
305  if (!sendFeedback(conn, now, true, false))
306  goto error;
307 
308  last_status = now;
309  }
310 
311  /* got SIGHUP, close output file */
312  if (outfd != -1 && output_reopen && strcmp(outfile, "-") != 0)
313  {
314  now = feGetCurrentTimestamp();
315  if (!OutputFsync(now))
316  goto error;
317  close(outfd);
318  outfd = -1;
319  }
320  output_reopen = false;
321 
322  /* open the output file, if not open yet */
323  if (outfd == -1)
324  {
325  struct stat statbuf;
326 
327  if (strcmp(outfile, "-") == 0)
328  outfd = fileno(stdout);
329  else
330  outfd = open(outfile, O_CREAT | O_APPEND | O_WRONLY | PG_BINARY,
331  S_IRUSR | S_IWUSR);
332  if (outfd == -1)
333  {
334  pg_log_error("could not open log file \"%s\": %m", outfile);
335  goto error;
336  }
337 
338  if (fstat(outfd, &statbuf) != 0)
339  pg_log_error("could not stat file \"%s\": %m", outfile);
340 
341  output_isfile = S_ISREG(statbuf.st_mode) && !isatty(outfd);
342  }
343 
344  r = PQgetCopyData(conn, &copybuf, 1);
345  if (r == 0)
346  {
347  /*
348  * In async mode, and no data available. We block on reading but
349  * not more than the specified timeout, so that we can send a
350  * response back to the client.
351  */
352  fd_set input_mask;
353  TimestampTz message_target = 0;
354  TimestampTz fsync_target = 0;
355  struct timeval timeout;
356  struct timeval *timeoutptr = NULL;
357 
358  if (PQsocket(conn) < 0)
359  {
360  pg_log_error("invalid socket: %s", PQerrorMessage(conn));
361  goto error;
362  }
363 
364  FD_ZERO(&input_mask);
365  FD_SET(PQsocket(conn), &input_mask);
366 
367  /* Compute when we need to wakeup to send a keepalive message. */
369  message_target = last_status + (standby_message_timeout - 1) *
370  ((int64) 1000);
371 
372  /* Compute when we need to wakeup to fsync the output file. */
374  fsync_target = output_last_fsync + (fsync_interval - 1) *
375  ((int64) 1000);
376 
377  /* Now compute when to wakeup. */
378  if (message_target > 0 || fsync_target > 0)
379  {
380  TimestampTz targettime;
381  long secs;
382  int usecs;
383 
384  targettime = message_target;
385 
386  if (fsync_target > 0 && fsync_target < targettime)
387  targettime = fsync_target;
388 
390  targettime,
391  &secs,
392  &usecs);
393  if (secs <= 0)
394  timeout.tv_sec = 1; /* Always sleep at least 1 sec */
395  else
396  timeout.tv_sec = secs;
397  timeout.tv_usec = usecs;
398  timeoutptr = &timeout;
399  }
400 
401  r = select(PQsocket(conn) + 1, &input_mask, NULL, NULL, timeoutptr);
402  if (r == 0 || (r < 0 && errno == EINTR))
403  {
404  /*
405  * Got a timeout or signal. Continue the loop and either
406  * deliver a status packet to the server or just go back into
407  * blocking.
408  */
409  continue;
410  }
411  else if (r < 0)
412  {
413  pg_log_error("select() failed: %m");
414  goto error;
415  }
416 
417  /* Else there is actually data on the socket */
418  if (PQconsumeInput(conn) == 0)
419  {
420  pg_log_error("could not receive data from WAL stream: %s",
422  goto error;
423  }
424  continue;
425  }
426 
427  /* End of copy stream */
428  if (r == -1)
429  break;
430 
431  /* Failure while reading the copy stream */
432  if (r == -2)
433  {
434  pg_log_error("could not read COPY data: %s",
436  goto error;
437  }
438 
439  /* Check the message type. */
440  if (copybuf[0] == 'k')
441  {
442  int pos;
443  bool replyRequested;
444  XLogRecPtr walEnd;
445  bool endposReached = false;
446 
447  /*
448  * Parse the keepalive message, enclosed in the CopyData message.
449  * We just check if the server requested a reply, and ignore the
450  * rest.
451  */
452  pos = 1; /* skip msgtype 'k' */
453  walEnd = fe_recvint64(&copybuf[pos]);
455 
456  pos += 8; /* read walEnd */
457 
458  pos += 8; /* skip sendTime */
459 
460  if (r < pos + 1)
461  {
462  pg_log_error("streaming header too small: %d", r);
463  goto error;
464  }
465  replyRequested = copybuf[pos];
466 
467  if (endpos != InvalidXLogRecPtr && walEnd >= endpos)
468  {
469  /*
470  * If there's nothing to read on the socket until a keepalive
471  * we know that the server has nothing to send us; and if
472  * walEnd has passed endpos, we know nothing else can have
473  * committed before endpos. So we can bail out now.
474  */
475  endposReached = true;
476  }
477 
478  /* Send a reply, if necessary */
479  if (replyRequested || endposReached)
480  {
481  if (!flushAndSendFeedback(conn, &now))
482  goto error;
483  last_status = now;
484  }
485 
486  if (endposReached)
487  {
489  time_to_abort = true;
490  break;
491  }
492 
493  continue;
494  }
495  else if (copybuf[0] != 'w')
496  {
497  pg_log_error("unrecognized streaming header: \"%c\"",
498  copybuf[0]);
499  goto error;
500  }
501 
502  /*
503  * Read the header of the XLogData message, enclosed in the CopyData
504  * message. We only need the WAL location field (dataStart), the rest
505  * of the header is ignored.
506  */
507  hdr_len = 1; /* msgtype 'w' */
508  hdr_len += 8; /* dataStart */
509  hdr_len += 8; /* walEnd */
510  hdr_len += 8; /* sendTime */
511  if (r < hdr_len + 1)
512  {
513  pg_log_error("streaming header too small: %d", r);
514  goto error;
515  }
516 
517  /* Extract WAL location for this block */
518  cur_record_lsn = fe_recvint64(&copybuf[1]);
519 
520  if (endpos != InvalidXLogRecPtr && cur_record_lsn > endpos)
521  {
522  /*
523  * We've read past our endpoint, so prepare to go away being
524  * cautious about what happens to our output data.
525  */
526  if (!flushAndSendFeedback(conn, &now))
527  goto error;
528  prepareToTerminate(conn, endpos, false, cur_record_lsn);
529  time_to_abort = true;
530  break;
531  }
532 
533  output_written_lsn = Max(cur_record_lsn, output_written_lsn);
534 
535  bytes_left = r - hdr_len;
536  bytes_written = 0;
537 
538  /* signal that a fsync is needed */
539  output_needs_fsync = true;
540 
541  while (bytes_left)
542  {
543  int ret;
544 
545  ret = write(outfd,
546  copybuf + hdr_len + bytes_written,
547  bytes_left);
548 
549  if (ret < 0)
550  {
551  pg_log_error("could not write %u bytes to log file \"%s\": %m",
552  bytes_left, outfile);
553  goto error;
554  }
555 
556  /* Write was successful, advance our position */
557  bytes_written += ret;
558  bytes_left -= ret;
559  }
560 
561  if (write(outfd, "\n", 1) != 1)
562  {
563  pg_log_error("could not write %u bytes to log file \"%s\": %m",
564  1, outfile);
565  goto error;
566  }
567 
568  if (endpos != InvalidXLogRecPtr && cur_record_lsn == endpos)
569  {
570  /* endpos was exactly the record we just processed, we're done */
571  if (!flushAndSendFeedback(conn, &now))
572  goto error;
573  prepareToTerminate(conn, endpos, false, cur_record_lsn);
574  time_to_abort = true;
575  break;
576  }
577  }
578 
579  res = PQgetResult(conn);
580  if (PQresultStatus(res) == PGRES_COPY_OUT)
581  {
582  /*
583  * We're doing a client-initiated clean exit and have sent CopyDone to
584  * the server. We've already sent replay confirmation and fsync'd so
585  * we can just clean up the connection now.
586  */
587  goto error;
588  }
589  else if (PQresultStatus(res) != PGRES_COMMAND_OK)
590  {
591  pg_log_error("unexpected termination of replication stream: %s",
592  PQresultErrorMessage(res));
593  goto error;
594  }
595  PQclear(res);
596 
597  if (outfd != -1 && strcmp(outfile, "-") != 0)
598  {
600 
601  /* no need to jump to error on failure here, we're finishing anyway */
602  OutputFsync(t);
603 
604  if (close(outfd) != 0)
605  pg_log_error("could not close file \"%s\": %m", outfile);
606  }
607  outfd = -1;
608 error:
609  if (copybuf != NULL)
610  {
611  PQfreemem(copybuf);
612  copybuf = NULL;
613  }
614  destroyPQExpBuffer(query);
615  PQfinish(conn);
616  conn = NULL;
617 }
618 
619 /*
620  * Unfortunately we can't do sensible signal handling on windows...
621  */
622 #ifndef WIN32
623 
624 /*
625  * When sigint is called, just tell the system to exit at the next possible
626  * moment.
627  */
628 static void
629 sigint_handler(int signum)
630 {
631  time_to_abort = true;
632 }
633 
634 /*
635  * Trigger the output file to be reopened.
636  */
637 static void
638 sighup_handler(int signum)
639 {
640  output_reopen = true;
641 }
642 #endif
643 
644 
645 int
646 main(int argc, char **argv)
647 {
648  static struct option long_options[] = {
649 /* general options */
650  {"file", required_argument, NULL, 'f'},
651  {"fsync-interval", required_argument, NULL, 'F'},
652  {"no-loop", no_argument, NULL, 'n'},
653  {"verbose", no_argument, NULL, 'v'},
654  {"version", no_argument, NULL, 'V'},
655  {"help", no_argument, NULL, '?'},
656 /* connection options */
657  {"dbname", required_argument, NULL, 'd'},
658  {"host", required_argument, NULL, 'h'},
659  {"port", required_argument, NULL, 'p'},
660  {"username", required_argument, NULL, 'U'},
661  {"no-password", no_argument, NULL, 'w'},
662  {"password", no_argument, NULL, 'W'},
663 /* replication options */
664  {"startpos", required_argument, NULL, 'I'},
665  {"endpos", required_argument, NULL, 'E'},
666  {"option", required_argument, NULL, 'o'},
667  {"plugin", required_argument, NULL, 'P'},
668  {"status-interval", required_argument, NULL, 's'},
669  {"slot", required_argument, NULL, 'S'},
670 /* action */
671  {"create-slot", no_argument, NULL, 1},
672  {"start", no_argument, NULL, 2},
673  {"drop-slot", no_argument, NULL, 3},
674  {"if-not-exists", no_argument, NULL, 4},
675  {NULL, 0, NULL, 0}
676  };
677  int c;
678  int option_index;
679  uint32 hi,
680  lo;
681  char *db_name;
682 
683  pg_logging_init(argv[0]);
684  progname = get_progname(argv[0]);
685  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
686 
687  if (argc > 1)
688  {
689  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
690  {
691  usage();
692  exit(0);
693  }
694  else if (strcmp(argv[1], "-V") == 0 ||
695  strcmp(argv[1], "--version") == 0)
696  {
697  puts("pg_recvlogical (PostgreSQL) " PG_VERSION);
698  exit(0);
699  }
700  }
701 
702  while ((c = getopt_long(argc, argv, "E:f:F:nvd:h:p:U:wWI:o:P:s:S:",
703  long_options, &option_index)) != -1)
704  {
705  switch (c)
706  {
707 /* general options */
708  case 'f':
710  break;
711  case 'F':
712  fsync_interval = atoi(optarg) * 1000;
713  if (fsync_interval < 0)
714  {
715  pg_log_error("invalid fsync interval \"%s\"", optarg);
716  exit(1);
717  }
718  break;
719  case 'n':
720  noloop = 1;
721  break;
722  case 'v':
723  verbose++;
724  break;
725 /* connection options */
726  case 'd':
728  break;
729  case 'h':
731  break;
732  case 'p':
733  if (atoi(optarg) <= 0)
734  {
735  pg_log_error("invalid port number \"%s\"", optarg);
736  exit(1);
737  }
739  break;
740  case 'U':
742  break;
743  case 'w':
744  dbgetpassword = -1;
745  break;
746  case 'W':
747  dbgetpassword = 1;
748  break;
749 /* replication options */
750  case 'I':
751  if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
752  {
753  pg_log_error("could not parse start position \"%s\"", optarg);
754  exit(1);
755  }
756  startpos = ((uint64) hi) << 32 | lo;
757  break;
758  case 'E':
759  if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
760  {
761  pg_log_error("could not parse end position \"%s\"", optarg);
762  exit(1);
763  }
764  endpos = ((uint64) hi) << 32 | lo;
765  break;
766  case 'o':
767  {
768  char *data = pg_strdup(optarg);
769  char *val = strchr(data, '=');
770 
771  if (val != NULL)
772  {
773  /* remove =; separate data from val */
774  *val = '\0';
775  val++;
776  }
777 
778  noptions += 1;
779  options = pg_realloc(options, sizeof(char *) * noptions * 2);
780 
781  options[(noptions - 1) * 2] = data;
782  options[(noptions - 1) * 2 + 1] = val;
783  }
784 
785  break;
786  case 'P':
788  break;
789  case 's':
790  standby_message_timeout = atoi(optarg) * 1000;
791  if (standby_message_timeout < 0)
792  {
793  pg_log_error("invalid status interval \"%s\"", optarg);
794  exit(1);
795  }
796  break;
797  case 'S':
799  break;
800 /* action */
801  case 1:
802  do_create_slot = true;
803  break;
804  case 2:
805  do_start_slot = true;
806  break;
807  case 3:
808  do_drop_slot = true;
809  break;
810  case 4:
811  slot_exists_ok = true;
812  break;
813 
814  default:
815 
816  /*
817  * getopt_long already emitted a complaint
818  */
819  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
820  progname);
821  exit(1);
822  }
823  }
824 
825  /*
826  * Any non-option arguments?
827  */
828  if (optind < argc)
829  {
830  pg_log_error("too many command-line arguments (first is \"%s\")",
831  argv[optind]);
832  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
833  progname);
834  exit(1);
835  }
836 
837  /*
838  * Required arguments
839  */
840  if (replication_slot == NULL)
841  {
842  pg_log_error("no slot specified");
843  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
844  progname);
845  exit(1);
846  }
847 
848  if (do_start_slot && outfile == NULL)
849  {
850  pg_log_error("no target file specified");
851  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
852  progname);
853  exit(1);
854  }
855 
856  if (!do_drop_slot && dbname == NULL)
857  {
858  pg_log_error("no database specified");
859  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
860  progname);
861  exit(1);
862  }
863 
865  {
866  pg_log_error("at least one action needs to be specified");
867  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
868  progname);
869  exit(1);
870  }
871 
873  {
874  pg_log_error("cannot use --create-slot or --start together with --drop-slot");
875  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
876  progname);
877  exit(1);
878  }
879 
881  {
882  pg_log_error("cannot use --create-slot or --drop-slot together with --startpos");
883  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
884  progname);
885  exit(1);
886  }
887 
889  {
890  pg_log_error("--endpos may only be specified with --start");
891  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
892  progname);
893  exit(1);
894  }
895 
896 #ifndef WIN32
897  pqsignal(SIGINT, sigint_handler);
899 #endif
900 
901  /*
902  * Obtain a connection to server. This is not really necessary but it
903  * helps to get more precise error messages about authentication, required
904  * GUC parameters and such.
905  */
906  conn = GetConnection();
907  if (!conn)
908  /* Error message already written in GetConnection() */
909  exit(1);
910  atexit(disconnect_atexit);
911 
912  /*
913  * Run IDENTIFY_SYSTEM to make sure we connected using a database specific
914  * replication connection.
915  */
916  if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
917  exit(1);
918 
919  if (db_name == NULL)
920  {
921  pg_log_error("could not establish database-specific replication connection");
922  exit(1);
923  }
924 
925  /*
926  * Set umask so that directories/files are created with the same
927  * permissions as directories/files in the source data directory.
928  *
929  * pg_mode_mask is set to owner-only by default and then updated in
930  * GetConnection() where we get the mode from the server-side with
931  * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
932  */
933  umask(pg_mode_mask);
934 
935  /* Drop a replication slot. */
936  if (do_drop_slot)
937  {
938  if (verbose)
939  pg_log_info("dropping replication slot \"%s\"", replication_slot);
940 
942  exit(1);
943  }
944 
945  /* Create a replication slot. */
946  if (do_create_slot)
947  {
948  if (verbose)
949  pg_log_info("creating replication slot \"%s\"", replication_slot);
950 
952  false, false, slot_exists_ok))
953  exit(1);
955  }
956 
957  if (!do_start_slot)
958  exit(0);
959 
960  /* Stream loop */
961  while (true)
962  {
964  if (time_to_abort)
965  {
966  /*
967  * We've been Ctrl-C'ed or reached an exit limit condition. That's
968  * not an error, so exit without an errorcode.
969  */
970  exit(0);
971  }
972  else if (noloop)
973  {
974  pg_log_error("disconnected");
975  exit(1);
976  }
977  else
978  {
979  /* translator: check source for value for %d */
980  pg_log_info("disconnected; waiting %d seconds to try again",
982  pg_usleep(RECONNECT_SLEEP_TIME * 1000000);
983  }
984  }
985 }
986 
987 /*
988  * Fsync our output data, and send a feedback message to the server. Returns
989  * true if successful, false otherwise.
990  *
991  * If successful, *now is updated to the current timestamp just before sending
992  * feedback.
993  */
994 static bool
996 {
997  /* flush data to disk, so that we send a recent flush pointer */
998  if (!OutputFsync(*now))
999  return false;
1000  *now = feGetCurrentTimestamp();
1001  if (!sendFeedback(conn, *now, true, false))
1002  return false;
1003 
1004  return true;
1005 }
1006 
1007 /*
1008  * Try to inform the server about our upcoming demise, but don't wait around or
1009  * retry on failure.
1010  */
1011 static void
1013 {
1014  (void) PQputCopyEnd(conn, NULL);
1015  (void) PQflush(conn);
1016 
1017  if (verbose)
1018  {
1019  if (keepalive)
1020  pg_log_info("end position %X/%X reached by keepalive",
1021  (uint32) (endpos >> 32), (uint32) endpos);
1022  else
1023  pg_log_info("end position %X/%X reached by WAL record at %X/%X",
1024  (uint32) (endpos >> 32), (uint32) (endpos),
1025  (uint32) (lsn >> 32), (uint32) lsn);
1026  }
1027 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2317
static const char * plugin
static XLogRecPtr output_fsync_lsn
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6617
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int main(int argc, char **argv)
TimestampTz feGetCurrentTimestamp(void)
Definition: streamutil.c:606
static volatile sig_atomic_t time_to_abort
static void error(void)
Definition: sql-dyntest.c:147
static void sigint_handler(int signum)
static int fsync_interval
#define RECONNECT_SLEEP_TIME
#define write(a, b, c)
Definition: win32.h:14
const char * get_progname(const char *argv0)
Definition: path.c:453
static void DropReplicationSlot(DropReplicationSlotCmd *cmd)
Definition: walsender.c:1052
static bool OutputFsync(TimestampTz now)
int64 TimestampTz
Definition: timestamp.h:39
#define pg_log_error(...)
Definition: logging.h:79
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
static void sighup_handler(int signum)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void pg_logging_init(const char *argv0)
Definition: logging.c:39
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:407
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4119
static char * replication_slot
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2384
#define printf(...)
Definition: port.h:198
static bool flushAndSendFeedback(PGconn *conn, TimestampTz *now)
const char * progname
Definition: pg_standby.c:36
#define fprintf
Definition: port.h:196
bool feTimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: streamutil.c:647
#define PG_BINARY
Definition: c.h:1216
static int noloop
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2473
static int verbose
static XLogRecPtr endpos
void pg_usleep(long microsec)
Definition: signal.c:53
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
#define required_argument
Definition: getopt_long.h:25
#define fsync(fd)
Definition: win32_port.h:63
int optind
Definition: getopt.c:50
static bool output_isfile
#define S_IWUSR
Definition: win32_port.h:263
PGconn * conn
Definition: streamutil.c:54
static bool slot_exists_ok
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
int PQflush(PGconn *conn)
Definition: fe-exec.c:3283
void feTimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: streamutil.c:625
static int outfd
char * c
static void disconnect_atexit(void)
#define SIGHUP
Definition: win32_port.h:154
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define select(n, r, w, e, timeout)
Definition: win32_port.h:436
unsigned int uint32
Definition: c.h:359
static XLogRecPtr output_written_lsn
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
static bool output_needs_fsync
#define S_ISREG(m)
Definition: win32_port.h:299
static char ** options
static void prepareToTerminate(PGconn *conn, XLogRecPtr endpos, bool keepalive, XLogRecPtr lsn)
#define stat(a, b)
Definition: win32_port.h:255
int dbgetpassword
Definition: streamutil.c:51
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1160
static void usage(void)
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
static bool sendFeedback(PGconn *conn, TimestampTz now, bool force, bool replyRequested)
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1704
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
StringInfo copybuf
Definition: tablesync.c:108
char * dbport
Definition: streamutil.c:49
static bool do_drop_slot
void PQclear(PGresult *res)
Definition: fe-exec.c:694
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:105
#define Max(x, y)
Definition: c.h:899
uint64 XLogRecPtr
Definition: xlogdefs.h:21
char * dbhost
Definition: streamutil.c:47
char * dbname
Definition: streamutil.c:50
static XLogRecPtr startpos
static void StreamLogicalLog(void)
#define S_IRUSR
Definition: win32_port.h:260
static void CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
Definition: walsender.c:852
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2708
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
int64 fe_recvint64(char *buf)
Definition: streamutil.c:671
char * dbuser
Definition: streamutil.c:48
char * optarg
Definition: getopt.c:52
static bool do_start_slot
int i
static TimestampTz output_last_fsync
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
static char * outfile
#define close(a)
Definition: win32.h:12
void fe_sendint64(int64 i, char *buf)
Definition: streamutil.c:660
#define EINTR
Definition: win32_port.h:323
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
static size_t noptions
#define _(x)
Definition: elog.c:87
void PQfreemem(void *ptr)
Definition: fe-exec.c:3296
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6635
int pg_mode_mask
Definition: file_perm.c:25
long val
Definition: informix.c:684
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1778
#define pg_log_info(...)
Definition: logging.h:87
static bool do_create_slot
static volatile sig_atomic_t output_reopen
#define pg_log_fatal(...)
Definition: logging.h:75
static int standby_message_timeout