PostgreSQL Source Code git master
Loading...
Searching...
No Matches
receivelog.c File Reference
#include "postgres_fe.h"
#include <sys/select.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/xlog_internal.h"
#include "common/logging.h"
#include "libpq-fe.h"
#include "libpq/protocol.h"
#include "receivelog.h"
#include "streamutil.h"
Include dependency graph for receivelog.c:

Go to the source code of this file.

Functions

static PGresultHandleCopyStream (PGconn *conn, StreamCtl *stream, XLogRecPtr *stoppos)
 
static int CopyStreamPoll (PGconn *conn, long timeout_ms, pgsocket stop_socket)
 
static int CopyStreamReceive (PGconn *conn, long timeout, pgsocket stop_socket, char **buffer)
 
static bool ProcessKeepaliveMsg (PGconn *conn, StreamCtl *stream, char *copybuf, int len, XLogRecPtr blockpos, TimestampTz *last_status)
 
static bool ProcessWALDataMsg (PGconn *conn, StreamCtl *stream, char *copybuf, int len, XLogRecPtr *blockpos)
 
static PGresultHandleEndOfCopyStream (PGconn *conn, StreamCtl *stream, char *copybuf, XLogRecPtr blockpos, XLogRecPtr *stoppos)
 
static bool CheckCopyStreamStop (PGconn *conn, StreamCtl *stream, XLogRecPtr blockpos)
 
static long CalculateCopyStreamSleeptime (TimestampTz now, int standby_message_timeout, TimestampTz last_status)
 
static bool ReadEndOfStreamingResult (PGresult *res, XLogRecPtr *startpos, uint32 *timeline)
 
static bool mark_file_as_archived (StreamCtl *stream, const char *fname)
 
static bool open_walfile (StreamCtl *stream, XLogRecPtr startpoint)
 
static bool close_walfile (StreamCtl *stream, XLogRecPtr pos)
 
static bool existsTimeLineHistoryFile (StreamCtl *stream)
 
static bool writeTimeLineHistoryFile (StreamCtl *stream, char *filename, char *content)
 
static bool sendFeedback (PGconn *conn, XLogRecPtr blockpos, TimestampTz now, bool replyRequested)
 
bool CheckServerVersionForStreaming (PGconn *conn)
 
bool ReceiveXlogStream (PGconn *conn, StreamCtl *stream)
 

Variables

static Walfilewalfile = NULL
 
static bool reportFlushPosition = false
 
static XLogRecPtr lastFlushPosition = InvalidXLogRecPtr
 
static bool still_sending = true
 

Function Documentation

◆ CalculateCopyStreamSleeptime()

static long CalculateCopyStreamSleeptime ( TimestampTz  now,
int  standby_message_timeout,
TimestampTz  last_status 
)
static

Definition at line 1249 of file receivelog.c.

1251{
1253 long sleeptime;
1254
1257 (standby_message_timeout - 1) * ((int64) 1000);
1258
1259 if (status_targettime > 0)
1260 {
1261 long secs;
1262 int usecs;
1263
1266 &secs,
1267 &usecs);
1268 /* Always sleep at least 1 sec */
1269 if (secs <= 0)
1270 {
1271 secs = 1;
1272 usecs = 0;
1273 }
1274
1275 sleeptime = secs * 1000 + usecs / 1000;
1276 }
1277 else
1278 sleeptime = -1;
1279
1280 return sleeptime;
1281}
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1613
int64_t int64
Definition c.h:677
int64 TimestampTz
Definition timestamp.h:39
static int standby_message_timeout
static int fb(int x)
static bool still_sending
Definition receivelog.c:33
void feTimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition streamutil.c:849

References fb(), feTimestampDifference(), now(), standby_message_timeout, and still_sending.

Referenced by HandleCopyStream().

◆ CheckCopyStreamStop()

static bool CheckCopyStreamStop ( PGconn conn,
StreamCtl stream,
XLogRecPtr  blockpos 
)
static

Definition at line 1224 of file receivelog.c.

1225{
1226 if (still_sending && stream->stream_stop(blockpos, stream->timeline, false))
1227 {
1228 if (!close_walfile(stream, blockpos))
1229 {
1230 /* Potential error message is written by close_walfile */
1231 return false;
1232 }
1233 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1234 {
1235 pg_log_error("could not send copy-end packet: %s",
1237 return false;
1238 }
1239 still_sending = false;
1240 }
1241
1242 return true;
1243}
char * PQerrorMessage(const PGconn *conn)
int PQflush(PGconn *conn)
Definition fe-exec.c:4036
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition fe-exec.c:2766
#define pg_log_error(...)
Definition logging.h:108
static bool close_walfile(StreamCtl *stream, XLogRecPtr pos)
Definition receivelog.c:192
PGconn * conn
Definition streamutil.c:52
TimeLineID timeline
Definition receivelog.h:32
stream_stop_callback stream_stop
Definition receivelog.h:41

References close_walfile(), conn, fb(), pg_log_error, PQerrorMessage(), PQflush(), PQputCopyEnd(), still_sending, StreamCtl::stream_stop, and StreamCtl::timeline.

Referenced by HandleCopyStream().

◆ CheckServerVersionForStreaming()

bool CheckServerVersionForStreaming ( PGconn conn)

Definition at line 375 of file receivelog.c.

376{
377 int minServerMajor,
379 int serverMajor;
380
381 /*
382 * The message format used in streaming replication changed in 9.3, so we
383 * cannot stream from older servers. And we don't support servers newer
384 * than the client; it might work, but we don't know, so err on the safe
385 * side.
386 */
387 minServerMajor = 903;
391 {
392 const char *serverver = PQparameterStatus(conn, "server_version");
393
394 pg_log_error("incompatible server version %s; client does not support streaming from server versions older than %s",
395 serverver ? serverver : "'unknown'",
396 "9.3");
397 return false;
398 }
399 else if (serverMajor > maxServerMajor)
400 {
401 const char *serverver = PQparameterStatus(conn, "server_version");
402
403 pg_log_error("incompatible server version %s; client does not support streaming from server versions newer than %s",
404 serverver ? serverver : "'unknown'",
405 PG_VERSION);
406 return false;
407 }
408 return true;
409}
int PQserverVersion(const PGconn *conn)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)

