1 /*-------------------------------------------------------------------------
3 * receivelog.c - receive transaction log files using the streaming
4 * replication protocol.
6 * Author: Magnus Hagander <magnus@hagander.net>
8 * Portions Copyright (c) 1996-2016, 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 int walfile = -1;
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);
43 static int CopyStreamReceive(PGconn *conn, long timeout, char **buffer);
44 static bool ProcessKeepaliveMsg(PGconn *conn, char *copybuf, int len,
45 XLogRecPtr blockpos, int64 *last_status);
46 static bool ProcessXLogDataMsg(PGconn *conn, StreamCtl *stream, char *copybuf, int len,
47 XLogRecPtr *blockpos);
48 static PGresult *HandleEndOfCopyStream(PGconn *conn, StreamCtl *stream, char *copybuf,
49 XLogRecPtr blockpos, XLogRecPtr *stoppos);
50 static bool CheckCopyStreamStop(PGconn *conn, StreamCtl *stream, XLogRecPtr blockpos,
52 static long CalculateCopyStreamSleeptime(int64 now, int standby_message_timeout,
55 static bool ReadEndOfStreamingResult(PGresult *res, XLogRecPtr *startpos,
59 mark_file_as_archived(const char *basedir, const char *fname)
62 static char tmppath[MAXPGPATH];
64 snprintf(tmppath, sizeof(tmppath), "%s/archive_status/%s.done",
67 fd = open(tmppath, O_WRONLY | O_CREAT | PG_BINARY, S_IRUSR | S_IWUSR);
70 fprintf(stderr, _("%s: could not create archive status file \"%s\": %s\n"),
71 progname, tmppath, strerror(errno));
77 if (fsync_fname(tmppath, false, progname) != 0)
80 if (fsync_parent_path(tmppath, progname) != 0)
87 * Open a new WAL file in the specified directory.
89 * The file will be padded to 16Mb with zeroes. The base filename (without
90 * partial_suffix) is stored in current_walfile_name.
93 open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
102 XLByteToSeg(startpoint, segno);
103 XLogFileName(current_walfile_name, stream->timeline, segno);
105 snprintf(fn, sizeof(fn), "%s/%s%s", stream->basedir, current_walfile_name,
106 stream->partial_suffix ? stream->partial_suffix : "");
107 f = open(fn, O_WRONLY | O_CREAT | PG_BINARY, S_IRUSR | S_IWUSR);
111 _("%s: could not open transaction log file \"%s\": %s\n"),
112 progname, fn, strerror(errno));
117 * Verify that the file is either empty (just created), or a complete
118 * XLogSegSize segment. Anything in between indicates a corrupt file.
120 if (fstat(f, &statbuf) != 0)
123 _("%s: could not stat transaction log file \"%s\": %s\n"),
124 progname, fn, strerror(errno));
128 if (statbuf.st_size == XLogSegSize)
130 /* File is open and ready to use */
134 * fsync, in case of a previous crash between padding and fsyncing the
137 if (fsync_fname(fn, false, progname) != 0)
139 if (fsync_parent_path(fn, progname) != 0)
144 if (statbuf.st_size != 0)
147 _("%s: transaction log file \"%s\" has %d bytes, should be 0 or %d\n"),
148 progname, fn, (int) statbuf.st_size, XLogSegSize);
153 /* New, empty, file. So pad it to 16Mb with zeroes */
154 zerobuf = pg_malloc0(XLOG_BLCKSZ);
155 for (bytes = 0; bytes < XLogSegSize; bytes += XLOG_BLCKSZ)
157 if (write(f, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
160 _("%s: could not pad transaction log file \"%s\": %s\n"),
161 progname, fn, strerror(errno));
171 * fsync WAL file and containing directory, to ensure the file is
172 * persistently created and zeroed. That's particularly important when
173 * using synchronous mode, where the file is modified and fsynced
174 * in-place, without a directory fsync.
176 if (fsync_fname(fn, false, progname) != 0)
178 if (fsync_parent_path(fn, progname) != 0)
181 if (lseek(f, SEEK_SET, 0) != 0)
184 _("%s: could not seek to beginning of transaction log file \"%s\": %s\n"),
185 progname, fn, strerror(errno));
194 * Close the current WAL file (if open), and rename it to the correct
195 * filename if it's complete. On failure, prints an error message to stderr
196 * and returns false, otherwise returns true.
199 close_walfile(StreamCtl *stream, XLogRecPtr pos)
206 currpos = lseek(walfile, 0, SEEK_CUR);
210 _("%s: could not determine seek position in file \"%s\": %s\n"),
211 progname, current_walfile_name, strerror(errno));
215 if (fsync(walfile) != 0)
217 fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"),
218 progname, current_walfile_name, strerror(errno));
222 if (close(walfile) != 0)
224 fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
225 progname, current_walfile_name, strerror(errno));
232 * If we finished writing a .partial file, rename it into place.
234 if (currpos == XLOG_SEG_SIZE && stream->partial_suffix)
236 char oldfn[MAXPGPATH];
237 char newfn[MAXPGPATH];
239 snprintf(oldfn, sizeof(oldfn), "%s/%s%s", stream->basedir, current_walfile_name, stream->partial_suffix);
240 snprintf(newfn, sizeof(newfn), "%s/%s", stream->basedir, current_walfile_name);
241 if (durable_rename(oldfn, newfn, progname) != 0)
243 /* durable_rename produced a log entry */
247 else if (stream->partial_suffix)
249 _("%s: not renaming \"%s%s\", segment is not complete\n"),
250 progname, current_walfile_name, stream->partial_suffix);
253 * Mark file as archived if requested by the caller - pg_basebackup needs
254 * to do so as files can otherwise get archived again after promotion of a
255 * new node. This is in line with walreceiver.c always doing a
256 * XLogArchiveForceDone() after a complete segment.
258 if (currpos == XLOG_SEG_SIZE && stream->mark_done)
260 /* writes error message if failed */
261 if (!mark_file_as_archived(stream->basedir, current_walfile_name))
265 lastFlushPosition = pos;
271 * Check if a timeline history file exists.
274 existsTimeLineHistoryFile(StreamCtl *stream)
276 char path[MAXPGPATH];
277 char histfname[MAXFNAMELEN];
281 * Timeline 1 never has a history file. We treat that as if it existed,
282 * since we never need to stream it.
284 if (stream->timeline == 1)
287 TLHistoryFileName(histfname, stream->timeline);
289 snprintf(path, sizeof(path), "%s/%s", stream->basedir, histfname);
291 fd = open(path, O_RDONLY | PG_BINARY, 0);
295 fprintf(stderr, _("%s: could not open timeline history file \"%s\": %s\n"),
296 progname, path, strerror(errno));
307 writeTimeLineHistoryFile(StreamCtl *stream, char *filename, char *content)
309 int size = strlen(content);
310 char path[MAXPGPATH];
311 char tmppath[MAXPGPATH];
312 char histfname[MAXFNAMELEN];
316 * Check that the server's idea of how timeline history files should be
317 * named matches ours.
319 TLHistoryFileName(histfname, stream->timeline);
320 if (strcmp(histfname, filename) != 0)
322 fprintf(stderr, _("%s: server reported unexpected history file name for timeline %u: %s\n"),
323 progname, stream->timeline, filename);
327 snprintf(path, sizeof(path), "%s/%s", stream->basedir, histfname);
330 * Write into a temp file name.
332 snprintf(tmppath, MAXPGPATH, "%s.tmp", path);
336 fd = open(tmppath, O_WRONLY | O_CREAT | PG_BINARY, S_IRUSR | S_IWUSR);
339 fprintf(stderr, _("%s: could not create timeline history file \"%s\": %s\n"),
340 progname, tmppath, strerror(errno));
345 if ((int) write(fd, content, size) != size)
347 int save_errno = errno;
350 * If we fail to make the file, delete it to release disk space
356 fprintf(stderr, _("%s: could not write timeline history file \"%s\": %s\n"),
357 progname, tmppath, strerror(errno));
363 fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
364 progname, tmppath, strerror(errno));
369 * Now move the completed history file into place with its final name.
371 if (durable_rename(tmppath, path, progname) < 0)
373 /* durable_rename produced a log entry */
377 /* Maintain archive_status, check close_walfile() for details. */
378 if (stream->mark_done)
380 /* writes error message if failed */
381 if (!mark_file_as_archived(stream->basedir, histfname))
389 * Send a Standby Status Update message to server.
392 sendFeedback(PGconn *conn, XLogRecPtr blockpos, int64 now, bool replyRequested)
394 char replybuf[1 + 8 + 8 + 8 + 8 + 1];
399 fe_sendint64(blockpos, &replybuf[len]); /* write */
401 if (reportFlushPosition)
402 fe_sendint64(lastFlushPosition, &replybuf[len]); /* flush */
404 fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* flush */
406 fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */
408 fe_sendint64(now, &replybuf[len]); /* sendTime */
410 replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */
413 if (PQputCopyData(conn, replybuf, len) <= 0 || PQflush(conn))
415 fprintf(stderr, _("%s: could not send feedback packet: %s"),
416 progname, PQerrorMessage(conn));
424 * Check that the server version we're connected to is supported by
425 * ReceiveXlogStream().
427 * If it's not, an error message is printed to stderr, and false is returned.
430 CheckServerVersionForStreaming(PGconn *conn)
437 * The message format used in streaming replication changed in 9.3, so we
438 * cannot stream from older servers. And we don't support servers newer
439 * than the client; it might work, but we don't know, so err on the safe
442 minServerMajor = 903;
443 maxServerMajor = PG_VERSION_NUM / 100;
444 serverMajor = PQserverVersion(conn) / 100;
445 if (serverMajor < minServerMajor)
447 const char *serverver = PQparameterStatus(conn, "server_version");
449 fprintf(stderr, _("%s: incompatible server version %s; client does not support streaming from server versions older than %s\n"),
451 serverver ? serverver : "'unknown'",
455 else if (serverMajor > maxServerMajor)
457 const char *serverver = PQparameterStatus(conn, "server_version");
459 fprintf(stderr, _("%s: incompatible server version %s; client does not support streaming from server versions newer than %s\n"),
461 serverver ? serverver : "'unknown'",
469 * Receive a log stream starting at the specified position.
471 * Individual parameters are passed through the StreamCtl structure.
473 * If sysidentifier is specified, validate that both the system
474 * identifier and the timeline matches the specified ones
475 * (by sending an extra IDENTIFY_SYSTEM command)
477 * All received segments will be written to the directory
478 * specified by basedir. This will also fetch any missing timeline history
481 * The stream_stop callback will be called every time data
482 * is received, and whenever a segment is completed. If it returns
483 * true, the streaming will stop and the function
484 * return. As long as it returns false, streaming will continue
487 * standby_message_timeout controls how often we send a message
488 * back to the master letting it know our progress, in milliseconds.
489 * This message will only contain the write location, and never
492 * If 'partial_suffix' is not NULL, files are initially created with the
493 * given suffix, and the suffix is removed once the file is finished. That
494 * allows you to tell the difference between partial and completed files,
495 * so that you can continue later where you left.
497 * If 'synchronous' is true, the received WAL is flushed as soon as written,
498 * otherwise only when the WAL file is closed.
500 * Note: The log position *must* be at a log segment start!
503 ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
511 * The caller should've checked the server version already, but doesn't do
512 * any harm to check it here too.
514 if (!CheckServerVersionForStreaming(conn))
518 * Decide whether we want to report the flush position. If we report
519 * the flush position, the primary will know what WAL we'll
520 * possibly re-request, and it can then remove older WAL safely.
521 * We must always do that when we are using slots.
523 * Reporting the flush position makes one eligible as a synchronous
524 * replica. People shouldn't include generic names in
525 * synchronous_standby_names, but we've protected them against it so
526 * far, so let's continue to do so unless specifically requested.
528 if (replication_slot != NULL)
530 reportFlushPosition = true;
531 sprintf(slotcmd, "SLOT \"%s\" ", replication_slot);
535 if (stream->synchronous)
536 reportFlushPosition = true;
538 reportFlushPosition = false;
542 if (stream->sysidentifier != NULL)
544 /* Validate system identifier hasn't changed */
545 res = PQexec(conn, "IDENTIFY_SYSTEM");
546 if (PQresultStatus(res) != PGRES_TUPLES_OK)
549 _("%s: could not send replication command \"%s\": %s"),
550 progname, "IDENTIFY_SYSTEM", PQerrorMessage(conn));
554 if (PQntuples(res) != 1 || PQnfields(res) < 3)
557 _("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
558 progname, PQntuples(res), PQnfields(res), 1, 3);
562 if (strcmp(stream->sysidentifier, PQgetvalue(res, 0, 0)) != 0)
565 _("%s: system identifier does not match between base backup and streaming connection\n"),
570 if (stream->timeline > atoi(PQgetvalue(res, 0, 1)))
573 _("%s: starting timeline %u is not present in the server\n"),
574 progname, stream->timeline);
582 * initialize flush position to starting point, it's the caller's
583 * responsibility that that's sane.
585 lastFlushPosition = stream->startpos;
590 * Fetch the timeline history file for this timeline, if we don't have
593 if (!existsTimeLineHistoryFile(stream))
595 snprintf(query, sizeof(query), "TIMELINE_HISTORY %u", stream->timeline);
596 res = PQexec(conn, query);
597 if (PQresultStatus(res) != PGRES_TUPLES_OK)
599 /* FIXME: we might send it ok, but get an error */
600 fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
601 progname, "TIMELINE_HISTORY", PQresultErrorMessage(res));
607 * The response to TIMELINE_HISTORY is a single row result set
608 * with two fields: filename and content
610 if (PQnfields(res) != 2 || PQntuples(res) != 1)
613 _("%s: unexpected response to TIMELINE_HISTORY command: got %d rows and %d fields, expected %d rows and %d fields\n"),
614 progname, PQntuples(res), PQnfields(res), 1, 2);
617 /* Write the history file to disk */
618 writeTimeLineHistoryFile(stream,
619 PQgetvalue(res, 0, 0),
620 PQgetvalue(res, 0, 1));
626 * Before we start streaming from the requested location, check if the
627 * callback tells us to stop here.
629 if (stream->stream_stop(stream->startpos, stream->timeline, false))
632 /* Initiate the replication stream at specified location */
633 snprintf(query, sizeof(query), "START_REPLICATION %s%X/%X TIMELINE %u",
635 (uint32) (stream->startpos >> 32), (uint32) stream->startpos,
637 res = PQexec(conn, query);
638 if (PQresultStatus(res) != PGRES_COPY_BOTH)
640 fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
641 progname, "START_REPLICATION", PQresultErrorMessage(res));
648 res = HandleCopyStream(conn, stream, &stoppos);
653 * Streaming finished.
655 * There are two possible reasons for that: a controlled shutdown, or
656 * we reached the end of the current timeline. In case of
657 * end-of-timeline, the server sends a result set after Copy has
658 * finished, containing information about the next timeline. Read
659 * that, and restart streaming from the next timeline. In case of
660 * controlled shutdown, stop here.
662 if (PQresultStatus(res) == PGRES_TUPLES_OK)
665 * End-of-timeline. Read the next timeline's ID and starting
666 * position. Usually, the starting position will match the end of
667 * the previous timeline, but there are corner cases like if the
668 * server had sent us half of a WAL record, when it was promoted.
669 * The new timeline will begin at the end of the last complete
670 * record in that case, overlapping the partial WAL record on the
676 parsed = ReadEndOfStreamingResult(res, &stream->startpos, &newtimeline);
681 /* Sanity check the values the server gave us */
682 if (newtimeline <= stream->timeline)
685 _("%s: server reported unexpected next timeline %u, following timeline %u\n"),
686 progname, newtimeline, stream->timeline);
689 if (stream->startpos > stoppos)
692 _("%s: server stopped streaming timeline %u at %X/%X, but reported next timeline %u to begin at %X/%X\n"),
694 stream->timeline, (uint32) (stoppos >> 32), (uint32) stoppos,
695 newtimeline, (uint32) (stream->startpos >> 32), (uint32) stream->startpos);
699 /* Read the final result, which should be CommandComplete. */
700 res = PQgetResult(conn);
701 if (PQresultStatus(res) != PGRES_COMMAND_OK)
704 _("%s: unexpected termination of replication stream: %s"),
705 progname, PQresultErrorMessage(res));
712 * Loop back to start streaming from the new timeline. Always
713 * start streaming at the beginning of a segment.
715 stream->timeline = newtimeline;
716 stream->startpos = stream->startpos - (stream->startpos % XLOG_SEG_SIZE);
719 else if (PQresultStatus(res) == PGRES_COMMAND_OK)
724 * End of replication (ie. controlled shut down of the server).
726 * Check if the callback thinks it's OK to stop here. If not,
729 if (stream->stream_stop(stoppos, stream->timeline, false))
733 fprintf(stderr, _("%s: replication stream was terminated before stop point\n"),
740 /* Server returned an error. */
742 _("%s: unexpected termination of replication stream: %s"),
743 progname, PQresultErrorMessage(res));
750 if (walfile != -1 && close(walfile) != 0)
751 fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
752 progname, current_walfile_name, strerror(errno));
758 * Helper function to parse the result set returned by server after streaming
759 * has finished. On failure, prints an error to stderr and returns false.
762 ReadEndOfStreamingResult(PGresult *res, XLogRecPtr *startpos, uint32 *timeline)
764 uint32 startpos_xlogid,
768 * The result set consists of one row and two columns, e.g:
770 * next_tli | next_tli_startpos
771 * ----------+-------------------
774 * next_tli is the timeline ID of the next timeline after the one that
775 * just finished streaming. next_tli_startpos is the XLOG position where
776 * the server switched to it.
779 if (PQnfields(res) < 2 || PQntuples(res) != 1)
782 _("%s: unexpected result set after end-of-timeline: got %d rows and %d fields, expected %d rows and %d fields\n"),
783 progname, PQntuples(res), PQnfields(res), 1, 2);
787 *timeline = atoi(PQgetvalue(res, 0, 0));
788 if (sscanf(PQgetvalue(res, 0, 1), "%X/%X", &startpos_xlogid,
789 &startpos_xrecoff) != 2)
792 _("%s: could not parse next timeline's starting point \"%s\"\n"),
793 progname, PQgetvalue(res, 0, 1));
796 *startpos = ((uint64) startpos_xlogid << 32) | startpos_xrecoff;
802 * The main loop of ReceiveXlogStream. Handles the COPY stream after
803 * initiating streaming with the START_STREAMING command.
805 * If the COPY ends (not necessarily successfully) due a message from the
806 * server, returns a PGresult and sets *stoppos to the last byte written.
807 * On any other sort of error, returns NULL.
810 HandleCopyStream(PGconn *conn, StreamCtl *stream,
813 char *copybuf = NULL;
814 int64 last_status = -1;
815 XLogRecPtr blockpos = stream->startpos;
817 still_sending = true;
826 * Check if we should continue streaming, or abort at this point.
828 if (!CheckCopyStreamStop(conn, stream, blockpos, stoppos))
831 now = feGetCurrentTimestamp();
834 * If synchronous option is true, issue sync command as soon as there
835 * are WAL data which has not been flushed yet.
837 if (stream->synchronous && lastFlushPosition < blockpos && walfile != -1)
839 if (fsync(walfile) != 0)
841 fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"),
842 progname, current_walfile_name, strerror(errno));
845 lastFlushPosition = blockpos;
848 * Send feedback so that the server sees the latest WAL locations
851 if (!sendFeedback(conn, blockpos, now, false))
857 * Potentially send a status message to the master
859 if (still_sending && stream->standby_message_timeout > 0 &&
860 feTimestampDifferenceExceeds(last_status, now,
861 stream->standby_message_timeout))
863 /* Time to send feedback! */
864 if (!sendFeedback(conn, blockpos, now, false))
870 * Calculate how long send/receive loops should sleep
872 sleeptime = CalculateCopyStreamSleeptime(now, stream->standby_message_timeout,
875 r = CopyStreamReceive(conn, sleeptime, ©buf);
882 PGresult *res = HandleEndOfCopyStream(conn, stream, copybuf, blockpos, stoppos);
890 /* Check the message type. */
891 if (copybuf[0] == 'k')
893 if (!ProcessKeepaliveMsg(conn, copybuf, r, blockpos,
897 else if (copybuf[0] == 'w')
899 if (!ProcessXLogDataMsg(conn, stream, copybuf, r, &blockpos))
903 * Check if we should continue streaming, or abort at this
906 if (!CheckCopyStreamStop(conn, stream, blockpos, stoppos))
911 fprintf(stderr, _("%s: unrecognized streaming header: \"%c\"\n"),
912 progname, copybuf[0]);
917 * Process the received data, and any subsequent data we can read
920 r = CopyStreamReceive(conn, 0, ©buf);
931 * Wait until we can read CopyData message, or timeout.
933 * Returns 1 if data has become available for reading, 0 if timed out
934 * or interrupted by signal, and -1 on an error.
937 CopyStreamPoll(PGconn *conn, long timeout_ms)
941 struct timeval timeout;
942 struct timeval *timeoutptr;
944 if (PQsocket(conn) < 0)
946 fprintf(stderr, _("%s: invalid socket: %s"), progname,
947 PQerrorMessage(conn));
951 FD_ZERO(&input_mask);
952 FD_SET(PQsocket(conn), &input_mask);
958 timeout.tv_sec = timeout_ms / 1000L;
959 timeout.tv_usec = (timeout_ms % 1000L) * 1000L;
960 timeoutptr = &timeout;
963 ret = select(PQsocket(conn) + 1, &input_mask, NULL, NULL, timeoutptr);
964 if (ret == 0 || (ret < 0 && errno == EINTR))
965 return 0; /* Got a timeout or signal */
968 fprintf(stderr, _("%s: select() failed: %s\n"),
969 progname, strerror(errno));
977 * Receive CopyData message available from XLOG stream, blocking for
978 * maximum of 'timeout' ms.
980 * If data was received, returns the length of the data. *buffer is set to
981 * point to a buffer holding the received message. The buffer is only valid
982 * until the next CopyStreamReceive call.
984 * 0 if no data was available within timeout, or wait was interrupted
985 * by signal. -1 on error. -2 if the server ended the COPY.
988 CopyStreamReceive(PGconn *conn, long timeout, char **buffer)
990 char *copybuf = NULL;
997 /* Try to receive a CopyData message */
998 rawlen = PQgetCopyData(conn, ©buf, 1);
1002 * No data available. Wait for some to appear, but not longer than the
1003 * specified timeout, so that we can ping the server.
1009 ret = CopyStreamPoll(conn, timeout);
1014 /* Else there is actually data on the socket */
1015 if (PQconsumeInput(conn) == 0)
1018 _("%s: could not receive data from WAL stream: %s"),
1019 progname, PQerrorMessage(conn));
1023 /* Now that we've consumed some input, try again */
1024 rawlen = PQgetCopyData(conn, ©buf, 1);
1028 if (rawlen == -1) /* end-of-streaming or error */
1032 fprintf(stderr, _("%s: could not read COPY data: %s"),
1033 progname, PQerrorMessage(conn));
1037 /* Return received messages to caller */
1043 * Process the keepalive message.
1046 ProcessKeepaliveMsg(PGconn *conn, char *copybuf, int len,
1047 XLogRecPtr blockpos, int64 *last_status)
1050 bool replyRequested;
1054 * Parse the keepalive message, enclosed in the CopyData message. We just
1055 * check if the server requested a reply, and ignore the rest.
1057 pos = 1; /* skip msgtype 'k' */
1058 pos += 8; /* skip walEnd */
1059 pos += 8; /* skip sendTime */
1063 fprintf(stderr, _("%s: streaming header too small: %d\n"),
1067 replyRequested = copybuf[pos];
1069 /* If the server requested an immediate reply, send one. */
1070 if (replyRequested && still_sending)
1072 if (reportFlushPosition && lastFlushPosition < blockpos &&
1076 * If a valid flush location needs to be reported, flush the
1077 * current WAL file so that the latest flush location is sent back
1078 * to the server. This is necessary to see whether the last WAL
1079 * data has been successfully replicated or not, at the normal
1080 * shutdown of the server.
1082 if (fsync(walfile) != 0)
1084 fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"),
1085 progname, current_walfile_name, strerror(errno));
1088 lastFlushPosition = blockpos;
1091 now = feGetCurrentTimestamp();
1092 if (!sendFeedback(conn, blockpos, now, false))
1101 * Process XLogData message.
1104 ProcessXLogDataMsg(PGconn *conn, StreamCtl *stream, char *copybuf, int len,
1105 XLogRecPtr *blockpos)
1113 * Once we've decided we don't want to receive any more, just ignore any
1114 * subsequent XLogData messages.
1116 if (!(still_sending))
1120 * Read the header of the XLogData message, enclosed in the CopyData
1121 * message. We only need the WAL location field (dataStart), the rest of
1122 * the header is ignored.
1124 hdr_len = 1; /* msgtype 'w' */
1125 hdr_len += 8; /* dataStart */
1126 hdr_len += 8; /* walEnd */
1127 hdr_len += 8; /* sendTime */
1130 fprintf(stderr, _("%s: streaming header too small: %d\n"),
1134 *blockpos = fe_recvint64(©buf[1]);
1136 /* Extract WAL location for this block */
1137 xlogoff = *blockpos % XLOG_SEG_SIZE;
1140 * Verify that the initial location in the stream matches where we think
1145 /* No file open yet */
1149 _("%s: received transaction log record for offset %u with no file open\n"),
1156 /* More data in existing segment */
1157 /* XXX: store seek value don't reseek all the time */
1158 if (lseek(walfile, 0, SEEK_CUR) != xlogoff)
1161 _("%s: got WAL data offset %08x, expected %08x\n"),
1162 progname, xlogoff, (int) lseek(walfile, 0, SEEK_CUR));
1167 bytes_left = len - hdr_len;
1175 * If crossing a WAL boundary, only write up until we reach
1178 if (xlogoff + bytes_left > XLOG_SEG_SIZE)
1179 bytes_to_write = XLOG_SEG_SIZE - xlogoff;
1181 bytes_to_write = bytes_left;
1185 if (!open_walfile(stream, *blockpos))
1187 /* Error logged by open_walfile */
1193 copybuf + hdr_len + bytes_written,
1194 bytes_to_write) != bytes_to_write)
1197 _("%s: could not write %u bytes to WAL file \"%s\": %s\n"),
1198 progname, bytes_to_write, current_walfile_name,
1203 /* Write was successful, advance our position */
1204 bytes_written += bytes_to_write;
1205 bytes_left -= bytes_to_write;
1206 *blockpos += bytes_to_write;
1207 xlogoff += bytes_to_write;
1209 /* Did we reach the end of a WAL segment? */
1210 if (*blockpos % XLOG_SEG_SIZE == 0)
1212 if (!close_walfile(stream, *blockpos))
1213 /* Error message written in close_walfile() */
1218 if (still_sending && stream->stream_stop(*blockpos, stream->timeline, true))
1220 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1222 fprintf(stderr, _("%s: could not send copy-end packet: %s"),
1223 progname, PQerrorMessage(conn));
1226 still_sending = false;
1227 return true; /* ignore the rest of this XLogData packet */
1231 /* No more data left to write, receive next copy packet */
1237 * Handle end of the copy stream.
1240 HandleEndOfCopyStream(PGconn *conn, StreamCtl *stream, char *copybuf,
1241 XLogRecPtr blockpos, XLogRecPtr *stoppos)
1243 PGresult *res = PQgetResult(conn);
1246 * The server closed its end of the copy stream. If we haven't closed
1247 * ours already, we need to do so now, unless the server threw an error,
1248 * in which case we don't.
1252 if (!close_walfile(stream, blockpos))
1254 /* Error message written in close_walfile() */
1258 if (PQresultStatus(res) == PGRES_COPY_IN)
1260 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1263 _("%s: could not send copy-end packet: %s"),
1264 progname, PQerrorMessage(conn));
1268 res = PQgetResult(conn);
1270 still_sending = false;
1272 if (copybuf != NULL)
1274 *stoppos = blockpos;
1279 * Check if we should continue streaming, or abort at this point.
1282 CheckCopyStreamStop(PGconn *conn, StreamCtl *stream, XLogRecPtr blockpos,
1283 XLogRecPtr *stoppos)
1285 if (still_sending && stream->stream_stop(blockpos, stream->timeline, false))
1287 if (!close_walfile(stream, blockpos))
1289 /* Potential error message is written by close_walfile */
1292 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1294 fprintf(stderr, _("%s: could not send copy-end packet: %s"),
1295 progname, PQerrorMessage(conn));
1298 still_sending = false;
1305 * Calculate how long send/receive loops should sleep
1308 CalculateCopyStreamSleeptime(int64 now, int standby_message_timeout,
1311 int64 status_targettime = 0;
1314 if (standby_message_timeout && still_sending)
1315 status_targettime = last_status +
1316 (standby_message_timeout - 1) * ((int64) 1000);
1318 if (status_targettime > 0)
1323 feTimestampDifference(now,
1327 /* Always sleep at least 1 sec */
1334 sleeptime = secs * 1000 + usecs / 1000;