Debian Codegraph @

src/backend/replication/walreceiver.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * walreceiver.c
4 *
5 * The WAL receiver process (walreceiver) is new as of Postgres 9.0. It
6 * is the process in the standby server that takes charge of receiving
7 * XLOG records from a primary server during streaming replication.
8 *
9 * When the startup process determines that it's time to start streaming,
10 * it instructs postmaster to start walreceiver. Walreceiver first connects
11 * to the primary server (it will be served by a walsender process
12 * in the primary server), and then keeps receiving XLOG records and
13 * writing them to the disk as long as the connection is alive. As XLOG
14 * records are received and flushed to disk, it updates the
15 * WalRcv->flushedUpto variable in shared memory, to inform the startup
16 * process of how far it can proceed with XLOG replay.
17 *
18 * A WAL receiver cannot directly load GUC parameters used when establishing
19 * its connection to the primary. Instead it relies on parameter values
20 * that are passed down by the startup process when streaming is requested.
21 * This applies, for example, to the replication slot and the connection
22 * string to be used for the connection with the primary.
23 *
24 * If the primary server ends streaming, but doesn't disconnect, walreceiver
25 * goes into "waiting" mode, and waits for the startup process to give new
26 * instructions. The startup process will treat that the same as
27 * disconnection, and will rescan the archive/pg_wal directory. But when the
28 * startup process wants to try streaming replication again, it will just
29 * nudge the existing walreceiver process that's waiting, instead of launching
30 * a new one.
31 *
32 * Normal termination is by SIGTERM, which instructs the walreceiver to
33 * exit(0). Emergency termination is by SIGQUIT; like any postmaster child
34 * process, the walreceiver will simply abort and exit on SIGQUIT. A close
35 * of the connection and a FATAL error are treated not as a crash but as
36 * normal operation.
37 *
38 * This file contains the server-facing parts of walreceiver. The libpq-
39 * specific parts are in the libpqwalreceiver module. It's loaded
40 * dynamically to avoid linking the server with libpq.
41 *
42 * Portions Copyright (c) 2010-2025, PostgreSQL Global Development Group
43 *
44 *
45 * IDENTIFICATION
46 * src/backend/replication/walreceiver.c
47 *
48 *-------------------------------------------------------------------------
49 */
50 #include "postgres.h"
51
52 #include <unistd.h>
53
54 #include "access/htup_details.h"
55 #include "access/timeline.h"
56 #include "access/transam.h"
57 #include "access/xlog.h"
58 #include "access/xlog_internal.h"
59 #include "access/xlogarchive.h"
60 #include "access/xlogrecovery.h"
61 #include "catalog/pg_authid.h"
62 #include "funcapi.h"
63 #include "libpq/pqformat.h"
64 #include "libpq/pqsignal.h"
65 #include "miscadmin.h"
66 #include "pgstat.h"
67 #include "postmaster/auxprocess.h"
68 #include "postmaster/interrupt.h"
69 #include "replication/walreceiver.h"
70 #include "replication/walsender.h"
71 #include "storage/ipc.h"
72 #include "storage/proc.h"
73 #include "storage/procarray.h"
74 #include "storage/procsignal.h"
75 #include "tcop/tcopprot.h"
76 #include "utils/acl.h"
77 #include "utils/builtins.h"
78 #include "utils/guc.h"
79 #include "utils/pg_lsn.h"
80 #include "utils/ps_status.h"
81 #include "utils/timestamp.h"
82
83
84 /*
85 * GUC variables. (Other variables that affect walreceiver are in xlog.c
86 * because they're passed down from the startup process, for better
87 * synchronization.)
88 */
89 int wal_receiver_status_interval;
90 int wal_receiver_timeout;
91 bool hot_standby_feedback;
92
93 /* libpqwalreceiver connection */
94 static WalReceiverConn *wrconn = NULL;
95 WalReceiverFunctionsType *WalReceiverFunctions = NULL;
96
97 /*
98 * Server's WAL flush position from the last IDENTIFY_SYSTEM call.
99 * Written by libpqwalreceiver, read by walreceiver main loop.
100 */
101 XLogRecPtr WalRcvIdentifySystemLsn = InvalidXLogRecPtr;
102
103 /*
104 * These variables are used similarly to openLogFile/SegNo,
105 * but for walreceiver to write the XLOG. recvFileTLI is the TimeLineID
106 * corresponding the filename of recvFile.
107 */
108 static int recvFile = -1;
109 static TimeLineID recvFileTLI = 0;
110 static XLogSegNo recvSegNo = 0;
111
112 /*
113 * LogstreamResult indicates the byte positions that we have already
114 * written/fsynced.
115 */
116 static struct
117 {
118 XLogRecPtr Write; /* last byte + 1 written out in the standby */
119 XLogRecPtr Flush; /* last byte + 1 flushed in the standby */
120 } LogstreamResult;
121
122 /*
123 * Reasons to wake up and perform periodic tasks.
124 */
125 typedef enum WalRcvWakeupReason
126 {
127 WALRCV_WAKEUP_TERMINATE,
128 WALRCV_WAKEUP_PING,
129 WALRCV_WAKEUP_REPLY,
130 WALRCV_WAKEUP_HSFEEDBACK,
131 #define NUM_WALRCV_WAKEUPS (WALRCV_WAKEUP_HSFEEDBACK + 1)
132 } WalRcvWakeupReason;
133
134 /*
135 * Wake up times for periodic tasks.
136 */
137 static TimestampTz wakeup[NUM_WALRCV_WAKEUPS];
138
139 static StringInfoData reply_message;
140
141 /* Prototypes for private functions */
142 static void WalRcvFetchTimeLineHistoryFiles(TimeLineID first, TimeLineID last);
143 static void WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI);
144 static void WalRcvDie(int code, Datum arg);
145 static void XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len,
146 TimeLineID tli);
147 static void XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr,
148 TimeLineID tli);
149 static void XLogWalRcvFlush(bool dying, TimeLineID tli);
150 static void XLogWalRcvClose(XLogRecPtr recptr, TimeLineID tli);
151 static void XLogWalRcvSendReply(bool force, bool requestReply);
152 static void XLogWalRcvSendHSFeedback(bool immed);
153 static void ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime);
154 static void WalRcvComputeNextWakeup(WalRcvWakeupReason reason, TimestampTz now);
155
156
157 /* Main entry point for walreceiver process */
158 void
159 WalReceiverMain(const void *startup_data, size_t startup_data_len)
160 {
161 char conninfo[MAXCONNINFO];
162 char *tmp_conninfo;
163 char slotname[NAMEDATALEN];
164 bool is_temp_slot;
165 XLogRecPtr startpoint;
166 TimeLineID startpointTLI;
167 TimeLineID primaryTLI;
168 bool first_stream;
169 bool upstream_catchup_logged = false;
170 TimestampTz upstream_catchup_deadline = 0;
171 WalRcvData *walrcv;
172 TimestampTz now;
173 char *err;
174 char *sender_host = NULL;
175 int sender_port = 0;
176 char *appname;
177
178 Assert(startup_data_len == 0);
179
180 MyBackendType = B_WAL_RECEIVER;
181 AuxiliaryProcessMainCommon();
182
183 /*
184 * WalRcv should be set up already (if we are a backend, we inherit this
185 * by fork() or EXEC_BACKEND mechanism from the postmaster).
186 */
187 walrcv = WalRcv;
188 Assert(walrcv != NULL);
189
190 /*
191 * Mark walreceiver as running in shared memory.
192 *
193 * Do this as early as possible, so that if we fail later on, we'll set
194 * state to STOPPED. If we die before this, the startup process will keep
195 * waiting for us to start up, until it times out.
196 */
197 SpinLockAcquire(&walrcv->mutex);
198 Assert(walrcv->pid == 0);
199 switch (walrcv->walRcvState)
200 {
201 case WALRCV_STOPPING:
202 /* If we've already been requested to stop, don't start up. */
203 walrcv->walRcvState = WALRCV_STOPPED;
204 /* fall through */
205
206 case WALRCV_STOPPED:
207 SpinLockRelease(&walrcv->mutex);
208 ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
209 proc_exit(1);
210 break;
211
212 case WALRCV_STARTING:
213 /* The usual case */
214 break;
215
216 case WALRCV_WAITING:
217 case WALRCV_STREAMING:
218 case WALRCV_RESTARTING:
219 default:
220 /* Shouldn't happen */
221 SpinLockRelease(&walrcv->mutex);
222 elog(PANIC, "walreceiver still running according to shared memory state");
223 }
224 /* Advertise our PID so that the startup process can kill us */
225 walrcv->pid = MyProcPid;
226 walrcv->walRcvState = WALRCV_STREAMING;
227
228 /* Fetch information required to start streaming */
229 walrcv->ready_to_display = false;
230 strlcpy(conninfo, walrcv->conninfo, MAXCONNINFO);
231 strlcpy(slotname, walrcv->slotname, NAMEDATALEN);
232 is_temp_slot = walrcv->is_temp_slot;
233 startpoint = walrcv->receiveStart;
234 startpointTLI = walrcv->receiveStartTLI;
235
236 /*
237 * At most one of is_temp_slot and slotname can be set; otherwise,
238 * RequestXLogStreaming messed up.
239 */
240 Assert(!is_temp_slot || (slotname[0] == '\0'));
241
242 /* Initialise to a sanish value */
243 now = GetCurrentTimestamp();
244 walrcv->lastMsgSendTime =
245 walrcv->lastMsgReceiptTime = walrcv->latestWalEndTime = now;
246
247 /* Report our proc number so that others can wake us up */
248 walrcv->procno = MyProcNumber;
249
250 SpinLockRelease(&walrcv->mutex);
251
252 pg_atomic_write_u64(&WalRcv->writtenUpto, 0);
253
254 /* Arrange to clean up at walreceiver exit */
255 on_shmem_exit(WalRcvDie, PointerGetDatum(&startpointTLI));
256
257 /* Properly accept or ignore signals the postmaster might send us */
258 pqsignal(SIGHUP, SignalHandlerForConfigReload); /* set flag to read config
259 * file */
260 pqsignal(SIGINT, SIG_IGN);
261 pqsignal(SIGTERM, die); /* request shutdown */
262 /* SIGQUIT handler was already set up by InitPostmasterChild */
263 pqsignal(SIGALRM, SIG_IGN);
264 pqsignal(SIGPIPE, SIG_IGN);
265 pqsignal(SIGUSR1, procsignal_sigusr1_handler);
266 pqsignal(SIGUSR2, SIG_IGN);
267
268 /* Reset some signals that are accepted by postmaster but not here */
269 pqsignal(SIGCHLD, SIG_DFL);
270
271 /* Load the libpq-specific functions */
272 load_file("libpqwalreceiver", false);
273 if (WalReceiverFunctions == NULL)
274 elog(ERROR, "libpqwalreceiver didn't initialize correctly");
275
276 /* Unblock signals (they were blocked when the postmaster forked us) */
277 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
278
279 /* Establish the connection to the primary for XLOG streaming */
280 appname = cluster_name[0] ? cluster_name : "walreceiver";
281 wrconn = walrcv_connect(conninfo, true, false, false, appname, &err);
282 if (!wrconn)
283 ereport(ERROR,
284 (errcode(ERRCODE_CONNECTION_FAILURE),
285 errmsg("streaming replication receiver \"%s\" could not connect to the primary server: %s",
286 appname, err)));
287
288 /*
289 * Save user-visible connection string. This clobbers the original
290 * conninfo, for security. Also save host and port of the sender server
291 * this walreceiver is connected to.
292 */
293 tmp_conninfo = walrcv_get_conninfo(wrconn);
294 walrcv_get_senderinfo(wrconn, &sender_host, &sender_port);
295 SpinLockAcquire(&walrcv->mutex);
296 memset(walrcv->conninfo, 0, MAXCONNINFO);
297 if (tmp_conninfo)
298 strlcpy(walrcv->conninfo, tmp_conninfo, MAXCONNINFO);
299
300 memset(walrcv->sender_host, 0, NI_MAXHOST);
301 if (sender_host)
302 strlcpy(walrcv->sender_host, sender_host, NI_MAXHOST);
303
304 walrcv->sender_port = sender_port;
305 walrcv->ready_to_display = true;
306 SpinLockRelease(&walrcv->mutex);
307
308 if (tmp_conninfo)
309 pfree(tmp_conninfo);
310
311 if (sender_host)
312 pfree(sender_host);
313
314 first_stream = true;
315 for (;;)
316 {
317 char *primary_sysid;
318 char standby_sysid[32];
319 WalRcvStreamOptions options;
320
321 /*
322 * Check that we're connected to a valid server using the
323 * IDENTIFY_SYSTEM replication command. Reset the global LSN
324 * first so we don't act on a stale value if the call fails.
325 */
326 WalRcvIdentifySystemLsn = InvalidXLogRecPtr;
327 primary_sysid = walrcv_identify_system(wrconn, &primaryTLI);
328
329 snprintf(standby_sysid, sizeof(standby_sysid), UINT64_FORMAT,
330 GetSystemIdentifier());
331 if (strcmp(primary_sysid, standby_sysid) != 0)
332 {
333 ereport(ERROR,
334 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
335 errmsg("database system identifier differs between the primary and standby"),
336 errdetail("The primary's identifier is %s, the standby's identifier is %s.",
337 primary_sysid, standby_sysid)));
338 }
339
340 /*
341 * Confirm that the current timeline of the primary is the same or
342 * ahead of ours.
343 */
344 if (primaryTLI < startpointTLI)
345 ereport(ERROR,
346 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
347 errmsg("highest timeline %u of the primary is behind recovery timeline %u",
348 primaryTLI, startpointTLI)));
349
350 /*
351 * If our requested startpoint is ahead of the upstream server's
352 * current WAL flush position, we cannot start streaming yet. (We say
353 * "upstream" here and not "primary" because this condition can only
354 * happen on a cascading standby.) This can happen when such a
355 * cascading standby has advanced past the upstream via archive
356 * recovery but the intermediate standby has not caught up with that
357 * yet. In this case, wait for the upstream to catch up before
358 * attempting START_REPLICATION, because that would fail with
359 * "requested starting point is ahead of the WAL flush position".
360 *
361 * We only perform this check when we're on the same timeline as the
362 * primary; when timelines differ, let START_REPLICATION handle the
363 * timeline negotiation.
364 *
365 * We also only wait if the gap is within one WAL segment. This is
366 * the expected case because archive recovery processes whole segment
367 * files: the cascade's next read position lands at the start of the
368 * following segment while the upstream's flush position is still
369 * inside the just-replayed one, producing at most a sub-segment gap.
370 * A larger gap means the upstream is genuinely behind, so we let
371 * START_REPLICATION fail normally and allow the startup process to
372 * fall back to other WAL sources.
373 *
374 * Honor wal_receiver_timeout so the walreceiver doesn't wait
375 * indefinitely: if the upstream hasn't caught up within the timeout,
376 * exit and let the startup process retry normally.
377 */
378 if (startpointTLI == primaryTLI &&
379 XLogRecPtrIsValid(WalRcvIdentifySystemLsn) &&
380 startpoint > WalRcvIdentifySystemLsn &&
381 startpoint - WalRcvIdentifySystemLsn <= wal_segment_size)
382 {
383 /* Set deadline on first iteration */
384 if (!upstream_catchup_logged && wal_receiver_timeout > 0)
385 upstream_catchup_deadline =
386 TimestampTzPlusMilliseconds(GetCurrentTimestamp(),
387 wal_receiver_timeout);
388
389 ereport(upstream_catchup_logged ? DEBUG1 : LOG,
390 errmsg("walreceiver requested start point %X/%08X on timeline %u is ahead of the upstream server's flush position %X/%08X, waiting",
391 LSN_FORMAT_ARGS(startpoint), startpointTLI,
392 LSN_FORMAT_ARGS(WalRcvIdentifySystemLsn)));
393 upstream_catchup_logged = true;
394
395 (void) WaitLatch(MyLatch,
396 WL_EXIT_ON_PM_DEATH | WL_TIMEOUT | WL_LATCH_SET,
397 wal_retrieve_retry_interval,
398 WAIT_EVENT_WAL_RECEIVER_UPSTREAM_CATCHUP);
399 ResetLatch(MyLatch);
400
401 if (upstream_catchup_deadline > 0 &&
402 GetCurrentTimestamp() >= upstream_catchup_deadline)
403 ereport(ERROR,
404 (errcode(ERRCODE_CONNECTION_FAILURE),
405 errmsg("terminating walreceiver due to timeout while waiting for upstream to catch up")));
406
407 CHECK_FOR_INTERRUPTS();
408 continue;
409 }
410 else
411 {
412 upstream_catchup_logged = false;
413 upstream_catchup_deadline = 0;
414 }
415
416 /*
417 * Get any missing history files. We do this always, even when we're
418 * not interested in that timeline, so that if we're promoted to
419 * become the primary later on, we don't select the same timeline that
420 * was already used in the current primary. This isn't bullet-proof -
421 * you'll need some external software to manage your cluster if you
422 * need to ensure that a unique timeline id is chosen in every case,
423 * but let's avoid the confusion of timeline id collisions where we
424 * can.
425 */
426 WalRcvFetchTimeLineHistoryFiles(startpointTLI, primaryTLI);
427
428 /*
429 * Create temporary replication slot if requested, and update slot
430 * name in shared memory. (Note the slot name cannot already be set
431 * in this case.)
432 */
433 if (is_temp_slot)
434 {
435 snprintf(slotname, sizeof(slotname),
436 "pg_walreceiver_%lld",
437 (long long int) walrcv_get_backend_pid(wrconn));
438
439 walrcv_create_slot(wrconn, slotname, true, false, false, 0, NULL);
440
441 SpinLockAcquire(&walrcv->mutex);
442 strlcpy(walrcv->slotname, slotname, NAMEDATALEN);
443 SpinLockRelease(&walrcv->mutex);
444 }
445
446 /*
447 * Start streaming.
448 *
449 * We'll try to start at the requested starting point and timeline,
450 * even if it's different from the server's latest timeline. In case
451 * we've already reached the end of the old timeline, the server will
452 * finish the streaming immediately, and we will go back to await
453 * orders from the startup process. If recovery_target_timeline is
454 * 'latest', the startup process will scan pg_wal and find the new
455 * history file, bump recovery target timeline, and ask us to restart
456 * on the new timeline.
457 */
458 options.logical = false;
459 options.startpoint = startpoint;
460 options.slotname = slotname[0] != '\0' ? slotname : NULL;
461 options.proto.physical.startpointTLI = startpointTLI;
462 if (walrcv_startstreaming(wrconn, &options))
463 {
464 if (first_stream)
465 ereport(LOG,
466 (errmsg("started streaming WAL from primary at %X/%X on timeline %u",
467 LSN_FORMAT_ARGS(startpoint), startpointTLI)));
468 else
469 ereport(LOG,
470 (errmsg("restarted WAL streaming at %X/%X on timeline %u",
471 LSN_FORMAT_ARGS(startpoint), startpointTLI)));
472 first_stream = false;
473
474 /* Initialize LogstreamResult and buffers for processing messages */
475 LogstreamResult.Write = LogstreamResult.Flush = GetXLogReplayRecPtr(NULL);
476 initStringInfo(&reply_message);
477
478 /* Initialize nap wakeup times. */
479 now = GetCurrentTimestamp();
480 for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
481 WalRcvComputeNextWakeup(i, now);
482
483 /* Send initial reply/feedback messages. */
484 XLogWalRcvSendReply(true, false);
485 XLogWalRcvSendHSFeedback(true);
486
487 /* Loop until end-of-streaming or error */
488 for (;;)
489 {
490 char *buf;
491 int len;
492 bool endofwal = false;
493 pgsocket wait_fd = PGINVALID_SOCKET;
494 int rc;
495 TimestampTz nextWakeup;
496 long nap;
497
498 /*
499 * Exit walreceiver if we're not in recovery. This should not
500 * happen, but cross-check the status here.
501 */
502 if (!RecoveryInProgress())
503 ereport(FATAL,
504 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
505 errmsg("cannot continue WAL streaming, recovery has already ended")));
506
507 /* Process any requests or signals received recently */
508 CHECK_FOR_INTERRUPTS();
509
510 if (ConfigReloadPending)
511 {
512 ConfigReloadPending = false;
513 ProcessConfigFile(PGC_SIGHUP);
514 /* recompute wakeup times */
515 now = GetCurrentTimestamp();
516 for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
517 WalRcvComputeNextWakeup(i, now);
518 XLogWalRcvSendHSFeedback(true);
519 }
520
521 /* See if we can read data immediately */
522 len = walrcv_receive(wrconn, &buf, &wait_fd);
523 if (len != 0)
524 {
525 /*
526 * Process the received data, and any subsequent data we
527 * can read without blocking.
528 */
529 for (;;)
530 {
531 if (len > 0)
532 {
533 /*
534 * Something was received from primary, so adjust
535 * the ping and terminate wakeup times.
536 */
537 now = GetCurrentTimestamp();
538 WalRcvComputeNextWakeup(WALRCV_WAKEUP_TERMINATE,
539 now);
540 WalRcvComputeNextWakeup(WALRCV_WAKEUP_PING, now);
541 XLogWalRcvProcessMsg(buf[0], &buf[1], len - 1,
542 startpointTLI);
543 }
544 else if (len == 0)
545 break;
546 else if (len < 0)
547 {
548 ereport(LOG,
549 (errmsg("replication terminated by primary server"),
550 errdetail("End of WAL reached on timeline %u at %X/%X.",
551 startpointTLI,
552 LSN_FORMAT_ARGS(LogstreamResult.Write))));
553 endofwal = true;
554 break;
555 }
556 len = walrcv_receive(wrconn, &buf, &wait_fd);
557 }
558
559 /* Let the primary know that we received some data. */
560 XLogWalRcvSendReply(false, false);
561
562 /*
563 * If we've written some records, flush them to disk and
564 * let the startup process and primary server know about
565 * them.
566 */
567 XLogWalRcvFlush(false, startpointTLI);
568 }
569
570 /* Check if we need to exit the streaming loop. */
571 if (endofwal)
572 break;
573
574 /* Find the soonest wakeup time, to limit our nap. */
575 nextWakeup = TIMESTAMP_INFINITY;
576 for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
577 nextWakeup = Min(wakeup[i], nextWakeup);
578
579 /* Calculate the nap time, clamping as necessary. */
580 now = GetCurrentTimestamp();
581 nap = TimestampDifferenceMilliseconds(now, nextWakeup);
582
583 /*
584 * Ideally we would reuse a WaitEventSet object repeatedly
585 * here to avoid the overheads of WaitLatchOrSocket on epoll
586 * systems, but we can't be sure that libpq (or any other
587 * walreceiver implementation) has the same socket (even if
588 * the fd is the same number, it may have been closed and
589 * reopened since the last time). In future, if there is a
590 * function for removing sockets from WaitEventSet, then we
591 * could add and remove just the socket each time, potentially
592 * avoiding some system calls.
593 */
594 Assert(wait_fd != PGINVALID_SOCKET);
595 rc = WaitLatchOrSocket(MyLatch,
596 WL_EXIT_ON_PM_DEATH | WL_SOCKET_READABLE |
597 WL_TIMEOUT | WL_LATCH_SET,
598 wait_fd,
599 nap,
600 WAIT_EVENT_WAL_RECEIVER_MAIN);
601 if (rc & WL_LATCH_SET)
602 {
603 ResetLatch(MyLatch);
604 CHECK_FOR_INTERRUPTS();
605
606 if (walrcv->force_reply)
607 {
608 /*
609 * The recovery process has asked us to send apply
610 * feedback now. Make sure the flag is really set to
611 * false in shared memory before sending the reply, so
612 * we don't miss a new request for a reply.
613 */
614 walrcv->force_reply = false;
615 pg_memory_barrier();
616 XLogWalRcvSendReply(true, false);
617 }
618 }
619 if (rc & WL_TIMEOUT)
620 {
621 /*
622 * We didn't receive anything new. If we haven't heard
623 * anything from the server for more than
624 * wal_receiver_timeout / 2, ping the server. Also, if
625 * it's been longer than wal_receiver_status_interval
626 * since the last update we sent, send a status update to
627 * the primary anyway, to report any progress in applying
628 * WAL.
629 */
630 bool requestReply = false;
631
632 /*
633 * Report pending statistics to the cumulative stats
634 * system. This location is useful for the report as it
635 * is not within a tight loop in the WAL receiver, to
636 * avoid bloating pgstats with requests, while also making
637 * sure that the reports happen each time a status update
638 * is sent.
639 */
640 pgstat_report_wal(false);
641
642 /*
643 * Check if time since last receive from primary has
644 * reached the configured limit.
645 */
646 now = GetCurrentTimestamp();
647 if (now >= wakeup[WALRCV_WAKEUP_TERMINATE])
648 ereport(ERROR,
649 (errcode(ERRCODE_CONNECTION_FAILURE),
650 errmsg("terminating walreceiver due to timeout")));
651
652 /*
653 * If we didn't receive anything new for half of receiver
654 * replication timeout, then ping the server.
655 */
656 if (now >= wakeup[WALRCV_WAKEUP_PING])
657 {
658 requestReply = true;
659 wakeup[WALRCV_WAKEUP_PING] = TIMESTAMP_INFINITY;
660 }
661
662 XLogWalRcvSendReply(requestReply, requestReply);
663 XLogWalRcvSendHSFeedback(false);
664 }
665 }
666
667 /*
668 * The backend finished streaming. Exit streaming COPY-mode from
669 * our side, too.
670 */
671 walrcv_endstreaming(wrconn, &primaryTLI);
672
673 /*
674 * If the server had switched to a new timeline that we didn't
675 * know about when we began streaming, fetch its timeline history
676 * file now.
677 */
678 WalRcvFetchTimeLineHistoryFiles(startpointTLI, primaryTLI);
679 }
680 else
681 ereport(LOG,
682 (errmsg("primary server contains no more WAL on requested timeline %u",
683 startpointTLI)));
684
685 /*
686 * End of WAL reached on the requested timeline. Close the last
687 * segment, and await for new orders from the startup process.
688 */
689 if (recvFile >= 0)
690 {
691 char xlogfname[MAXFNAMELEN];
692
693 XLogWalRcvFlush(false, startpointTLI);
694 XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
695 if (close(recvFile) != 0)
696 ereport(PANIC,
697 (errcode_for_file_access(),
698 errmsg("could not close WAL segment %s: %m",
699 xlogfname)));
700
701 /*
702 * Create .done file forcibly to prevent the streamed segment from
703 * being archived later.
704 */
705 if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS)
706 XLogArchiveForceDone(xlogfname);
707 else
708 XLogArchiveNotify(xlogfname);
709 }
710 recvFile = -1;
711
712 elog(DEBUG1, "walreceiver ended streaming and awaits new instructions");
713 WalRcvWaitForStartPosition(&startpoint, &startpointTLI);
714 }
715 /* not reached */
716 }
717
718 /*
719 * Wait for startup process to set receiveStart and receiveStartTLI.
720 */
721 static void
722 WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
723 {
724 WalRcvData *walrcv = WalRcv;
725 int state;
726
727 SpinLockAcquire(&walrcv->mutex);
728 state = walrcv->walRcvState;
729 if (state != WALRCV_STREAMING)
730 {
731 SpinLockRelease(&walrcv->mutex);
732 if (state == WALRCV_STOPPING)
733 proc_exit(0);
734 else
735 elog(FATAL, "unexpected walreceiver state");
736 }
737 walrcv->walRcvState = WALRCV_WAITING;
738 walrcv->receiveStart = InvalidXLogRecPtr;
739 walrcv->receiveStartTLI = 0;
740 SpinLockRelease(&walrcv->mutex);
741
742 set_ps_display("idle");
743
744 /*
745 * nudge startup process to notice that we've stopped streaming and are
746 * now waiting for instructions.
747 */
748 WakeupRecovery();
749 for (;;)
750 {
751 ResetLatch(MyLatch);
752
753 CHECK_FOR_INTERRUPTS();
754
755 SpinLockAcquire(&walrcv->mutex);
756 Assert(walrcv->walRcvState == WALRCV_RESTARTING ||
757 walrcv->walRcvState == WALRCV_WAITING ||
758 walrcv->walRcvState == WALRCV_STOPPING);
759 if (walrcv->walRcvState == WALRCV_RESTARTING)
760 {
761 /*
762 * No need to handle changes in primary_conninfo or
763 * primary_slot_name here. Startup process will signal us to
764 * terminate in case those change.
765 */
766 *startpoint = walrcv->receiveStart;
767 *startpointTLI = walrcv->receiveStartTLI;
768 walrcv->walRcvState = WALRCV_STREAMING;
769 SpinLockRelease(&walrcv->mutex);
770 break;
771 }
772 if (walrcv->walRcvState == WALRCV_STOPPING)
773 {
774 /*
775 * We should've received SIGTERM if the startup process wants us
776 * to die, but might as well check it here too.
777 */
778 SpinLockRelease(&walrcv->mutex);
779 exit(1);
780 }
781 SpinLockRelease(&walrcv->mutex);
782
783 (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
784 WAIT_EVENT_WAL_RECEIVER_WAIT_START);
785 }
786
787 if (update_process_title)
788 {
789 char activitymsg[50];
790
791 snprintf(activitymsg, sizeof(activitymsg), "restarting at %X/%X",
792 LSN_FORMAT_ARGS(*startpoint));
793 set_ps_display(activitymsg);
794 }
795 }
796
797 /*
798 * Fetch any missing timeline history files between 'first' and 'last'
799 * (inclusive) from the server.
800 */
801 static void
802 WalRcvFetchTimeLineHistoryFiles(TimeLineID first, TimeLineID last)
803 {
804 TimeLineID tli;
805
806 for (tli = first; tli <= last; tli++)
807 {
808 /* there's no history file for timeline 1 */
809 if (tli != 1 && !existsTimeLineHistory(tli))
810 {
811 char *fname;
812 char *content;
813 int len;
814 char expectedfname[MAXFNAMELEN];
815
816 ereport(LOG,
817 (errmsg("fetching timeline history file for timeline %u from primary server",
818 tli)));
819
820 walrcv_readtimelinehistoryfile(wrconn, tli, &fname, &content, &len);
821
822 /*
823 * Check that the filename on the primary matches what we
824 * calculated ourselves. This is just a sanity check, it should
825 * always match.
826 */
827 TLHistoryFileName(expectedfname, tli);
828 if (strcmp(fname, expectedfname) != 0)
829 ereport(ERROR,
830 (errcode(ERRCODE_PROTOCOL_VIOLATION),
831 errmsg_internal("primary reported unexpected file name for timeline history file of timeline %u",
832 tli)));
833
834 /*
835 * Write the file to pg_wal.
836 */
837 writeTimeLineHistoryFile(tli, content, len);
838
839 /*
840 * Mark the streamed history file as ready for archiving if
841 * archive_mode is always.
842 */
843 if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS)
844 XLogArchiveForceDone(fname);
845 else
846 XLogArchiveNotify(fname);
847
848 pfree(fname);
849 pfree(content);
850 }
851 }
852 }
853
854 /*
855 * Mark us as STOPPED in shared memory at exit.
856 */
857 static void
858 WalRcvDie(int code, Datum arg)
859 {
860 WalRcvData *walrcv = WalRcv;
861 TimeLineID *startpointTLI_p = (TimeLineID *) DatumGetPointer(arg);
862
863 Assert(*startpointTLI_p != 0);
864
865 /* Ensure that all WAL records received are flushed to disk */
866 XLogWalRcvFlush(true, *startpointTLI_p);
867
868 /* Mark ourselves inactive in shared memory */
869 SpinLockAcquire(&walrcv->mutex);
870 Assert(walrcv->walRcvState == WALRCV_STREAMING ||
871 walrcv->walRcvState == WALRCV_RESTARTING ||
872 walrcv->walRcvState == WALRCV_STARTING ||
873 walrcv->walRcvState == WALRCV_WAITING ||
874 walrcv->walRcvState == WALRCV_STOPPING);
875 Assert(walrcv->pid == MyProcPid);
876 walrcv->walRcvState = WALRCV_STOPPED;
877 walrcv->pid = 0;
878 walrcv->procno = INVALID_PROC_NUMBER;
879 walrcv->ready_to_display = false;
880 SpinLockRelease(&walrcv->mutex);
881
882 ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
883
884 /* Terminate the connection gracefully. */
885 if (wrconn != NULL)
886 walrcv_disconnect(wrconn);
887
888 /* Wake up the startup process to notice promptly that we're gone */
889 WakeupRecovery();
890 }
891
892 /*
893 * Accept the message from XLOG stream, and process it.
894 */
895 static void
896 XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len, TimeLineID tli)
897 {
898 int hdrlen;
899 XLogRecPtr dataStart;
900 XLogRecPtr walEnd;
901 TimestampTz sendTime;
902 bool replyRequested;
903
904 switch (type)
905 {
906 case 'w': /* WAL records */
907 {
908 StringInfoData incoming_message;
909
910 hdrlen = sizeof(int64) + sizeof(int64) + sizeof(int64);
911 if (len < hdrlen)
912 ereport(ERROR,
913 (errcode(ERRCODE_PROTOCOL_VIOLATION),
914 errmsg_internal("invalid WAL message received from primary")));
915
916 /* initialize a StringInfo with the given buffer */
917 initReadOnlyStringInfo(&incoming_message, buf, hdrlen);
918
919 /* read the fields */
920 dataStart = pq_getmsgint64(&incoming_message);
921 walEnd = pq_getmsgint64(&incoming_message);
922 sendTime = pq_getmsgint64(&incoming_message);
923 ProcessWalSndrMessage(walEnd, sendTime);
924
925 buf += hdrlen;
926 len -= hdrlen;
927 XLogWalRcvWrite(buf, len, dataStart, tli);
928 break;
929 }
930 case 'k': /* Keepalive */
931 {
932 StringInfoData incoming_message;
933
934 hdrlen = sizeof(int64) + sizeof(int64) + sizeof(char);
935 if (len != hdrlen)
936 ereport(ERROR,
937 (errcode(ERRCODE_PROTOCOL_VIOLATION),
938 errmsg_internal("invalid keepalive message received from primary")));
939
940 /* initialize a StringInfo with the given buffer */
941 initReadOnlyStringInfo(&incoming_message, buf, hdrlen);
942
943 /* read the fields */
944 walEnd = pq_getmsgint64(&incoming_message);
945 sendTime = pq_getmsgint64(&incoming_message);
946 replyRequested = pq_getmsgbyte(&incoming_message);
947
948 ProcessWalSndrMessage(walEnd, sendTime);
949
950 /* If the primary requested a reply, send one immediately */
951 if (replyRequested)
952 XLogWalRcvSendReply(true, false);
953 break;
954 }
955 default:
956 ereport(ERROR,
957 (errcode(ERRCODE_PROTOCOL_VIOLATION),
958 errmsg_internal("invalid replication message type %d",
959 type)));
960 }
961 }
962
963 /*
964 * Write XLOG data to disk.
965 */
966 static void
967 XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr, TimeLineID tli)
968 {
969 int startoff;
970 int byteswritten;
971 instr_time start;
972
973 Assert(tli != 0);
974
975 while (nbytes > 0)
976 {
977 int segbytes;
978
979 /* Close the current segment if it's completed */
980 if (recvFile >= 0 && !XLByteInSeg(recptr, recvSegNo, wal_segment_size))
981 XLogWalRcvClose(recptr, tli);
982
983 if (recvFile < 0)
984 {
985 /* Create/use new log file */
986 XLByteToSeg(recptr, recvSegNo, wal_segment_size);
987 recvFile = XLogFileInit(recvSegNo, tli);
988 recvFileTLI = tli;
989 }
990
991 /* Calculate the start offset of the received logs */
992 startoff = XLogSegmentOffset(recptr, wal_segment_size);
993
994 if (startoff + nbytes > wal_segment_size)
995 segbytes = wal_segment_size - startoff;
996 else
997 segbytes = nbytes;
998
999 /* OK to write the logs */
1000 errno = 0;
1001
1002 /*
1003 * Measure I/O timing to write WAL data, for pg_stat_io.
1004 */
1005 start = pgstat_prepare_io_time(track_wal_io_timing);
1006
1007 pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
1008 byteswritten = pg_pwrite(recvFile, buf, segbytes, (off_t) startoff);
1009 pgstat_report_wait_end();
1010
1011 if (byteswritten <= 0)
1012 {
1013 char xlogfname[MAXFNAMELEN];
1014 int save_errno;
1015
1016 /* if write didn't set errno, assume no disk space */
1017 if (errno == 0)
1018 errno = ENOSPC;
1019
1020 save_errno = errno;
1021 XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
1022 errno = save_errno;
1023 ereport(PANIC,
1024 (errcode_for_file_access(),
1025 errmsg("could not write to WAL segment %s "
1026 "at offset %d, length %lu: %m",
1027 xlogfname, startoff, (unsigned long) segbytes)));
1028 }
1029
1030 pgstat_count_io_op_time(IOOBJECT_WAL, IOCONTEXT_NORMAL,
1031 IOOP_WRITE, start, 1, byteswritten);
1032
1033 /* Update state for write */
1034 recptr += byteswritten;
1035
1036 nbytes -= byteswritten;
1037 buf += byteswritten;
1038
1039 LogstreamResult.Write = recptr;
1040 }
1041
1042 /* Update shared-memory status */
1043 pg_atomic_write_u64(&WalRcv->writtenUpto, LogstreamResult.Write);
1044
1045 /*
1046 * Close the current segment if it's fully written up in the last cycle of
1047 * the loop, to create its archive notification file soon. Otherwise WAL
1048 * archiving of the segment will be delayed until any data in the next
1049 * segment is received and written.
1050 */
1051 if (recvFile >= 0 && !XLByteInSeg(recptr, recvSegNo, wal_segment_size))
1052 XLogWalRcvClose(recptr, tli);
1053 }
1054
1055 /*
1056 * Flush the log to disk.
1057 *
1058 * If we're in the midst of dying, it's unwise to do anything that might throw
1059 * an error, so we skip sending a reply in that case.
1060 */
1061 static void
1062 XLogWalRcvFlush(bool dying, TimeLineID tli)
1063 {
1064 Assert(tli != 0);
1065
1066 if (LogstreamResult.Flush < LogstreamResult.Write)
1067 {
1068 WalRcvData *walrcv = WalRcv;
1069
1070 issue_xlog_fsync(recvFile, recvSegNo, tli);
1071
1072 LogstreamResult.Flush = LogstreamResult.Write;
1073
1074 /* Update shared-memory status */
1075 SpinLockAcquire(&walrcv->mutex);
1076 if (walrcv->flushedUpto < LogstreamResult.Flush)
1077 {
1078 walrcv->latestChunkStart = walrcv->flushedUpto;
1079 walrcv->flushedUpto = LogstreamResult.Flush;
1080 walrcv->receivedTLI = tli;
1081 }
1082 SpinLockRelease(&walrcv->mutex);
1083
1084 /* Signal the startup process and walsender that new WAL has arrived */
1085 WakeupRecovery();
1086 if (AllowCascadeReplication())
1087 WalSndWakeup(true, false);
1088
1089 /* Report XLOG streaming progress in PS display */
1090 if (update_process_title)
1091 {
1092 char activitymsg[50];
1093
1094 snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
1095 LSN_FORMAT_ARGS(LogstreamResult.Write));
1096 set_ps_display(activitymsg);
1097 }
1098
1099 /* Also let the primary know that we made some progress */
1100 if (!dying)
1101 {
1102 XLogWalRcvSendReply(false, false);
1103 XLogWalRcvSendHSFeedback(false);
1104 }
1105 }
1106 }
1107
1108 /*
1109 * Close the current segment.
1110 *
1111 * Flush the segment to disk before closing it. Otherwise we have to
1112 * reopen and fsync it later.
1113 *
1114 * Create an archive notification file since the segment is known completed.
1115 */
1116 static void
1117 XLogWalRcvClose(XLogRecPtr recptr, TimeLineID tli)
1118 {
1119 char xlogfname[MAXFNAMELEN];
1120
1121 Assert(recvFile >= 0 && !XLByteInSeg(recptr, recvSegNo, wal_segment_size));
1122 Assert(tli != 0);
1123
1124 /*
1125 * fsync() and close current file before we switch to next one. We would
1126 * otherwise have to reopen this file to fsync it later
1127 */
1128 XLogWalRcvFlush(false, tli);
1129
1130 XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
1131
1132 /*
1133 * XLOG segment files will be re-read by recovery in startup process soon,
1134 * so we don't advise the OS to release cache pages associated with the
1135 * file like XLogFileClose() does.
1136 */
1137 if (close(recvFile) != 0)
1138 ereport(PANIC,
1139 (errcode_for_file_access(),
1140 errmsg("could not close WAL segment %s: %m",
1141 xlogfname)));
1142
1143 /*
1144 * Create .done file forcibly to prevent the streamed segment from being
1145 * archived later.
1146 */
1147 if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS)
1148 XLogArchiveForceDone(xlogfname);
1149 else
1150 XLogArchiveNotify(xlogfname);
1151
1152 recvFile = -1;
1153 }
1154
1155 /*
1156 * Send reply message to primary, indicating our current WAL locations, oldest
1157 * xmin and the current time.
1158 *
1159 * If 'force' is not set, the message is only sent if enough time has
1160 * passed since last status update to reach wal_receiver_status_interval.
1161 * If wal_receiver_status_interval is disabled altogether and 'force' is
1162 * false, this is a no-op.
1163 *
1164 * If 'requestReply' is true, requests the server to reply immediately upon
1165 * receiving this message. This is used for heartbeats, when approaching
1166 * wal_receiver_timeout.
1167 */
1168 static void
1169 XLogWalRcvSendReply(bool force, bool requestReply)
1170 {
1171 static XLogRecPtr writePtr = 0;
1172 static XLogRecPtr flushPtr = 0;
1173 XLogRecPtr applyPtr;
1174 TimestampTz now;
1175
1176 /*
1177 * If the user doesn't want status to be reported to the primary, be sure
1178 * to exit before doing anything at all.
1179 */
1180 if (!force && wal_receiver_status_interval <= 0)
1181 return;
1182
1183 /* Get current timestamp. */
1184 now = GetCurrentTimestamp();
1185
1186 /*
1187 * We can compare the write and flush positions to the last message we
1188 * sent without taking any lock, but the apply position requires a spin
1189 * lock, so we don't check that unless something else has changed or 10
1190 * seconds have passed. This means that the apply WAL location will
1191 * appear, from the primary's point of view, to lag slightly, but since
1192 * this is only for reporting purposes and only on idle systems, that's
1193 * probably OK.
1194 */
1195 if (!force
1196 && writePtr == LogstreamResult.Write
1197 && flushPtr == LogstreamResult.Flush
1198 && now < wakeup[WALRCV_WAKEUP_REPLY])
1199 return;
1200
1201 /* Make sure we wake up when it's time to send another reply. */
1202 WalRcvComputeNextWakeup(WALRCV_WAKEUP_REPLY, now);
1203
1204 /* Construct a new message */
1205 writePtr = LogstreamResult.Write;
1206 flushPtr = LogstreamResult.Flush;
1207 applyPtr = GetXLogReplayRecPtr(NULL);
1208
1209 resetStringInfo(&reply_message);
1210 pq_sendbyte(&reply_message, 'r');
1211 pq_sendint64(&reply_message, writePtr);
1212 pq_sendint64(&reply_message, flushPtr);
1213 pq_sendint64(&reply_message, applyPtr);
1214 pq_sendint64(&reply_message, GetCurrentTimestamp());
1215 pq_sendbyte(&reply_message, requestReply ? 1 : 0);
1216
1217 /* Send it */
1218 elog(DEBUG2, "sending write %X/%X flush %X/%X apply %X/%X%s",
1219 LSN_FORMAT_ARGS(writePtr),
1220 LSN_FORMAT_ARGS(flushPtr),
1221 LSN_FORMAT_ARGS(applyPtr),
1222 requestReply ? " (reply requested)" : "");
1223
1224 walrcv_send(wrconn, reply_message.data, reply_message.len);
1225 }
1226
1227 /*
1228 * Send hot standby feedback message to primary, plus the current time,
1229 * in case they don't have a watch.
1230 *
1231 * If the user disables feedback, send one final message to tell sender
1232 * to forget about the xmin on this standby. We also send this message
1233 * on first connect because a previous connection might have set xmin
1234 * on a replication slot. (If we're not using a slot it's harmless to
1235 * send a feedback message explicitly setting InvalidTransactionId).
1236 */
1237 static void
1238 XLogWalRcvSendHSFeedback(bool immed)
1239 {
1240 TimestampTz now;
1241 FullTransactionId nextFullXid;
1242 TransactionId nextXid;
1243 uint32 xmin_epoch,
1244 catalog_xmin_epoch;
1245 TransactionId xmin,
1246 catalog_xmin;
1247
1248 /* initially true so we always send at least one feedback message */
1249 static bool primary_has_standby_xmin = true;
1250
1251 /*
1252 * If the user doesn't want status to be reported to the primary, be sure
1253 * to exit before doing anything at all.
1254 */
1255 if ((wal_receiver_status_interval <= 0 || !hot_standby_feedback) &&
1256 !primary_has_standby_xmin)
1257 return;
1258
1259 /* Get current timestamp. */
1260 now = GetCurrentTimestamp();
1261
1262 /* Send feedback at most once per wal_receiver_status_interval. */
1263 if (!immed && now < wakeup[WALRCV_WAKEUP_HSFEEDBACK])
1264 return;
1265
1266 /* Make sure we wake up when it's time to send feedback again. */
1267 WalRcvComputeNextWakeup(WALRCV_WAKEUP_HSFEEDBACK, now);
1268
1269 /*
1270 * If Hot Standby is not yet accepting connections there is nothing to
1271 * send. Check this after the interval has expired to reduce number of
1272 * calls.
1273 *
1274 * Bailing out here also ensures that we don't send feedback until we've
1275 * read our own replication slot state, so we don't tell the primary to
1276 * discard needed xmin or catalog_xmin from any slots that may exist on
1277 * this replica.
1278 */
1279 if (!HotStandbyActive())
1280 return;
1281
1282 /*
1283 * Make the expensive call to get the oldest xmin once we are certain
1284 * everything else has been checked.
1285 */
1286 if (hot_standby_feedback)
1287 {
1288 GetReplicationHorizons(&xmin, &catalog_xmin);
1289 }
1290 else
1291 {
1292 xmin = InvalidTransactionId;
1293 catalog_xmin = InvalidTransactionId;
1294 }
1295
1296 /*
1297 * Get epoch and adjust if nextXid and oldestXmin are different sides of
1298 * the epoch boundary.
1299 */
1300 nextFullXid = ReadNextFullTransactionId();
1301 nextXid = XidFromFullTransactionId(nextFullXid);
1302 xmin_epoch = EpochFromFullTransactionId(nextFullXid);
1303 catalog_xmin_epoch = xmin_epoch;
1304 if (nextXid < xmin)
1305 xmin_epoch--;
1306 if (nextXid < catalog_xmin)
1307 catalog_xmin_epoch--;
1308
1309 elog(DEBUG2, "sending hot standby feedback xmin %u epoch %u catalog_xmin %u catalog_xmin_epoch %u",
1310 xmin, xmin_epoch, catalog_xmin, catalog_xmin_epoch);
1311
1312 /* Construct the message and send it. */
1313 resetStringInfo(&reply_message);
1314 pq_sendbyte(&reply_message, 'h');
1315 pq_sendint64(&reply_message, GetCurrentTimestamp());
1316 pq_sendint32(&reply_message, xmin);
1317 pq_sendint32(&reply_message, xmin_epoch);
1318 pq_sendint32(&reply_message, catalog_xmin);
1319 pq_sendint32(&reply_message, catalog_xmin_epoch);
1320 walrcv_send(wrconn, reply_message.data, reply_message.len);
1321 if (TransactionIdIsValid(xmin) || TransactionIdIsValid(catalog_xmin))
1322 primary_has_standby_xmin = true;
1323 else
1324 primary_has_standby_xmin = false;
1325 }
1326
1327 /*
1328 * Update shared memory status upon receiving a message from primary.
1329 *
1330 * 'walEnd' and 'sendTime' are the end-of-WAL and timestamp of the latest
1331 * message, reported by primary.
1332 */
1333 static void
1334 ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime)
1335 {
1336 WalRcvData *walrcv = WalRcv;
1337 TimestampTz lastMsgReceiptTime = GetCurrentTimestamp();
1338
1339 /* Update shared-memory status */
1340 SpinLockAcquire(&walrcv->mutex);
1341 if (walrcv->latestWalEnd < walEnd)
1342 walrcv->latestWalEndTime = sendTime;
1343 walrcv->latestWalEnd = walEnd;
1344 walrcv->lastMsgSendTime = sendTime;
1345 walrcv->lastMsgReceiptTime = lastMsgReceiptTime;
1346 SpinLockRelease(&walrcv->mutex);
1347
1348 if (message_level_is_interesting(DEBUG2))
1349 {
1350 char *sendtime;
1351 char *receipttime;
1352 int applyDelay;
1353
1354 /* Copy because timestamptz_to_str returns a static buffer */
1355 sendtime = pstrdup(timestamptz_to_str(sendTime));
1356 receipttime = pstrdup(timestamptz_to_str(lastMsgReceiptTime));
1357 applyDelay = GetReplicationApplyDelay();
1358
1359 /* apply delay is not available */
1360 if (applyDelay == -1)
1361 elog(DEBUG2, "sendtime %s receipttime %s replication apply delay (N/A) transfer latency %d ms",
1362 sendtime,
1363 receipttime,
1364 GetReplicationTransferLatency());
1365 else
1366 elog(DEBUG2, "sendtime %s receipttime %s replication apply delay %d ms transfer latency %d ms",
1367 sendtime,
1368 receipttime,
1369 applyDelay,
1370 GetReplicationTransferLatency());
1371
1372 pfree(sendtime);
1373 pfree(receipttime);
1374 }
1375 }
1376
1377 /*
1378 * Compute the next wakeup time for a given wakeup reason. Can be called to
1379 * initialize a wakeup time, to adjust it for the next wakeup, or to
1380 * reinitialize it when GUCs have changed. We ask the caller to pass in the
1381 * value of "now" because this frequently avoids multiple calls of
1382 * GetCurrentTimestamp(). It had better be a reasonably up-to-date value
1383 * though.
1384 */
1385 static void
1386 WalRcvComputeNextWakeup(WalRcvWakeupReason reason, TimestampTz now)
1387 {
1388 switch (reason)
1389 {
1390 case WALRCV_WAKEUP_TERMINATE:
1391 if (wal_receiver_timeout <= 0)
1392 wakeup[reason] = TIMESTAMP_INFINITY;
1393 else
1394 wakeup[reason] = TimestampTzPlusMilliseconds(now, wal_receiver_timeout);
1395 break;
1396 case WALRCV_WAKEUP_PING:
1397 if (wal_receiver_timeout <= 0)
1398 wakeup[reason] = TIMESTAMP_INFINITY;
1399 else
1400 wakeup[reason] = TimestampTzPlusMilliseconds(now, wal_receiver_timeout / 2);
1401 break;
1402 case WALRCV_WAKEUP_HSFEEDBACK:
1403 if (!hot_standby_feedback || wal_receiver_status_interval <= 0)
1404 wakeup[reason] = TIMESTAMP_INFINITY;
1405 else
1406 wakeup[reason] = TimestampTzPlusSeconds(now, wal_receiver_status_interval);
1407 break;
1408 case WALRCV_WAKEUP_REPLY:
1409 if (wal_receiver_status_interval <= 0)
1410 wakeup[reason] = TIMESTAMP_INFINITY;
1411 else
1412 wakeup[reason] = TimestampTzPlusSeconds(now, wal_receiver_status_interval);
1413 break;
1414 /* there's intentionally no default: here */
1415 }
1416 }
1417
1418 /*
1419 * Wake up the walreceiver main loop.
1420 *
1421 * This is called by the startup process whenever interesting xlog records
1422 * are applied, so that walreceiver can check if it needs to send an apply
1423 * notification back to the primary which may be waiting in a COMMIT with
1424 * synchronous_commit = remote_apply.
1425 */
1426 void
1427 WalRcvForceReply(void)
1428 {
1429 ProcNumber procno;
1430
1431 WalRcv->force_reply = true;
1432 /* fetching the proc number is probably atomic, but don't rely on it */
1433 SpinLockAcquire(&WalRcv->mutex);
1434 procno = WalRcv->procno;
1435 SpinLockRelease(&WalRcv->mutex);
1436 if (procno != INVALID_PROC_NUMBER)
1437 SetLatch(&GetPGProcByNumber(procno)->procLatch);
1438 }
1439
1440 /*
1441 * Return a string constant representing the state. This is used
1442 * in system functions and views, and should *not* be translated.
1443 */
1444 static const char *
1445 WalRcvGetStateString(WalRcvState state)
1446 {
1447 switch (state)
1448 {
1449 case WALRCV_STOPPED:
1450 return "stopped";
1451 case WALRCV_STARTING:
1452 return "starting";
1453 case WALRCV_STREAMING:
1454 return "streaming";
1455 case WALRCV_WAITING:
1456 return "waiting";
1457 case WALRCV_RESTARTING:
1458 return "restarting";
1459 case WALRCV_STOPPING:
1460 return "stopping";
1461 }
1462 return "UNKNOWN";
1463 }
1464
1465 /*
1466 * Returns activity of WAL receiver, including pid, state and xlog locations
1467 * received from the WAL sender of another server.
1468 */
1469 Datum
1470 pg_stat_get_wal_receiver(PG_FUNCTION_ARGS)
1471 {
1472 TupleDesc tupdesc;
1473 Datum *values;
1474 bool *nulls;
1475 int pid;
1476 bool ready_to_display;
1477 WalRcvState state;
1478 XLogRecPtr receive_start_lsn;
1479 TimeLineID receive_start_tli;
1480 XLogRecPtr written_lsn;
1481 XLogRecPtr flushed_lsn;
1482 TimeLineID received_tli;
1483 TimestampTz last_send_time;
1484 TimestampTz last_receipt_time;
1485 XLogRecPtr latest_end_lsn;
1486 TimestampTz latest_end_time;
1487 char sender_host[NI_MAXHOST];
1488 int sender_port = 0;
1489 char slotname[NAMEDATALEN];
1490 char conninfo[MAXCONNINFO];
1491
1492 /* Take a lock to ensure value consistency */
1493 SpinLockAcquire(&WalRcv->mutex);
1494 pid = (int) WalRcv->pid;
1495 ready_to_display = WalRcv->ready_to_display;
1496 state = WalRcv->walRcvState;
1497 receive_start_lsn = WalRcv->receiveStart;
1498 receive_start_tli = WalRcv->receiveStartTLI;
1499 flushed_lsn = WalRcv->flushedUpto;
1500 received_tli = WalRcv->receivedTLI;
1501 last_send_time = WalRcv->lastMsgSendTime;
1502 last_receipt_time = WalRcv->lastMsgReceiptTime;
1503 latest_end_lsn = WalRcv->latestWalEnd;
1504 latest_end_time = WalRcv->latestWalEndTime;
1505 strlcpy(slotname, WalRcv->slotname, sizeof(slotname));
1506 strlcpy(sender_host, WalRcv->sender_host, sizeof(sender_host));
1507 sender_port = WalRcv->sender_port;
1508 strlcpy(conninfo, WalRcv->conninfo, sizeof(conninfo));
1509 SpinLockRelease(&WalRcv->mutex);
1510
1511 /*
1512 * No WAL receiver (or not ready yet), just return a tuple with NULL
1513 * values
1514 */
1515 if (pid == 0 || !ready_to_display)
1516 PG_RETURN_NULL();
1517
1518 /*
1519 * Read "writtenUpto" without holding a spinlock. Note that it may not be
1520 * consistent with the other shared variables of the WAL receiver
1521 * protected by a spinlock, but this should not be used for data integrity
1522 * checks.
1523 */
1524 written_lsn = pg_atomic_read_u64(&WalRcv->writtenUpto);
1525
1526 /* determine result type */
1527 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1528 elog(ERROR, "return type must be a row type");
1529
1530 values = palloc0(sizeof(Datum) * tupdesc->natts);
1531 nulls = palloc0(sizeof(bool) * tupdesc->natts);
1532
1533 /* Fetch values */
1534 values[0] = Int32GetDatum(pid);
1535
1536 if (!has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS))
1537 {
1538 /*
1539 * Only superusers and roles with privileges of pg_read_all_stats can
1540 * see details. Other users only get the pid value to know whether it
1541 * is a WAL receiver, but no details.
1542 */
1543 memset(&nulls[1], true, sizeof(bool) * (tupdesc->natts - 1));
1544 }
1545 else
1546 {
1547 values[1] = CStringGetTextDatum(WalRcvGetStateString(state));
1548
1549 if (XLogRecPtrIsInvalid(receive_start_lsn))
1550 nulls[2] = true;
1551 else
1552 values[2] = LSNGetDatum(receive_start_lsn);
1553 values[3] = Int32GetDatum(receive_start_tli);
1554 if (XLogRecPtrIsInvalid(written_lsn))
1555 nulls[4] = true;
1556 else
1557 values[4] = LSNGetDatum(written_lsn);
1558 if (XLogRecPtrIsInvalid(flushed_lsn))
1559 nulls[5] = true;
1560 else
1561 values[5] = LSNGetDatum(flushed_lsn);
1562 values[6] = Int32GetDatum(received_tli);
1563 if (last_send_time == 0)
1564 nulls[7] = true;
1565 else
1566 values[7] = TimestampTzGetDatum(last_send_time);
1567 if (last_receipt_time == 0)
1568 nulls[8] = true;
1569 else
1570 values[8] = TimestampTzGetDatum(last_receipt_time);
1571 if (XLogRecPtrIsInvalid(latest_end_lsn))
1572 nulls[9] = true;
1573 else
1574 values[9] = LSNGetDatum(latest_end_lsn);
1575 if (latest_end_time == 0)
1576 nulls[10] = true;
1577 else
1578 values[10] = TimestampTzGetDatum(latest_end_time);
1579 if (*slotname == '\0')
1580 nulls[11] = true;
1581 else
1582 values[11] = CStringGetTextDatum(slotname);
1583 if (*sender_host == '\0')
1584 nulls[12] = true;
1585 else
1586 values[12] = CStringGetTextDatum(sender_host);
1587 if (sender_port == 0)
1588 nulls[13] = true;
1589 else
1590 values[13] = Int32GetDatum(sender_port);
1591 if (*conninfo == '\0')
1592 nulls[14] = true;
1593 else
1594 values[14] = CStringGetTextDatum(conninfo);
1595 }
1596
1597 /* Returns the record as Datum */
1598 PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
1599 }