References conn, fb(), pg_log_error, PQparameterStatus(), and PQserverVersion().

Referenced by BaseBackup(), ReceiveXlogStream(), and StreamLog().

◆ close_walfile()

static bool close_walfile ( StreamCtl stream,
XLogRecPtr  pos 
)
static

Definition at line 192 of file receivelog.c.

193{
194 char *fn;
195 pgoff_t currpos;
196 int r;
198
199 if (walfile == NULL)
200 return true;
201
203 currpos = walfile->currpos;
204
205 /* Note that this considers the compression used if necessary */
206 fn = stream->walmethod->ops->get_file_name(stream->walmethod,
208 stream->partial_suffix);
209
210 if (stream->partial_suffix)
211 {
212 if (currpos == WalSegSz)
213 r = stream->walmethod->ops->close(walfile, CLOSE_NORMAL);
214 else
215 {
216 pg_log_info("not renaming \"%s\", segment is not complete", fn);
217 r = stream->walmethod->ops->close(walfile, CLOSE_NO_RENAME);
218 }
219 }
220 else
221 r = stream->walmethod->ops->close(walfile, CLOSE_NORMAL);
222
223 walfile = NULL;
224
225 if (r != 0)
226 {
227 pg_log_error("could not close file \"%s\": %s",
229
230 pg_free(fn);
231 return false;
232 }
233
234 pg_free(fn);
235
236 /*
237 * Mark file as archived if requested by the caller - pg_basebackup needs
238 * to do so as files can otherwise get archived again after promotion of a
239 * new node. This is in line with walreceiver.c always doing a
240 * XLogArchiveForceDone() after a complete segment.
241 */
242 if (currpos == WalSegSz && stream->mark_done)
243 {
244 /* writes error message if failed */
246 return false;
247 }
248
249 lastFlushPosition = pos;
250 return true;
251}
void pg_free(void *ptr)
#define pg_log_info(...)
Definition logging.h:126
#define MAXPGPATH
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
off_t pgoff_t
Definition port.h:422
static bool mark_file_as_archived(StreamCtl *stream, const char *fname)
Definition receivelog.c:54
static Walfile * walfile
Definition receivelog.c:29
static XLogRecPtr lastFlushPosition
Definition receivelog.c:31
int WalSegSz
Definition streamutil.c:32
WalWriteMethod * walmethod
Definition receivelog.h:46
bool mark_done
Definition receivelog.h:37
char * partial_suffix
Definition receivelog.h:47
int(* close)(Walfile *f, WalCloseMethod method)
Definition walmethods.h:55
char *(* get_file_name)(WalWriteMethod *wwmethod, const char *pathname, const char *temp_suffix)
Definition walmethods.h:67
const WalWriteMethodOps * ops
Definition walmethods.h:105
pgoff_t currpos
Definition walmethods.h:20
char * pathname
Definition walmethods.h:21
static void * fn(void *arg)
const char * GetLastWalMethodError(WalWriteMethod *wwmethod)
@ CLOSE_NO_RENAME
Definition walmethods.h:35
@ CLOSE_NORMAL
Definition walmethods.h:33

References WalWriteMethodOps::close, CLOSE_NO_RENAME, CLOSE_NORMAL, Walfile::currpos, fb(), fn(), WalWriteMethodOps::get_file_name, GetLastWalMethodError(), lastFlushPosition, StreamCtl::mark_done, mark_file_as_archived(), MAXPGPATH, WalWriteMethod::ops, StreamCtl::partial_suffix, Walfile::pathname, pg_free(), pg_log_error, pg_log_info, strlcpy(), walfile, StreamCtl::walmethod, and WalSegSz.

Referenced by CheckCopyStreamStop(), HandleEndOfCopyStream(), and ProcessWALDataMsg().

◆ CopyStreamPoll()

static int CopyStreamPoll ( PGconn conn,
long  timeout_ms,
pgsocket  stop_socket 
)
static

Definition at line 884 of file receivelog.c.

885{
886 int ret;
888 int connsocket;
889 int maxfd;
890 struct timeval timeout;
891 struct timeval *timeoutptr;
892
894 if (connsocket < 0)
895 {
896 pg_log_error("invalid socket: %s", PQerrorMessage(conn));
897 return -1;
898 }
899
902 maxfd = connsocket;
903 if (stop_socket != PGINVALID_SOCKET)
904 {
905 FD_SET(stop_socket, &input_mask);
906 maxfd = Max(maxfd, stop_socket);
907 }
908
909 if (timeout_ms < 0)
911 else
912 {
913 timeout.tv_sec = timeout_ms / 1000L;
914 timeout.tv_usec = (timeout_ms % 1000L) * 1000L;
916 }
917
918 ret = select(maxfd + 1, &input_mask, NULL, NULL, timeoutptr);
919
920 if (ret < 0)
921 {
922 if (errno == EINTR)
923 return 0; /* Got a signal, so not an error */
924 pg_log_error("%s() failed: %m", "select");
925 return -1;
926 }
927 if (ret > 0 && FD_ISSET(connsocket, &input_mask))
928 return 1; /* Got input on connection socket */
929
930 return 0; /* Got timeout or input on stop_socket */
931}
#define Max(x, y)
Definition c.h:1141
int PQsocket(const PGconn *conn)
#define PGINVALID_SOCKET
Definition port.h:31
#define EINTR
Definition win32_port.h:361
#define select(n, r, w, e, timeout)
Definition win32_port.h:500

References conn, EINTR, fb(), Max, pg_log_error, PGINVALID_SOCKET, PQerrorMessage(), PQsocket(), and select.

Referenced by CopyStreamReceive().

◆ CopyStreamReceive()

static int CopyStreamReceive ( PGconn conn,
long  timeout,
pgsocket  stop_socket,
char **  buffer 
)
static

Definition at line 946 of file receivelog.c.

948{
949 char *copybuf = NULL;
950 int rawlen;
951
952 /* Caller should have cleared any prior buffer */
953 Assert(*buffer == NULL);
954
955 /* Try to receive a CopyData message */
957 if (rawlen == 0)
958 {
959 int ret;
960
961 /*
962 * No data available. Wait for some to appear, but not longer than
963 * the specified timeout, so that we can ping the server. Also stop
964 * waiting if input appears on stop_socket.
965 */
966 ret = CopyStreamPoll(conn, timeout, stop_socket);
967 if (ret <= 0)
968 return ret;
969
970 /* Now there is actually data on the socket */
971 if (PQconsumeInput(conn) == 0)
972 {
973 pg_log_error("could not receive data from WAL stream: %s",
975 return -1;
976 }
977
978 /* Now that we've consumed some input, try again */
980 if (rawlen == 0)
981 return 0;
982 }
983 if (rawlen == -1) /* end-of-streaming or error */
984 return -2;
985 if (rawlen == -2)
986 {
987 pg_log_error("could not read COPY data: %s", PQerrorMessage(conn));
988 return -1;
989 }
990
991 /* Return received messages to caller */
992 *buffer = copybuf;
993 return rawlen;
994}
#define Assert(condition)
Definition c.h:999
int PQconsumeInput(PGconn *conn)
Definition fe-exec.c:2001
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition fe-exec.c:2833
static int CopyStreamPoll(PGconn *conn, long timeout_ms, pgsocket stop_socket)
Definition receivelog.c:884
static StringInfo copybuf
Definition tablesync.c:129

References Assert, conn, copybuf, CopyStreamPoll(), fb(), pg_log_error, PQconsumeInput(), PQerrorMessage(), and PQgetCopyData().

Referenced by HandleCopyStream().

◆ existsTimeLineHistoryFile()

static bool existsTimeLineHistoryFile ( StreamCtl stream)
static

Definition at line 258 of file receivelog.c.

259{
261
262 /*
263 * Timeline 1 never has a history file. We treat that as if it existed,
264 * since we never need to stream it.
265 */
266 if (stream->timeline == 1)
267 return true;
268
270
271 return stream->walmethod->ops->existsfile(stream->walmethod, histfname);
272}
bool(* existsfile)(WalWriteMethod *wwmethod, const char *pathname)
Definition walmethods.h:58
#define MAXFNAMELEN
static void TLHistoryFileName(char *fname, TimeLineID tli)

References WalWriteMethodOps::existsfile, fb(), MAXFNAMELEN, WalWriteMethod::ops, StreamCtl::timeline, TLHistoryFileName(), and StreamCtl::walmethod.

Referenced by ReceiveXlogStream().

◆ HandleCopyStream()

static PGresult * HandleCopyStream ( PGconn conn,
StreamCtl stream,
XLogRecPtr stoppos 
)
static

Definition at line 751 of file receivelog.c.

753{
754 char *copybuf = NULL;
756 XLogRecPtr blockpos = stream->startpos;
757
758 still_sending = true;
759
760 while (1)
761 {
762 int r;
764 long sleeptime;
765
766 /*
767 * Check if we should continue streaming, or abort at this point.
768 */
769 if (!CheckCopyStreamStop(conn, stream, blockpos))
770 goto error;
771
773
774 /*
775 * If synchronous option is true, issue sync command as soon as there
776 * are WAL data which has not been flushed yet.
777 */
778 if (stream->synchronous && lastFlushPosition < blockpos && walfile != NULL)
779 {
780 if (stream->walmethod->ops->sync(walfile) != 0)
781 pg_fatal("could not fsync file \"%s\": %s",
784
785 /*
786 * Send feedback so that the server sees the latest WAL locations
787 * immediately.
788 */
789 if (!sendFeedback(conn, blockpos, now, false))
790 goto error;
792 }
793
794 /*
795 * Potentially send a status message to the primary
796 */
797 if (still_sending && stream->standby_message_timeout > 0 &&
800 {
801 /* Time to send feedback! */
802 if (!sendFeedback(conn, blockpos, now, false))
803 goto error;
805 }
806
807 /*
808 * Calculate how long send/receive loops should sleep
809 */
812
813 /* Done with any prior message */
815 copybuf = NULL;
816
818 while (r != 0)
819 {
820 if (r == -1)
821 goto error;
822 if (r == -2)
823 {
825
826 if (res == NULL)
827 goto error;
829 return res;
830 }
831
832 /* Check the message type. */
834 {
835 if (!ProcessKeepaliveMsg(conn, stream, copybuf, r, blockpos,
836 &last_status))
837 goto error;
838 }
839 else if (copybuf[0] == PqReplMsg_WALData)
840 {
841 if (!ProcessWALDataMsg(conn, stream, copybuf, r, &blockpos))
842 goto error;
843
844 /*
845 * Check if we should continue streaming, or abort at this
846 * point.
847 */
848 if (!CheckCopyStreamStop(conn, stream, blockpos))
849 goto error;
850 }
851 else
852 {
853 pg_log_error("unrecognized streaming header: \"%c\"",
854 copybuf[0]);
855 goto error;
856 }
857
858 /* Done with that message */
860 copybuf = NULL;
861
862 /*
863 * Process the received data, and any subsequent data we can read
864 * without blocking.
865 */
866 r = CopyStreamReceive(conn, 0, stream->stop_socket, &copybuf);
867 }
868 }
869
870error:
872 return NULL;
873}
void PQfreemem(void *ptr)
Definition fe-exec.c:4068
#define pg_fatal(...)
#define PqReplMsg_WALData
Definition protocol.h:77
#define PqReplMsg_Keepalive
Definition protocol.h:75
static int CopyStreamReceive(PGconn *conn, long timeout, pgsocket stop_socket, char **buffer)
Definition receivelog.c:946
static bool CheckCopyStreamStop(PGconn *conn, StreamCtl *stream, XLogRecPtr blockpos)
static bool ProcessKeepaliveMsg(PGconn *conn, StreamCtl *stream, char *copybuf, int len, XLogRecPtr blockpos, TimestampTz *last_status)
static bool ProcessWALDataMsg(PGconn *conn, StreamCtl *stream, char *copybuf, int len, XLogRecPtr *blockpos)
static PGresult * HandleEndOfCopyStream(PGconn *conn, StreamCtl *stream, char *copybuf, XLogRecPtr blockpos, XLogRecPtr *stoppos)
static long CalculateCopyStreamSleeptime(TimestampTz now, int standby_message_timeout, TimestampTz last_status)
static bool sendFeedback(PGconn *conn, XLogRecPtr blockpos, TimestampTz now, bool replyRequested)
Definition receivelog.c:337
static void error(void)
TimestampTz feGetCurrentTimestamp(void)
Definition streamutil.c:830
bool feTimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition streamutil.c:871
XLogRecPtr startpos
Definition receivelog.h:31
pgsocket stop_socket
Definition receivelog.h:43
int standby_message_timeout
Definition receivelog.h:35
bool synchronous
Definition receivelog.h:36
int(* sync)(Walfile *f)
Definition walmethods.h:78
uint64 XLogRecPtr
Definition xlogdefs.h:21

References CalculateCopyStreamSleeptime(), CheckCopyStreamStop(), conn, copybuf, CopyStreamReceive(), error(), fb(), feGetCurrentTimestamp(), feTimestampDifferenceExceeds(), GetLastWalMethodError(), HandleEndOfCopyStream(), lastFlushPosition, now(), WalWriteMethod::ops, Walfile::pathname, pg_fatal, pg_log_error, PQfreemem(), PqReplMsg_Keepalive, PqReplMsg_WALData, ProcessKeepaliveMsg(), ProcessWALDataMsg(), sendFeedback(), StreamCtl::standby_message_timeout, StreamCtl::startpos, still_sending, StreamCtl::stop_socket, WalWriteMethodOps::sync, StreamCtl::synchronous, walfile, and StreamCtl::walmethod.

Referenced by ReceiveXlogStream().

◆ HandleEndOfCopyStream()

static PGresult * HandleEndOfCopyStream ( PGconn conn,
StreamCtl stream,
char copybuf,
XLogRecPtr  blockpos,
XLogRecPtr stoppos 
)
static

Definition at line 1185 of file receivelog.c.

1187{
1188 PGresult *res = PQgetResult(conn);
1189
1190 /*
1191 * The server closed its end of the copy stream. If we haven't closed
1192 * ours already, we need to do so now, unless the server threw an error,
1193 * in which case we don't.
1194 */
1195 if (still_sending)
1196 {
1197 if (!close_walfile(stream, blockpos))
1198 {
1199 /* Error message written in close_walfile() */
1200 PQclear(res);
1201 return NULL;
1202 }
1203 if (PQresultStatus(res) == PGRES_COPY_IN)
1204 {
1205 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1206 {
1207 pg_log_error("could not send copy-end packet: %s",
1209 PQclear(res);
1210 return NULL;
1211 }
1212 res = PQgetResult(conn);
1213 }
1214 still_sending = false;
1215 }
1216 *stoppos = blockpos;
1217 return res;
1218}
#define PQgetResult
#define PQclear
#define PQresultStatus
@ PGRES_COPY_IN
Definition libpq-fe.h:138

References close_walfile(), conn, fb(), pg_log_error, PGRES_COPY_IN, PQclear, PQerrorMessage(), PQflush(), PQgetResult, PQputCopyEnd(), PQresultStatus, and still_sending.

Referenced by HandleCopyStream().

◆ mark_file_as_archived()

static bool mark_file_as_archived ( StreamCtl stream,
const char fname 
)
static

Definition at line 54 of file receivelog.c.

55{
56 Walfile *f;
57 static char tmppath[MAXPGPATH];
58
59 snprintf(tmppath, sizeof(tmppath), "archive_status/%s.done",
60 fname);
61
62 f = stream->walmethod->ops->open_for_write(stream->walmethod, tmppath,
63 NULL, 0);
64 if (f == NULL)
65 {
66 pg_log_error("could not create archive status file \"%s\": %s",
68 return false;
69 }
70
71 if (stream->walmethod->ops->close(f, CLOSE_NORMAL) != 0)
72 {
73 pg_log_error("could not close archive status file \"%s\": %s",
75 return false;
76 }
77
78 return true;
79}
#define snprintf
Definition port.h:261
Walfile *(* open_for_write)(WalWriteMethod *wwmethod, const char *pathname, const char *temp_suffix, size_t pad_to_size)
Definition walmethods.h:49

References WalWriteMethodOps::close, CLOSE_NORMAL, fb(), GetLastWalMethodError(), MAXPGPATH, WalWriteMethodOps::open_for_write, WalWriteMethod::ops, pg_log_error, snprintf, and StreamCtl::walmethod.

Referenced by close_walfile(), and writeTimeLineHistoryFile().

◆ open_walfile()

static bool open_walfile ( StreamCtl stream,
XLogRecPtr  startpoint 
)
static

Definition at line 90 of file receivelog.c.

91{
92 Walfile *f;
93 char *fn;
94 ssize_t size;
95 XLogSegNo segno;
97
98 XLByteToSeg(startpoint, segno, WalSegSz);
100
101 /* Note that this considers the compression used if necessary */
102 fn = stream->walmethod->ops->get_file_name(stream->walmethod,
104 stream->partial_suffix);
105
106 /*
107 * When streaming to files, if an existing file exists we verify that it's
108 * either empty (just created), or a complete WalSegSz segment (in which
109 * case it has been created and padded). Anything else indicates a corrupt
110 * file. Compressed files have no need for padding, so just ignore this
111 * case.
112 *
113 * When streaming to tar, no file with this name will exist before, so we
114 * never have to verify a size.
115 */
117 stream->walmethod->ops->existsfile(stream->walmethod, fn))
118 {
119 size = stream->walmethod->ops->get_file_size(stream->walmethod, fn);
120 if (size < 0)
121 {
122 pg_log_error("could not get size of write-ahead log file \"%s\": %s",
124 pg_free(fn);
125 return false;
126 }
127 if (size == WalSegSz)
128 {
129 /* Already padded file. Open it for use */
130 f = stream->walmethod->ops->open_for_write(stream->walmethod, walfile_name, stream->partial_suffix, 0);
131 if (f == NULL)
132 {
133 pg_log_error("could not open existing write-ahead log file \"%s\": %s",
135 pg_free(fn);
136 return false;
137 }
138
139 /* fsync file in case of a previous crash */
140 if (stream->walmethod->ops->sync(f) != 0)
141 {
142 pg_log_error("could not fsync existing write-ahead log file \"%s\": %s",
144 stream->walmethod->ops->close(f, CLOSE_UNLINK);
145 exit(1);
146 }
147
148 walfile = f;
149 pg_free(fn);
150 return true;
151 }
152 if (size != 0)
153 {
154 /* if write didn't set errno, assume problem is no disk space */
155 if (errno == 0)
156 errno = ENOSPC;
157 pg_log_error(ngettext("write-ahead log file \"%s\" has %zd byte, should be 0 or %d",
158 "write-ahead log file \"%s\" has %zd bytes, should be 0 or %d",
159 size),
160 fn, size, WalSegSz);
161 pg_free(fn);
162 return false;
163 }
164 /* File existed and was empty, so fall through and open */
165 }
166
167 /* No file existed, so create one */
168
169 f = stream->walmethod->ops->open_for_write(stream->walmethod,
171 stream->partial_suffix,
172 WalSegSz);
173 if (f == NULL)
174 {
175 pg_log_error("could not open write-ahead log file \"%s\": %s",
177 pg_free(fn);
178 return false;
179 }
180
181 pg_free(fn);
182 walfile = f;
183 return true;
184}
#define ngettext(s, p, n)
Definition c.h:1326
@ PG_COMPRESSION_NONE
Definition compression.h:23
ssize_t(* get_file_size)(WalWriteMethod *wwmethod, const char *pathname)
Definition walmethods.h:61
pg_compress_algorithm compression_algorithm
Definition walmethods.h:106
@ CLOSE_UNLINK
Definition walmethods.h:34
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
uint64 XLogSegNo
Definition xlogdefs.h:52

References WalWriteMethodOps::close, CLOSE_UNLINK, WalWriteMethod::compression_algorithm, WalWriteMethodOps::existsfile, fb(), fn(), WalWriteMethodOps::get_file_name, WalWriteMethodOps::get_file_size, GetLastWalMethodError(), MAXPGPATH, ngettext, WalWriteMethodOps::open_for_write, WalWriteMethod::ops, StreamCtl::partial_suffix, PG_COMPRESSION_NONE, pg_free(), pg_log_error, WalWriteMethodOps::sync, StreamCtl::timeline, walfile, StreamCtl::walmethod, WalSegSz, XLByteToSeg, and XLogFileName().

Referenced by ProcessWALDataMsg().

◆ ProcessKeepaliveMsg()

static bool ProcessKeepaliveMsg ( PGconn conn,
StreamCtl stream,
char copybuf,
int  len,
XLogRecPtr  blockpos,
TimestampTz last_status 
)
static

Definition at line 1000 of file receivelog.c.

1002{
1003 int pos;
1004 bool replyRequested;
1006
1007 /*
1008 * Parse the keepalive message, enclosed in the CopyData message. We just
1009 * check if the server requested a reply, and ignore the rest.
1010 */
1011 pos = 1; /* skip msgtype PqReplMsg_Keepalive */
1012 pos += 8; /* skip walEnd */
1013 pos += 8; /* skip sendTime */
1014
1015 if (len < pos + 1)
1016 {
1017 pg_log_error("streaming header too small: %d", len);
1018 return false;
1019 }
1020 replyRequested = copybuf[pos];
1021
1022 /* If the server requested an immediate reply, send one. */
1024 {
1026 walfile != NULL)
1027 {
1028 /*
1029 * If a valid flush location needs to be reported, flush the
1030 * current WAL file so that the latest flush location is sent back
1031 * to the server. This is necessary to see whether the last WAL
1032 * data has been successfully replicated or not, at the normal
1033 * shutdown of the server.
1034 */
1035 if (stream->walmethod->ops->sync(walfile) != 0)
1036 pg_fatal("could not fsync file \"%s\": %s",
1039 }
1040
1042 if (!sendFeedback(conn, blockpos, now, false))
1043 return false;
1044 *last_status = now;
1045 }
1046
1047 return true;
1048}
const void size_t len
static bool reportFlushPosition
Definition receivelog.c:30

References conn, copybuf, fb(), feGetCurrentTimestamp(), GetLastWalMethodError(), lastFlushPosition, len, now(), WalWriteMethod::ops, Walfile::pathname, pg_fatal, pg_log_error, reportFlushPosition, sendFeedback(), still_sending, WalWriteMethodOps::sync, walfile, and StreamCtl::walmethod.

Referenced by HandleCopyStream().

◆ ProcessWALDataMsg()

static bool ProcessWALDataMsg ( PGconn conn,
StreamCtl stream,
char copybuf,
int  len,
XLogRecPtr blockpos 
)
static

Definition at line 1054 of file receivelog.c.

1056{
1057 int xlogoff;
1058 int bytes_left;
1059 int bytes_written;
1060 int hdr_len;
1061
1062 /*
1063 * Once we've decided we don't want to receive any more, just ignore any
1064 * subsequent WALData messages.
1065 */
1066 if (!(still_sending))
1067 return true;
1068
1069 /*
1070 * Read the header of the WALData message, enclosed in the CopyData
1071 * message. We only need the WAL location field (dataStart), the rest of
1072 * the header is ignored.
1073 */
1074 hdr_len = 1; /* msgtype PqReplMsg_WALData */
1075 hdr_len += 8; /* dataStart */
1076 hdr_len += 8; /* walEnd */
1077 hdr_len += 8; /* sendTime */
1078 if (len < hdr_len)
1079 {
1080 pg_log_error("streaming header too small: %d", len);
1081 return false;
1082 }
1084
1085 /* Extract WAL location for this block */
1087
1088 /*
1089 * Verify that the initial location in the stream matches where we think
1090 * we are.
1091 */
1092 if (walfile == NULL)
1093 {
1094 /* No file open yet */
1095 if (xlogoff != 0)
1096 {
1097 pg_log_error("received write-ahead log record for offset %u with no file open",
1098 xlogoff);
1099 return false;
1100 }
1101 }
1102 else
1103 {
1104 /* More data in existing segment */
1105 if (walfile->currpos != xlogoff)
1106 {
1107 pg_log_error("got WAL data offset %08x, expected %08x",
1108 xlogoff, (int) walfile->currpos);
1109 return false;
1110 }
1111 }
1112
1114 bytes_written = 0;
1115
1116 while (bytes_left)
1117 {
1118 int bytes_to_write;
1119
1120 /*
1121 * If crossing a WAL boundary, only write up until we reach wal
1122 * segment size.
1123 */
1124 if (xlogoff + bytes_left > WalSegSz)
1126 else
1128
1129 if (walfile == NULL)
1130 {
1131 if (!open_walfile(stream, *blockpos))
1132 {
1133 /* Error logged by open_walfile */
1134 return false;
1135 }
1136 }
1137
1138 if (stream->walmethod->ops->write(walfile,
1141 {
1142 pg_log_error("could not write %d bytes to WAL file \"%s\": %s",
1145 return false;
1146 }
1147
1148 /* Write was successful, advance our position */
1153
1154 /* Did we reach the end of a WAL segment? */
1156 {
1157 if (!close_walfile(stream, *blockpos))
1158 /* Error message written in close_walfile() */
1159 return false;
1160
1161 xlogoff = 0;
1162
1163 if (still_sending && stream->stream_stop(*blockpos, stream->timeline, true))
1164 {
1165 if (PQputCopyEnd(conn, NULL) <= 0 || PQflush(conn))
1166 {
1167 pg_log_error("could not send copy-end packet: %s",
1169 return false;
1170 }
1171 still_sending = false;
1172 return true; /* ignore the rest of this WALData packet */
1173 }
1174 }
1175 }
1176 /* No more data left to write, receive next copy packet */
1177
1178 return true;
1179}
static bool open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
Definition receivelog.c:90
int64 fe_recvint64(char *buf)
Definition streamutil.c:895
ssize_t(* write)(Walfile *f, const void *buf, size_t count)
Definition walmethods.h:73
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)

References close_walfile(), conn, copybuf, Walfile::currpos, fb(), fe_recvint64(), GetLastWalMethodError(), len, open_walfile(), WalWriteMethod::ops, Walfile::pathname, pg_log_error, PQerrorMessage(), PQflush(), PQputCopyEnd(), still_sending, StreamCtl::stream_stop, StreamCtl::timeline, walfile, StreamCtl::walmethod, WalSegSz, WalWriteMethodOps::write, and XLogSegmentOffset.

Referenced by HandleCopyStream().

◆ ReadEndOfStreamingResult()

static bool ReadEndOfStreamingResult ( PGresult res,
XLogRecPtr startpos,
uint32 timeline 
)
static

Definition at line 705 of file receivelog.c.

706{
709
710 /*----------
711 * The result set consists of one row and two columns, e.g:
712 *
713 * next_tli | next_tli_startpos
714 * ----------+-------------------
715 * 4 | 0/9949AE0
716 *
717 * next_tli is the timeline ID of the next timeline after the one that
718 * just finished streaming. next_tli_startpos is the WAL location where
719 * the server switched to it.
720 *----------
721 */
722 if (PQnfields(res) < 2 || PQntuples(res) != 1)
723 {
724 pg_log_error("unexpected result set after end-of-timeline: got %d rows and %d fields, expected %d rows and %d fields",
725 PQntuples(res), PQnfields(res), 1, 2);
726 return false;
727 }
728
729 *timeline = atoi(PQgetvalue(res, 0, 0));
730 if (sscanf(PQgetvalue(res, 0, 1), "%X/%08X", &startpos_xlogid,
731 &startpos_xrecoff) != 2)
732 {
733 pg_log_error("could not parse next timeline's starting point \"%s\"",
734 PQgetvalue(res, 0, 1));
735 return false;
736 }
738
739 return true;
740}
uint64_t uint64
Definition c.h:681
uint32_t uint32
Definition c.h:680
#define PQgetvalue
#define PQnfields
#define PQntuples
static XLogRecPtr startpos

References fb(), pg_log_error, PQgetvalue, PQnfields, PQntuples, and startpos.

Referenced by ReceiveXlogStream().

◆ ReceiveXlogStream()

bool ReceiveXlogStream ( PGconn conn,
StreamCtl stream 
)

Definition at line 453 of file receivelog.c.

454{
455 PQExpBuffer query;
456 PGresult *res;
458
459 /*
460 * The caller should've checked the server version already, but doesn't do
461 * any harm to check it here too.
462 */
464 return false;
465
466 /*
467 * Decide whether we want to report the flush position. If we report the
468 * flush position, the primary will know what WAL we'll possibly
469 * re-request, and it can then remove older WAL safely. We must always do
470 * that when we are using slots.
471 *
472 * Reporting the flush position makes one eligible as a synchronous
473 * replica. People shouldn't include generic names in
474 * synchronous_standby_names, but we've protected them against it so far,
475 * so let's continue to do so unless specifically requested.
476 */
477 if (stream->replication_slot != NULL)
478 {
479 reportFlushPosition = true;
480 }
481 else
482 {
483 if (stream->synchronous)
484 reportFlushPosition = true;
485 else
486 reportFlushPosition = false;
487 }
488
489 if (stream->sysidentifier != NULL)
490 {
491 char *sysidentifier = NULL;
493
494 /*
495 * Get the server system identifier and timeline, and validate them.
496 */
497 if (!RunIdentifySystem(conn, &sysidentifier, &servertli, NULL, NULL))
498 {
499 pg_free(sysidentifier);
500 return false;
501 }
502
503 if (strcmp(stream->sysidentifier, sysidentifier) != 0)
504 {
505 pg_log_error("system identifier does not match between base backup and streaming connection");
506 pg_free(sysidentifier);
507 return false;
508 }
509 pg_free(sysidentifier);
510
511 if (stream->timeline > servertli)
512 {
513 pg_log_error("starting timeline %u is not present in the server",
514 stream->timeline);
515 return false;
516 }
517 }
518
519 /*
520 * initialize flush position to starting point, it's the caller's
521 * responsibility that that's sane.
522 */
523 lastFlushPosition = stream->startpos;
524
525 while (1)
526 {
527 /*
528 * Fetch the timeline history file for this timeline, if we don't have
529 * it already. When streaming log to tar, this will always return
530 * false, as we are never streaming into an existing file and
531 * therefore there can be no pre-existing timeline history file.
532 */
533 if (!existsTimeLineHistoryFile(stream))
534 {
535 query = createPQExpBuffer();
536 appendPQExpBuffer(query, "TIMELINE_HISTORY %u", stream->timeline);
537 res = PQexec(conn, query->data);
538 destroyPQExpBuffer(query);
540 {
541 /* FIXME: we might send it ok, but get an error */
542 pg_log_error("could not send replication command \"%s\": %s",
543 "TIMELINE_HISTORY", PQresultErrorMessage(res));
544 PQclear(res);
545 return false;
546 }
547
548 /*
549 * The response to TIMELINE_HISTORY is a single row result set
550 * with two fields: filename and content
551 */
552 if (PQnfields(res) != 2 || PQntuples(res) != 1)
553 {
554 pg_log_warning("unexpected response to TIMELINE_HISTORY command: got %d rows and %d fields, expected %d rows and %d fields",
555 PQntuples(res), PQnfields(res), 1, 2);
556 }
557
558 /* Write the history file to disk */
560 PQgetvalue(res, 0, 0),
561 PQgetvalue(res, 0, 1));
562
563 PQclear(res);
564 }
565
566 /*
567 * Before we start streaming from the requested location, check if the
568 * callback tells us to stop here.
569 */
570 if (stream->stream_stop(stream->startpos, stream->timeline, false))
571 return true;
572
573 /* Initiate the replication stream at specified location */
574 query = createPQExpBuffer();
575 appendPQExpBufferStr(query, "START_REPLICATION");
576 if (stream->replication_slot != NULL)
577 {
578 appendPQExpBufferStr(query, " SLOT ");
580 }
581 appendPQExpBuffer(query, " %X/%08X TIMELINE %u",
582 LSN_FORMAT_ARGS(stream->startpos),
583 stream->timeline);
584 res = PQexec(conn, query->data);
585 destroyPQExpBuffer(query);
587 {
588 pg_log_error("could not send replication command \"%s\": %s",
589 "START_REPLICATION", PQresultErrorMessage(res));
590 PQclear(res);
591 return false;
592 }
593 PQclear(res);
594
595 /* Stream the WAL */
596 res = HandleCopyStream(conn, stream, &stoppos);
597 if (res == NULL)
598 goto error;
599
600 /*
601 * Streaming finished.
602 *
603 * There are two possible reasons for that: a controlled shutdown, or
604 * we reached the end of the current timeline. In case of
605 * end-of-timeline, the server sends a result set after Copy has
606 * finished, containing information about the next timeline. Read
607 * that, and restart streaming from the next timeline. In case of
608 * controlled shutdown, stop here.
609 */
611 {
612 /*
613 * End-of-timeline. Read the next timeline's ID and starting
614 * position. Usually, the starting position will match the end of
615 * the previous timeline, but there are corner cases like if the
616 * server had sent us half of a WAL record, when it was promoted.
617 * The new timeline will begin at the end of the last complete
618 * record in that case, overlapping the partial WAL record on the
619 * old timeline.
620 */
622 bool parsed;
623
625 PQclear(res);
626 if (!parsed)
627 goto error;
628
629 /* Sanity check the values the server gave us */
630 if (newtimeline <= stream->timeline)
631 {
632 pg_log_error("server reported unexpected next timeline %u, following timeline %u",
633 newtimeline, stream->timeline);
634 goto error;
635 }
636 if (stream->startpos > stoppos)
637 {
638 pg_log_error("server stopped streaming timeline %u at %X/%08X, but reported next timeline %u to begin at %X/%08X",
641 goto error;
642 }
643
644 /* Read the final result, which should be CommandComplete. */
645 res = PQgetResult(conn);
647 {
648 pg_log_error("unexpected termination of replication stream: %s",
650 PQclear(res);
651 goto error;
652 }
653 PQclear(res);
654
655 /*
656 * Loop back to start streaming from the new timeline. Always
657 * start streaming at the beginning of a segment.
658 */
659 stream->timeline = newtimeline;
660 stream->startpos = stream->startpos -
662 continue;
663 }
664 else if (PQresultStatus(res) == PGRES_COMMAND_OK)
665 {
666 PQclear(res);
667
668 /*
669 * End of replication (ie. controlled shut down of the server).
670 *
671 * Check if the callback thinks it's OK to stop here. If not,
672 * complain.
673 */
674 if (stream->stream_stop(stoppos, stream->timeline, false))
675 return true;
676 else
677 {
678 pg_log_error("replication stream was terminated before stop point");
679 goto error;
680 }
681 }
682 else
683 {
684 /* Server returned an error. */
685 pg_log_error("unexpected termination of replication stream: %s",
687 PQclear(res);
688 goto error;
689 }
690 }
691
692error:
693 if (walfile != NULL && stream->walmethod->ops->close(walfile, CLOSE_NO_RENAME) != 0)
694 pg_log_error("could not close file \"%s\": %s",
696 walfile = NULL;
697 return false;
698}
PGresult * PQexec(PGconn *conn, const char *query)
Definition fe-exec.c:2279
#define PQresultErrorMessage
@ PGRES_COPY_BOTH
Definition libpq-fe.h:143
@ PGRES_COMMAND_OK
Definition libpq-fe.h:131
@ PGRES_TUPLES_OK
Definition libpq-fe.h:134
#define pg_log_warning(...)
Definition pgfnames.c:24
PQExpBuffer createPQExpBuffer(void)
Definition pqexpbuffer.c:72
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
static PGresult * HandleCopyStream(PGconn *conn, StreamCtl *stream, XLogRecPtr *stoppos)
Definition receivelog.c:751
static bool existsTimeLineHistoryFile(StreamCtl *stream)
Definition receivelog.c:258
static bool writeTimeLineHistoryFile(StreamCtl *stream, char *filename, char *content)
Definition receivelog.c:275
bool CheckServerVersionForStreaming(PGconn *conn)
Definition receivelog.c:375
static bool ReadEndOfStreamingResult(PGresult *res, XLogRecPtr *startpos, uint32 *timeline)
Definition receivelog.c:705
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition streamutil.c:409
#define AppendQuotedIdentifier(b, s)
Definition streamutil.h:47
char * sysidentifier
Definition receivelog.h:33
char * replication_slot
Definition receivelog.h:48
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint32 TimeLineID
Definition xlogdefs.h:63

References appendPQExpBuffer(), appendPQExpBufferStr(), AppendQuotedIdentifier, CheckServerVersionForStreaming(), WalWriteMethodOps::close, CLOSE_NO_RENAME, conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), error(), existsTimeLineHistoryFile(), fb(), GetLastWalMethodError(), HandleCopyStream(), lastFlushPosition, LSN_FORMAT_ARGS, WalWriteMethod::ops, Walfile::pathname, pg_free(), pg_log_error, pg_log_warning, PGRES_COMMAND_OK, PGRES_COPY_BOTH, PGRES_TUPLES_OK, PQclear, PQexec(), PQgetResult, PQgetvalue, PQnfields, PQntuples, PQresultErrorMessage, PQresultStatus, ReadEndOfStreamingResult(), StreamCtl::replication_slot, reportFlushPosition, RunIdentifySystem(), StreamCtl::startpos, StreamCtl::stream_stop, StreamCtl::synchronous, StreamCtl::sysidentifier, StreamCtl::timeline, walfile, StreamCtl::walmethod, WalSegSz, writeTimeLineHistoryFile(), and XLogSegmentOffset.

Referenced by LogStreamerMain(), and StreamLog().

◆ sendFeedback()

static bool sendFeedback ( PGconn conn,
XLogRecPtr  blockpos,
TimestampTz  now,
bool  replyRequested 
)
static

Definition at line 337 of file receivelog.c.

338{
339 char replybuf[1 + 8 + 8 + 8 + 8 + 1];
340 int len = 0;
341
343 len += 1;
344 fe_sendint64(blockpos, &replybuf[len]); /* write */
345 len += 8;
348 else
350 len += 8;
352 len += 8;
353 fe_sendint64(now, &replybuf[len]); /* sendTime */
354 len += 8;
355 replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */
356 len += 1;
357
358 if (PQputCopyData(conn, replybuf, len) <= 0 || PQflush(conn))
359 {
360 pg_log_error("could not send feedback packet: %s",
362 return false;
363 }
364
365 return true;
366}
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition fe-exec.c:2712
#define PqReplMsg_StandbyStatusUpdate
Definition protocol.h:84
void fe_sendint64(int64 i, char *buf)
Definition streamutil.c:884
#define InvalidXLogRecPtr
Definition xlogdefs.h:28

References conn, fb(), fe_sendint64(), InvalidXLogRecPtr, lastFlushPosition, len, now(), pg_log_error, PQerrorMessage(), PQflush(), PQputCopyData(), PqReplMsg_StandbyStatusUpdate, and reportFlushPosition.

Referenced by HandleCopyStream(), and ProcessKeepaliveMsg().

◆ writeTimeLineHistoryFile()

static bool writeTimeLineHistoryFile ( StreamCtl stream,
char filename,
char content 
)
static

Definition at line 275 of file receivelog.c.

276{
277 int size = strlen(content);
279 Walfile *f;
280
281 /*
282 * Check that the server's idea of how timeline history files should be
283 * named matches ours.
284 */
286 if (strcmp(histfname, filename) != 0)
287 {
288 pg_log_error("server reported unexpected history file name for timeline %u: %s",
289 stream->timeline, filename);
290 return false;
291 }
292
293 f = stream->walmethod->ops->open_for_write(stream->walmethod,
294 histfname, ".tmp", 0);
295 if (f == NULL)
296 {
297 pg_log_error("could not create timeline history file \"%s\": %s",
299 return false;
300 }
301
302 if ((int) stream->walmethod->ops->write(f, content, size) != size)
303 {
304 pg_log_error("could not write timeline history file \"%s\": %s",
306
307 /*
308 * If we fail to make the file, delete it to release disk space
309 */
310 stream->walmethod->ops->close(f, CLOSE_UNLINK);
311
312 return false;
313 }
314
315 if (stream->walmethod->ops->close(f, CLOSE_NORMAL) != 0)
316 {
317 pg_log_error("could not close file \"%s\": %s",
319 return false;
320 }
321
322 /* Maintain archive_status, check close_walfile() for details. */
323 if (stream->mark_done)
324 {
325 /* writes error message if failed */
326 if (!mark_file_as_archived(stream, histfname))
327 return false;
328 }
329
330 return true;
331}
static char * filename
Definition pg_dumpall.c:120

References WalWriteMethodOps::close, CLOSE_NORMAL, CLOSE_UNLINK, fb(), filename, GetLastWalMethodError(), StreamCtl::mark_done, mark_file_as_archived(), MAXFNAMELEN, WalWriteMethodOps::open_for_write, WalWriteMethod::ops, pg_log_error, StreamCtl::timeline, TLHistoryFileName(), StreamCtl::walmethod, and WalWriteMethodOps::write.

Referenced by ReceiveXlogStream().

Variable Documentation

◆ lastFlushPosition

XLogRecPtr lastFlushPosition = InvalidXLogRecPtr
static

◆ reportFlushPosition

bool reportFlushPosition = false
static

Definition at line 30 of file receivelog.c.

Referenced by ProcessKeepaliveMsg(), ReceiveXlogStream(), and sendFeedback().

◆ still_sending

◆ walfile