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