1 /*-------------------------------------------------------------------------
3 * pg_recvlogical.c - receive data from a logical decoding slot in a streaming
4 * fashion and write it to a local file.
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
9 * src/bin/pg_basebackup/pg_recvlogical.c
10 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
18 #ifdef HAVE_SYS_SELECT_H
19 #include <sys/select.h>
23 #include "streamutil.h"
25 #include "access/xlog_internal.h"
26 #include "common/fe_memutils.h"
27 #include "getopt_long.h"
29 #include "libpq/pqsignal.h"
30 #include "pqexpbuffer.h"
33 /* Time to sleep between reconnection attempts */
34 #define RECONNECT_SLEEP_TIME 5
37 static char *outfile = NULL;
38 static int verbose = 0;
39 static int noloop = 0;
40 static int standby_message_timeout = 10 * 1000; /* 10 sec = default */
41 static int fsync_interval = 10 * 1000; /* 10 sec = default */
42 static XLogRecPtr startpos = InvalidXLogRecPtr;
43 static XLogRecPtr endpos = InvalidXLogRecPtr;
44 static bool do_create_slot = false;
45 static bool slot_exists_ok = false;
46 static bool do_start_slot = false;
47 static bool do_drop_slot = false;
48 static char *replication_slot = NULL;
50 /* filled pairwise with option, value. value may be NULL */
51 static char **options;
52 static size_t noptions = 0;
53 static const char *plugin = "test_decoding";
56 static int outfd = -1;
57 static volatile sig_atomic_t time_to_abort = false;
58 static volatile sig_atomic_t output_reopen = false;
59 static bool output_isfile;
60 static TimestampTz output_last_fsync = -1;
61 static bool output_needs_fsync = false;
62 static XLogRecPtr output_written_lsn = InvalidXLogRecPtr;
63 static XLogRecPtr output_fsync_lsn = InvalidXLogRecPtr;
65 static void usage(void);
66 static void StreamLogicalLog(void);
67 static void disconnect_and_exit(int code);
68 static bool flushAndSendFeedback(PGconn *conn, TimestampTz *now);
69 static void prepareToTerminate(PGconn *conn, XLogRecPtr endpos,
70 bool keepalive, XLogRecPtr lsn);
75 printf(_("%s controls PostgreSQL logical decoding streams.\n\n"),
77 printf(_("Usage:\n"));
78 printf(_(" %s [OPTION]...\n"), progname);
79 printf(_("\nAction to be performed:\n"));
80 printf(_(" --create-slot create a new replication slot (for the slot's name see --slot)\n"));
81 printf(_(" --drop-slot drop the replication slot (for the slot's name see --slot)\n"));
82 printf(_(" --start start streaming in a replication slot (for the slot's name see --slot)\n"));
83 printf(_("\nOptions:\n"));
84 printf(_(" -E, --endpos=LSN exit after receiving the specified LSN\n"));
85 printf(_(" -f, --file=FILE receive log into this file, - for stdout\n"));
86 printf(_(" -F --fsync-interval=SECS\n"
87 " time between fsyncs to the output file (default: %d)\n"), (fsync_interval / 1000));
88 printf(_(" --if-not-exists do not error if slot already exists when creating a slot\n"));
89 printf(_(" -I, --startpos=LSN where in an existing slot should the streaming start\n"));
90 printf(_(" -n, --no-loop do not loop on connection lost\n"));
91 printf(_(" -o, --option=NAME[=VALUE]\n"
92 " pass option NAME with optional value VALUE to the\n"
94 printf(_(" -P, --plugin=PLUGIN use output plugin PLUGIN (default: %s)\n"), plugin);
95 printf(_(" -s, --status-interval=SECS\n"
96 " time between status packets sent to server (default: %d)\n"), (standby_message_timeout / 1000));
97 printf(_(" -S, --slot=SLOTNAME name of the logical replication slot\n"));
98 printf(_(" -v, --verbose output verbose messages\n"));
99 printf(_(" -V, --version output version information, then exit\n"));
100 printf(_(" -?, --help show this help, then exit\n"));
101 printf(_("\nConnection options:\n"));
102 printf(_(" -d, --dbname=DBNAME database to connect to\n"));
103 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
104 printf(_(" -p, --port=PORT database server port number\n"));
105 printf(_(" -U, --username=NAME connect as specified database user\n"));
106 printf(_(" -w, --no-password never prompt for password\n"));
107 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
108 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
112 * Send a Standby Status Update message to server.
115 sendFeedback(PGconn *conn, TimestampTz now, bool force, bool replyRequested)
117 static XLogRecPtr last_written_lsn = InvalidXLogRecPtr;
118 static XLogRecPtr last_fsync_lsn = InvalidXLogRecPtr;
120 char replybuf[1 + 8 + 8 + 8 + 8 + 1];
124 * we normally don't want to send superfluous feedbacks, but if it's
125 * because of a timeout we need to, otherwise wal_sender_timeout will kill
129 last_written_lsn == output_written_lsn &&
130 last_fsync_lsn != output_fsync_lsn)
135 _("%s: confirming write up to %X/%X, flush to %X/%X (slot %s)\n"),
137 (uint32) (output_written_lsn >> 32), (uint32) output_written_lsn,
138 (uint32) (output_fsync_lsn >> 32), (uint32) output_fsync_lsn,
143 fe_sendint64(output_written_lsn, &replybuf[len]); /* write */
145 fe_sendint64(output_fsync_lsn, &replybuf[len]); /* flush */
147 fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */
149 fe_sendint64(now, &replybuf[len]); /* sendTime */
151 replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */
154 startpos = output_written_lsn;
155 last_written_lsn = output_written_lsn;
156 last_fsync_lsn = output_fsync_lsn;
158 if (PQputCopyData(conn, replybuf, len) <= 0 || PQflush(conn))
160 fprintf(stderr, _("%s: could not send feedback packet: %s"),
161 progname, PQerrorMessage(conn));
169 disconnect_and_exit(int code)
178 OutputFsync(TimestampTz now)
180 output_last_fsync = now;
182 output_fsync_lsn = output_written_lsn;
184 if (fsync_interval <= 0)
187 if (!output_needs_fsync)
190 output_needs_fsync = false;
192 /* can only fsync if it's a regular file */
196 if (fsync(outfd) != 0)
199 _("%s: could not fsync log file \"%s\": %s\n"),
200 progname, outfile, strerror(errno));
208 * Start the log streaming
211 StreamLogicalLog(void)
214 char *copybuf = NULL;
215 TimestampTz last_status = -1;
219 output_written_lsn = InvalidXLogRecPtr;
220 output_fsync_lsn = InvalidXLogRecPtr;
222 query = createPQExpBuffer();
225 * Connect in replication mode to the server
228 conn = GetConnection();
230 /* Error message already written in GetConnection() */
234 * Start the replication
238 _("%s: starting log streaming at %X/%X (slot %s)\n"),
239 progname, (uint32) (startpos >> 32), (uint32) startpos,
242 /* Initiate the replication stream at specified location */
243 appendPQExpBuffer(query, "START_REPLICATION SLOT \"%s\" LOGICAL %X/%X",
244 replication_slot, (uint32) (startpos >> 32), (uint32) startpos);
246 /* print options if there are any */
248 appendPQExpBufferStr(query, " (");
250 for (i = 0; i < noptions; i++)
254 appendPQExpBufferStr(query, ", ");
256 /* write option name */
257 appendPQExpBuffer(query, "\"%s\"", options[(i * 2)]);
259 /* write option value if specified */
260 if (options[(i * 2) + 1] != NULL)
261 appendPQExpBuffer(query, " '%s'", options[(i * 2) + 1]);
265 appendPQExpBufferChar(query, ')');
267 res = PQexec(conn, query->data);
268 if (PQresultStatus(res) != PGRES_COPY_BOTH)
270 fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
271 progname, query->data, PQresultErrorMessage(res));
276 resetPQExpBuffer(query);
280 _("%s: streaming initiated\n"),
283 while (!time_to_abort)
290 XLogRecPtr cur_record_lsn = InvalidXLogRecPtr;
299 * Potentially send a status message to the master
301 now = feGetCurrentTimestamp();
304 feTimestampDifferenceExceeds(output_last_fsync, now,
307 if (!OutputFsync(now))
311 if (standby_message_timeout > 0 &&
312 feTimestampDifferenceExceeds(last_status, now,
313 standby_message_timeout))
315 /* Time to send feedback! */
316 if (!sendFeedback(conn, now, true, false))
322 /* got SIGHUP, close output file */
323 if (outfd != -1 && output_reopen && strcmp(outfile, "-") != 0)
325 now = feGetCurrentTimestamp();
326 if (!OutputFsync(now))
331 output_reopen = false;
333 /* open the output file, if not open yet */
338 if (strcmp(outfile, "-") == 0)
339 outfd = fileno(stdout);
341 outfd = open(outfile, O_CREAT | O_APPEND | O_WRONLY | PG_BINARY,
346 _("%s: could not open log file \"%s\": %s\n"),
347 progname, outfile, strerror(errno));
351 if (fstat(outfd, &statbuf) != 0)
353 _("%s: could not stat file \"%s\": %s\n"),
354 progname, outfile, strerror(errno));
356 output_isfile = S_ISREG(statbuf.st_mode) && !isatty(outfd);
359 r = PQgetCopyData(conn, ©buf, 1);
363 * In async mode, and no data available. We block on reading but
364 * not more than the specified timeout, so that we can send a
365 * response back to the client.
368 TimestampTz message_target = 0;
369 TimestampTz fsync_target = 0;
370 struct timeval timeout;
371 struct timeval *timeoutptr = NULL;
373 if (PQsocket(conn) < 0)
376 _("%s: invalid socket: %s"),
377 progname, PQerrorMessage(conn));
381 FD_ZERO(&input_mask);
382 FD_SET(PQsocket(conn), &input_mask);
384 /* Compute when we need to wakeup to send a keepalive message. */
385 if (standby_message_timeout)
386 message_target = last_status + (standby_message_timeout - 1) *
389 /* Compute when we need to wakeup to fsync the output file. */
390 if (fsync_interval > 0 && output_needs_fsync)
391 fsync_target = output_last_fsync + (fsync_interval - 1) *
394 /* Now compute when to wakeup. */
395 if (message_target > 0 || fsync_target > 0)
397 TimestampTz targettime;
401 targettime = message_target;
403 if (fsync_target > 0 && fsync_target < targettime)
404 targettime = fsync_target;
406 feTimestampDifference(now,
411 timeout.tv_sec = 1; /* Always sleep at least 1 sec */
413 timeout.tv_sec = secs;
414 timeout.tv_usec = usecs;
415 timeoutptr = &timeout;
418 r = select(PQsocket(conn) + 1, &input_mask, NULL, NULL, timeoutptr);
419 if (r == 0 || (r < 0 && errno == EINTR))
422 * Got a timeout or signal. Continue the loop and either
423 * deliver a status packet to the server or just go back into
430 fprintf(stderr, _("%s: select() failed: %s\n"),
431 progname, strerror(errno));
435 /* Else there is actually data on the socket */
436 if (PQconsumeInput(conn) == 0)
439 _("%s: could not receive data from WAL stream: %s"),
440 progname, PQerrorMessage(conn));
446 /* End of copy stream */
450 /* Failure while reading the copy stream */
453 fprintf(stderr, _("%s: could not read COPY data: %s"),
454 progname, PQerrorMessage(conn));
458 /* Check the message type. */
459 if (copybuf[0] == 'k')
464 bool endposReached = false;
467 * Parse the keepalive message, enclosed in the CopyData message.
468 * We just check if the server requested a reply, and ignore the
471 pos = 1; /* skip msgtype 'k' */
472 walEnd = fe_recvint64(©buf[pos]);
473 output_written_lsn = Max(walEnd, output_written_lsn);
475 pos += 8; /* read walEnd */
477 pos += 8; /* skip sendTime */
481 fprintf(stderr, _("%s: streaming header too small: %d\n"),
485 replyRequested = copybuf[pos];
487 if (endpos != InvalidXLogRecPtr && walEnd >= endpos)
490 * If there's nothing to read on the socket until a keepalive
491 * we know that the server has nothing to send us; and if
492 * walEnd has passed endpos, we know nothing else can have
493 * committed before endpos. So we can bail out now.
495 endposReached = true;
498 /* Send a reply, if necessary */
499 if (replyRequested || endposReached)
501 if (!flushAndSendFeedback(conn, &now))
508 prepareToTerminate(conn, endpos, true, InvalidXLogRecPtr);
509 time_to_abort = true;
515 else if (copybuf[0] != 'w')
517 fprintf(stderr, _("%s: unrecognized streaming header: \"%c\"\n"),
518 progname, copybuf[0]);
523 * Read the header of the XLogData message, enclosed in the CopyData
524 * message. We only need the WAL location field (dataStart), the rest
525 * of the header is ignored.
527 hdr_len = 1; /* msgtype 'w' */
528 hdr_len += 8; /* dataStart */
529 hdr_len += 8; /* walEnd */
530 hdr_len += 8; /* sendTime */
533 fprintf(stderr, _("%s: streaming header too small: %d\n"),
538 /* Extract WAL location for this block */
539 cur_record_lsn = fe_recvint64(©buf[1]);
541 if (endpos != InvalidXLogRecPtr && cur_record_lsn > endpos)
544 * We've read past our endpoint, so prepare to go away being
545 * cautious about what happens to our output data.
547 if (!flushAndSendFeedback(conn, &now))
549 prepareToTerminate(conn, endpos, false, cur_record_lsn);
550 time_to_abort = true;
554 output_written_lsn = Max(cur_record_lsn, output_written_lsn);
556 bytes_left = r - hdr_len;
559 /* signal that a fsync is needed */
560 output_needs_fsync = true;
567 copybuf + hdr_len + bytes_written,
573 _("%s: could not write %u bytes to log file \"%s\": %s\n"),
574 progname, bytes_left, outfile,
579 /* Write was successful, advance our position */
580 bytes_written += ret;
584 if (write(outfd, "\n", 1) != 1)
587 _("%s: could not write %u bytes to log file \"%s\": %s\n"),
588 progname, 1, outfile,
593 if (endpos != InvalidXLogRecPtr && cur_record_lsn == endpos)
595 /* endpos was exactly the record we just processed, we're done */
596 if (!flushAndSendFeedback(conn, &now))
598 prepareToTerminate(conn, endpos, false, cur_record_lsn);
599 time_to_abort = true;
604 res = PQgetResult(conn);
605 if (PQresultStatus(res) == PGRES_COPY_OUT)
608 * We're doing a client-initiated clean exit and have sent CopyDone to
609 * the server. We've already sent replay confirmation and fsync'd so
610 * we can just clean up the connection now.
614 else if (PQresultStatus(res) != PGRES_COMMAND_OK)
617 _("%s: unexpected termination of replication stream: %s"),
618 progname, PQresultErrorMessage(res));
623 if (outfd != -1 && strcmp(outfile, "-") != 0)
625 TimestampTz t = feGetCurrentTimestamp();
627 /* no need to jump to error on failure here, we're finishing anyway */
630 if (close(outfd) != 0)
631 fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
632 progname, outfile, strerror(errno));
641 destroyPQExpBuffer(query);
647 * Unfortunately we can't do sensible signal handling on windows...
652 * When sigint is called, just tell the system to exit at the next possible
656 sigint_handler(int signum)
658 time_to_abort = true;
662 * Trigger the output file to be reopened.
665 sighup_handler(int signum)
667 output_reopen = true;
673 main(int argc, char **argv)
675 static struct option long_options[] = {
676 /* general options */
677 {"file", required_argument, NULL, 'f'},
678 {"fsync-interval", required_argument, NULL, 'F'},
679 {"no-loop", no_argument, NULL, 'n'},
680 {"verbose", no_argument, NULL, 'v'},
681 {"version", no_argument, NULL, 'V'},
682 {"help", no_argument, NULL, '?'},
683 /* connection options */
684 {"dbname", required_argument, NULL, 'd'},
685 {"host", required_argument, NULL, 'h'},
686 {"port", required_argument, NULL, 'p'},
687 {"username", required_argument, NULL, 'U'},
688 {"no-password", no_argument, NULL, 'w'},
689 {"password", no_argument, NULL, 'W'},
690 /* replication options */
691 {"startpos", required_argument, NULL, 'I'},
692 {"endpos", required_argument, NULL, 'E'},
693 {"option", required_argument, NULL, 'o'},
694 {"plugin", required_argument, NULL, 'P'},
695 {"status-interval", required_argument, NULL, 's'},
696 {"slot", required_argument, NULL, 'S'},
698 {"create-slot", no_argument, NULL, 1},
699 {"start", no_argument, NULL, 2},
700 {"drop-slot", no_argument, NULL, 3},
701 {"if-not-exists", no_argument, NULL, 4},
710 progname = get_progname(argv[0]);
711 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
715 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
720 else if (strcmp(argv[1], "-V") == 0 ||
721 strcmp(argv[1], "--version") == 0)
723 puts("pg_recvlogical (PostgreSQL) " PG_VERSION);
728 while ((c = getopt_long(argc, argv, "E:f:F:nvd:h:p:U:wWI:o:P:s:S:",
729 long_options, &option_index)) != -1)
733 /* general options */
735 outfile = pg_strdup(optarg);
738 fsync_interval = atoi(optarg) * 1000;
739 if (fsync_interval < 0)
741 fprintf(stderr, _("%s: invalid fsync interval \"%s\"\n"),
752 /* connection options */
754 dbname = pg_strdup(optarg);
757 dbhost = pg_strdup(optarg);
760 if (atoi(optarg) <= 0)
762 fprintf(stderr, _("%s: invalid port number \"%s\"\n"),
766 dbport = pg_strdup(optarg);
769 dbuser = pg_strdup(optarg);
777 /* replication options */
779 if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
782 _("%s: could not parse start position \"%s\"\n"),
786 startpos = ((uint64) hi) << 32 | lo;
789 if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
792 _("%s: could not parse end position \"%s\"\n"),
796 endpos = ((uint64) hi) << 32 | lo;
800 char *data = pg_strdup(optarg);
801 char *val = strchr(data, '=');
805 /* remove =; separate data from val */
811 options = pg_realloc(options, sizeof(char *) * noptions * 2);
813 options[(noptions - 1) * 2] = data;
814 options[(noptions - 1) * 2 + 1] = val;
819 plugin = pg_strdup(optarg);
822 standby_message_timeout = atoi(optarg) * 1000;
823 if (standby_message_timeout < 0)
825 fprintf(stderr, _("%s: invalid status interval \"%s\"\n"),
831 replication_slot = pg_strdup(optarg);
835 do_create_slot = true;
838 do_start_slot = true;
844 slot_exists_ok = true;
850 * getopt_long already emitted a complaint
852 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
859 * Any non-option arguments?
864 _("%s: too many command-line arguments (first is \"%s\")\n"),
865 progname, argv[optind]);
866 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
874 if (replication_slot == NULL)
876 fprintf(stderr, _("%s: no slot specified\n"), progname);
877 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
882 if (do_start_slot && outfile == NULL)
884 fprintf(stderr, _("%s: no target file specified\n"), progname);
885 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
890 if (!do_drop_slot && dbname == NULL)
892 fprintf(stderr, _("%s: no database specified\n"), progname);
893 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
898 if (!do_drop_slot && !do_create_slot && !do_start_slot)
900 fprintf(stderr, _("%s: at least one action needs to be specified\n"), progname);
901 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
906 if (do_drop_slot && (do_create_slot || do_start_slot))
908 fprintf(stderr, _("%s: cannot use --create-slot or --start together with --drop-slot\n"), progname);
909 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
914 if (startpos != InvalidXLogRecPtr && (do_create_slot || do_drop_slot))
916 fprintf(stderr, _("%s: cannot use --create-slot or --drop-slot together with --startpos\n"), progname);
917 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
922 if (endpos != InvalidXLogRecPtr && !do_start_slot)
925 _("%s: --endpos may only be specified with --start\n"),
927 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
933 pqsignal(SIGINT, sigint_handler);
934 pqsignal(SIGHUP, sighup_handler);
938 * Obtain a connection to server. This is not really necessary but it
939 * helps to get more precise error messages about authentication, required
940 * GUC parameters and such.
942 conn = GetConnection();
944 /* Error message already written in GetConnection() */
948 * Run IDENTIFY_SYSTEM to make sure we connected using a database specific
949 * replication connection.
951 if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
952 disconnect_and_exit(1);
957 _("%s: could not establish database-specific replication connection\n"),
959 disconnect_and_exit(1);
962 /* Drop a replication slot. */
967 _("%s: dropping replication slot \"%s\"\n"),
968 progname, replication_slot);
970 if (!DropReplicationSlot(conn, replication_slot))
971 disconnect_and_exit(1);
974 /* Create a replication slot. */
979 _("%s: creating replication slot \"%s\"\n"),
980 progname, replication_slot);
982 if (!CreateReplicationSlot(conn, replication_slot, plugin,
983 false, slot_exists_ok))
984 disconnect_and_exit(1);
985 startpos = InvalidXLogRecPtr;
989 disconnect_and_exit(0);
998 * We've been Ctrl-C'ed or reached an exit limit condition. That's
999 * not an error, so exit without an errorcode.
1001 disconnect_and_exit(0);
1005 fprintf(stderr, _("%s: disconnected\n"), progname);
1011 /* translator: check source for value for %d */
1012 _("%s: disconnected; waiting %d seconds to try again\n"),
1013 progname, RECONNECT_SLEEP_TIME);
1014 pg_usleep(RECONNECT_SLEEP_TIME * 1000000);
1020 * Fsync our output data, and send a feedback message to the server. Returns
1021 * true if successful, false otherwise.
1023 * If successful, *now is updated to the current timestamp just before sending
1027 flushAndSendFeedback(PGconn *conn, TimestampTz *now)
1029 /* flush data to disk, so that we send a recent flush pointer */
1030 if (!OutputFsync(*now))
1032 *now = feGetCurrentTimestamp();
1033 if (!sendFeedback(conn, *now, true, false))
1040 * Try to inform the server about of upcoming demise, but don't wait around or
1044 prepareToTerminate(PGconn *conn, XLogRecPtr endpos, bool keepalive, XLogRecPtr lsn)
1046 (void) PQputCopyEnd(conn, NULL);
1047 (void) PQflush(conn);
1052 fprintf(stderr, "%s: endpos %X/%X reached by keepalive\n",
1054 (uint32) (endpos >> 32), (uint32) endpos);
1056 fprintf(stderr, "%s: endpos %X/%X reached by record at %X/%X\n",
1057 progname, (uint32) (endpos >> 32), (uint32) (endpos),
1058 (uint32) (lsn >> 32), (uint32) lsn);