1 /*-------------------------------------------------------------------------
3 * receivelog.c - receive WAL files using the streaming
4 * replication protocol.
6 * Author: Magnus Hagander <magnus@hagander.net>
8 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
11 * src/bin/pg_basebackup/receivelog.c
12 *-------------------------------------------------------------------------
15 #include "postgres_fe.h"
19 #ifdef HAVE_SYS_SELECT_H
20 #include <sys/select.h>
24 #include "receivelog.h"
25 #include "streamutil.h"
28 #include "access/xlog_internal.h"
29 #include "common/file_utils.h"
32 /* fd and filename for currently open WAL file */
33 static Walfile *walfile = NULL;
34 static char current_walfile_name[MAXPGPATH] = "";
35 static bool reportFlushPosition = false;
36 static XLogRecPtr lastFlushPosition = InvalidXLogRecPtr;
38 static bool still_sending = true; /* feedback still needs to be sent? */
40 static PGresult *HandleCopyStream(PGconn *conn, StreamCtl *stream,
42 static int CopyStreamPoll(PGconn *conn, long timeout_ms, pgsocket stop_socket);
43 static int CopyStreamReceive(PGconn *conn, long timeout, pgsocket stop_socket,
45 static bool ProcessKeepaliveMsg(PGconn *conn, StreamCtl *stream, char *copybuf,
46 int len, XLogRecPtr blockpos, TimestampTz *last_status);
47 static bool ProcessXLogDataMsg(PGconn *conn, StreamCtl *stream, char *copybuf, int len,
48 XLogRecPtr *blockpos);
49 static PGresult *HandleEndOfCopyStream(PGconn *conn, StreamCtl *stream, char *copybuf,
50 XLogRecPtr blockpos, XLogRecPtr *stoppos);
51 static bool CheckCopyStreamStop(PGconn *conn, StreamCtl *stream, XLogRecPtr blockpos,
53 static long CalculateCopyStreamSleeptime(TimestampTz now, int standby_message_timeout,
54 TimestampTz last_status);
56 static bool ReadEndOfStreamingResult(PGresult *res, XLogRecPtr *startpos,
60 mark_file_as_archived(StreamCtl *stream, const char *fname)
63 static char tmppath[MAXPGPATH];
65 snprintf(tmppath, sizeof(tmppath), "archive_status/%s.done",
68 f = stream->walmethod->open_for_write(tmppath, NULL, 0);
71 fprintf(stderr, _("%s: could not create archive status file \"%s\": %s\n"),
72 progname, tmppath, stream->walmethod->getlasterror());
76 stream->walmethod->close(f, CLOSE_NORMAL);
82 * Open a new WAL file in the specified directory.
84 * Returns true if OK; on failure, returns false after printing an error msg.
85 * On success, 'walfile' is set to the FD for the file, and the base filename
86 * (without partial_suffix) is stored in 'current_walfile_name'.
88 * The file will be padded to 16Mb with zeroes.
91 open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
98 XLByteToSeg(startpoint, segno);
99 XLogFileName(current_walfile_name, stream->timeline, segno);
101 snprintf(fn, sizeof(fn), "%s%s", current_walfile_name,
102 stream->partial_suffix ? stream->partial_suffix : "");
105 * When streaming to files, if an existing file exists we verify that it's
106 * either empty (just created), or a complete XLogSegSize segment (in
107 * which case it has been created and padded). Anything else indicates a
110 * When streaming to tar, no file with this name will exist before, so we
111 * never have to verify a size.
113 if (stream->walmethod->existsfile(fn))
115 size = stream->walmethod->get_file_size(fn);
119 _("%s: could not get size of write-ahead log file \"%s\": %s\n"),
120 progname, fn, stream->walmethod->getlasterror());
123 if (size == XLogSegSize)
125 /* Already padded file. Open it for use */
126 f = stream->walmethod->open_for_write(current_walfile_name, stream->partial_suffix, 0);
130 _("%s: could not open existing write-ahead log file \"%s\": %s\n"),
131 progname, fn, stream->walmethod->getlasterror());
135 /* fsync file in case of a previous crash */
136 if (stream->walmethod->sync(f) != 0)
139 _("%s: could not sync existing write-ahead log file \"%s\": %s\n"),
140 progname, fn, stream->walmethod->getlasterror());
141 stream->walmethod->close(f, CLOSE_UNLINK);
150 /* if write didn't set errno, assume problem is no disk space */
154 _("%s: write-ahead log file \"%s\" has %d bytes, should be 0 or %d\n"),
155 progname, fn, (int) size, XLogSegSize);
158 /* File existed and was empty, so fall through and open */
161 /* No file existed, so create one */
163 f = stream->walmethod->open_for_write(current_walfile_name, stream->partial_suffix, XLogSegSize);
167 _("%s: could not open write-ahead log file \"%s\": %s\n"),
168 progname, fn, stream->walmethod->getlasterror());
177 * Close the current WAL file (if open), and rename it to the correct
178 * filename if it's complete. On failure, prints an error message to stderr
179 * and returns false, otherwise returns true.
182 close_walfile(StreamCtl *stream, XLogRecPtr pos)
190 currpos = stream->walmethod->get_current_pos(walfile);
194 _("%s: could not determine seek position in file \"%s\": %s\n"),
195 progname, current_walfile_name, stream->walmethod->getlasterror());
196 stream->walmethod->close(walfile, CLOSE_UNLINK);
202 if (stream->partial_suffix)
204 if (currpos == XLOG_SEG_SIZE)
205 r = stream->walmethod->close(walfile, CLOSE_NORMAL);
209 _("%s: not renaming \"%s%s\", segment is not complete\n"),
210 progname, current_walfile_name, stream->partial_suffix);
211 r = stream->walmethod->close(walfile, CLOSE_NO_RENAME);
215 r = stream->walmethod->close(walfile, CLOSE_NORMAL);
221 fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
222 progname, current_walfile_name, stream->walmethod->getlasterror());
227 * Mark file as archived if requested by the caller - pg_basebackup needs
228 * to do so as files can otherwise get archived again after promotion of a
229 * new node. This is in line with walreceiver.c always doing a
230 * XLogArchiveForceDone() after a complete segment.
232 if (currpos == XLOG_SEG_SIZE && stream->mark_done)
234 /* writes error message if failed */
235 if (!mark_file_as_archived(stream, current_walfile_name))
239 lastFlushPosition = pos;
245 * Check if a timeline history file exists.
248 existsTimeLineHistoryFile(StreamCtl *stream)
250 char histfname[MAXFNAMELEN];
253 * Timeline 1 never has a history file. We treat that as if it existed,
254 * since we never need to stream it.
256 if (stream->timeline == 1)
259 TLHistoryFileName(histfname, stream->timeline);
261 return stream->walmethod->existsfile(histfname);
265 writeTimeLineHistoryFile(StreamCtl *stream, char *filename, char *content)
267 int size = strlen(content);
268 char histfname[MAXFNAMELEN];
272 * Check that the server's idea of how timeline history files should be
273 * named matches ours.
275 TLHistoryFileName(histfname, stream->timeline);
276 if (strcmp(histfname, filename) != 0)
278 fprintf(stderr, _("%s: server reported unexpected history file name for timeline %u: %s\n"),
279 progname, stream->timeline, filename);
283 f = stream->walmethod->open_for_write(histfname, ".tmp", 0);
286 fprintf(stderr, _("%s: could not create timeline history file \"%s\": %s\n"),
287 progname, histfname, stream->walmethod->getlasterror());
291 if ((int) stream->walmethod->write(f, content, size) != size)
293 fprintf(stderr, _("%s: could not write timeline history file \"%s\": %s\n"),
294 progname, histfname, stream->walmethod->getlasterror());
297 * If we fail to make the file, delete it to release disk space
299 stream->walmethod->close(f, CLOSE_UNLINK);
304 if (stream->walmethod->close(f, CLOSE_NORMAL) != 0)
306 fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
307 progname, histfname, stream->walmethod->getlasterror());
311 /* Maintain archive_status, check close_walfile() for details. */
312 if (stream->mark_done)
314 /* writes error message if failed */
315 if (!mark_file_as_archived(stream, histfname))
323 * Send a Standby Status Update message to server.
326 sendFeedback(PGconn *conn, XLogRecPtr blockpos, TimestampTz now, bool replyRequested)
328 char replybuf[1 + 8 + 8 + 8 + 8 + 1];
333 fe_sendint64(blockpos, &replybuf[len]); /* write */
335 if (reportFlushPosition)
336 fe_sendint64(lastFlushPosition, &replybuf[len]); /* flush */
338 fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* flush */
340 fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */
342 fe_sendint64(now, &replybuf[len]); /* sendTime */
344 replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */
347 if (PQputCopyData(conn, replybuf, len) <= 0 || PQflush(conn))
349 fprintf(stderr, _("%s: could not send feedback packet: %s"),
350 progname, PQerrorMessage(conn));
358 * Check that the server version we're connected to is supported by
359 * ReceiveXlogStream().
361 * If it's not, an error message is printed to stderr, and false is returned.
364 CheckServerVersionForStreaming(PGconn *conn)
371 * The message format used in streaming replication changed in 9.3, so we
372 * cannot stream from older servers. And we don't support servers newer
373 * than the client; it might work, but we don't know, so err on the safe
376 minServerMajor = 903;
377 maxServerMajor = PG_VERSION_NUM / 100;
378 serverMajor = PQserverVersion(conn) / 100;
379 if (serverMajor < minServerMajor)
381 const char *serverver = PQparameterStatus(conn, "server_version");
383 fprintf(stderr, _("%s: incompatible server version %s; client does not support streaming from server versions older than %s\n"),
385 serverver ? serverver : "'unknown'",
389 else if (serverMajor > maxServerMajor)
391 const char *serverver = PQparameterStatus(conn, "server_version");
393 fprintf(stderr, _("%s: incompatible server version %s; client does not support streaming from server versions newer than %s\n"),
395 serverver ? serverver : "'unknown'",
403 * Receive a log stream starting at the specified position.
405 * Individual parameters are passed through the StreamCtl structure.
407 * If sysidentifier is specified, validate that both the system
408 * identifier and the timeline matches the specified ones
409 * (by sending an extra IDENTIFY_SYSTEM command)
411 * All received segments will be written to the directory
412 * specified by basedir. This will also fetch any missing timeline history
415 * The stream_stop callback will be called every time data
416 * is received, and whenever a segment is completed. If it returns
417 * true, the streaming will stop and the function
418 * return. As long as it returns false, streaming will continue
421 * If stream_stop() checks for external input, stop_socket should be set to
422 * the FD it checks. This will allow such input to be detected promptly
423 * rather than after standby_message_timeout (which might be indefinite).
424 * Note that signals will interrupt waits for input as well, but that is
425 * race-y since a signal received while busy won't interrupt the wait.
427 * standby_message_timeout controls how often we send a message
428 * back to the master letting it know our progress, in milliseconds.
429 * Zero means no messages are sent.
430 * This message will only contain the write location, and never
433 * If 'partial_suffix' is not NULL, files are initially created with the
434 * given suffix, and the suffix is removed once the file is finished. That
435 * allows you to tell the difference between partial and completed files,
436 * so that you can continue later where you left.
438 * If 'synchronous' is true, the received WAL is flushed as soon as written,
439 * otherwise only when the WAL file is closed.
441 * Note: The WAL location *must* be at a log segment start!
444 ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
452 * The caller should've checked the server version already, but doesn't do
453 * any harm to check it here too.
455 if (!CheckServerVersionForStreaming(conn))
459 * Decide whether we want to report the flush position. If we report the
460 * flush position, the primary will know what WAL we'll possibly
461 * re-request, and it can then remove older WAL safely. We must always do
462 * that when we are using slots.
464 * Reporting the flush position makes one eligible as a synchronous
465 * replica. People shouldn't include generic names in
466 * synchronous_standby_names, but we've protected them against it so far,
467 * so let's continue to do so unless specifically requested.
469 if (stream->replication_slot != NULL)
471 reportFlushPosition = true;
472 sprintf(slotcmd, "SLOT \"%s\" ", stream->replication_slot);
476 if (stream->synchronous)
477 reportFlushPosition = true;
479 reportFlushPosition = false;
483 if (stream->sysidentifier != NULL)
485 /* Validate system identifier hasn't changed */
486 res = PQexec(conn, "IDENTIFY_SYSTEM");
487 if (PQresultStatus(res) != PGRES_TUPLES_OK)
490 _("%s: could not send replication command \"%s\": %s"),
491 progname, "IDENTIFY_SYSTEM", PQerrorMessage(conn));
495 if (PQntuples(res) != 1 || PQnfields(res) < 3)
498 _("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
499 progname, PQntuples(res), PQnfields(res), 1, 3);
503 if (strcmp(stream->sysidentifier, PQgetvalue(res, 0, 0)) != 0)
506 _("%s: system identifier does not match between base backup and streaming connection\n"),
511 if (stream->timeline > atoi(PQgetvalue(res, 0, 1)))
514 _("%s: starting timeline %u is not present in the server\n"),
515 progname, stream->timeline);
523 * Create temporary replication slot if one is needed
525 if (stream->temp_slot)
527 snprintf(query, sizeof(query),
528 "CREATE_REPLICATION_SLOT \"%s\" TEMPORARY PHYSICAL RESERVE_WAL",
529 stream->replication_slot);
530 res = PQexec(conn, query);
531 if (PQresultStatus(res) != PGRES_TUPLES_OK)
533 fprintf(stderr, _("%s: could not create temporary replication slot \"%s\": %s"),
534 progname, stream->replication_slot, PQerrorMessage(conn));
541 * initialize flush position to starting point, it's the caller's
542 * responsibility that that's sane.
544 lastFlushPosition = stream->startpos;
549 * Fetch the timeline history file for this timeline, if we don't have
550 * it already. When streaming log to tar, this will always return
551 * false, as we are never streaming into an existing file and
552 * therefore there can be no pre-existing timeline history file.
554 if (!existsTimeLineHistoryFile(stream))
556 snprintf(query, sizeof(query), "TIMELINE_HISTORY %u", stream->timeline);
557 res = PQexec(conn, query);
558 if (PQresultStatus(res) != PGRES_TUPLES_OK)
560 /* FIXME: we might send it ok, but get an error */
561 fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
562 progname, "TIMELINE_HISTORY", PQresultErrorMessage(res));
568 * The response to TIMELINE_HISTORY is a single row result set
569 * with two fields: filename and content
571 if (PQnfields(res) != 2 || PQntuples(res) != 1)
574 _("%s: unexpected response to TIMELINE_HISTORY command: got %d rows and %d fields, expected %d rows and %d fields\n"),
575 progname, PQntuples(res), PQnfields(res), 1, 2);
578 /* Write the history file to disk */
579 writeTimeLineHistoryFile(stream,
580 PQgetvalue(res, 0, 0),
581 PQgetvalue(res, 0, 1));
587 * Before we start streaming from the requested location, check if the
588 * callback tells us to stop here.
590 if (stream->stream_stop(stream->startpos, stream->timeline, false))
593 /* Initiate the replication stream at specified location */
594 snprintf(query, sizeof(query), "START_REPLICATION %s%X/%X TIMELINE %u",
596 (uint32) (stream->startpos >> 32), (uint32) stream->startpos,
598 res = PQexec(conn, query);
599 if (PQresultStatus(res) != PGRES_COPY_BOTH)
601 fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
602 progname, "START_REPLICATION", PQresultErrorMessage(res));
609 res = HandleCopyStream(conn, stream, &stoppos);
614 * Streaming finished.
616 * There are two possible reasons for that: a controlled shutdown, or
617 * we reached the end of the current timeline. In case of
618 * end-of-timeline, the server sends a result set after Copy has
619 * finished, containing information about the next timeline. Read
620 * that, and restart streaming from the next timeline. In case of
621 * controlled shutdown, stop here.
623 if (PQresultStatus(res) == PGRES_TUPLES_OK)
626 * End-of-timeline. Read the next timeline's ID and starting
627 * position. Usually, the starting position will match the end of
628 * the previous timeline, but there are corner cases like if the
629 * server had sent us half of a WAL record, when it was promoted.
630 * The new timeline will begin at the end of the last complete
631 * record in that case, overlapping the partial WAL record on the
637 parsed = ReadEndOfStreamingResult(res, &stream->startpos, &newtimeline);
642 /* Sanity check the values the server gave us */
643 if (newtimeline <= stream->timeline)
646 _("%s: server reported unexpected next timeline %u, following timeline %u\n"),
647 progname, newtimeline, stream->timeline);
650 if (stream->startpos > stoppos)
653 _("%s: server stopped streaming timeline %u at %X/%X, but reported next timeline %u to begin at %X/%X\n"),
655 stream->timeline, (uint32) (stoppos >> 32), (uint32) stoppos,
656 newtimeline, (uint32) (stream->startpos >> 32), (uint32) stream->startpos);
660 /* Read the final result, which should be CommandComplete. */
661 res = PQgetResult(conn);
662 if (PQresultStatus(res) != PGRES_COMMAND_OK)
665 _("%s: unexpected termination of replication stream: %s"),
666 progname, PQresultErrorMessage(res));
673 * Loop back to start streaming from the new timeline. Always
674 * start streaming at the beginning of a segment.
676 stream->timeline = newtimeline;
677 stream->startpos = stream->startpos - (stream->startpos % XLOG_SEG_SIZE);
680 else if (PQresultStatus(res) == PGRES_COMMAND_OK)
685 * End of replication (ie. controlled shut down of the server).
687 * Check if the callback thinks it's OK to stop here. If not,
690 if (stream->stream_stop(stoppos, stream->timeline, false))
694 fprintf(stderr, _("%s: replication stream was terminated before stop point\n"),
701 /* Server returned an error. */
703 _("%s: unexpected termination of replication stream: %s"),
704 progname, PQresultErrorMessage(res));
711 if (walfile != NULL && stream->walmethod->close(walfile, CLOSE_NO_RENAME) != 0)
712 fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
713 progname, current_walfile_name, stream->walmethod->getlasterror());
719 * Helper function to parse the result set returned by server after streaming
720 * has finished. On failure, prints an error to stderr and returns false.
723 ReadEndOfStreamingResult(PGresult *res, XLogRecPtr *startpos, uint32 *timeline)
725 uint32 startpos_xlogid,
729 * The result set consists of one row and two columns, e.g:
731 * next_tli | next_tli_startpos
732 * ----------+-------------------
735 * next_tli is the timeline ID of the next timeline after the one that
736 * just finished streaming. next_tli_startpos is the WAL location where
737 * the server switched to it.
740 if (PQnfields(res) < 2 || PQntuples(res) != 1)
743 _("%s: unexpected result set after end-of-timeline: got %d rows and %d fields, expected %d rows and %d fields\n"),
744 progname, PQntuples(res), PQnfields(res), 1, 2);
748 *timeline = atoi(PQgetvalue(res, 0, 0));
749 if (sscanf(PQgetvalue(res, 0, 1), "%X/%X", &startpos_xlogid,
750 &startpos_xrecoff) != 2)
753 _("%s: could not parse next timeline's starting point \"%s\"\n"),
754 progname, PQgetvalue(res, 0, 1));
757 *startpos = ((uint64) startpos_xlogid << 32) | startpos_xrecoff;
763 * The main loop of ReceiveXlogStream. Handles the COPY stream after
764 * initiating streaming with the START_STREAMING command.
766 * If the COPY ends (not necessarily successfully) due a message from the
767 * server, returns a PGresult and sets *stoppos to the last byte written.
768 * On any other sort of error, returns NULL.
771 HandleCopyStream(PGconn *conn, StreamCtl *stream,
774 char *copybuf = NULL;
775 TimestampTz last_status = -1;
776 XLogRecPtr blockpos = stream->startpos;
778 still_sending = true;
787 * Check if we should continue streaming, or abort at this point.
789 if (!CheckCopyStreamStop(conn, stream, blockpos, stoppos))
792 now = feGetCurrentTimestamp();
795 * If synchronous option is true, issue sync command as soon as there
796 * are WAL data which has not been flushed yet.
798 if (stream->synchronous && lastFlushPosition < blockpos && walfile != NULL)
800 if (stream->walmethod->sync(walfile) != 0)
802 fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"),
803 progname, current_walfile_name, stream->walmethod->getlasterror());
806 lastFlushPosition = blockpos;
809 * Send feedback so that the server sees the latest WAL locations
812 if (!sendFeedback(conn, blockpos, now, false))
818 * Potentially send a status message to the master
820 if (still_sending && stream->standby_message_timeout > 0 &&
821 feTimestampDifferenceExceeds(last_status, now,
822 stream->standby_message_timeout))
824 /* Time to send feedback! */
825 if (!sendFeedback(conn, blockpos, now, false))
831 * Calculate how long send/receive loops should sleep
833 sleeptime = CalculateCopyStreamSleeptime(now, stream->standby_message_timeout,
836 r = CopyStreamReceive(conn, sleeptime, stream->stop_socket, ©buf);
843 PGresult *res = HandleEndOfCopyStream(conn, stream, copybuf, blockpos, stoppos);
851 /* Check the message type. */
852 if (copybuf[0] == 'k')
854 if (!ProcessKeepaliveMsg(conn, stream, copybuf, r, blockpos,
858 else if (copybuf[0] == 'w')
860 if (!ProcessXLogDataMsg(conn, stream, copybuf, r, &blockpos))
864 * Check if we should continue streaming, or abort at this
867 if (!CheckCopyStreamStop(conn, stream, blockpos, stoppos))
872 fprintf(stderr, _("%s: unrecognized streaming header: \"%c\"\n"),
873 progname, copybuf[0]);
878 * Process the received data, and any subsequent data we can read
881 r = CopyStreamReceive(conn, 0, stream->stop_socket, ©buf);
892 * Wait until we can read a CopyData message,
893 * or timeout, or occurrence of a signal or input on the stop_socket.
894 * (timeout_ms < 0 means wait indefinitely; 0 means don't wait.)
896 * Returns 1 if data has become available for reading, 0 if timed out
897 * or interrupted by signal or stop_socket input, and -1 on an error.
900 CopyStreamPoll(PGconn *conn, long timeout_ms, pgsocket stop_socket)
906 struct timeval timeout;
907 struct timeval *timeoutptr;
909 connsocket = PQsocket(conn);
912 fprintf(stderr, _("%s: invalid socket: %s"), progname,
913 PQerrorMessage(conn));
917 FD_ZERO(&input_mask);
918 FD_SET(connsocket, &input_mask);
920 if (stop_socket != PGINVALID_SOCKET)
922 FD_SET(stop_socket, &input_mask);
923 maxfd = Max(maxfd, stop_socket);
930 timeout.tv_sec = timeout_ms / 1000L;
931 timeout.tv_usec = (timeout_ms % 1000L) * 1000L;
932 timeoutptr = &timeout;
935 ret = select(maxfd + 1, &input_mask, NULL, NULL, timeoutptr);
940 return 0; /* Got a signal, so not an error */
941 fprintf(stderr, _("%s: select() failed: %s\n"),
942 progname, strerror(errno));
945 if (ret > 0 && FD_ISSET(connsocket, &input_mask))
946 return 1; /* Got input on connection socket */
948 return 0; /* Got timeout or input on stop_socket */
952 * Receive CopyData message available from XLOG stream, blocking for
953 * maximum of 'timeout' ms.
955 * If data was received, returns the length of the data. *buffer is set to
956 * point to a buffer holding the received message. The buffer is only valid
957 * until the next CopyStreamReceive call.
959 * Returns 0 if no data was available within timeout, or if wait was
960 * interrupted by signal or stop_socket input.
961 * -1 on error. -2 if the server ended the COPY.
964 CopyStreamReceive(PGconn *conn, long timeout, pgsocket stop_socket,
967 char *copybuf = NULL;
974 /* Try to receive a CopyData message */
975 rawlen = PQgetCopyData(conn, ©buf, 1);
981 * No data available. Wait for some to appear, but not longer than
982 * the specified timeout, so that we can ping the server. Also stop
983 * waiting if input appears on stop_socket.
985 ret = CopyStreamPoll(conn, timeout, stop_socket);
989 /* Now there is actually data on the socket */
990 if (PQconsumeInput(conn) == 0)
993 _("%s: could not receive data from WAL stream: %s"),
994 progname, PQerrorMessage(conn));
998 /* Now that we've consumed some input, try again */
999 rawlen = PQgetCopyData(conn, ©buf, 1);
1003 if (rawlen == -1) /* end-of-streaming or error */
1007 fprintf(stderr, _("%s: could not read COPY data: %s"),
1008 progname, PQerrorMessage(conn));
1012 /* Return received messages to caller */
1018 * Process the keepalive message.
1021 ProcessKeepaliveMsg(PGconn *conn, StreamCtl *stream, char *copybuf, int len,
1022 XLogRecPtr blockpos, TimestampTz *last_status)
1025 bool replyRequested;
1029 * Parse the keepalive message, enclosed in the CopyData message. We just
1030 * check if the server requested a reply, and ignore the rest.
1032 pos = 1; /* skip msgtype 'k' */
1033 pos += 8; /* skip walEnd */
1034 pos += 8; /* skip sendTime */
1038 fprintf(stderr, _("%s: streaming header too small: %d\n"),
1042 replyRequested = copybuf[pos];
1044 /* If the server requested an immediate reply, send one. */
1045 if (replyRequested && still_sending)
1047 if (reportFlushPosition && lastFlushPosition < blockpos &&
1051 * If a valid flush location needs to be reported, flush the
1052 * current WAL file so that the latest flush location is sent back
1053 * to the server. This is necessary to see whether the last WAL
1054 * data has been successfully replicated or not, at the normal
1055 * shutdown of the server.
1057 if (stream->walmethod->sync(walfile) != 0)
1059 fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"),
1060 progname, current_walfile_name, stream->walmethod->getlasterror());
1063 lastFlushPosition = blockpos;
1066 now = feGetCurrentTimestamp();
1067 if (!sendFeedback(conn, blockpos, now, false))
1076 * Process XLogData message.
1079 ProcessXLogDataMsg(PGconn *conn, StreamCtl *stream, char *copybuf, int len,
1080 XLogRecPtr *blockpos)
1088 * Once we've decided we don't want to receive any more, just ignore any
1089 * subsequent XLogData messages.
1091 if (!(still_sending))
1095 * Read the header of the XLogData message, enclosed in the CopyData
1096 * message. We only need the WAL location field (dataStart), the rest of
1097 * the header is ignored.
1099 hdr_len = 1; /* msgtype 'w' */
1100 hdr_len += 8; /* dataStart */
1101 hdr_len += 8; /* walEnd */
1102 hdr_len += 8; /* sendTime */
1105 fprintf(stderr, _("%s: streaming header too small: %d\n"),
1109 *blockpos = fe_recvint64(©buf[1]);
1111 /* Extract WAL location for this block */
1112 xlogoff = *blockpos % XLOG_SEG_SIZE;
1115 * Verify that the initial location in the stream matches where we think
1118 if (walfile == NULL)
1120 /* No file open yet */
1124 _("%s: received write-ahead log record for offset %u with no file open\n"),
1131 /* More data in existing segment */
1132 if (stream->walmethod->get_current_pos(walfile) != xlogoff)
1135 _("%s: got WAL data offset %08x, expected %08x\n"),
1136 progname, xlogoff, (int) stream->walmethod->get_current_pos(walfile));
1141 bytes_left = len - hdr_len;
1149 * If crossing a WAL boundary, only write up until we reach
1152 if (xlogoff + bytes_left > XLOG_SEG_SIZE)
1153 bytes_to_write = XLOG_SEG_SIZE - xlogoff;
1155 bytes_to_write = bytes_left;
1157 if (walfile == NULL)
1159 if (!open_walfile(stream, *blockpos))
1161 /* Error logged by open_walfile */
1166 if (stream->walmethod->write(walfile, copybuf + hdr_len + bytes_written,
1167 bytes_to_write) != bytes_to_write)
1170 _("%s: could not write %u bytes to WAL file \"%s\": %s\n"),
1171 progname, bytes_to_write, current_walfile_name,
1172 stream->walmethod->getlasterror());
1176 /* Write was successful, advance our position */
1177 bytes_written += bytes_to_write;
1178 bytes_left -= bytes_to_write;
1179 *blockpos += bytes_to_write;
1180 xlogoff += bytes_to_write;
1182 /* Did we reach the end of a WAL segment? */
1183 if (*blockpos % XLOG_SEG_SIZE == 0)
1185 if (!close_walfile(stream, *blockpos))
1186 /* Error message written in close_walfile() */
1191 if (still_sending && stream->stream_stop(*blockpos, stream->timeline, true))
1193 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1195 fprintf(stderr, _("%s: could not send copy-end packet: %s"),
1196 progname, PQerrorMessage(conn));
1199 still_sending = false;
1200 return true; /* ignore the rest of this XLogData packet */
1204 /* No more data left to write, receive next copy packet */
1210 * Handle end of the copy stream.
1213 HandleEndOfCopyStream(PGconn *conn, StreamCtl *stream, char *copybuf,
1214 XLogRecPtr blockpos, XLogRecPtr *stoppos)
1216 PGresult *res = PQgetResult(conn);
1219 * The server closed its end of the copy stream. If we haven't closed
1220 * ours already, we need to do so now, unless the server threw an error,
1221 * in which case we don't.
1225 if (!close_walfile(stream, blockpos))
1227 /* Error message written in close_walfile() */
1231 if (PQresultStatus(res) == PGRES_COPY_IN)
1233 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1236 _("%s: could not send copy-end packet: %s"),
1237 progname, PQerrorMessage(conn));
1241 res = PQgetResult(conn);
1243 still_sending = false;
1245 if (copybuf != NULL)
1247 *stoppos = blockpos;
1252 * Check if we should continue streaming, or abort at this point.
1255 CheckCopyStreamStop(PGconn *conn, StreamCtl *stream, XLogRecPtr blockpos,
1256 XLogRecPtr *stoppos)
1258 if (still_sending && stream->stream_stop(blockpos, stream->timeline, false))
1260 if (!close_walfile(stream, blockpos))
1262 /* Potential error message is written by close_walfile */
1265 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1267 fprintf(stderr, _("%s: could not send copy-end packet: %s"),
1268 progname, PQerrorMessage(conn));
1271 still_sending = false;
1278 * Calculate how long send/receive loops should sleep
1281 CalculateCopyStreamSleeptime(TimestampTz now, int standby_message_timeout,
1282 TimestampTz last_status)
1284 TimestampTz status_targettime = 0;
1287 if (standby_message_timeout && still_sending)
1288 status_targettime = last_status +
1289 (standby_message_timeout - 1) * ((int64) 1000);
1291 if (status_targettime > 0)
1296 feTimestampDifference(now,
1300 /* Always sleep at least 1 sec */
1307 sleeptime = secs * 1000 + usecs / 1000;