1 /*-------------------------------------------------------------------------
5 * This file contains the libpq-specific parts of walreceiver. It's
6 * loaded as a dynamic module to avoid linking the main server binary with
9 * Portions Copyright (c) 2010-2014, PostgreSQL Global Development Group
13 * src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
15 *-------------------------------------------------------------------------
23 #include "access/xlog.h"
24 #include "miscadmin.h"
25 #include "replication/walreceiver.h"
26 #include "utils/builtins.h"
31 #ifdef HAVE_SYS_POLL_H
34 #ifdef HAVE_SYS_SELECT_H
35 #include <sys/select.h>
42 /* Current connection to the primary, if any */
43 static PGconn *streamConn = NULL;
45 /* Buffer for currently read records */
46 static char *recvBuf = NULL;
48 /* Prototypes for interface functions */
49 static void libpqrcv_connect(char *conninfo);
50 static void libpqrcv_identify_system(TimeLineID *primary_tli);
51 static void libpqrcv_readtimelinehistoryfile(TimeLineID tli, char **filename, char **content, int *len);
52 static bool libpqrcv_startstreaming(TimeLineID tli, XLogRecPtr startpoint,
54 static void libpqrcv_endstreaming(TimeLineID *next_tli);
55 static int libpqrcv_receive(int timeout, char **buffer);
56 static void libpqrcv_send(const char *buffer, int nbytes);
57 static void libpqrcv_disconnect(void);
59 /* Prototypes for private functions */
60 static bool libpq_select(int timeout_ms);
61 static PGresult *libpqrcv_PQexec(const char *query);
64 * Module load callback
69 /* Tell walreceiver how to reach us */
70 if (walrcv_connect != NULL || walrcv_identify_system != NULL ||
71 walrcv_readtimelinehistoryfile != NULL ||
72 walrcv_startstreaming != NULL || walrcv_endstreaming != NULL ||
73 walrcv_receive != NULL || walrcv_send != NULL ||
74 walrcv_disconnect != NULL)
75 elog(ERROR, "libpqwalreceiver already loaded");
76 walrcv_connect = libpqrcv_connect;
77 walrcv_identify_system = libpqrcv_identify_system;
78 walrcv_readtimelinehistoryfile = libpqrcv_readtimelinehistoryfile;
79 walrcv_startstreaming = libpqrcv_startstreaming;
80 walrcv_endstreaming = libpqrcv_endstreaming;
81 walrcv_receive = libpqrcv_receive;
82 walrcv_send = libpqrcv_send;
83 walrcv_disconnect = libpqrcv_disconnect;
87 * Establish the connection to the primary server for XLOG streaming
90 libpqrcv_connect(char *conninfo)
92 char conninfo_repl[MAXCONNINFO + 75];
95 * Connect using deliberately undocumented parameter: replication. The
96 * database name is ignored by the server in replication mode, but specify
97 * "replication" for .pgpass lookup.
99 snprintf(conninfo_repl, sizeof(conninfo_repl),
100 "%s dbname=replication replication=true fallback_application_name=walreceiver",
103 streamConn = PQconnectdb(conninfo_repl);
104 if (PQstatus(streamConn) != CONNECTION_OK)
106 (errmsg("could not connect to the primary server: %s",
107 PQerrorMessage(streamConn))));
111 * Check that primary's system identifier matches ours, and fetch the current
112 * timeline ID of the primary.
115 libpqrcv_identify_system(TimeLineID *primary_tli)
119 char standby_sysid[32];
122 * Get the system identifier and timeline ID as a DataRow message from the
125 res = libpqrcv_PQexec("IDENTIFY_SYSTEM");
126 if (PQresultStatus(res) != PGRES_TUPLES_OK)
130 (errmsg("could not receive database system identifier and timeline ID from "
131 "the primary server: %s",
132 PQerrorMessage(streamConn))));
134 if (PQnfields(res) != 3 || PQntuples(res) != 1)
136 int ntuples = PQntuples(res);
137 int nfields = PQnfields(res);
141 (errmsg("invalid response from primary server"),
142 errdetail("Expected 1 tuple with 3 fields, got %d tuples with %d fields.",
145 primary_sysid = PQgetvalue(res, 0, 0);
146 *primary_tli = pg_atoi(PQgetvalue(res, 0, 1), 4, 0);
149 * Confirm that the system identifier of the primary is the same as ours.
151 snprintf(standby_sysid, sizeof(standby_sysid), UINT64_FORMAT,
152 GetSystemIdentifier());
153 if (strcmp(primary_sysid, standby_sysid) != 0)
157 (errmsg("database system identifier differs between the primary and standby"),
158 errdetail("The primary's identifier is %s, the standby's identifier is %s.",
159 primary_sysid, standby_sysid)));
165 * Start streaming WAL data from given startpoint and timeline.
167 * Returns true if we switched successfully to copy-both mode. False
168 * means the server received the command and executed it successfully, but
169 * didn't switch to copy-mode. That means that there was no WAL on the
170 * requested timeline and starting point, because the server switched to
171 * another timeline at or before the requested starting point. On failure,
175 libpqrcv_startstreaming(TimeLineID tli, XLogRecPtr startpoint, char *slotname)
180 /* Start streaming from the point requested by startup process */
181 if (slotname != NULL)
182 snprintf(cmd, sizeof(cmd),
183 "START_REPLICATION SLOT \"%s\" %X/%X TIMELINE %u", slotname,
184 (uint32) (startpoint >> 32), (uint32) startpoint, tli);
186 snprintf(cmd, sizeof(cmd),
187 "START_REPLICATION %X/%X TIMELINE %u",
188 (uint32) (startpoint >> 32), (uint32) startpoint, tli);
189 res = libpqrcv_PQexec(cmd);
191 if (PQresultStatus(res) == PGRES_COMMAND_OK)
196 else if (PQresultStatus(res) != PGRES_COPY_BOTH)
200 (errmsg("could not start WAL streaming: %s",
201 PQerrorMessage(streamConn))));
208 * Stop streaming WAL data. Returns the next timeline's ID in *next_tli, as
209 * reported by the server, or 0 if it did not report it.
212 libpqrcv_endstreaming(TimeLineID *next_tli)
216 if (PQputCopyEnd(streamConn, NULL) <= 0 || PQflush(streamConn))
218 (errmsg("could not send end-of-streaming message to primary: %s",
219 PQerrorMessage(streamConn))));
222 * After COPY is finished, we should receive a result set indicating the
223 * next timeline's ID, or just CommandComplete if the server was shut
226 * If we had not yet received CopyDone from the backend, PGRES_COPY_IN
227 * would also be possible. However, at the moment this function is only
228 * called after receiving CopyDone from the backend - the walreceiver
229 * never terminates replication on its own initiative.
231 res = PQgetResult(streamConn);
232 if (PQresultStatus(res) == PGRES_TUPLES_OK)
235 * Read the next timeline's ID. The server also sends the timeline's
236 * starting point, but it is ignored.
238 if (PQnfields(res) < 2 || PQntuples(res) != 1)
240 (errmsg("unexpected result set after end-of-streaming")));
241 *next_tli = pg_atoi(PQgetvalue(res, 0, 0), sizeof(uint32), 0);
244 /* the result set should be followed by CommandComplete */
245 res = PQgetResult(streamConn);
250 if (PQresultStatus(res) != PGRES_COMMAND_OK)
252 (errmsg("error reading result of streaming command: %s",
253 PQerrorMessage(streamConn))));
255 /* Verify that there are no more results */
256 res = PQgetResult(streamConn);
259 (errmsg("unexpected result after CommandComplete: %s",
260 PQerrorMessage(streamConn))));
264 * Fetch the timeline history file for 'tli' from primary.
267 libpqrcv_readtimelinehistoryfile(TimeLineID tli,
268 char **filename, char **content, int *len)
274 * Request the primary to send over the history file for given timeline.
276 snprintf(cmd, sizeof(cmd), "TIMELINE_HISTORY %u", tli);
277 res = libpqrcv_PQexec(cmd);
278 if (PQresultStatus(res) != PGRES_TUPLES_OK)
282 (errmsg("could not receive timeline history file from "
283 "the primary server: %s",
284 PQerrorMessage(streamConn))));
286 if (PQnfields(res) != 2 || PQntuples(res) != 1)
288 int ntuples = PQntuples(res);
289 int nfields = PQnfields(res);
293 (errmsg("invalid response from primary server"),
294 errdetail("Expected 1 tuple with 2 fields, got %d tuples with %d fields.",
297 *filename = pstrdup(PQgetvalue(res, 0, 0));
299 *len = PQgetlength(res, 0, 1);
300 *content = palloc(*len);
301 memcpy(*content, PQgetvalue(res, 0, 1), *len);
306 * Wait until we can read WAL stream, or timeout.
308 * Returns true if data has become available for reading, false if timed out
309 * or interrupted by signal.
311 * This is based on pqSocketCheck.
314 libpq_select(int timeout_ms)
318 Assert(streamConn != NULL);
319 if (PQsocket(streamConn) < 0)
321 (errcode_for_socket_access(),
322 errmsg("socket not open")));
324 /* We use poll(2) if available, otherwise select(2) */
327 struct pollfd input_fd;
329 input_fd.fd = PQsocket(streamConn);
330 input_fd.events = POLLIN | POLLERR;
331 input_fd.revents = 0;
333 ret = poll(&input_fd, 1, timeout_ms);
334 #else /* !HAVE_POLL */
337 struct timeval timeout;
338 struct timeval *ptr_timeout;
340 FD_ZERO(&input_mask);
341 FD_SET(PQsocket(streamConn), &input_mask);
347 timeout.tv_sec = timeout_ms / 1000;
348 timeout.tv_usec = (timeout_ms % 1000) * 1000;
349 ptr_timeout = &timeout;
352 ret = select(PQsocket(streamConn) + 1, &input_mask,
353 NULL, NULL, ptr_timeout);
354 #endif /* HAVE_POLL */
357 if (ret == 0 || (ret < 0 && errno == EINTR))
361 (errcode_for_socket_access(),
362 errmsg("select() failed: %m")));
367 * Send a query and wait for the results by using the asynchronous libpq
368 * functions and the backend version of select().
370 * We must not use the regular blocking libpq functions like PQexec()
371 * since they are uninterruptible by signals on some platforms, such as
374 * We must also not use vanilla select() here since it cannot handle the
375 * signal emulation layer on Windows.
377 * The function is modeled on PQexec() in libpq, but only implements
378 * those parts that are in use in the walreceiver.
380 * Queries are always executed on the connection in streamConn.
383 libpqrcv_PQexec(const char *query)
385 PGresult *result = NULL;
386 PGresult *lastResult = NULL;
389 * PQexec() silently discards any prior query results on the connection.
390 * This is not required for walreceiver since it's expected that walsender
391 * won't generate any such junk results.
395 * Submit a query. Since we don't use non-blocking mode, this also can
396 * block. But its risk is relatively small, so we ignore that for now.
398 if (!PQsendQuery(streamConn, query))
404 * Receive data until PQgetResult is ready to get the result without
407 while (PQisBusy(streamConn))
410 * We don't need to break down the sleep into smaller increments,
411 * and check for interrupts after each nap, since we can just
412 * elog(FATAL) within SIGTERM signal handler if the signal arrives
413 * in the middle of establishment of replication connection.
415 if (!libpq_select(-1))
416 continue; /* interrupted */
417 if (PQconsumeInput(streamConn) == 0)
418 return NULL; /* trouble */
422 * Emulate the PQexec()'s behavior of returning the last result when
423 * there are many. Since walsender will never generate multiple
424 * results, we skip the concatenation of error messages.
426 result = PQgetResult(streamConn);
428 break; /* query is complete */
433 if (PQresultStatus(lastResult) == PGRES_COPY_IN ||
434 PQresultStatus(lastResult) == PGRES_COPY_OUT ||
435 PQresultStatus(lastResult) == PGRES_COPY_BOTH ||
436 PQstatus(streamConn) == CONNECTION_BAD)
444 * Disconnect connection to primary, if any.
447 libpqrcv_disconnect(void)
449 PQfinish(streamConn);
454 * Receive a message available from XLOG stream, blocking for
455 * maximum of 'timeout' ms.
459 * If data was received, returns the length of the data. *buffer is set to
460 * point to a buffer holding the received message. The buffer is only valid
461 * until the next libpqrcv_* call.
463 * 0 if no data was available within timeout, or wait was interrupted
466 * -1 if the server ended the COPY.
471 libpqrcv_receive(int timeout, char **buffer)
479 /* Try to receive a CopyData message */
480 rawlen = PQgetCopyData(streamConn, &recvBuf, 1);
484 * No data available yet. If the caller requested to block, wait for
485 * more data to arrive.
489 if (!libpq_select(timeout))
493 if (PQconsumeInput(streamConn) == 0)
495 (errmsg("could not receive data from WAL stream: %s",
496 PQerrorMessage(streamConn))));
498 /* Now that we've consumed some input, try again */
499 rawlen = PQgetCopyData(streamConn, &recvBuf, 1);
503 if (rawlen == -1) /* end-of-streaming or error */
507 res = PQgetResult(streamConn);
508 if (PQresultStatus(res) == PGRES_COMMAND_OK ||
509 PQresultStatus(res) == PGRES_COPY_IN)
518 (errmsg("could not receive data from WAL stream: %s",
519 PQerrorMessage(streamConn))));
524 (errmsg("could not receive data from WAL stream: %s",
525 PQerrorMessage(streamConn))));
527 /* Return received messages to caller */
533 * Send a message to XLOG stream.
538 libpqrcv_send(const char *buffer, int nbytes)
540 if (PQputCopyData(streamConn, buffer, nbytes) <= 0 ||
543 (errmsg("could not send data to WAL stream: %s",
544 PQerrorMessage(streamConn))));