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